Skip to content

Commit

Permalink
Alias sql method to query (#219)
Browse files Browse the repository at this point in the history
* Alias sql method to query

* Fix py code style
  • Loading branch information
auxten authored Jun 5, 2024
1 parent c84060d commit 66ac8e9
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
18 changes: 14 additions & 4 deletions chdb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class ChdbError(Exception):
_arrow_format = set({"dataframe", "arrowtable"})
_process_result_format_funs = {
"dataframe": lambda x: to_df(x),
"arrowtable": lambda x: to_arrowTable(x)
"arrowtable": lambda x: to_arrowTable(x),
}

# If any UDF is defined, the path of the UDF will be set to this variable
Expand All @@ -18,7 +18,7 @@ class ChdbError(Exception):
# UDF script path will be f"{g_udf_path}/{func_name}.py"
g_udf_path = ""

chdb_version = ('0', '6', '0')
chdb_version = ("0", "6", "0")
if sys.version_info[:2] >= (3, 7):
# get the path of the current file
current_path = os.path.dirname(os.path.abspath(__file__))
Expand Down Expand Up @@ -79,5 +79,15 @@ def query(sql, output_format="CSV", path="", udf_path=""):
return result_func(res)


__all__ = ["ChdbError", "query", "chdb_version",
"engine_version", "to_df", "to_arrowTable"]
# alias for query
sql = query

__all__ = [
"ChdbError",
"query",
"sql",
"chdb_version",
"engine_version",
"to_df",
"to_arrowTable",
]
3 changes: 3 additions & 0 deletions chdb/dataframe/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ def query(self, sql: str, **kwargs) -> "Table":
else:
raise ValueError("Table object is not initialized correctly")

# alias sql = query
sql = query

def show(self):
print(self.to_pandas())

Expand Down
3 changes: 3 additions & 0 deletions chdb/session/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,6 @@ def query(self, sql, fmt="CSV"):
Execute a query.
"""
return query(sql, fmt, path=self._path)

# alias sql = query
sql = query
8 changes: 5 additions & 3 deletions tests/test_udf.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import unittest
from chdb.udf import chdb_udf
from chdb.session import Session
from chdb import query
from chdb import query, sql


@chdb_udf()
Expand All @@ -30,7 +30,8 @@ def test_define_in_function(self):
def sum_udf2(lhs, rhs):
return int(lhs) + int(rhs)

ret = query("select sum_udf2(11, 22)", "Debug")
# sql is a alias for query
ret = sql("select sum_udf2(11, 22)", "Debug")
self.assertEqual(str(ret), '"33"\n')


Expand All @@ -51,7 +52,8 @@ def sum_udf2(lhs, rhs):
return int(lhs) + int(rhs)

with Session() as session:
ret = session.query("select sum_udf2(11, 22)", "Debug")
# sql is a alias for query
ret = session.sql("select sum_udf2(11, 22)", "Debug")
self.assertEqual(str(ret), '"33"\n')

if __name__ == "__main__":
Expand Down

0 comments on commit 66ac8e9

Please sign in to comment.