-
Notifications
You must be signed in to change notification settings - Fork 188
SQLAlchemy support #81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,11 @@ | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. |
This file contains hidden or 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
Empty file.
This file contains hidden or 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 hidden or 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,11 @@ | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. |
This file contains hidden or 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,46 @@ | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
from assertpy import add_extension, assert_that | ||
from sqlalchemy.sql.sqltypes import ARRAY | ||
|
||
from trino.sqlalchemy.datatype import MAP, ROW, SQLType | ||
|
||
|
||
def assert_sqltype(this: SQLType, that: SQLType): | ||
if isinstance(this, type): | ||
this = this() | ||
if isinstance(that, type): | ||
that = that() | ||
assert_that(type(this)).is_same_as(type(that)) | ||
if isinstance(this, ARRAY): | ||
assert_sqltype(this.item_type, that.item_type) | ||
if this.dimensions is None or this.dimensions == 1: | ||
# ARRAY(dimensions=None) == ARRAY(dimensions=1) | ||
assert_that(that.dimensions).is_in(None, 1) | ||
else: | ||
assert_that(this.dimensions).is_equal_to(this.dimensions) | ||
elif isinstance(this, MAP): | ||
assert_sqltype(this.key_type, that.key_type) | ||
assert_sqltype(this.value_type, that.value_type) | ||
elif isinstance(this, ROW): | ||
assert_that(len(this.attr_types)).is_equal_to(len(that.attr_types)) | ||
for (this_attr, that_attr) in zip(this.attr_types, that.attr_types): | ||
hashhar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert_that(this_attr[0]).is_equal_to(that_attr[0]) | ||
assert_sqltype(this_attr[1], that_attr[1]) | ||
else: | ||
assert_that(str(this)).is_equal_to(str(that)) | ||
hashhar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
@add_extension | ||
def is_sqltype(self, that): | ||
this = self.val | ||
assert_sqltype(this, that) |
This file contains hidden or 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,183 @@ | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
import pytest | ||
from assertpy import assert_that | ||
from sqlalchemy.sql.sqltypes import ( | ||
CHAR, VARCHAR, | ||
ARRAY, | ||
INTEGER, DECIMAL, | ||
DATE, TIME, TIMESTAMP | ||
) | ||
from sqlalchemy.sql.type_api import TypeEngine | ||
|
||
from trino.sqlalchemy import datatype | ||
from trino.sqlalchemy.datatype import MAP, ROW | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'type_str, sql_type', | ||
datatype._type_map.items(), | ||
ids=datatype._type_map.keys() | ||
) | ||
def test_parse_simple_type(type_str: str, sql_type: TypeEngine): | ||
actual_type = datatype.parse_sqltype(type_str) | ||
if not isinstance(actual_type, type): | ||
actual_type = type(actual_type) | ||
assert_that(actual_type).is_equal_to(sql_type) | ||
|
||
|
||
parse_cases_testcases = { | ||
'char(10)': CHAR(10), | ||
'Char(10)': CHAR(10), | ||
'char': CHAR(), | ||
'cHaR': CHAR(), | ||
'VARCHAR(10)': VARCHAR(10), | ||
'varCHAR(10)': VARCHAR(10), | ||
'VARchar(10)': VARCHAR(10), | ||
dungdm93 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
'VARCHAR': VARCHAR(), | ||
'VaRchAr': VARCHAR(), | ||
} | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'type_str, sql_type', | ||
parse_cases_testcases.items(), | ||
ids=parse_cases_testcases.keys() | ||
) | ||
def test_parse_cases(type_str: str, sql_type: TypeEngine): | ||
actual_type = datatype.parse_sqltype(type_str) | ||
assert_that(actual_type).is_sqltype(sql_type) | ||
|
||
|
||
parse_type_options_testcases = { | ||
'CHAR(10)': CHAR(10), | ||
'VARCHAR(10)': VARCHAR(10), | ||
'DECIMAL(20)': DECIMAL(20), | ||
'DECIMAL(20, 3)': DECIMAL(20, 3), | ||
hashhar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# TODO: support parametric timestamps (https://github.com/trinodb/trino-python-client/issues/107) | ||
} | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'type_str, sql_type', | ||
parse_type_options_testcases.items(), | ||
ids=parse_type_options_testcases.keys() | ||
) | ||
def test_parse_type_options(type_str: str, sql_type: TypeEngine): | ||
actual_type = datatype.parse_sqltype(type_str) | ||
assert_that(actual_type).is_sqltype(sql_type) | ||
|
||
|
||
parse_array_testcases = { | ||
'array(integer)': ARRAY(INTEGER()), | ||
'array(varchar(10))': ARRAY(VARCHAR(10)), | ||
'array(decimal(20,3))': ARRAY(DECIMAL(20, 3)), | ||
'array(array(varchar(10)))': ARRAY(VARCHAR(10), dimensions=2), | ||
hashhar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
'array(map(char, integer))': ARRAY(MAP(CHAR(), INTEGER())), | ||
'array(row(a integer, b varchar))': ARRAY(ROW([("a", INTEGER()), ("b", VARCHAR())])), | ||
} | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'type_str, sql_type', | ||
parse_array_testcases.items(), | ||
ids=parse_array_testcases.keys() | ||
) | ||
def test_parse_array(type_str: str, sql_type: ARRAY): | ||
actual_type = datatype.parse_sqltype(type_str) | ||
assert_that(actual_type).is_sqltype(sql_type) | ||
|
||
|
||
parse_map_testcases = { | ||
'map(char, integer)': MAP(CHAR(), INTEGER()), | ||
'map(varchar(10), varchar(10))': MAP(VARCHAR(10), VARCHAR(10)), | ||
'map(varchar(10), decimal(20,3))': MAP(VARCHAR(10), DECIMAL(20, 3)), | ||
'map(char, array(varchar(10)))': MAP(CHAR(), ARRAY(VARCHAR(10))), | ||
'map(varchar(10), array(varchar(10)))': MAP(VARCHAR(10), ARRAY(VARCHAR(10))), | ||
'map(varchar(10), array(array(varchar(10))))': MAP(VARCHAR(10), ARRAY(VARCHAR(10), dimensions=2)), | ||
} | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'type_str, sql_type', | ||
parse_map_testcases.items(), | ||
ids=parse_map_testcases.keys() | ||
) | ||
def test_parse_map(type_str: str, sql_type: ARRAY): | ||
actual_type = datatype.parse_sqltype(type_str) | ||
assert_that(actual_type).is_sqltype(sql_type) | ||
|
||
|
||
parse_row_testcases = { | ||
'row(a integer, b varchar)': | ||
ROW(attr_types=[ | ||
("a", INTEGER()), | ||
("b", VARCHAR()), | ||
]), | ||
'row(a varchar(20), b decimal(20,3))': | ||
ROW(attr_types=[ | ||
("a", VARCHAR(20)), | ||
("b", DECIMAL(20, 3)), | ||
]), | ||
'row(x array(varchar(10)), y array(array(varchar(10))), z decimal(20,3))': | ||
dungdm93 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ROW(attr_types=[ | ||
("x", ARRAY(VARCHAR(10))), | ||
("y", ARRAY(VARCHAR(10), dimensions=2)), | ||
("z", DECIMAL(20, 3)), | ||
]), | ||
'row(min timestamp(6) with time zone, max timestamp(6) with time zone)': | ||
ROW(attr_types=[ | ||
("min", TIMESTAMP(timezone=True)), | ||
("max", TIMESTAMP(timezone=True)), | ||
]), | ||
'row("first name" varchar, "last name" varchar)': | ||
ROW(attr_types=[ | ||
("first name", VARCHAR()), | ||
("last name", VARCHAR()), | ||
]), | ||
'row("foo,bar" varchar, "foo(bar)" varchar, "foo\\"bar" varchar)': | ||
ROW(attr_types=[ | ||
(r'foo,bar', VARCHAR()), | ||
(r'foo(bar)', VARCHAR()), | ||
(r'foo"bar', VARCHAR()), | ||
]), | ||
} | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'type_str, sql_type', | ||
parse_row_testcases.items(), | ||
ids=parse_row_testcases.keys() | ||
) | ||
def test_parse_row(type_str: str, sql_type: ARRAY): | ||
actual_type = datatype.parse_sqltype(type_str) | ||
assert_that(actual_type).is_sqltype(sql_type) | ||
|
||
|
||
parse_datetime_testcases = { | ||
hashhar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# TODO: support parametric timestamps (https://github.com/trinodb/trino-python-client/issues/107) | ||
'date': DATE(), | ||
'time': TIME(), | ||
'time with time zone': TIME(timezone=True), | ||
'timestamp': TIMESTAMP(), | ||
'timestamp with time zone': TIMESTAMP(timezone=True), | ||
} | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'type_str, sql_type', | ||
parse_datetime_testcases.items(), | ||
ids=parse_datetime_testcases.keys() | ||
) | ||
def test_parse_datetime(type_str: str, sql_type: ARRAY): | ||
actual_type = datatype.parse_sqltype(type_str) | ||
assert_that(actual_type).is_sqltype(sql_type) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.