Skip to content

AVRO-1891: Fix specific nested logical types - #118

Closed
rdblue wants to merge 3 commits into
apache:masterfrom
rdblue:AVRO-1891-fix-specific-nested-logical-types
Closed

AVRO-1891: Fix specific nested logical types#118
rdblue wants to merge 3 commits into
apache:masterfrom
rdblue:AVRO-1891-fix-specific-nested-logical-types

Conversation

@rdblue

@rdblue rdblue commented Sep 5, 2016

Copy link
Copy Markdown
Contributor

This changes the datum readers to use getConversionFor and getConversionByClass instance methods that delegate to the data models. This allows specific to use a set of conversions determined by the current record class to pick up the conversions that were present when it was compiled.

AVRO-1884 changed makePath to a private method from a package-private
static method. This broke the test that references the method in IPC.
The fix is to make the instance method package-private and update the
test to use an instance of SpecificCompiler.
This adds getConversionFor and getConversionByClass to
GenericDatumReader so that SpecificDatumReader can override them to
resolve conversions using the set of conversions used when a specific
class was compiled, determined by the CONVERSIONS static variable or by
adding the conversions for each field returned by getConversion(String).
This adds a CONVERSIONS static field with the converisons that the
specific compiler used to produce a record. Together with the last
commit, this will enable new specific classes to use nested logical
types.
if (logicalType == null) {
return null;
}
Conversion<?> conversion = conversions.get().peekLast()

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.

peekLast() could return null. We need to check it to avoid NPE.

@yibing-shi

Copy link
Copy Markdown
Contributor

And also, we would need to change the datum writers to support the conversions, do we?

@rdblue

rdblue commented Sep 6, 2016

Copy link
Copy Markdown
Contributor Author

Yes, this just shows how I think we could fix the problem. We still need to finish the write side and add tests.

@jeremyvdw

Copy link
Copy Markdown

Hi,
Any updates on this?

Thanks

@simplesteph

Copy link
Copy Markdown

protected static final org.apache.avro.Conversions.DecimalConversion DECIMAL_CONVERSION = new org.apache.avro.Conversions.DecimalConversion();
#end

public static final Collection<Conversion<?>> CONVERSIONS = new ArrayList<Conversion<?>>();

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.

There are missing imports from the template:

import java.util.ArrayList;
import java.util.Collection;
import org.apache.avro.Conversion;

What is CONVERSIONS used for? Is this needed?

@nandorKollar

Copy link
Copy Markdown
Contributor

@rdblue I think think the template is not yet processed correctly. It seems, that for union schema, SpecificCompiler#hasLogicalTypeField returns false even when the union has logical types, due to this the conversion fields are not included in the compiled class. Should it instead check each type in the union? Something like this:

public boolean hasLogicalTypeField(Schema schema) {
for (Schema.Field field : schema.getFields()) {
if (field.schema().getType() == Schema.Type.UNION) {
for (Schema unionSchema : field.schema().getTypes()) {
if (unionSchema.getLogicalType() != null) {
return true;
}
}
} else {
if (field.schema().getLogicalType() != null) {
return true;
}
}
}
return false;
}

@busbey

busbey commented Aug 18, 2017

Copy link
Copy Markdown
Contributor

@rdblue are you still working on this?

@Igosuki

Igosuki commented Sep 26, 2017

Copy link
Copy Markdown

This won't work for unions, because a union field may have multiple logical types by definition...

@wheelerlaw

Copy link
Copy Markdown

What happened to this? This seems pretty major, because without it, unions with built-in logical types are broken.

@rdblue

rdblue commented Mar 23, 2018

Copy link
Copy Markdown
Contributor Author

@wheelerlaw, feel free to pick this up. I'll help review the changes.

@lewisdawson

Copy link
Copy Markdown

Is there still any intention of finishing & merging this PR? I ran into this today when trying to serialize avro data to a Kafka stream.

@katrinsko

Copy link
Copy Markdown
Contributor

I submitted a new PR today, #329, with a different solution approach to AVRO-1891. Feedback appreciated!

@katrinsko

Copy link
Copy Markdown
Contributor

Hey @lewisdawson, there is a workaround for the current version that might help you. It involves implementing your own Avro serializer (or slightly modifying the one you're using, I did that with Confluent's). The key is to explicitly add the right conversions for logical types to SpecificData when creating the DatumWriter and DatumReader. If you still need help, create a post on StackOverflow and we'll take it over there.

@lazyval

lazyval commented Dec 4, 2018

Copy link
Copy Markdown

@scatrin can you follow up on that? it seems like the problem is still ongoing despite the effort from Avro community

I wonder if it's possible to do in user space, without building the custom version of avro library

@katrinsko

katrinsko commented Dec 6, 2018

Copy link
Copy Markdown
Contributor

@lazyval The workaround that I did applies to a setting with Kafka and Confluent's Schema Registry. The trick is to modify the serializer/deserializers (from Confluent) to explicitly register the needed conversions when creating the SpecificDatumWriter/Reader. (for instance here). SpecificDatumWriter has a getter for the SpecificData instance and SpecificData (via its superclass) has a method for registering logical type conversions. The cool thing is that the conversion class does not have to be part of Avro, you can build your own if you want to. Then just make sure that the modified classes are in the classpath when serializing/deserializing and that it is referenced properly in the Kafka settings.

@dkulp

dkulp commented Dec 11, 2018

Copy link
Copy Markdown
Member

I merged #329, is this not needed now?

@katrinsko

Copy link
Copy Markdown
Contributor

@dkulp I looked into the possibility of completing this PR first but ended up choosing a different solution and submitting it in #329 instead. With #329 merged I would say that this one is obsolete.

@iemejia iemejia closed this Dec 13, 2018
SingingBush pushed a commit to SingingBush/avro that referenced this pull request Jun 29, 2023
Correcting cache annotation for classpath
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Java Pull Requests for Java binding

Projects

None yet

Development

Successfully merging this pull request may close these issues.