lance 12.0.0

A columnar data format that is 100x faster than Parquet for random access.
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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright The Lance Authors

use crate::Dataset;
use crate::datafusion::LanceTableProvider;
use crate::dataset::scanner::validate_batch_size;
use crate::dataset::utils::SchemaAdapter;
use arrow_array::RecordBatch;
use datafusion::common::DataFusionError;
use datafusion::dataframe::DataFrame;
use datafusion::execution::SendableRecordBatchStream;
use datafusion::logical_expr::{Expr as LogicalExpr, LogicalPlan};
use datafusion::physical_plan::execute_stream;
use datafusion::prelude::{SessionConfig, SessionContext};
use datafusion::sql::{
    parser::Statement as DFStatement,
    sqlparser::ast::{Expr, Ident, SelectItem, SetExpr, Statement},
};
use futures::TryStreamExt;
use lance_core::{ROW_ADDR, ROW_ID, datatypes::BlobHandling};
use lance_datafusion::udf::register_functions;
use std::sync::Arc;

/// A SQL builder to prepare options for running SQL queries against a Lance dataset.
#[derive(Clone, Debug)]
pub struct SqlQueryBuilder {
    /// The dataset to run the SQL query
    pub(crate) dataset: Arc<Dataset>,

    /// The SQL query to run
    pub(crate) sql: String,

    /// the name of the table to register in the datafusion context
    pub(crate) table_name: String,

    /// If true, the query result will include the internal row id
    pub(crate) with_row_id: bool,

    /// If true, the query result will include the internal row address
    pub(crate) with_row_addr: bool,

    /// Override how blob columns are materialized for this query.
    pub(crate) blob_handling: Option<BlobHandling>,

    /// Override the maximum number of rows in each scan batch.
    pub(crate) batch_size: Option<usize>,

    /// Override the approximate maximum bytes in each scan batch.
    pub(crate) batch_size_bytes: Option<u64>,
}

impl SqlQueryBuilder {
    pub fn new(dataset: Dataset, sql: &str) -> Self {
        Self {
            dataset: Arc::new(dataset),
            sql: sql.to_string(),
            table_name: "dataset".to_string(),
            with_row_id: false,
            with_row_addr: false,
            blob_handling: None,
            batch_size: None,
            batch_size_bytes: None,
        }
    }

    /// The table name to register in the datafusion context.
    /// This is used to specify a "table name" for the dataset.
    /// So that you can run SQL queries against it.
    /// If not set, the default table name is "dataset".
    pub fn table_name(mut self, table_name: &str) -> Self {
        self.table_name = table_name.to_string();
        self
    }

    /// Specify if the query result should include the internal row id.
    /// If true, the query result will include an additional column named "_rowid".
    ///
    /// The column is appended only when output rows map one-to-one to dataset
    /// rows. For other queries (DISTINCT, GROUP BY, aggregates, ...) it is not
    /// appended, but can still be referenced explicitly in the SQL text.
    pub fn with_row_id(mut self, row_id: bool) -> Self {
        self.with_row_id = row_id;
        self
    }

    /// Specify if the query result should include the internal row address.
    /// If true, the query result will include an additional column named "_rowaddr".
    ///
    /// The column is appended only when output rows map one-to-one to dataset
    /// rows. For other queries (DISTINCT, GROUP BY, aggregates, ...) it is not
    /// appended, but can still be referenced explicitly in the SQL text.
    pub fn with_row_addr(mut self, row_addr: bool) -> Self {
        self.with_row_addr = row_addr;
        self
    }

    /// Override how blob columns are materialized for this query.
    ///
    /// When unset, the underlying dataset scan uses its default
    /// [`BlobHandling::BlobsDescriptions`] policy.
    pub fn blob_handling(mut self, blob_handling: BlobHandling) -> Self {
        self.blob_handling = Some(blob_handling);
        self
    }

    /// Set the maximum number of rows produced by each query batch.
    ///
    /// The batch size must be between 1 and [`u32::MAX`], inclusive.
    ///
    /// When [`Self::batch_size_bytes`] is also set, both limits apply and the
    /// one reached first determines the scan batch size.
    pub fn batch_size(mut self, batch_size: usize) -> Self {
        self.batch_size = Some(batch_size);
        self
    }

    /// Set the approximate maximum number of bytes produced by each scan batch.
    ///
    /// When [`Self::batch_size`] is also set, both limits apply and the one
    /// reached first determines the scan batch size.
    pub fn batch_size_bytes(mut self, batch_size_bytes: u64) -> Self {
        self.batch_size_bytes = Some(batch_size_bytes);
        self
    }

