-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathray_data_test.py
More file actions
789 lines (654 loc) · 30.2 KB
/
ray_data_test.py
File metadata and controls
789 lines (654 loc) · 30.2 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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
################################################################################
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
################################################################################
import os
import tempfile
import unittest
import shutil
import pyarrow as pa
import pyarrow.types as pa_types
import ray
from pypaimon import CatalogFactory, Schema
from pypaimon.common.options.core_options import CoreOptions
from pypaimon.schema.data_types import PyarrowFieldParser
class RayDataTest(unittest.TestCase):
"""Tests for Ray Data integration with PyPaimon."""
@classmethod
def setUpClass(cls):
"""Set up test environment."""
cls.tempdir = tempfile.mkdtemp()
cls.warehouse = os.path.join(cls.tempdir, 'warehouse')
cls.catalog = CatalogFactory.create({
'warehouse': cls.warehouse
})
cls.catalog.create_database('default', True)
if not ray.is_initialized():
ray.init(ignore_reinit_error=True, num_cpus=2)
try:
from ray.data import DataContext
context = DataContext.get_current()
if hasattr(context, 'shuffle_strategy'):
try:
from ray.data._internal.execution.interfaces.execution_options import ShuffleStrategy
context.shuffle_strategy = ShuffleStrategy.SORT_SHUFFLE_PUSH_BASED
except (ImportError, AttributeError):
pass
if hasattr(context, 'use_polars_sort'):
context.use_polars_sort = True
except (ImportError, AttributeError):
pass
@classmethod
def tearDownClass(cls):
"""Clean up test environment."""
try:
if ray.is_initialized():
ray.shutdown()
except Exception:
pass
try:
shutil.rmtree(cls.tempdir)
except OSError:
pass
def setUp(self):
"""Set up test method."""
pass
def test_basic_ray_data_read(self):
"""Test basic Ray Data read from PyPaimon table."""
# Create schema
pa_schema = pa.schema([
('id', pa.int32()),
('name', pa.string()),
('value', pa.int64()),
])
schema = Schema.from_pyarrow_schema(pa_schema)
self.catalog.create_table('default.test_ray_basic', schema, False)
table = self.catalog.get_table('default.test_ray_basic')
# Write test data
test_data = pa.Table.from_pydict({
'id': [1, 2, 3, 4, 5],
'name': ['Alice', 'Bob', 'Charlie', 'David', 'Eve'],
'value': [100, 200, 300, 400, 500],
}, schema=pa_schema)
write_builder = table.new_batch_write_builder()
writer = write_builder.new_write()
writer.write_arrow(test_data)
commit_messages = writer.prepare_commit()
commit = write_builder.new_commit()
commit.commit(commit_messages)
writer.close()
# Read using Ray Data
read_builder = table.new_read_builder()
table_read = read_builder.new_read()
table_scan = read_builder.new_scan()
splits = table_scan.plan().splits()
ray_dataset = table_read.to_ray(splits, override_num_blocks=2)
# Verify Ray dataset
self.assertIsNotNone(ray_dataset, "Ray dataset should not be None")
self.assertEqual(ray_dataset.count(), 5, "Should have 5 rows")
def test_ray_data_read_and_write_with_blob(self):
import time
pa_schema = pa.schema([
('id', pa.int64()),
('name', pa.string()),
('data', pa.large_binary()), # Table uses large_binary for blob
])
schema = Schema.from_pyarrow_schema(
pa_schema,
options={
'row-tracking.enabled': 'true',
'data-evolution.enabled': 'true',
'blob-field': 'data',
}
)
table_name = f'default.test_ray_read_write_blob_{int(time.time() * 1000000)}'
self.catalog.create_table(table_name, schema, False)
table = self.catalog.get_table(table_name)
# Step 1: Write data to Paimon table using write_arrow (large_binary type)
initial_data = pa.Table.from_pydict({
'id': [1, 2, 3],
'name': ['Alice', 'Bob', 'Charlie'],
'data': [b'blob_data_1', b'blob_data_2', b'blob_data_3'],
}, schema=pa_schema)
write_builder = table.new_batch_write_builder()
writer = write_builder.new_write()
writer.write_arrow(initial_data)
commit_messages = writer.prepare_commit()
commit = write_builder.new_commit()
commit.commit(commit_messages)
writer.close()
# Step 2: Read from Paimon table using to_ray()
read_builder = table.new_read_builder()
table_read = read_builder.new_read()
table_scan = read_builder.new_scan()
splits = table_scan.plan().splits()
ray_dataset = table_read.to_ray(splits)
df_check = ray_dataset.to_pandas()
ray_table_check = pa.Table.from_pandas(df_check)
ray_schema_check = ray_table_check.schema
ray_data_field = ray_schema_check.field('data')
self.assertTrue(
pa_types.is_binary(ray_data_field.type),
f"Ray Dataset from Paimon should have binary() type (Ray Data converts large_binary to binary), but got {ray_data_field.type}"
)
self.assertFalse(
pa_types.is_large_binary(ray_data_field.type),
f"Ray Dataset from Paimon should NOT have large_binary() type, but got {ray_data_field.type}"
)
table_schema = table.table_schema
table_pa_schema = PyarrowFieldParser.from_paimon_schema(table_schema.fields)
table_data_field = table_pa_schema.field('data')
self.assertTrue(
pa_types.is_large_binary(table_data_field.type),
f"Paimon table should have large_binary() type for data, but got {table_data_field.type}"
)
# Step 3: Write Ray Dataset back to Paimon table using write_ray()
write_builder2 = table.new_batch_write_builder()
writer2 = write_builder2.new_write()
writer2.write_ray(
ray_dataset,
overwrite=False,
concurrency=1
)
writer2.close()
# Step 4: Verify the data was written correctly
read_builder2 = table.new_read_builder()
table_read2 = read_builder2.new_read()
result = table_read2.to_arrow(read_builder2.new_scan().plan().splits())
self.assertEqual(result.num_rows, 6, "Table should have 6 rows after roundtrip")
result_df = result.to_pandas()
result_df_sorted = result_df.sort_values(by='id').reset_index(drop=True)
self.assertEqual(list(result_df_sorted['id']), [1, 1, 2, 2, 3, 3], "ID column should match")
self.assertEqual(
list(result_df_sorted['name']),
['Alice', 'Alice', 'Bob', 'Bob', 'Charlie', 'Charlie'],
"Name column should match"
)
written_data_values = [bytes(d) if d is not None else None for d in result_df_sorted['data']]
self.assertEqual(
written_data_values,
[b'blob_data_1', b'blob_data_1', b'blob_data_2', b'blob_data_2', b'blob_data_3', b'blob_data_3'],
"Blob data column should match"
)
def test_basic_ray_data_write(self):
"""Test basic Ray Data write from PyPaimon table."""
# Create schema
pa_schema = pa.schema([
('id', pa.int32()),
('name', pa.string()),
('value', pa.int64()),
])
schema = Schema.from_pyarrow_schema(pa_schema)
self.catalog.create_table('default.test_ray_write', schema, False)
table = self.catalog.get_table('default.test_ray_write')
# Write test data
test_data = pa.Table.from_pydict({
'id': [1, 2, 3, 4, 5],
'name': ['Alice', 'Bob', 'Charlie', 'David', 'Eve'],
'value': [100, 200, 300, 400, 500],
}, schema=pa_schema)
from ray.data.read_api import from_arrow
ds = from_arrow(test_data)
write_builder = table.new_batch_write_builder()
writer = write_builder.new_write()
writer.write_ray(ds, concurrency=2)
# Read using Ray Data
read_builder = table.new_read_builder()
table_read = read_builder.new_read()
table_scan = read_builder.new_scan()
splits = table_scan.plan().splits()
arrow_result = table_read.to_arrow(splits)
# Verify PyArrow table
self.assertIsNotNone(arrow_result, "Arrow table should not be None")
self.assertEqual(arrow_result.num_rows, 5, "Should have 5 rows")
# Test basic operations - get first 3 rows
sample_table = arrow_result.slice(0, 3)
self.assertEqual(sample_table.num_rows, 3, "Should have 3 sample rows")
# Convert to pandas for verification
df = arrow_result.to_pandas()
self.assertEqual(len(df), 5, "DataFrame should have 5 rows")
# Sort by id to ensure order-independent comparison
df_sorted = df.sort_values(by='id').reset_index(drop=True)
self.assertEqual(list(df_sorted['id']), [1, 2, 3, 4, 5], "ID column should match")
self.assertEqual(
list(df_sorted['name']),
['Alice', 'Bob', 'Charlie', 'David', 'Eve'],
"Name column should match"
)
def test_ray_data_with_predicate(self):
"""Test Ray Data read with predicate filtering."""
# Create schema
pa_schema = pa.schema([
('id', pa.int32()),
('category', pa.string()),
('amount', pa.int64()),
])
schema = Schema.from_pyarrow_schema(pa_schema)
self.catalog.create_table('default.test_ray_predicate', schema, False)
table = self.catalog.get_table('default.test_ray_predicate')
# Write test data
test_data = pa.Table.from_pydict({
'id': [1, 2, 3, 4, 5],
'category': ['A', 'B', 'A', 'C', 'B'],
'amount': [100, 200, 150, 300, 250],
}, schema=pa_schema)
write_builder = table.new_batch_write_builder()
writer = write_builder.new_write()
writer.write_arrow(test_data)
commit_messages = writer.prepare_commit()
commit = write_builder.new_commit()
commit.commit(commit_messages)
writer.close()
# Read with predicate
read_builder = table.new_read_builder()
predicate_builder = read_builder.new_predicate_builder()
predicate = predicate_builder.equal('category', 'A')
read_builder = read_builder.with_filter(predicate)
table_read = read_builder.new_read()
table_scan = read_builder.new_scan()
splits = table_scan.plan().splits()
ray_dataset = table_read.to_ray(splits, override_num_blocks=2)
# Verify filtered results
self.assertEqual(ray_dataset.count(), 2, "Should have 2 rows after filtering")
df = ray_dataset.to_pandas()
self.assertEqual(set(df['category'].tolist()), {'A'}, "All rows should have category='A'")
self.assertEqual(set(df['id'].tolist()), {1, 3}, "Should have IDs 1 and 3")
def test_ray_data_with_projection(self):
"""Test Ray Data read with column projection."""
# Create schema
pa_schema = pa.schema([
('id', pa.int32()),
('name', pa.string()),
('email', pa.string()),
('age', pa.int32()),
])
schema = Schema.from_pyarrow_schema(pa_schema)
self.catalog.create_table('default.test_ray_projection', schema, False)
table = self.catalog.get_table('default.test_ray_projection')
# Write test data
test_data = pa.Table.from_pydict({
'id': [1, 2, 3],
'name': ['Alice', 'Bob', 'Charlie'],
'email': ['alice@example.com', 'bob@example.com', 'charlie@example.com'],
'age': [25, 30, 35],
}, schema=pa_schema)
write_builder = table.new_batch_write_builder()
writer = write_builder.new_write()
writer.write_arrow(test_data)
commit_messages = writer.prepare_commit()
commit = write_builder.new_commit()
commit.commit(commit_messages)
writer.close()
# Read with projection
read_builder = table.new_read_builder()
read_builder = read_builder.with_projection(['id', 'name'])
table_read = read_builder.new_read()
table_scan = read_builder.new_scan()
splits = table_scan.plan().splits()
ray_dataset = table_read.to_ray(splits, override_num_blocks=2)
# Verify projection
self.assertEqual(ray_dataset.count(), 3, "Should have 3 rows")
df = ray_dataset.to_pandas()
self.assertEqual(set(df.columns), {'id', 'name'}, "Should only have id and name columns")
self.assertFalse('email' in df.columns, "Should not have email column")
self.assertFalse('age' in df.columns, "Should not have age column")
def test_ray_data_map_operation(self):
"""Test Ray Data map operations after reading from PyPaimon."""
# Create schema
pa_schema = pa.schema([
('id', pa.int32()),
('value', pa.int64()),
])
schema = Schema.from_pyarrow_schema(pa_schema)
self.catalog.create_table('default.test_ray_map', schema, False)
table = self.catalog.get_table('default.test_ray_map')
# Write test data
test_data = pa.Table.from_pydict({
'id': [1, 2, 3, 4, 5],
'value': [10, 20, 30, 40, 50],
}, schema=pa_schema)
write_builder = table.new_batch_write_builder()
writer = write_builder.new_write()
writer.write_arrow(test_data)
commit_messages = writer.prepare_commit()
commit = write_builder.new_commit()
commit.commit(commit_messages)
writer.close()
# Read using Ray Data
read_builder = table.new_read_builder()
table_read = read_builder.new_read()
table_scan = read_builder.new_scan()
splits = table_scan.plan().splits()
ray_dataset = table_read.to_ray(splits, override_num_blocks=2)
# Apply map operation (double the value)
def double_value(row):
row['value'] = row['value'] * 2
return row
mapped_dataset = ray_dataset.map(double_value)
# Verify mapped results
df = mapped_dataset.to_pandas()
# Sort by id to ensure order-independent comparison
df_sorted = df.sort_values(by='id').reset_index(drop=True)
self.assertEqual(list(df_sorted['value']), [20, 40, 60, 80, 100], "Values should be doubled")
def test_ray_data_filter_operation(self):
"""Test Ray Data filter operations after reading from PyPaimon."""
# Create schema
pa_schema = pa.schema([
('id', pa.int32()),
('score', pa.int64()),
])
schema = Schema.from_pyarrow_schema(pa_schema)
self.catalog.create_table('default.test_ray_filter', schema, False)
table = self.catalog.get_table('default.test_ray_filter')
# Write test data
test_data = pa.Table.from_pydict({
'id': [1, 2, 3, 4, 5],
'score': [60, 70, 80, 90, 100],
}, schema=pa_schema)
write_builder = table.new_batch_write_builder()
writer = write_builder.new_write()
writer.write_arrow(test_data)
commit_messages = writer.prepare_commit()
commit = write_builder.new_commit()
commit.commit(commit_messages)
writer.close()
# Read using Ray Data
read_builder = table.new_read_builder()
table_read = read_builder.new_read()
table_scan = read_builder.new_scan()
splits = table_scan.plan().splits()
ray_dataset = table_read.to_ray(splits, override_num_blocks=2)
# Apply filter operation (score >= 80)
filtered_dataset = ray_dataset.filter(lambda row: row['score'] >= 80)
# Verify filtered results
df = filtered_dataset.to_pandas()
self.assertEqual(len(df), 3, "Should have 3 rows with score >= 80")
self.assertEqual(set(df['id'].tolist()), {3, 4, 5}, "Should have IDs 3, 4, 5")
self.assertEqual(set(df['score'].tolist()), {80, 90, 100}, "Should have scores 80, 90, 100")
def test_ray_data_distributed_vs_simple(self):
"""Test that both distributed and simple reading modes work correctly."""
# Create schema
pa_schema = pa.schema([
('id', pa.int32()),
('value', pa.int64()),
])
schema = Schema.from_pyarrow_schema(pa_schema)
self.catalog.create_table('default.test_ray_modes', schema, False)
table = self.catalog.get_table('default.test_ray_modes')
# Write test data
test_data = pa.Table.from_pydict({
'id': [1, 2, 3],
'value': [10, 20, 30],
}, schema=pa_schema)
write_builder = table.new_batch_write_builder()
writer = write_builder.new_write()
writer.write_arrow(test_data)
commit_messages = writer.prepare_commit()
commit = write_builder.new_commit()
commit.commit(commit_messages)
writer.close()
# Read using distributed mode
read_builder = table.new_read_builder()
table_read = read_builder.new_read()
table_scan = read_builder.new_scan()
splits = table_scan.plan().splits()
ray_dataset_distributed = table_read.to_ray(splits, override_num_blocks=2)
ray_dataset_simple = table_read.to_ray(splits, override_num_blocks=1)
# Both should produce the same results
self.assertEqual(ray_dataset_distributed.count(), 3, "Distributed mode should have 3 rows")
self.assertEqual(ray_dataset_simple.count(), 3, "Simple mode should have 3 rows")
df_distributed = ray_dataset_distributed.to_pandas()
df_simple = ray_dataset_simple.to_pandas()
# Sort both dataframes by id to ensure order-independent comparison
df_distributed_sorted = df_distributed.sort_values(by='id').reset_index(drop=True)
df_simple_sorted = df_simple.sort_values(by='id').reset_index(drop=True)
self.assertEqual(list(df_distributed_sorted['id']), list(df_simple_sorted['id']), "IDs should match")
self.assertEqual(list(df_distributed_sorted['value']), list(df_simple_sorted['value']), "Values should match")
def test_ray_data_primary_key_basic(self):
"""Test Ray Data read from PrimaryKey table."""
pa_schema = pa.schema([
('id', pa.int32()),
('name', pa.string()),
('value', pa.int64()),
])
schema = Schema.from_pyarrow_schema(pa_schema, primary_keys=['id'], options={'bucket': '2'})
self.catalog.create_table('default.test_ray_pk_basic', schema, False)
table = self.catalog.get_table('default.test_ray_pk_basic')
test_data = pa.Table.from_pydict({
'id': [1, 2, 3, 4, 5],
'name': ['Alice', 'Bob', 'Charlie', 'David', 'Eve'],
'value': [100, 200, 300, 400, 500],
}, schema=pa_schema)
write_builder = table.new_batch_write_builder()
writer = write_builder.new_write()
writer.write_arrow(test_data)
commit_messages = writer.prepare_commit()
commit = write_builder.new_commit()
commit.commit(commit_messages)
writer.close()
read_builder = table.new_read_builder()
table_read = read_builder.new_read()
table_scan = read_builder.new_scan()
splits = table_scan.plan().splits()
ray_dataset = table_read.to_ray(splits, override_num_blocks=1)
self.assertIsNotNone(ray_dataset, "Ray dataset should not be None")
self.assertEqual(ray_dataset.count(), 5, "Should have 5 rows")
df = ray_dataset.to_pandas()
self.assertEqual(len(df), 5, "DataFrame should have 5 rows")
df_sorted = df.sort_values(by='id').reset_index(drop=True)
self.assertEqual(list(df_sorted['id']), [1, 2, 3, 4, 5], "ID column should match")
self.assertEqual(
list(df_sorted['name']),
['Alice', 'Bob', 'Charlie', 'David', 'Eve'],
"Name column should match"
)
def test_ray_data_primary_key_update(self):
"""Test Ray Data read from PrimaryKey table with updates (upsert behavior)."""
pa_schema = pa.schema([
('id', pa.int32()),
('name', pa.string()),
('value', pa.int64()),
])
schema = Schema.from_pyarrow_schema(pa_schema, primary_keys=['id'], options={'bucket': '2'})
self.catalog.create_table('default.test_ray_pk_update', schema, False)
table = self.catalog.get_table('default.test_ray_pk_update')
initial_data = pa.Table.from_pydict({
'id': [1, 2, 3],
'name': ['Alice', 'Bob', 'Charlie'],
'value': [100, 200, 300],
}, schema=pa_schema)
write_builder = table.new_batch_write_builder()
writer = write_builder.new_write()
writer.write_arrow(initial_data)
commit_messages = writer.prepare_commit()
commit = write_builder.new_commit()
commit.commit(commit_messages)
writer.close()
updated_data = pa.Table.from_pydict({
'id': [1, 2, 4], # id=1,2 updated, id=4 new
'name': ['Alice-Updated', 'Bob-Updated', 'David'],
'value': [150, 250, 400],
}, schema=pa_schema)
write_builder = table.new_batch_write_builder()
writer = write_builder.new_write()
writer.write_arrow(updated_data)
commit_messages = writer.prepare_commit()
commit = write_builder.new_commit()
commit.commit(commit_messages)
writer.close()
read_builder = table.new_read_builder()
table_read = read_builder.new_read()
table_scan = read_builder.new_scan()
splits = table_scan.plan().splits()
ray_dataset = table_read.to_ray(splits, override_num_blocks=2)
self.assertIsNotNone(ray_dataset, "Ray dataset should not be None")
self.assertEqual(ray_dataset.count(), 4, "Should have 4 rows after upsert")
df = ray_dataset.to_pandas()
df_sorted = df.sort_values(by='id').reset_index(drop=True)
self.assertEqual(list(df_sorted['id']), [1, 2, 3, 4], "ID column should match")
self.assertEqual(
list(df_sorted['name']),
['Alice-Updated', 'Bob-Updated', 'Charlie', 'David'],
"Name column should reflect updates"
)
self.assertEqual(list(df_sorted['value']), [150, 250, 300, 400], "Value column should reflect updates")
def test_ray_data_primary_key_with_predicate(self):
"""Test Ray Data read from PrimaryKey table with predicate filtering."""
pa_schema = pa.schema([
('id', pa.int32()),
('category', pa.string()),
('amount', pa.int64()),
('dt', pa.string()),
])
schema = Schema.from_pyarrow_schema(
pa_schema,
primary_keys=['id', 'dt'],
partition_keys=['dt'],
options={'bucket': '2'}
)
self.catalog.create_table('default.test_ray_pk_predicate', schema, False)
table = self.catalog.get_table('default.test_ray_pk_predicate')
test_data = pa.Table.from_pydict({
'id': [1, 2, 3, 4, 5],
'category': ['A', 'B', 'A', 'C', 'B'],
'amount': [100, 200, 150, 300, 250],
'dt': ['2024-01-01', '2024-01-01', '2024-01-02', '2024-01-02', '2024-01-03'],
}, schema=pa_schema)
write_builder = table.new_batch_write_builder()
writer = write_builder.new_write()
writer.write_arrow(test_data)
commit_messages = writer.prepare_commit()
commit = write_builder.new_commit()
commit.commit(commit_messages)
writer.close()
read_builder = table.new_read_builder()
predicate_builder = read_builder.new_predicate_builder()
predicate = predicate_builder.equal('category', 'A')
read_builder = read_builder.with_filter(predicate)
table_read = read_builder.new_read()
table_scan = read_builder.new_scan()
splits = table_scan.plan().splits()
ray_dataset = table_read.to_ray(splits, override_num_blocks=1)
# Verify filtered results
self.assertEqual(ray_dataset.count(), 2, "Should have 2 rows after filtering")
df = ray_dataset.to_pandas()
self.assertEqual(set(df['category'].tolist()), {'A'}, "All rows should have category='A'")
self.assertEqual(set(df['id'].tolist()), {1, 3}, "Should have IDs 1 and 3")
# Read with predicate on partition
read_builder = table.new_read_builder()
predicate_builder = read_builder.new_predicate_builder()
predicate = predicate_builder.equal('dt', '2024-01-01')
read_builder = read_builder.with_filter(predicate)
table_read = read_builder.new_read()
table_scan = read_builder.new_scan()
splits = table_scan.plan().splits()
ray_dataset = table_read.to_ray(splits, override_num_blocks=1)
# Verify filtered results by partition
self.assertEqual(ray_dataset.count(), 2, "Should have 2 rows in partition 2024-01-01")
df = ray_dataset.to_pandas()
self.assertEqual(set(df['dt'].tolist()), {'2024-01-01'}, "All rows should be in partition 2024-01-01")
def test_ray_data_primary_key_multiple_splits_same_bucket(self):
"""Test Ray Data read from PrimaryKey table with small target_split_size."""
pa_schema = pa.schema([
('id', pa.int32()),
('name', pa.string()),
('value', pa.int64()),
])
schema = Schema.from_pyarrow_schema(
pa_schema,
primary_keys=['id'],
options={
'bucket': '2',
CoreOptions.SOURCE_SPLIT_TARGET_SIZE.key(): '1b'
}
)
self.catalog.create_table('default.test_ray_pk_multi_split', schema, False)
table = self.catalog.get_table('default.test_ray_pk_multi_split')
initial_data = pa.Table.from_pydict({
'id': [1, 2, 3],
'name': ['Alice', 'Bob', 'Charlie'],
'value': [100, 200, 300],
}, schema=pa_schema)
write_builder = table.new_batch_write_builder()
writer = write_builder.new_write()
writer.write_arrow(initial_data)
commit_messages = writer.prepare_commit()
commit = write_builder.new_commit()
commit.commit(commit_messages)
writer.close()
updated_data = pa.Table.from_pydict({
'id': [1, 2, 4],
'name': ['Alice-Updated', 'Bob-Updated', 'David'],
'value': [150, 250, 400],
}, schema=pa_schema)
write_builder = table.new_batch_write_builder()
writer = write_builder.new_write()
writer.write_arrow(updated_data)
commit_messages = writer.prepare_commit()
commit = write_builder.new_commit()
commit.commit(commit_messages)
writer.close()
read_builder = table.new_read_builder()
table_read = read_builder.new_read()
table_scan = read_builder.new_scan()
splits = table_scan.plan().splits()
ray_dataset = table_read.to_ray(splits, override_num_blocks=2)
self.assertIsNotNone(ray_dataset, "Ray dataset should not be None")
self.assertEqual(ray_dataset.count(), 4, "Should have 4 rows after upsert")
df = ray_dataset.to_pandas()
df_sorted = df.sort_values(by='id').reset_index(drop=True)
self.assertEqual(list(df_sorted['id']), [1, 2, 3, 4], "ID column should match")
self.assertEqual(
list(df_sorted['name']),
['Alice-Updated', 'Bob-Updated', 'Charlie', 'David'],
"Name column should reflect updates"
)
self.assertEqual(list(df_sorted['value']), [150, 250, 300, 400], "Value column should reflect updates")
def test_ray_data_invalid_parallelism(self):
pa_schema = pa.schema([
('id', pa.int32()),
('name', pa.string()),
])
schema = Schema.from_pyarrow_schema(pa_schema)
self.catalog.create_table('default.test_ray_invalid_parallelism', schema, False)
table = self.catalog.get_table('default.test_ray_invalid_parallelism')
# Write some data
data = pa.Table.from_pydict({
'id': [1, 2, 3],
'name': ['Alice', 'Bob', 'Charlie'],
}, schema=pa_schema)
write_builder = table.new_batch_write_builder()
writer = write_builder.new_write()
writer.write_arrow(data)
commit_messages = writer.prepare_commit()
commit = write_builder.new_commit()
commit.commit(commit_messages)
writer.close()
read_builder = table.new_read_builder()
table_read = read_builder.new_read()
table_scan = read_builder.new_scan()
splits = table_scan.plan().splits()
with self.assertRaises(ValueError) as context:
table_read.to_ray(splits, override_num_blocks=0)
self.assertIn("override_num_blocks must be at least 1", str(context.exception))
with self.assertRaises(ValueError) as context:
table_read.to_ray(splits, override_num_blocks=-1)
self.assertIn("override_num_blocks must be at least 1", str(context.exception))
with self.assertRaises(ValueError) as context:
table_read.to_ray(splits, override_num_blocks=-10)
self.assertIn("override_num_blocks must be at least 1", str(context.exception))
if __name__ == '__main__':
unittest.main()