polyc-query 2026.9.0

Read layer over the event log: a DataFusion engine for SQL over replayed partitions, and a per-conversation Parquet projection for participation-scoped search.
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
//! Exposes exact-generation Parquet capabilities to `DataFusion`.

use std::fmt;
use std::ops::Range;
use std::sync::Arc;

use arrow::datatypes::SchemaRef;
use async_trait::async_trait;
use bytes::Bytes;
use datafusion::catalog::{Session, TableProvider};
use datafusion::datasource::TableType;
use datafusion::datasource::listing::PartitionedFile;
use datafusion::datasource::physical_plan::{
    FileGroup, FileScanConfigBuilder, ParquetFileReaderFactory, ParquetSource,
};
use datafusion::datasource::source::DataSourceExec;
use datafusion::error::{DataFusionError, Result as DataFusionResult};
use datafusion::execution::object_store::ObjectStoreUrl;
use datafusion::logical_expr::{Expr, TableProviderFilterPushDown};
use datafusion::physical_plan::ExecutionPlan;
use datafusion::physical_plan::metrics::ExecutionPlanMetricsSet;
use futures::FutureExt;
use futures::future::{BoxFuture, ready};
use parquet::arrow::arrow_reader::ArrowReaderOptions;
use parquet::arrow::async_reader::AsyncFileReader;
use parquet::arrow::parquet_to_arrow_schema;
use parquet::errors::ParquetError;
use parquet::file::metadata::{ParquetMetaData, ParquetMetaDataReader};
use polyc_projection_artifact::{FleetRealm, RetainedProjectionFile, VisibleRealm};
use polyc_state::immutable::ContentReference;
use polyc_state::projection::artifact::ObjectNamespace;
use polyc_state::revision::JournalSource;
use tokio_util::sync::CancellationToken;

use super::{CoreExecutionError, CoreOperationContext, CoreRealm, operation_refusal};

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub(super) struct VerifiedFileIdentity {
    namespace: ObjectNamespace,
    key: ContentReference,
    generation: u64,
    realm: CoreRealm,
}

#[derive(Clone)]
pub(super) enum VerifiedCoreFile {
    Visible {
        file: RetainedProjectionFile<VisibleRealm>,
        source: JournalSource,
    },
    Fleet {
        file: RetainedProjectionFile<FleetRealm>,
        source: JournalSource,
    },
}

impl fmt::Debug for VerifiedCoreFile {
    /// Reports the realm and byte length only.
    ///
    /// A file identity names an object key and generation, and the source
    /// names a partition incarnation. Neither belongs in a log line.
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("VerifiedCoreFile")
            .field(
                "realm",
                match self {
                    Self::Visible { .. } => &"visible",
                    Self::Fleet { .. } => &"fleet",
                },
            )
            .field("byte_len", &self.byte_len())
            .finish_non_exhaustive()
    }
}

impl VerifiedCoreFile {
    pub(super) const fn byte_len(&self) -> u64 {
        match self {
            Self::Visible { file, .. } => file.byte_len(),
            Self::Fleet { file, .. } => file.byte_len(),
        }
    }

    const fn declared_rows(&self) -> u64 {
        match self {
            Self::Visible { file, .. } => file.descriptor().row_count(),
            Self::Fleet { file, .. } => file.descriptor().row_count(),
        }
    }

    const fn source(&self) -> &JournalSource {
        match self {
            Self::Visible { source, .. } | Self::Fleet { source, .. } => source,
        }
    }

    pub(super) fn identity(&self) -> VerifiedFileIdentity {
        let (object, realm) = match self {
            Self::Visible { file, .. } => (file.object(), CoreRealm::Visible),
            Self::Fleet { file, .. } => (file.object(), CoreRealm::Fleet),
        };
        VerifiedFileIdentity {
            namespace: object.namespace().clone(),
            key: object.key().clone(),
            generation: object.generation(),
            realm,
        }
    }

    fn read_range(
        &self,
        offset: u64,
        len: u64,
    ) -> Result<Bytes, polyc_projection_artifact::ArtifactReadError> {
        match self {
            Self::Visible { file, .. } => file.slice(offset, len),
            Self::Fleet { file, .. } => file.slice(offset, len),
        }
    }

    fn bytes(&self) -> &Bytes {
        match self {
            Self::Visible { file, .. } => file.bytes(),
            Self::Fleet { file, .. } => file.bytes(),
        }
    }

