Package org.apache.calcite.linq4j.tree
Class BlockBuilder
- java.lang.Object
-
- org.apache.calcite.linq4j.tree.BlockBuilder
-
public class BlockBuilder extends java.lang.Object
Builder forBlockStatement
.Has methods that help ensure that variable names are unique.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
BlockBuilder.InlineVariableVisitor
Inline Variable Visitor.private static class
BlockBuilder.Slot
Holds the number of times a declaration was used.private static class
BlockBuilder.SubstituteVariableVisitor
Substitute Variable Visitor.private static class
BlockBuilder.UseCounter
Use counter.
-
Field Summary
Fields Modifier and Type Field Description (package private) java.util.Map<Expression,DeclarationStatement>
expressionForReuse
Contains final-fine-to-reuse-declarations.private static Shuttle
OPTIMIZE_SHUTTLE
private boolean
optimizing
private BlockBuilder
parent
(package private) java.util.List<Statement>
statements
(package private) java.util.Set<java.lang.String>
variables
-
Constructor Summary
Constructors Constructor Description BlockBuilder()
Creates a non-optimizing BlockBuilder.BlockBuilder(boolean optimizing)
Creates a BlockBuilder.BlockBuilder(boolean optimizing, BlockBuilder parent)
Creates a BlockBuilder.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
add(Expression expression)
void
add(Statement statement)
protected void
addExpressionForReuse(DeclarationStatement decl)
Expression
append(java.lang.String name, BlockStatement block)
Appends a block to a list of statements and returns an expression (possibly a variable) that represents the result of the newly added block.Expression
append(java.lang.String name, BlockStatement block, boolean optimize)
Appends an expression to a list of statements, optionally optimizing it to a variable if it is used more than once.Expression
append(java.lang.String name, Expression expression)
Appends an expression to a list of statements, and returns an expression (possibly a variable) that represents the result of the newly added block.Expression
append(java.lang.String name, Expression expression, boolean optimize)
Appends an expression to a list of statements, optionally optimizing if the expression is used more than once.BlockBuilder
append(Expression expression)
private Expression
append_(java.lang.String name, Expression expression, boolean optimize)
Expression
appendIfNotNull(java.lang.String name, Expression expression)
Appends an expression to a list of statements, if it is not null.void
clear()
Clears this BlockBuilder.protected Shuttle
createFinishingOptimizeShuttle()
Creates a final optimization shuttle.protected Shuttle
createOptimizeShuttle()
Creates a shuttle that will be used during block optimization.DeclarationStatement
getComputedExpression(Expression expr)
Returns the reference to ParameterExpression if given expression was already computed and stored to local variableboolean
hasVariable(java.lang.String name)
private boolean
isCostly(DeclarationStatement decl)
protected boolean
isSafeForReuse(DeclarationStatement decl)
protected boolean
isSimpleExpression(Expression expr)
Checks if expression is simple enough to always inline at zero cost.java.lang.String
newName(java.lang.String suggestion)
Creates a name for a new variable, unique within this block.private java.lang.String
newName(java.lang.String suggestion, boolean optimize)
Creates a name for a new variable, unique within this block, controlling whether the variable can be inlined later.private Expression
normalizeDeclaration(DeclarationStatement decl)
Prepares declaration for inlining: adds castprivate boolean
optimize(Shuttle optimizer, boolean performInline)
Optimizes the list of statements.BlockStatement
toBlock()
Returns a block consisting of the current list of statements.
-
-
-
Field Detail
-
statements
final java.util.List<Statement> statements
-
variables
final java.util.Set<java.lang.String> variables
-
expressionForReuse
final java.util.Map<Expression,DeclarationStatement> expressionForReuse
Contains final-fine-to-reuse-declarations. An entry to this map is added when adding final declaration of a statement with optimize=true parameter.
-
optimizing
private final boolean optimizing
-
parent
private final BlockBuilder parent
-
OPTIMIZE_SHUTTLE
private static final Shuttle OPTIMIZE_SHUTTLE
-
-
Constructor Detail
-
BlockBuilder
public BlockBuilder()
Creates a non-optimizing BlockBuilder.
-
BlockBuilder
public BlockBuilder(boolean optimizing)
Creates a BlockBuilder.- Parameters:
optimizing
- Whether to eliminate common sub-expressions
-
BlockBuilder
public BlockBuilder(boolean optimizing, BlockBuilder parent)
Creates a BlockBuilder.- Parameters:
optimizing
- Whether to eliminate common sub-expressions
-
-
Method Detail
-
clear
public void clear()
Clears this BlockBuilder.
-
append
public Expression append(java.lang.String name, BlockStatement block)
Appends a block to a list of statements and returns an expression (possibly a variable) that represents the result of the newly added block.
-
append
public Expression append(java.lang.String name, BlockStatement block, boolean optimize)
Appends an expression to a list of statements, optionally optimizing it to a variable if it is used more than once.- Parameters:
name
- Suggested variable nameblock
- Expressionoptimize
- Whether to try to optimize by assigning the expression to a variable. Do not do this if the expression has side-effects or a time-dependent value.
-
append
public Expression append(java.lang.String name, Expression expression)
Appends an expression to a list of statements, and returns an expression (possibly a variable) that represents the result of the newly added block.
-
appendIfNotNull
public Expression appendIfNotNull(java.lang.String name, Expression expression)
Appends an expression to a list of statements, if it is not null.
-
append
public Expression append(java.lang.String name, Expression expression, boolean optimize)
Appends an expression to a list of statements, optionally optimizing if the expression is used more than once.
-
append_
private Expression append_(java.lang.String name, Expression expression, boolean optimize)
-
isSimpleExpression
protected boolean isSimpleExpression(Expression expr)
Checks if expression is simple enough to always inline at zero cost.- Parameters:
expr
- expression to test- Returns:
- true when given expression is safe to always inline
-
isSafeForReuse
protected boolean isSafeForReuse(DeclarationStatement decl)
-
addExpressionForReuse
protected void addExpressionForReuse(DeclarationStatement decl)
-
isCostly
private boolean isCostly(DeclarationStatement decl)
-
normalizeDeclaration
private Expression normalizeDeclaration(DeclarationStatement decl)
Prepares declaration for inlining: adds cast- Parameters:
decl
- inlining candidate- Returns:
- normalized expression
-
getComputedExpression
public DeclarationStatement getComputedExpression(Expression expr)
Returns the reference to ParameterExpression if given expression was already computed and stored to local variable- Parameters:
expr
- expression to test- Returns:
- existing ParameterExpression or null
-
add
public void add(Statement statement)
-
add
public void add(Expression expression)
-
toBlock
public BlockStatement toBlock()
Returns a block consisting of the current list of statements.
-
optimize
private boolean optimize(Shuttle optimizer, boolean performInline)
Optimizes the list of statements. If an expression is used only once, it is inlined.- Returns:
- whether any optimizations were made
-
createOptimizeShuttle
protected Shuttle createOptimizeShuttle()
Creates a shuttle that will be used during block optimization. Sub-classes might provide more specific optimizations (e.g. partial evaluation).- Returns:
- shuttle used to optimize the statements when converting to block
-
createFinishingOptimizeShuttle
protected Shuttle createFinishingOptimizeShuttle()
Creates a final optimization shuttle. Typically, the visitor will factor out constant expressions.- Returns:
- shuttle that is used to finalize the optimization
-
newName
private java.lang.String newName(java.lang.String suggestion, boolean optimize)
Creates a name for a new variable, unique within this block, controlling whether the variable can be inlined later.
-
newName
public java.lang.String newName(java.lang.String suggestion)
Creates a name for a new variable, unique within this block.
-
hasVariable
public boolean hasVariable(java.lang.String name)
-
append
public BlockBuilder append(Expression expression)
-
-