-
Notifications
You must be signed in to change notification settings - Fork 416
Documented row_filter expressions
#1862
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
21b6196
added docs for expressions
norton120 0335463
Update mkdocs/docs/expression-dsl.md
norton120 4a61019
Update mkdocs/docs/row-filter-syntax.md
norton120 5bb4be9
Update mkdocs/docs/expression-dsl.md
norton120 c1eef4b
Update mkdocs/docs/expression-dsl.md
norton120 5b347e4
Update mkdocs/docs/row-filter-syntax.md
norton120 f8ee296
added NaN
norton120 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
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,259 @@ | ||||||
| <!-- | ||||||
| - 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. | ||||||
| --> | ||||||
|
|
||||||
| # Expression DSL | ||||||
|
|
||||||
| The PyIceberg library provides a powerful expression DSL (Domain Specific Language) for building complex row filter expressions. This guide will help you understand how to use the expression DSL effectively. This DSL allows you to build type-safe expressions for use in the `row_filter` scan argument. | ||||||
|
|
||||||
| They are composed of terms, predicates, and logical operators. | ||||||
|
|
||||||
| ## Basic Concepts | ||||||
|
|
||||||
| ### Terms | ||||||
|
|
||||||
| Terms are the basic building blocks of expressions. They represent references to fields in your data: | ||||||
|
|
||||||
| ```python | ||||||
| from pyiceberg.expressions import Reference | ||||||
|
|
||||||
| # Create a reference to a field named "age" | ||||||
| age_field = Reference("age") | ||||||
| ``` | ||||||
|
|
||||||
| ### Predicates | ||||||
|
|
||||||
| Predicates are expressions that evaluate to a boolean value. They can be combined using logical operators. | ||||||
|
|
||||||
| #### Literal Predicates | ||||||
|
|
||||||
| ```python | ||||||
| from pyiceberg.expressions import EqualTo, NotEqualTo, LessThan, LessThanOrEqual, GreaterThan, GreaterThanOrEqual | ||||||
|
|
||||||
| # age equals 18 | ||||||
| age_equals_18 = EqualTo("age", 18) | ||||||
|
|
||||||
| # age is not equal to 18 | ||||||
| age_not_equals_18 = NotEqualTo("age", 18) | ||||||
|
|
||||||
| # age is less than 18 | ||||||
| age_less_than_18 = LessThan("age", 18) | ||||||
|
|
||||||
| # Less than or equal to | ||||||
| age_less_than_or_equal_18 = LessThanOrEqual("age", 18) | ||||||
|
|
||||||
| # Greater than | ||||||
| age_greater_than_18 = GreaterThan("age", 18) | ||||||
|
|
||||||
| # Greater than or equal to | ||||||
| age_greater_than_or_equal_18 = GreaterThanOrEqual("age", 18) | ||||||
| ``` | ||||||
|
|
||||||
| #### Set Predicates | ||||||
|
|
||||||
| ```python | ||||||
| from pyiceberg.expressions import In, NotIn | ||||||
|
|
||||||
| # age is one of 18, 19, 20 | ||||||
| age_in_set = In("age", [18, 19, 20]) | ||||||
|
|
||||||
| # age is not 18, 19, oer 20 | ||||||
| age_not_in_set = NotIn("age", [18, 19, 20]) | ||||||
| ``` | ||||||
|
|
||||||
| #### Unary Predicates | ||||||
|
|
||||||
| ```python | ||||||
| from pyiceberg.expressions import IsNull, NotNull | ||||||
|
|
||||||
| # Is null | ||||||
| name_is_null = IsNull("name") | ||||||
|
|
||||||
| # Is not null | ||||||
| name_is_not_null = NotNull("name") | ||||||
| ``` | ||||||
|
|
||||||
| #### String Predicates | ||||||
|
|
||||||
| ```python | ||||||
| from pyiceberg.expressions import StartsWith, NotStartsWith | ||||||
|
|
||||||
| # TRUE for 'Johnathan', FALSE for 'Johan' | ||||||
| name_starts_with = StartsWith("name", "John") | ||||||
|
|
||||||
| # FALSE for 'Johnathan', TRUE for 'Johan' | ||||||
| name_not_starts_with = NotStartsWith("name", "John") | ||||||
| ``` | ||||||
|
|
||||||
| ### Logical Operators | ||||||
|
|
||||||
| You can combine predicates using logical operators: | ||||||
|
|
||||||
| ```python | ||||||
| from pyiceberg.expressions import And, Or, Not | ||||||
|
|
||||||
| # TRUE for 25, FALSE for 67 and 15 | ||||||
| age_between = And( | ||||||
| GreaterThanOrEqual("age", 18), | ||||||
| LessThanOrEqual("age", 65) | ||||||
| ) | ||||||
|
|
||||||
| # FALSE for 25, TRUE for 67 and 15 | ||||||
| age_outside = Or( | ||||||
| LessThan("age", 18), | ||||||
| GreaterThan("age", 65) | ||||||
| ) | ||||||
|
|
||||||
| # NOT operator | ||||||
| not_adult = Not(GreaterThanOrEqual("age", 18)) | ||||||
| ``` | ||||||
|
|
||||||
| ## Advanced Usage | ||||||
|
|
||||||
| ### Complex Expressions | ||||||
|
|
||||||
| You can build complex expressions by combining multiple predicates and operators: | ||||||
|
|
||||||
| ```python | ||||||
| from pyiceberg.expressions import And, Or, Not, EqualTo, GreaterThan, LessThan, In | ||||||
|
|
||||||
| # (age >= 18 AND age <= 65) AND (status = 'active' OR status = 'pending') | ||||||
| complex_filter = And( | ||||||
| And( | ||||||
| GreaterThanOrEqual("age", 18), | ||||||
| LessThanOrEqual("age", 65) | ||||||
| ), | ||||||
| Or( | ||||||
| EqualTo("status", "active"), | ||||||
| EqualTo("status", "pending") | ||||||
| ) | ||||||
| ) | ||||||
|
|
||||||
| # NOT (age < 18 OR age > 65) | ||||||
| age_in_range = Not( | ||||||
| Or( | ||||||
| LessThan("age", 18), | ||||||
| GreaterThan("age", 65) | ||||||
| ) | ||||||
| ) | ||||||
| ``` | ||||||
|
|
||||||
| ### Type Safety | ||||||
|
|
||||||
| The expression DSL provides type safety through Python's type system. When you create expressions, the types are checked at runtime: | ||||||
|
|
||||||
| ```python | ||||||
| from pyiceberg.expressions import EqualTo | ||||||
|
|
||||||
| # This will work | ||||||
| age_equals_18 = EqualTo("age", 18) | ||||||
|
|
||||||
| # This will raise a TypeError if the field type doesn't match | ||||||
| age_equals_18 = EqualTo("age", "18") # Will fail if age is an integer field | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||
| ``` | ||||||
|
|
||||||
| ## Best Practices | ||||||
|
|
||||||
| 1. **Use Type Hints**: Always use type hints when working with expressions to catch type-related errors early. | ||||||
|
|
||||||
| 2. **Break Down Complex Expressions**: For complex expressions, break them down into smaller, more manageable parts: | ||||||
|
|
||||||
| ```python | ||||||
| # Instead of this: | ||||||
| complex_filter = And( | ||||||
| And( | ||||||
| GreaterThanOrEqual("age", 18), | ||||||
| LessThanOrEqual("age", 65) | ||||||
| ), | ||||||
| Or( | ||||||
| EqualTo("status", "active"), | ||||||
| EqualTo("status", "pending") | ||||||
| ) | ||||||
| ) | ||||||
|
|
||||||
| # Do this: | ||||||
| age_range = And( | ||||||
| GreaterThanOrEqual("age", 18), | ||||||
| LessThanOrEqual("age", 65) | ||||||
| ) | ||||||
|
|
||||||
| status_filter = Or( | ||||||
| EqualTo("status", "active"), | ||||||
| EqualTo("status", "pending") | ||||||
| ) | ||||||
|
|
||||||
| complex_filter = And(age_range, status_filter) | ||||||
| ``` | ||||||
|
|
||||||
| ## Common Pitfalls | ||||||
|
|
||||||
| 1. **Null Handling**: Be careful when using `IsNull` and `NotNull` predicates with required fields. The expression DSL will automatically optimize these cases: | ||||||
| - `IsNull` (and `IsNaN` for doubles/floats) on a required field will always return `False` | ||||||
| - `NotNull` (and `NotNaN` for doubles/floats) on a required field will always return `True` | ||||||
|
|
||||||
| 2. **String Comparisons**: When using string predicates like `StartsWith`, ensure that the field type is actually a string type. | ||||||
|
|
||||||
| ## Examples | ||||||
|
|
||||||
| Here are some practical examples of using the expression DSL: | ||||||
|
|
||||||
| ### Basic Filtering | ||||||
|
|
||||||
| ```python | ||||||
|
|
||||||
|
Comment on lines
+215
to
+216
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| from datetime import datetime | ||||||
| from pyiceberg.expressions import ( | ||||||
| And, | ||||||
| EqualTo, | ||||||
| GreaterThanOrEqual, | ||||||
| LessThanOrEqual, | ||||||
| GreaterThan, | ||||||
| In | ||||||
| ) | ||||||
|
|
||||||
| active_adult_users_filter = And( | ||||||
| EqualTo("status", "active"), | ||||||
| GreaterThanOrEqual("age", 18) | ||||||
| ) | ||||||
|
|
||||||
|
|
||||||
| high_value_customers = And( | ||||||
| GreaterThan("total_spent", 1000), | ||||||
| In("membership_level", ["gold", "platinum"]) | ||||||
| ) | ||||||
|
|
||||||
| date_range_filter = And( | ||||||
| GreaterThanOrEqual("created_at", datetime(2024, 1, 1)), | ||||||
| LessThanOrEqual("created_at", datetime(2024, 12, 31)) | ||||||
| ) | ||||||
| ``` | ||||||
|
|
||||||
| ### Multi-Condition Filter | ||||||
|
|
||||||
| ```python | ||||||
| from pyiceberg.expressions import And, Or, Not, EqualTo, GreaterThan | ||||||
|
|
||||||
| complex_filter = And( | ||||||
| Not(EqualTo("status", "deleted")), | ||||||
| Or( | ||||||
| And( | ||||||
| EqualTo("type", "premium"), | ||||||
| GreaterThan("subscription_months", 12) | ||||||
| ), | ||||||
| EqualTo("type", "enterprise") | ||||||
| ) | ||||||
| ) | ||||||
| ``` | ||||||
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.

Uh oh!
There was an error while loading. Please reload this page.