lance-namespace-impls 14.0.2

Lance Namespace Implementations
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
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright The Lance Authors

use super::*;

#[tokio::test]
async fn test_root_namespace_operations() {
    let (namespace, _temp_dir) = create_test_namespace().await;

    // Test list_namespaces - should return empty list for root
    let mut request = ListNamespacesRequest::new();
    request.id = Some(vec![]);
    let result = namespace.list_namespaces(request).await;
    assert!(result.is_ok());
    assert_eq!(result.unwrap().namespaces.len(), 0);

    // Test describe_namespace - should succeed for root
    let mut request = DescribeNamespaceRequest::new();
    request.id = Some(vec![]);
    let result = namespace.describe_namespace(request).await;
    assert!(result.is_ok());

    // Test namespace_exists - root always exists
    let mut request = NamespaceExistsRequest::new();
    request.id = Some(vec![]);
    let result = namespace.namespace_exists(request).await;
    assert!(result.is_ok());

    // Test create_namespace - root cannot be created
    let mut request = CreateNamespaceRequest::new();
    request.id = Some(vec![]);
    let result = namespace.create_namespace(request).await;
    assert!(result.is_err());
    assert!(result.unwrap_err().to_string().contains("already exists"));

    // Test drop_namespace - root cannot be dropped
    let mut request = DropNamespaceRequest::new();
    request.id = Some(vec![]);
    let result = namespace.drop_namespace(request).await;
    assert!(result.is_err());
    assert!(
        result
            .unwrap_err()
            .to_string()
            .contains("cannot be dropped")
    );
}

#[tokio::test]
async fn test_non_root_namespace_operations() {
    let (namespace, _temp_dir) = create_test_namespace().await;

    // With manifest enabled (default), child namespaces are now supported
    // Test create_namespace for non-root - should succeed with manifest
    let mut request = CreateNamespaceRequest::new();
    request.id = Some(vec!["child".to_string()]);
    let result = namespace.create_namespace(request).await;
    assert!(
        result.is_ok(),
        "Child namespace creation should succeed with manifest enabled"
    );

    // Test namespace_exists for non-root - should exist after creation
    let mut request = NamespaceExistsRequest::new();
    request.id = Some(vec!["child".to_string()]);
    let result = namespace.namespace_exists(request).await;
    assert!(
        result.is_ok(),
        "Child namespace should exist after creation"
    );

    // Test drop_namespace for non-root - should succeed
    let mut request = DropNamespaceRequest::new();
    request.id = Some(vec!["child".to_string()]);
    let result = namespace.drop_namespace(request).await;
    assert!(
        result.is_ok(),
        "Child namespace drop should succeed with manifest enabled"
    );

    // Verify namespace no longer exists
    let mut request = NamespaceExistsRequest::new();
    request.id = Some(vec!["child".to_string()]);
    let result = namespace.namespace_exists(request).await;
    assert!(
        result.is_err(),
        "Child namespace should not exist after drop"
    );
}

#[tokio::test]
async fn test_config_custom_root() {
    let temp_dir = TempStdDir::default();
    let custom_path = temp_dir.join("custom");
    std::fs::create_dir(&custom_path).unwrap();

    let namespace = DirectoryNamespaceBuilder::new(custom_path.to_string_lossy().to_string())
        .build()
        .await
        .unwrap();

    // Create test IPC data
    let schema = create_test_schema();
    let ipc_data = create_test_ipc_data(&schema);

    // Create a table and verify location
    let mut request = CreateTableRequest::new();
    request.id = Some(vec!["test_table".to_string()]);

    let response = namespace
        .create_table(request, bytes::Bytes::from(ipc_data))
        .await
        .unwrap();

    assert!(response.location.unwrap().contains("custom"));
}

#[tokio::test]
async fn test_config_storage_options() {
    let temp_dir = TempStdDir::default();

    let namespace = DirectoryNamespaceBuilder::new(temp_dir.to_str().unwrap())
        .storage_option("option1", "value1")
        .storage_option("option2", "value2")
        .build()
        .await
        .unwrap();

    // Create test IPC data
    let schema = create_test_schema();
    let ipc_data = create_test_ipc_data(&schema);

    // Create a table and check storage options are included
    let mut request = CreateTableRequest::new();
    request.id = Some(vec!["test_table".to_string()]);

    let response = namespace
        .create_table(request, bytes::Bytes::from(ipc_data))
        .await
        .unwrap();

    let storage_options = response.storage_options.unwrap();
    assert_eq!(storage_options.get("option1"), Some(&"value1".to_string()));
    assert_eq!(storage_options.get("option2"), Some(&"value2".to_string()));
}

