net.sf.farrago.fennel.calc
Class CalcRexImplementorTableImpl

java.lang.Object
  extended by net.sf.farrago.fennel.calc.CalcRexImplementorTableImpl
All Implemented Interfaces:
CalcRexImplementorTable

public class CalcRexImplementorTableImpl
extends Object
implements CalcRexImplementorTable

Implementation of CalcRexImplementorTable, containing implementations for all standard functions.

Since:
June 2nd, 2004
Version:
$Id: //open/dev/farrago/src/net/sf/farrago/fennel/calc/CalcRexImplementorTableImpl.java#3 $
Author:
jhyde

Nested Class Summary
static class CalcRexImplementorTableImpl.AbstractCalcRexAggImplementor
          Abstract base class for classes which implement CalcRexAggImplementor.
static class CalcRexImplementorTableImpl.AbstractCalcRexImplementor
          Abstract base class for classes which implement CalcRexImplementor.
private static class CalcRexImplementorTableImpl.AddCharSetNameInstrImplementor
          A Class that gets a specified operand of a call and retrieves its charset name and add it as a vc literal to the program.
private static class CalcRexImplementorTableImpl.BinaryNumericMakeSametypeImplementor
          Makes all numeric types the same before calling a given instruction.
private static class CalcRexImplementorTableImpl.BinaryStringMakeSametypeImplementor
          Makes all string types the same before calling a given instruction.
private static class CalcRexImplementorTableImpl.CaseImplementor
          Implementor for CASE operator.
private static class CalcRexImplementorTableImpl.CastDecimalImplementor
          Implementor for casting between char and decimal types.
private static class CalcRexImplementorTableImpl.CastImplementor
          Implementor for CAST operator.
private static class CalcRexImplementorTableImpl.CastTimestampToDateImplementor
          Implementor for casting from TIMESTAMP to DATE.
private static class CalcRexImplementorTableImpl.CastTimestampToTimeImplementor
          Implementor for casting from TIMESTAMP to TIME.
private static class CalcRexImplementorTableImpl.CastTimeToTimestampImplementor
          Implementor for casting from TIME to TIMESTAMP.
private static class CalcRexImplementorTableImpl.ConcatImplementor
           
private static class CalcRexImplementorTableImpl.CountCalcRexImplementor
          Implementation of the COUNT aggregate function, SqlStdOperatorTable.countOperator.
private static class CalcRexImplementorTableImpl.DatetimeRoundingImplementor
          Helper to implement CAST to TIMESTAMP or TIME values.
(package private) static class CalcRexImplementorTableImpl.DoubleKeyMap
           
private static class CalcRexImplementorTableImpl.FirstValueCalcRexImplementor
          Implementation of the FIST_VALUE aggregate function, SqlStdOperatorTable.sumOperator.
