streaming-sql-engine 0.1.22__tar.gz → 0.1.23__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.22 → streaming_sql_engine-0.1.23}/PKG-INFO +651 -279
- streaming_sql_engine-0.1.22/streaming_sql_engine.egg-info/PKG-INFO → streaming_sql_engine-0.1.23/README.md +1806 -1480
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/pyproject.toml +1 -1
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/setup.py +1 -1
- streaming_sql_engine-0.1.22/README.md → streaming_sql_engine-0.1.23/streaming_sql_engine.egg-info/PKG-INFO +1852 -1434
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/DEPLOYMENT.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/MANIFEST.in +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/QUICK_DEPLOY.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/QUICK_FIX.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/RELEASE_READINESS_1.0.0.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/__init__.py +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/debug_tests.py +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/documentation/APACHE_DRILL_COMPARISON.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/documentation/API_JOIN_OPERATIONS.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/documentation/ARCHITECTURAL_RECOMMENDATION.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/documentation/ARCHITECTURE_DECISION.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/documentation/ARCHITECTURE_SYNCED.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/documentation/BEST_USE_CASES.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/documentation/BETTER_FILTER_APPROACH.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/documentation/CARTESIAN_PRODUCTS_CAUSES.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/documentation/CHANGES_SUMMARY.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/documentation/COMPREHENSIVE_ARTICLE.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/documentation/COMPREHENSIVE_DOCUMENTATION.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/documentation/DEVELOPER_GUIDE.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/documentation/DUCKDB_COMPARISON.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/documentation/ENGINE_OPTIONS_REFERENCE.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/documentation/FINAL_IMPLEMENTATION_RECOMMENDATIONS.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/documentation/FINAL_RECOMMENDATION.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/documentation/FIRST_MATCH_ONLY_WARNINGS.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/documentation/HANDLING_DUPLICATES_WITHOUT_CARTESIAN.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/documentation/HOW_PROTOCOL_DETECTION_WORKS.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/documentation/HOW_TO_ADD_TO_PROJECT.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/documentation/INSTALLATION.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/documentation/JSONL_EXECUTOR_ANALYSIS.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/documentation/JSONL_EXECUTOR_REMOVED.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/documentation/LIBRARY_DOCUMENTATION.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/documentation/MEDIUM_ARTICLE.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/documentation/MEDIUM_ARTICLE_SIMPLIFIED.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/documentation/MIGRATION_GUIDE.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/documentation/MMAP_WITHOUT_POLARS_SLOW.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/documentation/MOST_STABLE_AND_PERFORMANT.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/documentation/MULTI_SOURCE_EXAMPLE_README.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/documentation/MYSQL_USAGE.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/documentation/OPTIMIZATIONS_BREAKDOWN.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/documentation/OPTIMIZATIONS_NOT_DATABASE_ONLY.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/documentation/OPTIMIZATIONS_VS_POLARS.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/documentation/OPTIMIZER_AUTOMATED.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/documentation/PERFORMANCE.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/documentation/PERFORMANCE_COMPARISON.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/documentation/POLARS_AND_MMAP_GUIDE.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/documentation/POLARS_VS_POLARS_MMAP.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/documentation/PROTOCOL_AND_OPTIMIZATIONS_EXPLAINED.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/documentation/PROTOCOL_EXPLANATION.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/documentation/PUBLISHING.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/documentation/PYPI_CHECKLIST.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/documentation/QUICK_PUBLISH.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/documentation/QUICK_START.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/documentation/SIMPLE_PROTOCOL_GUIDE.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/documentation/SIMPLE_SOURCE_FUNCTIONS.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/documentation/SOURCE_COMBINATIONS_GUIDE.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/documentation/TECHNICAL_DOCUMENTATION.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/documentation/USER_GUIDE.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/documentation/WHAT_IS_WHERE_TO_PARAMS.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/documentation/WHEN_POLARS_IS_USED.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/documentation/WHERE_POLARS_IS_USED.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/documentation/WHY_API_CONVERTER_NEEDED.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/documentation/WHY_MANUAL_PARSING.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/documentation/WHY_ORIGINAL_IS_FASTER.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/documentation/WHY_PROTOCOL_IN_SOURCES.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/documentation/XML_MONGO_USE_CASE.md +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/engine.py +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/evaluator.py +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/executor.py +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/mmap_index.py +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/operators.py +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/operators_mmap.py +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/operators_polars.py +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/optimizer.py +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/parser.py +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/planner.py +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/polars_expression_translator.py +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/polars_operators.py +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/pre_release_test.py +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/protocol_helpers.py +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/quick_test.py +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/requirements.txt +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/setup.cfg +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/streaming_sql_engine.egg-info/SOURCES.txt +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/streaming_sql_engine.egg-info/dependency_links.txt +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/streaming_sql_engine.egg-info/requires.txt +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/streaming_sql_engine.egg-info/top_level.txt +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/test_full_pipeline.py +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/test_polars_fixes.py +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/test_polars_schema_fix.py +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/test_where_debug.py +0 -0
- {streaming_sql_engine-0.1.22 → streaming_sql_engine-0.1.23}/tests/test_engine_options.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: streaming-sql-engine
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.23
|
|
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
|
|
@@ -193,284 +193,6 @@ The engine follows a **pipeline architecture** inspired by database query execut
|
|
|
193
193
|
|
|
194
194
|
**Key insight:** Python's iterator protocol is perfect for streaming data. Each operator in the pipeline is an iterator that processes rows one at a time.
|
|
195
195
|
|
|
196
|
-
### The Architecture: Four Layers
|
|
197
|
-
|
|
198
|
-
#### Layer 1: SQL Parsing
|
|
199
|
-
|
|
200
|
-
**Technology:** Uses `sqlglot` library for SQL parsing
|
|
201
|
-
|
|
202
|
-
**Why sqlglot:**
|
|
203
|
-
|
|
204
|
-
- Supports multiple SQL dialects (MySQL, PostgreSQL, SQLite)
|
|
205
|
-
- Produces a standard AST (Abstract Syntax Tree)
|
|
206
|
-
- Handles SQL parsing edge cases
|
|
207
|
-
- No need to write a parser from scratch
|
|
208
|
-
|
|
209
|
-
**How it works:**
|
|
210
|
-
|
|
211
|
-
```python
|
|
212
|
-
def parse_sql(sql: str):
|
|
213
|
-
# Try multiple dialects
|
|
214
|
-
for dialect in ['mysql', 'postgres', 'sqlite']:
|
|
215
|
-
try:
|
|
216
|
-
ast = sqlglot.parse_one(sql, dialect=dialect)
|
|
217
|
-
return ast
|
|
218
|
-
except:
|
|
219
|
-
continue
|
|
220
|
-
raise ValueError("Could not parse SQL")
|
|
221
|
-
```
|
|
222
|
-
|
|
223
|
-
**Logic:** The parser converts SQL string into an AST, which is a tree structure representing the query. This AST is then used by the planner to build an execution plan.
|
|
224
|
-
|
|
225
|
-
#### Layer 2: Logical Planning
|
|
226
|
-
|
|
227
|
-
**Purpose:** Convert AST into a logical execution plan
|
|
228
|
-
|
|
229
|
-
**What it extracts:**
|
|
230
|
-
|
|
231
|
-
- Root table (the table in FROM clause)
|
|
232
|
-
- Join information (which tables to join, on what keys)
|
|
233
|
-
- WHERE clause conditions
|
|
234
|
-
- SELECT projections (which columns to return)
|
|
235
|
-
|
|
236
|
-
**Key design decision:** Separate logical planning from execution. This allows:
|
|
237
|
-
|
|
238
|
-
- Query optimization before execution
|
|
239
|
-
- Reuse of planning logic
|
|
240
|
-
- Easier testing and debugging
|
|
241
|
-
|
|
242
|
-
**How it works:**
|
|
243
|
-
|
|
244
|
-
```python
|
|
245
|
-
class LogicalPlan:
|
|
246
|
-
root_table: str
|
|
247
|
-
joins: List[JoinInfo]
|
|
248
|
-
where_expr: Optional[Expression]
|
|
249
|
-
projections: List[str]
|
|
250
|
-
required_columns: Dict[str, List[str]] # Column pruning
|
|
251
|
-
pushable_where_expr: Optional[Expression] # Filter pushdown
|
|
252
|
-
```
|
|
253
|
-
|
|
254
|
-
**Logic:** The planner walks the AST tree, extracting information about tables, joins, filters, and projections. It identifies which columns are needed from each table (for column pruning) and which WHERE conditions can be pushed to sources (for filter pushdown).
|
|
255
|
-
|
|
256
|
-
#### Layer 3: Optimization
|
|
257
|
-
|
|
258
|
-
**Two main optimizations:**
|
|
259
|
-
|
|
260
|
-
**1. Column Pruning**
|
|
261
|
-
|
|
262
|
-
- Identifies which columns are actually needed
|
|
263
|
-
- Only requests those columns from sources
|
|
264
|
-
- Reduces I/O and memory usage
|
|
265
|
-
|
|
266
|
-
**Logic:**
|
|
267
|
-
|
|
268
|
-
```python
|
|
269
|
-
# Analyze SELECT clause
|
|
270
|
-
required_columns = {}
|
|
271
|
-
for projection in plan.projections:
|
|
272
|
-
table_name = extract_table_name(projection)
|
|
273
|
-
column_name = extract_column_name(projection)
|
|
274
|
-
required_columns[table_name].add(column_name)
|
|
275
|
-
|
|
276
|
-
# Also check WHERE clause - columns used in filters are needed
|
|
277
|
-
for condition in plan.where_expr:
|
|
278
|
-
table_name = extract_table_name(condition)
|
|
279
|
-
column_name = extract_column_name(condition)
|
|
280
|
-
required_columns[table_name].add(column_name)
|
|
281
|
-
```
|
|
282
|
-
|
|
283
|
-
**2. Filter Pushdown**
|
|
284
|
-
|
|
285
|
-
- Identifies WHERE conditions that reference only the root table
|
|
286
|
-
- Pushes these conditions to the source function
|
|
287
|
-
- Reduces data transfer from databases
|
|
288
|
-
|
|
289
|
-
**Logic:**
|
|
290
|
-
|
|
291
|
-
```python
|
|
292
|
-
# Check if WHERE condition only references root table
|
|
293
|
-
def can_push_where(where_expr, root_table):
|
|
294
|
-
referenced_tables = extract_table_references(where_expr)
|
|
295
|
-
return len(referenced_tables) == 1 and root_table in referenced_tables
|
|
296
|
-
|
|
297
|
-
if can_push_where(plan.where_expr, plan.root_table):
|
|
298
|
-
pushable_where_expr = plan.where_expr
|
|
299
|
-
```
|
|
300
|
-
|
|
301
|
-
**Protocol-based detection:** The engine automatically detects if a source function supports optimizations by checking if it accepts `dynamic_where` and `dynamic_columns` parameters:
|
|
302
|
-
|
|
303
|
-
```python
|
|
304
|
-
import inspect
|
|
305
|
-
|
|
306
|
-
def source_supports_optimizations(source_fn):
|
|
307
|
-
sig = inspect.signature(source_fn)
|
|
308
|
-
params = list(sig.parameters.keys())
|
|
309
|
-
return 'dynamic_where' in params or 'dynamic_columns' in params
|
|
310
|
-
```
|
|
311
|
-
|
|
312
|
-
**Why this approach:** No flags needed. If your source function accepts these parameters, optimizations apply automatically. This makes the engine flexible - any Python function can be a source, and optimizations work if the source supports them.
|
|
313
|
-
|
|
314
|
-
#### Layer 4: Execution Pipeline
|
|
315
|
-
|
|
316
|
-
**The iterator pipeline:** Each operator is a Python iterator that processes rows incrementally.
|
|
317
|
-
|
|
318
|
-
**Pipeline structure:**
|
|
319
|
-
|
|
320
|
-
```
|
|
321
|
-
ScanIterator → FilterIterator → JoinIterators → ProjectIterator → Results
|
|
322
|
-
```
|
|
323
|
-
|
|
324
|
-
**How each iterator works:**
|
|
325
|
-
|
|
326
|
-
**1. ScanIterator**
|
|
327
|
-
|
|
328
|
-
- Reads rows from source function
|
|
329
|
-
- Prefixes columns with table name (e.g., `id` becomes `users.id`)
|
|
330
|
-
- Applies column pruning (only includes requested columns)
|
|
331
|
-
|
|
332
|
-
**Logic:**
|
|
333
|
-
|
|
334
|
-
```python
|
|
335
|
-
class ScanIterator:
|
|
336
|
-
def __init__(self, source_fn, table_name, required_columns):
|
|
337
|
-
self.source_fn = source_fn
|
|
338
|
-
self.table_name = table_name
|
|
339
|
-
self.required_columns = required_columns
|
|
340
|
-
|
|
341
|
-
def __next__(self):
|
|
342
|
-
row = next(self.source_iterator)
|
|
343
|
-
# Column pruning
|
|
344
|
-
if self.required_columns:
|
|
345
|
-
row = {k: v for k, v in row.items() if k in self.required_columns}
|
|
346
|
-
# Prefix columns with table name
|
|
347
|
-
return {f"{self.table_name}.{k}": v for k, v in row.items()}
|
|
348
|
-
```
|
|
349
|
-
|
|
350
|
-
**2. FilterIterator**
|
|
351
|
-
|
|
352
|
-
- Applies WHERE clause conditions
|
|
353
|
-
- Evaluates expressions row-by-row
|
|
354
|
-
- Only yields rows that match conditions
|
|
355
|
-
|
|
356
|
-
**Logic:**
|
|
357
|
-
|
|
358
|
-
```python
|
|
359
|
-
class FilterIterator:
|
|
360
|
-
def __init__(self, source_iterator, where_expr):
|
|
361
|
-
self.source_iterator = source_iterator
|
|
362
|
-
self.where_expr = where_expr
|
|
363
|
-
|
|
364
|
-
def __next__(self):
|
|
365
|
-
while True:
|
|
366
|
-
row = next(self.source_iterator)
|
|
367
|
-
if evaluate_expression(self.where_expr, row):
|
|
368
|
-
return row
|
|
369
|
-
# Skip rows that don't match
|
|
370
|
-
```
|
|
371
|
-
|
|
372
|
-
**3. JoinIterators (LookupJoinIterator, MergeJoinIterator)**
|
|
373
|
-
|
|
374
|
-
- **LookupJoinIterator:** Builds hash index on right side, looks up each left row
|
|
375
|
-
- **MergeJoinIterator:** Both sides sorted, merges like merge sort algorithm
|
|
376
|
-
|
|
377
|
-
**LookupJoinIterator logic:**
|
|
378
|
-
|
|
379
|
-
```python
|
|
380
|
-
class LookupJoinIterator:
|
|
381
|
-
def __init__(self, left_iterator, right_source_fn, left_key, right_key):
|
|
382
|
-
self.left_iterator = left_iterator
|
|
383
|
-
self.right_source_fn = right_source_fn
|
|
384
|
-
self.left_key = left_key
|
|
385
|
-
self.right_key = right_key
|
|
386
|
-
self.index = None # Hash index on right side
|
|
387
|
-
|
|
388
|
-
def _build_index(self):
|
|
389
|
-
"""Build hash index on right side - O(n) time, O(n) space"""
|
|
390
|
-
self.index = {}
|
|
391
|
-
for right_row in self.right_source_fn():
|
|
392
|
-
key = right_row[self.right_key]
|
|
393
|
-
if key not in self.index:
|
|
394
|
-
self.index[key] = []
|
|
395
|
-
self.index[key].append(right_row)
|
|
396
|
-
|
|
397
|
-
def __next__(self):
|
|
398
|
-
if self.index is None:
|
|
399
|
-
self._build_index()
|
|
400
|
-
|
|
401
|
-
left_row = next(self.left_iterator)
|
|
402
|
-
left_key_value = left_row[self.left_key]
|
|
403
|
-
|
|
404
|
-
# Lookup in index - O(1) time
|
|
405
|
-
matching_right_rows = self.index.get(left_key_value, [])
|
|
406
|
-
|
|
407
|
-
for right_row in matching_right_rows:
|
|
408
|
-
# Merge left and right rows
|
|
409
|
-
merged_row = {**left_row, **right_row}
|
|
410
|
-
yield merged_row
|
|
411
|
-
```
|
|
412
|
-
|
|
413
|
-
**Why hash index:** O(1) lookup time per left row. Trade-off: O(n) memory for right side, but this is necessary for efficient joins.
|
|
414
|
-
|
|
415
|
-
**MergeJoinIterator logic:**
|
|
416
|
-
|
|
417
|
-
```python
|
|
418
|
-
class MergeJoinIterator:
|
|
419
|
-
def __init__(self, left_iterator, right_iterator, left_key, right_key):
|
|
420
|
-
self.left_iterator = left_iterator
|
|
421
|
-
self.right_iterator = right_iterator
|
|
422
|
-
self.left_key = left_key
|
|
423
|
-
self.right_key = right_key
|
|
424
|
-
self.right_buffer = [] # Buffer for right side
|
|
425
|
-
|
|
426
|
-
def __next__(self):
|
|
427
|
-
left_row = next(self.left_iterator)
|
|
428
|
-
left_key_value = left_row[self.left_key]
|
|
429
|
-
|
|
430
|
-
# Advance right iterator until we find matching key
|
|
431
|
-
while True:
|
|
432
|
-
if not self.right_buffer:
|
|
433
|
-
right_row = next(self.right_iterator)
|
|
434
|
-
self.right_buffer.append(right_row)
|
|
435
|
-
|
|
436
|
-
right_key_value = self.right_buffer[0][self.right_key]
|
|
437
|
-
|
|
438
|
-
if right_key_value == left_key_value:
|
|
439
|
-
# Match found - merge and yield
|
|
440
|
-
right_row = self.right_buffer.pop(0)
|
|
441
|
-
return {**left_row, **right_row}
|
|
442
|
-
elif right_key_value > left_key_value:
|
|
443
|
-
# Right side ahead - no match for this left row
|
|
444
|
-
return None
|
|
445
|
-
else:
|
|
446
|
-
# Right side behind - advance right
|
|
447
|
-
self.right_buffer.pop(0)
|
|
448
|
-
```
|
|
449
|
-
|
|
450
|
-
**Why merge join:** O(n+m) time complexity, O(1) memory. Perfect for pre-sorted data.
|
|
451
|
-
|
|
452
|
-
**4. ProjectIterator**
|
|
453
|
-
|
|
454
|
-
- Applies SELECT clause
|
|
455
|
-
- Only includes requested columns
|
|
456
|
-
- Handles column aliases
|
|
457
|
-
|
|
458
|
-
**Logic:**
|
|
459
|
-
|
|
460
|
-
```python
|
|
461
|
-
class ProjectIterator:
|
|
462
|
-
def __init__(self, source_iterator, projections):
|
|
463
|
-
self.source_iterator = source_iterator
|
|
464
|
-
self.projections = projections # List of (table.col, alias) tuples
|
|
465
|
-
|
|
466
|
-
def __next__(self):
|
|
467
|
-
row = next(self.source_iterator)
|
|
468
|
-
result = {}
|
|
469
|
-
for table_col, alias in self.projections:
|
|
470
|
-
result[alias or table_col] = row[table_col]
|
|
471
|
-
return result
|
|
472
|
-
```
|
|
473
|
-
|
|
474
196
|
### Why Iterator-Based Architecture?
|
|
475
197
|
|
|
476
198
|
**Key advantages:**
|
|
@@ -1377,6 +1099,656 @@ Filter pushdown, column pruning, and vectorization applied automatically. No con
|
|
|
1377
1099
|
|
|
1378
1100
|
---
|
|
1379
1101
|
|
|
1102
|
+
## 🎯 Start Here: The Most Secure Way
|
|
1103
|
+
|
|
1104
|
+
### Step 1: Install and Basic Setup
|
|
1105
|
+
|
|
1106
|
+
```bash
|
|
1107
|
+
pip install streaming-sql-engine
|
|
1108
|
+
```
|
|
1109
|
+
|
|
1110
|
+
### Step 2: Use the Default Configuration (Most Stable)
|
|
1111
|
+
|
|
1112
|
+
**This is the safest way to start** — it handles all edge cases, works with any data types, and requires no special configuration:
|
|
1113
|
+
|
|
1114
|
+
```python
|
|
1115
|
+
from streaming_sql_engine import Engine
|
|
1116
|
+
|
|
1117
|
+
# Default configuration: Most stable and reliable
|
|
1118
|
+
engine = Engine() # use_polars=False (default)
|
|
1119
|
+
|
|
1120
|
+
# Register your data sources
|
|
1121
|
+
def postgres_users():
|
|
1122
|
+
# Your PostgreSQL connection code
|
|
1123
|
+
for row in cursor:
|
|
1124
|
+
yield {"id": row[0], "name": row[1]}
|
|
1125
|
+
|
|
1126
|
+
def mysql_orders():
|
|
1127
|
+
# Your MySQL connection code
|
|
1128
|
+
for row in cursor:
|
|
1129
|
+
yield {"id": row[0], "user_id": row[1], "total": row[2]}
|
|
1130
|
+
|
|
1131
|
+
engine.register("users", postgres_users)
|
|
1132
|
+
engine.register("orders", mysql_orders)
|
|
1133
|
+
|
|
1134
|
+
# Write SQL queries
|
|
1135
|
+
query = """
|
|
1136
|
+
SELECT users.name, orders.total
|
|
1137
|
+
FROM users
|
|
1138
|
+
JOIN orders ON users.id = orders.user_id
|
|
1139
|
+
WHERE orders.total > 100
|
|
1140
|
+
"""
|
|
1141
|
+
|
|
1142
|
+
# Execute and iterate results
|
|
1143
|
+
for row in engine.query(query):
|
|
1144
|
+
print(row)
|
|
1145
|
+
```
|
|
1146
|
+
|
|
1147
|
+
**Why This Configuration is Best to Start:**
|
|
1148
|
+
|
|
1149
|
+
✅ **Most Stable**: Handles mixed data types gracefully
|
|
1150
|
+
✅ **No Schema Errors**: No type inference issues
|
|
1151
|
+
✅ **Works Everywhere**: No external dependencies required
|
|
1152
|
+
✅ **Reliable**: Battle-tested Python code
|
|
1153
|
+
✅ **Fast for Small-Medium Data**: 0.72s for 10K rows
|
|
1154
|
+
|
|
1155
|
+
**Use this when:**
|
|
1156
|
+
|
|
1157
|
+
- You're just getting started
|
|
1158
|
+
- Your datasets are < 100K rows
|
|
1159
|
+
- You have mixed data types
|
|
1160
|
+
- You need maximum reliability
|
|
1161
|
+
- Polars is not available
|
|
1162
|
+
|
|
1163
|
+
---
|
|
1164
|
+
|
|
1165
|
+
## 🚀 Experimenting with Options
|
|
1166
|
+
|
|
1167
|
+
Once you're comfortable with the basics, you can experiment with different options to optimize for your specific use case.
|
|
1168
|
+
|
|
1169
|
+
### Option 1: Enable Debug Mode
|
|
1170
|
+
|
|
1171
|
+
See what's happening under the hood:
|
|
1172
|
+
|
|
1173
|
+
```python
|
|
1174
|
+
engine = Engine(debug=True) # Shows execution details
|
|
1175
|
+
```
|
|
1176
|
+
|
|
1177
|
+
**Output:**
|
|
1178
|
+
|
|
1179
|
+
```
|
|
1180
|
+
============================================================
|
|
1181
|
+
STREAMING SQL ENGINE - DEBUG MODE
|
|
1182
|
+
============================================================
|
|
1183
|
+
|
|
1184
|
+
[1/3] PARSING SQL QUERY...
|
|
1185
|
+
✓ SQL parsed successfully
|
|
1186
|
+
|
|
1187
|
+
[2/3] BUILDING LOGICAL PLAN...
|
|
1188
|
+
✓ Logical plan built
|
|
1189
|
+
|
|
1190
|
+
[3/3] EXECUTING QUERY...
|
|
1191
|
+
Using LOOKUP JOIN (building index...)
|
|
1192
|
+
```
|
|
1193
|
+
|
|
1194
|
+
---
|
|
1195
|
+
|
|
1196
|
+
### Option 2: Enable Polars (For Large Datasets)
|
|
1197
|
+
|
|
1198
|
+
**When to use**: Large datasets (> 100K rows), consistent data types
|
|
1199
|
+
|
|
1200
|
+
```python
|
|
1201
|
+
engine = Engine(use_polars=True) # Enable Polars for speed
|
|
1202
|
+
```
|
|
1203
|
+
|
|
1204
|
+
**Benefits:**
|
|
1205
|
+
|
|
1206
|
+
- ⚡ Faster for large datasets (vectorized operations)
|
|
1207
|
+
- 🎯 SIMD acceleration
|
|
1208
|
+
- 📊 Better for consistent schemas
|
|
1209
|
+
|
|
1210
|
+
**Trade-offs:**
|
|
1211
|
+
|
|
1212
|
+
- ⚠️ Requires data normalization (consistent types)
|
|
1213
|
+
- ⚠️ Can fail on mixed types
|
|
1214
|
+
- ⚠️ Requires Polars dependency
|
|
1215
|
+
|
|
1216
|
+
**Example:**
|
|
1217
|
+
|
|
1218
|
+
```python
|
|
1219
|
+
engine = Engine(use_polars=True)
|
|
1220
|
+
|
|
1221
|
+
# Make sure your data has consistent types
|
|
1222
|
+
def normalized_source():
|
|
1223
|
+
for row in raw_source():
|
|
1224
|
+
yield {
|
|
1225
|
+
"id": int(row.get("id", 0)),
|
|
1226
|
+
"price": float(row.get("price", 0.0)),
|
|
1227
|
+
"name": str(row.get("name", "")),
|
|
1228
|
+
}
|
|
1229
|
+
|
|
1230
|
+
engine.register("products", normalized_source)
|
|
1231
|
+
```
|
|
1232
|
+
|
|
1233
|
+
---
|
|
1234
|
+
|
|
1235
|
+
### Option 3: Enable MMAP (For Large Files)
|
|
1236
|
+
|
|
1237
|
+
**When to use**: Large files (> 100MB), memory-constrained systems
|
|
1238
|
+
|
|
1239
|
+
```python
|
|
1240
|
+
engine = Engine()
|
|
1241
|
+
engine.register("products", source, filename="products.jsonl") # MMAP enabled
|
|
1242
|
+
```
|
|
1243
|
+
|
|
1244
|
+
**Benefits:**
|
|
1245
|
+
|
|
1246
|
+
- 💾 90-99% memory reduction
|
|
1247
|
+
- 📁 Works with files larger than RAM
|
|
1248
|
+
- 🔄 OS-managed memory mapping
|
|
1249
|
+
|
|
1250
|
+
**Trade-offs:**
|
|
1251
|
+
|
|
1252
|
+
- ⚠️ Requires file-based sources
|
|
1253
|
+
- ⚠️ Slower for small files (overhead)
|
|
1254
|
+
|
|
1255
|
+
**Example:**
|
|
1256
|
+
|
|
1257
|
+
```python
|
|
1258
|
+
engine = Engine()
|
|
1259
|
+
|
|
1260
|
+
def jsonl_source():
|
|
1261
|
+
with open("products.jsonl", "r") as f:
|
|
1262
|
+
for line in f:
|
|
1263
|
+
if line.strip():
|
|
1264
|
+
yield json.loads(line)
|
|
1265
|
+
|
|
1266
|
+
engine.register("products", jsonl_source, filename="products.jsonl")
|
|
1267
|
+
```
|
|
1268
|
+
|
|
1269
|
+
---
|
|
1270
|
+
|
|
1271
|
+
### Option 4: Enable Merge Join (For Sorted Data)
|
|
1272
|
+
|
|
1273
|
+
**When to use**: Pre-sorted data, memory-constrained environments
|
|
1274
|
+
|
|
1275
|
+
```python
|
|
1276
|
+
engine = Engine()
|
|
1277
|
+
engine.register("products", source, ordered_by="id") # Merge join enabled
|
|
1278
|
+
```
|
|
1279
|
+
|
|
1280
|
+
**Benefits:**
|
|
1281
|
+
|
|
1282
|
+
- 💾 Lowest memory usage (no index needed)
|
|
1283
|
+
- ⚡ Fast for sorted data
|
|
1284
|
+
- 🔄 Streaming algorithm
|
|
1285
|
+
|
|
1286
|
+
**Trade-offs:**
|
|
1287
|
+
|
|
1288
|
+
- ⚠️ Requires pre-sorted data
|
|
1289
|
+
- ⚠️ Both tables must be sorted
|
|
1290
|
+
|
|
1291
|
+
**Example:**
|
|
1292
|
+
|
|
1293
|
+
```python
|
|
1294
|
+
engine = Engine()
|
|
1295
|
+
|
|
1296
|
+
# Data must be sorted by join key
|
|
1297
|
+
def sorted_users():
|
|
1298
|
+
# Users sorted by id
|
|
1299
|
+
return iter([
|
|
1300
|
+
{"id": 1, "name": "Alice"},
|
|
1301
|
+
{"id": 2, "name": "Bob"},
|
|
1302
|
+
{"id": 3, "name": "Charlie"},
|
|
1303
|
+
])
|
|
1304
|
+
|
|
1305
|
+
def sorted_orders():
|
|
1306
|
+
# Orders sorted by user_id
|
|
1307
|
+
return iter([
|
|
1308
|
+
{"id": 1, "user_id": 1, "total": 100},
|
|
1309
|
+
{"id": 2, "user_id": 2, "total": 200},
|
|
1310
|
+
])
|
|
1311
|
+
|
|
1312
|
+
engine.register("users", sorted_users, ordered_by="id")
|
|
1313
|
+
engine.register("orders", sorted_orders, ordered_by="user_id")
|
|
1314
|
+
```
|
|
1315
|
+
|
|
1316
|
+
---
|
|
1317
|
+
|
|
1318
|
+
## 🔬 Advanced: Mixing Options
|
|
1319
|
+
|
|
1320
|
+
### Mix 1: Polars + MMAP (Best for Large Files)
|
|
1321
|
+
|
|
1322
|
+
**The Ultimate Configuration** for large files with consistent types:
|
|
1323
|
+
|
|
1324
|
+
```python
|
|
1325
|
+
engine = Engine(use_polars=True) # Polars for speed
|
|
1326
|
+
engine.register("products", source, filename="products.jsonl") # MMAP for memory
|
|
1327
|
+
```
|
|
1328
|
+
|
|
1329
|
+
**What You Get:**
|
|
1330
|
+
|
|
1331
|
+
- ⚡ Fast (Polars vectorized operations)
|
|
1332
|
+
- 💾 Low memory (MMAP 90-99% reduction)
|
|
1333
|
+
- 🎯 Best balance for large datasets
|
|
1334
|
+
|
|
1335
|
+
**Performance:**
|
|
1336
|
+
|
|
1337
|
+
- Time: 8-15s for 500MB files
|
|
1338
|
+
- Memory: 0.01 MB (vs 500MB+ without MMAP)
|
|
1339
|
+
|
|
1340
|
+
**When to Use:**
|
|
1341
|
+
|
|
1342
|
+
- Large files (> 100MB)
|
|
1343
|
+
- Consistent data types
|
|
1344
|
+
- Memory-constrained systems
|
|
1345
|
+
|
|
1346
|
+
---
|
|
1347
|
+
|
|
1348
|
+
### Mix 2: Polars + Column Pruning (For Wide Tables)
|
|
1349
|
+
|
|
1350
|
+
**Optimize for tables with many columns:**
|
|
1351
|
+
|
|
1352
|
+
```python
|
|
1353
|
+
engine = Engine(use_polars=True)
|
|
1354
|
+
|
|
1355
|
+
def optimized_source(dynamic_columns=None):
|
|
1356
|
+
# Only read requested columns
|
|
1357
|
+
if dynamic_columns:
|
|
1358
|
+
columns = dynamic_columns
|
|
1359
|
+
else:
|
|
1360
|
+
columns = ["id", "name", "price", "description", "category", ...] # All columns
|
|
1361
|
+
|
|
1362
|
+
for row in read_data(columns):
|
|
1363
|
+
yield row
|
|
1364
|
+
|
|
1365
|
+
engine.register("products", optimized_source)
|
|
1366
|
+
```
|
|
1367
|
+
|
|
1368
|
+
**What You Get:**
|
|
1369
|
+
|
|
1370
|
+
- 📉 Reduced I/O (only reads needed columns)
|
|
1371
|
+
- ⚡ Faster queries (less data to process)
|
|
1372
|
+
- 💾 Lower memory usage
|
|
1373
|
+
|
|
1374
|
+
---
|
|
1375
|
+
|
|
1376
|
+
### Mix 3: Polars + Filter Pushdown (For Selective Queries)
|
|
1377
|
+
|
|
1378
|
+
**Optimize when queries filter most rows:**
|
|
1379
|
+
|
|
1380
|
+
```python
|
|
1381
|
+
engine = Engine(use_polars=True)
|
|
1382
|
+
|
|
1383
|
+
def optimized_source(dynamic_where=None, dynamic_columns=None):
|
|
1384
|
+
# Apply WHERE clause at source level
|
|
1385
|
+
query = "SELECT * FROM products"
|
|
1386
|
+
if dynamic_where:
|
|
1387
|
+
query += f" WHERE {dynamic_where}"
|
|
1388
|
+
|
|
1389
|
+
for row in execute_query(query):
|
|
1390
|
+
yield row
|
|
1391
|
+
|
|
1392
|
+
engine.register("products", optimized_source)
|
|
1393
|
+
```
|
|
1394
|
+
|
|
1395
|
+
**What You Get:**
|
|
1396
|
+
|
|
1397
|
+
- 🎯 Early filtering (reduces data volume)
|
|
1398
|
+
- ⚡ Faster execution (less data to process)
|
|
1399
|
+
- 📉 Lower memory usage
|
|
1400
|
+
|
|
1401
|
+
---
|
|
1402
|
+
|
|
1403
|
+
### Mix 4: All Optimizations Combined
|
|
1404
|
+
|
|
1405
|
+
**The Ultimate Configuration** for maximum performance:
|
|
1406
|
+
|
|
1407
|
+
```python
|
|
1408
|
+
engine = Engine(use_polars=True)
|
|
1409
|
+
|
|
1410
|
+
def ultimate_source(dynamic_where=None, dynamic_columns=None):
|
|
1411
|
+
"""
|
|
1412
|
+
Source with all optimizations:
|
|
1413
|
+
- Filter pushdown (dynamic_where)
|
|
1414
|
+
- Column pruning (dynamic_columns)
|
|
1415
|
+
- Data normalization (for Polars)
|
|
1416
|
+
"""
|
|
1417
|
+
# Build optimized query
|
|
1418
|
+
query = build_query(dynamic_where, dynamic_columns)
|
|
1419
|
+
|
|
1420
|
+
for row in execute_query(query):
|
|
1421
|
+
# Normalize types for Polars stability
|
|
1422
|
+
yield normalize_types(row)
|
|
1423
|
+
|
|
1424
|
+
engine.register("products", ultimate_source, filename="products.jsonl")
|
|
1425
|
+
```
|
|
1426
|
+
|
|
1427
|
+
**What You Get:**
|
|
1428
|
+
|
|
1429
|
+
- ⚡ Polars (speed)
|
|
1430
|
+
- 💾 MMAP (memory)
|
|
1431
|
+
- 📉 Column Pruning (I/O)
|
|
1432
|
+
- 🎯 Filter Pushdown (early filtering)
|
|
1433
|
+
|
|
1434
|
+
**Best for:** Very large datasets (> 1M rows)
|
|
1435
|
+
|
|
1436
|
+
---
|
|
1437
|
+
|
|
1438
|
+
## 📊 Performance Guide
|
|
1439
|
+
|
|
1440
|
+
### By Dataset Size
|
|
1441
|
+
|
|
1442
|
+
| Size | Configuration | Why |
|
|
1443
|
+
| ----------------- | ------------------------------ | ---------------------------------- |
|
|
1444
|
+
| **< 10K rows** | `use_polars=False` (default) | Fastest, most stable |
|
|
1445
|
+
| **10K-100K rows** | `use_polars=False` (default) | Still fastest, handles mixed types |
|
|
1446
|
+
| **100K-1M rows** | `use_polars=True` + `filename` | Polars + MMAP for best balance |
|
|
1447
|
+
| **> 1M rows** | All optimizations | Maximum performance |
|
|
1448
|
+
|
|
1449
|
+
### By Priority
|
|
1450
|
+
|
|
1451
|
+
**Priority: Stability** → Use default (`use_polars=False`)
|
|
1452
|
+
|
|
1453
|
+
```python
|
|
1454
|
+
engine = Engine() # Most stable
|
|
1455
|
+
```
|
|
1456
|
+
|
|
1457
|
+
**Priority: Speed** → Use Polars
|
|
1458
|
+
|
|
1459
|
+
```python
|
|
1460
|
+
engine = Engine(use_polars=True) # Fastest for large datasets
|
|
1461
|
+
```
|
|
1462
|
+
|
|
1463
|
+
**Priority: Memory** → Use MMAP
|
|
1464
|
+
|
|
1465
|
+
```python
|
|
1466
|
+
engine = Engine()
|
|
1467
|
+
engine.register("table", source, filename="data.jsonl") # Lowest memory
|
|
1468
|
+
```
|
|
1469
|
+
|
|
1470
|
+
**Priority: Both** → Use Polars + MMAP
|
|
1471
|
+
|
|
1472
|
+
```python
|
|
1473
|
+
engine = Engine(use_polars=True)
|
|
1474
|
+
engine.register("table", source, filename="data.jsonl") # Fast + Low memory
|
|
1475
|
+
```
|
|
1476
|
+
|
|
1477
|
+
---
|
|
1478
|
+
|
|
1479
|
+
## 🎓 Learning Path
|
|
1480
|
+
|
|
1481
|
+
### Level 1: Beginner (Start Here)
|
|
1482
|
+
|
|
1483
|
+
```python
|
|
1484
|
+
# Most stable configuration
|
|
1485
|
+
engine = Engine() # Default: use_polars=False
|
|
1486
|
+
engine.register("table1", source1)
|
|
1487
|
+
engine.register("table2", source2)
|
|
1488
|
+
```
|
|
1489
|
+
|
|
1490
|
+
**Learn:**
|
|
1491
|
+
|
|
1492
|
+
- Basic source registration
|
|
1493
|
+
- Simple SQL queries
|
|
1494
|
+
- How joins work
|
|
1495
|
+
|
|
1496
|
+
---
|
|
1497
|
+
|
|
1498
|
+
### Level 2: Intermediate
|
|
1499
|
+
|
|
1500
|
+
```python
|
|
1501
|
+
# Add debug mode to see what's happening
|
|
1502
|
+
engine = Engine(debug=True)
|
|
1503
|
+
|
|
1504
|
+
# Experiment with Polars for large datasets
|
|
1505
|
+
engine = Engine(use_polars=True)
|
|
1506
|
+
```
|
|
1507
|
+
|
|
1508
|
+
**Learn:**
|
|
1509
|
+
|
|
1510
|
+
- Debug output
|
|
1511
|
+
- When to use Polars
|
|
1512
|
+
- Data normalization
|
|
1513
|
+
|
|
1514
|
+
---
|
|
1515
|
+
|
|
1516
|
+
### Level 3: Advanced
|
|
1517
|
+
|
|
1518
|
+
```python
|
|
1519
|
+
# Use MMAP for large files
|
|
1520
|
+
engine = Engine(use_polars=True)
|
|
1521
|
+
engine.register("table", source, filename="data.jsonl")
|
|
1522
|
+
|
|
1523
|
+
# Use Merge Join for sorted data
|
|
1524
|
+
engine.register("table", source, ordered_by="key")
|
|
1525
|
+
```
|
|
1526
|
+
|
|
1527
|
+
**Learn:**
|
|
1528
|
+
|
|
1529
|
+
- MMAP for memory efficiency
|
|
1530
|
+
- Merge Join for sorted data
|
|
1531
|
+
- Protocol optimizations
|
|
1532
|
+
|
|
1533
|
+
---
|
|
1534
|
+
|
|
1535
|
+
### Level 4: Expert
|
|
1536
|
+
|
|
1537
|
+
```python
|
|
1538
|
+
# All optimizations combined
|
|
1539
|
+
engine = Engine(use_polars=True)
|
|
1540
|
+
|
|
1541
|
+
def optimized_source(dynamic_where=None, dynamic_columns=None):
|
|
1542
|
+
# Filter pushdown + Column pruning
|
|
1543
|
+
pass
|
|
1544
|
+
|
|
1545
|
+
engine.register("table", optimized_source, filename="data.jsonl")
|
|
1546
|
+
```
|
|
1547
|
+
|
|
1548
|
+
**Learn:**
|
|
1549
|
+
|
|
1550
|
+
- Protocol-based optimizations
|
|
1551
|
+
- Combining all options
|
|
1552
|
+
- Maximum performance tuning
|
|
1553
|
+
|
|
1554
|
+
---
|
|
1555
|
+
|
|
1556
|
+
## ⚠️ Common Pitfalls
|
|
1557
|
+
|
|
1558
|
+
### Pitfall 1: Using Polars Without Normalization
|
|
1559
|
+
|
|
1560
|
+
**Problem:**
|
|
1561
|
+
|
|
1562
|
+
```python
|
|
1563
|
+
engine = Engine(use_polars=True)
|
|
1564
|
+
# Mixed types cause schema inference errors
|
|
1565
|
+
```
|
|
1566
|
+
|
|
1567
|
+
**Solution:**
|
|
1568
|
+
|
|
1569
|
+
```python
|
|
1570
|
+
def normalized_source():
|
|
1571
|
+
for row in raw_source():
|
|
1572
|
+
yield {
|
|
1573
|
+
"id": int(row.get("id", 0)),
|
|
1574
|
+
"price": float(row.get("price", 0.0)),
|
|
1575
|
+
}
|
|
1576
|
+
```
|
|
1577
|
+
|
|
1578
|
+
---
|
|
1579
|
+
|
|
1580
|
+
### Pitfall 2: Using MMAP Without Polars (Very Slow)
|
|
1581
|
+
|
|
1582
|
+
**Problem:**
|
|
1583
|
+
|
|
1584
|
+
```python
|
|
1585
|
+
engine = Engine(use_polars=False)
|
|
1586
|
+
engine.register("table", source, filename="data.jsonl") # Very slow!
|
|
1587
|
+
```
|
|
1588
|
+
|
|
1589
|
+
**Solution:**
|
|
1590
|
+
|
|
1591
|
+
```python
|
|
1592
|
+
engine = Engine(use_polars=True) # Polars speeds up MMAP index building
|
|
1593
|
+
engine.register("table", source, filename="data.jsonl")
|
|
1594
|
+
```
|
|
1595
|
+
|
|
1596
|
+
---
|
|
1597
|
+
|
|
1598
|
+
### Pitfall 3: Using MMAP for Small Files
|
|
1599
|
+
|
|
1600
|
+
**Problem:**
|
|
1601
|
+
|
|
1602
|
+
```python
|
|
1603
|
+
# MMAP overhead > benefit for small files
|
|
1604
|
+
engine.register("table", source, filename="small.jsonl") # Slower!
|
|
1605
|
+
```
|
|
1606
|
+
|
|
1607
|
+
**Solution:**
|
|
1608
|
+
|
|
1609
|
+
```python
|
|
1610
|
+
# No filename for small files
|
|
1611
|
+
engine.register("table", source) # Faster for < 100MB
|
|
1612
|
+
```
|
|
1613
|
+
|
|
1614
|
+
---
|
|
1615
|
+
|
|
1616
|
+
## 🎯 Quick Decision Guide
|
|
1617
|
+
|
|
1618
|
+
**Just starting?** → Use default (`Engine()`)
|
|
1619
|
+
|
|
1620
|
+
**Have large datasets?** → Use `use_polars=True`
|
|
1621
|
+
|
|
1622
|
+
**Memory constrained?** → Use `filename` parameter (MMAP)
|
|
1623
|
+
|
|
1624
|
+
**Data is sorted?** → Use `ordered_by` parameter (Merge Join)
|
|
1625
|
+
|
|
1626
|
+
**Want maximum performance?** → Combine all: `use_polars=True` + `filename` + protocols
|
|
1627
|
+
|
|
1628
|
+
---
|
|
1629
|
+
|
|
1630
|
+
## Real-World Example: Complete Workflow
|
|
1631
|
+
|
|
1632
|
+
### Step 1: Start Simple (Most Secure)
|
|
1633
|
+
|
|
1634
|
+
```python
|
|
1635
|
+
from streaming_sql_engine import Engine
|
|
1636
|
+
|
|
1637
|
+
# Start with default (most stable)
|
|
1638
|
+
engine = Engine()
|
|
1639
|
+
|
|
1640
|
+
def postgres_users():
|
|
1641
|
+
# Your PostgreSQL code
|
|
1642
|
+
pass
|
|
1643
|
+
|
|
1644
|
+
def mysql_orders():
|
|
1645
|
+
# Your MySQL code
|
|
1646
|
+
pass
|
|
1647
|
+
|
|
1648
|
+
engine.register("users", postgres_users)
|
|
1649
|
+
engine.register("orders", mysql_orders)
|
|
1650
|
+
|
|
1651
|
+
results = engine.query("SELECT * FROM users JOIN orders ON users.id = orders.user_id")
|
|
1652
|
+
```
|
|
1653
|
+
|
|
1654
|
+
### Step 2: Add Debug Mode (Understand What's Happening)
|
|
1655
|
+
|
|
1656
|
+
```python
|
|
1657
|
+
engine = Engine(debug=True) # See execution details
|
|
1658
|
+
```
|
|
1659
|
+
|
|
1660
|
+
### Step 3: Optimize for Your Use Case
|
|
1661
|
+
|
|
1662
|
+
**If you have large datasets:**
|
|
1663
|
+
|
|
1664
|
+
```python
|
|
1665
|
+
engine = Engine(use_polars=True) # Enable Polars
|
|
1666
|
+
```
|
|
1667
|
+
|
|
1668
|
+
**If you have large files:**
|
|
1669
|
+
|
|
1670
|
+
```python
|
|
1671
|
+
engine.register("table", source, filename="data.jsonl") # Enable MMAP
|
|
1672
|
+
```
|
|
1673
|
+
|
|
1674
|
+
**If you have sorted data:**
|
|
1675
|
+
|
|
1676
|
+
```python
|
|
1677
|
+
engine.register("table", source, ordered_by="key") # Enable Merge Join
|
|
1678
|
+
```
|
|
1679
|
+
|
|
1680
|
+
### Step 4: Combine Optimizations
|
|
1681
|
+
|
|
1682
|
+
```python
|
|
1683
|
+
# Ultimate configuration for large files
|
|
1684
|
+
engine = Engine(use_polars=True)
|
|
1685
|
+
|
|
1686
|
+
def optimized_source(dynamic_where=None, dynamic_columns=None):
|
|
1687
|
+
# Supports all optimizations
|
|
1688
|
+
pass
|
|
1689
|
+
|
|
1690
|
+
engine.register("table", optimized_source, filename="data.jsonl")
|
|
1691
|
+
```
|
|
1692
|
+
|
|
1693
|
+
---
|
|
1694
|
+
|
|
1695
|
+
## Summary
|
|
1696
|
+
|
|
1697
|
+
### Start Here (Most Secure)
|
|
1698
|
+
|
|
1699
|
+
```python
|
|
1700
|
+
engine = Engine() # Default: use_polars=False
|
|
1701
|
+
engine.register("table1", source1)
|
|
1702
|
+
engine.register("table2", source2)
|
|
1703
|
+
```
|
|
1704
|
+
|
|
1705
|
+
**Why:** Most stable, handles all edge cases, works with any data types
|
|
1706
|
+
|
|
1707
|
+
---
|
|
1708
|
+
|
|
1709
|
+
### Then Experiment
|
|
1710
|
+
|
|
1711
|
+
1. **Add debug mode**: `Engine(debug=True)` - See what's happening
|
|
1712
|
+
2. **Try Polars**: `Engine(use_polars=True)` - For large datasets
|
|
1713
|
+
3. **Try MMAP**: `filename="data.jsonl"` - For large files
|
|
1714
|
+
4. **Try Merge Join**: `ordered_by="key"` - For sorted data
|
|
1715
|
+
|
|
1716
|
+
---
|
|
1717
|
+
|
|
1718
|
+
### Advanced: Mix Options
|
|
1719
|
+
|
|
1720
|
+
**Best Mix for Large Files:**
|
|
1721
|
+
|
|
1722
|
+
```python
|
|
1723
|
+
engine = Engine(use_polars=True) # Speed
|
|
1724
|
+
engine.register("table", source, filename="data.jsonl") # Memory
|
|
1725
|
+
```
|
|
1726
|
+
|
|
1727
|
+
**Best Mix for Maximum Performance:**
|
|
1728
|
+
|
|
1729
|
+
```python
|
|
1730
|
+
engine = Engine(use_polars=True)
|
|
1731
|
+
|
|
1732
|
+
def source(dynamic_where=None, dynamic_columns=None):
|
|
1733
|
+
# All optimizations
|
|
1734
|
+
pass
|
|
1735
|
+
|
|
1736
|
+
engine.register("table", source, filename="data.jsonl")
|
|
1737
|
+
```
|
|
1738
|
+
|
|
1739
|
+
---
|
|
1740
|
+
|
|
1741
|
+
## Key Takeaways
|
|
1742
|
+
|
|
1743
|
+
1. **Start Simple**: Use default configuration (`Engine()`) - it's the most stable
|
|
1744
|
+
2. **Experiment Gradually**: Add options one at a time to understand their impact
|
|
1745
|
+
3. **Mix Wisely**: Combine options (Polars + MMAP) for best results
|
|
1746
|
+
4. **Know When to Use Each**: Small files → default, Large files → Polars + MMAP
|
|
1747
|
+
|
|
1748
|
+
---
|
|
1749
|
+
|
|
1750
|
+
**Remember**: Start with the default configuration, then experiment with options as you understand your data and performance needs better.
|
|
1751
|
+
|
|
1380
1752
|
## Getting Started
|
|
1381
1753
|
|
|
1382
1754
|
Installation:
|