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); createTargetRowType( table, insert.getTargetColumnList(), if (source instanceof SqlSelect) { final SqlSelect sqlSelect = (SqlSelect) source; validateSelect(sqlSelect, targetRowType); } else { final SqlValidatorScope scope = scopes.get(source); validateQuery(source, scope, targetRowType); final RelDataType sourceRowType = getNamespace(source).getRowType(); final RelDataType logicalTargetRowType = getLogicalTargetRowType(targetRowType, insert); setValidatedNodeType(insert, logicalTargetRowType); final RelDataType logicalSourceRowType = getLogicalSourceRowType(sourceRowType, insert); checkFieldCount(insert.getTargetTable(), table, source, logicalSourceRowType, logicalTargetRowType); checkTypeAssignment(logicalSourceRowType, logicalTargetRowType, insert); checkConstraint(table, source, logicalTargetRowType);
private boolean addOrExpandField(List<SqlNode> selectItems, Set<String> aliases, List<Map.Entry<String, RelDataType>> fields, boolean includeSystemVars, SelectScope scope, SqlIdentifier id, RelDataTypeField field) { switch (field.getType().getStructKind()) { case PEEK_FIELDS: case PEEK_FIELDS_DEFAULT: final SqlNode starExp = id.plusStar(); expandStar( selectItems, aliases, fields, includeSystemVars, scope, starExp); return true; default: addToSelectList( selectItems, aliases, fields, id, scope, includeSystemVars); } return false; }
private List<Map.Entry<String, RelDataType>> validateMeasure(SqlMatchRecognize mr, MatchRecognizeScope scope, boolean allRows) { final List<String> aliases = new ArrayList<>(); final List<SqlNode> sqlNodes = new ArrayList<>(); final SqlNodeList measures = mr.getMeasureList(); final List<Map.Entry<String, RelDataType>> fields = new ArrayList<>(); for (SqlNode measure : measures) { assert measure instanceof SqlCall; final String alias = deriveAlias(measure, aliases.size()); aliases.add(alias); SqlNode expand = expand(measure, scope); expand = navigationInMeasure(expand, allRows); setOriginal(expand, measure); inferUnknownTypes(unknownType, scope, expand); final RelDataType type = deriveType(scope, expand); setValidatedNodeType(measure, type); fields.add(Pair.of(alias, type)); sqlNodes.add( SqlStdOperatorTable.AS.createCall(SqlParserPos.ZERO, expand, new SqlIdentifier(alias, SqlParserPos.ZERO))); } SqlNodeList list = new SqlNodeList(sqlNodes, measures.getParserPosition()); inferUnknownTypes(unknownType, scope, list); for (SqlNode node : list) { validateExpr(node, scope); } mr.setOperand(SqlMatchRecognize.OPERAND_MEASURES, list); return fields; }
public SqlNode expandOrderExpr(SqlSelect select, SqlNode orderExpr) { final SqlNode newSqlNode = new OrderExpressionExpander(select, orderExpr).go(); if (newSqlNode != orderExpr) { final SqlValidatorScope scope = getOrderScope(select); inferUnknownTypes(unknownType, scope, newSqlNode); final RelDataType type = deriveType(scope, newSqlNode); setValidatedNodeType(newSqlNode, type); } return newSqlNode; }
private void validateGroupItem(SqlValidatorScope groupScope, AggregatingSelectScope aggregatingScope, SqlNode groupItem) { switch (groupItem.getKind()) { case GROUPING_SETS: case ROLLUP: case CUBE: validateGroupingSets(groupScope, aggregatingScope, (SqlCall) groupItem); break; default: if (groupItem instanceof SqlNodeList) { break; } final RelDataType type = deriveType(groupScope, groupItem); setValidatedNodeType(groupItem, type); } }
protected void validateWhereClause(SqlSelect select) { // validate WHERE clause final SqlNode where = select.getWhere(); if (where == null) { return; } final SqlValidatorScope whereScope = getWhereScope(select); final SqlNode expandedWhere = expand(where, whereScope); select.setWhere(expandedWhere); validateWhereOrOn(whereScope, expandedWhere, "WHERE"); }
getNamespace(select).unwrap(SelectNamespace.class); validateFeature(RESOURCE.sQLFeature_E051_01(), select.getModifierNode(SqlSelectKeyword.DISTINCT) .getParserPosition()); final SelectScope fromScope = (SelectScope) getFromScope(select); List<String> names = fromScope.getChildNames(); if (!catalogReader.nameMatcher().isCaseSensitive()) { final ScopeChild child = fromScope.children.get(duplicateAliasOrdinal); throw newValidationError(child.namespace.getEnclosingNode(), RESOURCE.fromAliasDuplicate(child.name)); throw newValidationError(select, RESOURCE.selectMissingFrom()); validateFrom(select.getFrom(), fromType, fromScope); validateWhereClause(select); validateGroupClause(select); validateHavingClause(select); validateWindowClause(select); handleOffsetFetch(select.getOffset(), select.getFetch()); validateSelectList(selectItems, select, targetRowType); ns.setType(rowType); validateOrderList(select);
final JoinConditionType conditionType = join.getConditionType(); final SqlValidatorScope joinScope = scopes.get(join); validateFrom(left, unknownType, joinScope); validateFrom(right, unknownType, joinScope); case ON: Preconditions.checkArgument(condition != null); SqlNode expandedCondition = expand(condition, joinScope); join.setOperand(5, expandedCondition); condition = join.getCondition(); validateWhereOrOn(joinScope, condition, "ON"); checkRollUp(null, join, condition, joinScope, "ON"); break; case USING: for (SqlNode node : list) { SqlIdentifier id = (SqlIdentifier) node; final RelDataType leftColType = validateUsingCol(id, left); final RelDataType rightColType = validateUsingCol(id, right); if (!SqlTypeUtil.isComparable(leftColType, rightColType)) { throw newValidationError(id, RESOURCE.naturalOrUsingColumnNotCompatible(id.getSimple(), leftColType.toString(), rightColType.toString())); checkRollUpInUsing(id, left); checkRollUpInUsing(id, right); throw newValidationError(condition, RESOURCE.naturalDisallowsOnOrUsing());
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) 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)); } } } }
List<Map.Entry<String, RelDataType>> fields, final boolean includeSystemVars) { final SelectScope scope = (SelectScope) getWhereScope(select); if (expandStar(selectItems, aliases, fields, includeSystemVars, scope, selectItem)) { return true; SqlNode expanded = expand(selectItem, scope); final String alias = deriveAlias( selectItem, aliases.size()); final SqlValidatorScope selectScope = getSelectScope(select); if (expanded != selectItem) { String newAlias = deriveAlias( expanded, aliases.size()); expanded, new SqlIdentifier(alias, SqlParserPos.ZERO)); deriveTypeImpl(selectScope, expanded); inferUnknownTypes(targetType, scope, expanded); final RelDataType type = deriveType(selectScope, expanded); setValidatedNodeType(expanded, type); fields.add(Pair.of(alias, type)); return false;
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)); 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); new SqlIdentifier(alias, SqlParserPos.ZERO))); final RelDataType type = deriveType(scope, expand); if (!SqlTypeUtil.inBooleanFamily(type)) { throw newValidationError(expand, RESOURCE.condMustBeBoolean("DEFINE")); setValidatedNodeType(item, type); inferUnknownTypes(unknownType, scope, list); for (SqlNode node : list) { validateExpr(node, scope);
switch (node.getKind()) { case AS: validateFrom( ((SqlCall) node).operand(0), targetRowType, break; case VALUES: validateValues((SqlCall) node, targetRowType, scope); break; case JOIN: validateJoin((SqlJoin) node, scope); break; case OVER: validateOver((SqlCall) node, scope); break; default: validateQuery(node, scope, targetRowType); break; getNamespace(node, scope).validate(targetRowType);
@Override public void validateMatchRecognize(SqlCall call) { final SqlMatchRecognize matchRecognize = (SqlMatchRecognize) call; final MatchRecognizeScope scope = (MatchRecognizeScope) getMatchRecognizeScope(matchRecognize); getNamespace(call).unwrap(MatchRecognizeNamespace.class); assert ns.rowType == null; SqlIdentifier identifier = (SqlIdentifier) node; identifier.validate(this, scope); RelDataType type = deriveType(scope, identifier); String name = identifier.names.get(1); typeBuilder.add(name, type); RelDataType type = deriveType(scope, identifier); String name = identifier.names.get(1); if (!typeBuilder.nameExists(name)) { getNamespace(matchRecognize.getTableRef()); final RelDataType inputDataType = sqlNs.getRowType(); for (RelDataTypeField fs : inputDataType.getFieldList()) { interval.validate(this, scope); if (((SqlIntervalLiteral) interval).signum() < 0) { throw newValidationError(interval, RESOURCE.intervalMustBeNonNegative(interval.toValue())); throw newValidationError(interval, RESOURCE.cannotUseWithinWithoutOrderBy());
DynamicRecordType.DYNAMIC_STAR_PREFIX), startPosition); addToSelectList( selectItems, aliases, } else { final SqlNode from = child.namespace.getNode(); final SqlValidatorNamespace fromNs = getNamespace(from, scope); assert fromNs != null; final RelDataType rowType = fromNs.getRowType(); if (!isRolledUpColumn(exp, scope)) { addOrExpandField( selectItems, aliases, throw newValidationError(prefixId, RESOURCE.unknownIdentifier(prefixId.toString())); addToSelectList( selectItems, aliases, addOrExpandField( selectItems, aliases, throw newValidationError(prefixId, RESOURCE.starRequiresRecordType());
return; if (!shouldAllowIntermediateOrderBy()) { if (!cursorSet.contains(select)) { throw newValidationError(select, RESOURCE.invalidOrderByPos()); final SqlValidatorScope orderScope = getOrderScope(select); Objects.requireNonNull(orderScope); SqlNode expandedOrderItem = expand(orderItem, orderScope); expandList.add(expandedOrderItem); validateOrderItem(select, orderItem);
throw newValidationError(selectItem, RESOURCE.onlyScalarSubQueryAllowed()); deriveAlias( selectItem, aliasList.size()); aliasList.add(alias); final SelectScope scope = (SelectScope) getWhereScope(parentSelect); final RelDataType type = deriveType(scope, selectItem); setValidatedNodeType(selectItem, type);
public Void visit(SqlIdentifier id) { if (isGroupExpr(id) || id.isStar()) { // Star may validly occur in "SELECT COUNT(*) OVER w" return null; } // Is it a call to a parentheses-free function? SqlCall call = SqlUtil.makeCall( validator.getOperatorTable(), id); if (call != null) { return call.accept(this); } // Didn't find the identifier in the group-by list as is, now find // it fully-qualified. // TODO: It would be better if we always compared fully-qualified // to fully-qualified. final SqlQualified fqId = scopes.peek().fullyQualify(id); if (isGroupExpr(fqId.identifier)) { return null; } SqlNode originalExpr = validator.getOriginal(id); final String exprString = originalExpr.toString(); throw validator.newValidationError(originalExpr, distinct ? RESOURCE.notSelectDistinctExpr(exprString) : RESOURCE.notGroupExpr(exprString)); }
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();
protected void validateWhereOrOn( SqlValidatorScope scope, SqlNode condition, String clause) { validateNoAggs(aggOrOverOrGroupFinder, condition, clause); inferUnknownTypes( booleanType, scope, condition); condition.validate(this, scope); final RelDataType type = deriveType(scope, condition); if (!SqlTypeUtil.inBooleanFamily(type)) { throw newValidationError(condition, RESOURCE.condMustBeBoolean(clause)); } }
if (inferredType.equals(unknownType)) { if (isNullLiteral) { throw newValidationError(node, RESOURCE.nullIllegal()); } else { throw newValidationError(node, RESOURCE.dynamicParamIllegal()); setValidatedNodeType(node, newInferredType); } else if (node instanceof SqlNodeList) { SqlNodeList nodeList = (SqlNodeList) node; inferUnknownTypes(type, scope, child); caseCall.getValueOperand() == null ? booleanType : unknownType; for (SqlNode sqlNode : caseCall.getWhenOperands().getList()) { inferUnknownTypes(whenType, scope, sqlNode); RelDataType returnType = deriveType(scope, node); for (SqlNode sqlNode : caseCall.getThenOperands().getList()) { inferUnknownTypes(returnType, scope, sqlNode); inferUnknownTypes( returnType, scope, caseCall.getElseOperand()); } else { setValidatedNodeType(caseCall.getElseOperand(), returnType); inferUnknownTypes(operandTypes[i], scope, operands.get(i));