Skip to content

Literals and Constants Nodes

Literal and Constant nodes represent fixed values in SQL statements, including numbers, strings, dates, and special constants like NULL. They form the basic data elements that don't require computation or lookup.

Overview

Literal nodes represent constant values that appear directly in SQL text. They are leaf nodes in the AST tree and provide methods to access their typed values and original string representations.

Literal Categories

Textual Literals

  • String Literals - TConstant
  • Quoted string values
  • Character encoding handling
  • Escape sequence processing

  • Character Literals

  • Single character constants
  • National character sets
  • Database-specific character types

Numeric Literals

  • Numeric Literals - TConstant
  • Integer values
  • Decimal and floating-point numbers
  • Scientific notation
  • Precision and scale handling

  • Binary Literals

  • Hexadecimal notation (0x...)
  • Binary notation (0b...)
  • Bit string literals

Temporal Literals

  • Date Literals - TConstant
  • Date constants
  • Time constants
  • Timestamp values
  • Database-specific date formats

  • Interval Literals

  • Time interval specifications
  • Day-time and year-month intervals
  • Database-specific interval syntax

Special Constants

  • NULL Values - TConstant
  • NULL constant handling
  • Typed NULL specifications
  • Three-valued logic implications

  • Boolean Literals

  • TRUE and FALSE constants
  • Database-specific boolean syntax
  • Boolean expression evaluation

Constant Structure

TConstant Properties

1
2
3
4
5
6
7
TConstant
├── constantType -> EConstantType
├── string -> String (original text)
├── asInteger -> Integer (if numeric)
├── asLong -> Long (if numeric)
├── asDouble -> Double (if numeric)
└── asBigDecimal -> BigDecimal (if numeric)

Constant Types

Type Description Example
ctString String literal 'Hello World'
ctInteger Integer number 42
ctFloat Floating point 3.14159
ctDate Date literal DATE '2023-01-01'
ctTime Time literal TIME '14:30:00'
ctNull NULL value NULL
ctBoolean Boolean value TRUE, FALSE

Common Usage Patterns

Type-Safe Value Extraction

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public Object extractTypedValue(TConstant constant) {
    switch (constant.getConstantType()) {
        case ctString:
            return constant.getString();

        case ctInteger:
            return constant.asInteger();

        case ctFloat:
            return constant.asDouble();

        case ctDate:
            // Parse date string according to database format
            return parseDate(constant.getString());

        case ctNull:
            return null;

        case ctBoolean:
            return constant.getString().equalsIgnoreCase("TRUE");

        default:
            return constant.getString();
    }
}

Finding All Literals in Expression

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
public List<TConstant> findAllLiterals(TExpression expr) {
    List<TConstant> literals = new ArrayList<>();

    expr.postOrderTraverse(new IExpressionVisitor() {
        public boolean exprVisit(TParseTreeNode node, boolean isLeafNode) {
            if (node instanceof TExpression) {
                TExpression e = (TExpression) node;
                if (e.getConstantOperand() != null) {
                    literals.add(e.getConstantOperand());
                }
            }
            return true;
        }
    });

    return literals;
}

Constant Value Validation

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
public boolean validateConstant(TConstant constant) {
    switch (constant.getConstantType()) {
        case ctString:
            return validateStringLiteral(constant.getString());

        case ctInteger:
            return validateIntegerRange(constant.asLong());

        case ctFloat:
            return validateFloatPrecision(constant.asDouble());

        case ctDate:
            return validateDateFormat(constant.getString());

        default:
            return true;
    }
}

Database-Specific Literal Formats

SQL Server

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
-- String literals
'Standard string'
N'Unicode string'

-- Numeric literals
123
123.45
1.23E4

-- Date literals
'2023-01-01'
'14:30:00'
'2023-01-01 14:30:00'

PostgreSQL

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
-- String literals
'Standard string'
E'String with escapes\n'
$$Dollar quoted string$$

-- Numeric literals
123
123.45
1.23E4

-- Date literals
DATE '2023-01-01'
TIME '14:30:00'
TIMESTAMP '2023-01-01 14:30:00'

Oracle

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
-- String literals
'Standard string'
N'National character string'
Q'[Quoted string with special chars]'

-- Numeric literals
123
123.45
1.23E4

-- Date literals
DATE '2023-01-01'
TIMESTAMP '2023-01-01 14:30:00'
INTERVAL '5' DAY

MySQL

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
-- String literals
'Standard string'
"Double quoted string"
`Backtick identifier`

-- Numeric literals
123
123.45
1.23E4

-- Date literals
'2023-01-01'
'14:30:00'
'2023-01-01 14:30:00'

Literal Processing Considerations

String Handling

  • Quote character escaping
  • Unicode and character set handling
  • Collation implications
  • Maximum string length limits

Numeric Precision

  • Integer overflow detection
  • Floating-point precision loss
  • Decimal scale preservation
  • Scientific notation normalization

Date and Time

  • Format string variations
  • Timezone handling
  • Leap year considerations
  • Database-specific date functions

NULL Handling

  • Three-valued logic (TRUE, FALSE, UNKNOWN)
  • NULL propagation in expressions
  • IS NULL vs = NULL semantics
  • COALESCE and ISNULL functions

Performance Implications

Constant Folding

1
2
// The parser may pre-evaluate constant expressions
// Example: 2 + 3 might be stored as constant 5

Parameter Binding

1
2
// Consider parameterizing frequently-used literals
// For better query plan reuse

See Also