@@ -20,6 +20,25 @@ Expression _toExpression(Object? value) {
2020 return Constant (value);
2121}
2222
23+ /// Valid unit strings for timestamp add/subtract/truncate expressions.
24+ const Set <String > _timestampUnits = {
25+ 'microsecond' ,
26+ 'millisecond' ,
27+ 'second' ,
28+ 'minute' ,
29+ 'hour' ,
30+ 'day' ,
31+ };
32+
33+ void _validateTimestampUnit (String unit) {
34+ if (! _timestampUnits.contains (unit)) {
35+ throw ArgumentError (
36+ "Timestamp unit must be one of: 'microsecond', 'millisecond', 'second', "
37+ "'minute', 'hour', 'day'. Got: '$unit '" ,
38+ );
39+ }
40+ }
41+
2342/// Base class for all pipeline expressions
2443abstract class Expression implements PipelineSerializable {
2544 /// Creates an aliased expression
@@ -631,47 +650,67 @@ abstract class Expression implements PipelineSerializable {
631650 return _CurrentTimestampExpression ();
632651 }
633652
634- /// Adds time to a timestamp expression
653+ /// Adds time to a timestamp expression.
654+ ///
655+ /// [unit] must be one of: `microsecond` , `millisecond` , `second` , `minute` ,
656+ /// `hour` , `day` .
635657 static Expression timestampAdd (
636658 Expression timestamp,
637659 String unit,
638660 Expression amount,
639661 ) {
662+ _validateTimestampUnit (unit);
640663 return _TimestampAddExpression (timestamp, unit, amount);
641664 }
642665
643- /// Adds time to a timestamp with a literal amount
666+ /// Adds time to a timestamp with a literal amount.
667+ ///
668+ /// [unit] must be one of: `microsecond` , `millisecond` , `second` , `minute` ,
669+ /// `hour` , `day` .
644670 static Expression timestampAddLiteral (
645671 Expression timestamp,
646672 String unit,
647673 int amount,
648674 ) {
675+ _validateTimestampUnit (unit);
649676 return _TimestampAddExpression (timestamp, unit, Constant (amount));
650677 }
651678
652- /// Subtracts time from a timestamp expression
679+ /// Subtracts time from a timestamp expression.
680+ ///
681+ /// [unit] must be one of: `microsecond` , `millisecond` , `second` , `minute` ,
682+ /// `hour` , `day` .
653683 static Expression timestampSubtract (
654684 Expression timestamp,
655685 String unit,
656686 Expression amount,
657687 ) {
688+ _validateTimestampUnit (unit);
658689 return _TimestampSubtractExpression (timestamp, unit, amount);
659690 }
660691
661- /// Subtracts time from a timestamp with a literal amount
692+ /// Subtracts time from a timestamp with a literal amount.
693+ ///
694+ /// [unit] must be one of: `microsecond` , `millisecond` , `second` , `minute` ,
695+ /// `hour` , `day` .
662696 static Expression timestampSubtractLiteral (
663697 Expression timestamp,
664698 String unit,
665699 int amount,
666700 ) {
701+ _validateTimestampUnit (unit);
667702 return _TimestampSubtractExpression (timestamp, unit, Constant (amount));
668703 }
669704
670- /// Truncates a timestamp to a specific unit
705+ /// Truncates a timestamp to a specific unit.
706+ ///
707+ /// [unit] must be one of: `microsecond` , `millisecond` , `second` , `minute` ,
708+ /// `hour` , `day` .
671709 static Expression timestampTruncate (
672710 Expression timestamp,
673711 String unit,
674712 ) {
713+ _validateTimestampUnit (unit);
675714 return _TimestampTruncateExpression (timestamp, unit);
676715 }
677716
@@ -2644,7 +2683,8 @@ class _CurrentTimestampExpression extends FunctionExpression {
26442683 }
26452684}
26462685
2647- /// Represents a timestampAdd function expression
2686+ /// Represents a timestamp_add function expression.
2687+ /// Unit must be one of: microsecond, millisecond, second, minute, hour, day.
26482688class _TimestampAddExpression extends FunctionExpression {
26492689 final Expression timestamp;
26502690 final String unit;
@@ -2668,7 +2708,8 @@ class _TimestampAddExpression extends FunctionExpression {
26682708 }
26692709}
26702710
2671- /// Represents a timestampSubtract function expression
2711+ /// Represents a timestamp_subtract function expression.
2712+ /// Unit must be one of: microsecond, millisecond, second, minute, hour, day.
26722713class _TimestampSubtractExpression extends FunctionExpression {
26732714 final Expression timestamp;
26742715 final String unit;
@@ -2692,7 +2733,8 @@ class _TimestampSubtractExpression extends FunctionExpression {
26922733 }
26932734}
26942735
2695- /// Represents a timestampTruncate function expression
2736+ /// Represents a timestamp_truncate function expression.
2737+ /// Unit must be one of: microsecond, millisecond, second, minute, hour, day.
26962738class _TimestampTruncateExpression extends FunctionExpression {
26972739 final Expression timestamp;
26982740 final String unit;
0 commit comments