Search in sources :

Example 1 with ModifiableViewTable

use of org.apache.calcite.schema.impl.ModifiableViewTable in project flink by apache.

the class SqlValidatorImpl method checkConstraint.

/**
 * Validates updates against the constraint of a modifiable view.
 *
 * @param validatorTable A {@link SqlValidatorTable} that may wrap a ModifiableViewTable
 * @param update The UPDATE parse tree node
 * @param targetRowType The target type
 */
private void checkConstraint(SqlValidatorTable validatorTable, SqlUpdate update, RelDataType targetRowType) {
    final ModifiableViewTable modifiableViewTable = validatorTable.unwrap(ModifiableViewTable.class);
    if (modifiableViewTable != null) {
        final Table table = modifiableViewTable.unwrap(Table.class);
        final RelDataType tableRowType = table.getRowType(typeFactory);
        final Map<Integer, RexNode> projectMap = RelOptUtil.getColumnConstraints(modifiableViewTable, targetRowType, typeFactory);
        final Map<String, Integer> nameToIndex = SqlValidatorUtil.mapNameToIndex(tableRowType.getFieldList());
        // Validate update values against the view constraint.
        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);
                final ValidationError validationError = new ValidationError(column.right, RESOURCE.viewConstraintNotSatisfied(columnName, Util.last(validatorTable.getQualifiedName())));
                RelOptUtil.validateValueAgainstConstraint(column.right, columnConstraint, validationError);
            }
        }
    }
}
Also used : Table(org.apache.calcite.schema.Table) SqlStdOperatorTable(org.apache.calcite.sql.fun.SqlStdOperatorTable) ModifiableViewTable(org.apache.calcite.schema.impl.ModifiableViewTable) RelOptTable(org.apache.calcite.plan.RelOptTable) SqlOperatorTable(org.apache.calcite.sql.SqlOperatorTable) RelDataType(org.apache.calcite.rel.type.RelDataType) BitString(org.apache.calcite.util.BitString) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) BigInteger(java.math.BigInteger) ModifiableViewTable(org.apache.calcite.schema.impl.ModifiableViewTable) RexNode(org.apache.calcite.rex.RexNode) SqlNode(org.apache.calcite.sql.SqlNode)

Example 2 with ModifiableViewTable

use of org.apache.calcite.schema.impl.ModifiableViewTable in project flink by apache.

the class SqlValidatorImpl method checkConstraint.

/**
 * Validates insert values against the constraint of a modifiable view.
 *
 * @param validatorTable Table that may wrap a ModifiableViewTable
 * @param source The values being inserted
 * @param targetRowType The target type for the view
 */
private void checkConstraint(SqlValidatorTable validatorTable, SqlNode source, RelDataType targetRowType) {
    final ModifiableViewTable modifiableViewTable = validatorTable.unwrap(ModifiableViewTable.class);
    if (modifiableViewTable != null && source instanceof SqlCall) {
        final Table table = modifiableViewTable.unwrap(Table.class);
        final RelDataType tableRowType = table.getRowType(typeFactory);
        final List<RelDataTypeField> tableFields = tableRowType.getFieldList();
        // Get the mapping from column indexes of the underlying table
        // to the target columns and view constraints.
        final Map<Integer, RelDataTypeField> tableIndexToTargetField = SqlValidatorUtil.getIndexToFieldMap(tableFields, targetRowType);
        final Map<Integer, RexNode> projectMap = RelOptUtil.getColumnConstraints(modifiableViewTable, targetRowType, typeFactory);
        // Determine columns (indexed to the underlying table) that need
        // to be validated against the view constraint.
        final ImmutableBitSet targetColumns = ImmutableBitSet.of(tableIndexToTargetField.keySet());
        final ImmutableBitSet constrainedColumns = ImmutableBitSet.of(projectMap.keySet());
        final ImmutableBitSet constrainedTargetColumns = targetColumns.intersect(constrainedColumns);
        // Validate insert values against the view constraint.
        final List<SqlNode> values = ((SqlCall) source).getOperandList();
        for (final int colIndex : constrainedTargetColumns.asList()) {
            final String colName = tableFields.get(colIndex).getName();
            final RelDataTypeField targetField = tableIndexToTargetField.get(colIndex);
            for (SqlNode row : values) {
                final SqlCall call = (SqlCall) row;
                final SqlNode sourceValue = call.operand(targetField.getIndex());
                final ValidationError validationError = new ValidationError(sourceValue, RESOURCE.viewConstraintNotSatisfied(colName, Util.last(validatorTable.getQualifiedName())));
                RelOptUtil.validateValueAgainstConstraint(sourceValue, projectMap.get(colIndex), validationError);
            }
        }
    }
}
Also used : Table(org.apache.calcite.schema.Table) SqlStdOperatorTable(org.apache.calcite.sql.fun.SqlStdOperatorTable) ModifiableViewTable(org.apache.calcite.schema.impl.ModifiableViewTable) RelOptTable(org.apache.calcite.plan.RelOptTable) SqlOperatorTable(org.apache.calcite.sql.SqlOperatorTable) ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) SqlCall(org.apache.calcite.sql.SqlCall) RelDataType(org.apache.calcite.rel.type.RelDataType) BitString(org.apache.calcite.util.BitString) BigInteger(java.math.BigInteger) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) ModifiableViewTable(org.apache.calcite.schema.impl.ModifiableViewTable) RexNode(org.apache.calcite.rex.RexNode) SqlNode(org.apache.calcite.sql.SqlNode)

