Skip to main content

datafusion_substrait/logical_plan/producer/expr/
lambda_variable.rs

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
18use datafusion::logical_expr::expr::LambdaVariable;
19use substrait::proto::{
20    Expression,
21    expression::{
22        FieldReference, ReferenceSegment, RexType,
23        field_reference::{LambdaParameterReference, ReferenceType, RootType},
24        reference_segment::{self, StructField},
25    },
26};
27
28use crate::logical_plan::producer::SubstraitProducer;
29
30pub fn from_lambda_variable(
31    producer: &mut impl SubstraitProducer,
32    lambda_variable: &LambdaVariable,
33    _schema: &datafusion::common::DFSchema,
34) -> Result<Expression, datafusion::error::DataFusionError> {
35    let (steps_out, field) = producer.lambda_variable(&lambda_variable.name)?;
36
37    Ok(Expression {
38        rex_type: Some(RexType::Selection(Box::new(FieldReference {
39            reference_type: Some(ReferenceType::DirectReference(ReferenceSegment {
40                reference_type: Some(reference_segment::ReferenceType::StructField(
41                    Box::new(StructField { field, child: None }),
42                )),
43            })),
44            root_type: Some(RootType::LambdaParameterReference(
45                LambdaParameterReference { steps_out },
46            )),
47        }))),
48    })
49}