Skip to content

Commit f6de615

Browse files
committed
Added warpper
1 parent ab2afa8 commit f6de615

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

chdb/__init__.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import sys
22
import os
3+
import pyarrow as pa
4+
35

46
chdb_version = (0, 1, 0)
57
if sys.version_info[:2] >= (3, 7):
@@ -22,7 +24,19 @@
2224
except: # pragma: no cover
2325
__version__ = "unknown"
2426

25-
# wrap _chdb functions
2627

28+
def _to_arrowTable(res):
29+
"""convert res to arrow table"""
30+
return pa.RecordBatchFileReader(res.get_memview()).read_all()
31+
32+
def to_df(r):
33+
""""convert arrow table to Dataframe"""
34+
t = _to_arrowTable(r)
35+
return t.to_pandas(use_threads=True)
36+
37+
# wrap _chdb functions
2738
def query(sql, output_format="CSV", **kwargs):
39+
if output_format.lower() == "dataframe":
40+
r = _chdb.query(sql, "Arrow", **kwargs)
41+
return to_df(r)
2842
return _chdb.query(sql, output_format, **kwargs)

chdb/__main__.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import sys
2+
from .__init__ import query
3+
4+
def main():
5+
if len(sys.argv) < 2:
6+
exit(0)
7+
output_format = sys.argv[2] if len(sys.argv) > 2 else "CSV"
8+
res = query(sys.argv[1], output_format)
9+
if output_format.lower() == 'dataframe':
10+
temp = res
11+
else:
12+
temp = res.data()
13+
print(temp, end="")
14+
15+
if __name__ == '__main__':
16+
main()

chdb/test_smoke.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,5 @@ python3 -c \
2424
python3 -c \
2525
"import chdb; res = chdb.query('select version()', 'CSV'); print(str(res.get_memview().tobytes()))"
2626

27+
# test cli
28+
python3 -m chdb "select 1112222222,555" Dataframe

0 commit comments

Comments
 (0)