Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
60 changes: 34 additions & 26 deletions src/metadata/type_mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,13 +327,8 @@ fn split_top_level_commas(s: &str) -> Vec<&str> {
parts
}

/// Parse `ARRAY<inner>` into a `List(Field("item", inner, true))`.
/// Field name `"item"` matches arrow-rs's canonical default
/// (`Field::new_list_field`) and every other call site in this
/// kernel (`reader/post_processor/`). pyarrow's
/// `list_(t)` default also uses `"item"`. Using anything else would
/// make the empty-result schema fail field-level equality against
/// the non-empty Arrow IPC payload.
/// Parse `ARRAY<inner>` using DBSQL's Arrow child name, `element`, so
/// manifest-only empty results match the non-empty IPC schema.
fn parse_array(
s: &str,
type_interval_type: Option<&str>,
Expand All @@ -347,8 +342,8 @@ fn parse_array(
geospatial_representation,
depth + 1,
)?;
Some(DataType::List(Arc::new(Field::new_list_field(
inner_dt, true,
Some(DataType::List(Arc::new(Field::new(
"element", inner_dt, true,
))))
}

Expand Down Expand Up @@ -834,40 +829,32 @@ mod tests {

#[test]
fn test_databricks_type_to_arrow_array_nested() {
// ARRAY<INT> → List<Int32>. Field name is "item" — the
// arrow-rs canonical default; matches the non-empty wire
// payload and every other call site in this kernel.
assert_eq!(
databricks_type_to_arrow("ARRAY<INT>"),
DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)))
DataType::List(Arc::new(Field::new("element", DataType::Int32, true)))
);
// ARRAY<STRING>
assert_eq!(
databricks_type_to_arrow("ARRAY<STRING>"),
DataType::List(Arc::new(Field::new_list_field(DataType::Utf8, true)))
DataType::List(Arc::new(Field::new("element", DataType::Utf8, true)))
);
// Nested: ARRAY<ARRAY<INT>>
assert_eq!(
databricks_type_to_arrow("ARRAY<ARRAY<INT>>"),
DataType::List(Arc::new(Field::new_list_field(
DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true))),
DataType::List(Arc::new(Field::new(
"element",
DataType::List(Arc::new(Field::new("element", DataType::Int32, true))),
true,
)))
);
}

#[test]
fn test_array_field_name_is_item_not_element() {
// Lock in the field-name convention. The non-empty-result
// schema (wire payload) uses "item"; if this regresses,
// empty-result and non-empty-result schemas stop being
// field-equal — directly defeats the empty-result schema-fix
// goal of bucket A/E in the comparator parity tasklist.
fn test_array_field_name_matches_databricks_arrow_schema() {
let dt = databricks_type_to_arrow("ARRAY<INT>");
let DataType::List(field) = dt else {
panic!("expected List, got {dt:?}");
};
assert_eq!(field.name(), "item", "list field name must be 'item'");
assert_eq!(field.name(), "element");
assert!(field.is_nullable());
}

#[test]
Expand Down Expand Up @@ -911,6 +898,23 @@ mod tests {
};
}

#[test]
fn test_databricks_type_to_arrow_map_with_array_values() {
let dt = databricks_type_to_arrow("MAP<STRING, ARRAY<INT>>");
let DataType::Map(entries, _) = dt else {
panic!("expected Map");
};
let DataType::Struct(fields) = entries.data_type() else {
panic!("expected Struct");
};
assert_eq!(fields[0].name(), "key");
assert_eq!(fields[1].name(), "value");
assert_eq!(
fields[1].data_type(),
&DataType::List(Arc::new(Field::new("element", DataType::Int32, true)))
);
}

#[test]
fn test_databricks_type_to_arrow_struct() {
// STRUCT<x:INT,y:STRING>
Expand Down Expand Up @@ -938,7 +942,10 @@ mod tests {
};
assert_eq!(fields.len(), 2);
assert_eq!(fields[0].name(), "a");
assert!(matches!(fields[0].data_type(), DataType::List(_)));
assert_eq!(
fields[0].data_type(),
&DataType::List(Arc::new(Field::new("element", DataType::Int32, true)))
);
assert_eq!(fields[1].name(), "b");
assert!(matches!(fields[1].data_type(), DataType::Map(_, _)));
}
Expand All @@ -952,6 +959,7 @@ mod tests {
let DataType::List(field) = dt else {
panic!("expected List");
};
assert_eq!(field.name(), "element");
let DataType::Struct(fields) = field.data_type() else {
panic!("expected Struct inside List");
};
Expand Down
26 changes: 26 additions & 0 deletions src/reader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1341,6 +1341,32 @@ mod tests {
assert_eq!(physical.field(0).name(), "col_0");
}

#[test]
fn test_extract_physical_schema_preserves_array_child_fields() {
for child_name in ["element", "item"] {
let mut manifest = make_manifest(vec![make_column_info(
"a",
"ARRAY",
"ARRAY<INT>",
0,
None,
None,
None,
)]);
let schema = Schema::new(vec![Field::new(
"a",
DataType::List(Arc::new(Field::new(child_name, DataType::Int32, false))),
true,
)]);
manifest.arrow_schema = Some(schema_ipc(&schema));

let physical =
ResultReaderFactory::extract_physical_schema_from_manifest(Some(&manifest))
.unwrap();
assert_eq!(physical.as_ref(), &schema);
}
}

#[test]
fn test_extract_physical_schema_rejects_invalid_or_mismatched_ipc() {
let mut manifest = make_manifest(vec![make_column_info(
Expand Down
96 changes: 94 additions & 2 deletions tests/v0_execute_wiremock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
use arrow_array::builder::{BinaryBuilder, Int32Builder};
use arrow_array::cast::AsArray;
use arrow_array::{
Array, ArrayRef, BinaryArray, Int32Array, Int64Array, LargeStringArray, RecordBatch,
Array, ArrayRef, BinaryArray, Int32Array, Int64Array, LargeStringArray, ListArray, RecordBatch,
StringArray, StructArray,
};
use arrow_buffer::NullBuffer;
use arrow_buffer::{NullBuffer, OffsetBuffer};
use arrow_ipc::writer::StreamWriter;
use arrow_schema::{DataType, Field, Fields, Schema};
use base64::{engine::general_purpose::STANDARD, Engine};
Expand Down Expand Up @@ -981,6 +981,98 @@ async fn execute_cloudfetch_geospatial_reconciles_manifest_and_untagged_payload(
}
}

#[tokio::test]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Low — The new execute_array_child_names_match_for_empty_and_non_empty_results test constructs both the empty and non-empty branches from the same locally-built ListArray whose child field is named "element". Because the mock IPC payload is fabricated by the test itself (not emitted by a real warehouse), the final assert_eq!(schemas[0], schemas[1]) only proves the two code paths agree with each other — it would pass identically if the kernel had chosen "item" on both sides. It cannot catch a wrong-direction choice relative to what DBSQL actually sends.

The actual correctness premise of this PR — that DBSQL's Arrow IPC names ARRAY children "element" (matching Spark's ArrowUtils), not "item" as the prior comment asserted — is only validated by the ignored live-warehouse path. Consider adding an #[ignore] e2e assertion in tests/v0_*_e2e.rs that runs SELECT array(...) and an empty-filtered variant against a live warehouse and compares the advertised schemas, so the server-match premise is covered by something that can actually fail if DBSQL's convention differs. The unit/wiremock change itself looks correct.

async fn execute_array_child_names_match_for_empty_and_non_empty_results() {
let array = Arc::new(
ListArray::try_new(
Arc::new(Field::new("element", DataType::Int32, true)),
OffsetBuffer::new(vec![0, 2].into()),
Arc::new(Int32Array::from(vec![1, 2])),
None,
)
.unwrap(),
);
let nested = Arc::new(
ListArray::try_new(
Arc::new(Field::new("element", array.data_type().clone(), true)),
OffsetBuffer::new(vec![0, 1].into()),
array.clone(),
None,
)
.unwrap(),
);
let batch = RecordBatch::try_new(
Arc::new(Schema::new(vec![
Field::new("a", array.data_type().clone(), true),
Field::new("nested", nested.data_type().clone(), true),
])),
vec![array, nested],
)
.unwrap();
let mut schemas = Vec::new();

for empty in [false, true] {
let server = MockServer::start().await;
mount_create_session(&server).await;
mount_delete_session(&server).await;
mount_close_any_statement(&server).await;
let mut body = json!({
"statement_id": "stmt-arrays",
"status": { "state": "SUCCEEDED" },
"manifest": {
"format": "ARROW_STREAM",
"schema": {
"column_count": 2,
"columns": [
{
"name": "a",
"type_name": "ARRAY",
"type_text": "ARRAY<INT>",
"position": 0,
},
{
"name": "nested",
"type_name": "ARRAY",
"type_text": "ARRAY<ARRAY<INT>>",
"position": 1,
},
],
},
"total_chunk_count": if empty { 0 } else { 1 },
"total_row_count": if empty { 0 } else { 1 },
"truncated": false,
},
});
if !empty {
body["result"] = json!({ "attachment": STANDARD.encode(arrow_ipc_bytes(&batch)) });
}
mount_execute(&server, body.to_string()).await;

let session = open_session(&server).await;
let mut stmt = session.statement();
stmt.spec().sql(if empty {
"SELECT a, nested FROM array_values WHERE 1=0"
} else {
"SELECT a, nested FROM array_values"
});
let mut exec = stmt.execute().await.expect("execute");
schemas.push(exec.schema());
let batches = drain(exec.result_stream_mut()).await;
if empty {
assert!(batches.is_empty());
} else {
assert_eq!(batches.len(), 1);
assert_eq!(batches[0].num_rows(), 1);
for i in 0..batch.num_columns() {
assert_eq!(batches[0].column(i).to_data(), batch.column(i).to_data());
}
}
exec.close().await.expect("close");
}

assert_eq!(schemas[0], schemas[1]);
}

#[tokio::test]
async fn execute_empty_geospatial_result_has_the_requested_client_schema() {
for representation in [
Expand Down
Loading