Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 38 additions & 8 deletions crates/ourios-querier/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ mod compile;
mod drift;
pub mod dsl;
mod log_row;
mod schema_adapt;
mod template_map;
mod template_registry;

Expand Down Expand Up @@ -905,6 +906,12 @@ impl Backend {
#[derive(Debug, Clone)]
pub struct Querier {
backend: Backend,
/// The deployment's declared promoted set (RFC 0042 §3.3): drives
/// the scan schema's type for declared promoted columns, so files
/// written under other declarations read those columns as absent.
/// Defaults to the implicit-`service.name`-only set, under which
/// the scan stays purely schema-driven (the RFC 0022 behaviour).
promoted: ourios_parquet::PromotedAttributes,
}

/// The object-store URL scheme/authority the S3 scan registers its
Expand Down Expand Up @@ -979,9 +986,25 @@ impl Querier {
pub fn new(bucket_root: impl Into<PathBuf>) -> Self {
Self {
backend: Backend::Local(bucket_root.into()),
promoted: ourios_parquet::PromotedAttributes::default(),
}
}

/// Declare the deployment's promoted set (RFC 0042 §3.3): a
/// declared key's class fixes its scan-schema column type, so
/// files written under a different declaration read that column
/// as absent instead of erroring the scan or coercing values.
/// Without this, the scan is purely schema-driven (the RFC 0022
/// behaviour, and the default for tests/dev).
#[must_use]
pub fn with_promoted_attributes(
mut self,
promoted: ourios_parquet::PromotedAttributes,
) -> Self {
self.promoted = promoted;
self
}

/// Create a querier from a resolved [`StoreConfig`] (RFC 0019 §3.2)
/// — the S3-capable constructor the server wires the querier role
/// through. A `Local` config is equivalent to [`Self::new`]; an
Expand All @@ -1002,7 +1025,10 @@ impl Querier {
Backend::Remote(store)
}
};
Ok(Self { backend })
Ok(Self {
backend,
promoted: ourios_parquet::PromotedAttributes::default(),
})
}

/// Execute `request` against the RFC 0005 store with predicate
Expand Down Expand Up @@ -1289,15 +1315,19 @@ impl Querier {
.map_err(storage_err)?;
schemas.push(schema.as_ref().clone());
}
let file_schema =
datafusion::arrow::datatypes::Schema::try_merge(schemas).map_err(|e| {
QueryError::Storage {
detail: format!("merging scanned file schemas: {e}"),
}
})?;
// RFC 0042 §3.3: a declared promoted key's class fixes its
// union-schema type; an undeclared promoted-column conflict
// resolves to Utf8; the per-file expression adapter reads a
// type-mismatched promoted column as absent (typed NULL) —
// DataFusion's default adapter would cast, and Arrow's safe
// Utf8→Int64 cast *parses* string content, the coercion §3.3
// forbids.
let file_schema = schema_adapt::merge_scanned_schemas(schemas, &self.promoted)
.map_err(|detail| QueryError::Storage { detail })?;
let config = ListingTableConfig::new_with_multi_paths(urls)
.with_listing_options(options)
.with_schema(Arc::new(file_schema));
.with_schema(Arc::new(file_schema))
.with_expr_adapter_factory(Arc::new(schema_adapt::PromotedNoCoercionFactory));
let table = ListingTable::try_new(config).map_err(storage_err)?;
ctx.register_table("logs", Arc::new(table))
.map_err(storage_err)?;
Expand Down
176 changes: 176 additions & 0 deletions crates/ourios-querier/src/schema_adapt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
//! RFC 0042 §3.3 — cross-file promoted-column type handling.
//!
//! Two seams keep a scan correct when files written under different
//! promoted declarations coexist:
//!
//! 1. [`merge_scanned_schemas`] builds the scan's union schema. A
//! promoted column (`resource.<key>` / `attr.<key>`) whose key is in
//! the **declared** set takes the declared class's type regardless of
//! what any file carries; an undeclared promoted column that
//! conflicts across files resolves to `Utf8` (the string class is
//! the pre-RFC-0042 universal, and the §3.3 rule below makes the
//! numeric files' cells read `NULL` under it). Non-promoted columns
//! keep Arrow's own merge — a conflict there is RFC 0005 schema
//! corruption and stays an error.
//! 2. [`PromotedNoCoercionFactory`] is the per-file physical-expression
//! adapter: a promoted column whose file type differs from the scan
//! schema's type is read as if the column were **absent** (a typed
//! `NULL` literal). `DataFusion`'s default adapter would insert a cast
//! instead — and Arrow's safe `Utf8 → Int64` cast *parses* string
//! content, the exact coercion RFC 0042 §3.1/§3.3 forbids
//! (projection must depend on the variant, never the value).
//!
//! Everything that is not a type-mismatched promoted column delegates to
//! the default adapter, so missing-column `NULL` filling and
//! non-promoted casts behave exactly as stock `DataFusion`.

use std::sync::Arc;

use datafusion::arrow::datatypes::{DataType, Field, Schema, SchemaRef};
use datafusion::common::tree_node::{Transformed, TreeNode};
use datafusion::error::Result as DfResult;
use datafusion::physical_expr::PhysicalExpr;
use datafusion::physical_expr::expressions::{Column, Literal};
use datafusion::physical_expr_adapter::{
DefaultPhysicalExprAdapterFactory, PhysicalExprAdapter, PhysicalExprAdapterFactory,
};
use datafusion::scalar::ScalarValue;
use ourios_parquet::{PromotedAttributes, promoted};

