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::SubstraitConsumer;
use crate::logical_plan::consumer::from_substrait_literal;
use crate::logical_plan::consumer::from_substrait_named_struct;
use crate::logical_plan::consumer::utils::ensure_schema_compatibility;
use datafusion::common::{
    DFSchema, DFSchemaRef, TableReference, not_impl_err, plan_err,
    substrait_datafusion_err, substrait_err,
};
use datafusion::datasource::provider_as_source;
use datafusion::logical_expr::utils::split_conjunction_owned;
use datafusion::logical_expr::{
    EmptyRelation, Expr, LogicalPlan, LogicalPlanBuilder, TableScanBuilder, Values,
};
use std::sync::Arc;
use substrait::proto::expression::MaskExpression;
use substrait::proto::read_rel::ReadType;
use substrait::proto::read_rel::local_files::file_or_files::PathType::UriFile;
use substrait::proto::{Expression, ReadRel};
use url::Url;

pub async fn from_read_rel(
    consumer: &impl SubstraitConsumer,
    read: &ReadRel,
) -> datafusion::common::Result<LogicalPlan> {
    async fn read_with_schema(
        consumer: &impl SubstraitConsumer,
        table_ref: TableReference,
        schema: DFSchema,
        projection: Option<&MaskExpression>,
        filter: Option<&Expression>,
    ) -> datafusion::common::Result<LogicalPlan> {
        let schema = schema.replace_qualifier(table_ref.clone());

        let filters = if let Some(f) = filter {
            let filter_expr = consumer.consume_expression(f, &schema).await?;
            split_conjunction_owned(filter_expr)
        } else {
            vec![]
        };

        let plan = {
            let provider = match consumer.resolve_table_ref(&table_ref).await? {
                Some(ref provider) => Arc::clone(provider),
                _ => return plan_err!("No table named '{table_ref}'"),
            };

            let table_scan = TableScanBuilder::new(
                table_ref,
                provider_as_source(Arc::clone(&provider)),
            )
            .with_filters(filters)
            .build()?;
            LogicalPlanBuilder::table_scan(table_scan)?.build()?
        };

        ensure_schema_compatibility(plan.schema(), schema.clone())?;

        let schema = apply_masking(schema, projection)?;

        apply_projection(plan, schema)
    }

    let named_struct = read.base_schema.as_ref().ok_or_else(|| {
        substrait_datafusion_err!("No base schema provided for Read Relation")
    })?;

    let substrait_schema = from_substrait_named_struct(consumer, named_struct)?;

    match &read.read_type {
        Some(ReadType::NamedTable(nt)) => {
            let table_reference = match nt.names.len() {
                0 => {
                    return plan_err!("No table name found in NamedTable");
                }
                1 => TableReference::Bare {
                    table: nt.names[0].clone().into(),
                },
                2 => TableReference::Partial {
                    schema: nt.names[0].clone().into(),
                    table: nt.names[1].clone().into(),
                },
                _ => TableReference::Full {
                    catalog: nt.names[0].clone().into(),
                    schema: nt.names[1].clone().into(),
                    table: nt.names[2].clone().into(),
                },
            };

            read_with_schema(
                consumer,
                table_reference,
                substrait_schema,
                read.projection.as_ref(),
                read.filter.as_deref(),
            )
            .await
        }
        Some(ReadType::VirtualTable(vt)) => {
            if vt.expressions.is_empty() {
                return Ok(LogicalPlan::EmptyRelation(EmptyRelation {
                    produce_one_row: false,
                    schema: DFSchemaRef::new(substrait_schema),
                }));
            }

            // Check for produce_one_row pattern
            // A VirtualTable with exactly one row containing only empty/default fields represents
            // an EmptyRelation with produce_one_row=true. This pattern is used for queries without
            // a FROM clause (e.g., "SELECT 1 AS one") where a single phantom row is needed to
            // provide a context for evaluating scalar expressions. This is conceptually similar to
            // the SQL "DUAL" table (see: https://en.wikipedia.org/wiki/DUAL_table) which some
            // databases provide as a single-row source for selecting constant expressions when no
            // real table is present.
            let is_produce_one_row = vt.expressions.len() == 1
                && substrait_schema.fields().is_empty()
                && vt.expressions[0].fields.is_empty();

            if is_produce_one_row {
                return Ok(LogicalPlan::EmptyRelation(EmptyRelation {
                    produce_one_row: true,
                    schema: DFSchemaRef::new(substrait_schema),
                }));
            }

            let mut rows = Vec::with_capacity(vt.expressions.len());
            for row in &vt.expressions {
                if row.fields.len() != substrait_schema.fields().len() {
                    return substrait_err!(
                        "Field count mismatch: expected {} fields but found {} in virtual table row",
                        substrait_schema.fields().len(),
                        row.fields.len()
                    );
                }

                let mut row_exprs = Vec::with_capacity(row.fields.len());
                let mut name_idx = 0;
                for expression in &row.fields {
                    // Top-level names are provided through schema
                    // Each expression consumes at least one name, and Literals may consume additional names.
                    name_idx += 1;
                    let expr = match expression.rex_type.as_ref() {
                        Some(substrait::proto::expression::RexType::Literal(lit)) => {
                            // Literal expressions need 'named_struct.names' so nested struct fields
                            // keep their names from the ReadRel base schema.
                            Expr::Literal(
                                from_substrait_literal(
                                    consumer,
                                    lit,
                                    &named_struct.names,
                                    &mut name_idx,
                                )?,
                                None,
                            )
                        }
                        _ => {
                            consumer
                                .consume_expression(expression, &substrait_schema)
                                .await?
                        }
                    };
                    row_exprs.push(expr);
                }

                if name_idx != named_struct.names.len() {
                    return substrait_err!(
                        "Names list must match exactly to nested schema, but found {} uses for {} names",
                        name_idx,
                        named_struct.names.len()
                    );
                }
                rows.push(row_exprs);
            }

            Ok(LogicalPlan::Values(Values {
                schema: DFSchemaRef::new(substrait_schema),
                values: rows,
            }))
        }
        Some(ReadType::LocalFiles(lf)) => {
            fn extract_filename(name: &str) -> Option<String> {
                let corrected_url =
                    if name.starts_with("file://") && !name.starts_with("file:///") {
                        name.replacen("file://", "file:///", 1)
                    } else {
                        name.to_string()
                    };

                Url::parse(&corrected_url).ok().and_then(|url| {
                    let path = url.path();
                    std::path::Path::new(path)
                        .file_name()
                        .map(|filename| filename.to_string_lossy().to_string())
                })
            }

            // we could use the file name to check the original table provider
            // TODO: currently does not support multiple local files
            let filename: Option<String> =
                lf.items.first().and_then(|x| match x.path_type.as_ref() {
                    Some(UriFile(name)) => extract_filename(name),
                    _ => None,
                });

            if lf.items.len() > 1 || filename.is_none() {
                return not_impl_err!("Only single file reads are supported");
            }
            let name = filename.unwrap();
            // directly use unwrap here since we could determine it is a valid one
            let table_reference = TableReference::Bare { table: name.into() };

            read_with_schema(
                consumer,
                table_reference,
                substrait_schema,
                read.projection.as_ref(),
                read.filter.as_deref(),
            )
            .await
        }
        _ => {
            not_impl_err!("Unsupported Readtype: {:?}", read.read_type)
        }
    }
}

