Skip to content

Escape values in the generated SQL-filter catalog snippet - #78

Open
SID-6921 wants to merge 1 commit into
neuralinkcorp:mainfrom
SID-6921:fix/sql-filter-codegen-escaping
Open

SID-6921 wants to merge 1 commit into
neuralinkcorp:mainfrom
SID-6921:fix/sql-filter-codegen-escaping

Conversation

@SID-6921

@SID-6921 SID-6921 commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #77.

The formatSqlFilter: true path of genTableCode (formatSqlPredicate in src/datarepo/export/static_site/src/lib/codegen.ts) interpolated partition values into a SQL string with no escaping:

  • A string value with an embedded single quote (e.g. O'Brien) produced invalid SQL: name = 'O'Brien'.
  • A value with a double quote broke out of the surrounding Python string early: filters="name = 'say "hi"'".
  • contains values containing % or _ were silently treated as LIKE wildcards instead of literal characters.
  • in / not in list values rendered via JavaScript's Array.toString(): id in 1,2 instead of id in (1, 2).
  • Quoting only triggered when type_annotation was exactly "str"/"string", so a large_string or date-typed string value came out unquoted.

This is the SQL-string snippet path specifically. The non-SQL Filter(...) snippet path (formatFilterValue) is a different function with its own open fix in #73; this PR doesn't touch it.

Changes

  • escapeSqlString: doubles embedded ' (standard SQL string-literal escaping).
  • escapeSqlLikeValue: additionally escapes \, %, _ for contains, paired with an explicit ESCAPE '\' clause.
  • in/not in now render list values as a proper (a, b, c) tuple, with each element quoted/escaped individually.
  • Quoting now checks the value's actual runtime type (typeof value === 'string'), not just type_annotation.
  • The whole generated filters="..." argument is now produced via JSON.stringify, which yields a valid Python double-quoted string literal for any content (Python accepts the same \", \, \uXXXX escapes JSON does), instead of raw string interpolation.
  • Updated ExportedTablePartition.value's type to include array values, since in/not in partitions already carry one at runtime (the pre-fix Array.toString() behavior is direct evidence of this; the type was just stale).

Test plan

  • New test/export/test_sql_filter_codegen.py compiles the real codegen.ts/types.ts via tsc and executes the compiled JS, covering all 5 examples from Catalog site: SQL-style code snippet does not escape filter values #77 plus an is null regression check (7 cases total).
  • Verified these are real, reproducible bugs on main by running the identical test suite against the pre-fix compiled code: all 6 content-bearing cases fail there with exactly the symptoms described in Catalog site: SQL-style code snippet does not escape filter values #77 (confirmed via a local script, not included in the PR).
  • tsc --strict compiles clean.
  • black --check clean on the new test file.
  • Ran the existing test/export/ and test/tables/ suites locally; no regressions (some pre-existing, unrelated failures on this Windows dev environment: Windows path-separator mismatches in test_parquet_table.py/test_parquet_pruning_or.py, and a deltalake/arro3 version mismatch in test_deltalake_table.py — neither touches code this PR changes).

formatSqlPredicate (the formatSqlFilter=true path of genTableCode)
interpolated partition values into a SQL string with no escaping, so a
value containing a single quote produced invalid SQL, an embedded
double quote broke out of the surrounding Python string early,
contains values with '%'/'_' were silently treated as LIKE wildcards,
in/not in list values rendered via JavaScript's Array.toString()
(e.g. 'id in 1,2' instead of 'id in (1, 2)'), and quoting only
triggered when type_annotation was exactly 'str'/'string' rather than
by the value's actual runtime type.

Fixes neuralinkcorp#77. The non-SQL Filter(...) snippet path (formatFilterValue) is
a different function and already has its own fix in PR neuralinkcorp#73; this is
scoped to the SQL-string path only.
Copilot AI balanced review requested due to automatic review settings October 5, 2026 23:17

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@SID-6921

SID-6921 commented Oct 7, 2026

Copy link
Copy Markdown
Contributor Author

Friendly check-in — this has been open a couple of days with only automated review so far. Let me know if anything needs adjusting.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Catalog site: SQL-style code snippet does not escape filter values

3 participants