polyglot-sql 0.3.3

SQL parsing, validating, formatting, and dialect translation library
Documentation
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
//! Teradata Dialect
//!
//! Teradata-specific transformations based on sqlglot patterns.
//! Teradata has unique syntax including ** for exponentiation,
//! TOP instead of LIMIT, and TRYCAST for safe casting.

use super::{DialectImpl, DialectType};
use crate::error::Result;
use crate::expressions::{AggFunc, Case, Cast, Expression, Function, UnaryFunc, VarArgFunc};
use crate::generator::GeneratorConfig;
use crate::tokens::TokenizerConfig;

/// Teradata dialect
pub struct TeradataDialect;

impl DialectImpl for TeradataDialect {
    fn dialect_type(&self) -> DialectType {
        DialectType::Teradata
    }

    fn tokenizer_config(&self) -> TokenizerConfig {
        let mut config = TokenizerConfig::default();
        // Teradata uses double quotes for identifiers
        config.identifiers.insert('"', '"');
        // Teradata does NOT support nested comments
        config.nested_comments = false;
        // Teradata-specific keywords and operators
        config
            .keywords
            .insert("SEL".to_string(), crate::tokens::TokenType::Select);
        config
            .keywords
            .insert("UPD".to_string(), crate::tokens::TokenType::Update);
        config
            .keywords
            .insert("DEL".to_string(), crate::tokens::TokenType::Delete);
        config
            .keywords
            .insert("INS".to_string(), crate::tokens::TokenType::Insert);
        config
            .keywords
            .insert("SAMPLE".to_string(), crate::tokens::TokenType::Sample);
        config
            .keywords
            .insert("LOCKING".to_string(), crate::tokens::TokenType::Lock);
        config
            .keywords
            .insert("HELP".to_string(), crate::tokens::TokenType::Command);
        config
            .keywords
            .insert("COLLECT".to_string(), crate::tokens::TokenType::Command);
        config
            .keywords
            .insert("EQ".to_string(), crate::tokens::TokenType::Eq);
        config
            .keywords
            .insert("NE".to_string(), crate::tokens::TokenType::Neq);
        config
            .keywords
            .insert("GE".to_string(), crate::tokens::TokenType::Gte);
        config
            .keywords
            .insert("GT".to_string(), crate::tokens::TokenType::Gt);
        config
            .keywords
            .insert("LE".to_string(), crate::tokens::TokenType::Lte);
        config
            .keywords
            .insert("LT".to_string(), crate::tokens::TokenType::Lt);
        config
            .keywords
            .insert("MOD".to_string(), crate::tokens::TokenType::Mod);
        config
            .keywords
            .insert("BYTEINT".to_string(), crate::tokens::TokenType::SmallInt);
        config.keywords.insert(
            "ST_GEOMETRY".to_string(),
            crate::tokens::TokenType::Geometry,
        );
        // Teradata does not support % as modulo operator
        config.single_tokens.remove(&'%');
        // Teradata treats 0x prefix as hex string literals
        config.hex_number_strings = true;
        config
    }

    fn generator_config(&self) -> GeneratorConfig {
        use crate::generator::IdentifierQuoteStyle;
        GeneratorConfig {
            identifier_quote: '"',
            identifier_quote_style: IdentifierQuoteStyle::DOUBLE_QUOTE,
            dialect: Some(DialectType::Teradata),
            tablesample_keywords: "SAMPLE",
            tablesample_requires_parens: false,
            tz_to_with_time_zone: true,
            ..Default::default()
        }
    }

    fn transform_expr(&self, expr: Expression) -> Result<Expression> {
        match expr {
            // IFNULL -> COALESCE in Teradata
            Expression::IfNull(f) => Ok(Expression::Coalesce(Box::new(VarArgFunc {
                original_name: None,
                expressions: vec![f.this, f.expression],
                inferred_type: None,
            }))),

            // NVL -> COALESCE in Teradata
            Expression::Nvl(f) => Ok(Expression::Coalesce(Box::new(VarArgFunc {
                original_name: None,
                expressions: vec![f.this, f.expression],
                inferred_type: None,
            }))),

            // Coalesce with original_name (e.g., IFNULL parsed as Coalesce) -> clear original_name
            Expression::Coalesce(mut f) => {
                f.original_name = None;
                Ok(Expression::Coalesce(f))
            }

            // TryCast -> TRYCAST in Teradata (native)
            Expression::TryCast(c) => Ok(Expression::TryCast(c)),

            // SafeCast -> TRYCAST in Teradata
            Expression::SafeCast(c) => Ok(Expression::TryCast(c)),

            // CountIf -> SUM(CASE WHEN condition THEN 1 ELSE 0 END)
            Expression::CountIf(f) => {
                let case_expr = Expression::Case(Box::new(Case {
                    operand: None,
                    whens: vec![(f.this.clone(), Expression::number(1))],
                    else_: Some(Expression::number(0)),
                    comments: Vec::new(),
                    inferred_type: None,
                }));
                Ok(Expression::Sum(Box::new(AggFunc {
                    ignore_nulls: None,
                    having_max: None,
                    this: case_expr,
                    distinct: f.distinct,
                    filter: f.filter,
                    order_by: Vec::new(),
                    name: None,
                    limit: None,
                    inferred_type: None,
                })))
            }

            // RAND -> RANDOM in Teradata (but preserve lower/upper for RANDOM(l, u))
            Expression::Rand(r) => {
                if r.lower.is_some() || r.upper.is_some() {
                    // Keep as Rand with lower/upper for Teradata RANDOM(l, u)
                    Ok(Expression::Rand(r))
                } else {
                    Ok(Expression::Random(crate::expressions::Random))
                }
            }

            // Generic function transformations
            Expression::Function(f) => self.transform_function(*f),

            // Generic aggregate function transformations
            Expression::AggregateFunction(f) => self.transform_aggregate_function(f),

            // Cast transformations
            Expression::Cast(c) => self.transform_cast(*c),

            // Pass through everything else
            _ => Ok(expr),
        }
    }
}