/// When no credential vendor is configured, `describe_table` and
/// `declare_table` must strip credential keys from storage options
/// while preserving non-credential config (region, endpoint, etc.).
#[tokio::test]
async fn test_no_storage_options_without_vendor() {
    use lance_namespace::models::DeclareTableRequest;

    let temp_dir = TempStdDir::default();

    // No manifest, no credential vendor, but storage options with credentials
    let namespace = DirectoryNamespaceBuilder::new(temp_dir.to_str().unwrap())
        .manifest_enabled(false)
        .storage_option("aws_access_key_id", "AKID")
        .storage_option("aws_secret_access_key", "SECRET")
        .storage_option("region", "us-east-1")
        .build()
        .await
        .unwrap();

    let schema = create_test_schema();
    let ipc_data = create_test_ipc_data(&schema);

    // create_table
    let mut create_req = CreateTableRequest::new();
    create_req.id = Some(vec!["t1".to_string()]);
    namespace
        .create_table(create_req, bytes::Bytes::from(ipc_data))
        .await
        .unwrap();

    // describe_table should not return storage options without a vendor
    let mut desc_req = DescribeTableRequest::new();
    desc_req.id = Some(vec!["t1".to_string()]);
    let resp = namespace.describe_table(desc_req).await.unwrap();
    assert!(resp.storage_options.is_none());

    // declare_table should not return storage options without a vendor
    let mut decl_req = DeclareTableRequest::new();
    decl_req.id = Some(vec!["t2".to_string()]);
    let resp = namespace.declare_table(decl_req).await.unwrap();
    assert!(resp.storage_options.is_none());
}

/// Same test with manifest mode enabled.
#[tokio::test]
async fn test_no_storage_options_without_vendor_manifest() {
    let temp_dir = TempStdDir::default();

    let namespace = DirectoryNamespaceBuilder::new(temp_dir.to_str().unwrap())
        .storage_option("aws_access_key_id", "AKID")
        .storage_option("aws_secret_access_key", "SECRET")
        .storage_option("region", "us-east-1")
        .build()
        .await
        .unwrap();

    let schema = create_test_schema();
    let ipc_data = create_test_ipc_data(&schema);

    let mut create_req = CreateTableRequest::new();
    create_req.id = Some(vec!["t1".to_string()]);
    namespace
        .create_table(create_req, bytes::Bytes::from(ipc_data))
        .await
        .unwrap();

    // describe_table through manifest should not return storage options without a vendor
    let mut desc_req = DescribeTableRequest::new();
    desc_req.id = Some(vec!["t1".to_string()]);
    let resp = namespace.describe_table(desc_req).await.unwrap();
    assert!(resp.storage_options.is_none());
}

#[tokio::test]
async fn test_from_properties_manifest_enabled() {
    let temp_dir = TempStdDir::default();

    let mut properties = HashMap::new();
    properties.insert("root".to_string(), temp_dir.to_str().unwrap().to_string());
    properties.insert("manifest_enabled".to_string(), "true".to_string());
    properties.insert("dir_listing_enabled".to_string(), "false".to_string());

    let builder = DirectoryNamespaceBuilder::from_properties(properties, None).unwrap();
    assert!(builder.manifest_enabled);
    assert!(!builder.dir_listing_enabled);

    let namespace = builder.build().await.unwrap();

    // Create test IPC data
    let schema = create_test_schema();
    let ipc_data = create_test_ipc_data(&schema);

    // Create a table
    let mut request = CreateTableRequest::new();
    request.id = Some(vec!["test_table".to_string()]);

    let response = namespace
        .create_table(request, bytes::Bytes::from(ipc_data))
        .await
        .unwrap();

    assert!(response.location.is_some());
}

#[tokio::test]
async fn test_from_properties_dir_listing_enabled() {
    let temp_dir = TempStdDir::default();

    let mut properties = HashMap::new();
    properties.insert("root".to_string(), temp_dir.to_str().unwrap().to_string());
    properties.insert("manifest_enabled".to_string(), "false".to_string());
    properties.insert("dir_listing_enabled".to_string(), "true".to_string());

    let builder = DirectoryNamespaceBuilder::from_properties(properties, None).unwrap();
    assert!(!builder.manifest_enabled);
    assert!(builder.dir_listing_enabled);

    let namespace = builder.build().await.unwrap();

    // Create test IPC data
    let schema = create_test_schema();
    let ipc_data = create_test_ipc_data(&schema);

    // Create a table
    let mut request = CreateTableRequest::new();
    request.id = Some(vec!["test_table".to_string()]);

    let response = namespace
        .create_table(request, bytes::Bytes::from(ipc_data))
        .await
        .unwrap();

    assert!(response.location.is_some());
}

