public class RexProgramBuilder extends Object
RexProgram.
RexProgramBuilder is necessary because a RexProgram is immutable.
(The String class has the same problem: it is immutable, so they
introduced StringBuilder.)
| Constructor and Description |
|---|
RexProgramBuilder(RelDataType inputRowType,
RexBuilder rexBuilder)
Creates a program-builder.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addCondition(RexNode expr)
Sets the condition of the program.
|
RexLocalRef |
addExpr(RexNode expr)
Adds an expression to the list of common expressions, and returns a
reference to the expression.
|
void |
addIdentity()
Adds a project item for every input field.
|
RexLocalRef |
addProject(int at,
int ordinal,
String name)
Adds a projection based upon the
indexth expression at a
given position. |
RexLocalRef |
addProject(int at,
RexNode expr,
String name)
Adds a project expression to the program at a given position.
|
RexLocalRef |
addProject(int ordinal,
String name)
Adds a projection based upon the
indexth expression. |
RexLocalRef |
addProject(RexNode expr,
String name)
Adds a project expression to the program.
|
void |
clearCondition()
Clears the condition.
|
void |
clearProjects()
Removes all project items.
|
static RexProgramBuilder |
create(RexBuilder rexBuilder,
RelDataType inputRowType,
List<RexNode> exprList,
List<RexLocalRef> projectRefList,
RexLocalRef conditionRef,
RelDataType outputRowType,
boolean normalize)
Creates a program builder with the same contents as a program.
|
static RexProgramBuilder |
create(RexBuilder rexBuilder,
RelDataType inputRowType,
List<RexNode> exprList,
List<RexLocalRef> projectRefList,
RexLocalRef conditionRef,
RelDataType outputRowType,
RexShuttle shuttle,
boolean updateRefs)
Creates a program builder with the same contents as a program, applying a
shuttle first.
|
static RexProgramBuilder |
forProgram(RexProgram program,
RexBuilder rexBuilder,
boolean normalize)
Creates a program builder and initializes it from an existing program.
|
RelDataType |
getInputRowType()
Returns the rowtype of the input to the program
|
RexProgram |
getProgram()
Converts the state of the program builder to an immutable program,
normalizing in the process.
|
RexProgram |
getProgram(boolean normalize)
Converts the state of the program builder to an immutable program.
|
List<RexLocalRef> |
getProjectList()
Returns the list of project expressions.
|
RexLocalRef |
makeInputRef(int index)
Creates a reference to a given input field.
|
static RexProgram |
mergePrograms(RexProgram topProgram,
RexProgram bottomProgram,
RexBuilder rexBuilder)
Merges two programs together, and normalizes the result.
|
static RexProgram |
mergePrograms(RexProgram topProgram,
RexProgram bottomProgram,
RexBuilder rexBuilder,
boolean normalize)
Merges two programs together.
|
static RexProgram |
normalize(RexBuilder rexBuilder,
RexProgram program)
Normalizes a program.
|
RexLocalRef |
registerInput(RexNode expr)
Registers an expression in the list of common sub-expressions, and
returns a reference to that expression.
|
RexLocalRef |
registerOutput(RexNode expr)
Converts an expression expressed in terms of the outputs of this
program into an expression expressed in terms of the inputs,
registers it in the list of common sub-expressions, and returns a
reference to that expression.
|
public RexProgramBuilder(RelDataType inputRowType, RexBuilder rexBuilder)
public RexLocalRef addProject(RexNode expr, String name)
The expression specified in terms of the input fields. If not, call
registerOutput(RexNode) first.
expr - Expression to addname - Name of field in output row type; if null, a unique name will
be generated when the program is createdpublic RexLocalRef addProject(int ordinal, String name)
indexth expression.ordinal - Index of expression to projectname - Name of field in output row type; if null, a unique name
will be generated when the program is createdpublic RexLocalRef addProject(int at, RexNode expr, String name)
The expression specified in terms of the input fields. If not, call
registerOutput(RexNode) first.
at - Position in project list to add expressionexpr - Expression to addname - Name of field in output row type; if null, a unique name will
be generated when the program is createdpublic RexLocalRef addProject(int at, int ordinal, String name)
indexth expression at a
given position.at - Position in project list to add expressionordinal - Index of expression to projectname - Name of field in output row type; if null, a unique name
will be generated when the program is createdpublic void addCondition(RexNode expr)
The expression must be specified in terms of the input fields. If
not, call registerOutput(RexNode) first.
public RexLocalRef registerInput(RexNode expr)
The expression must be expressed in terms of the inputs of this program.
public RexLocalRef registerOutput(RexNode expr)
expr - Expression to registerpublic RexLocalRef addExpr(RexNode expr)
expr - Expressionpublic RexProgram getProgram()
It is OK to call this method, modify the program specification (by adding projections, and so forth), and call this method again.
public RexProgram getProgram(boolean normalize)
It is OK to call this method, modify the program specification (by adding projections, and so forth), and call this method again.
normalize - Whether to normalizepublic static RexProgramBuilder forProgram(RexProgram program, RexBuilder rexBuilder, boolean normalize)
Calling getProgram() immediately after creation will return a
program equivalent (in terms of external behavior) to the existing
program.
The existing program will not be changed. (It cannot: programs are immutable.)
program - Existing programrexBuilder - Rex buildernormalize - Whether to normalizepublic static RexProgramBuilder create(RexBuilder rexBuilder, RelDataType inputRowType, List<RexNode> exprList, List<RexLocalRef> projectRefList, RexLocalRef conditionRef, RelDataType outputRowType, boolean normalize)
If normalize, converts the program to canonical form. In
canonical form, in addition to the usual constraints:
RexInputRefs to the N
input fields;
RexCalls must be RexLocalRefs (that is,
expressions must have maximum depth 1)
rexBuilder - Rex builderinputRowType - Input row typeexprList - Common expressionsprojectRefList - ProjectionsconditionRef - Condition, or nulloutputRowType - Output row typenormalize - Whether to normalizepublic static RexProgramBuilder create(RexBuilder rexBuilder, RelDataType inputRowType, List<RexNode> exprList, List<RexLocalRef> projectRefList, RexLocalRef conditionRef, RelDataType outputRowType, RexShuttle shuttle, boolean updateRefs)
TODO: Refactor the above create method in terms of this one.
rexBuilder - Rex builderinputRowType - Input row typeexprList - Common expressionsprojectRefList - ProjectionsconditionRef - Condition, or nulloutputRowType - Output row typeshuttle - Shuttle to apply to each expression before adding it
to the program builderupdateRefs - Whether to update references that changes as a result
of rewrites made by the shuttlepublic static RexProgram normalize(RexBuilder rexBuilder, RexProgram program)
rexBuilder - Rex builderprogram - Programpublic static RexProgram mergePrograms(RexProgram topProgram, RexProgram bottomProgram, RexBuilder rexBuilder)
topProgram - Top program. Its expressions are in terms of the
outputs of the bottom program.bottomProgram - Bottom program. Its expressions are in terms of the
result fields of the relational expression's inputrexBuilder - Rex buildermergePrograms(RexProgram, RexProgram, RexBuilder, boolean)public static RexProgram mergePrograms(RexProgram topProgram, RexProgram bottomProgram, RexBuilder rexBuilder, boolean normalize)
All expressions become common sub-expressions. For example, the query
SELECT x + 1 AS p, x + y AS q FROM (
SELECT a + b AS x, c AS y
FROM t
WHERE c = 6)
would be represented as the programs
Calc:
Projects={$2, $3},
Condition=null,
Exprs={$0, $1, $0 + 1, $0 + $1})
Calc(
Projects={$3, $2},
Condition={$4}
Exprs={$0, $1, $2, $0 + $1, $2 = 6}
The merged program is
Calc(
Projects={$4, $5}
Condition=$6
Exprs={0: $0 // a
1: $1 // b
2: $2 // c
3: ($0 + $1) // x = a + b
4: ($3 + 1) // p = x + 1
5: ($3 + $2) // q = x + y
6: ($2 = 6) // c = 6
Another example:
becomesSELECT * FROM ( SELECT a + b AS x, c AS y FROM t WHERE c = 6) WHERE x = 5
SELECT a + b AS x, c AS y FROM t WHERE c = 6 AND (a + b) = 5
topProgram - Top program. Its expressions are in terms of the
outputs of the bottom program.bottomProgram - Bottom program. Its expressions are in terms of the
result fields of the relational expression's inputrexBuilder - Rex buildernormalize - Whether to convert program to canonical formpublic void clearProjects()
After calling this method, you may need to re-normalize.
public void clearCondition()
After calling this method, you may need to re-normalize.
public void addIdentity()
You cannot call this method if there are other project items.
public RexLocalRef makeInputRef(int index)
index - Ordinal of input field, must be less than the number of
fields in the input typepublic RelDataType getInputRowType()
public List<RexLocalRef> getProjectList()
Copyright © 2012–2015 The Apache Software Foundation. All rights reserved.