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
//! CockroachDB Dialect
//!
//! CockroachDB-specific transformations based on sqlglot patterns.
//! CockroachDB is PostgreSQL-compatible with distributed database extensions.
use super::{DialectImpl, DialectType};
use crate::error::Result;
use crate::expressions::{AggFunc, Case, Cast, Expression, Function, VarArgFunc};
use crate::generator::GeneratorConfig;
use crate::tokens::TokenizerConfig;
/// CockroachDB dialect (PostgreSQL-compatible distributed database)
pub struct CockroachDBDialect;
impl DialectImpl for CockroachDBDialect {
fn dialect_type(&self) -> DialectType {
DialectType::CockroachDB
}
fn tokenizer_config(&self) -> TokenizerConfig {
let mut config = TokenizerConfig::default();
// CockroachDB uses double quotes for identifiers (PostgreSQL-style)
config.identifiers.insert('"', '"');
// PostgreSQL-style nested comments supported
config.nested_comments = true;
config
}
fn generator_config(&self) -> GeneratorConfig {
use crate::generator::IdentifierQuoteStyle;
GeneratorConfig {
identifier_quote: '"',
identifier_quote_style: IdentifierQuoteStyle::DOUBLE_QUOTE,
dialect: Some(DialectType::CockroachDB),
..Default::default()
}
}
fn transform_expr(&self, expr: Expression) -> Result<Expression> {
match expr {
// IFNULL -> COALESCE in CockroachDB
Expression::IfNull(f) => Ok(Expression::Coalesce(Box::new(VarArgFunc {
original_name: None,
expressions: vec![f.this, f.expression],
inferred_type: None,
}))),
// NVL -> COALESCE in CockroachDB
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 is not directly supported, use CAST
Expression::TryCast(c) => Ok(Expression::Cast(c)),
// SafeCast -> CAST in CockroachDB
Expression::SafeCast(c) => Ok(Expression::Cast(c)),
// ILIKE is native in CockroachDB (PostgreSQL-style)
Expression::ILike(op) => Ok(Expression::ILike(op)),
// 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 CockroachDB (PostgreSQL-style)
Expression::Rand(r) => {
let _ = r.seed;
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 CockroachDBDialect {
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 is native in CockroachDB
"NOW" => Ok(Expression::CurrentTimestamp(
crate::expressions::CurrentTimestamp {
precision: None,
sysdate: false,
},
)),
// GETDATE -> NOW
"GETDATE" => Ok(Expression::CurrentTimestamp(
crate::expressions::CurrentTimestamp {
precision: None,
sysdate: false,
},
)),
// RAND -> RANDOM
"RAND" => Ok(Expression::Random(crate::expressions::Random)),
// STRING_AGG is native in CockroachDB (PostgreSQL-style)
"STRING_AGG" => Ok(Expression::Function(Box::new(f))),
// GROUP_CONCAT -> STRING_AGG
"GROUP_CONCAT" if !f.args.is_empty() => Ok(Expression::Function(Box::new(
Function::new("STRING_AGG".to_string(), f.args),
))),
// LISTAGG -> STRING_AGG
"LISTAGG" if !f.args.is_empty() => Ok(Expression::Function(Box::new(Function::new(
"STRING_AGG".to_string(),
f.args,
)))),
// SUBSTR -> SUBSTRING
"SUBSTR" => Ok(Expression::Function(Box::new(Function::new(
"SUBSTRING".to_string(),
f.args,
)))),
// LENGTH is native in CockroachDB
"LENGTH" => Ok(Expression::Function(Box::new(f))),
// LEN -> LENGTH
"LEN" if f.args.len() == 1 => Ok(Expression::Function(Box::new(Function::new(
"LENGTH".to_string(),
f.args,
)))),
// CHARINDEX -> STRPOS (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(
"STRPOS".to_string(),
vec![string, substring],
))))
}
// INSTR -> STRPOS
"INSTR" if f.args.len() >= 2 => Ok(Expression::Function(Box::new(Function::new(
"STRPOS".to_string(),
f.args,
)))),
// LOCATE -> STRPOS (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(
"STRPOS".to_string(),
vec![string, substring],
))))
}
// STRPOS is native in CockroachDB
"STRPOS" => Ok(Expression::Function(Box::new(f))),
// ARRAY_LENGTH is native in CockroachDB
"ARRAY_LENGTH" => Ok(Expression::Function(Box::new(f))),
// SIZE -> ARRAY_LENGTH
"SIZE" if f.args.len() == 1 => Ok(Expression::Function(Box::new(Function::new(
"ARRAY_LENGTH".to_string(),
f.args,
)))),
// CARDINALITY is native in CockroachDB
"CARDINALITY" => Ok(Expression::Function(Box::new(f))),
// TO_CHAR is native in CockroachDB
"TO_CHAR" => Ok(Expression::Function(Box::new(f))),
// DATE_FORMAT -> TO_CHAR
"DATE_FORMAT" if f.args.len() >= 2 => Ok(Expression::Function(Box::new(
Function::new("TO_CHAR".to_string(), f.args),
))),
// strftime -> TO_CHAR
"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],
))))
}
// JSONB_EXTRACT_PATH_TEXT is native in CockroachDB
"JSONB_EXTRACT_PATH_TEXT" => Ok(Expression::Function(Box::new(f))),
// JSON_EXTRACT_PATH_TEXT is native in CockroachDB
"JSON_EXTRACT_PATH_TEXT" => Ok(Expression::Function(Box::new(f))),
// GET_JSON_OBJECT -> JSONB_EXTRACT_PATH_TEXT
"GET_JSON_OBJECT" if f.args.len() == 2 => Ok(Expression::Function(Box::new(
Function::new("JSONB_EXTRACT_PATH_TEXT".to_string(), f.args),
))),
// JSON_EXTRACT -> JSONB_EXTRACT_PATH
"JSON_EXTRACT" if f.args.len() >= 2 => Ok(Expression::Function(Box::new(
Function::new("JSONB_EXTRACT_PATH".to_string(), f.args),
))),
// REGEXP_LIKE is native in CockroachDB
"REGEXP_LIKE" => Ok(Expression::Function(Box::new(f))),
// RLIKE -> REGEXP_LIKE
"RLIKE" if f.args.len() >= 2 => Ok(Expression::Function(Box::new(Function::new(
"REGEXP_LIKE".to_string(),
f.args,
)))),
// 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,
})))
}
// Pass through everything else
_ => Ok(Expression::AggregateFunction(f)),
}
}
fn transform_cast(&self, c: Cast) -> Result<Expression> {
// CockroachDB type mappings are handled in the generator
Ok(Expression::Cast(Box::new(c)))
}
}