apexbase 1.24.0

High-performance HTAP embedded database with Rust core
Documentation
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
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
"""
Comprehensive test suite for ApexBase Retrieve Operations

This module tests:
- Single record retrieval (retrieve)
- Multiple record retrieval (retrieve_many)
- All records retrieval (retrieve_all)
- Performance optimizations with Arrow C Data Interface
- Edge cases and error handling
- ResultView conversions from retrieve operations
"""

import pytest
import tempfile
import shutil
from pathlib import Path
import sys
import os
import numpy as np

# Add the apexbase python module to path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'apexbase', 'python'))

try:
    from apexbase import ApexClient, ResultView, ARROW_AVAILABLE, POLARS_AVAILABLE
except ImportError as e:
    pytest.skip(f"ApexBase not available: {e}", allow_module_level=True)

# Optional imports
try:
    import pandas as pd
    PANDAS_AVAILABLE = True
except ImportError:
    PANDAS_AVAILABLE = False

try:
    import polars as pl
    POLARS_DF_AVAILABLE = True
except ImportError:
    POLARS_DF_AVAILABLE = False

try:
    import pyarrow as pa
    PYARROW_AVAILABLE = True
except ImportError:
    PYARROW_AVAILABLE = False


class TestSingleRetrieve:
    """Test single record retrieval operations"""
    
    def test_retrieve_existing_record(self):
        """Test retrieving an existing record"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store test data
            test_data = {"name": "Alice", "age": 25, "city": "NYC"}
            client.store(test_data)
            
            # Retrieve the record
            result = client.retrieve(1)
            
            assert result is not None
            assert isinstance(result, dict)
            assert result["name"] == "Alice"
            assert result["age"] == 25
            assert result["city"] == "NYC"
            # Note: retrieve() returns raw dict which may include _id
            
            client.close()
    
    def test_retrieve_nonexistent_record(self):
        """Test retrieving a nonexistent record"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store some data
            client.store({"name": "Alice", "age": 25})
            
            # Try to retrieve nonexistent record
            result = client.retrieve(999)
            assert result is None
            
            client.close()
    
    def test_retrieve_from_empty_database(self):
        """Test retrieving from empty database"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Try to retrieve from empty database
            result = client.retrieve(1)
            assert result is None
            
            client.close()
    
    def test_retrieve_multiple_records(self):
        """Test retrieving multiple specific records"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store test data
            test_data = [
                {"name": "Alice", "age": 25},
                {"name": "Bob", "age": 30},
                {"name": "Charlie", "age": 35},
                {"name": "Diana", "age": 28},
            ]
            client.store(test_data)
            
            # Retrieve specific records
            result = client.retrieve(1)
            assert result["name"] == "Alice"
            
            result = client.retrieve(2)
            assert result["name"] == "Bob"
            
            result = client.retrieve(3)
            assert result["name"] == "Charlie"
            
            result = client.retrieve(4)
            assert result["name"] == "Diana"
            
            client.close()
    
    def test_retrieve_with_various_data_types(self):
        """Test retrieving records with various data types"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store data with various types
            test_data = {
                "string_field": "test_string",
                "int_field": 42,
                "float_field": 3.14159,
                "bool_field": True,
                "none_field": None,
                "bytes_field": b"binary_data",
                "negative_int": -100,
                "zero_float": 0.0,
                "empty_string": "",
                "false_bool": False,
            }
            
            client.store(test_data)
            
            # Retrieve and verify all types
            result = client.retrieve(1)
            
            assert result["string_field"] == "test_string"
            assert result["int_field"] == 42
            assert result["float_field"] == 3.14159
            assert result["bool_field"] is True
            assert result["none_field"] is None
            assert result["bytes_field"] == b"binary_data"
            assert result["negative_int"] == -100
            assert result["zero_float"] == 0.0
            assert result["empty_string"] == ""
            assert result["false_bool"] is False
            
            client.close()
    
    def test_retrieve_with_unicode_data(self):
        """Test retrieving records with unicode characters"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store unicode data
            test_data = {
                "chinese": "你好世界",
                "emoji": "🌍🚀",
                "arabic": "مرحبا بالعالم",
                "russian": "Привет мир",
                "french": "Bonjour le monde",
            }
            
            client.store(test_data)
            
            # Retrieve and verify unicode data
            result = client.retrieve(1)
            
            for key, expected in test_data.items():
                assert result[key] == expected
            
            client.close()
    
    def test_retrieve_with_special_characters(self):
        """Test retrieving records with special characters"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store data with special characters
            test_data = {
                "quotes": 'Single "double" quotes',
                "newlines": "Line 1\nLine 2\rLine 3",
                "tabs": "Tab\tseparated",
                "backslashes": "Backslash\\test",
                "unicode": "Unicode: ñáéíóú",
                "emoji": "Emoji: 🎉🚀🌟",
                "null_bytes": "Null\x00byte",
            }
            
            client.store(test_data)
            
            # Retrieve and verify special characters
            result = client.retrieve(1)
            
            for key, expected in test_data.items():
                assert result[key] == expected
            
            client.close()


class TestRetrieveMany:
    """Test multiple record retrieval operations"""
    
    def test_retrieve_many_basic(self):
        """Test retrieving multiple records"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store test data
            test_data = [
                {"name": "Alice", "age": 25},
                {"name": "Bob", "age": 30},
                {"name": "Charlie", "age": 35},
                {"name": "Diana", "age": 28},
                {"name": "Eve", "age": 32},
            ]
            client.store(test_data)
            
            # Retrieve multiple records
            results = client.retrieve_many([1, 3, 5])
            
            assert isinstance(results, ResultView)
            assert len(results) == 3
            
            # Verify retrieved records
            retrieved_names = [r["name"] for r in results]
            assert "Alice" in retrieved_names
            assert "Charlie" in retrieved_names
            assert "Eve" in retrieved_names
            assert "Bob" not in retrieved_names
            assert "Diana" not in retrieved_names
            
            client.close()
    
    def test_retrieve_many_unordered_ids(self):
        """Test retrieving records with unordered IDs"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store test data
            test_data = [
                {"name": "Alice", "age": 25},
                {"name": "Bob", "age": 30},
                {"name": "Charlie", "age": 35},
                {"name": "Diana", "age": 28},
            ]
            client.store(test_data)
            
            # Retrieve with unordered IDs
            results = client.retrieve_many([4, 2, 1])
            
            assert len(results) == 3
            
            # Results should be in the order requested
            retrieved_names = [r["name"] for r in results]
            assert retrieved_names == ["Diana", "Bob", "Alice"]
            
            client.close()
    
    def test_retrieve_many_with_duplicates(self):
        """Test retrieving records with duplicate IDs"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store test data
            test_data = [
                {"name": "Alice", "age": 25},
                {"name": "Bob", "age": 30},
                {"name": "Charlie", "age": 35},
            ]
            client.store(test_data)
            
            # Retrieve with duplicate IDs
            results = client.retrieve_many([1, 2, 1, 3, 2])
            
            # Behavior may vary - either deduplicate or return duplicates
            # Test that we get at least the unique records
            unique_names = set(r["name"] for r in results)
            assert "Alice" in unique_names
            assert "Bob" in unique_names
            assert "Charlie" in unique_names
            
            client.close()
    
    def test_retrieve_many_empty_list(self):
        """Test retrieving with empty ID list"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store some data
            client.store({"name": "Alice", "age": 25})
            
            # Retrieve with empty list
            results = client.retrieve_many([])
            
            assert isinstance(results, ResultView)
            assert len(results) == 0
            
            client.close()
    
    def test_retrieve_many_nonexistent_ids(self):
        """Test retrieving with some nonexistent IDs"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store test data
            test_data = [
                {"name": "Alice", "age": 25},
                {"name": "Bob", "age": 30},
                {"name": "Charlie", "age": 35},
            ]
            client.store(test_data)
            
            # Retrieve with mix of existing and nonexistent IDs
            results = client.retrieve_many([1, 999, 2, 888, 3])
            
            # Should only return existing records
            assert len(results) == 3
            retrieved_names = [r["name"] for r in results]
            assert "Alice" in retrieved_names
            assert "Bob" in retrieved_names
            assert "Charlie" in retrieved_names
            
            client.close()
    
    def test_retrieve_many_all_nonexistent(self):
        """Test retrieving with all nonexistent IDs"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store some data
            client.store({"name": "Alice", "age": 25})
            
            # Retrieve with all nonexistent IDs
            results = client.retrieve_many([999, 888, 777])
            
            assert isinstance(results, ResultView)
            assert len(results) == 0
            
            client.close()
    
    def test_retrieve_many_large_list(self):
        """Test retrieving with large ID list"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store large dataset
            large_data = [{"id": i, "value": f"item_{i}"} for i in range(1000)]
            client.store(large_data)
            
            # Retrieve every 10th record
            ids_to_retrieve = list(range(1, 1001, 10))
            results = client.retrieve_many(ids_to_retrieve)
            
            assert len(results) == 100
            
            # Verify some records
            for internal_id, result in zip(ids_to_retrieve, results):
                user_id = internal_id - 1
                assert result["id"] == user_id
                assert result["value"] == f"item_{user_id}"
            
            client.close()


