org.eigenbase.rex
Class RexBuilder

java.lang.Object
  extended by org.eigenbase.rex.RexBuilder
Direct Known Subclasses:
JavaRexBuilder

public class RexBuilder
extends Object

Factory for row expressions.

Some common literal values (NULL, TRUE, FALSE, 0, 1, '') are cached.

Since:
Nov 23, 2003
Version:
$Id: //open/dev/farrago/src/org/eigenbase/rex/RexBuilder.java#43 $
Author:
jhyde

Field Summary
private  RexLiteral booleanFalse
           
private  RexLiteral booleanTrue
           
private  RexLiteral charEmpty
           
private  RexLiteral constantNull
           
private  SqlStdOperatorTable opTab
           
protected  RelDataTypeFactory typeFactory
           
 
Constructor Summary
RexBuilder(RelDataTypeFactory typeFactory)
          Creates a RexBuilder.
 
Method Summary
 RexNode addAggCall(AggregateCall aggCall, int groupCount, List<AggregateCall> aggCalls, Map<AggregateCall,RexNode> aggCallMapping)
          Creates a reference to an aggregate call, checking for repeated calls.
 RexLiteral constantNull()
          Creates a constant for the SQL NULL value.
 RexNode copy(RexNode expr)
          Creates a copy of an expression, which may have been created using a different RexBuilder and/or RelDataTypeFactory, using this RexBuilder.
 RelDataType deriveReturnType(SqlOperator op, RelDataTypeFactory typeFactory, RexNode[] exprs)
          Derives the return type of a call to an operator.
 SqlStdOperatorTable getOpTab()
          Returns this RexBuilder's operator table
 RelDataTypeFactory getTypeFactory()
          Returns this RexBuilder's type factory
 RexNode makeAbstractCast(RelDataType type, RexNode exp)
          Creates a call to the CAST operator.
 RexLiteral makeApproxLiteral(BigDecimal bd)
          Creates a double-precision literal.
 RexLiteral makeApproxLiteral(BigDecimal bd, RelDataType type)
          Creates an approximate numeric literal (double or float).
 RexLiteral makeBigintLiteral(BigDecimal bd)
          Creates a BIGINT literal.
 RexLiteral makeBinaryLiteral(byte[] byteArray)
          Creates a byte array literal.
 RexNode makeCall(RelDataType returnType, SqlOperator op, RexNode... exprs)
          Creates a call with an array of arguments and a predetermined type.
 RexNode makeCall(SqlOperator op, List<? extends RexNode> exprList)
          Creates a call with a list of arguments.
 RexNode makeCall(SqlOperator op, RexNode... exprs)
          Creates a call with an array of arguments.
 RexNode makeCast(RelDataType type, RexNode exp)
          Creates a call to the CAST operator, expanding if possible.
 RexLiteral makeCharLiteral(NlsString str)
          Creates a character string literal from an NlsString.
 RexNode makeCorrel(RelDataType type, String name)
          Creates an expression referencing a correlation variable.
 RexLiteral makeDateLiteral(Calendar date)
          Creates a Date literal.
 RexDynamicParam makeDynamicParam(RelDataType type, int index)
          Creates a reference to a dynamic parameter
 RexLiteral makeExactLiteral(BigDecimal bd)
          Creates a numeric literal.
 RexLiteral makeExactLiteral(BigDecimal bd, RelDataType type)
          Creates a numeric literal.
 RexNode makeFieldAccess(RexNode expr, int i)
          Creates an expression accessing a field with a given ordinal from a record.
 RexNode makeFieldAccess(RexNode expr, String fieldName)
          Creates an expression accessing a given named field from a record.
private  RexNode makeFieldAccessInternal(RexNode expr, RelDataTypeField field)
          Creates an expression accessing a given field from a record.
 RexLiteral makeFlag(Object flag)
          Creates a literal representing a flag.
 RexNode makeInputRef(RelDataType type, int i)
          Creates a referenence to a given field of the input record.
 RexLiteral makeIntervalLiteral(long l, SqlIntervalQualifier intervalQualifier)
          Creates an interval literal.
 RexLiteral makeIntervalLiteral(SqlIntervalQualifier intervalQualifier)
          Creates an interval literal.
 RexLiteral makeLiteral(boolean b)
          Creates a boolean literal.
