streaming-sql-engine 0.1.28__tar.gz → 0.1.29__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 (132) hide show
  1. {streaming_sql_engine-0.1.28/streaming_sql_engine.egg-info → streaming_sql_engine-0.1.29}/PKG-INFO +1 -1
  2. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/__init__.py +1 -1
  3. streaming_sql_engine-0.1.29/planner.py +174 -0
  4. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/pyproject.toml +1 -1
  5. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/setup.py +1 -1
  6. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29/streaming_sql_engine.egg-info}/PKG-INFO +1 -1
  7. streaming_sql_engine-0.1.28/planner.py +0 -460
  8. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/DEPLOYMENT.md +0 -0
  9. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/MANIFEST.in +0 -0
  10. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/QUICK_DEPLOY.md +0 -0
  11. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/QUICK_FIX.md +0 -0
  12. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/README.md +0 -0
  13. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/benchmark_all_configurations.py +0 -0
  14. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/benchmark_all_configurations_1k.py +0 -0
  15. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/debug_where.py +0 -0
  16. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/documentation/APACHE_DRILL_COMPARISON.md +0 -0
  17. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/documentation/API_JOIN_OPERATIONS.md +0 -0
  18. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/documentation/ARCHITECTURAL_RECOMMENDATION.md +0 -0
  19. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/documentation/ARCHITECTURE_DECISION.md +0 -0
  20. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/documentation/ARCHITECTURE_SYNCED.md +0 -0
  21. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/documentation/BEST_USE_CASES.md +0 -0
  22. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/documentation/BETTER_FILTER_APPROACH.md +0 -0
  23. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/documentation/CARTESIAN_PRODUCTS_CAUSES.md +0 -0
  24. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/documentation/CHANGES_SUMMARY.md +0 -0
  25. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/documentation/COMPREHENSIVE_ARTICLE.md +0 -0
  26. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/documentation/COMPREHENSIVE_DOCUMENTATION.md +0 -0
  27. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/documentation/COMPREHENSIVE_GUIDE.md +0 -0
  28. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/documentation/DEVELOPER_GUIDE.md +0 -0
  29. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/documentation/DUCKDB_COMPARISON.md +0 -0
  30. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/documentation/ENGINE_OPTIONS_REFERENCE.md +0 -0
  31. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/documentation/FINAL_IMPLEMENTATION_RECOMMENDATIONS.md +0 -0
  32. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/documentation/FINAL_RECOMMENDATION.md +0 -0
  33. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/documentation/FIRST_MATCH_ONLY_WARNINGS.md +0 -0
  34. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/documentation/HANDLING_DUPLICATES_WITHOUT_CARTESIAN.md +0 -0
  35. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/documentation/HOW_PROTOCOL_DETECTION_WORKS.md +0 -0
  36. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/documentation/HOW_TO_ADD_TO_PROJECT.md +0 -0
  37. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/documentation/INSTALLATION.md +0 -0
  38. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/documentation/JSONL_EXECUTOR_ANALYSIS.md +0 -0
  39. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/documentation/JSONL_EXECUTOR_REMOVED.md +0 -0
  40. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/documentation/LIBRARY_DOCUMENTATION.md +0 -0
  41. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/documentation/MEDIUM_ARTICLE.md +0 -0
  42. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/documentation/MEDIUM_ARTICLE_SIMPLIFIED.md +0 -0
  43. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/documentation/MIGRATION_GUIDE.md +0 -0
  44. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/documentation/MMAP_WITHOUT_POLARS_SLOW.md +0 -0
  45. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/documentation/MOST_STABLE_AND_PERFORMANT.md +0 -0
  46. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/documentation/MULTI_SOURCE_EXAMPLE_README.md +0 -0
  47. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/documentation/MYSQL_USAGE.md +0 -0
  48. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/documentation/OPTIMIZATIONS_BREAKDOWN.md +0 -0
  49. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/documentation/OPTIMIZATIONS_NOT_DATABASE_ONLY.md +0 -0
  50. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/documentation/OPTIMIZATIONS_VS_POLARS.md +0 -0
  51. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/documentation/OPTIMIZER_AUTOMATED.md +0 -0
  52. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/documentation/PERFORMANCE.md +0 -0
  53. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/documentation/PERFORMANCE_COMPARISON.md +0 -0
  54. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/documentation/POLARS_AND_MMAP_GUIDE.md +0 -0
  55. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/documentation/POLARS_VS_POLARS_MMAP.md +0 -0
  56. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/documentation/PROTOCOL_AND_OPTIMIZATIONS_EXPLAINED.md +0 -0
  57. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/documentation/PROTOCOL_EXPLANATION.md +0 -0
  58. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/documentation/PUBLISHING.md +0 -0
  59. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/documentation/PYPI_CHECKLIST.md +0 -0
  60. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/documentation/QUICK_PUBLISH.md +0 -0
  61. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/documentation/QUICK_START.md +0 -0
  62. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/documentation/SIMPLE_PROTOCOL_GUIDE.md +0 -0
  63. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/documentation/SIMPLE_SOURCE_FUNCTIONS.md +0 -0
  64. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/documentation/SOURCE_COMBINATIONS_GUIDE.md +0 -0
  65. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/documentation/TECHNICAL_DOCUMENTATION.md +0 -0
  66. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/documentation/USER_GUIDE.md +0 -0
  67. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/documentation/WHAT_IS_WHERE_TO_PARAMS.md +0 -0
  68. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/documentation/WHEN_POLARS_IS_USED.md +0 -0
  69. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/documentation/WHERE_POLARS_IS_USED.md +0 -0
  70. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/documentation/WHY_API_CONVERTER_NEEDED.md +0 -0
  71. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/documentation/WHY_MANUAL_PARSING.md +0 -0
  72. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/documentation/WHY_ORIGINAL_IS_FASTER.md +0 -0
  73. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/documentation/WHY_PROTOCOL_IN_SOURCES.md +0 -0
  74. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/documentation/XML_MONGO_USE_CASE.md +0 -0
  75. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/engine.py +0 -0
  76. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/evaluator.py +0 -0
  77. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/executor.py +0 -0
  78. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/mmap_index.py +0 -0
  79. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/operators.py +0 -0
  80. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/operators_mmap.py +0 -0
  81. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/operators_polars.py +0 -0
  82. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/optimized_export.py +0 -0
  83. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/optimizer.py +0 -0
  84. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/parser.py +0 -0
  85. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/polars_expression_translator.py +0 -0
  86. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/polars_operators.py +0 -0
  87. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/pre_release_test.py +0 -0
  88. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/protocol_helpers.py +0 -0
  89. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/quick_test.py +0 -0
  90. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/requirements.txt +0 -0
  91. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/setup.cfg +0 -0
  92. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/streaming_sql_engine.egg-info/SOURCES.txt +0 -0
  93. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/streaming_sql_engine.egg-info/dependency_links.txt +0 -0
  94. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/streaming_sql_engine.egg-info/requires.txt +0 -0
  95. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/streaming_sql_engine.egg-info/top_level.txt +0 -0
  96. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/test_evaluator_direct.py +0 -0
  97. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/test_lookup_join_where.py +0 -0
  98. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/test_polars_where.py +0 -0
  99. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/test_where_debug_simple.py +0 -0
  100. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/test_where_fix.py +0 -0
  101. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/test_where_string.py +0 -0
  102. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/tests/test_engine_options.py +0 -0
  103. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/tests/test_full_pipeline.py +0 -0
  104. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/tests/test_merge_buffer.py +0 -0
  105. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/tests/test_merge_join_debug.py +0 -0
  106. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/tests/test_merge_join_detailed_debug.py +0 -0
  107. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/tests/test_merge_join_direct.py +0 -0
  108. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/tests/test_merge_join_issue.py +0 -0
  109. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/tests/test_merge_join_simple.py +0 -0
  110. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/tests/test_merge_join_with_user_data.py +0 -0
  111. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/tests/test_mmap_debug.py +0 -0
  112. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/tests/test_mmap_direct.py +0 -0
  113. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/tests/test_mmap_get_matches.py +0 -0
  114. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/tests/test_mmap_get_matches_direct.py +0 -0
  115. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/tests/test_mmap_get_rows_variants.py +0 -0
  116. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/tests/test_mmap_index.py +0 -0
  117. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/tests/test_mmap_index_lookup.py +0 -0
  118. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/tests/test_mmap_join_benchmark_match.py +0 -0
  119. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/tests/test_mmap_join_debug.py +0 -0
  120. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/tests/test_mmap_join_simple.py +0 -0
  121. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/tests/test_mmap_join_simple_fixed.py +0 -0
  122. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/tests/test_mmap_join_trace.py +0 -0
  123. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/tests/test_mmap_key_extraction.py +0 -0
  124. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/tests/test_mmap_required_columns.py +0 -0
  125. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/tests/test_mmap_simple_query.py +0 -0
  126. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/tests/test_polars_fixes.py +0 -0
  127. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/tests/test_polars_schema_fix.py +0 -0
  128. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/tests/test_readme_cases.py +0 -0
  129. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/tests/test_required_columns_analysis.py +0 -0
  130. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/tests/test_where_clause_1k.py +0 -0
  131. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/tests/test_where_debug.py +0 -0
  132. {streaming_sql_engine-0.1.28 → streaming_sql_engine-0.1.29}/tests/test_where_pushdown.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: streaming-sql-engine
