org.eigenbase.sql
Class SqlNode

java.lang.Object
  extended by org.eigenbase.sql.SqlNode
All Implemented Interfaces:
Cloneable
Direct Known Subclasses:
SqlCall, SqlDataTypeSpec, SqlDynamicParam, SqlIdentifier, SqlIntervalQualifier, SqlLiteral, SqlNodeList

public abstract class SqlNode
extends Object
implements Cloneable

A SqlNode is a SQL parse tree. It may be an operator, literal, identifier, and so forth.

Since:
Dec 12, 2003
Version:
$Id: //open/dev/farrago/src/org/eigenbase/sql/SqlNode.java#28 $
Author:
jhyde

Field Summary
static SqlNode[] emptyArray
           
private  SqlParserPos pos
           
 
Constructor Summary
SqlNode(SqlParserPos pos)
          Creates a node.
 
Method Summary
abstract
<R> R
accept(SqlVisitor<R> visitor)
          Accepts a generic visitor.
 Object clone()
           
 SqlNode clone(SqlParserPos pos)
          Clones a SqlNode with a different position.
static SqlNode[] cloneArray(SqlNode[] nodes)
           
static boolean equalDeep(SqlNode node1, SqlNode node2, boolean fail)
          Returns whether two nodes are equal (using equalsDeep(SqlNode,boolean)) or are both null.
abstract  boolean equalsDeep(SqlNode node, boolean fail)
          Returns whether this node is structurally equivalent to another node.
 void findValidOptions(SqlValidator validator, SqlValidatorScope scope, SqlParserPos pos, List<SqlMoniker> hintList)
          Lists all the valid alternatives for this node if the parse position of the node matches that of pos.
 SqlKind getKind()
          Returns the type of node this is, or SqlKind.Other if it's nothing special.
 SqlMonotonicity getMonotonicity(SqlValidatorScope scope)
          Returns whether expression is always ascending, descending or constant.
 SqlParserPos getParserPosition()
           
 boolean isA(SqlKind kind)
          Returns whether this node is a particular kind.
 String toSqlString(SqlDialect dialect)
           
 String toSqlString(SqlDialect dialect, boolean forceParens)
          Returns the SQL text of the tree of which this SqlNode is the root.
 String toString()
           
abstract  void unparse(SqlWriter writer, int leftPrec, int rightPrec)
          Writes a SQL representation of this node to a writer.
abstract  void validate(SqlValidator validator, SqlValidatorScope scope)
          Validates this node.
 void validateExpr(SqlValidator validator, SqlValidatorScope scope)
          Validates this node in an expression context.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

emptyArray

public static final SqlNode[] emptyArray

pos

private final SqlParserPos pos
Constructor Detail

SqlNode

SqlNode(SqlParserPos pos)
Creates a node.

Parameters:
pos - Parser position, must not be null.
Method Detail

clone

public Object clone()
Overrides:
clone in class Object

clone

public SqlNode clone(SqlParserPos pos)
Clones a SqlNode with a different position.


isA

public boolean isA(SqlKind kind)
Returns whether this node is a particular kind.

Parameters:
kind - a SqlKind value

getKind

public SqlKind getKind()
Returns the type of node this is, or SqlKind.Other if it's nothing special.

Returns:
a SqlKind value, never null

cloneArray

public static SqlNode[] cloneArray(SqlNode[] nodes)

toString

public String toString()
Overrides:
toString in class Object

toSqlString

public String toSqlString(SqlDialect dialect,
                          boolean forceParens)
Returns the SQL text of the tree of which this SqlNode is the root.

Parameters:
dialect -
forceParens - wraps all expressions in parentheses; good for parse test, but false by default.

Typical return values are:

  • 'It''s a bird!'
  • NULL
  • 12.3
  • DATE '1969-04-29'

toSqlString

public String toSqlString(SqlDialect dialect)

unparse

public abstract void unparse(SqlWriter writer,
                             int leftPrec,
                             int rightPrec)
Writes a SQL representation of this node to a writer.

The leftPrec and rightPrec parameters give us enough context to decide whether we need to enclose the expression in parentheses. For example, we need parentheses around "2 + 3" if preceded by "5 *". This is because the precedence of the "*" operator is greater than the precedence of the "+" operator.

The algorithm handles left- and right-associative operators by giving them slightly different left- and right-precedence.

If SqlWriter.isAlwaysUseParentheses() is true, we use parentheses even when they are not required by the precedence rules.

For the details of this algorithm, see SqlCall.unparse(org.eigenbase.sql.SqlWriter, int, int).

Parameters:
writer - Target writer
leftPrec - The precedence of the SqlNode immediately preceding this node in a depth-first scan of the parse tree
rightPrec - The precedence of the SqlNode immediately

getParserPosition

public SqlParserPos getParserPosition()

validate

public abstract void validate(SqlValidator validator,
                              SqlValidatorScope scope)
Validates this node.

The typical implementation of this method will make a callback to the validator appropriate to the node type and context. The validator has methods such as SqlValidator.validateLiteral(org.eigenbase.sql.SqlLiteral) for these purposes.

Parameters:
scope - Validator

findValidOptions

public void findValidOptions(SqlValidator validator,
                             SqlValidatorScope scope,
                             SqlParserPos pos,
                             List<SqlMoniker> hintList)
Lists all the valid alternatives for this node if the parse position of the node matches that of pos. Only implemented now for SqlCall and SqlOperator.

Parameters:
validator - Validator
scope - Validation scope
pos - SqlParserPos indicating the cursor position at which competion hints are requested for
hintList - list of valid options

validateExpr

public void validateExpr(SqlValidator validator,
                         SqlValidatorScope scope)
Validates this node in an expression context.

Usually, this method does much the same as validate(org.eigenbase.sql.validate.SqlValidator, org.eigenbase.sql.validate.SqlValidatorScope), but a SqlIdentifier can occur in expression and non-expression contexts.


accept

public abstract <R> R accept(SqlVisitor<R> visitor)
Accepts a generic visitor.

Implementations of this method in subtypes simply call the appropriate visit method on the visitor object.

The type parameter R must be consistent with the type parameter of the visitor.


equalsDeep

public abstract boolean equalsDeep(SqlNode node,
                                   boolean fail)
Returns whether this node is structurally equivalent to another node. Some examples:


equalDeep

public static boolean equalDeep(SqlNode node1,
                                SqlNode node2,
                                boolean fail)
Returns whether two nodes are equal (using equalsDeep(SqlNode,boolean)) or are both null.

Parameters:
node1 - First expression
node2 - Second expression
fail - Whether to throw AssertionError if expressions are not equal

getMonotonicity

public SqlMonotonicity getMonotonicity(SqlValidatorScope scope)
Returns whether expression is always ascending, descending or constant. This property is useful because it allows to safely aggregte infinite streams of values.

The default implementation returns SqlMonotonicity.NotMonotonic.