use of org.apache.calcite.sql.SqlSelect in project calcite by apache.
the class SqlValidatorImpl method validateUpdate.
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);
}
use of org.apache.calcite.sql.SqlSelect in project calcite by apache.
the class SqlValidatorImpl method validateDelete.
public void validateDelete(SqlDelete call) {
final SqlSelect sqlSelect = call.getSourceSelect();
validateSelect(sqlSelect, unknownType);
final SqlValidatorNamespace targetNamespace = getNamespace(call);
validateNamespace(targetNamespace, unknownType);
final SqlValidatorTable table = targetNamespace.getTable();
validateAccess(call.getTargetTable(), table, SqlAccessEnum.DELETE);
}
use of org.apache.calcite.sql.SqlSelect in project calcite by apache.
the class SqlValidatorImpl method validateModality.
/**
* Validates that a query can deliver the modality it promises. Only called
* on the top-most SELECT or set operator in the tree.
*/
private void validateModality(SqlNode query) {
final SqlModality modality = deduceModality(query);
if (query instanceof SqlSelect) {
final SqlSelect select = (SqlSelect) query;
validateModality(select, modality, true);
} else if (query.getKind() == SqlKind.VALUES) {
switch(modality) {
case STREAM:
throw newValidationError(query, Static.RESOURCE.cannotStreamValues());
}
} else {
assert query.isA(SqlKind.SET_QUERY);
final SqlCall call = (SqlCall) query;
for (SqlNode operand : call.getOperandList()) {
if (deduceModality(operand) != modality) {
throw newValidationError(operand, Static.RESOURCE.streamSetOpInconsistentInputs());
}
validateModality(operand);
}
}
}
use of org.apache.calcite.sql.SqlSelect in project calcite by apache.
the class SqlValidatorImpl method rewriteUpdateToMerge.
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;
}
use of org.apache.calcite.sql.SqlSelect in project calcite by apache.
the class SqlValidatorImpl method createSourceSelectForDelete.
/**
* Creates the SELECT statement that putatively feeds rows into a DELETE
* statement to be deleted.
*
* @param call Call to the DELETE operator
* @return select statement
*/
protected SqlSelect createSourceSelectForDelete(SqlDelete call) {
final SqlNodeList selectList = new SqlNodeList(SqlParserPos.ZERO);
selectList.add(SqlIdentifier.star(SqlParserPos.ZERO));
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);
}
Aggregations