-
Notifications
You must be signed in to change notification settings - Fork 57
Fixed avro timestamp types #613
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
chernser
wants to merge
3
commits into
main
Choose a base branch
from
fix_date_time_serialization
base: main
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.
+638
−31
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
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 |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| package com.clickhouse.kafka.connect.sink; | ||
|
|
||
| import com.clickhouse.client.api.ClientConfigProperties; | ||
| import com.clickhouse.kafka.connect.avro.test.Event; | ||
| import com.clickhouse.kafka.connect.avro.test.Image; | ||
| import com.clickhouse.kafka.connect.sink.db.helper.ClickHouseHelperClient; | ||
| import com.clickhouse.kafka.connect.sink.helper.ClickHouseTestHelpers; | ||
|
|
@@ -13,6 +14,7 @@ | |
| import io.confluent.connect.protobuf.ProtobufConverter; | ||
| import io.confluent.connect.protobuf.ProtobufConverterConfig; | ||
| import io.confluent.connect.protobuf.ProtobufDataConfig; | ||
| import io.confluent.kafka.schemaregistry.avro.AvroSchema; | ||
| import io.confluent.kafka.schemaregistry.client.MockSchemaRegistryClient; | ||
| import io.confluent.kafka.schemaregistry.protobuf.ProtobufSchema; | ||
| import io.confluent.kafka.serializers.KafkaAvroSerializer; | ||
|
|
@@ -42,10 +44,21 @@ | |
| import java.io.Serializable; | ||
| import java.math.BigDecimal; | ||
| import java.nio.ByteBuffer; | ||
| import java.text.DateFormat; | ||
| import java.text.SimpleDateFormat; | ||
| import java.time.Instant; | ||
| import java.time.LocalDate; | ||
| import java.time.LocalTime; | ||
| import java.time.ZoneId; | ||
| import java.time.format.DateTimeFormatter; | ||
| import java.time.temporal.ChronoUnit; | ||
| import java.util.*; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
| import java.util.stream.LongStream; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.IntStream; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertInstanceOf; | ||
|
|
@@ -227,7 +240,9 @@ public void supportDatesTest() { | |
|
|
||
| String topic = createTopicName("support-dates-table-test"); | ||
| ClickHouseTestHelpers.dropTable(chc, topic); | ||
| ClickHouseTestHelpers.createTable(chc, topic, "CREATE TABLE `%s` ( `off16` Int16, date_number Nullable(Date), date32_number Nullable(Date32), datetime_number DateTime, datetime64_number DateTime64, timestamp_int64 Int64, timestamp_date DateTime64, time_int32 Int32, time_date32 Date32, date_date Date, datetime_date DateTime ) Engine = MergeTree ORDER BY off16"); | ||
| ClickHouseTestHelpers.createTable(chc, topic, "CREATE TABLE `%s` " + | ||
| " ( `off16` Int16, date_number Nullable(Date), date32_number Nullable(Date32), datetime_number DateTime, " + | ||
| " datetime64_number DateTime64, datetime64_3_number DateTime64(3), datetime64_6_number DateTime64(6), datetime64_9_number DateTime64(9), timestamp_int64 Int64, timestamp_date DateTime64, time_int32 Int32, time_date32 Date32, date_date Date, datetime_date DateTime ) Engine = MergeTree ORDER BY off16"); | ||
| // https://github.com/apache/kafka/blob/trunk/connect/api/src/test/java/org/apache/kafka/connect/data/StructTest.java#L95-L98 | ||
| Collection<SinkRecord> sr = SchemaTestData.createDateType(topic, 1); | ||
|
|
||
|
|
@@ -237,6 +252,16 @@ public void supportDatesTest() { | |
| chst.stop(); | ||
|
|
||
| assertEquals(sr.size(), ClickHouseTestHelpers.countRows(chc, topic)); | ||
|
|
||
| List<JSONObject> rows = ClickHouseTestHelpers.getAllRowsAsJson(chc, topic); | ||
| for (JSONObject row : rows) { | ||
| String dateTime64_9 = row.getString("datetime64_9_number"); | ||
| String dateTime64_6 = row.getString("datetime64_6_number"); | ||
| String dateTime64_3 = row.getString("datetime64_3_number"); | ||
|
|
||
| assertTrue(dateTime64_9.contains(dateTime64_3), dateTime64_3 + " is not a substring of " + dateTime64_9); | ||
| assertTrue(dateTime64_9.contains(dateTime64_6), dateTime64_6 + " is not a substring of " + dateTime64_9); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
|
|
@@ -1228,38 +1253,14 @@ public void testAvroWithUnion() throws Exception { | |
| .setName("image1") | ||
| .setContent("content") | ||
| .build(); | ||
|
|
||
|
|
||
| Image image2 = Image.newBuilder() | ||
| .setName("image2") | ||
| .setContent(ByteBuffer.wrap("content2".getBytes())) | ||
| .setDescription("description") | ||
| .build(); | ||
| String topic = createTopicName("test_avro_union_string"); | ||
|
|
||
| MockSchemaRegistryClient schemaRegistry = new MockSchemaRegistryClient(); | ||
| // Register your test schema | ||
| String subject = topic + "-value"; | ||
| schemaRegistry.register(subject, image1.getSchema()); | ||
|
|
||
| AvroConverter converter = new AvroConverter(schemaRegistry); | ||
| Map<String, Object> converterConfig = new HashMap<>(); | ||
| converterConfig.put(ProtobufConverterConfig.AUTO_REGISTER_SCHEMAS, true); | ||
| converterConfig.put(ProtobufDataConfig.GENERATE_INDEX_FOR_UNIONS_CONFIG, false); | ||
| converterConfig.put(KafkaAvroSerializerConfig.SCHEMA_REGISTRY_URL_CONFIG, "mock://test-url"); | ||
|
|
||
| converter.configure(converterConfig, false); | ||
| KafkaAvroSerializer serializer = new KafkaAvroSerializer(schemaRegistry); | ||
|
|
||
| SchemaAndValue image1ConnectData = converter.toConnectData(topic, serializer.serialize(topic, image1)); | ||
| SchemaAndValue image2ConnectData = converter.toConnectData(topic, serializer.serialize(topic, image2)); | ||
|
|
||
| List<SinkRecord> records = Arrays.asList( | ||
| new SinkRecord(topic, 0, null, null, | ||
| image1ConnectData.schema(), image1ConnectData.value(), 0), | ||
| new SinkRecord(topic, 0, null, null, | ||
| image2ConnectData.schema(), image2ConnectData.value(), 1) | ||
| ); | ||
| List<SinkRecord> records = SchemaTestData.convertAvroToSinkRecord(topic, new AvroSchema(Image.getClassSchema()), Arrays.asList(image1, image2)); | ||
| Map<String, String> props = createProps(); | ||
| ClickHouseHelperClient chc = createClient(props); | ||
| ClickHouseTestHelpers.dropTable(chc, topic); | ||
|
|
@@ -1289,4 +1290,48 @@ public void testAvroWithUnion() throws Exception { | |
| assertEquals("description", row.getString("description")); | ||
| assertEquals("content2", row.getString("content")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testAvroDateAndTimeTypes() throws Exception { | ||
|
|
||
| final String topic = createTopicName("test_avro_timestamps"); | ||
| final ZoneId tz = ZoneId.of("UTC"); | ||
| final Instant now = Instant.now(); | ||
|
|
||
| List<Object> events = IntStream.range(0, 3).mapToObj(i -> { | ||
| Instant time = now.plus(i * 1000, ChronoUnit.MILLIS); | ||
| Event event = Event.newBuilder() | ||
| .setId(i) | ||
| .setTime1(time) | ||
| .setTime2(LocalTime.ofInstant(time, tz)) | ||
| .build(); | ||
| return event; | ||
| }).collect(Collectors.toList()); | ||
|
|
||
| List<SinkRecord> records = SchemaTestData.convertAvroToSinkRecord(topic, new AvroSchema(Event.getClassSchema()), events); | ||
|
|
||
| Map<String, String> props = createProps(); | ||
| ClickHouseHelperClient chc = createClient(props); | ||
| ClickHouseTestHelpers.dropTable(chc, topic); | ||
| ClickHouseTestHelpers.createTable(chc, topic, | ||
| "CREATE TABLE `%s` (`id` Int64, `time1` DateTime64(3), `time2` DateTime64(3)) Engine = MergeTree ORDER BY ()"); | ||
|
|
||
| ClickHouseSinkTask chst = new ClickHouseSinkTask(); | ||
| chst.start(props); | ||
| chst.put(records); | ||
| chst.stop(); | ||
|
Collaborator
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. I do not see any assertion to validate the test
Contributor
Author
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. Sorry, missed it :-( |
||
|
|
||
|
|
||
| List<JSONObject> rows = ClickHouseTestHelpers.getAllRowsAsJson(chc, topic); | ||
| assertEquals(events.size(), rows.size()); | ||
| DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS").withZone(tz); | ||
| DateTimeFormatter localFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"); | ||
| for (int i = 0; i < events.size(); i++) { | ||
| Event event = (Event) events.get(i); | ||
| JSONObject row = rows.get(i); | ||
| assertEquals(event.getId(), row.getLong("id")); | ||
| assertEquals(formatter.format(event.getTime1()), row.get("time1")); | ||
| assertEquals(event.getTime2().atDate(LocalDate.of(1970, 1, 1)).format(localFormatter), row.get("time2")); | ||
| } | ||
| } | ||
| } | ||
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,10 @@ | ||
|
|
||
| To generate avro | ||
| ```shell | ||
| java -jar ~/tools/avro-tools-1.12.0.jar idl event.idl > even.avro | ||
chernser marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ``` | ||
|
|
||
| To generate code | ||
| ```shell | ||
| java -jar ~/tools/avro-tools-1.12.0.jar compile schema event.avro ../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,13 @@ | ||
| namespace com.clickhouse.kafka.connect.avro.test; | ||
| schema Event; | ||
|
|
||
| record Event { | ||
| // Just ID | ||
| long id; | ||
|
|
||
| // The timestamp-millis logical type represents an instant on the global timeline, independent of a particular time zone or calendar, with a precision of one millisecond. | ||
| timestamp_ms time1; | ||
|
|
||
| // The time-millis logical type represents a time of day, with no reference to a particular calendar, time zone or date, with a precision of one millisecond. | ||
| time_ms time2; | ||
| } |
Oops, something went wrong.
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.
The
BASESarray lacks documentation explaining the mapping between array indices and precision levels. Add a comment describing how each index corresponds to a specific precision value (e.g., index 3 = precision 3, value 1 = no scaling).