Search in sources :

Example 56 with SqlIdentifier

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

the class SqlValidatorImpl method expandCommonColumn.

private static SqlNode expandCommonColumn(SqlSelect sqlSelect, SqlNode selectItem, SelectScope scope, SqlValidatorImpl validator) {
    if (!(selectItem instanceof SqlIdentifier)) {
        return selectItem;
    }
    final SqlNode from = sqlSelect.getFrom();
    if (!(from instanceof SqlJoin)) {
        return selectItem;
    }
    final SqlIdentifier identifier = (SqlIdentifier) selectItem;
    if (!identifier.isSimple()) {
        if (!validator.config().sqlConformance().allowQualifyingCommonColumn()) {
            validateQualifiedCommonColumn((SqlJoin) from, identifier, scope, validator);
        }
        return selectItem;
    }
    return expandExprFromJoin((SqlJoin) from, identifier, scope);
}
Also used : SqlJoin(org.apache.calcite.sql.SqlJoin) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlNode(org.apache.calcite.sql.SqlNode)

Example 57 with SqlIdentifier

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

the class SqlValidatorImpl method validateWindow.

public void validateWindow(SqlNode windowOrId, SqlValidatorScope scope, SqlCall call) {
    // Enable nested aggregates with window aggregates (OVER operator)
    inWindow = true;
    final SqlWindow targetWindow;
    switch(windowOrId.getKind()) {
        case IDENTIFIER:
            // Just verify the window exists in this query.  It will validate
            // when the definition is processed
            targetWindow = getWindowByName((SqlIdentifier) windowOrId, scope);
            break;
        case WINDOW:
            targetWindow = (SqlWindow) windowOrId;
            break;
        default:
            throw Util.unexpected(windowOrId.getKind());
    }
    assert targetWindow.getWindowCall() == null;
    targetWindow.setWindowCall(call);
    targetWindow.validate(this, scope);
    targetWindow.setWindowCall(null);
    call.validate(this, scope);
    validateAggregateParams(call, null, null, scope);
    // Disable nested aggregates post validation
    inWindow = false;
}
Also used : SqlWindow(org.apache.calcite.sql.SqlWindow) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier)

Example 58 with SqlIdentifier

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

the class SqlValidatorImpl method validateDefinitions.

private void validateDefinitions(SqlMatchRecognize mr, MatchRecognizeScope scope) {
    final Set<String> aliases = catalogReader.nameMatcher().createSet();
    for (SqlNode item : mr.getPatternDefList().getList()) {
        final String alias = alias(item);
        if (!aliases.add(alias)) {
            throw newValidationError(item, Static.RESOURCE.patternVarAlreadyDefined(alias));
        }
        scope.addPatternVar(alias);
    }
    final List<SqlNode> sqlNodes = new ArrayList<>();
    for (SqlNode item : mr.getPatternDefList().getList()) {
        final String alias = alias(item);
        SqlNode expand = expand(item, scope);
        expand = navigationInDefine(expand, alias);
        setOriginal(expand, item);
        inferUnknownTypes(booleanType, scope, expand);
        expand.validate(this, scope);
        // Some extra work need required here.
        // In PREV, NEXT, FINAL and LAST, only one pattern variable is allowed.
        sqlNodes.add(SqlStdOperatorTable.AS.createCall(SqlParserPos.ZERO, expand, new SqlIdentifier(alias, SqlParserPos.ZERO)));
        final RelDataType type = deriveType(scope, expand);
        if (!SqlTypeUtil.inBooleanFamily(type)) {
            throw newValidationError(expand, RESOURCE.condMustBeBoolean("DEFINE"));
        }
        setValidatedNodeType(item, type);
    }
    SqlNodeList list = new SqlNodeList(sqlNodes, mr.getPatternDefList().getParserPosition());
    inferUnknownTypes(unknownType, scope, list);
    for (SqlNode node : list) {
        validateExpr(node, scope);
    }
    mr.setOperand(SqlMatchRecognize.OPERAND_PATTERN_DEFINES, list);
}
Also used : ArrayList(java.util.ArrayList) SqlNodeList(org.apache.calcite.sql.SqlNodeList) RelDataType(org.apache.calcite.rel.type.RelDataType) BitString(org.apache.calcite.util.BitString) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlNode(org.apache.calcite.sql.SqlNode)

Example 59 with SqlIdentifier

use of org.apache.calcite.sql.SqlIdentifier in project flink 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 60 with SqlIdentifier

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

the class SqlValidatorImpl method validatePivot.

