Skip to content

Apache DataFusion SQL Query Engine

Add empty string normalization to sqllogictests (#4501)

Andrew Lamba2bc49922ece
diff --git a/datafusion/core/tests/sqllogictests/src/main.rs b/datafusion/core/tests/sqllogictests/src/main.rs
index 0efac489f3..813a180e8a 100644
--- a/datafusion/core/tests/sqllogictests/src/main.rs
+++ b/datafusion/core/tests/sqllogictests/src/main.rs
@@ -19,6 +19,7 @@ use async_trait::async_trait;
use datafusion::arrow::csv::WriterBuilder;
use datafusion::arrow::record_batch::RecordBatch;
use datafusion::prelude::{SessionConfig, SessionContext};
+use normalize::normalize_batch;
use std::path::Path;
use std::time::Duration;
@@ -27,6 +28,7 @@ use crate::insert::insert;
mod error;
mod insert;
+mod normalize;
mod setup;
mod utils;
@@ -127,13 +129,13 @@ async fn context_for_test_file(file_name: &str) -> SessionContext {
}
}
-fn format_batches(batches: &[RecordBatch]) -> Result<String> {
+fn format_batches(batches: Vec<RecordBatch>) -> Result<String> {
let mut bytes = vec![];
{
let builder = WriterBuilder::new().has_headers(false).with_delimiter(b' ');
let mut writer = builder.build(&mut bytes);
for batch in batches {
- writer.write(batch).unwrap();
+ writer.write(&normalize_batch(batch)).unwrap();
}
}
Ok(String::from_utf8(bytes).unwrap())
@@ -149,6 +151,6 @@ async fn run_query(ctx: &SessionContext, sql: impl Into<String>) -> Result<Strin
}
let df = ctx.sql(sql.as_str()).await.unwrap();
let results: Vec<RecordBatch> = df.collect().await.unwrap();
- let formatted_batches = format_batches(&results)?;
+ let formatted_batches = format_batches(results)?;
Ok(formatted_batches)
}
diff --git a/datafusion/core/tests/sqllogictests/src/normalize.rs b/datafusion/core/tests/sqllogictests/src/normalize.rs
new file mode 100644
index 0000000000..a5ed84d899
--- /dev/null
+++ b/datafusion/core/tests/sqllogictests/src/normalize.rs
@@ -0,0 +1,77 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use std::sync::Arc;
+
+use arrow::{
+ array::{
+ as_largestring_array, as_string_array, ArrayRef, LargeStringArray, StringArray,
+ },
+ datatypes::DataType,
+ record_batch::RecordBatch,
+};
+
+/// Normalizes the content of a RecordBatch prior to printing.
+///
+/// This is to make the output comparable to the semi-standard .slt format
+///
+/// Normalizations applied:
+/// 1. Null Values (TODO)
+/// 2. [Empty Strings]
+///
+/// [Empty Strings]: https://duckdb.org/dev/sqllogictest/result_verification#null-values-and-empty-strings
+pub fn normalize_batch(batch: RecordBatch) -> RecordBatch {
+ let new_columns = batch
+ .columns()
+ .iter()
+ .map(|array| {
+ match array.data_type() {
+ DataType::Utf8 => {
+ let arr: StringArray = as_string_array(array.as_ref())
+ .iter()
+ .map(normalize_string)
+ .collect();
+ Arc::new(arr) as ArrayRef
+ }
+ DataType::LargeUtf8 => {
+ let arr: LargeStringArray = as_largestring_array(array.as_ref())
+ .iter()
+ .map(normalize_string)
+ .collect();
+ Arc::new(arr) as ArrayRef
+ }
+ // todo normalize dictionary values
+
+ // no normalization on this type
+ _ => array.clone(),
+ }
+ })
+ .collect();
+
+ RecordBatch::try_new(batch.schema(), new_columns).expect("creating normalized batch")
+}
+
+fn normalize_string(v: Option<&str>) -> Option<&str> {
+ v.map(|v| {
+ // All empty strings are replaced with this value
+ if v.is_empty() {
+ "(empty)"
+ } else {
+ v
+ }
+ })
+}
diff --git a/datafusion/core/tests/sqllogictests/test_files/information_schema.slt b/datafusion/core/tests/sqllogictests/test_files/information_schema.slt
index 5ec28b162e..36202fb439 100644
--- a/datafusion/core/tests/sqllogictests/test_files/information_schema.slt
+++ b/datafusion/core/tests/sqllogictests/test_files/information_schema.slt
@@ -58,4 +58,4 @@ datafusion.execution.time_zone +00:00
query R
SHOW TIMEZONE
----
-datafusion.execution.time_zone +00:00
\ No newline at end of file
+datafusion.execution.time_zone +00:00
diff --git a/datafusion/core/tests/sqllogictests/test_files/misc.slt b/datafusion/core/tests/sqllogictests/test_files/misc.slt
new file mode 100644
index 0000000000..da902add93
--- /dev/null
+++ b/datafusion/core/tests/sqllogictests/test_files/misc.slt
@@ -0,0 +1,26 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+
+# http://www.apache.org/licenses/LICENSE-2.0
+
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+# Miscellaneous tests that should eventually live elsewhere
+
+# Show string normalization working
+# '' --> (empty)
+# '' --> NULL (TODO: needs https://github.com/apache/arrow-rs/issues/3268)
+query II
+select 'foo', '', NULL
+----
+foo (empty)
\ No newline at end of file