wqc200 edited a comment on pull request #9531:
URL: https://github.com/apache/arrow/pull/9531#issuecomment-782699720
@alamb
I'm using it in struct
```
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 database_function = move |_args: &[ArrayRef]| {
let res = StringArray::from(vec![Some(self.db_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(())
}
```
but there's an error
> error[E0759]: `self` has an anonymous lifetime `'_` but it needs to
satisfy a `'static` lifetime requirement
> --> datafusion/tests/func_provider.rs:43:23
> |
> 43 | pub async fn test(&self, expected_name: String) -> Result<()> {
> | ^^^^^
> | |
> | this data with an anonymous lifetime `'_`...
> | ...is captured here...
> ...
> 56 |
ActualFunction::FuncImpl(make_scalar_function(database_function))), // function
implementation
> | -------------------- ...and is
required to live as long as `'static` here
I think the function pointer doesn't support this syntactic sugar, so i want
to add a provider that supports context variables.
----------------------------------------------------------------
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]