#[tokio::test]
async fn test_from_properties_defaults() {
    let temp_dir = TempStdDir::default();

    let mut properties = HashMap::new();
    properties.insert("root".to_string(), temp_dir.to_str().unwrap().to_string());

    let builder = DirectoryNamespaceBuilder::from_properties(properties, None).unwrap();
    assert!(builder.manifest_enabled);
    assert!(builder.dir_listing_enabled);
    assert!(!builder.inline_optimization_enabled);
}

#[test]
fn test_builder_disables_inline_optimization_by_default() {
    let builder = DirectoryNamespaceBuilder::new("memory://");
    assert!(!builder.inline_optimization_enabled);
}

#[tokio::test]
async fn test_from_properties_with_storage_options() {
    let temp_dir = TempStdDir::default();

    let mut properties = HashMap::new();
    properties.insert("root".to_string(), temp_dir.to_str().unwrap().to_string());
    properties.insert("manifest_enabled".to_string(), "true".to_string());
    properties.insert("storage.region".to_string(), "us-west-2".to_string());
    properties.insert("storage.bucket".to_string(), "my-bucket".to_string());

    let builder = DirectoryNamespaceBuilder::from_properties(properties, None).unwrap();
    assert!(builder.manifest_enabled);
    assert!(builder.storage_options.is_some());

    let storage_options = builder.storage_options.unwrap();
    assert_eq!(
        storage_options.get("region"),
        Some(&"us-west-2".to_string())
    );
    assert_eq!(
        storage_options.get("bucket"),
        Some(&"my-bucket".to_string())
    );
}

#[tokio::test]
async fn test_various_arrow_types() {
    let (namespace, _temp_dir) = create_test_namespace().await;

    // Create schema with various types
    let fields = vec![
        JsonArrowField {
            name: "bool_col".to_string(),
            r#type: Box::new(JsonArrowDataType::new("bool".to_string())),
            nullable: true,
            metadata: None,
        },
        JsonArrowField {
            name: "int8_col".to_string(),
            r#type: Box::new(JsonArrowDataType::new("int8".to_string())),
            nullable: true,
            metadata: None,
        },
        JsonArrowField {
            name: "float64_col".to_string(),
            r#type: Box::new(JsonArrowDataType::new("float64".to_string())),
            nullable: true,
            metadata: None,
        },
        JsonArrowField {
            name: "binary_col".to_string(),
            r#type: Box::new(JsonArrowDataType::new("binary".to_string())),
            nullable: true,
            metadata: None,
        },
    ];

    let schema = JsonArrowSchema {
        fields,
        metadata: None,
    };

    // Create IPC data
    let ipc_data = create_test_ipc_data(&schema);

    let mut request = CreateTableRequest::new();
    request.id = Some(vec!["complex_table".to_string()]);

    let response = namespace
        .create_table(request, bytes::Bytes::from(ipc_data))
        .await
        .unwrap();

    assert!(response.location.is_some());
}

#[tokio::test]
async fn test_connect_dir() {
    let temp_dir = TempStdDir::default();

    let namespace = DirectoryNamespaceBuilder::new(temp_dir.to_str().unwrap())
        .build()
        .await
        .unwrap();

    // Test basic operation through the concrete type
    let mut request = ListTablesRequest::new();
    request.id = Some(vec![]);
    let response = namespace.list_tables(request).await.unwrap();
    assert_eq!(response.tables.len(), 0);
}

#[tokio::test]
async fn test_create_table_with_ipc_data() {
    use arrow::array::{Int32Array, StringArray};
    use arrow::ipc::writer::StreamWriter;

    let (namespace, _temp_dir) = create_test_namespace().await;

    // Create a schema with some fields
    let schema = create_test_schema();

    // Create some test data that matches the schema
    let arrow_schema = convert_json_arrow_schema(&schema).unwrap();
    let arrow_schema = Arc::new(arrow_schema);

    // Create a RecordBatch with actual data
    let id_array = Int32Array::from(vec![1, 2, 3]);
    let name_array = StringArray::from(vec!["Alice", "Bob", "Charlie"]);
    let batch = arrow::record_batch::RecordBatch::try_new(
        arrow_schema.clone(),
        vec![Arc::new(id_array), Arc::new(name_array)],
    )
    .unwrap();

    // Write the batch to an IPC stream
    let mut buffer = Vec::new();
    {
        let mut writer = StreamWriter::try_new(&mut buffer, &arrow_schema).unwrap();
        writer.write(&batch).unwrap();
        writer.finish().unwrap();
    }

    // Create table with the IPC data
    let mut request = CreateTableRequest::new();
    request.id = Some(vec!["test_table_with_data".to_string()]);

    let response = namespace
        .create_table(request, Bytes::from(buffer))
        .await
        .unwrap();

    assert_eq!(response.version, Some(1));
    assert!(
        response
            .location
            .unwrap()
            .contains("test_table_with_data.lance")
    );

    // Verify table exists
    let mut exists_request = TableExistsRequest::new();
    exists_request.id = Some(vec!["test_table_with_data".to_string()]);
    namespace.table_exists(exists_request).await.unwrap();
}

