Class PushProjector
- java.lang.Object
-
- org.apache.calcite.rel.rules.PushProjector
-
public class PushProjector extends java.lang.Object
PushProjector is a utility class used to perform operations used in push projection rules.Pushing is particularly interesting in the case of join, because there are multiple inputs. Generally an expression can be pushed down to a particular input if it depends upon no other inputs. If it can be pushed down to both sides, it is pushed down to the left.
Sometimes an expression needs to be split before it can be pushed down. To flag that an expression cannot be split, specify a rule that it must be preserved. Such an expression will be pushed down intact to one of the inputs, or not pushed down at all.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
PushProjector.ExprCondition
A functor that replies true or false for a given expression.private class
PushProjector.InputSpecialOpFinder
Visitor which builds a bitmap of the inputs used by an expressions, as well as locating expressions corresponding to special operators.(package private) class
PushProjector.OperatorExprCondition
An expression condition that evaluates to true if the expression is a call to one of a set of operators.private class
PushProjector.RefAndExprConverter
Walks an expression tree, replacing input refs with new values to reflect projection and converting special expressions to field references.
-
Field Summary
Fields Modifier and Type Field Description (package private) ImmutableBitSet
childBitmap
Bitmap containing the fields in the RelNode that the projection is being pushed past, if the RelNode is not a join.(package private) java.util.List<RelDataTypeField>
childFields
Fields from the RelNode that the projection is being pushed past(package private) java.util.List<RexNode>
childPreserveExprs
Expressions referenced in the projection/filter that should be preserved.private RelNode
childRel
(package private) int
nChildFields
Number of fields in the RelNode that the projection is being pushed past(package private) int
nFields
Number of fields in the RelNode that the projection is being pushed past, if the RelNode is not a join.(package private) int
nFieldsRight
Number of fields in the right hand side of a join, in the case where the projection is being pushed past a join.(package private) int
nProject
Number of fields being projected.(package private) int
nRightProject
Number of fields being projected from the right hand side of a join, in the case where the projection is being pushed past a join.private int
nSysFields
Number of system fields.(package private) int
nSystemProject
Number of system fields being projected.private RexNode
origFilter
private Project
origProj
(package private) java.util.List<RexNode>
origProjExprs
Original projection expressionsprivate PushProjector.ExprCondition
preserveExprCondition
(package private) java.util.BitSet
projRefs
Bitmap containing the references in the original projectionprivate RelBuilder
relBuilder
(package private) RexBuilder
rexBuilder
Rex builder used to create new expressions.(package private) ImmutableBitSet
rightBitmap
Bitmap containing the fields in the right hand side of a join, in the case where the projection is being pushed past a join.(package private) java.util.List<RexNode>
rightPreserveExprs
Expressions referenced in the projection/filter that should be preserved, corresponding to expressions on the right hand side of the join, if the projection is being pushed past a join.(package private) ImmutableBitSet
strongBitmap
Bitmap containing the fields that should be strong, i.e.
-
Constructor Summary
Constructors Constructor Description PushProjector(Project origProj, RexNode origFilter, RelNode childRel, PushProjector.ExprCondition preserveExprCondition, RelBuilder relBuilder)
Creates a PushProjector object for pushing projects past a RelNode.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description RelNode
convertProject(RexNode defaultExpr)
Decomposes a projection to the input references referenced by a projection and a filter, either of which is optional.RexNode
convertRefsAndExprs(RexNode rex, java.util.List<RelDataTypeField> destFields, int[] adjustments)
Clones an expression tree and walks through it, adjusting each RexInputRef index by some amount, and converting expressions that need to be preserved to field references.RelNode
createNewProject(RelNode projChild, int[] adjustments)
Creates a new projection based on the original projection, adjusting all input refs using an adjustment array passed in.Project
createProjectRefsAndExprs(RelNode projChild, boolean adjust, boolean rightSide)
Creates a projection based on the inputs specified in a bitmap and the expressions that need to be preserved.int[]
getAdjustments()
Determines how much each input reference needs to be adjusted as a result of projectionboolean
locateAllRefs()
Locates all references found in either the projection expressions a filter, as well as references to expressions that should be preserved.
-
-
-
Field Detail
-
origProj
private final Project origProj
-
origFilter
private final RexNode origFilter
-
childRel
private final RelNode childRel
-
preserveExprCondition
private final PushProjector.ExprCondition preserveExprCondition
-
relBuilder
private final RelBuilder relBuilder
-
origProjExprs
final java.util.List<RexNode> origProjExprs
Original projection expressions
-
childFields
final java.util.List<RelDataTypeField> childFields
Fields from the RelNode that the projection is being pushed past
-
nChildFields
final int nChildFields
Number of fields in the RelNode that the projection is being pushed past
-
projRefs
final java.util.BitSet projRefs
Bitmap containing the references in the original projection
-
childBitmap
final ImmutableBitSet childBitmap
Bitmap containing the fields in the RelNode that the projection is being pushed past, if the RelNode is not a join. If the RelNode is a join, then the fields correspond to the left hand side of the join.
-
rightBitmap
final ImmutableBitSet rightBitmap
Bitmap containing the fields in the right hand side of a join, in the case where the projection is being pushed past a join. Not used otherwise.
-
strongBitmap
final ImmutableBitSet strongBitmap
Bitmap containing the fields that should be strong, i.e. when preserving expressions we can only preserve them if the expressions if it is null when these fields are null.
-
nFields
final int nFields
Number of fields in the RelNode that the projection is being pushed past, if the RelNode is not a join. If the RelNode is a join, then this is the number of fields in the left hand side of the join.The identity
nChildFields == nSysFields + nFields + nFieldsRight
holds.nFields
does not includenSysFields
. The output of a join looks like this:| nSysFields | nFields | nFieldsRight |
The output of a single-input rel looks like this:
| nSysFields | nFields |
-
nFieldsRight
final int nFieldsRight
Number of fields in the right hand side of a join, in the case where the projection is being pushed past a join. Always 0 otherwise.
-
nSysFields
private final int nSysFields
Number of system fields. System fields appear at the start of a join, before the first field from the left input.
-
childPreserveExprs
final java.util.List<RexNode> childPreserveExprs
Expressions referenced in the projection/filter that should be preserved. In the case where the projection is being pushed past a join, then the list only contains the expressions corresponding to the left hand side of the join.
-
rightPreserveExprs
final java.util.List<RexNode> rightPreserveExprs
Expressions referenced in the projection/filter that should be preserved, corresponding to expressions on the right hand side of the join, if the projection is being pushed past a join. Empty list otherwise.
-
nSystemProject
int nSystemProject
Number of system fields being projected.
-
nProject
int nProject
Number of fields being projected. In the case where the projection is being pushed past a join, the number of fields being projected from the left hand side of the join.
-
nRightProject
int nRightProject
Number of fields being projected from the right hand side of a join, in the case where the projection is being pushed past a join. 0 otherwise.
-
rexBuilder
final RexBuilder rexBuilder
Rex builder used to create new expressions.
-
-
Constructor Detail
-
PushProjector
public PushProjector(Project origProj, RexNode origFilter, RelNode childRel, PushProjector.ExprCondition preserveExprCondition, RelBuilder relBuilder)
Creates a PushProjector object for pushing projects past a RelNode.- Parameters:
origProj
- the original projection that is being pushed; may be null if the projection is implied as a result of a projection having been trivially removedorigFilter
- the filter that the projection must also be pushed past, if applicablechildRel
- the RelNode that the projection is being pushed pastpreserveExprCondition
- condition for whether an expression should be preserved in the projection
-
-
Method Detail
-
convertProject
public RelNode convertProject(RexNode defaultExpr)
Decomposes a projection to the input references referenced by a projection and a filter, either of which is optional. If both are provided, the filter is underneath the project.Creates a projection containing all input references as well as preserving any special expressions. Converts the original projection and/or filter to reference the new projection. Then, finally puts on top, a final projection corresponding to the original projection.
- Parameters:
defaultExpr
- expression to be used in the projection if no fields or special columns are selected- Returns:
- the converted projection if it makes sense to push elements of the projection; otherwise returns null
-
locateAllRefs
public boolean locateAllRefs()
Locates all references found in either the projection expressions a filter, as well as references to expressions that should be preserved. Based on that, determines whether pushing the projection makes sense.- Returns:
- true if all inputs from the child that the projection is being pushed past are referenced in the projection/filter and no special preserve expressions are referenced; in that case, it does not make sense to push the projection
-
createProjectRefsAndExprs
public Project createProjectRefsAndExprs(RelNode projChild, boolean adjust, boolean rightSide)
Creates a projection based on the inputs specified in a bitmap and the expressions that need to be preserved. The expressions are appended after the input references.- Parameters:
projChild
- child that the projection will be created on top ofadjust
- if true, need to create new projection expressions; otherwise, the existing ones are reusedrightSide
- if true, creating a projection for the right hand side of a join- Returns:
- created projection
-
getAdjustments
public int[] getAdjustments()
Determines how much each input reference needs to be adjusted as a result of projection- Returns:
- array indicating how much each input needs to be adjusted by
-
convertRefsAndExprs
public RexNode convertRefsAndExprs(RexNode rex, java.util.List<RelDataTypeField> destFields, int[] adjustments)
Clones an expression tree and walks through it, adjusting each RexInputRef index by some amount, and converting expressions that need to be preserved to field references.- Parameters:
rex
- the expressiondestFields
- fields that the new expressions will be referencingadjustments
- the amount each input reference index needs to be adjusted by- Returns:
- modified expression tree
-
createNewProject
public RelNode createNewProject(RelNode projChild, int[] adjustments)
Creates a new projection based on the original projection, adjusting all input refs using an adjustment array passed in. If there was no original projection, create a new one that selects every field from the underlying rel.If the resulting projection would be trivial, return the child.
- Parameters:
projChild
- child of the new projectadjustments
- array indicating how much each input reference should be adjusted by- Returns:
- the created projection
-
-