    pub async fn build(self) -> lance_core::Result<SqlQuery> {
        if let Some(batch_size) = self.batch_size {
            validate_batch_size(batch_size)?;
        }

        let ctx = if let Some(batch_size) = self.batch_size {
            SessionContext::new_with_config(SessionConfig::new().with_batch_size(batch_size))
        } else {
            SessionContext::new()
        };
        let row_id = self.with_row_id;
        let row_addr = self.with_row_addr;
        let mut provider = LanceTableProvider::new(self.dataset.clone(), row_id, row_addr);
        if let Some(blob_handling) = self.blob_handling {
            provider = provider.with_blob_handling(blob_handling);
        }
        if let Some(batch_size) = self.batch_size {
            provider = provider.with_batch_size(batch_size);
        }
        if let Some(batch_size_bytes) = self.batch_size_bytes {
            provider = provider.with_batch_size_bytes(batch_size_bytes);
        }
        ctx.register_table(self.table_name, Arc::new(provider))?;
        register_functions(&ctx);
        let state = ctx.state();
        let dialect = state.config_options().sql_parser.dialect;
        let statement = state
            .sql_to_statement(&self.sql, &dialect)
            .map_err(planning_error)?;
        let mut projected = statement.clone();
        let columns = [(self.with_row_id, ROW_ID), (self.with_row_addr, ROW_ADDR)];
        let plan = state
            .statement_to_plan(statement)
            .await
            .map_err(planning_error)?;
        let plan = if safe_to_inject_system_columns(&plan, &columns)
            && project_system_columns(&mut projected, &columns)
        {
            // Fall back to the original plan when the rewritten statement
            // fails to plan (e.g. another expression aliased to a system
            // column name), so the query still runs without the extra columns.
            state.statement_to_plan(projected).await.unwrap_or(plan)
        } else {
            plan
        };
        let df = ctx
            .execute_logical_plan(plan)
            .await
            .map_err(planning_error)?;
        Ok(SqlQuery::new(df))
    }
}

/// Returns true when appending the enabled system columns to the query's
/// top-level SELECT list is provably safe:
///
/// 1. Row identity: every output row maps to exactly one scanned source row
///    (whitelist of row-preserving operators; aggregates, DISTINCT, joins,
///    unions, ... collapse, duplicate, or synthesize rows), so the injection
///    cannot change the other columns' values or cardinality.
/// 2. Name lineage: no intermediate projection redefines an enabled system
///    column name (e.g. `SELECT (_rowid + 1) AS _rowid` in a subquery), so
///    the injected identifiers can only bind to the real scan columns.
fn safe_to_inject_system_columns(plan: &LogicalPlan, columns: &[(bool, &str)]) -> bool {
    match plan {
        LogicalPlan::TableScan(_) => true,
        LogicalPlan::Projection(projection) => {
            let shadows_system_column = projection
                .schema
                .fields()
                .iter()
                .zip(&projection.expr)
                .filter(|(field, _)| {
                    columns
                        .iter()
                        .any(|&(enabled, name)| enabled && field.name().as_str() == name)
                })
                .any(|(field, expr)| {
                    let mut expr = expr;
                    while let LogicalExpr::Alias(alias) = expr {
                        expr = &alias.expr;
                    }
                    !matches!(expr, LogicalExpr::Column(column) if &column.name == field.name())
                });
            !shadows_system_column && safe_to_inject_system_columns(&projection.input, columns)
        }
        LogicalPlan::Filter(_)
        | LogicalPlan::Sort(_)
        | LogicalPlan::Limit(_)
        | LogicalPlan::SubqueryAlias(_) => plan
            .inputs()
            .iter()
            .all(|input| safe_to_inject_system_columns(input, columns)),
        _ => false,
    }
}

/// Appends each enabled system column in `columns` to the statement's SELECT
/// list unless the query already projects it (directly or via a wildcard).
/// Returns true if the statement was modified.
///
/// Only rewrites top-level `SELECT` statements; the caller must separately
/// verify that the injection is safe (see [`safe_to_inject_system_columns`])
/// before planning the rewritten statement.
fn project_system_columns(statement: &mut DFStatement, columns: &[(bool, &str)]) -> bool {
    let DFStatement::Statement(statement) = statement else {
        return false;
    };
    let Statement::Query(query) = statement.as_mut() else {
        return false;
    };
    let SetExpr::Select(select) = query.body.as_mut() else {
        return false;
    };

    let mut changed = false;
    for &(enabled, name) in columns {
        if !enabled {
            continue;
        }
        let already_projected = select
            .projection
            .iter()
            .any(|item| projects_column(item, name));
        if already_projected {
            continue;
        }
        select
            .projection
            .push(SelectItem::UnnamedExpr(Expr::Identifier(Ident::new(name))));
        changed = true;
    }
    changed
}

