JingsongLi commented on code in PR #8076:
URL: https://github.com/apache/paimon/pull/8076#discussion_r3342214232


##########
paimon-python/pypaimon/ray/merge_condition.py:
##########
@@ -0,0 +1,109 @@
+################################################################################
+#  Licensed to the Apache Software Foundation (ASF) under one
+#  or more contributor license agreements.  See the NOTICE file
+#  distributed with this work for additional information
+#  regarding copyright ownership.  The ASF licenses this file
+#  to you under the Apache License, Version 2.0 (the
+#  "License"); you may not use this file except in compliance
+#  with the License.  You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing, software
+#  distributed under the License is distributed on an "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#  See the License for the specific language governing permissions and
+# limitations under the License.
+################################################################################
+
+import re
+from typing import Mapping, Set
+
+import pyarrow as pa
+
+
+_COL_REF_PATTERN = re.compile(r'\b([st])\.(\w+)\b')
+
+
+def _require_datafusion():
+    try:
+        import datafusion
+        return datafusion
+    except ImportError:
+        raise ImportError(
+            "merge_into condition expressions require the 'datafusion' "
+            "package. Install it with: pip install pypaimon[sql]"
+        )
+
+
+_STRING_LITERAL = re.compile(r"'(?:[^']|'')*'")
+
+
+def _strip_string_literals(condition: str) -> str:
+    return _STRING_LITERAL.sub('', condition)
+
+
+def rewrite_condition(condition: str) -> str:
+    parts, last = [], 0
+    for m in _STRING_LITERAL.finditer(condition):
+        parts.append(_COL_REF_PATTERN.sub(r'"\1.\2"', 
condition[last:m.start()]))
+        parts.append(m.group())
+        last = m.end()
+    parts.append(_COL_REF_PATTERN.sub(r'"\1.\2"', condition[last:]))
+    return ''.join(parts)
+
+
+def remap_source_on_keys(
+    rewritten: str, on_map: Mapping[str, str],
+) -> str:
+    for s_col, t_col in on_map.items():
+        rewritten = rewritten.replace(f'"s.{s_col}"', f'"t.{t_col}"')

Review Comment:
   Could we avoid doing this raw replace across the whole rewritten SQL? 
`rewrite_condition()` keeps string literals intact, but this second pass can 
still mutate literals that happen to contain the quoted ON-key token. For 
example, a matched condition like `s.note = '"s.id"' AND s.id = 1` is rewritten 
to `"s.note" = '"s.id"' AND "s.id" = 1`, then this remap turns it into 
`"s.note" = '"t.id"' AND "t.id" = 1`, so DataFusion filters against the wrong 
literal and can skip/update the wrong rows. It would be safer to apply the 
ON-key remap while still respecting SQL string-literal spans (similar to 
`rewrite_condition()`), and add a regression test for literals containing 
`"s.<on-key>"`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to