From 9f00465d59c78988939dd18314372591c63396c0 Mon Sep 17 00:00:00 2001 From: Sreekanth Vadigi Date: Tue, 2 Jun 2026 09:57:33 +0000 Subject: [PATCH] fix(reader): source result column names from SEA manifest, not Arrow payload MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit SEA emits positional placeholder names (`col_0`, `col_1`, …) in the Arrow result payload by design; the authoritative column names live in the manifest's `ColumnInfo.name`. The kernel was surfacing the Arrow placeholder names for row-bearing results, while the zero-row path (`extract_schema_from_manifest`) already reconstructed real names from the manifest — so the same `SELECT *` returned `col_N` with rows but real names when empty. THRIFT carries real names inline, so the comparator flagged the divergence. `augment_schema_with_manifest` now overlays `ColumnInfo.name` onto each field by position (unconditional), in addition to attaching the existing `databricks.*` type metadata. Because `ResultPostProcessor` rebuilds each batch onto the augmented schema whenever it differs from the inner schema, the rename propagates to the batches consumers receive — not just the advertised schema. Fields with no matching manifest column keep their original Arrow name. Decided client-side per the kernel-comparator triage (SEA contract item #12). Tests: - reader::tests::test_augment_schema_overlays_manifest_names_over_positional — the mismatch fixture (Arrow `col_N` vs manifest real names) that the pre-existing augment tests couldn't catch (they used matching names). - reader::tests::test_augment_schema_keeps_arrow_name_when_no_manifest_column - post_processor::tests::manifest_overlays_real_names_onto_positional_columns_and_batches — asserts both the advertised schema and the yielded batch carry the manifest names, with data preserved. Co-authored-by: Isaac Signed-off-by: Sreekanth Vadigi --- src/reader/mod.rs | 79 +++++++++++++++++++++++++++++--- src/reader/post_processor/mod.rs | 56 ++++++++++++++++++++++ 2 files changed, 129 insertions(+), 6 deletions(-) diff --git a/src/reader/mod.rs b/src/reader/mod.rs index 3ac5b97a..ed67e4e4 100644 --- a/src/reader/mod.rs +++ b/src/reader/mod.rs @@ -715,12 +715,16 @@ impl ResultReaderAdapter { Ok(Self { inner, schema }) } - /// Augment an Arrow schema with Databricks column metadata from the manifest. + /// Augment an Arrow schema with column info from the manifest. /// - /// For each field in the schema, if a matching ColumnInfo exists (by position), - /// attach `databricks.*` key-value metadata to the field. This preserves - /// server-provided type_name, type_text, precision, scale, etc. through the - /// Arrow C Data Interface FFI boundary. + /// For each field with a matching `ColumnInfo` (by position): + /// - overwrite the field name with `ColumnInfo.name` — SEA sends + /// positional placeholders (`col_0`, …) in the Arrow payload; the + /// authoritative names live in the manifest; + /// - attach `databricks.*` type metadata (type_name, type_text, + /// precision, scale, interval_type) for the Arrow C Data FFI boundary. + /// + /// Fields with no matching `ColumnInfo` are left unchanged. pub(crate) fn augment_schema_with_manifest( schema: &SchemaRef, manifest: &ResultManifest, @@ -764,7 +768,13 @@ impl ResultReaderAdapter { interval.clone(), ); } - field.as_ref().clone().with_metadata(metadata) + // Overlay the real manifest name (Arrow payload has + // positional `col_N`) and attach the type metadata. + field + .as_ref() + .clone() + .with_name(info.name.clone()) + .with_metadata(metadata) } None => field.as_ref().clone(), } @@ -1095,6 +1105,63 @@ mod tests { } } + /// Overlay must replace the Arrow payload's positional names + /// (`col_0`, …) with the manifest's real names. The other augment + /// tests use fixtures where the names already match, so this is the + /// only one that catches a broken overlay. + #[test] + fn test_augment_schema_overlays_manifest_names_over_positional() { + // Arrow payload = positional placeholders; manifest = real names. + let schema = Arc::new(Schema::new(vec![ + Field::new("col_0", DataType::Int32, true), + Field::new("col_1", DataType::Utf8, true), + Field::new("col_2", DataType::Boolean, true), + ])); + let manifest = make_manifest(vec![ + make_column_info("id", "INT", "INT", 0, None, None, None), + make_column_info("varchar_column", "STRING", "STRING", 1, None, None, None), + make_column_info("boolean_column", "BOOLEAN", "BOOLEAN", 2, None, None, None), + ]); + + let augmented = ResultReaderAdapter::augment_schema_with_manifest(&schema, &manifest); + + // Names come from the manifest, not the Arrow payload. + assert_eq!(augmented.field(0).name(), "id"); + assert_eq!(augmented.field(1).name(), "varchar_column"); + assert_eq!(augmented.field(2).name(), "boolean_column"); + // Data types are preserved unchanged. + assert_eq!(augmented.field(0).data_type(), &DataType::Int32); + assert_eq!(augmented.field(1).data_type(), &DataType::Utf8); + assert_eq!(augmented.field(2).data_type(), &DataType::Boolean); + // Type metadata still attached. + assert_eq!( + augmented.field(0).metadata().get(metadata_keys::TYPE_NAME), + Some(&"INT".to_string()) + ); + } + + /// A field with no matching `ColumnInfo` keeps its original Arrow name. + #[test] + fn test_augment_schema_keeps_arrow_name_when_no_manifest_column() { + let schema = Arc::new(Schema::new(vec![ + Field::new("col_0", DataType::Int32, true), + Field::new("col_1", DataType::Utf8, true), + ])); + // Manifest only describes position 0. + let manifest = make_manifest(vec![make_column_info( + "id", "INT", "INT", 0, None, None, None, + )]); + + let augmented = ResultReaderAdapter::augment_schema_with_manifest(&schema, &manifest); + + assert_eq!(augmented.field(0).name(), "id", "manifest name overlaid"); + assert_eq!( + augmented.field(1).name(), + "col_1", + "no manifest column at position 1 → original Arrow name kept" + ); + } + #[test] fn test_augment_schema_basic_types() { let schema = Arc::new(Schema::new(vec![ diff --git a/src/reader/post_processor/mod.rs b/src/reader/post_processor/mod.rs index 08a65e48..a5d7d632 100644 --- a/src/reader/post_processor/mod.rs +++ b/src/reader/post_processor/mod.rs @@ -422,6 +422,62 @@ mod tests { ); } + #[tokio::test] + async fn manifest_overlays_real_names_onto_positional_columns_and_batches() { + // Real names must appear on both the advertised schema and the + // yielded batch (rebuilt onto the renamed schema), not the col_N + // names from the Arrow payload. + let positional = Arc::new(Schema::new(vec![ + Field::new("col_0", DataType::Int32, true), + Field::new("col_1", DataType::Utf8, true), + ])); + let batch = RecordBatch::try_new( + positional.clone(), + vec![ + Arc::new(Int32Array::from(vec![1])) as _, + Arc::new(StringArray::from(vec!["abc"])) as _, + ], + ) + .unwrap(); + let inner: Box = Box::new(MockReader::new(vec![batch])); + let manifest = make_manifest(vec![ + column_info("id", "INT", "INT", 0), + column_info("varchar_column", "STRING", "STRING", 1), + ]); + + let mut pp = + ResultPostProcessor::new(inner, Some(&manifest), PostProcessOptions::default()) + .await + .unwrap(); + + // Advertised schema carries the manifest names. + let advertised = pp.schema().await.unwrap(); + assert_eq!(advertised.field(0).name(), "id"); + assert_eq!(advertised.field(1).name(), "varchar_column"); + + // The actual yielded batch carries them too (not the col_N names), + // and the data is untouched. + let out = pp.next_batch().await.unwrap().unwrap(); + assert_eq!(out.schema().field(0).name(), "id"); + assert_eq!(out.schema().field(1).name(), "varchar_column"); + assert_eq!( + out.column(0) + .as_any() + .downcast_ref::() + .unwrap() + .value(0), + 1 + ); + assert_eq!( + out.column(1) + .as_any() + .downcast_ref::() + .unwrap() + .value(0), + "abc" + ); + } + #[tokio::test] async fn json_path_rewrites_list_column_to_utf8() { // Build a one-row batch with a list column.