public void validatePivot(SqlPivot pivot) {
    final PivotScope scope = (PivotScope) getJoinScope(pivot);
    final PivotNamespace ns = getNamespace(pivot).unwrap(PivotNamespace.class);
    assert ns.rowType == null;
    // Given
    // query PIVOT (agg1 AS a, agg2 AS b, ...
    // FOR (axis1, ..., axisN)
    // IN ((v11, ..., v1N) AS label1,
    // (v21, ..., v2N) AS label2, ...))
    // the type is
    // k1, ... kN, a_label1, b_label1, ..., a_label2, b_label2, ...
    // where k1, ... kN are columns that are not referenced as an argument to
    // an aggregate or as an axis.
    // Aggregates, e.g. "PIVOT (sum(x) AS sum_x, count(*) AS c)"
    final List<Pair<String, RelDataType>> aggNames = new ArrayList<>();
    pivot.forEachAgg((alias, call) -> {
        call.validate(this, scope);
        final RelDataType type = deriveType(scope, call);
        aggNames.add(Pair.of(alias, type));
        if (!(call instanceof SqlCall) || !(((SqlCall) call).getOperator() instanceof SqlAggFunction)) {
            throw newValidationError(call, RESOURCE.pivotAggMalformed());
        }
    });
    // Axes, e.g. "FOR (JOB, DEPTNO)"
    final List<RelDataType> axisTypes = new ArrayList<>();
    final List<SqlIdentifier> axisIdentifiers = new ArrayList<>();
    for (SqlNode axis : pivot.axisList) {
        SqlIdentifier identifier = (SqlIdentifier) axis;
        identifier.validate(this, scope);
        final RelDataType type = deriveType(scope, identifier);
        axisTypes.add(type);
        axisIdentifiers.add(identifier);
    }
    // Columns that have been seen as arguments to aggregates or as axes
    // do not appear in the output.
    final Set<String> columnNames = pivot.usedColumnNames();
    final RelDataTypeFactory.Builder typeBuilder = typeFactory.builder();
    scope.getChild().getRowType().getFieldList().forEach(field -> {
        if (!columnNames.contains(field.getName())) {
            typeBuilder.add(field);
        }
    });
    // Values, e.g. "IN (('CLERK', 10) AS c10, ('MANAGER, 20) AS m20)"
    pivot.forEachNameValues((alias, nodeList) -> {
        if (nodeList.size() != axisTypes.size()) {
            throw newValidationError(nodeList, RESOURCE.pivotValueArityMismatch(nodeList.size(), axisTypes.size()));
        }
        final SqlOperandTypeChecker typeChecker = OperandTypes.COMPARABLE_UNORDERED_COMPARABLE_UNORDERED;
        Pair.forEach(axisIdentifiers, nodeList, (identifier, subNode) -> {
            subNode.validate(this, scope);
            typeChecker.checkOperandTypes(new SqlCallBinding(this, scope, SqlStdOperatorTable.EQUALS.createCall(subNode.getParserPosition(), identifier, subNode)), true);
        });
        Pair.forEach(aggNames, (aggAlias, aggType) -> typeBuilder.add(aggAlias == null ? alias : alias + "_" + aggAlias, aggType));
    });
    final RelDataType rowType = typeBuilder.build();
    ns.setType(rowType);
}
Also used : SqlCall(org.apache.calcite.sql.SqlCall) ArrayList(java.util.ArrayList) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlAggFunction(org.apache.calcite.sql.SqlAggFunction) BitString(org.apache.calcite.util.BitString) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) SqlCallBinding(org.apache.calcite.sql.SqlCallBinding) SqlOperandTypeChecker(org.apache.calcite.sql.type.SqlOperandTypeChecker) IdPair(org.apache.calcite.sql.util.IdPair) Pair(org.apache.calcite.util.Pair) SqlNode(org.apache.calcite.sql.SqlNode)

Aggregations

SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)131 SqlNode (org.apache.calcite.sql.SqlNode)84 RelDataType (org.apache.calcite.rel.type.RelDataType)46 SqlNodeList (org.apache.calcite.sql.SqlNodeList)43 ArrayList (java.util.ArrayList)41 SqlCall (org.apache.calcite.sql.SqlCall)32 BitString (org.apache.calcite.util.BitString)28 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)21 SqlSelect (org.apache.calcite.sql.SqlSelect)21 SqlParserPos (org.apache.calcite.sql.parser.SqlParserPos)12 SchemaPlus (org.apache.calcite.schema.SchemaPlus)11 SqlOperator (org.apache.calcite.sql.SqlOperator)11 NlsString (org.apache.calcite.util.NlsString)11 List (java.util.List)9 Map (java.util.Map)9 RelOptTable (org.apache.calcite.plan.RelOptTable)9 RexNode (org.apache.calcite.rex.RexNode)9 AbstractSchema (org.apache.drill.exec.store.AbstractSchema)9 ImmutableList (com.google.common.collect.ImmutableList)8 RelNode (org.apache.calcite.rel.RelNode)7