Top Description Fields Constructors Methods
org.apache.derby.impl.sql.compile

pack-priv Class CoalesceFunctionNode

extends ValueNode
Class Inheritance
Imports
java.lang.reflect.Modifier, java.util.List, org.apache.derby.shared.common.error.StandardException, org.apache.derby.shared.common.reference.ClassName, .SQLState, org.apache.derby.iapi.services.classfile.VMOpcode, org.apache.derby.iapi.services.compiler.LocalField, .MethodBuilder, org.apache.derby.iapi.services.context.ContextManager, org.apache.derby.shared.common.sanity.SanityManager, org.apache.derby.iapi.sql.compile.Visitor, org.apache.derby.iapi.util.JBitSet

This node represents coalesce/value function which returns the first argument that is not null. The arguments are evaluated in the order in which they are specified, and the result of the function is the first argument that is not null. The result can be null only if all the arguments can be null. The selected argument is converted, if necessary, to the attributes of the result. SQL Reference Guide for DB2 has section titled "Rules for result data types" at the following url http://publib.boulder.ibm.com/infocenter/db2help/index.jsp?topic=/com.ibm.db2.udb.doc/admin/r0008480.htm I have constructed following table based on various tables and information under "Rules for result data types" This table has FOR BIT DATA TYPES broken out into separate columns for clarity Note that are few differences between Derby and DB2 1)there are few differences between what data types are considered compatible In DB2, CHAR FOR BIT DATA data types are compatible with CHAR data types ie in addition to following table, CHAR is compatible with CHAR FOR BIT DATA, VARCHAR FOR BIT DATA and LONG VARCHAR FOR BIT DATA ie in addition to following table, VARCHAR is compatible with CHAR FOR BIT DATA, VARCHAR FOR BIT DATA and LONG VARCHAR FOR BIT DATA ie in addition to following table, LONG VARCHAR is compatible with CHAR FOR BIT DATA, VARCHAR FOR BIT DATA and LONG VARCHAR FOR BIT DATA ie in addition to following table, CHAR FOR BIT DATA is compatible with DATE, TIME, TIMESTAMP ie in addition to following table, VARCHAR FOR BIT DATA is compatible with DATE, TIME, TIMESTAMP 2)few data types do not have matching precision in Derby and DB2 In DB2, precision of TIME is 8. In Derby, precision of TIME is 0. In DB2, precision,scale of TIMESTAMP is 26,6. In Derby, precision of TIMESTAMP is 0,0. In DB2, precision of DOUBLE is 15. In Derby, precision of DOUBLE is 52. In DB2, precision of REAL is 23. In Derby, precision of REAL is 7. In DB2, precision calculation equation is incorrect when we have int and decimal arguments. The equation should be p=x+max(w-x,10) since precision of integer is 10 in both DB2 and Derby. Instead, DB2 has p=x+max(w-x,11) Types. S I B D R D C V L C V L C D T T B M N I E E O H A O H A O L A I I L A T G C A U A R N A R N O T M M O L E I I L B R C G R C G B E E E B L G N M L H V . H V S I E T A E A A B A A T N R L R R I R R A T C T . . M H B B P A I I R T T SMALLINT { "SMALLINT", "INTEGER", "BIGINT", "DECIMAL", "DOUBLE", "DOUBLE", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR" }, INTEGER { "INTEGER", "INTEGER", "BIGINT", "DECIMAL", "DOUBLE", "DOUBLE", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR" }, BIGINT { "BIGINT", "BIGINT", "BIGINT", "DECIMAL", "DOUBLE", "DOUBLE", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR" }, DECIMAL { "DECIMAL", "DECIMAL", "DECIMAL", "DECIMAL", "DOUBLE", "DOUBLE", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR" }, REAL { "DOUBLE", "DOUBLE", "DOUBLE", "DOUBLE", "REAL", "DOUBLE", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR" }, DOUBLE { "DOUBLE", "DOUBLE", "DOUBLE", "DOUBLE", "DOUBLE", "DOUBLE", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR" }, CHAR { "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "CHAR", "VARCHAR", "LONG VARCHAR", "ERROR", "ERROR", "ERROR", "CLOB", "DATE", "TIME", "TIMESTAMP", "ERROR" }, VARCHAR { "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "VARCHAR", "VARCHAR","LONG VARCHAR", "ERROR", "ERROR", "ERROR", "CLOB", "DATE", "TIME", "TIMESTAMP", "ERROR" }, LONGVARCHAR { "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "LONG VARCHAR", "LONG VARCHAR", "LONG VARCHAR", "ERROR", "ERROR", "ERROR", "CLOB", "ERROR", "ERROR", "ERROR", "ERROR" }, CHAR FOR BIT { "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "BIT", "BIT VARYING", "LONG BIT VARYING", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR" }, VARCH. BIT { "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "BIT VARYING", "BIT VARYING", "LONG BIT VARYING", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR" }, LONGVAR. BIT { "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "LONG BIT VARYING", "LONG BIT VARYING", "LONG BIT VARYING", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR" }, CLOB { "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "CLOB", "CLOB", "CLOB", "ERROR", "ERROR", "ERROR", "CLOB", "ERROR", "ERROR", "ERROR", "ERROR" }, DATE { "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "DATE", "DATE", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "DATE", "ERROR", "ERROR", "ERROR" }, TIME { "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "TIME", "TIME", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "TIME", "ERROR", "ERROR" }, TIMESTAMP { "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "TIMESTAMP", "TIMESTAMP", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "TIMESTAMP", "ERROR" }, BLOB { "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "BLOB" }

