-
Notifications
You must be signed in to change notification settings - Fork 15.1k
KAFKA-2622: Add Time logical type for Copycat. #285
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
77 changes: 77 additions & 0 deletions
77
copycat/api/src/main/java/org/apache/kafka/copycat/data/Time.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,77 @@ | ||
| /** | ||
| * 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.kafka.copycat.data; | ||
|
|
||
| import org.apache.kafka.copycat.errors.DataException; | ||
|
|
||
| import java.util.Calendar; | ||
| import java.util.TimeZone; | ||
|
|
||
| /** | ||
| * <p> | ||
| * A time representing a specific point in a day, not tied to any specific date. The corresponding Java type is a | ||
| * java.util.Date where only hours, minutes, seconds, and milliseconds can be non-zero. This effectively makes it a | ||
| * point in time during the first day after the Unix epoch. The underlying representation is an integer | ||
| * representing the number of milliseconds after midnight. | ||
| * </p> | ||
| */ | ||
| public class Time { | ||
| public static final String LOGICAL_NAME = "org.apache.kafka.copycat.data.Time"; | ||
|
|
||
| private static final long MILLIS_PER_DAY = 24 * 60 * 60 * 1000; | ||
|
|
||
| private static final TimeZone UTC = TimeZone.getTimeZone("UTC"); | ||
|
|
||
| /** | ||
| * Returns a SchemaBuilder for a Time. By returning a SchemaBuilder you can override additional schema settings such | ||
| * as required/optional, default value, and documentation. | ||
| * @return a SchemaBuilder | ||
| */ | ||
| public static SchemaBuilder builder() { | ||
| return SchemaBuilder.int32() | ||
| .name(LOGICAL_NAME) | ||
| .version(1); | ||
| } | ||
|
|
||
| public static final Schema SCHEMA = builder().schema(); | ||
|
|
||
| /** | ||
| * Convert a value from its logical format (Time) to it's encoded format. | ||
| * @param value the logical value | ||
| * @return the encoded value | ||
| */ | ||
| public static int fromLogical(Schema schema, java.util.Date value) { | ||
| if (schema.name() == null || !(schema.name().equals(LOGICAL_NAME))) | ||
| throw new DataException("Requested conversion of Time object but the schema does not match."); | ||
| Calendar calendar = Calendar.getInstance(UTC); | ||
| calendar.setTime(value); | ||
| long unixMillis = calendar.getTimeInMillis(); | ||
| if (unixMillis < 0 || unixMillis > MILLIS_PER_DAY) { | ||
| throw new DataException("Copycat Time type should not have any time fields set to non-zero values."); | ||
| } | ||
| return (int) unixMillis; | ||
| } | ||
|
|
||
| public static java.util.Date toLogical(Schema schema, int value) { | ||
| if (schema.name() == null || !(schema.name().equals(LOGICAL_NAME))) | ||
| throw new DataException("Requested conversion of Date object but the schema does not match."); | ||
| if (value < 0 || value > MILLIS_PER_DAY) | ||
| throw new DataException("Time values must use number of milliseconds greater than 0 and less than 86400000"); | ||
| return new java.util.Date(value); | ||
| } | ||
| } | ||
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
80 changes: 80 additions & 0 deletions
80
copycat/api/src/test/java/org/apache/kafka/copycat/data/TimeTest.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,80 @@ | ||
| /** | ||
| * 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.kafka.copycat.data; | ||
|
|
||
| import org.apache.kafka.copycat.errors.DataException; | ||
| import org.junit.Test; | ||
|
|
||
| import java.util.Calendar; | ||
| import java.util.GregorianCalendar; | ||
| import java.util.TimeZone; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
|
|
||
| public class TimeTest { | ||
| private static final GregorianCalendar EPOCH; | ||
| private static final GregorianCalendar EPOCH_PLUS_DATE_COMPONENT; | ||
| private static final GregorianCalendar EPOCH_PLUS_TEN_THOUSAND_MILLIS; | ||
| static { | ||
| EPOCH = new GregorianCalendar(1970, Calendar.JANUARY, 1, 0, 0, 0); | ||
| EPOCH.setTimeZone(TimeZone.getTimeZone("UTC")); | ||
|
|
||
| EPOCH_PLUS_TEN_THOUSAND_MILLIS = new GregorianCalendar(1970, Calendar.JANUARY, 1, 0, 0, 0); | ||
| EPOCH_PLUS_TEN_THOUSAND_MILLIS.setTimeZone(TimeZone.getTimeZone("UTC")); | ||
| EPOCH_PLUS_TEN_THOUSAND_MILLIS.add(Calendar.MILLISECOND, 10000); | ||
|
|
||
|
|
||
| EPOCH_PLUS_DATE_COMPONENT = new GregorianCalendar(1970, Calendar.JANUARY, 1, 0, 0, 0); | ||
| EPOCH_PLUS_DATE_COMPONENT.setTimeZone(TimeZone.getTimeZone("UTC")); | ||
| EPOCH_PLUS_DATE_COMPONENT.add(Calendar.DATE, 10000); | ||
| } | ||
|
|
||
| @Test | ||
| public void testBuilder() { | ||
| Schema plain = Time.SCHEMA; | ||
| assertEquals(Time.LOGICAL_NAME, plain.name()); | ||
| assertEquals(1, (Object) plain.version()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testFromLogical() { | ||
| assertEquals(0, Time.fromLogical(Time.SCHEMA, EPOCH.getTime())); | ||
| assertEquals(10000, Time.fromLogical(Time.SCHEMA, EPOCH_PLUS_TEN_THOUSAND_MILLIS.getTime())); | ||
| } | ||
|
|
||
| @Test(expected = DataException.class) | ||
| public void testFromLogicalInvalidSchema() { | ||
| Time.fromLogical(Time.builder().name("invalid").build(), EPOCH.getTime()); | ||
| } | ||
|
|
||
| @Test(expected = DataException.class) | ||
| public void testFromLogicalInvalidHasDateComponents() { | ||
| Time.fromLogical(Time.SCHEMA, EPOCH_PLUS_DATE_COMPONENT.getTime()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testToLogical() { | ||
| assertEquals(EPOCH.getTime(), Time.toLogical(Time.SCHEMA, 0)); | ||
| assertEquals(EPOCH_PLUS_TEN_THOUSAND_MILLIS.getTime(), Time.toLogical(Time.SCHEMA, 10000)); | ||
| } | ||
|
|
||
| @Test(expected = DataException.class) | ||
| public void testToLogicalInvalidSchema() { | ||
| Time.toLogical(Time.builder().name("invalid").build(), 0); | ||
| } | ||
| } |
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
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.
Is the claim in the exception correct? We should have have non-zero values for some fields.
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.
Right. It should be "any date field"
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.
Yup, good catch. Filed #291.