Skip to content

AVRO-3248: Rust: Support named types in UnionSchema#1396

Merged
martin-g merged 9 commits intoapache:masterfrom
lulitao1997:record_union
Jan 19, 2022
Merged

AVRO-3248: Rust: Support named types in UnionSchema#1396
martin-g merged 9 commits intoapache:masterfrom
lulitao1997:record_union

Conversation

@lulitao1997
Copy link
Contributor

change Value::Union to value it holds and its position in the type list,
this allows us to

Make sure you have checked all steps below.

Jira

  • [x ] My PR addresses the following Avro Jira issues and references them in the PR title.

Tests

  • [ x] My PR adds the following unit tests
    1. schema::tests::test_union_of_records
    2. schema::tests::test_nullable_record

Commits

  • [x ] My commits all reference Jira issues in their subject lines. In addition, my commits follow the guidelines from "How to write a good git commit message":
    1. Subject is separated from body by a blank line
    2. Subject is limited to 50 characters (not including Jira issue reference)
    3. Subject does not end with a period
    4. Subject uses the imperative mood ("add", not "adding")
    5. Body wraps at 72 characters
    6. Body explains "what" and "why", not "how"

/// of its corresponding schema
/// This allows schema-less encoding, as well as schema resolution while
/// reading values.
Union(i32, Box<Value>),
Copy link
Member

Choose a reason for hiding this comment

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

Why i32 ? Could we have negative values ?

Copy link
Member

Choose a reason for hiding this comment

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

I see, it just follows the way Value::Enum is implemented.
I'll take a deeper look why i32 is used there. It also leads to a lot of casting later.

@praetp
Copy link

praetp commented Jan 18, 2022

I am interested in this change.

Didn't compile for me. Maybe I did something wrong ?

error[E0061]: this enum variant takes 2 arguments but 1 argument was supplied
   --> /home/spetsnaz/.cargo/git/checkouts/avro-d44366ca41b07ddf/912e979/lang/rust/src/decode.rs:209:20
    |
209 |                 Ok(Value::Union(Box::new(value)))
    |                    ^^^^^^^^^^^^ --------------- supplied 1 argument
    |                    |
    |                    expected 2 arguments
    |
note: tuple variant defined here
   --> /home/spetsnaz/.cargo/git/checkouts/avro-d44366ca41b07ddf/912e979/lang/rust/src/types.rs:74:5
    |
74  |     Union(i32, Box<Value>),
    |     ^^^^^

@martin-g
Copy link
Member

I start working on this PR!
Hopefully it will be merged later today!

lulitao1997 and others added 7 commits January 19, 2022 10:08
previously union does not support named types, and we will get error if
we add 2 records into 1 UnionSchema.
Signed-off-by: Martin Tzvetanov Grigorov <mgrigorov@apache.org>
Signed-off-by: Martin Tzvetanov Grigorov <mgrigorov@apache.org>
@martin-g
Copy link
Member

@praetp Please try the PR now! It is fixed and rebased to latest master!

Signed-off-by: Martin Tzvetanov Grigorov <mgrigorov@apache.org>
Err(de::Error::custom("A record must have a least one field")),
|item| match (item.0.as_ref(), &item.1) {
("type", Value::String(x)) => Ok((
("type", Value::String(x)) | ("type", Value::Enum(_, x)) => Ok((
Copy link
Member

Choose a reason for hiding this comment

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

This change does not look related to this PR.
@lulitao1997 Why did you make it ?

// Find the first match in the reader schema.
let (_, inner) = schema.find_schema(&v).ok_or(Error::FindUnionVariant)?;
Ok(Value::Union(Box::new(v.resolve(inner)?)))
// FIXME: this is wrong when the union consists of multiple same records that have different names
Copy link
Member

Choose a reason for hiding this comment

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

True, but I wonder how big a problem it is ?
The name and doc might be wrong but the fields will be the same, so the application logic should work.

{
fn from(value: Option<T>) -> Self {
Self::Union(Box::new(value.map_or_else(|| Self::Null, Into::into)))
// NOTE: this is incorrect in case first type in union is not "none"
Copy link
Member

Choose a reason for hiding this comment

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

This seems incorrect also when the union has more than two items.
And looks like a stopper for this PR.

Copy link
Member

Choose a reason for hiding this comment

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

Ignore this comment! I misunderstood it the first time!
It looks OK to me!

Signed-off-by: Martin Tzvetanov Grigorov <mgrigorov@apache.org>
@martin-g martin-g merged commit 49c6e10 into apache:master Jan 19, 2022
@martin-g
Copy link
Member

Thank you for the contribution, @lulitao1997 !

martin-g added a commit that referenced this pull request Feb 1, 2022
* AVRO-3248: Rust: Support named types in UnionSchema

previously union does not support named types, and we will get error if
we add 2 records into 1 UnionSchema.

* AVRO-3248: Fix a typo in error message

* AVRO-3248: Give better names for the schemata as string

* AVRO-3248: More better names for variables

* AVRO-3248: Code formatting

* AVRO-3248 Fix formatting & build

Signed-off-by: Martin Tzvetanov Grigorov <mgrigorov@apache.org>

* AVRO-3248: Fix generate_interop_data and formatting

Signed-off-by: Martin Tzvetanov Grigorov <mgrigorov@apache.org>

* AVRO-3248: Fix some regressions after the rebase to master

Signed-off-by: Martin Tzvetanov Grigorov <mgrigorov@apache.org>

* AVRO-3248 Fix the position in the Union for the Double value

Signed-off-by: Martin Tzvetanov Grigorov <mgrigorov@apache.org>

Co-authored-by: Martin Grigorov <martin-g@users.noreply.github.com>
Co-authored-by: Martin Tzvetanov Grigorov <mgrigorov@apache.org>
(cherry picked from commit 49c6e10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants