Search in sources :

Example 71 with SqlCall

use of org.apache.calcite.sql.SqlCall in project flink by apache.

the class SqlValidatorImpl method checkConstraint.

/**
 * Validates insert values against the constraint of a modifiable view.
 *
 * @param validatorTable Table that may wrap a ModifiableViewTable
 * @param source The values being inserted
 * @param targetRowType The target type for the view
 */
private void checkConstraint(SqlValidatorTable validatorTable, SqlNode source, RelDataType targetRowType) {
    final ModifiableViewTable modifiableViewTable = validatorTable.unwrap(ModifiableViewTable.class);
    if (modifiableViewTable != null && source instanceof SqlCall) {
        final Table table = modifiableViewTable.unwrap(Table.class);
        final RelDataType tableRowType = table.getRowType(typeFactory);
        final List<RelDataTypeField> tableFields = tableRowType.getFieldList();
        // Get the mapping from column indexes of the underlying table
        // to the target columns and view constraints.
        final Map<Integer, RelDataTypeField> tableIndexToTargetField = SqlValidatorUtil.getIndexToFieldMap(tableFields, targetRowType);
        final Map<Integer, RexNode> projectMap = RelOptUtil.getColumnConstraints(modifiableViewTable, targetRowType, typeFactory);
        // Determine columns (indexed to the underlying table) that need
        // to be validated against the view constraint.
        final ImmutableBitSet targetColumns = ImmutableBitSet.of(tableIndexToTargetField.keySet());
        final ImmutableBitSet constrainedColumns = ImmutableBitSet.of(projectMap.keySet());
        final ImmutableBitSet constrainedTargetColumns = targetColumns.intersect(constrainedColumns);
        // Validate insert values against the view constraint.
        final List<SqlNode> values = ((SqlCall) source).getOperandList();
        for (final int colIndex : constrainedTargetColumns.asList()) {
            final String colName = tableFields.get(colIndex).getName();
            final RelDataTypeField targetField = tableIndexToTargetField.get(colIndex);
            for (SqlNode row : values) {
                final SqlCall call = (SqlCall) row;
                final SqlNode sourceValue = call.operand(targetField.getIndex());
                final ValidationError validationError = new ValidationError(sourceValue, RESOURCE.viewConstraintNotSatisfied(colName, Util.last(validatorTable.getQualifiedName())));
                RelOptUtil.validateValueAgainstConstraint(sourceValue, projectMap.get(colIndex), validationError);
            }
        }
    }
}
Also used : Table(org.apache.calcite.schema.Table) SqlStdOperatorTable(org.apache.calcite.sql.fun.SqlStdOperatorTable) ModifiableViewTable(org.apache.calcite.schema.impl.ModifiableViewTable) RelOptTable(org.apache.calcite.plan.RelOptTable) SqlOperatorTable(org.apache.calcite.sql.SqlOperatorTable) ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) SqlCall(org.apache.calcite.sql.SqlCall) RelDataType(org.apache.calcite.rel.type.RelDataType) BitString(org.apache.calcite.util.BitString) BigInteger(java.math.BigInteger) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) ModifiableViewTable(org.apache.calcite.schema.impl.ModifiableViewTable) RexNode(org.apache.calcite.rex.RexNode) SqlNode(org.apache.calcite.sql.SqlNode)

Example 72 with SqlCall

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

the class SqlFloorFunction method unparseDatetimeFunction.

/**
 * Most dialects that natively support datetime floor will use this.
 * In those cases the call will look like TRUNC(datetime, 'year').
 *
 * @param writer SqlWriter
 * @param call SqlCall
 * @param funName Name of the sql function to call
 * @param datetimeFirst Specify the order of the datetime &amp; timeUnit
 * arguments
 */
public static void unparseDatetimeFunction(SqlWriter writer, SqlCall call, String funName, Boolean datetimeFirst) {
    SqlFunction func = new SqlFunction(funName, SqlKind.OTHER_FUNCTION, ReturnTypes.ARG0_NULLABLE_VARYING, null, null, SqlFunctionCategory.STRING);
    SqlCall call1;
    if (datetimeFirst) {
        call1 = call;
    } else {
        // switch order of operands
        SqlNode op1 = call.operand(0);
        SqlNode op2 = call.operand(1);
        call1 = call.getOperator().createCall(call.getParserPosition(), op2, op1);
    }
    SqlUtil.unparseFunctionSyntax(func, writer, call1);
}
Also used : SqlCall(org.apache.calcite.sql.SqlCall) SqlFunction(org.apache.calcite.sql.SqlFunction) SqlNode(org.apache.calcite.sql.SqlNode)

Example 73 with SqlCall

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

the class SqlCase method createSwitched.

