-
Notifications
You must be signed in to change notification settings - Fork 194
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
Count rows as a metadata only operation #1388
Open
tusharchou
wants to merge
16
commits into
apache:main
Choose a base branch
from
tusharchou:main
base: main
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.
+562
−0
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
731542e
Create test_scan_count.py
tusharchou c6c971e
moved test_scan_count.py to tests
tusharchou da18837
implemented count in data scan
tusharchou 3104a2f
tested table scan count in test_sql catalog
tusharchou c2740ea
refactoring
tusharchou 90bca84
make lint
tusharchou f7202b9
Merge pull request #1 from tusharchou/gh-1223-count-rows-metadata-onl…
tusharchou c7205b3
Merge branch 'apache:main' into main
tusharchou 09f9c10
Merge branch 'apache:main' into main
tusharchou 1e9da22
Merge branch 'apache:main' into main
tusharchou 3ab20d4
Merge branch 'apache:main' into main
tusharchou 091c0af
implemeted residual_evaluator.py with tests
tusharchou 3cd797d
added license
tusharchou 6b0924e
fixed lint
tusharchou 96cb4e9
fixed lint errors
tusharchou 212c83b
Merge pull request #3 from tusharchou/gh-1223-metadata-only-row-count
tusharchou 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 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,247 @@ | ||
# 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 abc import ABC | ||
from typing import Any, Set | ||
|
||
from pyiceberg.expressions import And, Or | ||
from pyiceberg.expressions.literals import Literal | ||
from pyiceberg.expressions.visitors import ( | ||
AlwaysFalse, | ||
AlwaysTrue, | ||
BooleanExpression, | ||
BoundBooleanExpressionVisitor, | ||
BoundPredicate, | ||
BoundTerm, | ||
Not, | ||
UnboundPredicate, | ||
visit, | ||
) | ||
from pyiceberg.partitioning import PartitionSpec | ||
from pyiceberg.schema import Schema | ||
from pyiceberg.typedef import Record | ||
from pyiceberg.types import L | ||
|
||
|
||
class ResidualVisitor(BoundBooleanExpressionVisitor[BooleanExpression], ABC): | ||
schema: Schema | ||
spec: PartitionSpec | ||
case_sensitive: bool | ||
|
||
def __init__(self, schema: Schema, spec: PartitionSpec, case_sensitive: bool, expr: BooleanExpression): | ||
self.schema = schema | ||
self.spec = spec | ||
self.case_sensitive = case_sensitive | ||
self.expr = expr | ||
|
||
def eval(self, partition_data: Record) -> BooleanExpression: | ||
self.struct = partition_data | ||
return visit(self.expr, visitor=self) | ||
|
||
def visit_true(self) -> BooleanExpression: | ||
return AlwaysTrue() | ||
|
||
def visit_false(self) -> BooleanExpression: | ||
return AlwaysFalse() | ||
|
||
def visit_not(self, child_result: BooleanExpression) -> BooleanExpression: | ||
return Not(child_result) | ||
|
||
def visit_and(self, left_result: BooleanExpression, right_result: BooleanExpression) -> BooleanExpression: | ||
return And(left_result, right_result) | ||
|
||
def visit_or(self, left_result: BooleanExpression, right_result: BooleanExpression) -> BooleanExpression: | ||
return Or(left_result, right_result) | ||
|
||
def visit_is_null(self, term: BoundTerm[L]) -> BooleanExpression: | ||
if term.eval(self.struct) is None: | ||
return AlwaysTrue() | ||
else: | ||
return AlwaysFalse() | ||
|
||
def visit_not_null(self, term: BoundTerm[L]) -> BooleanExpression: | ||
if term.eval(self.struct) is not None: | ||
return AlwaysTrue() | ||
else: | ||
return AlwaysFalse() | ||
|
||
def visit_is_nan(self, term: BoundTerm[L]) -> BooleanExpression: | ||
val = term.eval(self.struct) | ||
if val is None: | ||
return self.visit_true() | ||
else: | ||
return self.visit_false() | ||
|
||
def visit_not_nan(self, term: BoundTerm[L]) -> BooleanExpression: | ||
val = term.eval(self.struct) | ||
if val is not None: | ||
return self.visit_true() | ||
else: | ||
return self.visit_false() | ||
|
||
def visit_less_than(self, term: BoundTerm[L], literal: Literal[L]) -> BooleanExpression: | ||
if term.eval(self.struct) < literal.value: | ||
return self.visit_true() | ||
else: | ||
return self.visit_false() | ||
|
||
def visit_less_than_or_equal(self, term: BoundTerm[L], literal: Literal[L]) -> BooleanExpression: | ||
if term.eval(self.struct) <= literal.value: | ||
return self.visit_true() | ||
else: | ||
return self.visit_false() | ||
|
||
def visit_greater_than(self, term: BoundTerm[L], literal: Literal[L]) -> BooleanExpression: | ||
if term.eval(self.struct) > literal.value: | ||
return self.visit_true() | ||
else: | ||
return self.visit_false() | ||
|
||
def visit_greater_than_or_equal(self, term: BoundTerm[L], literal: Literal[L]) -> BooleanExpression: | ||
if term.eval(self.struct) >= literal.value: | ||
return self.visit_true() | ||
else: | ||
return self.visit_false() | ||
|
||
def visit_equal(self, term: BoundTerm[L], literal: Literal[L]) -> BooleanExpression: | ||
if term.eval(self.struct) == literal.value: | ||
return self.visit_true() | ||
else: | ||
return self.visit_false() | ||
|
||
def visit_not_equal(self, term: BoundTerm[L], literal: Literal[L]) -> BooleanExpression: | ||
if term.eval(self.struct) != literal.value: | ||
return self.visit_true() | ||
else: | ||
return self.visit_false() | ||
|
||
def visit_in(self, term: BoundTerm[L], literals: Set[L]) -> BooleanExpression: | ||
if term.eval(self.struct) in literals: | ||
return self.visit_true() | ||
else: | ||
return self.visit_false() | ||
|
||
def visit_not_in(self, term: BoundTerm[L], literals: Set[L]) -> BooleanExpression: | ||
if term.eval(self.struct) not in literals: | ||
return self.visit_true() | ||
else: | ||
return self.visit_false() | ||
|
||
def visit_starts_with(self, term: BoundTerm[L], literal: Literal[L]) -> BooleanExpression: | ||
eval_res = term.eval(self.struct) | ||
if eval_res is not None and str(eval_res).startswith(str(literal.value)): | ||
return AlwaysTrue() | ||
else: | ||
return AlwaysFalse() | ||
|
||
def visit_not_starts_with(self, term: BoundTerm[L], literal: Literal[L]) -> BooleanExpression: | ||
if not self.visit_starts_with(term, literal): | ||
return AlwaysTrue() | ||
else: | ||
return AlwaysFalse() | ||
|
||
def visit_bound_predicate(self, predicate: BoundPredicate[Any]) -> BooleanExpression: | ||
""" | ||
If there is no strict projection or if it evaluates to false, then return the predicate. | ||
|
||
Get the strict projection and inclusive projection of this predicate in partition data, | ||
then use them to determine whether to return the original predicate. The strict projection | ||
returns true iff the original predicate would have returned true, so the predicate can be | ||
eliminated if the strict projection evaluates to true. Similarly the inclusive projection | ||
returns false iff the original predicate would have returned false, so the predicate can | ||
also be eliminated if the inclusive projection evaluates to false. | ||
|
||
""" | ||
parts = self.spec.fields_by_source_id(predicate.term.ref().field.field_id) | ||
if parts == []: | ||
return predicate | ||
|
||
from pyiceberg.types import StructType | ||
|
||
def struct_to_schema(struct: StructType) -> Schema: | ||
return Schema(*[f for f in struct.fields]) | ||
|
||
for part in parts: | ||
strict_projection = part.transform.strict_project(part.name, predicate) | ||
strict_result = None | ||
|
||
if strict_projection is not None: | ||
bound = strict_projection.bind(struct_to_schema(self.spec.partition_type(self.schema))) | ||
if isinstance(bound, BoundPredicate): | ||
strict_result = super().visit_bound_predicate(bound) | ||
else: | ||
strict_result = bound | ||
|
||
if strict_result is not None and isinstance(strict_result, AlwaysTrue): | ||
return AlwaysTrue() | ||
|
||
inclusive_projection = part.transform.project(part.name, predicate) | ||
inclusive_result = None | ||
if inclusive_projection is not None: | ||
bound_inclusive = inclusive_projection.bind(struct_to_schema(self.spec.partition_type(self.schema))) | ||
if isinstance(bound_inclusive, BoundPredicate): | ||
# using predicate method specific to inclusive | ||
inclusive_result = super().visit_bound_predicate(bound_inclusive) | ||
else: | ||
# if the result is not a predicate, then it must be a constant like alwaysTrue or | ||
# alwaysFalse | ||
inclusive_result = bound_inclusive | ||
if inclusive_result is not None and isinstance(inclusive_result, AlwaysFalse): | ||
return AlwaysFalse() | ||
|
||
return predicate | ||
|
||
def visit_unbound_predicate(self, predicate: UnboundPredicate[L]) -> BooleanExpression: | ||
bound = predicate.bind(self.schema, case_sensitive=True) | ||
|
||
if isinstance(bound, BoundPredicate): | ||
bound_residual = self.visit_bound_predicate(predicate=bound) | ||
# if isinstance(bound_residual, BooleanExpression): | ||
if bound_residual not in (AlwaysFalse(), AlwaysTrue()): | ||
# replace inclusive original unbound predicate | ||
return predicate | ||
|
||
# use the non-predicate residual (e.g. alwaysTrue) | ||
return bound_residual | ||
|
||
# if binding didn't result in a Predicate, return the expression | ||
return bound | ||
|
||
|
||
class ResidualEvaluator(ResidualVisitor): | ||
def residual_for(self, partition_data: Record) -> BooleanExpression: | ||
return self.eval(partition_data) | ||
|
||
|
||
class UnpartitionedResidualEvaluator(ResidualEvaluator): | ||
# Finds the residuals for an Expression the partitions in the given PartitionSpec | ||
def __init__(self, schema: Schema, expr: BooleanExpression): | ||
from pyiceberg.partitioning import UNPARTITIONED_PARTITION_SPEC | ||
|
||
super().__init__(schema=schema, spec=UNPARTITIONED_PARTITION_SPEC, expr=expr, case_sensitive=False) | ||
self.expr = expr | ||
|
||
def residual_for(self, partition_data: Record) -> BooleanExpression: | ||
return self.expr | ||
|
||
|
||
def residual_evaluator_of( | ||
spec: PartitionSpec, expr: BooleanExpression, case_sensitive: bool, schema: Schema | ||
) -> ResidualEvaluator: | ||
if len(spec.fields) != 0: | ||
return ResidualEvaluator(spec=spec, expr=expr, schema=schema, case_sensitive=case_sensitive) | ||
else: | ||
return UnpartitionedResidualEvaluator(schema=schema, expr=expr) |
This file contains 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 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.
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.
this count will not be accurate when there are deletes files
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.
Hi @kevinjqliu thank you for the review. I am trying to account for positional deletes, do you have a suggestion on how that can be achieved?
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.
Yes, this can be widely off, not just because the merge-on-read deletes, but because
plan_files
returns all the files that (might) contain relevant rows. For example, if it cannot be determined if has relevant data, it will be returned byplan_files
.I think there are two ways forward:
task.file.record_count
. We would need to extend this to also see if there are also merge-on-read deletes as Kevin already mentioned, or just fail when there are positional deletes.residual-predicate
in theFileScanTask
. When we run a query, likeday_added = 2024-12-01 and user_id = 10
, then theday_added = 2024-12-01
might be satisfied with the partitioning already. This is the case when the table is partitioned by day, and we know that all the data in the file evaluatestrue
forday_added = 2024-12-01
, then we need to open the file, and filter foruser_id = 10
. If we would leave out theuser_id = 10
, then it would beALWAYS_TRUE
, and then we know that we can just usetask.file.record_count
. This way we could very easily loop over the.plan_files()
:To get to the second step, we first have to port the
ResidualEvaluator
. The java code can be found here, including some excellent tests.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.
Hi @Fokko I have added Residual Evaluator with Tests.
Now I am trying to create the breaking tests for count where delete has occurred and the counts should differ