use of org.apache.calcite.sql.SqlIdentifier in project flink by apache.
the class SqlValidatorImpl method expandCommonColumn.
private static SqlNode expandCommonColumn(SqlSelect sqlSelect, SqlNode selectItem, SelectScope scope, SqlValidatorImpl validator) {
if (!(selectItem instanceof SqlIdentifier)) {
return selectItem;
}
final SqlNode from = sqlSelect.getFrom();
if (!(from instanceof SqlJoin)) {
return selectItem;
}
final SqlIdentifier identifier = (SqlIdentifier) selectItem;
if (!identifier.isSimple()) {
if (!validator.config().sqlConformance().allowQualifyingCommonColumn()) {
validateQualifiedCommonColumn((SqlJoin) from, identifier, scope, validator);
}
return selectItem;
}
return expandExprFromJoin((SqlJoin) from, identifier, scope);
}
use of org.apache.calcite.sql.SqlIdentifier in project flink by apache.
the class SqlValidatorImpl method validateWindow.
public void validateWindow(SqlNode windowOrId, SqlValidatorScope scope, SqlCall call) {
// Enable nested aggregates with window aggregates (OVER operator)
inWindow = true;
final SqlWindow targetWindow;
switch(windowOrId.getKind()) {
case IDENTIFIER:
// Just verify the window exists in this query. It will validate
// when the definition is processed
targetWindow = getWindowByName((SqlIdentifier) windowOrId, scope);
break;
case WINDOW:
targetWindow = (SqlWindow) windowOrId;
break;
default:
throw Util.unexpected(windowOrId.getKind());
}
assert targetWindow.getWindowCall() == null;
targetWindow.setWindowCall(call);
targetWindow.validate(this, scope);
targetWindow.setWindowCall(null);
call.validate(this, scope);
validateAggregateParams(call, null, null, scope);
// Disable nested aggregates post validation
inWindow = false;
}
use of org.apache.calcite.sql.SqlIdentifier in project flink by apache.
the class SqlValidatorImpl method validateDefinitions.
private void validateDefinitions(SqlMatchRecognize mr, MatchRecognizeScope scope) {
final Set<String> aliases = catalogReader.nameMatcher().createSet();
for (SqlNode item : mr.getPatternDefList().getList()) {
final String alias = alias(item);
if (!aliases.add(alias)) {
throw newValidationError(item, Static.RESOURCE.patternVarAlreadyDefined(alias));
}
scope.addPatternVar(alias);
}
final List<SqlNode> sqlNodes = new ArrayList<>();
for (SqlNode item : mr.getPatternDefList().getList()) {
final String alias = alias(item);
SqlNode expand = expand(item, scope);
expand = navigationInDefine(expand, alias);
setOriginal(expand, item);
inferUnknownTypes(booleanType, scope, expand);
expand.validate(this, scope);
// Some extra work need required here.
// In PREV, NEXT, FINAL and LAST, only one pattern variable is allowed.
sqlNodes.add(SqlStdOperatorTable.AS.createCall(SqlParserPos.ZERO, expand, new SqlIdentifier(alias, SqlParserPos.ZERO)));
final RelDataType type = deriveType(scope, expand);
if (!SqlTypeUtil.inBooleanFamily(type)) {
throw newValidationError(expand, RESOURCE.condMustBeBoolean("DEFINE"));
}
setValidatedNodeType(item, type);
}
SqlNodeList list = new SqlNodeList(sqlNodes, mr.getPatternDefList().getParserPosition());
inferUnknownTypes(unknownType, scope, list);
for (SqlNode node : list) {
validateExpr(node, scope);
}
mr.setOperand(SqlMatchRecognize.OPERAND_PATTERN_DEFINES, list);
}
use of org.apache.calcite.sql.SqlIdentifier 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.SqlIdentifier in project flink by apache.
the class SqlValidatorImpl method validatePivot.
public void validatePivot(SqlPivot pivot) {
final PivotScope scope = (PivotScope) getJoinScope(pivot);
final PivotNamespace ns = getNamespace(pivot).unwrap(PivotNamespace.class);
assert ns.rowType == null;
// Given
// query PIVOT (agg1 AS a, agg2 AS b, ...
// FOR (axis1, ..., axisN)
// IN ((v11, ..., v1N) AS label1,
// (v21, ..., v2N) AS label2, ...))
// the type is
// k1, ... kN, a_label1, b_label1, ..., a_label2, b_label2, ...
// where k1, ... kN are columns that are not referenced as an argument to
// an aggregate or as an axis.
// Aggregates, e.g. "PIVOT (sum(x) AS sum_x, count(*) AS c)"
final List<Pair<String, RelDataType>> aggNames = new ArrayList<>();
pivot.forEachAgg((alias, call) -> {
call.validate(this, scope);
final RelDataType type = deriveType(scope, call);
aggNames.add(Pair.of(alias, type));
if (!(call instanceof SqlCall) || !(((SqlCall) call).getOperator() instanceof SqlAggFunction)) {
throw newValidationError(call, RESOURCE.pivotAggMalformed());
}
});
// Axes, e.g. "FOR (JOB, DEPTNO)"
final List<RelDataType> axisTypes = new ArrayList<>();
final List<SqlIdentifier> axisIdentifiers = new ArrayList<>();
for (SqlNode axis : pivot.axisList) {
SqlIdentifier identifier = (SqlIdentifier) axis;
identifier.validate(this, scope);
final RelDataType type = deriveType(scope, identifier);
axisTypes.add(type);
axisIdentifiers.add(identifier);
}
// Columns that have been seen as arguments to aggregates or as axes
// do not appear in the output.
final Set<String> columnNames = pivot.usedColumnNames();
final RelDataTypeFactory.Builder typeBuilder = typeFactory.builder();
scope.getChild().getRowType().getFieldList().forEach(field -> {
if (!columnNames.contains(field.getName())) {
typeBuilder.add(field);
}
});
// Values, e.g. "IN (('CLERK', 10) AS c10, ('MANAGER, 20) AS m20)"
pivot.forEachNameValues((alias, nodeList) -> {
if (nodeList.size() != axisTypes.size()) {
throw newValidationError(nodeList, RESOURCE.pivotValueArityMismatch(nodeList.size(), axisTypes.size()));
}
final SqlOperandTypeChecker typeChecker = OperandTypes.COMPARABLE_UNORDERED_COMPARABLE_UNORDERED;
Pair.forEach(axisIdentifiers, nodeList, (identifier, subNode) -> {
subNode.validate(this, scope);
typeChecker.checkOperandTypes(new SqlCallBinding(this, scope, SqlStdOperatorTable.EQUALS.createCall(subNode.getParserPosition(), identifier, subNode)), true);
});
Pair.forEach(aggNames, (aggAlias, aggType) -> typeBuilder.add(aggAlias == null ? alias : alias + "_" + aggAlias, aggType));
});
final RelDataType rowType = typeBuilder.build();
ns.setType(rowType);
}
Aggregations