danny0405 commented on a change in pull request #1141: [CALCITE-2914] Improve how LatticeSuggester deduces foreign keys URL: https://github.com/apache/calcite/pull/1141#discussion_r270724726
########## File path: core/src/main/java/org/apache/calcite/statistic/QuerySqlStatisticProvider.java ########## @@ -0,0 +1,192 @@ +/* + * 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.calcite.statistic; + +import org.apache.calcite.adapter.jdbc.JdbcRules; +import org.apache.calcite.adapter.jdbc.JdbcSchema; +import org.apache.calcite.adapter.jdbc.JdbcTable; +import org.apache.calcite.materialize.SqlStatisticProvider; +import org.apache.calcite.plan.RelOptCluster; +import org.apache.calcite.plan.RelOptSchema; +import org.apache.calcite.plan.RelOptTable; +import org.apache.calcite.plan.ViewExpanders; +import org.apache.calcite.rel.RelNode; +import org.apache.calcite.rel.rel2sql.RelToSqlConverter; +import org.apache.calcite.rel.rel2sql.SqlImplementor; +import org.apache.calcite.sql.SqlDialect; +import org.apache.calcite.sql.SqlNode; +import org.apache.calcite.sql.fun.SqlStdOperatorTable; +import org.apache.calcite.tools.Frameworks; +import org.apache.calcite.tools.RelBuilder; +import org.apache.calcite.util.Util; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.List; +import java.util.Objects; +import java.util.function.Consumer; +import java.util.stream.Collectors; +import javax.sql.DataSource; + +/** + * Implementation of {@link SqlStatisticProvider} that generates and executes + * SQL queries. + */ +public class QuerySqlStatisticProvider implements SqlStatisticProvider { + private final Consumer<String> sqlConsumer; + + /** Creates a QuerySqlStatisticProvider. + * + * @param sqlConsumer Called when each SQL statement is generated + */ + public QuerySqlStatisticProvider(Consumer<String> sqlConsumer) { + this.sqlConsumer = Objects.requireNonNull(sqlConsumer); + } + + public double tableCardinality(RelOptTable table) { + final JdbcTable jdbcTable = table.unwrap(JdbcTable.class); + return withBuilder(jdbcTable.jdbcSchema, + (cluster, relOptSchema, jdbcSchema, relBuilder) -> { + // Generate: + // SELECT COUNT(*) FROM `EMP` + relBuilder.push(table.toRel(ViewExpanders.simpleContext(cluster))) + .aggregate(relBuilder.groupKey(), relBuilder.count()); + + final String sql = toSql(relBuilder.build(), jdbcSchema.dialect); + final DataSource dataSource = jdbcSchema.getDataSource(); + try (Connection connection = dataSource.getConnection(); + Statement statement = connection.createStatement(); + ResultSet resultSet = statement.executeQuery(sql)) { + if (!resultSet.next()) { + throw new AssertionError(); + } + return resultSet.getDouble(1); + } catch (SQLException e) { + throw handle(e, sql); + } + }); + } + + public boolean isForeignKey(RelOptTable fromTable, List<Integer> fromColumns, + RelOptTable toTable, List<Integer> toColumns) { + final JdbcTable jdbcTable = fromTable.unwrap(JdbcTable.class); + return withBuilder(jdbcTable.jdbcSchema, + (cluster, relOptSchema, jdbcSchema, relBuilder) -> { + // EMP(DEPTNO) is a foreign key to DEPT(DEPTNO) if the following + // query returns 0: + // + // SELECT COUNT(*) FROM ( + // SELECT deptno FROM `EMP` WHERE deptno IS NOT NULL + // MINUS + // SELECT deptno FROM `DEPT`) Review comment: We also need to check that the toColumn are primary key of toTable ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
