nautilus-persistence 0.63.0

Data persistence and storage for the Nautilus trading engine
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
// -------------------------------------------------------------------------------------------------
//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
//  https://nautechsystems.io
//
//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
//  You may not use this file except in compliance with the License.
//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
// -------------------------------------------------------------------------------------------------

use std::{
    sync::{
        Arc,
        atomic::{AtomicBool, Ordering},
    },
    vec::IntoIter,
};

use ahash::{AHashMap, AHashSet};
use datafusion::{
    arrow::record_batch::RecordBatch,
    error::{DataFusionError, Result},
    logical_expr::expr::Sort,
    physical_plan::SendableRecordBatchStream,
    prelude::*,
};
use futures::{Stream, StreamExt};
use nautilus_common::live::get_runtime;
use nautilus_core::UnixNanos;
use nautilus_model::data::{Data, HasTsInit};
use nautilus_serialization::arrow::{
    DataStreamingError, DecodeDataFromRecordBatch, EncodeToRecordBatch, EncodingError, WriteStream,
};
use object_store::ObjectStore;
use parking_lot::Mutex;
use url::Url;

use super::{
    compare::Compare,
    kmerge_batch::{EagerStream, ElementBatchIter, KMerge},
};

#[derive(Debug, Default)]
pub struct TsInitComparator;

impl<I> Compare<ElementBatchIter<I, Data>> for TsInitComparator
where
    I: Iterator<Item = IntoIter<Data>>,
{
    fn compare(
        &self,
        l: &ElementBatchIter<I, Data>,
        r: &ElementBatchIter<I, Data>,
    ) -> std::cmp::Ordering {
        // Max heap ordering must be reversed
        l.item.ts_init().cmp(&r.item.ts_init()).reverse()
    }
}

/// Represents a failure raised by a query's underlying data stream.
#[derive(Debug, thiserror::Error)]
pub enum QueryError {
    /// The record batch stream returned an error.
    #[error("Record batch stream error: {0}")]
    Stream(#[from] DataFusionError),
    /// A record batch could not be decoded into Nautilus data.
    #[error("Record batch decode error: {0}")]
    Decode(#[from] EncodingError),
}

/// Holds the first failure observed by any of a query's batch streams.
///
/// `failed` keeps the common path off the mutex, because [`QueryResult::next`] consults the slot
/// once per merged item while loading a catalog.
#[derive(Default)]
struct ErrorSlot {
    failed: AtomicBool,
    error: Mutex<Option<QueryError>>,
}

impl ErrorSlot {
    fn record(&self, error: QueryError) {
        self.error.lock().get_or_insert(error);
        self.failed.store(true, Ordering::Release);
    }

    fn failed(&self) -> bool {
        self.failed.load(Ordering::Acquire)
    }

    fn take(&self) -> Option<QueryError> {
        self.error.lock().take()
    }
}

/// Iterates the merged data of every registered query stream in ascending `ts_init` order.
///
/// A batch stream that fails stops contributing data and the failure is yielded as an error, so a
/// failed query can never be mistaken for an exhausted one.
pub struct QueryResult {
    merge: KMerge<BatchStream, Data, TsInitComparator>,
    error: Arc<ErrorSlot>,
}

impl QueryResult {
    /// Discards the remaining data streams without draining them.
    pub fn clear(&mut self) {
        self.merge.clear();
    }
}

impl Iterator for QueryResult {
    // Spelled out because `Result` is the DataFusion alias in this module
    type Item = std::result::Result<Data, QueryError>;

    fn next(&mut self) -> Option<Self::Item> {
        // A failure recorded while merging the previous item ends the query, so drop the
        // remaining streams rather than returning data from an incomplete result.
        if self.error.failed()
            && let Some(e) = self.error.take()
        {
            self.clear();
            return Some(Err(e));
        }

        match self.merge.next() {
            Some(item) => Some(Ok(item)),
            // Always taken, so a failure recorded on the final poll cannot read as exhaustion
            None => self.error.take().map(Err),
        }
    }
}

/// Provides a DataFusion session and registers DataFusion queries.
///
/// The session is used to register data sources and make queries on them. A
/// query returns a Chunk of Arrow records. It is decoded and converted into
/// a Vec of data by types that implement [`DecodeDataFromRecordBatch`].
#[cfg_attr(
    feature = "python",
    pyo3::pyclass(module = "nautilus_trader.persistence", unsendable)
)]
#[cfg_attr(
    feature = "python",
    pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.persistence")
)]
pub struct DataBackendSession {
    pub chunk_size: usize,
    pub runtime: tokio::runtime::Handle,
    session_ctx: SessionContext,
    batch_streams: Vec<BatchStream>,
    error: Arc<ErrorSlot>,
    registered_tables: AHashSet<String>,
}