/// Returns true if the SELECT item already yields the column `name`, either
/// as a bare/qualified identifier (e.g. `_rowid`, `t._rowid`) or through a
/// wildcard (`*`, `t.*`), so injecting it again would duplicate the column.
///
/// Expressions that merely reference the column (e.g. `_rowid + 1`, aliases)
/// intentionally don't count: they produce a different output column.
fn projects_column(item: &SelectItem, name: &str) -> bool {
    match item {
        SelectItem::Wildcard(_) | SelectItem::QualifiedWildcard(_, _) => true,
        SelectItem::UnnamedExpr(Expr::Identifier(ident)) => ident_matches(ident, name),
        SelectItem::UnnamedExpr(Expr::CompoundIdentifier(idents)) => idents
            .last()
            .is_some_and(|ident| ident_matches(ident, name)),
        _ => false,
    }
}

fn ident_matches(ident: &Ident, name: &str) -> bool {
    if ident.quote_style.is_some() {
        ident.value == name
    } else {
        ident.value.eq_ignore_ascii_case(name)
    }
}

pub struct SqlQuery {
    dataframe: DataFrame,
}

/// Classify a failure raised while DataFusion was building a plan.
///
/// A query that fails to plan is malformed input, but DataFusion does not
/// report these under a consistent error variant: `get_field` rejects an
/// unsupported base type with `exec_err!`, and the analyzer wraps that in
/// `Context`, so it would otherwise reach callers as an internal failure.
///
/// Only the execution category is re-classified. Every other category is either
/// already accurate or describes a failure the caller did not cause: an
/// unreachable object store is not a malformed query, and any other SQL would
/// have hit it too.
///
/// Errors Lance itself raised reach DataFusion through `External` and keep the
/// category they came with. Lance uses the execution category for its own
/// internal failures as well — a spill file that cannot be created, a poisoned
/// lock — and those are not bad input either.
fn planning_error(error: DataFusionError) -> lance_core::Error {
    let raised_by_lance = matches!(error.find_root(), DataFusionError::External(_));
    let error = lance_core::Error::from(error);
    if raised_by_lance {
        return error;
    }
    match error {
        error @ lance_core::Error::Execution { .. } => {
            lance_core::Error::invalid_input_source(Box::new(error))
        }
        error => error,
    }
}

impl SqlQuery {
    pub fn new(dataframe: DataFrame) -> Self {
        Self { dataframe }
    }

    pub async fn into_stream(self) -> lance_core::Result<SendableRecordBatchStream> {
        // Physical planning runs the analyzer, so a malformed query can still
        // fail here rather than in `SqlQueryBuilder::build`. Keep it separate
        // from execution so the two phases can be classified differently.
        let task_ctx = Arc::new(self.dataframe.task_ctx());
        let plan = self
            .dataframe
            .create_physical_plan()
            .await
            .map_err(planning_error)?;
        let exec_node = execute_stream(plan, task_ctx).map_err(lance_core::Error::from)?;
        let schema = exec_node.schema();
        if SchemaAdapter::requires_logical_conversion(&schema) {
            let adapter = SchemaAdapter::new(schema);
            Ok(adapter.to_logical_stream(exec_node))
        } else {
            Ok(exec_node)
        }
    }

    pub async fn into_batch_records(self) -> lance_core::Result<Vec<RecordBatch>> {
        self.into_stream()
            .await?
            .try_collect::<Vec<_>>()
            .await
            .map_err(|e| e.into())
    }

    pub fn into_dataframe(self) -> DataFrame {
        self.dataframe
    }
}

#[cfg(test)]
mod tests {
    use crate::utils::test::{DatagenExt, FragmentCount, FragmentRowCount, assert_string_matches};
    use crate::{BlobArrayBuilder, blob_field};
    use std::collections::HashMap;
    use std::sync::Arc;

    use super::planning_error;
    use crate::dataset::ReadParams;
    use crate::dataset::builder::DatasetBuilder;
    use crate::dataset::write::WriteParams;
    use crate::{Dataset, Error};
    use all_asserts::assert_true;
    use arrow_array::cast::AsArray;
    use arrow_array::types::{Int32Type, Int64Type, UInt64Type};
    use arrow_array::{Int32Array, RecordBatch, RecordBatchIterator, StringArray};
    use arrow_schema::Schema as ArrowSchema;
    use arrow_schema::{DataType, Field};
    use datafusion::common::DataFusionError;
    use lance_arrow::json::ARROW_JSON_EXT_NAME;
    use lance_arrow::{ARROW_EXT_NAME_KEY, SchemaExt};
    use lance_core::datatypes::BlobHandling;
    use lance_core::utils::tempfile::TempStrDir;
    use lance_datagen::{array, gen_batch};
    use lance_file::reader::FileReaderOptions;
    use lance_file::version::LanceFileVersion;
    use rstest::rstest;

