Skip to content

Commit e180e27

Browse files
committed
SQL: [Tests] Add tests for optimization of aliased expressions (#53048)
Add a unit test to verify that the optimization of expression (e.g. COALESCE) is applied to all instances of the expression: SELECT, WHERE, GROUP BY and HAVING. Relates to #35270 (cherry picked from commit 2ceedc7)
1 parent 1d5c842 commit e180e27

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,33 @@ public void testTranslateIsNotNullExpression_HavingClause_Painless() {
647647
assertThat(aggFilter.scriptTemplate().params().toString(), startsWith("[{a=max(int)"));
648648
}
649649

650+
public void testTranslateCoalesceExpression_WhereGroupByAndHaving_Painless() {
651+
PhysicalPlan p = optimizeAndPlan("SELECT COALESCE(null, int) AS c, COALESCE(max(date), NULL) as m FROM test " +
652+
"WHERE c > 10 GROUP BY c HAVING m > '2020-01-01'::date");
653+
assertTrue(p instanceof EsQueryExec);
654+
EsQueryExec esQExec = (EsQueryExec) p;
655+
assertEquals(2, esQExec.output().size());
656+
AggregationBuilder aggBuilder = esQExec.queryContainer().aggs().asAggBuilder();
657+
assertEquals(1, aggBuilder.getSubAggregations().size());
658+
assertEquals(1, aggBuilder.getPipelineAggregations().size());
659+
String aggName = aggBuilder.getSubAggregations().iterator().next().getName();
660+
String havingName = aggBuilder.getPipelineAggregations().iterator().next().getName();
661+
assertThat(aggBuilder.toString(), containsString("{\"terms\":{\"script\":{\"source\":\"" +
662+
"InternalSqlScriptUtils.coalesce([InternalSqlScriptUtils.docValue(doc,params.v0)])\",\"lang\":\"painless\"" +
663+
",\"params\":{\"v0\":\"int\"}},\"missing_bucket\":true,\"value_type\":\"long\",\"order\":\"asc\"}}}]}," +
664+
"\"aggregations\":{\"" + aggName + "\":{\"max\":{\"field\":\"date\"}},\"" + havingName + "\":" +
665+
"{\"bucket_selector\":{\"buckets_path\":{\"a0\":\"" + aggName + "\"},\"script\":{\"source\":\"" +
666+
"InternalSqlScriptUtils.nullSafeFilter(InternalSqlScriptUtils.gt(InternalSqlScriptUtils.coalesce(" +
667+
"[params.a0]),InternalSqlScriptUtils.asDateTime(params.v0)))\",\"lang\":\"painless\",\"params\":" +
668+
"{\"v0\":\"2020-01-01T00:00:00.000Z\"}}"));
669+
assertTrue(esQExec.queryContainer().query() instanceof ScriptQuery);
670+
ScriptQuery sq = (ScriptQuery) esQExec.queryContainer().query();
671+
assertEquals("InternalSqlScriptUtils.nullSafeFilter(InternalSqlScriptUtils.gt(" +
672+
"InternalSqlScriptUtils.coalesce([InternalSqlScriptUtils.docValue(doc,params.v0)]),params.v1))",
673+
sq.script().toString());
674+
assertEquals("[{v=int}, {v=10}]", sq.script().params().toString());
675+
}
676+
650677
public void testTranslateInExpression_WhereClause() {
651678
LogicalPlan p = plan("SELECT * FROM test WHERE keyword IN ('foo', 'bar', 'lala', 'foo', concat('la', 'la'))");
652679
assertTrue(p instanceof Project);

0 commit comments

Comments
 (0)