impl DataBackendSession {
    /// Creates a new [`DataBackendSession`] instance.
    #[must_use]
    pub fn new(chunk_size: usize) -> Self {
        let session_cfg = SessionConfig::new()
            .set_str("datafusion.optimizer.repartition_file_scans", "false")
            .set_str("datafusion.optimizer.prefer_existing_sort", "true");
        let session_ctx = SessionContext::new_with_config(session_cfg);
        Self {
            session_ctx,
            batch_streams: Vec::default(),
            error: Arc::default(),
            chunk_size,
            runtime: get_runtime().handle().clone(),
            registered_tables: AHashSet::new(),
        }
    }

    /// Register an object store with the session context
    pub fn register_object_store(&mut self, url: &Url, object_store: Arc<dyn ObjectStore>) {
        self.session_ctx.register_object_store(url, object_store);
    }

    /// Register an object store with the session context from a URI with optional storage options.
    ///
    /// # Errors
    ///
    /// Returns an error if the object store URI cannot be normalized or the backend
    /// cannot be created.
    pub fn register_object_store_from_uri(
        &mut self,
        uri: &str,
        storage_options: Option<AHashMap<String, String>>,
    ) -> anyhow::Result<()> {
        let location =
            crate::parquet::create_object_store_location_from_path(uri, storage_options)?;

        if let Some(root_url) = location.store_root_url().cloned() {
            self.register_object_store(&root_url, location.object_store);
        }

        Ok(())
    }

    /// Writes encoded data to a streaming sink.
    ///
    /// # Errors
    ///
    /// Returns an error if Arrow encoding or stream writing fails.
    pub fn write_data<T: EncodeToRecordBatch>(
        data: &[T],
        metadata: &AHashMap<String, String>,
        stream: &mut dyn WriteStream,
    ) -> Result<(), DataStreamingError> {
        // Convert AHashMap to HashMap for Arrow compatibility
        let metadata: std::collections::HashMap<String, String> = metadata
            .iter()
            .map(|(k, v)| (k.clone(), v.clone()))
            .collect();
        let record_batch = T::encode_batch(&metadata, data)?;
        stream.write(&record_batch)?;
        Ok(())
    }

    /// Registers a Parquet file and adds a batch stream for decoding.
    ///
    /// The caller must specify `T` to indicate the kind of data expected. `table_name` is
    /// the logical name for queries; `file_path` is the Parquet path; `sql_query` defaults
    /// to `SELECT * FROM {table_name} ORDER BY ts_init` if `None`.
    ///
    /// When `custom_type_name` is `Some`, it is merged into each batch's schema metadata
    /// before decoding (as `type_name`). Use this for custom data when Parquet/DataFusion
    /// does not preserve schema metadata so the decoder can look up the type in the registry.
    ///
    /// The file data must be ordered by the `ts_init` in ascending order for this
    /// to work correctly.
    ///
    /// # Errors
    ///
    /// Returns an error if parquet registration, SQL planning, stream execution, or
    /// data decoding setup fails.
    pub fn add_file<T>(
        &mut self,
        table_name: &str,
        file_path: &str,
        sql_query: Option<&str>,
        custom_type_name: Option<&str>,
    ) -> Result<()>
    where
        T: DecodeDataFromRecordBatch,
    {
        // Check if table is already registered to avoid duplicates
        let is_new_table = !self.registered_tables.contains(table_name);

        if is_new_table {
            // Register the table only if it doesn't exist
            let parquet_options = ParquetReadOptions::<'_> {
                skip_metadata: Some(false),
                file_sort_order: vec![vec![Sort {
                    expr: col("ts_init"),
                    asc: true,
                    nulls_first: false,
                }]],
                ..Default::default()
            };
            super::block_on(
                &self.runtime,
                self.session_ctx
                    .register_parquet(table_name, file_path, parquet_options),
            )?;

            self.registered_tables.insert(table_name.to_string());

            // Only add batch stream for newly registered tables to avoid duplicates
            let default_query = format!("SELECT * FROM {table_name} ORDER BY ts_init");
            let sql_query = sql_query.unwrap_or(&default_query);
            let query = super::block_on(&self.runtime, self.session_ctx.sql(sql_query))?;
            let batch_stream = super::block_on(&self.runtime, query.execute_stream())?;
            self.add_batch_stream::<T>(batch_stream, custom_type_name.map(String::from));
        }

