Search in sources :

Example 31 with SqlDynamicParam

use of org.apache.calcite.sql.SqlDynamicParam in project flink by splunk.

the class SqlValidatorImpl method checkTypeAssignment.

/**
 * Checks the type assignment of an INSERT or UPDATE query.
 *
 * <p>Skip the virtual columns(can not insert into) type assignment check if the source fields
 * count equals with the real target table fields count, see how #checkFieldCount was used.
 *
 * @param sourceScope Scope of query source which is used to infer node type
 * @param table Target table
 * @param sourceRowType Source row type
 * @param targetRowType Target row type, it should either contain all the virtual columns (can
 *     not insert into) or exclude all the virtual columns
 * @param query The query
 */
protected void checkTypeAssignment(SqlValidatorScope sourceScope, SqlValidatorTable table, RelDataType sourceRowType, RelDataType targetRowType, final SqlNode query) {
    // NOTE jvs 23-Feb-2006: subclasses may allow for extra targets
    // representing system-maintained columns, so stop after all sources
    // matched
    boolean isUpdateModifiableViewTable = false;
    if (query instanceof SqlUpdate) {
        final SqlNodeList targetColumnList = ((SqlUpdate) query).getTargetColumnList();
        if (targetColumnList != null) {
            final int targetColumnCnt = targetColumnList.size();
            targetRowType = SqlTypeUtil.extractLastNFields(typeFactory, targetRowType, targetColumnCnt);
            sourceRowType = SqlTypeUtil.extractLastNFields(typeFactory, sourceRowType, targetColumnCnt);
        }
        isUpdateModifiableViewTable = table.unwrap(ModifiableViewTable.class) != null;
    }
    if (SqlTypeUtil.equalAsStructSansNullability(typeFactory, sourceRowType, targetRowType, null)) {
        // Returns early if source and target row type equals sans nullability.
        return;
    }
    if (config.typeCoercionEnabled() && !isUpdateModifiableViewTable) {
        // Try type coercion first if implicit type coercion is allowed.
        boolean coerced = typeCoercion.querySourceCoercion(sourceScope, sourceRowType, targetRowType, query);
        if (coerced) {
            return;
        }
    }
    // Fall back to default behavior: compare the type families.
    List<RelDataTypeField> sourceFields = sourceRowType.getFieldList();
    List<RelDataTypeField> targetFields = targetRowType.getFieldList();
    final int sourceCount = sourceFields.size();
    for (int i = 0; i < sourceCount; ++i) {
        RelDataType sourceType = sourceFields.get(i).getType();
        RelDataType targetType = targetFields.get(i).getType();
        if (!SqlTypeUtil.canAssignFrom(targetType, sourceType)) {
            SqlNode node = getNthExpr(query, i, sourceCount);
            if (node instanceof SqlDynamicParam) {
                continue;
            }
            String targetTypeString;
            String sourceTypeString;
            if (SqlTypeUtil.areCharacterSetsMismatched(sourceType, targetType)) {
                sourceTypeString = sourceType.getFullTypeString();
                targetTypeString = targetType.getFullTypeString();
            } else {
                sourceTypeString = sourceType.toString();
                targetTypeString = targetType.toString();
            }
            throw newValidationError(node, RESOURCE.typeNotAssignable(targetFields.get(i).getName(), targetTypeString, sourceFields.get(i).getName(), sourceTypeString));
        }
    }
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) SqlDynamicParam(org.apache.calcite.sql.SqlDynamicParam) SqlNodeList(org.apache.calcite.sql.SqlNodeList) RelDataType(org.apache.calcite.rel.type.RelDataType) BitString(org.apache.calcite.util.BitString) SqlUpdate(org.apache.calcite.sql.SqlUpdate) SqlNode(org.apache.calcite.sql.SqlNode)

Example 32 with SqlDynamicParam

use of org.apache.calcite.sql.SqlDynamicParam in project flink-mirror by flink-ci.

the class SqlValidatorImpl method getParameterRowType.

