Skip to content

Expression Nodes

Expression nodes represent computational elements in SQL statements, including mathematical operations, logical conditions, function calls, and value comparisons. They form the building blocks of SQL logic.

Overview

Expression nodes inherit from TExpression and represent any SQL element that evaluates to a value. They can be simple literals, column references, or complex nested expressions involving multiple operations.

Expression Categories

Basic Elements

  • Basic Expressions - TExpression
  • Core expression concepts
  • Expression types and structure
  • Navigation and traversal patterns

  • Column References - TObjectName

  • Table and column identifiers
  • Qualified and unqualified names
  • Alias handling

Operations and Functions

  • Arithmetic Expressions
  • Mathematical operations (+, -, *, /, %)
  • Operator precedence
  • Numeric type handling

  • Logical Expressions

  • Boolean operations (AND, OR, NOT)
  • Comparison operators (=, <>, <, >, <=, >=)
  • Pattern matching (LIKE, REGEXP)

  • Function Calls - TFunctionCall

  • Built-in SQL functions
  • User-defined functions
  • Aggregate functions
  • Window functions

Conditional Logic

  • CASE Expressions - TCaseExpression
  • Simple and searched CASE
  • Conditional value selection
  • NULL handling patterns

  • Subquery Expressions

  • Scalar subqueries
  • EXISTS conditions
  • IN/NOT IN with subqueries

Advanced Constructs

Expression Types

The TExpression class uses EExpressionType to distinguish between different expression types:

Type Description Example
simple_object_name_t Column/table reference users.id
function_t Function call COUNT(*)
arithmetic_plus_t Addition a + b
arithmetic_minus_t Subtraction a - b
logical_and_t AND operation a AND b
logical_or_t OR operation a OR b
comparison_eq_t Equality a = b
comparison_ne_t Inequality a <> b
parenthesis_t Parenthesized (expression)
case_t CASE expression CASE WHEN ... END

Common Usage Patterns

Expression Type Identification

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
TExpression expr = // ... some expression
switch (expr.getExpressionType()) {
    case simple_object_name_t:
        // Handle column reference
        String columnName = expr.getObjectOperand().toString();
        break;
    case function_t:
        // Handle function call
        TFunctionCall func = expr.getFunctionCall();
        break;
    case arithmetic_plus_t:
        // Handle addition
        TExpression left = expr.getLeftOperand();
        TExpression right = expr.getRightOperand();
        break;
    // ... other cases
}

Expression Tree Traversal

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
public void traverseExpression(TExpression expr) {
    // Pre-order processing
    processExpression(expr);

    // Traverse child expressions
    if (expr.getLeftOperand() != null) {
        traverseExpression(expr.getLeftOperand());
    }
    if (expr.getRightOperand() != null) {
        traverseExpression(expr.getRightOperand());
    }

    // Special handling for function parameters
    if (expr.getFunctionCall() != null) {
        TParameterList params = expr.getFunctionCall().getArgs();
        if (params != null) {
            for (int i = 0; i < params.size(); i++) {
                traverseExpression(params.getExpression(i));
            }
        }
    }
}

Finding Column References

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
public Set<String> getReferencedColumns(TExpression expr) {
    Set<String> columns = new HashSet<>();

    expr.postOrderTraverse(new IExpressionVisitor() {
        public boolean exprVisit(TParseTreeNode pNode, boolean isLeafNode) {
            if (pNode instanceof TExpression) {
                TExpression e = (TExpression) pNode;
                if (e.getExpressionType() == EExpressionType.simple_object_name_t) {
                    columns.add(e.getObjectOperand().toString());
                }
            }
            return true;
        }
    });

    return columns;
}

Expression Context

Expressions appear in various SQL contexts:

  • SELECT lists: Column values and calculations
  • WHERE clauses: Filter conditions
  • JOIN conditions: Table relationship criteria
  • GROUP BY: Grouping specifications
  • ORDER BY: Sort criteria
  • HAVING: Aggregate filter conditions
  • CASE statements: Conditional logic

Database-Specific Features

SQL Server

  • IIF() function for inline conditionals
  • CHOOSE() function for indexed selection
  • TRY_CAST() and TRY_CONVERT() functions

PostgreSQL

  • Array operations and indexing
  • JSON/JSONB path operators
  • Regular expression operators (~, ~*)

Oracle

  • DECODE() function
  • Hierarchical query operators (PRIOR, CONNECT_BY_ROOT)
  • Advanced analytic functions

MySQL

  • IF() function
  • Regular expression functions (REGEXP, RLIKE)
  • JSON functions and operators

See Also