        Ok(())
    }

    /// Registers a Parquet file and executes a query, returning the raw record batches.
    ///
    /// # Errors
    ///
    /// Returns an error if parquet registration, SQL planning, stream execution, or
    /// batch collection fails.
    pub fn collect_query_batches(
        &mut self,
        table_name: &str,
        file_path: &str,
        sql_query: Option<&str>,
    ) -> Result<Vec<RecordBatch>> {
        if !self.registered_tables.contains(table_name) {
            let parquet_options = ParquetReadOptions::<'_> {
                skip_metadata: Some(false),
                file_sort_order: vec![vec![Sort {
                    expr: col("ts_init"),
                    asc: true,
                    nulls_first: false,
                }]],
                ..Default::default()
            };
            super::block_on(
                &self.runtime,
                self.session_ctx
                    .register_parquet(table_name, file_path, parquet_options),
            )?;

            self.registered_tables.insert(table_name.to_string());
        }

        let default_query = format!("SELECT * FROM {table_name} ORDER BY ts_init");
        let sql_query = sql_query.unwrap_or(&default_query);
        let query = super::block_on(&self.runtime, self.session_ctx.sql(sql_query))?;
        let mut batch_stream = super::block_on(&self.runtime, query.execute_stream())?;

        super::block_on(&self.runtime, async {
            let mut batches = Vec::new();
            while let Some(batch) = batch_stream.next().await {
                batches.push(batch?);
            }
            Ok::<_, datafusion::error::DataFusionError>(batches)
        })
    }

    fn add_batch_stream<T>(
        &mut self,
        stream: SendableRecordBatchStream,
        custom_type_name: Option<String>,
    ) where
        T: DecodeDataFromRecordBatch,
    {
        self.batch_streams.push(BatchStream {
            inner: EagerStream::from_stream_with_runtime(
                decode_batches::<T>(stream, custom_type_name),
                self.runtime.clone(),
            ),
            error: Arc::clone(&self.error),
        });
    }

    // Consumes the registered queries and returns a [`QueryResult].
    // Passes the output of the query though the a KMerge which sorts the
    // queries in ascending order of `ts_init`.
    // QueryResult is an iterator that return Vec<Data>.
    pub fn get_query_result(&mut self) -> QueryResult {
        let mut merge: KMerge<_, _, _> = KMerge::new(TsInitComparator);

        self.batch_streams
            .drain(..)
            .for_each(|batch_stream| merge.push_iter(batch_stream));

        QueryResult {
            merge,
            error: std::mem::take(&mut self.error),
        }
    }

    /// Clears all registered tables and batch streams.
    ///
    /// This is useful when the underlying files have changed and we need to
    /// re-register tables with updated data.
    pub fn clear_registered_tables(&mut self) {
        self.registered_tables.clear();
        self.batch_streams.clear();
        self.error = Arc::default();

        // Create a new session context to completely reset the DataFusion state
        let session_cfg = SessionConfig::new()
            .set_str("datafusion.optimizer.repartition_file_scans", "false")
            .set_str("datafusion.optimizer.prefer_existing_sort", "true");
        self.session_ctx = SessionContext::new_with_config(session_cfg);
    }
}

