Skip to content

Commit

Permalink
663 added mssql support
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinasKen committed Jan 29, 2025
1 parent b8a23c7 commit b30c885
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
9 changes: 9 additions & 0 deletions spinta/datasets/backends/sql/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ def _group_array_mysql(column: Union[sa.Column, Sequence[sa.Column]]):
return sa.sql.func.json_arrayagg(column)


def _group_array_mssql(column: Union[sa.Column, Sequence[sa.Column]]):
if isinstance(column, Sequence) and not isinstance(column, str):
column = sa.sql.func.json_array(*column)
# Using concat for mssql, because `json_arrayagg` is only available for Azure SQL version
# https://learn.microsoft.com/en-us/sql/t-sql/functions/json-array-transact-sql?view=azuresqldb-current
return sa.sql.func.concat('[', column, ']')


def _default_group_array(column: sa.Column):
raise NotImplemented("Current Sql dialect currently does not support array aggregation.")

Expand All @@ -111,6 +119,7 @@ def _default_group_array(column: sa.Column):
_GROUP_ARRAY_DIALECT_MAPPER = {
"sqlite": _group_array_sqlite,
"postgresql": _group_array_postgresql,
"mssql": _group_array_mssql,
("mysql", "mariadb"): _group_array_mysql,
_DEFAULT_DIALECT_KEY: _default_group_array
}
Expand Down
65 changes: 65 additions & 0 deletions tests/datasets/sql/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,37 @@ def test_array_intermediate_table_mysql(db_dialect: str, rc: RawConfig, mocker):
'''


@pytest.mark.parametrize('db_dialect', ['mssql'])
def test_array_intermediate_table_mssql(db_dialect: str, rc: RawConfig, mocker):
use_dialect_functions(mocker, db_dialect)
assert _build(rc, '''
d | r | b | m | property | type | ref | source | prepare | access
example | | | | |
| data | sql | | | |
| | | | | | |
| | | Language | | id | LANGUAGE | |
| | | | id | string | | ID | | open
| | | | code | string | | CODE | | open
| | | | | | |
| | | Country | | id | COUNTRY | |
| | | | id | string | | ID | | open
| | | | code | string | | CODE | | open
| | | | languages | array | CountryLanguage | | | open
| | | | languages[] | ref | Language | | | open
| | | | | | |
| | | CountryLanguage | | | COUNTRYLANGUAGE | |
| | | | country | ref | Country | COUNTRY | | open
| | | | language | ref | Language | LANGUAGE | | open
''', 'example/Country', page_mapping={}) == '''
SELECT
"COUNTRY"."ID",
"COUNTRY"."CODE", concat(:concat_2, "COUNTRYLANGUAGE_1"."LANGUAGE", :concat_3) AS concat_1
FROM "COUNTRY"
LEFT OUTER JOIN "COUNTRYLANGUAGE" AS "COUNTRYLANGUAGE_1" ON "COUNTRYLANGUAGE_1"."COUNTRY" = "COUNTRY"."ID" GROUP BY "COUNTRY"."ID",
"COUNTRY"."CODE"
'''


@pytest.mark.parametrize('db_dialect', ['sqlite'])
def test_array_intermediate_table_multi_column_sqlite(db_dialect: str, rc: RawConfig, mocker):
use_dialect_functions(mocker, db_dialect)
Expand Down Expand Up @@ -930,3 +961,37 @@ def test_array_intermediate_table_multi_column_mysql(db_dialect: str, rc: RawCon
LEFT OUTER JOIN "COUNTRYLANGUAGE" AS "COUNTRYLANGUAGE_1" ON "COUNTRYLANGUAGE_1"."COUNTRY" = "COUNTRY"."ID" GROUP BY "COUNTRY"."ID",
"COUNTRY"."CODE"
'''


@pytest.mark.parametrize('db_dialect', ['mssql'])
def test_array_intermediate_table_multi_column_mssql(db_dialect: str, rc: RawConfig, mocker):
use_dialect_functions(mocker, db_dialect)
assert _build(rc, '''
d | r | b | m | property | type | ref | source | prepare | access | level
example | | | | | |
| data | sql | | | | |
| | | | | | | |
| | | Language | | id, code | LANGUAGE | | |
| | | | id | string | | ID | | open |
| | | | code | string | | CODE | | open |
| | | | | | | |
| | | Country | | id | COUNTRY | | |
| | | | id | string | | ID | | open |
| | | | code | string | | CODE | | open |
| | | | languages | array | CountryLanguage | | | open |
| | | | languages[] | ref | Language | | | open |
| | | | | | | |
| | | CountryLanguage | | | COUNTRYLANGUAGE | | |
| | | | language_id | string | | LANGUAGEID | | open |
| | | | language_code | string | | LANGUAGECODE | | open |
| | | | country | ref | Country | COUNTRY | | open |
| | | | language | ref | Language | | language_id, language_code | open | 3
''', 'example/Country', page_mapping={}) == '''
SELECT
"COUNTRY"."ID",
"COUNTRY"."CODE", concat(:concat_2, json_array("COUNTRYLANGUAGE_1"."LANGUAGEID",
"COUNTRYLANGUAGE_1"."LANGUAGECODE"), :concat_3) AS concat_1
FROM "COUNTRY"
LEFT OUTER JOIN "COUNTRYLANGUAGE" AS "COUNTRYLANGUAGE_1" ON "COUNTRYLANGUAGE_1"."COUNTRY" = "COUNTRY"."ID" GROUP BY "COUNTRY"."ID",
"COUNTRY"."CODE"
'''

0 comments on commit b30c885

Please sign in to comment.