Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor byte_to_string and string_to_byte #7091

Merged
merged 4 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 10 additions & 14 deletions datafusion/proto/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,22 @@

use datafusion_common::{DataFusionError, Result};

pub fn csv_delimiter_to_string(b: u8) -> Result<String> {
let b = &[b];
let b = std::str::from_utf8(b)
.map_err(|_| DataFusionError::Internal("Invalid CSV delimiter".to_owned()))?;
Ok(b.to_owned())
}

pub fn str_to_byte(s: &String) -> Result<u8> {
pub(crate) fn str_to_byte(s: &String, description: &str) -> Result<u8> {
if s.len() != 1 {
return Err(DataFusionError::Internal(
"Invalid CSV delimiter".to_owned(),
));
return Err(DataFusionError::Internal(format!(
"Invalid CSV {description}: expected single character, got {s}"
)));
}
Ok(s.as_bytes()[0])
}

pub fn byte_to_string(b: u8) -> Result<String> {
pub(crate) fn byte_to_string(b: u8, description: &str) -> Result<String> {
let b = &[b];
let b = std::str::from_utf8(b)
.map_err(|_| DataFusionError::Internal("Invalid CSV delimiter".to_owned()))?;
let b = std::str::from_utf8(b).map_err(|_| {
DataFusionError::Internal(format!(
"Invalid CSV {description}: can not represent {b:0x?} as utf8"
))
})?;
Ok(b.to_owned())
}

Expand Down
12 changes: 6 additions & 6 deletions datafusion/proto/src/logical_plan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,10 +353,10 @@ impl AsLogicalPlan for LogicalPlanNode {
}) => {
let mut csv = CsvFormat::default()
.with_has_header(*has_header)
.with_delimiter(str_to_byte(delimiter)?)
.with_quote(str_to_byte(quote)?);
.with_delimiter(str_to_byte(delimiter, "delimiter")?)
.with_quote(str_to_byte(quote, "quote")?);
if let Some(protobuf::csv_format::OptionalEscape::Escape(escape)) = optional_escape {
csv = csv.with_quote(str_to_byte(escape)?);
csv = csv.with_quote(str_to_byte(escape, "escape")?);
}
Arc::new(csv)},
FileFormatType::Avro(..) => Arc::new(AvroFormat),
Expand Down Expand Up @@ -850,12 +850,12 @@ impl AsLogicalPlan for LogicalPlanNode {
FileFormatType::Parquet(protobuf::ParquetFormat {})
} else if let Some(csv) = any.downcast_ref::<CsvFormat>() {
FileFormatType::Csv(protobuf::CsvFormat {
delimiter: byte_to_string(csv.delimiter())?,
delimiter: byte_to_string(csv.delimiter(), "delimiter")?,
has_header: csv.has_header(),
quote: byte_to_string(csv.quote())?,
quote: byte_to_string(csv.quote(), "quote")?,
optional_escape: if let Some(escape) = csv.escape() {
Some(protobuf::csv_format::OptionalEscape::Escape(
byte_to_string(escape)?,
byte_to_string(escape, "escape")?,
))
} else {
None
Expand Down
14 changes: 7 additions & 7 deletions datafusion/proto/src/physical_plan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ use datafusion_common::{DataFusionError, Result};
use prost::bytes::BufMut;
use prost::Message;

use crate::common::str_to_byte;
use crate::common::{byte_to_string, proto_error};
use crate::common::{csv_delimiter_to_string, str_to_byte};
use crate::physical_plan::from_proto::{
parse_physical_expr, parse_physical_sort_expr, parse_protobuf_file_scan_config,
};
Expand Down Expand Up @@ -155,13 +155,13 @@ impl AsExecutionPlan for PhysicalPlanNode {
registry,
)?,
scan.has_header,
str_to_byte(&scan.delimiter)?,
str_to_byte(&scan.quote)?,
str_to_byte(&scan.delimiter, "delimiter")?,
str_to_byte(&scan.quote, "quote")?,
if let Some(protobuf::csv_scan_exec_node::OptionalEscape::Escape(
escape,
)) = &scan.optional_escape
{
Some(str_to_byte(escape)?)
Some(str_to_byte(escape, "escape")?)
} else {
None
},
Expand Down Expand Up @@ -1079,11 +1079,11 @@ impl AsExecutionPlan for PhysicalPlanNode {
protobuf::CsvScanExecNode {
base_conf: Some(exec.base_config().try_into()?),
has_header: exec.has_header(),
delimiter: csv_delimiter_to_string(exec.delimiter())?,
quote: byte_to_string(exec.quote())?,
delimiter: byte_to_string(exec.delimiter(), "delimiter")?,
quote: byte_to_string(exec.quote(), "quote")?,
optional_escape: if let Some(escape) = exec.escape() {
Some(protobuf::csv_scan_exec_node::OptionalEscape::Escape(
byte_to_string(escape)?,
byte_to_string(escape, "escape")?,
))
} else {
None
Expand Down