Apache DataFusion SQL Query Engine
Coerce expressions to udtf (#19915)
Xiangpeng Hao9f27e933ae97
diff --git a/datafusion/core/src/execution/session_state.rs b/datafusion/core/src/execution/session_state.rsindex 7cdbc77ae9..0de2ace4f2 100644--- a/datafusion/core/src/execution/session_state.rs+++ b/datafusion/core/src/execution/session_state.rs@@ -1843,9 +1843,14 @@ impl ContextProvider for SessionContextProvider<'_> {self.state.execution_props().query_execution_start_time,);let simplifier = ExprSimplifier::new(simplify_context);+ let schema = DFSchema::empty();let args = args.into_iter()- .map(|arg| simplifier.simplify(arg))+ .map(|arg| {+ simplifier+ .coerce(arg, &schema)+ .and_then(|e| simplifier.simplify(e))+ }).collect::<datafusion_common::Result<Vec<_>>>()?;let provider = tbl_func.create_table_provider(&args)?;diff --git a/datafusion/core/tests/user_defined/user_defined_table_functions.rs b/datafusion/core/tests/user_defined/user_defined_table_functions.rsindex 8be8609c62..95694d00a6 100644--- a/datafusion/core/tests/user_defined/user_defined_table_functions.rs+++ b/datafusion/core/tests/user_defined/user_defined_table_functions.rs@@ -221,6 +221,31 @@ impl TableFunctionImpl for SimpleCsvTableFunc {}}+/// Test that expressions passed to UDTFs are properly type-coerced+/// This is a regression test for https://github.com/apache/datafusion/issues/19914+#[tokio::test]+async fn test_udtf_type_coercion() -> Result<()> {+ use datafusion::datasource::MemTable;++ #[derive(Debug)]+ struct NoOpTableFunc;++ impl TableFunctionImpl for NoOpTableFunc {+ fn call(&self, _: &[Expr]) -> Result<Arc<dyn TableProvider>> {+ let schema = Arc::new(arrow::datatypes::Schema::empty());+ Ok(Arc::new(MemTable::try_new(schema, vec![vec![]])?))+ }+ }++ let ctx = SessionContext::new();+ ctx.register_udtf("f", Arc::new(NoOpTableFunc));++ // This should not panic - the array elements should be coerced to Float64+ let _ = ctx.sql("SELECT * FROM f(ARRAY[0.1, 1, 2])").await?;++ Ok(())+}+fn read_csv_batches(csv_path: impl AsRef<Path>) -> Result<(SchemaRef, Vec<RecordBatch>)> {let mut file = File::open(csv_path)?;let (schema, _) = Format::default()