Skip to content

Commit 2d6215c

Browse files
nom-sql: lowercase all function names
This makes our migration to sqlparser smoother. Change-Id: If0cd95067063af22f49821643083d24dc7e50dba Reviewed-on: https://gerrit.readyset.name/c/readyset/+/9250 Reviewed-by: Michael Zink <michael.z@readyset.io> Tested-by: Buildkite CI
1 parent e85664e commit 2d6215c

File tree

9 files changed

+35
-67
lines changed

9 files changed

+35
-67
lines changed

dataflow-expression/src/lower.rs

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ impl BuiltinFunction {
178178
null_on_failure: true,
179179
};
180180

181-
let result = match name.to_lowercase().as_str() {
181+
let result = match name {
182182
"convert_tz" => {
183183
// Type is inferred from input argument
184184
let input = next_arg()?;
@@ -1662,44 +1662,6 @@ pub(crate) mod tests {
16621662
);
16631663
}
16641664

1665-
#[test]
1666-
fn call_coalesce_uppercase() {
1667-
let input = AstExpr::Call(FunctionExpr::Call {
1668-
name: "COALESCE".into(),
1669-
arguments: vec![AstExpr::Column("t.x".into()), AstExpr::Literal(2.into())],
1670-
});
1671-
1672-
let result = Expr::lower(
1673-
input,
1674-
Dialect::DEFAULT_MYSQL,
1675-
&resolve_columns(|c| {
1676-
if c == "t.x".into() {
1677-
Ok((0, DfType::Int))
1678-
} else {
1679-
internal!("what's this column!?")
1680-
}
1681-
}),
1682-
)
1683-
.unwrap();
1684-
1685-
assert_eq!(
1686-
result,
1687-
Expr::Call {
1688-
func: Box::new(BuiltinFunction::Coalesce(
1689-
Expr::Column {
1690-
index: 0,
1691-
ty: DfType::Int
1692-
},
1693-
vec![Expr::Literal {
1694-
val: 2.into(),
1695-
ty: DfType::BigInt
1696-
}]
1697-
)),
1698-
ty: DfType::Int
1699-
}
1700-
);
1701-
}
1702-
17031665
#[test]
17041666
fn call_concat_with_texts() {
17051667
let input = parse_expr(ParserDialect::MySQL, "concat('My', 'SQ', 'L')").unwrap();

nom-sql/src/column.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ mod tests {
322322

323323
#[test]
324324
fn on_update_current_timestamp_no_precision() {
325-
let input = b"`lastModified` DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP";
325+
let input = b"`lastModified` DATETIME(6) NOT NULL DEFAULT current_timestamp(6) ON UPDATE CURRENT_TIMESTAMP";
326326
let (_, res) = column_specification(Dialect::MySQL)(LocatedSpan::new(input)).unwrap();
327327
let cspec = ColumnSpecification {
328328
column: Column {
@@ -335,7 +335,7 @@ mod tests {
335335
constraints: vec![
336336
ColumnConstraint::NotNull,
337337
ColumnConstraint::DefaultValue(Expr::Call(FunctionExpr::Call {
338-
name: "CURRENT_TIMESTAMP".into(),
338+
name: "current_timestamp".into(),
339339
arguments: vec![Expr::Literal(Literal::Integer(6))],
340340
})),
341341
ColumnConstraint::OnUpdateCurrentTimestamp(None),
@@ -348,7 +348,7 @@ mod tests {
348348

349349
#[test]
350350
fn on_update_current_timestamp_precision() {
351-
let canonical = "`lastModified` DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6)";
351+
let canonical = "`lastModified` DATETIME(6) NOT NULL DEFAULT current_timestamp(6) ON UPDATE CURRENT_TIMESTAMP(6)";
352352
let inputs = vec![
353353
canonical,
354354
"`lastModified` DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP (6) ",
@@ -371,7 +371,7 @@ mod tests {
371371
constraints: vec![
372372
ColumnConstraint::NotNull,
373373
ColumnConstraint::DefaultValue(Expr::Call(FunctionExpr::Call {
374-
name: "CURRENT_TIMESTAMP".into(),
374+
name: "current_timestamp".into(),
375375
arguments: vec![Expr::Literal(Literal::Integer(6))],
376376
})),
377377
ColumnConstraint::OnUpdateCurrentTimestamp(Some(Literal::Integer(6))),

nom-sql/src/common.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ fn delim_fx_args(
132132
}
133133

134134
fn function_call_without_parens(
135-
dialect: Dialect,
135+
_dialect: Dialect,
136136
) -> impl Fn(LocatedSpan<&[u8]>) -> NomSqlResult<&[u8], FunctionExpr> {
137137
move |i| {
138138
// Some functions can be called without parentheses, in both mysql and postgres
@@ -146,11 +146,10 @@ fn function_call_without_parens(
146146
tag_no_case("localtime"),
147147
)),
148148
|n: LocatedSpan<&[u8]>| {
149-
let s = String::from_utf8(n.to_vec()).expect("Only constant string literals");
150-
match dialect {
151-
Dialect::MySQL => s.into(),
152-
Dialect::PostgreSQL => s.to_lowercase().into(),
153-
}
149+
String::from_utf8(n.to_vec())
150+
.expect("Only constant string literals")
151+
.to_lowercase()
152+
.into()
154153
},
155154
)(i)?;
156155

@@ -317,7 +316,13 @@ fn function_call(
317316
let (i, name) = dialect.function_identifier()(i)?;
318317
let (i, _) = whitespace0(i)?;
319318
let (i, arguments) = delim_fx_args(dialect)(i)?;
320-
Ok((i, FunctionExpr::Call { name, arguments }))
319+
Ok((
320+
i,
321+
FunctionExpr::Call {
322+
name: name.to_lowercase().into(),
323+
arguments,
324+
},
325+
))
321326
}
322327
}
323328

@@ -1027,7 +1032,7 @@ mod tests {
10271032
assert_eq!(
10281033
res,
10291034
FunctionExpr::Call {
1030-
name: "NOW".into(),
1035+
name: "now".into(),
10311036
arguments: vec![]
10321037
}
10331038
);
@@ -1138,7 +1143,7 @@ mod tests {
11381143

11391144
#[test]
11401145
fn parse_extract_expr() {
1141-
let expr = format!("EXTRACT({} FROM \"col\")", $field_expr);
1146+
let expr = format!("extract({} FROM \"col\")", $field_expr);
11421147
assert_eq!(
11431148
test_parse!(extract(Dialect::PostgreSQL), expr.as_bytes()),
11441149
FunctionExpr::Extract {
@@ -1153,7 +1158,7 @@ mod tests {
11531158

11541159
#[test]
11551160
fn format_round_trip() {
1156-
let expected = format!("EXTRACT({} FROM \"col\")", $field_expr);
1161+
let expected = format!("extract({} FROM \"col\")", $field_expr);
11571162
let actual = test_parse!(extract(Dialect::PostgreSQL), expected.as_bytes())
11581163
.display(Dialect::PostgreSQL)
11591164
.to_string();
@@ -1194,7 +1199,7 @@ mod tests {
11941199
fn test(dialect: Dialect, func_name: &str, val: &str, collate: Option<&str>) {
11951200
let expected = format!(
11961201
"{}(\'{}\'{})",
1197-
func_name.to_uppercase(),
1202+
func_name,
11981203
val,
11991204
if let Some(collation_name) = collate {
12001205
format!(" COLLATE \"{}\"", collation_name)
@@ -1209,7 +1214,7 @@ mod tests {
12091214
}
12101215
.display(dialect)
12111216
.to_string();
1212-
assert_eq!(expected, actual);
1217+
assert_eq!(expected, actual, "dialect: {}", dialect);
12131218
}
12141219

12151220
test(Dialect::PostgreSQL, "lower", "AbC", Some("es_ES"));

nom-sql/src/create.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2718,7 +2718,7 @@ PRIMARY KEY (`id`));";
27182718
#[test]
27192719
fn on_update_current_timestamp_precision() {
27202720
let qstring = b"CREATE TABLE foo (
2721-
`lastModified` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6)
2721+
`lastModified` datetime(6) NOT NULL DEFAULT current_timestamp(6) ON UPDATE CURRENT_TIMESTAMP(6)
27222722
);";
27232723
let res = test_parse!(create_table(Dialect::MySQL), qstring);
27242724
assert_eq!(res.table.name, "foo");
@@ -2734,7 +2734,7 @@ PRIMARY KEY (`id`));";
27342734
vec![
27352735
ColumnConstraint::NotNull,
27362736
ColumnConstraint::DefaultValue(Expr::Call(FunctionExpr::Call {
2737-
name: "CURRENT_TIMESTAMP".into(),
2737+
name: "current_timestamp".into(),
27382738
arguments: vec![Expr::Literal(Literal::Integer(6,),),],
27392739
},),),
27402740
ColumnConstraint::OnUpdateCurrentTimestamp(Some(Literal::Integer(6)),),

nom-sql/src/insert.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ mod tests {
149149
Expr::Literal("test".into()),
150150
Expr::Literal("test".into()),
151151
Expr::Call(FunctionExpr::Call {
152-
name: "CURRENT_TIMESTAMP".into(),
152+
name: "current_timestamp".into(),
153153
arguments: vec![]
154154
}),
155155
],],

nom-sql/src/select.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1661,7 +1661,7 @@ mod tests {
16611661
Expr::Literal(Literal::String("foo".to_owned()),)
16621662
),
16631663
FieldDefinitionExpr::from(Expr::Call(FunctionExpr::Call {
1664-
name: "CURRENT_TIME".into(),
1664+
name: "current_time".into(),
16651665
arguments: vec![]
16661666
})),
16671667
],

nom-sql/src/update.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ mod tests {
188188
fields: vec![(
189189
Column::from("permission"),
190190
Expr::Call(FunctionExpr::Call {
191-
name: "REPLACE".into(),
191+
name: "replace".into(),
192192
arguments: vec![
193193
Expr::Column(Column::from("permission")),
194194
Expr::Literal(Literal::String("viewDiscussions".into())),

readyset-sql-passes/src/util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ pub fn map_aggregates(expr: &mut Expr) -> Vec<(FunctionExpr, SqlIdentifier)> {
145145
Expr::Call(FunctionExpr::Call {
146146
name,
147147
arguments: exprs,
148-
}) if matches!(name.to_lowercase().as_str(), "round") => {
148+
}) if matches!(name.as_str(), "round") => {
149149
let expr = exprs
150150
.first_mut()
151151
.expect("round should have at least one argument");

readyset-sql/src/ast/expression.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -273,17 +273,17 @@ impl DialectDisplay for FunctionExpr {
273273
)
274274
}
275275
FunctionExpr::Extract { field, expr } => {
276-
write!(f, "EXTRACT({field} FROM {})", expr.display(dialect))
276+
write!(f, "extract({field} FROM {})", expr.display(dialect))
277277
}
278278
FunctionExpr::Lower { expr, collation } => {
279-
write!(f, "LOWER({}", expr.display(dialect))?;
279+
write!(f, "lower({}", expr.display(dialect))?;
280280
if let Some(c) = collation {
281281
write!(f, " COLLATE \"{}\"", c)?;
282282
}
283283
write!(f, ")")
284284
}
285285
FunctionExpr::Upper { expr, collation } => {
286-
write!(f, "UPPER({}", expr.display(dialect))?;
286+
write!(f, "upper({}", expr.display(dialect))?;
287287
if let Some(c) = collation {
288288
write!(f, " COLLATE \"{}\"", c)?;
289289
}
@@ -1138,9 +1138,9 @@ impl TryFromDialect<sqlparser::ast::Expr> for Expr {
11381138
// XXX: Unfortunately, we have lost the original casing by now. This will cause some
11391139
// spurious mismatches with nom-sql.
11401140
let name = if shorthand {
1141-
"SUBSTR".into_dialect(dialect)
1141+
"substr".into_dialect(dialect)
11421142
} else {
1143-
"SUBSTRING".into_dialect(dialect)
1143+
"substring".into_dialect(dialect)
11441144
};
11451145
Ok(Self::Call(FunctionExpr::Call { name, arguments }))
11461146
}
@@ -1249,7 +1249,7 @@ impl TryFromDialect<sqlparser::ast::Function> for Expr {
12491249
// TODO: handle null treatment and other stuff
12501250
let sqlparser::ast::Function { args, name, .. } = value;
12511251

1252-
let sqlparser::ast::ObjectNamePart::Identifier(ident) = name
1252+
let sqlparser::ast::ObjectNamePart::Identifier(mut ident) = name
12531253
.0
12541254
.into_iter()
12551255
.exactly_one()
@@ -1362,6 +1362,7 @@ impl TryFromDialect<sqlparser::ast::Function> for Expr {
13621362
} else if ident.value.eq_ignore_ascii_case("EXTRACT") {
13631363
return failed!("{ident} should have been converted earlier");
13641364
} else {
1365+
ident.value = ident.value.to_lowercase();
13651366
Self::Call(FunctionExpr::Call {
13661367
name: ident.into_dialect(dialect),
13671368
arguments: exprs.try_collect()?,

0 commit comments

Comments
 (0)