Search in sources :

Example 21 with RelDataTypeField

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

the class ElasticsearchSort method implement.

@Override
public void implement(Implementor implementor) {
    implementor.visitChild(0, getInput());
    if (!collation.getFieldCollations().isEmpty()) {
        final List<String> keys = new ArrayList<>();
        if (input instanceof Project) {
            final List<RexNode> projects = ((Project) input).getProjects();
            for (RelFieldCollation fieldCollation : collation.getFieldCollations()) {
                RexNode project = projects.get(fieldCollation.getFieldIndex());
                String name = project.accept(MapProjectionFieldVisitor.INSTANCE);
                keys.add(ElasticsearchRules.quote(name) + ": " + direction(fieldCollation));
            }
        } else {
            final List<RelDataTypeField> fields = getRowType().getFieldList();
            for (RelFieldCollation fieldCollation : collation.getFieldCollations()) {
                final String name = fields.get(fieldCollation.getFieldIndex()).getName();
                keys.add(ElasticsearchRules.quote(name) + ": " + direction(fieldCollation));
            }
        }
        implementor.add("\"sort\": [ " + Util.toString(keys, "{", "}, {", "}") + "]");
    }
    if (offset != null) {
        implementor.add("\"from\": " + ((RexLiteral) offset).getValue());
    }
    if (fetch != null) {
        implementor.add("\"size\": " + ((RexLiteral) fetch).getValue());
    }
}
Also used : Project(org.apache.calcite.rel.core.Project) RexLiteral(org.apache.calcite.rex.RexLiteral) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) ArrayList(java.util.ArrayList) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) RexNode(org.apache.calcite.rex.RexNode)

Example 22 with RelDataTypeField

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

the class TableScanNode method createEnumerable.

private static TableScanNode createEnumerable(Compiler compiler, TableScan rel, Enumerable<Row> enumerable, final ImmutableIntList acceptedProjects, List<RexNode> rejectedFilters, final ImmutableIntList rejectedProjects) {
    if (!rejectedFilters.isEmpty()) {
        final RexNode filter = RexUtil.composeConjunction(rel.getCluster().getRexBuilder(), rejectedFilters, false);
        assert filter != null;
        // Re-map filter for the projects that have been applied already
        final RexNode filter2;
        final RelDataType inputRowType;
        if (acceptedProjects == null) {
            filter2 = filter;
            inputRowType = rel.getRowType();
        } else {
            final Mapping mapping = Mappings.target(acceptedProjects, rel.getTable().getRowType().getFieldCount());
            filter2 = RexUtil.apply(mapping, filter);
            final RelDataTypeFactory.Builder builder = rel.getCluster().getTypeFactory().builder();
            final List<RelDataTypeField> fieldList = rel.getTable().getRowType().getFieldList();
            for (int acceptedProject : acceptedProjects) {
                builder.add(fieldList.get(acceptedProject));
            }
            inputRowType = builder.build();
        }
        final Scalar condition = compiler.compile(ImmutableList.of(filter2), inputRowType);
        final Context context = compiler.createContext();
        enumerable = enumerable.where(new Predicate1<Row>() {

            @Override
            public boolean apply(Row row) {
                context.values = row.getValues();
                Boolean b = (Boolean) condition.execute(context);
                return b != null && b;
            }
        });
    }
    if (rejectedProjects != null) {
        enumerable = enumerable.select(new Function1<Row, Row>() {

            final Object[] values = new Object[rejectedProjects.size()];

            @Override
            public Row apply(Row row) {
                final Object[] inValues = row.getValues();
                for (int i = 0; i < rejectedProjects.size(); i++) {
                    values[i] = inValues[rejectedProjects.get(i)];
                }
                return Row.asCopy(values);
            }
        });
    }
    return new TableScanNode(compiler, rel, enumerable);
}
Also used : DataContext(org.apache.calcite.DataContext) Function1(org.apache.calcite.linq4j.function.Function1) RelDataType(org.apache.calcite.rel.type.RelDataType) Mapping(org.apache.calcite.util.mapping.Mapping) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) Predicate1(org.apache.calcite.linq4j.function.Predicate1) RexNode(org.apache.calcite.rex.RexNode)

Example 23 with RelDataTypeField

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

the class CalciteMetaImpl method columns.

public Enumerable<MetaColumn> columns(final MetaTable table_) {
    final CalciteMetaTable table = (CalciteMetaTable) table_;
    final RelDataType rowType = table.calciteTable.getRowType(getConnection().typeFactory);
    return Linq4j.asEnumerable(rowType.getFieldList()).select(new Function1<RelDataTypeField, MetaColumn>() {

        public MetaColumn apply(RelDataTypeField field) {
            final int precision = field.getType().getSqlTypeName().allowsPrec() && !(field.getType() instanceof RelDataTypeFactoryImpl.JavaType) ? field.getType().getPrecision() : -1;
            return new MetaColumn(table.tableCat, table.tableSchem, table.tableName, field.getName(), field.getType().getSqlTypeName().getJdbcOrdinal(), field.getType().getFullTypeString(), precision, field.getType().getSqlTypeName().allowsScale() ? field.getType().getScale() : null, 10, field.getType().isNullable() ? DatabaseMetaData.columnNullable : DatabaseMetaData.columnNoNulls, precision, field.getIndex() + 1, field.getType().isNullable() ? "YES" : "NO");
        }
    });
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelDataTypeFactoryImpl(org.apache.calcite.rel.type.RelDataTypeFactoryImpl) RelDataType(org.apache.calcite.rel.type.RelDataType)

Example 24 with RelDataTypeField

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

the class RelMdSize method averageColumnSizes.

public List<Double> averageColumnSizes(Values rel, RelMetadataQuery mq) {
    final List<RelDataTypeField> fields = rel.getRowType().getFieldList();
    final ImmutableList.Builder<Double> list = ImmutableList.builder();
    for (int i = 0; i < fields.size(); i++) {
        RelDataTypeField field = fields.get(i);
        double d;
        if (rel.getTuples().isEmpty()) {
            d = averageTypeValueSize(field.getType());
        } else {
            d = 0;
            for (ImmutableList<RexLiteral> literals : rel.getTuples()) {
                d += typeValueSize(field.getType(), literals.get(i).getValueAs(Comparable.class));
            }
            d /= rel.getTuples().size();
        }
        list.add(d);
    }
    return list.build();
}
Also used : RexLiteral(org.apache.calcite.rex.RexLiteral) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) ImmutableList(com.google.common.collect.ImmutableList)

Example 25 with RelDataTypeField

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

the class RelMdSize method averageColumnSizes.

public List<Double> averageColumnSizes(TableScan rel, RelMetadataQuery mq) {
    final List<RelDataTypeField> fields = rel.getRowType().getFieldList();
    final ImmutableList.Builder<Double> list = ImmutableList.builder();
    for (RelDataTypeField field : fields) {
        list.add(averageTypeValueSize(field.getType()));
    }
    return list.build();
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) ImmutableList(com.google.common.collect.ImmutableList)

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)173 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