1
1
from __future__ import annotations
2
2
3
3
import pytest
4
+ import sqlglot as sg
5
+ import sqlglot .expressions as sge
4
6
from pytest import param
5
7
6
8
import ibis
7
9
import ibis .expr .datatypes as dt
8
10
from ibis import udf
9
11
10
- DB_TYPES = [
12
+ RAW_DB_TYPES = [
11
13
# Exact numbers
12
14
("BIGINT" , dt .int64 ),
13
15
("BIT" , dt .boolean ),
36
38
("DATETIME" , dt .Timestamp (scale = 3 )),
37
39
# Characters strings
38
40
("CHAR" , dt .string ),
39
- param (
40
- "TEXT" ,
41
- dt .string ,
42
- marks = pytest .mark .notyet (
43
- ["mssql" ], reason = "Not supported by UTF-8 aware collations"
44
- ),
45
- ),
46
41
("VARCHAR" , dt .string ),
47
42
# Unicode character strings
48
43
("NCHAR" , dt .string ),
49
- param (
50
- "NTEXT" ,
51
- dt .string ,
52
- marks = pytest .mark .notyet (
53
- ["mssql" ], reason = "Not supported by UTF-8 aware collations"
54
- ),
55
- ),
56
44
("NVARCHAR" , dt .string ),
57
45
# Binary strings
58
46
("BINARY" , dt .binary ),
67
55
("GEOGRAPHY" , dt .geography ),
68
56
("HIERARCHYID" , dt .string ),
69
57
]
58
+ PARAM_TYPES = [
59
+ param (
60
+ "TEXT" ,
61
+ dt .string ,
62
+ marks = pytest .mark .notyet (
63
+ ["mssql" ], reason = "Not supported by UTF-8 aware collations"
64
+ ),
65
+ ),
66
+ param (
67
+ "NTEXT" ,
68
+ dt .string ,
69
+ marks = pytest .mark .notyet (
70
+ ["mssql" ], reason = "Not supported by UTF-8 aware collations"
71
+ ),
72
+ ),
73
+ ]
74
+ DB_TYPES = RAW_DB_TYPES + PARAM_TYPES
70
75
71
76
72
77
@pytest .mark .parametrize (("server_type" , "expected_type" ), DB_TYPES , ids = str )
@@ -81,6 +86,40 @@ def test_get_schema(con, server_type, expected_type, temp_table):
81
86
assert con .sql (f"SELECT * FROM [{ temp_table } ]" ).schema () == expected_schema
82
87
83
88
89
+ def test_schema_type_order (con , temp_table ):
90
+ columns = []
91
+ pairs = {}
92
+
93
+ quoted = con .compiler .quoted
94
+ dialect = con .dialect
95
+ table_id = sg .to_identifier (temp_table , quoted = quoted )
96
+
97
+ for i , (server_type , expected_type ) in enumerate (RAW_DB_TYPES ):
98
+ column_name = f"col_{ i } "
99
+ columns .append (
100
+ sge .ColumnDef (
101
+ this = sg .to_identifier (column_name , quoted = quoted ), kind = server_type
102
+ )
103
+ )
104
+ pairs [column_name ] = expected_type
105
+
106
+ query = sge .Create (
107
+ kind = "TABLE" , this = sge .Schema (this = table_id , expressions = columns )
108
+ )
109
+ stmt = query .sql (dialect )
110
+
111
+ with con .begin () as c :
112
+ c .execute (stmt )
113
+
114
+ expected_schema = ibis .schema (pairs )
115
+
116
+ assert con .get_schema (temp_table ) == expected_schema
117
+ assert con .table (temp_table ).schema () == expected_schema
118
+
119
+ raw_sql = sg .select ("*" ).from_ (table_id ).sql (dialect )
120
+ assert con .sql (raw_sql ).schema () == expected_schema
121
+
122
+
84
123
def test_builtin_scalar_udf (con ):
85
124
@udf .scalar .builtin
86
125
def difference (a : str , b : str ) -> int :
0 commit comments