org.eigenbase.sql.validate
Class SqlValidatorUtil

java.lang.Object
  extended by org.eigenbase.sql.validate.SqlValidatorUtil

public class SqlValidatorUtil
extends Object

Utility methods related to validation.

Since:
Mar 25, 2003
Version:
$Id: //open/dev/farrago/src/org/eigenbase/sql/validate/SqlValidatorUtil.java#23 $
Author:
jhyde

Nested Class Summary
static class SqlValidatorUtil.DeepCopier
          Walks over an expression, copying every node, and fully-qualifying every identifier.
 
Constructor Summary
SqlValidatorUtil()
           
 
Method Summary
static SqlNode addAlias(SqlNode expr, String alias)
          Converts an expression "expr" into "expr AS alias".
static void checkCharsetAndCollateConsistentIfCharType(RelDataType type)
           
(package private) static int countOccurrences(String name, String[] names)
           
static List<String> deriveNaturalJoinColumnList(RelDataType leftRowType, RelDataType rightRowType)
          Derives the list of column names suitable for NATURAL JOIN.
static String getAlias(SqlNode node)
          Derives an alias for a node.
static String getAlias(SqlNode node, int ordinal)
          Derives an alias for a node, and invents a mangled identifier if it cannot.
static SelectScope getEnclosingSelectScope(SqlValidatorScope scope)
           
static RelOptTable getRelOptTable(SqlValidatorNamespace namespace, RelOptSchema schema, String datasetName, boolean[] usedDataset)
          Converts a SqlValidatorScope into a RelOptTable.
static void getSchemaObjectMonikers(SqlValidatorCatalogReader catalogReader, List<String> names, List<SqlMoniker> hints)
           
static SqlValidatorNamespace lookup(SqlValidatorScope scope, List<String> names)
          Resolves a multi-part identifier such as "SCHEMA.EMP.EMPNO" to a namespace.
static int lookupField(RelDataType rowType, String columnName)
          Looks up a field with a given name and if found returns its ordinal.
(package private) static RelDataType lookupFieldType(RelDataType rowType, String columnName)
          Looks up a field with a given name and if found returns its type.
static SqlValidatorWithHints newValidator(SqlOperatorTable opTab, SqlValidatorCatalogReader catalogReader, RelDataTypeFactory typeFactory)
          Factory method for SqlValidator.
static void uniquify(List<String> nameList)
          Makes sure that the names in a list are unique.
static String uniquify(String name, Collection<String> nameList)
          Makes a name distinct from other names which have already been used, adds it to the list, and returns it.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SqlValidatorUtil

public SqlValidatorUtil()
Method Detail

getRelOptTable

public static RelOptTable getRelOptTable(SqlValidatorNamespace namespace,
                                         RelOptSchema schema,
                                         String datasetName,
                                         boolean[] usedDataset)
Converts a SqlValidatorScope into a RelOptTable. This is only possible if the scope represents an identifier, such as "sales.emp". Otherwise, returns null.

Parameters:
namespace - Namespace
schema - Schema
datasetName - Name of sample dataset to substitute, or null to use the regular table
usedDataset - Output parameter which is set to true if a sample dataset is found; may be null

lookupFieldType

static RelDataType lookupFieldType(RelDataType rowType,
                                   String columnName)
Looks up a field with a given name and if found returns its type.

Parameters:
rowType - Row type
columnName - Field name
Returns:
Field's type, or null if not found

lookupField

public static int lookupField(RelDataType rowType,
                              String columnName)
Looks up a field with a given name and if found returns its ordinal.

Parameters:
rowType - Row type
columnName - Field name
Returns:
Ordinal of field, or -1 if not found

checkCharsetAndCollateConsistentIfCharType

public static void checkCharsetAndCollateConsistentIfCharType(RelDataType type)

addAlias

public static SqlNode addAlias(SqlNode expr,
                               String alias)
Converts an expression "expr" into "expr AS alias".


getAlias

public static String getAlias(SqlNode node)
Derives an alias for a node. If it cannot derive an alias, returns null.

This method doesn't try very hard. It doesn't invent mangled aliases, and doesn't even recognize an AS clause. (See getAlias(SqlNode, int) for that.) It just takes the last part of an identifier.


getAlias

public static String getAlias(SqlNode node,
                              int ordinal)
Derives an alias for a node, and invents a mangled identifier if it cannot.

Examples:

Returns:
An alias, if one can be derived; or a synthetic alias "expr$ordinal" if ordinal >= 0; otherwise null

uniquify

public static String uniquify(String name,
                              Collection<String> nameList)
Makes a name distinct from other names which have already been used, adds it to the list, and returns it.

Parameters:
name - Suggested name, may not be unique
nameList - Collection of names already used
Returns:
Unique name

newValidator

public static SqlValidatorWithHints newValidator(SqlOperatorTable opTab,
                                                 SqlValidatorCatalogReader catalogReader,
                                                 RelDataTypeFactory typeFactory)
Factory method for SqlValidator.


uniquify

public static void uniquify(List<String> nameList)
Makes sure that the names in a list are unique.


lookup

public static SqlValidatorNamespace lookup(SqlValidatorScope scope,
                                           List<String> names)
Resolves a multi-part identifier such as "SCHEMA.EMP.EMPNO" to a namespace. The returned namespace may represent a schema, table, column, etc.

"Precondition:"
names.size() > 0
"Postcondition:"
return != null

getSchemaObjectMonikers

public static void getSchemaObjectMonikers(SqlValidatorCatalogReader catalogReader,
                                           List<String> names,
                                           List<SqlMoniker> hints)

getEnclosingSelectScope

public static SelectScope getEnclosingSelectScope(SqlValidatorScope scope)

deriveNaturalJoinColumnList

public static List<String> deriveNaturalJoinColumnList(RelDataType leftRowType,
                                                       RelDataType rightRowType)
Derives the list of column names suitable for NATURAL JOIN. These are the columns that occur exactly once on each side of the join.

Parameters:
leftRowType - Row type of left input to the join
rightRowType - Row type of right input to the join
Returns:
List of columns that occur once on each side

countOccurrences

static int countOccurrences(String name,
                            String[] names)