3535import org .elasticsearch .xpack .sql .expression .function .aggregate .SumOfSquares ;
3636import org .elasticsearch .xpack .sql .expression .function .aggregate .VarPop ;
3737import org .elasticsearch .xpack .sql .expression .function .scalar .Cast ;
38+ import org .elasticsearch .xpack .sql .expression .function .scalar .datetime .DateAdd ;
39+ import org .elasticsearch .xpack .sql .expression .function .scalar .datetime .DatePart ;
40+ import org .elasticsearch .xpack .sql .expression .function .scalar .datetime .DateTrunc ;
3841import org .elasticsearch .xpack .sql .expression .function .scalar .datetime .DayName ;
3942import org .elasticsearch .xpack .sql .expression .function .scalar .datetime .DayOfMonth ;
4043import org .elasticsearch .xpack .sql .expression .function .scalar .datetime .DayOfYear ;
@@ -614,14 +617,39 @@ public void testSimplifyLeastRandomNullsWithValue() {
614617 assertEquals (TWO , e .children ().get (1 ));
615618 assertEquals (DataType .INTEGER , e .dataType ());
616619 }
617-
620+
618621 public void testConcatFoldingIsNotNull () {
619622 FoldNull foldNull = new FoldNull ();
620623 assertEquals (1 , foldNull .rule (new Concat (EMPTY , NULL , ONE )).fold ());
621624 assertEquals (1 , foldNull .rule (new Concat (EMPTY , ONE , NULL )).fold ());
622625 assertEquals (StringUtils .EMPTY , foldNull .rule (new Concat (EMPTY , NULL , NULL )).fold ());
623626 }
624627
628+ public void testFoldNullDateAdd () {
629+ FoldNull foldNull = new FoldNull ();
630+ assertNullLiteral (foldNull .rule (new DateAdd (EMPTY , NULL , TWO , THREE , UTC )));
631+ assertNullLiteral (foldNull .rule (new DateAdd (EMPTY , ONE , NULL , THREE , UTC )));
632+ assertNullLiteral (foldNull .rule (new DateAdd (EMPTY , ONE , TWO , NULL , UTC )));
633+ assertNullLiteral (foldNull .rule (new DateAdd (EMPTY , NULL , NULL , NULL , UTC )));
634+ assertTrue (foldNull .rule (new DateAdd (EMPTY , ONE , TWO , THREE , UTC )) instanceof DateAdd );
635+ }
636+
637+ public void testFoldNullDatePart () {
638+ FoldNull foldNull = new FoldNull ();
639+ assertNullLiteral (foldNull .rule (new DatePart (EMPTY , NULL , TWO , UTC )));
640+ assertNullLiteral (foldNull .rule (new DatePart (EMPTY , ONE , NULL , UTC )));
641+ assertNullLiteral (foldNull .rule (new DatePart (EMPTY , NULL , NULL , UTC )));
642+ assertTrue (foldNull .rule (new DatePart (EMPTY , ONE , TWO , UTC )) instanceof DatePart );
643+ }
644+
645+ public void testFoldNullDateTrunc () {
646+ FoldNull foldNull = new FoldNull ();
647+ assertNullLiteral (foldNull .rule (new DateTrunc (EMPTY , NULL , TWO , UTC )));
648+ assertNullLiteral (foldNull .rule (new DateTrunc (EMPTY , ONE , NULL , UTC )));
649+ assertNullLiteral (foldNull .rule (new DateTrunc (EMPTY , NULL , NULL , UTC )));
650+ assertTrue (foldNull .rule (new DateTrunc (EMPTY , ONE , TWO , UTC )) instanceof DateTrunc );
651+ }
652+
625653 public void testSimplifyCaseConditionsFoldWhenFalse () {
626654 // CASE WHEN a = 1 THEN 'foo1'
627655 // WHEN 1 = 2 THEN 'bar1'
@@ -1453,7 +1481,7 @@ public void testTranslateMaxToLast() {
14531481 assertSame (last , aggregates .get (0 ));
14541482 assertEquals (max2 , aggregates .get (1 ));
14551483 }
1456-
1484+
14571485 public void testSortAggregateOnOrderByWithTwoFields () {
14581486 FieldAttribute firstField = new FieldAttribute (EMPTY , "first_field" , new EsField ("first_field" , DataType .BYTE , emptyMap (), true ));
14591487 FieldAttribute secondField = new FieldAttribute (EMPTY , "second_field" ,
@@ -1462,20 +1490,20 @@ public void testSortAggregateOnOrderByWithTwoFields() {
14621490 Alias secondAlias = new Alias (EMPTY , "second_alias" , secondField );
14631491 Order firstOrderBy = new Order (EMPTY , firstField , OrderDirection .ASC , Order .NullsPosition .LAST );
14641492 Order secondOrderBy = new Order (EMPTY , secondField , OrderDirection .ASC , Order .NullsPosition .LAST );
1465-
1493+
14661494 OrderBy orderByPlan = new OrderBy (EMPTY ,
14671495 new Aggregate (EMPTY , FROM (), Arrays .asList (secondField , firstField ), Arrays .asList (secondAlias , firstAlias )),
14681496 Arrays .asList (firstOrderBy , secondOrderBy ));
14691497 LogicalPlan result = new SortAggregateOnOrderBy ().apply (orderByPlan );
1470-
1498+
14711499 assertTrue (result instanceof OrderBy );
14721500 List <Order > order = ((OrderBy ) result ).order ();
14731501 assertEquals (2 , order .size ());
14741502 assertTrue (order .get (0 ).child () instanceof FieldAttribute );
14751503 assertTrue (order .get (1 ).child () instanceof FieldAttribute );
14761504 assertEquals ("first_field" , ((FieldAttribute ) order .get (0 ).child ()).name ());
14771505 assertEquals ("second_field" , ((FieldAttribute ) order .get (1 ).child ()).name ());
1478-
1506+
14791507 assertTrue (((OrderBy ) result ).child () instanceof Aggregate );
14801508 Aggregate agg = (Aggregate ) ((OrderBy ) result ).child ();
14811509 List <?> groupings = agg .groupings ();
@@ -1485,7 +1513,7 @@ public void testSortAggregateOnOrderByWithTwoFields() {
14851513 assertEquals (firstField , groupings .get (0 ));
14861514 assertEquals (secondField , groupings .get (1 ));
14871515 }
1488-
1516+
14891517 public void testSortAggregateOnOrderByOnlyAliases () {
14901518 FieldAttribute firstField = new FieldAttribute (EMPTY , "first_field" , new EsField ("first_field" , DataType .BYTE , emptyMap (), true ));
14911519 FieldAttribute secondField = new FieldAttribute (EMPTY , "second_field" ,
@@ -1494,20 +1522,20 @@ public void testSortAggregateOnOrderByOnlyAliases() {
14941522 Alias secondAlias = new Alias (EMPTY , "second_alias" , secondField );
14951523 Order firstOrderBy = new Order (EMPTY , firstAlias , OrderDirection .ASC , Order .NullsPosition .LAST );
14961524 Order secondOrderBy = new Order (EMPTY , secondAlias , OrderDirection .ASC , Order .NullsPosition .LAST );
1497-
1525+
14981526 OrderBy orderByPlan = new OrderBy (EMPTY ,
14991527 new Aggregate (EMPTY , FROM (), Arrays .asList (secondAlias , firstAlias ), Arrays .asList (secondAlias , firstAlias )),
15001528 Arrays .asList (firstOrderBy , secondOrderBy ));
15011529 LogicalPlan result = new SortAggregateOnOrderBy ().apply (orderByPlan );
1502-
1530+
15031531 assertTrue (result instanceof OrderBy );
15041532 List <Order > order = ((OrderBy ) result ).order ();
15051533 assertEquals (2 , order .size ());
15061534 assertTrue (order .get (0 ).child () instanceof Alias );
15071535 assertTrue (order .get (1 ).child () instanceof Alias );
15081536 assertEquals ("first_alias" , ((Alias ) order .get (0 ).child ()).name ());
15091537 assertEquals ("second_alias" , ((Alias ) order .get (1 ).child ()).name ());
1510-
1538+
15111539 assertTrue (((OrderBy ) result ).child () instanceof Aggregate );
15121540 Aggregate agg = (Aggregate ) ((OrderBy ) result ).child ();
15131541 List <?> groupings = agg .groupings ();
@@ -1536,28 +1564,28 @@ public void testPivotRewrite() {
15361564 assertEquals (column , in .value ());
15371565 assertEquals (Arrays .asList (L (1 ), L (2 )), in .list ());
15381566 }
1539-
1567+
15401568 /**
15411569 * Test queries like SELECT MIN(agg_field), MAX(agg_field) FROM table WHERE MATCH(match_field,'A') AND/OR QUERY('match_field:A')
15421570 * or SELECT STDDEV_POP(agg_field), VAR_POP(agg_field) FROM table WHERE MATCH(match_field,'A') AND/OR QUERY('match_field:A')
15431571 */
15441572 public void testAggregatesPromoteToStats_WithFullTextPredicatesConditions () {
15451573 FieldAttribute matchField = new FieldAttribute (EMPTY , "match_field" , new EsField ("match_field" , DataType .TEXT , emptyMap (), true ));
15461574 FieldAttribute aggField = new FieldAttribute (EMPTY , "agg_field" , new EsField ("agg_field" , DataType .INTEGER , emptyMap (), true ));
1547-
1575+
15481576 FullTextPredicate matchPredicate = new MatchQueryPredicate (EMPTY , matchField , "A" , StringUtils .EMPTY );
15491577 FullTextPredicate multiMatchPredicate = new MultiMatchQueryPredicate (EMPTY , "match_field" , "A" , StringUtils .EMPTY );
15501578 FullTextPredicate stringQueryPredicate = new StringQueryPredicate (EMPTY , "match_field:A" , StringUtils .EMPTY );
15511579 List <FullTextPredicate > predicates = Arrays .asList (matchPredicate , multiMatchPredicate , stringQueryPredicate );
15521580
15531581 FullTextPredicate left = randomFrom (predicates );
15541582 FullTextPredicate right = randomFrom (predicates );
1555-
1583+
15561584 BinaryLogic or = new Or (EMPTY , left , right );
15571585 BinaryLogic and = new And (EMPTY , left , right );
15581586 BinaryLogic condition = randomFrom (or , and );
15591587 Filter filter = new Filter (EMPTY , FROM (), condition );
1560-
1588+
15611589 List <AggregateFunction > aggregates ;
15621590 boolean isSimpleStats = randomBoolean ();
15631591 if (isSimpleStats ) {
@@ -1576,13 +1604,13 @@ public void testAggregatesPromoteToStats_WithFullTextPredicatesConditions() {
15761604 } else {
15771605 result = new ReplaceAggsWithExtendedStats ().apply (aggregatePlan );
15781606 }
1579-
1607+
15801608 assertTrue (result instanceof Aggregate );
15811609 Aggregate resultAgg = (Aggregate ) result ;
15821610 assertEquals (2 , resultAgg .aggregates ().size ());
15831611 assertTrue (resultAgg .aggregates ().get (0 ) instanceof InnerAggregate );
15841612 assertTrue (resultAgg .aggregates ().get (1 ) instanceof InnerAggregate );
1585-
1613+
15861614 InnerAggregate resultFirstAgg = (InnerAggregate ) resultAgg .aggregates ().get (0 );
15871615 InnerAggregate resultSecondAgg = (InnerAggregate ) resultAgg .aggregates ().get (1 );
15881616 assertEquals (resultFirstAgg .inner (), firstAggregate );
@@ -1598,8 +1626,8 @@ public void testAggregatesPromoteToStats_WithFullTextPredicatesConditions() {
15981626 assertEquals (((ExtendedStats ) resultFirstAgg .outer ()).field (), aggField );
15991627 assertEquals (((ExtendedStats ) resultSecondAgg .outer ()).field (), aggField );
16001628 }
1601-
1629+
16021630 assertTrue (resultAgg .child () instanceof Filter );
16031631 assertEquals (resultAgg .child (), filter );
16041632 }
1605- }
1633+ }
0 commit comments