streaming-sql-engine 0.1.15__tar.gz → 0.1.17__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {streaming_sql_engine-0.1.15/streaming_sql_engine.egg-info → streaming_sql_engine-0.1.17}/PKG-INFO +1 -1
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/__init__.py +1 -1
- streaming_sql_engine-0.1.17/documentation/CARTESIAN_PRODUCTS_CAUSES.md +265 -0
- streaming_sql_engine-0.1.17/documentation/ENGINE_OPTIONS_REFERENCE.md +264 -0
- streaming_sql_engine-0.1.17/documentation/HANDLING_DUPLICATES_WITHOUT_CARTESIAN.md +320 -0
- streaming_sql_engine-0.1.17/documentation/WHERE_POLARS_IS_USED.md +284 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/executor.py +460 -448
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/operators.py +4 -4
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/operators_polars.py +21 -17
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/pyproject.toml +68 -68
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/setup.py +69 -69
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17/streaming_sql_engine.egg-info}/PKG-INFO +1 -1
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/streaming_sql_engine.egg-info/SOURCES.txt +4 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/DEPLOYMENT.md +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/MANIFEST.in +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/QUICK_DEPLOY.md +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/QUICK_FIX.md +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/README.md +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/documentation/APACHE_DRILL_COMPARISON.md +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/documentation/API_JOIN_OPERATIONS.md +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/documentation/ARCHITECTURAL_RECOMMENDATION.md +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/documentation/ARCHITECTURE_DECISION.md +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/documentation/ARCHITECTURE_SYNCED.md +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/documentation/BEST_USE_CASES.md +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/documentation/BETTER_FILTER_APPROACH.md +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/documentation/CHANGES_SUMMARY.md +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/documentation/COMPREHENSIVE_ARTICLE.md +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/documentation/COMPREHENSIVE_DOCUMENTATION.md +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/documentation/DEVELOPER_GUIDE.md +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/documentation/DUCKDB_COMPARISON.md +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/documentation/FINAL_IMPLEMENTATION_RECOMMENDATIONS.md +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/documentation/FINAL_RECOMMENDATION.md +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/documentation/FIRST_MATCH_ONLY_WARNINGS.md +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/documentation/HOW_PROTOCOL_DETECTION_WORKS.md +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/documentation/HOW_TO_ADD_TO_PROJECT.md +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/documentation/INSTALLATION.md +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/documentation/JSONL_EXECUTOR_ANALYSIS.md +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/documentation/JSONL_EXECUTOR_REMOVED.md +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/documentation/LIBRARY_DOCUMENTATION.md +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/documentation/MEDIUM_ARTICLE.md +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/documentation/MIGRATION_GUIDE.md +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/documentation/MULTI_SOURCE_EXAMPLE_README.md +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/documentation/MYSQL_USAGE.md +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/documentation/OPTIMIZATIONS_BREAKDOWN.md +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/documentation/OPTIMIZATIONS_NOT_DATABASE_ONLY.md +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/documentation/OPTIMIZATIONS_VS_POLARS.md +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/documentation/OPTIMIZER_AUTOMATED.md +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/documentation/PERFORMANCE.md +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/documentation/PERFORMANCE_COMPARISON.md +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/documentation/POLARS_AND_MMAP_GUIDE.md +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/documentation/PROTOCOL_AND_OPTIMIZATIONS_EXPLAINED.md +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/documentation/PROTOCOL_EXPLANATION.md +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/documentation/PUBLISHING.md +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/documentation/PYPI_CHECKLIST.md +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/documentation/QUICK_PUBLISH.md +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/documentation/QUICK_START.md +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/documentation/SIMPLE_PROTOCOL_GUIDE.md +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/documentation/SIMPLE_SOURCE_FUNCTIONS.md +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/documentation/SOURCE_COMBINATIONS_GUIDE.md +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/documentation/TECHNICAL_DOCUMENTATION.md +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/documentation/USER_GUIDE.md +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/documentation/WHAT_IS_WHERE_TO_PARAMS.md +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/documentation/WHEN_POLARS_IS_USED.md +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/documentation/WHY_API_CONVERTER_NEEDED.md +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/documentation/WHY_MANUAL_PARSING.md +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/documentation/WHY_ORIGINAL_IS_FASTER.md +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/documentation/WHY_PROTOCOL_IN_SOURCES.md +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/documentation/XML_MONGO_USE_CASE.md +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/engine.py +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/evaluator.py +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/mmap_index.py +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/operators_mmap.py +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/optimizer.py +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/parser.py +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/planner.py +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/polars_expression_translator.py +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/polars_operators.py +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/protocol_helpers.py +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/requirements.txt +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/setup.cfg +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/streaming_sql_engine.egg-info/dependency_links.txt +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/streaming_sql_engine.egg-info/requires.txt +0 -0
- {streaming_sql_engine-0.1.15 → streaming_sql_engine-0.1.17}/streaming_sql_engine.egg-info/top_level.txt +0 -0
{streaming_sql_engine-0.1.15/streaming_sql_engine.egg-info → streaming_sql_engine-0.1.17}/PKG-INFO
RENAMED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: streaming-sql-engine
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.17
|
|
4
4
|
Summary: A lightweight SQL execution engine for streaming row-by-row queries with joins across multiple data sources
|
|
5
5
|
Home-page: https://github.com/Ierofantis/streaming-sql-engine/tree/master
|
|
6
6
|
Author: Theodore Pantazopoulo
|
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
# What Causes Cartesian Products in the Engine
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
Cartesian products occur when **multiple rows match the same join key**, creating all possible combinations. This multiplies the output rows dramatically.
|
|
6
|
+
|
|
7
|
+
## Root Causes
|
|
8
|
+
|
|
9
|
+
### 1. **Duplicate Keys on Right Side** (Most Common)
|
|
10
|
+
|
|
11
|
+
**What happens:**
|
|
12
|
+
- Right side has multiple rows with the same join key value
|
|
13
|
+
- Each left row matches ALL right rows with that key
|
|
14
|
+
- Result: `left_rows × right_rows_with_same_key`
|
|
15
|
+
|
|
16
|
+
**Example with your data:**
|
|
17
|
+
```json
|
|
18
|
+
// Spryker (left) - 1 row
|
|
19
|
+
{"product_offer_reference": "MER304--.--8848--.--SF-14041057"}
|
|
20
|
+
|
|
21
|
+
// Mongo (right) - 3 rows with same merchant_reference
|
|
22
|
+
{"merchant_reference": "MER304", "price": "8.50"}
|
|
23
|
+
{"merchant_reference": "MER304", "price": "9.00"} // duplicate!
|
|
24
|
+
{"merchant_reference": "MER304", "price": "10.00"} // duplicate!
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
**If joining on `merchant_reference`:**
|
|
28
|
+
- 1 left row × 3 right rows = **3 output rows** (cartesian product)
|
|
29
|
+
|
|
30
|
+
**Code location:** `operators.py:220-232`
|
|
31
|
+
```python
|
|
32
|
+
if key_value not in self.lookup_index:
|
|
33
|
+
self.lookup_index[key_value] = []
|
|
34
|
+
self.lookup_index[key_value].append(prefixed_row)
|
|
35
|
+
else:
|
|
36
|
+
# Key already exists - ADD to list (not overwrite)
|
|
37
|
+
if deduplicate:
|
|
38
|
+
# Skip duplicates - only keep first match
|
|
39
|
+
duplicate_count += 1
|
|
40
|
+
else:
|
|
41
|
+
# Keep all matches (normal SQL behavior) ← CARTESIAN PRODUCT HERE
|
|
42
|
+
self.lookup_index[key_value].append(prefixed_row)
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### 2. **Duplicate Keys on Left Side**
|
|
46
|
+
|
|
47
|
+
**What happens:**
|
|
48
|
+
- Left side has multiple rows with the same join key value
|
|
49
|
+
- Each left row matches the same right rows
|
|
50
|
+
- Result: `left_rows_with_same_key × right_rows`
|
|
51
|
+
|
|
52
|
+
**Example:**
|
|
53
|
+
```json
|
|
54
|
+
// Spryker (left) - 2 rows with same reference
|
|
55
|
+
{"product_offer_reference": "MER304--.--8848--.--SF-14041057"}
|
|
56
|
+
{"product_offer_reference": "MER304--.--8848--.--SF-14041057"} // duplicate!
|
|
57
|
+
|
|
58
|
+
// Mongo (right) - 1 row
|
|
59
|
+
{"merchant_reference": "MER304"}
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
**If joining on matching keys:**
|
|
63
|
+
- 2 left rows × 1 right row = **2 output rows** (cartesian product)
|
|
64
|
+
|
|
65
|
+
**Code location:** `operators.py:273-280`
|
|
66
|
+
```python
|
|
67
|
+
matches = self.lookup_index.get(left_key_value, [])
|
|
68
|
+
self._right_matches = [m for m in matches if m is not None]
|
|
69
|
+
|
|
70
|
+
# If first_match_only mode, only take first match
|
|
71
|
+
if self.first_match_only and len(self._right_matches) > 1:
|
|
72
|
+
self._right_matches = self._right_matches[:1] # ← Prevents cartesian
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### 3. **`first_match_only=False` (Default)**
|
|
76
|
+
|
|
77
|
+
**What happens:**
|
|
78
|
+
- Engine keeps ALL matches per key (standard SQL behavior)
|
|
79
|
+
- If duplicates exist, all combinations are returned
|
|
80
|
+
|
|
81
|
+
**Code location:** `operators.py:163, 185`
|
|
82
|
+
```python
|
|
83
|
+
def __init__(self, ..., first_match_only=False): # ← Default is False
|
|
84
|
+
...
|
|
85
|
+
self._build_index(deduplicate=first_match_only) # ← Passes False
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
**With `first_match_only=False`:**
|
|
89
|
+
- Right side duplicates → All matches kept → Cartesian products
|
|
90
|
+
- Left side duplicates → Each left row gets all matches → Cartesian products
|
|
91
|
+
|
|
92
|
+
**With `first_match_only=True`:**
|
|
93
|
+
- Right side duplicates → Only first match kept → No cartesian products
|
|
94
|
+
- Left side duplicates → Each left row gets only first match → No cartesian products
|
|
95
|
+
|
|
96
|
+
### 4. **Merge Join with Unsorted Data**
|
|
97
|
+
|
|
98
|
+
**What happens:**
|
|
99
|
+
- Merge join assumes sorted data
|
|
100
|
+
- On unsorted data, it buffers all rows with same key
|
|
101
|
+
- Creates cartesian products when duplicates exist
|
|
102
|
+
|
|
103
|
+
**Code location:** `operators.py:422-438`
|
|
104
|
+
```python
|
|
105
|
+
def _fill_right_buffer(self, target_key):
|
|
106
|
+
"""Fill right buffer with all rows matching target_key."""
|
|
107
|
+
self._right_buffer = []
|
|
108
|
+
# Collect all right rows with matching key ← CARTESIAN PRODUCT HERE
|
|
109
|
+
while right_key_value == target_key:
|
|
110
|
+
self._right_buffer.append(self._right_row)
|
|
111
|
+
if not self._advance_right():
|
|
112
|
+
break
|
|
113
|
+
right_key_value = self._get_key_value(self._right_row, self.right_key)
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
**Note:** Merge join does NOT support `first_match_only` - it always creates cartesian products with duplicates.
|
|
117
|
+
|
|
118
|
+
### 5. **Mmap Join with Duplicates**
|
|
119
|
+
|
|
120
|
+
**What happens:**
|
|
121
|
+
- Mmap join stores file positions for all rows with same key
|
|
122
|
+
- Returns all matches (no deduplication)
|
|
123
|
+
- Creates cartesian products
|
|
124
|
+
|
|
125
|
+
**Code location:** `operators_mmap.py`
|
|
126
|
+
- Mmap join does NOT support `first_match_only` - it always creates cartesian products with duplicates.
|
|
127
|
+
|
|
128
|
+
## Your Specific Case
|
|
129
|
+
|
|
130
|
+
### Data Analysis:
|
|
131
|
+
|
|
132
|
+
**Spryker data:**
|
|
133
|
+
```json
|
|
134
|
+
{
|
|
135
|
+
"product_offer_reference": "MER304--.--8848--.--SF-14041057",
|
|
136
|
+
"merchant_reference": "MER304"
|
|
137
|
+
}
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
**Mongo data:**
|
|
141
|
+
```json
|
|
142
|
+
{
|
|
143
|
+
"merchant_reference": "MER304",
|
|
144
|
+
"product_id": 34017149
|
|
145
|
+
}
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
### Potential Cartesian Product Scenarios:
|
|
149
|
+
|
|
150
|
+
**Scenario 1: Joining on `merchant_reference`**
|
|
151
|
+
- If Mongo has multiple rows with `merchant_reference: "MER304"`
|
|
152
|
+
- Each Spryker row matches ALL Mongo rows with that reference
|
|
153
|
+
- Result: `spryker_rows × mongo_rows_with_MER304`
|
|
154
|
+
|
|
155
|
+
**Scenario 2: Joining on `product_offer_reference`**
|
|
156
|
+
- If Spryker has multiple rows with same `product_offer_reference`
|
|
157
|
+
- Each Mongo row matches ALL Spryker rows with that reference
|
|
158
|
+
- Result: `mongo_rows × spryker_rows_with_same_reference`
|
|
159
|
+
|
|
160
|
+
**Scenario 3: Wrong Join Key**
|
|
161
|
+
- If joining on fields that don't match exactly
|
|
162
|
+
- Example: `spryker.product_offer_reference` vs `mongo.merchant_reference`
|
|
163
|
+
- These don't match → No matches, but if they did match partially, could create cartesian products
|
|
164
|
+
|
|
165
|
+
## How to Prevent Cartesian Products
|
|
166
|
+
|
|
167
|
+
### Solution 1: Use `first_match_only=True`
|
|
168
|
+
|
|
169
|
+
```python
|
|
170
|
+
engine = Engine(debug=True, use_polars=True, first_match_only=True)
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
**What it does:**
|
|
174
|
+
- Deduplicates right side during index building (keeps only first match per key)
|
|
175
|
+
- Returns only first match per left key
|
|
176
|
+
- Prevents cartesian products
|
|
177
|
+
|
|
178
|
+
**Limitations:**
|
|
179
|
+
- Only works with `LookupJoinIterator` (Python) and `PolarsLookupJoinIterator`
|
|
180
|
+
- Does NOT work with `MergeJoinIterator` or `MmapLookupJoinIterator`
|
|
181
|
+
|
|
182
|
+
### Solution 2: Ensure Unique Join Keys
|
|
183
|
+
|
|
184
|
+
**Before joining:**
|
|
185
|
+
- Deduplicate data sources
|
|
186
|
+
- Ensure join keys are unique
|
|
187
|
+
- Use `DISTINCT` or group by join key
|
|
188
|
+
|
|
189
|
+
### Solution 3: Use Polars (with `first_match_only=True`)
|
|
190
|
+
|
|
191
|
+
```python
|
|
192
|
+
engine = Engine(debug=True, use_polars=True, first_match_only=True)
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
**Why:**
|
|
196
|
+
- Polars supports `first_match_only` deduplication
|
|
197
|
+
- Faster than Python fallback
|
|
198
|
+
- Prevents cartesian products
|
|
199
|
+
|
|
200
|
+
### Solution 4: Avoid Merge Join
|
|
201
|
+
|
|
202
|
+
**Don't use:**
|
|
203
|
+
```python
|
|
204
|
+
engine = Engine(use_polars=False) # ← Allows merge join
|
|
205
|
+
engine.register("table1", source1, ordered_by="key") # ← Enables merge join
|
|
206
|
+
engine.register("table2", source2, ordered_by="key") # ← Enables merge join
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
**Use instead:**
|
|
210
|
+
```python
|
|
211
|
+
engine = Engine(use_polars=True, first_match_only=True) # ← Forces Polars/Python lookup
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
## Detection
|
|
215
|
+
|
|
216
|
+
### Debug Output Shows:
|
|
217
|
+
|
|
218
|
+
**When cartesian products occur:**
|
|
219
|
+
```
|
|
220
|
+
Index built: 1,000,000 rows, 500,000 unique keys ← Many duplicates!
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
**When deduplication happens:**
|
|
224
|
+
```
|
|
225
|
+
⚠️ Deduplicated: 500,000 duplicate keys removed (kept first match only)
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
**When Polars deduplicates:**
|
|
229
|
+
```
|
|
230
|
+
[POLARS] ⚠️ Deduplicated: 500,000 duplicate keys removed (kept first match only)
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
### Check Your Data:
|
|
234
|
+
|
|
235
|
+
**Before joining, check for duplicates:**
|
|
236
|
+
```python
|
|
237
|
+
# Count unique vs total rows
|
|
238
|
+
unique_keys = len(set(row['join_key'] for row in data))
|
|
239
|
+
total_rows = len(data)
|
|
240
|
+
duplicates = total_rows - unique_keys
|
|
241
|
+
|
|
242
|
+
if duplicates > 0:
|
|
243
|
+
print(f"⚠️ Warning: {duplicates} duplicate keys found - cartesian products possible!")
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
## Summary
|
|
247
|
+
|
|
248
|
+
**Cartesian products are caused by:**
|
|
249
|
+
1. ✅ **Duplicate keys on right side** + `first_match_only=False`
|
|
250
|
+
2. ✅ **Duplicate keys on left side** + `first_match_only=False`
|
|
251
|
+
3. ✅ **Merge join** (always creates cartesian products with duplicates)
|
|
252
|
+
4. ✅ **Mmap join** (always creates cartesian products with duplicates)
|
|
253
|
+
|
|
254
|
+
**Prevent by:**
|
|
255
|
+
1. ✅ Use `first_match_only=True` (with Python/Polars joins)
|
|
256
|
+
2. ✅ Ensure unique join keys in data
|
|
257
|
+
3. ✅ Use Polars with `first_match_only=True`
|
|
258
|
+
4. ✅ Avoid merge join when duplicates exist
|
|
259
|
+
|
|
260
|
+
**Your reconciliation script:**
|
|
261
|
+
```python
|
|
262
|
+
self.engine = Engine(debug=True, use_polars=True, first_match_only=True)
|
|
263
|
+
```
|
|
264
|
+
✅ This configuration prevents cartesian products!
|
|
265
|
+
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
# Streaming SQL Engine - Options and Join Selection Reference
|
|
2
|
+
|
|
3
|
+
## Engine Initialization Options
|
|
4
|
+
|
|
5
|
+
### `Engine(debug=False, use_polars=True, first_match_only=False)`
|
|
6
|
+
|
|
7
|
+
**`debug` (bool, default: False)**
|
|
8
|
+
|
|
9
|
+
- Enables verbose logging of execution stages
|
|
10
|
+
- Shows: SQL parsing, logical plan building, iterator selection, join progress
|
|
11
|
+
- Prints which iterator type is used (MERGE JOIN, MMAP LOOKUP JOIN, POLARS LOOKUP JOIN, LOOKUP JOIN (Python))
|
|
12
|
+
- Shows optimization messages (filter pushdown, column pruning)
|
|
13
|
+
- Displays row counts and progress indicators
|
|
14
|
+
|
|
15
|
+
```python
|
|
16
|
+
self.engine = Engine(debug=True, use_polars=True, first_match_only=True)
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
**`use_polars` (bool, default: True)**
|
|
20
|
+
|
|
21
|
+
- Enables Polars acceleration for joins, filtering, and projection
|
|
22
|
+
- When True: Prioritizes Polars over mmap and merge join (unless explicitly overridden)
|
|
23
|
+
- When False: Uses Python-based iterators (fallback)
|
|
24
|
+
- Requires Polars installed (`pip install polars`)
|
|
25
|
+
- Automatically falls back to Python if Polars unavailable or fails
|
|
26
|
+
|
|
27
|
+
```python
|
|
28
|
+
self.engine = Engine(debug=True, use_polars=True, first_match_only=True)
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
**`first_match_only` (bool, default: False)**
|
|
32
|
+
|
|
33
|
+
- Prevents cartesian products from duplicate keys in joins
|
|
34
|
+
- When True: Deduplicates right side during index building, returns only first match per left key
|
|
35
|
+
- When False: Returns all matches (standard SQL behavior, can create cartesian products)
|
|
36
|
+
- Applies to: LookupJoinIterator (Python), PolarsLookupJoinIterator
|
|
37
|
+
- Does NOT apply to: MergeJoinIterator, MmapLookupJoinIterator (they handle duplicates differently)
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
self.engine = Engine(debug=True, use_polars=True, first_match_only=True)
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
---
|
|
44
|
+
|
|
45
|
+
## Source Registration Options
|
|
46
|
+
|
|
47
|
+
### `engine.register(table_name, source_fn, ordered_by=None, filename=None)`
|
|
48
|
+
|
|
49
|
+
**`table_name` (str, required)**
|
|
50
|
+
|
|
51
|
+
- Name used in SQL queries to reference this source
|
|
52
|
+
|
|
53
|
+
**`source_fn` (callable, required)**
|
|
54
|
+
|
|
55
|
+
- Function returning iterator of row dictionaries
|
|
56
|
+
- If accepts `dynamic_where` and/or `dynamic_columns`: Enables filter pushdown and column pruning automatically
|
|
57
|
+
- Protocol detection: Engine checks function signature to detect optimization support
|
|
58
|
+
|
|
59
|
+
**`ordered_by` (str, optional)**
|
|
60
|
+
|
|
61
|
+
- Column name if source is pre-sorted by this column
|
|
62
|
+
- Enables merge join when both sides have matching `ordered_by` metadata
|
|
63
|
+
- Must match the join column name exactly
|
|
64
|
+
- Example: If joining on `product_id`, set `ordered_by="product_id"`
|
|
65
|
+
|
|
66
|
+
```python
|
|
67
|
+
engine.register("products", products_source, ordered_by="product_id")
|
|
68
|
+
engine.register("categories", categories_source, ordered_by="category_id")
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
**`filename` (str, optional)**
|
|
72
|
+
|
|
73
|
+
- File path if source is file-based (JSONL, CSV, etc.)
|
|
74
|
+
- Enables mmap-based joins (90-99% memory reduction)
|
|
75
|
+
- Only used when `use_polars=False` (Polars takes priority when enabled)
|
|
76
|
+
- Stores file positions instead of full rows in memory
|
|
77
|
+
|
|
78
|
+
```python
|
|
79
|
+
engine.register("products", products_source, filename="products.jsonl")
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
---
|
|
83
|
+
|
|
84
|
+
## Join Selection Logic
|
|
85
|
+
|
|
86
|
+
### Priority Order (checked in sequence):
|
|
87
|
+
|
|
88
|
+
**1. MERGE JOIN**
|
|
89
|
+
|
|
90
|
+
- **Conditions (ALL must be true):**
|
|
91
|
+
- `use_polars=False` (Polars explicitly disabled)
|
|
92
|
+
- Left side has `ordered_by` metadata matching left join column
|
|
93
|
+
- Right side has `ordered_by` metadata matching right join column
|
|
94
|
+
- **When used:** Both tables pre-sorted by join keys
|
|
95
|
+
- **Performance:** O(n+m), minimal memory
|
|
96
|
+
- **Limitation:** Requires sorted data, produces incorrect results on unsorted data
|
|
97
|
+
- **Note:** Merge join can create cartesian products if data has duplicates (not deduplicated)
|
|
98
|
+
|
|
99
|
+
```python
|
|
100
|
+
engine = Engine(debug=True, use_polars=False, first_match_only=False)
|
|
101
|
+
engine.register("left_table", left_source, ordered_by="join_key")
|
|
102
|
+
engine.register("right_table", right_source, ordered_by="join_key")
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
**2. MMAP LOOKUP JOIN**
|
|
106
|
+
|
|
107
|
+
- **Conditions (ALL must be true):**
|
|
108
|
+
- `MMAP_AVAILABLE` (mmap module available)
|
|
109
|
+
- `MmapLookupJoinIterator` available
|
|
110
|
+
- `filename` provided in source metadata
|
|
111
|
+
- `use_polars=False` (Polars explicitly disabled)
|
|
112
|
+
- **When used:** File-based sources with memory constraints
|
|
113
|
+
- **Performance:** Similar to in-memory joins, 90-99% memory reduction
|
|
114
|
+
- **Memory:** Stores file positions only, reads rows on-demand from disk
|
|
115
|
+
- **Fallback:** Falls back to Polars/Python if mmap fails
|
|
116
|
+
|
|
117
|
+
```python
|
|
118
|
+
engine = Engine(debug=True, use_polars=False, first_match_only=False)
|
|
119
|
+
engine.register("products", products_source, filename="products.jsonl")
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
**3. POLARS LOOKUP JOIN**
|
|
123
|
+
|
|
124
|
+
- **Conditions (ALL must be true):**
|
|
125
|
+
- `use_polars=True` (explicitly enabled)
|
|
126
|
+
- `POLARS_AVAILABLE` (Polars installed)
|
|
127
|
+
- `should_use_polars()` returns True (right side has ≥10,000 rows estimated)
|
|
128
|
+
- **When used:** Large datasets, Polars enabled, no mmap/merge join conditions met
|
|
129
|
+
- **Performance:** 10-50x faster than Python (SIMD-accelerated)
|
|
130
|
+
- **Memory:** Loads full right side into Polars DataFrame
|
|
131
|
+
- **Features:** Supports `first_match_only` deduplication
|
|
132
|
+
- **Fallback:** Falls back to Python if Polars fails
|
|
133
|
+
|
|
134
|
+
```python
|
|
135
|
+
self.engine = Engine(debug=True, use_polars=True, first_match_only=True)
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
**4. LOOKUP JOIN (Python)**
|
|
139
|
+
|
|
140
|
+
- **Conditions:** Fallback when none of above apply
|
|
141
|
+
- **When used:** Small datasets, Polars disabled/unavailable, no mmap/merge conditions
|
|
142
|
+
- **Performance:** Standard Python dict-based lookup
|
|
143
|
+
- **Memory:** Stores full row dictionaries in memory
|
|
144
|
+
- **Features:** Supports `first_match_only` deduplication
|
|
145
|
+
|
|
146
|
+
```python
|
|
147
|
+
engine = Engine(debug=True, use_polars=False, first_match_only=True)
|
|
148
|
+
engine.register("products", products_source)
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
---
|
|
152
|
+
|
|
153
|
+
## Optimization Features
|
|
154
|
+
|
|
155
|
+
### Column Pruning
|
|
156
|
+
|
|
157
|
+
- **What:** Only requests needed columns from sources
|
|
158
|
+
- **When:** Source function accepts `dynamic_columns` parameter
|
|
159
|
+
- **Benefit:** Reduces I/O and memory usage
|
|
160
|
+
- **Automatic:** Detected via function signature inspection
|
|
161
|
+
|
|
162
|
+
### Filter Pushdown
|
|
163
|
+
|
|
164
|
+
- **What:** Pushes WHERE clauses to data sources for early filtering
|
|
165
|
+
- **When:** Source function accepts `dynamic_where` parameter
|
|
166
|
+
- **Benefit:** Reduces rows processed upstream
|
|
167
|
+
- **Automatic:** Detected via function signature inspection
|
|
168
|
+
- **Limitation:** Only pushes filters referencing single table (root table or joined table)
|
|
169
|
+
|
|
170
|
+
### Polars Vectorization
|
|
171
|
+
|
|
172
|
+
- **What:** Uses SIMD instructions for batch processing
|
|
173
|
+
- **When:** `use_polars=True` and Polars available
|
|
174
|
+
- **Applied to:** Joins, filtering (FilterIterator), projection (ProjectIterator)
|
|
175
|
+
- **Benefit:** 10-50x speedup for large datasets
|
|
176
|
+
|
|
177
|
+
### Mmap Memory Efficiency
|
|
178
|
+
|
|
179
|
+
- **What:** Stores file positions instead of full rows
|
|
180
|
+
- **When:** `filename` provided and `use_polars=False`
|
|
181
|
+
- **Benefit:** 90-99% memory reduction for large files
|
|
182
|
+
- **Trade-off:** Slightly slower than in-memory (but still fast due to mmap)
|
|
183
|
+
|
|
184
|
+
---
|
|
185
|
+
|
|
186
|
+
## Join Type Behavior
|
|
187
|
+
|
|
188
|
+
### INNER JOIN
|
|
189
|
+
|
|
190
|
+
- Returns rows where join keys match on both sides
|
|
191
|
+
- If no match: Row skipped
|
|
192
|
+
- With duplicates: Creates cartesian product (unless `first_match_only=True`)
|
|
193
|
+
|
|
194
|
+
### LEFT JOIN
|
|
195
|
+
|
|
196
|
+
- Returns all rows from left side
|
|
197
|
+
- If match found: Combines left + right columns
|
|
198
|
+
- If no match: Right columns set to NULL
|
|
199
|
+
- With duplicates: Creates cartesian product (unless `first_match_only=True`)
|
|
200
|
+
|
|
201
|
+
---
|
|
202
|
+
|
|
203
|
+
## `first_match_only` Behavior
|
|
204
|
+
|
|
205
|
+
### When `first_match_only=True`:
|
|
206
|
+
|
|
207
|
+
**LookupJoinIterator (Python):**
|
|
208
|
+
|
|
209
|
+
- During `_build_index()`: Only keeps first row per key in lookup dict
|
|
210
|
+
- During `__next__()`: If multiple matches found, only returns first
|
|
211
|
+
- Result: Prevents cartesian products, faster execution
|
|
212
|
+
|
|
213
|
+
```python
|
|
214
|
+
engine = Engine(debug=True, use_polars=False, first_match_only=True)
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
**PolarsLookupJoinIterator:**
|
|
218
|
+
|
|
219
|
+
- During `_build_polars_index()`: Uses `group_by().first()` to deduplicate
|
|
220
|
+
- During `__next__()`: If multiple matches found, only returns first
|
|
221
|
+
- Result: Prevents cartesian products, faster execution
|
|
222
|
+
|
|
223
|
+
```python
|
|
224
|
+
self.engine = Engine(debug=True, use_polars=True, first_match_only=True)
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
**MergeJoinIterator:**
|
|
228
|
+
|
|
229
|
+
- NOT supported (merge join handles duplicates via buffering mechanism)
|
|
230
|
+
- Warning: Merge join can still create cartesian products with duplicates
|
|
231
|
+
|
|
232
|
+
**MmapLookupJoinIterator:**
|
|
233
|
+
|
|
234
|
+
- NOT supported (mmap uses position-based index, deduplication not implemented)
|
|
235
|
+
- Warning: Mmap join can still create cartesian products with duplicates
|
|
236
|
+
|
|
237
|
+
---
|
|
238
|
+
|
|
239
|
+
## Decision Flow Summary
|
|
240
|
+
|
|
241
|
+
```
|
|
242
|
+
Is use_polars=True?
|
|
243
|
+
├─ YES → Skip merge join, skip mmap (unless filename explicitly provided)
|
|
244
|
+
│ └─ Use Polars if available and right side ≥10K rows
|
|
245
|
+
│ └─ Fallback to Python if Polars fails/unavailable
|
|
246
|
+
│
|
|
247
|
+
└─ NO → Check merge join conditions
|
|
248
|
+
├─ Both sides sorted? → Use MERGE JOIN
|
|
249
|
+
└─ Not sorted → Check mmap conditions
|
|
250
|
+
├─ Filename provided? → Use MMAP LOOKUP JOIN
|
|
251
|
+
└─ No filename → Use Python LOOKUP JOIN
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
---
|
|
255
|
+
|
|
256
|
+
## Key Takeaways
|
|
257
|
+
|
|
258
|
+
- **`use_polars=True`**: Prioritizes Polars, skips merge join and mmap (unless explicitly overridden)
|
|
259
|
+
- **`first_match_only=True`**: Prevents cartesian products in Python and Polars joins (not merge/mmap)
|
|
260
|
+
- **`ordered_by`**: Enables merge join (only when `use_polars=False`)
|
|
261
|
+
- **`filename`**: Enables mmap join (only when `use_polars=False`)
|
|
262
|
+
- **Merge join**: Requires sorted data, can create cartesian products with duplicates
|
|
263
|
+
- **Polars**: Fastest for unsorted data, supports `first_match_only`
|
|
264
|
+
- **Mmap**: Lowest memory, requires file-based sources, doesn't support `first_match_only`
|