|
| 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 async_recursion::async_recursion; |
| 19 | +use chrono::DateTime; |
| 20 | +use datafusion::arrow::datatypes::Schema; |
| 21 | +use datafusion::datasource::listing::PartitionedFile; |
| 22 | +use datafusion::datasource::object_store::ObjectStoreUrl; |
| 23 | +use datafusion::error::{DataFusionError, Result}; |
| 24 | +use datafusion::physical_plan::file_format::{FileScanConfig, ParquetExec}; |
| 25 | +use datafusion::physical_plan::ExecutionPlan; |
| 26 | +use datafusion::prelude::SessionContext; |
| 27 | +use object_store::ObjectMeta; |
| 28 | +use std::collections::HashMap; |
| 29 | +use std::sync::Arc; |
| 30 | +use substrait::proto::read_rel::local_files::file_or_files::PathType; |
| 31 | +use substrait::proto::{ |
| 32 | + expression::MaskExpression, read_rel::ReadType, rel::RelType, Rel, |
| 33 | +}; |
| 34 | + |
| 35 | +/// Convert Substrait Rel to DataFusion ExecutionPlan |
| 36 | +#[async_recursion] |
| 37 | +pub async fn from_substrait_rel( |
| 38 | + _ctx: &mut SessionContext, |
| 39 | + rel: &Rel, |
| 40 | + _extensions: &HashMap<u32, &String>, |
| 41 | +) -> Result<Arc<dyn ExecutionPlan>> { |
| 42 | + match &rel.rel_type { |
| 43 | + Some(RelType::Read(read)) => { |
| 44 | + if read.filter.is_some() || read.best_effort_filter.is_some() { |
| 45 | + return Err(DataFusionError::NotImplemented( |
| 46 | + "Read with filter is not supported".to_string(), |
| 47 | + )); |
| 48 | + } |
| 49 | + if read.base_schema.is_some() { |
| 50 | + return Err(DataFusionError::NotImplemented( |
| 51 | + "Read with schema is not supported".to_string(), |
| 52 | + )); |
| 53 | + } |
| 54 | + if read.advanced_extension.is_some() { |
| 55 | + return Err(DataFusionError::NotImplemented( |
| 56 | + "Read with AdvancedExtension is not supported".to_string(), |
| 57 | + )); |
| 58 | + } |
| 59 | + match &read.as_ref().read_type { |
| 60 | + Some(ReadType::LocalFiles(files)) => { |
| 61 | + let mut file_groups = vec![]; |
| 62 | + |
| 63 | + for file in &files.items { |
| 64 | + let path = if let Some(path_type) = &file.path_type { |
| 65 | + match path_type { |
| 66 | + PathType::UriPath(path) => Ok(path.clone()), |
| 67 | + PathType::UriPathGlob(path) => Ok(path.clone()), |
| 68 | + PathType::UriFile(path) => Ok(path.clone()), |
| 69 | + PathType::UriFolder(path) => Ok(path.clone()), |
| 70 | + } |
| 71 | + } else { |
| 72 | + Err(DataFusionError::Substrait( |
| 73 | + "Missing PathType".to_string(), |
| 74 | + )) |
| 75 | + }?; |
| 76 | + |
| 77 | + // TODO substrait plans do not have `last_modified` or `size` but `ObjectMeta` |
| 78 | + // requires them both - perhaps we can change the object-store crate |
| 79 | + // to make these optional? We cannot guarantee that we have access to the |
| 80 | + // files to get this information, depending on how this library is being |
| 81 | + // used |
| 82 | + let last_modified = DateTime::parse_from_str( |
| 83 | + "1970 Jan 1 00:00:00.000 +0000", |
| 84 | + "%Y %b %d %H:%M:%S%.3f %z", |
| 85 | + ) |
| 86 | + .unwrap(); |
| 87 | + let size = 0; |
| 88 | + |
| 89 | + let partitioned_file = PartitionedFile { |
| 90 | + object_meta: ObjectMeta { |
| 91 | + last_modified: last_modified.into(), |
| 92 | + location: path.into(), |
| 93 | + size, |
| 94 | + }, |
| 95 | + partition_values: vec![], |
| 96 | + range: None, |
| 97 | + extensions: None, |
| 98 | + }; |
| 99 | + |
| 100 | + let part_index = file.partition_index as usize; |
| 101 | + while part_index >= file_groups.len() { |
| 102 | + file_groups.push(vec![]); |
| 103 | + } |
| 104 | + file_groups[part_index].push(partitioned_file) |
| 105 | + } |
| 106 | + |
| 107 | + let mut base_config = FileScanConfig { |
| 108 | + object_store_url: ObjectStoreUrl::local_filesystem(), |
| 109 | + file_schema: Arc::new(Schema::empty()), |
| 110 | + file_groups, |
| 111 | + statistics: Default::default(), |
| 112 | + projection: None, |
| 113 | + limit: None, |
| 114 | + table_partition_cols: vec![], |
| 115 | + output_ordering: None, |
| 116 | + infinite_source: false, |
| 117 | + }; |
| 118 | + |
| 119 | + if let Some(MaskExpression { select, .. }) = &read.projection { |
| 120 | + if let Some(projection) = &select.as_ref() { |
| 121 | + let column_indices: Vec<usize> = projection |
| 122 | + .struct_items |
| 123 | + .iter() |
| 124 | + .map(|item| item.field as usize) |
| 125 | + .collect(); |
| 126 | + base_config.projection = Some(column_indices); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + Ok(Arc::new(ParquetExec::new(base_config, None, None)) |
| 131 | + as Arc<dyn ExecutionPlan>) |
| 132 | + } |
| 133 | + _ => Err(DataFusionError::NotImplemented( |
| 134 | + "Only LocalFile reads are supported when parsing physical" |
| 135 | + .to_string(), |
| 136 | + )), |
| 137 | + } |
| 138 | + } |
| 139 | + _ => Err(DataFusionError::NotImplemented(format!( |
| 140 | + "Unsupported RelType: {:?}", |
| 141 | + rel.rel_type |
| 142 | + ))), |
| 143 | + } |
| 144 | +} |
0 commit comments