This is an automated email from the ASF dual-hosted git repository.

rubenada pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/calcite.git


The following commit(s) were added to refs/heads/main by this push:
     new bae4409739 [CALCITE-7667] Improve string-literal encoding in pushdown 
translators (follow-up)
bae4409739 is described below

commit bae4409739eb471a1da7d776f8feea29783e9a96
Author: Ruben Quesada Lopez <[email protected]>
AuthorDate: Fri Aug 21 13:25:05 2026 +0100

    [CALCITE-7667] Improve string-literal encoding in pushdown translators 
(follow-up)
---
 .../org/apache/calcite/adapter/pig/PigFilter.java    |  9 ++++++---
 .../adapter/pig/PigFilterLiteralEscapeTest.java      | 20 ++++++++++++++++----
 .../java/org/apache/calcite/test/PigAdapterTest.java |  6 +++---
 3 files changed, 25 insertions(+), 10 deletions(-)

diff --git a/pig/src/main/java/org/apache/calcite/adapter/pig/PigFilter.java 
b/pig/src/main/java/org/apache/calcite/adapter/pig/PigFilter.java
index f36b0dd6f5..cc6a8a8c0f 100644
--- a/pig/src/main/java/org/apache/calcite/adapter/pig/PigFilter.java
+++ b/pig/src/main/java/org/apache/calcite/adapter/pig/PigFilter.java
@@ -135,9 +135,12 @@ private static boolean containsOnlyConjunctions(RexNode 
condition) {
    * Converts a literal to a Pig Latin string literal.
    */
   private static String getLiteralAsString(RexLiteral literal) {
-    // Pig Latin string literals use `''` to represent a single `'` inside
-    // a `'...'` literal, so double any embedded `'` before wrapping
     final String raw = RexLiteral.stringValue(literal);
-    return '\'' + (raw != null ? raw.replace("'", "''") : null) + '\'';
+    // Escape before wrapping
+    return '\''
+        + (raw != null
+            ? raw.replace("\\", "\\\\").replace("'", "\\'")
+            : null)
+        + '\'';
   }
 }
diff --git 
a/pig/src/test/java/org/apache/calcite/adapter/pig/PigFilterLiteralEscapeTest.java
 
b/pig/src/test/java/org/apache/calcite/adapter/pig/PigFilterLiteralEscapeTest.java
index f90bbb3cbd..a2cbebc2b4 100644
--- 
a/pig/src/test/java/org/apache/calcite/adapter/pig/PigFilterLiteralEscapeTest.java
+++ 
b/pig/src/test/java/org/apache/calcite/adapter/pig/PigFilterLiteralEscapeTest.java
@@ -57,20 +57,32 @@ private static String call(RexLiteral literal) throws 
Throwable {
     assertThat(call(charLiteral("alice")), is("'alice'"));
   }
 
+  @Test void valueWithBackslash() throws Throwable {
+    assertThat(call(charLiteral("a\\b")), is("'a\\\\b'"));
+  }
+
+  @Test void valueWithBackslashBeforeApostrophe() throws Throwable {
+    assertThat(call(charLiteral("a\\'b")), is("'a\\\\\\'b'"));
+  }
+
+  @Test void valueWithTrailingBackslash() throws Throwable {
+    assertThat(call(charLiteral("a\\")), is("'a\\\\'"));
+  }
+
   @Test void valueWithApostrophe() throws Throwable {
-    assertThat(call(charLiteral("O'Brien")), is("'O''Brien'"));
+    assertThat(call(charLiteral("O'Brien")), is("'O\\'Brien'"));
   }
 
   @Test void valueWithApostropheAtTheEnd() throws Throwable {
-    assertThat(call(charLiteral("a'")), is("'a'''"));
+    assertThat(call(charLiteral("a'")), is("'a\\''"));
   }
 
   @Test void valueWithApostropheAtTheStart() throws Throwable {
-    assertThat(call(charLiteral("'a")), is("'''a'"));
+    assertThat(call(charLiteral("'a")), is("'\\'a'"));
   }
 
   @Test void valueWithMultipleApostrophes() throws Throwable {
-    assertThat(call(charLiteral("a''b")), is("'a''''b'"));
+    assertThat(call(charLiteral("a''b")), is("'a\\'\\'b'"));
   }
 
   @Test void emptyValue() throws Throwable {
diff --git a/pig/src/test/java/org/apache/calcite/test/PigAdapterTest.java 
b/pig/src/test/java/org/apache/calcite/test/PigAdapterTest.java
index 62b247a756..18a61e468b 100644
--- a/pig/src/test/java/org/apache/calcite/test/PigAdapterTest.java
+++ b/pig/src/test/java/org/apache/calcite/test/PigAdapterTest.java
@@ -58,9 +58,9 @@ class PigAdapterTest extends AbstractPigTest {
   }
 
   @Test void testFilterWithSingleQuote() {
-    // A string literal containing a single quote must be doubled per Pig Latin
+    // A string literal containing a single quote must be escaped per Pig Latin
     // string-literal rules so it does not break out of the '...' literal in
-    // the generated FILTER statement.
+    // the generated FILTER statement. Verified against pig.
     CalciteAssert.that()
         .with(MODEL)
         .query("select * from \"t\" where \"tc0\" = 'a''b'")
@@ -69,7 +69,7 @@ class PigAdapterTest extends AbstractPigTest {
             pigScriptChecker("t = LOAD '"
                 + getFullPathForTestDataFile("data.txt")
                 + "' USING PigStorage() AS (tc0:chararray, tc1:chararray);\n"
-                + "t = FILTER t BY (tc0 == 'a''b');"));
+                + "t = FILTER t BY (tc0 == 'a\\'b');"));
   }
 
   @Test void testImplWithMultipleFilters() {

Reply via email to