Field Summary

Modifier and TypeField and Description
pack-priv ValueNodeList
private int
firstNonParameterNodeIdx

The generated method will generate code to call coalesce on this non-parameter argument.

pack-priv String
Inherited from org.apache.derby.impl.sql.compile.ValueNode:
transformed

Constructor Summary

AccessConstructor and Description
pack-priv
CoalesceFunctionNode(String
Tells if the function was called with name COALESCE or with name VALUE
functionName
,
ValueNodeList
The list of arguments to the coalesce/value function
argumentsList
,
ContextManager
The context manager
cm
)

Constructor for a CoalesceFunctionNode

Method Summary

Modifier and TypeMethod and Description
pack-priv void
acceptChildren(Visitor
the visitor
v
)

Overrides org.apache.derby.impl.sql.compile.QueryTreeNode.acceptChildren.

Accept the visitor for all visitable children of this node.

pack-priv ValueNode

Returns:

The new top of the expression tree.
bindExpression
(FromList
The FROM list for the statement.
fromList
,
SubqueryList
The subquery list being built as we find SubqueryNodes.
subqueryList
,
List<AggregateNode>
The aggregate list being built as we find AggregateNodes.
aggregates
)

Overrides org.apache.derby.impl.sql.compile.ValueNode.bindExpression.

Binding this expression means setting the result DataTypeServices.

public boolean
categorize(JBitSet
JBitSet with bit map of referenced FromTables
referencedTabs
,
boolean
Whether or not to consider method calls, field references and conditional nodes when building bit map
simplePredsOnly
)

Overrides org.apache.derby.impl.sql.compile.ValueNode.categorize.

Categorize this predicate.

pack-priv void
generateExpression(ExpressionClassBuilder
The ExpressionClassBuilder for the class we're generating
acb
,
MethodBuilder
The method the expression will go into
mb
)

Overrides org.apache.derby.impl.sql.compile.ValueNode.generateExpression.

Do code generation for coalesce/value

pack-priv boolean
isEquivalent(ValueNode
the node to compare this ValueNode against.
o
)

Implements abstract org.apache.derby.impl.sql.compile.ValueNode.isEquivalent.

Tests if this node is equivalent to the specified ValueNode.

pack-priv ValueNode

Returns:

The modified expression
preprocess
(int
Number of tables in the DML Statement
numTables
,
FromList
FromList from outer query block
outerFromList
,
SubqueryList
SubqueryList from outer query block
outerSubqueryList
,
PredicateList
PredicateList from outer query block
outerPredicateList
)

Overrides org.apache.derby.impl.sql.compile.ValueNode.preprocess.

Preprocess an expression tree.

pack-priv void
printSubNodes(int
The depth of this node in the tree
depth
)

Overrides org.apache.derby.impl.sql.compile.QueryTreeNode.printSubNodes.

Prints the sub-nodes of this object.

public ValueNode

Returns:

