@@ -701,6 +701,41 @@ abstract class Expression implements PipelineSerializable {
701701 return _ArrayIndexOfAllExpression (this , _toExpression (element));
702702 }
703703
704+ /// Returns a slice of this array starting at [offset] .
705+ ///
706+ /// When [length] is provided, at most [length] elements are returned.
707+ Expression arraySlice (Object ? offset, [Object ? length]) {
708+ return _ArraySliceExpression (
709+ this ,
710+ _toExpression (offset),
711+ length == null ? null : _toExpression (length),
712+ );
713+ }
714+
715+ /// Filters this array by evaluating [filter] for each element bound to [alias] .
716+ Expression arrayFilter (String alias, BooleanExpression filter) {
717+ return _ArrayFilterExpression (this , alias, filter);
718+ }
719+
720+ /// Transforms each element of this array bound to [elementAlias] .
721+ Expression arrayTransform (String elementAlias, Expression transform) {
722+ return _ArrayTransformExpression (this , elementAlias, null , transform);
723+ }
724+
725+ /// Transforms each element of this array with both element and index aliases.
726+ Expression arrayTransformWithIndex (
727+ String elementAlias,
728+ String indexAlias,
729+ Expression transform,
730+ ) {
731+ return _ArrayTransformExpression (
732+ this ,
733+ elementAlias,
734+ indexAlias,
735+ transform,
736+ );
737+ }
738+
704739 // ============================================================================
705740 // AGGREGATE FUNCTIONS
706741 // ============================================================================
@@ -840,6 +875,9 @@ abstract class Expression implements PipelineSerializable {
840875 /// Creates a field reference expression from a field path string
841876 static Field field (String fieldPath) => Field (fieldPath);
842877
878+ /// Creates a variable reference expression from a variable name.
879+ static Variable variable (String name) => Variable (name);
880+
843881 /// Creates a field reference expression from a FieldPath object
844882 static Field fieldPath (FieldPath fieldPath) => Field (fieldPath.toString ());
845883
@@ -1596,6 +1634,52 @@ abstract class Expression implements PipelineSerializable {
15961634 );
15971635 }
15981636
1637+ /// Returns a slice of [array] starting at [offset] .
1638+ static Expression arraySliceStatic (
1639+ Expression array,
1640+ Object ? offset, [
1641+ Object ? length,
1642+ ]) {
1643+ return _ArraySliceExpression (
1644+ array,
1645+ _toExpression (offset),
1646+ length == null ? null : _toExpression (length),
1647+ );
1648+ }
1649+
1650+ /// Filters [array] by evaluating [filter] for each element bound to [alias] .
1651+ static Expression arrayFilterStatic (
1652+ Expression array,
1653+ String alias,
1654+ BooleanExpression filter,
1655+ ) {
1656+ return _ArrayFilterExpression (array, alias, filter);
1657+ }
1658+
1659+ /// Transforms each element of [array] bound to [elementAlias] .
1660+ static Expression arrayTransformStatic (
1661+ Expression array,
1662+ String elementAlias,
1663+ Expression transform,
1664+ ) {
1665+ return _ArrayTransformExpression (array, elementAlias, null , transform);
1666+ }
1667+
1668+ /// Transforms each element of [array] with both element and index aliases.
1669+ static Expression arrayTransformWithIndexStatic (
1670+ Expression array,
1671+ String elementAlias,
1672+ String indexAlias,
1673+ Expression transform,
1674+ ) {
1675+ return _ArrayTransformExpression (
1676+ array,
1677+ elementAlias,
1678+ indexAlias,
1679+ transform,
1680+ );
1681+ }
1682+
15991683 /// Creates a raw/custom function expression
16001684 static Expression rawFunction (
16011685 String name,
@@ -1684,6 +1768,26 @@ class Field extends Selectable {
16841768 }
16851769}
16861770
1771+ /// Represents a variable reference in a pipeline expression.
1772+ class Variable extends Expression {
1773+ final String variableName;
1774+
1775+ Variable (this .variableName);
1776+
1777+ @override
1778+ String get name => 'variable' ;
1779+
1780+ @override
1781+ Map <String , dynamic > toMap () {
1782+ return {
1783+ 'name' : name,
1784+ 'args' : {
1785+ 'name' : variableName,
1786+ },
1787+ };
1788+ }
1789+ }
1790+
16871791/// Represents a null value expression
16881792class _NullExpression extends Expression {
16891793 _NullExpression ();
@@ -2423,6 +2527,92 @@ class _ArraySumExpression extends FunctionExpression {
24232527 }
24242528}
24252529
2530+ /// Represents an array slice expression.
2531+ class _ArraySliceExpression extends FunctionExpression {
2532+ final Expression expression;
2533+ final Expression offset;
2534+ final Expression ? length;
2535+
2536+ _ArraySliceExpression (this .expression, this .offset, this .length);
2537+
2538+ @override
2539+ String get name => 'array_slice' ;
2540+
2541+ @override
2542+ Map <String , dynamic > toMap () {
2543+ final args = < String , dynamic > {
2544+ 'expression' : expression.toMap (),
2545+ 'offset' : offset.toMap (),
2546+ };
2547+ if (length != null ) {
2548+ args['length' ] = length! .toMap ();
2549+ }
2550+ return {
2551+ 'name' : name,
2552+ 'args' : args,
2553+ };
2554+ }
2555+ }
2556+
2557+ /// Represents an array filter expression.
2558+ class _ArrayFilterExpression extends FunctionExpression {
2559+ final Expression expression;
2560+ final String alias;
2561+ final BooleanExpression filter;
2562+
2563+ _ArrayFilterExpression (this .expression, this .alias, this .filter);
2564+
2565+ @override
2566+ String get name => 'array_filter' ;
2567+
2568+ @override
2569+ Map <String , dynamic > toMap () {
2570+ return {
2571+ 'name' : name,
2572+ 'args' : {
2573+ 'expression' : expression.toMap (),
2574+ 'alias' : alias,
2575+ 'filter' : filter.toMap (),
2576+ },
2577+ };
2578+ }
2579+ }
2580+
2581+ /// Represents an array transform expression.
2582+ class _ArrayTransformExpression extends FunctionExpression {
2583+ final Expression expression;
2584+ final String elementAlias;
2585+ final String ? indexAlias;
2586+ final Expression transform;
2587+
2588+ _ArrayTransformExpression (
2589+ this .expression,
2590+ this .elementAlias,
2591+ this .indexAlias,
2592+ this .transform,
2593+ );
2594+
2595+ @override
2596+ String get name =>
2597+ indexAlias == null ? 'array_transform' : 'array_transform_with_index' ;
2598+
2599+ @override
2600+ Map <String , dynamic > toMap () {
2601+ final args = < String , dynamic > {
2602+ 'expression' : expression.toMap (),
2603+ 'element_alias' : elementAlias,
2604+ 'transform' : transform.toMap (),
2605+ };
2606+ if (indexAlias != null ) {
2607+ args['index_alias' ] = indexAlias;
2608+ }
2609+ return {
2610+ 'name' : name,
2611+ 'args' : args,
2612+ };
2613+ }
2614+ }
2615+
24262616// ============================================================================
24272617// CONDITIONAL / LOGIC OPERATION EXPRESSION CLASSES
24282618// ============================================================================
0 commit comments