-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Flink: Add DynamicRecord / DynamicRecordInternal / DynamicRecordInternalSerializer #12996
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/* | ||
* 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.iceberg.flink.sink.dynamic; | ||
|
||
import java.util.List; | ||
import javax.annotation.Nullable; | ||
import org.apache.flink.table.data.RowData; | ||
import org.apache.iceberg.DistributionMode; | ||
import org.apache.iceberg.PartitionSpec; | ||
import org.apache.iceberg.Schema; | ||
import org.apache.iceberg.catalog.TableIdentifier; | ||
|
||
/** A DynamicRecord contains RowData alongside with the Iceberg table metadata. */ | ||
public class DynamicRecord { | ||
|
||
private TableIdentifier tableIdentifier; | ||
private String branch; | ||
private Schema schema; | ||
private RowData rowData; | ||
private PartitionSpec partitionSpec; | ||
private DistributionMode distributionMode; | ||
private int writeParallelism; | ||
private boolean upsertMode; | ||
@Nullable private List<String> equalityFields; | ||
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. Only this field is nullable? 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. Correct, only this field is currently nullable / optional. We could add some defaults. I was thinking to add a builder, what do you think? 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. A builder makes sense to me, as we have many parameters |
||
|
||
public DynamicRecord( | ||
TableIdentifier tableIdentifier, | ||
String branch, | ||
Schema schema, | ||
RowData rowData, | ||
PartitionSpec partitionSpec, | ||
DistributionMode distributionMode, | ||
int writeParallelism) { | ||
this.tableIdentifier = tableIdentifier; | ||
this.branch = branch; | ||
this.schema = schema; | ||
this.partitionSpec = partitionSpec; | ||
this.rowData = rowData; | ||
this.distributionMode = distributionMode; | ||
this.writeParallelism = writeParallelism; | ||
} | ||
|
||
public TableIdentifier tableIdentifier() { | ||
return tableIdentifier; | ||
} | ||
|
||
public void setTableIdentifier(TableIdentifier tableIdentifier) { | ||
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. Do we need these setters, if we have a builder? 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. We wouldn't. I'm not sure though we should remove these methods, as they allow DynamicRecord to be reused. If we add the builder, that won't be possible anymore. |
||
this.tableIdentifier = tableIdentifier; | ||
} | ||
|
||
public String branch() { | ||
return branch; | ||
} | ||
|
||
public void setBranch(String branch) { | ||
this.branch = branch; | ||
} | ||
|
||
public Schema schema() { | ||
return schema; | ||
} | ||
|
||
public void setSchema(Schema schema) { | ||
this.schema = schema; | ||
} | ||
|
||
public PartitionSpec spec() { | ||
return partitionSpec; | ||
} | ||
|
||
public void setPartitionSpec(PartitionSpec partitionSpec) { | ||
this.partitionSpec = partitionSpec; | ||
} | ||
|
||
public RowData rowData() { | ||
return rowData; | ||
} | ||
|
||
public void setRowData(RowData rowData) { | ||
this.rowData = rowData; | ||
} | ||
|
||
public DistributionMode distributionMode() { | ||
return distributionMode; | ||
} | ||
|
||
public void setDistributionMode(DistributionMode distributionMode) { | ||
this.distributionMode = distributionMode; | ||
} | ||
|
||
public int writeParallelism() { | ||
return writeParallelism; | ||
} | ||
|
||
public void writeParallelism(int parallelism) { | ||
this.writeParallelism = parallelism; | ||
} | ||
|
||
public boolean upsertMode() { | ||
return upsertMode; | ||
} | ||
|
||
public void setUpsertMode(boolean upsertMode) { | ||
this.upsertMode = upsertMode; | ||
} | ||
|
||
public List<String> equalityFields() { | ||
return equalityFields; | ||
} | ||
|
||
public void setEqualityFields(List<String> equalityFields) { | ||
this.equalityFields = equalityFields; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,166 @@ | ||||
/* | ||||
* 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.iceberg.flink.sink.dynamic; | ||||
|
||||
import java.util.List; | ||||
import java.util.Objects; | ||||
import org.apache.flink.annotation.Internal; | ||||
import org.apache.flink.table.data.RowData; | ||||
import org.apache.flink.table.runtime.typeutils.RowDataSerializer; | ||||
import org.apache.iceberg.PartitionSpec; | ||||
import org.apache.iceberg.Schema; | ||||
import org.apache.iceberg.flink.FlinkSchemaUtil; | ||||
|
||||
@Internal | ||||
class DynamicRecordInternal { | ||||
|
||||
private String tableName; | ||||
private String branch; | ||||
private Schema schema; | ||||
private PartitionSpec spec; | ||||
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. Should we rename this to partitionSpec in case some other kind of spec appears in the future? 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 was also leaning towards this name in the beginning, but it's Iceberg convention to use this name across the code base. We can rename though if this is a concern. |
||||
private int writerKey; | ||||
private RowData rowData; | ||||
private boolean upsertMode; | ||||
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. Should we rename this to 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. Can do but it's consistent with the coding style. We often omit these verbs from the getters in Iceberg. 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. in that case |
||||
private List<Integer> equalityFieldIds; | ||||
|
||||
// Required for serialization instantiation | ||||
DynamicRecordInternal() {} | ||||
mxm marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
|
||||
DynamicRecordInternal( | ||||
String tableName, | ||||
String branch, | ||||
Schema schema, | ||||
RowData rowData, | ||||
PartitionSpec spec, | ||||
int writerKey, | ||||
boolean upsertMode, | ||||
List<Integer> equalityFieldsIds) { | ||||
this.tableName = tableName; | ||||
this.branch = branch; | ||||
this.schema = schema; | ||||
this.spec = spec; | ||||
this.writerKey = writerKey; | ||||
this.rowData = rowData; | ||||
this.upsertMode = upsertMode; | ||||
this.equalityFieldIds = equalityFieldsIds; | ||||
} | ||||
|
||||
public String tableName() { | ||||
return tableName; | ||||
} | ||||
|
||||
public void setTableName(String tableName) { | ||||
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. Do we need this setters? 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. We currently use these setters here to allow for Flink's object reuse mode: Line 204 in bf02ad6
|
||||
this.tableName = tableName; | ||||
} | ||||
|
||||
public String branch() { | ||||
return branch; | ||||
} | ||||
|
||||
public void setBranch(String branch) { | ||||
this.branch = branch; | ||||
} | ||||
|
||||
public Schema schema() { | ||||
return schema; | ||||
} | ||||
|
||||
public void setSchema(Schema schema) { | ||||
this.schema = schema; | ||||
} | ||||
|
||||
public RowData rowData() { | ||||
return rowData; | ||||
} | ||||
|
||||
public void setRowData(RowData rowData) { | ||||
this.rowData = rowData; | ||||
} | ||||
|
||||
public PartitionSpec spec() { | ||||
return spec; | ||||
} | ||||
|
||||
public void setSpec(PartitionSpec spec) { | ||||
this.spec = spec; | ||||
} | ||||
|
||||
public int writerKey() { | ||||
return writerKey; | ||||
} | ||||
|
||||
public void setWriterKey(int writerKey) { | ||||
this.writerKey = writerKey; | ||||
} | ||||
|
||||
public boolean upsertMode() { | ||||
return upsertMode; | ||||
} | ||||
|
||||
public void setUpsertMode(boolean upsertMode) { | ||||
this.upsertMode = upsertMode; | ||||
} | ||||
|
||||
public List<Integer> equalityFields() { | ||||
return equalityFieldIds; | ||||
} | ||||
|
||||
public void setEqualityFieldIds(List<Integer> equalityFieldIds) { | ||||
this.equalityFieldIds = equalityFieldIds; | ||||
} | ||||
|
||||
@Override | ||||
public int hashCode() { | ||||
return Objects.hash( | ||||
tableName, branch, schema, spec, writerKey, rowData, upsertMode, equalityFieldIds); | ||||
} | ||||
|
||||
@Override | ||||
public boolean equals(Object other) { | ||||
if (this == other) { | ||||
return true; | ||||
} | ||||
|
||||
if (other == null || getClass() != other.getClass()) { | ||||
return false; | ||||
} | ||||
|
||||
DynamicRecordInternal that = (DynamicRecordInternal) other; | ||||
boolean tableFieldsMatch = | ||||
Objects.equals(tableName, that.tableName) | ||||
&& Objects.equals(branch, that.branch) | ||||
&& schema.schemaId() == that.schema.schemaId() | ||||
&& Objects.equals(spec, that.spec) | ||||
&& writerKey == that.writerKey | ||||
&& upsertMode == that.upsertMode | ||||
&& Objects.equals(equalityFieldIds, that.equalityFieldIds); | ||||
if (!tableFieldsMatch) { | ||||
return false; | ||||
} | ||||
|
||||
if (rowData.getClass().equals(that.rowData.getClass())) { | ||||
return Objects.equals(rowData, that.rowData); | ||||
} else { | ||||
RowDataSerializer rowDataSerializer = new RowDataSerializer(FlinkSchemaUtil.convert(schema)); | ||||
return rowDataSerializer | ||||
.toBinaryRow(rowData) | ||||
.equals(rowDataSerializer.toBinaryRow(that.rowData)); | ||||
} | ||||
} | ||||
} |
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.
a boolean doesn't really describe a mode , should this be an enum or
isUpsert
maybe?Uh oh!
There was an error while loading. Please reload this page.
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 think it does. If enabled, upsert mode will be used.
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.
See also #12996 (comment)