Skip to content

Commit

Permalink
fix(format): handle byte to hex
Browse files Browse the repository at this point in the history
  • Loading branch information
grieve54706 committed Sep 6, 2024
1 parent a02be46 commit 6cf8c97
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 4 deletions.
Empty file.
3 changes: 3 additions & 0 deletions ibis-server/app/custom_sqlglot/dialects/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# ruff: noqa: F401

from app.custom_sqlglot.dialects.mysql import MySQL
10 changes: 10 additions & 0 deletions ibis-server/app/custom_sqlglot/dialects/mysql.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from sqlglot import exp
from sqlglot.dialects import MySQL as OriginalMySQL


class MySQL(OriginalMySQL):
class Generator(OriginalMySQL.Generator):
TYPE_MAPPING = {
**OriginalMySQL.Generator.TYPE_MAPPING,
exp.DataType.Type.VARBINARY: "BINARY",
}
3 changes: 3 additions & 0 deletions ibis-server/app/mdl/rewriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
# To register custom dialects from ibis library for sqlglot
importlib.import_module("ibis.backends.sql.dialects")

# Register custom dialects
importlib.import_module("app.custom_sqlglot.dialects")


class Rewriter(ABC):
def __init__(self, manifest_str: str, data_source: DataSource = None):
Expand Down
2 changes: 2 additions & 0 deletions ibis-server/app/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ def default(obj):
return None
if isinstance(obj, decimal.Decimal):
return str(obj)
if isinstance(obj, (bytes, bytearray)):
return obj.hex()
raise TypeError

json_obj = orjson.loads(
Expand Down
7 changes: 7 additions & 0 deletions ibis-server/tests/routers/v2/connector/test_bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@
"expression": "cast(NULL as timestamp)",
"type": "timestamp",
},
{
"name": "bytea_column",
"expression": "cast('abc' as bytea)",
"type": "bytea",
},
],
"primaryKey": "orderkey",
},
Expand Down Expand Up @@ -92,6 +97,7 @@ def test_query():
"2024-01-01 23:59:59.000000",
"2024-01-01 23:59:59.000000 UTC",
None,
"616263",
]
assert result["dtypes"] == {
"orderkey": "int64",
Expand All @@ -103,6 +109,7 @@ def test_query():
"timestamp": "object",
"timestamptz": "object",
"test_null_time": "datetime64[ns]",
"bytea_column": "object",
}


Expand Down
11 changes: 9 additions & 2 deletions ibis-server/tests/routers/v2/connector/test_clickhouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@
"expression": "toDateTime64(NULL, 9)",
"type": "timestamp",
},
{
"name": "bytea_column",
"expression": "cast('abc' as bytea)",
"type": "bytea",
},
{
"name": "customer",
"type": "Customer",
Expand Down Expand Up @@ -167,7 +172,7 @@ def test_query(clickhouse: ClickHouseContainer):
)
assert response.status_code == 200
result = response.json()
assert len(result["columns"]) == 9
assert len(result["columns"]) == 10
assert len(result["data"]) == 1
assert result["data"][0] == [
1,
Expand All @@ -179,6 +184,7 @@ def test_query(clickhouse: ClickHouseContainer):
"2024-01-01 23:59:59.000000",
"2024-01-01 23:59:59.000000 UTC",
None,
"abc", # Clickhouse does not support bytea, so it is returned as string
]
assert result["dtypes"] == {
"orderkey": "int32",
Expand All @@ -190,6 +196,7 @@ def test_query(clickhouse: ClickHouseContainer):
"timestamp": "object",
"timestamptz": "object",
"test_null_time": "object",
"bytea_column": "object",
}


