-
Notifications
You must be signed in to change notification settings - Fork 13.9k
[FLINK-39036][formats] Honor microsecond Avro logical types in RowDataToAvroConverters #28071
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
daguimu
wants to merge
1
commit into
apache:master
Choose a base branch
from
daguimu:fix/avro-timestamp-micros-FLINK-39036
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+140
−3
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,13 +29,15 @@ | |
| import org.apache.flink.table.types.logical.RowType; | ||
| import org.apache.flink.util.CollectionUtil; | ||
|
|
||
| import org.apache.avro.LogicalTypes; | ||
| import org.apache.avro.Schema; | ||
| import org.apache.avro.generic.GenericData; | ||
| import org.apache.avro.generic.GenericRecord; | ||
| import org.apache.avro.util.Utf8; | ||
|
|
||
| import java.io.Serializable; | ||
| import java.nio.ByteBuffer; | ||
| import java.time.Instant; | ||
| import java.time.ZoneOffset; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
@@ -166,7 +168,11 @@ public Object convert(Schema schema, Object object) { | |
|
|
||
| @Override | ||
| public Object convert(Schema schema, Object object) { | ||
| return ((TimestampData) object).toInstant().toEpochMilli(); | ||
| final TimestampData timestampData = (TimestampData) object; | ||
| if (isMicrosLogicalType(schema)) { | ||
| return toEpochMicros(timestampData.toInstant()); | ||
| } | ||
| return timestampData.toInstant().toEpochMilli(); | ||
| } | ||
| }; | ||
| } else { | ||
|
|
@@ -176,7 +182,14 @@ public Object convert(Schema schema, Object object) { | |
|
|
||
| @Override | ||
| public Object convert(Schema schema, Object object) { | ||
| return ((TimestampData) object) | ||
| final TimestampData timestampData = (TimestampData) object; | ||
| if (isMicrosLogicalType(schema)) { | ||
| return toEpochMicros( | ||
| timestampData | ||
| .toLocalDateTime() | ||
| .toInstant(ZoneOffset.UTC)); | ||
| } | ||
| return timestampData | ||
| .toLocalDateTime() | ||
| .toInstant(ZoneOffset.UTC) | ||
| .toEpochMilli(); | ||
|
|
@@ -194,7 +207,11 @@ public Object convert(Schema schema, Object object) { | |
|
|
||
| @Override | ||
| public Object convert(Schema schema, Object object) { | ||
| return ((TimestampData) object).toInstant().toEpochMilli(); | ||
| final TimestampData timestampData = (TimestampData) object; | ||
| if (isMicrosLogicalType(schema)) { | ||
| return toEpochMicros(timestampData.toInstant()); | ||
| } | ||
| return timestampData.toInstant().toEpochMilli(); | ||
| } | ||
| }; | ||
| } | ||
|
|
@@ -353,4 +370,16 @@ public Object convert(Schema schema, Object object) { | |
| } | ||
| }; | ||
| } | ||
|
|
||
| private static boolean isMicrosLogicalType(Schema schema) { | ||
| final org.apache.avro.LogicalType logicalType = schema.getLogicalType(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: this should be an import |
||
| return logicalType == LogicalTypes.timestampMicros() | ||
| || logicalType == LogicalTypes.localTimestampMicros(); | ||
| } | ||
|
|
||
| private static long toEpochMicros(Instant instant) { | ||
| return Math.addExact( | ||
| Math.multiplyExact(instant.getEpochSecond(), 1_000_000L), | ||
| instant.getNano() / 1_000L); | ||
| } | ||
| } | ||
108 changes: 108 additions & 0 deletions
108
...s/flink-avro/src/test/java/org/apache/flink/formats/avro/RowDataToAvroConvertersTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.flink.formats.avro; | ||
|
|
||
| import org.apache.flink.formats.avro.RowDataToAvroConverters.RowDataToAvroConverter; | ||
| import org.apache.flink.table.data.TimestampData; | ||
| import org.apache.flink.table.types.logical.LocalZonedTimestampType; | ||
| import org.apache.flink.table.types.logical.TimestampType; | ||
|
|
||
| import org.apache.avro.Schema; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| /** Tests for {@link RowDataToAvroConverters}. */ | ||
| class RowDataToAvroConvertersTest { | ||
|
|
||
| private static final String TIMESTAMP_MILLIS_SCHEMA = | ||
| "{\"type\":\"long\",\"logicalType\":\"timestamp-millis\"}"; | ||
| private static final String TIMESTAMP_MICROS_SCHEMA = | ||
| "{\"type\":\"long\",\"logicalType\":\"timestamp-micros\"}"; | ||
| private static final String LOCAL_TIMESTAMP_MILLIS_SCHEMA = | ||
| "{\"type\":\"long\",\"logicalType\":\"local-timestamp-millis\"}"; | ||
| private static final String LOCAL_TIMESTAMP_MICROS_SCHEMA = | ||
| "{\"type\":\"long\",\"logicalType\":\"local-timestamp-micros\"}"; | ||
|
|
||
| @Test | ||
| void testTimestampWithLocalTimeZoneRespectsMicrosLogicalType() { | ||
| // FLINK-39036: writer must produce micros when the Avro schema declares | ||
| // logicalType=timestamp-micros, otherwise downstream readers that respect | ||
| // the logical type interpret the millis value as micros and shift the | ||
| // timestamp by a factor of 1000. | ||
| final RowDataToAvroConverter converter = | ||
| RowDataToAvroConverters.createConverter(new LocalZonedTimestampType(6), false); | ||
|
|
||
| final TimestampData timestamp = | ||
| TimestampData.fromLocalDateTime( | ||
| LocalDateTime.parse("2024-01-02T03:04:05.123456")); | ||
|
|
||
| final long micros = | ||
| (long) converter.convert(new Schema.Parser().parse(TIMESTAMP_MICROS_SCHEMA), timestamp); | ||
| assertThat(micros).isEqualTo(1_704_164_645_123_456L); | ||
|
|
||
| final long millis = | ||
| (long) converter.convert(new Schema.Parser().parse(TIMESTAMP_MILLIS_SCHEMA), timestamp); | ||
| assertThat(millis).isEqualTo(1_704_164_645_123L); | ||
| } | ||
|
|
||
| @Test | ||
| void testTimestampWithoutTimeZoneRespectsLocalMicrosLogicalType() { | ||
| // TIMESTAMP_WITHOUT_TIME_ZONE maps to Avro local-timestamp-* logical types | ||
| // under the new mapping. The converter must honor local-timestamp-micros | ||
| // and emit microseconds instead of milliseconds. | ||
| final RowDataToAvroConverter converter = | ||
| RowDataToAvroConverters.createConverter(new TimestampType(6), false); | ||
|
|
||
| final TimestampData timestamp = | ||
| TimestampData.fromLocalDateTime( | ||
| LocalDateTime.parse("2024-01-02T03:04:05.123456")); | ||
|
|
||
| final long micros = | ||
| (long) | ||
| converter.convert( | ||
| new Schema.Parser().parse(LOCAL_TIMESTAMP_MICROS_SCHEMA), timestamp); | ||
| assertThat(micros).isEqualTo(1_704_164_645_123_456L); | ||
|
|
||
| final long millis = | ||
| (long) | ||
| converter.convert( | ||
| new Schema.Parser().parse(LOCAL_TIMESTAMP_MILLIS_SCHEMA), timestamp); | ||
| assertThat(millis).isEqualTo(1_704_164_645_123L); | ||
| } | ||
|
|
||
| @Test | ||
| void testLegacyTimestampMappingRespectsMicrosLogicalType() { | ||
| // The legacy mapping path also has to honor timestamp-micros when present | ||
| // in the Avro schema, since users may serialize against an externally | ||
| // provided schema with the micros logical type. | ||
| final RowDataToAvroConverter converter = | ||
| RowDataToAvroConverters.createConverter(new TimestampType(6), true); | ||
|
|
||
| final TimestampData timestamp = | ||
| TimestampData.fromLocalDateTime( | ||
| LocalDateTime.parse("2024-01-02T03:04:05.123456")); | ||
|
|
||
| final long micros = | ||
| (long) converter.convert(new Schema.Parser().parse(TIMESTAMP_MICROS_SCHEMA), timestamp); | ||
| assertThat(micros).isEqualTo(1_704_164_645_123_456L); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree the the idea here, but am concerned that introducing this behaviour might result in a regression for existing applications. I wonder if it would be safer to introduce the new behaviour under a config flag until there is a version change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the careful read.
My read is that this is a correctness fix rather than a behavior change worth preserving. The read path in
AvroToRowDataConvertersalready honorstimestamp-micros/local-timestamp-microsand returns microseconds, while the write path here ignores the same logical type and always emits milliseconds. A pipeline that round-trips through Flink with atimestamp-microsschema silently scales timestamps by 1000× — that's a data-corruption bug, not a stable contract.The fix is also gated by what the schema declares: users whose schemas are
timestamp-millis(the previous behavior for both branches) see no change at all. Only users who already declaredtimestamp-microsand were silently getting wrong values are affected, and for them the new output is what their schema asked for.That said, the scope question deserves a committer's call. Could one of the flink-avro maintainers weigh in on whether a config flag is warranted on master? If so, happy to add one as a follow-up.