type BatchResult = std::result::Result<IntoIter<Data>, QueryError>;

/// Decodes each record batch, yielding the first failure and then ending the stream.
///
/// A record batch stream that has returned an error gives no guarantee about being polled again,
/// and a panic in the producer task would abort the process under `panic = "abort"`.
fn decode_batches<T>(
    stream: SendableRecordBatchStream,
    custom_type_name: Option<String>,
) -> impl Stream<Item = BatchResult> + Send + 'static
where
    T: DecodeDataFromRecordBatch,
{
    futures::stream::unfold(
        (stream, custom_type_name, false),
        |(mut stream, custom_type_name, failed)| async move {
            if failed {
                return None;
            }

            let batch = decode_batch::<T>(stream.next().await?, custom_type_name.as_deref());
            let failed = batch.is_err();

            Some((batch, (stream, custom_type_name, failed)))
        },
    )
}

fn decode_batch<T>(
    result: std::result::Result<RecordBatch, DataFusionError>,
    custom_type_name: Option<&str>,
) -> BatchResult
where
    T: DecodeDataFromRecordBatch,
{
    let batch = result?;
    let mut metadata: std::collections::HashMap<String, String> = batch.schema().metadata().clone();

    if let Some(type_name) = custom_type_name {
        metadata.insert("type_name".to_string(), type_name.to_string());
    }

    Ok(T::decode_data_batch(&metadata, batch)?.into_iter())
}

/// Feeds decoded batches to the merge and diverts a failure to the shared error slot.
///
/// The merge orders items by `ts_init`, so a failure cannot travel with the data. Recording it
/// here ends this stream for the merge while leaving the remaining streams intact, and lets
/// [`QueryResult`] report the failure instead of exhaustion.
struct BatchStream {
    inner: EagerStream<BatchResult>,
    error: Arc<ErrorSlot>,
}

impl Iterator for BatchStream {
    type Item = IntoIter<Data>;

    fn next(&mut self) -> Option<Self::Item> {
        match self.inner.next()? {
            Ok(batch) => Some(batch),
            Err(e) => {
                self.error.record(e);
                None
            }
        }
    }
}

#[must_use]
pub fn build_query(
    table: &str,
    start: Option<UnixNanos>,
    end: Option<UnixNanos>,
    where_clause: Option<&str>,
) -> String {
    let mut conditions = Vec::new();

    // Add where clause if provided
    if let Some(clause) = where_clause {
        conditions.push(clause.to_string());
    }

    // Add start condition if provided
    if let Some(start_ts) = start {
        conditions.push(format!("ts_init >= {start_ts}"));
    }

    // Add end condition if provided
    if let Some(end_ts) = end {
        conditions.push(format!("ts_init <= {end_ts}"));
    }

    // Build base query
    let mut query = format!("SELECT * FROM {table}");

    // Add WHERE clause if there are conditions
    if !conditions.is_empty() {
        query.push_str(" WHERE ");
        query.push_str(&conditions.join(" AND "));
    }

    // Add ORDER BY clause
    query.push_str(" ORDER BY ts_init");

    query
}

#[cfg_attr(
    feature = "python",
    pyo3::pyclass(module = "nautilus_trader.persistence", unsendable)
)]
#[cfg_attr(
    feature = "python",
    pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.persistence")
)]
pub struct DataQueryResult {
    pub result: QueryResult,
    pub acc: Vec<Data>,
    pub size: usize,
}

impl DataQueryResult {
    /// Creates a new [`DataQueryResult`] instance.
    #[must_use]
    pub const fn new(result: QueryResult, size: usize) -> Self {
        Self {
            result,
            acc: Vec::new(),
            size,
        }
    }
}

impl Iterator for DataQueryResult {
    // An empty chunk signals exhaustion, so a failure must be reported as an error
    type Item = std::result::Result<Vec<Data>, QueryError>;

