Search in sources :

Example 6 with ImmutableIntList

use of org.apache.calcite.util.ImmutableIntList in project calcite by apache.

the class FilterTableScanRule method apply.

// ~ Methods ----------------------------------------------------------------
protected void apply(RelOptRuleCall call, Filter filter, TableScan scan) {
    final ImmutableIntList projects;
    final ImmutableList.Builder<RexNode> filters = ImmutableList.builder();
    if (scan instanceof Bindables.BindableTableScan) {
        final Bindables.BindableTableScan bindableScan = (Bindables.BindableTableScan) scan;
        filters.addAll(bindableScan.filters);
        projects = bindableScan.projects;
    } else {
        projects = scan.identity();
    }
    final Mapping mapping = Mappings.target(projects, scan.getTable().getRowType().getFieldCount());
    filters.add(RexUtil.apply(mapping, filter.getCondition()));
    call.transformTo(Bindables.BindableTableScan.create(scan.getCluster(), scan.getTable(), filters.build(), projects));
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) ImmutableIntList(org.apache.calcite.util.ImmutableIntList) Mapping(org.apache.calcite.util.mapping.Mapping) Bindables(org.apache.calcite.interpreter.Bindables) RexNode(org.apache.calcite.rex.RexNode)

Example 7 with ImmutableIntList

use of org.apache.calcite.util.ImmutableIntList in project calcite by apache.

the class SqlToRelConverter method createSource.

/**
 * Wraps a relational expression in the projects and filters implied by
 * a {@link ModifiableView}.
 *
 * <p>The input relational expression is suitable for inserting into the view,
 * and the returned relational expression is suitable for inserting into its
 * delegate table.
 *
 * <p>In principle, the delegate table of a view might be another modifiable
 * view, and if so, the process can be repeated.
 */
