Class RexToLixTranslator
- java.lang.Object
-
- org.apache.calcite.adapter.enumerable.RexToLixTranslator
-
public class RexToLixTranslator extends java.lang.Object
TranslatesREX expressions
tolinq4j expressions
.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static class
RexToLixTranslator.AlwaysNull
Thrown in the unusual (but not erroneous) situation where the expression we are translating is the null literal but we have already checked that it is not null.static interface
RexToLixTranslator.InputGetter
Translates a field of an input to an expression.static class
RexToLixTranslator.InputGetterImpl
Implementation ofRexToLixTranslator.InputGetter
that callsPhysType.fieldReference(org.apache.calcite.linq4j.tree.Expression, int)
.
-
Field Summary
Fields Modifier and Type Field Description (package private) RexBuilder
builder
(package private) SqlConformance
conformance
private Function1<java.lang.String,RexToLixTranslator.InputGetter>
correlates
private java.util.Map<? extends RexNode,java.lang.Boolean>
exprNullableMap
private RexToLixTranslator.InputGetter
inputGetter
static java.util.Map<java.lang.reflect.Method,SqlOperator>
JAVA_TO_SQL_METHOD_MAP
private BlockBuilder
list
private RexToLixTranslator
parent
private RexProgram
program
private Expression
root
(package private) JavaTypeFactory
typeFactory
-
Constructor Summary
Constructors Modifier Constructor Description private
RexToLixTranslator(RexProgram program, JavaTypeFactory typeFactory, Expression root, RexToLixTranslator.InputGetter inputGetter, BlockBuilder list, java.util.Map<? extends RexNode,java.lang.Boolean> exprNullableMap, RexBuilder builder, SqlConformance conformance, RexToLixTranslator parent, Function1<java.lang.String,RexToLixTranslator.InputGetter> correlates)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static Expression
convert(Expression operand, java.lang.reflect.Type toType)
static Expression
convert(Expression operand, java.lang.reflect.Type fromType, java.lang.reflect.Type toType)
RexNode
deref(RexNode expr)
Dereferences an expression if it is aRexLocalRef
.private static java.lang.reflect.Method
findMethod(java.lang.Class<?> clazz, java.lang.String name, java.lang.Class... parameterTypes)
static RexToLixTranslator
forAggregation(JavaTypeFactory typeFactory, BlockBuilder list, RexToLixTranslator.InputGetter inputGetter, SqlConformance conformance)
Creates a translator for translating aggregate functions.Expression
getRoot()
Expression
handleNull(Expression input, RexImpTable.NullAs nullAs)
Adapts an expression with "normal" result to one that adheres to this particular policy.private Expression
handleNullUnboxingIfNecessary(Expression input, RexImpTable.NullAs nullAs, java.lang.reflect.Type storageType)
(package private) static boolean
isA(java.lang.reflect.Type fromType, Primitive primitive)
protected java.lang.Boolean
isKnownNullable(RexNode node)
Walks parent translator chain and verifies if the expression is nullable.boolean
isNullable(RexNode e)
Returns whether an expression is nullable.private Primitive
javaPrimitive(RelDataType type)
RelDataType
nullifyType(RelDataType type, boolean nullable)
private static Expression
scaleIntervalToNumber(RelDataType sourceType, RelDataType targetType, Expression operand)
RexToLixTranslator
setBlock(BlockBuilder block)
RexToLixTranslator
setCorrelates(Function1<java.lang.String,RexToLixTranslator.InputGetter> correlates)
RexToLixTranslator
setNullable(java.util.Map<? extends RexNode,java.lang.Boolean> nullable)
Creates a read-only copy of this translator that records that a given expression is nullable.RexToLixTranslator
setNullable(RexNode e, boolean nullable)
Creates a read-only copy of this translator that records that a given expression is nullable.(package private) Expression
translate(RexNode expr)
(package private) Expression
translate(RexNode expr, java.lang.reflect.Type storageType)
(package private) Expression
translate(RexNode expr, RexImpTable.NullAs nullAs)
(package private) Expression
translate(RexNode expr, RexImpTable.NullAs nullAs, java.lang.reflect.Type storageType)
private Expression
translate0(RexNode expr, RexImpTable.NullAs nullAs, java.lang.reflect.Type storageType)
Translates an expression that is not in the cache.private Expression
translateCall(RexCall call, RexImpTable.NullAs nullAs)
Translates a call to an operator or function.(package private) Expression
translateCast(RelDataType sourceType, RelDataType targetType, Expression operand)
static Expression
translateCondition(RexProgram program, JavaTypeFactory typeFactory, BlockBuilder list, RexToLixTranslator.InputGetter inputGetter, Function1<java.lang.String,RexToLixTranslator.InputGetter> correlates, SqlConformance conformance)
Expression
translateConstructor(java.util.List<RexNode> operandList, SqlKind kind)
java.util.List<Expression>
translateList(java.util.List<? extends RexNode> operandList)
Translates the list ofRexNode
, using the default output types.java.util.List<Expression>
translateList(java.util.List<? extends RexNode> operandList, java.util.List<? extends java.lang.reflect.Type> storageTypes)
Translates the list ofRexNode
, while optimizing for output storage.java.util.List<Expression>
translateList(java.util.List<RexNode> operandList, RexImpTable.NullAs nullAs)
java.util.List<Expression>
translateList(java.util.List<RexNode> operandList, RexImpTable.NullAs nullAs, java.util.List<? extends java.lang.reflect.Type> storageTypes)
static Expression
translateLiteral(RexLiteral literal, RelDataType type, JavaTypeFactory typeFactory, RexImpTable.NullAs nullAs)
Translates a literal.private Expression
translateParameter(RexDynamicParam expr, RexImpTable.NullAs nullAs, java.lang.reflect.Type storageType)
Translates a parameter.static java.util.List<Expression>
translateProjects(RexProgram program, JavaTypeFactory typeFactory, SqlConformance conformance, BlockBuilder list, PhysType outputPhysType, Expression root, RexToLixTranslator.InputGetter inputGetter, Function1<java.lang.String,RexToLixTranslator.InputGetter> correlates)
Translates aRexProgram
to a sequence of expressions and declarations.private RexToLixTranslator
withConformance(SqlConformance conformance)
-
-
-
Field Detail
-
JAVA_TO_SQL_METHOD_MAP
public static final java.util.Map<java.lang.reflect.Method,SqlOperator> JAVA_TO_SQL_METHOD_MAP
-
typeFactory
final JavaTypeFactory typeFactory
-
builder
final RexBuilder builder
-
program
private final RexProgram program
-
conformance
final SqlConformance conformance
-
root
private final Expression root
-
inputGetter
private final RexToLixTranslator.InputGetter inputGetter
-
list
private final BlockBuilder list
-
exprNullableMap
private final java.util.Map<? extends RexNode,java.lang.Boolean> exprNullableMap
-
parent
private final RexToLixTranslator parent
-
correlates
private final Function1<java.lang.String,RexToLixTranslator.InputGetter> correlates
-
-
Constructor Detail
-
RexToLixTranslator
private RexToLixTranslator(RexProgram program, JavaTypeFactory typeFactory, Expression root, RexToLixTranslator.InputGetter inputGetter, BlockBuilder list, java.util.Map<? extends RexNode,java.lang.Boolean> exprNullableMap, RexBuilder builder, SqlConformance conformance, RexToLixTranslator parent, Function1<java.lang.String,RexToLixTranslator.InputGetter> correlates)
-
-
Method Detail
-
findMethod
private static java.lang.reflect.Method findMethod(java.lang.Class<?> clazz, java.lang.String name, java.lang.Class... parameterTypes)
-
translateProjects
public static java.util.List<Expression> translateProjects(RexProgram program, JavaTypeFactory typeFactory, SqlConformance conformance, BlockBuilder list, PhysType outputPhysType, Expression root, RexToLixTranslator.InputGetter inputGetter, Function1<java.lang.String,RexToLixTranslator.InputGetter> correlates)
Translates aRexProgram
to a sequence of expressions and declarations.- Parameters:
program
- Program to be translatedtypeFactory
- Type factoryconformance
- SQL conformancelist
- List of statements, populated with declarationsoutputPhysType
- Output type, or nullroot
- Root expressioninputGetter
- Generates expressions for inputscorrelates
- Provider of references to the values of correlated variables- Returns:
- Sequence of expressions, optional condition
-
forAggregation
public static RexToLixTranslator forAggregation(JavaTypeFactory typeFactory, BlockBuilder list, RexToLixTranslator.InputGetter inputGetter, SqlConformance conformance)
Creates a translator for translating aggregate functions.
-
translate
Expression translate(RexNode expr)
-
translate
Expression translate(RexNode expr, RexImpTable.NullAs nullAs)
-
translate
Expression translate(RexNode expr, java.lang.reflect.Type storageType)
-
translate
Expression translate(RexNode expr, RexImpTable.NullAs nullAs, java.lang.reflect.Type storageType)
-
translateCast
Expression translateCast(RelDataType sourceType, RelDataType targetType, Expression operand)
-
handleNullUnboxingIfNecessary
private Expression handleNullUnboxingIfNecessary(Expression input, RexImpTable.NullAs nullAs, java.lang.reflect.Type storageType)
-
handleNull
public Expression handleNull(Expression input, RexImpTable.NullAs nullAs)
Adapts an expression with "normal" result to one that adheres to this particular policy. Wraps the result expression into a new parameter if need be.- Parameters:
input
- ExpressionnullAs
- If false, if expression is definitely not null at runtime. Therefore we can optimize. For example, we can cast to int using x.intValue().- Returns:
- Translated expression
-
translate0
private Expression translate0(RexNode expr, RexImpTable.NullAs nullAs, java.lang.reflect.Type storageType)
Translates an expression that is not in the cache.- Parameters:
expr
- ExpressionnullAs
- If false, if expression is definitely not null at runtime. Therefore we can optimize. For example, we can cast to int using x.intValue().- Returns:
- Translated expression
-
deref
public RexNode deref(RexNode expr)
Dereferences an expression if it is aRexLocalRef
.
-
translateCall
private Expression translateCall(RexCall call, RexImpTable.NullAs nullAs)
Translates a call to an operator or function.
-
translateParameter
private Expression translateParameter(RexDynamicParam expr, RexImpTable.NullAs nullAs, java.lang.reflect.Type storageType)
Translates a parameter.
-
translateLiteral
public static Expression translateLiteral(RexLiteral literal, RelDataType type, JavaTypeFactory typeFactory, RexImpTable.NullAs nullAs)
Translates a literal.- Throws:
RexToLixTranslator.AlwaysNull
- if literal is null butnullAs
isRexImpTable.NullAs.NOT_POSSIBLE
.
-
translateList
public java.util.List<Expression> translateList(java.util.List<RexNode> operandList, RexImpTable.NullAs nullAs)
-
translateList
public java.util.List<Expression> translateList(java.util.List<RexNode> operandList, RexImpTable.NullAs nullAs, java.util.List<? extends java.lang.reflect.Type> storageTypes)
-
translateList
public java.util.List<Expression> translateList(java.util.List<? extends RexNode> operandList)
Translates the list ofRexNode
, using the default output types. This might be suboptimal in terms of additional box-unbox when you use the translation later. If you know the java class that will be used to store the results, usetranslateList(java.util.List, java.util.List)
version.- Parameters:
operandList
- list of RexNodes to translate- Returns:
- translated expressions
-
translateList
public java.util.List<Expression> translateList(java.util.List<? extends RexNode> operandList, java.util.List<? extends java.lang.reflect.Type> storageTypes)
Translates the list ofRexNode
, while optimizing for output storage. For instance, if the result of translation is going to be stored inObject[]
, and the input isObject[]
as well, then translator will avoid casting, boxing, etc.- Parameters:
operandList
- list of RexNodes to translatestorageTypes
- hints of the java classes that will be used to store translation results. Use null to use default storage type- Returns:
- translated expressions
-
translateCondition
public static Expression translateCondition(RexProgram program, JavaTypeFactory typeFactory, BlockBuilder list, RexToLixTranslator.InputGetter inputGetter, Function1<java.lang.String,RexToLixTranslator.InputGetter> correlates, SqlConformance conformance)
-
convert
public static Expression convert(Expression operand, java.lang.reflect.Type toType)
-
convert
public static Expression convert(Expression operand, java.lang.reflect.Type fromType, java.lang.reflect.Type toType)
-
isA
static boolean isA(java.lang.reflect.Type fromType, Primitive primitive)
-
translateConstructor
public Expression translateConstructor(java.util.List<RexNode> operandList, SqlKind kind)
-
isNullable
public boolean isNullable(RexNode e)
Returns whether an expression is nullable. Even if its type says it is nullable, if we have previously generated a check to make sure that it is not null, we will say so.For example,
WHERE a == b
translates toa != null && b != null && a.equals(b)
. When translating the 3rd part of the disjunction, we already know a and b are not null.- Parameters:
e
- Expression- Returns:
- Whether expression is nullable in the current translation context
-
isKnownNullable
protected java.lang.Boolean isKnownNullable(RexNode node)
Walks parent translator chain and verifies if the expression is nullable.- Parameters:
node
- RexNode to check if it is nullable or not- Returns:
- null when nullability is not known, true or false otherwise
-
setNullable
public RexToLixTranslator setNullable(RexNode e, boolean nullable)
Creates a read-only copy of this translator that records that a given expression is nullable.
-
setNullable
public RexToLixTranslator setNullable(java.util.Map<? extends RexNode,java.lang.Boolean> nullable)
Creates a read-only copy of this translator that records that a given expression is nullable.
-
setBlock
public RexToLixTranslator setBlock(BlockBuilder block)
-
setCorrelates
public RexToLixTranslator setCorrelates(Function1<java.lang.String,RexToLixTranslator.InputGetter> correlates)
-
withConformance
private RexToLixTranslator withConformance(SqlConformance conformance)
-
nullifyType
public RelDataType nullifyType(RelDataType type, boolean nullable)
-
javaPrimitive
private Primitive javaPrimitive(RelDataType type)
-
getRoot
public Expression getRoot()
-
scaleIntervalToNumber
private static Expression scaleIntervalToNumber(RelDataType sourceType, RelDataType targetType, Expression operand)
-
-