Skip to content

TSelectSqlStatement

Overview

The TSelectSqlStatement node represents a complete SELECT query in the AST tree. It serves as the root container for all components of a SELECT statement including columns, tables, conditions, grouping, and ordering.

SQL to AST Mapping

Simple SELECT Example

1
SELECT id, name FROM users WHERE age > 18;

AST Structure:

1
2
3
4
5
6
7
8
TSelectSqlStatement
├── ResultColumnList
│   ├── TResultColumn (id)
│   └── TResultColumn (name)
├── FromClause
│   └── TTable (users)
└── WhereClause
    └── TExpression (age > 18)

Complex SELECT Example

1
2
3
4
5
6
7
SELECT u.id, u.name, COUNT(o.id) as order_count
FROM users u 
LEFT JOIN orders o ON u.id = o.user_id 
WHERE u.status = 'active'
GROUP BY u.id, u.name
HAVING COUNT(o.id) > 5
ORDER BY order_count DESC;

AST Structure:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
TSelectSqlStatement
├── ResultColumnList
│   ├── TResultColumn (u.id)
│   ├── TResultColumn (u.name)  
│   └── TResultColumn (COUNT(o.id) as order_count)
├── FromClause
│   ├── TTable (users u)
│   └── TJoin (LEFT JOIN orders o ON u.id = o.user_id)
├── WhereClause
│   └── TExpression (u.status = 'active')
├── GroupByClause
│   ├── TExpression (u.id)
│   └── TExpression (u.name)
├── HavingClause
│   └── TExpression (COUNT(o.id) > 5)
└── OrderByClause
    └── TOrderByItem (order_count DESC)

Key Properties and Relationships

Property Type Description Example Usage
resultColumnList TResultColumnList Columns being selected Access with getResultColumnList()
fromClause TFromClause Tables and joins Check if present: getFromClause() != null
whereClause TWhereClause Filter conditions Get condition: getWhereClause().getCondition()
groupByClause TGroupByClause Grouping expressions Iterate: getGroupByClause().getItems()
havingClause THavingClause Post-grouping filters Similar to WHERE clause
orderByClause TOrderByClause Sorting specification Get sort items: getOrderByClause().getItems()

Common Usage Patterns

1. Extracting All Referenced Tables

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
// Get all tables referenced in the SELECT statement
TSelectSqlStatement selectStmt = // ... parsed statement
Set<String> tableNames = new HashSet<>();

// From main FROM clause
if (selectStmt.getFromClause() != null) {
    collectTableNames(selectStmt.getFromClause(), tableNames);
}

// From subqueries in SELECT list
for (int i = 0; i < selectStmt.getResultColumnList().size(); i++) {
    TResultColumn column = selectStmt.getResultColumnList().getResultColumn(i);
    if (column.getExpr().getSubQuery() != null) {
        // Handle subquery...
    }
}

2. Analyzing Filter Conditions

1
2
3
4
5
6
// Extract all WHERE conditions
TSelectSqlStatement selectStmt = // ... parsed statement
if (selectStmt.getWhereClause() != null) {
    TExpression whereCondition = selectStmt.getWhereClause().getCondition();
    analyzeCondition(whereCondition); // Your analysis logic
}

3. Column Dependency Analysis

1
2
3
4
5
6
7
8
9
// Find which columns depend on which tables
TSelectSqlStatement selectStmt = // ... parsed statement
Map<String, Set<String>> columnToTables = new HashMap<>();

for (int i = 0; i < selectStmt.getResultColumnList().size(); i++) {
    TResultColumn column = selectStmt.getResultColumnList().getResultColumn(i);
    Set<String> referencedTables = extractTableReferences(column.getExpr());
    columnToTables.put(column.getExpr().toString(), referencedTables);
}
  • Parent Nodes: TSqlStatement, TCustomSqlStatement
  • Child Nodes: TResultColumnList, TFromClause, TWhereClause, TGroupByClause, THavingClause, TOrderByClause
  • Expression Nodes: TExpression, TFunctionCall, TConstant
  • Table Reference Nodes: TTable, TJoin, TTableList

Database-Specific Considerations

SQL Server T-SQL

1
SELECT TOP 10 * FROM users;  -- TOP clause
- Access via: selectStmt.getTopClause()

Oracle

1
SELECT * FROM users WHERE ROWNUM <= 10;  -- ROWNUM in WHERE
- ROWNUM appears as regular expression in WHERE clause

PostgreSQL

1
SELECT * FROM users LIMIT 10;  -- LIMIT clause  
- Access via: selectStmt.getLimitClause()

Performance Tips

  1. Lazy Evaluation: Only traverse nodes you need
  2. Caching: Cache frequently accessed node properties
  3. Null Checks: Always check if optional clauses exist before accessing
  4. Memory: Be mindful of memory usage with large AST trees

See Also