englefly opened a new pull request, #71049:
URL: https://github.com/apache/doris/pull/71049

   ### Background
   
   Nereids enforces column-level SELECT privileges during rewrite. 
`CheckPrivileges` extends the `ColumnPruning` rule: pruning computes which 
columns each table contributes to the query, and the privilege check verifies 
exactly that set. A DISTINCT union is rewritten to `Agg` + `UNION ALL` only 
later, in the set-operation optimization stage, so the privilege pass still 
sees a `LogicalUnion` with the DISTINCT qualifier.
   
   ### What problem does this PR solve?
   
   Issue Number: None
   Related PR: #58237 (introduced the bug)
   PR Type: Bugfix
   
   Problem Summary:
   
   A user granted SELECT on a subset of a table's columns can read the other 
columns through a DISTINCT union. The pruning pass dropped the union's branch 
columns before the privilege check, which then checked only one retained column 
while execution returned them all. The fix requires every branch output column 
during the privilege check; the union output stays intact because pruning it 
would change dedup semantics.
   
   With `t(id, name, secret)` holding `(1, 'alice', 987654321)` and a user 
granted only `GRANT SELECT_PRIV(id)`:
   
   ```sql
   SELECT name, id FROM t UNION SELECT name, id FROM t;
   ```
   
   | | Result |
   |---|---|
   | Before the fix | Returns `alice` and `1`. |
   | After the fix | `Permission denied ... [name]`. |
   
   ### Details
   
   - **Frequency:** Always when a column-level grant is used and a DISTINCT 
union outputs an ungranted column together with a granted column that the 
pruning pass retains. In the test the granted int key is the only retained 
column.
   - **Root cause:** `ColumnPruning.visitLogicalUnion()` sends DISTINCT unions 
to `skipPruneThis()`, which propagates `plan.getOutputExprIdBitSet()` and 
`plan.getExpressions()` only. `LogicalUnion.getExpressions()` returns the 
constant rows only, so the union's real child output slots are not required; 
the child scans are pruned to `selectMinimumColumn()` and `CheckPrivileges` 
checks that single column.
   - **Why existing tests missed it:** The FE `TestCheckPrivileges` covers 
views, CTEs and Ranger column checks but has no set-operation case. The 
`auth_p0` regression suites cover direct, view and MV column grants but never a 
DISTINCT union. The new tests run as a user holding only `select_priv(id)`.
   - **Fix:** The DISTINCT branch now requires every child output column 
(`skipPruneThisAndFirstLevelChildren`), and the union output stays unpruned. 
UNION ALL, INTERSECT and EXCEPT keep their behavior.
   - **Other paths and branches:** Introduced by #58237 (`bf080303f17`); 
present on master. 3.0/3.1 do not contain that commit; 4.x release branches 
were not checked (no local refs). INTERSECT/EXCEPT use the same skip call but 
already rejected the unauthorized column in the probe; the new test keeps them 
covered.
   - **Behavior change:** `select id from (select name, id from t union select 
name, id from t) q` now requires privilege on `name`, because the dedup result 
depends on it; this restores the pre-#58237 behavior.
   
   ### Testing
   
   - `run-fe-ut.sh --run 
org.apache.doris.nereids.privileges.TestCheckPrivilegesUnionDistinct`: fails on 
baseline `3a29606ed56` with `Expected ... AnalysisException to be thrown, but 
nothing was thrown`; passes with the fix at `276cf3324a0`.
   - The same command with `TestCheckPrivileges` and `ColumnPruningTest`: 1/1, 
1/1 and 14/14 pass locally.
   - New `auth_p0` suite `test_select_column_auth_union_distinct` added; not 
run locally because no cluster runs a FE built with the fix. The pipeline's 
`auth_p0` group runs it.
   - A local cluster on an earlier build with byte-identical relevant classes 
returned the ungranted `name` and `secret` values before the fix; UNION ALL, 
INTERSECT and EXCEPT were denied.
   
   ### Notes for Reviewers
   
   - The DISTINCT branch keeps the union output because pruning it changes 
dedup semantics; only the child columns become required. The UNION ALL branch's 
`pruneUnionOutput()` is deliberately not reused.
   - The fix sits in `ColumnPruning` rather than `CheckPrivileges` because the 
privilege check must see the real read column set. The normal optimizer does 
not reach the DISTINCT branch: `BuildAggForUnion` runs earlier.
   
   ### Release note
   
   Users granted SELECT on individual columns can no longer read other columns 
of the same table through `UNION DISTINCT`; such queries now fail with a 
permission error.
   
   ### Check List (For Author)
   
   - Behavior changed: Yes (see Details)
   - Does this need documentation: No
   - Pre-submission status: Partially verified: the new `auth_p0` suite has not 
been run locally; the FE unit test fails before the fix and passes after it.
   
   ### AI Disclosure
   
   - Tool: DeepSeek Harness (deepseek-flash)
   - Generated: code fix, unit test, regression suite, and this description.
   - Human review: not stated
   


-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to