Skip to content

Commit 2f47e3d

Browse files
committed
SQL: values in datetime script aggs should be treated as long (#39773)
When a query is translated into script terms agg where key has a date type, it should generate a terms agg with value_type long instead of date, otherwise the key gets formatted as a string, which confuses hit extractor. Fixes #37042
1 parent 9eb4614 commit 2f47e3d

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

x-pack/plugin/sql/qa/src/main/resources/datetime.csv-spec

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,3 +397,11 @@ Bezalel
397397
Bojan
398398

399399
;
400+
401+
// datetime in aggregations
402+
doubleCastOfDateInAggs
403+
SELECT CAST (CAST (birth_date AS VARCHAR) AS TIMESTAMP) a FROM test_emp WHERE YEAR(birth_date) = 1965 GROUP BY a;
404+
a:ts
405+
---------------
406+
1965-01-03T00:00:00Z
407+
;

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/GroupByKey.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public final CompositeValuesSourceBuilder<?> asValueSource() {
4242
} else if (script.outputType() == DataType.DATE) {
4343
builder.valueType(ValueType.LONG);
4444
} else if (script.outputType() == DataType.DATETIME) {
45-
builder.valueType(ValueType.DATE);
45+
builder.valueType(ValueType.LONG);
4646
} else if (script.outputType() == DataType.BOOLEAN) {
4747
builder.valueType(ValueType.BOOLEAN);
4848
} else if (script.outputType() == DataType.IP) {

x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/planner/QueryFolderTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ public void testGroupKeyTypes_DateTime() {
302302
"\"source\":\"InternalSqlScriptUtils.add(InternalSqlScriptUtils.docValue(doc,params.v0)," +
303303
"InternalSqlScriptUtils.intervalYearMonth(params.v1,params.v2))\",\"lang\":\"painless\",\"params\":{" +
304304
"\"v0\":\"date\",\"v1\":\"P1Y2M\",\"v2\":\"INTERVAL_YEAR_TO_MONTH\"}},\"missing_bucket\":true," +
305-
"\"value_type\":\"date\",\"order\":\"asc\"}}}]}}}"));
305+
"\"value_type\":\"long\",\"order\":\"asc\"}}}]}}}"));
306306
assertEquals(2, ee.output().size());
307307
assertThat(ee.output().get(0).toString(), startsWith("count(*){a->"));
308308
assertThat(ee.output().get(1).toString(), startsWith("a{s->"));

0 commit comments

Comments
 (0)