|
| 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 datafusion::{ |
| 19 | + common::{DFSchema, substrait_err}, |
| 20 | + prelude::{Expr, lambda}, |
| 21 | +}; |
| 22 | +use substrait::proto; |
| 23 | + |
| 24 | +use crate::logical_plan::consumer::SubstraitConsumer; |
| 25 | + |
| 26 | +pub async fn from_lambda( |
| 27 | + consumer: &impl SubstraitConsumer, |
| 28 | + expr: &proto::expression::Lambda, |
| 29 | + input_schema: &DFSchema, |
| 30 | +) -> datafusion::common::Result<Expr> { |
| 31 | + let Some(parameters) = expr.parameters.as_ref() else { |
| 32 | + return substrait_err!("Lambda expression without parameters is not allowed"); |
| 33 | + }; |
| 34 | + |
| 35 | + let (names, consumer_with_parameters) = |
| 36 | + consumer.with_lambda_parameters(¶meters.types, input_schema)?; |
| 37 | + |
| 38 | + let Some(body) = expr.body.as_ref() else { |
| 39 | + return substrait_err!("Lambda expression without body is not allowed"); |
| 40 | + }; |
| 41 | + |
| 42 | + let body = consumer_with_parameters |
| 43 | + .consume_expression(body, input_schema) |
| 44 | + .await?; |
| 45 | + |
| 46 | + Ok(lambda(names, body)) |
| 47 | +} |
0 commit comments