/// Is this column name a promoted attribute column (RFC 0022 §3.1
/// naming: the DSL path, literally)?
fn is_promoted_name(name: &str) -> bool {
name.starts_with(promoted::RESOURCE_PREFIX) || name.starts_with(promoted::ATTR_PREFIX)
}

/// The declared class's Arrow type for a promoted column name, if the
/// name is in the declared set.
fn declared_type(declared: &PromotedAttributes, name: &str) -> Option<DataType> {
let key_class = |keys: &[ourios_parquet::PromotedKey], prefix: &str| {
let key = name.strip_prefix(prefix)?;
keys.iter()
.find(|k| k.key == key)
.map(|k| k.class.data_type())
};
key_class(declared.resource_keys(), promoted::RESOURCE_PREFIX)
.or_else(|| key_class(declared.log_keys(), promoted::ATTR_PREFIX))
}

/// Build the scan's union schema across every scanned file's schema
/// (RFC0007.4 / RFC 0005 §3.9), applying the RFC 0042 §3.3 rules to
/// promoted columns. Field order is first appearance across the inputs,
/// matching `Schema::try_merge`.
pub(crate) fn merge_scanned_schemas(
schemas: Vec<Schema>,
declared: &PromotedAttributes,
) -> Result<Schema, String> {
let mut order: Vec<String> = Vec::new();
let mut fields: std::collections::HashMap<String, Vec<Field>> =
std::collections::HashMap::new();
for schema in schemas {
for field in schema.fields() {
let entry = fields.entry(field.name().clone()).or_insert_with(|| {
order.push(field.name().clone());
Vec::new()
});
entry.push(field.as_ref().clone());
}
}

let mut merged = Vec::with_capacity(order.len());
for name in order {
let seen = fields.remove(&name).expect("field recorded in order");
if is_promoted_name(&name) {
let target = declared_type(declared, &name).unwrap_or_else(|| {
let mut types = seen.iter().map(Field::data_type);
let first = types.next().expect("at least one occurrence").clone();
if types.all(|t| *t == first) {
first
} else {
// Undeclared conflict: resolve to the string class
// (the pre-RFC-0042 universal); the §3.3 rule reads
// the non-matching files' cells as NULL.
DataType::Utf8
}
});
// Promoted columns are OPTIONAL by construction (RFC 0022
// §3.1); nullable also covers the mismatched-file NULLs.
merged.push(Field::new(name, target, true));
} else {
// Non-promoted columns: Arrow's own merge semantics. A type
// conflict here is RFC 0005 schema corruption — error, as
// `try_merge` always did.
let unioned = Schema::try_merge(seen.into_iter().map(|f| Schema::new(vec![f])))
.map_err(|e| format!("merging scanned file schemas: {e}"))?;
merged.push(unioned.fields()[0].as_ref().clone());
}
}
Ok(Schema::new(merged))
}

/// The RFC 0042 §3.3 per-file expression adapter factory (installed via
/// `ListingTableConfig::with_expr_adapter_factory`).
#[derive(Debug)]
pub(crate) struct PromotedNoCoercionFactory;

impl PhysicalExprAdapterFactory for PromotedNoCoercionFactory {
fn create(
&self,
logical_file_schema: SchemaRef,
physical_file_schema: SchemaRef,
) -> DfResult<Arc<dyn PhysicalExprAdapter>> {
let inner = DefaultPhysicalExprAdapterFactory.create(
Arc::clone(&logical_file_schema),
Arc::clone(&physical_file_schema),
)?;
Ok(Arc::new(PromotedNoCoercion {
logical: logical_file_schema,
physical: physical_file_schema,
inner,
}))
}
}

/// See [`PromotedNoCoercionFactory`]. `logical` is the scan schema the
/// query addresses; `physical` is one file's own schema.
#[derive(Debug)]
struct PromotedNoCoercion {
logical: SchemaRef,
physical: SchemaRef,
inner: Arc<dyn PhysicalExprAdapter>,
}

impl PhysicalExprAdapter for PromotedNoCoercion {
fn rewrite(&self, expr: Arc<dyn PhysicalExpr>) -> DfResult<Arc<dyn PhysicalExpr>> {
// First pass: replace references to type-mismatched promoted
// columns with a typed NULL literal (the §3.3 "read as absent"
// rule). Second pass: the default adapter handles everything
// else (missing columns, benign casts, index remapping).
let expr = expr
.transform(|e| {
let Some(column) = e.downcast_ref::<Column>() else {
return Ok(Transformed::no(e));
};
if !is_promoted_name(column.name()) {
return Ok(Transformed::no(e));
}
let (Ok(logical), Ok(physical)) = (
self.logical.field_with_name(column.name()),
self.physical.field_with_name(column.name()),
) else {
// Absent in the file: the default adapter's NULL
// fill is already the §3.3 behaviour.
return Ok(Transformed::no(e));
};
if logical.data_type() == physical.data_type() {
return Ok(Transformed::no(e));
}
Comment thread
jensholdgaard marked this conversation as resolved.
Resolved
let null = ScalarValue::Null.cast_to(logical.data_type())?;
Ok(Transformed::yes(
Arc::new(Literal::new(null)) as Arc<dyn PhysicalExpr>
))
})?
.data;
self.inner.rewrite(expr)
}
}
1 change: 1 addition & 0 deletions crates/ourios-querier/tests/it/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ mod rfc0025_rendering;
mod rfc0031_single_pass;
mod rfc0033_cached_template_map;
mod rfc0036_window_materialization;
mod rfc0042_mixed_schema;
Loading