Example 3 with ModifiableViewTable

use of org.apache.calcite.schema.impl.ModifiableViewTable in project calcite by apache.

the class SqlValidatorImpl method checkConstraint.

/**
 * Validates updates against the constraint of a modifiable view.
 *
 * @param validatorTable A {@link SqlValidatorTable} that may wrap a
 *                       ModifiableViewTable
 * @param update         The UPDATE parse tree node
 * @param targetRowType  The target type
 */
private void checkConstraint(SqlValidatorTable validatorTable, SqlUpdate update, RelDataType targetRowType) {
    final ModifiableViewTable modifiableViewTable = validatorTable.unwrap(ModifiableViewTable.class);
    if (modifiableViewTable != null) {
        final Table table = modifiableViewTable.unwrap(Table.class);
        final RelDataType tableRowType = table.getRowType(typeFactory);
        final Map<Integer, RexNode> projectMap = RelOptUtil.getColumnConstraints(modifiableViewTable, targetRowType, typeFactory);
        final Map<String, Integer> nameToIndex = SqlValidatorUtil.mapNameToIndex(tableRowType.getFieldList());
        // Validate update values against the view constraint.
        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);
                final ValidationError validationError = new ValidationError(column.right, RESOURCE.viewConstraintNotSatisfied(columnName, Util.last(validatorTable.getQualifiedName())));
                RelOptUtil.validateValueAgainstConstraint(column.right, columnConstraint, validationError);
            }
        }
    }
}
Also used : Table(org.apache.calcite.schema.Table) SqlStdOperatorTable(org.apache.calcite.sql.fun.SqlStdOperatorTable) ModifiableViewTable(org.apache.calcite.schema.impl.ModifiableViewTable) RelOptTable(org.apache.calcite.plan.RelOptTable) SqlOperatorTable(org.apache.calcite.sql.SqlOperatorTable) RelDataType(org.apache.calcite.rel.type.RelDataType) BitString(org.apache.calcite.util.BitString) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) BigInteger(java.math.BigInteger) ModifiableViewTable(org.apache.calcite.schema.impl.ModifiableViewTable) RexNode(org.apache.calcite.rex.RexNode) SqlNode(org.apache.calcite.sql.SqlNode)

Example 4 with ModifiableViewTable

use of org.apache.calcite.schema.impl.ModifiableViewTable in project calcite by apache.

the class SqlValidatorImpl method checkConstraint.

/**
 * Validates insert values against the constraint of a modifiable view.
 *
 * @param validatorTable Table that may wrap a ModifiableViewTable
 * @param source        The values being inserted
 * @param targetRowType The target type for the view
 */
