Skip to content

Commit

Permalink
opensearch-project#2550 Not removing leading periods when quoting/unq…
Browse files Browse the repository at this point in the history
…uoting.

Signed-off-by: jzonthemtn <[email protected]>
  • Loading branch information
jzonthemtn committed Jun 3, 2024
1 parent 3dd1729 commit f544d67
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,26 @@ public static String unquoteSingleField(String text) {
* @return A string whose each dot-separated field has been unquoted from back-ticks (if any)
*/
public static String unquoteFullColumn(String text, String quote) {
boolean startsWithPeriod = false;
if (text.startsWith(quote + ".")) {
startsWithPeriod = true;
text = quote + text.substring(2);
}
String[] strs = text.split("\\.");
for (int i = 0; i < strs.length; i++) {
String unquotedSubstr = unquoteSingleField(strs[i], quote);
strs[i] = unquotedSubstr;
}
return String.join(".", strs);
if (startsWithPeriod) {
String s = String.join(".", strs);
if (s.startsWith(quote)) {
return new StringBuilder(s).insert(1, ".").toString();
} else {
return "." + s;
}
} else {
return String.join(".", strs);
}
}

public static String unquoteFullColumn(String text) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,25 @@ public class BackticksUnquoterTest {
public void assertNotQuotedStringShouldKeepTheSame() {
assertThat(unquoteSingleField("identifier"), equalTo("identifier"));
assertThat(unquoteFullColumn("identifier"), equalTo("identifier"));
assertThat(unquoteFullColumn(".identifier"), equalTo(".identifier"));
}

@Test
public void assertStringWithOneBackTickShouldKeepTheSame() {
assertThat(unquoteSingleField("`identifier"), equalTo("`identifier"));
assertThat(unquoteFullColumn("`identifier"), equalTo("`identifier"));
assertThat(unquoteFullColumn("`.identifier"), equalTo("`.identifier"));
}

@Test
public void assertBackticksQuotedStringShouldBeUnquoted() {
assertThat("identifier", equalTo(unquoteSingleField("`identifier`")));
assertThat(".identifier", equalTo(unquoteSingleField("`.identifier`")));

assertThat(
"identifier1.identifier2", equalTo(unquoteFullColumn("`identifier1`.`identifier2`")));
assertThat("identifier1.identifier2", equalTo(unquoteFullColumn("`identifier1`.identifier2")));
assertThat(
".identifier1.identifier2", equalTo(unquoteFullColumn("`.identifier1`.`identifier2`")));
}
}

0 comments on commit f544d67

Please sign in to comment.