Search in sources :

Example 61 with SqlNode

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlNode in project calcite by apache.

the class SqlValidatorImpl method validateWindowClause.

protected void validateWindowClause(SqlSelect select) {
    final SqlNodeList windowList = select.getWindowList();
    @SuppressWarnings("unchecked") final List<SqlWindow> windows = (List) windowList.getList();
    if (windows.isEmpty()) {
        return;
    }
    final SelectScope windowScope = (SelectScope) getFromScope(select);
    assert windowScope != null;
    // 2. ensure they are unique within this scope
    for (SqlWindow window : windows) {
        SqlIdentifier declName = window.getDeclName();
        if (!declName.isSimple()) {
            throw newValidationError(declName, RESOURCE.windowNameMustBeSimple());
        }
        if (windowScope.existingWindowName(declName.toString())) {
            throw newValidationError(declName, RESOURCE.duplicateWindowName());
        } else {
            windowScope.addWindowName(declName.toString());
        }
    }
    // Check for pairs of windows which are equivalent.
    for (int i = 0; i < windows.size(); i++) {
        SqlNode window1 = windows.get(i);
        for (int j = i + 1; j < windows.size(); j++) {
            SqlNode window2 = windows.get(j);
            if (window1.equalsDeep(window2, Litmus.IGNORE)) {
                throw newValidationError(window2, RESOURCE.dupWindowSpec());
            }
        }
    }
    for (SqlWindow window : windows) {
        final SqlNodeList expandedOrderList = (SqlNodeList) expand(window.getOrderList(), windowScope);
        window.setOrderList(expandedOrderList);
        expandedOrderList.validate(this, windowScope);
        final SqlNodeList expandedPartitionList = (SqlNodeList) expand(window.getPartitionList(), windowScope);
        window.setPartitionList(expandedPartitionList);
        expandedPartitionList.validate(this, windowScope);
    }
    // Hand off to validate window spec components
    windowList.validate(this, windowScope);
}
Also used : SqlWindow(org.apache.calcite.sql.SqlWindow) SqlNodeList(org.apache.calcite.sql.SqlNodeList) 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) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlNode(org.apache.calcite.sql.SqlNode)

Example 62 with SqlNode

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlNode 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 63 with SqlNode

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlNode in project calcite by apache.

the class SqlValidatorImpl method getFieldOrigin.

private List<String> getFieldOrigin(SqlNode sqlQuery, int i) {
    if (sqlQuery instanceof SqlSelect) {
        SqlSelect sqlSelect = (SqlSelect) sqlQuery;
        final SelectScope scope = getRawSelectScope(sqlSelect);
        final List<SqlNode> selectList = scope.getExpandedSelectList();
        final SqlNode selectItem = stripAs(selectList.get(i));
        if (selectItem instanceof SqlIdentifier) {
            final SqlQualified qualified = scope.fullyQualify((SqlIdentifier) selectItem);
            SqlValidatorNamespace namespace = qualified.namespace;
            final SqlValidatorTable table = namespace.getTable();
            if (table == null) {
                return null;
            }
            final List<String> origin = new ArrayList<>(table.getQualifiedName());
            for (String name : qualified.suffix()) {
                namespace = namespace.lookupChild(name);
                if (namespace == null) {
                    return null;
                }
                origin.add(name);
            }
            return origin;
        }
        return null;
    } else if (sqlQuery instanceof SqlOrderBy) {
        return getFieldOrigin(((SqlOrderBy) sqlQuery).query, i);
    } else {
        return null;
    }
}
Also used : SqlSelect(org.apache.calcite.sql.SqlSelect) ArrayList(java.util.ArrayList) BitString(org.apache.calcite.util.BitString) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlOrderBy(org.apache.calcite.sql.SqlOrderBy) SqlNode(org.apache.calcite.sql.SqlNode)

Example 64 with SqlNode

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlNode 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 65 with SqlNode

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlNode in project calcite by apache.

the class SqlToRelConverter method convertAgg.

/**
 * Converts the SELECT, GROUP BY and HAVING clauses of an aggregate query.
 *
 * <p>This method extracts SELECT, GROUP BY and HAVING clauses, and creates
 * an {@link AggConverter}, then delegates to {@link #createAggImpl}.
 * Derived class may override this method to change any of those clauses or
 * specify a different {@link AggConverter}.
 *
 * @param bb            Scope within which to resolve identifiers
 * @param select        Query
 * @param orderExprList Additional expressions needed to implement ORDER BY
 */
protected void convertAgg(Blackboard bb, SqlSelect select, List<SqlNode> orderExprList) {
    assert bb.root != null : "precondition: child != null";
    SqlNodeList groupList = select.getGroup();
    SqlNodeList selectList = select.getSelectList();
    SqlNode having = select.getHaving();
    final AggConverter aggConverter = new AggConverter(bb, select);
    createAggImpl(bb, aggConverter, selectList, groupList, having, orderExprList);
}
Also used : SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlNode(org.apache.calcite.sql.SqlNode)

Aggregations

SqlNode (org.apache.calcite.sql.SqlNode)510 RelDataType (org.apache.calcite.rel.type.RelDataType)141 SqlNodeList (org.apache.calcite.sql.SqlNodeList)98 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)84 SqlCall (org.apache.calcite.sql.SqlCall)81 ArrayList (java.util.ArrayList)78 RelNode (org.apache.calcite.rel.RelNode)60 Test (org.junit.Test)59 BitString (org.apache.calcite.util.BitString)46 SqlSelect (org.apache.calcite.sql.SqlSelect)45 SqlWriter (org.apache.calcite.sql.SqlWriter)42 RexNode (org.apache.calcite.rex.RexNode)40 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)33 RelOptPlanner (org.apache.calcite.plan.RelOptPlanner)25 SqlLiteral (org.apache.calcite.sql.SqlLiteral)23 SqlOperator (org.apache.calcite.sql.SqlOperator)23 SqlParserPos (org.apache.calcite.sql.parser.SqlParserPos)22 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)21 ImmutableList (com.google.common.collect.ImmutableList)20 SqlValidator (org.apache.calcite.sql.validate.SqlValidator)20