    fn next(&mut self) -> Option<Self::Item> {
        // Poll at least once, since a zero chunk size would return an empty chunk without ever
        // consulting the query, hiding a failure behind the exhaustion signal.
        let size = self.size.max(1);

        for _ in 0..size {
            match self.result.next() {
                Some(Ok(item)) => self.acc.push(item),
                Some(Err(e)) => {
                    self.acc.clear();
                    return Some(Err(e));
                }
                None => break,
            }
        }

        // TODO: consider using drain here if perf is unchanged
        // Some(self.acc.drain(0..).collect())
        let mut acc: Vec<Data> = Vec::new();
        std::mem::swap(&mut acc, &mut self.acc);
        Some(Ok(acc))
    }
}

impl Drop for DataQueryResult {
    fn drop(&mut self) {
        self.result.clear();
    }
}

#[cfg(test)]
mod tests {
    use std::{collections::HashMap, sync::atomic::AtomicUsize, task::Poll};

    use datafusion::physical_plan::stream::RecordBatchStreamAdapter;
    use nautilus_common::live::get_runtime;
    use nautilus_model::{
        data::QuoteTick,
        identifiers::InstrumentId,
        types::{Price, Quantity},
    };
    use nautilus_serialization::arrow::{
        ArrowSchemaProvider, KEY_INSTRUMENT_ID, KEY_PRICE_PRECISION, KEY_SIZE_PRECISION,
    };
    #[cfg(feature = "python")]
    use pyo3::{Py, Python, exceptions::PyRuntimeError, types::PyAnyMethods};
    use rstest::rstest;

    use super::*;

    const INSTRUMENT_ID: &str = "EUR/USD.SIM";

    fn quote(ts_init: u64) -> QuoteTick {
        QuoteTick::new(
            InstrumentId::from(INSTRUMENT_ID),
            Price::from("1.0001"),
            Price::from("1.0002"),
            Quantity::from("100"),
            Quantity::from("100"),
            UnixNanos::from(ts_init),
            UnixNanos::from(ts_init),
        )
    }

    fn quote_metadata() -> HashMap<String, String> {
        HashMap::from([
            (KEY_INSTRUMENT_ID.to_string(), INSTRUMENT_ID.to_string()),
            (KEY_PRICE_PRECISION.to_string(), "4".to_string()),
            (KEY_SIZE_PRECISION.to_string(), "0".to_string()),
        ])
    }

    fn quote_batch(quotes: &[QuoteTick]) -> RecordBatch {
        QuoteTick::encode_batch(&quote_metadata(), quotes).expect("failed to encode quotes")
    }

    fn stream_error() -> DataFusionError {
        DataFusionError::Execution("injected stream failure".to_string())
    }

    fn batch_stream(
        batches: Vec<std::result::Result<RecordBatch, DataFusionError>>,
    ) -> SendableRecordBatchStream {
        Box::pin(RecordBatchStreamAdapter::new(
            Arc::new(QuoteTick::get_schema(Some(quote_metadata()))),
            futures::stream::iter(batches),
        ))
    }

    fn ts_inits(items: &[std::result::Result<Data, QueryError>]) -> Vec<u64> {
        items
            .iter()
            .filter_map(|item| item.as_ref().ok())
            .map(|data| data.ts_init().as_u64())
            .collect()
    }

    #[rstest]
    fn data_backend_sessions_share_global_runtime() {
        let first = DataBackendSession::new(10);
        let second = DataBackendSession::new(10);

        assert_eq!(first.runtime.id(), second.runtime.id());
        assert_eq!(first.runtime.id(), get_runtime().handle().id());
    }

    #[rstest]
    fn query_result_merges_streams_in_order_then_exhausts() {
        let mut session = DataBackendSession::new(10);
        session.add_batch_stream::<QuoteTick>(
            batch_stream(vec![
                Ok(quote_batch(&[quote(1), quote(3)])),
                Ok(quote_batch(&[quote(5)])),
            ]),
            None,
        );
        session.add_batch_stream::<QuoteTick>(
            batch_stream(vec![Ok(quote_batch(&[quote(2), quote(4)]))]),
            None,
        );

        let mut result = session.get_query_result();
        let items: Vec<_> = result.by_ref().collect();

        assert_eq!(ts_inits(&items), vec![1, 2, 3, 4, 5]);
        assert_eq!(items.len(), 5);
        assert!(result.next().is_none());
    }