the remapped tree
remapColumnReferencesToExpressions
()

Overrides org.apache.derby.impl.sql.compile.ValueNode.remapColumnReferencesToExpressions.

Remap all the ColumnReferences in this tree to be clones of the underlying expression.

public String
toString()

Overrides org.apache.derby.impl.sql.compile.ValueNode.toString.

Convert this object to a String.

Inherited from org.apache.derby.impl.sql.compile.ValueNode:
bindExpressionchangeToCNFcheckIsBooleancheckTopPredicatesForEqualsConditionsconstantExpressioncopyFieldseliminateNotsevaluateConstantExpressionsgenEqualsFalseTreegenerategenIsNullTreegenSQLJavaSQLTreegetClonegetColumnNamegetConstantValueAsObjectgetDataValueFactorygetOrderableVariantTypegetSchemaNamegetSourceResultColumngetTableNamegetTablesReferencedgetTransformedgetTypeCompilergetTypeIdgetTypeServicesisBinaryEqualsOperatorNodeisBooleanFalseisBooleanTrueisCloneableisConstantExpressionisInListProbeNodeisParameterNodeisRelationalOperatorisSameNodeKindoptimizableEqualityNodeputAndsOnToprequiresTypeFromContextselectivitysetCollationInfosetCollationInfosetCollationUsingCompilationSchemasetCollationUsingCompilationSchemasetNullabilitysetTransformedsetTypesetTypesetTypeupdatableByCursorverifyChangeToCNFverifyEliminateNotsverifyPutAndsOnTop

Field Detail

argumentsListback to summary
pack-priv ValueNodeList argumentsList
firstNonParameterNodeIdxback to summary
private int firstNonParameterNodeIdx

The generated method will generate code to call coalesce on this non-parameter argument.

functionNameback to summary
pack-priv String functionName

Constructor Detail

CoalesceFunctionNodeback to summary
pack-priv CoalesceFunctionNode(String functionName, ValueNodeList argumentsList, ContextManager cm)

Constructor for a CoalesceFunctionNode

Parameters
functionName:String

Tells if the function was called with name COALESCE or with name VALUE

argumentsList:ValueNodeList

The list of arguments to the coalesce/value function

cm:ContextManager

The context manager

Method Detail

acceptChildrenback to summary
pack-priv void acceptChildren(Visitor v) throws StandardException

Overrides org.apache.derby.impl.sql.compile.QueryTreeNode.acceptChildren.

Accept the visitor for all visitable children of this node.

Parameters
v:Visitor

the visitor

Annotations
@Override
Exceptions
StandardException:
on error in the visitor
bindExpressionback to summary
pack-priv ValueNode bindExpression(FromList fromList, SubqueryList subqueryList, List<AggregateNode> aggregates) throws StandardException

Overrides org.apache.derby.impl.sql.compile.ValueNode.bindExpression.

Binding this expression means setting the result DataTypeServices. In this case, the result type is based on the rules in the table listed earlier.

Parameters
fromList:FromList

The FROM list for the statement.

subqueryList:SubqueryList

The subquery list being built as we find SubqueryNodes.

aggregates:List<AggregateNode>

The aggregate list being built as we find AggregateNodes.

Returns:ValueNode

The new top of the expression tree.

Annotations
@Override
Exceptions
StandardException:
Thrown on error
categorizeback to summary
public boolean categorize(JBitSet referencedTabs, boolean simplePredsOnly) throws StandardException

Overrides org.apache.derby.impl.sql.compile.ValueNode.categorize.

Categorize this predicate.

Parameters
referencedTabs:JBitSet

Doc from org.apache.derby.impl.sql.compile.ValueNode.categorize.

JBitSet with bit map of referenced FromTables

simplePredsOnly:boolean

Doc from org.apache.derby.impl.sql.compile.ValueNode.categorize.

Whether or not to consider method calls, field references and conditional nodes when building bit map

Returns:boolean

Doc from org.apache.derby.impl.sql.compile.ValueNode.categorize.

boolean Whether or not source.expression is a ColumnReference or a VirtualColumnNode.

Annotations
@Override
Exceptions
StandardException:

Doc from org.apache.derby.impl.sql.compile.ValueNode.categorize.

Thrown on error

