streaming-sql-engine 0.1.9__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.9/streaming_sql_engine.egg-info → streaming_sql_engine-0.1.11}/PKG-INFO +1 -1
  2. {streaming_sql_engine-0.1.9 → 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/WHY_ORIGINAL_IS_FASTER.md +263 -0
  5. streaming_sql_engine-0.1.11/documentation/XML_MONGO_USE_CASE.md +224 -0
  6. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/evaluator.py +57 -0
  7. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/pyproject.toml +1 -1
  8. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/setup.py +1 -1
  9. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11/streaming_sql_engine.egg-info}/PKG-INFO +1 -1
  10. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/streaming_sql_engine.egg-info/SOURCES.txt +3 -0
  11. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/DEPLOYMENT.md +0 -0
  12. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/MANIFEST.in +0 -0
  13. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/QUICK_DEPLOY.md +0 -0
  14. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/QUICK_FIX.md +0 -0
  15. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/README.md +0 -0
  16. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/documentation/APACHE_DRILL_COMPARISON.md +0 -0
  17. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/documentation/API_JOIN_OPERATIONS.md +0 -0
  18. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/documentation/ARCHITECTURAL_RECOMMENDATION.md +0 -0
  19. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/documentation/ARCHITECTURE_DECISION.md +0 -0
  20. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/documentation/ARCHITECTURE_SYNCED.md +0 -0
  21. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/documentation/BEST_USE_CASES.md +0 -0
  22. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/documentation/BETTER_FILTER_APPROACH.md +0 -0
  23. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/documentation/CHANGES_SUMMARY.md +0 -0
  24. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/documentation/COMPREHENSIVE_DOCUMENTATION.md +0 -0
  25. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/documentation/DEVELOPER_GUIDE.md +0 -0
  26. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/documentation/DUCKDB_COMPARISON.md +0 -0
  27. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/documentation/FINAL_IMPLEMENTATION_RECOMMENDATIONS.md +0 -0
  28. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/documentation/FINAL_RECOMMENDATION.md +0 -0
  29. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/documentation/HOW_PROTOCOL_DETECTION_WORKS.md +0 -0
  30. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/documentation/HOW_TO_ADD_TO_PROJECT.md +0 -0
  31. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/documentation/INSTALLATION.md +0 -0
  32. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/documentation/JSONL_EXECUTOR_ANALYSIS.md +0 -0
  33. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/documentation/JSONL_EXECUTOR_REMOVED.md +0 -0
  34. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/documentation/LIBRARY_DOCUMENTATION.md +0 -0
  35. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/documentation/MIGRATION_GUIDE.md +0 -0
  36. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/documentation/MULTI_SOURCE_EXAMPLE_README.md +0 -0
  37. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/documentation/MYSQL_USAGE.md +0 -0
  38. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/documentation/OPTIMIZATIONS_BREAKDOWN.md +0 -0
  39. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/documentation/OPTIMIZATIONS_NOT_DATABASE_ONLY.md +0 -0
  40. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/documentation/OPTIMIZATIONS_VS_POLARS.md +0 -0
  41. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/documentation/OPTIMIZER_AUTOMATED.md +0 -0
  42. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/documentation/PERFORMANCE.md +0 -0
  43. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/documentation/PERFORMANCE_COMPARISON.md +0 -0
  44. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/documentation/POLARS_AND_MMAP_GUIDE.md +0 -0
  45. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/documentation/PROTOCOL_AND_OPTIMIZATIONS_EXPLAINED.md +0 -0
  46. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/documentation/PROTOCOL_EXPLANATION.md +0 -0
  47. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/documentation/PUBLISHING.md +0 -0
  48. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/documentation/PYPI_CHECKLIST.md +0 -0
  49. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/documentation/QUICK_PUBLISH.md +0 -0
  50. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/documentation/QUICK_START.md +0 -0
  51. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/documentation/SIMPLE_PROTOCOL_GUIDE.md +0 -0
  52. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/documentation/SIMPLE_SOURCE_FUNCTIONS.md +0 -0
  53. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/documentation/SOURCE_COMBINATIONS_GUIDE.md +0 -0
  54. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/documentation/TECHNICAL_DOCUMENTATION.md +0 -0
  55. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/documentation/USER_GUIDE.md +0 -0
  56. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/documentation/WHAT_IS_WHERE_TO_PARAMS.md +0 -0
  57. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/documentation/WHEN_POLARS_IS_USED.md +0 -0
  58. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/documentation/WHY_API_CONVERTER_NEEDED.md +0 -0
  59. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/documentation/WHY_MANUAL_PARSING.md +0 -0
  60. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/documentation/WHY_PROTOCOL_IN_SOURCES.md +0 -0
  61. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/engine.py +0 -0
  62. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/executor.py +0 -0
  63. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/mmap_index.py +0 -0
  64. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/operators.py +0 -0
  65. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/operators_mmap.py +0 -0
  66. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/operators_polars.py +0 -0
  67. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/optimizer.py +0 -0
  68. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/parser.py +0 -0
  69. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/planner.py +0 -0
  70. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/polars_expression_translator.py +0 -0
  71. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/polars_operators.py +0 -0
  72. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/protocol_helpers.py +0 -0
  73. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/requirements.txt +0 -0
  74. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/setup.cfg +0 -0
  75. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/streaming_sql_engine.egg-info/dependency_links.txt +0 -0
  76. {streaming_sql_engine-0.1.9 → streaming_sql_engine-0.1.11}/streaming_sql_engine.egg-info/requires.txt +0 -0
  77. {streaming_sql_engine-0.1.9 → 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.9
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.9"
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,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
+
@@ -0,0 +1,224 @@
1
+ # XML Price Comparison with MongoDB EAN Matching - Use Case Analysis
2
+
3
+ ## Your Use Case
4
+
5
+ 1. **Compare two XML files** for price differences
6
+ 2. **If price differs**, check if XML EAN matches EAN in MongoDB collection
7
+ 3. **If match found**, print the result
8
+
9
+ ## Is This a Good Case for SQL Engine?
10
+
11
+ ### ✅ **YES - This is a PERFECT use case!**
12
+
13
+ Here's why:
14
+
15
+ ## Why SQL Engine is Ideal Here
16
+
17
+ ### 1. **Cross-System Joins** ✅
18
+
19
+ You need to join:
20
+ - **XML File 1** (prices)
21
+ - **XML File 2** (prices)
22
+ - **MongoDB Collection** (EANs)
23
+
24
+ The SQL engine excels at joining different data sources!
25
+
26
+ ### 2. **Complex Matching Logic** ✅
27
+
28
+ You need:
29
+ - Compare prices between two XMLs
30
+ - Match EANs with MongoDB
31
+ - Conditional logic (only if price differs)
32
+
33
+ SQL makes this easy!
34
+
35
+ ### 3. **Dynamic Query Patterns** ✅
36
+
37
+ If your comparison logic changes, you can modify SQL without rewriting Python code.
38
+
39
+ ## Implementation Example
40
+
41
+ ```python
42
+ from streaming_sql_engine import Engine
43
+ from streaming_sql_engine.protocol_helpers import register_file_source
44
+ import pymongo
45
+
46
+ # Initialize engine
47
+ engine = Engine(debug=True, use_polars=True)
48
+
49
+ # Register XML File 1 (prices)
50
+ def xml1_source():
51
+ # Parse XML and yield rows
52
+ import xml.etree.ElementTree as ET
53
+ tree = ET.parse('prices1.xml')
54
+ for item in tree.findall('.//item'):
55
+ yield {
56
+ 'ean': item.find('ean').text,
57
+ 'price': float(item.find('price').text),
58
+ 'name': item.find('name').text
59
+ }
60
+
61
+ # Register XML File 2 (prices)
62
+ def xml2_source():
63
+ import xml.etree.ElementTree as ET
64
+ tree = ET.parse('prices2.xml')
65
+ for item in tree.findall('.//item'):
66
+ yield {
67
+ 'ean': item.find('ean').text,
68
+ 'price': float(item.find('price').text),
69
+ 'name': item.find('name').text
70
+ }
71
+
72
+ # Register MongoDB collection (EANs)
73
+ def mongo_source():
74
+ client = pymongo.MongoClient('mongodb://localhost:27017/')
75
+ db = client['your_database']
76
+ collection = db['your_collection']
77
+
78
+ for doc in collection.find({}, {'ean': 1, 'product_name': 1}):
79
+ yield {
80
+ 'ean': doc.get('ean'),
81
+ 'product_name': doc.get('product_name')
82
+ }
83
+
84
+ # Register sources
85
+ engine.register('xml1', xml1_source)
86
+ engine.register('xml2', xml2_source)
87
+ engine.register('mongo', mongo_source)
88
+
89
+ # Query: Find price differences and match with MongoDB
90
+ query = """
91
+ SELECT
92
+ xml1.ean,
93
+ xml1.price AS price1,
94
+ xml2.price AS price2,
95
+ xml1.price - xml2.price AS price_difference,
96
+ mongo.product_name
97
+ FROM xml1
98
+ JOIN xml2 ON xml1.ean = xml2.ean
99
+ JOIN mongo ON xml1.ean = mongo.ean
100
+ WHERE xml1.price != xml2.price
101
+ """
102
+
103
+ # Execute and print results
104
+ for row in engine.query(query):
105
+ print(f"EAN: {row['ean']}")
106
+ print(f" Price 1: {row['price1']}")
107
+ print(f" Price 2: {row['price2']}")
108
+ print(f" Difference: {row['price_difference']}")
109
+ print(f" Product: {row['product_name']}")
110
+ print()
111
+ ```
112
+
113
+ ## Alternative: Without SQL Engine
114
+
115
+ If you did this without SQL engine, you'd need:
116
+
117
+ ```python
118
+ # Load XML 1 into dict
119
+ xml1_dict = {}
120
+ for item in parse_xml1():
121
+ xml1_dict[item['ean']] = item
122
+
123
+ # Load XML 2 into dict
124
+ xml2_dict = {}
125
+ for item in parse_xml2():
126
+ xml2_dict[item['ean']] = item
127
+
128
+ # Load MongoDB into dict
129
+ mongo_dict = {}
130
+ for doc in mongo_collection.find():
131
+ mongo_dict[doc['ean']] = doc
132
+
133
+ # Manual comparison and matching
134
+ for ean in xml1_dict:
135
+ if ean in xml2_dict:
136
+ if xml1_dict[ean]['price'] != xml2_dict[ean]['price']:
137
+ if ean in mongo_dict:
138
+ print(...)
139
+ ```
140
+
141
+ **Problems with manual approach:**
142
+ - ❌ More code to write
143
+ - ❌ Harder to maintain
144
+ - ❌ Less flexible
145
+ - ❌ Need to handle all edge cases manually
146
+
147
+ ## Performance Comparison
148
+
149
+ | Approach | Performance | Complexity | Flexibility |
150
+ |----------|-------------|------------|-------------|
151
+ | **SQL Engine** | Good (streaming) | Low (SQL) | High (easy to modify) |
152
+ | **Manual Dicts** | Faster (in-memory) | High (more code) | Low (hard to change) |
153
+
154
+ **For your use case:**
155
+ - If XMLs are **small** (< 100K items): Manual dicts might be faster
156
+ - If XMLs are **large** (> 100K items): SQL engine is better (streaming)
157
+ - If you need **flexibility**: SQL engine wins
158
+
159
+ ## Recommendation
160
+
161
+ ### ✅ **Use SQL Engine if:**
162
+
163
+ 1. XML files are large (> 100K items)
164
+ 2. You want easy query modifications
165
+ 3. You need to add more data sources later
166
+ 4. You want readable, maintainable code
167
+
168
+ ### ⚠️ **Consider Manual Approach if:**
169
+
170
+ 1. XML files are very small (< 10K items)
171
+ 2. Performance is absolutely critical
172
+ 3. Query logic never changes
173
+ 4. You're comfortable with manual dict management
174
+
175
+ ## Optimized Implementation
176
+
177
+ For best performance with SQL engine:
178
+
179
+ ```python
180
+ from streaming_sql_engine import Engine
181
+ from streaming_sql_engine.protocol_helpers import register_file_source
182
+
183
+ engine = Engine(debug=False, use_polars=True, first_match_only=True)
184
+
185
+ # Use protocol helpers for automatic optimizations
186
+ register_file_source(engine, 'xml1', 'prices1.xml')
187
+ register_file_source(engine, 'xml2', 'prices2.xml')
188
+
189
+ # MongoDB source with protocol support
190
+ def mongo_source():
191
+ # Your MongoDB code
192
+ pass
193
+
194
+ engine.register('mongo', mongo_source)
195
+
196
+ # Optimized query
197
+ query = """
198
+ SELECT
199
+ xml1.ean,
200
+ xml1.price AS price1,
201
+ xml2.price AS price2,
202
+ mongo.product_name
203
+ FROM xml1
204
+ JOIN xml2 ON xml1.ean = xml2.ean
205
+ JOIN mongo ON xml1.ean = mongo.ean
206
+ WHERE xml1.price != xml2.price
207
+ """
208
+
209
+ for row in engine.query(query):
210
+ print(row)
211
+ ```
212
+
213
+ ## Summary
214
+
215
+ **YES, this is an EXCELLENT use case for the SQL engine because:**
216
+
217
+ 1. ✅ **Cross-system joins** (XML + XML + MongoDB)
218
+ 2. ✅ **Complex matching logic** (price comparison + EAN matching)
219
+ 3. ✅ **Readable SQL queries** (easier than manual dict management)
220
+ 4. ✅ **Flexible** (easy to modify queries)
221
+ 5. ✅ **Streaming** (handles large XML files efficiently)
222
+
223
+ The SQL engine is designed exactly for cases like this - joining data from different sources with SQL!
224
+
@@ -21,6 +21,12 @@ _IS_TYPE = exp.Is
21
21
  _ALIAS_TYPE = exp.Alias
22
22
  _PAREN_TYPE = exp.Paren
23
23
  _IN_TYPE = exp.In
24
+ # Arithmetic operations
25
+ _ADD_TYPE = exp.Add
26
+ _SUB_TYPE = exp.Sub
27
+ _MUL_TYPE = exp.Mul
28
+ _DIV_TYPE = exp.Div
29
+ _MOD_TYPE = exp.Mod
24
30
 
25
31
 
26
32
  def _coerce_comparable_types(left, right):
@@ -213,6 +219,57 @@ def evaluate_expression(expr, row):
213
219
  # If it reaches here, unwrap it and evaluate the inner expression
214
220
  return evaluate_expression(expr.this, row)
215
221
 
222
+ elif expr_type is _ADD_TYPE:
223
+ # Addition: left + right
224
+ left = evaluate_expression(expr.this, row)
225
+ right = evaluate_expression(expr.expression, row)
226
+ # Handle None values
227
+ if left is None or right is None:
228
+ return None
229
+ return left + right
230
+
231
+ elif expr_type is _SUB_TYPE:
232
+ # Subtraction: left - right
233
+ left = evaluate_expression(expr.this, row)
234
+ right = evaluate_expression(expr.expression, row)
235
+ # Handle None values
236
+ if left is None or right is None:
237
+ return None
238
+ return left - right
239
+
240
+ elif expr_type is _MUL_TYPE:
241
+ # Multiplication: left * right
242
+ left = evaluate_expression(expr.this, row)
243
+ right = evaluate_expression(expr.expression, row)
244
+ # Handle None values
245
+ if left is None or right is None:
246
+ return None
247
+ return left * right
248
+
249
+ elif expr_type is _DIV_TYPE:
250
+ # Division: left / right
251
+ left = evaluate_expression(expr.this, row)
252
+ right = evaluate_expression(expr.expression, row)
253
+ # Handle None values
254
+ if left is None or right is None:
255
+ return None
256
+ # Handle division by zero
257
+ if right == 0:
258
+ return None # Return None for division by zero (SQL behavior)
259
+ return left / right
260
+
261
+ elif expr_type is _MOD_TYPE:
262
+ # Modulo: left % right
263
+ left = evaluate_expression(expr.this, row)
264
+ right = evaluate_expression(expr.expression, row)
265
+ # Handle None values
266
+ if left is None or right is None:
267
+ return None
268
+ # Handle modulo by zero
269
+ if right == 0:
270
+ return None # Return None for modulo by zero
271
+ return left % right
272
+
216
273
  else:
217
274
  raise ValueError(f"Unsupported expression type: {type(expr).__name__}")
218
275
 
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "streaming-sql-engine"
7
- version = "0.1.9"
7
+ version = "0.1.11"
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.9",
15
+ version="0.1.11",
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",
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: streaming-sql-engine
3
- Version: 0.1.9
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
@@ -53,6 +53,7 @@ documentation/DEVELOPER_GUIDE.md
53
53
  documentation/DUCKDB_COMPARISON.md
54
54
  documentation/FINAL_IMPLEMENTATION_RECOMMENDATIONS.md
55
55
  documentation/FINAL_RECOMMENDATION.md
56
+ documentation/FIRST_MATCH_ONLY_WARNINGS.md
56
57
  documentation/HOW_PROTOCOL_DETECTION_WORKS.md
57
58
  documentation/HOW_TO_ADD_TO_PROJECT.md
58
59
  documentation/INSTALLATION.md
@@ -84,7 +85,9 @@ documentation/WHAT_IS_WHERE_TO_PARAMS.md
84
85
  documentation/WHEN_POLARS_IS_USED.md
85
86
  documentation/WHY_API_CONVERTER_NEEDED.md
86
87
  documentation/WHY_MANUAL_PARSING.md
88
+ documentation/WHY_ORIGINAL_IS_FASTER.md
87
89
  documentation/WHY_PROTOCOL_IN_SOURCES.md
90
+ documentation/XML_MONGO_USE_CASE.md
88
91
  streaming_sql_engine.egg-info/PKG-INFO
89
92
  streaming_sql_engine.egg-info/SOURCES.txt
90
93
  streaming_sql_engine.egg-info/dependency_links.txt