Expand All @@ -205,7 +212,7 @@ def test_query_with_connection_url(clickhouse: ClickHouseContainer):
)
assert response.status_code == 200
result = response.json()
assert len(result["columns"]) == 9
assert len(result["columns"]) == 10
assert len(result["data"]) == 1
assert result["data"][0][0] == 1
assert result["dtypes"] is not None
Expand Down
7 changes: 7 additions & 0 deletions ibis-server/tests/routers/v2/connector/test_mssql.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@
"expression": "cast(NULL as timestamp)",
"type": "timestamp",
},
{
"name": "bytea_column",
"expression": "cast('abc' as bytea)",
"type": "bytea",
},
],
"primaryKey": "orderkey",
},
Expand Down Expand Up @@ -103,6 +108,7 @@ def test_query(mssql: SqlServerContainer):
"2024-01-01 23:59:59.000000",
"2024-01-01 23:59:59.000000 UTC",
None,
"616263",
]
assert result["dtypes"] == {
"orderkey": "int32",
Expand All @@ -114,6 +120,7 @@ def test_query(mssql: SqlServerContainer):
"timestamp": "object",
"timestamptz": "object",
"test_null_time": "datetime64[ns]",
"bytea_column": "object",
}


Expand Down
7 changes: 7 additions & 0 deletions ibis-server/tests/routers/v2/connector/test_mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@
"expression": "cast(NULL as timestamp)",
"type": "timestamp",
},
{
"name": "bytea_column",
"expression": "cast('abc' as bytea)",
"type": "bytea",
},
],
"primaryKey": "orderkey",
},
Expand Down Expand Up @@ -123,6 +128,7 @@ def test_query(mysql: MySqlContainer):
"2024-01-01 23:59:59.000000",
"2024-01-01 23:59:59.000000",
None,
"616263",
]
assert result["dtypes"] == {
"orderkey": "int32",
Expand All @@ -134,6 +140,7 @@ def test_query(mysql: MySqlContainer):
"timestamp": "object",
"timestamptz": "object",
"test_null_time": "datetime64[ns]",
"bytea_column": "object",
}


Expand Down
11 changes: 9 additions & 2 deletions ibis-server/tests/routers/v2/connector/test_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@
"expression": "cast(NULL as timestamp)",
"type": "timestamp",
},
{
"name": "bytea_column",
"expression": "cast('abc' as bytea)",
"type": "bytea",
},
],
"primaryKey": "orderkey",
},
Expand Down Expand Up @@ -91,7 +96,7 @@ def test_query(postgres: PostgresContainer):
)
assert response.status_code == 200
result = response.json()
assert len(result["columns"]) == 9
assert len(result["columns"]) == len(manifest["models"][0]["columns"])
assert len(result["data"]) == 1
assert result["data"][0] == [
1,
Expand All @@ -103,6 +108,7 @@ def test_query(postgres: PostgresContainer):
"2024-01-01 23:59:59.000000",
"2024-01-01 23:59:59.000000 UTC",
None,
"616263",
]
assert result["dtypes"] == {
"orderkey": "int32",
Expand All @@ -114,6 +120,7 @@ def test_query(postgres: PostgresContainer):
"timestamp": "object",
"timestamptz": "object",
"test_null_time": "datetime64[ns]",
"bytea_column": "object",
}


Expand All @@ -129,7 +136,7 @@ def test_query_with_connection_url(postgres: PostgresContainer):
)
assert response.status_code == 200
result = response.json()
assert len(result["columns"]) == 9
assert len(result["columns"]) == len(manifest["models"][0]["columns"])
assert len(result["data"]) == 1
assert result["data"][0][0] == 1
assert result["dtypes"] is not None
Expand Down
7 changes: 7 additions & 0 deletions ibis-server/tests/routers/v2/connector/test_trino.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@
"expression": "cast(NULL as timestamp)",
"type": "timestamp",
},
{
"name": "bytea_column",
"expression": "cast('abc' as bytea)",
"type": "bytea",
},
],
"primaryKey": "orderkey",
},
Expand Down Expand Up @@ -94,6 +99,7 @@ def test_query(trino: TrinoContainer):
"2024-01-01 23:59:59.000000",
"2024-01-01 23:59:59.000000 UTC",
None,
"616263",
]
assert result["dtypes"] == {
"orderkey": "int64",
Expand All @@ -105,6 +111,7 @@ def test_query(trino: TrinoContainer):
"timestamp": "object",
"timestamptz": "object",
"test_null_time": "datetime64[ns]",
"bytea_column": "object",
}


Expand Down

0 comments on commit 6cf8c97

Please sign in to comment.