#[tokio::test]
async fn test_child_namespace_create_and_list() {
    let (namespace, _temp_dir) = create_test_namespace().await;

    // Create multiple child namespaces
    for i in 1..=3 {
        let mut create_req = CreateNamespaceRequest::new();
        create_req.id = Some(vec![format!("ns{}", i)]);
        let result = namespace.create_namespace(create_req).await;
        assert!(result.is_ok(), "Failed to create child namespace ns{}", i);
    }

    // List child namespaces
    let list_req = ListNamespacesRequest {
        id: Some(vec![]),
        ..Default::default()
    };
    let result = namespace.list_namespaces(list_req).await;
    assert!(result.is_ok());
    let namespaces = result.unwrap().namespaces;
    assert_eq!(namespaces.len(), 3);
    assert!(namespaces.contains(&"ns1".to_string()));
    assert!(namespaces.contains(&"ns2".to_string()));
    assert!(namespaces.contains(&"ns3".to_string()));
}

#[tokio::test]
async fn test_nested_namespace_hierarchy() {
    let (namespace, _temp_dir) = create_test_namespace().await;

    // Create parent namespace
    let mut create_req = CreateNamespaceRequest::new();
    create_req.id = Some(vec!["parent".to_string()]);
    namespace.create_namespace(create_req).await.unwrap();

    // Create nested children
    let mut create_req = CreateNamespaceRequest::new();
    create_req.id = Some(vec!["parent".to_string(), "child1".to_string()]);
    namespace.create_namespace(create_req).await.unwrap();

    let mut create_req = CreateNamespaceRequest::new();
    create_req.id = Some(vec!["parent".to_string(), "child2".to_string()]);
    namespace.create_namespace(create_req).await.unwrap();

    // List children of parent
    let list_req = ListNamespacesRequest {
        id: Some(vec!["parent".to_string()]),
        ..Default::default()
    };
    let result = namespace.list_namespaces(list_req).await;
    assert!(result.is_ok());
    let children = result.unwrap().namespaces;
    assert_eq!(children.len(), 2);
    assert!(children.contains(&"child1".to_string()));
    assert!(children.contains(&"child2".to_string()));

    // List root should only show parent
    let list_req = ListNamespacesRequest {
        id: Some(vec![]),
        ..Default::default()
    };
    let result = namespace.list_namespaces(list_req).await;
    assert!(result.is_ok());
    let root_namespaces = result.unwrap().namespaces;
    assert_eq!(root_namespaces.len(), 1);
    assert_eq!(root_namespaces[0], "parent");
}

#[tokio::test]
async fn test_table_in_child_namespace() {
    let (namespace, _temp_dir) = create_test_namespace().await;

    // Create child namespace
    let mut create_ns_req = CreateNamespaceRequest::new();
    create_ns_req.id = Some(vec!["test_ns".to_string()]);
    namespace.create_namespace(create_ns_req).await.unwrap();

    // Create table in child namespace
    let schema = create_test_schema();
    let ipc_data = create_test_ipc_data(&schema);
    let mut create_table_req = CreateTableRequest::new();
    create_table_req.id = Some(vec!["test_ns".to_string(), "table1".to_string()]);
    let result = namespace
        .create_table(create_table_req, bytes::Bytes::from(ipc_data))
        .await;
    assert!(result.is_ok(), "Failed to create table in child namespace");

    // List tables in child namespace
    let list_req = ListTablesRequest {
        id: Some(vec!["test_ns".to_string()]),
        ..Default::default()
    };
    let result = namespace.list_tables(list_req).await;
    assert!(result.is_ok());
    let tables = result.unwrap().tables;
    assert_eq!(tables.len(), 1);
    assert_eq!(tables[0], "table1");

    // Verify table exists
    let mut exists_req = TableExistsRequest::new();
    exists_req.id = Some(vec!["test_ns".to_string(), "table1".to_string()]);
    let result = namespace.table_exists(exists_req).await;
    assert!(result.is_ok());

    // Describe table in child namespace
    let mut describe_req = DescribeTableRequest::new();
    describe_req.id = Some(vec!["test_ns".to_string(), "table1".to_string()]);
    let result = namespace.describe_table(describe_req).await;
    assert!(result.is_ok());
    let response = result.unwrap();
    assert!(response.location.is_some());
}

