fix(bigframes): improve error message when unescaped { are found in SQL cells#17346
fix(bigframes): improve error message when unescaped { are found in SQL cells#17346tswast wants to merge 5 commits into
{ are found in SQL cells#17346Conversation
… SQL cells
Hints to the user that they may need to escape `{` and `}` characters by
doubling them, and includes context as to where to correct such errors.
Fixes internal issue b/517909919
There was a problem hiding this comment.
Code Review
This pull request enhances the pyformat utility by tracking replacement field positions in SQL templates, allowing it to raise informative ValueError exceptions with visual error context when variables are missing. Unit tests have been added to verify this behavior. The review feedback suggests several robust improvements: adding a bounds check in _consume_literal to prevent potential IndexError exceptions, wrapping _parse_fields in a try-except block to handle parsing errors gracefully, and conditionally appending the error context to avoid trailing newlines.
| context = get_error_context_at_pos(sql_template, pos) | ||
| raise ValueError( | ||
| f"Undetected variable {name!r} in SQL template. " | ||
| "Did you mean to escape '{' and '}' by doubling them?\n" | ||
| f"{context}" | ||
| ) from e |
There was a problem hiding this comment.
If get_error_context_at_pos returns an empty string (e.g., when the position is -1 or not found), the error message will end with a trailing newline, which looks untidy. We should only append the context if it is non-empty.
context = get_error_context_at_pos(sql_template, pos)
msg = (
f"Undetected variable {name!r} in SQL template. "
"Did you mean to escape '{' and '}' by doubling them?"
)
if context:
msg += f"\n{context}"
raise ValueError(msg) from eThere was a problem hiding this comment.
Since we don't expect this case to happen, I would prefer not to add logic for it.
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Hints to the user that they may need to escape
{and}characters by doubling them, and includes context as to where to correct such errors.Fixes internal issue b/517909919
🦕