-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FLINK-35243][base] Support more schema change events & PreSchema bac…
…kfill
- Loading branch information
Showing
50 changed files
with
2,857 additions
and
246 deletions.
There are no files selected for viewing
143 changes: 143 additions & 0 deletions
143
...k-cdc-common/src/main/java/org/apache/flink/cdc/common/event/AlterColumnCommentEvent.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,143 @@ | ||
/* | ||
* 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.cdc.common.event; | ||
|
||
import org.apache.flink.cdc.common.schema.Schema; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* A {@link SchemaChangeEvent} that represents an {@code ALTER COLUMN} DDL, which may contain the | ||
* comment changes. | ||
*/ | ||
public class AlterColumnCommentEvent implements SchemaChangeEventWithPreSchema { | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
private final TableId tableId; | ||
|
||
/** key => column name, value => column type after changing. */ | ||
private final Map<String, String> commentMapping; | ||
|
||
private final Map<String, String> oldCommentMapping; | ||
|
||
public AlterColumnCommentEvent(TableId tableId, Map<String, String> commentMapping) { | ||
this.tableId = tableId; | ||
this.commentMapping = commentMapping; | ||
this.oldCommentMapping = new HashMap<>(); | ||
} | ||
|
||
public AlterColumnCommentEvent( | ||
TableId tableId, | ||
Map<String, String> commentMapping, | ||
Map<String, String> oldCommentMapping) { | ||
this.tableId = tableId; | ||
this.commentMapping = commentMapping; | ||
this.oldCommentMapping = oldCommentMapping; | ||
} | ||
|
||
/** Returns the type mapping. */ | ||
public Map<String, String> getCommentMapping() { | ||
return commentMapping; | ||
} | ||
|
||
public Map<String, String> getOldCommentMapping() { | ||
return oldCommentMapping; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (!(o instanceof AlterColumnCommentEvent)) { | ||
return false; | ||
} | ||
AlterColumnCommentEvent that = (AlterColumnCommentEvent) o; | ||
return Objects.equals(tableId, that.tableId) | ||
&& Objects.equals(commentMapping, that.commentMapping) | ||
&& Objects.equals(oldCommentMapping, that.oldCommentMapping); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(tableId, commentMapping, oldCommentMapping); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
if (hasPreSchema()) { | ||
return "AlterColumnCommentEvent{" | ||
+ "tableId=" | ||
+ tableId | ||
+ ", commentMapping=" | ||
+ commentMapping | ||
+ ", oldCommentMapping=" | ||
+ oldCommentMapping | ||
+ '}'; | ||
} else { | ||
return "AlterColumnCommentEvent{" | ||
+ "tableId=" | ||
+ tableId | ||
+ ", commentMapping=" | ||
+ commentMapping | ||
+ '}'; | ||
} | ||
} | ||
|
||
@Override | ||
public TableId tableId() { | ||
return tableId; | ||
} | ||
|
||
@Override | ||
public boolean hasPreSchema() { | ||
return !oldCommentMapping.isEmpty(); | ||
} | ||
|
||
@Override | ||
public void fillPreSchema(Schema oldTypeSchema) { | ||
oldCommentMapping.clear(); | ||
oldTypeSchema.getColumns().stream() | ||
.filter(e -> commentMapping.containsKey(e.getName())) | ||
.forEach(e -> oldCommentMapping.put(e.getName(), e.getComment())); | ||
} | ||
|
||
@Override | ||
public boolean trimRedundantChanges() { | ||
if (hasPreSchema()) { | ||
Set<String> redundantlyChangedColumns = | ||
commentMapping.keySet().stream() | ||
.filter( | ||
e -> | ||
Objects.equals( | ||
commentMapping.get(e), | ||
oldCommentMapping.get(e))) | ||
.collect(Collectors.toSet()); | ||
|
||
// Remove redundant alter column type records that doesn't really change the type | ||
commentMapping.keySet().removeAll(redundantlyChangedColumns); | ||
oldCommentMapping.keySet().removeAll(redundantlyChangedColumns); | ||
} | ||
return !commentMapping.isEmpty(); | ||
} | ||
} |
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
72 changes: 72 additions & 0 deletions
72
flink-cdc-common/src/main/java/org/apache/flink/cdc/common/event/AlterTableCommentEvent.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,72 @@ | ||
/* | ||
* 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.cdc.common.event; | ||
|
||
import java.util.Objects; | ||
|
||
/** A {@link SchemaChangeEvent} that represents an {@code ALTER TABLE COMMENT = ...} DDL. */ | ||
public class AlterTableCommentEvent implements SchemaChangeEvent { | ||
private static final long serialVersionUID = 1L; | ||
|
||
private final TableId tableId; | ||
|
||
/** key => column name, value => column type after changing. */ | ||
private final String tableComment; | ||
|
||
public AlterTableCommentEvent(TableId tableId, String tableComment) { | ||
this.tableId = tableId; | ||
this.tableComment = tableComment; | ||
} | ||
|
||
public String getTableComment() { | ||
return tableComment; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (!(o instanceof AlterTableCommentEvent)) { | ||
return false; | ||
} | ||
AlterTableCommentEvent that = (AlterTableCommentEvent) o; | ||
return Objects.equals(tableId, that.tableId) | ||
&& Objects.equals(tableComment, that.tableComment); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(tableId, tableComment); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "AlterTableCommentEvent{" | ||
+ "tableId=" | ||
+ tableId | ||
+ ", tableComment=" | ||
+ tableComment | ||
+ '}'; | ||
} | ||
|
||
@Override | ||
public TableId tableId() { | ||
return tableId; | ||
} | ||
} |
Oops, something went wrong.