Skip to main content

datafusion_python/
analyzer.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
18//! Analyzer rules layered on top of DataFusion's defaults.
19
20use datafusion::common::Result;
21use datafusion::common::config::ConfigOptions;
22use datafusion::logical_expr::LogicalPlan;
23use datafusion::optimizer::AnalyzerRule;
24
25/// Resolve [`LambdaVariable`] references into bound lambda parameters.
26///
27/// DataFusion's SQL planner resolves lambda variables inline as it plans a
28/// higher-order function call, so SQL-built plans never carry unresolved
29/// variables. Plans assembled programmatically through the Python expression
30/// builder (e.g. `array_transform(col("xs"), lambda_(["v"], lambda_var("v")))`)
31/// do carry them, and nothing in the default analyzer resolves them. This rule
32/// runs [`LogicalPlan::resolve_lambda_variables`] so both construction paths
33/// reach the optimizer with bound lambdas.
34///
35/// [`LambdaVariable`]: datafusion::logical_expr::expr::LambdaVariable
36#[derive(Debug)]
37pub struct ResolveLambdaVariables {}
38
39impl ResolveLambdaVariables {
40    pub fn new() -> Self {
41        Self {}
42    }
43}
44
45impl Default for ResolveLambdaVariables {
46    fn default() -> Self {
47        Self::new()
48    }
49}
50
51impl AnalyzerRule for ResolveLambdaVariables {
52    fn analyze(&self, plan: LogicalPlan, _config: &ConfigOptions) -> Result<LogicalPlan> {
53        plan.resolve_lambda_variables().map(|t| t.data)
54    }
55
56    fn name(&self) -> &str {
57        "resolve_lambda_variables"
58    }
59}