Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/main/java/net/sf/jsqlparser/expression/StringValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ public StringValue(String escapedValue) {
}

if (escapedValue.length() > 2) {
// Databricks raw literal R"..."
if (escapedValue.regionMatches(true, 0, "R\"", 0, 2) && escapedValue.endsWith("\"")) {
prefix = "R";
quoteStr = "\"";
value = escapedValue.substring(2, escapedValue.length() - 1);
return;
}
for (String p : ALLOWED_PREFIXES) {
if (escapedValue.length() > p.length()
&& escapedValue.substring(0, p.length()).equalsIgnoreCase(p)
Expand Down
43 changes: 29 additions & 14 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -6974,22 +6974,37 @@ TOKEN:
// lexUnicodeEscapeString(). Backslashes do not escape its quotes.
| "U&'" ("''" | ~["'"])* "'"
( ([" ", "\t", "\r", "\n"])* "UESCAPE" ([" ", "\t", "\r", "\n"])* "'" ~["'"] "'" )?
// Databricks raw literal, see below
| "R\"" (~["\""])* "\""
) >
{
// The longest match may run past the closing quote of the active rules. A backslash
// escapes the next character only with backslash escapes or in a PostgreSQL E'' string;
// otherwise the literal ends at the first quote that is not doubled, so LIKE ... ESCAPE '\\'
// still works.
boolean backslashEscapes = configuration.getAsBoolean(Feature.allowBackslashEscapeCharacter)
|| (AbstractJSqlParser.Dialect.POSTGRESQL.name().equals(configuration.getValue(Feature.dialect))
&& matchedToken.image.regionMatches(true, 0, "E'", 0, 2));
int end = closingSingleQuote(matchedToken.image, backslashEscapes);
if (end > 0 && end < matchedToken.image.length()) {
matchedToken.image = image.substring(0, end);
input_stream.backup(image.length() - end);
}
if (isUnicodeEscapeString(matchedToken.image)) {
lexUnicodeEscapeString(matchedToken);
// A Databricks raw literal R'...' or R"..." has no escapes at all: it ends at the first
// closing quote, also after a backslash.
boolean databricks = AbstractJSqlParser.Dialect.DATABRICKS.name().equals(configuration.getValue(Feature.dialect));
if (matchedToken.image.regionMatches(true, 0, "R\"", 0, 2)) {
// Other dialects read the identifier R before a quoted name
if (!databricks) {
truncateIdentifierToken(matchedToken, 1);
matchedToken.kind = S_IDENTIFIER;
}
} else {
// The longest match may run past the closing quote of the active rules. A backslash
// escapes the next character only with backslash escapes or in a PostgreSQL E'' string;
// otherwise the literal ends at the first quote that is not doubled, so LIKE ... ESCAPE '\\'
// still works.
boolean backslashEscapes = configuration.getAsBoolean(Feature.allowBackslashEscapeCharacter)
|| (AbstractJSqlParser.Dialect.POSTGRESQL.name().equals(configuration.getValue(Feature.dialect))
&& matchedToken.image.regionMatches(true, 0, "E'", 0, 2));
int end = databricks && matchedToken.image.regionMatches(true, 0, "R'", 0, 2)
? matchedToken.image.indexOf('\'', 2) + 1
: closingSingleQuote(matchedToken.image, backslashEscapes);
if (end > 0 && end < matchedToken.image.length()) {
matchedToken.image = image.substring(0, end);
input_stream.backup(image.length() - end);
}
if (isUnicodeEscapeString(matchedToken.image)) {
lexUnicodeEscapeString(matchedToken);
}
}
}
| < S_QUOTED_IDENTIFIER: "\"" ( "\"\"" | ~["\""])* "\""
Expand Down
33 changes: 33 additions & 0 deletions src/test/java/net/sf/jsqlparser/expression/StringValueTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,39 @@ void testOtherBackslashDialectsKeepTheirSource() throws JSQLParserException {
}
}

@ParameterizedTest
@ValueSource(strings = {"r'a\\'", "R'a\\'", "r\"a\\\"", "R\"a\\\""})
void testDatabricksRawLiteralEndsAtFirstQuote(String literal) throws JSQLParserException {
Statement statement = CCJSqlParserUtil.parse(
"SELECT " + literal + " UNION SELECT pw FROM secrets -- " + literal.charAt(1),
parser -> parser.withDialect(Dialect.DATABRICKS));

assertEquals(new HashSet<>(Arrays.asList("secrets")),
new TablesNamesFinder<Void>().getTables(statement));
}

@Test
void testDatabricksRawLiteralBody() throws JSQLParserException {
StringValue single = literal("r'C:\\dir\\'", Dialect.DATABRICKS);
assertEquals("R", single.getPrefix());
assertEquals("C:\\dir\\", single.getValue());

StringValue dbl = literal("r\"C:\\dir\\\"", Dialect.DATABRICKS);
assertEquals("R", dbl.getPrefix());
assertEquals("\"", dbl.getQuoteStr());
assertEquals("C:\\dir\\", dbl.getValue());
assertEquals("R\"C:\\dir\\\"", dbl.toString());
}

@Test
void testRawLiteralPrefixElsewhereIsUnchanged() throws JSQLParserException {
assertEquals("a\\' x", literal("r'a\\' x'", Dialect.BIGQUERY).getValue());
assertEquals("a\\' x", literal("r'a\\' x'", Dialect.MYSQL).getValue());
TestUtils.assertSqlCanBeParsedAndDeparsed("SELECT r \"x\" FROM t", true);
assertEquals("SELECT r \"x\" FROM t",
CCJSqlParserUtil.parse("SELECT r\"x\" FROM t").toString());
}

@Test
void testConstructedValuesDefaultToStandardEscaping() {
StringValue value = new StringValue("'a\\n''b'");
Expand Down
Loading