    const fn physical(&self) -> polyc_state::projection::artifact::PhysicalDecodeEnvelope {
        match self {
            Self::Visible { file, .. } => file.descriptor().physical(),
            Self::Fleet { file, .. } => file.descriptor().physical(),
        }
    }
}

#[derive(Clone)]
struct ExactFileExtension {
    file: VerifiedCoreFile,
    operation: Arc<CoreOperationContext>,
    cancellation: CancellationToken,
}

#[derive(Debug)]
struct ExactParquetReaderFactory;

impl ParquetFileReaderFactory for ExactParquetReaderFactory {
    fn create_reader(
        &self,
        _partition_index: usize,
        file: PartitionedFile,
        _metadata_size_hint: Option<usize>,
        _metrics: &ExecutionPlanMetricsSet,
    ) -> DataFusionResult<Box<dyn AsyncFileReader + Send>> {
        let extension = file
            .extension::<ExactFileExtension>()
            .cloned()
            .ok_or_else(|| DataFusionError::Execution("exact file token is absent".to_owned()))?;
        Ok(Box::new(ExactParquetReader::from(extension)))
    }
}

struct ExactParquetReader {
    capability: ExactFileExtension,
}

impl From<ExactFileExtension> for ExactParquetReader {
    fn from(value: ExactFileExtension) -> Self {
        Self { capability: value }
    }
}

impl ExactParquetReader {
    fn read(&self, range: Range<u64>) -> Result<Bytes, ParquetError> {
        if range.start > range.end || range.end > self.capability.file.byte_len() {
            return Err(parquet_error(
                "Parquet requested an out-of-bound exact range",
            ));
        }
        if self.capability.cancellation.is_cancelled() {
            return Err(parquet_error("exact Parquet read was cancelled"));
        }
        self.capability
            .operation
            .check()
            .map_err(operation_refusal)
            .map_err(|error| ParquetError::External(Box::new(error)))?;
        self.capability
            .file
            .read_range(range.start, range.end - range.start)
            .map_err(|error| parquet_error(&error.to_string()))
    }
}

impl AsyncFileReader for ExactParquetReader {
    fn get_bytes(&mut self, range: Range<u64>) -> BoxFuture<'_, Result<Bytes, ParquetError>> {
        ready(self.read(range)).boxed()
    }

    fn get_byte_ranges(
        &mut self,
        ranges: Vec<Range<u64>>,
    ) -> BoxFuture<'_, Result<Vec<Bytes>, ParquetError>> {
        ready(ranges.into_iter().map(|range| self.read(range)).collect()).boxed()
    }

    fn get_metadata<'a>(
        &'a mut self,
        options: Option<&'a ArrowReaderOptions>,
    ) -> BoxFuture<'a, Result<Arc<ParquetMetaData>, ParquetError>> {
        let file_len = self.capability.file.byte_len();
        async move {
            let metadata_options = options.map(|value| value.metadata_options().clone());
            let metadata = ParquetMetaDataReader::new()
                .with_metadata_options(metadata_options)
                .load_and_finish(self, file_len)
                .await?;
            Ok(Arc::new(metadata))
        }
        .boxed()
    }
}

fn parquet_error(reason: &str) -> ParquetError {
    ParquetError::General(reason.to_owned())
}

pub(super) async fn validate_parquet_file(
    file: VerifiedCoreFile,
    expected_schema: SchemaRef,
    operation: Arc<CoreOperationContext>,
    cancellation: CancellationToken,
) -> Result<(), CoreExecutionError> {
    let expected_rows = file.declared_rows();
    let expected_source = file.source().clone();
    polyc_projection_artifact::parquet_profile::inspect_exact(
        file.bytes(),
        expected_schema.as_ref(),
        expected_rows,
        file.physical(),
    )
    .map_err(|_| {
        CoreExecutionError::ParquetContract(
            "the retained file violates its signed physical envelope",
        )
    })?;
    let mut reader = ExactParquetReader::from(ExactFileExtension {
        file,
        operation,
        cancellation,
    });
    let metadata = reader.get_metadata(None).await?;
    let observed_schema = parquet_to_arrow_schema(
        metadata.file_metadata().schema_descr(),
        metadata.file_metadata().key_value_metadata(),
    )?;
    if observed_schema.fields() != expected_schema.fields() {
        return Err(CoreExecutionError::ParquetContract(
            "the exact file schema differs from conversation-core",
        ));
    }
    let rows = metadata
        .row_groups()
        .iter()
        .try_fold(0_u64, |held, group| {
            let rows = u64::try_from(group.num_rows()).map_err(|_| {
                CoreExecutionError::ParquetContract("a Parquet row count is negative")
            })?;
            if rows > 0 {
                verify_source_statistics(group, &expected_schema, &expected_source)?;
            }
            held.checked_add(rows)
                .ok_or(CoreExecutionError::ParquetContract(
                    "the Parquet row count overflowed",
                ))
        })?;
    if rows != expected_rows {
        return Err(CoreExecutionError::ParquetContract(
            "the exact file row count differs from its admitted segment",
        ));
    }
    Ok(())
}

