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
Numeric Literals
Temporal Literals
Special Constants
Constant Structure
TConstant Properties
| TConstant
├── getLiteralType() -> ELiteralType
├── getValue() -> String (original text)
├── getStringValue() -> String
├── getIntegerValue() -> Integer (if integral)
└── getValueToken() -> TSourceToken (source position)
|
Constant Types
| Type |
Description |
Example |
etString |
String literal |
'Hello World' |
etNumber |
Integer number |
42 |
etFloat |
Floating point |
3.14159 |
etDate |
Date literal |
DATE '2023-01-01' |
etTime |
Time literal |
TIME '14:30:00' |
etTimestamp |
Timestamp literal |
TIMESTAMP '2023-01-01 14:30:00' |
null_et |
NULL value |
NULL |
etTrue / etFalse |
Boolean value |
TRUE, FALSE |
Common Usage Patterns
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
26
27
28 | public Object extractTypedValue(TConstant constant) {
switch (constant.getLiteralType()) {
case etString:
return constant.getStringValue();
case etNumber:
return constant.getIntegerValue();
case etFloat:
return Double.valueOf(constant.getValue());
case etDate:
// Parse date string according to database format
return parseDate(constant.getValue());
case null_et:
return null;
case etTrue:
return Boolean.TRUE;
case etFalse:
return Boolean.FALSE;
default:
return constant.getValue();
}
}
|
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.getLiteralType()) {
case etString:
return validateStringLiteral(constant.getStringValue());
case etNumber:
return validateIntegerRange(constant.getIntegerValue());
case etFloat:
return validateFloatPrecision(Double.valueOf(constant.getValue()));
case etDate:
return validateDateFormat(constant.getValue());
default:
return true;
}
}
|
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
Constant Folding
| // The parser may pre-evaluate constant expressions
// Example: 2 + 3 might be stored as constant 5
|
Parameter Binding
| // Consider parameterizing frequently-used literals
// For better query plan reuse
|
See Also
- Basic Expressions - Expressions containing literals
- String Literals - Detailed string handling
- Numeric Literals - Number format specifics
- Date Literals - Date and time formats