protected  RexLiteral makeLiteral(Comparable o, RelDataType type, SqlTypeName typeName)
          Internal method to create a call to a literal.
 RexLiteral makeLiteral(String s)
          Creates a character string literal.
 RexNode makeNewInvocation(RelDataType type, RexNode[] exprs)
          Creates an invocation of the NEW operator.
 RexNode makeNotNullCast(RexNode expr)
          Makes an expression which converts a value of type T to a value of type T NOT NULL, or throws if the value is NULL.
 RexNode makeNullLiteral(SqlTypeName typeName)
          Creates a literal whose value is NULL, with a particular type.
 RexNode makeNullLiteral(SqlTypeName typeName, int precision)
          Creates an expression corresponding to a null literal, cast to a specific type and precision
 RexNode makeOver(RelDataType type, SqlAggFunction operator, RexNode[] exprs, RexNode[] partitionKeys, RexNode[] orderKeys, SqlNode lowerBound, SqlNode upperBound, boolean physical, boolean allowPartial, boolean nullWhenCountZero)
          Creates a call to a windowed agg.
protected  RexLiteral makePreciseStringLiteral(String s)
          Creates a character string literal with type CHAR and default charset and collation.
 RexNode makeRangeReference(RelDataType rowType)
          Creates a reference to all the fields in the row.
 RexNode makeRangeReference(RelDataType type, int offset, boolean nullable)
          Creates a reference to all the fields in the row.
 RexNode makeReinterpretCast(RelDataType type, RexNode exp, RexNode checkOverflow)
          Makes a reinterpret cast.
 RexLiteral makeTimeLiteral(Calendar time, int precision)
          Creates a Time literal.
 RexLiteral makeTimestampLiteral(Calendar timestamp, int precision)
          Creates a Timestamp literal.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

typeFactory

protected final RelDataTypeFactory typeFactory

booleanTrue

private final RexLiteral booleanTrue

booleanFalse

private final RexLiteral booleanFalse

charEmpty

private final RexLiteral charEmpty

constantNull

private final RexLiteral constantNull

opTab

private final SqlStdOperatorTable opTab
Constructor Detail

RexBuilder

public RexBuilder(RelDataTypeFactory typeFactory)
Creates a RexBuilder.

Parameters:
typeFactory - Type factory
Method Detail

getTypeFactory

public RelDataTypeFactory getTypeFactory()
Returns this RexBuilder's type factory

Returns:
type factory

getOpTab

public SqlStdOperatorTable getOpTab()
Returns this RexBuilder's operator table

Returns:
operator table

makeFieldAccess

public RexNode makeFieldAccess(RexNode expr,
                               String fieldName)
Creates an expression accessing a given named field from a record.

Parameters:
expr - Expression yielding a record
fieldName - Name of field in record
Returns:
Expression accessing a given named field

makeFieldAccess

public RexNode makeFieldAccess(RexNode expr,
                               int i)
Creates an expression accessing a field with a given ordinal from a record.

Parameters:
expr - Expression yielding a record
i - Ordinal of field
Returns:
Expression accessing given field

makeFieldAccessInternal

private RexNode makeFieldAccessInternal(RexNode expr,
                                        RelDataTypeField field)
Creates an expression accessing a given field from a record.

Parameters:
expr - Expression yielding a record
field - Field
Returns:
Expression accessing given field

makeCall

public RexNode makeCall(RelDataType returnType,
                        SqlOperator op,
                        RexNode... exprs)
Creates a call with an array of arguments and a predetermined type.


makeCall

public RexNode makeCall(SqlOperator op,
                        RexNode... exprs)
Creates a call with an array of arguments.

This is the fundamental method called by all of the other makeCall methods. If you derive a class from RexBuilder, this is the only method you need to override.


