github-advanced-security[bot] commented on code in PR #20607:
URL: https://github.com/apache/druid/pull/20607#discussion_r4286815990
##########
sql/src/test/java/org/apache/druid/sql/calcite/CalciteJoinQueryTest.java:
##########
@@ -6584,4 +6589,128 @@
)
);
}
+
+ @DecoupledTestConfig(quidemReason =
QuidemTestCaseReason.JOIN_FILTER_LOCATIONS)
+ @Test
+ public void testLeftJoinWithTimeFilterAndNonTimeOrderBy()
+ {
+ // Cannot vectorize direct queries on lookup tables.
+ cannotVectorize();
+
+ final Interval interval =
Intervals.utc(DateTimes.of("2001-01-01").getMillis(), JodaUtils.MAX_INSTANT);
+
+ // The filter on __time becomes the intervals of the scan.
+ testQuery(
+ "SELECT f1.dim1, f2.v\n"
+ + "FROM foo f1 LEFT JOIN (SELECT k, v FROM lookup.lookyloo GROUP BY k,
v) f2 ON f1.dim2 = f2.k\n"
+ + "WHERE f1.__time >= TIMESTAMP '2001-01-01 00:00:00'\n"
+ + "ORDER BY f2.v, f1.dim1",
+ ImmutableList.of(
+ new WindowOperatorQuery(
+ new QueryDataSource(
+ newScanQueryBuilder()
+ .dataSource(
+ join(
+ new TableDataSource(CalciteTests.DATASOURCE1),
+ new QueryDataSource(
+ GroupByQuery.builder()
+ .setDataSource(new
LookupDataSource("lookyloo"))
+
.setInterval(querySegmentSpec(Filtration.eternity()))
+
.setGranularity(Granularities.ALL)
+ .setDimensions(
+ new
DefaultDimensionSpec("k", "d0"),
+ new
DefaultDimensionSpec("v", "d1")
+ )
+
.setContext(QUERY_CONTEXT_DEFAULT)
+ .build()
+ ),
+ "j0.",
+ equalsCondition(makeColumnExpression("dim2"),
makeColumnExpression("j0.d0")),
Review Comment:
## CodeQL / Deprecated method or constructor invocation
Invoking [CalciteTestBase.makeColumnExpression](1) should be avoided because
it has been deprecated.
[Show more
details](https://github.com/apache/druid/security/code-scanning/12090)
##########
sql/src/test/java/org/apache/druid/sql/calcite/CalciteJoinQueryTest.java:
##########
@@ -6584,4 +6589,128 @@
)
);
}
+
+ @DecoupledTestConfig(quidemReason =
QuidemTestCaseReason.JOIN_FILTER_LOCATIONS)
+ @Test
+ public void testLeftJoinWithTimeFilterAndNonTimeOrderBy()
+ {
+ // Cannot vectorize direct queries on lookup tables.
+ cannotVectorize();
+
+ final Interval interval =
Intervals.utc(DateTimes.of("2001-01-01").getMillis(), JodaUtils.MAX_INSTANT);
+
+ // The filter on __time becomes the intervals of the scan.
+ testQuery(
+ "SELECT f1.dim1, f2.v\n"
+ + "FROM foo f1 LEFT JOIN (SELECT k, v FROM lookup.lookyloo GROUP BY k,
v) f2 ON f1.dim2 = f2.k\n"
+ + "WHERE f1.__time >= TIMESTAMP '2001-01-01 00:00:00'\n"
+ + "ORDER BY f2.v, f1.dim1",
+ ImmutableList.of(
+ new WindowOperatorQuery(
+ new QueryDataSource(
+ newScanQueryBuilder()
+ .dataSource(
+ join(
+ new TableDataSource(CalciteTests.DATASOURCE1),
+ new QueryDataSource(
+ GroupByQuery.builder()
+ .setDataSource(new
LookupDataSource("lookyloo"))
+
.setInterval(querySegmentSpec(Filtration.eternity()))
+
.setGranularity(Granularities.ALL)
+ .setDimensions(
+ new
DefaultDimensionSpec("k", "d0"),
+ new
DefaultDimensionSpec("v", "d1")
+ )
+
.setContext(QUERY_CONTEXT_DEFAULT)
+ .build()
+ ),
+ "j0.",
+ equalsCondition(makeColumnExpression("dim2"),
makeColumnExpression("j0.d0")),
Review Comment:
## CodeQL / Deprecated method or constructor invocation
Invoking [CalciteTestBase.makeColumnExpression](1) should be avoided because
it has been deprecated.
[Show more
details](https://github.com/apache/druid/security/code-scanning/12091)
##########
sql/src/test/java/org/apache/druid/sql/calcite/CalciteTimeConditionQueryTest.java:
##########
@@ -0,0 +1,569 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.druid.sql.calcite;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import org.apache.druid.error.DruidException;
+import org.apache.druid.error.DruidExceptionMatcher;
+import org.apache.druid.java.util.common.Intervals;
+import org.apache.druid.java.util.common.granularity.Granularities;
+import org.apache.druid.query.Druids;
+import org.apache.druid.query.GlobalTableDataSource;
+import org.apache.druid.query.LookupDataSource;
+import org.apache.druid.query.QueryContexts;
+import org.apache.druid.query.QueryDataSource;
+import org.apache.druid.query.TableDataSource;
+import org.apache.druid.query.UnionDataSource;
+import org.apache.druid.query.UnnestDataSource;
+import org.apache.druid.query.aggregation.CountAggregatorFactory;
+import org.apache.druid.query.aggregation.LongSumAggregatorFactory;
+import org.apache.druid.query.dimension.DefaultDimensionSpec;
+import org.apache.druid.query.groupby.GroupByQuery;
+import org.apache.druid.query.timeboundary.TimeBoundaryQuery;
+import org.apache.druid.segment.column.ColumnType;
+import org.apache.druid.segment.join.JoinType;
+import org.apache.druid.sql.calcite.NotYetSupported.Modes;
+import org.apache.druid.sql.calcite.filtration.Filtration;
+import org.apache.druid.sql.calcite.planner.PlannerContext;
+import org.apache.druid.sql.calcite.util.CalciteTests;
+import org.junit.jupiter.api.Test;
+
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Tests for the {@link PlannerContext#CTX_SQL_TIME_CONDITION} context
parameter.
+ */
+public class CalciteTimeConditionQueryTest extends BaseCalciteQueryTest
+{
+ // __time for foo is [2000-01-01, 2000-01-02, 2000-01-03, 2001-01-01,
2001-01-02, 2001-01-03]
+
+ @Test
+ public void testScan()
+ {
+ final Map<String, Object> context = makeContext("2000-01-02/2000-01-03,
2001-01-01/2001-01-02");
+
+ testBuilder()
+ .sql("SELECT __time, dim1 FROM foo")
+ .queryContext(context)
+ .expectedQueries(
+ ImmutableList.of(
+ newScanQueryBuilder()
+ .dataSource(CalciteTests.DATASOURCE1)
+ .intervals(
+ querySegmentSpec(
+ Intervals.of("2000-01-02/2000-01-03"),
+ Intervals.of("2001-01-01/2001-01-02")
+ )
+ )
+ .columns("__time", "dim1")
+ .columnTypes(ColumnType.LONG, ColumnType.STRING)
+ .context(context)
+ .build()
+ )
+ )
+ // Unordered, since MSQ does not guarantee the order of scan results
across segments.
+ .expectedResults(
+ new UnorderedResultsVerifier(
+ ImmutableList.of(
+ new Object[]{timestamp("2000-01-02"), "10.1"},
+ new Object[]{timestamp("2001-01-01"), "1"}
+ ),
+ ResultMatchMode.EQUALS,
+ null
+ )
+ )
+ .run();
+ }
+
+ @Test
+ public void testScanWithEmptyIntersection()
+ {
+ final Map<String, Object> context = makeContext("2000/2001");
+
+ testQuery(
+ "SELECT dim1 FROM foo WHERE __time >= TIMESTAMP '2001-01-01 00:00:00'",
+ context,
+ ImmutableList.of(
+ newScanQueryBuilder()
+ .dataSource(CalciteTests.DATASOURCE1)
+ .intervals(querySegmentSpec())
+ .columns("dim1")
+ .columnTypes(ColumnType.STRING)
+ .context(context)
+ .build()
+ ),
+ ImmutableList.of()
+ );
+ }
+
+ @Test
+ public void testTimeseriesWithTimeFilter()
+ {
+ final Map<String, Object> context = makeContext("2000/2001");
+
+ testQuery(
+ "SELECT COUNT(*) FROM foo WHERE __time >= TIMESTAMP '2000-01-02
00:00:00'",
+ context,
+ ImmutableList.of(
+ Druids.newTimeseriesQueryBuilder()
+ .dataSource(CalciteTests.DATASOURCE1)
+
.intervals(querySegmentSpec(Intervals.of("2000-01-02/2001-01-01")))
+ .granularity(Granularities.ALL)
+ .aggregators(aggregators(new CountAggregatorFactory("a0")))
+ .context(context)
+ .build()
+ ),
+ ImmutableList.of(
+ new Object[]{2L}
+ )
+ );
+ }
+
+ @Test
+ public void testGroupByWithArray()
+ {
+ final Map<String, Object> context =
makeContext(List.of("2001-01-01/2001-01-03", "2000-01-01/2000-01-02"));
+
+ testQuery(
+ "SELECT dim2, COUNT(*) FROM foo GROUP BY dim2",
+ context,
+ ImmutableList.of(
+ GroupByQuery.builder()
+ .setDataSource(CalciteTests.DATASOURCE1)
+ .setInterval(
+ querySegmentSpec(
+ Intervals.of("2000-01-01/2000-01-02"),
+ Intervals.of("2001-01-01/2001-01-03")
+ )
+ )
+ .setGranularity(Granularities.ALL)
+ .setDimensions(dimensions(new
DefaultDimensionSpec("dim2", "d0")))
+ .setAggregatorSpecs(aggregators(new
CountAggregatorFactory("a0")))
+ .setContext(context)
+ .build()
+ ),
+ ImmutableList.of(
+ new Object[]{"a", 2L},
+ new Object[]{"abc", 1L}
+ )
+ );
+ }
+
+ @Test
+ public void testTimeBoundary()
+ {
+ final Map<String, Object> context = ImmutableMap.<String, Object>builder()
+
.putAll(makeContext("2000/2001"))
+
.put(QueryContexts.TIME_BOUNDARY_PLANNING_KEY, true)
+ .build();
+
+ testQuery(
+ "SELECT MAX(__time) FROM foo",
+ context,
+ ImmutableList.of(
+ Druids.newTimeBoundaryQueryBuilder()
+ .dataSource(CalciteTests.DATASOURCE1)
+ .intervals(querySegmentSpec(Intervals.of("2000/2001")))
+ .bound(TimeBoundaryQuery.MAX_TIME)
+ .context(
+ ImmutableMap.<String, Object>builder()
+ .putAll(context)
+
.put(TimeBoundaryQuery.MAX_TIME_ARRAY_OUTPUT_NAME, "a0")
+ .build()
+ )
+ .build()
+ ),
+ ImmutableList.of(
+ new Object[]{timestamp("2000-01-03")}
+ )
+ );
+ }
+
+ @NotYetSupported(Modes.DD_UNION)
+ @Test
+ public void testUnionDataSource()
+ {
+ final Map<String, Object> context = makeContext("2000-01-01/2000-01-03");
+
+ testQuery(
+ "SELECT dim1, COUNT(*) FROM (SELECT * FROM foo UNION ALL SELECT * FROM
foo) GROUP BY dim1",
+ context,
+ ImmutableList.of(
+ GroupByQuery.builder()
+ .setDataSource(
+ new UnionDataSource(
+ ImmutableList.of(
+ new
TableDataSource(CalciteTests.DATASOURCE1),
+ new
TableDataSource(CalciteTests.DATASOURCE1)
+ )
+ )
+ )
+
.setInterval(querySegmentSpec(Intervals.of("2000-01-01/2000-01-03")))
+ .setGranularity(Granularities.ALL)
+ .setDimensions(dimensions(new
DefaultDimensionSpec("dim1", "d0")))
+ .setAggregatorSpecs(aggregators(new
CountAggregatorFactory("a0")))
+ .setContext(context)
+ .build()
+ ),
+ ImmutableList.of(
+ new Object[]{"", 2L},
+ new Object[]{"10.1", 2L}
+ )
+ );
+ }
+
+ @Test
+ public void testUnnest()
+ {
+ final Map<String, Object> context = makeContext("2000-01-01/2000-01-03");
+
+ testQuery(
+ "SELECT d3 FROM foo, UNNEST(MV_TO_ARRAY(dim3)) AS unnested (d3)",
+ context,
+ ImmutableList.of(
+ newScanQueryBuilder()
+ .dataSource(
+ UnnestDataSource.create(
+ new TableDataSource(CalciteTests.DATASOURCE1),
+ nestedExpressionVirtualColumn("j0.unnest", "\"dim3\"",
ColumnType.STRING),
+ null
+ )
+ )
+
.intervals(querySegmentSpec(Intervals.of("2000-01-01/2000-01-03")))
+ .columns("j0.unnest")
+ .columnTypes(ColumnType.STRING)
+ .context(context)
+ .build()
+ ),
+ ImmutableList.of(
+ new Object[]{"a"},
+ new Object[]{"b"},
+ new Object[]{"b"},
+ new Object[]{"c"}
+ )
+ );
+ }
+
+ @Test
+ public void testJoinWithLookup()
+ {
+ final Map<String, Object> context = makeContext("2001/2002");
+
+ testQuery(
+ "SELECT foo.dim1, lookyloo.v FROM foo INNER JOIN lookup.lookyloo ON
foo.dim2 = lookyloo.k",
+ context,
+ ImmutableList.of(
+ newScanQueryBuilder()
+ .dataSource(
+ join(
+ new TableDataSource(CalciteTests.DATASOURCE1),
+ new LookupDataSource("lookyloo"),
+ "j0.",
+ equalsCondition(makeColumnExpression("dim2"),
makeColumnExpression("j0.k")),
Review Comment:
## CodeQL / Deprecated method or constructor invocation
Invoking [CalciteTestBase.makeColumnExpression](1) should be avoided because
it has been deprecated.
[Show more
details](https://github.com/apache/druid/security/code-scanning/12094)
##########
sql/src/test/java/org/apache/druid/sql/calcite/CalciteJoinQueryTest.java:
##########
@@ -6584,4 +6589,128 @@
)
);
}
+
+ @DecoupledTestConfig(quidemReason =
QuidemTestCaseReason.JOIN_FILTER_LOCATIONS)
+ @Test
+ public void testLeftJoinWithTimeFilterAndNonTimeOrderBy()
+ {
+ // Cannot vectorize direct queries on lookup tables.
+ cannotVectorize();
+
+ final Interval interval =
Intervals.utc(DateTimes.of("2001-01-01").getMillis(), JodaUtils.MAX_INSTANT);
+
+ // The filter on __time becomes the intervals of the scan.
+ testQuery(
+ "SELECT f1.dim1, f2.v\n"
+ + "FROM foo f1 LEFT JOIN (SELECT k, v FROM lookup.lookyloo GROUP BY k,
v) f2 ON f1.dim2 = f2.k\n"
+ + "WHERE f1.__time >= TIMESTAMP '2001-01-01 00:00:00'\n"
+ + "ORDER BY f2.v, f1.dim1",
+ ImmutableList.of(
+ new WindowOperatorQuery(
+ new QueryDataSource(
+ newScanQueryBuilder()
+ .dataSource(
+ join(
+ new TableDataSource(CalciteTests.DATASOURCE1),
+ new QueryDataSource(
+ GroupByQuery.builder()
+ .setDataSource(new
LookupDataSource("lookyloo"))
+
.setInterval(querySegmentSpec(Filtration.eternity()))
+
.setGranularity(Granularities.ALL)
+ .setDimensions(
+ new
DefaultDimensionSpec("k", "d0"),
+ new
DefaultDimensionSpec("v", "d1")
+ )
+
.setContext(QUERY_CONTEXT_DEFAULT)
+ .build()
+ ),
+ "j0.",
+ equalsCondition(makeColumnExpression("dim2"),
makeColumnExpression("j0.d0")),
+ JoinType.LEFT
+ )
+ )
+ .intervals(querySegmentSpec(interval))
+ .columns("dim1", "j0.d1")
+ .columnTypes(ColumnType.STRING, ColumnType.STRING)
+ .context(QUERY_CONTEXT_DEFAULT)
+ .build()
+ ),
+ new LegacySegmentSpec(interval),
+ QUERY_CONTEXT_DEFAULT,
+ RowSignature.builder().add("dim1",
ColumnType.STRING).add("j0.d1", ColumnType.STRING).build(),
+ ImmutableList.of(
+ OperatorFactoryBuilders.naiveSortOperator(
+ ColumnWithDirection.ascending("j0.d1"),
+ ColumnWithDirection.ascending("dim1")
+ ),
+
OperatorFactoryBuilders.scanOperatorFactoryBuilder().setProjectedColumns("dim1",
"j0.d1").build()
+ ),
+ null
+ )
+ ),
+ ImmutableList.of(
+ new Object[]{"abc", null},
+ new Object[]{"1", "xa"},
+ new Object[]{"def", "xabc"}
+ )
+ );
+ }
+
+ @DecoupledTestConfig(quidemReason =
QuidemTestCaseReason.JOIN_FILTER_LOCATIONS)
+ @Test
+ public void testJoinWithSubqueryOnLeftAndOuterTimeFilter()
+ {
+ // The filter on __time applies to the subquery's output, so it stays a
filter rather than becoming intervals.
+ testBuilder()
+ .sql(
+ "SELECT base.dim2, lookyloo.v\n"
+ + "FROM (SELECT __time, dim2 FROM foo ORDER BY __time LIMIT 10)
base\n"
+ + "LEFT JOIN lookup.lookyloo ON base.dim2 = lookyloo.k\n"
+ + "WHERE base.__time >= TIMESTAMP '2000-01-03 00:00:00'"
+ )
+ .expectedQueries(
+ ImmutableList.of(
+ newScanQueryBuilder()
+ .dataSource(
+ join(
+ new QueryDataSource(
+ newScanQueryBuilder()
+ .dataSource(CalciteTests.DATASOURCE1)
+
.intervals(querySegmentSpec(Filtration.eternity()))
+ .columns("__time", "dim2")
+ .columnTypes(ColumnType.LONG,
ColumnType.STRING)
+ .order(Order.ASCENDING)
+ .limit(10)
+ .context(QUERY_CONTEXT_DEFAULT)
+ .build()
+ ),
+ new LookupDataSource("lookyloo"),
+ "j0.",
+ equalsCondition(makeColumnExpression("dim2"),
makeColumnExpression("j0.k")),
Review Comment:
## CodeQL / Deprecated method or constructor invocation
Invoking [CalciteTestBase.makeColumnExpression](1) should be avoided because
it has been deprecated.
[Show more
details](https://github.com/apache/druid/security/code-scanning/12092)
##########
sql/src/test/java/org/apache/druid/sql/calcite/CalciteJoinQueryTest.java:
##########
@@ -6584,4 +6589,128 @@
)
);
}
+
+ @DecoupledTestConfig(quidemReason =
QuidemTestCaseReason.JOIN_FILTER_LOCATIONS)
+ @Test
+ public void testLeftJoinWithTimeFilterAndNonTimeOrderBy()
+ {
+ // Cannot vectorize direct queries on lookup tables.
+ cannotVectorize();
+
+ final Interval interval =
Intervals.utc(DateTimes.of("2001-01-01").getMillis(), JodaUtils.MAX_INSTANT);
+
+ // The filter on __time becomes the intervals of the scan.
+ testQuery(
+ "SELECT f1.dim1, f2.v\n"
+ + "FROM foo f1 LEFT JOIN (SELECT k, v FROM lookup.lookyloo GROUP BY k,
v) f2 ON f1.dim2 = f2.k\n"
+ + "WHERE f1.__time >= TIMESTAMP '2001-01-01 00:00:00'\n"
+ + "ORDER BY f2.v, f1.dim1",
+ ImmutableList.of(
+ new WindowOperatorQuery(
+ new QueryDataSource(
+ newScanQueryBuilder()
+ .dataSource(
+ join(
+ new TableDataSource(CalciteTests.DATASOURCE1),
+ new QueryDataSource(
+ GroupByQuery.builder()
+ .setDataSource(new
LookupDataSource("lookyloo"))
+
.setInterval(querySegmentSpec(Filtration.eternity()))
+
.setGranularity(Granularities.ALL)
+ .setDimensions(
+ new
DefaultDimensionSpec("k", "d0"),
+ new
DefaultDimensionSpec("v", "d1")
+ )
+
.setContext(QUERY_CONTEXT_DEFAULT)
+ .build()
+ ),
+ "j0.",
+ equalsCondition(makeColumnExpression("dim2"),
makeColumnExpression("j0.d0")),
+ JoinType.LEFT
+ )
+ )
+ .intervals(querySegmentSpec(interval))
+ .columns("dim1", "j0.d1")
+ .columnTypes(ColumnType.STRING, ColumnType.STRING)
+ .context(QUERY_CONTEXT_DEFAULT)
+ .build()
+ ),
+ new LegacySegmentSpec(interval),
+ QUERY_CONTEXT_DEFAULT,
+ RowSignature.builder().add("dim1",
ColumnType.STRING).add("j0.d1", ColumnType.STRING).build(),
+ ImmutableList.of(
+ OperatorFactoryBuilders.naiveSortOperator(
+ ColumnWithDirection.ascending("j0.d1"),
+ ColumnWithDirection.ascending("dim1")
+ ),
+
OperatorFactoryBuilders.scanOperatorFactoryBuilder().setProjectedColumns("dim1",
"j0.d1").build()
+ ),
+ null
+ )
+ ),
+ ImmutableList.of(
+ new Object[]{"abc", null},
+ new Object[]{"1", "xa"},
+ new Object[]{"def", "xabc"}
+ )
+ );
+ }
+
+ @DecoupledTestConfig(quidemReason =
QuidemTestCaseReason.JOIN_FILTER_LOCATIONS)
+ @Test
+ public void testJoinWithSubqueryOnLeftAndOuterTimeFilter()
+ {
+ // The filter on __time applies to the subquery's output, so it stays a
filter rather than becoming intervals.
+ testBuilder()
+ .sql(
+ "SELECT base.dim2, lookyloo.v\n"
+ + "FROM (SELECT __time, dim2 FROM foo ORDER BY __time LIMIT 10)
base\n"
+ + "LEFT JOIN lookup.lookyloo ON base.dim2 = lookyloo.k\n"
+ + "WHERE base.__time >= TIMESTAMP '2000-01-03 00:00:00'"
+ )
+ .expectedQueries(
+ ImmutableList.of(
+ newScanQueryBuilder()
+ .dataSource(
+ join(
+ new QueryDataSource(
+ newScanQueryBuilder()
+ .dataSource(CalciteTests.DATASOURCE1)
+
.intervals(querySegmentSpec(Filtration.eternity()))
+ .columns("__time", "dim2")
+ .columnTypes(ColumnType.LONG,
ColumnType.STRING)
+ .order(Order.ASCENDING)
+ .limit(10)
+ .context(QUERY_CONTEXT_DEFAULT)
+ .build()
+ ),
+ new LookupDataSource("lookyloo"),
+ "j0.",
+ equalsCondition(makeColumnExpression("dim2"),
makeColumnExpression("j0.k")),
Review Comment:
## CodeQL / Deprecated method or constructor invocation
Invoking [CalciteTestBase.makeColumnExpression](1) should be avoided because
it has been deprecated.
[Show more
details](https://github.com/apache/druid/security/code-scanning/12093)
##########
sql/src/test/java/org/apache/druid/sql/calcite/CalciteTimeConditionQueryTest.java:
##########
@@ -0,0 +1,569 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.druid.sql.calcite;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import org.apache.druid.error.DruidException;
+import org.apache.druid.error.DruidExceptionMatcher;
+import org.apache.druid.java.util.common.Intervals;
+import org.apache.druid.java.util.common.granularity.Granularities;
+import org.apache.druid.query.Druids;
+import org.apache.druid.query.GlobalTableDataSource;
+import org.apache.druid.query.LookupDataSource;
+import org.apache.druid.query.QueryContexts;
+import org.apache.druid.query.QueryDataSource;
+import org.apache.druid.query.TableDataSource;
+import org.apache.druid.query.UnionDataSource;
+import org.apache.druid.query.UnnestDataSource;
+import org.apache.druid.query.aggregation.CountAggregatorFactory;
+import org.apache.druid.query.aggregation.LongSumAggregatorFactory;
+import org.apache.druid.query.dimension.DefaultDimensionSpec;
+import org.apache.druid.query.groupby.GroupByQuery;
+import org.apache.druid.query.timeboundary.TimeBoundaryQuery;
+import org.apache.druid.segment.column.ColumnType;
+import org.apache.druid.segment.join.JoinType;
+import org.apache.druid.sql.calcite.NotYetSupported.Modes;
+import org.apache.druid.sql.calcite.filtration.Filtration;
+import org.apache.druid.sql.calcite.planner.PlannerContext;
+import org.apache.druid.sql.calcite.util.CalciteTests;
+import org.junit.jupiter.api.Test;
+
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Tests for the {@link PlannerContext#CTX_SQL_TIME_CONDITION} context
parameter.
+ */
+public class CalciteTimeConditionQueryTest extends BaseCalciteQueryTest
+{
+ // __time for foo is [2000-01-01, 2000-01-02, 2000-01-03, 2001-01-01,
2001-01-02, 2001-01-03]
+
+ @Test
+ public void testScan()
+ {
+ final Map<String, Object> context = makeContext("2000-01-02/2000-01-03,
2001-01-01/2001-01-02");
+
+ testBuilder()
+ .sql("SELECT __time, dim1 FROM foo")
+ .queryContext(context)
+ .expectedQueries(
+ ImmutableList.of(
+ newScanQueryBuilder()
+ .dataSource(CalciteTests.DATASOURCE1)
+ .intervals(
+ querySegmentSpec(
+ Intervals.of("2000-01-02/2000-01-03"),
+ Intervals.of("2001-01-01/2001-01-02")
+ )
+ )
+ .columns("__time", "dim1")
+ .columnTypes(ColumnType.LONG, ColumnType.STRING)
+ .context(context)
+ .build()
+ )
+ )
+ // Unordered, since MSQ does not guarantee the order of scan results
across segments.
+ .expectedResults(
+ new UnorderedResultsVerifier(
+ ImmutableList.of(
+ new Object[]{timestamp("2000-01-02"), "10.1"},
+ new Object[]{timestamp("2001-01-01"), "1"}
+ ),
+ ResultMatchMode.EQUALS,
+ null
+ )
+ )
+ .run();
+ }
+
+ @Test
+ public void testScanWithEmptyIntersection()
+ {
+ final Map<String, Object> context = makeContext("2000/2001");
+
+ testQuery(
+ "SELECT dim1 FROM foo WHERE __time >= TIMESTAMP '2001-01-01 00:00:00'",
+ context,
+ ImmutableList.of(
+ newScanQueryBuilder()
+ .dataSource(CalciteTests.DATASOURCE1)
+ .intervals(querySegmentSpec())
+ .columns("dim1")
+ .columnTypes(ColumnType.STRING)
+ .context(context)
+ .build()
+ ),
+ ImmutableList.of()
+ );
+ }
+
+ @Test
+ public void testTimeseriesWithTimeFilter()
+ {
+ final Map<String, Object> context = makeContext("2000/2001");
+
+ testQuery(
+ "SELECT COUNT(*) FROM foo WHERE __time >= TIMESTAMP '2000-01-02
00:00:00'",
+ context,
+ ImmutableList.of(
+ Druids.newTimeseriesQueryBuilder()
+ .dataSource(CalciteTests.DATASOURCE1)
+
.intervals(querySegmentSpec(Intervals.of("2000-01-02/2001-01-01")))
+ .granularity(Granularities.ALL)
+ .aggregators(aggregators(new CountAggregatorFactory("a0")))
+ .context(context)
+ .build()
+ ),
+ ImmutableList.of(
+ new Object[]{2L}
+ )
+ );
+ }
+
+ @Test
+ public void testGroupByWithArray()
+ {
+ final Map<String, Object> context =
makeContext(List.of("2001-01-01/2001-01-03", "2000-01-01/2000-01-02"));
+
+ testQuery(
+ "SELECT dim2, COUNT(*) FROM foo GROUP BY dim2",
+ context,
+ ImmutableList.of(
+ GroupByQuery.builder()
+ .setDataSource(CalciteTests.DATASOURCE1)
+ .setInterval(
+ querySegmentSpec(
+ Intervals.of("2000-01-01/2000-01-02"),
+ Intervals.of("2001-01-01/2001-01-03")
+ )
+ )
+ .setGranularity(Granularities.ALL)
+ .setDimensions(dimensions(new
DefaultDimensionSpec("dim2", "d0")))
+ .setAggregatorSpecs(aggregators(new
CountAggregatorFactory("a0")))
+ .setContext(context)
+ .build()
+ ),
+ ImmutableList.of(
+ new Object[]{"a", 2L},
+ new Object[]{"abc", 1L}
+ )
+ );
+ }
+
+ @Test
+ public void testTimeBoundary()
+ {
+ final Map<String, Object> context = ImmutableMap.<String, Object>builder()
+
.putAll(makeContext("2000/2001"))
+
.put(QueryContexts.TIME_BOUNDARY_PLANNING_KEY, true)
+ .build();
+
+ testQuery(
+ "SELECT MAX(__time) FROM foo",
+ context,
+ ImmutableList.of(
+ Druids.newTimeBoundaryQueryBuilder()
+ .dataSource(CalciteTests.DATASOURCE1)
+ .intervals(querySegmentSpec(Intervals.of("2000/2001")))
+ .bound(TimeBoundaryQuery.MAX_TIME)
+ .context(
+ ImmutableMap.<String, Object>builder()
+ .putAll(context)
+
.put(TimeBoundaryQuery.MAX_TIME_ARRAY_OUTPUT_NAME, "a0")
+ .build()
+ )
+ .build()
+ ),
+ ImmutableList.of(
+ new Object[]{timestamp("2000-01-03")}
+ )
+ );
+ }
+
+ @NotYetSupported(Modes.DD_UNION)
+ @Test
+ public void testUnionDataSource()
+ {
+ final Map<String, Object> context = makeContext("2000-01-01/2000-01-03");
+
+ testQuery(
+ "SELECT dim1, COUNT(*) FROM (SELECT * FROM foo UNION ALL SELECT * FROM
foo) GROUP BY dim1",
+ context,
+ ImmutableList.of(
+ GroupByQuery.builder()
+ .setDataSource(
+ new UnionDataSource(
+ ImmutableList.of(
+ new
TableDataSource(CalciteTests.DATASOURCE1),
+ new
TableDataSource(CalciteTests.DATASOURCE1)
+ )
+ )
+ )
+
.setInterval(querySegmentSpec(Intervals.of("2000-01-01/2000-01-03")))
+ .setGranularity(Granularities.ALL)
+ .setDimensions(dimensions(new
DefaultDimensionSpec("dim1", "d0")))
+ .setAggregatorSpecs(aggregators(new
CountAggregatorFactory("a0")))
+ .setContext(context)
+ .build()
+ ),
+ ImmutableList.of(
+ new Object[]{"", 2L},
+ new Object[]{"10.1", 2L}
+ )
+ );
+ }
+
+ @Test
+ public void testUnnest()
+ {
+ final Map<String, Object> context = makeContext("2000-01-01/2000-01-03");
+
+ testQuery(
+ "SELECT d3 FROM foo, UNNEST(MV_TO_ARRAY(dim3)) AS unnested (d3)",
+ context,
+ ImmutableList.of(
+ newScanQueryBuilder()
+ .dataSource(
+ UnnestDataSource.create(
+ new TableDataSource(CalciteTests.DATASOURCE1),
+ nestedExpressionVirtualColumn("j0.unnest", "\"dim3\"",
ColumnType.STRING),
+ null
+ )
+ )
+
.intervals(querySegmentSpec(Intervals.of("2000-01-01/2000-01-03")))
+ .columns("j0.unnest")
+ .columnTypes(ColumnType.STRING)
+ .context(context)
+ .build()
+ ),
+ ImmutableList.of(
+ new Object[]{"a"},
+ new Object[]{"b"},
+ new Object[]{"b"},
+ new Object[]{"c"}
+ )
+ );
+ }
+
+ @Test
+ public void testJoinWithLookup()
+ {
+ final Map<String, Object> context = makeContext("2001/2002");
+
+ testQuery(
+ "SELECT foo.dim1, lookyloo.v FROM foo INNER JOIN lookup.lookyloo ON
foo.dim2 = lookyloo.k",
+ context,
+ ImmutableList.of(
+ newScanQueryBuilder()
+ .dataSource(
+ join(
+ new TableDataSource(CalciteTests.DATASOURCE1),
+ new LookupDataSource("lookyloo"),
+ "j0.",
+ equalsCondition(makeColumnExpression("dim2"),
makeColumnExpression("j0.k")),
+ JoinType.INNER
+ )
+ )
+ .intervals(querySegmentSpec(Intervals.of("2001/2002")))
+ .columns("dim1", "j0.v")
+ .columnTypes(ColumnType.STRING, ColumnType.STRING)
+ .context(context)
+ .build()
+ ),
+ ImmutableList.of(
+ new Object[]{"1", "xa"},
+ new Object[]{"def", "xabc"}
+ )
+ );
+ }
+
+ @Test
+ public void testJoinWithTableSubquery()
+ {
+ final Map<String, Object> context = makeContext("2000/2001");
+
+ testQuery(
+ "SELECT f1.dim1, f2.dim2 FROM foo f1 INNER JOIN foo f2 ON f1.dim1 =
f2.dim1",
+ context,
+ ImmutableList.of(
+ newScanQueryBuilder()
+ .dataSource(
+ join(
+ new TableDataSource(CalciteTests.DATASOURCE1),
+ new QueryDataSource(
+ newScanQueryBuilder()
+ .dataSource(CalciteTests.DATASOURCE1)
+
.intervals(querySegmentSpec(Intervals.of("2000/2001")))
+ .columns("dim1", "dim2")
+ .columnTypes(ColumnType.STRING,
ColumnType.STRING)
+ .context(context)
+ .build()
+ ),
+ "j0.",
+ equalsCondition(makeColumnExpression("dim1"),
makeColumnExpression("j0.dim1")),
Review Comment:
## CodeQL / Deprecated method or constructor invocation
Invoking [CalciteTestBase.makeColumnExpression](1) should be avoided because
it has been deprecated.
[Show more
details](https://github.com/apache/druid/security/code-scanning/12096)
##########
sql/src/test/java/org/apache/druid/sql/calcite/CalciteTimeConditionQueryTest.java:
##########
@@ -0,0 +1,569 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.druid.sql.calcite;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import org.apache.druid.error.DruidException;
+import org.apache.druid.error.DruidExceptionMatcher;
+import org.apache.druid.java.util.common.Intervals;
+import org.apache.druid.java.util.common.granularity.Granularities;
+import org.apache.druid.query.Druids;
+import org.apache.druid.query.GlobalTableDataSource;
+import org.apache.druid.query.LookupDataSource;
+import org.apache.druid.query.QueryContexts;
+import org.apache.druid.query.QueryDataSource;
+import org.apache.druid.query.TableDataSource;
+import org.apache.druid.query.UnionDataSource;
+import org.apache.druid.query.UnnestDataSource;
+import org.apache.druid.query.aggregation.CountAggregatorFactory;
+import org.apache.druid.query.aggregation.LongSumAggregatorFactory;
+import org.apache.druid.query.dimension.DefaultDimensionSpec;
+import org.apache.druid.query.groupby.GroupByQuery;
+import org.apache.druid.query.timeboundary.TimeBoundaryQuery;
+import org.apache.druid.segment.column.ColumnType;
+import org.apache.druid.segment.join.JoinType;
+import org.apache.druid.sql.calcite.NotYetSupported.Modes;
+import org.apache.druid.sql.calcite.filtration.Filtration;
+import org.apache.druid.sql.calcite.planner.PlannerContext;
+import org.apache.druid.sql.calcite.util.CalciteTests;
+import org.junit.jupiter.api.Test;
+
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Tests for the {@link PlannerContext#CTX_SQL_TIME_CONDITION} context
parameter.
+ */
+public class CalciteTimeConditionQueryTest extends BaseCalciteQueryTest
+{
+ // __time for foo is [2000-01-01, 2000-01-02, 2000-01-03, 2001-01-01,
2001-01-02, 2001-01-03]
+
+ @Test
+ public void testScan()
+ {
+ final Map<String, Object> context = makeContext("2000-01-02/2000-01-03,
2001-01-01/2001-01-02");
+
+ testBuilder()
+ .sql("SELECT __time, dim1 FROM foo")
+ .queryContext(context)
+ .expectedQueries(
+ ImmutableList.of(
+ newScanQueryBuilder()
+ .dataSource(CalciteTests.DATASOURCE1)
+ .intervals(
+ querySegmentSpec(
+ Intervals.of("2000-01-02/2000-01-03"),
+ Intervals.of("2001-01-01/2001-01-02")
+ )
+ )
+ .columns("__time", "dim1")
+ .columnTypes(ColumnType.LONG, ColumnType.STRING)
+ .context(context)
+ .build()
+ )
+ )
+ // Unordered, since MSQ does not guarantee the order of scan results
across segments.
+ .expectedResults(
+ new UnorderedResultsVerifier(
+ ImmutableList.of(
+ new Object[]{timestamp("2000-01-02"), "10.1"},
+ new Object[]{timestamp("2001-01-01"), "1"}
+ ),
+ ResultMatchMode.EQUALS,
+ null
+ )
+ )
+ .run();
+ }
+
+ @Test
+ public void testScanWithEmptyIntersection()
+ {
+ final Map<String, Object> context = makeContext("2000/2001");
+
+ testQuery(
+ "SELECT dim1 FROM foo WHERE __time >= TIMESTAMP '2001-01-01 00:00:00'",
+ context,
+ ImmutableList.of(
+ newScanQueryBuilder()
+ .dataSource(CalciteTests.DATASOURCE1)
+ .intervals(querySegmentSpec())
+ .columns("dim1")
+ .columnTypes(ColumnType.STRING)
+ .context(context)
+ .build()
+ ),
+ ImmutableList.of()
+ );
+ }
+
+ @Test
+ public void testTimeseriesWithTimeFilter()
+ {
+ final Map<String, Object> context = makeContext("2000/2001");
+
+ testQuery(
+ "SELECT COUNT(*) FROM foo WHERE __time >= TIMESTAMP '2000-01-02
00:00:00'",
+ context,
+ ImmutableList.of(
+ Druids.newTimeseriesQueryBuilder()
+ .dataSource(CalciteTests.DATASOURCE1)
+
.intervals(querySegmentSpec(Intervals.of("2000-01-02/2001-01-01")))
+ .granularity(Granularities.ALL)
+ .aggregators(aggregators(new CountAggregatorFactory("a0")))
+ .context(context)
+ .build()
+ ),
+ ImmutableList.of(
+ new Object[]{2L}
+ )
+ );
+ }
+
+ @Test
+ public void testGroupByWithArray()
+ {
+ final Map<String, Object> context =
makeContext(List.of("2001-01-01/2001-01-03", "2000-01-01/2000-01-02"));
+
+ testQuery(
+ "SELECT dim2, COUNT(*) FROM foo GROUP BY dim2",
+ context,
+ ImmutableList.of(
+ GroupByQuery.builder()
+ .setDataSource(CalciteTests.DATASOURCE1)
+ .setInterval(
+ querySegmentSpec(
+ Intervals.of("2000-01-01/2000-01-02"),
+ Intervals.of("2001-01-01/2001-01-03")
+ )
+ )
+ .setGranularity(Granularities.ALL)
+ .setDimensions(dimensions(new
DefaultDimensionSpec("dim2", "d0")))
+ .setAggregatorSpecs(aggregators(new
CountAggregatorFactory("a0")))
+ .setContext(context)
+ .build()
+ ),
+ ImmutableList.of(
+ new Object[]{"a", 2L},
+ new Object[]{"abc", 1L}
+ )
+ );
+ }
+
+ @Test
+ public void testTimeBoundary()
+ {
+ final Map<String, Object> context = ImmutableMap.<String, Object>builder()
+
.putAll(makeContext("2000/2001"))
+
.put(QueryContexts.TIME_BOUNDARY_PLANNING_KEY, true)
+ .build();
+
+ testQuery(
+ "SELECT MAX(__time) FROM foo",
+ context,
+ ImmutableList.of(
+ Druids.newTimeBoundaryQueryBuilder()
+ .dataSource(CalciteTests.DATASOURCE1)
+ .intervals(querySegmentSpec(Intervals.of("2000/2001")))
+ .bound(TimeBoundaryQuery.MAX_TIME)
+ .context(
+ ImmutableMap.<String, Object>builder()
+ .putAll(context)
+
.put(TimeBoundaryQuery.MAX_TIME_ARRAY_OUTPUT_NAME, "a0")
+ .build()
+ )
+ .build()
+ ),
+ ImmutableList.of(
+ new Object[]{timestamp("2000-01-03")}
+ )
+ );
+ }
+
+ @NotYetSupported(Modes.DD_UNION)
+ @Test
+ public void testUnionDataSource()
+ {
+ final Map<String, Object> context = makeContext("2000-01-01/2000-01-03");
+
+ testQuery(
+ "SELECT dim1, COUNT(*) FROM (SELECT * FROM foo UNION ALL SELECT * FROM
foo) GROUP BY dim1",
+ context,
+ ImmutableList.of(
+ GroupByQuery.builder()
+ .setDataSource(
+ new UnionDataSource(
+ ImmutableList.of(
+ new
TableDataSource(CalciteTests.DATASOURCE1),
+ new
TableDataSource(CalciteTests.DATASOURCE1)
+ )
+ )
+ )
+
.setInterval(querySegmentSpec(Intervals.of("2000-01-01/2000-01-03")))
+ .setGranularity(Granularities.ALL)
+ .setDimensions(dimensions(new
DefaultDimensionSpec("dim1", "d0")))
+ .setAggregatorSpecs(aggregators(new
CountAggregatorFactory("a0")))
+ .setContext(context)
+ .build()
+ ),
+ ImmutableList.of(
+ new Object[]{"", 2L},
+ new Object[]{"10.1", 2L}
+ )
+ );
+ }
+
+ @Test
+ public void testUnnest()
+ {
+ final Map<String, Object> context = makeContext("2000-01-01/2000-01-03");
+
+ testQuery(
+ "SELECT d3 FROM foo, UNNEST(MV_TO_ARRAY(dim3)) AS unnested (d3)",
+ context,
+ ImmutableList.of(
+ newScanQueryBuilder()
+ .dataSource(
+ UnnestDataSource.create(
+ new TableDataSource(CalciteTests.DATASOURCE1),
+ nestedExpressionVirtualColumn("j0.unnest", "\"dim3\"",
ColumnType.STRING),
+ null
+ )
+ )
+
.intervals(querySegmentSpec(Intervals.of("2000-01-01/2000-01-03")))
+ .columns("j0.unnest")
+ .columnTypes(ColumnType.STRING)
+ .context(context)
+ .build()
+ ),
+ ImmutableList.of(
+ new Object[]{"a"},
+ new Object[]{"b"},
+ new Object[]{"b"},
+ new Object[]{"c"}
+ )
+ );
+ }
+
+ @Test
+ public void testJoinWithLookup()
+ {
+ final Map<String, Object> context = makeContext("2001/2002");
+
+ testQuery(
+ "SELECT foo.dim1, lookyloo.v FROM foo INNER JOIN lookup.lookyloo ON
foo.dim2 = lookyloo.k",
+ context,
+ ImmutableList.of(
+ newScanQueryBuilder()
+ .dataSource(
+ join(
+ new TableDataSource(CalciteTests.DATASOURCE1),
+ new LookupDataSource("lookyloo"),
+ "j0.",
+ equalsCondition(makeColumnExpression("dim2"),
makeColumnExpression("j0.k")),
+ JoinType.INNER
+ )
+ )
+ .intervals(querySegmentSpec(Intervals.of("2001/2002")))
+ .columns("dim1", "j0.v")
+ .columnTypes(ColumnType.STRING, ColumnType.STRING)
+ .context(context)
+ .build()
+ ),
+ ImmutableList.of(
+ new Object[]{"1", "xa"},
+ new Object[]{"def", "xabc"}
+ )
+ );
+ }
+
+ @Test
+ public void testJoinWithTableSubquery()
+ {
+ final Map<String, Object> context = makeContext("2000/2001");
+
+ testQuery(
+ "SELECT f1.dim1, f2.dim2 FROM foo f1 INNER JOIN foo f2 ON f1.dim1 =
f2.dim1",
+ context,
+ ImmutableList.of(
+ newScanQueryBuilder()
+ .dataSource(
+ join(
+ new TableDataSource(CalciteTests.DATASOURCE1),
+ new QueryDataSource(
+ newScanQueryBuilder()
+ .dataSource(CalciteTests.DATASOURCE1)
+
.intervals(querySegmentSpec(Intervals.of("2000/2001")))
+ .columns("dim1", "dim2")
+ .columnTypes(ColumnType.STRING,
ColumnType.STRING)
+ .context(context)
+ .build()
+ ),
+ "j0.",
+ equalsCondition(makeColumnExpression("dim1"),
makeColumnExpression("j0.dim1")),
Review Comment:
## CodeQL / Deprecated method or constructor invocation
Invoking [CalciteTestBase.makeColumnExpression](1) should be avoided because
it has been deprecated.
[Show more
details](https://github.com/apache/druid/security/code-scanning/12097)
##########
sql/src/test/java/org/apache/druid/sql/calcite/CalciteTimeConditionQueryTest.java:
##########
@@ -0,0 +1,569 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.druid.sql.calcite;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import org.apache.druid.error.DruidException;
+import org.apache.druid.error.DruidExceptionMatcher;
+import org.apache.druid.java.util.common.Intervals;
+import org.apache.druid.java.util.common.granularity.Granularities;
+import org.apache.druid.query.Druids;
+import org.apache.druid.query.GlobalTableDataSource;
+import org.apache.druid.query.LookupDataSource;
+import org.apache.druid.query.QueryContexts;
+import org.apache.druid.query.QueryDataSource;
+import org.apache.druid.query.TableDataSource;
+import org.apache.druid.query.UnionDataSource;
+import org.apache.druid.query.UnnestDataSource;
+import org.apache.druid.query.aggregation.CountAggregatorFactory;
+import org.apache.druid.query.aggregation.LongSumAggregatorFactory;
+import org.apache.druid.query.dimension.DefaultDimensionSpec;
+import org.apache.druid.query.groupby.GroupByQuery;
+import org.apache.druid.query.timeboundary.TimeBoundaryQuery;
+import org.apache.druid.segment.column.ColumnType;
+import org.apache.druid.segment.join.JoinType;
+import org.apache.druid.sql.calcite.NotYetSupported.Modes;
+import org.apache.druid.sql.calcite.filtration.Filtration;
+import org.apache.druid.sql.calcite.planner.PlannerContext;
+import org.apache.druid.sql.calcite.util.CalciteTests;
+import org.junit.jupiter.api.Test;
+
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Tests for the {@link PlannerContext#CTX_SQL_TIME_CONDITION} context
parameter.
+ */
+public class CalciteTimeConditionQueryTest extends BaseCalciteQueryTest
+{
+ // __time for foo is [2000-01-01, 2000-01-02, 2000-01-03, 2001-01-01,
2001-01-02, 2001-01-03]
+
+ @Test
+ public void testScan()
+ {
+ final Map<String, Object> context = makeContext("2000-01-02/2000-01-03,
2001-01-01/2001-01-02");
+
+ testBuilder()
+ .sql("SELECT __time, dim1 FROM foo")
+ .queryContext(context)
+ .expectedQueries(
+ ImmutableList.of(
+ newScanQueryBuilder()
+ .dataSource(CalciteTests.DATASOURCE1)
+ .intervals(
+ querySegmentSpec(
+ Intervals.of("2000-01-02/2000-01-03"),
+ Intervals.of("2001-01-01/2001-01-02")
+ )
+ )
+ .columns("__time", "dim1")
+ .columnTypes(ColumnType.LONG, ColumnType.STRING)
+ .context(context)
+ .build()
+ )
+ )
+ // Unordered, since MSQ does not guarantee the order of scan results
across segments.
+ .expectedResults(
+ new UnorderedResultsVerifier(
+ ImmutableList.of(
+ new Object[]{timestamp("2000-01-02"), "10.1"},
+ new Object[]{timestamp("2001-01-01"), "1"}
+ ),
+ ResultMatchMode.EQUALS,
+ null
+ )
+ )
+ .run();
+ }
+
+ @Test
+ public void testScanWithEmptyIntersection()
+ {
+ final Map<String, Object> context = makeContext("2000/2001");
+
+ testQuery(
+ "SELECT dim1 FROM foo WHERE __time >= TIMESTAMP '2001-01-01 00:00:00'",
+ context,
+ ImmutableList.of(
+ newScanQueryBuilder()
+ .dataSource(CalciteTests.DATASOURCE1)
+ .intervals(querySegmentSpec())
+ .columns("dim1")
+ .columnTypes(ColumnType.STRING)
+ .context(context)
+ .build()
+ ),
+ ImmutableList.of()
+ );
+ }
+
+ @Test
+ public void testTimeseriesWithTimeFilter()
+ {
+ final Map<String, Object> context = makeContext("2000/2001");
+
+ testQuery(
+ "SELECT COUNT(*) FROM foo WHERE __time >= TIMESTAMP '2000-01-02
00:00:00'",
+ context,
+ ImmutableList.of(
+ Druids.newTimeseriesQueryBuilder()
+ .dataSource(CalciteTests.DATASOURCE1)
+
.intervals(querySegmentSpec(Intervals.of("2000-01-02/2001-01-01")))
+ .granularity(Granularities.ALL)
+ .aggregators(aggregators(new CountAggregatorFactory("a0")))
+ .context(context)
+ .build()
+ ),
+ ImmutableList.of(
+ new Object[]{2L}
+ )
+ );
+ }
+
+ @Test
+ public void testGroupByWithArray()
+ {
+ final Map<String, Object> context =
makeContext(List.of("2001-01-01/2001-01-03", "2000-01-01/2000-01-02"));
+
+ testQuery(
+ "SELECT dim2, COUNT(*) FROM foo GROUP BY dim2",
+ context,
+ ImmutableList.of(
+ GroupByQuery.builder()
+ .setDataSource(CalciteTests.DATASOURCE1)
+ .setInterval(
+ querySegmentSpec(
+ Intervals.of("2000-01-01/2000-01-02"),
+ Intervals.of("2001-01-01/2001-01-03")
+ )
+ )
+ .setGranularity(Granularities.ALL)
+ .setDimensions(dimensions(new
DefaultDimensionSpec("dim2", "d0")))
+ .setAggregatorSpecs(aggregators(new
CountAggregatorFactory("a0")))
+ .setContext(context)
+ .build()
+ ),
+ ImmutableList.of(
+ new Object[]{"a", 2L},
+ new Object[]{"abc", 1L}
+ )
+ );
+ }
+
+ @Test
+ public void testTimeBoundary()
+ {
+ final Map<String, Object> context = ImmutableMap.<String, Object>builder()
+
.putAll(makeContext("2000/2001"))
+
.put(QueryContexts.TIME_BOUNDARY_PLANNING_KEY, true)
+ .build();
+
+ testQuery(
+ "SELECT MAX(__time) FROM foo",
+ context,
+ ImmutableList.of(
+ Druids.newTimeBoundaryQueryBuilder()
+ .dataSource(CalciteTests.DATASOURCE1)
+ .intervals(querySegmentSpec(Intervals.of("2000/2001")))
+ .bound(TimeBoundaryQuery.MAX_TIME)
+ .context(
+ ImmutableMap.<String, Object>builder()
+ .putAll(context)
+
.put(TimeBoundaryQuery.MAX_TIME_ARRAY_OUTPUT_NAME, "a0")
+ .build()
+ )
+ .build()
+ ),
+ ImmutableList.of(
+ new Object[]{timestamp("2000-01-03")}
+ )
+ );
+ }
+
+ @NotYetSupported(Modes.DD_UNION)
+ @Test
+ public void testUnionDataSource()
+ {
+ final Map<String, Object> context = makeContext("2000-01-01/2000-01-03");
+
+ testQuery(
+ "SELECT dim1, COUNT(*) FROM (SELECT * FROM foo UNION ALL SELECT * FROM
foo) GROUP BY dim1",
+ context,
+ ImmutableList.of(
+ GroupByQuery.builder()
+ .setDataSource(
+ new UnionDataSource(
+ ImmutableList.of(
+ new
TableDataSource(CalciteTests.DATASOURCE1),
+ new
TableDataSource(CalciteTests.DATASOURCE1)
+ )
+ )
+ )
+
.setInterval(querySegmentSpec(Intervals.of("2000-01-01/2000-01-03")))
+ .setGranularity(Granularities.ALL)
+ .setDimensions(dimensions(new
DefaultDimensionSpec("dim1", "d0")))
+ .setAggregatorSpecs(aggregators(new
CountAggregatorFactory("a0")))
+ .setContext(context)
+ .build()
+ ),
+ ImmutableList.of(
+ new Object[]{"", 2L},
+ new Object[]{"10.1", 2L}
+ )
+ );
+ }
+
+ @Test
+ public void testUnnest()
+ {
+ final Map<String, Object> context = makeContext("2000-01-01/2000-01-03");
+
+ testQuery(
+ "SELECT d3 FROM foo, UNNEST(MV_TO_ARRAY(dim3)) AS unnested (d3)",
+ context,
+ ImmutableList.of(
+ newScanQueryBuilder()
+ .dataSource(
+ UnnestDataSource.create(
+ new TableDataSource(CalciteTests.DATASOURCE1),
+ nestedExpressionVirtualColumn("j0.unnest", "\"dim3\"",
ColumnType.STRING),
+ null
+ )
+ )
+
.intervals(querySegmentSpec(Intervals.of("2000-01-01/2000-01-03")))
+ .columns("j0.unnest")
+ .columnTypes(ColumnType.STRING)
+ .context(context)
+ .build()
+ ),
+ ImmutableList.of(
+ new Object[]{"a"},
+ new Object[]{"b"},
+ new Object[]{"b"},
+ new Object[]{"c"}
+ )
+ );
+ }
+
+ @Test
+ public void testJoinWithLookup()
+ {
+ final Map<String, Object> context = makeContext("2001/2002");
+
+ testQuery(
+ "SELECT foo.dim1, lookyloo.v FROM foo INNER JOIN lookup.lookyloo ON
foo.dim2 = lookyloo.k",
+ context,
+ ImmutableList.of(
+ newScanQueryBuilder()
+ .dataSource(
+ join(
+ new TableDataSource(CalciteTests.DATASOURCE1),
+ new LookupDataSource("lookyloo"),
+ "j0.",
+ equalsCondition(makeColumnExpression("dim2"),
makeColumnExpression("j0.k")),
Review Comment:
## CodeQL / Deprecated method or constructor invocation
Invoking [CalciteTestBase.makeColumnExpression](1) should be avoided because
it has been deprecated.
[Show more
details](https://github.com/apache/druid/security/code-scanning/12095)
##########
sql/src/test/java/org/apache/druid/sql/calcite/CalciteTimeConditionQueryTest.java:
##########
@@ -0,0 +1,569 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.druid.sql.calcite;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import org.apache.druid.error.DruidException;
+import org.apache.druid.error.DruidExceptionMatcher;
+import org.apache.druid.java.util.common.Intervals;
+import org.apache.druid.java.util.common.granularity.Granularities;
+import org.apache.druid.query.Druids;
+import org.apache.druid.query.GlobalTableDataSource;
+import org.apache.druid.query.LookupDataSource;
+import org.apache.druid.query.QueryContexts;
+import org.apache.druid.query.QueryDataSource;
+import org.apache.druid.query.TableDataSource;
+import org.apache.druid.query.UnionDataSource;
+import org.apache.druid.query.UnnestDataSource;
+import org.apache.druid.query.aggregation.CountAggregatorFactory;
+import org.apache.druid.query.aggregation.LongSumAggregatorFactory;
+import org.apache.druid.query.dimension.DefaultDimensionSpec;
+import org.apache.druid.query.groupby.GroupByQuery;
+import org.apache.druid.query.timeboundary.TimeBoundaryQuery;
+import org.apache.druid.segment.column.ColumnType;
+import org.apache.druid.segment.join.JoinType;
+import org.apache.druid.sql.calcite.NotYetSupported.Modes;
+import org.apache.druid.sql.calcite.filtration.Filtration;
+import org.apache.druid.sql.calcite.planner.PlannerContext;
+import org.apache.druid.sql.calcite.util.CalciteTests;
+import org.junit.jupiter.api.Test;
+
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Tests for the {@link PlannerContext#CTX_SQL_TIME_CONDITION} context
parameter.
+ */
+public class CalciteTimeConditionQueryTest extends BaseCalciteQueryTest
+{
+ // __time for foo is [2000-01-01, 2000-01-02, 2000-01-03, 2001-01-01,
2001-01-02, 2001-01-03]
+
+ @Test
+ public void testScan()
+ {
+ final Map<String, Object> context = makeContext("2000-01-02/2000-01-03,
2001-01-01/2001-01-02");
+
+ testBuilder()
+ .sql("SELECT __time, dim1 FROM foo")
+ .queryContext(context)
+ .expectedQueries(
+ ImmutableList.of(
+ newScanQueryBuilder()
+ .dataSource(CalciteTests.DATASOURCE1)
+ .intervals(
+ querySegmentSpec(
+ Intervals.of("2000-01-02/2000-01-03"),
+ Intervals.of("2001-01-01/2001-01-02")
+ )
+ )
+ .columns("__time", "dim1")
+ .columnTypes(ColumnType.LONG, ColumnType.STRING)
+ .context(context)
+ .build()
+ )
+ )
+ // Unordered, since MSQ does not guarantee the order of scan results
across segments.
+ .expectedResults(
+ new UnorderedResultsVerifier(
+ ImmutableList.of(
+ new Object[]{timestamp("2000-01-02"), "10.1"},
+ new Object[]{timestamp("2001-01-01"), "1"}
+ ),
+ ResultMatchMode.EQUALS,
+ null
+ )
+ )
+ .run();
+ }
+
+ @Test
+ public void testScanWithEmptyIntersection()
+ {
+ final Map<String, Object> context = makeContext("2000/2001");
+
+ testQuery(
+ "SELECT dim1 FROM foo WHERE __time >= TIMESTAMP '2001-01-01 00:00:00'",
+ context,
+ ImmutableList.of(
+ newScanQueryBuilder()
+ .dataSource(CalciteTests.DATASOURCE1)
+ .intervals(querySegmentSpec())
+ .columns("dim1")
+ .columnTypes(ColumnType.STRING)
+ .context(context)
+ .build()
+ ),
+ ImmutableList.of()
+ );
+ }
+
+ @Test
+ public void testTimeseriesWithTimeFilter()
+ {
+ final Map<String, Object> context = makeContext("2000/2001");
+
+ testQuery(
+ "SELECT COUNT(*) FROM foo WHERE __time >= TIMESTAMP '2000-01-02
00:00:00'",
+ context,
+ ImmutableList.of(
+ Druids.newTimeseriesQueryBuilder()
+ .dataSource(CalciteTests.DATASOURCE1)
+
.intervals(querySegmentSpec(Intervals.of("2000-01-02/2001-01-01")))
+ .granularity(Granularities.ALL)
+ .aggregators(aggregators(new CountAggregatorFactory("a0")))
+ .context(context)
+ .build()
+ ),
+ ImmutableList.of(
+ new Object[]{2L}
+ )
+ );
+ }
+
+ @Test
+ public void testGroupByWithArray()
+ {
+ final Map<String, Object> context =
makeContext(List.of("2001-01-01/2001-01-03", "2000-01-01/2000-01-02"));
+
+ testQuery(
+ "SELECT dim2, COUNT(*) FROM foo GROUP BY dim2",
+ context,
+ ImmutableList.of(
+ GroupByQuery.builder()
+ .setDataSource(CalciteTests.DATASOURCE1)
+ .setInterval(
+ querySegmentSpec(
+ Intervals.of("2000-01-01/2000-01-02"),
+ Intervals.of("2001-01-01/2001-01-03")
+ )
+ )
+ .setGranularity(Granularities.ALL)
+ .setDimensions(dimensions(new
DefaultDimensionSpec("dim2", "d0")))
+ .setAggregatorSpecs(aggregators(new
CountAggregatorFactory("a0")))
+ .setContext(context)
+ .build()
+ ),
+ ImmutableList.of(
+ new Object[]{"a", 2L},
+ new Object[]{"abc", 1L}
+ )
+ );
+ }
+
+ @Test
+ public void testTimeBoundary()
+ {
+ final Map<String, Object> context = ImmutableMap.<String, Object>builder()
+
.putAll(makeContext("2000/2001"))
+
.put(QueryContexts.TIME_BOUNDARY_PLANNING_KEY, true)
+ .build();
+
+ testQuery(
+ "SELECT MAX(__time) FROM foo",
+ context,
+ ImmutableList.of(
+ Druids.newTimeBoundaryQueryBuilder()
+ .dataSource(CalciteTests.DATASOURCE1)
+ .intervals(querySegmentSpec(Intervals.of("2000/2001")))
+ .bound(TimeBoundaryQuery.MAX_TIME)
+ .context(
+ ImmutableMap.<String, Object>builder()
+ .putAll(context)
+
.put(TimeBoundaryQuery.MAX_TIME_ARRAY_OUTPUT_NAME, "a0")
+ .build()
+ )
+ .build()
+ ),
+ ImmutableList.of(
+ new Object[]{timestamp("2000-01-03")}
+ )
+ );
+ }
+
+ @NotYetSupported(Modes.DD_UNION)
+ @Test
+ public void testUnionDataSource()
+ {
+ final Map<String, Object> context = makeContext("2000-01-01/2000-01-03");
+
+ testQuery(
+ "SELECT dim1, COUNT(*) FROM (SELECT * FROM foo UNION ALL SELECT * FROM
foo) GROUP BY dim1",
+ context,
+ ImmutableList.of(
+ GroupByQuery.builder()
+ .setDataSource(
+ new UnionDataSource(
+ ImmutableList.of(
+ new
TableDataSource(CalciteTests.DATASOURCE1),
+ new
TableDataSource(CalciteTests.DATASOURCE1)
+ )
+ )
+ )
+
.setInterval(querySegmentSpec(Intervals.of("2000-01-01/2000-01-03")))
+ .setGranularity(Granularities.ALL)
+ .setDimensions(dimensions(new
DefaultDimensionSpec("dim1", "d0")))
+ .setAggregatorSpecs(aggregators(new
CountAggregatorFactory("a0")))
+ .setContext(context)
+ .build()
+ ),
+ ImmutableList.of(
+ new Object[]{"", 2L},
+ new Object[]{"10.1", 2L}
+ )
+ );
+ }
+
+ @Test
+ public void testUnnest()
+ {
+ final Map<String, Object> context = makeContext("2000-01-01/2000-01-03");
+
+ testQuery(
+ "SELECT d3 FROM foo, UNNEST(MV_TO_ARRAY(dim3)) AS unnested (d3)",
+ context,
+ ImmutableList.of(
+ newScanQueryBuilder()
+ .dataSource(
+ UnnestDataSource.create(
+ new TableDataSource(CalciteTests.DATASOURCE1),
+ nestedExpressionVirtualColumn("j0.unnest", "\"dim3\"",
ColumnType.STRING),
+ null
+ )
+ )
+
.intervals(querySegmentSpec(Intervals.of("2000-01-01/2000-01-03")))
+ .columns("j0.unnest")
+ .columnTypes(ColumnType.STRING)
+ .context(context)
+ .build()
+ ),
+ ImmutableList.of(
+ new Object[]{"a"},
+ new Object[]{"b"},
+ new Object[]{"b"},
+ new Object[]{"c"}
+ )
+ );
+ }
+
+ @Test
+ public void testJoinWithLookup()
+ {
+ final Map<String, Object> context = makeContext("2001/2002");
+
+ testQuery(
+ "SELECT foo.dim1, lookyloo.v FROM foo INNER JOIN lookup.lookyloo ON
foo.dim2 = lookyloo.k",
+ context,
+ ImmutableList.of(
+ newScanQueryBuilder()
+ .dataSource(
+ join(
+ new TableDataSource(CalciteTests.DATASOURCE1),
+ new LookupDataSource("lookyloo"),
+ "j0.",
+ equalsCondition(makeColumnExpression("dim2"),
makeColumnExpression("j0.k")),
+ JoinType.INNER
+ )
+ )
+ .intervals(querySegmentSpec(Intervals.of("2001/2002")))
+ .columns("dim1", "j0.v")
+ .columnTypes(ColumnType.STRING, ColumnType.STRING)
+ .context(context)
+ .build()
+ ),
+ ImmutableList.of(
+ new Object[]{"1", "xa"},
+ new Object[]{"def", "xabc"}
+ )
+ );
+ }
+
+ @Test
+ public void testJoinWithTableSubquery()
+ {
+ final Map<String, Object> context = makeContext("2000/2001");
+
+ testQuery(
+ "SELECT f1.dim1, f2.dim2 FROM foo f1 INNER JOIN foo f2 ON f1.dim1 =
f2.dim1",
+ context,
+ ImmutableList.of(
+ newScanQueryBuilder()
+ .dataSource(
+ join(
+ new TableDataSource(CalciteTests.DATASOURCE1),
+ new QueryDataSource(
+ newScanQueryBuilder()
+ .dataSource(CalciteTests.DATASOURCE1)
+
.intervals(querySegmentSpec(Intervals.of("2000/2001")))
+ .columns("dim1", "dim2")
+ .columnTypes(ColumnType.STRING,
ColumnType.STRING)
+ .context(context)
+ .build()
+ ),
+ "j0.",
+ equalsCondition(makeColumnExpression("dim1"),
makeColumnExpression("j0.dim1")),
+ JoinType.INNER
+ )
+ )
+ .intervals(querySegmentSpec(Intervals.of("2000/2001")))
+ .columns("dim1", "j0.dim2")
+ .columnTypes(ColumnType.STRING, ColumnType.STRING)
+ .context(context)
+ .build()
+ ),
+ ImmutableList.of(
+ new Object[]{"", "a"},
+ new Object[]{"10.1", null},
+ new Object[]{"2", ""}
+ )
+ );
+ }
+
+ @Test
+ public void testJoinWithBroadcastTable()
+ {
+ // Cannot vectorize JOIN operator.
+ cannotVectorize();
+
+ final Map<String, Object> context = makeContext("2000-01-01/2001-01-02");
+
+ // The broadcast table on the right-hand side is read through a subquery,
so the time condition applies to it.
+ testQuery(
+ "SELECT broadcast.dim4, COUNT(*)\n"
+ + "FROM numfoo INNER JOIN broadcast ON numfoo.dim4 = broadcast.dim4\n"
+ + "GROUP BY 1",
+ context,
+ ImmutableList.of(
+ GroupByQuery.builder()
+ .setDataSource(
+ join(
+ new TableDataSource(CalciteTests.DATASOURCE3),
+ new QueryDataSource(
+ newScanQueryBuilder()
+ .dataSource(new
GlobalTableDataSource(CalciteTests.BROADCAST_DATASOURCE))
+
.intervals(querySegmentSpec(Intervals.of("2000-01-01/2001-01-02")))
+ .columns("dim4")
+ .columnTypes(ColumnType.STRING)
+ .context(context)
+ .build()
+ ),
+ "j0.",
+ equalsCondition(makeColumnExpression("dim4"),
makeColumnExpression("j0.dim4")),
Review Comment:
## CodeQL / Deprecated method or constructor invocation
Invoking [CalciteTestBase.makeColumnExpression](1) should be avoided because
it has been deprecated.
[Show more
details](https://github.com/apache/druid/security/code-scanning/12098)
##########
sql/src/test/java/org/apache/druid/sql/calcite/CalciteTimeConditionQueryTest.java:
##########
@@ -0,0 +1,569 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.druid.sql.calcite;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import org.apache.druid.error.DruidException;
+import org.apache.druid.error.DruidExceptionMatcher;
+import org.apache.druid.java.util.common.Intervals;
+import org.apache.druid.java.util.common.granularity.Granularities;
+import org.apache.druid.query.Druids;
+import org.apache.druid.query.GlobalTableDataSource;
+import org.apache.druid.query.LookupDataSource;
+import org.apache.druid.query.QueryContexts;
+import org.apache.druid.query.QueryDataSource;
+import org.apache.druid.query.TableDataSource;
+import org.apache.druid.query.UnionDataSource;
+import org.apache.druid.query.UnnestDataSource;
+import org.apache.druid.query.aggregation.CountAggregatorFactory;
+import org.apache.druid.query.aggregation.LongSumAggregatorFactory;
+import org.apache.druid.query.dimension.DefaultDimensionSpec;
+import org.apache.druid.query.groupby.GroupByQuery;
+import org.apache.druid.query.timeboundary.TimeBoundaryQuery;
+import org.apache.druid.segment.column.ColumnType;
+import org.apache.druid.segment.join.JoinType;
+import org.apache.druid.sql.calcite.NotYetSupported.Modes;
+import org.apache.druid.sql.calcite.filtration.Filtration;
+import org.apache.druid.sql.calcite.planner.PlannerContext;
+import org.apache.druid.sql.calcite.util.CalciteTests;
+import org.junit.jupiter.api.Test;
+
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Tests for the {@link PlannerContext#CTX_SQL_TIME_CONDITION} context
parameter.
+ */
+public class CalciteTimeConditionQueryTest extends BaseCalciteQueryTest
+{
+ // __time for foo is [2000-01-01, 2000-01-02, 2000-01-03, 2001-01-01,
2001-01-02, 2001-01-03]
+
+ @Test
+ public void testScan()
+ {
+ final Map<String, Object> context = makeContext("2000-01-02/2000-01-03,
2001-01-01/2001-01-02");
+
+ testBuilder()
+ .sql("SELECT __time, dim1 FROM foo")
+ .queryContext(context)
+ .expectedQueries(
+ ImmutableList.of(
+ newScanQueryBuilder()
+ .dataSource(CalciteTests.DATASOURCE1)
+ .intervals(
+ querySegmentSpec(
+ Intervals.of("2000-01-02/2000-01-03"),
+ Intervals.of("2001-01-01/2001-01-02")
+ )
+ )
+ .columns("__time", "dim1")
+ .columnTypes(ColumnType.LONG, ColumnType.STRING)
+ .context(context)
+ .build()
+ )
+ )
+ // Unordered, since MSQ does not guarantee the order of scan results
across segments.
+ .expectedResults(
+ new UnorderedResultsVerifier(
+ ImmutableList.of(
+ new Object[]{timestamp("2000-01-02"), "10.1"},
+ new Object[]{timestamp("2001-01-01"), "1"}
+ ),
+ ResultMatchMode.EQUALS,
+ null
+ )
+ )
+ .run();
+ }
+
+ @Test
+ public void testScanWithEmptyIntersection()
+ {
+ final Map<String, Object> context = makeContext("2000/2001");
+
+ testQuery(
+ "SELECT dim1 FROM foo WHERE __time >= TIMESTAMP '2001-01-01 00:00:00'",
+ context,
+ ImmutableList.of(
+ newScanQueryBuilder()
+ .dataSource(CalciteTests.DATASOURCE1)
+ .intervals(querySegmentSpec())
+ .columns("dim1")
+ .columnTypes(ColumnType.STRING)
+ .context(context)
+ .build()
+ ),
+ ImmutableList.of()
+ );
+ }
+
+ @Test
+ public void testTimeseriesWithTimeFilter()
+ {
+ final Map<String, Object> context = makeContext("2000/2001");
+
+ testQuery(
+ "SELECT COUNT(*) FROM foo WHERE __time >= TIMESTAMP '2000-01-02
00:00:00'",
+ context,
+ ImmutableList.of(
+ Druids.newTimeseriesQueryBuilder()
+ .dataSource(CalciteTests.DATASOURCE1)
+
.intervals(querySegmentSpec(Intervals.of("2000-01-02/2001-01-01")))
+ .granularity(Granularities.ALL)
+ .aggregators(aggregators(new CountAggregatorFactory("a0")))
+ .context(context)
+ .build()
+ ),
+ ImmutableList.of(
+ new Object[]{2L}
+ )
+ );
+ }
+
+ @Test
+ public void testGroupByWithArray()
+ {
+ final Map<String, Object> context =
makeContext(List.of("2001-01-01/2001-01-03", "2000-01-01/2000-01-02"));
+
+ testQuery(
+ "SELECT dim2, COUNT(*) FROM foo GROUP BY dim2",
+ context,
+ ImmutableList.of(
+ GroupByQuery.builder()
+ .setDataSource(CalciteTests.DATASOURCE1)
+ .setInterval(
+ querySegmentSpec(
+ Intervals.of("2000-01-01/2000-01-02"),
+ Intervals.of("2001-01-01/2001-01-03")
+ )
+ )
+ .setGranularity(Granularities.ALL)
+ .setDimensions(dimensions(new
DefaultDimensionSpec("dim2", "d0")))
+ .setAggregatorSpecs(aggregators(new
CountAggregatorFactory("a0")))
+ .setContext(context)
+ .build()
+ ),
+ ImmutableList.of(
+ new Object[]{"a", 2L},
+ new Object[]{"abc", 1L}
+ )
+ );
+ }
+
+ @Test
+ public void testTimeBoundary()
+ {
+ final Map<String, Object> context = ImmutableMap.<String, Object>builder()
+
.putAll(makeContext("2000/2001"))
+
.put(QueryContexts.TIME_BOUNDARY_PLANNING_KEY, true)
+ .build();
+
+ testQuery(
+ "SELECT MAX(__time) FROM foo",
+ context,
+ ImmutableList.of(
+ Druids.newTimeBoundaryQueryBuilder()
+ .dataSource(CalciteTests.DATASOURCE1)
+ .intervals(querySegmentSpec(Intervals.of("2000/2001")))
+ .bound(TimeBoundaryQuery.MAX_TIME)
+ .context(
+ ImmutableMap.<String, Object>builder()
+ .putAll(context)
+
.put(TimeBoundaryQuery.MAX_TIME_ARRAY_OUTPUT_NAME, "a0")
+ .build()
+ )
+ .build()
+ ),
+ ImmutableList.of(
+ new Object[]{timestamp("2000-01-03")}
+ )
+ );
+ }
+
+ @NotYetSupported(Modes.DD_UNION)
+ @Test
+ public void testUnionDataSource()
+ {
+ final Map<String, Object> context = makeContext("2000-01-01/2000-01-03");
+
+ testQuery(
+ "SELECT dim1, COUNT(*) FROM (SELECT * FROM foo UNION ALL SELECT * FROM
foo) GROUP BY dim1",
+ context,
+ ImmutableList.of(
+ GroupByQuery.builder()
+ .setDataSource(
+ new UnionDataSource(
+ ImmutableList.of(
+ new
TableDataSource(CalciteTests.DATASOURCE1),
+ new
TableDataSource(CalciteTests.DATASOURCE1)
+ )
+ )
+ )
+
.setInterval(querySegmentSpec(Intervals.of("2000-01-01/2000-01-03")))
+ .setGranularity(Granularities.ALL)
+ .setDimensions(dimensions(new
DefaultDimensionSpec("dim1", "d0")))
+ .setAggregatorSpecs(aggregators(new
CountAggregatorFactory("a0")))
+ .setContext(context)
+ .build()
+ ),
+ ImmutableList.of(
+ new Object[]{"", 2L},
+ new Object[]{"10.1", 2L}
+ )
+ );
+ }
+
+ @Test
+ public void testUnnest()
+ {
+ final Map<String, Object> context = makeContext("2000-01-01/2000-01-03");
+
+ testQuery(
+ "SELECT d3 FROM foo, UNNEST(MV_TO_ARRAY(dim3)) AS unnested (d3)",
+ context,
+ ImmutableList.of(
+ newScanQueryBuilder()
+ .dataSource(
+ UnnestDataSource.create(
+ new TableDataSource(CalciteTests.DATASOURCE1),
+ nestedExpressionVirtualColumn("j0.unnest", "\"dim3\"",
ColumnType.STRING),
+ null
+ )
+ )
+
.intervals(querySegmentSpec(Intervals.of("2000-01-01/2000-01-03")))
+ .columns("j0.unnest")
+ .columnTypes(ColumnType.STRING)
+ .context(context)
+ .build()
+ ),
+ ImmutableList.of(
+ new Object[]{"a"},
+ new Object[]{"b"},
+ new Object[]{"b"},
+ new Object[]{"c"}
+ )
+ );
+ }
+
+ @Test
+ public void testJoinWithLookup()
+ {
+ final Map<String, Object> context = makeContext("2001/2002");
+
+ testQuery(
+ "SELECT foo.dim1, lookyloo.v FROM foo INNER JOIN lookup.lookyloo ON
foo.dim2 = lookyloo.k",
+ context,
+ ImmutableList.of(
+ newScanQueryBuilder()
+ .dataSource(
+ join(
+ new TableDataSource(CalciteTests.DATASOURCE1),
+ new LookupDataSource("lookyloo"),
+ "j0.",
+ equalsCondition(makeColumnExpression("dim2"),
makeColumnExpression("j0.k")),
+ JoinType.INNER
+ )
+ )
+ .intervals(querySegmentSpec(Intervals.of("2001/2002")))
+ .columns("dim1", "j0.v")
+ .columnTypes(ColumnType.STRING, ColumnType.STRING)
+ .context(context)
+ .build()
+ ),
+ ImmutableList.of(
+ new Object[]{"1", "xa"},
+ new Object[]{"def", "xabc"}
+ )
+ );
+ }
+
+ @Test
+ public void testJoinWithTableSubquery()
+ {
+ final Map<String, Object> context = makeContext("2000/2001");
+
+ testQuery(
+ "SELECT f1.dim1, f2.dim2 FROM foo f1 INNER JOIN foo f2 ON f1.dim1 =
f2.dim1",
+ context,
+ ImmutableList.of(
+ newScanQueryBuilder()
+ .dataSource(
+ join(
+ new TableDataSource(CalciteTests.DATASOURCE1),
+ new QueryDataSource(
+ newScanQueryBuilder()
+ .dataSource(CalciteTests.DATASOURCE1)
+
.intervals(querySegmentSpec(Intervals.of("2000/2001")))
+ .columns("dim1", "dim2")
+ .columnTypes(ColumnType.STRING,
ColumnType.STRING)
+ .context(context)
+ .build()
+ ),
+ "j0.",
+ equalsCondition(makeColumnExpression("dim1"),
makeColumnExpression("j0.dim1")),
+ JoinType.INNER
+ )
+ )
+ .intervals(querySegmentSpec(Intervals.of("2000/2001")))
+ .columns("dim1", "j0.dim2")
+ .columnTypes(ColumnType.STRING, ColumnType.STRING)
+ .context(context)
+ .build()
+ ),
+ ImmutableList.of(
+ new Object[]{"", "a"},
+ new Object[]{"10.1", null},
+ new Object[]{"2", ""}
+ )
+ );
+ }
+
+ @Test
+ public void testJoinWithBroadcastTable()
+ {
+ // Cannot vectorize JOIN operator.
+ cannotVectorize();
+
+ final Map<String, Object> context = makeContext("2000-01-01/2001-01-02");
+
+ // The broadcast table on the right-hand side is read through a subquery,
so the time condition applies to it.
+ testQuery(
+ "SELECT broadcast.dim4, COUNT(*)\n"
+ + "FROM numfoo INNER JOIN broadcast ON numfoo.dim4 = broadcast.dim4\n"
+ + "GROUP BY 1",
+ context,
+ ImmutableList.of(
+ GroupByQuery.builder()
+ .setDataSource(
+ join(
+ new TableDataSource(CalciteTests.DATASOURCE3),
+ new QueryDataSource(
+ newScanQueryBuilder()
+ .dataSource(new
GlobalTableDataSource(CalciteTests.BROADCAST_DATASOURCE))
+
.intervals(querySegmentSpec(Intervals.of("2000-01-01/2001-01-02")))
+ .columns("dim4")
+ .columnTypes(ColumnType.STRING)
+ .context(context)
+ .build()
+ ),
+ "j0.",
+ equalsCondition(makeColumnExpression("dim4"),
makeColumnExpression("j0.dim4")),
+ JoinType.INNER
+ )
+ )
+
.setInterval(querySegmentSpec(Intervals.of("2000-01-01/2001-01-02")))
+ .setGranularity(Granularities.ALL)
+ .setDimensions(dimensions(new
DefaultDimensionSpec("j0.dim4", "d0")))
+ .setAggregatorSpecs(aggregators(new
CountAggregatorFactory("a0")))
+ .setContext(context)
+ .build()
+ ),
+ ImmutableList.of(
+ new Object[]{"a", 9L},
+ new Object[]{"b", 1L}
+ )
+ );
+ }
+
+ @Test
+ public void testJoinWithSubqueryOnLeft()
+ {
+ final Map<String, Object> context =
makeContext("2000-01-01/2000-01-02,2000-01-03/2001");
+
+ // Time condition applies to the subquery on the left-hand side, but not
the outer query, which reads the join.
+ testQuery(
+ "SELECT base.dim2, lookyloo.v, base.cnt\n"
+ + "FROM (SELECT dim2, COUNT(*) AS cnt FROM foo GROUP BY dim2) base\n"
+ + "LEFT JOIN lookup.lookyloo ON base.dim2 = lookyloo.k",
+ context,
+ ImmutableList.of(
+ newScanQueryBuilder()
+ .dataSource(
+ join(
+ new QueryDataSource(
+ GroupByQuery.builder()
+
.setDataSource(CalciteTests.DATASOURCE1)
+ .setInterval(
+ querySegmentSpec(
+
Intervals.of("2000-01-01/2000-01-02"),
+ Intervals.of("2000-01-03/2001")
+ )
+ )
+ .setGranularity(Granularities.ALL)
+ .setDimensions(dimensions(new
DefaultDimensionSpec("dim2", "d0")))
+ .setAggregatorSpecs(aggregators(new
CountAggregatorFactory("a0")))
+ .setContext(context)
+ .build()
+ ),
+ new LookupDataSource("lookyloo"),
+ "j0.",
+ equalsCondition(makeColumnExpression("d0"),
makeColumnExpression("j0.k")),
Review Comment:
## CodeQL / Deprecated method or constructor invocation
Invoking [CalciteTestBase.makeColumnExpression](1) should be avoided because
it has been deprecated.
[Show more
details](https://github.com/apache/druid/security/code-scanning/12101)
##########
sql/src/test/java/org/apache/druid/sql/calcite/CalciteTimeConditionQueryTest.java:
##########
@@ -0,0 +1,569 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.druid.sql.calcite;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import org.apache.druid.error.DruidException;
+import org.apache.druid.error.DruidExceptionMatcher;
+import org.apache.druid.java.util.common.Intervals;
+import org.apache.druid.java.util.common.granularity.Granularities;
+import org.apache.druid.query.Druids;
+import org.apache.druid.query.GlobalTableDataSource;
+import org.apache.druid.query.LookupDataSource;
+import org.apache.druid.query.QueryContexts;
+import org.apache.druid.query.QueryDataSource;
+import org.apache.druid.query.TableDataSource;
+import org.apache.druid.query.UnionDataSource;
+import org.apache.druid.query.UnnestDataSource;
+import org.apache.druid.query.aggregation.CountAggregatorFactory;
+import org.apache.druid.query.aggregation.LongSumAggregatorFactory;
+import org.apache.druid.query.dimension.DefaultDimensionSpec;
+import org.apache.druid.query.groupby.GroupByQuery;
+import org.apache.druid.query.timeboundary.TimeBoundaryQuery;
+import org.apache.druid.segment.column.ColumnType;
+import org.apache.druid.segment.join.JoinType;
+import org.apache.druid.sql.calcite.NotYetSupported.Modes;
+import org.apache.druid.sql.calcite.filtration.Filtration;
+import org.apache.druid.sql.calcite.planner.PlannerContext;
+import org.apache.druid.sql.calcite.util.CalciteTests;
+import org.junit.jupiter.api.Test;
+
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Tests for the {@link PlannerContext#CTX_SQL_TIME_CONDITION} context
parameter.
+ */
+public class CalciteTimeConditionQueryTest extends BaseCalciteQueryTest
+{
+ // __time for foo is [2000-01-01, 2000-01-02, 2000-01-03, 2001-01-01,
2001-01-02, 2001-01-03]
+
+ @Test
+ public void testScan()
+ {
+ final Map<String, Object> context = makeContext("2000-01-02/2000-01-03,
2001-01-01/2001-01-02");
+
+ testBuilder()
+ .sql("SELECT __time, dim1 FROM foo")
+ .queryContext(context)
+ .expectedQueries(
+ ImmutableList.of(
+ newScanQueryBuilder()
+ .dataSource(CalciteTests.DATASOURCE1)
+ .intervals(
+ querySegmentSpec(
+ Intervals.of("2000-01-02/2000-01-03"),
+ Intervals.of("2001-01-01/2001-01-02")
+ )
+ )
+ .columns("__time", "dim1")
+ .columnTypes(ColumnType.LONG, ColumnType.STRING)
+ .context(context)
+ .build()
+ )
+ )
+ // Unordered, since MSQ does not guarantee the order of scan results
across segments.
+ .expectedResults(
+ new UnorderedResultsVerifier(
+ ImmutableList.of(
+ new Object[]{timestamp("2000-01-02"), "10.1"},
+ new Object[]{timestamp("2001-01-01"), "1"}
+ ),
+ ResultMatchMode.EQUALS,
+ null
+ )
+ )
+ .run();
+ }
+
+ @Test
+ public void testScanWithEmptyIntersection()
+ {
+ final Map<String, Object> context = makeContext("2000/2001");
+
+ testQuery(
+ "SELECT dim1 FROM foo WHERE __time >= TIMESTAMP '2001-01-01 00:00:00'",
+ context,
+ ImmutableList.of(
+ newScanQueryBuilder()
+ .dataSource(CalciteTests.DATASOURCE1)
+ .intervals(querySegmentSpec())
+ .columns("dim1")
+ .columnTypes(ColumnType.STRING)
+ .context(context)
+ .build()
+ ),
+ ImmutableList.of()
+ );
+ }
+
+ @Test
+ public void testTimeseriesWithTimeFilter()
+ {
+ final Map<String, Object> context = makeContext("2000/2001");
+
+ testQuery(
+ "SELECT COUNT(*) FROM foo WHERE __time >= TIMESTAMP '2000-01-02
00:00:00'",
+ context,
+ ImmutableList.of(
+ Druids.newTimeseriesQueryBuilder()
+ .dataSource(CalciteTests.DATASOURCE1)
+
.intervals(querySegmentSpec(Intervals.of("2000-01-02/2001-01-01")))
+ .granularity(Granularities.ALL)
+ .aggregators(aggregators(new CountAggregatorFactory("a0")))
+ .context(context)
+ .build()
+ ),
+ ImmutableList.of(
+ new Object[]{2L}
+ )
+ );
+ }
+
+ @Test
+ public void testGroupByWithArray()
+ {
+ final Map<String, Object> context =
makeContext(List.of("2001-01-01/2001-01-03", "2000-01-01/2000-01-02"));
+
+ testQuery(
+ "SELECT dim2, COUNT(*) FROM foo GROUP BY dim2",
+ context,
+ ImmutableList.of(
+ GroupByQuery.builder()
+ .setDataSource(CalciteTests.DATASOURCE1)
+ .setInterval(
+ querySegmentSpec(
+ Intervals.of("2000-01-01/2000-01-02"),
+ Intervals.of("2001-01-01/2001-01-03")
+ )
+ )
+ .setGranularity(Granularities.ALL)
+ .setDimensions(dimensions(new
DefaultDimensionSpec("dim2", "d0")))
+ .setAggregatorSpecs(aggregators(new
CountAggregatorFactory("a0")))
+ .setContext(context)
+ .build()
+ ),
+ ImmutableList.of(
+ new Object[]{"a", 2L},
+ new Object[]{"abc", 1L}
+ )
+ );
+ }
+
+ @Test
+ public void testTimeBoundary()
+ {
+ final Map<String, Object> context = ImmutableMap.<String, Object>builder()
+
.putAll(makeContext("2000/2001"))
+
.put(QueryContexts.TIME_BOUNDARY_PLANNING_KEY, true)
+ .build();
+
+ testQuery(
+ "SELECT MAX(__time) FROM foo",
+ context,
+ ImmutableList.of(
+ Druids.newTimeBoundaryQueryBuilder()
+ .dataSource(CalciteTests.DATASOURCE1)
+ .intervals(querySegmentSpec(Intervals.of("2000/2001")))
+ .bound(TimeBoundaryQuery.MAX_TIME)
+ .context(
+ ImmutableMap.<String, Object>builder()
+ .putAll(context)
+
.put(TimeBoundaryQuery.MAX_TIME_ARRAY_OUTPUT_NAME, "a0")
+ .build()
+ )
+ .build()
+ ),
+ ImmutableList.of(
+ new Object[]{timestamp("2000-01-03")}
+ )
+ );
+ }
+
+ @NotYetSupported(Modes.DD_UNION)
+ @Test
+ public void testUnionDataSource()
+ {
+ final Map<String, Object> context = makeContext("2000-01-01/2000-01-03");
+
+ testQuery(
+ "SELECT dim1, COUNT(*) FROM (SELECT * FROM foo UNION ALL SELECT * FROM
foo) GROUP BY dim1",
+ context,
+ ImmutableList.of(
+ GroupByQuery.builder()
+ .setDataSource(
+ new UnionDataSource(
+ ImmutableList.of(
+ new
TableDataSource(CalciteTests.DATASOURCE1),
+ new
TableDataSource(CalciteTests.DATASOURCE1)
+ )
+ )
+ )
+
.setInterval(querySegmentSpec(Intervals.of("2000-01-01/2000-01-03")))
+ .setGranularity(Granularities.ALL)
+ .setDimensions(dimensions(new
DefaultDimensionSpec("dim1", "d0")))
+ .setAggregatorSpecs(aggregators(new
CountAggregatorFactory("a0")))
+ .setContext(context)
+ .build()
+ ),
+ ImmutableList.of(
+ new Object[]{"", 2L},
+ new Object[]{"10.1", 2L}
+ )
+ );
+ }
+
+ @Test
+ public void testUnnest()
+ {
+ final Map<String, Object> context = makeContext("2000-01-01/2000-01-03");
+
+ testQuery(
+ "SELECT d3 FROM foo, UNNEST(MV_TO_ARRAY(dim3)) AS unnested (d3)",
+ context,
+ ImmutableList.of(
+ newScanQueryBuilder()
+ .dataSource(
+ UnnestDataSource.create(
+ new TableDataSource(CalciteTests.DATASOURCE1),
+ nestedExpressionVirtualColumn("j0.unnest", "\"dim3\"",
ColumnType.STRING),
+ null
+ )
+ )
+
.intervals(querySegmentSpec(Intervals.of("2000-01-01/2000-01-03")))
+ .columns("j0.unnest")
+ .columnTypes(ColumnType.STRING)
+ .context(context)
+ .build()
+ ),
+ ImmutableList.of(
+ new Object[]{"a"},
+ new Object[]{"b"},
+ new Object[]{"b"},
+ new Object[]{"c"}
+ )
+ );
+ }
+
+ @Test
+ public void testJoinWithLookup()
+ {
+ final Map<String, Object> context = makeContext("2001/2002");
+
+ testQuery(
+ "SELECT foo.dim1, lookyloo.v FROM foo INNER JOIN lookup.lookyloo ON
foo.dim2 = lookyloo.k",
+ context,
+ ImmutableList.of(
+ newScanQueryBuilder()
+ .dataSource(
+ join(
+ new TableDataSource(CalciteTests.DATASOURCE1),
+ new LookupDataSource("lookyloo"),
+ "j0.",
+ equalsCondition(makeColumnExpression("dim2"),
makeColumnExpression("j0.k")),
+ JoinType.INNER
+ )
+ )
+ .intervals(querySegmentSpec(Intervals.of("2001/2002")))
+ .columns("dim1", "j0.v")
+ .columnTypes(ColumnType.STRING, ColumnType.STRING)
+ .context(context)
+ .build()
+ ),
+ ImmutableList.of(
+ new Object[]{"1", "xa"},
+ new Object[]{"def", "xabc"}
+ )
+ );
+ }
+
+ @Test
+ public void testJoinWithTableSubquery()
+ {
+ final Map<String, Object> context = makeContext("2000/2001");
+
+ testQuery(
+ "SELECT f1.dim1, f2.dim2 FROM foo f1 INNER JOIN foo f2 ON f1.dim1 =
f2.dim1",
+ context,
+ ImmutableList.of(
+ newScanQueryBuilder()
+ .dataSource(
+ join(
+ new TableDataSource(CalciteTests.DATASOURCE1),
+ new QueryDataSource(
+ newScanQueryBuilder()
+ .dataSource(CalciteTests.DATASOURCE1)
+
.intervals(querySegmentSpec(Intervals.of("2000/2001")))
+ .columns("dim1", "dim2")
+ .columnTypes(ColumnType.STRING,
ColumnType.STRING)
+ .context(context)
+ .build()
+ ),
+ "j0.",
+ equalsCondition(makeColumnExpression("dim1"),
makeColumnExpression("j0.dim1")),
+ JoinType.INNER
+ )
+ )
+ .intervals(querySegmentSpec(Intervals.of("2000/2001")))
+ .columns("dim1", "j0.dim2")
+ .columnTypes(ColumnType.STRING, ColumnType.STRING)
+ .context(context)
+ .build()
+ ),
+ ImmutableList.of(
+ new Object[]{"", "a"},
+ new Object[]{"10.1", null},
+ new Object[]{"2", ""}
+ )
+ );
+ }
+
+ @Test
+ public void testJoinWithBroadcastTable()
+ {
+ // Cannot vectorize JOIN operator.
+ cannotVectorize();
+
+ final Map<String, Object> context = makeContext("2000-01-01/2001-01-02");
+
+ // The broadcast table on the right-hand side is read through a subquery,
so the time condition applies to it.
+ testQuery(
+ "SELECT broadcast.dim4, COUNT(*)\n"
+ + "FROM numfoo INNER JOIN broadcast ON numfoo.dim4 = broadcast.dim4\n"
+ + "GROUP BY 1",
+ context,
+ ImmutableList.of(
+ GroupByQuery.builder()
+ .setDataSource(
+ join(
+ new TableDataSource(CalciteTests.DATASOURCE3),
+ new QueryDataSource(
+ newScanQueryBuilder()
+ .dataSource(new
GlobalTableDataSource(CalciteTests.BROADCAST_DATASOURCE))
+
.intervals(querySegmentSpec(Intervals.of("2000-01-01/2001-01-02")))
+ .columns("dim4")
+ .columnTypes(ColumnType.STRING)
+ .context(context)
+ .build()
+ ),
+ "j0.",
+ equalsCondition(makeColumnExpression("dim4"),
makeColumnExpression("j0.dim4")),
Review Comment:
## CodeQL / Deprecated method or constructor invocation
Invoking [CalciteTestBase.makeColumnExpression](1) should be avoided because
it has been deprecated.
[Show more
details](https://github.com/apache/druid/security/code-scanning/12099)
##########
sql/src/test/java/org/apache/druid/sql/calcite/CalciteTimeConditionQueryTest.java:
##########
@@ -0,0 +1,569 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.druid.sql.calcite;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import org.apache.druid.error.DruidException;
+import org.apache.druid.error.DruidExceptionMatcher;
+import org.apache.druid.java.util.common.Intervals;
+import org.apache.druid.java.util.common.granularity.Granularities;
+import org.apache.druid.query.Druids;
+import org.apache.druid.query.GlobalTableDataSource;
+import org.apache.druid.query.LookupDataSource;
+import org.apache.druid.query.QueryContexts;
+import org.apache.druid.query.QueryDataSource;
+import org.apache.druid.query.TableDataSource;
+import org.apache.druid.query.UnionDataSource;
+import org.apache.druid.query.UnnestDataSource;
+import org.apache.druid.query.aggregation.CountAggregatorFactory;
+import org.apache.druid.query.aggregation.LongSumAggregatorFactory;
+import org.apache.druid.query.dimension.DefaultDimensionSpec;
+import org.apache.druid.query.groupby.GroupByQuery;
+import org.apache.druid.query.timeboundary.TimeBoundaryQuery;
+import org.apache.druid.segment.column.ColumnType;
+import org.apache.druid.segment.join.JoinType;
+import org.apache.druid.sql.calcite.NotYetSupported.Modes;
+import org.apache.druid.sql.calcite.filtration.Filtration;
+import org.apache.druid.sql.calcite.planner.PlannerContext;
+import org.apache.druid.sql.calcite.util.CalciteTests;
+import org.junit.jupiter.api.Test;
+
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Tests for the {@link PlannerContext#CTX_SQL_TIME_CONDITION} context
parameter.
+ */
+public class CalciteTimeConditionQueryTest extends BaseCalciteQueryTest
+{
+ // __time for foo is [2000-01-01, 2000-01-02, 2000-01-03, 2001-01-01,
2001-01-02, 2001-01-03]
+
+ @Test
+ public void testScan()
+ {
+ final Map<String, Object> context = makeContext("2000-01-02/2000-01-03,
2001-01-01/2001-01-02");
+
+ testBuilder()
+ .sql("SELECT __time, dim1 FROM foo")
+ .queryContext(context)
+ .expectedQueries(
+ ImmutableList.of(
+ newScanQueryBuilder()
+ .dataSource(CalciteTests.DATASOURCE1)
+ .intervals(
+ querySegmentSpec(
+ Intervals.of("2000-01-02/2000-01-03"),
+ Intervals.of("2001-01-01/2001-01-02")
+ )
+ )
+ .columns("__time", "dim1")
+ .columnTypes(ColumnType.LONG, ColumnType.STRING)
+ .context(context)
+ .build()
+ )
+ )
+ // Unordered, since MSQ does not guarantee the order of scan results
across segments.
+ .expectedResults(
+ new UnorderedResultsVerifier(
+ ImmutableList.of(
+ new Object[]{timestamp("2000-01-02"), "10.1"},
+ new Object[]{timestamp("2001-01-01"), "1"}
+ ),
+ ResultMatchMode.EQUALS,
+ null
+ )
+ )
+ .run();
+ }
+
+ @Test
+ public void testScanWithEmptyIntersection()
+ {
+ final Map<String, Object> context = makeContext("2000/2001");
+
+ testQuery(
+ "SELECT dim1 FROM foo WHERE __time >= TIMESTAMP '2001-01-01 00:00:00'",
+ context,
+ ImmutableList.of(
+ newScanQueryBuilder()
+ .dataSource(CalciteTests.DATASOURCE1)
+ .intervals(querySegmentSpec())
+ .columns("dim1")
+ .columnTypes(ColumnType.STRING)
+ .context(context)
+ .build()
+ ),
+ ImmutableList.of()
+ );
+ }
+
+ @Test
+ public void testTimeseriesWithTimeFilter()
+ {
+ final Map<String, Object> context = makeContext("2000/2001");
+
+ testQuery(
+ "SELECT COUNT(*) FROM foo WHERE __time >= TIMESTAMP '2000-01-02
00:00:00'",
+ context,
+ ImmutableList.of(
+ Druids.newTimeseriesQueryBuilder()
+ .dataSource(CalciteTests.DATASOURCE1)
+
.intervals(querySegmentSpec(Intervals.of("2000-01-02/2001-01-01")))
+ .granularity(Granularities.ALL)
+ .aggregators(aggregators(new CountAggregatorFactory("a0")))
+ .context(context)
+ .build()
+ ),
+ ImmutableList.of(
+ new Object[]{2L}
+ )
+ );
+ }
+
+ @Test
+ public void testGroupByWithArray()
+ {
+ final Map<String, Object> context =
makeContext(List.of("2001-01-01/2001-01-03", "2000-01-01/2000-01-02"));
+
+ testQuery(
+ "SELECT dim2, COUNT(*) FROM foo GROUP BY dim2",
+ context,
+ ImmutableList.of(
+ GroupByQuery.builder()
+ .setDataSource(CalciteTests.DATASOURCE1)
+ .setInterval(
+ querySegmentSpec(
+ Intervals.of("2000-01-01/2000-01-02"),
+ Intervals.of("2001-01-01/2001-01-03")
+ )
+ )
+ .setGranularity(Granularities.ALL)
+ .setDimensions(dimensions(new
DefaultDimensionSpec("dim2", "d0")))
+ .setAggregatorSpecs(aggregators(new
CountAggregatorFactory("a0")))
+ .setContext(context)
+ .build()
+ ),
+ ImmutableList.of(
+ new Object[]{"a", 2L},
+ new Object[]{"abc", 1L}
+ )
+ );
+ }
+
+ @Test
+ public void testTimeBoundary()
+ {
+ final Map<String, Object> context = ImmutableMap.<String, Object>builder()
+
.putAll(makeContext("2000/2001"))
+
.put(QueryContexts.TIME_BOUNDARY_PLANNING_KEY, true)
+ .build();
+
+ testQuery(
+ "SELECT MAX(__time) FROM foo",
+ context,
+ ImmutableList.of(
+ Druids.newTimeBoundaryQueryBuilder()
+ .dataSource(CalciteTests.DATASOURCE1)
+ .intervals(querySegmentSpec(Intervals.of("2000/2001")))
+ .bound(TimeBoundaryQuery.MAX_TIME)
+ .context(
+ ImmutableMap.<String, Object>builder()
+ .putAll(context)
+
.put(TimeBoundaryQuery.MAX_TIME_ARRAY_OUTPUT_NAME, "a0")
+ .build()
+ )
+ .build()
+ ),
+ ImmutableList.of(
+ new Object[]{timestamp("2000-01-03")}
+ )
+ );
+ }
+
+ @NotYetSupported(Modes.DD_UNION)
+ @Test
+ public void testUnionDataSource()
+ {
+ final Map<String, Object> context = makeContext("2000-01-01/2000-01-03");
+
+ testQuery(
+ "SELECT dim1, COUNT(*) FROM (SELECT * FROM foo UNION ALL SELECT * FROM
foo) GROUP BY dim1",
+ context,
+ ImmutableList.of(
+ GroupByQuery.builder()
+ .setDataSource(
+ new UnionDataSource(
+ ImmutableList.of(
+ new
TableDataSource(CalciteTests.DATASOURCE1),
+ new
TableDataSource(CalciteTests.DATASOURCE1)
+ )
+ )
+ )
+
.setInterval(querySegmentSpec(Intervals.of("2000-01-01/2000-01-03")))
+ .setGranularity(Granularities.ALL)
+ .setDimensions(dimensions(new
DefaultDimensionSpec("dim1", "d0")))
+ .setAggregatorSpecs(aggregators(new
CountAggregatorFactory("a0")))
+ .setContext(context)
+ .build()
+ ),
+ ImmutableList.of(
+ new Object[]{"", 2L},
+ new Object[]{"10.1", 2L}
+ )
+ );
+ }
+
+ @Test
+ public void testUnnest()
+ {
+ final Map<String, Object> context = makeContext("2000-01-01/2000-01-03");
+
+ testQuery(
+ "SELECT d3 FROM foo, UNNEST(MV_TO_ARRAY(dim3)) AS unnested (d3)",
+ context,
+ ImmutableList.of(
+ newScanQueryBuilder()
+ .dataSource(
+ UnnestDataSource.create(
+ new TableDataSource(CalciteTests.DATASOURCE1),
+ nestedExpressionVirtualColumn("j0.unnest", "\"dim3\"",
ColumnType.STRING),
+ null
+ )
+ )
+
.intervals(querySegmentSpec(Intervals.of("2000-01-01/2000-01-03")))
+ .columns("j0.unnest")
+ .columnTypes(ColumnType.STRING)
+ .context(context)
+ .build()
+ ),
+ ImmutableList.of(
+ new Object[]{"a"},
+ new Object[]{"b"},
+ new Object[]{"b"},
+ new Object[]{"c"}
+ )
+ );
+ }
+
+ @Test
+ public void testJoinWithLookup()
+ {
+ final Map<String, Object> context = makeContext("2001/2002");
+
+ testQuery(
+ "SELECT foo.dim1, lookyloo.v FROM foo INNER JOIN lookup.lookyloo ON
foo.dim2 = lookyloo.k",
+ context,
+ ImmutableList.of(
+ newScanQueryBuilder()
+ .dataSource(
+ join(
+ new TableDataSource(CalciteTests.DATASOURCE1),
+ new LookupDataSource("lookyloo"),
+ "j0.",
+ equalsCondition(makeColumnExpression("dim2"),
makeColumnExpression("j0.k")),
+ JoinType.INNER
+ )
+ )
+ .intervals(querySegmentSpec(Intervals.of("2001/2002")))
+ .columns("dim1", "j0.v")
+ .columnTypes(ColumnType.STRING, ColumnType.STRING)
+ .context(context)
+ .build()
+ ),
+ ImmutableList.of(
+ new Object[]{"1", "xa"},
+ new Object[]{"def", "xabc"}
+ )
+ );
+ }
+
+ @Test
+ public void testJoinWithTableSubquery()
+ {
+ final Map<String, Object> context = makeContext("2000/2001");
+
+ testQuery(
+ "SELECT f1.dim1, f2.dim2 FROM foo f1 INNER JOIN foo f2 ON f1.dim1 =
f2.dim1",
+ context,
+ ImmutableList.of(
+ newScanQueryBuilder()
+ .dataSource(
+ join(
+ new TableDataSource(CalciteTests.DATASOURCE1),
+ new QueryDataSource(
+ newScanQueryBuilder()
+ .dataSource(CalciteTests.DATASOURCE1)
+
.intervals(querySegmentSpec(Intervals.of("2000/2001")))
+ .columns("dim1", "dim2")
+ .columnTypes(ColumnType.STRING,
ColumnType.STRING)
+ .context(context)
+ .build()
+ ),
+ "j0.",
+ equalsCondition(makeColumnExpression("dim1"),
makeColumnExpression("j0.dim1")),
+ JoinType.INNER
+ )
+ )
+ .intervals(querySegmentSpec(Intervals.of("2000/2001")))
+ .columns("dim1", "j0.dim2")
+ .columnTypes(ColumnType.STRING, ColumnType.STRING)
+ .context(context)
+ .build()
+ ),
+ ImmutableList.of(
+ new Object[]{"", "a"},
+ new Object[]{"10.1", null},
+ new Object[]{"2", ""}
+ )
+ );
+ }
+
+ @Test
+ public void testJoinWithBroadcastTable()
+ {
+ // Cannot vectorize JOIN operator.
+ cannotVectorize();
+
+ final Map<String, Object> context = makeContext("2000-01-01/2001-01-02");
+
+ // The broadcast table on the right-hand side is read through a subquery,
so the time condition applies to it.
+ testQuery(
+ "SELECT broadcast.dim4, COUNT(*)\n"
+ + "FROM numfoo INNER JOIN broadcast ON numfoo.dim4 = broadcast.dim4\n"
+ + "GROUP BY 1",
+ context,
+ ImmutableList.of(
+ GroupByQuery.builder()
+ .setDataSource(
+ join(
+ new TableDataSource(CalciteTests.DATASOURCE3),
+ new QueryDataSource(
+ newScanQueryBuilder()
+ .dataSource(new
GlobalTableDataSource(CalciteTests.BROADCAST_DATASOURCE))
+
.intervals(querySegmentSpec(Intervals.of("2000-01-01/2001-01-02")))
+ .columns("dim4")
+ .columnTypes(ColumnType.STRING)
+ .context(context)
+ .build()
+ ),
+ "j0.",
+ equalsCondition(makeColumnExpression("dim4"),
makeColumnExpression("j0.dim4")),
+ JoinType.INNER
+ )
+ )
+
.setInterval(querySegmentSpec(Intervals.of("2000-01-01/2001-01-02")))
+ .setGranularity(Granularities.ALL)
+ .setDimensions(dimensions(new
DefaultDimensionSpec("j0.dim4", "d0")))
+ .setAggregatorSpecs(aggregators(new
CountAggregatorFactory("a0")))
+ .setContext(context)
+ .build()
+ ),
+ ImmutableList.of(
+ new Object[]{"a", 9L},
+ new Object[]{"b", 1L}
+ )
+ );
+ }
+
+ @Test
+ public void testJoinWithSubqueryOnLeft()
+ {
+ final Map<String, Object> context =
makeContext("2000-01-01/2000-01-02,2000-01-03/2001");
+
+ // Time condition applies to the subquery on the left-hand side, but not
the outer query, which reads the join.
+ testQuery(
+ "SELECT base.dim2, lookyloo.v, base.cnt\n"
+ + "FROM (SELECT dim2, COUNT(*) AS cnt FROM foo GROUP BY dim2) base\n"
+ + "LEFT JOIN lookup.lookyloo ON base.dim2 = lookyloo.k",
+ context,
+ ImmutableList.of(
+ newScanQueryBuilder()
+ .dataSource(
+ join(
+ new QueryDataSource(
+ GroupByQuery.builder()
+
.setDataSource(CalciteTests.DATASOURCE1)
+ .setInterval(
+ querySegmentSpec(
+
Intervals.of("2000-01-01/2000-01-02"),
+ Intervals.of("2000-01-03/2001")
+ )
+ )
+ .setGranularity(Granularities.ALL)
+ .setDimensions(dimensions(new
DefaultDimensionSpec("dim2", "d0")))
+ .setAggregatorSpecs(aggregators(new
CountAggregatorFactory("a0")))
+ .setContext(context)
+ .build()
+ ),
+ new LookupDataSource("lookyloo"),
+ "j0.",
+ equalsCondition(makeColumnExpression("d0"),
makeColumnExpression("j0.k")),
Review Comment:
## CodeQL / Deprecated method or constructor invocation
Invoking [CalciteTestBase.makeColumnExpression](1) should be avoided because
it has been deprecated.
[Show more
details](https://github.com/apache/druid/security/code-scanning/12100)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]