| 1284 | |
| 1285 | |
| 1286 | def test_record_batch_reader_cast(): |
| 1287 | schema_src = pa.schema([pa.field('a', pa.int64())]) |
| 1288 | data = [ |
| 1289 | pa.record_batch([pa.array([1, 2, 3], type=pa.int64())], names=['a']), |
| 1290 | pa.record_batch([pa.array([4, 5, 6], type=pa.int64())], names=['a']), |
| 1291 | ] |
| 1292 | table_src = pa.Table.from_batches(data) |
| 1293 | |
| 1294 | # Cast to same type should always work |
| 1295 | reader = pa.RecordBatchReader.from_batches(schema_src, data) |
| 1296 | assert reader.cast(schema_src).read_all() == table_src |
| 1297 | |
| 1298 | # Check non-trivial cast |
| 1299 | schema_dst = pa.schema([pa.field('a', pa.int32())]) |
| 1300 | reader = pa.RecordBatchReader.from_batches(schema_src, data) |
| 1301 | assert reader.cast(schema_dst).read_all() == table_src.cast(schema_dst) |
| 1302 | |
| 1303 | # Check error for field name/length mismatch |
| 1304 | reader = pa.RecordBatchReader.from_batches(schema_src, data) |
| 1305 | with pytest.raises(ValueError, match="Target schema's field names"): |
| 1306 | reader.cast(pa.schema([])) |
| 1307 | |
| 1308 | # Check error for impossible cast in call to .cast() |
| 1309 | reader = pa.RecordBatchReader.from_batches(schema_src, data) |
| 1310 | with pytest.raises(pa.lib.ArrowTypeError, match='Field 0 cannot be cast'): |
| 1311 | reader.cast(pa.schema([pa.field('a', pa.list_(pa.int32()))])) |
| 1312 | |
| 1313 | # Cast to same type should always work (also for types without a T->T cast function) |
| 1314 | # (https://github.com/apache/arrow/issues/41884) |
| 1315 | schema_src = pa.schema([pa.field('a', pa.date32())]) |
| 1316 | arr = pa.array([datetime.date(2024, 6, 11)], type=pa.date32()) |
| 1317 | data = [pa.record_batch([arr], names=['a']), pa.record_batch([arr], names=['a'])] |
| 1318 | table_src = pa.Table.from_batches(data) |
| 1319 | reader = pa.RecordBatchReader.from_batches(schema_src, data) |
| 1320 | assert reader.cast(schema_src).read_all() == table_src |
| 1321 | |
| 1322 | |
| 1323 | def test_record_batch_reader_cast_nulls(): |