Class RelFieldTrimmer
- All Implemented Interfaces:
ReflectiveVisitor
RelNode
with a 'slimmed down' relational expression that projects
only the columns required by its consumer.
Uses multi-methods to fire the right rule for each type of relational expression. This allows the transformer to be extended without having to add a new method to RelNode, and without requiring a collection of rule classes scattered to the four winds.
REVIEW: jhyde, 2009/7/28: Is sql2rel the correct package for this class?
Trimming fields is not an essential part of SQL-to-Rel translation, and
arguably belongs in the optimization phase. But this transformer does not
obey the usual pattern for planner rules; it is difficult to do so, because
each RelNode
needs to return a different set of fields after
trimming.
TODO: Change 2nd arg of the trimFields(org.apache.calcite.rel.RelNode, org.apache.calcite.util.ImmutableBitSet, java.util.Set<org.apache.calcite.rel.type.RelDataTypeField>)
method from BitSet to
Mapping. Sometimes it helps the consumer if you return the columns in a
particular order. For instance, it may avoid a project at the top of the
tree just for reordering. Could ease the transition by writing methods that
convert BitSet to Mapping and vice versa.
-
Nested Class Summary
Modifier and TypeClassDescriptionprotected static class
Result of an attempt to trim columns from a relational expression. -
Constructor Summary
ConstructorDescriptionRelFieldTrimmer
(@Nullable SqlValidator validator, RelOptCluster cluster, RelFactories.ProjectFactory projectFactory, RelFactories.FilterFactory filterFactory, RelFactories.JoinFactory joinFactory, RelFactories.SortFactory sortFactory, RelFactories.AggregateFactory aggregateFactory, RelFactories.SetOpFactory setOpFactory) Deprecated.RelFieldTrimmer
(@Nullable SqlValidator validator, RelBuilder relBuilder) Creates a RelFieldTrimmer. -
Method Summary
Modifier and TypeMethodDescriptionprotected Mapping
createMapping
(ImmutableBitSet fieldsUsed, int fieldCount) protected final RelFieldTrimmer.TrimResult
dispatchTrimFields
(RelNode rel, ImmutableBitSet fieldsUsed, Set<RelDataTypeField> extraFields) InvokestrimFields(org.apache.calcite.rel.RelNode, org.apache.calcite.util.ImmutableBitSet, java.util.Set<org.apache.calcite.rel.type.RelDataTypeField>)
, or the appropriate method for the type of the rel parameter, using multi-method dispatch.protected RelFieldTrimmer.TrimResult
dummyProject
(int fieldCount, RelNode input) Creates a project with a dummy column, to protect the parts of the system that cannot handle a relational expression with no columns.protected RelFieldTrimmer.TrimResult
dummyProject
(int fieldCount, RelNode input, @Nullable RelNode originalRelNode) Creates a project with a dummy column, to protect the parts of the system that cannot handle a relational expression with no columns.protected RelFieldTrimmer.TrimResult
protected RelFieldTrimmer.TrimResult
Trims unused fields from a relational expression.protected RelFieldTrimmer.TrimResult
trimChild
(RelNode rel, RelNode input, ImmutableBitSet fieldsUsed, Set<RelDataTypeField> extraFields) Trims the fields of an input relational expression.protected RelFieldTrimmer.TrimResult
trimChildRestore
(RelNode rel, RelNode input, ImmutableBitSet fieldsUsed, Set<RelDataTypeField> extraFields) Trims a child relational expression, then adds back a dummy project to restore the fields that were removed.trimFields
(Aggregate aggregate, ImmutableBitSet fieldsUsed, Set<RelDataTypeField> extraFields) Variant oftrimFields(RelNode, ImmutableBitSet, Set)
forLogicalAggregate
.trimFields
(Calc calc, ImmutableBitSet fieldsUsed, Set<RelDataTypeField> extraFields) Variant oftrimFields(RelNode, ImmutableBitSet, Set)
forLogicalCalc
.trimFields
(Exchange exchange, ImmutableBitSet fieldsUsed, Set<RelDataTypeField> extraFields) trimFields
(Filter filter, ImmutableBitSet fieldsUsed, Set<RelDataTypeField> extraFields) Variant oftrimFields(RelNode, ImmutableBitSet, Set)
forLogicalFilter
.trimFields
(Join join, ImmutableBitSet fieldsUsed, Set<RelDataTypeField> extraFields) Variant oftrimFields(RelNode, ImmutableBitSet, Set)
forLogicalJoin
.trimFields
(Project project, ImmutableBitSet fieldsUsed, Set<RelDataTypeField> extraFields) Variant oftrimFields(RelNode, ImmutableBitSet, Set)
forLogicalProject
.trimFields
(Sample sample, ImmutableBitSet fieldsUsed, Set<RelDataTypeField> extraFields) Variant oftrimFields(RelNode, ImmutableBitSet, Set)
forSample
.trimFields
(SetOp setOp, ImmutableBitSet fieldsUsed, Set<RelDataTypeField> extraFields) Variant oftrimFields(RelNode, ImmutableBitSet, Set)
forSetOp
(Only UNION ALL is supported).trimFields
(Snapshot snapshot, ImmutableBitSet fieldsUsed, Set<RelDataTypeField> extraFields) Variant oftrimFields(RelNode, ImmutableBitSet, Set)
forSnapshot
.trimFields
(SortExchange sortExchange, ImmutableBitSet fieldsUsed, Set<RelDataTypeField> extraFields) trimFields
(Sort sort, ImmutableBitSet fieldsUsed, Set<RelDataTypeField> extraFields) Variant oftrimFields(RelNode, ImmutableBitSet, Set)
forSort
.trimFields
(TableScan tableAccessRel, ImmutableBitSet fieldsUsed, Set<RelDataTypeField> extraFields) Variant oftrimFields(RelNode, ImmutableBitSet, Set)
forLogicalTableScan
.trimFields
(LogicalTableFunctionScan tabFun, ImmutableBitSet fieldsUsed, Set<RelDataTypeField> extraFields) Variant oftrimFields(RelNode, ImmutableBitSet, Set)
forLogicalTableFunctionScan
.trimFields
(LogicalTableModify modifier, ImmutableBitSet fieldsUsed, Set<RelDataTypeField> extraFields) Variant oftrimFields(RelNode, ImmutableBitSet, Set)
forLogicalTableModify
.trimFields
(LogicalValues values, ImmutableBitSet fieldsUsed, Set<RelDataTypeField> extraFields) Variant oftrimFields(RelNode, ImmutableBitSet, Set)
forLogicalValues
.trimFields
(RelNode rel, ImmutableBitSet fieldsUsed, Set<RelDataTypeField> extraFields) Visit method, perReflectiveVisitor
.
-
Constructor Details
-
RelFieldTrimmer
Creates a RelFieldTrimmer.- Parameters:
validator
- Validator
-
RelFieldTrimmer
@Deprecated public RelFieldTrimmer(@Nullable SqlValidator validator, RelOptCluster cluster, RelFactories.ProjectFactory projectFactory, RelFactories.FilterFactory filterFactory, RelFactories.JoinFactory joinFactory, RelFactories.SortFactory sortFactory, RelFactories.AggregateFactory aggregateFactory, RelFactories.SetOpFactory setOpFactory) Deprecated.
-
-
Method Details
-
trim
Trims unused fields from a relational expression.We presume that all fields of the relational expression are wanted by its consumer, so only trim fields that are not used within the tree.
- Parameters:
root
- Root node of relational expression- Returns:
- Trimmed relational expression
-
trimChild
protected RelFieldTrimmer.TrimResult trimChild(RelNode rel, RelNode input, ImmutableBitSet fieldsUsed, Set<RelDataTypeField> extraFields) Trims the fields of an input relational expression.- Parameters:
rel
- Relational expressioninput
- Input relational expression, whose fields to trimfieldsUsed
- Bitmap of fields needed by the consumer- Returns:
- New relational expression and its field mapping
-
trimChildRestore
protected RelFieldTrimmer.TrimResult trimChildRestore(RelNode rel, RelNode input, ImmutableBitSet fieldsUsed, Set<RelDataTypeField> extraFields) Trims a child relational expression, then adds back a dummy project to restore the fields that were removed.Sounds pointless? It causes unused fields to be removed further down the tree (towards the leaves), but it ensure that the consuming relational expression continues to see the same fields.
- Parameters:
rel
- Relational expressioninput
- Input relational expression, whose fields to trimfieldsUsed
- Bitmap of fields needed by the consumer- Returns:
- New relational expression and its field mapping
-
dispatchTrimFields
protected final RelFieldTrimmer.TrimResult dispatchTrimFields(RelNode rel, ImmutableBitSet fieldsUsed, Set<RelDataTypeField> extraFields) InvokestrimFields(org.apache.calcite.rel.RelNode, org.apache.calcite.util.ImmutableBitSet, java.util.Set<org.apache.calcite.rel.type.RelDataTypeField>)
, or the appropriate method for the type of the rel parameter, using multi-method dispatch.- Parameters:
rel
- Relational expressionfieldsUsed
- Bitmap of fields needed by the consumer- Returns:
- New relational expression and its field mapping
-
result
-
result
-
trimFields
public RelFieldTrimmer.TrimResult trimFields(RelNode rel, ImmutableBitSet fieldsUsed, Set<RelDataTypeField> extraFields) Visit method, perReflectiveVisitor
.This method is invoked reflectively, so there may not be any apparent calls to it. The class (or derived classes) may contain overloads of this method with more specific types for the
rel
parameter.Returns a pair: the relational expression created, and the mapping between the original fields and the fields of the newly created relational expression.
- Parameters:
rel
- Relational expressionfieldsUsed
- Fields needed by the consumer- Returns:
- relational expression and mapping
-
trimFields
public RelFieldTrimmer.TrimResult trimFields(Calc calc, ImmutableBitSet fieldsUsed, Set<RelDataTypeField> extraFields) Variant oftrimFields(RelNode, ImmutableBitSet, Set)
forLogicalCalc
. -
trimFields
public RelFieldTrimmer.TrimResult trimFields(Project project, ImmutableBitSet fieldsUsed, Set<RelDataTypeField> extraFields) Variant oftrimFields(RelNode, ImmutableBitSet, Set)
forLogicalProject
. -
dummyProject
Creates a project with a dummy column, to protect the parts of the system that cannot handle a relational expression with no columns.- Parameters:
fieldCount
- Number of fields in the original relational expressioninput
- Trimmed input- Returns:
- Dummy project
-
dummyProject
protected RelFieldTrimmer.TrimResult dummyProject(int fieldCount, RelNode input, @Nullable RelNode originalRelNode) Creates a project with a dummy column, to protect the parts of the system that cannot handle a relational expression with no columns.- Parameters:
fieldCount
- Number of fields in the original relational expressioninput
- Trimmed inputoriginalRelNode
- Source RelNode for hint propagation (or null if no propagation needed)- Returns:
- Dummy project
-
trimFields
public RelFieldTrimmer.TrimResult trimFields(Filter filter, ImmutableBitSet fieldsUsed, Set<RelDataTypeField> extraFields) Variant oftrimFields(RelNode, ImmutableBitSet, Set)
forLogicalFilter
. -
trimFields
public RelFieldTrimmer.TrimResult trimFields(Sort sort, ImmutableBitSet fieldsUsed, Set<RelDataTypeField> extraFields) Variant oftrimFields(RelNode, ImmutableBitSet, Set)
forSort
. -
trimFields
public RelFieldTrimmer.TrimResult trimFields(Exchange exchange, ImmutableBitSet fieldsUsed, Set<RelDataTypeField> extraFields) -
trimFields
public RelFieldTrimmer.TrimResult trimFields(SortExchange sortExchange, ImmutableBitSet fieldsUsed, Set<RelDataTypeField> extraFields) -
trimFields
public RelFieldTrimmer.TrimResult trimFields(Join join, ImmutableBitSet fieldsUsed, Set<RelDataTypeField> extraFields) Variant oftrimFields(RelNode, ImmutableBitSet, Set)
forLogicalJoin
. -
trimFields
public RelFieldTrimmer.TrimResult trimFields(SetOp setOp, ImmutableBitSet fieldsUsed, Set<RelDataTypeField> extraFields) Variant oftrimFields(RelNode, ImmutableBitSet, Set)
forSetOp
(Only UNION ALL is supported). -
trimFields
public RelFieldTrimmer.TrimResult trimFields(Aggregate aggregate, ImmutableBitSet fieldsUsed, Set<RelDataTypeField> extraFields) Variant oftrimFields(RelNode, ImmutableBitSet, Set)
forLogicalAggregate
. -
trimFields
public RelFieldTrimmer.TrimResult trimFields(LogicalTableModify modifier, ImmutableBitSet fieldsUsed, Set<RelDataTypeField> extraFields) Variant oftrimFields(RelNode, ImmutableBitSet, Set)
forLogicalTableModify
. -
trimFields
public RelFieldTrimmer.TrimResult trimFields(LogicalTableFunctionScan tabFun, ImmutableBitSet fieldsUsed, Set<RelDataTypeField> extraFields) Variant oftrimFields(RelNode, ImmutableBitSet, Set)
forLogicalTableFunctionScan
. -
trimFields
public RelFieldTrimmer.TrimResult trimFields(LogicalValues values, ImmutableBitSet fieldsUsed, Set<RelDataTypeField> extraFields) Variant oftrimFields(RelNode, ImmutableBitSet, Set)
forLogicalValues
. -
trimFields
public RelFieldTrimmer.TrimResult trimFields(Sample sample, ImmutableBitSet fieldsUsed, Set<RelDataTypeField> extraFields) Variant oftrimFields(RelNode, ImmutableBitSet, Set)
forSample
. -
trimFields
public RelFieldTrimmer.TrimResult trimFields(Snapshot snapshot, ImmutableBitSet fieldsUsed, Set<RelDataTypeField> extraFields) Variant oftrimFields(RelNode, ImmutableBitSet, Set)
forSnapshot
. -
createMapping
-
trimFields
public RelFieldTrimmer.TrimResult trimFields(TableScan tableAccessRel, ImmutableBitSet fieldsUsed, Set<RelDataTypeField> extraFields) Variant oftrimFields(RelNode, ImmutableBitSet, Set)
forLogicalTableScan
.
-