Skip to content

Columns and Lists Nodes

Column and List nodes represent collections of data elements in SQL statements, including SELECT column lists, INSERT value lists, and parameter collections. They handle both individual items and their container structures.

Overview

These nodes manage ordered collections of SQL elements. They provide indexing, iteration, and manipulation capabilities for lists of columns, values, parameters, and other SQL constructs.

Column and List Categories

Result and Column Lists

  • Result Columns - TResultColumn, TResultColumnList
  • SELECT clause column specifications
  • Column aliases and expressions
  • Aggregate function handling

  • Column References - TObjectName

  • Qualified and unqualified column names
  • Table prefixes and schema qualification
  • Column alias resolution

Value Collections

  • Value Lists - TValuesList, TMultiTarget
  • INSERT VALUES clauses
  • Multi-row value specifications
  • Expression lists in various contexts

  • Parameter Lists - TParameterList

  • Function parameter collections
  • Procedure argument lists
  • Type specifications

Specialized Lists

  • Group By Items - TGroupByItemList
  • GROUP BY expression collections
  • Grouping sets and rollup operations
  • Position-based grouping

  • Order By Items - TOrderByItemList

  • Sort specification collections
  • Ascending/descending indicators
  • NULL ordering preferences

List Structure Patterns

Generic List Pattern

1
2
3
4
5
TListType
├── size() -> int
├── getItem(index) -> TItemType
├── addItem(item) -> void
└── iterator() -> Iterator<TItemType>

Result Column List Example

1
2
3
4
5
6
7
TResultColumnList
├── size() -> int
├── getResultColumn(index) -> TResultColumn
└── TResultColumn
    ├── expr -> TExpression
    ├── aliasClause -> TObjectName
    └── fieldAttr -> field attributes

Common Usage Patterns

Iterating Column Lists

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
TSelectSqlStatement select = // ... parsed statement
TResultColumnList columns = select.getResultColumnList();

for (int i = 0; i < columns.size(); i++) {
    TResultColumn column = columns.getResultColumn(i);

    // Get column expression
    TExpression expr = column.getExpr();

    // Get column alias (if any)
    TObjectName alias = column.getAliasClause();
    String aliasName = alias != null ? alias.toString() : null;

    // Process column
    processColumn(expr, aliasName);
}

Analyzing Value Lists

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
TInsertSqlStatement insert = // ... parsed statement
TMultiTarget multiTarget = insert.getValues();

if (multiTarget != null) {
    for (int i = 0; i < multiTarget.size(); i++) {
        TValuesList valuesList = multiTarget.getMultiTarget(i);

        for (int j = 0; j < valuesList.size(); j++) {
            TExpression value = valuesList.getExpression(j);
            // Process each value
            processValue(value);
        }
    }
}

Building Column Mappings

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
public Map<String, TExpression> buildColumnMappings(TResultColumnList columns) {
    Map<String, TExpression> mappings = new HashMap<>();

    for (int i = 0; i < columns.size(); i++) {
        TResultColumn column = columns.getResultColumn(i);
        TExpression expr = column.getExpr();

        // Use alias if available, otherwise derive from expression
        String columnName;
        if (column.getAliasClause() != null) {
            columnName = column.getAliasClause().toString();
        } else {
            columnName = deriveColumnName(expr);
        }

        mappings.put(columnName, expr);
    }

    return mappings;
}

List Types and Operations

Result Column Operations

1
2
3
4
5
6
7
8
// Access specific column
TResultColumn firstColumn = resultList.getResultColumn(0);

// Check for SELECT *
boolean hasSelectAll = resultList.isSelectAll();

// Get column count
int columnCount = resultList.size();

Value List Operations

1
2
3
4
5
6
7
// Multi-row INSERT values
TMultiTarget multiTarget = insert.getValues();
int rowCount = multiTarget.size();

// Single row values
TValuesList singleRow = multiTarget.getMultiTarget(0);
int valueCount = singleRow.size();

Parameter List Operations

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
// Function parameters
TFunctionCall func = expr.getFunctionCall();
TParameterList params = func.getArgs();

if (params != null) {
    for (int i = 0; i < params.size(); i++) {
        TExpression param = params.getExpression(i);
        processParameter(param);
    }
}

Column Alias Handling

Explicit Aliases

1
SELECT customer_id AS id, customer_name AS name FROM customers;
1
2
3
TResultColumn column = columns.getResultColumn(0);
TObjectName alias = column.getAliasClause(); // "id"
TExpression expr = column.getExpr();         // "customer_id"

Implicit Column Names

1
SELECT customer_id, COUNT(*) FROM customers;
1
2
// First column: uses original column name
// Second column: derived name like "COUNT(*)" or system-generated

Database-Specific Considerations

SQL Server

  • OUTPUT clause result lists
  • Table-valued parameter lists
  • MERGE statement target lists

PostgreSQL

  • RETURNING clause result lists
  • Array constructor syntax
  • Row constructor expressions

Oracle

  • Collection types in parameter lists
  • Bulk collect operations
  • Object type constructors

MySQL

  • ON DUPLICATE KEY UPDATE value lists
  • Multi-table UPDATE target lists
  • Prepared statement parameter markers

Performance Tips

  1. List Size: Check list size before iteration to avoid empty collections
  2. Index Access: Use direct index access when possible rather than iteration
  3. Null Checks: Always verify list existence before accessing items
  4. Memory: Be aware of large lists in complex queries

See Also