Class TIndices
- All Implemented Interfaces:
Visitable,Iterator<TSourceToken>
Array Access Syntax (ARRAY_ACCESS)
Used for accessing array elements and slicing operations:array_column[5]- Single element accessarray_column[2:4]- Array slice from index 2 to 4array_column[:3]- Array slice from start to index 3array_column[2:]- Array slice from index 2 to endarray_column[:]- Full array slice
Qualified Name Syntax (QUALIFIED_NAME)
Used for accessing object fields and properties:json_column.field_name- Access specific fieldrecord_column.*- Access all fieldscomposite_type.attribute- Access composite type attribute
Usage Examples
// Example SQL: SELECT users[1].name FROM user_array;
// This creates two TIndices:
// 1. TIndices for [1] with type ARRAY_ACCESS
// 2. TIndices for .name with type QUALIFIED_NAME
TIndices arrayAccess = ...; // represents [1]
if (arrayAccess.isArrayAccess()) {
TExpression index = arrayAccess.getLowerSubscript(); // gets "1"
// Process array index...
}
TIndices fieldAccess = ...; // represents .name
if (fieldAccess.isQualifiedName()) {
TObjectName fieldName = fieldAccess.getAttributeName(); // gets "name"
// Process field access...
}
SQL Examples by Database
| Database | Array Access | Field Access |
|---|---|---|
| PostgreSQL | arr[1], arr[1:3] | json_col.field, record.* |
| Databricks | array_col[0] | struct_col.attr, json.key |
| Snowflake | array_col[0] | variant_col.field, object.* |
Type Safety and Backward Compatibility
This class maintains backward compatibility with existing code while providing type-safe access:isRealIndices()- Legacy method, still works as beforeisArrayAccess()- New type-safe method for array accessisQualifiedName()- New type-safe method for field accessgetIndicesType()- Get explicit type information
Complete Usage Example
// Processing SQL: SELECT data[1:3].users.name FROM complex_table;
// This would create a chain of TIndices in TIndirection
public void processIndirection(TIndirection indirection) {
for (TIndices indices : indirection.getIndices()) {
switch (indices.getIndicesType()) {
case ARRAY_ACCESS:
System.out.println("Array access detected:");
TExpression lower = indices.getLowerSubscript();
TExpression upper = indices.getUpperSubscript();
if (lower != null && upper != null) {
System.out.println(" Slice: [" + lower + ":" + upper + "]");
} else if (lower != null) {
System.out.println(" Single element: [" + lower + "]");
}
break;
case QUALIFIED_NAME:
System.out.println("Field access detected:");
TObjectName field = indices.getAttributeName();
System.out.println(" Field: ." + field.toString());
break;
}
}
}
// Legacy compatibility - existing code still works
if (indices.isRealIndices()) {
// Handle as array access (legacy approach)
} else {
// Handle as field access (legacy approach)
}
- Since:
- PostgreSQL parser support
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumEnum to distinguish between array access syntax and qualified name syntax. -
Field Summary
Fields inherited from class gudusoft.gsqlparser.nodes.TParseTreeNode
dbvendor, doubleLinkedTokenListToString, nodeActionAppend, nodeActionInsert, nodeActionRemove, nodeActionUnknown, nodeActionUpdate, nodeActionUpdateText, nodeChangeEndToken, nodeChangeStartToken -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidAccept a visitorvoidAccept a visitor to iterate this class and sub-nodes of this classvoidaddSubscript(TExpression expr) static voidaddSubscript(ArrayList<TIndices> indicesArrayList, TExpression expr) Gets the attribute name for qualified name access patterns.Gets the explicit type of this indices element.Gets the lower bound expression for array access patterns.Gets the upper bound expression for array slice patterns.voidInitializes this TIndices with the specified components.booleanChecks if this represents array access syntax using bracket notation.booleanChecks if this represents qualified name syntax using dot notation.booleanDeprecated.booleanisSlice()Checks if this represents an array slice operation (contains colon syntax).voidsetAttributeName(TObjectName attributeName) Sets the attribute name for qualified name access operations.voidsetIndicesType(TIndices.IndicesType indicesType) Sets the explicit type of this indices element.voidsetLowerSubscript(TExpression lowerSubscript) Sets the lower bound expression for array access operations.voidsetSlice(boolean isSlice) Sets whether this represents an array slice operation.voidsetUpperSubscript(TExpression upperSubscript) Sets the upper bound expression for array slice operations.Methods inherited from class gudusoft.gsqlparser.nodes.TParseTreeNode
addAllMyTokensToTokenList, addToTokenChain, appendNewNode, calculateTokenCount, doAppendNewNode, doParse, fastSetString, getAnchorNode, getColumnNo, getCommentAfterNode, getCommentBeforeNode, getCompactString, getDummyTag, getEndToken, getEvaluateDatatype, getEvalValue, getGsqlparser, getLineNo, getLocation, getMd5, getNodeStatus, getNodeType, getParentObjectName, getPlainText, getStartToken, getTokenCount, hasNext, init, init, init, init, init, insertAfterAToken, insertNewNodeBeforeMe, isChanged, isTokensInChain, next, refreshAllNodesTokenCount, remove, removeAllMyTokensFromTokenList, removeTokens, removeTokensBetweenNodes, removeTokensBetweenToken, replaceWithNewNode, resetIterator, setAnchorNode, setChanged, setDummyTag, setEndToken, setEndToken, setEndToken, setEndToken, setEndToken, setEndTokenDirectly, setEvaluateDatatype, setEvalValue, setGsqlparser, setIncludingComment, setLocation, setNewSubNode, setNodeStatus, setNodeType, setParent, setPlainText, setStartToken, setStartToken, setStartToken, setStartToken, setStartTokenDirectly, setString, setString2, subNodeInNode, toScript, toString, toString2Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Iterator
forEachRemaining
-
Constructor Details
-
TIndices
public TIndices()
-
-
Method Details
-
getAttributeName
Gets the attribute name for qualified name access patterns. This is only populated for QUALIFIED_NAME type indices (e.g., .field, .*).Examples:
- For
json_col.user_name→ returns "user_name" - For
record.*→ returns "*" - For
array[5]→ returns null (not a qualified name)
- Returns:
- the attribute/field name for qualified access, or null for array access
- See Also:
- For
-
getLowerSubscript
Gets the lower bound expression for array access patterns. This represents the starting index or single index in array access operations.Examples:
- For
arr[5]→ returns expression "5" - For
arr[2:8]→ returns expression "2" - For
arr[:3]→ returns null (no lower bound) - For
obj.field→ returns null (not array access)
- Returns:
- the lower bound expression, or null if not applicable
- See Also:
- For
-
getUpperSubscript
Gets the upper bound expression for array slice patterns. This represents the ending index in array slice operations (e.g., [start:end]).Examples:
- For
arr[2:8]→ returns expression "8" - For
arr[:3]→ returns expression "3" - For
arr[5]→ returns null (single element, not slice) - For
arr[2:]→ returns null (no upper bound)
- Returns:
- the upper bound expression for slices, or null if not a slice or no upper bound
- See Also:
- For
-
isRealIndices
Deprecated.UseisArrayAccess()for better code clarityLegacy method to check if this represents "real" array indices.Note: This method is maintained for backward compatibility. For new code, prefer using
isArrayAccess()andisQualifiedName()for clearer semantic meaning.Returns true for array access patterns and false for qualified name patterns:
arr[5]→ true (real array indices)arr[2:4]→ true (real array slice)obj.field→ false (not real indices, just field access)
- Returns:
- true if this represents array access, false for qualified name access
-
getIndicesType
Gets the explicit type of this indices element.This method returns the semantic type of the indirection, helping distinguish between array access operations and qualified name field access. The method includes fallback logic for backward compatibility with legacy code.
Type determination logic:
- If explicit type was set via
setIndicesType(IndicesType), return that - Otherwise, infer from legacy fields: attributeName present → QUALIFIED_NAME, absent → ARRAY_ACCESS
- Returns:
TIndices.IndicesType.ARRAY_ACCESSfor bracket notation orTIndices.IndicesType.QUALIFIED_NAMEfor dot notation- See Also:
- If explicit type was set via
-
setIndicesType
Sets the explicit type of this indices element.This method is typically called by the parser to explicitly mark whether an indices element represents array access or qualified name access.
- Parameters:
indicesType- the semantic type to set -TIndices.IndicesType.ARRAY_ACCESSfor bracket notation orTIndices.IndicesType.QUALIFIED_NAMEfor dot notation- See Also:
-
isSlice
Checks if this represents an array slice operation (contains colon syntax).Returns true for slice patterns that contain a colon:
arr[2:8]- Slice with both boundsarr[:3]- Slice with upper bound onlyarr[2:]- Slice with lower bound onlyarr[:]- Full slice
Returns false for single element access:
arr[5]- Single element access (no colon)
- Returns:
- true if this represents a slice operation with colon syntax
- See Also:
-
setSlice
Sets whether this represents an array slice operation.This method is typically called by the parser to indicate whether the original syntax contained a colon (slice) or not (single element).
- Parameters:
isSlice- true if this represents slice syntax with colon, false for single element- See Also:
-
isArrayAccess
Checks if this represents array access syntax using bracket notation.Returns true for all forms of array/list access patterns:
arr[5]- Single element accessarr[2:8]- Array slice with both boundsarr[:3]- Array slice to index 3arr[2:]- Array slice from index 2arr[:]- Full array slice
This is the preferred method for type-safe checking of array access patterns.
- Returns:
- true if this represents array access using bracket notation
- See Also:
-
isQualifiedName
Checks if this represents qualified name syntax using dot notation.Returns true for all forms of field/property access patterns:
obj.field_name- Access specific fieldjson_col.key- Access JSON object keyrecord.*- Access all fields (wildcard)composite_type.attribute- Access composite type attribute
This is the preferred method for type-safe checking of qualified name patterns.
- Returns:
- true if this represents qualified name access using dot notation
- See Also:
-
init
Initializes this TIndices with the specified components.This method is typically called by the parser during AST construction. The parameters determine the type and content of the indices element:
For Array Access: arg1=null, arg2=lowerBound, arg3=upperBound
For Qualified Names: arg1=attributeName, arg2=null, arg3=null
- Overrides:
initin classTParseTreeNode- Parameters:
arg1- attribute name for qualified access, or null for array accessarg2- lower bound expression for array access, or null for qualified namesarg3- upper bound expression for array slices, or null for single elements or qualified names
-
setLowerSubscript
Sets the lower bound expression for array access operations.This represents the starting index in array access patterns:
- For
[5]→ set to expression "5" - For
[2:8]→ set to expression "2" - For
[:3]→ set to null (no lower bound)
- Parameters:
lowerSubscript- the expression representing the lower bound, or null- See Also:
- For
-
setUpperSubscript
Sets the upper bound expression for array slice operations.This represents the ending index in array slice patterns:
- For
[2:8]→ set to expression "8" - For
[:3]→ set to expression "3" - For
[5]→ set to null (single element, not a slice)
- Parameters:
upperSubscript- the expression representing the upper bound, or null- See Also:
- For
-
setAttributeName
Sets the attribute name for qualified name access operations.This represents the field or property name in dot notation patterns:
- For
.field_name→ set to "field_name" - For
.*→ set to "*" - For array access → should be null
- Parameters:
attributeName- the object name representing the field/property, or null for array access- See Also:
- For
-
getSubscriptList
-
addSubscript
-
addSubscript
-
accept
Description copied from class:TParseTreeNodeAccept a visitor- Specified by:
acceptin interfaceVisitable- Overrides:
acceptin classTParseTreeNode- Parameters:
v- visitor is a descendant class ofTParseTreeVisitor
-
acceptChildren
Description copied from class:TParseTreeNodeAccept a visitor to iterate this class and sub-nodes of this class- Specified by:
acceptChildrenin interfaceVisitable- Overrides:
acceptChildrenin classTParseTreeNode- Parameters:
v- visitor is a descendant class ofTParseTreeVisitor
-
isArrayAccess()for better code clarity