|
| 1 | +import pytest |
| 2 | +import pyarrow |
| 3 | +import pandas |
| 4 | +import datetime |
| 5 | +from unittest.mock import MagicMock, patch |
| 6 | + |
| 7 | +from databricks.sql.client import ResultSet, Connection, ExecuteResponse |
| 8 | +from databricks.sql.types import Row |
| 9 | +from databricks.sql.utils import ArrowQueue |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture |
| 13 | +def mock_connection(): |
| 14 | + conn = MagicMock(spec=Connection) |
| 15 | + conn.disable_pandas = False |
| 16 | + conn._arrow_pandas_type_override = {} |
| 17 | + conn._arrow_to_pandas_kwargs = {} |
| 18 | + if not hasattr(conn, '_arrow_to_pandas_kwargs'): |
| 19 | + conn._arrow_to_pandas_kwargs = {} |
| 20 | + return conn |
| 21 | + |
| 22 | +@pytest.fixture |
| 23 | +def mock_thrift_backend(sample_arrow_table): |
| 24 | + tb = MagicMock() |
| 25 | + empty_arrays = [pyarrow.array([], type=field.type) for field in sample_arrow_table.schema] |
| 26 | + empty_table = pyarrow.Table.from_arrays(empty_arrays, schema=sample_arrow_table.schema) |
| 27 | + tb.fetch_results.return_value = (ArrowQueue(empty_table, 0) , False) |
| 28 | + return tb |
| 29 | + |
| 30 | +@pytest.fixture |
| 31 | +def mock_raw_execute_response(): |
| 32 | + er = MagicMock(spec=ExecuteResponse) |
| 33 | + er.description = [("col_int", "int", None, None, None, None, None), |
| 34 | + ("col_str", "string", None, None, None, None, None)] |
| 35 | + er.arrow_schema_bytes = None |
| 36 | + er.arrow_queue = None |
| 37 | + er.has_more_rows = False |
| 38 | + er.lz4_compressed = False |
| 39 | + er.command_handle = MagicMock() |
| 40 | + er.status = MagicMock() |
| 41 | + er.has_been_closed_server_side = False |
| 42 | + er.is_staging_operation = False |
| 43 | + return er |
| 44 | + |
| 45 | +@pytest.fixture |
| 46 | +def sample_arrow_table(): |
| 47 | + data = [ |
| 48 | + pyarrow.array([1, 2, 3], type=pyarrow.int32()), |
| 49 | + pyarrow.array(["a", "b", "c"], type=pyarrow.string()) |
| 50 | + ] |
| 51 | + schema = pyarrow.schema([ |
| 52 | + ('col_int', pyarrow.int32()), |
| 53 | + ('col_str', pyarrow.string()) |
| 54 | + ]) |
| 55 | + return pyarrow.Table.from_arrays(data, schema=schema) |
| 56 | + |
| 57 | + |
| 58 | +def test_convert_arrow_table_default(mock_connection, mock_thrift_backend, mock_raw_execute_response, sample_arrow_table): |
| 59 | + mock_raw_execute_response.arrow_queue = ArrowQueue(sample_arrow_table, sample_arrow_table.num_rows) |
| 60 | + rs = ResultSet(mock_connection, mock_raw_execute_response, mock_thrift_backend) |
| 61 | + result_one = rs.fetchone() |
| 62 | + assert isinstance(result_one, Row) |
| 63 | + assert result_one.col_int == 1 |
| 64 | + assert result_one.col_str == "a" |
| 65 | + mock_raw_execute_response.arrow_queue = ArrowQueue(sample_arrow_table, sample_arrow_table.num_rows) |
| 66 | + rs = ResultSet(mock_connection, mock_raw_execute_response, mock_thrift_backend) |
| 67 | + result_all = rs.fetchall() |
| 68 | + assert len(result_all) == 3 |
| 69 | + assert isinstance(result_all[0], Row) |
| 70 | + assert result_all[0].col_int == 1 |
| 71 | + assert result_all[1].col_str == "b" |
| 72 | + |
| 73 | + |
| 74 | +def test_convert_arrow_table_disable_pandas(mock_connection, mock_thrift_backend, mock_raw_execute_response, sample_arrow_table): |
| 75 | + mock_connection.disable_pandas = True |
| 76 | + mock_raw_execute_response.arrow_queue = ArrowQueue(sample_arrow_table, sample_arrow_table.num_rows) |
| 77 | + rs = ResultSet(mock_connection, mock_raw_execute_response, mock_thrift_backend) |
| 78 | + result = rs.fetchall() |
| 79 | + assert len(result) == 3 |
| 80 | + assert isinstance(result[0], Row) |
| 81 | + assert result[0].col_int == 1 |
| 82 | + assert result[0].col_str == "a" |
| 83 | + assert isinstance(sample_arrow_table.column(0)[0].as_py(), int) |
| 84 | + assert isinstance(sample_arrow_table.column(1)[0].as_py(), str) |
| 85 | + |
| 86 | + |
| 87 | +def test_convert_arrow_table_type_override(mock_connection, mock_thrift_backend, mock_raw_execute_response, sample_arrow_table): |
| 88 | + mock_connection._arrow_pandas_type_override = {pyarrow.int32(): pandas.Float64Dtype()} |
| 89 | + mock_raw_execute_response.arrow_queue = ArrowQueue(sample_arrow_table, sample_arrow_table.num_rows) |
| 90 | + rs = ResultSet(mock_connection, mock_raw_execute_response, mock_thrift_backend) |
| 91 | + result = rs.fetchall() |
| 92 | + assert len(result) == 3 |
| 93 | + assert isinstance(result[0].col_int, float) |
| 94 | + assert result[0].col_int == 1.0 |
| 95 | + assert result[0].col_str == "a" |
| 96 | + |
| 97 | + |
| 98 | +def test_convert_arrow_table_to_pandas_kwargs(mock_connection, mock_thrift_backend, mock_raw_execute_response): |
| 99 | + dt_obj = datetime.datetime(2021, 1, 1, 12, 0, 0, tzinfo=datetime.timezone.utc) |
| 100 | + ts_array = pyarrow.array([dt_obj], type=pyarrow.timestamp('us', tz='UTC')) |
| 101 | + ts_schema = pyarrow.schema([('col_ts', pyarrow.timestamp('us', tz='UTC'))]) |
| 102 | + ts_table = pyarrow.Table.from_arrays([ts_array], schema=ts_schema) |
| 103 | + |
| 104 | + mock_raw_execute_response.description = [("col_ts", "timestamp", None, None, None, None, None)] |
| 105 | + mock_raw_execute_response.arrow_queue = ArrowQueue(ts_table, ts_table.num_rows) |
| 106 | + |
| 107 | + # Scenario 1: timestamp_as_object = True. Observed as datetime.datetime in Row. |
| 108 | + mock_connection._arrow_to_pandas_kwargs = {"timestamp_as_object": True} |
| 109 | + rs_ts_true = ResultSet(mock_connection, mock_raw_execute_response, mock_thrift_backend) |
| 110 | + result_true = rs_ts_true.fetchall() |
| 111 | + assert len(result_true) == 1 |
| 112 | + assert isinstance(result_true[0].col_ts, datetime.datetime) |
| 113 | + |
| 114 | + # Scenario 2: timestamp_as_object = False. Observed as pandas.Timestamp in Row for this input. |
| 115 | + mock_raw_execute_response.arrow_queue = ArrowQueue(ts_table, ts_table.num_rows) |
| 116 | + mock_connection._arrow_to_pandas_kwargs = {"timestamp_as_object": False} |
| 117 | + rs_ts_false = ResultSet(mock_connection, mock_raw_execute_response, mock_thrift_backend) |
| 118 | + result_false = rs_ts_false.fetchall() |
| 119 | + assert len(result_false) == 1 |
| 120 | + assert isinstance(result_false[0].col_ts, pandas.Timestamp) |
| 121 | + |
| 122 | + # Scenario 3: no override. Observed as datetime.datetime in Row since timestamp_as_object is True by default. |
| 123 | + mock_raw_execute_response.arrow_queue = ArrowQueue(ts_table, ts_table.num_rows) |
| 124 | + mock_connection._arrow_to_pandas_kwargs = {} |
| 125 | + rs_ts_true = ResultSet(mock_connection, mock_raw_execute_response, mock_thrift_backend) |
| 126 | + result_true = rs_ts_true.fetchall() |
| 127 | + assert len(result_true) == 1 |
| 128 | + assert isinstance(result_true[0].col_ts, datetime.datetime) |
0 commit comments