Search in sources :

Example 96 with RelDataType

use of org.apache.calcite.rel.type.RelDataType in project calcite by apache.

the class SqlValidatorImpl method getValidatedNodeTypeIfKnown.

public RelDataType getValidatedNodeTypeIfKnown(SqlNode node) {
    final RelDataType type = nodeToTypeMap.get(node);
    if (type != null) {
        return type;
    }
    final SqlValidatorNamespace ns = getNamespace(node);
    if (ns != null) {
        return ns.getType();
    }
    final SqlNode original = originalExprs.get(node);
    if (original != null && original != node) {
        return getValidatedNodeType(original);
    }
    return null;
}
Also used : RelDataType(org.apache.calcite.rel.type.RelDataType) SqlNode(org.apache.calcite.sql.SqlNode)

Example 97 with RelDataType

use of org.apache.calcite.rel.type.RelDataType in project calcite by apache.

the class SqlValidatorImpl method expandStar.

public SqlNodeList expandStar(SqlNodeList selectList, SqlSelect select, boolean includeSystemVars) {
    final List<SqlNode> list = new ArrayList<>();
    final List<Map.Entry<String, RelDataType>> types = new ArrayList<>();
    for (int i = 0; i < selectList.size(); i++) {
        final SqlNode selectItem = selectList.get(i);
        final RelDataType originalType = getValidatedNodeTypeIfKnown(selectItem);
        expandSelectItem(selectItem, select, Util.first(originalType, unknownType), list, catalogReader.nameMatcher().isCaseSensitive() ? new LinkedHashSet<String>() : new TreeSet<>(String.CASE_INSENSITIVE_ORDER), types, includeSystemVars);
    }
    getRawSelectScope(select).setExpandedSelectList(list);
    return new SqlNodeList(list, SqlParserPos.ZERO);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) SqlNodeList(org.apache.calcite.sql.SqlNodeList) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlNode(org.apache.calcite.sql.SqlNode)

Example 98 with RelDataType

use of org.apache.calcite.rel.type.RelDataType in project calcite by apache.

the class SqlValidatorImpl method expandOrderExpr.

public SqlNode expandOrderExpr(SqlSelect select, SqlNode orderExpr) {
    final SqlNode newSqlNode = new OrderExpressionExpander(select, orderExpr).go();
    if (newSqlNode != orderExpr) {
        final SqlValidatorScope scope = getOrderScope(select);
        inferUnknownTypes(unknownType, scope, newSqlNode);
        final RelDataType type = deriveType(scope, newSqlNode);
        setValidatedNodeType(newSqlNode, type);
    }
    return newSqlNode;
}
Also used : RelDataType(org.apache.calcite.rel.type.RelDataType) SqlNode(org.apache.calcite.sql.SqlNode)

Example 99 with RelDataType

use of org.apache.calcite.rel.type.RelDataType in project calcite by apache.

the class SqlValidatorImpl method inferUnknownTypes.

