mihaibudiu commented on code in PR #3562: URL: https://github.com/apache/calcite/pull/3562#discussion_r1911402324
########## core/src/main/java/org/apache/calcite/adapter/jdbc/JdbcCorrelationDataContextBuilder.java: ########## @@ -0,0 +1,28 @@ +/* + * 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.adapter.jdbc; + +import org.apache.calcite.rel.core.CorrelationId; + +import java.lang.reflect.Type; + +/** + * A class to build an object of type JdbcCorrelationDataContext. Review Comment: Technically this is an interface. ########## core/src/main/java/org/apache/calcite/adapter/jdbc/JdbcImplementor.java: ########## @@ -18,20 +18,66 @@ import org.apache.calcite.adapter.java.JavaTypeFactory; import org.apache.calcite.rel.RelNode; +import org.apache.calcite.rel.core.CorrelationId; import org.apache.calcite.rel.rel2sql.RelToSqlConverter; +import org.apache.calcite.rel.rel2sql.SqlImplementor; +import org.apache.calcite.rel.type.RelDataTypeField; +import org.apache.calcite.rex.RexCorrelVariable; import org.apache.calcite.sql.SqlDialect; -import org.apache.calcite.util.Util; +import org.apache.calcite.sql.SqlDynamicParam; +import org.apache.calcite.sql.SqlNode; +import org.apache.calcite.sql.parser.SqlParserPos; + +import java.lang.reflect.Type; +import java.util.List; /** * State for generating a SQL statement. */ public class JdbcImplementor extends RelToSqlConverter { - public JdbcImplementor(SqlDialect dialect, JavaTypeFactory typeFactory) { + + private final JdbcCorrelationDataContextBuilder dataContextBuilder; + private final JavaTypeFactory typeFactory; + + public JdbcImplementor(SqlDialect dialect, JavaTypeFactory typeFactory, + JdbcCorrelationDataContextBuilder dataContextBuilder) { super(dialect); - Util.discard(typeFactory); + this. typeFactory = typeFactory; Review Comment: extra space ########## core/src/main/java/org/apache/calcite/adapter/jdbc/JdbcCorrelationDataContext.java: ########## @@ -0,0 +1,63 @@ +/* + * 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.adapter.jdbc; + +import org.apache.calcite.DataContext; +import org.apache.calcite.adapter.java.JavaTypeFactory; +import org.apache.calcite.linq4j.QueryProvider; +import org.apache.calcite.schema.SchemaPlus; + +import org.checkerframework.checker.nullness.qual.Nullable; + +import static java.lang.Integer.parseInt; + +/** + * A special DataContext which handles correlation variable for batch nested loop joins. + */ +public class JdbcCorrelationDataContext implements DataContext { + public static final int OFFSET = 10000; Review Comment: constant offsets scare me. can there be some safety check that the number of "standard" parameters does not exceed OFFSET? ########## core/src/test/java/org/apache/calcite/test/JdbcAdapterTest.java: ########## @@ -1424,6 +1430,57 @@ private LockWrapper exclusiveCleanDb(Connection c) throws SQLException { + "GROUP BY \"t2\".\"DNAME\", \"t2\".\"DNAME0\"") .runs(); } + + /** + * Test case for + * <a href="https://issues.apache.org/jira/browse/CALCITE-4188">[CALCITE-4188] + * Support EnumerableBatchNestedLoopJoin for JDBC</a>. */ + @Test void testBatchNestedLoopJoinPlan() { + final String sql = "SELECT *\n" + + "FROM \"s\".\"emps\" A\n" + + "LEFT OUTER JOIN \"foodmart\".\"store\" B ON A.\"empid\" = B.\"store_id\""; + final String explain = "JdbcFilter(condition=[OR(=($cor0.empid0, $0), =($cor1.empid0, $0)"; + final String jdbcSql = "SELECT *\n" + + "FROM \"foodmart\".\"store\"\n" + + "WHERE ? = \"store_id\" OR (? = \"store_id\" OR ? = \"store_id\") OR (? = \"store_id\" OR" + + " (? = \"store_id\" OR ? = \"store_id\")) OR (? = \"store_id\" OR (? = \"store_id\" OR ? " Review Comment: this plan looks terrible. do you know why? ########## core/src/test/java/org/apache/calcite/test/JdbcAdapterTest.java: ########## @@ -1424,6 +1430,57 @@ private LockWrapper exclusiveCleanDb(Connection c) throws SQLException { + "GROUP BY \"t2\".\"DNAME\", \"t2\".\"DNAME0\"") .runs(); } + + /** + * Test case for + * <a href="https://issues.apache.org/jira/browse/CALCITE-4188">[CALCITE-4188] + * Support EnumerableBatchNestedLoopJoin for JDBC</a>. */ + @Test void testBatchNestedLoopJoinPlan() { + final String sql = "SELECT *\n" + + "FROM \"s\".\"emps\" A\n" + + "LEFT OUTER JOIN \"foodmart\".\"store\" B ON A.\"empid\" = B.\"store_id\""; + final String explain = "JdbcFilter(condition=[OR(=($cor0.empid0, $0), =($cor1.empid0, $0)"; + final String jdbcSql = "SELECT *\n" + + "FROM \"foodmart\".\"store\"\n" + + "WHERE ? = \"store_id\" OR (? = \"store_id\" OR ? = \"store_id\") OR (? = \"store_id\" OR" + + " (? = \"store_id\" OR ? = \"store_id\")) OR (? = \"store_id\" OR (? = \"store_id\" OR ? " + + "= \"store_id\") OR (? = \"store_id\" OR (? = \"store_id\" OR ? = \"store_id\"))) OR (? =" + + " \"store_id\" OR (? = \"store_id\" OR ? = \"store_id\") OR (? = \"store_id\" OR (? = " + + "\"store_id\" OR ? = \"store_id\")) OR (? = \"store_id\" OR (? = \"store_id\" OR ? = " + + "\"store_id\") OR (? = \"store_id\" OR ? = \"store_id\" OR (? = \"store_id\" OR ? = " + + "\"store_id\")))) OR (? = \"store_id\" OR (? = \"store_id\" OR ? = \"store_id\") OR (? = " + + "\"store_id\" OR (? = \"store_id\" OR ? = \"store_id\")) OR (? = \"store_id\" OR (? = " + + "\"store_id\" OR ? = \"store_id\") OR (? = \"store_id\" OR (? = \"store_id\" OR ? = " + + "\"store_id\"))) OR (? = \"store_id\" OR (? = \"store_id\" OR ? = \"store_id\") OR (? = " + + "\"store_id\" OR (? = \"store_id\" OR ? = \"store_id\")) OR (? = \"store_id\" OR (? = " + + "\"store_id\" OR ? = \"store_id\") OR (? = \"store_id\" OR ? = \"store_id\" OR (? = " + + "\"store_id\" OR ? = \"store_id\"))))) OR (? = \"store_id\" OR (? = \"store_id\" OR ? = " + + "\"store_id\") OR (? = \"store_id\" OR (? = \"store_id\" OR ? = \"store_id\")) OR (? = " + + "\"store_id\" OR (? = \"store_id\" OR ? = \"store_id\") OR (? = \"store_id\" OR (? = " + + "\"store_id\" OR ? = \"store_id\"))) OR (? = \"store_id\" OR (? = \"store_id\" OR ? = " + + "\"store_id\") OR (? = \"store_id\" OR (? = \"store_id\" OR ? = \"store_id\")) OR (? = " + + "\"store_id\" OR (? = \"store_id\" OR ? = \"store_id\") OR (? = \"store_id\" OR ? = " + + "\"store_id\" OR (? = \"store_id\" OR ? = \"store_id\")))) OR (? = \"store_id\" OR (? = " + + "\"store_id\" OR ? = \"store_id\") OR (? = \"store_id\" OR (? = \"store_id\" OR ? = " + + "\"store_id\")) OR (? = \"store_id\" OR (? = \"store_id\" OR ? = \"store_id\") OR (? = " + + "\"store_id\" OR (? = \"store_id\" OR ? = \"store_id\"))) OR (? = \"store_id\" OR (? = " + + "\"store_id\" OR ? = \"store_id\") OR (? = \"store_id\" OR (? = \"store_id\" OR ? = " + + "\"store_id\")) OR (? = \"store_id\" OR (? = \"store_id\" OR ? = \"store_id\") OR (? = " + + "\"store_id\" OR ? = \"store_id\" OR (? = \"store_id\" OR ? = \"store_id\"))))))"; + CalciteAssert.model(FoodmartSchema.FOODMART_MODEL) + .withSchema("s", new ReflectiveSchema(new HrSchema())) + .withHook(Hook.PLANNER, (Consumer<RelOptPlanner>) planner -> { + planner.addRule(EnumerableRules.ENUMERABLE_BATCH_NESTED_LOOP_JOIN_RULE); + }) + .query(sql) + .explainContains(explain) + .runs() + .enable(CalciteAssert.DB == CalciteAssert.DatabaseInstance.HSQLDB + || CalciteAssert.DB == DatabaseInstance.POSTGRESQL) + .planHasSql(jdbcSql) + .returnsCount(4); + } Review Comment: how about a test with 2 nested correlated subqueries, i.e., nested loops? ########## core/src/main/java/org/apache/calcite/adapter/jdbc/JdbcImplementor.java: ########## @@ -18,20 +18,66 @@ import org.apache.calcite.adapter.java.JavaTypeFactory; import org.apache.calcite.rel.RelNode; +import org.apache.calcite.rel.core.CorrelationId; import org.apache.calcite.rel.rel2sql.RelToSqlConverter; +import org.apache.calcite.rel.rel2sql.SqlImplementor; +import org.apache.calcite.rel.type.RelDataTypeField; +import org.apache.calcite.rex.RexCorrelVariable; import org.apache.calcite.sql.SqlDialect; -import org.apache.calcite.util.Util; +import org.apache.calcite.sql.SqlDynamicParam; +import org.apache.calcite.sql.SqlNode; +import org.apache.calcite.sql.parser.SqlParserPos; + +import java.lang.reflect.Type; +import java.util.List; /** * State for generating a SQL statement. */ public class JdbcImplementor extends RelToSqlConverter { - public JdbcImplementor(SqlDialect dialect, JavaTypeFactory typeFactory) { + + private final JdbcCorrelationDataContextBuilder dataContextBuilder; + private final JavaTypeFactory typeFactory; + + public JdbcImplementor(SqlDialect dialect, JavaTypeFactory typeFactory, Review Comment: I don't think you can change public methods in public classes, you need to provide a new constructor and deprecate this one. ########## core/src/main/java/org/apache/calcite/adapter/jdbc/JdbcCorrelationDataContextBuilder.java: ########## @@ -0,0 +1,28 @@ +/* + * 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.adapter.jdbc; + +import org.apache.calcite.rel.core.CorrelationId; + +import java.lang.reflect.Type; + +/** + * A class to build an object of type JdbcCorrelationDataContext. + */ +public interface JdbcCorrelationDataContextBuilder { + int add(CorrelationId id, int ordinal, Type type); Review Comment: I think you should document the methods in an interface. I am also not sure how "add" builds an object. -- 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]