/// Regression: a connection built before `__manifest` exists must still
/// resolve a child-namespaced table that a *different* connection registers
/// afterwards. Phalanx caches one DirectoryNamespace per db and the first op
/// on a fresh db is usually a read, so without a self-healing read path the
/// cached reader pins an empty manifest cell and every describe/exists/list
/// on the table reports "not found" forever -- even though `create_table`
/// reports it already exists. This is the geneva `__system$geneva_jobs`
/// open->create->open livelock.
#[tokio::test]
async fn test_read_self_heals_after_manifest_created_by_other_connection() {
    let temp_dir = TempStdDir::default();
    let root = temp_dir.to_str().unwrap();

    // Reader is built while no `__manifest` exists yet -> its read cell is
    // empty and, before the fix, stays empty forever.
    let reader = DirectoryNamespaceBuilder::new(root).build().await.unwrap();

    // A *separate* connection creates the child namespace + table, which
    // lazily creates `__manifest` and registers the entry.
    let writer = DirectoryNamespaceBuilder::new(root).build().await.unwrap();
    let mut create_ns_req = CreateNamespaceRequest::new();
    create_ns_req.id = Some(vec!["test_ns".to_string()]);
    writer.create_namespace(create_ns_req).await.unwrap();
    let ipc_data = create_test_ipc_data(&create_test_schema());
    let mut create_table_req = CreateTableRequest::new();
    create_table_req.id = Some(vec!["test_ns".to_string(), "table1".to_string()]);
    writer
        .create_table(create_table_req, bytes::Bytes::from(ipc_data))
        .await
        .unwrap();

    // The reader, though built before the manifest existed, must now resolve
    // the table on every read path (was TableNotFound before the fix).
    let mut describe_req = DescribeTableRequest::new();
    describe_req.id = Some(vec!["test_ns".to_string(), "table1".to_string()]);
    let resp = reader
        .describe_table(describe_req)
        .await
        .expect("describe_table must resolve a table registered after build");
    assert!(resp.location.is_some());

    let mut exists_req = TableExistsRequest::new();
    exists_req.id = Some(vec!["test_ns".to_string(), "table1".to_string()]);
    reader
        .table_exists(exists_req)
        .await
        .expect("table_exists must resolve a table registered after build");

    let list_req = ListTablesRequest {
        id: Some(vec!["test_ns".to_string()]),
        ..Default::default()
    };
    let tables = reader.list_tables(list_req).await.unwrap().tables;
    assert_eq!(tables, vec!["table1".to_string()]);
}

/// Migration mode promises manifest-first lookup even at the root, so a
/// reader built before `__manifest` existed must still resolve a
/// manifest-only alias (`registered_table` -> `external_table.lance`) that
/// dir-listing cannot produce. Before the read path self-healed in migration
/// mode, the root gate bypassed the manifest probe and fell back to
/// dir-listing, which sees `external_table` but never `registered_table` ->
/// permanent TableNotFound for every registration made after build.
#[tokio::test]
async fn test_migration_root_read_self_heals_registered_alias() {
    use lance_namespace::models::RegisterTableRequest;

    let temp_dir = TempStdDir::default();
    let temp_path = temp_dir.to_str().unwrap();

    // Reader built while the root is empty -> no `__manifest`, read cell
    // empty (and, before the fix, frozen empty forever).
    let reader = DirectoryNamespaceBuilder::new(temp_path)
        .dir_listing_enabled(true)
        .dir_listing_to_manifest_migration_enabled(true)
        .build()
        .await
        .unwrap();

    // A separate connection writes an external dataset and registers it in
    // the manifest under a *different* logical name -- an alias dir-listing
    // cannot resolve. This is what lazily creates `__manifest`.
    let writer = DirectoryNamespaceBuilder::new(temp_path)
        .dir_listing_enabled(true)
        .dir_listing_to_manifest_migration_enabled(true)
        .build()
        .await
        .unwrap();
    let ipc_data = create_test_ipc_data(&create_test_schema());
    let table_uri = format!("{}/external_table.lance", temp_path);
    let cursor = Cursor::new(ipc_data);
    let stream_reader = StreamReader::try_new(cursor, None).unwrap();
    let batches: Vec<_> = stream_reader
        .collect::<std::result::Result<Vec<_>, _>>()
        .unwrap();
    let schema = batches[0].schema();
    let batch_results: Vec<_> = batches.into_iter().map(Ok).collect();
    let batch_reader = RecordBatchIterator::new(batch_results, schema);
    Dataset::write(Box::new(batch_reader), &table_uri, None)
        .await
        .unwrap();
    let mut register_req = RegisterTableRequest::new("external_table.lance".to_string());
    register_req.id = Some(vec!["registered_table".to_string()]);
    writer.register_table(register_req).await.unwrap();

    // The reader, built before `__manifest` existed, must now resolve the
    // manifest-only alias on every read path.
    let mut describe_req = DescribeTableRequest::new();
    describe_req.id = Some(vec!["registered_table".to_string()]);
    reader
        .describe_table(describe_req)
        .await
        .expect("describe_table must resolve a manifest alias registered after build");

    let mut exists_req = TableExistsRequest::new();
    exists_req.id = Some(vec!["registered_table".to_string()]);
    reader
        .table_exists(exists_req)
        .await
        .expect("table_exists must resolve a manifest alias registered after build");

    let list_req = ListTablesRequest {
        id: Some(vec![]),
        ..Default::default()
    };
    let tables = reader.list_tables(list_req).await.unwrap().tables;
    assert!(
        tables.contains(&"registered_table".to_string()),
        "list_tables must include the manifest alias registered after build, got {:?}",
        tables
    );
}