3
- Version: 0.1.28
3
+ Version: 0.1.29
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.28"
30
+ __version__ = "0.1.29"
31
31
 
@@ -0,0 +1,174 @@
1
+ """
2
+ Logical plan construction from parsed SQL AST.
3
+ """
4
+
5
+ from dataclasses import dataclass, field
6
+ from sqlglot import expressions as exp
7
+ from .parser import ParseError
8
+
9
+
10
+ @dataclass
11
+ class JoinInfo:
12
+ """Information about a single join."""
13
+ table: str
14
+ alias: object
15
+ join_type: str # "INNER" or "LEFT"
16
+ left_key: str # e.g., "sp.fk_abs"
17
+ right_key: str # e.g., "spa.id"
18
+
19
+
20
+ @dataclass
21
+ class LogicalPlan:
22
+ """Logical execution plan for a query."""
23
+ root_table: str
24
+ root_alias: object
25
+ joins: list
26
+ where_expr: object
27
+ projections: list # SELECT expressions with aliases
28
+ required_columns: dict = field(default_factory=dict) # Table name -> set of column names (added by optimizer)
29
+ pushable_where_expr: object = None # WHERE conditions that can be pushed to root table
30
+
31
+
32
+ def build_logical_plan(ast, registered_tables):
33
+ """
34
+ Convert SQL AST into a logical plan.
35
+
36
+ Args:
37
+ ast: Parsed SELECT expression
38
+ registered_tables: Set of registered table names
39
+
40
+ Returns:
41
+ LogicalPlan object
42
+
43
+ Raises:
44
+ ParseError: If plan construction fails
45
+ """
46
+ # Extract FROM clause
47
+ # Note: sqlglot uses 'from_' (with underscore) to avoid Python keyword conflict
48
+ from_expr = ast.args.get("from_") or ast.args.get("from")
49
+ if not from_expr:
50
+ raise ParseError("Query must have a FROM clause")
51
+
52
+ root_table, root_alias = _extract_table_and_alias(from_expr.this)
53
+
54
+ if root_table not in registered_tables:
55
+ raise ParseError(f"Table '{root_table}' is not registered")
56
+
57
+ # Extract JOINs
58
+ joins = []
59
+ for join_expr in ast.args.get("joins", []):
60
+ join_info = _extract_join(join_expr, registered_tables)
61
+ joins.append(join_info)
62
+
63
+ # Extract WHERE clause
64
+ where_expr = ast.args.get("where")
65
+ # Unwrap Where node if present (sqlglot wraps WHERE expressions in a Where node)
66
+ if where_expr is not None:
67
+ if isinstance(where_expr, exp.Where):
68
+ where_expr = where_expr.this
69
+ # where_expr is now the actual expression (And, Or, EQ, etc.)
70
+
71
+ # Extract SELECT projections
72
+ projections = list(ast.expressions)
73
+
74
+ # Build initial plan
75
+ plan = LogicalPlan(
76
+ root_table=root_table,
77
+ root_alias=root_alias,
78
+ joins=joins,
79
+ where_expr=where_expr,
80
+ projections=projections
81
+ )
82
+
83
+ # Apply optimizations (column pruning and filter pushdown)
84
+ from .optimizer import analyze_required_columns, analyze_filter_pushdown
85
+
86
+ # Analyze required columns
87
+ plan.required_columns = analyze_required_columns(plan)
88
+
89
+ # Analyze filter pushdown
90
+ pushable_where, remaining_where = analyze_filter_pushdown(plan)
91
+ plan.pushable_where_expr = pushable_where
92
+ plan.where_expr = remaining_where # Update to only non-pushable conditions
93
+
94
+ return plan
95
+
96
+
97
+ def _extract_table_and_alias(expr):
98
+ """Extract table name and alias from a table expression."""
99
+ if isinstance(expr, exp.Table):
100
+ table_name = expr.name
101
+ alias = expr.alias
102
+ return table_name, alias
103
+ elif isinstance(expr, exp.Alias):
104
+ # Handle table with alias
105
+ if isinstance(expr.this, exp.Table):
106
+ return expr.this.name, expr.alias
107
+ else:
108
+ raise ParseError(f"Unsupported table expression: {type(expr.this)}")
109
+ else:
110
+ raise ParseError(f"Unsupported FROM expression: {type(expr)}")
111
+
112
+
113
+ def _extract_join(join_expr, registered_tables):
114
+ """Extract join information from a JOIN expression."""
115
+ # Determine join type
116
+ # sqlglot stores LEFT/RIGHT in 'side' attribute, not 'kind'
117
+ join_type = "INNER"
118
+ side = join_expr.args.get("side") or getattr(join_expr, "side", None)
119
+ if side and side.upper() == "LEFT":
120
+ join_type = "LEFT"
121
+ elif side and side.upper() not in ("INNER", "LEFT", "RIGHT"):
122
+ raise ParseError(f"Unsupported join type: {side}")
123
+
124
+ # Extract table and alias
125
+ table, alias = _extract_table_and_alias(join_expr.this)
126
+
127
+ if table not in registered_tables:
128
+ raise ParseError(f"Table '{table}' in JOIN is not registered")
129
+
130
+ # Extract join condition
131
+ on_expr = join_expr.args.get("on")
132
+ if not on_expr:
133
+ raise ParseError("JOIN must have an ON condition")
134
+
135
+ left_key, right_key = _extract_join_keys(on_expr)
136
+
137
+ return JoinInfo(
138
+ table=table,
139
+ alias=alias,
140
+ join_type=join_type,
141
+ left_key=left_key,
142
+ right_key=right_key
143
+ )
144
+
145
+
146
+ def _extract_join_keys(on_expr):
147
+ """
148
+ Extract join keys from ON condition.
149
+ Currently only supports equality joins: alias1.col1 = alias2.col2
150
+ """
151
+ if not isinstance(on_expr, exp.EQ):
152
+ raise ParseError("Only equality joins are supported in ON clause")
153
+
154
+ left = on_expr.this
155
+ right = on_expr.expression
156
+
157
+ left_key = _column_to_string(left)
158
+ right_key = _column_to_string(right)
159
+
160
+ return left_key, right_key
161
+
162
+
163
+ def _column_to_string(expr):
164
+ """Convert a column expression to string format 'alias.column'."""
165
+ if isinstance(expr, exp.Column):
166
+ table = expr.table
167
+ column = expr.name
168
+ if table:
169
+ return f"{table}.{column}"
170
+ else:
171
+ raise ParseError(f"Column reference must include table alias: {expr}")
172
+ else:
173
+ raise ParseError(f"Join key must be a column reference, got {type(expr)}")
174
+
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "streaming-sql-engine"
7
- version = "0.1.28"
7
+ version = "0.1.29"
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.28",
15
+ version="0.1.29",
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.28
3
+ Version: 0.1.29
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
@@ -1,460 +0,0 @@
1
- """
2
- Execution engine - builds iterator pipeline from logical plan.
3
- """
4
-
5
- from .planner import LogicalPlan, JoinInfo
6
- from .operators import (
7
- ScanIterator,
8
- FilterIterator,
9
- ProjectIterator,
10
- LookupJoinIterator,
11
- MergeJoinIterator
12
- )
13
-
14
- # Try importing Polars operators (optional)
15
- try:
16
- from .operators_polars import (
17
- PolarsLookupJoinIterator,
18
- PolarsBatchFilterIterator,
19
- PolarsBatchProjectIterator,
20
- should_use_polars
21
- )
22
- POLARS_AVAILABLE = True
23
- except ImportError:
24
- POLARS_AVAILABLE = False
25
- PolarsBatchFilterIterator = None
26
- PolarsBatchProjectIterator = None
27
-
28
- # Try importing mmap operators (optional)
29
- try:
30
- from .operators_mmap import MmapLookupJoinIterator
31
- MMAP_AVAILABLE = True
32
- except ImportError:
33
- MMAP_AVAILABLE = False
34
- MmapLookupJoinIterator = None
35
-
36
-
37
- def execute_plan(
38
- plan,
39
- sources,
40
- source_metadata,
41
- debug=False,
42
- use_polars=False,
43
- first_match_only=False
44
- ):
45
- """
46
- Execute a logical plan and return a generator of result row dictionaries.
47
-
48
- Args:
49
- plan: Logical execution plan (with optimizations: required_columns, pushable_where_expr)
50
- sources: Dictionary mapping table names to source functions
51
- source_metadata: Dictionary with metadata about sources (e.g., ordered_by)
52
-
53
- Returns:
54
- Generator of result row dictionaries
55
- """
56
- # Get required columns for root table (column pruning)
57
- root_required_columns = plan.required_columns.get(plan.root_table)
58
-
59
- # Handle pushable WHERE clause (filter pushdown)
60
- # For sources that support the protocol, we can push WHERE to the source
61
- # For other sources, we'll apply it after scanning
62
- root_source_fn = sources[plan.root_table]
63
- pushable_where_sql = None
64
-
65
- if plan.pushable_where_expr:
66
- # Convert pushable WHERE expression to SQL string
67
- from .optimizer import expression_to_sql_string
68
- try:
69
- pushable_where_sql = expression_to_sql_string(plan.pushable_where_expr)
70
- if debug:
71
- print(f" [OPTIMIZATION] Pushing WHERE clause to source: {pushable_where_sql}")
72
- except Exception as e:
73
- if debug:
74
- print(f" [OPTIMIZATION] Could not push WHERE clause: {e}")
75
- pushable_where_sql = None
76
-
77
- # Check if source supports optimization via protocol
78
- # Protocol: If function accepts dynamic_where or dynamic_columns parameters, optimizations apply
79
- import inspect
80
-
81
- def source_supports_optimizations(source_fn):
82
- """Check if source implements optimization protocol."""
83
- try:
84
- sig = inspect.signature(source_fn)
85
- params = list(sig.parameters.keys())
86
- return 'dynamic_where' in params or 'dynamic_columns' in params
87
- except (ValueError, TypeError):
88
- # Can't inspect signature, assume no protocol
89
- return False
90
-
91
- # Apply optimizations if protocol is supported
92
- if source_supports_optimizations(root_source_fn) and (root_required_columns or pushable_where_sql):
93
- if debug:
94
- print(f" [OPTIMIZATION] Source supports protocol - applying column pruning and filter pushdown")
95
-
96
- original_source_fn = root_source_fn
97
-
98
- def optimized_source_fn():
99
- # Call source with optimization parameters
100
- return original_source_fn(
101
- dynamic_where=pushable_where_sql,
102
- dynamic_columns=list(root_required_columns) if root_required_columns else None
103
- )
104
-
105
- root_source_fn = optimized_source_fn
106
-
107
- # Start with scan of root table
108
- if debug:
109
- if root_required_columns:
110
- print(f" [SCAN] Scanning table: {plan.root_table} (columns: {len(root_required_columns)})")
111
- else:
112
- print(f" [SCAN] Scanning table: {plan.root_table}")
113
-
114
- iterator = ScanIterator(
115
- root_source_fn,
116
- plan.root_table,
117
- plan.root_alias,
118
- required_columns=root_required_columns,
119
- debug=debug
120
- )
121
-
122
- # Apply joins in order
123
- # Track WHERE clauses that have been pushed to sources
124
- remaining_where_expr = plan.where_expr
125
- if debug and remaining_where_expr:
126
- print(f" [DEBUG] Initial remaining_where_expr: {remaining_where_expr}")
127
-
128
- for i, join_info in enumerate(plan.joins, 1):
129
- # Get required columns for joined table (needed for optimizations)
130
- join_required_columns = plan.required_columns.get(join_info.table)
131
-
132
- if debug:
133
- if join_required_columns:
134
- print(f" [JOIN {i}/{len(plan.joins)}] {join_info.join_type} JOIN {join_info.table} (columns: {len(join_required_columns)})")
135
- else:
136
- print(f" [JOIN {i}/{len(plan.joins)}] {join_info.join_type} JOIN {join_info.table}")
137
-
138
- # Check if we can push WHERE clauses to this joined table
139
- right_source_fn = sources[join_info.table]
140
- join_where_sql = None
141
- table_where_expr = None
142
-
143
- # Extract WHERE clauses that reference this joined table
144
- if remaining_where_expr:
145
- from .optimizer import extract_table_where_clauses, expression_to_sql_string, remove_expression_from_where
146
- if debug:
147
- print(f" [DEBUG] remaining_where_expr is not None: {remaining_where_expr}")
148
- try:
149
- # Try both alias and table name (WHERE clause might use either)
150
- table_alias = join_info.alias or join_info.table
151
- table_name = join_info.table
152
-
153
- if debug:
154
- print(f" [DEBUG] Checking WHERE clauses for alias '{table_alias}' or table '{table_name}'")
155
- print(f" [DEBUG] Remaining WHERE expr: {remaining_where_expr}")
156
-
157
- # Try alias first, then table name
158
- table_where_expr = extract_table_where_clauses(remaining_where_expr, table_alias)
159
- if not table_where_expr and table_alias != table_name:
160
- # If alias didn't match and it's different from table name, try table name
161
- table_where_expr = extract_table_where_clauses(remaining_where_expr, table_name)
162
-
163
- if table_where_expr:
164
- join_where_sql = expression_to_sql_string(table_where_expr)
165
- if debug:
166
- print(f" [OPTIMIZATION] Pushing WHERE clause to {join_info.table}: {join_where_sql}")
167
- # Remove pushed clauses from remaining WHERE
168
- remaining_where_expr = remove_expression_from_where(remaining_where_expr, table_where_expr)
169
- if debug:
170
- print(f" [DEBUG] Remaining WHERE after removal: {remaining_where_expr}")
171
- else:
172
- if debug:
173
- print(f" [DEBUG] No WHERE clauses found for alias '{table_alias}' or table '{table_name}'")
174
- except Exception as e:
175
- if debug:
176
- print(f" [OPTIMIZATION] Could not push WHERE to {join_info.table}: {e}")
177
- import traceback
178
- traceback.print_exc()
179
- else:
180
- # Even if not debug, log the error so we can see what's wrong
181
- import traceback
182
- print(f" [ERROR] Could not push WHERE to {join_info.table}: {e}")
183
- traceback.print_exc()
184
- join_where_sql = None
185
-
186
- # Apply protocol optimizations to joined table source if supported
187
- import inspect
188
- def source_supports_optimizations(source_fn):
189
- """Check if source implements optimization protocol."""
190
- try:
191
- sig = inspect.signature(source_fn)
192
- params = list(sig.parameters.keys())
193
- return 'dynamic_where' in params or 'dynamic_columns' in params
194
- except (ValueError, TypeError):
195
- return False
196
-
197
- optimized_right_source_fn = right_source_fn
198
- if source_supports_optimizations(right_source_fn) and (join_required_columns or join_where_sql):
199
- if debug:
200
- print(f" [OPTIMIZATION] Source {join_info.table} supports protocol - applying optimizations")
201
-
202
- original_right_source_fn = right_source_fn
203
-
204
- def optimized_right_source_fn():
205
- # Call source with optimization parameters
206
- return original_right_source_fn(
207
- dynamic_where=join_where_sql,
208
- dynamic_columns=list(join_required_columns) if join_required_columns else None
209
- )
210
-
211
- iterator = _build_join_iterator(
212
- iterator,
213
- join_info,
214
- sources,
215
- source_metadata,
216
- plan.required_columns.get(join_info.table), # Pass required columns
217
- optimized_right_source_fn=optimized_right_source_fn, # Pass optimized source
218
- debug=debug,
219
- use_polars=use_polars, # Pass Polars flag
220
- first_match_only=first_match_only # Pass first-match-only flag
221
- )
222
-
223
- # Update plan.where_expr to only include clauses not pushed to sources
224
- plan.where_expr = remaining_where_expr
225
-
226
- # Apply WHERE filter if present (non-pushable conditions)
227
- # Must be applied AFTER joins since remaining WHERE conditions may reference joined tables
228
- if plan.where_expr:
229
- if debug:
230
- print(f" [FILTER] Applying WHERE clause (non-pushable conditions)")
231
-
232
- # Use Polars batch filtering if available and beneficial
233
- if (use_polars and POLARS_AVAILABLE and PolarsBatchFilterIterator is not None):
234
- if debug:
235
- print(f" [POLARS] Using Polars vectorized filtering (SIMD-accelerated)")
236
- try:
237
- iterator = PolarsBatchFilterIterator(iterator, plan.where_expr, batch_size=10000, debug=debug)
238
- except Exception as e:
239
- if debug:
240
- print(f" [POLARS] ✗ Filtering failed: {e}, using Python")
241
- iterator = FilterIterator(iterator, plan.where_expr, debug=debug)
242
- else:
243
- iterator = FilterIterator(iterator, plan.where_expr, debug=debug)
244
-
245
- # Apply projection
246
- if debug:
247
- print(f" [PROJECT] Applying SELECT projection")
248
- print(f"\nPipeline ready. Starting row processing...\n")
249
- print("-" * 60)
250
-
251
- # Use Polars batch projection if available and beneficial
252
- if (use_polars and POLARS_AVAILABLE and PolarsBatchProjectIterator is not None):
253
- if debug:
254
- print(f" [POLARS] Using Polars vectorized projection (SIMD-accelerated)")
255
- try:
256
- iterator = PolarsBatchProjectIterator(iterator, plan.projections, batch_size=10000, debug=debug)
257
- except Exception as e:
258
- if debug:
259
- print(f" [POLARS] ✗ Projection failed: {e}, using Python")
260
- iterator = ProjectIterator(iterator, plan.projections, debug=debug)
261
- else:
262
- iterator = ProjectIterator(iterator, plan.projections, debug=debug)
263
-
264
- return iterator
265
-
266
-
267
- def _build_join_iterator(
268
- left_iterator,
269
- join_info,
270
- sources,
271
- source_metadata,
272
- required_columns=None,
273
- optimized_right_source_fn=None,
274
- debug=False,
275
- use_polars=False,
276
- first_match_only=False
277
- ):
278
- """
279
- Build appropriate join iterator based on source capabilities.
280
-
281
- Args:
282
- required_columns: Set of column names needed from right table (for column pruning)
283
- optimized_right_source_fn: Optional optimized source function (with protocol applied)
284
- """
285
- # Use optimized source if provided, otherwise use original
286
- if optimized_right_source_fn is not None:
287
- right_source = optimized_right_source_fn
288
- else:
289
- right_source = sources[join_info.table]
290
- right_metadata = source_metadata.get(join_info.table, {})
291
-
292
- # Apply column pruning to right source if needed
293
- # For database sources, this would be handled at source creation
294
- # For other sources, LookupJoinIterator will handle it via ScanIterator
295
-
296
- # Check if both sides are ordered by join keys
297
- # Extract left table name to check its metadata
298
- left_table_name = _extract_table_from_key(join_info.left_key)
299
- left_metadata = source_metadata.get(left_table_name, {}) if left_table_name else {}
300
- left_ordered_by = left_metadata.get("ordered_by")
301
- right_ordered_by = right_metadata.get("ordered_by")
302
-
303
- # Extract column names from join keys
304
- left_join_column = _extract_column_from_key(join_info.left_key)
305
- right_join_column = _extract_column_from_key(join_info.right_key)
306
-
307
- # For merge join, we need BOTH sides sorted on their respective join keys
308
- # Merge join REQUIRES sorted data - if data isn't sorted, it produces incorrect results
309
- # Only use merge join when:
310
- # 1. Explicitly told both sides are sorted (via ordered_by metadata)
311
- # 2. NOT using Polars (Polars is faster for unsorted data, merge join only for sorted)
312
- use_merge_join = (
313
- not use_polars and # Don't use merge join if Polars is explicitly requested
314
- left_ordered_by is not None and
315
- left_ordered_by == left_join_column and
316
- right_ordered_by is not None and
317
- right_ordered_by == right_join_column
318
- )
319
-
320
- if use_merge_join:
321
- if debug:
322
- iterator_type = "MERGE JOIN"
323
- print(f" Using {iterator_type} (sorted data)")
324
- print(f" Left ordered_by: {left_ordered_by}, Right ordered_by: {right_ordered_by}")
325
- print(f" Left join column: {left_join_column}, Right join column: {right_join_column}")
326
- return MergeJoinIterator(
327
- left_iterator,
328
- right_source,
329
- join_info.left_key,
330
- join_info.right_key,
331
- join_info.join_type,
332
- join_info.table,
333
- join_info.alias,
334
- debug=debug
335
- )
336
- else:
337
- # When use_polars=True is explicitly set, prioritize Polars over mmap
338
- # (unless user explicitly wants mmap by providing filename)
339
- right_metadata = source_metadata.get(join_info.table, {})
340
- right_table_filename = right_metadata.get("filename")
341
-
342
- # Only use mmap if:
343
- # 1. Filename is explicitly provided (user wants mmap)
344
- # 2. Polars is NOT explicitly requested (use_polars=False)
345
- # This ensures that when user sets use_polars=True, Polars is used instead of mmap
346
- use_mmap = (
347
- MMAP_AVAILABLE and
348
- MmapLookupJoinIterator is not None and
349
- right_table_filename and
350
- not use_polars # Don't use mmap if Polars is explicitly requested
351
- )
352
-
353
- if use_mmap:
354
- if debug:
355
- iterator_type = "MMAP LOOKUP JOIN"
356
- if required_columns:
357
- print(f" Using {iterator_type} (low memory, columns: {len(required_columns)})...")
358
- else:
359
- print(f" Using {iterator_type} (low memory, position-based index)...")
360
- try:
361
- return MmapLookupJoinIterator(
362
- left_iterator,
363
- right_source,
364
- join_info.left_key,
365
- join_info.right_key,
366
- join_info.join_type,
367
- join_info.table,
368
- join_info.alias,
369
- right_table_filename=right_table_filename,
370
- required_columns=required_columns,
371
- debug=debug
372
- )
373
- except Exception as e:
374
- if debug:
375
- print(f" ⚠️ Mmap join failed: {e}")
376
- import traceback
377
- traceback.print_exc()
378
- print(f" Falling back to Polars/Python")
379
- # Fallback to Polars or Python
380
- pass
381
-
382
- # Decide between Polars and Python implementation
383
- # When use_polars=True, Polars is prioritized (unless mmap explicitly requested via filename)
384
- if debug:
385
- if use_polars:
386
- if POLARS_AVAILABLE:
387
- print(f" [POLARS] Checking if Polars should be used...")
388
- try:
389
- should_use = should_use_polars(right_source, threshold=10000)
390
- if not should_use:
391
- print(f" [POLARS] Right side too small or estimation failed, using Python join instead")
392
- except Exception as e:
393
- print(f" [POLARS] Estimation failed: {e}, using Python join instead")
394
- else:
395
- print(f" [POLARS] Polars not available (not installed), falling back to Python")
396
-
397
- if (use_polars and POLARS_AVAILABLE and
398
- should_use_polars(right_source, threshold=10000)):
399
- if debug:
400
- iterator_type = "POLARS LOOKUP JOIN"
401
- print(f" [POLARS] ✓ Using {iterator_type} (SIMD-accelerated)")
402
- if required_columns:
403
- print(f" [POLARS] Columns: {len(required_columns)}")
404
- if first_match_only:
405
- print(f" [POLARS] ⚡ First-match-only mode enabled")
406
- try:
407
- return PolarsLookupJoinIterator(
408
- left_iterator,
409
- right_source,
410
- join_info.left_key,
411
- join_info.right_key,
412
- join_info.join_type,
413
- join_info.table,
414
- join_info.alias,
415
- batch_size=10000,
416
- required_columns=required_columns,
417
- debug=debug,
418
- first_match_only=first_match_only
419
- )
420
- except Exception as e:
421
- if debug:
422
- print(f" [POLARS] ✗ Polars join failed: {e}, falling back to Python")
423
- # Fallback to Python implementation
424
- pass
425
-
426
- if debug:
427
- iterator_type = "LOOKUP JOIN (Python)"
428
- if required_columns:
429
- print(f" Using {iterator_type} (building index, columns: {len(required_columns)})...")
430
- else:
431
- print(f" Using {iterator_type} (building index...)")
432
- if first_match_only:
433
- print(f" ⚡ First-match-only mode enabled")
434
- return LookupJoinIterator(
435
- left_iterator,
436
- right_source,
437
- join_info.left_key,
438
- join_info.right_key,
439
- join_info.join_type,
440
- join_info.table,
441
- join_info.alias,
442
- required_columns=required_columns,
443
- debug=debug,
444
- first_match_only=first_match_only
445
- )
446
-
447
-
448
- def _extract_table_from_key(key):
449
- """Extract table alias from a key like 'alias.column'."""
450
- if "." in key:
451
- return key.split(".", 1)[0]
452
- return None
453
-
454
-
455
- def _extract_column_from_key(key):
456
- """Extract column name from a key like 'alias.column'."""
457
- if "." in key:
458
- return key.split(".", 1)[1]
459
- return key
460
-