    #[tokio::test]
    async fn test_sql_execute() {
        let ds = gen_batch()
            .col("x", array::step::<Int32Type>())
            .col("y", array::step_custom::<Int32Type>(0, 2))
            .into_dataset(
                "memory://test_sql_dataset",
                FragmentCount::from(10),
                FragmentRowCount::from(10),
            )
            .await
            .unwrap();

        let results = ds
            .sql("SELECT SUM(x) FROM foo WHERE y > 100")
            .table_name("foo")
            .build()
            .await
            .unwrap()
            .into_batch_records()
            .await
            .unwrap();
        pretty_assertions::assert_eq!(results.len(), 1);
        let results = results.into_iter().next().unwrap();
        pretty_assertions::assert_eq!(results.num_columns(), 1);
        pretty_assertions::assert_eq!(results.num_rows(), 1);
        // SUM(0..100) - SUM(0..50) = 3675
        pretty_assertions::assert_eq!(results.column(0).as_primitive::<Int64Type>().value(0), 3675);

        let results = ds
            .sql("SELECT x, y, _rowid, _rowaddr FROM foo where y > 100")
            .table_name("foo")
            .with_row_id(true)
            .with_row_addr(true)
            .build()
            .await
            .unwrap()
            .into_batch_records()
            .await
            .unwrap();
        let total_rows: usize = results.iter().map(|batch| batch.num_rows()).sum();
        let expect_rows = ds.count_rows(Some("y > 100".to_string())).await.unwrap();
        pretty_assertions::assert_eq!(total_rows, expect_rows);
        let results = results.into_iter().next().unwrap();
        pretty_assertions::assert_eq!(results.num_columns(), 4);
        assert_true!(results.column(2).as_primitive::<UInt64Type>().value(0) > 100);
        assert_true!(results.column(3).as_primitive::<UInt64Type>().value(0) > 100);
    }

