Skip to content
Open
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
84 changes: 2 additions & 82 deletions ballista/core/src/serde/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ use datafusion::common::{DataFusionError, Result};
use datafusion::execution::TaskContext;
use datafusion::logical_expr::Extension;
use datafusion::physical_plan::{ExecutionPlan, Partitioning};
use datafusion_proto::logical_plan::file_formats::{
ArrowLogicalExtensionCodec, AvroLogicalExtensionCodec, CsvLogicalExtensionCodec,
JsonLogicalExtensionCodec, ParquetLogicalExtensionCodec,
};
use datafusion_proto::physical_plan::from_proto::parse_physical_sort_exprs;
use datafusion_proto::physical_plan::from_proto::parse_protobuf_hash_partitioning;
use datafusion_proto::physical_plan::from_proto::parse_protobuf_partitioning;
Expand Down Expand Up @@ -189,49 +185,12 @@ impl<T: 'static + AsLogicalPlan, U: 'static + AsExecutionPlan> BallistaCodec<T,
#[derive(Debug)]
pub struct BallistaLogicalExtensionCodec {
default_codec: Arc<dyn LogicalExtensionCodec>,
file_format_codecs: Vec<Arc<dyn LogicalExtensionCodec>>,
}

impl BallistaLogicalExtensionCodec {
/// looks for a codec which can operate on this node
/// returns a position of codec in the list and result.
///
/// position is important with encoding process
/// as position of used codecs is needed
/// so the same codec can be used for decoding
fn try_any<R>(
&self,
mut f: impl FnMut(&dyn LogicalExtensionCodec) -> Result<R>,
) -> Result<(u32, R)> {
let mut last_err = None;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this changes the protobuf encoding wire numbers? If so, we should bump BALLISTA_PROTOCOL_VERSION

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#2337 ( you 🫵🏻 ) update it two days ago 😀. i believe, we as we haven't released it yet, no need to update it. do i miss something @avantgardnerio ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was not sure what convention we wanted to support here. I'm not sure if folks are tracking ballista/main in their own projects. It seemed like monotonic bumps couldn't hurt. @phillipleblanc ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no problem to increment it, id argue that we should have API checkpoint on release

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes me wonder if it should just be the hash of the protobuf, or a git SHA. Not advocating for either, just thinking out loud.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could do it, it would be easy to automate, it would be hard to capture backward compatible changes, though.

i believe this change should produce same binary output (as the logic has been moved from here to datafusion), i would not put my money on that claim 😀

for (position, codec) in self.file_format_codecs.iter().enumerate() {
match f(codec.as_ref()) {
Ok(result) => return Ok((position as u32, result)),
Err(err) => last_err = Some(err),
}
}

Err(last_err.unwrap_or_else(|| {
DataFusionError::Internal(
"List of provided extended logical codecs is empty".to_owned(),
)
}))
}
}

impl Default for BallistaLogicalExtensionCodec {
fn default() -> Self {
Self {
default_codec: Arc::new(DefaultLogicalExtensionCodec {}),
// Position in this list is important as it will be used for decoding.
// If new codec is added it should go to last position.
file_format_codecs: vec![
Arc::new(ParquetLogicalExtensionCodec {}),
Arc::new(CsvLogicalExtensionCodec {}),
Arc::new(JsonLogicalExtensionCodec {}),
Arc::new(ArrowLogicalExtensionCodec {}),
Arc::new(AvroLogicalExtensionCodec {}),
],
}
}
}
Expand Down Expand Up @@ -320,35 +279,15 @@ impl LogicalExtensionCodec for BallistaLogicalExtensionCodec {
buf: &[u8],
ctx: &TaskContext,
) -> Result<Arc<dyn datafusion::datasource::file_format::FileFormatFactory>> {
let proto = FileFormatProto::decode(buf)
.map_err(|e| DataFusionError::Internal(e.to_string()))?;

let codec = self
.file_format_codecs
.get(proto.encoder_position as usize)
.ok_or(DataFusionError::Internal(
"Can't find required codec in file codec list".to_owned(),
))?;

codec.try_decode_file_format(&proto.blob, ctx)
self.default_codec.try_decode_file_format(buf, ctx)
}

fn try_encode_file_format(
&self,
buf: &mut Vec<u8>,
node: Arc<dyn datafusion::datasource::file_format::FileFormatFactory>,
) -> Result<()> {
let mut blob = vec![];
let (encoder_position, _) =
self.try_any(|codec| codec.try_encode_file_format(&mut blob, node.clone()))?;

let proto = FileFormatProto {
encoder_position,
blob,
};
proto
.encode(buf)
.map_err(|e| DataFusionError::Internal(e.to_string()))
self.default_codec.try_encode_file_format(buf, node)
}
}

Expand Down Expand Up @@ -1168,25 +1107,6 @@ impl PhysicalExtensionCodec for BallistaPhysicalExtensionCodec {
}
}

/// FileFormatProto captures data encoded by file format codecs
///
/// it captures position of codec used to encode FileFormat
/// and actual encoded value.
///
/// capturing codec position is required, as same codec can decode
/// blobs encoded by different encoders (probability is low but it
/// happened in the past)
///
#[derive(Clone, PartialEq, prost::Message)]
struct FileFormatProto {
/// encoder id used to encode blob
/// (to be used for decoding)
#[prost(uint32, tag = 1)]
pub encoder_position: u32,
#[prost(bytes, tag = 2)]
pub blob: Vec<u8>,
}

#[cfg(test)]
mod test {
use super::*;
Expand Down