Search in sources :

Example 26 with SqlNodeList

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

the class SqlValidatorUtil method analyzeGroupExpr.

/**
 * Analyzes a component of a tuple in a GROUPING SETS clause.
 */
private static ImmutableBitSet analyzeGroupExpr(SqlValidatorScope scope, GroupAnalyzer groupAnalyzer, SqlNode groupExpr) {
    final SqlNode expandedGroupExpr = scope.getValidator().expand(groupExpr, scope);
    switch(expandedGroupExpr.getKind()) {
        case ROW:
            return ImmutableBitSet.union(analyzeGroupTuple(scope, groupAnalyzer, ((SqlCall) expandedGroupExpr).getOperandList()));
        case OTHER:
            if (expandedGroupExpr instanceof SqlNodeList && ((SqlNodeList) expandedGroupExpr).size() == 0) {
                return ImmutableBitSet.of();
            }
    }
    final int ref = lookupGroupExpr(groupAnalyzer, groupExpr);
    if (expandedGroupExpr instanceof SqlIdentifier) {
        // SQL 2003 does not allow expressions of column references
        SqlIdentifier expr = (SqlIdentifier) expandedGroupExpr;
        // column references should be fully qualified.
        assert expr.names.size() == 2;
        String originalRelName = expr.names.get(0);
        String originalFieldName = expr.names.get(1);
        final SqlNameMatcher nameMatcher = scope.getValidator().getCatalogReader().nameMatcher();
        final SqlValidatorScope.ResolvedImpl resolved = new SqlValidatorScope.ResolvedImpl();
        scope.resolve(ImmutableList.of(originalRelName), nameMatcher, false, resolved);
        assert resolved.count() == 1;
        final SqlValidatorScope.Resolve resolve = resolved.only();
        final RelDataType rowType = resolve.rowType();
        final int childNamespaceIndex = resolve.path.steps().get(0).i;
        int namespaceOffset = 0;
        if (childNamespaceIndex > 0) {
            // If not the first child, need to figure out the width of
            // output types from all the preceding namespaces
            final SqlValidatorScope ancestorScope = resolve.scope;
            assert ancestorScope instanceof ListScope;
            List<SqlValidatorNamespace> children = ((ListScope) ancestorScope).getChildren();
            for (int j = 0; j < childNamespaceIndex; j++) {
                namespaceOffset += children.get(j).getRowType().getFieldCount();
            }
        }
        RelDataTypeField field = nameMatcher.field(rowType, originalFieldName);
        int origPos = namespaceOffset + field.getIndex();
        groupAnalyzer.groupExprProjection.put(origPos, ref);
    }
    return ImmutableBitSet.of(ref);
}
Also used : SqlCall(org.apache.calcite.sql.SqlCall) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlNode(org.apache.calcite.sql.SqlNode)

Example 27 with SqlNodeList

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

the class SelectScope method lookupWindow.

public SqlWindow lookupWindow(String name) {
    final SqlNodeList windowList = select.getWindowList();
    for (int i = 0; i < windowList.size(); i++) {
        SqlWindow window = (SqlWindow) windowList.get(i);
        final SqlIdentifier declId = window.getDeclName();
        assert declId.isSimple();
        if (declId.names.get(0).equals(name)) {
            return window;
        }
    }
    // if not in the select scope, then check window scope
    if (windowParent != null) {
        return windowParent.lookupWindow(name);
    } else {
        return null;
    }
}
Also used : SqlWindow(org.apache.calcite.sql.SqlWindow) SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier)

Example 28 with SqlNodeList

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

the class SqlDdlNodes method renameColumns.

/**
 * Wraps a query to rename its columns. Used by CREATE VIEW and CREATE
 * MATERIALIZED VIEW.
 */
static SqlNode renameColumns(SqlNodeList columnList, SqlNode query) {
    if (columnList == null) {
        return query;
    }
    final SqlParserPos p = query.getParserPosition();
    final SqlNodeList selectList = new SqlNodeList(ImmutableList.<SqlNode>of(SqlIdentifier.star(p)), p);
    final SqlCall from = SqlStdOperatorTable.AS.createCall(p, ImmutableList.<SqlNode>builder().add(query).add(new SqlIdentifier("_", p)).addAll(columnList).build());
    return new SqlSelect(p, null, selectList, from, null, null, null, null, null, null, null);
}
Also used : SqlSelect(org.apache.calcite.sql.SqlSelect) SqlParserPos(org.apache.calcite.sql.parser.SqlParserPos) SqlCall(org.apache.calcite.sql.SqlCall) SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier)

