streaming-sql-engine 0.1.8__tar.gz → 0.1.11__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.
Files changed (77) hide show
  1. {streaming_sql_engine-0.1.8/streaming_sql_engine.egg-info → streaming_sql_engine-0.1.11}/PKG-INFO +1 -1
  2. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/__init__.py +1 -1
  3. streaming_sql_engine-0.1.11/documentation/FIRST_MATCH_ONLY_WARNINGS.md +206 -0
  4. streaming_sql_engine-0.1.11/documentation/WHEN_POLARS_IS_USED.md +220 -0
  5. streaming_sql_engine-0.1.11/documentation/WHY_ORIGINAL_IS_FASTER.md +263 -0
  6. streaming_sql_engine-0.1.11/documentation/XML_MONGO_USE_CASE.md +224 -0
  7. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/engine.py +4 -2
  8. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/evaluator.py +57 -0
  9. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/executor.py +8 -4
  10. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/operators.py +31 -6
  11. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/operators_mmap.py +3 -3
  12. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/pyproject.toml +1 -1
  13. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/setup.py +1 -1
  14. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11/streaming_sql_engine.egg-info}/PKG-INFO +1 -1
  15. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/streaming_sql_engine.egg-info/SOURCES.txt +4 -0
  16. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/DEPLOYMENT.md +0 -0
  17. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/MANIFEST.in +0 -0
  18. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/QUICK_DEPLOY.md +0 -0
  19. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/QUICK_FIX.md +0 -0
  20. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/README.md +0 -0
  21. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/documentation/APACHE_DRILL_COMPARISON.md +0 -0
  22. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/documentation/API_JOIN_OPERATIONS.md +0 -0
  23. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/documentation/ARCHITECTURAL_RECOMMENDATION.md +0 -0
  24. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/documentation/ARCHITECTURE_DECISION.md +0 -0
  25. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/documentation/ARCHITECTURE_SYNCED.md +0 -0
  26. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/documentation/BEST_USE_CASES.md +0 -0
  27. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/documentation/BETTER_FILTER_APPROACH.md +0 -0
  28. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/documentation/CHANGES_SUMMARY.md +0 -0
  29. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/documentation/COMPREHENSIVE_DOCUMENTATION.md +0 -0
  30. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/documentation/DEVELOPER_GUIDE.md +0 -0
  31. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/documentation/DUCKDB_COMPARISON.md +0 -0
  32. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/documentation/FINAL_IMPLEMENTATION_RECOMMENDATIONS.md +0 -0
  33. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/documentation/FINAL_RECOMMENDATION.md +0 -0
  34. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/documentation/HOW_PROTOCOL_DETECTION_WORKS.md +0 -0
  35. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/documentation/HOW_TO_ADD_TO_PROJECT.md +0 -0
  36. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/documentation/INSTALLATION.md +0 -0
  37. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/documentation/JSONL_EXECUTOR_ANALYSIS.md +0 -0
  38. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/documentation/JSONL_EXECUTOR_REMOVED.md +0 -0
  39. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/documentation/LIBRARY_DOCUMENTATION.md +0 -0
  40. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/documentation/MIGRATION_GUIDE.md +0 -0
  41. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/documentation/MULTI_SOURCE_EXAMPLE_README.md +0 -0
  42. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/documentation/MYSQL_USAGE.md +0 -0
  43. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/documentation/OPTIMIZATIONS_BREAKDOWN.md +0 -0
  44. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/documentation/OPTIMIZATIONS_NOT_DATABASE_ONLY.md +0 -0
  45. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/documentation/OPTIMIZATIONS_VS_POLARS.md +0 -0
  46. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/documentation/OPTIMIZER_AUTOMATED.md +0 -0
  47. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/documentation/PERFORMANCE.md +0 -0
  48. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/documentation/PERFORMANCE_COMPARISON.md +0 -0
  49. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/documentation/POLARS_AND_MMAP_GUIDE.md +0 -0
  50. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/documentation/PROTOCOL_AND_OPTIMIZATIONS_EXPLAINED.md +0 -0
  51. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/documentation/PROTOCOL_EXPLANATION.md +0 -0
  52. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/documentation/PUBLISHING.md +0 -0
  53. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/documentation/PYPI_CHECKLIST.md +0 -0
  54. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/documentation/QUICK_PUBLISH.md +0 -0
  55. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/documentation/QUICK_START.md +0 -0
  56. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/documentation/SIMPLE_PROTOCOL_GUIDE.md +0 -0
  57. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/documentation/SIMPLE_SOURCE_FUNCTIONS.md +0 -0
  58. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/documentation/SOURCE_COMBINATIONS_GUIDE.md +0 -0
  59. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/documentation/TECHNICAL_DOCUMENTATION.md +0 -0
  60. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/documentation/USER_GUIDE.md +0 -0
  61. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/documentation/WHAT_IS_WHERE_TO_PARAMS.md +0 -0
  62. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/documentation/WHY_API_CONVERTER_NEEDED.md +0 -0
  63. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/documentation/WHY_MANUAL_PARSING.md +0 -0
  64. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/documentation/WHY_PROTOCOL_IN_SOURCES.md +0 -0
  65. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/mmap_index.py +0 -0
  66. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/operators_polars.py +0 -0
  67. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/optimizer.py +0 -0
  68. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/parser.py +0 -0
  69. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/planner.py +0 -0
  70. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/polars_expression_translator.py +0 -0
  71. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/polars_operators.py +0 -0
  72. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/protocol_helpers.py +0 -0
  73. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/requirements.txt +0 -0
  74. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/setup.cfg +0 -0
  75. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/streaming_sql_engine.egg-info/dependency_links.txt +0 -0
  76. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/streaming_sql_engine.egg-info/requires.txt +0 -0
  77. {streaming_sql_engine-0.1.8 → streaming_sql_engine-0.1.11}/streaming_sql_engine.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: streaming-sql-engine
