Search in sources :

Example 76 with RelDataTypeField

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataTypeField 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 77 with RelDataTypeField

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataTypeField in project calcite by apache.

the class SqlCreateTable method execute.

public void execute(CalcitePrepare.Context context) {
    final Pair<CalciteSchema, String> pair = SqlDdlNodes.schema(context, true, name);
    final JavaTypeFactory typeFactory = new JavaTypeFactoryImpl();
    final RelDataType queryRowType;
    if (query != null) {
        // A bit of a hack: pretend it's a view, to get its row type
        final String sql = query.toSqlString(CalciteSqlDialect.DEFAULT).getSql();
        final ViewTableMacro viewTableMacro = ViewTable.viewMacro(pair.left.plus(), sql, pair.left.path(null), context.getObjectPath(), false);
        final TranslatableTable x = viewTableMacro.apply(ImmutableList.of());
        queryRowType = x.getRowType(typeFactory);
        if (columnList != null && queryRowType.getFieldCount() != columnList.size()) {
            throw SqlUtil.newContextException(columnList.getParserPosition(), RESOURCE.columnCountMismatch());
        }
    } else {
        queryRowType = null;
    }
    final List<SqlNode> columnList;
    if (this.columnList != null) {
        columnList = this.columnList.getList();
    } else {
        if (queryRowType == null) {
            // a list of column names and types, "CREATE TABLE t (INT c)".
            throw SqlUtil.newContextException(name.getParserPosition(), RESOURCE.createTableRequiresColumnList());
        }
        columnList = new ArrayList<>();
        for (String name : queryRowType.getFieldNames()) {
            columnList.add(new SqlIdentifier(name, SqlParserPos.ZERO));
        }
    }
    final ImmutableList.Builder<ColumnDef> b = ImmutableList.builder();
    final RelDataTypeFactory.Builder builder = typeFactory.builder();
    final RelDataTypeFactory.Builder storedBuilder = typeFactory.builder();
    for (Ord<SqlNode> c : Ord.zip(columnList)) {
        if (c.e instanceof SqlColumnDeclaration) {
            final SqlColumnDeclaration d = (SqlColumnDeclaration) c.e;
            final RelDataType type = d.dataType.deriveType(typeFactory, true);
            builder.add(d.name.getSimple(), type);
            if (d.strategy != ColumnStrategy.VIRTUAL) {
                storedBuilder.add(d.name.getSimple(), type);
            }
            b.add(ColumnDef.of(d.expression, type, d.strategy));
        } else if (c.e instanceof SqlIdentifier) {
            final SqlIdentifier id = (SqlIdentifier) c.e;
            if (queryRowType == null) {
                throw SqlUtil.newContextException(id.getParserPosition(), RESOURCE.createTableRequiresColumnTypes(id.getSimple()));
            }
            final RelDataTypeField f = queryRowType.getFieldList().get(c.i);
            final ColumnStrategy strategy = f.getType().isNullable() ? ColumnStrategy.NULLABLE : ColumnStrategy.NOT_NULLABLE;
            b.add(ColumnDef.of(c.e, f.getType(), strategy));
            builder.add(id.getSimple(), f.getType());
            storedBuilder.add(id.getSimple(), f.getType());
        } else {
            throw new AssertionError(c.e.getClass());
        }
    }
    final RelDataType rowType = builder.build();
    final RelDataType storedRowType = storedBuilder.build();
    final List<ColumnDef> columns = b.build();
    final InitializerExpressionFactory ief = new NullInitializerExpressionFactory() {

        @Override
        public ColumnStrategy generationStrategy(RelOptTable table, int iColumn) {
            return columns.get(iColumn).strategy;
        }

        @Override
        public RexNode newColumnDefaultValue(RelOptTable table, int iColumn, InitializerContext context) {
            final ColumnDef c = columns.get(iColumn);
            if (c.expr != null) {
                return context.convertExpression(c.expr);
            }
            return super.newColumnDefaultValue(table, iColumn, context);
        }
    };
    if (pair.left.plus().getTable(pair.right) != null) {
        // Table exists.
        if (!ifNotExists) {
            // They did not specify IF NOT EXISTS, so give error.
            throw SqlUtil.newContextException(name.getParserPosition(), RESOURCE.tableExists(pair.right));
        }
        return;
    }
    // Table does not exist. Create it.
    pair.left.add(pair.right, new MutableArrayTable(pair.right, RelDataTypeImpl.proto(storedRowType), RelDataTypeImpl.proto(rowType), ief));
    if (query != null) {
        SqlDdlNodes.populate(name, query, context);
    }
}
Also used : NullInitializerExpressionFactory(org.apache.calcite.sql2rel.NullInitializerExpressionFactory) NullInitializerExpressionFactory(org.apache.calcite.sql2rel.NullInitializerExpressionFactory) InitializerExpressionFactory(org.apache.calcite.sql2rel.InitializerExpressionFactory) ImmutableList(com.google.common.collect.ImmutableList) ViewTableMacro(org.apache.calcite.schema.impl.ViewTableMacro) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) JavaTypeFactoryImpl(org.apache.calcite.jdbc.JavaTypeFactoryImpl) JavaTypeFactory(org.apache.calcite.adapter.java.JavaTypeFactory) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) TranslatableTable(org.apache.calcite.schema.TranslatableTable) SqlNode(org.apache.calcite.sql.SqlNode) ColumnStrategy(org.apache.calcite.schema.ColumnStrategy) InitializerContext(org.apache.calcite.sql2rel.InitializerContext) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) CalciteSchema(org.apache.calcite.jdbc.CalciteSchema) RelOptTable(org.apache.calcite.plan.RelOptTable)

