From 38f3cf9e65ea49ae017747b7d3351fe7b7a188fe Mon Sep 17 00:00:00 2001 From: Soumyava <93540295+somu-imply@users.noreply.github.com> Date: Tue, 12 Dec 2023 12:50:32 -0800 Subject: [PATCH] Fixing a case where datatype mismatch was happenning in join (#15541) --- .../druid/sql/calcite/rule/DruidJoinRule.java | 2 +- .../sql/calcite/CalciteJoinQueryTest.java | 81 +++++++++++++++++++ 2 files changed, 82 insertions(+), 1 deletion(-) diff --git a/sql/src/main/java/org/apache/druid/sql/calcite/rule/DruidJoinRule.java b/sql/src/main/java/org/apache/druid/sql/calcite/rule/DruidJoinRule.java index 35e3e6eca80..d5a307bc54f 100644 --- a/sql/src/main/java/org/apache/druid/sql/calcite/rule/DruidJoinRule.java +++ b/sql/src/main/java/org/apache/druid/sql/calcite/rule/DruidJoinRule.java @@ -215,7 +215,7 @@ public class DruidJoinRule extends RelOptRule if (postJoinFilter != null) { relBuilder = relBuilder.filter(postJoinFilter); } - + relBuilder.convert(join.getRowType(), false); call.transformTo(relBuilder.build()); } diff --git a/sql/src/test/java/org/apache/druid/sql/calcite/CalciteJoinQueryTest.java b/sql/src/test/java/org/apache/druid/sql/calcite/CalciteJoinQueryTest.java index 020cc83ccae..7d6daec5d24 100644 --- a/sql/src/test/java/org/apache/druid/sql/calcite/CalciteJoinQueryTest.java +++ b/sql/src/test/java/org/apache/druid/sql/calcite/CalciteJoinQueryTest.java @@ -6466,4 +6466,85 @@ public class CalciteJoinQueryTest extends BaseCalciteQueryTest ) ); } + + @Test + public void testLeftJoinsOnTwoWithTables() + { + Map context = new HashMap<>(QUERY_CONTEXT_DEFAULT); + testQuery( + "with raw1 as (\n" + + " select\n" + + " dim1,\n" + + " count(*)/2 as c\n" + + " from foo\n" + + " GROUP BY 1\n" + + "),\n" + + " raw2 as (\n" + + " select \n" + + " dim1,\n" + + " count(*)/2 as c\n" + + " from foo\n" + + " GROUP BY 1\n" + + ")\n" + + "select\n" + + " r1.c-r2.c\n" + + "from raw1 r1\n" + + "left join raw2 r2\n" + + "on r1.dim1 = r2.dim1", + context, + ImmutableList.of( + newScanQueryBuilder() + .dataSource( + join( + new QueryDataSource( + GroupByQuery.builder() + .setInterval(querySegmentSpec(Filtration.eternity())) + .setGranularity(Granularities.ALL) + .setDataSource(new TableDataSource(CalciteTests.DATASOURCE1)) + .setDimensions(new DefaultDimensionSpec("dim1", "d0", ColumnType.STRING)) + .setAggregatorSpecs(aggregators(new CountAggregatorFactory("a0"))) + .setPostAggregatorSpecs(expressionPostAgg( + "p0", + "(\"a0\" / 2)", + ColumnType.LONG + )) + .setContext(context) + .build() + ), + new QueryDataSource( + GroupByQuery.builder() + .setInterval(querySegmentSpec(Filtration.eternity())) + .setGranularity(Granularities.ALL) + .setDataSource(new TableDataSource(CalciteTests.DATASOURCE1)) + .setDimensions(new DefaultDimensionSpec("dim1", "d0", ColumnType.STRING)) + .setAggregatorSpecs(aggregators(new CountAggregatorFactory("a0"))) + .setPostAggregatorSpecs(expressionPostAgg( + "p0", + "(\"a0\" / 2)", + ColumnType.LONG + )) + .setContext(context) + .build() + ), + "j0.", + "(\"d0\" == \"j0.d0\")", + JoinType.LEFT + ) + ) + .intervals(querySegmentSpec(Filtration.eternity())) + .columns("v0") + .virtualColumns(expressionVirtualColumn("v0", "(\"p0\" - \"j0.p0\")", ColumnType.LONG)) + .context(context) + .build() + ), + ImmutableList.of( + new Object[]{0L}, + new Object[]{0L}, + new Object[]{0L}, + new Object[]{0L}, + new Object[]{0L}, + new Object[]{0L} + ) + ); + } }