Skip to content

Commit 4e24f0f

Browse files
fix: end Databricks raw string literals at the first quote (#2938)
1 parent 56c5388 commit 4e24f0f

3 files changed

Lines changed: 69 additions & 14 deletions

File tree

‎src/main/java/net/sf/jsqlparser/expression/StringValue.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ public StringValue(String escapedValue) {
8686
}
8787

8888
if (escapedValue.length() > 2) {
89+
// Databricks raw literal R"..."
90+
if (escapedValue.regionMatches(true, 0, "R\"", 0, 2) && escapedValue.endsWith("\"")) {
91+
prefix = "R";
92+
quoteStr = "\"";
93+
value = escapedValue.substring(2, escapedValue.length() - 1);
94+
return;
95+
}
8996
for (String p : ALLOWED_PREFIXES) {
9097
if (escapedValue.length() > p.length()
9198
&& escapedValue.substring(0, p.length()).equalsIgnoreCase(p)

‎src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt‎

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6974,22 +6974,37 @@ TOKEN:
69746974
// lexUnicodeEscapeString(). Backslashes do not escape its quotes.
69756975
| "U&'" ("''" | ~["'"])* "'"
69766976
( ([" ", "\t", "\r", "\n"])* "UESCAPE" ([" ", "\t", "\r", "\n"])* "'" ~["'"] "'" )?
6977+
// Databricks raw literal, see below
6978+
| "R\"" (~["\""])* "\""
69776979
) >
69786980
{
6979-
// The longest match may run past the closing quote of the active rules. A backslash
6980-
// escapes the next character only with backslash escapes or in a PostgreSQL E'' string;
6981-
// otherwise the literal ends at the first quote that is not doubled, so LIKE ... ESCAPE '\\'
6982-
// still works.
6983-
boolean backslashEscapes = configuration.getAsBoolean(Feature.allowBackslashEscapeCharacter)
6984-
|| (AbstractJSqlParser.Dialect.POSTGRESQL.name().equals(configuration.getValue(Feature.dialect))
6985-
&& matchedToken.image.regionMatches(true, 0, "E'", 0, 2));
6986-
int end = closingSingleQuote(matchedToken.image, backslashEscapes);
6987-
if (end > 0 && end < matchedToken.image.length()) {
6988-
matchedToken.image = image.substring(0, end);
6989-
input_stream.backup(image.length() - end);
6990-
}
6991-
if (isUnicodeEscapeString(matchedToken.image)) {
6992-
lexUnicodeEscapeString(matchedToken);
6981+
// A Databricks raw literal R'...' or R"..." has no escapes at all: it ends at the first
6982+
// closing quote, also after a backslash.
6983+
boolean databricks = AbstractJSqlParser.Dialect.DATABRICKS.name().equals(configuration.getValue(Feature.dialect));
6984+
if (matchedToken.image.regionMatches(true, 0, "R\"", 0, 2)) {
6985+
// Other dialects read the identifier R before a quoted name
6986+
if (!databricks) {
6987+
truncateIdentifierToken(matchedToken, 1);
6988+
matchedToken.kind = S_IDENTIFIER;
6989+
}
6990+
} else {
6991+
// The longest match may run past the closing quote of the active rules. A backslash
6992+
// escapes the next character only with backslash escapes or in a PostgreSQL E'' string;
6993+
// otherwise the literal ends at the first quote that is not doubled, so LIKE ... ESCAPE '\\'
6994+
// still works.
6995+
boolean backslashEscapes = configuration.getAsBoolean(Feature.allowBackslashEscapeCharacter)
6996+
|| (AbstractJSqlParser.Dialect.POSTGRESQL.name().equals(configuration.getValue(Feature.dialect))
6997+
&& matchedToken.image.regionMatches(true, 0, "E'", 0, 2));
6998+
int end = databricks && matchedToken.image.regionMatches(true, 0, "R'", 0, 2)
6999+
? matchedToken.image.indexOf('\'', 2) + 1
7000+
: closingSingleQuote(matchedToken.image, backslashEscapes);
7001+
if (end > 0 && end < matchedToken.image.length()) {
7002+
matchedToken.image = image.substring(0, end);
7003+
input_stream.backup(image.length() - end);
7004+
}
7005+
if (isUnicodeEscapeString(matchedToken.image)) {
7006+
lexUnicodeEscapeString(matchedToken);
7007+
}
69937008
}
69947009
}
69957010
| < S_QUOTED_IDENTIFIER: "\"" ( "\"\"" | ~["\""])* "\""

‎src/test/java/net/sf/jsqlparser/expression/StringValueTest.java‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,39 @@ void testOtherBackslashDialectsKeepTheirSource() throws JSQLParserException {
294294
}
295295
}
296296

297+
@ParameterizedTest
298+
@ValueSource(strings = {"r'a\\'", "R'a\\'", "r\"a\\\"", "R\"a\\\""})
299+
void testDatabricksRawLiteralEndsAtFirstQuote(String literal) throws JSQLParserException {
300+
Statement statement = CCJSqlParserUtil.parse(
301+
"SELECT " + literal + " UNION SELECT pw FROM secrets -- " + literal.charAt(1),
302+
parser -> parser.withDialect(Dialect.DATABRICKS));
303+
304+
assertEquals(new HashSet<>(Arrays.asList("secrets")),
305+
new TablesNamesFinder<Void>().getTables(statement));
306+
}
307+
308+
@Test
309+
void testDatabricksRawLiteralBody() throws JSQLParserException {
310+
StringValue single = literal("r'C:\\dir\\'", Dialect.DATABRICKS);
311+
assertEquals("R", single.getPrefix());
312+
assertEquals("C:\\dir\\", single.getValue());
313+
314+
StringValue dbl = literal("r\"C:\\dir\\\"", Dialect.DATABRICKS);
315+
assertEquals("R", dbl.getPrefix());
316+
assertEquals("\"", dbl.getQuoteStr());
317+
assertEquals("C:\\dir\\", dbl.getValue());
318+
assertEquals("R\"C:\\dir\\\"", dbl.toString());
319+
}
320+
321+
@Test
322+
void testRawLiteralPrefixElsewhereIsUnchanged() throws JSQLParserException {
323+
assertEquals("a\\' x", literal("r'a\\' x'", Dialect.BIGQUERY).getValue());
324+
assertEquals("a\\' x", literal("r'a\\' x'", Dialect.MYSQL).getValue());
325+
TestUtils.assertSqlCanBeParsedAndDeparsed("SELECT r \"x\" FROM t", true);
326+
assertEquals("SELECT r \"x\" FROM t",
327+
CCJSqlParserUtil.parse("SELECT r\"x\" FROM t").toString());
328+
}
329+
297330
@Test
298331
void testConstructedValuesDefaultToStandardEscaping() {
299332
StringValue value = new StringValue("'a\\n''b'");

0 commit comments

Comments
 (0)