Search in sources :

Example 81 with SqlNode

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

the class SqlValidatorUtil method convertGroupSet.

/**
 * Analyzes a GROUPING SETS item in a GROUP BY clause.
 */
private static void convertGroupSet(SqlValidatorScope scope, GroupAnalyzer groupAnalyzer, ImmutableList.Builder<ImmutableBitSet> builder, SqlNode groupExpr) {
    switch(groupExpr.getKind()) {
        case GROUPING_SETS:
            final SqlCall call = (SqlCall) groupExpr;
            for (SqlNode node : call.getOperandList()) {
                convertGroupSet(scope, groupAnalyzer, builder, node);
            }
            return;
        case ROW:
            final List<ImmutableBitSet> bitSets = analyzeGroupTuple(scope, groupAnalyzer, ((SqlCall) groupExpr).getOperandList());
            builder.add(ImmutableBitSet.union(bitSets));
            return;
        case ROLLUP:
        case CUBE:
            {
                // GROUPING SETS ( (a), ROLLUP(c,b), CUBE(d,e) )
                // is EQUIVALENT to
                // GROUPING SETS ( (a), (c,b), (b) ,(), (d,e), (d), (e) ).
                // Expand all ROLLUP/CUBE nodes
                List<ImmutableBitSet> operandBitSet = analyzeGroupTuple(scope, groupAnalyzer, ((SqlCall) groupExpr).getOperandList());
                switch(groupExpr.getKind()) {
                    case ROLLUP:
                        builder.addAll(rollup(operandBitSet));
                        return;
                    default:
                        builder.addAll(cube(operandBitSet));
                        return;
                }
            }
        default:
            builder.add(analyzeGroupExpr(scope, groupAnalyzer, groupExpr));
            return;
    }
}
Also used : ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) SqlCall(org.apache.calcite.sql.SqlCall) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlNode(org.apache.calcite.sql.SqlNode)

Example 82 with SqlNode

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

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

the class TableNamespace method checkExtendedColumnTypes.

/**
 * Ensures that extended columns that have the same name as a base column also
 * have the same data-type.
 */
private void checkExtendedColumnTypes(SqlNodeList extendList) {
    final List<RelDataTypeField> extendedFields = SqlValidatorUtil.getExtendedColumns(validator.getTypeFactory(), table, extendList);
    final List<RelDataTypeField> baseFields = getBaseRowType().getFieldList();
    final Map<String, Integer> nameToIndex = SqlValidatorUtil.mapNameToIndex(baseFields);
    for (final RelDataTypeField extendedField : extendedFields) {
        final String extFieldName = extendedField.getName();
        if (nameToIndex.containsKey(extFieldName)) {
            final Integer baseIndex = nameToIndex.get(extFieldName);
            final RelDataType baseType = baseFields.get(baseIndex).getType();
            final RelDataType extType = extendedField.getType();
            if (!extType.equals(baseType)) {
                // Get the extended column node that failed validation.
                final Predicate<SqlNode> nameMatches = new PredicateImpl<SqlNode>() {

                    @Override
                    public boolean test(SqlNode sqlNode) {
                        if (sqlNode instanceof SqlIdentifier) {
                            final SqlIdentifier identifier = (SqlIdentifier) sqlNode;
                            return Util.last(identifier.names).equals(extendedField.getName());
                        }
                        return false;
                    }
                };
                final SqlNode extColNode = Iterables.find(extendList.getList(), nameMatches);
                throw validator.getValidationErrorFunction().apply(extColNode, RESOURCE.typeNotAssignable(baseFields.get(baseIndex).getName(), baseType.getFullTypeString(), extendedField.getName(), extType.getFullTypeString()));
            }
        }
    }
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) PredicateImpl(org.apache.calcite.runtime.PredicateImpl) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlNode(org.apache.calcite.sql.SqlNode)

Example 84 with SqlNode

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

the class SelectNamespace method getMonotonicity.

public SqlMonotonicity getMonotonicity(String columnName) {
    final RelDataType rowType = this.getRowTypeSansSystemColumns();
    final int field = SqlTypeUtil.findField(rowType, columnName);
    final SqlNode selectItem = validator.getRawSelectScope(select).getExpandedSelectList().get(field);
    return validator.getSelectScope(select).getMonotonicity(selectItem);
}
Also used : RelDataType(org.apache.calcite.rel.type.RelDataType) SqlNode(org.apache.calcite.sql.SqlNode)

Example 85 with SqlNode

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

the class SqlCreateMaterializedView method unparse.

@Override
public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
    writer.keyword("CREATE");
    writer.keyword("MATERIALIZED VIEW");
    if (ifNotExists) {
        writer.keyword("IF NOT EXISTS");
    }
    name.unparse(writer, leftPrec, rightPrec);
    if (columnList != null) {
        SqlWriter.Frame frame = writer.startList("(", ")");
        for (SqlNode c : columnList) {
            writer.sep(",");
            c.unparse(writer, 0, 0);
        }
        writer.endList(frame);
    }
    writer.keyword("AS");
    writer.newlineAndIndent();
    query.unparse(writer, 0, 0);
}
Also used : SqlWriter(org.apache.calcite.sql.SqlWriter) 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