makeCall

public final RexNode makeCall(SqlOperator op,
                              List<? extends RexNode> exprList)
Creates a call with a list of arguments.

Equivalent to makeCall(op, exprList.toArray(new RexNode[exprList.size()])).


deriveReturnType

public RelDataType deriveReturnType(SqlOperator op,
                                    RelDataTypeFactory typeFactory,
                                    RexNode[] exprs)
Derives the return type of a call to an operator.

Parameters:
op - the operator being called
typeFactory - factory for return type
exprs - actual operands
Returns:
derived type

addAggCall

public RexNode addAggCall(AggregateCall aggCall,
                          int groupCount,
                          List<AggregateCall> aggCalls,
                          Map<AggregateCall,RexNode> aggCallMapping)
Creates a reference to an aggregate call, checking for repeated calls.


makeOver

public RexNode makeOver(RelDataType type,
                        SqlAggFunction operator,
                        RexNode[] exprs,
                        RexNode[] partitionKeys,
                        RexNode[] orderKeys,
                        SqlNode lowerBound,
                        SqlNode upperBound,
                        boolean physical,
                        boolean allowPartial,
                        boolean nullWhenCountZero)
Creates a call to a windowed agg.


constantNull

public RexLiteral constantNull()
Creates a constant for the SQL NULL value.


makeCorrel

public RexNode makeCorrel(RelDataType type,
                          String name)
Creates an expression referencing a correlation variable.

Parameters:
type - Type of variable
name - Name of variable
Returns:
Correlation variable

makeNewInvocation

public RexNode makeNewInvocation(RelDataType type,
                                 RexNode[] exprs)
Creates an invocation of the NEW operator.

Parameters:
type - Type to be instantiated
exprs - Arguments to NEW operator
Returns:
Expression invoking NEW operator

makeCast

public RexNode makeCast(RelDataType type,
                        RexNode exp)
Creates a call to the CAST operator, expanding if possible.

Parameters:
type - Type to cast to
exp - Expression being cast
Returns:
Call to CAST operator

makeAbstractCast

public RexNode makeAbstractCast(RelDataType type,
                                RexNode exp)
Creates a call to the CAST operator.

Parameters:
type - Type to cast to
exp - Expression being cast
Returns:
Call to CAST operator

makeReinterpretCast

public RexNode makeReinterpretCast(RelDataType type,
                                   RexNode exp,
                                   RexNode checkOverflow)
Makes a reinterpret cast.

Parameters:
type - type returned by the cast
exp - expression to be casted
checkOverflow - whether an overflow check is required
Returns:
a RexCall with two operands and a special return type

makeNotNullCast

public RexNode makeNotNullCast(RexNode expr)
Makes an expression which converts a value of type T to a value of type T NOT NULL, or throws if the value is NULL. If the expression is already NOT NULL, does nothing.


makeRangeReference

public RexNode makeRangeReference(RelDataType rowType)
Creates a reference to all the fields in the row. That is, the whole row as a single record object.

Parameters:
rowType - Type of the input row.

makeRangeReference

public RexNode makeRangeReference(RelDataType type,
                                  int offset,
                                  boolean nullable)
Creates a reference to all the fields in the row.

For example, if the input row has type T{f0,f1,f2,f3,f4} then makeRangeReference(T{f0,f1,f2,f3,f4}, S{f3,f4}, 3) is an expression which yields the last 2 fields.

Parameters:
type - Type of the resulting range record.
offset - Index of first field.
nullable - Whether the record is nullable.

makeInputRef

public RexNode makeInputRef(RelDataType type,
                            int i)
Creates a referenence to a given field of the input record.

Parameters:
type - Type of field
i - Ordinal of field
Returns:
Reference to field

makeFlag

public RexLiteral makeFlag(Object flag)
Creates a literal representing a flag.

Parameters:
flag - Flag value; must be either a Enum14.Value or a Enum, and hence a Comparable.

makeLiteral

