Skip to content

Commit 0743d2d

Browse files
authored
Support backslash escapes in Databricks string literals (#42)
2 parents 36b8847 + 28af4c9 commit 0743d2d

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

src/dialect/databricks.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,9 @@ impl Dialect for DatabricksDialect {
8484
fn supports_nested_comments(&self) -> bool {
8585
true
8686
}
87+
88+
// https://docs.databricks.com/aws/en/sql/language-manual/data-types/string-type#literals
89+
fn supports_string_literal_backslash_escape(&self) -> bool {
90+
true
91+
}
8792
}

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@
153153
// Splitting complex nodes (expressions, statements, types) into separate types
154154
// would bloat the API and hide intent. Extra memory is a worthwhile tradeoff.
155155
#![allow(clippy::large_enum_variant)]
156+
// TODO: Fix and remove this.
157+
#![expect(clippy::unnecessary_unwrap)]
156158

157159
// Allow proc-macros to find this crate
158160
extern crate self as sqlparser;

tests/sqlparser_databricks.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use sqlparser::ast::helpers::attached_token::AttachedToken;
1919
use sqlparser::ast::*;
2020
use sqlparser::dialect::{DatabricksDialect, GenericDialect};
21-
use sqlparser::parser::ParserError;
21+
use sqlparser::parser::{ParserError, ParserOptions};
2222
use test_utils::*;
2323

2424
#[macro_use]
@@ -28,6 +28,13 @@ fn databricks() -> TestedDialects {
2828
TestedDialects::new(vec![Box::new(DatabricksDialect {})])
2929
}
3030

31+
fn databricks_no_unescape() -> TestedDialects {
32+
TestedDialects::new_with_options(
33+
vec![Box::new(DatabricksDialect {})],
34+
ParserOptions::new().with_unescape(false),
35+
)
36+
}
37+
3138
fn databricks_and_generic() -> TestedDialects {
3239
TestedDialects::new(vec![
3340
Box::new(DatabricksDialect {}),
@@ -54,6 +61,22 @@ fn test_databricks_identifiers() {
5461
);
5562
}
5663

64+
// Test examples from https://docs.databricks.com/aws/en/sql/language-manual/data-types/string-type#examples
65+
//
66+
// Note: string literals are broken on DBx when unescape in turned on (the default).
67+
#[test]
68+
fn test_databricks_string_literals() {
69+
databricks_no_unescape().verified_expr(r"'O\'Connell'");
70+
databricks_no_unescape().verified_expr(r"'Some\nText'");
71+
databricks_no_unescape().verified_expr("'서울시'");
72+
databricks_no_unescape().verified_expr(r"'\\'");
73+
74+
// FIXME: raw strings are broken
75+
// databricks_no_unescape().verified_query(r"SELECT 'Hou$e', 'Hou\$e', r'Hou$e', r'Hou\$e'");
76+
// databricks_no_unescape().verified_expr(r"r'Some\nText'");
77+
// databricks_no_unescape().verified_expr(r"r'\\'");
78+
}
79+
5780
#[test]
5881
fn test_databricks_exists() {
5982
// exists is a function in databricks

0 commit comments

Comments
 (0)