Skip to content

Apache DataFusion SQL Query Engine

fix: Preserves field metadata when creating logical plan for VALUES expression (#17525)

Kristin Cowalcijk293bf3e42c1a
diff --git a/datafusion/core/tests/user_defined/user_defined_scalar_functions.rs b/datafusion/core/tests/user_defined/user_defined_scalar_functions.rs
index e1c7a875e0..1e0515356b 100644
--- a/datafusion/core/tests/user_defined/user_defined_scalar_functions.rs
+++ b/datafusion/core/tests/user_defined/user_defined_scalar_functions.rs
@@ -1833,6 +1833,87 @@ async fn test_config_options_work_for_scalar_func() -> Result<()> {
Ok(())
}
+/// https://github.com/apache/datafusion/issues/17425
+#[tokio::test]
+async fn test_extension_metadata_preserve_in_sql_values() -> Result<()> {
+ #[derive(Debug, Hash, PartialEq, Eq)]
+ struct MakeExtension {
+ signature: Signature,
+ }
+
+ impl Default for MakeExtension {
+ fn default() -> Self {
+ Self {
+ signature: Signature::user_defined(Volatility::Immutable),
+ }
+ }
+ }
+
+ impl ScalarUDFImpl for MakeExtension {
+ fn as_any(&self) -> &dyn Any {
+ self
+ }
+
+ fn name(&self) -> &str {
+ "make_extension"
+ }
+
+ fn signature(&self) -> &Signature {
+ &self.signature
+ }
+
+ fn coerce_types(&self, arg_types: &[DataType]) -> Result<Vec<DataType>> {
+ Ok(arg_types.to_vec())
+ }
+
+ fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
+ unreachable!("This shouldn't have been called")
+ }
+
+ fn return_field_from_args(&self, args: ReturnFieldArgs) -> Result<FieldRef> {
+ Ok(args.arg_fields[0]
+ .as_ref()
+ .clone()
+ .with_metadata(HashMap::from([(
+ "ARROW:extension:metadata".to_string(),
+ "foofy.foofy".to_string(),
+ )]))
+ .into())
+ }
+
+ fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
+ Ok(args.args[0].clone())
+ }
+ }
+
+ let ctx = SessionContext::new();
+ ctx.register_udf(MakeExtension::default().into());
+
+ let batches = ctx
+ .sql(
+ "
+SELECT extension FROM (VALUES
+ ('one', make_extension('foofy one')),
+ ('two', make_extension('foofy two')),
+ ('three', make_extension('foofy three')))
+AS t(string, extension)
+ ",
+ )
+ .await?
+ .collect()
+ .await?;
+
+ assert_eq!(
+ batches[0]
+ .schema()
+ .field(0)
+ .metadata()
+ .get("ARROW:extension:metadata"),
+ Some(&"foofy.foofy".into())
+ );
+ Ok(())
+}
+
/// https://github.com/apache/datafusion/issues/17422
#[tokio::test]
async fn test_extension_metadata_preserve_in_subquery() -> Result<()> {
diff --git a/datafusion/expr/src/logical_plan/builder.rs b/datafusion/expr/src/logical_plan/builder.rs
index ac699b9d30..3b4fd1aff9 100644
--- a/datafusion/expr/src/logical_plan/builder.rs
+++ b/datafusion/expr/src/logical_plan/builder.rs
@@ -25,7 +25,7 @@ use std::iter::once;
use std::sync::Arc;
use crate::dml::CopyTo;
-use crate::expr::{Alias, PlannedReplaceSelectItem, Sort as SortExpr};
+use crate::expr::{Alias, FieldMetadata, PlannedReplaceSelectItem, Sort as SortExpr};
use crate::expr_rewriter::{
coerce_plan_expr_for_schema, normalize_col,
normalize_col_with_schemas_and_ambiguity_check, normalize_cols, normalize_sorts,
@@ -306,8 +306,17 @@ impl LogicalPlanBuilder {
for j in 0..n_cols {
let mut common_type: Option<DataType> = None;
+ let mut common_metadata: Option<FieldMetadata> = None;
for (i, row) in values.iter().enumerate() {
let value = &row[j];
+ let metadata = value.metadata(&schema)?;
+ if let Some(ref cm) = common_metadata {
+ if &metadata != cm {
+ return plan_err!("Inconsistent metadata across values list at row {i} column {j}. Was {:?} but found {:?}", cm, metadata);
+ }
+ } else {
+ common_metadata = Some(metadata.clone());
+ }
let data_type = value.get_type(&schema)?;
if data_type == DataType::Null {
continue;
@@ -326,7 +335,11 @@ impl LogicalPlanBuilder {
}
// assuming common_type was not set, and no error, therefore the type should be NULL
// since the code loop skips NULL
- fields.push(common_type.unwrap_or(DataType::Null), true);
+ fields.push_with_metadata(
+ common_type.unwrap_or(DataType::Null),
+ true,
+ common_metadata,
+ );
}
Self::infer_inner(values, fields, &schema)
@@ -1507,10 +1520,23 @@ impl ValuesFields {
}
pub fn push(&mut self, data_type: DataType, nullable: bool) {
+ self.push_with_metadata(data_type, nullable, None);
+ }
+
+ pub fn push_with_metadata(
+ &mut self,
+ data_type: DataType,
+ nullable: bool,
+ metadata: Option<FieldMetadata>,
+ ) {
// Naming follows the convention described here:
// https://www.postgresql.org/docs/current/queries-values.html
let name = format!("column{}", self.inner.len() + 1);
- self.inner.push(Field::new(name, data_type, nullable));
+ let mut field = Field::new(name, data_type, nullable);
+ if let Some(metadata) = metadata {
+ field.set_metadata(metadata.to_hashmap());
+ }
+ self.inner.push(field);
}
pub fn into_fields(self) -> Fields {
@@ -2153,7 +2179,10 @@ pub fn unnest_with_options(
#[cfg(test)]
mod tests {
+ use std::vec;
+
use super::*;
+ use crate::lit_with_metadata;
use crate::logical_plan::StringifiedPlan;
use crate::{col, expr, expr_fn::exists, in_subquery, lit, scalar_subquery};
@@ -2773,6 +2802,35 @@ mod tests {
Ok(())
}
+ #[test]
+ fn test_values_metadata() -> Result<()> {
+ let metadata: HashMap<String, String> =
+ [("ARROW:extension:metadata".to_string(), "test".to_string())]
+ .into_iter()
+ .collect();
+ let metadata = FieldMetadata::from(metadata);
+ let values = LogicalPlanBuilder::values(vec![
+ vec![lit_with_metadata(1, Some(metadata.clone()))],
+ vec![lit_with_metadata(2, Some(metadata.clone()))],
+ ])?
+ .build()?;
+ assert_eq!(*values.schema().field(0).metadata(), metadata.to_hashmap());
+
+ // Do not allow VALUES with different metadata mixed together
+ let metadata2: HashMap<String, String> =
+ [("ARROW:extension:metadata".to_string(), "test2".to_string())]
+ .into_iter()
+ .collect();
+ let metadata2 = FieldMetadata::from(metadata2);
+ assert!(LogicalPlanBuilder::values(vec![
+ vec![lit_with_metadata(1, Some(metadata.clone()))],
+ vec![lit_with_metadata(2, Some(metadata2.clone()))],
+ ])
+ .is_err());
+
+ Ok(())
+ }
+
#[test]
fn test_unique_field_aliases() {
let t1_field_1 = Field::new("a", DataType::Int32, false);