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));
}
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();
}
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);
}
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);
}
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));
}
Aggregations