#[tokio::test]
async fn test_multiple_tables_in_child_namespace() {
    let (namespace, _temp_dir) = create_test_namespace().await;

    // Create child namespace
    let mut create_ns_req = CreateNamespaceRequest::new();
    create_ns_req.id = Some(vec!["test_ns".to_string()]);
    namespace.create_namespace(create_ns_req).await.unwrap();

    // Create multiple tables
    let schema = create_test_schema();
    let ipc_data = create_test_ipc_data(&schema);
    for i in 1..=3 {
        let mut create_table_req = CreateTableRequest::new();
        create_table_req.id = Some(vec!["test_ns".to_string(), format!("table{}", i)]);
        namespace
            .create_table(create_table_req, bytes::Bytes::from(ipc_data.clone()))
            .await
            .unwrap();
    }

    // List tables
    let list_req = ListTablesRequest {
        id: Some(vec!["test_ns".to_string()]),
        ..Default::default()
    };
    let result = namespace.list_tables(list_req).await;
    assert!(result.is_ok());
    let tables = result.unwrap().tables;
    assert_eq!(tables.len(), 3);
    assert!(tables.contains(&"table1".to_string()));
    assert!(tables.contains(&"table2".to_string()));
    assert!(tables.contains(&"table3".to_string()));
}

#[tokio::test]
async fn test_drop_table_in_child_namespace() {
    let (namespace, _temp_dir) = create_test_namespace().await;

    // Create child namespace
    let mut create_ns_req = CreateNamespaceRequest::new();
    create_ns_req.id = Some(vec!["test_ns".to_string()]);
    namespace.create_namespace(create_ns_req).await.unwrap();

    // Create table
    let schema = create_test_schema();
    let ipc_data = create_test_ipc_data(&schema);
    let mut create_table_req = CreateTableRequest::new();
    create_table_req.id = Some(vec!["test_ns".to_string(), "table1".to_string()]);
    namespace
        .create_table(create_table_req, bytes::Bytes::from(ipc_data))
        .await
        .unwrap();

    // Drop table
    let mut drop_req = DropTableRequest::new();
    drop_req.id = Some(vec!["test_ns".to_string(), "table1".to_string()]);
    let result = namespace.drop_table(drop_req).await;
    assert!(result.is_ok(), "Failed to drop table in child namespace");

    // Verify table no longer exists
    let mut exists_req = TableExistsRequest::new();
    exists_req.id = Some(vec!["test_ns".to_string(), "table1".to_string()]);
    let result = namespace.table_exists(exists_req).await;
    assert!(result.is_err());
}