See Also
ValueNode#categorize(JBitSet, boolean)
generateExpressionback to summary
pack-priv void generateExpression(ExpressionClassBuilder acb, MethodBuilder mb) throws StandardException

Overrides org.apache.derby.impl.sql.compile.ValueNode.generateExpression.

Do code generation for coalesce/value

Parameters
acb:ExpressionClassBuilder

The ExpressionClassBuilder for the class we're generating

mb:MethodBuilder

The method the expression will go into

Annotations
@Override
Exceptions
StandardException:
Thrown on error
isEquivalentback to summary
pack-priv boolean isEquivalent(ValueNode o) throws StandardException

Implements abstract org.apache.derby.impl.sql.compile.ValueNode.isEquivalent.

Doc from org.apache.derby.impl.sql.compile.ValueNode.isEquivalent.

Tests if this node is equivalent to the specified ValueNode. Two ValueNodes are considered equivalent if they will evaluate to the same value during query execution.

This method provides basic expression matching facility for the derived class of ValueNode and it is used by the language layer to compare the node structural form of the two expressions for equivalence at bind phase.

Note that it is not comparing the actual row values at runtime to produce a result; hence, when comparing SQL NULLs, they are considered to be equivalent and not unknown.

One usage case of this method in this context is to compare the select column expression against the group by expression to check if they are equivalent. e.g.:

SELECT c1+c2 FROM t1 GROUP BY c1+c2

In general, node equivalence is determined by the derived class of ValueNode. But they generally abide to the rules below:

  • The two ValueNodes must be of the same node type to be considered equivalent. e.g.: CastNode vs. CastNode - equivalent (if their args also match), ColumnReference vs CastNode - not equivalent.
  • If node P contains other ValueNode(s) and so on, those node(s) must also be of the same node type to be considered equivalent.
  • If node P takes a parameter list, then the number of arguments and its arguments for the two nodes must also match to be considered equivalent. e.g.: CAST(c1 as INTEGER) vs CAST(c1 as SMALLINT), they are not equivalent.
  • When comparing SQL NULLs in this context, they are considered to be equivalent.
  • If this does not apply or it is determined that the two nodes are not equivalent then the derived class of this method should return false; otherwise, return true.
Parameters
o:ValueNode

the node to compare this ValueNode against.

Returns:boolean

true if the two nodes are equivalent, false otherwise.

preprocessback to summary
pack-priv ValueNode preprocess(int numTables, FromList outerFromList, SubqueryList outerSubqueryList, PredicateList outerPredicateList) throws StandardException

Overrides org.apache.derby.impl.sql.compile.ValueNode.preprocess.

Preprocess an expression tree. We do a number of transformations here (including subqueries, IN lists, LIKE and BETWEEN) plus subquery flattening.

Note

This is done before the outer ResultSetNode is preprocessed.

Parameters
numTables:int

Number of tables in the DML Statement

outerFromList:FromList

FromList from outer query block

outerSubqueryList:SubqueryList

SubqueryList from outer query block

outerPredicateList:PredicateList

PredicateList from outer query block

Returns:ValueNode

The modified expression

Annotations
@Override
Exceptions
StandardException:
Thrown on error
printSubNodesback to summary
pack-priv void printSubNodes(int depth)

Overrides org.apache.derby.impl.sql.compile.QueryTreeNode.printSubNodes.

Prints the sub-nodes of this object. See QueryTreeNode.java for how tree printing is supposed to work.

Parameters
depth:int

The depth of this node in the tree

Annotations
@Override
remapColumnReferencesToExpressionsback to summary
public ValueNode remapColumnReferencesToExpressions() throws StandardException

Overrides org.apache.derby.impl.sql.compile.ValueNode.remapColumnReferencesToExpressions.

Remap all the ColumnReferences in this tree to be clones of the underlying expression.

Returns:ValueNode

the remapped tree

Annotations
@Override
Exceptions
StandardException:
if an error occurs
toStringback to summary
public String toString()

Overrides org.apache.derby.impl.sql.compile.ValueNode.toString.

Doc from org.apache.derby.impl.sql.compile.ValueNode.toString.

Convert this object to a String. See comments in QueryTreeNode.java for how this should be done for tree printing.

Returns:String

This object as a String

Annotations
@Override