-
Notifications
You must be signed in to change notification settings - Fork 990
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
[core] Support timestamp field type in record level expire #4417
Merged
Merged
Changes from all commits
Commits
Show all changes
4 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 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 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 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 |
---|---|---|
|
@@ -20,11 +20,15 @@ | |
|
||
import org.apache.paimon.CoreOptions; | ||
import org.apache.paimon.KeyValue; | ||
import org.apache.paimon.data.Timestamp; | ||
import org.apache.paimon.reader.RecordReader; | ||
import org.apache.paimon.types.BigIntType; | ||
import org.apache.paimon.types.DataField; | ||
import org.apache.paimon.types.DataType; | ||
import org.apache.paimon.types.IntType; | ||
import org.apache.paimon.types.LocalZonedTimestampType; | ||
import org.apache.paimon.types.RowType; | ||
import org.apache.paimon.types.TimestampType; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
|
@@ -38,6 +42,7 @@ public class RecordLevelExpire { | |
private final int timeFieldIndex; | ||
private final int expireTime; | ||
private final CoreOptions.TimeFieldType timeFieldType; | ||
private final DataField rawDataField; | ||
|
||
@Nullable | ||
public static RecordLevelExpire create(CoreOptions options, RowType rowType) { | ||
|
@@ -62,27 +67,44 @@ public static RecordLevelExpire create(CoreOptions options, RowType rowType) { | |
|
||
CoreOptions.TimeFieldType timeFieldType = options.recordLevelTimeFieldType(); | ||
DataField field = rowType.getField(timeFieldName); | ||
if (!((timeFieldType == CoreOptions.TimeFieldType.SECONDS_INT | ||
&& field.type() instanceof IntType) | ||
|| (timeFieldType == CoreOptions.TimeFieldType.SECONDS_LONG | ||
&& field.type() instanceof BigIntType) | ||
|| (timeFieldType == CoreOptions.TimeFieldType.MILLIS_LONG | ||
&& field.type() instanceof BigIntType))) { | ||
if (!isValidateFieldType(timeFieldType, field)) { | ||
throw new IllegalArgumentException( | ||
String.format( | ||
"The record level time field type should be one of SECONDS_INT,SECONDS_LONG or MILLIS_LONG, " | ||
+ "but time field type is %s, field type is %s.", | ||
timeFieldType, field.type())); | ||
"The record level time field type should be one of SECONDS_INT, SECONDS_LONG, MILLIS_LONG or TIMESTAMP, " | ||
+ "but time field type is %s, field type is %s. You can specify the type through the config '%s'.", | ||
timeFieldType, | ||
field.type(), | ||
CoreOptions.RECORD_LEVEL_TIME_FIELD_TYPE.key())); | ||
} | ||
|
||
return new RecordLevelExpire(fieldIndex, (int) expireTime.getSeconds(), timeFieldType); | ||
return new RecordLevelExpire( | ||
fieldIndex, (int) expireTime.getSeconds(), timeFieldType, field); | ||
} | ||
|
||
private static boolean isValidateFieldType( | ||
CoreOptions.TimeFieldType timeFieldType, DataField field) { | ||
DataType dataType = field.type(); | ||
return ((timeFieldType == CoreOptions.TimeFieldType.SECONDS_INT | ||
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. DataType dataType = field.type(); |
||
&& dataType instanceof IntType) | ||
|| (timeFieldType == CoreOptions.TimeFieldType.SECONDS_LONG | ||
&& dataType instanceof BigIntType) | ||
|| (timeFieldType == CoreOptions.TimeFieldType.MILLIS_LONG | ||
&& dataType instanceof BigIntType) | ||
|| (timeFieldType == CoreOptions.TimeFieldType.TIMESTAMP | ||
&& dataType instanceof TimestampType) | ||
|| (timeFieldType == CoreOptions.TimeFieldType.TIMESTAMP | ||
&& dataType instanceof LocalZonedTimestampType)); | ||
} | ||
|
||
private RecordLevelExpire( | ||
int timeFieldIndex, int expireTime, CoreOptions.TimeFieldType timeFieldType) { | ||
int timeFieldIndex, | ||
int expireTime, | ||
CoreOptions.TimeFieldType timeFieldType, | ||
DataField rawDataField) { | ||
this.timeFieldIndex = timeFieldIndex; | ||
this.expireTime = expireTime; | ||
this.timeFieldType = timeFieldType; | ||
this.rawDataField = rawDataField; | ||
} | ||
|
||
public FileReaderFactory<KeyValue> wrap(FileReaderFactory<KeyValue> readerFactory) { | ||
|
@@ -107,6 +129,29 @@ private RecordReader<KeyValue> wrap(RecordReader<KeyValue> reader) { | |
case MILLIS_LONG: | ||
recordTime = (int) (kv.value().getLong(timeFieldIndex) / 1000); | ||
break; | ||
case TIMESTAMP: | ||
Timestamp timestamp; | ||
if (rawDataField.type() instanceof TimestampType) { | ||
TimestampType timestampType = (TimestampType) rawDataField.type(); | ||
timestamp = | ||
kv.value() | ||
.getTimestamp( | ||
timeFieldIndex, | ||
timestampType.getPrecision()); | ||
} else if (rawDataField.type() instanceof LocalZonedTimestampType) { | ||
LocalZonedTimestampType timestampType = | ||
(LocalZonedTimestampType) rawDataField.type(); | ||
timestamp = | ||
kv.value() | ||
.getTimestamp( | ||
timeFieldIndex, | ||
timestampType.getPrecision()); | ||
} else { | ||
throw new UnsupportedOperationException( | ||
"Unsupported timestamp type: " + rawDataField.type()); | ||
} | ||
recordTime = (int) (timestamp.getMillisecond() / 1000); | ||
break; | ||
default: | ||
String msg = | ||
String.format( | ||
|
66 changes: 66 additions & 0 deletions
66
...on-core/src/test/java/org/apache/paimon/table/RecordLevelExpireWithTimestampBaseTest.java
This file contains 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,66 @@ | ||
/* | ||
* 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.paimon.table; | ||
|
||
import org.apache.paimon.CoreOptions; | ||
import org.apache.paimon.catalog.PrimaryKeyTableTestBase; | ||
import org.apache.paimon.data.GenericRow; | ||
import org.apache.paimon.data.Timestamp; | ||
import org.apache.paimon.options.Options; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.time.Duration; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
abstract class RecordLevelExpireWithTimestampBaseTest extends PrimaryKeyTableTestBase { | ||
|
||
@Override | ||
protected Options tableOptions() { | ||
Options options = new Options(); | ||
options.set(CoreOptions.BUCKET, 1); | ||
options.set(CoreOptions.RECORD_LEVEL_EXPIRE_TIME, Duration.ofSeconds(1)); | ||
options.set(CoreOptions.RECORD_LEVEL_TIME_FIELD, "col1"); | ||
options.set(CoreOptions.RECORD_LEVEL_TIME_FIELD_TYPE, CoreOptions.TimeFieldType.TIMESTAMP); | ||
return options; | ||
} | ||
|
||
@Test | ||
public void testTimestampTypeExpire() throws Exception { | ||
long millis = System.currentTimeMillis(); | ||
Timestamp timestamp1 = Timestamp.fromEpochMillis(millis - 60 * 1000); | ||
Timestamp timestamp2 = Timestamp.fromEpochMillis(millis); | ||
Timestamp timestamp3 = Timestamp.fromEpochMillis(millis + 60 * 1000); | ||
|
||
// create at least two files in one bucket | ||
writeCommit(GenericRow.of(1, 1, timestamp1), GenericRow.of(1, 2, timestamp2)); | ||
writeCommit(GenericRow.of(1, 3, timestamp3)); | ||
|
||
// no compaction, can be queried | ||
assertThat(query(new int[] {0, 1})) | ||
.containsExactlyInAnyOrder( | ||
GenericRow.of(1, 1), GenericRow.of(1, 2), GenericRow.of(1, 3)); | ||
Thread.sleep(2000); | ||
|
||
// compact, expired | ||
compact(1); | ||
assertThat(query(new int[] {0, 1})).containsExactlyInAnyOrder(GenericRow.of(1, 3)); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
paimon-core/src/test/java/org/apache/paimon/table/RecordLevelExpireWithTimestampLTZTest.java
This file contains 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,58 @@ | ||
/* | ||
* 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.paimon.table; | ||
|
||
import org.apache.paimon.catalog.Catalog; | ||
import org.apache.paimon.catalog.CatalogContext; | ||
import org.apache.paimon.catalog.CatalogFactory; | ||
import org.apache.paimon.catalog.Identifier; | ||
import org.apache.paimon.fs.Path; | ||
import org.apache.paimon.schema.Schema; | ||
import org.apache.paimon.types.DataTypes; | ||
import org.apache.paimon.utils.TraceableFileIO; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
|
||
import java.util.UUID; | ||
|
||
class RecordLevelExpireWithTimestampLTZTest extends RecordLevelExpireWithTimestampBaseTest { | ||
|
||
@Override | ||
@BeforeEach | ||
public void beforeEachBase() throws Exception { | ||
CatalogContext context = | ||
CatalogContext.create( | ||
new Path(TraceableFileIO.SCHEME + "://" + tempPath.toString())); | ||
Catalog catalog = CatalogFactory.createCatalog(context); | ||
Identifier identifier = new Identifier("default", "T"); | ||
catalog.createDatabase(identifier.getDatabaseName(), true); | ||
Schema schema = | ||
Schema.newBuilder() | ||
.column("pt", DataTypes.INT()) | ||
.column("pk", DataTypes.INT()) | ||
.column("col1", DataTypes.TIMESTAMP_WITH_LOCAL_TIME_ZONE()) | ||
.partitionKeys("pt") | ||
.primaryKey("pk", "pt") | ||
.options(tableOptions().toMap()) | ||
.build(); | ||
catalog.createTable(identifier, schema, true); | ||
table = (FileStoreTable) catalog.getTable(identifier); | ||
commitUser = UUID.randomUUID().toString(); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
paimon-core/src/test/java/org/apache/paimon/table/RecordLevelExpireWithTimestampTest.java
This file contains 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,58 @@ | ||
/* | ||
* 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.paimon.table; | ||
|
||
import org.apache.paimon.catalog.Catalog; | ||
import org.apache.paimon.catalog.CatalogContext; | ||
import org.apache.paimon.catalog.CatalogFactory; | ||
import org.apache.paimon.catalog.Identifier; | ||
import org.apache.paimon.fs.Path; | ||
import org.apache.paimon.schema.Schema; | ||
import org.apache.paimon.types.DataTypes; | ||
import org.apache.paimon.utils.TraceableFileIO; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
|
||
import java.util.UUID; | ||
|
||
class RecordLevelExpireWithTimestampTest extends RecordLevelExpireWithTimestampBaseTest { | ||
|
||
@Override | ||
@BeforeEach | ||
public void beforeEachBase() throws Exception { | ||
CatalogContext context = | ||
CatalogContext.create( | ||
new Path(TraceableFileIO.SCHEME + "://" + tempPath.toString())); | ||
Catalog catalog = CatalogFactory.createCatalog(context); | ||
Identifier identifier = new Identifier("default", "T"); | ||
catalog.createDatabase(identifier.getDatabaseName(), true); | ||
Schema schema = | ||
Schema.newBuilder() | ||
.column("pt", DataTypes.INT()) | ||
.column("pk", DataTypes.INT()) | ||
.column("col1", DataTypes.TIMESTAMP()) | ||
.partitionKeys("pt") | ||
.primaryKey("pk", "pt") | ||
.options(tableOptions().toMap()) | ||
.build(); | ||
catalog.createTable(identifier, schema, true); | ||
table = (FileStoreTable) catalog.getTable(identifier); | ||
commitUser = UUID.randomUUID().toString(); | ||
} | ||
} |
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.
Can we just set this to
AUTO
? If the field is timestamp, we don't need to set this option.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.
AUTO
maybe cannot adapt to all types, just added detailed log.