    #[rstest]
    fn query_result_reports_stream_error_after_its_data() {
        let mut session = DataBackendSession::new(10);
        session.add_batch_stream::<QuoteTick>(
            batch_stream(vec![
                Ok(quote_batch(&[quote(1), quote(2)])),
                Err(stream_error()),
            ]),
            None,
        );

        let mut result = session.get_query_result();
        let items: Vec<_> = result.by_ref().collect();

        assert_eq!(ts_inits(&items), vec![1, 2]);
        assert_eq!(items.len(), 3);
        assert!(
            matches!(items[2], Err(QueryError::Stream(_))),
            "expected a stream error, was {:?}",
            items[2]
        );
        assert!(result.next().is_none());
    }

    #[rstest]
    fn query_result_stops_when_one_of_many_streams_fails() {
        let mut session = DataBackendSession::new(10);
        session.add_batch_stream::<QuoteTick>(
            batch_stream(vec![Ok(quote_batch(&[quote(1)])), Err(stream_error())]),
            None,
        );
        session
            .add_batch_stream::<QuoteTick>(batch_stream(vec![Ok(quote_batch(&[quote(2)]))]), None);

        let mut result = session.get_query_result();
        let items: Vec<_> = result.by_ref().collect();

        // The healthy stream still holds `quote(2)`, so exhaustion here would look successful
        assert_eq!(ts_inits(&items), vec![1]);
        assert_eq!(items.len(), 2);
        assert!(
            matches!(items[1], Err(QueryError::Stream(_))),
            "expected a stream error, was {:?}",
            items[1]
        );
        assert!(result.next().is_none());
    }

    #[rstest]
    fn query_result_reports_decode_error() {
        let mut session = DataBackendSession::new(10);
        // Encode without schema metadata so the decoder cannot resolve the instrument
        let batch =
            QuoteTick::encode_batch(&HashMap::new(), &[quote(1)]).expect("failed to encode quotes");
        session.add_batch_stream::<QuoteTick>(batch_stream(vec![Ok(batch)]), None);

        let items: Vec<_> = session.get_query_result().collect();

        assert_eq!(items.len(), 1);
        assert!(
            matches!(
                items[0],
                Err(QueryError::Decode(EncodingError::MissingMetadata(
                    KEY_INSTRUMENT_ID
                )))
            ),
            "expected a decode error, was {:?}",
            items[0]
        );
    }

    #[rstest]
    fn data_query_result_reports_error_instead_of_an_empty_chunk() {
        let mut session = DataBackendSession::new(10);
        session.add_batch_stream::<QuoteTick>(
            batch_stream(vec![Ok(quote_batch(&[quote(1)])), Err(stream_error())]),
            None,
        );

        let mut result = DataQueryResult::new(session.get_query_result(), 10);
        let chunk = result.next().expect("chunked result must yield an item");

        assert!(
            matches!(chunk, Err(QueryError::Stream(_))),
            "expected a stream error, was {chunk:?}"
        );

        let after = result
            .next()
            .expect("chunked result must signal exhaustion")
            .expect("a failed query must not fail twice");

        assert!(after.is_empty(), "the discarded chunk must not be replayed");
    }

    #[rstest]
    fn data_query_result_reports_an_error_with_a_zero_chunk_size() {
        let mut session = DataBackendSession::new(10);
        session.add_batch_stream::<QuoteTick>(batch_stream(vec![Err(stream_error())]), None);

        let mut result = DataQueryResult::new(session.get_query_result(), 0);
        let chunk = result.next().expect("chunked result must yield an item");

        assert!(
            matches!(chunk, Err(QueryError::Stream(_))),
            "expected a stream error, was {chunk:?}"
        );
    }

    #[rstest]
    fn data_query_result_ends_with_an_empty_chunk_when_successful() {
        let mut session = DataBackendSession::new(10);
        session.add_batch_stream::<QuoteTick>(
            batch_stream(vec![Ok(quote_batch(&[quote(1), quote(2)]))]),
            None,
        );

        let mut result = DataQueryResult::new(session.get_query_result(), 10);
        let chunk = result
            .next()
            .expect("chunked result must yield a chunk")
            .expect("query must not fail");

        assert_eq!(chunk.len(), 2);
        assert_eq!(
            chunk
                .iter()
                .map(|data| data.ts_init().as_u64())
                .collect::<Vec<_>>(),
            vec![1, 2]
        );

        let last = result
            .next()
            .expect("chunked result must signal exhaustion")
            .expect("query must not fail");

        assert!(last.is_empty());
    }

