-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding
DataChain.column(...)
and fixing functions and types (#226)
* fixing sql to python * added tests for sql to python and changed input type of sql to python * changing docstring * fixing tests and Decimal type conversion * returning exception when column is not found * changed docstring * fixed typo * added new exception type * renaming error class * skipping division expression tests for CH * using new column method from dc * updating studio branch * return to develop
- Loading branch information
Showing
7 changed files
with
175 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,18 @@ | ||
from datetime import datetime | ||
from decimal import Decimal | ||
from typing import Any | ||
|
||
from sqlalchemy import ARRAY, JSON, Boolean, DateTime, Float, Integer, String | ||
from sqlalchemy import ColumnElement | ||
|
||
from datachain.data_storage.sqlite import Column | ||
|
||
SQL_TO_PYTHON = { | ||
String: str, | ||
Integer: int, | ||
Float: float, | ||
Boolean: bool, | ||
DateTime: datetime, | ||
ARRAY: list, | ||
JSON: dict, | ||
} | ||
def sql_to_python(args_map: dict[str, ColumnElement]) -> dict[str, Any]: | ||
res = {} | ||
for name, sql_exp in args_map.items(): | ||
try: | ||
type_ = sql_exp.type.python_type | ||
if type_ == Decimal: | ||
type_ = float | ||
except NotImplementedError: | ||
type_ = str | ||
res[name] = type_ | ||
|
||
|
||
def sql_to_python(args_map: dict[str, Column]) -> dict[str, Any]: | ||
return { | ||
k: SQL_TO_PYTHON.get(type(v.type), str) # type: ignore[union-attr] | ||
for k, v in args_map.items() | ||
} | ||
return res |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from sqlalchemy.sql.sqltypes import NullType | ||
|
||
from datachain import Column | ||
from datachain.lib.convert.sql_to_python import sql_to_python | ||
from datachain.sql import functions as func | ||
from datachain.sql.types import Float, Int64, String | ||
|
||
|
||
def test_sql_columns_to_python_types(): | ||
assert sql_to_python( | ||
{ | ||
"name": Column("name", String), | ||
"age": Column("age", Int64), | ||
"score": Column("score", Float), | ||
} | ||
) == {"name": str, "age": int, "score": float} | ||
|
||
|
||
def test_sql_expression_to_python_types(): | ||
assert sql_to_python({"age": Column("age", Int64) - 2}) == {"age": int} | ||
|
||
|
||
def test_sql_function_to_python_types(): | ||
assert sql_to_python({"age": func.avg(Column("age", Int64))}) == {"age": float} | ||
|
||
|
||
def test_sql_to_python_types_default_type(): | ||
assert sql_to_python({"null": Column("null", NullType)}) == {"null": str} |