From 2f47e3d05aee9b3e2e5103e5640ca40784338ff8 Mon Sep 17 00:00:00 2001 From: Igor Motov Date: Fri, 8 Mar 2019 10:24:52 -0500 Subject: [PATCH] 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 --- x-pack/plugin/sql/qa/src/main/resources/datetime.csv-spec | 8 ++++++++ .../elasticsearch/xpack/sql/querydsl/agg/GroupByKey.java | 2 +- .../elasticsearch/xpack/sql/planner/QueryFolderTests.java | 2 +- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/x-pack/plugin/sql/qa/src/main/resources/datetime.csv-spec b/x-pack/plugin/sql/qa/src/main/resources/datetime.csv-spec index fdb106251824d..367b5d0ddfdcf 100644 --- a/x-pack/plugin/sql/qa/src/main/resources/datetime.csv-spec +++ b/x-pack/plugin/sql/qa/src/main/resources/datetime.csv-spec @@ -397,3 +397,11 @@ Bezalel Bojan ; + +// datetime in aggregations +doubleCastOfDateInAggs +SELECT CAST (CAST (birth_date AS VARCHAR) AS TIMESTAMP) a FROM test_emp WHERE YEAR(birth_date) = 1965 GROUP BY a; + a:ts +--------------- +1965-01-03T00:00:00Z +; \ No newline at end of file diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/GroupByKey.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/GroupByKey.java index 6f26ee1dd960c..9638a1bd305d2 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/GroupByKey.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/GroupByKey.java @@ -42,7 +42,7 @@ public final CompositeValuesSourceBuilder asValueSource() { } else if (script.outputType() == DataType.DATE) { builder.valueType(ValueType.LONG); } else if (script.outputType() == DataType.DATETIME) { - builder.valueType(ValueType.DATE); + builder.valueType(ValueType.LONG); } else if (script.outputType() == DataType.BOOLEAN) { builder.valueType(ValueType.BOOLEAN); } else if (script.outputType() == DataType.IP) { diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/planner/QueryFolderTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/planner/QueryFolderTests.java index 17b1eedf06d93..6e538bb0e7a5d 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/planner/QueryFolderTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/planner/QueryFolderTests.java @@ -302,7 +302,7 @@ public void testGroupKeyTypes_DateTime() { "\"source\":\"InternalSqlScriptUtils.add(InternalSqlScriptUtils.docValue(doc,params.v0)," + "InternalSqlScriptUtils.intervalYearMonth(params.v1,params.v2))\",\"lang\":\"painless\",\"params\":{" + "\"v0\":\"date\",\"v1\":\"P1Y2M\",\"v2\":\"INTERVAL_YEAR_TO_MONTH\"}},\"missing_bucket\":true," + - "\"value_type\":\"date\",\"order\":\"asc\"}}}]}}}")); + "\"value_type\":\"long\",\"order\":\"asc\"}}}]}}}")); assertEquals(2, ee.output().size()); assertThat(ee.output().get(0).toString(), startsWith("count(*){a->")); assertThat(ee.output().get(1).toString(), startsWith("a{s->"));