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);
}
}
}
}
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);
}
}
}
}
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);
}
}
}
}
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);
}
}
}
}
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);
}
Aggregations