-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[python] Support query auth (row filter & column masking) for REST catalog #8136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MgjLLL
wants to merge
9
commits into
apache:master
Choose a base branch
from
MgjLLL:python-query-auth
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,888
−47
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
35dcd3f
[python] Support query auth (row filter & column masking) for REST ca…
MgjLLL 18eec73
[python] Fix query auth correctness on all read paths
MgjLLL 677cdbb
[python] Fix pickle serialization for query auth disabled tables
MgjLLL 85cf00a
[python] Address round-2 review for query auth
MgjLLL 45621f7
[python] Fix flake8 E501 in table_query_auth_test predicate literals
MgjLLL 034cfb1
[python] Add null literal defense and generic field-ref collection fo…
MgjLLL 6f6b75e
[python] Address round-3 review for query auth
MgjLLL aad7740
[python] Fix predicate null semantics, CONCAT_WS alignment, and maski…
MgjLLL 44bdea6
[python] Skip empty filter/masking entries to match Java extractPredi…
MgjLLL File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| ################################################################################ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| ################################################################################ | ||
|
|
||
| from typing import Callable, Dict, List, Optional | ||
|
|
||
| import pyarrow as pa | ||
| import pyarrow.compute as pc | ||
|
|
||
| from pypaimon.common.predicate_json_parser import ( | ||
| extract_referenced_fields, | ||
| parse_predicate_to_batch_filter, | ||
| ) | ||
| from pypaimon.schema.data_types import DataField | ||
|
|
||
|
|
||
| class TableQueryAuthResult: | ||
|
|
||
| def __init__(self, filter: Optional[List[str]], column_masking: Optional[Dict[str, str]]): | ||
| self.filter = [f for f in filter if f] if filter else filter | ||
| self.column_masking = ( | ||
| {k: v for k, v in column_masking.items() if k and v} | ||
| if column_masking else column_masking | ||
| ) | ||
|
|
||
| def convert_plan(self, plan): | ||
| from pypaimon.read.query_auth_split import QueryAuthSplit | ||
| from pypaimon.read.plan import Plan | ||
|
|
||
| if not self.filter and not self.column_masking: | ||
| return plan | ||
| auth_splits = [QueryAuthSplit(split, self) for split in plan.splits()] | ||
| return Plan(auth_splits, snapshot_id=plan.snapshot_id) | ||
|
|
||
| def extract_row_filter(self) -> Optional[Callable[[pa.RecordBatch], pa.Array]]: | ||
| if not self.filter: | ||
| return None | ||
| filters = [parse_predicate_to_batch_filter(json_str) for json_str in self.filter] | ||
| if len(filters) == 1: | ||
| return filters[0] | ||
|
|
||
| def combined(batch: pa.RecordBatch) -> pa.Array: | ||
| result = filters[0](batch) | ||
| for f in filters[1:]: | ||
| result = pc.and_(result, f(batch)) | ||
| return result | ||
| return combined | ||
|
|
||
| def get_extra_fields_for_filter( | ||
| self, | ||
| read_fields: List[DataField], | ||
| table_fields: List[DataField], | ||
| ) -> List[DataField]: | ||
| if not self.filter: | ||
| return [] | ||
| read_field_names = {f.name for f in read_fields} | ||
| extra = [] | ||
| for json_str in self.filter: | ||
| referenced = extract_referenced_fields(json_str) | ||
| for name in referenced: | ||
| if name not in read_field_names: | ||
| field = next((f for f in table_fields if f.name == name), None) | ||
| if field: | ||
| extra.append(field) | ||
| read_field_names.add(name) | ||
| return extra | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Java
TableQueryAuthResult.extractPredicate()skips empty filter strings before deserializing them, but this path tries to parse every entry. If the REST server returnsfilter=[""](or a mix of empty and valid filters), Python will fail the read withJSONDecodeErrorin bothextract_row_filter()andget_extra_fields_for_filter(), while the JVM client treats the empty entry as no-op. Please filter out empty/blank JSON strings before wrapping the plan or before parsing, and add a regression test for empty filter entries.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed. Now filter empty/blank entries at the source — TableQueryAuthResult constructor strips falsy entries from the filter list (
[f for f in filter if f]) and empty keys/values from column_masking dict. AuthMaskingReader also guardsif not tj: continuebefore json.loads. Python truthiness check (if f) matches Java StringUtils.isEmpty semantics: skips null and "" but passes through whitespace strings. Added regression tests for mixed empty/valid filters and empty masking values.