Skip to content

Commit

Permalink
Add 2 tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rzhang10 committed Nov 6, 2023
1 parent 4096f79 commit afd1d2a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -692,9 +692,9 @@ protected SqlNode visitIdentifier(ASTNode node, ParseContext ctx) {
return new SqlIdentifier(node.getText(), ZERO);
}

/** See {@link #removeBackslashBeforeSingleQuote}
* We use removeBackslashBeforeSingleQuote to remove the backslash before single quote,
* so that we maintain patterns like {@code I'm} or {@code won't} as is in the java object in memory,
/** See {@link #removeBackslashBeforeQuotes}
* We use removeBackslashBeforeQuotes to remove the backslash before quotes,
* so that we maintain patterns like {@code I'm} or {@code abc"xyz} as is in the java object in memory,
* the escaped literal string representation will be generated when the SqlNode is written to string
* by the SqlWriter, which can be controlled by the SqlDialect to decide the choice of escaping mechanism.
* */
Expand All @@ -703,12 +703,12 @@ protected SqlNode visitStringLiteral(ASTNode node, ParseContext ctx) {
// TODO: Add charset here. UTF-8 is not supported by calcite
String text = node.getText();
checkState(text.length() >= 2);
return SqlLiteral.createCharString(removeBackslashBeforeSingleQuote(text.substring(1, text.length() - 1)), ZERO);
return SqlLiteral.createCharString(removeBackslashBeforeQuotes(text.substring(1, text.length() - 1)), ZERO);
}

private String removeBackslashBeforeSingleQuote(String input) {
private String removeBackslashBeforeQuotes(String input) {
// matches a \' literal pattern
Pattern pattern = Pattern.compile("\\\\'");
Pattern pattern = Pattern.compile("\\\\['\"]");
Matcher matcher = pattern.matcher(input);

StringBuffer res = new StringBuffer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,24 @@ public void testSingleQuoteInsideDoubleQuote() {
assertEquals(targetSql, expectedSql);
}

@Test
public void testDoubleQuoteInsideDoubleQuote() {
RelNode relNode = TestUtils.toRelNode("SELECT \"abc[\\\"xyz\\\"]\" col1 FROM default.complex");
String targetSql = createCoralSpark(relNode).getSparkSql();

String expectedSql = "SELECT 'abc[\"xyz\"]' col1\n" + "FROM default.complex complex";
assertEquals(targetSql, expectedSql);
}

@Test
public void testDoubleQuoteInsideSingleQuote() {
RelNode relNode = TestUtils.toRelNode("SELECT 'abc[\"xyz\"]' col1 FROM default.complex");
String targetSql = createCoralSpark(relNode).getSparkSql();

String expectedSql = "SELECT 'abc[\"xyz\"]' col1\n" + "FROM default.complex complex";
assertEquals(targetSql, expectedSql);
}

private String getCoralSparkTranslatedSqlWithAliasFromCoralSchema(String db, String view) {
RelNode relNode = TestUtils.toRelNode(db, view);
Schema schema = TestUtils.getAvroSchemaForView(db, view, false);
Expand Down

0 comments on commit afd1d2a

Please sign in to comment.