protected void inferUnknownTypes(RelDataType inferredType, SqlValidatorScope scope, SqlNode node) {
    final SqlValidatorScope newScope = scopes.get(node);
    if (newScope != null) {
        scope = newScope;
    }
    boolean isNullLiteral = SqlUtil.isNullLiteral(node, false);
    if ((node instanceof SqlDynamicParam) || isNullLiteral) {
        if (inferredType.equals(unknownType)) {
            if (isNullLiteral) {
                throw newValidationError(node, RESOURCE.nullIllegal());
            } else {
                throw newValidationError(node, RESOURCE.dynamicParamIllegal());
            }
        }
        // REVIEW:  should dynamic parameter types always be nullable?
        RelDataType newInferredType = typeFactory.createTypeWithNullability(inferredType, true);
        if (SqlTypeUtil.inCharFamily(inferredType)) {
            newInferredType = typeFactory.createTypeWithCharsetAndCollation(newInferredType, inferredType.getCharset(), inferredType.getCollation());
        }
        setValidatedNodeType(node, newInferredType);
    } else if (node instanceof SqlNodeList) {
        SqlNodeList nodeList = (SqlNodeList) node;
        if (inferredType.isStruct()) {
            if (inferredType.getFieldCount() != nodeList.size()) {
                // bust out, and the error will be detected higher up
                return;
            }
        }
        int i = 0;
        for (SqlNode child : nodeList) {
            RelDataType type;
            if (inferredType.isStruct()) {
                type = inferredType.getFieldList().get(i).getType();
                ++i;
            } else {
                type = inferredType;
            }
            inferUnknownTypes(type, scope, child);
        }
    } else if (node instanceof SqlCase) {
        final SqlCase caseCall = (SqlCase) node;
        final RelDataType whenType = caseCall.getValueOperand() == null ? booleanType : unknownType;
        for (SqlNode sqlNode : caseCall.getWhenOperands().getList()) {
            inferUnknownTypes(whenType, scope, sqlNode);
        }
        RelDataType returnType = deriveType(scope, node);
        for (SqlNode sqlNode : caseCall.getThenOperands().getList()) {
            inferUnknownTypes(returnType, scope, sqlNode);
        }
        if (!SqlUtil.isNullLiteral(caseCall.getElseOperand(), false)) {
            inferUnknownTypes(returnType, scope, caseCall.getElseOperand());
        } else {
            setValidatedNodeType(caseCall.getElseOperand(), returnType);
        }
    } else if (node instanceof SqlCall) {
        final SqlCall call = (SqlCall) node;
        final SqlOperandTypeInference operandTypeInference = call.getOperator().getOperandTypeInference();
        final SqlCallBinding callBinding = new SqlCallBinding(this, scope, call);
        final List<SqlNode> operands = callBinding.operands();
        final RelDataType[] operandTypes = new RelDataType[operands.size()];
        if (operandTypeInference == null) {
            // TODO:  eventually should assert(operandTypeInference != null)
            // instead; for now just eat it
            Arrays.fill(operandTypes, unknownType);
        } else {
            operandTypeInference.inferOperandTypes(callBinding, inferredType, operandTypes);
        }
        for (int i = 0; i < operands.size(); ++i) {
            inferUnknownTypes(operandTypes[i], scope, operands.get(i));
        }
    }
}
Also used : SqlDynamicParam(org.apache.calcite.sql.SqlDynamicParam) SqlCase(org.apache.calcite.sql.fun.SqlCase) SqlCall(org.apache.calcite.sql.SqlCall) SqlCallBinding(org.apache.calcite.sql.SqlCallBinding) SqlNodeList(org.apache.calcite.sql.SqlNodeList) RelDataType(org.apache.calcite.rel.type.RelDataType) ArrayList(java.util.ArrayList) AbstractList(java.util.AbstractList) ImmutableNullableList(org.apache.calcite.util.ImmutableNullableList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlOperandTypeInference(org.apache.calcite.sql.type.SqlOperandTypeInference) SqlNode(org.apache.calcite.sql.SqlNode)

Example 100 with RelDataType

use of org.apache.calcite.rel.type.RelDataType in project calcite by apache.

the class SqlValidatorImpl method validateGroupItem.

private void validateGroupItem(SqlValidatorScope groupScope, AggregatingSelectScope aggregatingScope, SqlNode groupItem) {
    switch(groupItem.getKind()) {
        case GROUPING_SETS:
        case ROLLUP:
        case CUBE:
            validateGroupingSets(groupScope, aggregatingScope, (SqlCall) groupItem);
            break;
        default:
            if (groupItem instanceof SqlNodeList) {
                break;
            }
            final RelDataType type = deriveType(groupScope, groupItem);
            setValidatedNodeType(groupItem, type);
    }
}
Also used : SqlNodeList(org.apache.calcite.sql.SqlNodeList) RelDataType(org.apache.calcite.rel.type.RelDataType)

Aggregations

RelDataType (org.apache.calcite.rel.type.RelDataType)834 RexNode (org.apache.calcite.rex.RexNode)268 ArrayList (java.util.ArrayList)213 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)209 RelNode (org.apache.calcite.rel.RelNode)153 SqlNode (org.apache.calcite.sql.SqlNode)143 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)123 RexBuilder (org.apache.calcite.rex.RexBuilder)118 Test (org.junit.Test)62 ImmutableList (com.google.common.collect.ImmutableList)58 RexInputRef (org.apache.calcite.rex.RexInputRef)57 List (java.util.List)51 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)45 RexLiteral (org.apache.calcite.rex.RexLiteral)44 SqlNodeList (org.apache.calcite.sql.SqlNodeList)42 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)40 AggregateCall (org.apache.calcite.rel.core.AggregateCall)39 BitString (org.apache.calcite.util.BitString)38 BigDecimal (java.math.BigDecimal)35 RelBuilder (org.apache.calcite.tools.RelBuilder)34