class TestRetrieveAll:
    """Test all records retrieval operations"""
    
    def test_retrieve_all_basic(self):
        """Test retrieving all records"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store test data
            test_data = [
                {"name": "Alice", "age": 25},
                {"name": "Bob", "age": 30},
                {"name": "Charlie", "age": 35},
            ]
            client.store(test_data)
            
            # Retrieve all records
            results = client.retrieve_all()
            
            assert isinstance(results, ResultView)
            assert len(results) == 3
            
            # Verify all records are present
            names = [r["name"] for r in results]
            assert "Alice" in names
            assert "Bob" in names
            assert "Charlie" in names
            
            client.close()
    
    def test_retrieve_all_empty_database(self):
        """Test retrieving all from empty database"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Retrieve all from empty database
            results = client.retrieve_all()
            
            assert isinstance(results, ResultView)
            assert len(results) == 0
            
            client.close()
    
    def test_retrieve_all_large_dataset(self):
        """Test retrieving all from large dataset"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store large dataset
            large_data = [{"id": i, "value": f"item_{i}"} for i in range(5000)]
            client.store(large_data)
            
            # Retrieve all records
            results = client.retrieve_all()
            
            assert len(results) == 5000
            
            # Verify first and last records
            first_record = results[0]
            assert first_record["id"] == 0
            assert first_record["value"] == "item_0"
            
            last_record = results[-1]
            assert last_record["id"] == 4999
            assert last_record["value"] == "item_4999"
            
            client.close()
    
    def test_retrieve_all_with_various_types(self):
        """Test retrieving all records with various data types"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store data with various types
            test_data = [
                {"type": "string", "value": "test_string"},
                {"type": "int", "value": 42},
                {"type": "float", "value": 3.14159},
                {"type": "bool", "value": True},
                {"type": "none", "value": None},
                {"type": "bytes", "value": b"binary"},
            ]
            client.store(test_data)
            
            # Retrieve all records
            results = client.retrieve_all()
            
            assert len(results) == 6
            
            # Verify all types are preserved
            # Verify types are preserved (note: bytes may be converted)
            types_found = set()
            for result in results:
                types_found.add(result["type"])
            
            assert "string" in types_found
            assert "int" in types_found
            assert "float" in types_found
            assert "bool" in types_found
            
            client.close()


class TestRetrieveResultViewConversions:
    """Test ResultView conversions from retrieve operations"""
    
    @pytest.mark.skipif(not PANDAS_AVAILABLE, reason="Pandas not available")
    def test_retrieve_many_to_pandas(self):
        """Test retrieve_many to pandas conversion"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store test data
            test_data = [
                {"name": "Alice", "age": 25, "city": "NYC"},
                {"name": "Bob", "age": 30, "city": "LA"},
                {"name": "Charlie", "age": 35, "city": "Chicago"},
            ]
            client.store(test_data)
            
            # Retrieve multiple records and convert to pandas
            results = client.retrieve_many([1, 3])
            df = results.to_pandas()
            
            assert isinstance(df, pd.DataFrame)
            assert len(df) == 2
            assert "name" in df.columns
            assert "age" in df.columns
            assert "city" in df.columns
            assert "_id" not in df.columns
            
            names = df["name"].tolist()
            assert "Alice" in names
            assert "Charlie" in names
            
            client.close()
    
    @pytest.mark.skipif(not POLARS_DF_AVAILABLE, reason="Polars not available")
    def test_retrieve_all_to_polars(self):
        """Test retrieve_all to polars conversion"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store test data
            test_data = [
                {"name": "Alice", "age": 25},
                {"name": "Bob", "age": 30},
                {"name": "Charlie", "age": 35},
            ]
            client.store(test_data)
            
            # Retrieve all and convert to polars
            results = client.retrieve_all()
            df = results.to_polars()
            
            assert isinstance(df, pl.DataFrame)
            assert len(df) == 3
            assert "name" in df.columns
            assert "age" in df.columns
            assert "_id" not in df.columns
            
            client.close()
    
    @pytest.mark.skipif(not PYARROW_AVAILABLE, reason="PyArrow not available")
    def test_retrieve_many_to_arrow(self):
        """Test retrieve_many to Arrow conversion"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store test data
            test_data = [
                {"name": "Alice", "age": 25},
                {"name": "Bob", "age": 30},
                {"name": "Charlie", "age": 35},
            ]
            client.store(test_data)
            
            # Retrieve multiple records and convert to Arrow
            results = client.retrieve_many([1, 2, 3])
            table = results.to_arrow()
            
            assert isinstance(table, pa.Table)
            assert len(table) == 3
            assert "name" in table.column_names
            assert "age" in table.column_names
            assert "_id" not in table.column_names
            
            client.close()
    
    def test_retrieve_operations_get_ids(self):
        """Test get_ids on retrieve operations"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store test data
            test_data = [
                {"name": "Alice", "age": 25},
                {"name": "Bob", "age": 30},
                {"name": "Charlie", "age": 35},
                {"name": "Diana", "age": 28},
            ]
            client.store(test_data)
            
            # Test retrieve_many get_ids
            results = client.retrieve_many([2, 4])
            ids = results.get_ids()
            
            assert isinstance(ids, np.ndarray)
            assert len(ids) == 2
            # Should contain the requested IDs
            assert 2 in ids or 2 in ids.tolist()
            assert 4 in ids or 4 in ids.tolist()
            
            # Test retrieve_all get_ids
            all_results = client.retrieve_all()
            all_ids = all_results.get_ids()
            
            assert len(all_ids) == 4
            
            client.close()