#[tokio::test]
async fn test_deeply_nested_namespace() {
    let (namespace, _temp_dir) = create_test_namespace().await;

    // Create deeply nested namespace hierarchy
    let mut create_req = CreateNamespaceRequest::new();
    create_req.id = Some(vec!["level1".to_string()]);
    namespace.create_namespace(create_req).await.unwrap();

    let mut create_req = CreateNamespaceRequest::new();
    create_req.id = Some(vec!["level1".to_string(), "level2".to_string()]);
    namespace.create_namespace(create_req).await.unwrap();

    let mut create_req = CreateNamespaceRequest::new();
    create_req.id = Some(vec![
        "level1".to_string(),
        "level2".to_string(),
        "level3".to_string(),
    ]);
    namespace.create_namespace(create_req).await.unwrap();

    // Create table in deeply nested namespace
    let schema = create_test_schema();
    let ipc_data = create_test_ipc_data(&schema);
    let mut create_table_req = CreateTableRequest::new();
    create_table_req.id = Some(vec![
        "level1".to_string(),
        "level2".to_string(),
        "level3".to_string(),
        "table1".to_string(),
    ]);
    let result = namespace
        .create_table(create_table_req, bytes::Bytes::from(ipc_data))
        .await;
    assert!(
        result.is_ok(),
        "Failed to create table in deeply nested namespace"
    );

    // Verify table exists
    let mut exists_req = TableExistsRequest::new();
    exists_req.id = Some(vec![
        "level1".to_string(),
        "level2".to_string(),
        "level3".to_string(),
        "table1".to_string(),
    ]);
    let result = namespace.table_exists(exists_req).await;
    assert!(result.is_ok());
}

#[tokio::test]
async fn test_namespace_with_properties() {
    let (namespace, _temp_dir) = create_test_namespace().await;

    // Create namespace with properties
    let mut properties = HashMap::new();
    properties.insert("owner".to_string(), "test_user".to_string());
    properties.insert("description".to_string(), "Test namespace".to_string());

    let mut create_req = CreateNamespaceRequest::new();
    create_req.id = Some(vec!["test_ns".to_string()]);
    create_req.properties = Some(properties.clone());
    namespace.create_namespace(create_req).await.unwrap();

    // Describe namespace and verify properties
    let describe_req = DescribeNamespaceRequest {
        id: Some(vec!["test_ns".to_string()]),
        ..Default::default()
    };
    let result = namespace.describe_namespace(describe_req).await;
    assert!(result.is_ok());
    let response = result.unwrap();
    assert!(response.properties.is_some());
    let props = response.properties.unwrap();
    assert_eq!(props.get("owner"), Some(&"test_user".to_string()));
    assert_eq!(
        props.get("description"),
        Some(&"Test namespace".to_string())
    );
}

#[tokio::test]
async fn test_cannot_drop_namespace_with_tables() {
    let (namespace, _temp_dir) = create_test_namespace().await;

    // Create namespace
    let mut create_ns_req = CreateNamespaceRequest::new();
    create_ns_req.id = Some(vec!["test_ns".to_string()]);
    namespace.create_namespace(create_ns_req).await.unwrap();

    // Create table in namespace
    let schema = create_test_schema();
    let ipc_data = create_test_ipc_data(&schema);
    let mut create_table_req = CreateTableRequest::new();
    create_table_req.id = Some(vec!["test_ns".to_string(), "table1".to_string()]);
    namespace
        .create_table(create_table_req, bytes::Bytes::from(ipc_data))
        .await
        .unwrap();

    // Try to drop namespace - should fail
    let mut drop_req = DropNamespaceRequest::new();
    drop_req.id = Some(vec!["test_ns".to_string()]);
    let result = namespace.drop_namespace(drop_req).await;
    assert!(
        result.is_err(),
        "Should not be able to drop namespace with tables"
    );
}

#[tokio::test]
async fn test_isolation_between_namespaces() {
    let (namespace, _temp_dir) = create_test_namespace().await;

    // Create two namespaces
    let mut create_req = CreateNamespaceRequest::new();
    create_req.id = Some(vec!["ns1".to_string()]);
    namespace.create_namespace(create_req).await.unwrap();

    let mut create_req = CreateNamespaceRequest::new();
    create_req.id = Some(vec!["ns2".to_string()]);
    namespace.create_namespace(create_req).await.unwrap();

    // Create table with same name in both namespaces
    let schema = create_test_schema();
    let ipc_data = create_test_ipc_data(&schema);

    let mut create_table_req = CreateTableRequest::new();
    create_table_req.id = Some(vec!["ns1".to_string(), "table1".to_string()]);
    namespace
        .create_table(create_table_req, bytes::Bytes::from(ipc_data.clone()))
        .await
        .unwrap();

    let mut create_table_req = CreateTableRequest::new();
    create_table_req.id = Some(vec!["ns2".to_string(), "table1".to_string()]);
    namespace
        .create_table(create_table_req, bytes::Bytes::from(ipc_data))
        .await
        .unwrap();

    // List tables in each namespace
    let list_req = ListTablesRequest {
        id: Some(vec!["ns1".to_string()]),
        page_token: None,
        limit: None,
        ..Default::default()
    };
    let result = namespace.list_tables(list_req).await.unwrap();
    assert_eq!(result.tables.len(), 1);
    assert_eq!(result.tables[0], "table1");

    let list_req = ListTablesRequest {
        id: Some(vec!["ns2".to_string()]),
        page_token: None,
        limit: None,
        ..Default::default()
    };
    let result = namespace.list_tables(list_req).await.unwrap();
    assert_eq!(result.tables.len(), 1);
    assert_eq!(result.tables[0], "table1");

    // Drop table in ns1 shouldn't affect ns2
    let mut drop_req = DropTableRequest::new();
    drop_req.id = Some(vec!["ns1".to_string(), "table1".to_string()]);
    namespace.drop_table(drop_req).await.unwrap();

    // Verify ns1 table is gone but ns2 table still exists
    let mut exists_req = TableExistsRequest::new();
    exists_req.id = Some(vec!["ns1".to_string(), "table1".to_string()]);
    assert!(namespace.table_exists(exists_req).await.is_err());

    let mut exists_req = TableExistsRequest::new();
    exists_req.id = Some(vec!["ns2".to_string(), "table1".to_string()]);
    assert!(namespace.table_exists(exists_req).await.is_ok());
}

