Skip to content

Commit 5727d6c

Browse files
nmreadelfauxten
authored andcommitted
Added Dataframe output format (#6)
1 parent 0ba91a7 commit 5727d6c

File tree

4 files changed

+48
-1
lines changed

4 files changed

+48
-1
lines changed

chdb/__init__.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import sys
22
import os
3+
import pyarrow as pa
34

45
chdb_version = (0, 1, 0)
56
if sys.version_info[:2] >= (3, 7):
@@ -22,7 +23,19 @@
2223
except: # pragma: no cover
2324
__version__ = "unknown"
2425

25-
# wrap _chdb functions
2626

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

chdb/__main__.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import sys
2+
import argparse
3+
from .__init__ import query
4+
5+
def main():
6+
prog = 'python -m chdb'
7+
description = ('''A simple command line interface for chdb
8+
to run SQL and output in specified format''')
9+
parser = argparse.ArgumentParser(prog=prog, description=description)
10+
parser.add_argument('sql', nargs=1,
11+
type=str,
12+
help='sql, e.g: select 1112222222,555')
13+
parser.add_argument('format', nargs='?',
14+
type=str,
15+
help='''sql result output format,
16+
e.g: CSV, Dataframe, JSON etc,
17+
more format checkout on
18+
https://clickhouse.com/docs/en/interfaces/formats''',
19+
default="CSV")
20+
options = parser.parse_args()
21+
sql = options.sql[0]
22+
output_format = options.format
23+
res = query(sql, output_format)
24+
if output_format.lower() == 'dataframe':
25+
temp = res
26+
else:
27+
temp = res.data()
28+
print(temp, end="")
29+
30+
if __name__ == '__main__':
31+
main()

chdb/test_smoke.sh

+2
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

setup.py

+1
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ def build_extensions(self):
155155
exclude_package_data={'': ['*.pyc', 'src/**']},
156156
ext_modules=ext_modules,
157157
python_requires='>=3.7',
158+
install_requires=['pyarrow', 'pandas'],
158159
cmdclass={'build_ext': BuildExt},
159160
test_suite="tests",
160161
zip_safe=False,

0 commit comments

Comments
 (0)