use of org.apache.calcite.sql.SqlSelect in project flink by apache.
the class SqlValidatorImpl method checkRollUp.
private void checkRollUp(SqlNode grandParent, SqlNode parent, SqlNode current, SqlValidatorScope scope, String optionalClause) {
current = stripAs(current);
if (current instanceof SqlCall && !(current instanceof SqlSelect)) {
// Validate OVER separately
checkRollUpInWindow(getWindowInOver(current), scope);
current = stripOver(current);
List<SqlNode> children = ((SqlCall) stripAs(stripDot(current))).getOperandList();
for (SqlNode child : children) {
checkRollUp(parent, current, child, scope, optionalClause);
}
} else if (current instanceof SqlIdentifier) {
SqlIdentifier id = (SqlIdentifier) current;
if (!id.isStar() && isRolledUpColumn(id, scope)) {
if (!isAggregation(parent.getKind()) || !isRolledUpColumnAllowedInAgg(id, scope, (SqlCall) parent, grandParent)) {
String context = optionalClause != null ? optionalClause : parent.getKind().toString();
throw newValidationError(id, RESOURCE.rolledUpNotAllowed(deriveAlias(id, 0), context));
}
}
}
}
use of org.apache.calcite.sql.SqlSelect in project flink by apache.
the class SqlValidatorImpl method validateMerge.
public void validateMerge(SqlMerge call) {
SqlSelect sqlSelect = call.getSourceSelect();
// REVIEW zfong 5/25/06 - Does an actual type have to be passed into
// validateSelect()?
// REVIEW jvs 6-June-2006: In general, passing unknownType like
// this means we won't be able to correctly infer the types
// for dynamic parameter markers (SET x = ?). But
// maybe validateUpdate and validateInsert below will do
// the job?
// REVIEW ksecretan 15-July-2011: They didn't get a chance to
// since validateSelect() would bail.
// Let's use the update/insert targetRowType when available.
IdentifierNamespace targetNamespace = (IdentifierNamespace) getNamespace(call.getTargetTable());
validateNamespace(targetNamespace, unknownType);
SqlValidatorTable table = targetNamespace.getTable();
validateAccess(call.getTargetTable(), table, SqlAccessEnum.UPDATE);
RelDataType targetRowType = unknownType;
if (call.getUpdateCall() != null) {
targetRowType = createTargetRowType(table, call.getUpdateCall().getTargetColumnList(), true);
}
if (call.getInsertCall() != null) {
targetRowType = createTargetRowType(table, call.getInsertCall().getTargetColumnList(), false);
}
validateSelect(sqlSelect, targetRowType);
if (call.getUpdateCall() != null) {
validateUpdate(call.getUpdateCall());
}
if (call.getInsertCall() != null) {
validateInsert(call.getInsertCall());
}
}
use of org.apache.calcite.sql.SqlSelect in project flink 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 = getValidatedNodeType(select);
checkTypeAssignment(scopes.get(select), table, sourceRowType, targetRowType, call);
checkConstraint(table, call, targetRowType);
validateAccess(call.getTargetTable(), table, SqlAccessEnum.UPDATE);
}
use of org.apache.calcite.sql.SqlSelect in project flink by apache.
the class SqlValidatorImpl method getFieldOrigin.
private List<String> getFieldOrigin(SqlNode sqlQuery, int i) {
if (sqlQuery instanceof SqlSelect) {
SqlSelect sqlSelect = (SqlSelect) sqlQuery;
final SelectScope scope = getRawSelectScope(sqlSelect);
final List<SqlNode> selectList = scope.getExpandedSelectList();
final SqlNode selectItem = stripAs(selectList.get(i));
if (selectItem instanceof SqlIdentifier) {
final SqlQualified qualified = scope.fullyQualify((SqlIdentifier) selectItem);
SqlValidatorNamespace namespace = qualified.namespace;
final SqlValidatorTable table = namespace.getTable();
if (table == null) {
return null;
}
final List<String> origin = new ArrayList<>(table.getQualifiedName());
for (String name : qualified.suffix()) {
namespace = namespace.lookupChild(name);
if (namespace == null) {
return null;
}
origin.add(name);
}
return origin;
}
return null;
} else if (sqlQuery instanceof SqlOrderBy) {
return getFieldOrigin(((SqlOrderBy) sqlQuery).query, i);
} else {
return null;
}
}
use of org.apache.calcite.sql.SqlSelect in project flink by apache.
the class SqlValidatorImpl method validateInsert.
public void validateInsert(SqlInsert insert) {
final SqlValidatorNamespace targetNamespace = getNamespace(insert);
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);
// INSERT has an optional column name list. If present then
// reduce the rowtype to the columns specified. If not present
// then the entire target rowtype is used.
final RelDataType targetRowType = createTargetRowType(table, insert.getTargetColumnList(), false);
final SqlNode source = insert.getSource();
if (source instanceof SqlSelect) {
final SqlSelect sqlSelect = (SqlSelect) source;
validateSelect(sqlSelect, targetRowType);
} else {
final SqlValidatorScope scope = scopes.get(source);
validateQuery(source, scope, targetRowType);
}
// REVIEW jvs 4-Dec-2008: In FRG-365, this namespace row type is
// discarding the type inferred by inferUnknownTypes (which was invoked
// from validateSelect above). It would be better if that information
// were used here so that we never saw any untyped nulls during
// checkTypeAssignment.
final RelDataType sourceRowType = getNamespace(source).getRowType();
final RelDataType logicalTargetRowType = getLogicalTargetRowType(targetRowType, insert);
setValidatedNodeType(insert, logicalTargetRowType);
final RelDataType logicalSourceRowType = getLogicalSourceRowType(sourceRowType, insert);
final List<ColumnStrategy> strategies = table.unwrap(RelOptTable.class).getColumnStrategies();
final RelDataType realTargetRowType = typeFactory.createStructType(logicalTargetRowType.getFieldList().stream().filter(f -> strategies.get(f.getIndex()).canInsertInto()).collect(Collectors.toList()));
final RelDataType targetRowTypeToValidate = logicalSourceRowType.getFieldCount() == logicalTargetRowType.getFieldCount() ? logicalTargetRowType : realTargetRowType;
checkFieldCount(insert.getTargetTable(), table, strategies, targetRowTypeToValidate, realTargetRowType, source, logicalSourceRowType, logicalTargetRowType);
checkTypeAssignment(scopes.get(source), table, logicalSourceRowType, targetRowTypeToValidate, insert);
checkConstraint(table, source, logicalTargetRowType);
validateAccess(insert.getTargetTable(), table, SqlAccessEnum.INSERT);
// Refresh the insert row type to keep sync with source.
setValidatedNodeType(insert, targetRowTypeToValidate);
}
Aggregations