net.sf.saffron.sql
Class SqlValidator

java.lang.Object
  |
  +--net.sf.saffron.sql.SqlValidator

public class SqlValidator
extends Object

SqlValidator validates the parse tree of a SQL statement, and provides semantic information about the parse tree.

Since:
Mar 25, 2003
Version:
$Id: //open/saffron/src/net/sf/saffron/sql/SqlValidator.java#13 $
Author:
jhyde

Nested Class Summary
static interface SqlValidator.CatalogReader
          A CatalogReader supplies catalog information for a SqlValidator.
 class SqlValidator.IdentifierScope
           
(package private)  class SqlValidator.JoinScope
          The name-resolution scope of a JOIN operator.
 class SqlValidator.Scope
          Name-resolution scope.
 class SqlValidator.SelectScope
          The name-resolution scope of a SELECT clause.
(package private)  class SqlValidator.SetopScope
           
static interface SqlValidator.Table
          A Table supplies a SqlValidator with the metadata for a table.
(package private)  class SqlValidator.TableConstructorScope
           
 
Field Summary
private  SqlValidator.CatalogReader catalogReader
           
static String[] emptyStrings
           
static SaffronType[] emptyTypes
           
private  int nextGeneratedId
           
 SqlOperatorTable opTab
           
private  SqlNode outermostNode
           
private  HashMap scopes
          Maps query nodes to the scope created from them.
(package private)  SaffronTypeFactory typeFactory
           
 
Constructor Summary
SqlValidator(SqlOperatorTable opTab, SqlValidator.CatalogReader catalogReader, SaffronTypeFactory typeFactory)
          Creates a validator.
 
Method Summary
private  void addToSelectList(ArrayList list, List aliases, List types, SqlNode exp, SqlValidator.Scope scope)
          Adds an expression to a select list, ensuring that its alias does not clash with any existing expressions on the list.
 void check(String s)
           
 void checkFails(String s, String message)
           
private  SqlSelect createInternalSelect(SqlNode node, boolean setOutermostNode)
           
private  String deriveAlias(SqlNode node, int ordinal)
          Derives an alias for an expression.
(package private)  SaffronType deriveType(SqlValidator.Scope scope, SqlNode operand)
           
private  boolean expandSelectItem(SqlNode selectItem, SqlSelect select, ArrayList selectItems, List aliases, List types)
          If selectItem is "*" or "TABLE.*", expands it and returns true; otherwise writes the unexpanded item.
 SqlNodeList expandStar(SqlNodeList selectList, SqlSelect query)
          Returns a list of expressions, with every occurrence of "*" or "TABLE.*" expanded.
private  SqlNode getChild(SqlSelect select, String alias)
           
 SqlNode getOutermostNode()
           
 SqlValidator.Scope getScope(SqlNode node)
           
 SqlValidator.SelectScope getScope(SqlSelect node)
           
private  SaffronType getTableConstructorRowType(SqlCall values, SqlValidator.Scope scope)
           
private  boolean isStar(SqlNode selectItem)
          Returns whether a select item is "*" or "TABLE.*".
private static SaffronType lookupField(SaffronType rowType, String columnName)
           
private  RuntimeException newValidationError(String s)
           
private  void register(SqlValidator.Scope scope, boolean inFrom)
          Registers a new scope.
private  void registerFrom(SqlValidator.Scope scope, SqlNode node, String alias)
           
private  void registerQuery(SqlValidator.Scope scope, SqlNode node, String alias, boolean inFrom)
           
private  void registerSubqueries(SqlValidator.SelectScope selectScope, SqlNode node)
           
 void testCrossJoinOnFails()
           
 void testCrossJoinUsingFails()
           
 void testDoubleNoAlias()
           
 void testDuplicateColumnAliasFails()
           
 void testFromReferenceFails()
           
 void testIncompatibleUnionFails()
           
 void testInnerJoinWithoutUsingOrOnFails()
           
 void testInTooManyColumnsFails()
           
 void testInvalidGroupBy(TestCase test)
           
 void testJoinUsingInvalidColsFails()
           
 void testNaturalCrossJoinFails()
           
 void testObscuredAliasFails()
           
 void testSingleNoAlias()
           
 void testUnionOfNonQueryFails()
           
 void testWhereReference()
           
 void validate(SqlNode topNode)
          Validates a tree.
private  void validateDelete(SqlDelete call)
           
private  void validateExpression(SqlNode node)
           
private  void validateFrom(SqlNode node)
           
private  void validateInsert(SqlInsert call)
           
 void validateQuery(SqlNode node)
          Checks that a query (select statement, or a set operation union, intersect, except) is valid.
private  void validateSelect(SqlSelect select)
           
private  void validateValues(SqlCall node)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

opTab

public final SqlOperatorTable opTab

catalogReader

private final SqlValidator.CatalogReader catalogReader

scopes

private HashMap scopes
Maps query nodes to the scope created from them.


outermostNode

private SqlNode outermostNode

nextGeneratedId

private int nextGeneratedId

typeFactory

final SaffronTypeFactory typeFactory

