From 0c0ed1c456c0dce8d3479bbd666b744ba5454016 Mon Sep 17 00:00:00 2001 From: comphead Date: Sun, 12 Feb 2023 03:08:28 -0800 Subject: [PATCH] [SQLLogicTest] Make schema validation ignore nullable and metadata attributes (#5246) * rebase * Make schema validation ignore nullable and metadata attributes * minor comment --- .../src/engines/datafusion/normalize.rs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/datafusion/core/tests/sqllogictests/src/engines/datafusion/normalize.rs b/datafusion/core/tests/sqllogictests/src/engines/datafusion/normalize.rs index 601033e6ac3c..f9e4b631378f 100644 --- a/datafusion/core/tests/sqllogictests/src/engines/datafusion/normalize.rs +++ b/datafusion/core/tests/sqllogictests/src/engines/datafusion/normalize.rs @@ -15,8 +15,9 @@ // specific language governing permissions and limitations // under the License. +use arrow::datatypes::SchemaRef; use arrow::{array, array::ArrayRef, datatypes::DataType, record_batch::RecordBatch}; -use datafusion::error::DataFusionError; +use datafusion_common::DataFusionError; use sqllogictest::DBOutput; use crate::output::{DFColumnType, DFOutput}; @@ -43,11 +44,11 @@ pub fn convert_batches(batches: Vec) -> Result { let mut rows = vec![]; for batch in batches { // Verify schema - if schema != batch.schema() { + if !equivalent_names_and_types(&schema, batch.schema()) { return Err(DFSqlLogicTestError::DataFusion(DataFusionError::Internal( format!( "Schema mismatch. Previously had\n{:#?}\n\nGot:\n{:#?}", - schema, + &schema, batch.schema() ), ))); @@ -58,6 +59,18 @@ pub fn convert_batches(batches: Vec) -> Result { Ok(DBOutput::Rows { types, rows }) } +/// Check two schemas for being equal for field names/types +fn equivalent_names_and_types(schema: &SchemaRef, other: SchemaRef) -> bool { + if schema.fields().len() != other.fields().len() { + return false; + } + let self_fields = schema.fields().iter(); + let other_fields = other.fields().iter(); + self_fields + .zip(other_fields) + .all(|(f1, f2)| f1.name() == f2.name() && f1.data_type() == f2.data_type()) +} + /// Convert a single batch to a `Vec>` for comparison fn convert_batch(batch: RecordBatch) -> Result>> { (0..batch.num_rows())