private void checkConstraint(SqlValidatorTable validatorTable, SqlNode source, RelDataType targetRowType) {
    final ModifiableViewTable modifiableViewTable = validatorTable.unwrap(ModifiableViewTable.class);
    if (modifiableViewTable != null && source instanceof SqlCall) {
        final Table table = modifiableViewTable.unwrap(Table.class);
        final RelDataType tableRowType = table.getRowType(typeFactory);
        final List<RelDataTypeField> tableFields = tableRowType.getFieldList();
        // Get the mapping from column indexes of the underlying table
        // to the target columns and view constraints.
        final Map<Integer, RelDataTypeField> tableIndexToTargetField = SqlValidatorUtil.getIndexToFieldMap(tableFields, targetRowType);
        final Map<Integer, RexNode> projectMap = RelOptUtil.getColumnConstraints(modifiableViewTable, targetRowType, typeFactory);
        // Determine columns (indexed to the underlying table) that need
        // to be validated against the view constraint.
        final ImmutableBitSet targetColumns = ImmutableBitSet.of(tableIndexToTargetField.keySet());
        final ImmutableBitSet constrainedColumns = ImmutableBitSet.of(projectMap.keySet());
        final ImmutableBitSet constrainedTargetColumns = targetColumns.intersect(constrainedColumns);
        // Validate insert values against the view constraint.
        final List<SqlNode> values = ((SqlCall) source).getOperandList();
        for (final int colIndex : constrainedTargetColumns.asList()) {
            final String colName = tableFields.get(colIndex).getName();
            final RelDataTypeField targetField = tableIndexToTargetField.get(colIndex);
            for (SqlNode row : values) {
                final SqlCall call = (SqlCall) row;
                final SqlNode sourceValue = call.operand(targetField.getIndex());
                final ValidationError validationError = new ValidationError(sourceValue, RESOURCE.viewConstraintNotSatisfied(colName, Util.last(validatorTable.getQualifiedName())));
                RelOptUtil.validateValueAgainstConstraint(sourceValue, projectMap.get(colIndex), validationError);
            }
        }
    }
}
Also used : Table(org.apache.calcite.schema.Table) SqlStdOperatorTable(org.apache.calcite.sql.fun.SqlStdOperatorTable) ModifiableViewTable(org.apache.calcite.schema.impl.ModifiableViewTable) RelOptTable(org.apache.calcite.plan.RelOptTable) SqlOperatorTable(org.apache.calcite.sql.SqlOperatorTable) ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) SqlCall(org.apache.calcite.sql.SqlCall) RelDataType(org.apache.calcite.rel.type.RelDataType) BitString(org.apache.calcite.util.BitString) BigInteger(java.math.BigInteger) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) ModifiableViewTable(org.apache.calcite.schema.impl.ModifiableViewTable) RexNode(org.apache.calcite.rex.RexNode) SqlNode(org.apache.calcite.sql.SqlNode)

Example 5 with ModifiableViewTable

use of org.apache.calcite.schema.impl.ModifiableViewTable in project calcite by apache.

the class TableNamespace method extend.

/**
 * Creates a TableNamespace based on the same table as this one, but with
 * extended fields.
 *
 * <p>Extended fields are "hidden" or undeclared fields that may nevertheless
 * be present if you ask for them. Phoenix uses them, for instance, to access
 * rarely used fields in the underlying HBase table.
 */
public TableNamespace extend(SqlNodeList extendList) {
    final List<SqlNode> identifierList = Util.quotientList(extendList.getList(), 2, 0);
    SqlValidatorUtil.checkIdentifierListForDuplicates(identifierList, validator.getValidationErrorFunction());
    final ImmutableList.Builder<RelDataTypeField> builder = ImmutableList.builder();
    builder.addAll(this.extendedFields);
    builder.addAll(SqlValidatorUtil.getExtendedColumns(validator.getTypeFactory(), getTable(), extendList));
    final List<RelDataTypeField> extendedFields = builder.build();
    final Table schemaTable = table.unwrap(Table.class);
    if (schemaTable != null && table instanceof RelOptTable && (schemaTable instanceof ExtensibleTable || schemaTable instanceof ModifiableViewTable)) {
        checkExtendedColumnTypes(extendList);
        final RelOptTable relOptTable = ((RelOptTable) table).extend(extendedFields);
        final SqlValidatorTable validatorTable = relOptTable.unwrap(SqlValidatorTable.class);
        return new TableNamespace(validator, validatorTable, ImmutableList.<RelDataTypeField>of());
    }
    return new TableNamespace(validator, table, extendedFields);
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) Table(org.apache.calcite.schema.Table) ExtensibleTable(org.apache.calcite.schema.ExtensibleTable) ModifiableViewTable(org.apache.calcite.schema.impl.ModifiableViewTable) RelOptTable(org.apache.calcite.plan.RelOptTable) ImmutableList(com.google.common.collect.ImmutableList) RelOptTable(org.apache.calcite.plan.RelOptTable) ExtensibleTable(org.apache.calcite.schema.ExtensibleTable) ModifiableViewTable(org.apache.calcite.schema.impl.ModifiableViewTable) SqlNode(org.apache.calcite.sql.SqlNode)

Aggregations

RelOptTable (org.apache.calcite.plan.RelOptTable)5 Table (org.apache.calcite.schema.Table)5 ModifiableViewTable (org.apache.calcite.schema.impl.ModifiableViewTable)5 SqlNode (org.apache.calcite.sql.SqlNode)5 BigInteger (java.math.BigInteger)4 RelDataType (org.apache.calcite.rel.type.RelDataType)4 RexNode (org.apache.calcite.rex.RexNode)4 SqlOperatorTable (org.apache.calcite.sql.SqlOperatorTable)4 SqlStdOperatorTable (org.apache.calcite.sql.fun.SqlStdOperatorTable)4 BitString (org.apache.calcite.util.BitString)4 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)3 SqlCall (org.apache.calcite.sql.SqlCall)2 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)2 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)2 ImmutableList (com.google.common.collect.ImmutableList)1 ExtensibleTable (org.apache.calcite.schema.ExtensibleTable)1