-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Implement timestamp predicate pushdown in Druid connector #8474
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,12 +13,16 @@ | |
| */ | ||
| package io.trino.plugin.druid; | ||
|
|
||
| import com.google.common.collect.ImmutableMap; | ||
| import io.trino.Session; | ||
| import io.trino.plugin.jdbc.BaseJdbcConnectorTest; | ||
| import io.trino.plugin.jdbc.JdbcTableHandle; | ||
| import io.trino.spi.connector.ConnectorSession; | ||
| import io.trino.spi.connector.SchemaTableName; | ||
| import io.trino.spi.predicate.TupleDomain; | ||
| import io.trino.sql.planner.assertions.PlanMatchPattern; | ||
| import io.trino.sql.planner.plan.AggregationNode; | ||
| import io.trino.sql.planner.plan.FilterNode; | ||
| import io.trino.sql.planner.plan.JoinNode; | ||
| import io.trino.sql.planner.plan.TableScanNode; | ||
| import io.trino.sql.planner.plan.TopNNode; | ||
|
|
@@ -35,6 +39,7 @@ | |
| import static io.trino.spi.type.VarcharType.VARCHAR; | ||
| import static io.trino.sql.planner.assertions.PlanMatchPattern.anyTree; | ||
| import static io.trino.sql.planner.assertions.PlanMatchPattern.node; | ||
| import static io.trino.sql.planner.assertions.PlanMatchPattern.tableScan; | ||
| import static io.trino.testing.MaterializedResult.resultBuilder; | ||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
|
|
@@ -275,4 +280,122 @@ public void testLimitPushDown() | |
| "LIMIT 30")) | ||
| .isNotFullyPushedDown(joinOverTableScans); | ||
| } | ||
|
|
||
| @Test | ||
| public void testPredicatePushdown() | ||
| { | ||
| assertThat(query("SELECT * FROM orders where __time > DATE'1970-01-01'")).isFullyPushedDown(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Copy the tests from TestPostgreSqlConnectorTest#testPredicatePushdown and remove the ones not relevant for Druid. Keeping tests consistent is useful and allows extracting them to base test classes in the future. For |
||
| assertThat(query("SELECT * FROM orders where totalprice > 0")).isFullyPushedDown(); | ||
| assertThat(query("SELECT * FROM orders where comment = ''")).isFullyPushedDown(); | ||
|
|
||
| // varchar equality | ||
| assertThat(query("SELECT regionkey, nationkey, name FROM nation WHERE name = 'ROMANIA'")) | ||
| .matches("VALUES (BIGINT '3', BIGINT '19', CAST('ROMANIA' AS varchar(25)))") | ||
| .isFullyPushedDown(); | ||
|
|
||
| // varchar range | ||
| assertThat(query("SELECT regionkey, nationkey, name FROM nation WHERE name BETWEEN 'POLAND' AND 'RPA'")) | ||
| .matches("VALUES (BIGINT '3', BIGINT '19', CAST('ROMANIA' AS varchar(25)))") | ||
| .isNotFullyPushedDown(FilterNode.class); | ||
|
|
||
| // varchar IN without domain compaction | ||
| assertThat(query("SELECT regionkey, nationkey, name FROM nation WHERE name IN ('POLAND', 'ROMANIA', 'VIETNAM')")) | ||
| .matches("VALUES " + | ||
| "(BIGINT '3', BIGINT '19', CAST('ROMANIA' AS varchar(25))), " + | ||
| "(BIGINT '2', BIGINT '21', CAST('VIETNAM' AS varchar(25)))") | ||
| .isFullyPushedDown(); | ||
|
|
||
| // varchar IN with small compaction threshold | ||
| assertThat(query( | ||
| Session.builder(getSession()) | ||
| .setCatalogSessionProperty("postgresql", "domain_compaction_threshold", "1") | ||
| .build(), | ||
| "SELECT regionkey, nationkey, name FROM nation WHERE name IN ('POLAND', 'ROMANIA', 'VIETNAM')")) | ||
| .matches("VALUES " + | ||
| "(BIGINT '3', BIGINT '19', CAST('ROMANIA' AS varchar(25))), " + | ||
| "(BIGINT '2', BIGINT '21', CAST('VIETNAM' AS varchar(25)))") | ||
| // Filter node is retained as no constraint is pushed into connector. | ||
| // The compacted domain is a range predicate which can give wrong results | ||
| // if pushed down as PostgreSQL has different sort ordering for letters from Trino | ||
| .isNotFullyPushedDown( | ||
| node( | ||
| FilterNode.class, | ||
| // verify that no constraint is applied by the connector | ||
| tableScan( | ||
| tableHandle -> ((JdbcTableHandle) tableHandle).getConstraint().isAll(), | ||
| TupleDomain.all(), | ||
| ImmutableMap.of()))); | ||
|
|
||
| // varchar different case | ||
| assertThat(query("SELECT regionkey, nationkey, name FROM nation WHERE name = 'romania'")) | ||
| .returnsEmptyResult() | ||
| .isFullyPushedDown(); | ||
|
|
||
| // bigint equality | ||
| assertThat(query("SELECT regionkey, nationkey, name FROM nation WHERE nationkey = 19")) | ||
| .matches("VALUES (BIGINT '3', BIGINT '19', CAST('ROMANIA' AS varchar(25)))") | ||
| .isFullyPushedDown(); | ||
|
|
||
| // bigint equality with small compaction threshold | ||
| assertThat(query( | ||
| Session.builder(getSession()) | ||
| .setCatalogSessionProperty("postgresql", "domain_compaction_threshold", "1") | ||
| .build(), | ||
| "SELECT regionkey, nationkey, name FROM nation WHERE nationkey IN (19, 21)")) | ||
| .matches("VALUES " + | ||
| "(BIGINT '3', BIGINT '19', CAST('ROMANIA' AS varchar(25))), " + | ||
| "(BIGINT '2', BIGINT '21', CAST('VIETNAM' AS varchar(25)))") | ||
| .isNotFullyPushedDown(FilterNode.class); | ||
|
|
||
| // bigint range, with decimal to bigint simplification | ||
| assertThat(query("SELECT regionkey, nationkey, name FROM nation WHERE nationkey BETWEEN 18.5 AND 19.5")) | ||
| .matches("VALUES (BIGINT '3', BIGINT '19', CAST('ROMANIA' AS varchar(25)))") | ||
| .isFullyPushedDown(); | ||
|
|
||
| // date equality | ||
| assertThat(query("SELECT orderkey FROM orders WHERE orderdate = DATE '1992-09-29'")) | ||
| .matches("VALUES BIGINT '1250', 34406, 38436, 57570") | ||
| .isFullyPushedDown(); | ||
|
|
||
| // predicate over aggregation key (likely to be optimized before being pushed down into the connector) | ||
| assertThat(query("SELECT * FROM (SELECT regionkey, sum(nationkey) FROM nation GROUP BY regionkey) WHERE regionkey = 3")) | ||
| .matches("VALUES (BIGINT '3', BIGINT '77')") | ||
| .isFullyPushedDown(); | ||
|
|
||
| // predicate over aggregation result | ||
| assertThat(query("SELECT regionkey, sum(nationkey) FROM nation GROUP BY regionkey HAVING sum(nationkey) = 77")) | ||
| .matches("VALUES (BIGINT '3', BIGINT '77')") | ||
| .isFullyPushedDown(); | ||
|
|
||
| // predicate over TopN result | ||
| assertThat(query("" + | ||
| "SELECT orderkey " + | ||
| "FROM (SELECT * FROM orders ORDER BY orderdate DESC, orderkey ASC LIMIT 10)" + | ||
| "WHERE orderdate = DATE '1998-08-01'")) | ||
| .matches("VALUES BIGINT '27588', 22403, 37735") | ||
| .ordered() | ||
| .isFullyPushedDown(); | ||
|
|
||
| assertThat(query("" + | ||
| "SELECT custkey " + | ||
| "FROM (SELECT SUM(totalprice) as sum, custkey, COUNT(*) as cnt FROM orders GROUP BY custkey order by sum desc limit 10) " + | ||
| "WHERE cnt > 30")) | ||
| .matches("VALUES BIGINT '643', 898") | ||
| .ordered() | ||
| .isFullyPushedDown(); | ||
|
|
||
| // predicate over join | ||
| Session joinPushdownEnabled = joinPushdownEnabled(getSession()); | ||
| assertThat(query(joinPushdownEnabled, "SELECT c.name, n.name FROM customer c JOIN nation n ON c.custkey = n.nationkey WHERE acctbal > 8000")) | ||
| .isFullyPushedDown(); | ||
|
|
||
| // varchar predicate over join | ||
| assertThat(query(joinPushdownEnabled, "SELECT c.name, n.name FROM customer c JOIN nation n ON c.custkey = n.nationkey WHERE address = 'TcGe5gaZNgVePxU5kRrvXBfkasDTea'")) | ||
| .isFullyPushedDown(); | ||
| assertThat(query(joinPushdownEnabled, "SELECT c.name, n.name FROM customer c JOIN nation n ON c.custkey = n.nationkey WHERE address < 'TcGe5gaZNgVePxU5kRrvXBfkasDTea'")) | ||
| .isNotFullyPushedDown( | ||
| node(JoinNode.class, | ||
| anyTree(node(TableScanNode.class)), | ||
| anyTree(node(TableScanNode.class)))); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| /* | ||
| * Licensed 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 io.trino.plugin.druid; | ||
|
|
||
| import io.airlift.log.Logger; | ||
| import io.trino.plugin.druid.ingestion.IndexTaskBuilder; | ||
| import io.trino.plugin.druid.ingestion.TimestampSpec; | ||
| import io.trino.testing.datatype.ColumnSetup; | ||
| import io.trino.testing.datatype.DataSetup; | ||
| import io.trino.testing.sql.SqlExecutor; | ||
| import io.trino.testing.sql.TestTable; | ||
|
|
||
| import java.io.BufferedWriter; | ||
| import java.io.File; | ||
| import java.io.FileWriter; | ||
| import java.io.IOException; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import static java.lang.String.format; | ||
|
|
||
| public class DruidCreateAndInsertDataSetup | ||
| implements DataSetup | ||
| { | ||
| private static final Logger log = Logger.get(DruidCreateAndInsertDataSetup.class); | ||
| private final SqlExecutor sqlExecutor; | ||
| private final TestingDruidServer druidServer; | ||
| private final String dataSourceNamePrefix; | ||
|
|
||
| public DruidCreateAndInsertDataSetup(SqlExecutor sqlExecutor, TestingDruidServer druidServer, String dataSourceNamePrefix) | ||
| { | ||
| this.sqlExecutor = sqlExecutor; | ||
| this.druidServer = druidServer; | ||
| this.dataSourceNamePrefix = dataSourceNamePrefix; | ||
| } | ||
|
|
||
| @Override | ||
| public TestTable setupTestTable(List<ColumnSetup> inputs) | ||
| { | ||
| TestTable testTable = new TestTable(this.sqlExecutor, this.dataSourceNamePrefix, "(col1 TIMESTAMP(3))", false); | ||
| try { | ||
| ingestData(testTable, inputs); | ||
| } | ||
| catch (Exception e) { | ||
| log.error(e); | ||
| } | ||
| return testTable; | ||
| } | ||
|
|
||
| private void ingestData(TestTable testTable, List<ColumnSetup> inputs) | ||
| throws Exception | ||
| { | ||
| IndexTaskBuilder builder = new IndexTaskBuilder(); | ||
| builder.setDatasource(testTable.getName()); | ||
| TimestampSpec timestampSpec = getTimestampSpec(inputs); | ||
| builder.setTimestampSpec(timestampSpec); | ||
|
|
||
| List<ColumnSetup> normalInputs = inputs.stream().filter(input -> !isTimestampDimension(input)).collect(Collectors.toList()); | ||
| for (int index = 0; index < inputs.size() - 1; index++) { | ||
| builder.addColumn(format("col_%s", index), normalInputs.get(index).getDeclaredType().orElse("string")); | ||
| } | ||
|
|
||
| String dataFilePath = format("%s.tsv", testTable.getName()); | ||
| writeTsvFile(dataFilePath, inputs); | ||
|
|
||
| log.info(builder.build()); | ||
| this.druidServer.ingestDataWithoutTaskFile(builder.build(), dataFilePath, testTable.getName()); | ||
| } | ||
|
|
||
| private TimestampSpec getTimestampSpec(List<ColumnSetup> inputs) | ||
| { | ||
| List<ColumnSetup> timestampInputs = inputs.stream().filter(this::isTimestampDimension).collect(Collectors.toList()); | ||
|
|
||
| if (timestampInputs.size() > 1) { | ||
| throw new UnsupportedOperationException("Druid only allows one timestamp field"); | ||
| } | ||
|
|
||
| return new TimestampSpec("dummy_druid_ts", "auto"); | ||
| } | ||
|
|
||
| private boolean isTimestampDimension(ColumnSetup input) | ||
| { | ||
| if (input.getDeclaredType().isEmpty()) { | ||
| return false; | ||
| } | ||
| String type = input.getDeclaredType().get(); | ||
|
|
||
| // TODO: support more types | ||
| if (type.startsWith("timestamp")) { | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| private void writeTsvFile(String dataFilePath, List<ColumnSetup> inputs) | ||
| throws IOException | ||
| { | ||
| String tsvFileLocation = format("%s/%s", druidServer.getHostWorkingDirectory(), dataFilePath); | ||
| File file = new File(tsvFileLocation); | ||
| try (BufferedWriter bw = new BufferedWriter(new FileWriter(file))) { | ||
| bw.write(inputs.stream().map(ColumnSetup::getInputLiteral).collect(Collectors.joining("\t"))); | ||
| } | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.