Search in sources :

Example 51 with SqlSelect

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

Example 52 with SqlSelect

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

Example 53 with SqlSelect

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

Example 54 with SqlSelect

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

Example 55 with SqlSelect

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