pub fn apply_masking(
    schema: DFSchema,
    mask_expression: Option<&MaskExpression>,
) -> datafusion::common::Result<DFSchema> {
    match mask_expression {
        Some(MaskExpression { select, .. }) => match &select.as_ref() {
            Some(projection) => {
                let column_indices: Vec<usize> = projection
                    .struct_items
                    .iter()
                    .map(|item| item.field as usize)
                    .collect();

                let fields = column_indices
                    .iter()
                    .map(|i| schema.qualified_field(*i))
                    .map(|(qualifier, field)| (qualifier.cloned(), Arc::clone(field)))
                    .collect();

                Ok(DFSchema::new_with_metadata(
                    fields,
                    schema.metadata().clone(),
                )?)
            }
            None => Ok(schema),
        },
        None => Ok(schema),
    }
}

/// This function returns a DataFrame with fields adjusted if necessary in the event that the
/// Substrait schema is a subset of the DataFusion schema.
fn apply_projection(
    plan: LogicalPlan,
    substrait_schema: DFSchema,
) -> datafusion::common::Result<LogicalPlan> {
    let df_schema = plan.schema();

    if df_schema.logically_equivalent_names_and_types(&substrait_schema) {
        return Ok(plan);
    }

    let df_schema = df_schema.to_owned();

    match plan {
        LogicalPlan::TableScan(mut scan) => {
            let column_indices: Vec<usize> = substrait_schema
                .strip_qualifiers()
                .fields()
                .iter()
                .map(|substrait_field| {
                    Ok(df_schema
                        .index_of_column_by_name(None, substrait_field.name().as_str())
                        .unwrap())
                })
                .collect::<datafusion::common::Result<_>>()?;

            let fields = column_indices
                .iter()
                .map(|i| df_schema.qualified_field(*i))
                .map(|(qualifier, field)| (qualifier.cloned(), Arc::clone(field)))
                .collect();

            scan.projected_schema = DFSchemaRef::new(DFSchema::new_with_metadata(
                fields,
                df_schema.metadata().clone(),
            )?);
            scan.projection = Some(column_indices);

            Ok(LogicalPlan::TableScan(scan))
        }
        _ => plan_err!("DataFrame passed to apply_projection must be a TableScan"),
    }
}