Example 29 with SqlNodeList

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

the class AggregatingSelectScope method resolve.

// ~ Methods ----------------------------------------------------------------
private Resolved resolve() {
    final ImmutableList.Builder<ImmutableList<ImmutableBitSet>> builder = ImmutableList.builder();
    List<SqlNode> extraExprs = ImmutableList.of();
    Map<Integer, Integer> groupExprProjection = ImmutableMap.of();
    if (select.getGroup() != null) {
        final SqlNodeList groupList = select.getGroup();
        final SqlValidatorUtil.GroupAnalyzer groupAnalyzer = new SqlValidatorUtil.GroupAnalyzer(temporaryGroupExprList);
        for (SqlNode groupExpr : groupList) {
            SqlValidatorUtil.analyzeGroupItem(this, groupAnalyzer, builder, groupExpr);
        }
        extraExprs = groupAnalyzer.extraExprs;
        groupExprProjection = groupAnalyzer.groupExprProjection;
    }
    final Set<ImmutableBitSet> flatGroupSets = Sets.newTreeSet(ImmutableBitSet.COMPARATOR);
    for (List<ImmutableBitSet> groupSet : Linq4j.product(builder.build())) {
        flatGroupSets.add(ImmutableBitSet.union(groupSet));
    }
    // For GROUP BY (), we need a singleton grouping set.
    if (flatGroupSets.isEmpty()) {
        flatGroupSets.add(ImmutableBitSet.of());
    }
    return new Resolved(extraExprs, temporaryGroupExprList, flatGroupSets, groupExprProjection);
}
Also used : ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) ImmutableList(com.google.common.collect.ImmutableList) SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlNode(org.apache.calcite.sql.SqlNode)

Example 30 with SqlNodeList

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

the class AliasNamespace method validateImpl.

// ~ Methods ----------------------------------------------------------------
protected RelDataType validateImpl(RelDataType targetRowType) {
    final List<String> nameList = new ArrayList<String>();
    final List<SqlNode> operands = call.getOperandList();
    final SqlValidatorNamespace childNs = validator.getNamespace(operands.get(0));
    final RelDataType rowType = childNs.getRowTypeSansSystemColumns();
    final List<SqlNode> columnNames = Util.skip(operands, 2);
    for (final SqlNode operand : columnNames) {
        String name = ((SqlIdentifier) operand).getSimple();
        if (nameList.contains(name)) {
            throw validator.newValidationError(operand, RESOURCE.aliasListDuplicate(name));
        }
        nameList.add(name);
    }
    if (nameList.size() != rowType.getFieldCount()) {
        // Position error over all column names
        final SqlNode node = operands.size() == 3 ? operands.get(2) : new SqlNodeList(columnNames, SqlParserPos.sum(columnNames));
        throw validator.newValidationError(node, RESOURCE.aliasListDegree(rowType.getFieldCount(), getString(rowType), nameList.size()));
    }
    final List<RelDataType> typeList = new ArrayList<RelDataType>();
    for (RelDataTypeField field : rowType.getFieldList()) {
        typeList.add(field.getType());
    }
    return validator.getTypeFactory().createStructType(typeList, nameList);
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) ArrayList(java.util.ArrayList) SqlNodeList(org.apache.calcite.sql.SqlNodeList) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlNode(org.apache.calcite.sql.SqlNode)

Aggregations

SqlNodeList (org.apache.calcite.sql.SqlNodeList)123 SqlNode (org.apache.calcite.sql.SqlNode)97 ArrayList (java.util.ArrayList)45 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)43 RelDataType (org.apache.calcite.rel.type.RelDataType)39 SqlCall (org.apache.calcite.sql.SqlCall)30 SqlSelect (org.apache.calcite.sql.SqlSelect)29 BitString (org.apache.calcite.util.BitString)23 RexNode (org.apache.calcite.rex.RexNode)13 SqlLiteral (org.apache.calcite.sql.SqlLiteral)11 ImmutableList (com.google.common.collect.ImmutableList)10 List (java.util.List)10 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)10 NlsString (org.apache.calcite.util.NlsString)9 RelNode (org.apache.calcite.rel.RelNode)8 SqlUpdate (org.apache.calcite.sql.SqlUpdate)8 SqlBasicCall (org.apache.calcite.sql.SqlBasicCall)7 SqlWriter (org.apache.calcite.sql.SqlWriter)7 SqlParserPos (org.apache.calcite.sql.parser.SqlParserPos)7 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)6