Skip to content

Commit 21b6196

Browse files
committed
added docs for expressions
1 parent 1a5e32a commit 21b6196

File tree

3 files changed

+400
-0
lines changed

3 files changed

+400
-0
lines changed

mkdocs/docs/SUMMARY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
- [Configuration](configuration.md)
2525
- [CLI](cli.md)
2626
- [API](api.md)
27+
- [Row Filter Syntax](row-filter-syntax.md)
28+
- [Expression DSL](expression-dsl.md)
2729
- [Contributing](contributing.md)
2830
- [Community](community.md)
2931
- Releases

mkdocs/docs/expression-dsl.md

Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
# Expression DSL
2+
3+
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.
4+
5+
They are composed of terms, predicates, and logical operators.
6+
7+
## Basic Concepts
8+
9+
### Terms
10+
11+
Terms are the basic building blocks of expressions. They represent references to fields in your data:
12+
13+
```python
14+
from pyiceberg.expressions import Reference
15+
16+
# Create a reference to a field named "age"
17+
age_field = Reference("age")
18+
```
19+
20+
### Predicates
21+
22+
Predicates are expressions that evaluate to a boolean value. They can be combined using logical operators.
23+
24+
#### Comparison Predicates
25+
26+
```python
27+
from pyiceberg.expressions import EqualTo, NotEqualTo, LessThan, LessThanOrEqual, GreaterThan, GreaterThanOrEqual
28+
29+
# age equals 18
30+
age_equals_18 = EqualTo("age", 18)
31+
32+
# age is not equal to 18
33+
age_not_equals_18 = NotEqualTo("age", 18)
34+
35+
# age is less than 18
36+
age_less_than_18 = LessThan("age", 18)
37+
38+
# Less than or equal to
39+
age_less_than_or_equal_18 = LessThanOrEqual("age", 18)
40+
41+
# Greater than
42+
age_greater_than_18 = GreaterThan("age", 18)
43+
44+
# Greater than or equal to
45+
age_greater_than_or_equal_18 = GreaterThanOrEqual("age", 18)
46+
```
47+
48+
#### Set Predicates
49+
50+
```python
51+
from pyiceberg.expressions import In, NotIn
52+
53+
# age is one of 18, 19, 20
54+
age_in_set = In("age", [18, 19, 20])
55+
56+
# age is not 18, 19, oer 20
57+
age_not_in_set = NotIn("age", [18, 19, 20])
58+
```
59+
60+
#### Null Predicates
61+
62+
```python
63+
from pyiceberg.expressions import IsNull, NotNull
64+
65+
# Is null
66+
name_is_null = IsNull("name")
67+
68+
# Is not null
69+
name_is_not_null = NotNull("name")
70+
```
71+
72+
#### String Predicates
73+
74+
```python
75+
from pyiceberg.expressions import StartsWith, NotStartsWith
76+
77+
# TRUE for 'Johnathan', FALSE for 'Johan'
78+
name_starts_with = StartsWith("name", "John")
79+
80+
# FALSE for 'Johnathan', TRUE for 'Johan'
81+
name_not_starts_with = NotStartsWith("name", "John")
82+
```
83+
84+
### Logical Operators
85+
86+
You can combine predicates using logical operators:
87+
88+
```python
89+
from pyiceberg.expressions import And, Or, Not
90+
91+
# TRUE for 25, FALSE for 67 and 15
92+
age_between = And(
93+
GreaterThanOrEqual("age", 18),
94+
LessThanOrEqual("age", 65)
95+
)
96+
97+
# FALSE for 25, TRUE for 67 and 15
98+
age_outside = Or(
99+
LessThan("age", 18),
100+
GreaterThan("age", 65)
101+
)
102+
103+
# NOT operator
104+
not_adult = Not(GreaterThanOrEqual("age", 18))
105+
```
106+
107+
## Advanced Usage
108+
109+
### Complex Expressions
110+
111+
You can build complex expressions by combining multiple predicates and operators:
112+
113+
```python
114+
from pyiceberg.expressions import And, Or, Not, EqualTo, GreaterThan, LessThan, In
115+
116+
# (age >= 18 AND age <= 65) AND (status = 'active' OR status = 'pending')
117+
complex_filter = And(
118+
And(
119+
GreaterThanOrEqual("age", 18),
120+
LessThanOrEqual("age", 65)
121+
),
122+
Or(
123+
EqualTo("status", "active"),
124+
EqualTo("status", "pending")
125+
)
126+
)
127+
128+
# NOT (age < 18 OR age > 65)
129+
age_in_range = Not(
130+
Or(
131+
LessThan("age", 18),
132+
GreaterThan("age", 65)
133+
)
134+
)
135+
```
136+
137+
### Type Safety
138+
139+
The expression DSL provides type safety through Python's type system. When you create expressions, the types are checked at runtime:
140+
141+
```python
142+
from pyiceberg.expressions import EqualTo
143+
144+
# This will work
145+
age_equals_18 = EqualTo("age", 18)
146+
147+
# This will raise a TypeError if the field type doesn't match
148+
age_equals_18 = EqualTo("age", "18") # Will fail if age is an integer field
149+
```
150+
151+
## Best Practices
152+
153+
1. **Use Type Hints**: Always use type hints when working with expressions to catch type-related errors early.
154+
155+
2. **Break Down Complex Expressions**: For complex expressions, break them down into smaller, more manageable parts:
156+
157+
```python
158+
# Instead of this:
159+
complex_filter = And(
160+
And(
161+
GreaterThanOrEqual("age", 18),
162+
LessThanOrEqual("age", 65)
163+
),
164+
Or(
165+
EqualTo("status", "active"),
166+
EqualTo("status", "pending")
167+
)
168+
)
169+
170+
# Do this:
171+
age_range = And(
172+
GreaterThanOrEqual("age", 18),
173+
LessThanOrEqual("age", 65)
174+
)
175+
176+
status_filter = Or(
177+
EqualTo("status", "active"),
178+
EqualTo("status", "pending")
179+
)
180+
181+
complex_filter = And(age_range, status_filter)
182+
```
183+
184+
## Common Pitfalls
185+
186+
1. **Type Mismatches**: Always ensure that the types of your literals match the field types in your schema.
187+
188+
2. **Null Handling**: Be careful when using `IsNull` and `NotNull` predicates with required fields. The expression DSL will automatically optimize these cases:
189+
- `IsNull` on a required field will always return `False`
190+
- `NotNull` on a required field will always return `True`
191+
192+
3. **String Comparisons**: When using string predicates like `StartsWith`, ensure that the field type is actually a string type.
193+
194+
## Examples
195+
196+
Here are some practical examples of using the expression DSL:
197+
198+
### Basic Filtering
199+
200+
```python
201+
202+
from datetime import datetime
203+
from pyiceberg.expressions import (
204+
And,
205+
EqualTo,
206+
GreaterThanOrEqual,
207+
LessThanOrEqual,
208+
GreaterThan,
209+
In
210+
)
211+
212+
active_adult_users_filter = And(
213+
EqualTo("status", "active"),
214+
GreaterThanOrEqual("age", 18)
215+
)
216+
217+
218+
high_value_customers = And(
219+
GreaterThan("total_spent", 1000),
220+
In("membership_level", ["gold", "platinum"])
221+
)
222+
223+
date_range_filter = And(
224+
GreaterThanOrEqual("created_at", datetime(2024, 1, 1)),
225+
LessThanOrEqual("created_at", datetime(2024, 12, 31))
226+
)
227+
```
228+
229+
### Multi-Condition Filter
230+
231+
```python
232+
from pyiceberg.expressions import And, Or, Not, EqualTo, GreaterThan
233+
234+
complex_filter = And(
235+
Not(EqualTo("status", "deleted")),
236+
Or(
237+
And(
238+
EqualTo("type", "premium"),
239+
GreaterThan("subscription_months", 12)
240+
),
241+
EqualTo("type", "enterprise")
242+
)
243+
)
244+
```

0 commit comments

Comments
 (0)