@@ -260,9 +260,7 @@ public static AstExpression Convert(AstExpression input, AstExpression to, AstEx
260
260
Ensure . IsNotNull ( input , nameof ( input ) ) ;
261
261
Ensure . IsNotNull ( to , nameof ( to ) ) ;
262
262
263
- if ( to is AstConstantExpression toConstantExpression &&
264
- ( toConstantExpression . Value as BsonString ) ? . Value is string toValue &&
265
- toValue != null &&
263
+ if ( to . IsStringConstant ( out var toValue ) &&
266
264
onError == null &&
267
265
onNull == null )
268
266
{
@@ -365,26 +363,26 @@ public static AstExpression DerivativeOrIntegralWindowExpression(AstDerivativeOr
365
363
366
364
public static AstExpression Divide ( AstExpression arg1 , AstExpression arg2 )
367
365
{
368
- if ( arg1 is AstConstantExpression constant1 && arg2 is AstConstantExpression constant2 )
366
+ if ( arg1 . IsConstant ( out var constant1 ) && arg2 . IsConstant ( out var constant2 ) )
369
367
{
370
368
return Divide ( constant1 , constant2 ) ;
371
369
}
372
370
373
371
return new AstBinaryExpression ( AstBinaryOperator . Divide , arg1 , arg2 ) ;
374
372
375
- static AstExpression Divide ( AstConstantExpression constant1 , AstConstantExpression constant2 )
373
+ static AstExpression Divide ( BsonValue constant1 , BsonValue constant2 )
376
374
{
377
- return ( constant1 . Value . BsonType , constant2 . Value . BsonType ) switch
375
+ return ( constant1 . BsonType , constant2 . BsonType ) switch
378
376
{
379
- ( BsonType . Double , BsonType . Double ) => constant1 . Value . AsDouble / constant2 . Value . AsDouble ,
380
- ( BsonType . Double , BsonType . Int32 ) => constant1 . Value . AsDouble / constant2 . Value . AsInt32 ,
381
- ( BsonType . Double , BsonType . Int64 ) => constant1 . Value . AsDouble / constant2 . Value . AsInt64 ,
382
- ( BsonType . Int32 , BsonType . Double ) => constant1 . Value . AsInt32 / constant2 . Value . AsDouble ,
383
- ( BsonType . Int32 , BsonType . Int32 ) => ( double ) constant1 . Value . AsInt32 / constant2 . Value . AsInt32 ,
384
- ( BsonType . Int32 , BsonType . Int64 ) => ( double ) constant1 . Value . AsInt32 / constant2 . Value . AsInt64 ,
385
- ( BsonType . Int64 , BsonType . Double ) => constant1 . Value . AsInt64 / constant2 . Value . AsDouble ,
386
- ( BsonType . Int64 , BsonType . Int32 ) => ( double ) constant1 . Value . AsInt64 / constant2 . Value . AsInt32 ,
387
- ( BsonType . Int64 , BsonType . Int64 ) => ( double ) constant1 . Value . AsInt64 / constant2 . Value . AsInt64 ,
377
+ ( BsonType . Double , BsonType . Double ) => constant1 . AsDouble / constant2 . AsDouble ,
378
+ ( BsonType . Double , BsonType . Int32 ) => constant1 . AsDouble / constant2 . AsInt32 ,
379
+ ( BsonType . Double , BsonType . Int64 ) => constant1 . AsDouble / constant2 . AsInt64 ,
380
+ ( BsonType . Int32 , BsonType . Double ) => constant1 . AsInt32 / constant2 . AsDouble ,
381
+ ( BsonType . Int32 , BsonType . Int32 ) => ( double ) constant1 . AsInt32 / constant2 . AsInt32 ,
382
+ ( BsonType . Int32 , BsonType . Int64 ) => ( double ) constant1 . AsInt32 / constant2 . AsInt64 ,
383
+ ( BsonType . Int64 , BsonType . Double ) => constant1 . AsInt64 / constant2 . AsDouble ,
384
+ ( BsonType . Int64 , BsonType . Int32 ) => ( double ) constant1 . AsInt64 / constant2 . AsInt32 ,
385
+ ( BsonType . Int64 , BsonType . Int64 ) => ( double ) constant1 . AsInt64 / constant2 . AsInt64 ,
388
386
_ => new AstBinaryExpression ( AstBinaryOperator . Divide , constant1 , constant2 )
389
387
} ;
390
388
}
@@ -819,9 +817,9 @@ public static AstExpression StrLenBytes(AstExpression arg)
819
817
820
818
public static AstExpression StrLenCP ( AstExpression arg )
821
819
{
822
- if ( arg is AstConstantExpression constantExpression && constantExpression . Value . BsonType == BsonType . String )
820
+ if ( arg . IsStringConstant ( out var stringConstant ) )
823
821
{
824
- var value = constantExpression . Value . AsString . Length ;
822
+ var value = stringConstant . Length ;
825
823
return new AstConstantExpression ( value ) ;
826
824
}
827
825
return new AstUnaryExpression ( AstUnaryOperator . StrLenCP , arg ) ;
@@ -880,9 +878,9 @@ public static AstExpression Switch(IEnumerable<(AstExpression Case, AstExpressio
880
878
881
879
public static AstExpression ToLower ( AstExpression arg )
882
880
{
883
- if ( arg is AstConstantExpression constantExpression && constantExpression . Value . BsonType == BsonType . String )
881
+ if ( arg . IsStringConstant ( out var stringConstant ) )
884
882
{
885
- var value = constantExpression . Value . AsString . ToLowerInvariant ( ) ;
883
+ var value = stringConstant . ToLowerInvariant ( ) ;
886
884
return new AstConstantExpression ( value ) ;
887
885
}
888
886
@@ -896,9 +894,9 @@ public static AstExpression ToString(AstExpression arg)
896
894
897
895
public static AstExpression ToUpper ( AstExpression arg )
898
896
{
899
- if ( arg is AstConstantExpression constantExpression && constantExpression . Value . BsonType == BsonType . String )
897
+ if ( arg . IsStringConstant ( out var stringConstant ) )
900
898
{
901
- var value = constantExpression . Value . AsString . ToUpperInvariant ( ) ;
899
+ var value = stringConstant . ToUpperInvariant ( ) ;
902
900
return new AstConstantExpression ( value ) ;
903
901
}
904
902
@@ -975,7 +973,7 @@ public static AstExpression Zip(IEnumerable<AstExpression> inputs, bool? useLong
975
973
// private static methods
976
974
private static bool AllArgsAreConstantBools ( AstExpression [ ] args , out List < bool > values )
977
975
{
978
- if ( args . All ( arg => arg is AstConstantExpression constantExpression && constantExpression . Value . BsonType == BsonType . Boolean ) )
976
+ if ( args . All ( arg => arg . IsBooleanConstant ( ) ) )
979
977
{
980
978
values = args . Select ( arg => ( ( AstConstantExpression ) arg ) . Value . AsBoolean ) . ToList ( ) ;
981
979
return true ;
@@ -987,7 +985,7 @@ private static bool AllArgsAreConstantBools(AstExpression[] args, out List<bool>
987
985
988
986
private static bool AllArgsAreConstantInt32s ( AstExpression [ ] args , out List < int > values )
989
987
{
990
- if ( args . All ( arg => arg is AstConstantExpression constantExpression && constantExpression . Value . BsonType == BsonType . Int32 ) )
988
+ if ( args . All ( arg => arg . IsInt32Constant ( ) ) )
991
989
{
992
990
values = args . Select ( arg => ( ( AstConstantExpression ) arg ) . Value . AsInt32 ) . ToList ( ) ;
993
991
return true ;
0 commit comments