impl TeradataDialect {
    fn transform_function(&self, f: Function) -> Result<Expression> {
        let name_upper = f.name.to_uppercase();
        match name_upper.as_str() {
            // IFNULL -> COALESCE
            "IFNULL" if f.args.len() == 2 => Ok(Expression::Coalesce(Box::new(VarArgFunc {
                original_name: None,
                expressions: f.args,
                inferred_type: None,
            }))),

            // NVL -> COALESCE
            "NVL" if f.args.len() == 2 => Ok(Expression::Coalesce(Box::new(VarArgFunc {
                original_name: None,
                expressions: f.args,
                inferred_type: None,
            }))),

            // ISNULL -> COALESCE
            "ISNULL" if f.args.len() == 2 => Ok(Expression::Coalesce(Box::new(VarArgFunc {
                original_name: None,
                expressions: f.args,
                inferred_type: None,
            }))),

            // NOW -> CURRENT_TIMESTAMP
            "NOW" => Ok(Expression::CurrentTimestamp(
                crate::expressions::CurrentTimestamp {
                    precision: None,
                    sysdate: false,
                },
            )),

            // GETDATE -> CURRENT_TIMESTAMP
            "GETDATE" => Ok(Expression::CurrentTimestamp(
                crate::expressions::CurrentTimestamp {
                    precision: None,
                    sysdate: false,
                },
            )),

            // RAND -> RANDOM in Teradata
            "RAND" => Ok(Expression::Random(crate::expressions::Random)),

            // LEN -> CHARACTER_LENGTH in Teradata
            "LEN" if f.args.len() == 1 => Ok(Expression::Length(Box::new(UnaryFunc::new(
                f.args.into_iter().next().unwrap(),
            )))),

            // LENGTH -> CHARACTER_LENGTH in Teradata
            "LENGTH" if f.args.len() == 1 => Ok(Expression::Length(Box::new(UnaryFunc::new(
                f.args.into_iter().next().unwrap(),
            )))),

            // CHARINDEX -> INSTR in Teradata (with swapped args)
            "CHARINDEX" if f.args.len() >= 2 => {
                let mut args = f.args;
                let substring = args.remove(0);
                let string = args.remove(0);
                Ok(Expression::Function(Box::new(Function::new(
                    "INSTR".to_string(),
                    vec![string, substring],
                ))))
            }

            // STRPOS -> INSTR in Teradata
            "STRPOS" if f.args.len() >= 2 => Ok(Expression::Function(Box::new(Function::new(
                "INSTR".to_string(),
                f.args,
            )))),

            // LOCATE -> INSTR in Teradata (with swapped args)
            "LOCATE" if f.args.len() >= 2 => {
                let mut args = f.args;
                let substring = args.remove(0);
                let string = args.remove(0);
                Ok(Expression::Function(Box::new(Function::new(
                    "INSTR".to_string(),
                    vec![string, substring],
                ))))
            }

            // ARRAY_LENGTH -> CARDINALITY in Teradata
            "ARRAY_LENGTH" if f.args.len() == 1 => Ok(Expression::Function(Box::new(
                Function::new("CARDINALITY".to_string(), f.args),
            ))),

            // SIZE -> CARDINALITY in Teradata
            "SIZE" if f.args.len() == 1 => Ok(Expression::Function(Box::new(Function::new(
                "CARDINALITY".to_string(),
                f.args,
            )))),

            // SUBSTR -> SUBSTRING
            "SUBSTR" => Ok(Expression::Function(Box::new(Function::new(
                "SUBSTRING".to_string(),
                f.args,
            )))),

            // DATE_FORMAT -> TO_CHAR in Teradata
            "DATE_FORMAT" if f.args.len() >= 2 => Ok(Expression::Function(Box::new(
                Function::new("TO_CHAR".to_string(), f.args),
            ))),

            // strftime -> TO_CHAR in Teradata
            "STRFTIME" if f.args.len() >= 2 => {
                let mut args = f.args;
                let format = args.remove(0);
                let date = args.remove(0);
                Ok(Expression::Function(Box::new(Function::new(
                    "TO_CHAR".to_string(),
                    vec![date, format],
                ))))
            }

            // GREATEST is native in Teradata
            "GREATEST" => Ok(Expression::Function(Box::new(f))),

            // LEAST is native in Teradata
            "LEAST" => Ok(Expression::Function(Box::new(f))),

            // Pass through everything else
            _ => Ok(Expression::Function(Box::new(f))),
        }
    }