fn verify_source_statistics(
    group: &parquet::file::metadata::RowGroupMetaData,
    schema: &SchemaRef,
    source: &JournalSource,
) -> Result<(), CoreExecutionError> {
    let partition = schema.index_of("partition")?;
    let incarnation = schema.index_of("source_incarnation")?;
    verify_constant_statistics(
        group.column(partition).statistics(),
        source.partition().as_str().as_bytes(),
    )?;
    verify_constant_statistics(
        group.column(incarnation).statistics(),
        source.incarnation().as_bytes(),
    )
}

fn verify_constant_statistics(
    statistics: Option<&parquet::file::statistics::Statistics>,
    expected: &[u8],
) -> Result<(), CoreExecutionError> {
    let statistics = statistics.ok_or(CoreExecutionError::ParquetContract(
        "source lineage statistics are absent",
    ))?;
    if !statistics.min_is_exact()
        || !statistics.max_is_exact()
        || statistics.null_count_opt() != Some(0)
        || statistics.min_bytes_opt() != Some(expected)
        || statistics.max_bytes_opt() != Some(expected)
    {
        return Err(CoreExecutionError::ParquetContract(
            "source lineage statistics do not prove one exact source",
        ));
    }
    Ok(())
}

#[derive(Debug)]
pub(super) struct ExactParquetTable {
    schema: SchemaRef,
    files: Vec<PartitionedFile>,
    factory: Arc<ExactParquetReaderFactory>,
}

impl ExactParquetTable {
    pub(super) fn new(
        schema: SchemaRef,
        files: Vec<VerifiedCoreFile>,
        operation: &Arc<CoreOperationContext>,
        cancellation: &CancellationToken,
    ) -> Self {
        let files = files
            .into_iter()
            .enumerate()
            .map(|(index, file)| {
                PartitionedFile::new(format!("exact-core/{index:08}.parquet"), file.byte_len())
                    .with_extension(ExactFileExtension {
                        file,
                        operation: Arc::clone(operation),
                        cancellation: cancellation.clone(),
                    })
            })
            .collect();
        Self {
            schema,
            files,
            factory: Arc::new(ExactParquetReaderFactory),
        }
    }
}

#[async_trait]
impl TableProvider for ExactParquetTable {
    fn schema(&self) -> SchemaRef {
        Arc::clone(&self.schema)
    }

    fn table_type(&self) -> TableType {
        TableType::Base
    }

    async fn scan(
        &self,
        _state: &dyn Session,
        projection: Option<&Vec<usize>>,
        filters: &[Expr],
        limit: Option<usize>,
    ) -> DataFusionResult<Arc<dyn ExecutionPlan>> {
        if !filters.is_empty() {
            return Err(DataFusionError::Plan(
                "exact projection tables do not evaluate pushed filters".to_owned(),
            ));
        }
        if projection.is_some_and(|indices| {
            indices
                .iter()
                .any(|index| *index >= self.schema.fields().len())
        }) {
            return Err(DataFusionError::Plan(
                "exact projection table received an out-of-bound column".to_owned(),
            ));
        }
        let source = Arc::new(
            ParquetSource::new(Arc::clone(&self.schema))
                .with_parquet_file_reader_factory(self.factory.clone()),
        );
        // DataFusion still resolves the default file:// registry entry.
        // The capability-bearing custom factory reads every artifact byte.
        let config = FileScanConfigBuilder::new(ObjectStoreUrl::local_filesystem(), source)
            .with_file_groups(vec![FileGroup::new(self.files.clone())])
            .with_preserve_order(true)
            .with_limit(limit)
            .with_projection_indices(projection.cloned())?
            .build();
        Ok(DataSourceExec::from_data_source(config))
    }

    fn supports_filters_pushdown(
        &self,
        filters: &[&Expr],
    ) -> DataFusionResult<Vec<TableProviderFilterPushDown>> {
        Ok(vec![
            TableProviderFilterPushDown::Unsupported;
            filters.len()
        ])
    }
}