Skip to content

Commit 8eb5b21

Browse files
committed
Add tests for the fix
1 parent 6832a26 commit 8eb5b21

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

datafusion/core/tests/user_defined/user_defined_scalar_functions.rs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1832,3 +1832,84 @@ async fn test_config_options_work_for_scalar_func() -> Result<()> {
18321832

18331833
Ok(())
18341834
}
1835+
1836+
/// https://github.com/apache/datafusion/issues/17425
1837+
#[tokio::test]
1838+
async fn test_extension_metadata_preserve_in_sql_values() -> Result<()> {
1839+
#[derive(Debug, Hash, PartialEq, Eq)]
1840+
struct MakeExtension {
1841+
signature: Signature,
1842+
}
1843+
1844+
impl Default for MakeExtension {
1845+
fn default() -> Self {
1846+
Self {
1847+
signature: Signature::user_defined(Volatility::Immutable),
1848+
}
1849+
}
1850+
}
1851+
1852+
impl ScalarUDFImpl for MakeExtension {
1853+
fn as_any(&self) -> &dyn Any {
1854+
self
1855+
}
1856+
1857+
fn name(&self) -> &str {
1858+
"make_extension"
1859+
}
1860+
1861+
fn signature(&self) -> &Signature {
1862+
&self.signature
1863+
}
1864+
1865+
fn coerce_types(&self, arg_types: &[DataType]) -> Result<Vec<DataType>> {
1866+
Ok(arg_types.to_vec())
1867+
}
1868+
1869+
fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
1870+
unreachable!("This shouldn't have been called")
1871+
}
1872+
1873+
fn return_field_from_args(&self, args: ReturnFieldArgs) -> Result<FieldRef> {
1874+
Ok(args.arg_fields[0]
1875+
.as_ref()
1876+
.clone()
1877+
.with_metadata(HashMap::from([(
1878+
"ARROW:extension:metadata".to_string(),
1879+
"foofy.foofy".to_string(),
1880+
)]))
1881+
.into())
1882+
}
1883+
1884+
fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
1885+
Ok(args.args[0].clone())
1886+
}
1887+
}
1888+
1889+
let ctx = SessionContext::new();
1890+
ctx.register_udf(MakeExtension::default().into());
1891+
1892+
let batches = ctx
1893+
.sql(
1894+
"
1895+
SELECT extension FROM (VALUES
1896+
('one', make_extension('foofy one')),
1897+
('two', make_extension('foofy two')),
1898+
('three', make_extension('foofy three')))
1899+
AS t(string, extension)
1900+
",
1901+
)
1902+
.await?
1903+
.collect()
1904+
.await?;
1905+
1906+
assert_eq!(
1907+
batches[0]
1908+
.schema()
1909+
.field(0)
1910+
.metadata()
1911+
.get("ARROW:extension:metadata"),
1912+
Some(&"foofy.foofy".into())
1913+
);
1914+
Ok(())
1915+
}

datafusion/expr/src/logical_plan/builder.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2163,7 +2163,10 @@ pub fn unnest_with_options(
21632163

21642164
#[cfg(test)]
21652165
mod tests {
2166+
use std::vec;
2167+
21662168
use super::*;
2169+
use crate::lit_with_metadata;
21672170
use crate::logical_plan::StringifiedPlan;
21682171
use crate::{col, expr, expr_fn::exists, in_subquery, lit, scalar_subquery};
21692172

@@ -2810,4 +2813,20 @@ mod tests {
28102813

28112814
Ok(())
28122815
}
2816+
2817+
#[test]
2818+
fn test_values_metadata() -> Result<()> {
2819+
let metadata: HashMap<String, String> =
2820+
[("ARROW:extension:metadata".to_string(), "test".to_string())]
2821+
.into_iter()
2822+
.collect();
2823+
let metadata = FieldMetadata::from(metadata);
2824+
let values = LogicalPlanBuilder::values(vec![
2825+
vec![lit_with_metadata(1, Some(metadata.clone()))],
2826+
vec![lit_with_metadata(2, Some(metadata.clone()))],
2827+
])?
2828+
.build()?;
2829+
assert_eq!(*values.schema().field(0).metadata(), metadata.to_hashmap());
2830+
Ok(())
2831+
}
28132832
}

0 commit comments

Comments
 (0)