|
110 | 110 | import static org.elasticsearch.xpack.esql.analysis.AnalyzerTestUtils.randomValueOtherThanTest; |
111 | 111 | import static org.elasticsearch.xpack.esql.analysis.AnalyzerTestUtils.tsdbIndexResolution; |
112 | 112 | import static org.elasticsearch.xpack.esql.core.tree.Source.EMPTY; |
| 113 | +import static org.elasticsearch.xpack.esql.core.type.DataType.DATETIME; |
| 114 | +import static org.elasticsearch.xpack.esql.type.EsqlDataTypeConverter.dateTimeToString; |
113 | 115 | import static org.hamcrest.Matchers.contains; |
114 | 116 | import static org.hamcrest.Matchers.containsString; |
115 | 117 | import static org.hamcrest.Matchers.empty; |
@@ -368,7 +370,7 @@ public void testNoProjection() { |
368 | 370 | DataType.INTEGER, |
369 | 371 | DataType.KEYWORD, |
370 | 372 | DataType.TEXT, |
371 | | - DataType.DATETIME, |
| 373 | + DATETIME, |
372 | 374 | DataType.TEXT, |
373 | 375 | DataType.KEYWORD, |
374 | 376 | DataType.INTEGER, |
@@ -3776,6 +3778,36 @@ public void testResolveCompletionOutputField() { |
3776 | 3778 | assertThat(getAttributeByName(esRelation.output(), "description"), not(equalTo(completion.targetField()))); |
3777 | 3779 | } |
3778 | 3780 |
|
| 3781 | + public void testResolveAggregateWithGroupings() { |
| 3782 | + var plan = analyze(""" |
| 3783 | + FROM test |
| 3784 | + | EVAL date = "2025-01-01"::datetime |
| 3785 | + | STATS c = count(emp_no) BY d = (date == "2025-01-01") |
| 3786 | + """, "mapping-default.json"); |
| 3787 | + |
| 3788 | + var limit = as(plan, Limit.class); |
| 3789 | + var agg = as(limit.child(), Aggregate.class); |
| 3790 | + var aggregates = agg.aggregates(); |
| 3791 | + assertThat(aggregates, hasSize(2)); |
| 3792 | + Alias a = as(aggregates.get(0), Alias.class); |
| 3793 | + assertEquals("c", a.name()); |
| 3794 | + Count c = as(a.child(), Count.class); |
| 3795 | + FieldAttribute fa = as(c.field(), FieldAttribute.class); |
| 3796 | + assertEquals("emp_no", fa.name()); |
| 3797 | + ReferenceAttribute ra = as(aggregates.get(1), ReferenceAttribute.class); // reference in aggregates is resolved |
| 3798 | + assertEquals("d", ra.name()); |
| 3799 | + List<Expression> groupings = agg.groupings(); |
| 3800 | + assertEquals(1, groupings.size()); |
| 3801 | + a = as(groupings.get(0), Alias.class); // reference in grouping is resolved |
| 3802 | + assertEquals("d", ra.name()); |
| 3803 | + Equals equals = as(a.child(), Equals.class); |
| 3804 | + ra = as(equals.left(), ReferenceAttribute.class); |
| 3805 | + assertEquals("date", ra.name()); |
| 3806 | + Literal literal = as(equals.right(), Literal.class); |
| 3807 | + assertEquals("2025-01-01T00:00:00.000Z", dateTimeToString(Long.parseLong(literal.value().toString()))); |
| 3808 | + assertEquals(DATETIME, literal.dataType()); |
| 3809 | + } |
| 3810 | + |
3779 | 3811 | @Override |
3780 | 3812 | protected IndexAnalyzers createDefaultIndexAnalyzers() { |
3781 | 3813 | return super.createDefaultIndexAnalyzers(); |
|
0 commit comments