Example 78 with RelDataTypeField

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataTypeField in project calcite by apache.

the class Handler method handle.

/**
 * Creates relational expressions for a given AST node.
 */
public Handler handle(Ast.Node node) {
    final RelNode input;
    final List<RexNode> rexNodes;
    switch(node.op) {
        case LOAD:
            final Ast.LoadStmt load = (Ast.LoadStmt) node;
            builder.scan((String) load.name.value);
            register(load.target.value);
            return this;
        case VALUES:
            final Ast.ValuesStmt values = (Ast.ValuesStmt) node;
            final RelDataType rowType = toType(values.schema);
            builder.values(tuples(values, rowType), rowType);
            register(values.target.value);
            return this;
        case FOREACH:
            final Ast.ForeachStmt foreach = (Ast.ForeachStmt) node;
            builder.clear();
            input = map.get(foreach.source.value);
            builder.push(input);
            rexNodes = new ArrayList<>();
            for (Ast.Node exp : foreach.expList) {
                rexNodes.add(toRex(exp));
            }
            builder.project(rexNodes);
            register(foreach.target.value);
            return this;
        case FOREACH_NESTED:
            final Ast.ForeachNestedStmt foreachNested = (Ast.ForeachNestedStmt) node;
            builder.clear();
            input = map.get(foreachNested.source.value);
            builder.push(input);
            System.out.println(input.getRowType());
            for (RelDataTypeField field : input.getRowType().getFieldList()) {
                switch(field.getType().getSqlTypeName()) {
                    case ARRAY:
                        System.out.println(field);
                }
            }
            for (Ast.Stmt stmt : foreachNested.nestedStmtList) {
                handle(stmt);
            }
            rexNodes = new ArrayList<>();
            for (Ast.Node exp : foreachNested.expList) {
                rexNodes.add(toRex(exp));
            }
            builder.project(rexNodes);
            register(foreachNested.target.value);
            return this;
        case FILTER:
            final Ast.FilterStmt filter = (Ast.FilterStmt) node;
            builder.clear();
            input = map.get(filter.source.value);
            builder.push(input);
            final RexNode rexNode = toRex(filter.condition);
            builder.filter(rexNode);
            register(filter.target.value);
            return this;
        case DISTINCT:
            final Ast.DistinctStmt distinct = (Ast.DistinctStmt) node;
            builder.clear();
            input = map.get(distinct.source.value);
            builder.push(input);
            builder.distinct(null, -1);
            register(distinct.target.value);
            return this;
        case ORDER:
            final Ast.OrderStmt order = (Ast.OrderStmt) node;
            builder.clear();
            input = map.get(order.source.value);
            builder.push(input);
            final List<RexNode> nodes = new ArrayList<>();
            for (Pair<Ast.Identifier, Ast.Direction> field : order.fields) {
                toSortRex(nodes, field);
            }
            builder.sort(nodes);
            register(order.target.value);
            return this;
        case LIMIT:
            final Ast.LimitStmt limit = (Ast.LimitStmt) node;
            builder.clear();
            input = map.get(limit.source.value);
            final int count = ((Number) limit.count.value).intValue();
            builder.push(input);
            builder.limit(0, count);
            register(limit.target.value);
            return this;
        case GROUP:
            final Ast.GroupStmt group = (Ast.GroupStmt) node;
            builder.clear();
            input = map.get(group.source.value);
            builder.push(input).as(group.source.value);
            final List<RelBuilder.GroupKey> groupKeys = new ArrayList<>();
            final List<RexNode> keys = new ArrayList<>();
            if (group.keys != null) {
                for (Ast.Node key : group.keys) {
                    keys.add(toRex(key));
                }
            }
            groupKeys.add(builder.groupKey(keys));
            builder.group(PigRelBuilder.GroupOption.COLLECTED, null, -1, groupKeys);
            register(group.target.value);
            return this;
        case PROGRAM:
            final Ast.Program program = (Ast.Program) node;
            for (Ast.Stmt stmt : program.stmtList) {
                handle(stmt);
            }
            return this;
        case DUMP:
            final Ast.DumpStmt dump = (Ast.DumpStmt) node;
            final RelNode relNode = map.get(dump.relation.value);
            dump(relNode);
            // nothing to do; contains no algebra
            return this;
        default:
            throw new AssertionError("unknown operation " + node.op);
    }
}
Also used : ArrayList(java.util.ArrayList) RelDataType(org.apache.calcite.rel.type.RelDataType) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelNode(org.apache.calcite.rel.RelNode) RexNode(org.apache.calcite.rex.RexNode)

