alamb commented on pull request #9531:
URL: https://github.com/apache/arrow/pull/9531#issuecomment-782837158
Hi @wqc200 -- I think the compiler issue in your example is due to the
reference of `self` in the wrapped closure.
If you pass in a `clone` of the string, then the example compiles and passes:
```rust
use std::sync::Arc;
use arrow::array::ArrayRef;
use arrow::array::StringArray;
use arrow::datatypes::DataType;
use arrow::util::display::array_value_to_string;
use datafusion::error::Result;
use datafusion::execution::context::ExecutionContext;
use datafusion::logical_plan::create_udf;
use datafusion::physical_plan::functions::ActualFunction;
use datafusion::physical_plan::functions::make_scalar_function;
#[derive(Debug, Clone)]
pub struct TestProvider {
db_name: String,
}
impl TestProvider {
pub fn try_new(db_name: &str) -> Result<Self> {
Ok(Self {
db_name: db_name.to_string(),
})
}
pub async fn test(&self, expected_name: String) -> Result<()> {
let mut ctx = ExecutionContext::new();
// implementation of `database()` function that returns expected_name
let captured_name = self.db_name.clone();
let database_function = move |_args: &[ArrayRef]| {
let res = StringArray::from(vec![Some(captured_name.as_str())]);
Ok(Arc::new(res) as ArrayRef)
};
ctx.register_udf(create_udf(
"database", // fuenction name
vec![], // input argument types
Arc::new(DataType::Utf8), // output type
ActualFunction::FuncImpl(make_scalar_function(database_function))), // function
implementation
);
let sql_results = ctx.sql(&format!("select
database()"))?.collect().await?;
let sql_result_col = array_value_to_string(sql_results[0].column(0),
0).unwrap();
assert_eq!(sql_result_col, expected_name);
Ok(())
}
}
#[tokio::test]
async fn test_func_provider_results() -> Result<()> {
let p = TestProvider::try_new("test")?;
p.test("test".to_string());
Ok(())
}
```
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]