    /// Requested system columns are appended after the user's columns when
    /// injection is safe, are not duplicated when already projected under any
    /// accepted spelling, and are skipped when a subquery alias shadows them
    /// (the injected identifiers would bind to the derived expressions and
    /// return arbitrary values as row metadata).
    #[rstest]
    #[case::plain("SELECT x FROM dataset", vec!["x", "_rowid", "_rowaddr"], vec![0, 1])]
    #[case::filter_sort_limit(
        "SELECT x FROM dataset WHERE x >= 0 ORDER BY x DESC LIMIT 2",
        vec!["x", "_rowid", "_rowaddr"],
        vec![1, 0]
    )]
    #[case::wildcard("SELECT * FROM dataset", vec!["x", "_rowid", "_rowaddr"], vec![0, 1])]
    #[case::already_projected(
        "SELECT x, _rowid, _rowaddr FROM dataset",
        vec!["x", "_rowid", "_rowaddr"],
        vec![0, 1]
    )]
    #[case::unquoted_uppercase(
        "SELECT x, _ROWID, _ROWADDR FROM dataset",
        vec!["x", "_rowid", "_rowaddr"],
        vec![0, 1]
    )]
    #[case::quoted(
        r#"SELECT x, "_rowid", "_rowaddr" FROM dataset"#,
        vec!["x", "_rowid", "_rowaddr"],
        vec![0, 1]
    )]
    #[case::table_qualified(
        "SELECT x, dataset._rowid, dataset._rowaddr FROM dataset",
        vec!["x", "_rowid", "_rowaddr"],
        vec![0, 1]
    )]
    #[case::expression_reference(
        "SELECT _rowid + 1 AS y FROM dataset",
        vec!["y", "_rowid", "_rowaddr"],
        vec![0, 1]
    )]
    #[case::system_columns_only("SELECT _rowid FROM dataset", vec!["_rowid", "_rowaddr"], vec![0, 1])]
    #[case::passthrough_subquery(
        "SELECT x FROM (SELECT x, _rowid, _rowaddr FROM dataset) s",
        vec!["x", "_rowid", "_rowaddr"],
        vec![0, 1]
    )]
    #[case::shadowed_subquery(
        "SELECT x FROM (SELECT x, (_rowid + 1) AS _rowid, (_rowaddr + 1) AS _rowaddr FROM dataset) s",
        vec!["x"],
        vec![]
    )]
    #[tokio::test]
    async fn test_sql_system_column_injection(
        #[case] sql: &str,
        #[case] expected_columns: Vec<&str>,
        #[case] expected_row_ids: Vec<u64>,
    ) {
        let ds = gen_batch()
            .col("x", array::step::<Int32Type>())
            .into_dataset(
                "memory://test_sql_system_column_injection",
                FragmentCount::from(1),
                FragmentRowCount::from(2),
            )
            .await
            .unwrap();

        let batches = ds
            .sql(sql)
            .with_row_id(true)
            .with_row_addr(true)
            .build()
            .await
            .unwrap()
            .into_batch_records()
            .await
            .unwrap();

        let batch = &batches[0];
        assert_eq!(batch.schema().field_names(), expected_columns);
        for name in ["_rowid", "_rowaddr"] {
            if expected_columns.contains(&name) {
                assert_eq!(
                    batch[name].as_primitive::<UInt64Type>().values().as_ref(),
                    expected_row_ids.as_slice(),
                    "unexpected values for column {name}",
                );
            }
        }
    }

    /// System columns must never be injected into queries whose output rows
    /// are not one-to-one with dataset rows: under GROUP BY ALL or DISTINCT
    /// the injected columns would become extra grouping/dedup keys and change
    /// the relational results.
    #[rstest]
    #[case::group_by_all("SELECT x % 1 AS k, COUNT(*) AS n FROM dataset GROUP BY ALL ORDER BY k")]
    #[case::group_by_expr("SELECT x % 1 AS k, COUNT(*) AS n FROM dataset GROUP BY k ORDER BY k")]
    #[case::distinct("SELECT DISTINCT x % 1 AS k FROM dataset ORDER BY k")]
    #[case::distinct_in_subquery(
        "SELECT k FROM (SELECT DISTINCT x % 1 AS k FROM dataset) ORDER BY k"
    )]
    #[case::bare_aggregate("SELECT COUNT(*) AS n FROM dataset")]
    #[tokio::test]
    async fn test_sql_system_columns_skip_cardinality_changing_queries(#[case] sql: &str) {
        let ds = gen_batch()
            .col("x", array::step::<Int32Type>())
            .into_dataset(
                "memory://test_sql_system_columns_cardinality",
                FragmentCount::from(1),
                FragmentRowCount::from(2),
            )
            .await
            .unwrap();

        let baseline = ds
            .sql(sql)
            .build()
            .await
            .unwrap()
            .into_batch_records()
            .await
            .unwrap();

        let with_system_columns = ds
            .sql(sql)
            .with_row_id(true)
            .with_row_addr(true)
            .build()
            .await
            .unwrap()
            .into_batch_records()
            .await
            .unwrap();

        pretty_assertions::assert_eq!(with_system_columns, baseline);
    }

    #[tokio::test]
    async fn test_sql_batch_size() {
        let ds = gen_batch()
            .col("x", array::step::<Int32Type>())
            .into_dataset(
                "memory://test_sql_batch_size",
                FragmentCount::from(2),
                FragmentRowCount::from(25),
            )
            .await
            .unwrap();

        let batches = ds
            .sql("SELECT x FROM dataset")
            .batch_size(7)
            .build()
            .await
            .unwrap()
            .into_batch_records()
            .await
            .unwrap();

        assert_eq!(batches.iter().map(RecordBatch::num_rows).sum::<usize>(), 50);
        assert!(batches.iter().all(|batch| batch.num_rows() <= 7));
    }

    #[tokio::test]
    async fn test_sql_rejects_invalid_batch_size() {
        let ds = gen_batch()
            .col("x", array::step::<Int32Type>())
            .into_dataset(
                "memory://test_sql_rejects_invalid_batch_size",
                FragmentCount::from(1),
                FragmentRowCount::from(3),
            )
            .await
            .unwrap();

        for batch_size in [0, u32::MAX as usize + 1] {
            let error = ds
                .sql("SELECT x FROM dataset")
                .batch_size(batch_size)
                .build()
                .await
                .err()
                .expect("invalid batch size should be rejected");
            assert!(matches!(error, Error::InvalidInput { .. }));
            assert!(
                error
                    .to_string()
                    .contains(&format!("batch_size must be between 1 and {}", u32::MAX))
            );
        }
    }

    #[tokio::test]
    async fn test_sql_batch_size_bytes_overrides_dataset_default() {
        let schema = Arc::new(ArrowSchema::new(vec![Field::new(
            "x",
            DataType::Int32,
            false,
        )]));
        let batch = RecordBatch::try_new(
            schema.clone(),
            vec![Arc::new(Int32Array::from_iter_values(0..1000))],
        )
        .unwrap();
        let test_dir = TempStrDir::default();
        Dataset::write(
            RecordBatchIterator::new([Ok(batch)], schema),
            &test_dir,
            Some(WriteParams {
                data_storage_version: Some(LanceFileVersion::V2_1),
                ..Default::default()
            }),
        )
        .await
        .unwrap();

        let dataset = DatasetBuilder::from_uri(&test_dir)
            .with_read_params(ReadParams {
                file_reader_options: Some(FileReaderOptions {
                    batch_size_bytes: Some(8_000),
                    ..Default::default()
                }),
                ..Default::default()
            })
            .load()
            .await
            .unwrap();

        let batches = dataset
            .sql("SELECT x FROM dataset")
            .batch_size_bytes(64)
            .build()
            .await
            .unwrap()
            .into_batch_records()
            .await
            .unwrap();

        assert_eq!(
            batches.iter().map(RecordBatch::num_rows).sum::<usize>(),
            1000
        );
        assert!(batches.iter().all(|batch| batch.num_rows() <= 16));
    }

    #[tokio::test]
    async fn test_sql_blob_all_binary() {
        let schema = Arc::new(ArrowSchema::new(vec![blob_field("blob", true)]));
        let mut blobs = BlobArrayBuilder::new(2);
        blobs.push_bytes(b"foo").unwrap();
        blobs.push_bytes(b"bar").unwrap();
        let batch = RecordBatch::try_new(schema.clone(), vec![blobs.finish().unwrap()]).unwrap();
        let dataset = Dataset::write(
            RecordBatchIterator::new([Ok(batch)], schema),
            "memory://test_sql_blob_all_binary",
            Some(WriteParams {
                data_storage_version: Some(LanceFileVersion::V2_3),
                ..Default::default()
            }),
        )
        .await
        .unwrap();

        let batches = dataset
            .sql("SELECT blob FROM dataset")
            .blob_handling(BlobHandling::AllBinary)
            .build()
            .await
            .unwrap()
            .into_batch_records()
            .await
            .unwrap();
        let blobs = batches[0].column(0).as_binary::<i64>();
        assert_eq!(blobs.value(0), b"foo");
        assert_eq!(blobs.value(1), b"bar");

        // Expressions over the blob column require the planner to see the
        // materialized LargeBinary type instead of the blob descriptor struct.
        let batches = dataset
            .sql("SELECT blob = X'666f6f' FROM dataset")
            .blob_handling(BlobHandling::AllBinary)
            .build()
            .await
            .unwrap()
            .into_batch_records()
            .await
            .unwrap();
        let is_foo = batches[0].column(0).as_boolean();
        assert!(is_foo.value(0));
        assert!(!is_foo.value(1));

        let batches = dataset
            .sql("SELECT blob, _rowid, _rowaddr FROM dataset")
            .with_row_id(true)
            .with_row_addr(true)
            .blob_handling(BlobHandling::AllBinary)
            .build()
            .await
            .unwrap()
            .into_batch_records()
            .await
            .unwrap();
        let batch = &batches[0];
        let blobs = batch.column(0).as_binary::<i64>();
        assert_eq!(blobs.value(0), b"foo");
        assert_eq!(blobs.value(1), b"bar");
        let row_ids = batch.column(1).as_primitive::<UInt64Type>();
        assert_eq!(row_ids.value(0), 0);
        assert_eq!(row_ids.value(1), 1);
        let row_addrs = batch.column(2).as_primitive::<UInt64Type>();
        assert_eq!(row_addrs.value(0), 0);
        assert_eq!(row_addrs.value(1), 1);
    }

    #[tokio::test]
    async fn test_sql_count() {
        let ds = gen_batch()
            .col("x", array::step::<Int32Type>())
            .col("y", array::step_custom::<Int32Type>(0, 2))
            .into_dataset(
                "memory://test_sql_dataset",
                FragmentCount::from(10),
                FragmentRowCount::from(10),
            )
            .await
            .unwrap();

        let results = ds
            .sql("SELECT COUNT(*) FROM foo")
            .table_name("foo")
            .build()
            .await
            .unwrap()
            .into_batch_records()
            .await
            .unwrap();
        pretty_assertions::assert_eq!(results.len(), 1);
        let results = results.into_iter().next().unwrap();
        pretty_assertions::assert_eq!(results.num_columns(), 1);
        pretty_assertions::assert_eq!(results.num_rows(), 1);
        pretty_assertions::assert_eq!(results.column(0).as_primitive::<Int64Type>().value(0), 100);

        let results = ds
            .sql("SELECT COUNT(*) FROM foo where y >= 100")
            .table_name("foo")
            .build()
            .await
            .unwrap()
            .into_batch_records()
            .await
            .unwrap();
        pretty_assertions::assert_eq!(results.len(), 1);
        let results = results.into_iter().next().unwrap();
        pretty_assertions::assert_eq!(results.num_columns(), 1);
        pretty_assertions::assert_eq!(results.num_rows(), 1);
        pretty_assertions::assert_eq!(results.column(0).as_primitive::<Int64Type>().value(0), 50);
    }

    #[tokio::test]
    async fn test_explain() {
        let ds = gen_batch()
            .col("x", array::step::<Int32Type>())
            .col("y", array::step_custom::<Int32Type>(0, 2))
            .into_dataset(
                "memory://test_sql_dataset",
                FragmentCount::from(10),
                FragmentRowCount::from(10),
            )
            .await
            .unwrap();

        let results = ds
            .sql("EXPLAIN SELECT * FROM foo where y >= 100")
            .table_name("foo")
            .build()
            .await
            .unwrap()
            .into_batch_records()
            .await
            .unwrap();
        let results = results.into_iter().next().unwrap();

        let plan = format!("{:?}", results);
        let expected_pattern = r#"...columns: [StringArray
[
  "logical_plan",
  "physical_plan",
], StringArray
[
  "TableScan: foo projection=[x, y], full_filters=[foo.y >= Int32(100)]",
  "ProjectionExec: expr=[x@0 as x, y@1 as y]\n  CooperativeExec\n    LanceRead: uri=test_sql_dataset/data, projection=[x, y], num_fragments=10, range_before=None, range_after=None, row_id=true, row_addr=false, full_filter=y >= Int32(100), refine_filter=y >= Int32(100)\n",
]], row_count: 2 }"#;
        assert_string_matches(&plan, expected_pattern).unwrap();
    }

    #[tokio::test]
    async fn test_analyze() {
        let ds = gen_batch()
            .col("x", array::step::<Int32Type>())
            .col("y", array::step_custom::<Int32Type>(0, 2))
            .into_dataset(
                "memory://test_sql_dataset",
                FragmentCount::from(10),
                FragmentRowCount::from(10),
            )
            .await
            .unwrap();

        let results = ds
            .sql("EXPLAIN ANALYZE SELECT * FROM foo where y >= 100")
            .table_name("foo")
            .build()
            .await
            .unwrap()
            .into_batch_records()
            .await
            .unwrap();
        let results = results.into_iter().next().unwrap();

        let plan = format!("{:?}", results);
        let expected_pattern = r#"...columns: [StringArray
[
  "Plan with Metrics",
], StringArray
[
  "ProjectionExec: expr=[x@0 as x, y@1 as y], metrics=[output_rows=50, elapsed_compute=...]\n  CooperativeExec, metrics=[]\n    LanceRead: uri=test_sql_dataset/data, projection=[x, y], num_fragments=..., range_before=None, range_after=None, row_id=true, row_addr=false, full_filter=y >= Int32(100), refine_filter=y >= Int32(100), metrics=[output_rows=..., elapsed_compute=..., fragments_scanned=..., ranges_scanned=..., rows_scanned=..., bytes_read=..., iops=..., requests=..., task_wait_time=...]\n",
]], row_count: 1 }"#;
        assert_string_matches(&plan, expected_pattern).unwrap();
    }

    #[tokio::test]
    async fn test_nested_json_access() {
        let json_rows = vec![
            Some(r#"{"user": {"profile": {"name": "Alice", "settings": {"theme": "dark"}}}}"#),
            Some(r#"{"user": {"profile": {"name": "Bob", "settings": {"theme": "light"}}}}"#),
        ];
        let json_array = StringArray::from(json_rows);
        let id_array = Int32Array::from(vec![1, 2]);

        let mut metadata = HashMap::new();
        metadata.insert(
            ARROW_EXT_NAME_KEY.to_string(),
            ARROW_JSON_EXT_NAME.to_string(),
        );

        let schema = Arc::new(ArrowSchema::new(vec![
            Field::new("id", DataType::Int32, false),
            Field::new("data", DataType::Utf8, true).with_metadata(metadata),
        ]));

        let batch = RecordBatch::try_new(
            schema.clone(),
            vec![Arc::new(id_array), Arc::new(json_array)],
        )
        .unwrap();

        let reader = RecordBatchIterator::new(vec![Ok(batch.clone())], schema.clone());
        let ds = Dataset::write(reader, "memory://test_nested_json_access", None)
            .await
            .unwrap();

        let results = ds
            .sql(
                "SELECT id FROM dataset WHERE \
                 json_get_string(json_get(json_get(data, 'user'), 'profile'), 'name') = 'Alice'",
            )
            .build()
            .await
            .unwrap()
            .into_batch_records()
            .await
            .unwrap();
        let batch = results.into_iter().next().unwrap();
        pretty_assertions::assert_eq!(batch.num_rows(), 1);
        pretty_assertions::assert_eq!(batch.num_columns(), 1);
        pretty_assertions::assert_eq!(batch.column(0).as_primitive::<Int32Type>().value(0), 1);

        let results = ds
            .sql(
                "SELECT id FROM dataset WHERE \
                 json_extract(data, '$.user.profile.settings.theme') = '\"dark\"'",
            )
            .build()
            .await
            .unwrap()
            .into_batch_records()
            .await
            .unwrap();
        let batch = results.into_iter().next().unwrap();
        pretty_assertions::assert_eq!(batch.num_rows(), 1);
        pretty_assertions::assert_eq!(batch.num_columns(), 1);
        pretty_assertions::assert_eq!(batch.column(0).as_primitive::<Int32Type>().value(0), 1);
    }

    /// A malformed query is user error and must surface as [`Error::InvalidInput`]
    /// so that bindings and servers can map it to a client error rather than a
    /// generic failure.
    ///
    /// The cases differ in how DataFusion reports them: a syntax error is a
    /// `SQL` error, an unknown function is a `Plan` error wrapped in
    /// `Diagnostic`, and a subscript on a non-struct column is `exec_err!`
    /// wrapped in `Context`. Only the first is classified as user input on its
    /// own; the other two would otherwise read as internal failures.
    #[rstest]
    #[case::syntax_error("SELEC id FROM dataset", "found: SELEC at")]
    #[case::unknown_function(
        "SELECT id FROM dataset WHERE no_such_function(data) = 'Alice'",
        "no_such_function"
    )]
    #[case::subscript_on_json_column(
        "SELECT id FROM dataset WHERE data['user'] = 'Alice'",
        "Cannot access field"
    )]
    #[tokio::test]
    async fn test_sql_malformed_query_is_invalid_input(
        #[case] sql: &str,
        #[case] expected_message: &str,
    ) {
        let mut metadata = HashMap::new();
        metadata.insert(
            ARROW_EXT_NAME_KEY.to_string(),
            ARROW_JSON_EXT_NAME.to_string(),
        );
        let schema = Arc::new(ArrowSchema::new(vec![
            Field::new("id", DataType::Int32, false),
            Field::new("data", DataType::Utf8, true).with_metadata(metadata),
        ]));
        let batch = RecordBatch::try_new(
            schema.clone(),
            vec![
                Arc::new(Int32Array::from(vec![1])),
                Arc::new(StringArray::from(vec![Some(r#"{"user": "Alice"}"#)])),
            ],
        )
        .unwrap();
        let reader = RecordBatchIterator::new(vec![Ok(batch)], schema.clone());
        let ds = Dataset::write(reader, "memory://test_sql_malformed_query", None)
            .await
            .unwrap();

        let result = async { ds.sql(sql).build().await?.into_batch_records().await }.await;
        let Err(error) = result else {
            panic!("expected `{sql}` to fail");
        };
        assert!(
            matches!(error, Error::InvalidInput { .. }),
            "expected InvalidInput, got {error:?}"
        );
        assert!(
            error.to_string().contains(expected_message),
            "expected the message to name {expected_message:?}, got: {error}"
        );
    }

    /// `LanceTableProvider::scan` runs during physical planning and reports
    /// every Lance failure as `DataFusionError::External`. Lance raises
    /// [`Error::Execution`] for its own internal failures, which is the one
    /// category `planning_error` re-classifies, so these must be recognized as
    /// Lance's own and left alone rather than blamed on the query.
    #[test]
    fn test_planning_error_keeps_lance_error_category() {
        let df_error = DataFusionError::Context(
            "while scanning".to_string(),
            Box::new(DataFusionError::from(Error::execution(
                "failed to create spill file",
            ))),
        );

        match planning_error(df_error) {
            Error::Execution { message, .. } => assert!(
                message.contains("failed to create spill file"),
                "expected the original message, got: {message}"
            ),
            other => panic!("expected the original execution error, got {other:?}"),
        }
    }

    /// A failure that is neither the caller's fault nor Lance's own — DataFusion
    /// hitting a resource limit while planning — must not be reported as a
    /// malformed query just because it surfaced during planning.
    #[test]
    fn test_planning_error_keeps_internal_datafusion_error() {
        let df_error = DataFusionError::Context(
            "while planning".to_string(),
            Box::new(DataFusionError::ResourcesExhausted(
                "failed to allocate memory".to_string(),
            )),
        );

        let error = planning_error(df_error);
        assert!(
            !matches!(error, Error::InvalidInput { .. }),
            "expected the error not to be blamed on the query, got {error:?}"
        );
    }
}