-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathtest_pandas_compatibility.py
More file actions
296 lines (242 loc) · 9.64 KB
/
test_pandas_compatibility.py
File metadata and controls
296 lines (242 loc) · 9.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
"""Tests for pandas compatibility across versions (pandas 2.x and 3.x).
These tests verify that _convert_arrow_table correctly converts Arrow tables
to Row objects using pandas as an intermediary, covering various data types
including nullable integers, floats, booleans, and strings.
"""
import unittest
from unittest.mock import Mock
import pandas
import pytest
try:
import pyarrow as pa
except ImportError:
pa = None
from databricks.sql.result_set import ResultSet
from databricks.sql.types import Row
class _ConcreteResultSet(ResultSet):
"""Minimal concrete subclass of ResultSet for testing _convert_arrow_table."""
def fetchone(self):
pass
def fetchmany(self, size):
pass
def fetchall(self):
pass
def fetchmany_arrow(self, size):
pass
def fetchall_arrow(self):
pass
@pytest.mark.skipif(pa is None, reason="PyArrow is not installed")
class TestConvertArrowTablePandasCompat(unittest.TestCase):
"""Test _convert_arrow_table with various Arrow types under current pandas version."""
def _make_result_set(self, description):
"""Create a minimal ResultSet instance for testing _convert_arrow_table."""
mock_connection = Mock()
mock_connection.disable_pandas = False
rs = object.__new__(_ConcreteResultSet)
rs.connection = mock_connection
rs.description = description
return rs
def test_integer_types(self):
table = pa.table(
{
"int8_col": pa.array([1, 2, None], type=pa.int8()),
"int16_col": pa.array([100, None, 300], type=pa.int16()),
"int32_col": pa.array([None, 2000, 3000], type=pa.int32()),
"int64_col": pa.array([10000, 20000, 30000], type=pa.int64()),
}
)
description = [
("int8_col", "tinyint", None, None, None, None, None),
("int16_col", "smallint", None, None, None, None, None),
("int32_col", "int", None, None, None, None, None),
("int64_col", "bigint", None, None, None, None, None),
]
rs = self._make_result_set(description)
rows = rs._convert_arrow_table(table)
self.assertEqual(len(rows), 3)
self.assertEqual(rows[0].int8_col, 1)
self.assertIsNone(rows[0].int32_col)
self.assertIsNone(rows[1].int16_col)
self.assertEqual(rows[2].int64_col, 30000)
def test_unsigned_integer_types(self):
table = pa.table(
{
"uint8_col": pa.array([1, None], type=pa.uint8()),
"uint16_col": pa.array([None, 200], type=pa.uint16()),
"uint32_col": pa.array([3000, 4000], type=pa.uint32()),
"uint64_col": pa.array([50000, None], type=pa.uint64()),
}
)
description = [
("uint8_col", "tinyint", None, None, None, None, None),
("uint16_col", "smallint", None, None, None, None, None),
("uint32_col", "int", None, None, None, None, None),
("uint64_col", "bigint", None, None, None, None, None),
]
rs = self._make_result_set(description)
rows = rs._convert_arrow_table(table)
self.assertEqual(len(rows), 2)
self.assertEqual(rows[0].uint8_col, 1)
self.assertIsNone(rows[0].uint16_col)
self.assertEqual(rows[1].uint16_col, 200)
self.assertIsNone(rows[1].uint64_col)
def test_float_types(self):
table = pa.table(
{
"float32_col": pa.array([1.5, None, 3.5], type=pa.float32()),
"float64_col": pa.array([None, 2.5, 4.5], type=pa.float64()),
}
)
description = [
("float32_col", "float", None, None, None, None, None),
("float64_col", "double", None, None, None, None, None),
]
rs = self._make_result_set(description)
rows = rs._convert_arrow_table(table)
self.assertEqual(len(rows), 3)
self.assertAlmostEqual(rows[0].float32_col, 1.5, places=5)
self.assertIsNone(rows[0].float64_col)
self.assertIsNone(rows[1].float32_col)
self.assertAlmostEqual(rows[2].float64_col, 4.5)
def test_boolean_type(self):
table = pa.table(
{
"bool_col": pa.array([True, False, None], type=pa.bool_()),
}
)
description = [
("bool_col", "boolean", None, None, None, None, None),
]
rs = self._make_result_set(description)
rows = rs._convert_arrow_table(table)
self.assertEqual(len(rows), 3)
self.assertTrue(rows[0].bool_col)
self.assertFalse(rows[1].bool_col)
self.assertIsNone(rows[2].bool_col)
def test_string_type(self):
"""Verify string conversion works with both pandas 2.x and 3.x.
In pandas 3.x, StringDtype() defaults to PyArrow-backed storage
instead of Python object-backed. This test ensures the conversion
still produces correct Python str values.
"""
table = pa.table(
{
"str_col": pa.array(["hello", "world", None], type=pa.string()),
}
)
description = [
("str_col", "string", None, None, None, None, None),
]
rs = self._make_result_set(description)
rows = rs._convert_arrow_table(table)
self.assertEqual(len(rows), 3)
self.assertEqual(rows[0].str_col, "hello")
self.assertEqual(rows[1].str_col, "world")
self.assertIsNone(rows[2].str_col)
def test_mixed_types(self):
"""Test a table with a mix of types, similar to real query results."""
table = pa.table(
{
"id": pa.array([1, 2, 3], type=pa.int64()),
"name": pa.array(["alice", "bob", None], type=pa.string()),
"score": pa.array([95.5, None, 87.3], type=pa.float64()),
"active": pa.array([True, False, None], type=pa.bool_()),
}
)
description = [
("id", "bigint", None, None, None, None, None),
("name", "string", None, None, None, None, None),
("score", "double", None, None, None, None, None),
("active", "boolean", None, None, None, None, None),
]
rs = self._make_result_set(description)
rows = rs._convert_arrow_table(table)
self.assertEqual(len(rows), 3)
# Row 0: all values present
self.assertEqual(rows[0].id, 1)
self.assertEqual(rows[0].name, "alice")
self.assertAlmostEqual(rows[0].score, 95.5)
self.assertTrue(rows[0].active)
# Row 1: name present, score null
self.assertEqual(rows[1].id, 2)
self.assertEqual(rows[1].name, "bob")
self.assertIsNone(rows[1].score)
self.assertFalse(rows[1].active)
# Row 2: name and active null
self.assertEqual(rows[2].id, 3)
self.assertIsNone(rows[2].name)
self.assertAlmostEqual(rows[2].score, 87.3)
self.assertIsNone(rows[2].active)
def test_duplicate_column_names(self):
"""Test that duplicate column names are handled correctly."""
table = pa.table(
[
pa.array([1, 2], type=pa.int32()),
pa.array([3, 4], type=pa.int32()),
],
names=["col", "col"],
)
description = [
("col", "int", None, None, None, None, None),
("col", "int", None, None, None, None, None),
]
rs = self._make_result_set(description)
rows = rs._convert_arrow_table(table)
self.assertEqual(len(rows), 2)
# Access by index since names are duplicated
self.assertEqual(rows[0][0], 1)
self.assertEqual(rows[0][1], 3)
def test_empty_table(self):
table = pa.table(
{
"col": pa.array([], type=pa.int32()),
}
)
description = [
("col", "int", None, None, None, None, None),
]
rs = self._make_result_set(description)
rows = rs._convert_arrow_table(table)
self.assertEqual(len(rows), 0)
def test_all_nulls(self):
table = pa.table(
{
"int_col": pa.array([None, None], type=pa.int64()),
"str_col": pa.array([None, None], type=pa.string()),
}
)
description = [
("int_col", "bigint", None, None, None, None, None),
("str_col", "string", None, None, None, None, None),
]
rs = self._make_result_set(description)
rows = rs._convert_arrow_table(table)
self.assertEqual(len(rows), 2)
self.assertIsNone(rows[0].int_col)
self.assertIsNone(rows[0].str_col)
self.assertIsNone(rows[1].int_col)
self.assertIsNone(rows[1].str_col)
def test_disable_pandas_path(self):
"""Verify the non-pandas code path still works."""
mock_connection = Mock()
mock_connection.disable_pandas = True
rs = object.__new__(_ConcreteResultSet)
rs.connection = mock_connection
rs.description = [
("id", "bigint", None, None, None, None, None),
("name", "string", None, None, None, None, None),
]
table = pa.table(
{
"id": pa.array([1, 2], type=pa.int64()),
"name": pa.array(["a", "b"], type=pa.string()),
}
)
rows = rs._convert_arrow_table(table)
self.assertEqual(len(rows), 2)
self.assertEqual(rows[0].id, 1)
self.assertEqual(rows[0].name, "a")
self.assertEqual(rows[1].id, 2)
self.assertEqual(rows[1].name, "b")
if __name__ == "__main__":
unittest.main()