You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "jecsand838 (via GitHub)" <gi...@apache.org> on 2025/08/07 06:06:13 UTC
[PR] Implement ArrowSchema to AvroSchema conversion logic in arrow-avro [arrow-rs]
jecsand838 opened a new pull request, #8075:
URL: https://github.com/apache/arrow-rs/pull/8075
# Which issue does this PR close?
- Part of https://github.com/apache/arrow-rs/issues/4886
# Rationale for this change
This change introduces functionality to convert an `ArrowSchema` into an `AvroSchema`. This is a crucial feature for improving interoperability between Arrow and Avro. By enabling direct schema conversion, we can streamline data pipelines that write Arrow data to Avro-compatible formats. Furthermore we'd be able to create reader `AvroSchema` instances directly from an arrow-rs `Schema` for easier schema evolution support.
These updates are also foundational to the upcoming `arrow-avro` `Writer`.
# What changes are included in this PR?
- **`TryFrom<&ArrowSchema> for AvroSchema`**: The core of this PR is the implementation of the `TryFrom` trait to allow a fallible conversion from an `ArrowSchema` reference to a new `AvroSchema`.
- **Type Mapping Logic**: Added comprehensive logic to map Arrow `DataType` variants to their corresponding Avro type representations. This includes:
- Primitive types (`Boolean`, `Int`, `Float`, `Binary`, `Utf8`).
- Logical types (e.g., `Timestamp`, `Date`, `Decimal`, `UUID`).
- Complex types (`Struct`, `List`, `Map`, `Dictionary`). Dictionaries are converted to Avro `enum` types.
- **Name Sanitization**: Implemented a `NameGenerator` to ensure that field names derived from the `ArrowSchema` are valid according to Avro naming conventions and are unique within their scope.
- **Metadata Handling**: The conversion preserves relevant metadata from the Arrow schema.
- `arrow-avro` metadata constants to simplify working with Avro metadata in Arrow DataTypes.
# Are these changes tested?
Yes, this change is accompanied by new tests in `schema.rs`. The tests cover:
- Correct mapping of all supported primitive, temporal, and logical types.
- Conversion of complex and nested structures like `Struct`, `List`, and `Map`.
- Proper handling of dictionary-encoded fields to Avro enums.
- Validation of name sanitization logic.
- Round-trip conversion tests for various data types to ensure correctness.
# Are there any user-facing changes?
N/A
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Implement ArrowSchema to AvroSchema conversion logic in arrow-avro [arrow-rs]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb merged PR #8075:
URL: https://github.com/apache/arrow-rs/pull/8075
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Implement ArrowSchema to AvroSchema conversion logic in arrow-avro [arrow-rs]
Posted by "jecsand838 (via GitHub)" <gi...@apache.org>.
jecsand838 commented on code in PR #8075:
URL: https://github.com/apache/arrow-rs/pull/8075#discussion_r2268150437
##########
arrow-avro/src/schema.rs:
##########
@@ -647,12 +715,336 @@ pub(crate) fn compute_fingerprint_rabin(canonical_form: &str) -> u64 {
fp
}
+#[inline]
+fn is_internal_arrow_key(key: &str) -> bool {
+ key.starts_with("ARROW:") || key == SCHEMA_METADATA_KEY
+}
+
+// Sanitize an arbitrary string so it is a valid Avro field or type name
+fn sanitise_avro_name(base_name: &str) -> String {
+ if base_name.is_empty() {
+ return "_".to_owned();
+ }
+ let mut out: String = base_name
+ .chars()
+ .map(|char| {
+ if char.is_ascii_alphanumeric() || char == '_' {
+ char
+ } else {
+ '_'
+ }
+ })
+ .collect();
+ if out.as_bytes()[0].is_ascii_digit() {
+ out.insert(0, '_');
+ }
+ out
+}
+
+#[derive(Default)]
+struct NameGenerator {
+ used: HashSet<String>,
+ counters: HashMap<String, usize>,
+}
+
+impl NameGenerator {
+ fn make_unique(&mut self, field_name: &str) -> String {
+ let field_name = sanitise_avro_name(field_name);
+ if self.used.insert(field_name.clone()) {
+ self.counters.insert(field_name.clone(), 1);
+ return field_name;
+ }
+ let counter = self.counters.entry(field_name.clone()).or_insert(1);
+ loop {
+ let candidate = format!("{field_name}_{}", *counter);
+ if self.used.insert(candidate.clone()) {
+ return candidate;
+ }
+ *counter += 1;
+ }
+ }
+}
+
+fn merge_extras(schema: Value, mut extras: JsonMap<String, Value>) -> Value {
+ if extras.is_empty() {
+ return schema;
+ }
+ match schema {
+ Value::Object(mut map) => {
+ map.extend(extras);
+ Value::Object(map)
+ }
+ Value::Array(mut union) => {
+ if let Some(non_null) = union.iter_mut().find(|val| val.as_str() != Some("null")) {
+ let original = std::mem::take(non_null);
+ *non_null = merge_extras(original, extras);
+ }
+ Value::Array(union)
+ }
+ primitive => {
+ let mut map = JsonMap::with_capacity(extras.len() + 1);
+ map.insert("type".into(), primitive);
+ map.extend(extras);
+ Value::Object(map)
+ }
+ }
+}
+
+// Convert an Arrow `DataType` into an Avro schema `Value`.
+fn datatype_to_avro(
+ dt: &DataType,
+ field_name: &str,
+ metadata: &HashMap<String, String>,
+ name_gen: &mut NameGenerator,
+) -> Result<(Value, JsonMap<String, Value>), ArrowError> {
Review Comment:
That's a good call out. There's definitely some follow-up improvements to be made both here and in the `AvroSchema` type as well.
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Implement ArrowSchema to AvroSchema conversion logic in arrow-avro [arrow-rs]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #8075:
URL: https://github.com/apache/arrow-rs/pull/8075#issuecomment-3180282510
Thanks again @jecsand838
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Implement ArrowSchema to AvroSchema conversion logic in arrow-avro [arrow-rs]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #8075:
URL: https://github.com/apache/arrow-rs/pull/8075#discussion_r2267965800
##########
arrow-avro/src/schema.rs:
##########
@@ -647,12 +715,336 @@ pub(crate) fn compute_fingerprint_rabin(canonical_form: &str) -> u64 {
fp
}
+#[inline]
+fn is_internal_arrow_key(key: &str) -> bool {
+ key.starts_with("ARROW:") || key == SCHEMA_METADATA_KEY
+}
+
+// Sanitize an arbitrary string so it is a valid Avro field or type name
+fn sanitise_avro_name(base_name: &str) -> String {
+ if base_name.is_empty() {
+ return "_".to_owned();
+ }
+ let mut out: String = base_name
+ .chars()
+ .map(|char| {
+ if char.is_ascii_alphanumeric() || char == '_' {
+ char
+ } else {
+ '_'
+ }
+ })
+ .collect();
+ if out.as_bytes()[0].is_ascii_digit() {
+ out.insert(0, '_');
+ }
+ out
+}
+
+#[derive(Default)]
+struct NameGenerator {
+ used: HashSet<String>,
+ counters: HashMap<String, usize>,
+}
+
+impl NameGenerator {
+ fn make_unique(&mut self, field_name: &str) -> String {
+ let field_name = sanitise_avro_name(field_name);
+ if self.used.insert(field_name.clone()) {
+ self.counters.insert(field_name.clone(), 1);
+ return field_name;
+ }
+ let counter = self.counters.entry(field_name.clone()).or_insert(1);
+ loop {
+ let candidate = format!("{field_name}_{}", *counter);
+ if self.used.insert(candidate.clone()) {
+ return candidate;
+ }
+ *counter += 1;
+ }
+ }
+}
+
+fn merge_extras(schema: Value, mut extras: JsonMap<String, Value>) -> Value {
+ if extras.is_empty() {
+ return schema;
+ }
+ match schema {
+ Value::Object(mut map) => {
+ map.extend(extras);
+ Value::Object(map)
+ }
+ Value::Array(mut union) => {
+ if let Some(non_null) = union.iter_mut().find(|val| val.as_str() != Some("null")) {
+ let original = std::mem::take(non_null);
+ *non_null = merge_extras(original, extras);
+ }
+ Value::Array(union)
+ }
+ primitive => {
+ let mut map = JsonMap::with_capacity(extras.len() + 1);
+ map.insert("type".into(), primitive);
+ map.extend(extras);
+ Value::Object(map)
+ }
+ }
+}
+
+// Convert an Arrow `DataType` into an Avro schema `Value`.
+fn datatype_to_avro(
+ dt: &DataType,
+ field_name: &str,
+ metadata: &HashMap<String, String>,
+ name_gen: &mut NameGenerator,
+) -> Result<(Value, JsonMap<String, Value>), ArrowError> {
Review Comment:
longer term it might be worth considering moving to something other than the JSON value representation unless its flexibility is really needed -- like this code might be better / more efficient if it directly creates an AvroSchema element or something
I haven't followed this code enough to have any sort of specific proposal, I am just pattern matching based on Json::Value usage here
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org