datafusion-substrait 56.0.0

DataFusion Substrait Producer and Consumer
Documentation
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements

use crate::logical_plan::consumer::{NameTracker, SubstraitConsumer};
use crate::logical_plan::consumer::{from_substrait_agg_func, from_substrait_sorts};
use datafusion::common::{Column, DFSchemaRef, internal_err, not_impl_err};
use datafusion::logical_expr::builder::project;
use datafusion::logical_expr::{
    Aggregate, Expr, GroupingSet, LogicalPlan, LogicalPlanBuilder,
};
use substrait::proto::AggregateRel;
use substrait::proto::aggregate_function::AggregationInvocation;
use substrait::proto::aggregate_rel::Grouping;

pub async fn from_aggregate_rel(
    consumer: &impl SubstraitConsumer,
    agg: &AggregateRel,
) -> datafusion::common::Result<LogicalPlan> {
    if let Some(input) = agg.input.as_ref() {
        let input = LogicalPlanBuilder::from(consumer.consume_rel(input).await?);
        let mut ref_group_exprs = Vec::with_capacity(agg.grouping_expressions.len());

        for e in &agg.grouping_expressions {
            let x = consumer.consume_expression(e, input.schema()).await?;
            ref_group_exprs.push(x);
        }

        let mut group_exprs = vec![];
        let mut aggr_exprs = Vec::with_capacity(agg.measures.len());

        match agg.groupings.len() {
            0 => {}
            1 => {
                group_exprs.extend_from_slice(
                    &from_substrait_grouping(
                        consumer,
                        &agg.groupings[0],
                        &ref_group_exprs,
                        input.schema(),
                    )
                    .await?,
                );
            }
            _ => {
                let mut grouping_sets = Vec::with_capacity(agg.groupings.len());
                for grouping in &agg.groupings {
                    let grouping_set = from_substrait_grouping(
                        consumer,
                        grouping,
                        &ref_group_exprs,
                        input.schema(),
                    )
                    .await?;
                    grouping_sets.push(grouping_set);
                }
                // Single-element grouping expression of type Expr::GroupingSet.
                // Note that GroupingSet::Rollup would become GroupingSet::GroupingSets, when
                // parsed by the producer and consumer, since Substrait does not have a type dedicated
                // to ROLLUP. Only vector of Groupings (grouping sets) is available.
                group_exprs
                    .push(Expr::GroupingSet(GroupingSet::GroupingSets(grouping_sets)));
            }
        }

        for m in &agg.measures {
            let filter = match &m.filter {
                Some(fil) => Some(Box::new(
                    consumer.consume_expression(fil, input.schema()).await?,
                )),
                None => None,
            };
            let agg_func = match &m.measure {
                Some(f) => {
                    let distinct = match f.invocation {
                        _ if f.invocation == AggregationInvocation::Distinct as i32 => {
                            true
                        }
                        _ if f.invocation == AggregationInvocation::All as i32 => false,
                        _ => false,
                    };
                    let order_by =
                        from_substrait_sorts(consumer, &f.sorts, input.schema()).await?;

                    from_substrait_agg_func(
                        consumer,
                        f,
                        input.schema(),
                        filter,
                        order_by,
                        distinct,
                    )
                    .await
                }
                None => {
                    not_impl_err!("Aggregate without aggregate function is not supported")
                }
            };
            aggr_exprs.push(std::sync::Arc::unwrap_or_clone(agg_func?));
        }

        // Ensure that all expressions have a unique name. Both grouping and
        // aggregate expressions become fields in the aggregate's output schema,
        // so they share a single namespace.
        let mut name_tracker = NameTracker::new();
        let group_exprs = group_exprs
            .into_iter()
            .map(|e| name_tracker.get_uniquely_named_expr(e))
            .collect::<Result<Vec<Expr>, _>>()?;
        let aggr_exprs = aggr_exprs
            .into_iter()
            .map(|e| name_tracker.get_uniquely_named_expr(e))
            .collect::<Result<Vec<Expr>, _>>()?;

        let plan = input.aggregate(group_exprs, aggr_exprs)?.build()?;
        if agg.groupings.len() > 1 {
            reorder_grouping_set_output(plan, agg.measures.len())
        } else {
            Ok(plan)
        }
    } else {
        not_impl_err!("Aggregate without an input is not valid")
    }
}

/// Reorders DataFusion's `[groups, grouping_id, measures]` aggregate schema to
/// Substrait's direct output order of `[groups, measures, grouping_id]`.
fn reorder_grouping_set_output(
    plan: LogicalPlan,
    measure_count: usize,
) -> datafusion::common::Result<LogicalPlan> {
    let exprs: Vec<Expr> = {
        let schema = plan.schema();
        let Some(grouping_id_index) =
            schema.index_of_column_by_name(None, Aggregate::INTERNAL_GROUPING_ID)
        else {
            return internal_err!(
                "Grouping set aggregate schema is missing {}",
                Aggregate::INTERNAL_GROUPING_ID
            );
        };
        if grouping_id_index + measure_count + 1 != schema.fields().len() {
            return internal_err!(
                "Grouping set aggregate schema has {} fields after {}, expected {} measures",
                schema.fields().len() - grouping_id_index - 1,
                Aggregate::INTERNAL_GROUPING_ID,
                measure_count
            );
        }

        (0..grouping_id_index)
            .chain(grouping_id_index + 1..schema.fields().len())
            .chain(std::iter::once(grouping_id_index))
            .map(|index| Expr::Column(Column::from(schema.qualified_field(index))))
            .collect()
    };
    project(plan, exprs)
}

#[expect(deprecated)]
async fn from_substrait_grouping(
    consumer: &impl SubstraitConsumer,
    grouping: &Grouping,
    expressions: &[Expr],
    input_schema: &DFSchemaRef,
) -> datafusion::common::Result<Vec<Expr>> {
    let mut group_exprs = vec![];
    if !grouping.grouping_expressions.is_empty() {
        for e in &grouping.grouping_expressions {
            let expr = consumer.consume_expression(e, input_schema).await?;
            group_exprs.push(expr);
        }
        return Ok(group_exprs);
    }
    for idx in &grouping.expression_references {
        let e = &expressions[*idx as usize];
        group_exprs.push(e.clone());
    }
    Ok(group_exprs)
}