/**
 * Creates a call to the switched form of the case operator, viz:
 *
 * <blockquote><code>CASE value<br>
 * WHEN whenList[0] THEN thenList[0]<br>
 * WHEN whenList[1] THEN thenList[1]<br>
 * ...<br>
 * ELSE elseClause<br>
 * END</code></blockquote>
 */
public static SqlCase createSwitched(SqlParserPos pos, SqlNode value, SqlNodeList whenList, SqlNodeList thenList, SqlNode elseClause) {
    if (null != value) {
        List<SqlNode> list = whenList.getList();
        for (int i = 0; i < list.size(); i++) {
            SqlNode e = list.get(i);
            final SqlCall call;
            if (e instanceof SqlNodeList) {
                call = SqlStdOperatorTable.IN.createCall(pos, value, e);
            } else {
                call = SqlStdOperatorTable.EQUALS.createCall(pos, value, e);
            }
            list.set(i, call);
        }
    }
    if (null == elseClause) {
        elseClause = SqlLiteral.createNull(pos);
    }
    return new SqlCase(pos, null, whenList, thenList, elseClause);
}
Also used : SqlCall(org.apache.calcite.sql.SqlCall) SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlNode(org.apache.calcite.sql.SqlNode)

Example 74 with SqlCall

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

the class OracleSqlDialect method unparseCall.

@Override
public void unparseCall(SqlWriter writer, SqlCall call, int leftPrec, int rightPrec) {
    if (call.getOperator() == SqlStdOperatorTable.SUBSTRING) {
        SqlUtil.unparseFunctionSyntax(OracleSqlOperatorTable.SUBSTR, writer, call);
    } else {
        switch(call.getKind()) {
            case FLOOR:
                if (call.operandCount() != 2) {
                    super.unparseCall(writer, call, leftPrec, rightPrec);
                    return;
                }
                final SqlLiteral timeUnitNode = call.operand(1);
                final TimeUnitRange timeUnit = timeUnitNode.getValueAs(TimeUnitRange.class);
                SqlCall call2 = SqlFloorFunction.replaceTimeUnitOperand(call, timeUnit.name(), timeUnitNode.getParserPosition());
                SqlFloorFunction.unparseDatetimeFunction(writer, call2, "TRUNC", true);
                break;
            default:
                super.unparseCall(writer, call, leftPrec, rightPrec);
        }
    }
}
Also used : SqlCall(org.apache.calcite.sql.SqlCall) TimeUnitRange(org.apache.calcite.avatica.util.TimeUnitRange) SqlLiteral(org.apache.calcite.sql.SqlLiteral)

Example 75 with SqlCall

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

the class HsqldbSqlDialect method unparseCall.

@Override
public void unparseCall(SqlWriter writer, SqlCall call, int leftPrec, int rightPrec) {
    switch(call.getKind()) {
        case FLOOR:
            if (call.operandCount() != 2) {
                super.unparseCall(writer, call, leftPrec, rightPrec);
                return;
            }
            final SqlLiteral timeUnitNode = call.operand(1);
            final TimeUnitRange timeUnit = timeUnitNode.getValueAs(TimeUnitRange.class);
            final String translatedLit = convertTimeUnit(timeUnit);
            SqlCall call2 = SqlFloorFunction.replaceTimeUnitOperand(call, translatedLit, timeUnitNode.getParserPosition());
            SqlFloorFunction.unparseDatetimeFunction(writer, call2, "TRUNC", true);
            break;
        default:
            super.unparseCall(writer, call, leftPrec, rightPrec);
    }
}
Also used : SqlCall(org.apache.calcite.sql.SqlCall) TimeUnitRange(org.apache.calcite.avatica.util.TimeUnitRange) SqlLiteral(org.apache.calcite.sql.SqlLiteral)

Aggregations

SqlCall (org.apache.calcite.sql.SqlCall)108 SqlNode (org.apache.calcite.sql.SqlNode)81 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)32 SqlNodeList (org.apache.calcite.sql.SqlNodeList)32 RelDataType (org.apache.calcite.rel.type.RelDataType)30 SqlOperator (org.apache.calcite.sql.SqlOperator)26 BitString (org.apache.calcite.util.BitString)19 ArrayList (java.util.ArrayList)18 SqlSelect (org.apache.calcite.sql.SqlSelect)17 RexNode (org.apache.calcite.rex.RexNode)13 SqlBasicCall (org.apache.calcite.sql.SqlBasicCall)12 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)11 SqlLiteral (org.apache.calcite.sql.SqlLiteral)11 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)9 SqlKind (org.apache.calcite.sql.SqlKind)9 SqlParserPos (org.apache.calcite.sql.parser.SqlParserPos)9 List (java.util.List)8 SqlCallBinding (org.apache.calcite.sql.SqlCallBinding)8 Pair (org.apache.calcite.util.Pair)8 RelNode (org.apache.calcite.rel.RelNode)7