Skip to content

Commit 0682e94

Browse files
committed
feat: initial SQL REPL for xarray-sql
1 parent 2cde4e5 commit 0682e94

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

xarray_sql/repl.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
"""Simple SQL REPL for xarray-sql.
2+
3+
Run with: python -m xarray_sql.repl
4+
5+
Starts with a demo "air" table (xarray tutorial dataset). Type SQL and see
6+
results. Commands: .quit or .exit to exit.
7+
"""
8+
9+
import sys
10+
11+
# Enable up/down arrow history for input() (Unix/Mac built-in; Windows: pip install pyreadline3)
12+
try:
13+
import readline # noqa: F401
14+
except ImportError:
15+
pass
16+
17+
import xarray as xr
18+
19+
from .sql import XarrayContext
20+
21+
MAX_DISPLAY_ROWS = 100
22+
23+
24+
def main():
25+
ctx = XarrayContext()
26+
# Demo table: streaming path (no read_all); requires _native to be built
27+
print("Loading demo table 'air' (xarray tutorial air_temperature)...")
28+
air = xr.tutorial.open_dataset("air_temperature").chunk({"time": 240})
29+
ctx.from_dataset("air", air)
30+
print("Ready. Type SQL or .quit / .exit to exit.\n")
31+
32+
while True:
33+
try:
34+
line = input("xarray-sql> ").strip()
35+
except EOFError:
36+
print()
37+
break
38+
39+
if not line:
40+
continue
41+
if line in (".quit", ".exit"):
42+
break
43+
44+
try:
45+
result = ctx.sql(line).to_pandas()
46+
display = result.head(MAX_DISPLAY_ROWS)
47+
print(display.to_string())
48+
if len(result) > MAX_DISPLAY_ROWS:
49+
print(f"... ({len(result) - MAX_DISPLAY_ROWS} more rows)")
50+
except Exception as e:
51+
print(f"Error: {e}", file=sys.stderr)
52+
print()
53+
54+
sys.exit(0)
55+
56+
57+
if __name__ == "__main__":
58+
main()

0 commit comments

Comments
 (0)