streaming-sql-engine 0.1.7__tar.gz → 0.1.9__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.7/streaming_sql_engine.egg-info → streaming_sql_engine-0.1.9}/PKG-INFO +1 -1
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/__init__.py +1 -1
- streaming_sql_engine-0.1.9/documentation/WHEN_POLARS_IS_USED.md +220 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/engine.py +4 -2
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/executor.py +8 -4
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/operators.py +31 -6
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/operators_mmap.py +3 -3
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/planner.py +91 -11
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/pyproject.toml +1 -1
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/setup.py +1 -1
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9/streaming_sql_engine.egg-info}/PKG-INFO +1 -1
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/streaming_sql_engine.egg-info/SOURCES.txt +1 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/DEPLOYMENT.md +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/MANIFEST.in +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/QUICK_DEPLOY.md +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/QUICK_FIX.md +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/README.md +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/APACHE_DRILL_COMPARISON.md +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/API_JOIN_OPERATIONS.md +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/ARCHITECTURAL_RECOMMENDATION.md +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/ARCHITECTURE_DECISION.md +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/ARCHITECTURE_SYNCED.md +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/BEST_USE_CASES.md +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/BETTER_FILTER_APPROACH.md +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/CHANGES_SUMMARY.md +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/COMPREHENSIVE_DOCUMENTATION.md +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/DEVELOPER_GUIDE.md +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/DUCKDB_COMPARISON.md +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/FINAL_IMPLEMENTATION_RECOMMENDATIONS.md +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/FINAL_RECOMMENDATION.md +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/HOW_PROTOCOL_DETECTION_WORKS.md +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/HOW_TO_ADD_TO_PROJECT.md +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/INSTALLATION.md +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/JSONL_EXECUTOR_ANALYSIS.md +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/JSONL_EXECUTOR_REMOVED.md +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/LIBRARY_DOCUMENTATION.md +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/MIGRATION_GUIDE.md +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/MULTI_SOURCE_EXAMPLE_README.md +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/MYSQL_USAGE.md +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/OPTIMIZATIONS_BREAKDOWN.md +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/OPTIMIZATIONS_NOT_DATABASE_ONLY.md +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/OPTIMIZATIONS_VS_POLARS.md +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/OPTIMIZER_AUTOMATED.md +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/PERFORMANCE.md +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/PERFORMANCE_COMPARISON.md +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/POLARS_AND_MMAP_GUIDE.md +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/PROTOCOL_AND_OPTIMIZATIONS_EXPLAINED.md +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/PROTOCOL_EXPLANATION.md +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/PUBLISHING.md +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/PYPI_CHECKLIST.md +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/QUICK_PUBLISH.md +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/QUICK_START.md +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/SIMPLE_PROTOCOL_GUIDE.md +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/SIMPLE_SOURCE_FUNCTIONS.md +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/SOURCE_COMBINATIONS_GUIDE.md +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/TECHNICAL_DOCUMENTATION.md +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/USER_GUIDE.md +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/WHAT_IS_WHERE_TO_PARAMS.md +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/WHY_API_CONVERTER_NEEDED.md +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/WHY_MANUAL_PARSING.md +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/WHY_PROTOCOL_IN_SOURCES.md +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/evaluator.py +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/mmap_index.py +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/operators_polars.py +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/optimizer.py +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/parser.py +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/polars_expression_translator.py +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/polars_operators.py +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/protocol_helpers.py +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/requirements.txt +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/setup.cfg +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/streaming_sql_engine.egg-info/dependency_links.txt +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/streaming_sql_engine.egg-info/requires.txt +0 -0
- {streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/streaming_sql_engine.egg-info/top_level.txt +0 -0
{streaming_sql_engine-0.1.7/streaming_sql_engine.egg-info → streaming_sql_engine-0.1.9}/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.9
|
|
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,220 @@
|
|
|
1
|
+
# When Polars is Used - Complete Guide
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
Polars is used in **4 main places** in the streaming SQL engine:
|
|
6
|
+
|
|
7
|
+
1. **Mmap Index Building** (for file-based joins)
|
|
8
|
+
2. **Join Operations** (PolarsLookupJoinIterator)
|
|
9
|
+
3. **Filtering** (PolarsBatchFilterIterator)
|
|
10
|
+
4. **Projection** (PolarsBatchProjectIterator)
|
|
11
|
+
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
## 1. Mmap Index Building (`mmap_index.py`)
|
|
15
|
+
|
|
16
|
+
**When:** When building a position-based index for file-based joins
|
|
17
|
+
|
|
18
|
+
**Location:** `MmapPositionIndex.__init__()` → `_build_index_polars()`
|
|
19
|
+
|
|
20
|
+
**What it does:**
|
|
21
|
+
- Scans JSONL file line by line
|
|
22
|
+
- Collects (key_value, file_position) pairs in batches of 500,000
|
|
23
|
+
- Uses Polars `group_by()` to group positions by key_value (SIMD-accelerated)
|
|
24
|
+
- **10-100x faster** than Python dict grouping
|
|
25
|
+
|
|
26
|
+
**When triggered:**
|
|
27
|
+
```python
|
|
28
|
+
# When you register a file source with filename parameter
|
|
29
|
+
register_file_source(engine, "mongo", "data.jsonl", filename="data.jsonl")
|
|
30
|
+
# AND the join uses mmap (not in-memory or Polars join)
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
**Debug message:**
|
|
34
|
+
```
|
|
35
|
+
Building mmap position index with Polars (fast grouping) for data.jsonl...
|
|
36
|
+
Indexed 1,000,000 rows...
|
|
37
|
+
Mmap index built (Polars): 5,000,000 rows, 100,000 unique keys
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
**Code location:** `mmap_index.py:62-134`
|
|
41
|
+
|
|
42
|
+
---
|
|
43
|
+
|
|
44
|
+
## 2. Join Operations (`operators_polars.py`)
|
|
45
|
+
|
|
46
|
+
**When:** For JOIN operations when:
|
|
47
|
+
- `use_polars=True` (default)
|
|
48
|
+
- Polars is installed
|
|
49
|
+
- Mmap join is NOT available (mmap takes priority for memory)
|
|
50
|
+
- Right table has > 10,000 rows (threshold check)
|
|
51
|
+
|
|
52
|
+
**Location:** `executor.py:_build_join_iterator()` → `PolarsLookupJoinIterator`
|
|
53
|
+
|
|
54
|
+
**What it does:**
|
|
55
|
+
- Loads entire right table into Polars DataFrame
|
|
56
|
+
- Builds lookup index using Polars `group_by()` (SIMD-accelerated)
|
|
57
|
+
- Processes left rows in batches
|
|
58
|
+
- Performs vectorized joins using Polars operations
|
|
59
|
+
- **10-50x faster** than Python dict-based joins
|
|
60
|
+
|
|
61
|
+
**When triggered:**
|
|
62
|
+
```python
|
|
63
|
+
# When joining tables and mmap is not available
|
|
64
|
+
SELECT * FROM spryker JOIN mongo ON ...
|
|
65
|
+
# → Uses PolarsLookupJoinIterator if conditions met
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
**Debug message:**
|
|
69
|
+
```
|
|
70
|
+
Using POLARS LOOKUP JOIN (fast, vectorized)...
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
**Code location:** `executor.py:353-377`, `operators_polars.py:14-582`
|
|
74
|
+
|
|
75
|
+
**Priority order:**
|
|
76
|
+
1. **Mmap join** (if filename provided) - Best for memory
|
|
77
|
+
2. **Polars join** (if available and large enough) - Best for speed
|
|
78
|
+
3. **Python join** (fallback) - Works always
|
|
79
|
+
|
|
80
|
+
---
|
|
81
|
+
|
|
82
|
+
## 3. Filtering (`operators_polars.py`)
|
|
83
|
+
|
|
84
|
+
**When:** For WHERE clause filtering when:
|
|
85
|
+
- `use_polars=True` (default)
|
|
86
|
+
- Polars is installed
|
|
87
|
+
- `PolarsBatchFilterIterator` is available
|
|
88
|
+
|
|
89
|
+
**Location:** `executor.py:230-241`
|
|
90
|
+
|
|
91
|
+
**What it does:**
|
|
92
|
+
- Collects rows into batches (10,000 rows)
|
|
93
|
+
- Converts batch to Polars DataFrame
|
|
94
|
+
- Applies WHERE clause using Polars expressions (SIMD-accelerated)
|
|
95
|
+
- **10-200x faster** than Python row-by-row filtering
|
|
96
|
+
|
|
97
|
+
**When triggered:**
|
|
98
|
+
```python
|
|
99
|
+
# When applying WHERE clause after joins
|
|
100
|
+
SELECT * FROM ... WHERE spryker.query = 'reference'
|
|
101
|
+
# → Uses PolarsBatchFilterIterator if available
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
**Debug message:**
|
|
105
|
+
```
|
|
106
|
+
[OPTIMIZATION] Using Polars vectorized filtering (SIMD)
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
**Code location:** `executor.py:230-241`, `operators_polars.py:585-650`
|
|
110
|
+
|
|
111
|
+
**Note:** Currently may fall back to Python if not fully implemented.
|
|
112
|
+
|
|
113
|
+
---
|
|
114
|
+
|
|
115
|
+
## 4. Projection (`operators_polars.py`)
|
|
116
|
+
|
|
117
|
+
**When:** For SELECT column projection when:
|
|
118
|
+
- `use_polars=True` (default)
|
|
119
|
+
- Polars is installed
|
|
120
|
+
- `PolarsBatchProjectIterator` is available
|
|
121
|
+
|
|
122
|
+
**Location:** `executor.py:249-258`
|
|
123
|
+
|
|
124
|
+
**What it does:**
|
|
125
|
+
- Collects rows into batches (10,000 rows)
|
|
126
|
+
- Converts batch to Polars DataFrame
|
|
127
|
+
- Selects only requested columns using Polars (SIMD-accelerated)
|
|
128
|
+
- **10-50x faster** than Python row-by-row projection
|
|
129
|
+
|
|
130
|
+
**When triggered:**
|
|
131
|
+
```python
|
|
132
|
+
# When applying SELECT projection
|
|
133
|
+
SELECT spryker.id, mongo.price FROM ...
|
|
134
|
+
# → Uses PolarsBatchProjectIterator if available
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
**Debug message:**
|
|
138
|
+
```
|
|
139
|
+
[OPTIMIZATION] Using Polars vectorized projection
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
**Code location:** `executor.py:249-258`, `operators_polars.py:653-720`
|
|
143
|
+
|
|
144
|
+
---
|
|
145
|
+
|
|
146
|
+
## Summary Table
|
|
147
|
+
|
|
148
|
+
| Operation | When Polars is Used | Speedup | Location |
|
|
149
|
+
|-----------|---------------------|---------|----------|
|
|
150
|
+
| **Mmap Index Building** | Building file index | 10-100x | `mmap_index.py:62` |
|
|
151
|
+
| **Join Operations** | Mmap not available, table > 10K rows | 10-50x | `operators_polars.py:14` |
|
|
152
|
+
| **Filtering** | WHERE clause after joins | 10-200x | `operators_polars.py:585` |
|
|
153
|
+
| **Projection** | SELECT column selection | 10-50x | `operators_polars.py:653` |
|
|
154
|
+
|
|
155
|
+
---
|
|
156
|
+
|
|
157
|
+
## How to Enable/Disable
|
|
158
|
+
|
|
159
|
+
### Enable (Default)
|
|
160
|
+
```python
|
|
161
|
+
engine = Engine(use_polars=True) # Default
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
### Disable
|
|
165
|
+
```python
|
|
166
|
+
engine = Engine(use_polars=False) # Use pure Python
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
### Check if Polars is Available
|
|
170
|
+
```python
|
|
171
|
+
try:
|
|
172
|
+
import polars as pl
|
|
173
|
+
print("Polars is available!")
|
|
174
|
+
except ImportError:
|
|
175
|
+
print("Polars not installed. Install with: pip install polars")
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
---
|
|
179
|
+
|
|
180
|
+
## Installation
|
|
181
|
+
|
|
182
|
+
Polars is **optional** but recommended:
|
|
183
|
+
|
|
184
|
+
```bash
|
|
185
|
+
# Install with Polars support
|
|
186
|
+
pip install streaming-sql-engine[polars]
|
|
187
|
+
|
|
188
|
+
# Or install Polars separately
|
|
189
|
+
pip install polars>=0.19.0
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
---
|
|
193
|
+
|
|
194
|
+
## Performance Impact
|
|
195
|
+
|
|
196
|
+
**Without Polars:**
|
|
197
|
+
- Pure Python operations
|
|
198
|
+
- Slower but works everywhere
|
|
199
|
+
- Lower memory for small datasets
|
|
200
|
+
|
|
201
|
+
**With Polars:**
|
|
202
|
+
- SIMD-accelerated operations
|
|
203
|
+
- 10-200x faster for large datasets
|
|
204
|
+
- Higher memory usage (loads data into DataFrames)
|
|
205
|
+
|
|
206
|
+
---
|
|
207
|
+
|
|
208
|
+
## Debug Messages
|
|
209
|
+
|
|
210
|
+
When `debug=True`, you'll see:
|
|
211
|
+
|
|
212
|
+
```
|
|
213
|
+
[OPTIMIZATION] Using Polars vectorized filtering (SIMD)
|
|
214
|
+
[OPTIMIZATION] Using Polars vectorized projection
|
|
215
|
+
Using POLARS LOOKUP JOIN (fast, vectorized)...
|
|
216
|
+
Building mmap position index with Polars (fast grouping)...
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
These messages confirm Polars is being used!
|
|
220
|
+
|
|
@@ -20,18 +20,20 @@ class Engine:
|
|
|
20
20
|
print(row)
|
|
21
21
|
"""
|
|
22
22
|
|
|
23
|
-
def __init__(self, debug=False, use_polars=True):
|
|
23
|
+
def __init__(self, debug=False, use_polars=True, first_match_only=False):
|
|
24
24
|
"""
|
|
25
25
|
Initialize a new engine instance.
|
|
26
26
|
|
|
27
27
|
Args:
|
|
28
28
|
debug: If True, enables verbose logging of execution stages
|
|
29
29
|
use_polars: If True, uses Polars for optimizations when available (default: True)
|
|
30
|
+
first_match_only: If True, only returns first match per left key in joins (prevents cartesian products from duplicates)
|
|
30
31
|
"""
|
|
31
32
|
self._sources = {}
|
|
32
33
|
self._source_metadata = {}
|
|
33
34
|
self.debug = debug
|
|
34
35
|
self.use_polars = use_polars
|
|
36
|
+
self.first_match_only = first_match_only
|
|
35
37
|
|
|
36
38
|
def register(
|
|
37
39
|
self,
|
|
@@ -120,4 +122,4 @@ class Engine:
|
|
|
120
122
|
print(f"Building execution pipeline...\n")
|
|
121
123
|
|
|
122
124
|
# Execute plan
|
|
123
|
-
return execute_plan(logical_plan, self._sources, self._source_metadata, debug=self.debug, use_polars=self.use_polars)
|
|
125
|
+
return execute_plan(logical_plan, self._sources, self._source_metadata, debug=self.debug, use_polars=self.use_polars, first_match_only=self.first_match_only)
|
|
@@ -39,7 +39,8 @@ def execute_plan(
|
|
|
39
39
|
sources,
|
|
40
40
|
source_metadata,
|
|
41
41
|
debug=False,
|
|
42
|
-
use_polars=True
|
|
42
|
+
use_polars=True,
|
|
43
|
+
first_match_only=False
|
|
43
44
|
):
|
|
44
45
|
"""
|
|
45
46
|
Execute a logical plan and return a generator of result row dictionaries.
|
|
@@ -215,7 +216,8 @@ def execute_plan(
|
|
|
215
216
|
plan.required_columns.get(join_info.table), # Pass required columns
|
|
216
217
|
optimized_right_source_fn=optimized_right_source_fn, # Pass optimized source
|
|
217
218
|
debug=debug,
|
|
218
|
-
use_polars=use_polars # Pass Polars flag
|
|
219
|
+
use_polars=use_polars, # Pass Polars flag
|
|
220
|
+
first_match_only=first_match_only # Pass first-match-only flag
|
|
219
221
|
)
|
|
220
222
|
|
|
221
223
|
# Update plan.where_expr to only include clauses not pushed to sources
|
|
@@ -270,7 +272,8 @@ def _build_join_iterator(
|
|
|
270
272
|
required_columns=None,
|
|
271
273
|
optimized_right_source_fn=None,
|
|
272
274
|
debug=False,
|
|
273
|
-
use_polars=True
|
|
275
|
+
use_polars=True,
|
|
276
|
+
first_match_only=False
|
|
274
277
|
):
|
|
275
278
|
"""
|
|
276
279
|
Build appropriate join iterator based on source capabilities.
|
|
@@ -392,7 +395,8 @@ def _build_join_iterator(
|
|
|
392
395
|
join_info.table,
|
|
393
396
|
join_info.alias,
|
|
394
397
|
required_columns=required_columns,
|
|
395
|
-
debug=debug
|
|
398
|
+
debug=debug,
|
|
399
|
+
first_match_only=first_match_only
|
|
396
400
|
)
|
|
397
401
|
|
|
398
402
|
|
|
@@ -159,7 +159,8 @@ class LookupJoinIterator:
|
|
|
159
159
|
right_table,
|
|
160
160
|
right_alias,
|
|
161
161
|
required_columns=None,
|
|
162
|
-
debug=False
|
|
162
|
+
debug=False,
|
|
163
|
+
first_match_only=False
|
|
163
164
|
):
|
|
164
165
|
self.left_source = left_source
|
|
165
166
|
self.right_source_fn = right_source_fn
|
|
@@ -170,6 +171,7 @@ class LookupJoinIterator:
|
|
|
170
171
|
self.right_alias = right_alias or right_table
|
|
171
172
|
self.required_columns = required_columns # Set of column names (for column pruning)
|
|
172
173
|
self.debug = debug
|
|
174
|
+
self.first_match_only = first_match_only # If True, only return first match per left key
|
|
173
175
|
|
|
174
176
|
# Build lookup index
|
|
175
177
|
self.lookup_index = {}
|
|
@@ -178,17 +180,24 @@ class LookupJoinIterator:
|
|
|
178
180
|
print(f" Building lookup index for {right_table} (columns: {len(required_columns)})...")
|
|
179
181
|
else:
|
|
180
182
|
print(f" Building lookup index for {right_table}...")
|
|
181
|
-
|
|
183
|
+
if first_match_only:
|
|
184
|
+
print(f" ⚡ First-match-only mode: Will deduplicate right side and return only first match per left key")
|
|
185
|
+
self._build_index(deduplicate=first_match_only)
|
|
182
186
|
|
|
183
187
|
self._left_row = None
|
|
184
188
|
self._right_matches = []
|
|
185
189
|
self._match_index = 0
|
|
186
190
|
self._join_count = 0
|
|
187
191
|
|
|
188
|
-
def _build_index(self):
|
|
189
|
-
"""Build lookup index from right side table.
|
|
192
|
+
def _build_index(self, deduplicate=False):
|
|
193
|
+
"""Build lookup index from right side table.
|
|
194
|
+
|
|
195
|
+
Args:
|
|
196
|
+
deduplicate: If True, only keep first match per key (prevents cartesian products)
|
|
197
|
+
"""
|
|
190
198
|
right_table_col = _extract_column_from_key(self.right_key)
|
|
191
199
|
index_size = 0
|
|
200
|
+
duplicate_count = 0
|
|
192
201
|
|
|
193
202
|
for row in self.right_source_fn():
|
|
194
203
|
# Skip None or invalid rows
|
|
@@ -207,13 +216,25 @@ class LookupJoinIterator:
|
|
|
207
216
|
# Skip rows with None join keys (can't join on None)
|
|
208
217
|
if key_value is None:
|
|
209
218
|
continue
|
|
219
|
+
|
|
210
220
|
if key_value not in self.lookup_index:
|
|
211
221
|
self.lookup_index[key_value] = []
|
|
212
|
-
|
|
213
|
-
|
|
222
|
+
self.lookup_index[key_value].append(prefixed_row)
|
|
223
|
+
index_size += 1
|
|
224
|
+
else:
|
|
225
|
+
# Key already exists
|
|
226
|
+
if deduplicate:
|
|
227
|
+
# Skip duplicates - only keep first match
|
|
228
|
+
duplicate_count += 1
|
|
229
|
+
else:
|
|
230
|
+
# Keep all matches (normal SQL behavior)
|
|
231
|
+
self.lookup_index[key_value].append(prefixed_row)
|
|
232
|
+
index_size += 1
|
|
214
233
|
|
|
215
234
|
if self.debug:
|
|
216
235
|
print(f" Index built: {index_size:,} rows, {len(self.lookup_index):,} unique keys")
|
|
236
|
+
if deduplicate and duplicate_count > 0:
|
|
237
|
+
print(f" ⚠️ Deduplicated: {duplicate_count:,} duplicate keys removed (kept first match only)")
|
|
217
238
|
|
|
218
239
|
def __iter__(self):
|
|
219
240
|
return self
|
|
@@ -253,6 +274,10 @@ class LookupJoinIterator:
|
|
|
253
274
|
matches = self.lookup_index.get(left_key_value, [])
|
|
254
275
|
# Filter out None or invalid rows
|
|
255
276
|
self._right_matches = [m for m in matches if m is not None and isinstance(m, dict)]
|
|
277
|
+
|
|
278
|
+
# If first_match_only mode, only take first match
|
|
279
|
+
if self.first_match_only and len(self._right_matches) > 1:
|
|
280
|
+
self._right_matches = self._right_matches[:1]
|
|
256
281
|
|
|
257
282
|
# Handle INNER JOIN
|
|
258
283
|
if self.join_type == "INNER":
|
|
@@ -223,7 +223,7 @@ class MmapLookupJoinIterator:
|
|
|
223
223
|
self._match_index = 0
|
|
224
224
|
|
|
225
225
|
self._join_count += 1
|
|
226
|
-
if self.debug and self._join_count %
|
|
226
|
+
if self.debug and self._join_count % 100000 == 0:
|
|
227
227
|
print(f" Mmap join: {self._join_count:,} rows matched")
|
|
228
228
|
return {**left_row_copy, **right_row}
|
|
229
229
|
|
|
@@ -233,7 +233,7 @@ class MmapLookupJoinIterator:
|
|
|
233
233
|
result = self._left_row.copy()
|
|
234
234
|
self._left_row = None
|
|
235
235
|
self._join_count += 1
|
|
236
|
-
if self.debug and self._join_count %
|
|
236
|
+
if self.debug and self._join_count % 100000 == 0:
|
|
237
237
|
print(f" Mmap join {self._join_count:,} rows (LEFT JOIN with NULLs)")
|
|
238
238
|
return result
|
|
239
239
|
|
|
@@ -257,7 +257,7 @@ class MmapLookupJoinIterator:
|
|
|
257
257
|
self._match_index = 0
|
|
258
258
|
|
|
259
259
|
self._join_count += 1
|
|
260
|
-
if self.debug and self._join_count %
|
|
260
|
+
if self.debug and self._join_count % 100000 == 0:
|
|
261
261
|
print(f" Mmap join: {self._join_count:,} rows matched")
|
|
262
262
|
return {**left_row_copy, **right_row}
|
|
263
263
|
|
|
@@ -15,6 +15,7 @@ class JoinInfo:
|
|
|
15
15
|
join_type: str # "INNER" or "LEFT"
|
|
16
16
|
left_key: str # e.g., "sp.fk_abs"
|
|
17
17
|
right_key: str # e.g., "spa.id"
|
|
18
|
+
additional_where: object = None # Additional WHERE conditions from ON clause
|
|
18
19
|
|
|
19
20
|
|
|
20
21
|
@dataclass
|
|
@@ -56,9 +57,13 @@ def build_logical_plan(ast, registered_tables):
|
|
|
56
57
|
|
|
57
58
|
# Extract JOINs
|
|
58
59
|
joins = []
|
|
60
|
+
additional_where_conditions = []
|
|
59
61
|
for join_expr in ast.args.get("joins", []):
|
|
60
62
|
join_info = _extract_join(join_expr, registered_tables)
|
|
61
63
|
joins.append(join_info)
|
|
64
|
+
# Collect additional WHERE conditions from JOIN ON clauses
|
|
65
|
+
if join_info.additional_where is not None:
|
|
66
|
+
additional_where_conditions.append(join_info.additional_where)
|
|
62
67
|
|
|
63
68
|
# Extract WHERE clause
|
|
64
69
|
where_expr = ast.args.get("where")
|
|
@@ -68,6 +73,21 @@ def build_logical_plan(ast, registered_tables):
|
|
|
68
73
|
where_expr = where_expr.this
|
|
69
74
|
# where_expr is now the actual expression (And, Or, EQ, etc.)
|
|
70
75
|
|
|
76
|
+
# Merge additional WHERE conditions from JOIN ON clauses into main WHERE clause
|
|
77
|
+
if additional_where_conditions:
|
|
78
|
+
if where_expr is None:
|
|
79
|
+
# No existing WHERE clause, use the first additional condition
|
|
80
|
+
where_expr = additional_where_conditions[0]
|
|
81
|
+
# Combine remaining conditions with AND
|
|
82
|
+
for cond in additional_where_conditions[1:]:
|
|
83
|
+
where_expr = exp.And(this=where_expr, expression=cond)
|
|
84
|
+
else:
|
|
85
|
+
# Combine existing WHERE with additional conditions using AND
|
|
86
|
+
combined = where_expr
|
|
87
|
+
for cond in additional_where_conditions:
|
|
88
|
+
combined = exp.And(this=combined, expression=cond)
|
|
89
|
+
where_expr = combined
|
|
90
|
+
|
|
71
91
|
# Extract SELECT projections
|
|
72
92
|
projections = list(ast.expressions)
|
|
73
93
|
|
|
@@ -130,32 +150,92 @@ def _extract_join(join_expr, registered_tables):
|
|
|
130
150
|
if not on_expr:
|
|
131
151
|
raise ParseError("JOIN must have an ON condition")
|
|
132
152
|
|
|
133
|
-
|
|
153
|
+
result = _extract_join_keys(on_expr)
|
|
154
|
+
if len(result) == 3:
|
|
155
|
+
left_key, right_key, additional_where = result
|
|
156
|
+
else:
|
|
157
|
+
# Backward compatibility: if function returns 2 values
|
|
158
|
+
left_key, right_key = result
|
|
159
|
+
additional_where = None
|
|
134
160
|
|
|
135
|
-
|
|
161
|
+
join_info = JoinInfo(
|
|
136
162
|
table=table,
|
|
137
163
|
alias=alias,
|
|
138
164
|
join_type=join_type,
|
|
139
165
|
left_key=left_key,
|
|
140
166
|
right_key=right_key
|
|
141
167
|
)
|
|
168
|
+
|
|
169
|
+
# Store additional WHERE conditions to be added later
|
|
170
|
+
if additional_where is not None:
|
|
171
|
+
join_info.additional_where = additional_where
|
|
172
|
+
else:
|
|
173
|
+
join_info.additional_where = None
|
|
174
|
+
|
|
175
|
+
return join_info
|
|
142
176
|
|
|
143
177
|
|
|
144
178
|
def _extract_join_keys(on_expr):
|
|
145
179
|
"""
|
|
146
180
|
Extract join keys from ON condition.
|
|
147
|
-
|
|
181
|
+
Supports single equality joins: alias1.col1 = alias2.col2
|
|
182
|
+
For multiple conditions with AND, uses the first equality condition as the join key
|
|
183
|
+
and returns additional conditions to be moved to WHERE clause.
|
|
148
184
|
"""
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
185
|
+
# Handle multiple conditions with AND
|
|
186
|
+
if isinstance(on_expr, exp.And):
|
|
187
|
+
# Extract all equality conditions
|
|
188
|
+
equality_conditions = []
|
|
189
|
+
other_conditions = []
|
|
190
|
+
|
|
191
|
+
def _extract_conditions(expr):
|
|
192
|
+
if isinstance(expr, exp.And):
|
|
193
|
+
_extract_conditions(expr.this)
|
|
194
|
+
_extract_conditions(expr.expression)
|
|
195
|
+
elif isinstance(expr, exp.EQ):
|
|
196
|
+
equality_conditions.append(expr)
|
|
197
|
+
else:
|
|
198
|
+
other_conditions.append(expr)
|
|
199
|
+
|
|
200
|
+
_extract_conditions(on_expr)
|
|
201
|
+
|
|
202
|
+
if not equality_conditions:
|
|
203
|
+
raise ParseError("ON clause must contain at least one equality condition")
|
|
204
|
+
|
|
205
|
+
# Use the first equality condition as the join key
|
|
206
|
+
first_eq = equality_conditions[0]
|
|
207
|
+
left = first_eq.this
|
|
208
|
+
right = first_eq.expression
|
|
209
|
+
|
|
210
|
+
left_key = _column_to_string(left)
|
|
211
|
+
right_key = _column_to_string(right)
|
|
212
|
+
|
|
213
|
+
# Combine remaining conditions to be added to WHERE clause
|
|
214
|
+
remaining_conditions = equality_conditions[1:] + other_conditions
|
|
215
|
+
additional_where = None
|
|
216
|
+
if remaining_conditions:
|
|
217
|
+
if len(remaining_conditions) == 1:
|
|
218
|
+
additional_where = remaining_conditions[0]
|
|
219
|
+
else:
|
|
220
|
+
# Combine multiple conditions with AND
|
|
221
|
+
additional_where = remaining_conditions[0]
|
|
222
|
+
for cond in remaining_conditions[1:]:
|
|
223
|
+
additional_where = exp.And(this=additional_where, expression=cond)
|
|
224
|
+
|
|
225
|
+
return left_key, right_key, additional_where
|
|
154
226
|
|
|
155
|
-
|
|
156
|
-
|
|
227
|
+
# Handle single equality condition
|
|
228
|
+
elif isinstance(on_expr, exp.EQ):
|
|
229
|
+
left = on_expr.this
|
|
230
|
+
right = on_expr.expression
|
|
231
|
+
|
|
232
|
+
left_key = _column_to_string(left)
|
|
233
|
+
right_key = _column_to_string(right)
|
|
234
|
+
|
|
235
|
+
return left_key, right_key, None
|
|
157
236
|
|
|
158
|
-
|
|
237
|
+
else:
|
|
238
|
+
raise ParseError("Only equality joins are supported in ON clause")
|
|
159
239
|
|
|
160
240
|
|
|
161
241
|
def _column_to_string(expr):
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "streaming-sql-engine"
|
|
7
|
-
version = "0.1.
|
|
7
|
+
version = "0.1.9"
|
|
8
8
|
description = "A lightweight SQL execution engine for streaming row-by-row queries with joins across multiple data sources"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
requires-python = ">=3.8"
|
|
@@ -12,7 +12,7 @@ with open("requirements.txt", "r", encoding="utf-8") as fh:
|
|
|
12
12
|
|
|
13
13
|
setup(
|
|
14
14
|
name="streaming-sql-engine",
|
|
15
|
-
version="0.1.
|
|
15
|
+
version="0.1.9",
|
|
16
16
|
author="Theodore Pantazopoulo",
|
|
17
17
|
author_email="your.email@example.com",
|
|
18
18
|
description="A lightweight SQL execution engine for streaming row-by-row queries with joins across multiple data sources",
|
{streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9/streaming_sql_engine.egg-info}/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.9
|
|
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
|
{streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/streaming_sql_engine.egg-info/SOURCES.txt
RENAMED
|
@@ -81,6 +81,7 @@ documentation/SOURCE_COMBINATIONS_GUIDE.md
|
|
|
81
81
|
documentation/TECHNICAL_DOCUMENTATION.md
|
|
82
82
|
documentation/USER_GUIDE.md
|
|
83
83
|
documentation/WHAT_IS_WHERE_TO_PARAMS.md
|
|
84
|
+
documentation/WHEN_POLARS_IS_USED.md
|
|
84
85
|
documentation/WHY_API_CONVERTER_NEEDED.md
|
|
85
86
|
documentation/WHY_MANUAL_PARSING.md
|
|
86
87
|
documentation/WHY_PROTOCOL_IN_SOURCES.md
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/APACHE_DRILL_COMPARISON.md
RENAMED
|
File without changes
|
{streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/API_JOIN_OPERATIONS.md
RENAMED
|
File without changes
|
|
File without changes
|
{streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/ARCHITECTURE_DECISION.md
RENAMED
|
File without changes
|
{streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/ARCHITECTURE_SYNCED.md
RENAMED
|
File without changes
|
|
File without changes
|
{streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/BETTER_FILTER_APPROACH.md
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/DUCKDB_COMPARISON.md
RENAMED
|
File without changes
|
|
File without changes
|
{streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/FINAL_RECOMMENDATION.md
RENAMED
|
File without changes
|
|
File without changes
|
{streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/HOW_TO_ADD_TO_PROJECT.md
RENAMED
|
File without changes
|
|
File without changes
|
{streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/JSONL_EXECUTOR_ANALYSIS.md
RENAMED
|
File without changes
|
{streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/JSONL_EXECUTOR_REMOVED.md
RENAMED
|
File without changes
|
{streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/LIBRARY_DOCUMENTATION.md
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/OPTIMIZATIONS_BREAKDOWN.md
RENAMED
|
File without changes
|
|
File without changes
|
{streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/OPTIMIZATIONS_VS_POLARS.md
RENAMED
|
File without changes
|
{streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/OPTIMIZER_AUTOMATED.md
RENAMED
|
File without changes
|
|
File without changes
|
{streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/PERFORMANCE_COMPARISON.md
RENAMED
|
File without changes
|
{streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/POLARS_AND_MMAP_GUIDE.md
RENAMED
|
File without changes
|
|
File without changes
|
{streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/PROTOCOL_EXPLANATION.md
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/SIMPLE_PROTOCOL_GUIDE.md
RENAMED
|
File without changes
|
{streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/SIMPLE_SOURCE_FUNCTIONS.md
RENAMED
|
File without changes
|
{streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/SOURCE_COMBINATIONS_GUIDE.md
RENAMED
|
File without changes
|
{streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/TECHNICAL_DOCUMENTATION.md
RENAMED
|
File without changes
|
|
File without changes
|
{streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/WHAT_IS_WHERE_TO_PARAMS.md
RENAMED
|
File without changes
|
{streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/WHY_API_CONVERTER_NEEDED.md
RENAMED
|
File without changes
|
{streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/WHY_MANUAL_PARSING.md
RENAMED
|
File without changes
|
{streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/documentation/WHY_PROTOCOL_IN_SOURCES.md
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{streaming_sql_engine-0.1.7 → streaming_sql_engine-0.1.9}/streaming_sql_engine.egg-info/requires.txt
RENAMED
|
File without changes
|
|
File without changes
|