    #[rstest]
    fn decode_batches_stops_polling_a_failed_stream() {
        let polls = Arc::new(AtomicUsize::new(0));
        let counted = Arc::clone(&polls);
        let mut batches = vec![Ok(quote_batch(&[quote(1)])), Err(stream_error())].into_iter();
        let inner = futures::stream::poll_fn(move |_| {
            counted.fetch_add(1, Ordering::SeqCst);
            Poll::Ready(batches.next())
        });
        let stream = Box::pin(RecordBatchStreamAdapter::new(
            Arc::new(QuoteTick::get_schema(Some(quote_metadata()))),
            inner,
        ));

        let decoded = decode_batches::<QuoteTick>(stream, None);
        let items: Vec<_> = futures::executor::block_on_stream(Box::pin(decoded)).collect();

        assert_eq!(items.len(), 2);
        assert!(items[0].is_ok());
        assert!(matches!(items[1], Err(QueryError::Stream(_))));
        assert_eq!(polls.load(Ordering::SeqCst), 2);
    }

    #[rstest]
    fn a_new_query_does_not_inherit_an_earlier_failure() {
        let mut session = DataBackendSession::new(10);
        session.add_batch_stream::<QuoteTick>(batch_stream(vec![Err(stream_error())]), None);

        // Held open so a shared error slot would leak into the query registered next
        let failed = session.get_query_result();

        session.add_batch_stream::<QuoteTick>(
            batch_stream(vec![Ok(quote_batch(&[quote(1), quote(2)]))]),
            None,
        );
        let items: Vec<_> = session.get_query_result().collect();

        let failed: Vec<_> = failed.collect();

        assert_eq!(ts_inits(&items), vec![1, 2]);
        assert_eq!(items.len(), 2);
        assert_eq!(failed.len(), 1);
        assert!(
            matches!(failed[0], Err(QueryError::Stream(_))),
            "expected a stream error, was {:?}",
            failed[0]
        );
    }

    #[rstest]
    #[cfg(feature = "python")]
    fn python_to_list_raises_on_stream_error() {
        let mut session = DataBackendSession::new(10);
        session.add_batch_stream::<QuoteTick>(
            batch_stream(vec![Ok(quote_batch(&[quote(1)])), Err(stream_error())]),
            None,
        );
        let result = DataQueryResult::new(session.get_query_result(), 10);

        Python::initialize();
        Python::attach(|py| {
            let result = Py::new(py, result).expect("failed to create the query result");
            let error = result
                .bind(py)
                .call_method0("to_list")
                .expect_err("to_list must raise when a stream fails");

            assert!(error.is_instance_of::<PyRuntimeError>(py));
            assert!(
                error.to_string().contains("Record batch stream error"),
                "was {error}"
            );
        });
    }

    #[rstest]
    #[cfg(feature = "python")]
    fn python_next_raises_on_decode_error() {
        let mut session = DataBackendSession::new(10);
        // Encode without schema metadata so the decoder cannot resolve the instrument
        let batch =
            QuoteTick::encode_batch(&HashMap::new(), &[quote(1)]).expect("failed to encode quotes");
        session.add_batch_stream::<QuoteTick>(batch_stream(vec![Ok(batch)]), None);
        let result = DataQueryResult::new(session.get_query_result(), 10);

        Python::initialize();
        Python::attach(|py| {
            let result = Py::new(py, result).expect("failed to create the query result");
            let error = result
                .bind(py)
                .call_method0("__next__")
                .expect_err("__next__ must raise when a batch cannot be decoded");

            assert!(error.is_instance_of::<PyRuntimeError>(py));
            assert!(
                error.to_string().contains("Record batch decode error"),
                "was {error}"
            );
        });
    }
}