Search in sources :

Example 21 with SqlSelect

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));
            }
        }
    }
}
Also used : SqlSelect(org.apache.calcite.sql.SqlSelect) SqlCall(org.apache.calcite.sql.SqlCall) BitString(org.apache.calcite.util.BitString) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlNode(org.apache.calcite.sql.SqlNode)

Example 22 with SqlSelect

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());
    }
}
Also used : SqlSelect(org.apache.calcite.sql.SqlSelect) RelDataType(org.apache.calcite.rel.type.RelDataType)

Example 23 with SqlSelect

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);
}
Also used : SqlSelect(org.apache.calcite.sql.SqlSelect) RelDataType(org.apache.calcite.rel.type.RelDataType) RelOptTable(org.apache.calcite.plan.RelOptTable)

Example 24 with SqlSelect

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;
    }
}
Also used : SqlSelect(org.apache.calcite.sql.SqlSelect) ArrayList(java.util.ArrayList) BitString(org.apache.calcite.util.BitString) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlOrderBy(org.apache.calcite.sql.SqlOrderBy) SqlNode(org.apache.calcite.sql.SqlNode)

Example 25 with SqlSelect

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);
}
Also used : ColumnStrategy(org.apache.calcite.schema.ColumnStrategy) SqlSelect(org.apache.calcite.sql.SqlSelect) RelDataType(org.apache.calcite.rel.type.RelDataType) RelOptTable(org.apache.calcite.plan.RelOptTable) SqlNode(org.apache.calcite.sql.SqlNode)

Aggregations

SqlSelect (org.apache.calcite.sql.SqlSelect)62 SqlNode (org.apache.calcite.sql.SqlNode)45 SqlNodeList (org.apache.calcite.sql.SqlNodeList)29 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)20 SqlCall (org.apache.calcite.sql.SqlCall)17 RelDataType (org.apache.calcite.rel.type.RelDataType)15 BitString (org.apache.calcite.util.BitString)12 ArrayList (java.util.ArrayList)9 SqlUpdate (org.apache.calcite.sql.SqlUpdate)8 SqlInsert (org.apache.calcite.sql.SqlInsert)7 SchemaPlus (org.apache.calcite.schema.SchemaPlus)6 SqlMerge (org.apache.calcite.sql.SqlMerge)6 SqlBasicCall (org.apache.calcite.sql.SqlBasicCall)5 SqlDelete (org.apache.calcite.sql.SqlDelete)5 SqlJoin (org.apache.calcite.sql.SqlJoin)5 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)4 SqlOrderBy (org.apache.calcite.sql.SqlOrderBy)4 AbstractSchema (org.apache.drill.exec.store.AbstractSchema)4 RelOptTable (org.apache.calcite.plan.RelOptTable)3 RelNode (org.apache.calcite.rel.RelNode)3