|
| 1 | +"""Tests for _create_rows and the result conversions that build rows with it.""" |
| 2 | + |
| 3 | +import pickle |
| 4 | +from decimal import Decimal |
| 5 | +from types import SimpleNamespace |
| 6 | + |
| 7 | +import numpy |
| 8 | +import pytest |
| 9 | + |
| 10 | +try: |
| 11 | + import pyarrow as pa |
| 12 | +except ImportError: |
| 13 | + pa = None |
| 14 | + |
| 15 | +from databricks.sql.backend.sea.result_set import SeaResultSet |
| 16 | +from databricks.sql.result_set import ResultSet, ThriftResultSet |
| 17 | +from databricks.sql.types import Row, _create_rows |
| 18 | +from databricks.sql.utils import ColumnTable |
| 19 | + |
| 20 | +FIELDS = Row("id", "name", "amount") |
| 21 | + |
| 22 | +ROW_INPUTS = { |
| 23 | + "tuples": lambda: [(1, "a", Decimal("1.10")), (2, None, None)], |
| 24 | + "lists": lambda: [[1, "a", Decimal("1.10")], [2, None, None]], |
| 25 | + "numpy_object_rows": lambda: numpy.array( |
| 26 | + [[1, "a", Decimal("1.10")], [2, None, 3.5]], dtype="object" |
| 27 | + ), |
| 28 | + "generator": lambda: ([i, str(i), None] for i in range(3)), |
| 29 | + "fewer_values_than_fields": lambda: [(1,), (2, "b")], |
| 30 | + "no_rows": lambda: [], |
| 31 | +} |
| 32 | + |
| 33 | + |
| 34 | +def _outcome(function): |
| 35 | + try: |
| 36 | + return ("value", function()) |
| 37 | + except Exception as error: |
| 38 | + return ("error", type(error)) |
| 39 | + |
| 40 | + |
| 41 | +def _assert_same_rows(expected, actual): |
| 42 | + assert len(actual) == len(expected) |
| 43 | + for want, got in zip(expected, actual): |
| 44 | + assert type(got) is Row |
| 45 | + assert tuple(got) == tuple(want) |
| 46 | + assert [type(value) for value in got] == [type(value) for value in want] |
| 47 | + assert got.__fields__ is want.__fields__ |
| 48 | + assert repr(got) == repr(want) |
| 49 | + assert got.asDict() == want.asDict() |
| 50 | + assert got == want |
| 51 | + for name in want.__fields__: |
| 52 | + assert _outcome(lambda: getattr(got, name)) == _outcome( |
| 53 | + lambda: getattr(want, name) |
| 54 | + ) |
| 55 | + assert _outcome(lambda: got[name]) == _outcome(lambda: want[name]) |
| 56 | + assert (name in got) == (name in want) |
| 57 | + |
| 58 | + |
| 59 | +@pytest.mark.parametrize("make_rows", ROW_INPUTS.values(), ids=ROW_INPUTS.keys()) |
| 60 | +def test_create_rows_matches_row_factory(make_rows): |
| 61 | + _assert_same_rows( |
| 62 | + [FIELDS(*values) for values in make_rows()], _create_rows(FIELDS, make_rows()) |
| 63 | + ) |
| 64 | + |
| 65 | + |
| 66 | +def test_create_rows_pickles_like_row_factory(): |
| 67 | + want = FIELDS(1, "a", None) |
| 68 | + (got,) = _create_rows(FIELDS, [(1, "a", None)]) |
| 69 | + assert pickle.dumps(got) == pickle.dumps(want) |
| 70 | + restored = pickle.loads(pickle.dumps(got)) |
| 71 | + assert restored == want |
| 72 | + assert restored.asDict() == want.asDict() |
| 73 | + |
| 74 | + |
| 75 | +@pytest.mark.parametrize( |
| 76 | + "extra_row", |
| 77 | + [ |
| 78 | + (1, "a", None, "extra"), |
| 79 | + [1, "a", None, "extra"], |
| 80 | + numpy.array([1, "a", None, "extra"], dtype="object"), |
| 81 | + ], |
| 82 | + ids=["tuple", "list", "numpy_object_row"], |
| 83 | +) |
| 84 | +def test_create_rows_rejects_more_values_than_fields_like_row_factory(extra_row): |
| 85 | + with pytest.raises(ValueError) as expected: |
| 86 | + FIELDS(*extra_row) |
| 87 | + with pytest.raises(ValueError) as actual: |
| 88 | + _create_rows(FIELDS, [(1, "a", None), extra_row]) |
| 89 | + assert str(actual.value) == str(expected.value) |
| 90 | + |
| 91 | + |
| 92 | +def test_create_rows_keeps_rows_read_only(): |
| 93 | + (row,) = _create_rows(FIELDS, [(1, "a", None)]) |
| 94 | + with pytest.raises(RuntimeError): |
| 95 | + row.id = 2 |
| 96 | + |
| 97 | + |
| 98 | +DESCRIPTION = [ |
| 99 | + ("id", "bigint", None, None, None, None, None), |
| 100 | + ("name", "string", None, None, None, None, None), |
| 101 | +] |
| 102 | +EXPECTED_VALUES = [(1, "a"), (None, None), (3, "c")] |
| 103 | + |
| 104 | + |
| 105 | +def _assert_rows_for_description(rows): |
| 106 | + expected = [Row("id", "name")(*values) for values in EXPECTED_VALUES] |
| 107 | + assert rows == expected |
| 108 | + assert all(type(row) is Row for row in rows) |
| 109 | + assert {id(row.__fields__) for row in rows} == {id(rows[0].__fields__)} |
| 110 | + assert rows[0].__fields__ == Row("id", "name") |
| 111 | + assert [row.name for row in rows] == ["a", None, "c"] |
| 112 | + |
| 113 | + |
| 114 | +@pytest.mark.skipif(pa is None, reason="PyArrow is not installed") |
| 115 | +@pytest.mark.parametrize("disable_pandas", [False, True]) |
| 116 | +def test_convert_arrow_table_builds_rows(disable_pandas): |
| 117 | + table = pa.table( |
| 118 | + { |
| 119 | + "id": pa.array([1, None, 3], pa.int64()), |
| 120 | + "name": pa.array(["a", None, "c"], pa.string()), |
| 121 | + } |
| 122 | + ) |
| 123 | + result_set = SimpleNamespace( |
| 124 | + description=DESCRIPTION, |
| 125 | + connection=SimpleNamespace(disable_pandas=disable_pandas), |
| 126 | + ) |
| 127 | + _assert_rows_for_description(ResultSet._convert_arrow_table(result_set, table)) |
| 128 | + |
| 129 | + |
| 130 | +@pytest.mark.skipif(pa is None, reason="PyArrow is not installed") |
| 131 | +@pytest.mark.parametrize("num_rows", [1023, 1024, 1025, 2049]) |
| 132 | +def test_convert_arrow_table_keeps_every_row_across_chunks(num_rows): |
| 133 | + ids = [None if i % 5 == 0 else i for i in range(num_rows)] |
| 134 | + names = [f"name-{i}" for i in range(num_rows)] |
| 135 | + table = pa.table( |
| 136 | + {"id": pa.array(ids, pa.int64()), "name": pa.array(names, pa.string())} |
| 137 | + ) |
| 138 | + result_set = SimpleNamespace( |
| 139 | + description=DESCRIPTION, connection=SimpleNamespace(disable_pandas=False) |
| 140 | + ) |
| 141 | + |
| 142 | + rows = ResultSet._convert_arrow_table(result_set, table) |
| 143 | + |
| 144 | + assert rows == [Row("id", "name")(*values) for values in zip(ids, names)] |
| 145 | + assert all(type(row) is Row for row in rows) |
| 146 | + assert {id(row.__fields__) for row in rows} == {id(rows[0].__fields__)} |
| 147 | + |
| 148 | + |
| 149 | +def test_convert_columnar_table_builds_rows(): |
| 150 | + table = ColumnTable([[1, None, 3], ["a", None, "c"]], ["id", "name"]) |
| 151 | + result_set = SimpleNamespace(description=DESCRIPTION) |
| 152 | + _assert_rows_for_description( |
| 153 | + ThriftResultSet._convert_columnar_table(result_set, table) |
| 154 | + ) |
| 155 | + |
| 156 | + |
| 157 | +def test_create_json_table_builds_rows(): |
| 158 | + result_set = SimpleNamespace( |
| 159 | + description=DESCRIPTION, _convert_json_types=lambda row: list(row) |
| 160 | + ) |
| 161 | + _assert_rows_for_description( |
| 162 | + SeaResultSet._create_json_table(result_set, [list(v) for v in EXPECTED_VALUES]) |
| 163 | + ) |
0 commit comments