streaming-sql-engine 0.1.0__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.0/LICENSE +22 -0
- streaming_sql_engine-0.1.0/MANIFEST.in +13 -0
- streaming_sql_engine-0.1.0/PKG-INFO +289 -0
- streaming_sql_engine-0.1.0/README.md +249 -0
- streaming_sql_engine-0.1.0/documentation/APACHE_DRILL_COMPARISON.md +555 -0
- streaming_sql_engine-0.1.0/documentation/API_JOIN_OPERATIONS.md +309 -0
- streaming_sql_engine-0.1.0/documentation/BEST_USE_CASES.md +316 -0
- streaming_sql_engine-0.1.0/documentation/COMPREHENSIVE_DOCUMENTATION.md +1173 -0
- streaming_sql_engine-0.1.0/documentation/DEVELOPER_GUIDE.md +757 -0
- streaming_sql_engine-0.1.0/documentation/DUCKDB_COMPARISON.md +372 -0
- streaming_sql_engine-0.1.0/documentation/HOW_TO_ADD_TO_PROJECT.md +235 -0
- streaming_sql_engine-0.1.0/documentation/INSTALLATION.md +197 -0
- streaming_sql_engine-0.1.0/documentation/MULTI_SOURCE_EXAMPLE_README.md +147 -0
- streaming_sql_engine-0.1.0/documentation/MYSQL_USAGE.md +212 -0
- streaming_sql_engine-0.1.0/documentation/PERFORMANCE.md +199 -0
- streaming_sql_engine-0.1.0/documentation/QUICK_START.md +153 -0
- streaming_sql_engine-0.1.0/documentation/TECHNICAL_DOCUMENTATION.md +816 -0
- streaming_sql_engine-0.1.0/documentation/USER_GUIDE.md +1298 -0
- streaming_sql_engine-0.1.0/pyproject.toml +55 -0
- streaming_sql_engine-0.1.0/requirements.txt +7 -0
- streaming_sql_engine-0.1.0/setup.cfg +4 -0
- streaming_sql_engine-0.1.0/setup.py +55 -0
- streaming_sql_engine-0.1.0/streaming_sql_engine/__init__.py +30 -0
- streaming_sql_engine-0.1.0/streaming_sql_engine/db_connector.py +353 -0
- streaming_sql_engine-0.1.0/streaming_sql_engine/engine.py +109 -0
- streaming_sql_engine-0.1.0/streaming_sql_engine/evaluator.py +218 -0
- streaming_sql_engine-0.1.0/streaming_sql_engine/executor.py +133 -0
- streaming_sql_engine-0.1.0/streaming_sql_engine/jsonl_executor.py +393 -0
- streaming_sql_engine-0.1.0/streaming_sql_engine/operators.py +479 -0
- streaming_sql_engine-0.1.0/streaming_sql_engine/parser.py +93 -0
- streaming_sql_engine-0.1.0/streaming_sql_engine/planner.py +156 -0
- streaming_sql_engine-0.1.0/streaming_sql_engine.egg-info/PKG-INFO +289 -0
- streaming_sql_engine-0.1.0/streaming_sql_engine.egg-info/SOURCES.txt +34 -0
- streaming_sql_engine-0.1.0/streaming_sql_engine.egg-info/dependency_links.txt +1 -0
- streaming_sql_engine-0.1.0/streaming_sql_engine.egg-info/requires.txt +12 -0
- streaming_sql_engine-0.1.0/streaming_sql_engine.egg-info/top_level.txt +1 -0
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Streaming SQL Engine Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
include README.md
|
|
2
|
+
include LICENSE
|
|
3
|
+
include requirements.txt
|
|
4
|
+
include pyproject.toml
|
|
5
|
+
recursive-include documentation *.md
|
|
6
|
+
recursive-exclude examples *
|
|
7
|
+
recursive-exclude __pycache__ *
|
|
8
|
+
recursive-exclude *.pyc *
|
|
9
|
+
recursive-exclude *.pyo *
|
|
10
|
+
recursive-exclude .git *
|
|
11
|
+
recursive-exclude .pytest_cache *
|
|
12
|
+
recursive-exclude .mypy_cache *
|
|
13
|
+
|
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: streaming-sql-engine
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A lightweight SQL execution engine for streaming row-by-row queries with joins
|
|
5
|
+
Home-page: https://github.com/Ierofantis/streaming-sql-engine
|
|
6
|
+
Author: Ierofantis
|
|
7
|
+
Author-email: Ierofantis <teopanta1986@gmail.com>
|
|
8
|
+
Project-URL: Homepage, https://github.com/Ierofantis/streaming-sql-engine
|
|
9
|
+
Project-URL: Repository, https://github.com/Ierofantis/streaming-sql-engine
|
|
10
|
+
Project-URL: Issues, https://github.com/Ierofantis/streaming-sql-engine/issues
|
|
11
|
+
Project-URL: Documentation, https://github.com/Ierofantis/streaming-sql-engine
|
|
12
|
+
Keywords: sql,streaming,join,database,query,engine
|
|
13
|
+
Classifier: Development Status :: 3 - Alpha
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
16
|
+
Classifier: Topic :: Database
|
|
17
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
18
|
+
Classifier: Programming Language :: Python :: 3
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
24
|
+
Requires-Python: >=3.8
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
Requires-Dist: sqlglot>=23.0.0
|
|
27
|
+
Requires-Dist: psycopg2-binary>=2.9.0
|
|
28
|
+
Requires-Dist: python-dotenv>=1.0.0
|
|
29
|
+
Requires-Dist: pymysql>=1.0.0
|
|
30
|
+
Requires-Dist: DBUtils>=3.0.0
|
|
31
|
+
Requires-Dist: requests>=2.28.0
|
|
32
|
+
Provides-Extra: dev
|
|
33
|
+
Requires-Dist: pytest>=7.0.0; extra == "dev"
|
|
34
|
+
Requires-Dist: pytest-cov>=4.0.0; extra == "dev"
|
|
35
|
+
Requires-Dist: black>=23.0.0; extra == "dev"
|
|
36
|
+
Requires-Dist: flake8>=6.0.0; extra == "dev"
|
|
37
|
+
Dynamic: author
|
|
38
|
+
Dynamic: home-page
|
|
39
|
+
Dynamic: requires-python
|
|
40
|
+
|
|
41
|
+
# Streaming SQL Join Engine
|
|
42
|
+
|
|
43
|
+
A lightweight Python library for executing SQL queries with joins in a streaming, row-by-row fashion without loading full tables into memory.
|
|
44
|
+
|
|
45
|
+
## Features
|
|
46
|
+
|
|
47
|
+
- **Streaming execution**: Processes queries row-by-row using Python generators
|
|
48
|
+
- **INNER and LEFT JOIN support**: Efficient join algorithms (lookup-based and merge joins)
|
|
49
|
+
- **WHERE clause filtering**: Supports comparisons, boolean logic, and NULL checks
|
|
50
|
+
- **Column projection**: SELECT with column selection and aliasing
|
|
51
|
+
- **Memory efficient**: Only materializes lookup-side tables for joins, not full result sets
|
|
52
|
+
|
|
53
|
+
## Installation
|
|
54
|
+
|
|
55
|
+
> **Quick Start:** For detailed installation instructions, see [INSTALLATION.md](INSTALLATION.md)
|
|
56
|
+
|
|
57
|
+
### Option 1: Install from Local Directory (Development)
|
|
58
|
+
|
|
59
|
+
If you have the library source code locally:
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
# Navigate to the library directory
|
|
63
|
+
cd sql_engine
|
|
64
|
+
|
|
65
|
+
# Install in editable mode (recommended for development)
|
|
66
|
+
pip install -e .
|
|
67
|
+
|
|
68
|
+
# Or install normally
|
|
69
|
+
pip install .
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Option 2: Install Dependencies Only
|
|
73
|
+
|
|
74
|
+
If you want to use the library without installing it as a package:
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
pip install -r requirements.txt
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Then add the library directory to your Python path or import directly:
|
|
81
|
+
|
|
82
|
+
```python
|
|
83
|
+
import sys
|
|
84
|
+
sys.path.insert(0, '/path/to/sql_engine')
|
|
85
|
+
from streaming_sql_engine import Engine
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### Option 3: Install from Git Repository
|
|
89
|
+
|
|
90
|
+
If the library is hosted on GitHub or another Git repository:
|
|
91
|
+
|
|
92
|
+
```bash
|
|
93
|
+
pip install git+https://github.com/Ierofantis/streaming-sql-engine.git
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### Option 4: Install as a Package in Your Project
|
|
97
|
+
|
|
98
|
+
Add to your project's `requirements.txt`:
|
|
99
|
+
|
|
100
|
+
```
|
|
101
|
+
streaming-sql-engine @ file:///path/to/sql_engine
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
Or if using `pyproject.toml`:
|
|
105
|
+
|
|
106
|
+
```toml
|
|
107
|
+
[project.dependencies]
|
|
108
|
+
streaming-sql-engine = {path = "../sql_engine", develop = true}
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## PostgreSQL Integration
|
|
112
|
+
|
|
113
|
+
The library includes utilities for connecting to PostgreSQL databases:
|
|
114
|
+
|
|
115
|
+
```python
|
|
116
|
+
from streaming_sql_engine import Engine, create_pool_from_env, create_table_source
|
|
117
|
+
from dotenv import load_dotenv
|
|
118
|
+
import os
|
|
119
|
+
|
|
120
|
+
load_dotenv()
|
|
121
|
+
|
|
122
|
+
# Create connection pool from environment variables
|
|
123
|
+
pool = create_pool_from_env()
|
|
124
|
+
|
|
125
|
+
# Create engine
|
|
126
|
+
engine = Engine()
|
|
127
|
+
|
|
128
|
+
# Register tables from PostgreSQL
|
|
129
|
+
engine.register(
|
|
130
|
+
"users",
|
|
131
|
+
create_table_source(pool, "users", where_clause="active = true", order_by="id"),
|
|
132
|
+
ordered_by="id" # Enable merge joins if sorted
|
|
133
|
+
)
|
|
134
|
+
|
|
135
|
+
# Execute queries
|
|
136
|
+
for row in engine.query("SELECT users.name FROM users WHERE users.id > 1"):
|
|
137
|
+
print(row)
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
Required environment variables (in `.env` file):
|
|
141
|
+
|
|
142
|
+
- `db_host`
|
|
143
|
+
- `db_port`
|
|
144
|
+
- `db_user`
|
|
145
|
+
- `db_password`
|
|
146
|
+
- `db_name`
|
|
147
|
+
|
|
148
|
+
## Quick Start
|
|
149
|
+
|
|
150
|
+
```python
|
|
151
|
+
from streaming_sql_engine import Engine
|
|
152
|
+
|
|
153
|
+
# Create engine instance
|
|
154
|
+
engine = Engine()
|
|
155
|
+
|
|
156
|
+
# Register table sources (functions that return iterators of dictionaries)
|
|
157
|
+
def users_source():
|
|
158
|
+
return iter([
|
|
159
|
+
{"id": 1, "name": "Alice", "dept_id": 10},
|
|
160
|
+
{"id": 2, "name": "Bob", "dept_id": 20},
|
|
161
|
+
{"id": 3, "name": "Charlie", "dept_id": 10},
|
|
162
|
+
])
|
|
163
|
+
|
|
164
|
+
def departments_source():
|
|
165
|
+
return iter([
|
|
166
|
+
{"id": 10, "name": "Engineering"},
|
|
167
|
+
{"id": 20, "name": "Sales"},
|
|
168
|
+
])
|
|
169
|
+
|
|
170
|
+
engine.register("users", users_source)
|
|
171
|
+
engine.register("departments", departments_source)
|
|
172
|
+
|
|
173
|
+
# Execute a query
|
|
174
|
+
query = """
|
|
175
|
+
SELECT users.name, departments.name AS dept_name
|
|
176
|
+
FROM users
|
|
177
|
+
JOIN departments ON users.dept_id = departments.id
|
|
178
|
+
WHERE users.id > 1
|
|
179
|
+
"""
|
|
180
|
+
|
|
181
|
+
for row in engine.query(query):
|
|
182
|
+
print(row)
|
|
183
|
+
# Output:
|
|
184
|
+
# {'name': 'Bob', 'dept_name': 'Sales'}
|
|
185
|
+
# {'name': 'Charlie', 'dept_name': 'Engineering'}
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
## Supported SQL Features
|
|
189
|
+
|
|
190
|
+
### SELECT
|
|
191
|
+
|
|
192
|
+
- Column selection: `SELECT col1, col2`
|
|
193
|
+
- Aliasing: `SELECT col1 AS alias1`
|
|
194
|
+
- Table-qualified columns: `SELECT users.name`
|
|
195
|
+
|
|
196
|
+
### FROM
|
|
197
|
+
|
|
198
|
+
- Single table: `FROM table_name`
|
|
199
|
+
- Table aliases: `FROM users AS u`
|
|
200
|
+
|
|
201
|
+
### JOIN
|
|
202
|
+
|
|
203
|
+
- INNER JOIN: `JOIN table ON left.key = right.key`
|
|
204
|
+
- LEFT JOIN: `LEFT JOIN table ON left.key = right.key`
|
|
205
|
+
- Multiple joins in sequence
|
|
206
|
+
- Only equality joins are supported
|
|
207
|
+
|
|
208
|
+
### WHERE
|
|
209
|
+
|
|
210
|
+
- Comparisons: `=`, `!=`, `<`, `>`, `<=`, `>=`
|
|
211
|
+
- Boolean operators: `AND`, `OR`, `NOT`
|
|
212
|
+
- NULL checks: `IS NULL`, `IS NOT NULL`
|
|
213
|
+
- IN clauses: `column IN (value1, value2, ...)`
|
|
214
|
+
- Column references: `alias.column`
|
|
215
|
+
- Constants: strings, numbers
|
|
216
|
+
|
|
217
|
+
## Not Supported
|
|
218
|
+
|
|
219
|
+
- GROUP BY and aggregations
|
|
220
|
+
- ORDER BY
|
|
221
|
+
- HAVING
|
|
222
|
+
- UNION
|
|
223
|
+
- Subqueries
|
|
224
|
+
- Non-equality joins
|
|
225
|
+
- Arithmetic expressions
|
|
226
|
+
- Functions (except basic literals)
|
|
227
|
+
|
|
228
|
+
## Advanced: Merge Joins
|
|
229
|
+
|
|
230
|
+
For better performance when both tables are sorted by the join key, you can register sources with `ordered_by`:
|
|
231
|
+
|
|
232
|
+
```python
|
|
233
|
+
# Register a source that's sorted by id
|
|
234
|
+
engine.register(
|
|
235
|
+
"users",
|
|
236
|
+
users_source,
|
|
237
|
+
ordered_by="id" # Enables merge join optimization
|
|
238
|
+
)
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
## Architecture
|
|
242
|
+
|
|
243
|
+
The engine follows a pipeline architecture:
|
|
244
|
+
|
|
245
|
+
1. **Parser**: Uses `sqlglot` to parse SQL into an AST
|
|
246
|
+
2. **Planner**: Converts AST into a logical execution plan
|
|
247
|
+
3. **Executor**: Builds a chain of iterator operators:
|
|
248
|
+
- `ScanIterator`: Reads from source
|
|
249
|
+
- `FilterIterator`: Applies WHERE clause
|
|
250
|
+
- `LookupJoinIterator`: Joins using hash lookup (indexes smaller table)
|
|
251
|
+
- `MergeJoinIterator`: Joins using merge algorithm (when both sides sorted)
|
|
252
|
+
- `ProjectIterator`: Applies SELECT projection
|
|
253
|
+
|
|
254
|
+
## Error Handling
|
|
255
|
+
|
|
256
|
+
The engine raises clear exceptions for:
|
|
257
|
+
|
|
258
|
+
- Unsupported SQL constructs
|
|
259
|
+
- Missing table registrations
|
|
260
|
+
- Ambiguous column references
|
|
261
|
+
- Invalid join conditions
|
|
262
|
+
|
|
263
|
+
## Performance Considerations
|
|
264
|
+
|
|
265
|
+
- **Lookup joins**: Memory usage proportional to size of lookup-side table
|
|
266
|
+
- **Merge joins**: Memory usage proportional to size of equal-key runs (usually very small)
|
|
267
|
+
- **Streaming**: Results are yielded as soon as they're produced
|
|
268
|
+
- **No buffering**: Intermediate results are not fully materialized
|
|
269
|
+
|
|
270
|
+
## Documentation
|
|
271
|
+
|
|
272
|
+
- **[TECHNICAL_DOCUMENTATION.md](TECHNICAL_DOCUMENTATION.md)**: Complete technical documentation explaining architecture, algorithms, and design decisions
|
|
273
|
+
- **[DEVELOPER_GUIDE.md](DEVELOPER_GUIDE.md)**: Guide for developers who want to understand, modify, or extend the codebase
|
|
274
|
+
- **[PERFORMANCE.md](PERFORMANCE.md)**: Performance comparison with database joins and optimization tips
|
|
275
|
+
- **[MYSQL_USAGE.md](MYSQL_USAGE.md)**: MySQL-specific usage guide
|
|
276
|
+
- **[QUICK_START.md](QUICK_START.md)**: Quick start guide for new users
|
|
277
|
+
- **[INSTALLATION.md](INSTALLATION.md)**: Detailed installation instructions
|
|
278
|
+
|
|
279
|
+
## Installation from PyPI
|
|
280
|
+
|
|
281
|
+
Once published, you can install the package directly from PyPI:
|
|
282
|
+
|
|
283
|
+
```bash
|
|
284
|
+
pip install streaming-sql-engine
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
## License
|
|
288
|
+
|
|
289
|
+
MIT License - see [LICENSE](LICENSE) file for details.
|
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
# Streaming SQL Join Engine
|
|
2
|
+
|
|
3
|
+
A lightweight Python library for executing SQL queries with joins in a streaming, row-by-row fashion without loading full tables into memory.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Streaming execution**: Processes queries row-by-row using Python generators
|
|
8
|
+
- **INNER and LEFT JOIN support**: Efficient join algorithms (lookup-based and merge joins)
|
|
9
|
+
- **WHERE clause filtering**: Supports comparisons, boolean logic, and NULL checks
|
|
10
|
+
- **Column projection**: SELECT with column selection and aliasing
|
|
11
|
+
- **Memory efficient**: Only materializes lookup-side tables for joins, not full result sets
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
> **Quick Start:** For detailed installation instructions, see [INSTALLATION.md](INSTALLATION.md)
|
|
16
|
+
|
|
17
|
+
### Option 1: Install from Local Directory (Development)
|
|
18
|
+
|
|
19
|
+
If you have the library source code locally:
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
# Navigate to the library directory
|
|
23
|
+
cd sql_engine
|
|
24
|
+
|
|
25
|
+
# Install in editable mode (recommended for development)
|
|
26
|
+
pip install -e .
|
|
27
|
+
|
|
28
|
+
# Or install normally
|
|
29
|
+
pip install .
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### Option 2: Install Dependencies Only
|
|
33
|
+
|
|
34
|
+
If you want to use the library without installing it as a package:
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
pip install -r requirements.txt
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Then add the library directory to your Python path or import directly:
|
|
41
|
+
|
|
42
|
+
```python
|
|
43
|
+
import sys
|
|
44
|
+
sys.path.insert(0, '/path/to/sql_engine')
|
|
45
|
+
from streaming_sql_engine import Engine
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### Option 3: Install from Git Repository
|
|
49
|
+
|
|
50
|
+
If the library is hosted on GitHub or another Git repository:
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
pip install git+https://github.com/Ierofantis/streaming-sql-engine.git
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Option 4: Install as a Package in Your Project
|
|
57
|
+
|
|
58
|
+
Add to your project's `requirements.txt`:
|
|
59
|
+
|
|
60
|
+
```
|
|
61
|
+
streaming-sql-engine @ file:///path/to/sql_engine
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Or if using `pyproject.toml`:
|
|
65
|
+
|
|
66
|
+
```toml
|
|
67
|
+
[project.dependencies]
|
|
68
|
+
streaming-sql-engine = {path = "../sql_engine", develop = true}
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## PostgreSQL Integration
|
|
72
|
+
|
|
73
|
+
The library includes utilities for connecting to PostgreSQL databases:
|
|
74
|
+
|
|
75
|
+
```python
|
|
76
|
+
from streaming_sql_engine import Engine, create_pool_from_env, create_table_source
|
|
77
|
+
from dotenv import load_dotenv
|
|
78
|
+
import os
|
|
79
|
+
|
|
80
|
+
load_dotenv()
|
|
81
|
+
|
|
82
|
+
# Create connection pool from environment variables
|
|
83
|
+
pool = create_pool_from_env()
|
|
84
|
+
|
|
85
|
+
# Create engine
|
|
86
|
+
engine = Engine()
|
|
87
|
+
|
|
88
|
+
# Register tables from PostgreSQL
|
|
89
|
+
engine.register(
|
|
90
|
+
"users",
|
|
91
|
+
create_table_source(pool, "users", where_clause="active = true", order_by="id"),
|
|
92
|
+
ordered_by="id" # Enable merge joins if sorted
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
# Execute queries
|
|
96
|
+
for row in engine.query("SELECT users.name FROM users WHERE users.id > 1"):
|
|
97
|
+
print(row)
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
Required environment variables (in `.env` file):
|
|
101
|
+
|
|
102
|
+
- `db_host`
|
|
103
|
+
- `db_port`
|
|
104
|
+
- `db_user`
|
|
105
|
+
- `db_password`
|
|
106
|
+
- `db_name`
|
|
107
|
+
|
|
108
|
+
## Quick Start
|
|
109
|
+
|
|
110
|
+
```python
|
|
111
|
+
from streaming_sql_engine import Engine
|
|
112
|
+
|
|
113
|
+
# Create engine instance
|
|
114
|
+
engine = Engine()
|
|
115
|
+
|
|
116
|
+
# Register table sources (functions that return iterators of dictionaries)
|
|
117
|
+
def users_source():
|
|
118
|
+
return iter([
|
|
119
|
+
{"id": 1, "name": "Alice", "dept_id": 10},
|
|
120
|
+
{"id": 2, "name": "Bob", "dept_id": 20},
|
|
121
|
+
{"id": 3, "name": "Charlie", "dept_id": 10},
|
|
122
|
+
])
|
|
123
|
+
|
|
124
|
+
def departments_source():
|
|
125
|
+
return iter([
|
|
126
|
+
{"id": 10, "name": "Engineering"},
|
|
127
|
+
{"id": 20, "name": "Sales"},
|
|
128
|
+
])
|
|
129
|
+
|
|
130
|
+
engine.register("users", users_source)
|
|
131
|
+
engine.register("departments", departments_source)
|
|
132
|
+
|
|
133
|
+
# Execute a query
|
|
134
|
+
query = """
|
|
135
|
+
SELECT users.name, departments.name AS dept_name
|
|
136
|
+
FROM users
|
|
137
|
+
JOIN departments ON users.dept_id = departments.id
|
|
138
|
+
WHERE users.id > 1
|
|
139
|
+
"""
|
|
140
|
+
|
|
141
|
+
for row in engine.query(query):
|
|
142
|
+
print(row)
|
|
143
|
+
# Output:
|
|
144
|
+
# {'name': 'Bob', 'dept_name': 'Sales'}
|
|
145
|
+
# {'name': 'Charlie', 'dept_name': 'Engineering'}
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
## Supported SQL Features
|
|
149
|
+
|
|
150
|
+
### SELECT
|
|
151
|
+
|
|
152
|
+
- Column selection: `SELECT col1, col2`
|
|
153
|
+
- Aliasing: `SELECT col1 AS alias1`
|
|
154
|
+
- Table-qualified columns: `SELECT users.name`
|
|
155
|
+
|
|
156
|
+
### FROM
|
|
157
|
+
|
|
158
|
+
- Single table: `FROM table_name`
|
|
159
|
+
- Table aliases: `FROM users AS u`
|
|
160
|
+
|
|
161
|
+
### JOIN
|
|
162
|
+
|
|
163
|
+
- INNER JOIN: `JOIN table ON left.key = right.key`
|
|
164
|
+
- LEFT JOIN: `LEFT JOIN table ON left.key = right.key`
|
|
165
|
+
- Multiple joins in sequence
|
|
166
|
+
- Only equality joins are supported
|
|
167
|
+
|
|
168
|
+
### WHERE
|
|
169
|
+
|
|
170
|
+
- Comparisons: `=`, `!=`, `<`, `>`, `<=`, `>=`
|
|
171
|
+
- Boolean operators: `AND`, `OR`, `NOT`
|
|
172
|
+
- NULL checks: `IS NULL`, `IS NOT NULL`
|
|
173
|
+
- IN clauses: `column IN (value1, value2, ...)`
|
|
174
|
+
- Column references: `alias.column`
|
|
175
|
+
- Constants: strings, numbers
|
|
176
|
+
|
|
177
|
+
## Not Supported
|
|
178
|
+
|
|
179
|
+
- GROUP BY and aggregations
|
|
180
|
+
- ORDER BY
|
|
181
|
+
- HAVING
|
|
182
|
+
- UNION
|
|
183
|
+
- Subqueries
|
|
184
|
+
- Non-equality joins
|
|
185
|
+
- Arithmetic expressions
|
|
186
|
+
- Functions (except basic literals)
|
|
187
|
+
|
|
188
|
+
## Advanced: Merge Joins
|
|
189
|
+
|
|
190
|
+
For better performance when both tables are sorted by the join key, you can register sources with `ordered_by`:
|
|
191
|
+
|
|
192
|
+
```python
|
|
193
|
+
# Register a source that's sorted by id
|
|
194
|
+
engine.register(
|
|
195
|
+
"users",
|
|
196
|
+
users_source,
|
|
197
|
+
ordered_by="id" # Enables merge join optimization
|
|
198
|
+
)
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
## Architecture
|
|
202
|
+
|
|
203
|
+
The engine follows a pipeline architecture:
|
|
204
|
+
|
|
205
|
+
1. **Parser**: Uses `sqlglot` to parse SQL into an AST
|
|
206
|
+
2. **Planner**: Converts AST into a logical execution plan
|
|
207
|
+
3. **Executor**: Builds a chain of iterator operators:
|
|
208
|
+
- `ScanIterator`: Reads from source
|
|
209
|
+
- `FilterIterator`: Applies WHERE clause
|
|
210
|
+
- `LookupJoinIterator`: Joins using hash lookup (indexes smaller table)
|
|
211
|
+
- `MergeJoinIterator`: Joins using merge algorithm (when both sides sorted)
|
|
212
|
+
- `ProjectIterator`: Applies SELECT projection
|
|
213
|
+
|
|
214
|
+
## Error Handling
|
|
215
|
+
|
|
216
|
+
The engine raises clear exceptions for:
|
|
217
|
+
|
|
218
|
+
- Unsupported SQL constructs
|
|
219
|
+
- Missing table registrations
|
|
220
|
+
- Ambiguous column references
|
|
221
|
+
- Invalid join conditions
|
|
222
|
+
|
|
223
|
+
## Performance Considerations
|
|
224
|
+
|
|
225
|
+
- **Lookup joins**: Memory usage proportional to size of lookup-side table
|
|
226
|
+
- **Merge joins**: Memory usage proportional to size of equal-key runs (usually very small)
|
|
227
|
+
- **Streaming**: Results are yielded as soon as they're produced
|
|
228
|
+
- **No buffering**: Intermediate results are not fully materialized
|
|
229
|
+
|
|
230
|
+
## Documentation
|
|
231
|
+
|
|
232
|
+
- **[TECHNICAL_DOCUMENTATION.md](TECHNICAL_DOCUMENTATION.md)**: Complete technical documentation explaining architecture, algorithms, and design decisions
|
|
233
|
+
- **[DEVELOPER_GUIDE.md](DEVELOPER_GUIDE.md)**: Guide for developers who want to understand, modify, or extend the codebase
|
|
234
|
+
- **[PERFORMANCE.md](PERFORMANCE.md)**: Performance comparison with database joins and optimization tips
|
|
235
|
+
- **[MYSQL_USAGE.md](MYSQL_USAGE.md)**: MySQL-specific usage guide
|
|
236
|
+
- **[QUICK_START.md](QUICK_START.md)**: Quick start guide for new users
|
|
237
|
+
- **[INSTALLATION.md](INSTALLATION.md)**: Detailed installation instructions
|
|
238
|
+
|
|
239
|
+
## Installation from PyPI
|
|
240
|
+
|
|
241
|
+
Once published, you can install the package directly from PyPI:
|
|
242
|
+
|
|
243
|
+
```bash
|
|
244
|
+
pip install streaming-sql-engine
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
## License
|
|
248
|
+
|
|
249
|
+
MIT License - see [LICENSE](LICENSE) file for details.
|