From 0153813c5c69741c7b444aab1fa0880e36345f89 Mon Sep 17 00:00:00 2001 From: ion-elgreco <15728914+ion-elgreco@users.noreply.github.com> Date: Sat, 18 Nov 2023 21:33:12 +0100 Subject: [PATCH] implement fromstr --- .../src/operations/convert_to_delta.rs | 5 ++--- crates/deltalake-core/src/protocol/mod.rs | 20 ++++++++++++++++++- python/src/lib.rs | 13 ++---------- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/crates/deltalake-core/src/operations/convert_to_delta.rs b/crates/deltalake-core/src/operations/convert_to_delta.rs index 53be090916..23c2e0c98a 100644 --- a/crates/deltalake-core/src/operations/convert_to_delta.rs +++ b/crates/deltalake-core/src/operations/convert_to_delta.rs @@ -86,14 +86,13 @@ impl FromStr for PartitionStrategy { type Err = DeltaTableError; fn from_str(s: &str) -> DeltaResult { - let strategy = match s { + match s { "hive" => Ok(PartitionStrategy::Hive), _ => Err(DeltaTableError::Generic(format!( "Invalid partition strategy provided {}", s ))), - }; - strategy + } } } diff --git a/crates/deltalake-core/src/protocol/mod.rs b/crates/deltalake-core/src/protocol/mod.rs index 8a5cd9f858..d3c47fbd12 100644 --- a/crates/deltalake-core/src/protocol/mod.rs +++ b/crates/deltalake-core/src/protocol/mod.rs @@ -23,8 +23,9 @@ use std::borrow::Borrow; use std::collections::HashMap; use std::hash::{Hash, Hasher}; use std::mem::take; +use std::str::FromStr; -use crate::errors::DeltaResult; +use crate::errors::{DeltaResult, DeltaTableError}; use crate::kernel::{Add, CommitInfo, Metadata, Protocol, Remove}; use crate::logstore::LogStore; use crate::table::CheckPoint; @@ -589,6 +590,23 @@ pub enum SaveMode { Ignore, } +impl FromStr for SaveMode { + type Err = DeltaTableError; + + fn from_str(s: &str) -> DeltaResult { + match s { + "append" => Ok(SaveMode::Append), + "overwrite" => Ok(SaveMode::Overwrite), + "error" => Ok(SaveMode::ErrorIfExists), + "ignore" => Ok(SaveMode::Ignore), + _ => Err(DeltaTableError::Generic(format!( + "Invalid save mode provided: {}, only these are supported: ['append', 'overwrite', 'error', 'ignore']", + s + ))), + } + } +} + /// The OutputMode used in streaming operations. #[derive(Serialize, Deserialize, Debug, Clone)] pub enum OutputMode { diff --git a/python/src/lib.rs b/python/src/lib.rs index 4b58412fbb..b9067dfec9 100644 --- a/python/src/lib.rs +++ b/python/src/lib.rs @@ -760,7 +760,8 @@ impl RawDeltaTable { schema: PyArrowType, partitions_filters: Option>, ) -> PyResult<()> { - let mode = save_mode_from_str(mode)?; + let mode = mode.parse().map_err(PythonError::from)?; + let schema: StructType = (&schema.0).try_into().map_err(PythonError::from)?; let existing_schema = self._table.get_schema().map_err(PythonError::from)?; @@ -1090,16 +1091,6 @@ fn batch_distinct(batch: PyArrowType) -> PyResult PyResult { - match value { - "append" => Ok(SaveMode::Append), - "overwrite" => Ok(SaveMode::Overwrite), - "error" => Ok(SaveMode::ErrorIfExists), - "ignore" => Ok(SaveMode::Ignore), - _ => Err(PyValueError::new_err("Invalid save mode")), - } -} - fn current_timestamp() -> i64 { let start = SystemTime::now(); let since_the_epoch = start