Example 79 with RelDataTypeField

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataTypeField in project calcite by apache.

the class MongoSort method implement.

public void implement(Implementor implementor) {
    implementor.visitChild(0, getInput());
    if (!collation.getFieldCollations().isEmpty()) {
        final List<String> keys = new ArrayList<String>();
        final List<RelDataTypeField> fields = getRowType().getFieldList();
        for (RelFieldCollation fieldCollation : collation.getFieldCollations()) {
            final String name = fields.get(fieldCollation.getFieldIndex()).getName();
            keys.add(name + ": " + direction(fieldCollation));
            if (false) {
                // TODO: NULLS FIRST and NULLS LAST
                switch(fieldCollation.nullDirection) {
                    case FIRST:
                        break;
                    case LAST:
                        break;
                }
            }
        }
        implementor.add(null, "{$sort: " + Util.toString(keys, "{", ", ", "}") + "}");
    }
    if (offset != null) {
        implementor.add(null, "{$skip: " + ((RexLiteral) offset).getValue() + "}");
    }
    if (fetch != null) {
        implementor.add(null, "{$limit: " + ((RexLiteral) fetch).getValue() + "}");
    }
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) ArrayList(java.util.ArrayList) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation)

Example 80 with RelDataTypeField

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataTypeField in project calcite by apache.

the class RelOptUtil method createRenameRel.

// to be removed before 2.0
@Deprecated
public static RelNode createRenameRel(RelDataType outputType, RelNode rel) {
    RelDataType inputType = rel.getRowType();
    List<RelDataTypeField> inputFields = inputType.getFieldList();
    int n = inputFields.size();
    List<RelDataTypeField> outputFields = outputType.getFieldList();
    assert outputFields.size() == n : "rename: field count mismatch: in=" + inputType + ", out" + outputType;
    final List<Pair<RexNode, String>> renames = new ArrayList<>();
    for (Pair<RelDataTypeField, RelDataTypeField> pair : Pair.zip(inputFields, outputFields)) {
        final RelDataTypeField inputField = pair.left;
        final RelDataTypeField outputField = pair.right;
        assert inputField.getType().equals(outputField.getType());
        final RexBuilder rexBuilder = rel.getCluster().getRexBuilder();
        renames.add(Pair.<RexNode, String>of(rexBuilder.makeInputRef(inputField.getType(), inputField.getIndex()), outputField.getName()));
    }
    final RelBuilder relBuilder = RelFactories.LOGICAL_BUILDER.create(rel.getCluster(), null);
    return relBuilder.push(rel).project(Pair.left(renames), Pair.right(renames), true).build();
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelBuilder(org.apache.calcite.tools.RelBuilder) ArrayList(java.util.ArrayList) RexBuilder(org.apache.calcite.rex.RexBuilder) RelDataType(org.apache.calcite.rel.type.RelDataType) Pair(org.apache.calcite.util.Pair)

Aggregations

RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)383 RelDataType (org.apache.calcite.rel.type.RelDataType)206 RexNode (org.apache.calcite.rex.RexNode)185 ArrayList (java.util.ArrayList)177 RelNode (org.apache.calcite.rel.RelNode)130 RexBuilder (org.apache.calcite.rex.RexBuilder)76 RexInputRef (org.apache.calcite.rex.RexInputRef)72 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)65 Pair (org.apache.calcite.util.Pair)55 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)47 HashMap (java.util.HashMap)39 Map (java.util.Map)35 AggregateCall (org.apache.calcite.rel.core.AggregateCall)35 SqlNode (org.apache.calcite.sql.SqlNode)32 ImmutableList (com.google.common.collect.ImmutableList)31 RelBuilder (org.apache.calcite.tools.RelBuilder)29 RelDataTypeFieldImpl (org.apache.calcite.rel.type.RelDataTypeFieldImpl)25 List (java.util.List)23 LinkedHashSet (java.util.LinkedHashSet)22 RelOptUtil (org.apache.calcite.plan.RelOptUtil)22