protected RexLiteral makeLiteral(Comparable o,
                                 RelDataType type,
                                 SqlTypeName typeName)
Internal method to create a call to a literal. Code outside this package should call one of the type-specific methods such as makeDateLiteral(Calendar), makeLiteral(boolean), makeLiteral(String).

Parameters:
o - Value of literal, must be appropriate for the type
type - Type of literal
typeName - SQL type of literal
Returns:
Literal

makeLiteral

public RexLiteral makeLiteral(boolean b)
Creates a boolean literal.


makeExactLiteral

public RexLiteral makeExactLiteral(BigDecimal bd)
Creates a numeric literal.


makeBigintLiteral

public RexLiteral makeBigintLiteral(BigDecimal bd)
Creates a BIGINT literal.


makeExactLiteral

public RexLiteral makeExactLiteral(BigDecimal bd,
                                   RelDataType type)
Creates a numeric literal.


makeBinaryLiteral

public RexLiteral makeBinaryLiteral(byte[] byteArray)
Creates a byte array literal.


makeApproxLiteral

public RexLiteral makeApproxLiteral(BigDecimal bd)
Creates a double-precision literal.


makeApproxLiteral

public RexLiteral makeApproxLiteral(BigDecimal bd,
                                    RelDataType type)
Creates an approximate numeric literal (double or float).

Parameters:
bd - literal value
type - approximate numeric type
Returns:
new literal

makeLiteral

public RexLiteral makeLiteral(String s)
Creates a character string literal.

"Precondition:"
s != null

makePreciseStringLiteral

protected RexLiteral makePreciseStringLiteral(String s)
Creates a character string literal with type CHAR and default charset and collation.

Parameters:
s - String value
Returns:
Character string literal

makeCharLiteral

public RexLiteral makeCharLiteral(NlsString str)
Creates a character string literal from an NlsString.

If the string's charset and collation are not set, uses the system defaults.

"Precondition:"
str != null

makeDateLiteral

public RexLiteral makeDateLiteral(Calendar date)
Creates a Date literal.

"Precondition:"
date != null

makeTimeLiteral

public RexLiteral makeTimeLiteral(Calendar time,
                                  int precision)
Creates a Time literal.

"Precondition:"
time != null

makeTimestampLiteral

public RexLiteral makeTimestampLiteral(Calendar timestamp,
                                       int precision)
Creates a Timestamp literal.

"Precondition:"
timestamp != null

makeIntervalLiteral

public RexLiteral makeIntervalLiteral(SqlIntervalQualifier intervalQualifier)
Creates an interval literal.


makeIntervalLiteral

public RexLiteral makeIntervalLiteral(long l,
                                      SqlIntervalQualifier intervalQualifier)
Creates an interval literal.


makeDynamicParam

public RexDynamicParam makeDynamicParam(RelDataType type,
                                        int index)
Creates a reference to a dynamic parameter

Parameters:
type - Type of dynamic parameter
index - Index of dynamic parameter
Returns:
Expression referencing dynamic parameter

makeNullLiteral

public RexNode makeNullLiteral(SqlTypeName typeName,
                               int precision)
Creates an expression corresponding to a null literal, cast to a specific type and precision

Parameters:
typeName - name of the type that the null will be cast to
precision - precision of the type
Returns:
created expression

makeNullLiteral

public RexNode makeNullLiteral(SqlTypeName typeName)
Creates a literal whose value is NULL, with a particular type.

The typing is necessary because RexNodes are strictly typed. For example, in the Rex world the NULL parameter to SUBSTRING(NULL FROM 2 FOR 4) must have a valid VARCHAR type so that the result type can be determined.

Parameters:
typeName - Type to cast NULL to
Returns:
NULL literal of given type

copy

public RexNode copy(RexNode expr)
Creates a copy of an expression, which may have been created using a different RexBuilder and/or RelDataTypeFactory, using this RexBuilder.

Parameters:
expr - Expression
Returns:
Copy of expression
See Also:
RelDataTypeFactory.copyType(RelDataType)