Class SqlIdentifier
- java.lang.Object
-
- org.apache.calcite.sql.SqlNode
-
- org.apache.calcite.sql.SqlIdentifier
-
- All Implemented Interfaces:
java.lang.Cloneable
public class SqlIdentifier extends SqlNode
ASqlIdentifier
is an identifier, possibly compound.
-
-
Field Summary
Fields Modifier and Type Field Description (package private) SqlCollation
collation
This identifier's collation (if any).protected com.google.common.collect.ImmutableList<SqlParserPos>
componentPositions
A list of the positions of the components of compound identifiers.com.google.common.collect.ImmutableList<java.lang.String>
names
Array of the components of this compound identifier.-
Fields inherited from class org.apache.calcite.sql.SqlNode
EMPTY_ARRAY, pos
-
-
Constructor Summary
Constructors Constructor Description SqlIdentifier(java.lang.String name, SqlParserPos pos)
Creates a simple identifier, for examplefoo
.SqlIdentifier(java.lang.String name, SqlCollation collation, SqlParserPos pos)
Creates a simple identifier, for examplefoo
, with a collation.SqlIdentifier(java.util.List<java.lang.String> names, SqlParserPos pos)
SqlIdentifier(java.util.List<java.lang.String> names, SqlCollation collation, SqlParserPos pos, java.util.List<SqlParserPos> componentPositions)
Creates a compound identifier, for examplefoo.bar
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description <R> R
accept(SqlVisitor<R> visitor)
Accepts a generic visitor.SqlIdentifier
add(int i, java.lang.String name, SqlParserPos pos)
Returns an identifier that is the same as this except with a component added at a given position.void
assignNamesFrom(SqlIdentifier other)
Copies names and components from another identifier.SqlNode
clone(SqlParserPos pos)
Clones a SqlNode with a different position.boolean
equalsDeep(SqlNode node, Litmus litmus)
Returns whether this node is structurally equivalent to another node.SqlCollation
getCollation()
SqlIdentifier
getComponent(int ordinal)
Creates an identifier which contains only theordinal
th component of this compound identifier.SqlIdentifier
getComponent(int from, int to)
SqlParserPos
getComponentParserPosition(int i)
Returns the position of thei
th component of a compound identifier, or the position of the whole identifier if that information is not present.SqlKind
getKind()
Returns the type of node this is, orSqlKind.OTHER
if it's nothing special.SqlMonotonicity
getMonotonicity(SqlValidatorScope scope)
Returns whether expression is always ascending, descending or constant.java.lang.String
getSimple()
static java.lang.String
getString(java.util.List<java.lang.String> names)
Converts a list of strings to a qualified identifier.boolean
isSimple()
Returns whether this is a simple identifier.boolean
isStar()
Returns whether this identifier is a star, such as "*" or "foo.bar.*".SqlIdentifier
plus(java.lang.String name, SqlParserPos pos)
Creates an identifier that consists of this identifier plus a name segment.SqlIdentifier
plusStar()
Creates an identifier that consists of this identifier plus a wildcard star.SqlIdentifier
setName(int i, java.lang.String name)
Returns an identifier that is the same as this except one modified name.void
setNames(java.util.List<java.lang.String> names, java.util.List<SqlParserPos> poses)
Modifies the components of this identifier and their positions.SqlIdentifier
skipLast(int n)
Creates an identifier that consists of all but the lastn
name segments of this one.static SqlIdentifier
star(java.util.List<java.lang.String> names, SqlParserPos pos, java.util.List<SqlParserPos> componentPositions)
Creates an identifier that ends in a wildcard star.static SqlIdentifier
star(SqlParserPos pos)
Creates an identifier that is a singleton wildcard star.static java.util.List<java.lang.String>
toStar(java.util.List<java.lang.String> names)
Converts empty strings in a list of names to stars.java.lang.String
toString()
void
unparse(SqlWriter writer, int leftPrec, int rightPrec)
Writes a SQL representation of this node to a writer.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 org.apache.calcite.sql.SqlNode
clone, clone, cloneArray, equalDeep, equalDeep, equalsDeep, findValidOptions, getParserPosition, isA, toSqlString, toSqlString
-
-
-
-
Field Detail
-
names
public com.google.common.collect.ImmutableList<java.lang.String> names
Array of the components of this compound identifier.The empty string represents the wildcard "*", to distinguish it from a real "*" (presumably specified using quotes).
It's convenient to have this member public, and it's convenient to have this member not-final, but it's a shame it's public and not-final. If you assign to this member, please use
setNames(java.util.List, java.util.List)
. And yes, we'd like to make identifiers immutable one day.
-
collation
final SqlCollation collation
This identifier's collation (if any).
-
componentPositions
protected com.google.common.collect.ImmutableList<SqlParserPos> componentPositions
A list of the positions of the components of compound identifiers.
-
-
Constructor Detail
-
SqlIdentifier
public SqlIdentifier(java.util.List<java.lang.String> names, SqlCollation collation, SqlParserPos pos, java.util.List<SqlParserPos> componentPositions)
Creates a compound identifier, for examplefoo.bar
.- Parameters:
names
- Parts of the identifier, length ≥ 1
-
SqlIdentifier
public SqlIdentifier(java.util.List<java.lang.String> names, SqlParserPos pos)
-
SqlIdentifier
public SqlIdentifier(java.lang.String name, SqlCollation collation, SqlParserPos pos)
Creates a simple identifier, for examplefoo
, with a collation.
-
SqlIdentifier
public SqlIdentifier(java.lang.String name, SqlParserPos pos)
Creates a simple identifier, for examplefoo
.
-
-
Method Detail
-
star
public static SqlIdentifier star(SqlParserPos pos)
Creates an identifier that is a singleton wildcard star.
-
star
public static SqlIdentifier star(java.util.List<java.lang.String> names, SqlParserPos pos, java.util.List<SqlParserPos> componentPositions)
Creates an identifier that ends in a wildcard star.
-
getKind
public SqlKind getKind()
Description copied from class:SqlNode
Returns the type of node this is, orSqlKind.OTHER
if it's nothing special.- Overrides:
getKind
in classSqlNode
- Returns:
- a
SqlKind
value, never null - See Also:
SqlNode.isA(java.util.Set<org.apache.calcite.sql.SqlKind>)
-
clone
public SqlNode clone(SqlParserPos pos)
Description copied from class:SqlNode
Clones a SqlNode with a different position.
-
getString
public static java.lang.String getString(java.util.List<java.lang.String> names)
Converts a list of strings to a qualified identifier.
-
toStar
public static java.util.List<java.lang.String> toStar(java.util.List<java.lang.String> names)
Converts empty strings in a list of names to stars.
-
setNames
public void setNames(java.util.List<java.lang.String> names, java.util.List<SqlParserPos> poses)
Modifies the components of this identifier and their positions.- Parameters:
names
- Names of componentsposes
- Positions of components
-
setName
public SqlIdentifier setName(int i, java.lang.String name)
Returns an identifier that is the same as this except one modified name. Does not modify this identifier.
-
add
public SqlIdentifier add(int i, java.lang.String name, SqlParserPos pos)
Returns an identifier that is the same as this except with a component added at a given position. Does not modify this identifier.
-
getComponentParserPosition
public SqlParserPos getComponentParserPosition(int i)
Returns the position of thei
th component of a compound identifier, or the position of the whole identifier if that information is not present.- Parameters:
i
- Ordinal of component.- Returns:
- Position of i'th component
-
assignNamesFrom
public void assignNamesFrom(SqlIdentifier other)
Copies names and components from another identifier. Does not modify the cross-component parser position.- Parameters:
other
- identifier from which to copy
-
getComponent
public SqlIdentifier getComponent(int ordinal)
Creates an identifier which contains only theordinal
th component of this compound identifier. It will have the correctSqlParserPos
, provided that detailed position information is available.
-
getComponent
public SqlIdentifier getComponent(int from, int to)
-
plus
public SqlIdentifier plus(java.lang.String name, SqlParserPos pos)
Creates an identifier that consists of this identifier plus a name segment. Does not modify this identifier.
-
plusStar
public SqlIdentifier plusStar()
Creates an identifier that consists of this identifier plus a wildcard star. Does not modify this identifier.
-
skipLast
public SqlIdentifier skipLast(int n)
Creates an identifier that consists of all but the lastn
name segments of this one.
-
unparse
public void unparse(SqlWriter writer, int leftPrec, int rightPrec)
Description copied from class:SqlNode
Writes a SQL representation of this node to a writer.The
leftPrec
andrightPrec
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.apache.calcite.sql.SqlWriter, int, int)
.
-
validate
public void validate(SqlValidator validator, SqlValidatorScope scope)
Description copied from class:SqlNode
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.apache.calcite.sql.SqlLiteral)
for these purposes.
-
validateExpr
public void validateExpr(SqlValidator validator, SqlValidatorScope scope)
Description copied from class:SqlNode
Validates this node in an expression context.Usually, this method does much the same as
SqlNode.validate(org.apache.calcite.sql.validate.SqlValidator, org.apache.calcite.sql.validate.SqlValidatorScope)
, but aSqlIdentifier
can occur in expression and non-expression contexts.- Overrides:
validateExpr
in classSqlNode
-
equalsDeep
public boolean equalsDeep(SqlNode node, Litmus litmus)
Description copied from class:SqlNode
Returns whether this node is structurally equivalent to another node. Some examples:- 1 + 2 is structurally equivalent to 1 + 2
- 1 + 2 + 3 is structurally equivalent to (1 + 2) + 3, but not to 1 + (2 + 3), because the '+' operator is left-associative
- Specified by:
equalsDeep
in classSqlNode
-
accept
public <R> R accept(SqlVisitor<R> visitor)
Description copied from class:SqlNode
Accepts a generic visitor.Implementations of this method in subtypes simply call the appropriate
visit
method on thevisitor object
.The type parameter
R
must be consistent with the type parameter of the visitor.
-
getCollation
public SqlCollation getCollation()
-
getSimple
public java.lang.String getSimple()
-
isStar
public boolean isStar()
Returns whether this identifier is a star, such as "*" or "foo.bar.*".
-
isSimple
public boolean isSimple()
Returns whether this is a simple identifier. "FOO" is simple; "*", "FOO.*" and "FOO.BAR" are not.
-
getMonotonicity
public SqlMonotonicity getMonotonicity(SqlValidatorScope scope)
Description copied from class:SqlNode
Returns whether expression is always ascending, descending or constant. This property is useful because it allows to safely aggregate infinite streams of values.The default implementation returns
SqlMonotonicity.NOT_MONOTONIC
.- Overrides:
getMonotonicity
in classSqlNode
- Parameters:
scope
- Scope
-
-