|
| 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 | +//! Protobuf conversion for the format-independent [`FileSinkConfig`]. |
| 19 | +
|
| 20 | +use std::sync::Arc; |
| 21 | + |
| 22 | +use arrow::compute::SortOptions; |
| 23 | +use arrow::datatypes::Schema; |
| 24 | +use datafusion_common::{Result, internal_datafusion_err}; |
| 25 | +use datafusion_execution::object_store::ObjectStoreUrl; |
| 26 | +use datafusion_expr::dml::InsertOp; |
| 27 | +use datafusion_physical_expr::PhysicalSortExpr; |
| 28 | +use datafusion_physical_expr_common::sort_expr::LexRequirement; |
| 29 | +use datafusion_physical_plan::proto::ExecutionPlanDecodeCtx; |
| 30 | +use datafusion_proto_models::protobuf; |
| 31 | + |
| 32 | +use crate::file_groups::FileGroup; |
| 33 | +use crate::file_sink_config::{FileOutputMode, FileSinkConfig}; |
| 34 | +use crate::ListingTableUrl; |
| 35 | + |
| 36 | +impl FileSinkConfig { |
| 37 | + /// Serialize this shared file-sink configuration without format-specific |
| 38 | + /// writer options. |
| 39 | + pub fn to_proto(&self) -> Result<protobuf::FileSinkConfig> { |
| 40 | + let file_groups = self |
| 41 | + .file_group |
| 42 | + .iter() |
| 43 | + .map(TryInto::try_into) |
| 44 | + .collect::<Result<Vec<_>>>()?; |
| 45 | + let table_paths = self |
| 46 | + .table_paths |
| 47 | + .iter() |
| 48 | + .map(ToString::to_string) |
| 49 | + .collect::<Vec<_>>(); |
| 50 | + let table_partition_cols = self |
| 51 | + .table_partition_cols |
| 52 | + .iter() |
| 53 | + .map(|(name, data_type)| { |
| 54 | + Ok(protobuf::PartitionColumn { |
| 55 | + name: name.to_owned(), |
| 56 | + arrow_type: Some(data_type.try_into()?), |
| 57 | + }) |
| 58 | + }) |
| 59 | + .collect::<Result<Vec<_>>>()?; |
| 60 | + let file_output_mode = match self.file_output_mode { |
| 61 | + FileOutputMode::Automatic => protobuf::FileOutputMode::Automatic, |
| 62 | + FileOutputMode::SingleFile => protobuf::FileOutputMode::SingleFile, |
| 63 | + FileOutputMode::Directory => protobuf::FileOutputMode::Directory, |
| 64 | + }; |
| 65 | + |
| 66 | + Ok(protobuf::FileSinkConfig { |
| 67 | + object_store_url: self.object_store_url.to_string(), |
| 68 | + file_groups, |
| 69 | + table_paths, |
| 70 | + output_schema: Some(self.output_schema.as_ref().try_into()?), |
| 71 | + table_partition_cols, |
| 72 | + keep_partition_by_columns: self.keep_partition_by_columns, |
| 73 | + insert_op: self.insert_op as i32, |
| 74 | + file_extension: self.file_extension.clone(), |
| 75 | + file_output_mode: file_output_mode.into(), |
| 76 | + }) |
| 77 | + } |
| 78 | + |
| 79 | + /// Reconstruct a shared file-sink configuration from protobuf. |
| 80 | + pub fn from_proto(conf: &protobuf::FileSinkConfig) -> Result<Self> { |
| 81 | + let file_group = FileGroup::new( |
| 82 | + conf.file_groups |
| 83 | + .iter() |
| 84 | + .map(TryInto::try_into) |
| 85 | + .collect::<Result<Vec<_>>>()?, |
| 86 | + ); |
| 87 | + let table_paths = conf |
| 88 | + .table_paths |
| 89 | + .iter() |
| 90 | + .map(ListingTableUrl::parse) |
| 91 | + .collect::<Result<Vec<_>>>()?; |
| 92 | + let table_partition_cols = conf |
| 93 | + .table_partition_cols |
| 94 | + .iter() |
| 95 | + .map(|protobuf::PartitionColumn { name, arrow_type }| { |
| 96 | + let data_type = arrow_type |
| 97 | + .as_ref() |
| 98 | + .ok_or_else(|| { |
| 99 | + internal_datafusion_err!( |
| 100 | + "PartitionColumn is missing required field 'arrow_type'" |
| 101 | + ) |
| 102 | + })? |
| 103 | + .try_into()?; |
| 104 | + Ok((name.clone(), data_type)) |
| 105 | + }) |
| 106 | + .collect::<Result<Vec<_>>>()?; |
| 107 | + let insert_op = match conf.insert_op() { |
| 108 | + protobuf::InsertOp::Append => InsertOp::Append, |
| 109 | + protobuf::InsertOp::Overwrite => InsertOp::Overwrite, |
| 110 | + protobuf::InsertOp::Replace => InsertOp::Replace, |
| 111 | + }; |
| 112 | + let file_output_mode = match conf.file_output_mode() { |
| 113 | + protobuf::FileOutputMode::Automatic => FileOutputMode::Automatic, |
| 114 | + protobuf::FileOutputMode::SingleFile => FileOutputMode::SingleFile, |
| 115 | + protobuf::FileOutputMode::Directory => FileOutputMode::Directory, |
| 116 | + }; |
| 117 | + let output_schema = conf.output_schema.as_ref().ok_or_else(|| { |
| 118 | + internal_datafusion_err!( |
| 119 | + "FileSinkConfig is missing required field 'output_schema'" |
| 120 | + ) |
| 121 | + })?; |
| 122 | + |
| 123 | + Ok(Self { |
| 124 | + original_url: String::default(), |
| 125 | + object_store_url: ObjectStoreUrl::parse(&conf.object_store_url)?, |
| 126 | + file_group, |
| 127 | + table_paths, |
| 128 | + output_schema: Arc::new(output_schema.try_into()?), |
| 129 | + table_partition_cols, |
| 130 | + insert_op, |
| 131 | + keep_partition_by_columns: conf.keep_partition_by_columns, |
| 132 | + file_extension: conf.file_extension.clone(), |
| 133 | + file_output_mode, |
| 134 | + }) |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +/// Decode a sink's optional required output ordering against its input schema. |
| 139 | +pub fn parse_sink_sort_order( |
| 140 | + collection: Option<&protobuf::PhysicalSortExprNodeCollection>, |
| 141 | + ctx: &ExecutionPlanDecodeCtx<'_>, |
| 142 | + schema: &Schema, |
| 143 | +) -> Result<Option<LexRequirement>> { |
| 144 | + let Some(collection) = collection else { |
| 145 | + return Ok(None); |
| 146 | + }; |
| 147 | + let sort_exprs = collection |
| 148 | + .physical_sort_expr_nodes |
| 149 | + .iter() |
| 150 | + .map(|node| { |
| 151 | + let expr = node.expr.as_ref().ok_or_else(|| { |
| 152 | + internal_datafusion_err!("Unexpected empty physical expression") |
| 153 | + })?; |
| 154 | + Ok(PhysicalSortExpr { |
| 155 | + expr: ctx.decode_expr(expr, schema)?, |
| 156 | + options: SortOptions { |
| 157 | + descending: !node.asc, |
| 158 | + nulls_first: node.nulls_first, |
| 159 | + }, |
| 160 | + }) |
| 161 | + }) |
| 162 | + .collect::<Result<Vec<_>>>()?; |
| 163 | + Ok(LexRequirement::new(sort_exprs.into_iter().map(Into::into))) |
| 164 | +} |
0 commit comments