net.sf.saffron.oj.xlat
Class ExtenderAggregation

java.lang.Object
  |
  +--net.sf.saffron.oj.xlat.ExtenderAggregation
All Implemented Interfaces:
Aggregation

class ExtenderAggregation
extends Object
implements Aggregation

ExtenderAggregation is an aggregation which works by instantiating a user-defined aggregation (AggregationExtender), as opposed to generating custom code.

Two bugs with the current implementation of implementStart(net.sf.saffron.opt.RelImplementor, net.sf.saffron.rel.SaffronRel, int[]) etc. First, the aggregation expression should be evaluated only once -- when the query starts -- and stored in a variable. I guess it should be added to the environment holding the query.

Second, we pass real expressions as the values of the dummy arguments to implementStart(net.sf.saffron.opt.RelImplementor, net.sf.saffron.rel.SaffronRel, int[]) and implementResult(openjava.ptree.Expression). This is inefficient, but moreover, it is conceivable that the expressions will not be valid in the scope where start and result are executed.

Since:
3 February, 2002
Version:
$Id: //open/saffron/src/net/sf/saffron/oj/xlat/ExtenderAggregation.java#5 $
Author:
jhyde

Field Summary
(package private)  OJClass aggClazz
           
(package private)  Expression aggExp
           
(package private)  OJMethod aggregateMethod
           
(package private)  OJClass[] argTypes
           
(package private)  OJMethod mergeMethod
           
(package private)  OJMethod nextMethod
           
(package private)  OJMethod resultMethod
           
(package private)  OJMethod startMethod
           
 
Constructor Summary
(package private) ExtenderAggregation(Expression aggExp, Environment env, OJClass[] argTypes)
           
 
Method Summary
 boolean canMerge()
          Whether this aggregation can merge together two accumulators.
private static OJMethod findMethod(OJClass aggClazz, String name, OJClass[] argTypes, int extra, OJClass desiredType, boolean fail)
           
 SaffronType[] getParameterTypes(SaffronTypeFactory typeFactory)
           
 SaffronType getReturnType(SaffronTypeFactory typeFactory)
           
 void implementMerge(RelImplementor implementor, SaffronRel rel, Expression accumulator, Expression otherAccumulator)
          Generates (into the current statement list, gleaned by calling implementor's RelImplementor.getStatementList() method) code to merge two accumulators.
 void implementNext(RelImplementor implementor, SaffronRel rel, Expression accumulator, int[] args)
          Generates (into the current statement list, gleaned by calling implementor's RelImplementor.getStatementList() method) the piece of code which gets called each time an extra row is seen.
 Expression implementResult(Expression accumulator)
          Generates the expression which gets called when a total is complete.
 Expression implementStart(RelImplementor implementor, SaffronRel rel, int[] args)
          Generates the expression which gets called when a new total is created.
 Expression implementStartAndNext(RelImplementor implementor, SaffronRel rel, int[] args)
          Generates code to create a new total and to add the first value.
static boolean matches(OJClass aggClazz, OJClass[] argTypes)
          Returns whether this aggregation has an overloading which matches the given name and argument types.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

aggExp

Expression aggExp

aggClazz

OJClass aggClazz

aggregateMethod

OJMethod aggregateMethod

mergeMethod

OJMethod mergeMethod

nextMethod

OJMethod nextMethod

resultMethod

OJMethod resultMethod

startMethod

OJMethod startMethod

argTypes

OJClass[] argTypes
Constructor Detail

ExtenderAggregation

ExtenderAggregation(Expression aggExp,
                    Environment env,
                    OJClass[] argTypes)
Method Detail

getParameterTypes

public SaffronType[] getParameterTypes(SaffronTypeFactory typeFactory)
Specified by:
getParameterTypes in interface Aggregation

getReturnType

public SaffronType getReturnType(SaffronTypeFactory typeFactory)
Specified by:
getReturnType in interface Aggregation

matches

public static boolean matches(OJClass aggClazz,
                              OJClass[] argTypes)
Returns whether this aggregation has an overloading which matches the given name and argument types.


canMerge

public boolean canMerge()
Description copied from interface: Aggregation
Whether this aggregation can merge together two accumulators. count can (you just add the accumulators); avg and Nth cannot.

Specified by:
canMerge in interface Aggregation

implementMerge

public void implementMerge(RelImplementor implementor,
                           SaffronRel rel,
                           Expression accumulator,
                           Expression otherAccumulator)
Description copied from interface: Aggregation
Generates (into the current statement list, gleaned by calling implementor's RelImplementor.getStatementList() method) code to merge two accumulators. For sum(x), this looks like ((saffron.runtime.Holder.int_Holder) accumulator).value += ((saffron.runtime.Holder.int_Holder) other).value.

The method is only called if Aggregation.canMerge() returns true.

Specified by:
implementMerge in interface Aggregation
Parameters:
implementor - a callback object which knows how to generate things
rel - the relational expression which is generating this code
accumulator - the expression which holds the total
otherAccumulator - accumulator to merge in

implementNext

public void implementNext(RelImplementor implementor,
                          SaffronRel rel,
                          Expression accumulator,
                          int[] args)
Description copied from interface: Aggregation
Generates (into the current statement list, gleaned by calling implementor's RelImplementor.getStatementList() method) the piece of code which gets called each time an extra row is seen. For sum(x), this looks like ((net.sf.saffron.runtime.Holder.int_Holder) accumulator).value += x.

Specified by:
implementNext in interface Aggregation
Parameters:
implementor - a callback object which knows how to generate things
rel - the relational expression which is generating this code
accumulator - the expression which holds the total
args - the ordinals of the fields of the child row which are arguments to this aggregation

implementResult

public Expression implementResult(Expression accumulator)
Description copied from interface: Aggregation
Generates the expression which gets called when a total is complete. For sum(x), this looks like ((saffron.runtime.Holder.int_Holder) accumulator).value.

Specified by:
implementResult in interface Aggregation

implementStart

public Expression implementStart(RelImplementor implementor,
                                 SaffronRel rel,
                                 int[] args)
Description copied from interface: Aggregation
Generates the expression which gets called when a new total is created. For sum(x), this looks like new saffron.runtime.Holder.int_Holder(0).

Specified by:
implementStart in interface Aggregation

implementStartAndNext

public Expression implementStartAndNext(RelImplementor implementor,
                                        SaffronRel rel,
                                        int[] args)
Description copied from interface: Aggregation
Generates code to create a new total and to add the first value. For sum(x), this looks like new saffron.runtime.Holder.int_Holder(x).

Specified by:
implementStartAndNext in interface Aggregation

findMethod

private static OJMethod findMethod(OJClass aggClazz,
                                   String name,
                                   OJClass[] argTypes,
                                   int extra,
                                   OJClass desiredType,
                                   boolean fail)

SourceForge.net_Logo