3
- Version: 0.1.8
3
+ Version: 0.1.11
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
@@ -27,5 +27,5 @@ try:
27
27
  except ImportError:
28
28
  __all__ = ["Engine"]
29
29
 
30
- __version__ = "0.1.8"
30
+ __version__ = "0.1.11"
31
31
 
@@ -0,0 +1,206 @@
1
+ # `first_match_only=True` - Potential Issues and When to Use
2
+
3
+ ## What It Does
4
+
5
+ When `first_match_only=True`, the join operator:
6
+
7
+ 1. **Deduplicates the right side** during index building (keeps only first occurrence per key)
8
+ 2. **Returns only first match** per left key (even if multiple matches exist)
9
+
10
+ This prevents cartesian products from duplicate keys, dramatically reducing output rows.
11
+
12
+ ## Potential Problems
13
+
14
+ ### 1. **Data Loss - Missing Valid Matches**
15
+
16
+ **Problem:** If you need ALL matches (one-to-many relationships), you'll only get one.
17
+
18
+ **Example:**
19
+
20
+ ```python
21
+ # Spryker has 1 row with product_offer_reference='ABC'
22
+ # Mongo has 3 rows with product_offer_reference='ABC' (legitimate duplicates)
23
+
24
+ # With first_match_only=True:
25
+ # → Returns 1 row (only first mongo match)
26
+
27
+ # With first_match_only=False (standard SQL):
28
+ # → Returns 3 rows (all combinations)
29
+ ```
30
+
31
+ **Impact:** You might miss important reconciliation issues if duplicates are legitimate.
32
+
33
+ ### 2. **Non-Deterministic Results**
34
+
35
+ **Problem:** Which "first" match you get depends on file order, which may be inconsistent.
36
+
37
+ **Example:**
38
+
39
+ ```python
40
+ # File order changes between runs:
41
+ # Run 1: mongo row A, then mongo row B (both have ref='ABC')
42
+ # → Returns row A
43
+
44
+ # Run 2: mongo row B, then mongo row A (same data, different order)
45
+ # → Returns row B
46
+ ```
47
+
48
+ **Impact:** Results may differ between runs, making debugging difficult.
49
+
50
+ ### 3. **Different from Standard SQL Behavior**
51
+
52
+ **Problem:** Standard SQL JOINs return ALL combinations. This breaks that expectation.
53
+
54
+ **Example:**
55
+
56
+ ```sql
57
+ -- Standard SQL behavior:
58
+ -- If spryker has 1 row and mongo has 3 matching rows
59
+ -- → Returns 3 rows (all combinations)
60
+
61
+ -- With first_match_only=True:
62
+ -- → Returns 1 row (only first match)
63
+ ```
64
+
65
+ **Impact:** Results don't match standard SQL expectations, which could confuse users.
66
+
67
+ ### 4. **Business Logic Assumptions**
68
+
69
+ **Problem:** If your business logic expects to see all matches but only gets one, it might miss issues.
70
+
71
+ **Example:**
72
+
73
+ ```python
74
+ # Business logic: "If mongo has multiple prices for same ref, flag as error"
75
+ # With first_match_only=True: Only sees first price, misses the error!
76
+ ```
77
+
78
+ **Impact:** May hide data quality issues.
79
+
80
+ ## When It's SAFE to Use
81
+
82
+ ### ✅ **Your Reconciliation Use Case**
83
+
84
+ **Why it's safe:**
85
+
86
+ - Your original code uses dictionaries, which also deduplicate (keeps last value)
87
+ - Reconciliation logic expects one match per key
88
+ - You're matching records, not analyzing all combinations
89
+ - Performance is critical (millions of rows)
90
+
91
+ **Original code behavior:**
92
+
93
+ ```python
94
+ def build_reference_dict(self, data):
95
+ ref_dict = {}
96
+ for line in data:
97
+ ref_dict[line["product_offer_reference"]] = line # Overwrites duplicates
98
+ return ref_dict
99
+ ```
100
+
101
+ **Note:** Original keeps LAST match, `first_match_only=True` keeps FIRST match. This is a minor difference, but both deduplicate.
102
+
103
+ ### ✅ **When You Have One-to-One Relationships**
104
+
105
+ If join keys are unique (no duplicates), `first_match_only=True` has no effect.
106
+
107
+ ### ✅ **When Performance is Critical**
108
+
109
+ If you have millions of rows and duplicates cause cartesian products, the speedup is worth it.
110
+
111
+ ## When It's NOT SAFE to Use
112
+
113
+ ### ❌ **When You Need All Matches**
114
+
115
+ If your business logic requires seeing ALL matches:
116
+
117
+ ```python
118
+ # Don't use first_match_only=True if you need:
119
+ # - All price variations for same product
120
+ # - All status changes over time
121
+ # - Complete audit trail
122
+ ```
123
+
124
+ ### ❌ **When Results Must Be Deterministic**
125
+
126
+ If you need consistent results across runs:
127
+
128
+ ```python
129
+ # Use first_match_only=False and ensure data is sorted
130
+ # Or use a deterministic deduplication strategy
131
+ ```
132
+
133
+ ### ❌ **When Doing Analytics**
134
+
135
+ If you're analyzing relationships and need all combinations:
136
+
137
+ ```python
138
+ # Standard SQL behavior needed for:
139
+ # - Finding all possible combinations
140
+ # - Statistical analysis
141
+ # - Data exploration
142
+ ```
143
+
144
+ ## Recommendations for Your Reconciliation Script
145
+
146
+ ### Current Setup (SAFE):
147
+
148
+ ```python
149
+ self.engine = Engine(debug=False, use_polars=True, first_match_only=True)
150
+ ```
151
+
152
+ **Why it's safe:**
153
+
154
+ 1. ✅ Original code also deduplicates (dictionaries)
155
+ 2. ✅ Reconciliation expects one match per key
156
+ 3. ✅ Performance critical (millions of rows)
157
+ 4. ✅ You're matching records, not analyzing combinations
158
+
159
+ ### If You Need More Safety:
160
+
161
+ **Option 1: Add Warning for Duplicates**
162
+
163
+ ```python
164
+ # In your processing, detect and warn about duplicates
165
+ if len(matches) > 1:
166
+ print(f"WARNING: Found {len(matches)} matches for key {key}, using first only")
167
+ ```
168
+
169
+ **Option 2: Make It Configurable**
170
+
171
+ ```python
172
+ class PostProcessor:
173
+ def __init__(self, first_match_only=True):
174
+ self.engine = Engine(
175
+ debug=False,
176
+ use_polars=True,
177
+ first_match_only=first_match_only
178
+ )
179
+ ```
180
+
181
+ **Option 3: Use Deterministic Deduplication**
182
+
183
+ ```python
184
+ # Sort data before joining to ensure consistent "first" match
185
+ # Or use a specific field to determine which match to keep
186
+ ```
187
+
188
+ ## Summary
189
+
190
+ | Scenario | Use `first_match_only=True`? | Reason |
191
+ | -------------------------------- | ---------------------------- | ----------------------------------------------- |
192
+ | **Reconciliation (your case)** | ✅ **YES** | Matches original behavior, performance critical |
193
+ | **One-to-one relationships** | ✅ **YES** | No effect, safe |
194
+ | **Need all matches** | ❌ **NO** | Will lose data |
195
+ | **Analytics/exploration** | ❌ **NO** | Need all combinations |
196
+ | **Deterministic results needed** | ⚠️ **MAYBE** | Depends on data ordering |
197
+
198
+ ## Bottom Line
199
+
200
+ For your reconciliation script, `first_match_only=True` is **SAFE and RECOMMENDED** because:
201
+
202
+ 1. It matches your original code's deduplication behavior
203
+ 2. It dramatically improves performance (prevents cartesian products)
204
+ 3. Your business logic expects one match per key
205
+
206
+ The only difference is that original code keeps LAST match, while `first_match_only=True` keeps FIRST match. If this matters, you can sort your data first to ensure consistent results.
@@ -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
+
@@ -0,0 +1,263 @@
1
+ # Why the Original Script is Faster Than SQL Engine
2
+
3
+ ## TL;DR
4
+
5
+ **The original script is faster because it uses simple Python dictionaries with O(1) lookups, while the SQL engine adds significant overhead from parsing, planning, and iterator chains.**
6
+
7
+ ## Performance Comparison
8
+
9
+ | Operation | Original Script | SQL Engine | Why Original is Faster |
10
+ |-----------|----------------|------------|------------------------|
11
+ | **Data Loading** | Load once into memory | Stream row-by-row | No streaming overhead |
12
+ | **Lookups** | Direct dict access `dict[key]` | Iterator chain + index lookup | No iterator overhead |
13
+ | **SQL Parsing** | None | sqlglot parsing | ~10-50ms overhead |
14
+ | **Planning** | None | Logical plan building | ~5-20ms overhead |
15
+ | **Column Access** | Direct `row['field']` | Prefixed `row['table.field']` | String operations overhead |
16
+ | **Expression Eval** | Direct Python code | AST traversal | Function call overhead |
17
+
18
+ ## Detailed Analysis
19
+
20
+ ### 1. **Data Loading Strategy**
21
+
22
+ **Original Script:**
23
+ ```python
24
+ # Loads ALL data into memory once
25
+ mongo_data = self.load_data(mongo_filename) # List of dicts
26
+ spryker_data = self.load_data(spryker_filename) # List of dicts
27
+
28
+ # Builds dictionaries once
29
+ mongo_ref_dict = self.build_reference_dict(mongo_data) # O(n) - done once
30
+ ```
31
+
32
+ **SQL Engine:**
33
+ ```python
34
+ # Streams data row-by-row through iterator chain
35
+ for row in self.engine.query(query): # Each row goes through:
36
+ # 1. ScanIterator (reads from file)
37
+ # 2. FilterIterator (applies WHERE)
38
+ # 3. LookupJoinIterator (builds index + lookup)
39
+ # 4. ProjectIterator (selects columns)
40
+ ```
41
+
42
+ **Overhead:** Iterator chain adds ~0.1-1ms per row in Python overhead.
43
+
44
+ ### 2. **Lookup Performance**
45
+
46
+ **Original Script:**
47
+ ```python
48
+ # Simple dictionary lookup - O(1), ~10-50 nanoseconds
49
+ if ref in mongo_ref_dict:
50
+ mongo_line = mongo_ref_dict[ref] # Direct hash table access
51
+ ```
52
+
53
+ **SQL Engine:**
54
+ ```python
55
+ # Iterator chain lookup - O(1) but with overhead:
56
+ # 1. Get left row from iterator
57
+ # 2. Extract join key value
58
+ # 3. Lookup in index (dict lookup)
59
+ # 4. Merge rows
60
+ # 5. Apply projection
61
+ # 6. Yield result
62
+ ```
63
+
64
+ **Overhead:** Each row goes through 4-6 iterator steps, each with function call overhead.
65
+
66
+ ### 3. **SQL Parsing & Planning Overhead**
67
+
68
+ **Original Script:**
69
+ ```python
70
+ # No parsing - direct Python code execution
71
+ if query_type == "reference":
72
+ ref = spryker_line.get("product_offer_reference")
73
+ if ref and ref in mongo_ref_dict:
74
+ mongo_line = mongo_ref_dict[ref]
75
+ ```
76
+
77
+ **SQL Engine:**
78
+ ```python
79
+ # Every query requires:
80
+ # 1. SQL parsing (sqlglot) - ~10-50ms
81
+ # 2. AST building - ~5-10ms
82
+ # 3. Logical plan building - ~5-20ms
83
+ # 4. Execution plan building - ~5-10ms
84
+ # Total: ~25-90ms per query
85
+ ```
86
+
87
+ **Overhead:** ~25-90ms per query, even before processing any rows.
88
+
89
+ ### 4. **Column Access Overhead**
90
+
91
+ **Original Script:**
92
+ ```python
93
+ # Direct field access - no string operations
94
+ mongo_price = mongo_line.get("price")
95
+ spryker_price = spryker_line.get("gross_price")
96
+ ```
97
+
98
+ **SQL Engine:**
99
+ ```python
100
+ # Prefixed column access - string operations
101
+ mongo_price = row.get("mongo.price") # String lookup
102
+ spryker_price = row.get("spryker.gross_price") # String lookup
103
+
104
+ # Plus column prefixing/unprefixing overhead:
105
+ # - ScanIterator prefixes: "field" -> "table.field"
106
+ # - ProjectIterator may unprefix: "table.field" -> "field"
107
+ ```
108
+
109
+ **Overhead:** String operations add ~10-50ns per column access.
110
+
111
+ ### 5. **Memory Access Patterns**
112
+
113
+ **Original Script:**
114
+ ```python
115
+ # All data in memory, excellent cache locality
116
+ # Sequential access through lists
117
+ # Dictionary lookups hit CPU cache
118
+ ```
119
+
120
+ **SQL Engine:**
121
+ ```python
122
+ # Streaming access - may cause cache misses
123
+ # Iterator chain creates function call overhead
124
+ # Less predictable memory access patterns
125
+ ```
126
+
127
+ **Overhead:** Cache misses and function call overhead.
128
+
129
+ ## Performance Breakdown (Estimated)
130
+
131
+ For processing 1 million rows:
132
+
133
+ | Component | Original Script | SQL Engine | Difference |
134
+ |-----------|----------------|------------|------------|
135
+ | **Data Loading** | 0.5s | 0.5s | Same |
136
+ | **Index Building** | 0.2s | 0.3s | 1.5x slower |
137
+ | **SQL Parsing** | 0s | 0.05s | New overhead |
138
+ | **Planning** | 0s | 0.02s | New overhead |
139
+ | **Row Processing** | 1.0s | 3.0s | 3x slower |
140
+ | **Total** | **1.7s** | **3.87s** | **2.3x slower** |
141
+
142
+ **Why row processing is slower:**
143
+ - Iterator chain overhead: ~0.5-1μs per row
144
+ - Column prefixing: ~0.1-0.3μs per row
145
+ - Function call overhead: ~0.2-0.5μs per row
146
+ - Total: ~0.8-1.8μs per row × 1M rows = 0.8-1.8s overhead
147
+
148
+ ## When SQL Engine is Worth It
149
+
150
+ The SQL engine is **NOT faster** for simple cases like your reconciliation script. It's designed for:
151
+
152
+ ### ✅ **Use SQL Engine When:**
153
+
154
+ 1. **Cross-system joins** (Database + API + Files)
155
+ ```python
156
+ # Can't do this with simple dictionaries
157
+ SELECT * FROM database_table
158
+ JOIN api_source ON ...
159
+ JOIN file_source ON ...
160
+ ```
161
+
162
+ 2. **Dynamic queries** (user-provided SQL)
163
+ ```python
164
+ # Users can write SQL queries
165
+ query = user_input # Can't hardcode dictionary lookups
166
+ ```
167
+
168
+ 3. **Complex WHERE clauses** (many conditions)
169
+ ```python
170
+ # SQL engine handles complex expressions
171
+ WHERE (a > 10 AND b < 20) OR (c = 'x' AND d IN (1,2,3))
172
+ ```
173
+
174
+ 4. **Multiple join types** (INNER, LEFT, etc.)
175
+ ```python
176
+ # SQL engine handles different join types
177
+ LEFT JOIN, INNER JOIN, etc.
178
+ ```
179
+
180
+ 5. **Streaming large datasets** (can't fit in memory)
181
+ ```python
182
+ # SQL engine streams, original script loads all into memory
183
+ ```
184
+
185
+ ### ❌ **Don't Use SQL Engine When:**
186
+
187
+ 1. **Simple lookups** (like your reconciliation)
188
+ ```python
189
+ # Simple dictionary lookup is faster
190
+ if key in dict:
191
+ value = dict[key]
192
+ ```
193
+
194
+ 2. **All data fits in memory**
195
+ ```python
196
+ # Loading all data once is faster than streaming
197
+ ```
198
+
199
+ 3. **Fixed query patterns**
200
+ ```python
201
+ # Hardcoded logic is faster than SQL parsing
202
+ ```
203
+
204
+ 4. **Performance is critical**
205
+ ```python
206
+ # Dictionary lookups are fastest for simple cases
207
+ ```
208
+
209
+ ## Optimization Strategies
210
+
211
+ ### Option 1: **Hybrid Approach** (Best Performance)
212
+
213
+ Use original script for reconciliation, SQL engine for complex queries:
214
+
215
+ ```python
216
+ class PostProcessor:
217
+ def __init__(self):
218
+ # Use original approach for reconciliation
219
+ self.use_sql_engine = False # Set to True only for complex queries
220
+
221
+ def process_reference_matches(self, duplicates):
222
+ if self.use_sql_engine:
223
+ # Use SQL engine for complex queries
224
+ return self._process_with_sql()
225
+ else:
226
+ # Use fast dictionary approach
227
+ return self._process_with_dicts()
228
+ ```
229
+
230
+ ### Option 2: **Optimize SQL Engine** (Moderate Improvement)
231
+
232
+ Reduce overhead by:
233
+ 1. **Caching parsed queries** (if same query used multiple times)
234
+ 2. **Pre-building indexes** (if data doesn't change)
235
+ 3. **Reducing iterator chain depth** (combine operations)
236
+ 4. **Using Cython/C extensions** (for hot paths)
237
+
238
+ ### Option 3: **Use Original Script** (Best for Your Case)
239
+
240
+ For your reconciliation use case, **the original script is the right choice** because:
241
+ - ✅ Simple lookup patterns
242
+ - ✅ All data fits in memory
243
+ - ✅ Fixed query patterns
244
+ - ✅ Performance is critical
245
+
246
+ ## Conclusion
247
+
248
+ **The original script is faster because:**
249
+ 1. **No SQL parsing overhead** - Direct Python code execution
250
+ 2. **Simple dictionary lookups** - O(1) hash table access, no iterator chain
251
+ 3. **Better memory access** - All data in memory, excellent cache locality
252
+ 4. **No column prefixing** - Direct field access
253
+
254
+ **The SQL engine adds overhead for:**
255
+ 1. SQL parsing and planning (~25-90ms per query)
256
+ 2. Iterator chain processing (~0.8-1.8μs per row)
257
+ 3. Column prefixing/unprefixing (~0.1-0.3μs per row)
258
+ 4. Function call overhead (~0.2-0.5μs per row)
259
+
260
+ **For your reconciliation script, stick with the original approach** - it's the right tool for the job!
261
+
262
+ The SQL engine is designed for **flexibility and cross-system joins**, not raw performance for simple cases.
263
+