|
| 1 | +#!python3 |
| 2 | + |
| 3 | +import unittest |
| 4 | +import numpy as np |
| 5 | +import pandas as pd |
| 6 | +import pyarrow as pa |
| 7 | +import chdb |
| 8 | + |
| 9 | + |
| 10 | +EXPECTED = """"auxten",9 |
| 11 | +"jerry",7 |
| 12 | +"tom",5 |
| 13 | +""" |
| 14 | + |
| 15 | + |
| 16 | +class myReader(chdb.PyReader): |
| 17 | + def __init__(self, data): |
| 18 | + self.data = data |
| 19 | + self.cursor = 0 |
| 20 | + super().__init__(data) |
| 21 | + |
| 22 | + def read(self, col_names, count): |
| 23 | + print("Python func read", col_names, count, self.cursor) |
| 24 | + if self.cursor >= len(self.data["a"]): |
| 25 | + return [] |
| 26 | + block = [self.data[col] for col in col_names] |
| 27 | + self.cursor += len(block[0]) |
| 28 | + return block |
| 29 | + |
| 30 | + |
| 31 | +class TestQueryPy(unittest.TestCase): |
| 32 | + def test_query_py(self): |
| 33 | + reader = myReader( |
| 34 | + { |
| 35 | + "a": [1, 2, 3, 4, 5, 6], |
| 36 | + "b": ["tom", "jerry", "auxten", "tom", "jerry", "auxten"], |
| 37 | + } |
| 38 | + ) |
| 39 | + |
| 40 | + ret = chdb.query("SELECT b, sum(a) FROM Python(reader) GROUP BY b ORDER BY b") |
| 41 | + self.assertEqual(str(ret), EXPECTED) |
| 42 | + |
| 43 | + def test_query_df(self): |
| 44 | + df = pd.DataFrame( |
| 45 | + { |
| 46 | + "a": [1, 2, 3, 4, 5, 6], |
| 47 | + "b": ["tom", "jerry", "auxten", "tom", "jerry", "auxten"], |
| 48 | + } |
| 49 | + ) |
| 50 | + |
| 51 | + ret = chdb.query("SELECT b, sum(a) FROM Python(df) GROUP BY b ORDER BY b") |
| 52 | + self.assertEqual(str(ret), EXPECTED) |
| 53 | + |
| 54 | + def test_query_arrow(self): |
| 55 | + table = pa.table( |
| 56 | + { |
| 57 | + "a": pa.array([1, 2, 3, 4, 5, 6]), |
| 58 | + "b": pa.array(["tom", "jerry", "auxten", "tom", "jerry", "auxten"]), |
| 59 | + } |
| 60 | + ) |
| 61 | + |
| 62 | + ret = chdb.query( |
| 63 | + "SELECT b, sum(a) FROM Python(table) GROUP BY b ORDER BY b", "debug" |
| 64 | + ) |
| 65 | + self.assertEqual(str(ret), EXPECTED) |
| 66 | + |
| 67 | + def test_query_arrow2(self): |
| 68 | + t2 = pa.table( |
| 69 | + { |
| 70 | + "a": [1, 2, 3, 4, 5, 6], |
| 71 | + "b": ["tom", "jerry", "auxten", "tom", "jerry", "auxten"], |
| 72 | + } |
| 73 | + ) |
| 74 | + |
| 75 | + ret = chdb.query( |
| 76 | + "SELECT b, sum(a) FROM Python(t2) GROUP BY b ORDER BY b", "debug" |
| 77 | + ) |
| 78 | + self.assertEqual(str(ret), EXPECTED) |
| 79 | + |
| 80 | + # def test_query_np(self): |
| 81 | + # t3 = { |
| 82 | + # "a": np.array([1, 2, 3, 4, 5, 6]), |
| 83 | + # "b": np.array(["tom", "jerry", "auxten", "tom", "jerry", "auxten"]), |
| 84 | + # } |
| 85 | + |
| 86 | + # ret = chdb.query( |
| 87 | + # "SELECT b, sum(a) FROM Python(t3) GROUP BY b ORDER BY b", "debug" |
| 88 | + # ) |
| 89 | + # self.assertEqual(str(ret), EXPECTED) |
| 90 | + |
| 91 | + # def test_query_dict(self): |
| 92 | + # data = { |
| 93 | + # "a": [1, 2, 3, 4, 5, 6], |
| 94 | + # "b": ["tom", "jerry", "auxten", "tom", "jerry", "auxten"], |
| 95 | + # } |
| 96 | + |
| 97 | + # ret = chdb.query("SELECT b, sum(a) FROM Python(data) GROUP BY b ORDER BY b") |
| 98 | + # self.assertEqual(str(ret), EXPECTED) |
| 99 | + |
| 100 | + |
| 101 | +if __name__ == "__main__": |
| 102 | + unittest.main() |
0 commit comments