Search in sources :

Example 11 with SqlUpdate

use of org.apache.calcite.sql.SqlUpdate in project calcite by apache.

the class SqlValidatorImpl method rewriteMerge.

private void rewriteMerge(SqlMerge call) {
    SqlNodeList selectList;
    SqlUpdate updateStmt = call.getUpdateCall();
    if (updateStmt != null) {
        // if we have an update statement, just clone the select list
        // from the update statement's source since it's the same as
        // what we want for the select list of the merge source -- '*'
        // followed by the update set expressions
        selectList = SqlNode.clone(updateStmt.getSourceSelect().getSelectList());
    } else {
        // otherwise, just use select *
        selectList = new SqlNodeList(SqlParserPos.ZERO);
        selectList.add(SqlIdentifier.star(SqlParserPos.ZERO));
    }
    SqlNode targetTable = call.getTargetTable();
    if (call.getAlias() != null) {
        targetTable = SqlValidatorUtil.addAlias(targetTable, call.getAlias().getSimple());
    }
    // Provided there is an insert substatement, the source select for
    // the merge is a left outer join between the source in the USING
    // clause and the target table; otherwise, the join is just an
    // inner join.  Need to clone the source table reference in order
    // for validation to work
    SqlNode sourceTableRef = call.getSourceTableRef();
    SqlInsert insertCall = call.getInsertCall();
    JoinType joinType = (insertCall == null) ? JoinType.INNER : JoinType.LEFT;
    final SqlNode leftJoinTerm = SqlNode.clone(sourceTableRef);
    SqlNode outerJoin = new SqlJoin(SqlParserPos.ZERO, leftJoinTerm, SqlLiteral.createBoolean(false, SqlParserPos.ZERO), joinType.symbol(SqlParserPos.ZERO), targetTable, JoinConditionType.ON.symbol(SqlParserPos.ZERO), call.getCondition());
    SqlSelect select = new SqlSelect(SqlParserPos.ZERO, null, selectList, outerJoin, null, null, null, null, null, null, null);
    call.setSourceSelect(select);
    // that via the from clause on the select
    if (insertCall != null) {
        SqlCall valuesCall = (SqlCall) insertCall.getSource();
        SqlCall rowCall = valuesCall.operand(0);
        selectList = new SqlNodeList(rowCall.getOperandList(), SqlParserPos.ZERO);
        final SqlNode insertSource = SqlNode.clone(sourceTableRef);
        select = new SqlSelect(SqlParserPos.ZERO, null, selectList, insertSource, null, null, null, null, null, null, null);
        insertCall.setSource(select);
    }
}
Also used : SqlSelect(org.apache.calcite.sql.SqlSelect) SqlJoin(org.apache.calcite.sql.SqlJoin) SqlCall(org.apache.calcite.sql.SqlCall) SqlNodeList(org.apache.calcite.sql.SqlNodeList) JoinType(org.apache.calcite.sql.JoinType) SqlInsert(org.apache.calcite.sql.SqlInsert) SqlUpdate(org.apache.calcite.sql.SqlUpdate) SqlNode(org.apache.calcite.sql.SqlNode)

Aggregations

SqlUpdate (org.apache.calcite.sql.SqlUpdate)11 SqlNode (org.apache.calcite.sql.SqlNode)10 SqlNodeList (org.apache.calcite.sql.SqlNodeList)8 SqlInsert (org.apache.calcite.sql.SqlInsert)7 SqlSelect (org.apache.calcite.sql.SqlSelect)7 SqlCall (org.apache.calcite.sql.SqlCall)6 SqlDelete (org.apache.calcite.sql.SqlDelete)5 RelDataType (org.apache.calcite.rel.type.RelDataType)4 SqlMerge (org.apache.calcite.sql.SqlMerge)4 BitString (org.apache.calcite.util.BitString)4 ArrayList (java.util.ArrayList)3 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)3 Ord (org.apache.calcite.linq4j.Ord)2 JoinType (org.apache.calcite.sql.JoinType)2 SqlBasicCall (org.apache.calcite.sql.SqlBasicCall)2 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)2 SqlJoin (org.apache.calcite.sql.SqlJoin)2 SqlKind (org.apache.calcite.sql.SqlKind)2 SqlOperator (org.apache.calcite.sql.SqlOperator)2 SqlOrderBy (org.apache.calcite.sql.SqlOrderBy)2