private static class CalcRexImplementorTableImpl.HistogramAggRexImplementor
          Implementation of the $HISTOGRAM aggregate function (SqlStdOperatorTable.histogramAggFunction, which helps implement MIN, MAX, FIRST_VALUE, LAST_VALUE in a windowed aggregation scenario.
private static class CalcRexImplementorTableImpl.HistogramResultRexImplementor
          Implementation of the operators which extract a result from a histogram: $HISTOGRAM_MIN, $HISTOGRAM_MAX, $HISTOGRAM_FIRST_VALUE, $HISTOGRAM_LAST_VALUE
private static class CalcRexImplementorTableImpl.IdentityImplementor
          Implements the identity operator.
static class CalcRexImplementorTableImpl.InstrDefImplementor
          Generic implementor that takes a CalcProgramBuilder.InstructionDef which implements an operator by generating a call to a given instruction.
private static class CalcRexImplementorTableImpl.IsBoolImplementor
          Implements "IS TRUE" and "IS FALSE" operators.
private static class CalcRexImplementorTableImpl.IsNotBoolImplementor
          Implements "IS NOT TRUE" and "IS NOT FALSE" operators.
private static class CalcRexImplementorTableImpl.LastValueCalcRexImplementor
          Implementation of the LAST_VALUE aggregate function, SqlStdOperatorTable.sumOperator.
private static class CalcRexImplementorTableImpl.MakeOperandsDoubleImplementor
          Implementor that will convert a RexCall's operands to approx DOUBLE if needed
private static class CalcRexImplementorTableImpl.MinMaxCalcRexImplementor
          Implementation of the MIN and MAX aggregate function, SqlStdOperatorTable.sumOperator.
private static class CalcRexImplementorTableImpl.ReinterpretCastImplementor
          Implementor for REINTERPRET operator.
private static class CalcRexImplementorTableImpl.SliceImplementor
          Implements the internal $SLICE operator.
private static class CalcRexImplementorTableImpl.SumCalcRexImplementor
          Implementation of the SUM aggregate function, SqlStdOperatorTable.sumOperator.
private static class CalcRexImplementorTableImpl.SumEmptyIsZeroCalcRexImplementor
          Implementation of the SUM0 aggregate function, SqlStdOperatorTable.sumEmptyIsZeroOperator.
private static class CalcRexImplementorTableImpl.TimeFunctionImplementor
           
private static class CalcRexImplementorTableImpl.TrimImplementor
          Implements the TRIM function.
private static class CalcRexImplementorTableImpl.UsingInstrImplementor
          Implements a call by invoking a given instruction.
 
Field Summary
private  Map<SqlAggFunction,CalcRexAggImplementor> aggImplementationMap
          Maps SqlAggFunction to CalcRexAggImplementor.
private  Map<SqlOperator,CalcRexImplementor> operatorImplementationMap
          Maps SqlOperator to CalcRexImplementorTableImpl.AbstractCalcRexImplementor.
protected static SqlStdOperatorTable opTab
           
private  CalcRexImplementorTable parent
          Parent implementor table, may be null.
private static CalcRexImplementorTableImpl std
           
 
Constructor Summary
CalcRexImplementorTableImpl(CalcRexImplementorTable parent)
          Creates an empty table which delegates to another table.
 
Method Summary
protected static CalcReg createResultRegister(RexToCalcTranslator translator, RexCall call)
          Creates a register to hold the result of a call.
 CalcRexImplementor get(SqlOperator op)
          Retrieves the implementor of an operator, or null if there is no implementor registered.
 CalcRexAggImplementor getAgg(SqlAggFunction op)
          Retrieves the implementor of an aggregate function.
private static RexCall implementFirstOperandWithDouble(RexCall call, RexToCalcTranslator translator, RexNode typeNode, int i, boolean castBack)
          Same as implementFirstOperandWithInt8(org.eigenbase.rex.RexCall, net.sf.farrago.fennel.calc.RexToCalcTranslator, org.eigenbase.rex.RexNode, int, boolean) but with CalcProgramBuilder.OpType.Double instead TODO need to abstract and merge functionality with implementFirstOperandWithInt8(org.eigenbase.rex.RexCall, net.sf.farrago.fennel.calc.RexToCalcTranslator, org.eigenbase.rex.RexNode, int, boolean) since they both contain nearly the same code
private static RexCall implementFirstOperandWithDoubleOrInt8(RexCall call, RexToCalcTranslator translator, RexNode typeNode, int i, boolean castBack)
          Converts a binary call (two regs as operands) by converting the first operand to type CalcProgramBuilder.OpType.Int8 for exact types and CalcProgramBuilder.OpType.Double for approximate types and then back again.
private static RexCall implementFirstOperandWithInt8(RexCall call, RexToCalcTranslator translator, RexNode typeNode, int i, boolean castBack)
          Converts a binary call (two regs as operands) by converting the first operand to type CalcProgramBuilder.OpType.Int8 if needed and then back again.
protected static List<CalcReg> implementOperands(RexCall call, int start, int stop, RexToCalcTranslator translator)
          Implements all operands to a call between start (inclusive) and stop (exclusive), and returns a list of the registers which hold the results.
protected static List<CalcReg> implementOperands(RexCall call, RexToCalcTranslator translator)
          Implements all operands to a call, and returns a list of the registers which hold the results.
private static CalcReg implementRounding(CalcReg inReg, RoundingMode roundingMode, RelDataType toType, int fromPrecision, RexToCalcTranslator translator)
          Generates code to round 'operand' to the precision in 'toType', if necessary.
private static CalcReg implementRounding(long alpha, RoundingMode roundingMode, CalcReg inReg, RelDataType toType, RexToCalcTranslator translator)
          Generates code to round a value to a multiple of a given multipler.
private static CalcReg implementUsingInstr(CalcProgramBuilder.InstructionDef instr, RexToCalcTranslator translator, RexCall call)
          Implements a call by invoking a given instruction.
private  CalcRexImplementorTableImpl initStandard()
          Registers the standard set of functions.
 void register(SqlOperator op, CalcRexImplementor impl)
          Registers an operator and its implementor.
 void registerAgg(SqlAggFunction agg, CalcRexAggImplementor impl)
          Registers an aggregate function and its implementor.
protected  void registerInstr(SqlOperator op, CalcProgramBuilder.InstructionDef instrDef)
          Registers an operator which is implemented in a trivial way by a single calculator instruction.
static CalcRexImplementorTable std()
          Returns the table of implementations of all of the standard SQL functions and operators.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

opTab

protected static final SqlStdOperatorTable opTab

std

private static final CalcRexImplementorTableImpl std

parent

private final CalcRexImplementorTable parent
Parent implementor table, may be null.


operatorImplementationMap

private final Map<SqlOperator,CalcRexImplementor> operatorImplementationMap
Maps SqlOperator to CalcRexImplementorTableImpl.AbstractCalcRexImplementor.


aggImplementationMap

private final Map<SqlAggFunction,CalcRexAggImplementor> aggImplementationMap
Maps SqlAggFunction to CalcRexAggImplementor.

Constructor Detail

CalcRexImplementorTableImpl

public CalcRexImplementorTableImpl(CalcRexImplementorTable parent)
Creates an empty table which delegates to another table.

See Also:
std
Method Detail

std

public static CalcRexImplementorTable std()
Returns the table of implementations of all of the standard SQL functions and operators.


register

public void register(SqlOperator op,
                     CalcRexImplementor impl)
Registers an operator and its implementor.

It is an error if the operator already has an implementor. But if the operator has an implementor in a parent table, it is simply overridden.

"Precondition:"
op != null, impl != null, !operatorImplementationMap.containsKey(op)

registerInstr

protected void registerInstr(SqlOperator op,
                             CalcProgramBuilder.InstructionDef instrDef)
Registers an operator which is implemented in a trivial way by a single calculator instruction.

"Precondition:"
op != null, instrDef != null

registerAgg

public void registerAgg(SqlAggFunction agg,
                        CalcRexAggImplementor impl)
Registers an aggregate function and its implementor.

It is an error if the aggregate function already has an implementor. But if the operator has an implementor in a parent table, it is simply overridden.

"Precondition:"
op != null, impl != null, !operatorImplementationMap.containsKey(op)

get

public CalcRexImplementor get(SqlOperator op)
Description copied from interface: CalcRexImplementorTable
Retrieves the implementor of an operator, or null if there is no implementor registered.

Specified by:
get in interface CalcRexImplementorTable

getAgg

public CalcRexAggImplementor getAgg(SqlAggFunction op)
Description copied from interface: CalcRexImplementorTable
Retrieves the implementor of an aggregate function.

Specified by:
getAgg in interface CalcRexImplementorTable

createResultRegister

protected static CalcReg createResultRegister(RexToCalcTranslator translator,
                                              RexCall call)
Creates a register to hold the result of a call.

Parameters:
translator - Translator
call - Call
Returns:
A register

implementOperands

protected static List<CalcReg> implementOperands(RexCall call,
                                                 RexToCalcTranslator translator)
Implements all operands to a call, and returns a list of the registers which hold the results.

"Postcondition:"
return != null

implementOperands

protected static List<CalcReg> implementOperands(RexCall call,
                                                 int start,
                                                 int stop,
                                                 RexToCalcTranslator translator)
Implements all operands to a call between start (inclusive) and stop (exclusive), and returns a list of the registers which hold the results.

"Postcondition:"
return != null

implementUsingInstr

private static CalcReg implementUsingInstr(CalcProgramBuilder.InstructionDef instr,
                                           RexToCalcTranslator translator,
                                           RexCall call)
Implements a call by invoking a given instruction.

Parameters:
instr - Instruction
translator - Translator
call - Call to translate
Returns:
Register which contains result, never null

implementFirstOperandWithDoubleOrInt8

private static RexCall implementFirstOperandWithDoubleOrInt8(RexCall call,
                                                             RexToCalcTranslator translator,
                                                             RexNode typeNode,
                                                             int i,
                                                             boolean castBack)
Converts a binary call (two regs as operands) by converting the first operand to type CalcProgramBuilder.OpType.Int8 for exact types and CalcProgramBuilder.OpType.Double for approximate types and then back again. Logically it will do something like
t0 = type of first operand
CAST(CALL(CAST(op0 as INT8), op1) as t0)
If t0 is not a numeric or is already is INT8 or DOUBLE, the CALL is simply returned as is.


implementFirstOperandWithInt8

private static RexCall implementFirstOperandWithInt8(RexCall call,
                                                     RexToCalcTranslator translator,
                                                     RexNode typeNode,
                                                     int i,
                                                     boolean castBack)
Converts a binary call (two regs as operands) by converting the first operand to type CalcProgramBuilder.OpType.Int8 if needed and then back again. Logically it will do something like
t0 = type of first operand
CAST(CALL(CAST(op0 as INT8), op1) as t0)
If t0 is not an exact type or is already is INT8, the CALL is simply returned as is.


implementFirstOperandWithDouble

private static RexCall implementFirstOperandWithDouble(RexCall call,
                                                       RexToCalcTranslator translator,
                                                       RexNode typeNode,
                                                       int i,
                                                       boolean castBack)
Same as implementFirstOperandWithInt8(org.eigenbase.rex.RexCall, net.sf.farrago.fennel.calc.RexToCalcTranslator, org.eigenbase.rex.RexNode, int, boolean) but with CalcProgramBuilder.OpType.Double instead TODO need to abstract and merge functionality with implementFirstOperandWithInt8(org.eigenbase.rex.RexCall, net.sf.farrago.fennel.calc.RexToCalcTranslator, org.eigenbase.rex.RexNode, int, boolean) since they both contain nearly the same code


initStandard

private CalcRexImplementorTableImpl initStandard()
Registers the standard set of functions.


implementRounding

private static CalcReg implementRounding(CalcReg inReg,
                                         RoundingMode roundingMode,
                                         RelDataType toType,
                                         int fromPrecision,
                                         RexToCalcTranslator translator)
Generates code to round 'operand' to the precision in 'toType', if necessary.

Rounding is to a multiple of 10 ^ (3 - precision), according to the RoundingMode strategy of the roundingMode parameter.

If the result has the same or more precision, generates no code.

Parameters:
inReg - Register holding input value
roundingMode - Rounding mode; one of RoundingMode.FLOOR, RoundingMode.HALF_UP, RoundingMode.DOWN.
toType - Desired target type
fromPrecision - Precision of the input value
translator - Translator
Returns:
Result register after applying rounding

implementRounding

private static CalcReg implementRounding(long alpha,
                                         RoundingMode roundingMode,
                                         CalcReg inReg,
                                         RelDataType toType,
                                         RexToCalcTranslator translator)
Generates code to round a value to a multiple of a given multipler.

Supports rounding modes .

Parameters:
alpha - The multiplier to round to
roundingMode - Rounding mode; one of RoundingMode.FLOOR, RoundingMode.HALF_UP, RoundingMode.DOWN.
inReg - Register holding input value
toType - Type to convert to
translator - Translator
Returns:
Register containing rounded result