You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by jh...@apache.org on 2017/04/11 22:36:27 UTC
[1/2] calcite git commit: Following [CALCITE-1666],
factor Rex dependencies out of SqlValidatorImpl (Kevin Liew)
Repository: calcite
Updated Branches:
refs/heads/master 9247cb990 -> 059142fe5
Following [CALCITE-1666], factor Rex dependencies out of SqlValidatorImpl (Kevin Liew)
Close apache/calcite#419
Project: http://git-wip-us.apache.org/repos/asf/calcite/repo
Commit: http://git-wip-us.apache.org/repos/asf/calcite/commit/059142fe
Tree: http://git-wip-us.apache.org/repos/asf/calcite/tree/059142fe
Diff: http://git-wip-us.apache.org/repos/asf/calcite/diff/059142fe
Branch: refs/heads/master
Commit: 059142fe5de9519e0289f3ff063cca9b2e7b5f04
Parents: fa8defd
Author: kliewkliew <kl...@users.noreply.github.com>
Authored: Fri Apr 7 20:07:39 2017 -0700
Committer: Julian Hyde <jh...@apache.org>
Committed: Tue Apr 11 15:35:36 2017 -0700
----------------------------------------------------------------------
.../org/apache/calcite/plan/RelOptUtil.java | 60 +++++++++++
.../calcite/sql/validate/SqlValidatorImpl.java | 108 +++++++------------
.../calcite/sql/validate/SqlValidatorUtil.java | 2 +-
3 files changed, 102 insertions(+), 68 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/calcite/blob/059142fe/core/src/main/java/org/apache/calcite/plan/RelOptUtil.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/calcite/plan/RelOptUtil.java b/core/src/main/java/org/apache/calcite/plan/RelOptUtil.java
index ac44add..9af225e 100644
--- a/core/src/main/java/org/apache/calcite/plan/RelOptUtil.java
+++ b/core/src/main/java/org/apache/calcite/plan/RelOptUtil.java
@@ -73,13 +73,20 @@ import org.apache.calcite.rex.RexNode;
import org.apache.calcite.rex.RexOver;
import org.apache.calcite.rex.RexProgram;
import org.apache.calcite.rex.RexShuttle;
+import org.apache.calcite.rex.RexSqlStandardConvertletTable;
import org.apache.calcite.rex.RexSubQuery;
+import org.apache.calcite.rex.RexToSqlNodeConverter;
+import org.apache.calcite.rex.RexToSqlNodeConverterImpl;
import org.apache.calcite.rex.RexUtil;
import org.apache.calcite.rex.RexVisitorImpl;
+import org.apache.calcite.runtime.CalciteContextException;
import org.apache.calcite.runtime.PredicateImpl;
+import org.apache.calcite.schema.ModifiableView;
import org.apache.calcite.sql.SqlExplainFormat;
import org.apache.calcite.sql.SqlExplainLevel;
import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.sql.SqlLiteral;
+import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.SqlOperator;
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
import org.apache.calcite.sql.type.MultisetSqlType;
@@ -98,10 +105,12 @@ import org.apache.calcite.util.mapping.Mappings;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
+import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import java.io.PrintWriter;
@@ -2229,6 +2238,57 @@ public abstract class RelOptUtil {
}
/**
+ * Returns a mapping of the column ordinal in the underlying table to a column
+ * constraint of the modifiable view.
+ *
+ * @param modifiableViewTable The modifiable view which has a constraint
+ * @param targetRowType The target type
+ */
+ public static Map<Integer, RexNode> getColumnConstraints(
+ ModifiableView modifiableViewTable, RelDataType targetRowType,
+ RelDataTypeFactory typeFactory) {
+ final RexBuilder rexBuilder = new RexBuilder(typeFactory);
+ final RexNode constraint =
+ modifiableViewTable.getConstraint(rexBuilder, targetRowType);
+ final Map<Integer, RexNode> projectMap = Maps.newHashMap();
+ final List<RexNode> filters = new ArrayList<>();
+ RelOptUtil.inferViewPredicates(projectMap, filters, constraint);
+ assert filters.isEmpty();
+ return projectMap;
+ }
+
+ /**
+ * Ensures that a source value does not violate the constraint of the target
+ * column.
+ *
+ * @param sourceValue The insert value being validated
+ * @param targetConstraint The constraint applied to sourceValue for validation
+ * @param errorSupplier The function to apply when validation fails
+ */
+ public static void validateValueAgainstConstraint(SqlNode sourceValue,
+ RexNode targetConstraint,
+ Supplier<CalciteContextException> errorSupplier) {
+ if (!(sourceValue instanceof SqlLiteral)) {
+ // We cannot guarantee that the value satisfies the constraint.
+ throw errorSupplier.get();
+ }
+ final SqlLiteral insertValue = (SqlLiteral) sourceValue;
+ final RexLiteral columnConstraint = (RexLiteral) targetConstraint;
+
+ final RexSqlStandardConvertletTable convertletTable =
+ new RexSqlStandardConvertletTable();
+ final RexToSqlNodeConverter sqlNodeToRexConverter =
+ new RexToSqlNodeConverterImpl(convertletTable);
+ final SqlLiteral constraintValue =
+ (SqlLiteral) sqlNodeToRexConverter.convertLiteral(columnConstraint);
+
+ if (!insertValue.equals(constraintValue)) {
+ // The value does not satisfy the constraint.
+ throw errorSupplier.get();
+ }
+ }
+
+ /**
* Adjusts key values in a list by some fixed amount.
*
* @param keys list of key values
http://git-wip-us.apache.org/repos/asf/calcite/blob/059142fe/core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java b/core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java
index ff7c38b..323bf19 100644
--- a/core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java
+++ b/core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java
@@ -29,11 +29,7 @@ import org.apache.calcite.rel.type.RelDataTypeField;
import org.apache.calcite.rel.type.RelDataTypeSystem;
import org.apache.calcite.rel.type.RelRecordType;
import org.apache.calcite.rex.RexBuilder;
-import org.apache.calcite.rex.RexLiteral;
import org.apache.calcite.rex.RexNode;
-import org.apache.calcite.rex.RexSqlStandardConvertletTable;
-import org.apache.calcite.rex.RexToSqlNodeConverter;
-import org.apache.calcite.rex.RexToSqlNodeConverterImpl;
import org.apache.calcite.runtime.CalciteContextException;
import org.apache.calcite.runtime.CalciteException;
import org.apache.calcite.runtime.Feature;
@@ -103,10 +99,10 @@ import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
+import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
-import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.slf4j.Logger;
@@ -3893,7 +3889,7 @@ public class SqlValidatorImpl implements SqlValidatorWithHints {
final Map<Integer, RelDataTypeField> tableIndexToTargetField =
SqlValidatorUtil.getIndexToFieldMap(tableFields, targetRowType);
final Map<Integer, RexNode> projectMap =
- getConstraintForModifiableView(modifiableViewTable, targetRowType);
+ RelOptUtil.getColumnConstraints(modifiableViewTable, targetRowType, typeFactory);
// Determine columns (indexed to the underlying table) that need
// to be validated against the view constraint.
@@ -3912,32 +3908,18 @@ public class SqlValidatorImpl implements SqlValidatorWithHints {
for (SqlNode row : values) {
final SqlCall call = (SqlCall) row;
final SqlNode sourceValue = call.operand(targetField.getIndex());
- checkConstraint(validatorTable, colName, sourceValue, projectMap.get(colIndex));
+ final ValidationError validationError =
+ new ValidationError(sourceValue,
+ RESOURCE.viewConstraintNotSatisfied(colName,
+ Util.last(validatorTable.getQualifiedName())));
+ RelOptUtil.validateValueAgainstConstraint(sourceValue,
+ projectMap.get(colIndex), validationError);
}
}
}
}
/**
- * Returns a mapping of the column ordinal in the underlying table to a column
- * constraint of the modifiable view.
- *
- * @param modifiableViewTable The modifiable view which has a constraint
- * @param targetRowType The target type
- */
- private Map<Integer, RexNode> getConstraintForModifiableView(
- ModifiableViewTable modifiableViewTable, RelDataType targetRowType) {
- final RexBuilder rexBuilder = new RexBuilder(typeFactory);
- final RexNode constraint =
- modifiableViewTable.getConstraint(rexBuilder, targetRowType);
- final Map<Integer, RexNode> projectMap = Maps.newHashMap();
- final List<RexNode> filters = new ArrayList<>();
- RelOptUtil.inferViewPredicates(projectMap, filters, constraint);
- assert filters.isEmpty();
- return projectMap;
- }
-
- /**
* Validates updates against the constraint of a modifiable view.
*
* @param validatorTable A {@link SqlValidatorTable} that may wrap a
@@ -3956,58 +3938,30 @@ public class SqlValidatorImpl implements SqlValidatorWithHints {
final RelDataType tableRowType = table.getRowType(typeFactory);
final Map<Integer, RexNode> projectMap =
- getConstraintForModifiableView(modifiableViewTable, targetRowType);
+ RelOptUtil.getColumnConstraints(modifiableViewTable, targetRowType,
+ typeFactory);
final Map<String, Integer> nameToIndex =
SqlValidatorUtil.mapNameToIndex(tableRowType.getFieldList());
// Validate update values against the view constraint.
- for (final Pair<SqlNode, SqlNode> column : Pair.zip(update.getTargetColumnList().getList(),
- update.getSourceExpressionList().getList())) {
+ final List<SqlNode> targets = update.getTargetColumnList().getList();
+ final List<SqlNode> sources = update.getSourceExpressionList().getList();
+ for (final Pair<SqlNode, SqlNode> column : Pair.zip(targets, sources)) {
final String columnName = ((SqlIdentifier) column.left).getSimple();
final Integer columnIndex = nameToIndex.get(columnName);
if (projectMap.containsKey(columnIndex)) {
final RexNode columnConstraint = projectMap.get(columnIndex);
- checkConstraint(validatorTable, columnName, column.right, columnConstraint);
+ final ValidationError validationError =
+ new ValidationError(column.right,
+ RESOURCE.viewConstraintNotSatisfied(columnName,
+ Util.last(validatorTable.getQualifiedName())));
+ RelOptUtil.validateValueAgainstConstraint(column.right,
+ columnConstraint, validationError);
}
}
}
}
- /**
- * Ensures that a source value does not violate the constraint of the target column.
- *
- * @param table The SqlValidatorTable which wraps a ModifiableViewTable.
- * @param columnName The target column name.
- * @param sourceValue The insert value being validated.
- * @param targetConstraint The constraint applied to sourceValue for validation.
- */
- private void checkConstraint(
- SqlValidatorTable table, String columnName,
- SqlNode sourceValue, RexNode targetConstraint) {
- if (!(sourceValue instanceof SqlLiteral)) {
- // We cannot guarantee that the value satisfies the constraint.
- throw newValidationError(sourceValue,
- RESOURCE.viewConstraintNotSatisfied(
- columnName, Util.last(table.getQualifiedName())));
- }
- final SqlLiteral insertValue = (SqlLiteral) sourceValue;
- final RexLiteral columnConstraint = (RexLiteral) targetConstraint;
-
- final RexSqlStandardConvertletTable convertletTable =
- new RexSqlStandardConvertletTable();
- final RexToSqlNodeConverter sqlNodeToRexConverter =
- new RexToSqlNodeConverterImpl(convertletTable);
- final SqlLiteral constraintValue =
- (SqlLiteral) sqlNodeToRexConverter.convertLiteral(columnConstraint);
-
- if (!insertValue.equals(constraintValue)) {
- // The value does not satisfy the constraint.
- throw newValidationError(sourceValue,
- RESOURCE.viewConstraintNotSatisfied(
- columnName, Util.last(table.getQualifiedName())));
- }
- }
-
private void checkFieldCount(
SqlNode node,
SqlValidatorTable table,
@@ -4364,9 +4318,29 @@ public class SqlValidatorImpl implements SqlValidatorWithHints {
}
/**
- * Throws a validator exception.
+ * Throws a validator exception with access to the validator context.
+ * The exception is determined when an instance is created.
+ */
+ private class ValidationError implements Supplier<CalciteContextException> {
+ private final SqlNode sqlNode;
+ private final Resources.ExInst<SqlValidatorException> validatorException;
+
+ ValidationError(SqlNode sqlNode,
+ Resources.ExInst<SqlValidatorException> validatorException) {
+ this.sqlNode = sqlNode;
+ this.validatorException = validatorException;
+ }
+
+ public CalciteContextException get() {
+ return newValidationError(sqlNode, validatorException);
+ }
+ }
+
+ /**
+ * Throws a validator exception with access to the validator context.
+ * The exception is determined when the function is applied.
*/
- public class ValidationErrorFunction
+ class ValidationErrorFunction
implements Function2<SqlNode, Resources.ExInst<SqlValidatorException>,
CalciteContextException> {
@Override public CalciteContextException apply(
http://git-wip-us.apache.org/repos/asf/calcite/blob/059142fe/core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorUtil.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorUtil.java b/core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorUtil.java
index 27c3e03..8a2b84d 100644
--- a/core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorUtil.java
+++ b/core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorUtil.java
@@ -264,7 +264,7 @@ public class SqlValidatorUtil {
/**
* Checks that there are no duplicates in a list of {@link SqlIdentifier}.
*/
- public static void checkIdentifierListForDuplicates(List<SqlNode> columnList,
+ static void checkIdentifierListForDuplicates(List<SqlNode> columnList,
SqlValidatorImpl.ValidationErrorFunction validationErrorFunction) {
final List<String> names = Lists.transform(columnList,
new Function<SqlNode, String>() {
[2/2] calcite git commit: [CALCITE-1747] Enable RelMdColumnUniqueness
for HepRelVertex
Posted by jh...@apache.org.
[CALCITE-1747] Enable RelMdColumnUniqueness for HepRelVertex
Close apache/calcite#420
Project: http://git-wip-us.apache.org/repos/asf/calcite/repo
Commit: http://git-wip-us.apache.org/repos/asf/calcite/commit/fa8defdd
Tree: http://git-wip-us.apache.org/repos/asf/calcite/tree/fa8defdd
Diff: http://git-wip-us.apache.org/repos/asf/calcite/diff/fa8defdd
Branch: refs/heads/master
Commit: fa8defdd2709489215927362f87da97af1769674
Parents: 9247cb9
Author: Minji Kim <mi...@dremio.com>
Authored: Mon Apr 10 17:30:08 2017 -0700
Committer: Julian Hyde <jh...@apache.org>
Committed: Tue Apr 11 15:35:36 2017 -0700
----------------------------------------------------------------------
.../java/org/apache/calcite/rel/metadata/RelMdColumnUniqueness.java | 1 -
1 file changed, 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/calcite/blob/fa8defdd/core/src/main/java/org/apache/calcite/rel/metadata/RelMdColumnUniqueness.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/calcite/rel/metadata/RelMdColumnUniqueness.java b/core/src/main/java/org/apache/calcite/rel/metadata/RelMdColumnUniqueness.java
index 48c626e..9b5d38c 100644
--- a/core/src/main/java/org/apache/calcite/rel/metadata/RelMdColumnUniqueness.java
+++ b/core/src/main/java/org/apache/calcite/rel/metadata/RelMdColumnUniqueness.java
@@ -349,7 +349,6 @@ public class RelMdColumnUniqueness
}
public Boolean areColumnsUnique(HepRelVertex rel, RelMetadataQuery mq,
- boolean dummy, // prevent method from being used
ImmutableBitSet columns, boolean ignoreNulls) {
return mq.areColumnsUnique(rel.getCurrentRel(), columns, ignoreNulls);
}