db-derby-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Bryan Pendleton <bpendleton.de...@gmail.com>
Subject Re: Checking an Expressions
Date Sun, 18 Apr 2010 15:05:43 GMT
Nirmal Fernando wrote:
> How can we check whether a particular ValueNode is an "expression" (eg: i*2) ?.
> 
> *Note: We can check "ValueNode instance of ColumnReference" to check
> whether it is a column (eg: i).

I think that every ValueNode is an expression of some sort. Consider:

    select name from employee;
    select year_hired - 1900 from employee;
    select max(salary) from employee;
    select current_date from employee;
    select first_name || ' ' || last_name from employee;

All of these will result in various ValueNode instances being constructed. Some are
simple ColumnReference nodes. Others are more complex expressions.

Unfortunately (or fortunately, depending on how you look at it), ValueNode is
an extremely general-purpose class in Derby, and there are a large number
of particular sub-classes of ValueNode that are of interest.

It so happens that the case of "a simple direct reference to a column in this table"
is extremely common, and has a number of optimizations that we perform, and so
you see code in Derby that says

    if (expression instanceof ColumnReference)

quite commonly. However, there are many sub-classes of ValueNode. Consider
that these classes are just some of the *direct* sub-classes of ValueNode:

BaseColumnNode.java:public class BaseColumnNode extends ValueNode
BinaryListOperatorNode.java:public abstract class BinaryListOperatorNode extends ValueNode
BinaryOperatorNode.java:public class BinaryOperatorNode extends ValueNode
CastNode.java:public class CastNode extends ValueNode
CoalesceFunctionNode.java:public class CoalesceFunctionNode extends ValueNode
ColumnReference.java:public class ColumnReference extends ValueNode
ConditionalNode.java:public class ConditionalNode extends ValueNode
ConstantNode.java:abstract class ConstantNode extends ValueNode
CurrentDatetimeOperatorNode.java:public class CurrentDatetimeOperatorNode extends ValueNode
{
CurrentRowLocationNode.java:public class CurrentRowLocationNode extends ValueNode
DefaultNode.java:public  class DefaultNode extends ValueNode
GenerationClauseNode.java:public class GenerationClauseNode extends ValueNode
JavaToSQLValueNode.java:public class JavaToSQLValueNode extends ValueNode
NextSequenceNode.java:public class NextSequenceNode extends ValueNode {
ParameterNode.java:public class ParameterNode extends ValueNode
ResultColumn.java:public class ResultColumn extends ValueNode
SpecialFunctionNode.java:public class SpecialFunctionNode extends ValueNode
SubqueryNode.java:public class SubqueryNode extends ValueNode
TernaryOperatorNode.java:public class TernaryOperatorNode extends ValueNode
UnaryOperatorNode.java:public class UnaryOperatorNode extends ValueNode
VirtualColumnNode.java:public class VirtualColumnNode extends ValueNode

And many of these classes have further sub-classes of them.

You might finding it helpful to use the parse-tree-printing support in Derby to dump
out the parse tree from several sample queries and have a look at how the parse
trees are put together.

I think that you can use the ASTVisitor support that was added in DERBY-4415
(https://issues.apache.org/jira/browse/DERBY-4415) and DERBY-791
(https://issues.apache.org/jira/browse/DERBY-791) to view the parse trees.

thanks,

bryan


Mime
View raw message