Skip to content

Conversation

@robvadai
Copy link

@robvadai robvadai commented Jun 19, 2021

Implementation of http://avro.apache.org/docs/current/spec.html#Duration

Can be used to serialize/deserialize to/from Java POJOs and Generic Fixed type.

Make sure you have checked all steps below.

Jira

Tests

  • My PR adds the following unit tests:
  • lang/java/avro/src/test/java/org/apache/avro/TestDurationConversion.java

Commits

Documentation

  • In case of new functionality, my PR adds documentation that describes how to use it.
    • All the public functions and the classes in the PR contain Javadoc that explain what it does: existing conversaion don't have Javadoc so I didn't add one. This is considered an internal API IMHO.

@github-actions github-actions bot added the Java Pull Requests for Java binding label Jun 19, 2021
@adiroy05
Copy link

assign to me


public static class DurationConversion extends Conversion<Duration> {

private static final int MONTH_DAYS = 30;
Copy link
Contributor

Choose a reason for hiding this comment

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

A month has 28, 29, 30 or 31 days. Hardcoding a single value (31 days/month occurs more often) is basically a bug. Note that this cannot be fixed if we choose to support all possible duration values (see the conversation comments for more info).

Copy link
Contributor

@opwvhk opwvhk left a comment

Choose a reason for hiding this comment

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

A Java Duration is a fixed amount of time, and cannot compensate for the varying number of days per month. A Java Period can, but cannot consider fractions of a day.

To "fix" the underlying incompatibility between an Avro duration and the Java Duration/Period classes, and also to avoid bugs with respect to leap seconds (avoided by Java because it doesn't mix days & day fractions), the conversion must disallow the combination of non-zero milliseconds with any other non-zero field. This means that either the milliseconds field must be 0, or all other fields must be 0.

Also, the conversion should accept both java.time.Duration and java.time.Period to convert to an Avro duration, and return the most appropriate one converting back (throwing an exception when both the milliseconds field and one of the other fields is non-zero).

@snuyanzin
Copy link
Contributor

Hi @robvadai
i'm looking forward to have that feature in Avro, could you please tell us whether you are going to continue work on it or not?

@opwvhk
Copy link
Contributor

opwvhk commented Sep 26, 2023

As an alternative to this PR, I've opened #2520. This implements a new Java Time class TimePeriod, that combines functionality of java.time.Period and java.time.Duration. Other than that, the PR is equivalent.

@opwvhk
Copy link
Contributor

opwvhk commented Oct 5, 2023

Closing, as there is now an alternative implementation.

@opwvhk opwvhk closed this Oct 5, 2023
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.

4 participants