jecsand838 commented on code in PR #8371:
URL: https://github.com/apache/arrow-rs/pull/8371#discussion_r2357401981
##########
arrow-avro/src/writer/format.rs:
##########
@@ -84,25 +106,87 @@ impl AvroFormat for AvroOcfFormat {
}
}
-/// Raw Avro binary streaming format (no header or footer).
+/// Raw Avro binary streaming format using **Single-Object Encoding** per
record.
+///
+/// Each record written by the stream writer is framed with a prefix determined
+/// by the schema fingerprinting algorithm.
+///
+/// See:
<https://avro.apache.org/docs/1.11.1/specification/#single-object-encoding>
+/// See:
<https://docs.confluent.io/platform/current/schema-registry/fundamentals/serdes-develop/index.html#wire-format>
#[derive(Debug, Default)]
-pub struct AvroBinaryFormat;
+pub struct AvroBinaryFormat {
+ /// Pre-built, variable-length prefix written before each record.
+ prefix: Vec<u8>,
+}
impl AvroFormat for AvroBinaryFormat {
fn start_stream<W: Write>(
&mut self,
_writer: &mut W,
- _schema: &Schema,
- _compression: Option<CompressionCodec>,
+ schema: &Schema,
+ compression: Option<CompressionCodec>,
+ fingerprint_strategy: FingerprintStrategy,
) -> Result<(), ArrowError> {
- Err(ArrowError::NotYetImplemented(
- "avro binary format not yet implemented".to_string(),
- ))
+ if compression.is_some() {
+ return Err(ArrowError::InvalidArgumentError(
+ "Compression not supported for Avro binary
streaming".to_string(),
+ ));
+ }
+
+ self.prefix.clear();
+
+ match fingerprint_strategy {
+ FingerprintStrategy::ConfluentSchemaId(id) => {
+ self.prefix.push(CONFLUENT_MAGIC[0]);
+ self.prefix.extend_from_slice(&id.to_be_bytes());
+ }
+ strategy => {
+ // All other strategies use the single-object encoding format
+ self.prefix.extend_from_slice(&SINGLE_OBJECT_MAGIC);
+
+ let avro_schema = AvroSchema::try_from(schema)?;
+ let fp = match strategy {
+ FingerprintStrategy::Rabin => avro_schema.fingerprint()?,
+ #[cfg(feature = "md5")]
+ FingerprintStrategy::MD5 =>
AvroSchema::generate_fingerprint(
+ &avro_schema.schema()?,
+ crate::schema::FingerprintAlgorithm::MD5,
+ )?,
+ #[cfg(feature = "sha256")]
+ FingerprintStrategy::SHA256 =>
AvroSchema::generate_fingerprint(
+ &avro_schema.schema()?,
+ crate::schema::FingerprintAlgorithm::SHA256,
+ )?,
+ FingerprintStrategy::ConfluentSchemaId(_) =>
unreachable!(),
+ };
+
+ match fp {
+ Fingerprint::Rabin(val) =>
self.prefix.extend_from_slice(&val.to_le_bytes()),
+ #[cfg(feature = "md5")]
+ Fingerprint::MD5(val) =>
self.prefix.extend_from_slice(val.as_ref()),
+ #[cfg(feature = "sha256")]
+ Fingerprint::SHA256(val) =>
self.prefix.extend_from_slice(val.as_ref()),
+ Fingerprint::Id(_) => return
Err(ArrowError::InvalidArgumentError(
+ "ConfluentSchemaId strategy cannot be used with a
hash-based fingerprint."
+ .to_string(),
+ )),
+ }
+ }
+ }
Review Comment:
I'd move this logic to the `WriterBuilder::build` and
`RecordEncoderBuilder::build` methods and simplify.
```suggestion
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]