Skip to content

Commit 483a279

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 353128c commit 483a279

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
@@ -640,6 +640,33 @@ public void testTranslateIsNotNullExpression_HavingClause_Painless() {
640640
assertThat(aggFilter.scriptTemplate().params().toString(), startsWith("[{a=max(int)"));
641641
}
642642

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

0 commit comments

Comments
 (0)