Skip to content

Commit a355d4a

Browse files
committed
Add dummy UDFs for sqlintegration
1 parent dfaa978 commit a355d4a

File tree

1 file changed

+61
-4
lines changed

1 file changed

+61
-4
lines changed

datafusion/sql/tests/sql_integration.rs

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18+
use std::any::Any;
1819
#[cfg(test)]
1920
use std::collections::HashMap;
2021
use std::{sync::Arc, vec};
@@ -29,7 +30,8 @@ use datafusion_common::{
2930
use datafusion_common::{plan_err, ParamValues};
3031
use datafusion_expr::{
3132
logical_plan::{LogicalPlan, Prepare},
32-
AggregateUDF, ScalarUDF, TableSource, WindowUDF,
33+
AggregateUDF, ColumnarValue, ScalarUDF, ScalarUDFImpl, Signature, TableSource,
34+
Volatility, WindowUDF,
3335
};
3436
use datafusion_sql::{
3537
parser::DFParser,
@@ -2671,13 +2673,62 @@ fn logical_plan_with_dialect_and_options(
26712673
dialect: &dyn Dialect,
26722674
options: ParserOptions,
26732675
) -> Result<LogicalPlan> {
2674-
let context = MockContextProvider::default();
2676+
let context = MockContextProvider::default().with_udf(make_udf(
2677+
"nullif",
2678+
vec![DataType::Int32, DataType::Int32],
2679+
DataType::Int32,
2680+
));
2681+
26752682
let planner = SqlToRel::new_with_options(&context, options);
26762683
let result = DFParser::parse_sql_with_dialect(sql, dialect);
26772684
let mut ast = result?;
26782685
planner.statement_to_plan(ast.pop_front().unwrap())
26792686
}
26802687

2688+
fn make_udf(name: &'static str, args: Vec<DataType>, return_type: DataType) -> ScalarUDF {
2689+
ScalarUDF::new_from_impl(DummyUDF::new(name, args, return_type))
2690+
}
2691+
2692+
/// Mocked UDF
2693+
#[derive(Debug)]
2694+
struct DummyUDF {
2695+
name: &'static str,
2696+
signature: Signature,
2697+
return_type: DataType,
2698+
}
2699+
2700+
impl DummyUDF {
2701+
fn new(name: &'static str, args: Vec<DataType>, return_type: DataType) -> Self {
2702+
Self {
2703+
name,
2704+
signature: Signature::exact(args, Volatility::Immutable),
2705+
return_type,
2706+
}
2707+
}
2708+
}
2709+
2710+
impl ScalarUDFImpl for DummyUDF {
2711+
fn as_any(&self) -> &dyn Any {
2712+
self
2713+
}
2714+
2715+
fn name(&self) -> &str {
2716+
&self.name
2717+
}
2718+
2719+
fn signature(&self) -> &Signature {
2720+
&self.signature
2721+
}
2722+
2723+
fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
2724+
Ok(self.return_type.clone())
2725+
}
2726+
2727+
fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> {
2728+
unimplemented!("DummyUDF::invoke")
2729+
}
2730+
}
2731+
26812732
/// Create logical plan, write with formatter, compare to expected output
26822733
fn quick_test(sql: &str, expected: &str) {
26832734
let plan = logical_plan(sql).unwrap();
@@ -2724,13 +2775,19 @@ fn prepare_stmt_replace_params_quick_test(
27242775
#[derive(Default)]
27252776
struct MockContextProvider {
27262777
options: ConfigOptions,
2778+
udfs: HashMap<String, Arc<ScalarUDF>>,
27272779
udafs: HashMap<String, Arc<AggregateUDF>>,
27282780
}
27292781

27302782
impl MockContextProvider {
27312783
fn options_mut(&mut self) -> &mut ConfigOptions {
27322784
&mut self.options
27332785
}
2786+
2787+
fn with_udf(mut self, udf: ScalarUDF) -> Self {
2788+
self.udfs.insert(udf.name().to_string(), Arc::new(udf));
2789+
self
2790+
}
27342791
}
27352792

27362793
impl ContextProvider for MockContextProvider {
@@ -2823,8 +2880,8 @@ impl ContextProvider for MockContextProvider {
28232880
}
28242881
}
28252882

2826-
fn get_function_meta(&self, _name: &str) -> Option<Arc<ScalarUDF>> {
2827-
None
2883+
fn get_function_meta(&self, name: &str) -> Option<Arc<ScalarUDF>> {
2884+
self.udfs.get(name).map(Arc::clone)
28282885
}
28292886

28302887
fn get_aggregate_meta(&self, name: &str) -> Option<Arc<AggregateUDF>> {

0 commit comments

Comments
 (0)