org.apache.calcite.sql.SqlUpdate Java Examples
The following examples show how to use
org.apache.calcite.sql.SqlUpdate.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SqlValidatorImpl.java From Flink-CEPplus with Apache License 2.0 | 6 votes |
/** * Creates the SELECT statement that putatively feeds rows into an UPDATE * statement to be updated. * * @param call Call to the UPDATE operator * @return select statement */ protected SqlSelect createSourceSelectForUpdate(SqlUpdate call) { final SqlNodeList selectList = new SqlNodeList(SqlParserPos.ZERO); selectList.add(SqlIdentifier.star(SqlParserPos.ZERO)); int ordinal = 0; for (SqlNode exp : call.getSourceExpressionList()) { // Force unique aliases to avoid a duplicate for Y with // SET X=Y String alias = SqlUtil.deriveAliasFromOrdinal(ordinal); selectList.add(SqlValidatorUtil.addAlias(exp, alias)); ++ordinal; } SqlNode sourceTable = call.getTargetTable(); if (call.getAlias() != null) { sourceTable = SqlValidatorUtil.addAlias( sourceTable, call.getAlias().getSimple()); } return new SqlSelect(SqlParserPos.ZERO, null, selectList, sourceTable, call.getCondition(), null, null, null, null, null, null); }
Example #2
Source File: SqlValidatorImpl.java From Flink-CEPplus with Apache License 2.0 | 6 votes |
public void validateUpdate(SqlUpdate call) { final SqlValidatorNamespace targetNamespace = getNamespace(call); validateNamespace(targetNamespace, unknownType); final RelOptTable relOptTable = SqlValidatorUtil.getRelOptTable( targetNamespace, catalogReader.unwrap(Prepare.CatalogReader.class), null, null); final SqlValidatorTable table = relOptTable == null ? targetNamespace.getTable() : relOptTable.unwrap(SqlValidatorTable.class); final RelDataType targetRowType = createTargetRowType( table, call.getTargetColumnList(), true); final SqlSelect select = call.getSourceSelect(); validateSelect(select, targetRowType); final RelDataType sourceRowType = getNamespace(call).getRowType(); checkTypeAssignment(sourceRowType, targetRowType, call); checkConstraint(table, call, targetRowType); validateAccess(call.getTargetTable(), table, SqlAccessEnum.UPDATE); }
Example #3
Source File: SqlValidatorImpl.java From flink with Apache License 2.0 | 6 votes |
public void validateUpdate(SqlUpdate call) { final SqlValidatorNamespace targetNamespace = getNamespace(call); validateNamespace(targetNamespace, unknownType); final RelOptTable relOptTable = SqlValidatorUtil.getRelOptTable( targetNamespace, catalogReader.unwrap(Prepare.CatalogReader.class), null, null); final SqlValidatorTable table = relOptTable == null ? targetNamespace.getTable() : relOptTable.unwrap(SqlValidatorTable.class); final RelDataType targetRowType = createTargetRowType( table, call.getTargetColumnList(), true); final SqlSelect select = call.getSourceSelect(); validateSelect(select, targetRowType); final RelDataType sourceRowType = getNamespace(call).getRowType(); checkTypeAssignment(sourceRowType, targetRowType, call); checkConstraint(table, call, targetRowType); validateAccess(call.getTargetTable(), table, SqlAccessEnum.UPDATE); }
Example #4
Source File: SqlValidatorImpl.java From flink with Apache License 2.0 | 6 votes |
/** * Creates the SELECT statement that putatively feeds rows into an UPDATE * statement to be updated. * * @param call Call to the UPDATE operator * @return select statement */ protected SqlSelect createSourceSelectForUpdate(SqlUpdate call) { final SqlNodeList selectList = new SqlNodeList(SqlParserPos.ZERO); selectList.add(SqlIdentifier.star(SqlParserPos.ZERO)); int ordinal = 0; for (SqlNode exp : call.getSourceExpressionList()) { // Force unique aliases to avoid a duplicate for Y with // SET X=Y String alias = SqlUtil.deriveAliasFromOrdinal(ordinal); selectList.add(SqlValidatorUtil.addAlias(exp, alias)); ++ordinal; } SqlNode sourceTable = call.getTargetTable(); if (call.getAlias() != null) { sourceTable = SqlValidatorUtil.addAlias( sourceTable, call.getAlias().getSimple()); } return new SqlSelect(SqlParserPos.ZERO, null, selectList, sourceTable, call.getCondition(), null, null, null, null, null, null); }
Example #5
Source File: TypeCoercionImpl.java From calcite with Apache License 2.0 | 5 votes |
/** * Coerces the field expression at index {@code columnIndex} of source * in an INSERT or UPDATE query to target type. * * @param sourceScope Query source scope * @param query Query * @param columnIndex Source column index to coerce type * @param targetType Target type */ private boolean coerceSourceRowType( SqlValidatorScope sourceScope, SqlNode query, int columnIndex, RelDataType targetType) { switch (query.getKind()) { case INSERT: SqlInsert insert = (SqlInsert) query; return coerceSourceRowType(sourceScope, insert.getSource(), columnIndex, targetType); case UPDATE: SqlUpdate update = (SqlUpdate) query; if (update.getSourceExpressionList() != null) { final SqlNodeList sourceExpressionList = update.getSourceExpressionList(); return coerceColumnType(sourceScope, sourceExpressionList, columnIndex, targetType); } else { return coerceSourceRowType(sourceScope, update.getSourceSelect(), columnIndex, targetType); } default: return rowTypeCoercion(sourceScope, query, columnIndex, targetType); } }
Example #6
Source File: SqlValidatorImpl.java From Flink-CEPplus with Apache License 2.0 | 5 votes |
/** * 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); } } } }
Example #7
Source File: SqlValidatorImpl.java From Flink-CEPplus with Apache License 2.0 | 5 votes |
/** * Locates the n'th expression in an INSERT or UPDATE query. * * @param query Query * @param ordinal Ordinal of expression * @param sourceCount Number of expressions * @return Ordinal'th expression, never null */ private SqlNode getNthExpr(SqlNode query, int ordinal, int sourceCount) { if (query instanceof SqlInsert) { SqlInsert insert = (SqlInsert) query; if (insert.getTargetColumnList() != null) { return insert.getTargetColumnList().get(ordinal); } else { return getNthExpr( insert.getSource(), ordinal, sourceCount); } } else if (query instanceof SqlUpdate) { SqlUpdate update = (SqlUpdate) query; if (update.getTargetColumnList() != null) { return update.getTargetColumnList().get(ordinal); } else if (update.getSourceExpressionList() != null) { return update.getSourceExpressionList().get(ordinal); } else { return getNthExpr( update.getSourceSelect(), ordinal, sourceCount); } } else if (query instanceof SqlSelect) { SqlSelect select = (SqlSelect) query; if (select.getSelectList().size() == sourceCount) { return select.getSelectList().get(ordinal); } else { return query; // give up } } else { return query; // give up } }
Example #8
Source File: SqlValidatorImpl.java From flink with Apache License 2.0 | 5 votes |
/** * 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); } } } }
Example #9
Source File: SqlValidatorImpl.java From flink with Apache License 2.0 | 5 votes |
/** * Locates the n'th expression in an INSERT or UPDATE query. * * @param query Query * @param ordinal Ordinal of expression * @param sourceCount Number of expressions * @return Ordinal'th expression, never null */ private SqlNode getNthExpr(SqlNode query, int ordinal, int sourceCount) { if (query instanceof SqlInsert) { SqlInsert insert = (SqlInsert) query; if (insert.getTargetColumnList() != null) { return insert.getTargetColumnList().get(ordinal); } else { return getNthExpr( insert.getSource(), ordinal, sourceCount); } } else if (query instanceof SqlUpdate) { SqlUpdate update = (SqlUpdate) query; if (update.getTargetColumnList() != null) { return update.getTargetColumnList().get(ordinal); } else if (update.getSourceExpressionList() != null) { return update.getSourceExpressionList().get(ordinal); } else { return getNthExpr( update.getSourceSelect(), ordinal, sourceCount); } } else if (query instanceof SqlSelect) { SqlSelect select = (SqlSelect) query; if (select.getSelectList().size() == sourceCount) { return select.getSelectList().get(ordinal); } else { return query; // give up } } else { return query; // give up } }
Example #10
Source File: SqlValidatorImpl.java From Flink-CEPplus with Apache License 2.0 | 4 votes |
private SqlNode rewriteUpdateToMerge( SqlUpdate updateCall, SqlNode selfJoinSrcExpr) { // Make sure target has an alias. if (updateCall.getAlias() == null) { updateCall.setAlias( new SqlIdentifier(UPDATE_TGT_ALIAS, SqlParserPos.ZERO)); } SqlNode selfJoinTgtExpr = getSelfJoinExprForUpdate( updateCall.getTargetTable(), updateCall.getAlias().getSimple()); assert selfJoinTgtExpr != null; // Create join condition between source and target exprs, // creating a conjunction with the user-level WHERE // clause if one was supplied SqlNode condition = updateCall.getCondition(); SqlNode selfJoinCond = SqlStdOperatorTable.EQUALS.createCall( SqlParserPos.ZERO, selfJoinSrcExpr, selfJoinTgtExpr); if (condition == null) { condition = selfJoinCond; } else { condition = SqlStdOperatorTable.AND.createCall( SqlParserPos.ZERO, selfJoinCond, condition); } SqlNode target = updateCall.getTargetTable().clone(SqlParserPos.ZERO); // For the source, we need to anonymize the fields, so // that for a statement like UPDATE T SET I = I + 1, // there's no ambiguity for the "I" in "I + 1"; // this is OK because the source and target have // identical values due to the self-join. // Note that we anonymize the source rather than the // target because downstream, the optimizer rules // don't want to see any projection on top of the target. IdentifierNamespace ns = new IdentifierNamespace(this, target, null, null); RelDataType rowType = ns.getRowType(); SqlNode source = updateCall.getTargetTable().clone(SqlParserPos.ZERO); final SqlNodeList selectList = new SqlNodeList(SqlParserPos.ZERO); int i = 1; for (RelDataTypeField field : rowType.getFieldList()) { SqlIdentifier col = new SqlIdentifier( field.getName(), SqlParserPos.ZERO); selectList.add( SqlValidatorUtil.addAlias(col, UPDATE_ANON_PREFIX + i)); ++i; } source = new SqlSelect(SqlParserPos.ZERO, null, selectList, source, null, null, null, null, null, null, null); source = SqlValidatorUtil.addAlias(source, UPDATE_SRC_ALIAS); SqlMerge mergeCall = new SqlMerge(updateCall.getParserPosition(), target, condition, source, updateCall, null, null, updateCall.getAlias()); rewriteMerge(mergeCall); return mergeCall; }
Example #11
Source File: SqlValidatorImpl.java From flink with Apache License 2.0 | 4 votes |
public SqlUpdate getNode() { return node; }
Example #12
Source File: SqlValidatorImpl.java From flink with Apache License 2.0 | 4 votes |
UpdateNamespace(SqlValidatorImpl validator, SqlUpdate node, SqlNode enclosingNode, SqlValidatorScope parentScope) { super(validator, node.getTargetTable(), enclosingNode, parentScope); this.node = Objects.requireNonNull(node); }
Example #13
Source File: SqlValidatorImpl.java From flink with Apache License 2.0 | 4 votes |
private SqlNode rewriteUpdateToMerge( SqlUpdate updateCall, SqlNode selfJoinSrcExpr) { // Make sure target has an alias. if (updateCall.getAlias() == null) { updateCall.setAlias( new SqlIdentifier(UPDATE_TGT_ALIAS, SqlParserPos.ZERO)); } SqlNode selfJoinTgtExpr = getSelfJoinExprForUpdate( updateCall.getTargetTable(), updateCall.getAlias().getSimple()); assert selfJoinTgtExpr != null; // Create join condition between source and target exprs, // creating a conjunction with the user-level WHERE // clause if one was supplied SqlNode condition = updateCall.getCondition(); SqlNode selfJoinCond = SqlStdOperatorTable.EQUALS.createCall( SqlParserPos.ZERO, selfJoinSrcExpr, selfJoinTgtExpr); if (condition == null) { condition = selfJoinCond; } else { condition = SqlStdOperatorTable.AND.createCall( SqlParserPos.ZERO, selfJoinCond, condition); } SqlNode target = updateCall.getTargetTable().clone(SqlParserPos.ZERO); // For the source, we need to anonymize the fields, so // that for a statement like UPDATE T SET I = I + 1, // there's no ambiguity for the "I" in "I + 1"; // this is OK because the source and target have // identical values due to the self-join. // Note that we anonymize the source rather than the // target because downstream, the optimizer rules // don't want to see any projection on top of the target. IdentifierNamespace ns = new IdentifierNamespace(this, target, null, null); RelDataType rowType = ns.getRowType(); SqlNode source = updateCall.getTargetTable().clone(SqlParserPos.ZERO); final SqlNodeList selectList = new SqlNodeList(SqlParserPos.ZERO); int i = 1; for (RelDataTypeField field : rowType.getFieldList()) { SqlIdentifier col = new SqlIdentifier( field.getName(), SqlParserPos.ZERO); selectList.add( SqlValidatorUtil.addAlias(col, UPDATE_ANON_PREFIX + i)); ++i; } source = new SqlSelect(SqlParserPos.ZERO, null, selectList, source, null, null, null, null, null, null, null); source = SqlValidatorUtil.addAlias(source, UPDATE_SRC_ALIAS); SqlMerge mergeCall = new SqlMerge(updateCall.getParserPosition(), target, condition, source, updateCall, null, null, updateCall.getAlias()); rewriteMerge(mergeCall); return mergeCall; }
Example #14
Source File: SqlValidatorImpl.java From Flink-CEPplus with Apache License 2.0 | 4 votes |
public SqlUpdate getNode() { return node; }
Example #15
Source File: SqlValidatorImpl.java From Flink-CEPplus with Apache License 2.0 | 4 votes |
UpdateNamespace(SqlValidatorImpl validator, SqlUpdate node, SqlNode enclosingNode, SqlValidatorScope parentScope) { super(validator, node.getTargetTable(), enclosingNode, parentScope); this.node = Objects.requireNonNull(node); }
Example #16
Source File: SqlValidator.java From Bats with Apache License 2.0 | 2 votes |
/** * Validates an UPDATE statement. * * @param update UPDATE statement */ void validateUpdate(SqlUpdate update);
Example #17
Source File: SqlValidator.java From calcite with Apache License 2.0 | 2 votes |
/** * Validates an UPDATE statement. * * @param update UPDATE statement */ void validateUpdate(SqlUpdate update);