emptyTypes

public static final SaffronType[] emptyTypes

emptyStrings

public static final String[] emptyStrings
Constructor Detail

SqlValidator

public SqlValidator(SqlOperatorTable opTab,
                    SqlValidator.CatalogReader catalogReader,
                    SaffronTypeFactory typeFactory)
Creates a validator.

Pre-condition:
opTab != null, // node is a "query expression" (per SQL standard), catalogReader != null, typeFactory != null
Method Detail

getOutermostNode

public SqlNode getOutermostNode()

getScope

public SqlValidator.SelectScope getScope(SqlSelect node)

check

public void check(String s)

checkFails

public void checkFails(String s,
                       String message)

expandStar

public SqlNodeList expandStar(SqlNodeList selectList,
                              SqlSelect query)
Returns a list of expressions, with every occurrence of "*" or "TABLE.*" expanded.


isStar

private boolean isStar(SqlNode selectItem)
Returns whether a select item is "*" or "TABLE.*".


expandSelectItem

private boolean expandSelectItem(SqlNode selectItem,
                                 SqlSelect select,
                                 ArrayList selectItems,
                                 List aliases,
                                 List types)
If selectItem is "*" or "TABLE.*", expands it and returns true; otherwise writes the unexpanded item.

Parameters:
selectItem - Select-list item
select - Containing select clause
selectItems - List that expanded items are written to
aliases - List of aliases
Returns:
Whether the node was expanded

testDoubleNoAlias

public void testDoubleNoAlias()

testDuplicateColumnAliasFails

public void testDuplicateColumnAliasFails()

testInvalidGroupBy

public void testInvalidGroupBy(TestCase test)

testSingleNoAlias

public void testSingleNoAlias()

testObscuredAliasFails

public void testObscuredAliasFails()

testFromReferenceFails

public void testFromReferenceFails()

testWhereReference

public void testWhereReference()

testIncompatibleUnionFails

public void testIncompatibleUnionFails()

testUnionOfNonQueryFails

public void testUnionOfNonQueryFails()

testInTooManyColumnsFails

public void testInTooManyColumnsFails()

testNaturalCrossJoinFails

public void testNaturalCrossJoinFails()

testCrossJoinUsingFails

public void testCrossJoinUsingFails()

testCrossJoinOnFails

public void testCrossJoinOnFails()

testInnerJoinWithoutUsingOrOnFails

public void testInnerJoinWithoutUsingOrOnFails()

testJoinUsingInvalidColsFails

public void testJoinUsingInvalidColsFails()

validate

public void validate(SqlNode topNode)
Validates a tree. You can call this method only once.

Pre-condition:
getOutermostNode() == null, topNode.getKind().isA(SqlKind.TopLevel)

validateQuery

public void validateQuery(SqlNode node)
Checks that a query (select statement, or a set operation union, intersect, except) is valid.

Throws:
RuntimeException - if the query is not valid

getScope

public SqlValidator.Scope getScope(SqlNode node)

getChild

private SqlNode getChild(SqlSelect select,
                         String alias)

createInternalSelect

private SqlSelect createInternalSelect(SqlNode node,
                                       boolean setOutermostNode)

getTableConstructorRowType

private SaffronType getTableConstructorRowType(SqlCall values,
                                               SqlValidator.Scope scope)

deriveType

SaffronType deriveType(SqlValidator.Scope scope,
                       SqlNode operand)

addToSelectList

private void addToSelectList(ArrayList list,
                             List aliases,
                             List types,
                             SqlNode exp,
                             SqlValidator.Scope scope)
Adds an expression to a select list, ensuring that its alias does not clash with any existing expressions on the list.


deriveAlias

private String deriveAlias(SqlNode node,
                           int ordinal)
Derives an alias for an expression. If no alias can be derived, returns null if ordinal is less than zero, otherwise generates an alias EXPR$ordinal.


newValidationError

private RuntimeException newValidationError(String s)

register

private void register(SqlValidator.Scope scope,
                      boolean inFrom)
Registers a new scope. We assume that its parent pointer has already been set.

Parameters:
scope - Scope to register
inFrom - Whether the scope is in the FROM list of its parent. Only scopes in the FROM list can be returned when resolving identifiers.

registerFrom

private void registerFrom(SqlValidator.Scope scope,
                          SqlNode node,
                          String alias)

registerQuery

private void registerQuery(SqlValidator.Scope scope,
                           SqlNode node,
                           String alias,
                           boolean inFrom)

registerSubqueries

private void registerSubqueries(SqlValidator.SelectScope selectScope,
                                SqlNode node)

validateExpression

private void validateExpression(SqlNode node)

validateFrom

private void validateFrom(SqlNode node)

validateSelect

private void validateSelect(SqlSelect select)

validateInsert

private void validateInsert(SqlInsert call)

validateDelete

private void validateDelete(SqlDelete call)

validateValues

private void validateValues(SqlCall node)

lookupField

private static SaffronType lookupField(SaffronType rowType,
                                       String columnName)

SourceForge.net_Logo