|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +use crate::serde::protobuf::{ |
| 19 | + ScalarUdfDocumentation, ScalarUdfDocumentationArgument, ScalarUdfInfo, |
| 20 | + ScalarUdfTypeSignature, |
| 21 | +}; |
| 22 | +use datafusion::execution::FunctionRegistry; |
| 23 | +use datafusion::functions::all_default_functions; |
| 24 | +use datafusion::prelude::SessionContext; |
| 25 | +use datafusion_proto_common::ArrowType; |
| 26 | +use std::collections::HashSet; |
| 27 | + |
| 28 | +/// Used to serialize function shapes to ship to Ballista clients |
| 29 | +pub trait RemoteFunctionSerializeExt { |
| 30 | + fn serialize_udfs(&self) -> Vec<ScalarUdfInfo>; |
| 31 | +} |
| 32 | + |
| 33 | +impl RemoteFunctionSerializeExt for SessionContext { |
| 34 | + fn serialize_udfs(&self) -> Vec<ScalarUdfInfo> { |
| 35 | + let mut udfs = vec![]; |
| 36 | + |
| 37 | + let skip = all_default_functions() |
| 38 | + .iter() |
| 39 | + .map(|f| f.name().to_string()) |
| 40 | + .collect::<HashSet<_>>(); |
| 41 | + |
| 42 | + for udf in self.udfs() { |
| 43 | + if skip.contains(&udf) { |
| 44 | + continue; |
| 45 | + } |
| 46 | + |
| 47 | + let f = self.udf(&udf).expect("Must find defined UDF"); |
| 48 | + let signature = f.signature(); |
| 49 | + let signatures = signature |
| 50 | + .type_signature |
| 51 | + .get_example_types() |
| 52 | + .into_iter() |
| 53 | + .filter_map(|t| { |
| 54 | + let arity = t |
| 55 | + .iter() |
| 56 | + .map(TryInto::try_into) |
| 57 | + .collect::<Result<Vec<ArrowType>, _>>() |
| 58 | + .expect("Must serialize data types"); |
| 59 | + |
| 60 | + // TODO: some functions use `ScalarUDF::return_field_from_args`, which this does not support |
| 61 | + f.return_type(&t) |
| 62 | + .ok() |
| 63 | + .and_then(|ref return_type| return_type.try_into().ok()) |
| 64 | + .map(|arrow_return_type| ScalarUdfTypeSignature { |
| 65 | + arity, |
| 66 | + return_type: Some(arrow_return_type), |
| 67 | + }) |
| 68 | + }) |
| 69 | + .collect::<Vec<_>>(); |
| 70 | + |
| 71 | + let docs = f.documentation().map(|d| { |
| 72 | + let arguments = d |
| 73 | + .arguments |
| 74 | + .iter() |
| 75 | + .flatten() |
| 76 | + .map(|(arg, desc)| ScalarUdfDocumentationArgument { |
| 77 | + argument: arg.clone(), |
| 78 | + description: desc.clone(), |
| 79 | + }) |
| 80 | + .collect::<Vec<_>>(); |
| 81 | + |
| 82 | + ScalarUdfDocumentation { |
| 83 | + description: d.description.clone(), |
| 84 | + syntax_example: d.syntax_example.clone(), |
| 85 | + sql_example: d.sql_example.clone(), |
| 86 | + arguments, |
| 87 | + } |
| 88 | + }); |
| 89 | + |
| 90 | + udfs.push(ScalarUdfInfo { |
| 91 | + name: f.name().to_string(), |
| 92 | + documentation: docs, |
| 93 | + signatures, |
| 94 | + }); |
| 95 | + } |
| 96 | + |
| 97 | + udfs |
| 98 | + } |
| 99 | +} |
0 commit comments