    fn transform_aggregate_function(
        &self,
        f: Box<crate::expressions::AggregateFunction>,
    ) -> Result<Expression> {
        let name_upper = f.name.to_uppercase();
        match name_upper.as_str() {
            // COUNT_IF -> SUM(CASE WHEN...)
            "COUNT_IF" if !f.args.is_empty() => {
                let condition = f.args.into_iter().next().unwrap();
                let case_expr = Expression::Case(Box::new(Case {
                    operand: None,
                    whens: vec![(condition, Expression::number(1))],
                    else_: Some(Expression::number(0)),
                    comments: Vec::new(),
                    inferred_type: None,
                }));
                Ok(Expression::Sum(Box::new(AggFunc {
                    ignore_nulls: None,
                    having_max: None,
                    this: case_expr,
                    distinct: f.distinct,
                    filter: f.filter,
                    order_by: Vec::new(),
                    name: None,
                    limit: None,
                    inferred_type: None,
                })))
            }

            // MAX_BY is native in Teradata
            "MAX_BY" => Ok(Expression::AggregateFunction(f)),

            // MIN_BY is native in Teradata
            "MIN_BY" => Ok(Expression::AggregateFunction(f)),

            // Pass through everything else
            _ => Ok(Expression::AggregateFunction(f)),
        }
    }

    fn transform_cast(&self, c: Cast) -> Result<Expression> {
        // Teradata CAST(x AS DATE FORMAT 'fmt') -> StrToDate(x, converted_fmt)
        // Teradata CAST(x AS TIMESTAMP FORMAT 'fmt') -> StrToTime(x, converted_fmt)
        if let Some(format_expr) = &c.format {
            let is_date = matches!(c.to, crate::expressions::DataType::Date);
            let is_timestamp = matches!(c.to, crate::expressions::DataType::Timestamp { .. });

            if is_date || is_timestamp {
                // Extract the format string from the expression
                let fmt_str = match format_expr.as_ref() {
                    Expression::Literal(lit)
                        if matches!(lit.as_ref(), crate::expressions::Literal::String(_)) =>
                    {
                        let crate::expressions::Literal::String(s) = lit.as_ref() else {
                            unreachable!()
                        };
                        Some(s.clone())
                    }
                    _ => None,
                };

                if let Some(teradata_fmt) = fmt_str {
                    // Convert Teradata format to strftime format
                    let strftime_fmt = Self::teradata_to_strftime(&teradata_fmt);

                    if is_date {
                        return Ok(Expression::StrToDate(Box::new(
                            crate::expressions::StrToDate {
                                this: Box::new(c.this),
                                format: Some(strftime_fmt),
                                safe: None,
                            },
                        )));
                    } else {
                        return Ok(Expression::StrToTime(Box::new(
                            crate::expressions::StrToTime {
                                this: Box::new(c.this),
                                format: strftime_fmt,
                                zone: None,
                                safe: None,
                                target_type: None,
                            },
                        )));
                    }
                }
            }
        }
        // Teradata type mappings are handled in the generator
        Ok(Expression::Cast(Box::new(c)))
    }

    /// Convert Teradata date/time format string to strftime format
    fn teradata_to_strftime(fmt: &str) -> String {
        // Teradata TIME_MAPPING: longest tokens first to avoid partial matches
        let mut result = fmt.to_string();
        // Order matters: replace longer tokens first
        result = result.replace("YYYY", "%Y");
        result = result.replace("Y4", "%Y");
        result = result.replace("YY", "%y");
        result = result.replace("MMMM", "%B");
        result = result.replace("MMM", "%b");
        result = result.replace("MM", "%m");
        result = result.replace("M4", "%B");
        result = result.replace("M3", "%b");
        result = result.replace("EEEE", "%A");
        result = result.replace("EEE", "%a");
        result = result.replace("EE", "%a");
        result = result.replace("E4", "%A");
        result = result.replace("E3", "%a");
        result = result.replace("DDD", "%j");
        result = result.replace("DD", "%d");
        result = result.replace("D3", "%j");
        result = result.replace("HH24", "%H");
        result = result.replace("HH", "%H");
        result = result.replace("SSSSSS", "%f");
        result = result.replace("SS", "%S");
        result = result.replace("MI", "%M");
        result
    }
}