Repository navigation
fix(frontend): preserve predicate SQL when merging role rules - #33132
Conversation
Qodo reviews are paused for this user.Troubleshooting steps vary by plan Learn more → On a Teams plan? Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center? |
|
This pull request is merged. |
|
mo-bot review · Commit: Review completed. Read the assessment. |
iamlinjunhong
left a comment
There was a problem hiding this comment.
Reviewed ba4f15f01d05a7fb5db2a6a7d7dd39c381588adf. This is the first review.
- [P2] Exercise the production predicate through role-rule execution (test/distributed/cases/zz_accesscontrol/role_rule.sql:325). The BVT checks leading-zero strings, Chinese text, and escaped quotes in top-level predicates. The nested
IN (SELECT … WHERE … IN (...))from issue #22006 is covered by a parse-tree comparison inpkg/frontend/rewrite_rule_test.go:2096. Add a two-role BVT with a lookup table that asserts both roles' permitted rows and excludes the unpadded code. This would cover persisted rule loading, rewrite-hint transport, subquery binding, and the resulting row visibility in the same test.
CI for this head reports 15 successful checks, 17 skipped checks, and 2 neutral checks out of 34. CI Required succeeded. The local MySQL parser suite and git diff --check passed. The local Iceberg test could not build because the checkout lacked the usearch, xxHash, and CRoaring headers. The distributed BVT was not rerun locally.
XuPeng-SH
left a comment
There was a problem hiding this comment.
mo-bot review · mo-bot-local · review
Commit: ba4f15f01d05a7fb5db2a6a7d7dd39c381588adf
Assessment: request_changes
The source-preserving merge addresses the literal-formatting problem, but it mishandles unqualified system-table sources. The earlier review’s nested-subquery validation concern remains open, and the new BVT does not restore its original global table-name setting.
Model: gpt-6.1-terra / max
Session: 01a211d7-e427-70e2-a993-c62f1960cb9e · slot 3 · generation 3
-
P2
pkg/frontend/rewrite_rule.go:1648: Two active roles target information_schema.character_sets with otherwise compatible rules whose sources are unqualified character_sets and CHARACTER_SETS, while the session uses information_schema and lower_case_table_names is 0 or 2.
Impact: The rules are treated as incompatible, so only the later role’s predicate survives and rows allowed by the earlier role disappear.
Evidence: The comparison passes an empty schema to normalizeSystemRelationNames, leaving the two table spellings different. Planning instead resolves an omitted schema from the session’s default database and folds system-table names. The merge then selects the later rule at line 1394. Compare sources using the effective database, and account for changes to that database when reusing the decision. -
P2
test/distributed/cases/zz_accesscontrol/role_rule.sql:325: The reported production rule contains a nested IN subquery with quoted, zero-padded values under two active roles.
Impact: The current regression does not establish that the reported predicate survives hint transport, subquery binding, and execution with the intended row visibility.
Evidence: The frontend unit test compares parsed trees for a nested predicate, while the BVT checks top-level literal predicates and a separate scalar subquery. The prior reviewer identified the same gap. Add a small two-role lookup-table case that checks both permitted row sets and excludes the unpadded value. -
P2
test/distributed/cases/zz_accesscontrol/role_rule.sql:449: The BVT runs on an instance whose existing lower_case_table_names value is 0 or 2.
Impact: It leaves the instance set to 1, changing identifier resolution for later sessions and tests.
Evidence: The new cases change this dynamic global variable and end by assigning a fixed value. The repository testing contract requires restoring the pre-existing global value. Save the value before the cases and restore it afterward.
Validation:
-
The private /tmp and loopback preflight passed; receipt: checks/1795596419786255734-3.json.
-
git diff --check passed for the assigned merge-base and head; receipt: checks/1795597291970221880-3.json.
-
Source tracing covered rule creation, merge fallback, default-database resolution, and the changed BVT. The earlier attempt retained a matching-head source-trace receipt and log.
-
The evidence reports successful current-head UT and two multi-CN BVT jobs; case-level logs were not supplied.
Limitations:
-
The focused frontend probe timed out after 900 seconds during its build and produced no test result; receipt: checks/1795597336579378468-3.json and log: review-probe.log.
-
An earlier probe’s compilation output referenced source inconsistent with the frozen head, so it was not treated as validation.
|
mo-bot review · Commit: Review completed. Read the assessment. |
XuPeng-SH
left a comment
There was a problem hiding this comment.
mo-bot review · mo-bot-local · review
Commit: 73209c0a8fbb2dc0132949a043f4dd423bb3e715
Assessment: request_changes
The three earlier review concerns are addressed. The source-preserving merge reuses the existing parser, but two cases in the new non-UTF-8 hint transport still change observable role-rule behavior.
Model: gpt-6.1-terra / max
Session: 01a211d7-e427-70e2-a993-c62f1960cb9e · slot 3 · generation 3
-
P2
pkg/frontend/rewrite_rule.go:632: An active role rule projects an unaliased binary string containing a raw invalid UTF-8 byte, such as SELECT _binary '<raw 0xff byte>' FROM db.t.
Impact: Hint encoding changes the derived column name from the sanitized original name to the hexadecimal spelling. Clients see different result metadata, and queries referring to the original derived name can no longer bind it.
Evidence: The encoder replaces the string token with 0xff, and pkg/sql/plan/query_builder.go:12903-12916 derives the output name from that spelling. The isolated frontend probe observed the name change from � to 0xff (frontend-probe.log; checks/1795615757590662985-3.json). Preserve the original derived name when re-encoding an unaliased projection. -
P2
pkg/frontend/rewrite_rule.go:643: A persisted role rule contains an invalid UTF-8 byte only inside an ordinary, non-executable SQL comment.
Impact: The new encoder rejects the rule during policy materialization, so even an unrelated SELECT 1 fails until the rule is edited. The previous JSON transport replaced the invalid byte inside the ignored comment without changing the parsed rule.
Evidence: The scanner discards ordinary block comments (pkg/sql/parsers/dialect/mysql/scanner.go:325-349), but roleRuleHintSQL rejects the remaining invalid byte. The isolated probe verified that the original and prior JSON-sanitized rules parse to the same tree, then observed the new error on SELECT 1 (comment-probe.log; checks/1795615891230636513-3.json). Sanitize or omit ordinary comments during transport while retaining fail-closed handling for invalid semantic tokens.
Validation:
-
Reviewed the complete frozen-head diff, linked issue and comments, pinned repository guidance, and all seven discussion items; discussion-assessment.json and checks/1795615016931700332-3.json record coverage.
-
Private /tmp and loopback preflight and git diff --check passed (checks/1795614244279651335-3.json and checks/1795614244275602902-3.json).
-
Source-matched native inputs and retained library hashes were checked, and jemalloc was built with the repository Makefile in the diagnostic directory (native-trees.log, native-verify.log, jemalloc-build.log).
-
The selected MySQL parser tests passed on the isolated executor (parser-tests-native.log; checks/1795615415911374768-3.json). Four selected existing frontend tests passed; that command exited 1 because the binary-heading counterexample failed. The separate comment counterexample also failed as described in the findings.
Limitations:
-
The counterexamples exercise frontend hint, parser and planner helpers through diagnostic Go overlays; a public SQL-service BVT was not rerun locally.
-
The supplied CI snapshot reports successful unit and multi-CN BVT jobs, and the PR body reports 420 role-rule checks, but case-level CI logs were not supplied.
fengttt
left a comment
There was a problem hiding this comment.
This is a string literal quotation bug. Do we really need to mess up with plan, binder, session, parser etc etc?
|
mo-bot review · Commit: Review completed. Read the assessment. |
XuPeng-SH
left a comment
There was a problem hiding this comment.
mo-bot review · mo-bot-local · review
Commit: ae41b66d02e652184223b93833cfc041fcea908b
Assessment: approve
The source-preserving merge fixes the reported loss of quoted role predicates, and the current head addresses the earlier system-schema, nested-subquery, global-mode restoration and binary-hint findings. The additional parser, binder and cache work preserves typed projection identity, source resolution and generated headings across the supported modes; it reuses the existing parser and planner owners. No actionable unresolved defect was established.
Model: gpt-6.1-terra / max
Session: 01a212e9-10a4-7203-9194-162fe0e55042 · slot 0 · generation 13
Validation:
-
Reviewed the full frozen-head delta, linked issue and its comments, and all 10 discussion items; discussion-assessment.json records their dispositions.
-
Private /tmp and loopback preflight passed (checks/1795632430597014633-3.json; preflight.log).
-
Focused parser rewrite-hint tests passed, including binary bytes, ordinary comments, invalid semantic tokens, and chain controls (checks/1795632824817507741-2.json; parser-native.log). Native jemalloc was built from matching checked-in inputs with the repository Makefile (checks/1795632742351878716-2.json; jemalloc-build.log).
-
git diff --check passed for the assigned base and head (checks/1795632867538541509-2.json; diff-check.log). The frozen CI snapshot reports successful shared build, unit tests, static analysis and two multi-CN BVT jobs.
Limitations:
-
The initial parser attempts failed before test execution because the default toolchain was too old and then the jemalloc headers were absent; the successful retry used Go 1.27.1 and the task-local native build.
-
The author reports 420 passing role BVT checks and focused frontend validation on this head; individual CI case logs were not supplied, and the public SQL BVT was not rerun locally.
What type of PR is this?
Which issue(s) this PR fixes:
Related to matrixorigin/matrixflow#22006.
What this PR does / why we need it:
Active roles with compatible projections should expose the union of the rows allowed by their WHERE predicates. Formatting the merged AST as display text removed string quotes before the query was parsed again. It could change a code such as
'0001'into a number or make a text predicate fail to parse.Compose predicates from parser-tracked source ranges, then reparse and check the resulting tree. A scalar subquery already supplies its own parentheses. Compare typed projection expressions in the statement's SQL mode so distinct values or types retain later-role precedence. Only competing rules need their ordered sources for another mode. Before JSON hint encoding, represent non-UTF-8 binary literals as hex and reject invalid bytes outside binary literals.
Allow transaction completion, dropping secondary roles, and a literal session SQL mode reset without evaluating or attaching role rules. Other statements still apply the role policy and fail when the rule cannot be parsed. Fold quoted charset and collation names for comparison while preserving ordinary string values and the source SQL.
Compare quoted and unquoted column identifiers using the parser's identifier comparison value, including inside projection expressions. Preserve the original SQL and keep string literals byte-exact.
Use the reader's table-name mode to compare source scopes and reparse the merged rule. Case-sensitive table aliases keep their spelling. A table-name mode change invalidates cached competing-rule decisions and prepared statements built on them.
Compare system catalog sources using the same case rules as catalog lookup. Compare table aliases separately using the parser's spelling for the active table-name mode.
Resolve mixed-case qualifiers for unaliased system tables after exact scope lookup. Compare Iceberg snapshot literals by the planner's snapshot ID and preserve the case of named refs.
Stop scope lookup on an ambiguous system table qualifier. Only a missing qualifier can continue to an outer scope. Compare explicitly typed Iceberg read refs using the runtime prefix parser and preserve the case of branch and tag names.
Compare the planner's output column names for unaliased expressions before merging. Equivalent typed literals can otherwise expose different implicit names. Trim Iceberg ref whitespace using the same policy as the read path before comparing names.
Compare binary hex values with the binder's one-nibble padding while preserving the original SQL for output column names. Compare Iceberg timestamp literals at the local millisecond precision consumed by scan planning. Equal local milliseconds stay equal across session time zones.
Compare hexadecimal, bit, and binary introducer literals by their canonical bytes within each literal category. Preserve whole leading zero bytes and use the original spelling for implicit output column names.
Validated on Go 1.27.1 with focused frontend tests, the full MySQL parser suite, vet, and lint. All 405 role-rule BVT checks passed. Each equivalent literal pair retained both roles' rows, including the zero-length non-NULL binary value. Focused tests covered all 12 changed executable lines in this follow-up.
Unqualified system-table sources use the session's default schema for case comparison. Prepared policy freshness also checks the default schema when rules compete. The role BVT now covers persisted nested IN predicates with a zero-padded string and restores the original global table-name mode.
For this follow-up, focused frontend tests, vet, and incremental lint passed. The role BVT passed all 420 checks with initial global modes 0 and 2, and both runs restored the initial mode. Focused tests covered 27 of 28 changed executable lines.
Materialized hints carry non-UTF-8 SQL as bytes. Reparsing preserves the original binary literal and its generated column name. The lexer accepts invalid bytes in binary strings and ordinary comments and rejects them in other tokens. Client-written rewrite hints still require SQL strings.
A single-node MySQL protocol probe reproduced the generated-name and comment failures before the fix. After the fix, direct and prepared queries bind the original heading and return byte FF, and a raw-byte comment does not block queries. The role BVT passed 420 of 420 checks. Focused frontend tests, the parser and MySQL parser suites, vet, and incremental lint passed. Focused tests covered 66 of 71 changed executable lines.