diff --git a/core/src/main/java/org/apache/calcite/adapter/jdbc/JdbcCorrelationDataContext.java b/core/src/main/java/org/apache/calcite/adapter/jdbc/JdbcCorrelationDataContext.java index f89f8726f19..802e490d83f 100644 --- a/core/src/main/java/org/apache/calcite/adapter/jdbc/JdbcCorrelationDataContext.java +++ b/core/src/main/java/org/apache/calcite/adapter/jdbc/JdbcCorrelationDataContext.java @@ -29,7 +29,7 @@ * A special DataContext which handles correlation variable for batch nested loop joins. */ public class JdbcCorrelationDataContext implements DataContext { - public static final int OFFSET = 10000; + public static final int OFFSET = Integer.MAX_VALUE - 10000; private final DataContext delegate; private final Object[] parameters; diff --git a/core/src/main/java/org/apache/calcite/adapter/jdbc/JdbcCorrelationDataContextBuilder.java b/core/src/main/java/org/apache/calcite/adapter/jdbc/JdbcCorrelationDataContextBuilder.java index 8c850580b87..1877fdbd1e2 100644 --- a/core/src/main/java/org/apache/calcite/adapter/jdbc/JdbcCorrelationDataContextBuilder.java +++ b/core/src/main/java/org/apache/calcite/adapter/jdbc/JdbcCorrelationDataContextBuilder.java @@ -21,8 +21,12 @@ import java.lang.reflect.Type; /** - * A class to build an object of type JdbcCorrelationDataContext. + * An interface to collect all correlation variables + * required to create a JdbcCorrelationDataContext. */ -public interface JdbcCorrelationDataContextBuilder { +interface JdbcCorrelationDataContextBuilder { + /** + * Collect a correlation variable. + */ int add(CorrelationId id, int ordinal, Type type); } diff --git a/core/src/main/java/org/apache/calcite/adapter/jdbc/JdbcImplementor.java b/core/src/main/java/org/apache/calcite/adapter/jdbc/JdbcImplementor.java index 294c08858e7..1485c4788e8 100644 --- a/core/src/main/java/org/apache/calcite/adapter/jdbc/JdbcImplementor.java +++ b/core/src/main/java/org/apache/calcite/adapter/jdbc/JdbcImplementor.java @@ -39,10 +39,10 @@ public class JdbcImplementor extends RelToSqlConverter { private final JdbcCorrelationDataContextBuilder dataContextBuilder; private final JavaTypeFactory typeFactory; - public JdbcImplementor(SqlDialect dialect, JavaTypeFactory typeFactory, + JdbcImplementor(SqlDialect dialect, JavaTypeFactory typeFactory, JdbcCorrelationDataContextBuilder dataContextBuilder) { super(dialect); - this. typeFactory = typeFactory; + this.typeFactory = typeFactory; this.dataContextBuilder = dataContextBuilder; } diff --git a/core/src/main/java/org/apache/calcite/rel/rel2sql/SqlImplementor.java b/core/src/main/java/org/apache/calcite/rel/rel2sql/SqlImplementor.java index 40a10e9cf16..470c9ada2e4 100644 --- a/core/src/main/java/org/apache/calcite/rel/rel2sql/SqlImplementor.java +++ b/core/src/main/java/org/apache/calcite/rel/rel2sql/SqlImplementor.java @@ -16,6 +16,7 @@ */ package org.apache.calcite.rel.rel2sql; +import org.apache.calcite.adapter.jdbc.JdbcCorrelationDataContext; import org.apache.calcite.config.CalciteSystemProperty; import org.apache.calcite.linq4j.Ord; import org.apache.calcite.linq4j.tree.Expressions; @@ -767,6 +768,11 @@ public SqlNode toSql(@Nullable RexProgram program, RexNode rex) { case DYNAMIC_PARAM: final RexDynamicParam caseParam = (RexDynamicParam) rex; + if (caseParam.getIndex() >= JdbcCorrelationDataContext.OFFSET) { + throw new AssertionError("More than " + + JdbcCorrelationDataContext.OFFSET + + " dynamic parameters used in query"); + } return new SqlDynamicParam(caseParam.getIndex(), POS); case IN: