apexbase 1.21.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
"""
Comprehensive test suite for ApexBase Table Management Operations

This module tests:
- Table creation, deletion, listing, and switching
- Table operations with FTS enabled/disabled
- Edge cases and error handling for table operations
- Table name validation and special characters
"""

import pytest
import tempfile
import shutil
from pathlib import Path
import sys
import os

# 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
except ImportError as e:
    pytest.skip(f"ApexBase not available: {e}", allow_module_level=True)


class TestTableManagement:
    """Test table management operations"""
    
    def test_create_table(self):
        """Test creating a new table"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Create a new table
            client.create_table("users")
            
            # Check current table is updated
            assert client.current_table == "users"
            
            # Verify table exists in list
            tables = client.list_tables()
            assert "users" in tables
            
            client.close()
    
    def test_create_table_with_data(self):
        """Test creating table and immediately storing data"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            
            client.create_table("products")
            client.store({"name": "Laptop", "price": 999.99})
            
            # Verify data was stored in new table
            count = client.count_rows()
            assert count == 1
            
            results = client.query()
            assert len(results) == 1
            assert results[0]["name"] == "Laptop"
            
            client.close()
    
    def test_use_table(self):
        """Test switching between tables"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Create multiple tables
            client.create_table("users")
            client.store({"name": "Alice"})  # Will be stored in users table
            
            client.create_table("products")
            client.store({"name": "Laptop"})  # Will be stored in products table
            
            # Switch back to users table
            client.use_table("users")
            assert client.current_table == "users"
            
            # Switch to products table
            client.use_table("products")
            assert client.current_table == "products"
            
            client.close()
    
    def test_drop_table(self):
        """Test dropping a table"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Create and populate table
            client.create_table("temp_table")
            client.store({"data": "test"})
            
            # Verify table exists
            tables = client.list_tables()
            assert "temp_table" in tables
            
            # Drop table
            client.drop_table("temp_table")
            
            # Verify table is gone
            tables = client.list_tables()
            assert "temp_table" not in tables
            
            # Current table should be None after dropping the active table
            assert client.current_table is None
            
            client.close()
    
    def test_drop_current_table(self):
        """Test dropping the current table"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Create and switch to table
            client.create_table("current")
            assert client.current_table == "current"
            
            # Drop current table
            client.drop_table("current")
            
            # Should be None after dropping the active table
            assert client.current_table is None
            
            client.close()
    
    def test_list_tables(self):
        """Test listing all tables"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Initially empty (no tables created yet)
            tables = client.list_tables()
            assert isinstance(tables, list)
            
            # Create multiple tables
            client.create_table("users")
            client.create_table("products")
            client.create_table("orders")
            
            # List all tables
            tables = client.list_tables()
            assert len(tables) >= 3  # 3 created tables
            assert "users" in tables
            assert "products" in tables
            assert "orders" in tables
            
            client.close()
    
    def test_list_tables_empty_database(self):
        """Test listing tables in fresh database"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir, drop_if_exists=True)
            
            # Fresh database has no tables until explicitly created
            tables = client.list_tables()
            assert isinstance(tables, list)
            assert len(tables) == 0
            
            # After creating a table, it should appear
            client.create_table("default")
            tables = client.list_tables()
            assert "default" in tables
            
            client.close()
    
    def test_current_table_property(self):
        """Test current_table property"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Default should be "default"
            assert client.current_table == "default"
            
            # Create table should update current_table
            client.create_table("test_table")
            assert client.current_table == "test_table"
            
            # Use table should update current_table
            client.use_table("default")
            assert client.current_table == "default"
            
            client.close()
    
    def test_table_isolation(self):
        """Test data isolation between tables"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store data in default table
            client.store({"type": "default_data", "value": 1})
            
            # Create and store data in another table
            client.create_table("other")
            client.store({"type": "other_data", "value": 2})
            
            # Check data isolation
            client.use_table("default")
            default_results = client.query()
            assert len(default_results) == 1
            assert default_results[0]["type"] == "default_data"
            
            client.use_table("other")
            other_results = client.query()
            assert len(other_results) == 1
            assert other_results[0]["type"] == "other_data"
            
            client.close()
    
    def test_table_name_validation(self):
        """Test table name validation with various inputs"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Valid table names
            valid_names = [
                "users",
                "user_data",
                "table123",
                "TABLE_UPPER",
                "table_with_underscores",
                "table-with-dashes",
                "a",  # Single character
                "a" * 100,  # Long name
            ]
            
            for name in valid_names:
                client.create_table(name)
                assert name in client.list_tables()
            
            client.close()
    
    def test_table_name_special_characters(self):
        """Test table names with special characters"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Test various special characters
            special_names = [
                "table with spaces",
                "table@with#special$chars",
                "table.with.dots",
                "table/with/slashes",
                "table\\with\\backslashes",
                "table:with:colons",
                "table;with;semicolons",
                "table'with'quotes",
                'table"with"doublequotes',
                "table(with)parentheses",
                "table[with]brackets",
                "table{with}braces",
            ]
            
            for name in special_names:
                try:
                    client.create_table(name)
                    assert name in client.list_tables()
                except Exception as e:
                    # Some special characters might not be supported
                    # That's acceptable, just log it
                    print(f"Table name '{name}' not supported: {e}")
            
            client.close()
    
    def test_table_name_unicode(self):
        """Test table names with unicode characters"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            unicode_names = [
                "用户",  # Chinese
                "пользователь",  # Russian
                "utilisateur",  # French with accent
                "benutzer",  # German with umlaut
                "📊data",  # Emoji
                "テーブル",  # Japanese
            ]
            
            for name in unicode_names:
                try:
                    client.create_table(name)
                    assert name in client.list_tables()
                except Exception as e:
                    print(f"Unicode table name '{name}' not supported: {e}")
            
            client.close()
    
    def test_drop_nonexistent_table(self):
        """Test dropping a table that doesn't exist"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Try to drop non-existent table
            # Should not raise exception (graceful handling)
            client.drop_table("nonexistent_table")
            
            client.close()
    
    def test_use_nonexistent_table(self):
        """Test switching to a table that doesn't exist"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Using non-existent table should create it or handle gracefully
            try:
                client.use_table("nonexistent")
                # If successful, table should be created or accessible
                assert client.current_table == "nonexistent"
            except Exception as e:
                # If it raises an exception, that's also acceptable behavior
                print(f"Using nonexistent table raised: {e}")
            
            client.close()
    
    def test_create_existing_table(self):
        """Test creating a table that already exists"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Create table
            client.create_table("existing")
            
            # Try to create same table again
            try:
                client.create_table("existing")
                # If successful, should handle gracefully
            except Exception as e:
                # If it raises an exception, that's acceptable
                print(f"Creating existing table raised: {e}")
            
            client.close()
    
    def test_table_operations_with_fts(self):
        """Test table operations when FTS is enabled"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Create table with FTS
            client.create_table("fts_table")
            client.init_fts(table_name="fts_table", index_fields=["content"])
            
            # Verify FTS is enabled for the table
            assert client._is_fts_enabled("fts_table")
            
            # Store data with searchable content
            client.store({"content": "searchable text", "metadata": "test"})
            
            # Perform search
            results = client.search_text("searchable", table_name="fts_table")
            assert results is not None
            assert len(results) > 0
            
            # Drop table with FTS
            client.drop_table("fts_table")
            
            # Verify FTS config is cleaned up
            assert not client._is_fts_enabled("fts_table")
            
            client.close()
    
    def test_multiple_table_fts_configs(self):
        """Test different FTS configurations for different tables"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Create multiple tables with different FTS configs
            client.create_table("articles")
            client.init_fts(table_name="articles", index_fields=["title", "body"])
            
            client.create_table("comments")
            client.init_fts(table_name="comments", index_fields=["text"], lazy_load=True)
            
            # Verify different configs
            articles_config = client._get_fts_config("articles")
            comments_config = client._get_fts_config("comments")
            
            assert articles_config is not None
            assert comments_config is not None
            assert articles_config["index_fields"] == ["title", "body"]
            assert comments_config["index_fields"] == ["text"]
            assert comments_config["config"]["lazy_load"] is True
            
            client.close()
    
    def test_table_operations_on_closed_client(self):
        """Test table operations on closed client"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            client.close()
            
            # All table operations should raise RuntimeError
            with pytest.raises(RuntimeError, match="connection has been closed"):
                client.create_table("test")
            
            with pytest.raises(RuntimeError, match="connection has been closed"):
                client.use_table("test")
            
            with pytest.raises(RuntimeError, match="connection has been closed"):
                client.drop_table("test")
            
            with pytest.raises(RuntimeError, match="connection has been closed"):
                client.list_tables()
            
            with pytest.raises(RuntimeError, match="connection has been closed"):
                _ = client.current_table
    
    def test_table_count_with_table_name(self):
        """Test count_rows with specific table name"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store data in default table
            client.store({"table": "default"})
            
            # Create and store data in another table
            client.create_table("other")
            client.store({"table": "other"})
            
            # Count rows in specific tables
            default_count = client.count_rows(table_name="default")
            other_count = client.count_rows(table_name="other")
            
            assert default_count == 1
            assert other_count == 1
            
            # Current table count should work
            current_count = client.count_rows()
            assert current_count == 1
            
            client.close()
    
    def test_table_edge_cases(self):
        """Test edge cases for table operations"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Test empty string table name
            try:
                client.create_table("")
                tables = client.list_tables()
                assert "" in tables
            except Exception as e:
                print(f"Empty table name not supported: {e}")
            
            # Test very long table name
            long_name = "a" * 1000
            try:
                client.create_table(long_name)
                tables = client.list_tables()
                assert long_name in tables
            except Exception as e:
                print(f"Very long table name not supported: {e}")
            
            # Test table name with only numbers
            try:
                client.create_table("123")
                tables = client.list_tables()
                assert "123" in tables
            except Exception as e:
                print(f"Numeric table name not supported: {e}")
            
            client.close()
    
    def test_table_persistence(self):
        """Test table persistence across client sessions"""
        with tempfile.TemporaryDirectory() as temp_dir:
            # Create tables with first client
            client1 = ApexClient(dirpath=temp_dir)
            client1.create_table("persistent1")
            client1.store({"data": "test"})  # Stored in persistent1
            client1.create_table("persistent2")
            client1.use_table("persistent1")  # Switch back to persistent1
            client1.close()
            
            # Verify tables persist with second client
            client2 = ApexClient(dirpath=temp_dir)
            client2.create_table("default")
            tables = client2.list_tables()
            assert "persistent1" in tables
            assert "persistent2" in tables
            
            # Verify data persists
            client2.use_table("persistent1")
            count = client2.count_rows()
            assert count == 1
            
            client2.close()
    
    def test_table_operations_with_different_durability(self):
        """Test table operations with different durability levels"""
        durability_levels = ['fast', 'safe', 'max']
        
        for durability in durability_levels:
            with tempfile.TemporaryDirectory() as temp_dir:
                client = ApexClient(dirpath=temp_dir, durability=durability)
                client.create_table("default")
                
                # Test all table operations
                client.create_table(f"test_{durability}")
                client.store({"durability": durability})
                
                tables = client.list_tables()
                assert f"test_{durability}" in tables
                
                client.use_table("default")
                assert client.current_table == "default"
                
                client.drop_table(f"test_{durability}")
                tables = client.list_tables()
                assert f"test_{durability}" not in tables
                
                client.close()


class TestCreateTableWithSchema:
    """Test create_table with pre-defined schema parameter"""

    def test_schema_basic(self):
        """Test creating table with basic schema types"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("t", schema={
                "name": "string",
                "age": "int64",
                "score": "float64",
                "active": "bool",
            })

            # Schema should be visible immediately before any data is stored
            fields = client.list_fields()
            assert "name" in fields
            assert "age" in fields
            assert "score" in fields
            assert "active" in fields

            client.close()

    def test_schema_store_and_query(self):
        """Test storing and querying data in a schema-defined table"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("users", schema={
                "name": "string",
                "age": "int64",
                "score": "float64",
            })

            client.store([
                {"name": "Alice", "age": 30, "score": 95.5},
                {"name": "Bob", "age": 25, "score": 88.0},
            ])

            assert client.count_rows() == 2

            result = client.execute("SELECT * FROM users ORDER BY age")
            rows = result.to_dict()
            assert len(rows) == 2
            assert rows[0]["name"] == "Bob"
            assert rows[0]["age"] == 25
            assert rows[1]["name"] == "Alice"
            assert rows[1]["score"] == 95.5

            client.close()

    def test_schema_all_int_types(self):
        """Test all integer type aliases"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("ints", schema={
                "a": "int8",
                "b": "int16",
                "c": "int32",
                "d": "int64",
                "e": "integer",
            })
            fields = client.list_fields()
            assert len(fields) == 5
            client.close()

    def test_schema_all_uint_types(self):
        """Test all unsigned integer type aliases"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("uints", schema={
                "a": "uint8",
                "b": "uint16",
                "c": "uint32",
                "d": "uint64",
            })
            fields = client.list_fields()
            assert len(fields) == 4
            client.close()

    def test_schema_float_types(self):
        """Test float type aliases"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("floats", schema={
                "a": "float32",
                "b": "float64",
                "c": "float",
                "d": "double",
            })
            fields = client.list_fields()
            assert len(fields) == 4
            client.close()

    def test_schema_string_aliases(self):
        """Test string type aliases"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("strs", schema={
                "a": "string",
                "b": "str",
                "c": "text",
                "d": "varchar",
            })
            fields = client.list_fields()
            assert len(fields) == 4
            client.close()

    def test_schema_binary_type(self):
        """Test binary type aliases"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("bins", schema={
                "a": "binary",
                "b": "bytes",
            })
            fields = client.list_fields()
            assert len(fields) == 2
            client.close()

    def test_schema_case_insensitive(self):
        """Test that type names are case-insensitive"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("t", schema={
                "a": "STRING",
                "b": "Int64",
                "c": "FLOAT64",
                "d": "Bool",
            })
            fields = client.list_fields()
            assert len(fields) == 4
            client.close()

    def test_schema_invalid_type_raises(self):
        """Test that invalid type string raises ValueError"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            with pytest.raises((ValueError, OSError)):
                client.create_table("bad", schema={"x": "invalid_type"})
            client.close()

    def test_schema_empty_dict(self):
        """Test creating table with empty schema dict (same as no schema)"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("t", schema={})
            # No pre-defined fields
            fields = client.list_fields()
            assert len(fields) == 0

            # Should still be able to store data (schema inferred)
            client.store({"x": 1, "y": "hello"})
            assert client.count_rows() == 1
            client.close()

    def test_schema_without_schema_backward_compat(self):
        """Test that create_table without schema still works (backward compatible)"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("t")
            client.store({"a": 1, "b": "hello"})
            assert client.count_rows() == 1
            client.close()

    def test_schema_persistence(self):
        """Test that schema-defined table persists across sessions"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client1 = ApexClient(dirpath=temp_dir)
            client1.create_table("t", schema={
                "name": "string",
                "value": "int64",
            })
            client1.store([
                {"name": "A", "value": 1},
                {"name": "B", "value": 2},
            ])
            client1.flush()
            client1.close()

            # Reopen
            client2 = ApexClient(dirpath=temp_dir)
            client2.use_table("t")
            assert client2.count_rows() == 2
            result = client2.execute("SELECT * FROM t ORDER BY value")
            rows = result.to_dict()
            assert rows[0]["name"] == "A"
            assert rows[1]["value"] == 2
            client2.close()

    def test_schema_with_data_type_correctness(self):
        """Test that pre-defined types are respected"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("t", schema={
                "count": "int64",
                "ratio": "float64",
                "label": "string",
                "flag": "bool",
            })

            client.store({"count": 42, "ratio": 3.14, "label": "test", "flag": True})
            row = client.retrieve(1)
            assert row["count"] == 42
            assert abs(row["ratio"] - 3.14) < 0.001
            assert row["label"] == "test"
            assert row["flag"] is True
            client.close()

    def test_schema_columnar_store(self):
        """Test columnar store into schema-defined table"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("t", schema={
                "id": "int64",
                "name": "string",
                "score": "float64",
            })

            n = 1000
            client.store({
                "id": list(range(n)),
                "name": [f"item_{i}" for i in range(n)],
                "score": [float(i) for i in range(n)],
            })
            assert client.count_rows() == n

            result = client.execute("SELECT COUNT(*) FROM t")
            assert result.scalar() == n
            client.close()

    def test_schema_multiple_batches(self):
        """Test multiple store calls into schema-defined table"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("t", schema={
                "x": "int64",
                "y": "float64",
            })

            for i in range(5):
                client.store({"x": i, "y": float(i) * 0.5})

            assert client.count_rows() == 5

            result = client.execute("SELECT SUM(x), SUM(y) FROM t")
            row = result.first()
            assert row["SUM(x)"] == 10  # 0+1+2+3+4
            assert abs(row["SUM(y)"] - 5.0) < 0.001  # 0+0.5+1+1.5+2
            client.close()

    def test_schema_with_from_pandas(self):
        """Test from_pandas into a schema-defined table"""
        pytest.importorskip("pandas")
        import pandas as pd

        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("t", schema={
                "name": "string",
                "value": "int64",
            })

            df = pd.DataFrame({"name": ["A", "B", "C"], "value": [10, 20, 30]})
            client.from_pandas(df)
            assert client.count_rows() == 3
            client.close()

    def test_schema_with_durability_levels(self):
        """Test schema creation with different durability levels"""
        for durability in ["fast", "safe", "max"]:
            with tempfile.TemporaryDirectory() as temp_dir:
                client = ApexClient(dirpath=temp_dir, durability=durability)
                client.create_table("t", schema={"x": "int64", "y": "string"})
                client.store({"x": 1, "y": "hello"})
                client.flush()
                assert client.count_rows() == 1
                client.close()

    def test_schema_performance(self):
        """Test that schema-defined table is at least as fast as no-schema"""
        import time

        n = 100000
        data = {
            "id": list(range(n)),
            "value": [float(i) for i in range(n)],
            "name": [f"item_{i}" for i in range(n)],
        }
        schema = {"id": "int64", "value": "float64", "name": "string"}

        times_schema = []
        times_no_schema = []

        for _ in range(3):
            with tempfile.TemporaryDirectory() as td:
                c = ApexClient(dirpath=td)
                c.create_table("t", schema=schema)
                t0 = time.perf_counter()
                c.store(data)
                times_schema.append(time.perf_counter() - t0)
                c.close()

            with tempfile.TemporaryDirectory() as td:
                c = ApexClient(dirpath=td)
                c.create_table("t")
                t0 = time.perf_counter()
                c.store(data)
                times_no_schema.append(time.perf_counter() - t0)
                c.close()

        avg_schema = sum(times_schema) / len(times_schema)
        avg_no_schema = sum(times_no_schema) / len(times_no_schema)
        # Schema should not be more than 2x slower than no-schema (relaxed for CI variance)
        assert avg_schema < avg_no_schema * 2.0, \
            f"Schema ({avg_schema*1000:.1f}ms) significantly slower than no-schema ({avg_no_schema*1000:.1f}ms)"


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