#[tokio::test]
async fn test_migrate_directory_tables() {
    let temp_dir = TempStdDir::default();
    let temp_path = temp_dir.to_str().unwrap();

    // Step 1: Create tables in directory-only mode
    let dir_only_ns = DirectoryNamespaceBuilder::new(temp_path)
        .manifest_enabled(false)
        .dir_listing_enabled(true)
        .build()
        .await
        .unwrap();

    // Create some tables
    let schema = create_test_schema();
    let ipc_data = create_test_ipc_data(&schema);

    for i in 1..=3 {
        let mut create_req = CreateTableRequest::new();
        create_req.id = Some(vec![format!("table{}", i)]);
        dir_only_ns
            .create_table(create_req, bytes::Bytes::from(ipc_data.clone()))
            .await
            .unwrap();
    }

    drop(dir_only_ns);

    // Step 2: Create namespace with dual mode (manifest + directory listing)
    let dual_mode_ns = DirectoryNamespaceBuilder::new(temp_path)
        .manifest_enabled(true)
        .dir_listing_enabled(true)
        .build()
        .await
        .unwrap();

    // Before migration, tables should be visible (via directory listing fallback)
    let mut list_req = ListTablesRequest::new();
    list_req.id = Some(vec![]);
    let tables = dual_mode_ns.list_tables(list_req).await.unwrap().tables;
    assert_eq!(tables.len(), 3);

    // Run migration
    let migrated_count = dual_mode_ns.migrate().await.unwrap();
    assert_eq!(migrated_count, 3, "Should migrate all 3 tables");

    // Verify tables are now in manifest
    let mut list_req = ListTablesRequest::new();
    list_req.id = Some(vec![]);
    let tables = dual_mode_ns.list_tables(list_req).await.unwrap().tables;
    assert_eq!(tables.len(), 3);

    // Run migration again - should be idempotent
    let migrated_count = dual_mode_ns.migrate().await.unwrap();
    assert_eq!(
        migrated_count, 0,
        "Should not migrate already-migrated tables"
    );

    drop(dual_mode_ns);

    // Step 3: Create namespace with manifest-only mode
    let manifest_only_ns = DirectoryNamespaceBuilder::new(temp_path)
        .manifest_enabled(true)
        .dir_listing_enabled(false)
        .build()
        .await
        .unwrap();

    // Tables should still be accessible (now from manifest only)
    let mut list_req = ListTablesRequest::new();
    list_req.id = Some(vec![]);
    let tables = manifest_only_ns.list_tables(list_req).await.unwrap().tables;
    assert_eq!(tables.len(), 3);
    assert!(tables.contains(&"table1".to_string()));
    assert!(tables.contains(&"table2".to_string()));
    assert!(tables.contains(&"table3".to_string()));
}

#[tokio::test]
async fn test_migrate_without_manifest() {
    let temp_dir = TempStdDir::default();
    let temp_path = temp_dir.to_str().unwrap();

    // Create namespace without manifest
    let namespace = DirectoryNamespaceBuilder::new(temp_path)
        .manifest_enabled(false)
        .dir_listing_enabled(true)
        .build()
        .await
        .unwrap();

    // migrate() should return 0 when manifest is not enabled
    let migrated_count = namespace.migrate().await.unwrap();
    assert_eq!(migrated_count, 0);
}