public RelDataType getParameterRowType(SqlNode sqlQuery) {
    // NOTE: We assume that bind variables occur in depth-first tree
    // traversal in the same order that they occurred in the SQL text.
    final List<RelDataType> types = new ArrayList<>();
    // NOTE: but parameters on fetch/offset would be counted twice
    // as they are counted in the SqlOrderBy call and the inner SqlSelect call
    final Set<SqlNode> alreadyVisited = new HashSet<>();
    sqlQuery.accept(new SqlShuttle() {

        @Override
        public SqlNode visit(SqlDynamicParam param) {
            if (alreadyVisited.add(param)) {
                RelDataType type = getValidatedNodeType(param);
                types.add(type);
            }
            return param;
        }
    });
    return typeFactory.createStructType(types, new AbstractList<String>() {

        @Override
        public String get(int index) {
            return "?" + index;
        }

        @Override
        public int size() {
            return types.size();
        }
    });
}
Also used : SqlShuttle(org.apache.calcite.sql.util.SqlShuttle) SqlDynamicParam(org.apache.calcite.sql.SqlDynamicParam) ArrayList(java.util.ArrayList) RelDataType(org.apache.calcite.rel.type.RelDataType) BitString(org.apache.calcite.util.BitString) SqlNode(org.apache.calcite.sql.SqlNode) HashSet(java.util.HashSet)

Example 33 with SqlDynamicParam

use of org.apache.calcite.sql.SqlDynamicParam in project flink-mirror by flink-ci.

the class SqlValidatorImpl method checkTypeAssignment.

/**
 * Checks the type assignment of an INSERT or UPDATE query.
 *
 * <p>Skip the virtual columns(can not insert into) type assignment check if the source fields
 * count equals with the real target table fields count, see how #checkFieldCount was used.
 *
 * @param sourceScope Scope of query source which is used to infer node type
 * @param table Target table
 * @param sourceRowType Source row type
 * @param targetRowType Target row type, it should either contain all the virtual columns (can
 *     not insert into) or exclude all the virtual columns
 * @param query The query
 */
protected void checkTypeAssignment(SqlValidatorScope sourceScope, SqlValidatorTable table, RelDataType sourceRowType, RelDataType targetRowType, final SqlNode query) {
    // NOTE jvs 23-Feb-2006: subclasses may allow for extra targets
    // representing system-maintained columns, so stop after all sources
    // matched
    boolean isUpdateModifiableViewTable = false;
    if (query instanceof SqlUpdate) {
        final SqlNodeList targetColumnList = ((SqlUpdate) query).getTargetColumnList();
        if (targetColumnList != null) {
            final int targetColumnCnt = targetColumnList.size();
            targetRowType = SqlTypeUtil.extractLastNFields(typeFactory, targetRowType, targetColumnCnt);
            sourceRowType = SqlTypeUtil.extractLastNFields(typeFactory, sourceRowType, targetColumnCnt);
        }
        isUpdateModifiableViewTable = table.unwrap(ModifiableViewTable.class) != null;
    }
    if (SqlTypeUtil.equalAsStructSansNullability(typeFactory, sourceRowType, targetRowType, null)) {
        // Returns early if source and target row type equals sans nullability.
        return;
    }
    if (config.typeCoercionEnabled() && !isUpdateModifiableViewTable) {
        // Try type coercion first if implicit type coercion is allowed.
        boolean coerced = typeCoercion.querySourceCoercion(sourceScope, sourceRowType, targetRowType, query);
        if (coerced) {
            return;
        }
    }
    // Fall back to default behavior: compare the type families.
    List<RelDataTypeField> sourceFields = sourceRowType.getFieldList();
    List<RelDataTypeField> targetFields = targetRowType.getFieldList();
    final int sourceCount = sourceFields.size();
    for (int i = 0; i < sourceCount; ++i) {
        RelDataType sourceType = sourceFields.get(i).getType();
        RelDataType targetType = targetFields.get(i).getType();
        if (!SqlTypeUtil.canAssignFrom(targetType, sourceType)) {
            SqlNode node = getNthExpr(query, i, sourceCount);
            if (node instanceof SqlDynamicParam) {
                continue;
            }
            String targetTypeString;
            String sourceTypeString;
            if (SqlTypeUtil.areCharacterSetsMismatched(sourceType, targetType)) {
                sourceTypeString = sourceType.getFullTypeString();
                targetTypeString = targetType.getFullTypeString();
            } else {
                sourceTypeString = sourceType.toString();
                targetTypeString = targetType.toString();
            }
            throw newValidationError(node, RESOURCE.typeNotAssignable(targetFields.get(i).getName(), targetTypeString, sourceFields.get(i).getName(), sourceTypeString));
        }
    }
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) SqlDynamicParam(org.apache.calcite.sql.SqlDynamicParam) SqlNodeList(org.apache.calcite.sql.SqlNodeList) RelDataType(org.apache.calcite.rel.type.RelDataType) BitString(org.apache.calcite.util.BitString) SqlUpdate(org.apache.calcite.sql.SqlUpdate) SqlNode(org.apache.calcite.sql.SqlNode)

Example 34 with SqlDynamicParam

