Skip to content

Commit

Permalink
SQL: values in datetime script aggs should be treated as long (#39773)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
imotov committed Mar 11, 2019
1 parent 9eb4614 commit 2f47e3d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
8 changes: 8 additions & 0 deletions x-pack/plugin/sql/qa/src/main/resources/datetime.csv-spec
Original file line number Diff line number Diff line change
Expand Up @@ -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
;
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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->"));
Expand Down

0 comments on commit 2f47e3d

Please sign in to comment.