raghav-reglobe opened a new pull request, #71333:
URL: https://github.com/apache/doris/pull/71333
### Background
A row policy's predicate and a data mask's expression are stored as SQL
text. `LogicalCheckPolicy.findPolicy` parses that text for every statement that
reads the table, and the binder resolves the function names in it together with
the rest of the statement.
### What problem does this PR solve?
Issue Number: None
Related PR: None
PR Type: Bugfix
Problem Summary:
The function names inside a policy expression were resolved with the same
lookup as the statement around them, and that lookup reads the session variable
`prefer_udf_over_builtin` and the session's current database. A policy is
written once for a table, so its functions should mean the same thing in every
session. With this PR an unqualified function name inside a policy expression
is resolved by one fixed rule: the built-in function of that name if there is
one, otherwise a user function in the database of the table the policy is on
(internal catalog only), then a global user function. A qualified name
(`db.fn(...)`) and every expression outside a policy are resolved as before.
More than half of the diff is tests.
### Details
- **Change map:** 705 changed lines: 274 of production code and 431 of tests.
- `FunctionLookupScope` (new, 90 lines): a thread-local scope that nests.
It names the database an unqualified user function may come from.
- `FunctionRegistry` (11 lines): while a scope is open, the built-in is
looked up first, and the scope's database takes the place of the session's
current database.
- `PolicyExpressionScope` (new, 131 lines): an unbound wrapper expression.
Visiting it opens the scope around the visit of its whole subtree.
- `LogicalCheckPolicy.findPolicy` (25 lines): wraps each data mask
expression and each conjunct of the merged row filter.
- `ExpressionAnalyzer` and `ExpressionVisitor` (17 lines): bind the child
and drop the wrapper, so nothing after analysis sees it.
- **Frequency:** Only where user functions are defined. Without them both
lookups give the same result.
- **Root cause:** A policy expression had no lookup rule of its own and took
the session's.
- **Why existing tests missed it:** No test bound a policy expression in a
session with a non-default `prefer_udf_over_builtin` or current database.
- **Fix:** As in the Problem Summary. The conjuncts that `CheckPolicy`
splits out of the merged row filter are the same as before, each wrapped.
- **Other paths and branches:** Row filters and data masks from every
authorization source pass through `findPolicy`, so both are covered, for
internal and external catalogs. For a table outside the internal catalog only
global user functions are considered.
- **Behavior change:** Inside a policy expression an unqualified user
function is taken from the database of the policy's table and then from the
global functions, not from the session's current database, and a built-in is
used even when `prefer_udf_over_builtin` is on. A policy that means a user
function of another database, or a user function that has the name of a
built-in, should write the name qualified.
### Testing
FE unit tests, run in a build pod on apache/doris master fc947b47d1f plus
this commit: 41 classes, 531 tests, 0 failures, 0 errors. `mvn checkstyle:check
-pl fe-core` passes.
- `PolicyFunctionResolutionTest` (new, 9 cases): row policies on a table and
on a view, nested calls, merged RESTRICTIVE and PERMISSIVE policies, tables
inside a CTE and a join, a global user function, an unqualified user function
that has no built-in of its name, a qualified name, and the nesting of scopes.
Each policy case binds one statement in four sessions (two current databases,
the variable off and on) and checks that the policy's function is the same in
all four, while the statement's own call of the same name follows the session.
- `TestCheckPrivileges`: one more block, with a data mask and a row filter
supplied by a catalog access controller.
- `CheckRowPolicyTest` and `RowPolicySqlModeTest`: they look at the
conjuncts before binding, so they unwrap the new node.
No regression suite is added.
### Release note
Function names inside a row policy predicate or a data mask expression are
resolved the same way in every session: the built-in first, then a user
function in the database of the policy's table, then a global user function.
`prefer_udf_over_builtin` and the current database no longer affect them.
### Check List (For Author)
- Behavior changed: Yes (see Details)
- Does this need documentation: No
- Pre-submission status: Ready
🤖 Generated with [Claude Code](https://claude.com/claude-code)
--
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]