Search in sources :

Example 91 with SqlCall

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

the class SqlValidatorImpl method validateQuery.

public void validateQuery(SqlNode node, SqlValidatorScope scope, RelDataType targetRowType) {
    final SqlValidatorNamespace ns = getNamespace(node, scope);
    if (node.getKind() == SqlKind.TABLESAMPLE) {
        List<SqlNode> operands = ((SqlCall) node).getOperandList();
        SqlSampleSpec sampleSpec = SqlLiteral.sampleValue(operands.get(1));
        if (sampleSpec instanceof SqlSampleSpec.SqlTableSampleSpec) {
            validateFeature(RESOURCE.sQLFeature_T613(), node.getParserPosition());
        } else if (sampleSpec instanceof SqlSampleSpec.SqlSubstitutionSampleSpec) {
            validateFeature(RESOURCE.sQLFeatureExt_T613_Substitution(), node.getParserPosition());
        }
    }
    validateNamespace(ns, targetRowType);
    if (node == top) {
        validateModality(node);
    }
    validateAccess(node, ns.getTable(), SqlAccessEnum.SELECT);
}
Also used : SqlCall(org.apache.calcite.sql.SqlCall) SqlSampleSpec(org.apache.calcite.sql.SqlSampleSpec) SqlNode(org.apache.calcite.sql.SqlNode)

Example 92 with SqlCall

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

the class SqlValidatorImpl method getNamespace.

private SqlValidatorNamespace getNamespace(SqlNode node, SqlValidatorScope scope) {
    if (node instanceof SqlIdentifier && scope instanceof DelegatingScope) {
        final SqlIdentifier id = (SqlIdentifier) node;
        final DelegatingScope idScope = (DelegatingScope) ((DelegatingScope) scope).getParent();
        return getNamespace(id, idScope);
    } else if (node instanceof SqlCall) {
        // Handle extended identifiers.
        final SqlCall call = (SqlCall) node;
        switch(call.getOperator().getKind()) {
            case EXTEND:
                final SqlIdentifier id = (SqlIdentifier) call.getOperandList().get(0);
                final DelegatingScope idScope = (DelegatingScope) scope;
                return getNamespace(id, idScope);
            case AS:
                final SqlNode nested = call.getOperandList().get(0);
                switch(nested.getKind()) {
                    case EXTEND:
                        return getNamespace(nested, scope);
                }
                break;
        }
    }
    return getNamespace(node);
}
Also used : SqlCall(org.apache.calcite.sql.SqlCall) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlNode(org.apache.calcite.sql.SqlNode)

Example 93 with SqlCall

use of org.apache.calcite.sql.SqlCall in project calcite 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 94 with SqlCall

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

the class SqlValidatorImpl method registerSetop.

private void registerSetop(SqlValidatorScope parentScope, SqlValidatorScope usingScope, SqlNode node, SqlNode enclosingNode, String alias, boolean forceNullable) {
    SqlCall call = (SqlCall) node;
    final SetopNamespace setopNamespace = createSetopNamespace(call, enclosingNode);
    registerNamespace(usingScope, alias, setopNamespace, forceNullable);
    // A setop is in the same scope as its parent.
    scopes.put(call, parentScope);
    for (SqlNode operand : call.getOperandList()) {
        registerQuery(parentScope, null, operand, operand, null, false);
    }
}
Also used : SqlCall(org.apache.calcite.sql.SqlCall) SqlNode(org.apache.calcite.sql.SqlNode)

Example 95 with SqlCall

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

the class SqlUserDefinedTableMacro method getValue.

private static Object getValue(SqlNode right) throws NonLiteralException {
    switch(right.getKind()) {
        case ARRAY_VALUE_CONSTRUCTOR:
            final List<Object> list = Lists.newArrayList();
            for (SqlNode o : ((SqlCall) right).getOperandList()) {
                list.add(getValue(o));
            }
            return ImmutableNullableList.copyOf(list);
        case MAP_VALUE_CONSTRUCTOR:
            final ImmutableMap.Builder<Object, Object> builder2 = ImmutableMap.builder();
            final List<SqlNode> operands = ((SqlCall) right).getOperandList();
            for (int i = 0; i < operands.size(); i += 2) {
                final SqlNode key = operands.get(i);
                final SqlNode value = operands.get(i + 1);
                builder2.put(getValue(key), getValue(value));
            }
            return builder2.build();
        default:
            if (SqlUtil.isNullLiteral(right, true)) {
                return null;
            }
            if (SqlUtil.isLiteral(right)) {
                return ((SqlLiteral) right).getValue();
            }
            if (right.getKind() == SqlKind.DEFAULT) {
                // currently NULL is the only default value
                return null;
            }
            throw new NonLiteralException();
    }
}
Also used : SqlCall(org.apache.calcite.sql.SqlCall) SqlLiteral(org.apache.calcite.sql.SqlLiteral) ImmutableMap(com.google.common.collect.ImmutableMap) SqlNode(org.apache.calcite.sql.SqlNode)

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