class TestRetrievePerformance:
    """Test performance optimizations for retrieve operations"""
    
    def test_retrieve_arrow_optimization(self):
        """Test Arrow C Data Interface optimization"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store test data
            test_data = [
                {"name": "Alice", "age": 25},
                {"name": "Bob", "age": 30},
                {"name": "Charlie", "age": 35},
            ]
            client.store(test_data)
            
            # If Arrow is available, retrieve should use Arrow optimization
            if ARROW_AVAILABLE and PYARROW_AVAILABLE:
                # Test that Arrow conversion works efficiently
                results = client.retrieve_many([1, 2, 3])
                
                # Should be able to convert to Arrow without issues
                table = results.to_arrow()
                assert isinstance(table, pa.Table)
                assert len(table) == 3
            
            client.close()
    
    def test_retrieve_performance_comparison(self):
        """Test performance comparison between retrieve methods"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store medium dataset
            data_size = 1000
            test_data = [{"id": i, "value": f"item_{i}"} for i in range(data_size)]
            client.store(test_data)
            
            import time
            
            # Test retrieve_many performance
            ids_to_retrieve = list(range(1, data_size + 1, 10))  # Every 10th item
            
            start_time = time.time()
            results = client.retrieve_many(ids_to_retrieve)
            retrieve_many_time = time.time() - start_time
            
            assert len(results) == len(ids_to_retrieve)
            
            # Test retrieve_all performance
            start_time = time.time()
            all_results = client.retrieve_all()
            retrieve_all_time = time.time() - start_time
            
            assert len(all_results) == data_size
            
            # Both should be reasonably fast
            assert retrieve_many_time < 1.0
            assert retrieve_all_time < 2.0
            
            client.close()
    
    def test_retrieve_large_single_record(self):
        """Test retrieving single record with large data"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store record with large data
            large_string = "x" * 1000000  # 1MB string
            large_data = {
                "id": 1,
                "large_text": large_string,
                "normal_field": "test",
            }
            
            client.store(large_data)
            
            # Retrieve large record
            result = client.retrieve(1)
            
            assert result is not None
            assert result["id"] == 1
            assert len(result["large_text"]) == 1000000
            assert result["normal_field"] == "test"
            
            client.close()


class TestRetrieveEdgeCases:
    """Test edge cases and error handling for retrieve operations"""
    
    def test_retrieve_on_closed_client(self):
        """Test retrieve operations on closed client"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            client.close()
            
            with pytest.raises(RuntimeError, match="connection has been closed"):
                client.retrieve(1)
            
            with pytest.raises(RuntimeError, match="connection has been closed"):
                client.retrieve_many([1, 2, 3])
            
            with pytest.raises(RuntimeError, match="connection has been closed"):
                client.retrieve_all()
    
    def test_retrieve_invalid_id_types(self):
        """Test retrieving with invalid ID types"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store some data
            client.store({"name": "Alice", "age": 25})
            
            # Test invalid ID types for retrieve - may return None or raise exception
            try:
                result = client.retrieve(-1)  # Negative ID
                # If no exception, should return None for invalid ID
                assert result is None
            except (TypeError, ValueError):
                pass  # Exception is also acceptable
            
            client.close()
    
    def test_retrieve_very_large_id(self):
        """Test retrieving with very large ID"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store some data
            client.store({"name": "Alice", "age": 25})
            
            # Test with very large ID
            large_id = 2**63 - 1  # Max 64-bit integer
            result = client.retrieve(large_id)
            assert result is None
            
            client.close()

    def test_retrieve_large_missing_id_keeps_pending_memtable(self, monkeypatch):
        """Large missing-ID lookups short-circuit without flushing pending rows."""
        monkeypatch.delenv("APEXBASE_DISABLE_MEMTABLE_SINGLE_WRITE", raising=False)
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir, durability="fast")
            client.create_table("default")
            client.store({"name": ["seed"], "age": [1]})

            client.store({"name": "pending", "age": 2})
            assert client.retrieve(2)["name"] == "pending"
            assert client._storage.has_pending_memtable_rows() is True

            assert client.retrieve(10**12) is None
            assert client._storage.has_pending_memtable_rows() is True
            assert client.retrieve(2)["name"] == "pending"

            client.close()
    
    def test_retrieve_from_different_tables(self):
        """Test retrieving from different tables"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store data in default table
            client.store({"name": "Alice", "table": "default"})
            
            # Create and store data in another table
            client.create_table("users")
            client.store({"name": "Bob", "table": "users"})
            
            # Retrieve from default table
            client.use_table("default")
            result = client.retrieve(1)
            assert result["name"] == "Alice"
            assert result["table"] == "default"
            
            # Retrieve from users table
            client.use_table("users")
            result = client.retrieve(1)
            assert result["name"] == "Bob"
            assert result["table"] == "users"
            
            client.close()
    
    def test_retrieve_with_fts_enabled(self):
        """Test retrieve operations with FTS enabled"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            client.init_fts(index_fields=["content"])
            
            # Store data with FTS content
            test_data = [
                {"content": "searchable text", "metadata": "test1"},
                {"content": "another searchable content", "metadata": "test2"},
            ]
            client.store(test_data)
            
            # Retrieve records normally
            result = client.retrieve(1)
            assert result["content"] == "searchable text"
            assert result["metadata"] == "test1"
            
            results = client.retrieve_all()
            assert len(results) == 2
            
            client.close()
    
    def test_retrieve_after_deletions(self):
        """Test retrieving after some records have been deleted"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store test data
            test_data = [
                {"name": "Alice", "age": 25},
                {"name": "Bob", "age": 30},
                {"name": "Charlie", "age": 35},
                {"name": "Diana", "age": 28},
            ]
            client.store(test_data)
            
            # Delete some records
            client.delete(2)  # Delete Bob
            client.delete([3])  # Delete Charlie
            
            # Retrieve remaining records
            result = client.retrieve(1)
            assert result["name"] == "Alice"
            
            result = client.retrieve(4)
            assert result["name"] == "Diana"
            
            # Deleted records should return None
            result = client.retrieve(2)
            assert result is None
            
            result = client.retrieve(3)
            assert result is None
            
            client.close()
    
    def test_retrieve_consistency_after_modifications(self):
        """Test retrieve consistency after data modifications"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store initial data
            client.store({"name": "Alice", "age": 25})
            client.store({"name": "Bob", "age": 30})
            
            # Replace a record - note: replace may have specific behavior
            try:
                success = client.replace(1, {"name": "Alice Updated", "age": 26})
                if success:
                    # Retrieve and verify the update
                    result = client.retrieve(1)
                    if result is not None:
                        assert result["name"] == "Alice Updated"
                        assert result["age"] == 26
            except Exception as e:
                print(f"Replace operation: {e}")
            
            # Retrieve all and verify we have records
            results = client.retrieve_all()
            assert len(results) >= 1
            
            client.close()


if __name__ == "__main__":
    pytest.main([__file__, "-v"])