use of org.apache.calcite.sql.SqlDynamicParam in project flink-mirror by flink-ci.

the class SqlCastFunction method inferReturnType.

// ~ Methods ----------------------------------------------------------------
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
    assert opBinding.getOperandCount() == 2;
    RelDataType ret = opBinding.getOperandType(1);
    RelDataType firstType = opBinding.getOperandType(0);
    ret = opBinding.getTypeFactory().createTypeWithNullability(ret, firstType.isNullable());
    if (opBinding instanceof SqlCallBinding) {
        SqlCallBinding callBinding = (SqlCallBinding) opBinding;
        SqlNode operand0 = callBinding.operand(0);
        // to them using the type they are casted to.
        if (((operand0 instanceof SqlLiteral) && (((SqlLiteral) operand0).getValue() == null)) || (operand0 instanceof SqlDynamicParam)) {
            final SqlValidatorImpl validator = (SqlValidatorImpl) callBinding.getValidator();
            validator.setValidatedNodeType(operand0, ret);
        }
    }
    return ret;
}
Also used : SqlValidatorImpl(org.apache.calcite.sql.validate.SqlValidatorImpl) SqlDynamicParam(org.apache.calcite.sql.SqlDynamicParam) SqlCallBinding(org.apache.calcite.sql.SqlCallBinding) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlLiteral(org.apache.calcite.sql.SqlLiteral) SqlNode(org.apache.calcite.sql.SqlNode)

Example 35 with SqlDynamicParam

use of org.apache.calcite.sql.SqlDynamicParam in project Mycat2 by MyCATApache.

the class SqlValidatorImpl method getParameterRowType.

public RelDataType getParameterRowType(SqlNode sqlQuery) {
    // NOTE: We assume that bind variables occur in depth-first tree
    // traversal in the same order that they occurred in the SQL text.
    final List<RelDataType> types = new ArrayList<>();
    // NOTE: but parameters on fetch/offset would be counted twice
    // as they are counted in the SqlOrderBy call and the inner SqlSelect call
    final Set<SqlNode> alreadyVisited = new HashSet<>();
    sqlQuery.accept(new SqlShuttle() {

        @Override
        public SqlNode visit(SqlDynamicParam param) {
            if (alreadyVisited.add(param)) {
                RelDataType type = getValidatedNodeType(param);
                types.add(type);
            }
            return param;
        }
    });
    return typeFactory.createStructType(types, new AbstractList<String>() {

        @Override
        public String get(int index) {
            return "?" + index;
        }

        @Override
        public int size() {
            return types.size();
        }
    });
}
Also used : SqlShuttle(org.apache.calcite.sql.util.SqlShuttle) SqlDynamicParam(org.apache.calcite.sql.SqlDynamicParam) ArrayList(java.util.ArrayList) RelDataType(org.apache.calcite.rel.type.RelDataType) BitString(org.apache.calcite.util.BitString) SqlNode(org.apache.calcite.sql.SqlNode) HashSet(java.util.HashSet)

Aggregations

SqlDynamicParam (org.apache.calcite.sql.SqlDynamicParam)35 SqlNode (org.apache.calcite.sql.SqlNode)33 RelDataType (org.apache.calcite.rel.type.RelDataType)30 SqlNodeList (org.apache.calcite.sql.SqlNodeList)10 BitString (org.apache.calcite.util.BitString)10 SqlCallBinding (org.apache.calcite.sql.SqlCallBinding)9 SqlCall (org.apache.calcite.sql.SqlCall)7 ArrayList (java.util.ArrayList)5 HashSet (java.util.HashSet)5 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)5 SqlUpdate (org.apache.calcite.sql.SqlUpdate)5 SqlCase (org.apache.calcite.sql.fun.SqlCase)5 SqlOperandTypeInference (org.apache.calcite.sql.type.SqlOperandTypeInference)5 SqlShuttle (org.apache.calcite.sql.util.SqlShuttle)5 ParameterConverter (com.hazelcast.sql.impl.ParameterConverter)4 SqlValidatorImpl (org.apache.calcite.sql.validate.SqlValidatorImpl)4 SqlLiteral (org.apache.calcite.sql.SqlLiteral)3 HazelcastSqlValidator (com.hazelcast.jet.sql.impl.validate.HazelcastSqlValidator)2 AnyToVarcharParameterConverter (com.hazelcast.jet.sql.impl.validate.param.AnyToVarcharParameterConverter)2 NumericPrecedenceParameterConverter (com.hazelcast.jet.sql.impl.validate.param.NumericPrecedenceParameterConverter)2