private RelNode createSource(RelOptTable targetTable, RelNode source, ModifiableView modifiableView, RelDataType delegateRowType) {
    final ImmutableIntList mapping = modifiableView.getColumnMapping();
    assert mapping.size() == targetTable.getRowType().getFieldCount();
    // For columns represented in the mapping, the expression is just a field
    // reference.
    final Map<Integer, RexNode> projectMap = new HashMap<>();
    final List<RexNode> filters = new ArrayList<>();
    for (int i = 0; i < mapping.size(); i++) {
        int target = mapping.get(i);
        if (target >= 0) {
            projectMap.put(target, RexInputRef.of(i, source.getRowType()));
        }
    }
    // For columns that are not in the mapping, and have a constraint of the
    // form "column = value", the expression is the literal "value".
    // 
    // If a column has multiple constraints, the extra ones will become a
    // filter.
    final RexNode constraint = modifiableView.getConstraint(rexBuilder, delegateRowType);
    RelOptUtil.inferViewPredicates(projectMap, filters, constraint);
    final List<Pair<RexNode, String>> projects = new ArrayList<>();
    for (RelDataTypeField field : delegateRowType.getFieldList()) {
        RexNode node = projectMap.get(field.getIndex());
        if (node == null) {
            node = rexBuilder.makeNullLiteral(field.getType());
        }
        projects.add(Pair.of(rexBuilder.ensureType(field.getType(), node, false), field.getName()));
    }
    return relBuilder.push(source).projectNamed(Pair.left(projects), Pair.right(projects), false).filter(filters).build();
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ImmutableIntList(org.apache.calcite.util.ImmutableIntList) RexNode(org.apache.calcite.rex.RexNode) Pair(org.apache.calcite.util.Pair)

Example 8 with ImmutableIntList

use of org.apache.calcite.util.ImmutableIntList in project calcite by apache.

the class RelDistributions method range.

/**
 * Creates a range distribution.
 */
public static RelDistribution range(Collection<? extends Number> numbers) {
    ImmutableIntList list = ImmutableIntList.copyOf(numbers);
    RelDistributionImpl trait = new RelDistributionImpl(RelDistribution.Type.RANGE_DISTRIBUTED, list);
    return RelDistributionTraitDef.INSTANCE.canonize(trait);
}
Also used : ImmutableIntList(org.apache.calcite.util.ImmutableIntList)

Example 9 with ImmutableIntList

use of org.apache.calcite.util.ImmutableIntList in project calcite by apache.

the class RelDistributions method hash.

/**
 * Creates a hash distribution.
 */
public static RelDistribution hash(Collection<? extends Number> numbers) {
    ImmutableIntList list = ImmutableIntList.copyOf(numbers);
    if (numbers.size() > 1 && !Ordering.natural().isOrdered(list)) {
        list = ImmutableIntList.copyOf(Ordering.natural().sortedCopy(list));
    }
    RelDistributionImpl trait = new RelDistributionImpl(RelDistribution.Type.HASH_DISTRIBUTED, list);
    return RelDistributionTraitDef.INSTANCE.canonize(trait);
}
Also used : ImmutableIntList(org.apache.calcite.util.ImmutableIntList)

Example 10 with ImmutableIntList

use of org.apache.calcite.util.ImmutableIntList in project calcite by apache.

the class ProjectTableScanRule method apply.

// ~ Methods ----------------------------------------------------------------
protected void apply(RelOptRuleCall call, Project project, TableScan scan) {
    final RelOptTable table = scan.getTable();
    assert table.unwrap(ProjectableFilterableTable.class) != null;
    final Mappings.TargetMapping mapping = project.getMapping();
    if (mapping == null || Mappings.isIdentity(mapping)) {
        return;
    }
    final ImmutableIntList projects;
    final ImmutableList<RexNode> filters;
    if (scan instanceof Bindables.BindableTableScan) {
        final Bindables.BindableTableScan bindableScan = (Bindables.BindableTableScan) scan;
        filters = bindableScan.filters;
        projects = bindableScan.projects;
    } else {
        filters = ImmutableList.of();
        projects = scan.identity();
    }
    final List<Integer> projects2 = Mappings.apply((Mapping) mapping, projects);
    call.transformTo(Bindables.BindableTableScan.create(scan.getCluster(), scan.getTable(), filters, projects2));
}
Also used : Mappings(org.apache.calcite.util.mapping.Mappings) ProjectableFilterableTable(org.apache.calcite.schema.ProjectableFilterableTable) ImmutableIntList(org.apache.calcite.util.ImmutableIntList) RelOptTable(org.apache.calcite.plan.RelOptTable) Bindables(org.apache.calcite.interpreter.Bindables) RexNode(org.apache.calcite.rex.RexNode)

Aggregations

ImmutableIntList (org.apache.calcite.util.ImmutableIntList)17 RexNode (org.apache.calcite.rex.RexNode)11 RexBuilder (org.apache.calcite.rex.RexBuilder)6 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)6 ArrayList (java.util.ArrayList)5 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)5 RelNode (org.apache.calcite.rel.RelNode)4 Join (org.apache.calcite.rel.core.Join)4 RelOptCluster (org.apache.calcite.plan.RelOptCluster)3 JoinInfo (org.apache.calcite.rel.core.JoinInfo)3 RelDataType (org.apache.calcite.rel.type.RelDataType)3 ImmutableList (com.google.common.collect.ImmutableList)2 Bindables (org.apache.calcite.interpreter.Bindables)2 RelOptTable (org.apache.calcite.plan.RelOptTable)2 HepRelVertex (org.apache.calcite.plan.hep.HepRelVertex)2 RelCollation (org.apache.calcite.rel.RelCollation)2 SemiJoin (org.apache.calcite.rel.core.SemiJoin)2 RelMetadataQuery (org.apache.calcite.rel.metadata.RelMetadataQuery)2 Pair (org.apache.calcite.util.Pair)2 HashMap (java.util.HashMap)1