github-actions[bot] commented on code in PR #41886:
URL: https://github.com/apache/beam/pull/41886#discussion_r4774690164
##########
sdks/python/apache_beam/ml/rag/enrichment/bigquery_vector_search.py:
##########
@@ -32,6 +35,161 @@
_LOGGER = logging.getLogger(__name__)
+_NUMERIC_LITERAL_RE = re.compile(
+ r'[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?')
+_BOOL_LITERALS = frozenset(('TRUE', 'FALSE'))
+_STRING_LITERAL_ESCAPES = {
+ '\\': '\\\\',
+ "'": "\\'",
+ '"': '\\"',
+ '\n': '\\n',
+ '\r': '\\r',
+ '\t': '\\t',
+}
+
+
+def _escape_string_literal_body(value: str) -> str:
+ """Returns ``value`` encoded for use inside a quoted BigQuery literal."""
+ out = []
+ for ch in value:
+ if ch in _STRING_LITERAL_ESCAPES:
+ out.append(_STRING_LITERAL_ESCAPES[ch])
+ elif ord(ch) < 0x20 or ord(ch) == 0x7f:
+ out.append('\\x%02x' % ord(ch))
+ else:
+ out.append(ch)
+ return ''.join(out)
+
+
+class _SqlTemplateScanner:
+ """Tracks the lexical context of a GoogleSQL template as it is scanned.
+
+ Only the literal parts of the template are fed to the scanner, so the
+ context reported for a placeholder is the one in which the template author
+ placed it.
+ """
+ def __init__(self):
+ self.quote = None # One of ', ", `, ''' or """ while inside a literal.
+ self.raw = False
+ self.comment = None # 'line' or 'block' while inside a comment.
+ self.pending_escape = False
+
+ def feed(self, text: str) -> None:
+ i = 0
+ n = len(text)
+ if self.pending_escape and n:
+ self.pending_escape = False
+ i = 1
+ while i < n:
+ c = text[i]
+ if self.comment == 'line':
+ if c == '\n':
+ self.comment = None
+ i += 1
+ elif self.comment == 'block':
+ if text.startswith('*/', i):
+ self.comment = None
+ i += 2
+ else:
+ i += 1
+ elif self.quote:
+ if c == '\\' and not self.raw:
Review Comment:
**🔴 Critical**: In GoogleSQL raw string literals (e.g. `r'...'` or
`r"..."`), a backslash still escapes the immediately following character so
that `\'` does not terminate the literal (e.g. `REGEXP_CONTAINS(col,
r'a\'b')`). Because `and not self.raw` skips backslash handling inside raw
strings, `r'a\'b'` prematurely closes `self.quote` at `\'` and opens a new
non-raw quote at the closing `'`. Any subsequent placeholder in the template
(such as `REGEXP_CONTAINS(col, r'a\'b') AND id = {id}`) then has its
quoted/unquoted state inverted, allowing an unquoted placeholder to be treated
as inside a quoted string literal without numeric/boolean validation.
Removing `and not self.raw` ensures `\` always consumes the next character
when scanning for the closing quote across all quoted literals.
```suggestion
if c == '\\':
```
--
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]