@@ -267,8 +267,7 @@ public static AstExpression Convert(
267267 Ensure . IsNotNull ( input , nameof ( input ) ) ;
268268 Ensure . IsNotNull ( to , nameof ( to ) ) ;
269269
270- if ( to is AstConstantExpression toConstantExpression &&
271- ( toConstantExpression . Value as BsonString ) ? . Value is { } toValue &&
270+ if ( to . IsStringConstant ( out var toValue ) &&
272271 subType == null &&
273272 byteOrder == null &&
274273 format == null &&
@@ -375,26 +374,26 @@ public static AstExpression DerivativeOrIntegralWindowExpression(AstDerivativeOr
375374
376375 public static AstExpression Divide ( AstExpression arg1 , AstExpression arg2 )
377376 {
378- if ( arg1 is AstConstantExpression constant1 && arg2 is AstConstantExpression constant2 )
377+ if ( arg1 . IsConstant ( out var constant1 ) && arg2 . IsConstant ( out var constant2 ) )
379378 {
380379 return Divide ( constant1 , constant2 ) ;
381380 }
382381
383382 return new AstBinaryExpression ( AstBinaryOperator . Divide , arg1 , arg2 ) ;
384383
385- static AstExpression Divide ( AstConstantExpression constant1 , AstConstantExpression constant2 )
384+ static AstExpression Divide ( BsonValue constant1 , BsonValue constant2 )
386385 {
387- return ( constant1 . Value . BsonType , constant2 . Value . BsonType ) switch
386+ return ( constant1 . BsonType , constant2 . BsonType ) switch
388387 {
389- ( BsonType . Double , BsonType . Double ) => constant1 . Value . AsDouble / constant2 . Value . AsDouble ,
390- ( BsonType . Double , BsonType . Int32 ) => constant1 . Value . AsDouble / constant2 . Value . AsInt32 ,
391- ( BsonType . Double , BsonType . Int64 ) => constant1 . Value . AsDouble / constant2 . Value . AsInt64 ,
392- ( BsonType . Int32 , BsonType . Double ) => constant1 . Value . AsInt32 / constant2 . Value . AsDouble ,
393- ( BsonType . Int32 , BsonType . Int32 ) => ( double ) constant1 . Value . AsInt32 / constant2 . Value . AsInt32 ,
394- ( BsonType . Int32 , BsonType . Int64 ) => ( double ) constant1 . Value . AsInt32 / constant2 . Value . AsInt64 ,
395- ( BsonType . Int64 , BsonType . Double ) => constant1 . Value . AsInt64 / constant2 . Value . AsDouble ,
396- ( BsonType . Int64 , BsonType . Int32 ) => ( double ) constant1 . Value . AsInt64 / constant2 . Value . AsInt32 ,
397- ( BsonType . Int64 , BsonType . Int64 ) => ( double ) constant1 . Value . AsInt64 / constant2 . Value . AsInt64 ,
388+ ( BsonType . Double , BsonType . Double ) => constant1 . AsDouble / constant2 . AsDouble ,
389+ ( BsonType . Double , BsonType . Int32 ) => constant1 . AsDouble / constant2 . AsInt32 ,
390+ ( BsonType . Double , BsonType . Int64 ) => constant1 . AsDouble / constant2 . AsInt64 ,
391+ ( BsonType . Int32 , BsonType . Double ) => constant1 . AsInt32 / constant2 . AsDouble ,
392+ ( BsonType . Int32 , BsonType . Int32 ) => ( double ) constant1 . AsInt32 / constant2 . AsInt32 ,
393+ ( BsonType . Int32 , BsonType . Int64 ) => ( double ) constant1 . AsInt32 / constant2 . AsInt64 ,
394+ ( BsonType . Int64 , BsonType . Double ) => constant1 . AsInt64 / constant2 . AsDouble ,
395+ ( BsonType . Int64 , BsonType . Int32 ) => ( double ) constant1 . AsInt64 / constant2 . AsInt32 ,
396+ ( BsonType . Int64 , BsonType . Int64 ) => ( double ) constant1 . AsInt64 / constant2 . AsInt64 ,
398397 _ => new AstBinaryExpression ( AstBinaryOperator . Divide , constant1 , constant2 )
399398 } ;
400399 }
@@ -829,9 +828,9 @@ public static AstExpression StrLenBytes(AstExpression arg)
829828
830829 public static AstExpression StrLenCP ( AstExpression arg )
831830 {
832- if ( arg is AstConstantExpression constantExpression && constantExpression . Value . BsonType == BsonType . String )
831+ if ( arg . IsStringConstant ( out var stringConstant ) )
833832 {
834- var value = constantExpression . Value . AsString . Length ;
833+ var value = stringConstant . Length ;
835834 return new AstConstantExpression ( value ) ;
836835 }
837836 return new AstUnaryExpression ( AstUnaryOperator . StrLenCP , arg ) ;
@@ -890,9 +889,9 @@ public static AstExpression Switch(IEnumerable<(AstExpression Case, AstExpressio
890889
891890 public static AstExpression ToLower ( AstExpression arg )
892891 {
893- if ( arg is AstConstantExpression constantExpression && constantExpression . Value . BsonType == BsonType . String )
892+ if ( arg . IsStringConstant ( out var stringConstant ) )
894893 {
895- var value = constantExpression . Value . AsString . ToLowerInvariant ( ) ;
894+ var value = stringConstant . ToLowerInvariant ( ) ;
896895 return new AstConstantExpression ( value ) ;
897896 }
898897
@@ -906,9 +905,9 @@ public static AstExpression ToString(AstExpression arg)
906905
907906 public static AstExpression ToUpper ( AstExpression arg )
908907 {
909- if ( arg is AstConstantExpression constantExpression && constantExpression . Value . BsonType == BsonType . String )
908+ if ( arg . IsStringConstant ( out var stringConstant ) )
910909 {
911- var value = constantExpression . Value . AsString . ToUpperInvariant ( ) ;
910+ var value = stringConstant . ToUpperInvariant ( ) ;
912911 return new AstConstantExpression ( value ) ;
913912 }
914913
@@ -985,7 +984,7 @@ public static AstExpression Zip(IEnumerable<AstExpression> inputs, bool? useLong
985984 // private static methods
986985 private static bool AllArgsAreConstantBools ( AstExpression [ ] args , out List < bool > values )
987986 {
988- if ( args . All ( arg => arg is AstConstantExpression constantExpression && constantExpression . Value . BsonType == BsonType . Boolean ) )
987+ if ( args . All ( arg => arg . IsBooleanConstant ( ) ) )
989988 {
990989 values = args . Select ( arg => ( ( AstConstantExpression ) arg ) . Value . AsBoolean ) . ToList ( ) ;
991990 return true ;
@@ -997,7 +996,7 @@ private static bool AllArgsAreConstantBools(AstExpression[] args, out List<bool>
997996
998997 private static bool AllArgsAreConstantInt32s ( AstExpression [ ] args , out List < int > values )
999998 {
1000- if ( args . All ( arg => arg is AstConstantExpression constantExpression && constantExpression . Value . BsonType == BsonType . Int32 ) )
999+ if ( args . All ( arg => arg . IsInt32Constant ( ) ) )
10011000 {
10021001 values = args . Select ( arg => ( ( AstConstantExpression ) arg ) . Value . AsInt32 ) . ToList ( ) ;
10031002 return true ;
0 commit comments