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
//! Apache Drill SQL Dialect
//!
//! Drill-specific SQL dialect based on sqlglot patterns.
//!
//! Key characteristics:
//! - Uses backticks for identifiers
//! - Backslash string escapes
//! - No TRY_CAST support (must use CAST)
//! - NULLS LAST is default ordering
//! - Functions: REPEATED_COUNT (array size), REPEATED_CONTAINS (array contains)
//! - POW for power function
//! - Date format: 'yyyy-MM-dd'
//! - Type mappings: INT→INTEGER, TEXT→VARCHAR, etc.
use super::{DialectImpl, DialectType};
use crate::error::Result;
use crate::expressions::{Expression, Function};
use crate::generator::{GeneratorConfig, NormalizeFunctions};
use crate::tokens::TokenizerConfig;
/// Apache Drill dialect
pub struct DrillDialect;
impl DialectImpl for DrillDialect {
fn dialect_type(&self) -> DialectType {
DialectType::Drill
}
fn tokenizer_config(&self) -> TokenizerConfig {
let mut config = TokenizerConfig::default();
// Drill uses backticks for identifiers
config.identifiers.insert('`', '`');
config
}
fn generator_config(&self) -> GeneratorConfig {
use crate::generator::IdentifierQuoteStyle;
GeneratorConfig {
identifier_quote: '`',
identifier_quote_style: IdentifierQuoteStyle::BACKTICK,
dialect: Some(DialectType::Drill),
// Drill: NORMALIZE_FUNCTIONS = False, PRESERVE_ORIGINAL_NAMES = True
normalize_functions: NormalizeFunctions::None,
..Default::default()
}
}
fn transform_expr(&self, expr: Expression) -> Result<Expression> {
match expr {
// TRY_CAST → CAST in Drill (no TRY_CAST support)
Expression::TryCast(c) => Ok(Expression::Cast(c)),
// SafeCast → CAST in Drill
Expression::SafeCast(c) => Ok(Expression::Cast(c)),
// CURRENT_TIMESTAMP without parentheses
Expression::CurrentTimestamp(_) => Ok(Expression::CurrentTimestamp(
crate::expressions::CurrentTimestamp {
precision: None,
sysdate: false,
},
)),
// ILIKE → `ILIKE` (backtick quoted function in Drill)
// Drill supports ILIKE but it needs to be backtick-quoted
Expression::ILike(op) => {
// Just pass through - Drill supports ILIKE
Ok(Expression::ILike(op))
}
// Power → POW in Drill
Expression::Power(op) => Ok(Expression::Function(Box::new(Function::new(
"POW".to_string(),
vec![op.this, op.expression],
)))),
// ArrayContains → REPEATED_CONTAINS in Drill
Expression::ArrayContains(f) => Ok(Expression::Function(Box::new(Function::new(
"REPEATED_CONTAINS".to_string(),
vec![f.this, f.expression],
)))),
// Generic function transformations
Expression::Function(f) => self.transform_function(*f),
// Pass through everything else
_ => Ok(expr),
}
}
}
impl DrillDialect {
fn transform_function(&self, f: Function) -> Result<Expression> {
let name_upper = f.name.to_uppercase();
match name_upper.as_str() {
// CURRENT_TIMESTAMP without parentheses
"CURRENT_TIMESTAMP" => Ok(Expression::CurrentTimestamp(
crate::expressions::CurrentTimestamp {
precision: None,
sysdate: false,
},
)),
// ARRAY_SIZE / ARRAY_LENGTH → REPEATED_COUNT
"ARRAY_SIZE" | "ARRAY_LENGTH" | "CARDINALITY" | "SIZE" => Ok(Expression::Function(
Box::new(Function::new("REPEATED_COUNT".to_string(), f.args)),
)),
// ARRAY_CONTAINS → REPEATED_CONTAINS
"ARRAY_CONTAINS" | "CONTAINS" => Ok(Expression::Function(Box::new(Function::new(
"REPEATED_CONTAINS".to_string(),
f.args,
)))),
// POWER → POW
"POWER" => Ok(Expression::Function(Box::new(Function::new(
"POW".to_string(),
f.args,
)))),
// LEVENSHTEIN → LEVENSHTEIN_DISTANCE
"LEVENSHTEIN" => Ok(Expression::Function(Box::new(Function::new(
"LEVENSHTEIN_DISTANCE".to_string(),
f.args,
)))),
// REGEXP_LIKE → REGEXP_MATCHES
"REGEXP_LIKE" | "RLIKE" => Ok(Expression::Function(Box::new(Function::new(
"REGEXP_MATCHES".to_string(),
f.args,
)))),
// TO_TIMESTAMP → TO_TIMESTAMP (native, but for parsing)
"TO_TIMESTAMP" => Ok(Expression::Function(Box::new(f))),
// TO_DATE → TO_DATE (native)
"TO_DATE" => Ok(Expression::Function(Box::new(f))),
// DATE_FORMAT → TO_CHAR
"DATE_FORMAT" => Ok(Expression::Function(Box::new(Function::new(
"TO_CHAR".to_string(),
f.args,
)))),
// strftime → TO_CHAR
"STRFTIME" => Ok(Expression::Function(Box::new(Function::new(
"TO_CHAR".to_string(),
f.args,
)))),
// UNIX_TIMESTAMP → native
"UNIX_TIMESTAMP" => Ok(Expression::Function(Box::new(f))),
// FROM_UNIXTIME → native (but named UNIX_TIMESTAMP_TO_TIMESTAMP in Drill)
"FROM_UNIXTIME" => Ok(Expression::Function(Box::new(f))),
// DATE_ADD with interval support
"DATE_ADD" => Ok(Expression::Function(Box::new(f))),
// DATE_SUB with interval support
"DATE_SUB" => Ok(Expression::Function(Box::new(f))),
// STRPOS → STRPOS (native in Drill)
"STRPOS" => Ok(Expression::Function(Box::new(f))),
// POSITION → STRPOS
"POSITION" => Ok(Expression::Function(Box::new(Function::new(
"STRPOS".to_string(),
f.args,
)))),
// Pass through everything else
_ => Ok(Expression::Function(Box::new(f))),
}
}
}