Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
import org.apache.flink.cdc.common.types.ZonedTimestampType;
import org.apache.flink.cdc.common.types.variant.Variant;

import org.apache.flink.shaded.guava31.com.google.common.collect.ArrayListMultimap;
import org.apache.flink.shaded.guava31.com.google.common.collect.ImmutableList;
import org.apache.flink.shaded.guava31.com.google.common.collect.Streams;
import org.apache.flink.shaded.guava31.com.google.common.io.BaseEncoding;
Expand Down Expand Up @@ -320,139 +319,6 @@ public static Object[] coerceRow(
return coercedRow;
}

/**
* Try to merge given {@link Schema}s and ensure they're identical. The only difference allowed
* is nullability, string and varchar precision, default value, and comments.
*/
public static Schema strictlyMergeSchemas(List<Schema> schemas) {
Preconditions.checkArgument(
!schemas.isEmpty(), "Trying to merge transformed schemas %s, but got empty list");
if (schemas.size() == 1) {
return schemas.get(0);
}

List<List<String>> primaryKeys =
schemas.stream()
.map(Schema::primaryKeys)
.filter(p -> !p.isEmpty())
.distinct()
.collect(Collectors.toList());
List<List<String>> partitionKeys =
schemas.stream()
.map(Schema::partitionKeys)
.filter(p -> !p.isEmpty())
.distinct()
.collect(Collectors.toList());
List<Map<String, String>> options =
schemas.stream()
.map(Schema::options)
.filter(p -> !p.isEmpty())
.distinct()
.collect(Collectors.toList());
List<List<String>> columnNames =
schemas.stream()
.map(Schema::getColumnNames)
.distinct()
.collect(Collectors.toList());

Preconditions.checkArgument(
primaryKeys.size() <= 1,
"Trying to merge transformed schemas %s, but got more than one primary key configurations: %s",
schemas,
primaryKeys);
Preconditions.checkArgument(
partitionKeys.size() <= 1,
"Trying to merge transformed schemas %s, but got more than one partition key configurations: %s",
schemas,
partitionKeys);
Preconditions.checkArgument(
options.size() <= 1,
"Trying to merge transformed schemas %s, but got more than one option configurations: %s",
schemas,
options);
Preconditions.checkArgument(
columnNames.size() == 1,
"Trying to merge transformed schemas %s, but got more than one column name views: %s",
schemas,
columnNames);

int arity = columnNames.get(0).size();

ArrayListMultimap<Integer, DataType> toBeMergedColumnTypes =
ArrayListMultimap.create(arity, 1);
for (Schema schema : schemas) {
List<DataType> columnTypes = schema.getColumnDataTypes();
for (int colIndex = 0; colIndex < columnTypes.size(); colIndex++) {
toBeMergedColumnTypes.put(colIndex, columnTypes.get(colIndex));
}
}

List<String> mergedColumnNames = columnNames.iterator().next();
List<DataType> mergedColumnTypes = new ArrayList<>(arity);
for (int i = 0; i < arity; i++) {
mergedColumnTypes.add(strictlyMergeDataTypes(toBeMergedColumnTypes.get(i)));
}

List<Column> mergedColumns = new ArrayList<>();
for (int i = 0; i < mergedColumnNames.size(); i++) {
mergedColumns.add(
Column.physicalColumn(mergedColumnNames.get(i), mergedColumnTypes.get(i)));
}

return Schema.newBuilder()
.primaryKey(primaryKeys.isEmpty() ? Collections.emptyList() : primaryKeys.get(0))
.partitionKey(
partitionKeys.isEmpty() ? Collections.emptyList() : partitionKeys.get(0))
.options(options.isEmpty() ? Collections.emptyMap() : options.get(0))
.setColumns(mergedColumns)
.build();
}

private static DataType strictlyMergeDataTypes(List<DataType> dataTypes) {
Preconditions.checkArgument(
!dataTypes.isEmpty(),
"Trying to merge transformed data types %s, but got empty list");

List<DataType> simpleMergeTypes =
dataTypes.stream().distinct().collect(Collectors.toList());
if (simpleMergeTypes.size() == 1) {
return simpleMergeTypes.get(0);
}

List<DataTypeRoot> typeRoots =
dataTypes.stream()
.map(DataType::getTypeRoot)
.distinct()
.collect(Collectors.toList());
Preconditions.checkArgument(
typeRoots.size() == 1,
"Trying to merge types %s, but got more than one type root: %s",
dataTypes,
typeRoots);

// Decay types to the most
DataType type = dataTypes.get(0);

if (type.is(DataTypeRoot.CHAR)) {
return DataTypes.CHAR(CharType.MAX_LENGTH);
} else if (type.is(DataTypeRoot.VARCHAR)) {
return DataTypes.STRING();
} else if (type.is(DataTypeRoot.BINARY)) {
return DataTypes.BINARY(BinaryType.MAX_LENGTH);
} else if (type.is(DataTypeRoot.VARBINARY)) {
return DataTypes.VARBINARY(VarBinaryType.MAX_LENGTH);
} else if (type.is(DataTypeRoot.TIMESTAMP_WITHOUT_TIME_ZONE)) {
return DataTypes.TIMESTAMP(TimestampType.MAX_PRECISION);
} else if (type.is(DataTypeRoot.TIMESTAMP_WITH_TIME_ZONE)) {
return DataTypes.TIMESTAMP_TZ(ZonedTimestampType.MAX_PRECISION);
} else if (type.is(DataTypeRoot.TIMESTAMP_WITH_LOCAL_TIME_ZONE)) {
return DataTypes.TIMESTAMP_LTZ(LocalZonedTimestampType.MAX_PRECISION);
} else {
throw new IllegalArgumentException(
"Unable to merge data types with different precision: " + dataTypes);
}
}

@VisibleForTesting
static boolean isDataTypeCompatible(@Nullable DataType currentType, DataType upcomingType) {
// If two types are identical, they're compatible of course.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -613,73 +613,6 @@ void testOpTypeMetadataColumnInBatchMode(ValuesDataSink.SinkApi sinkApi) throws
"DataChangeEvent{tableId=default_namespace.default_schema.table1, before=[], after=[2, 2, 20, +I, 2], op=INSERT, meta=({op_ts=2})}");
}

@ParameterizedTest
@EnumSource
void testTransformTwiceInBatchMode(ValuesDataSink.SinkApi sinkApi) throws Exception {
FlinkPipelineComposer composer = FlinkPipelineComposer.ofMiniCluster();

// Setup value source
Configuration sourceConfig = new Configuration();
sourceConfig.set(
ValuesDataSourceOptions.EVENT_SET_ID,
ValuesDataSourceHelper.EventSetId.TRANSFORM_BATCH_TABLE);
SourceDef sourceDef =
new SourceDef(ValuesDataFactory.IDENTIFIER, "Value Source", sourceConfig);

// Setup value sink
Configuration sinkConfig = new Configuration();
sinkConfig.set(ValuesDataSinkOptions.MATERIALIZED_IN_MEMORY, true);
sinkConfig.set(ValuesDataSinkOptions.SINK_API, sinkApi);
SinkDef sinkDef = new SinkDef(ValuesDataFactory.IDENTIFIER, "Value Sink", sinkConfig);

// Setup transform
TransformDef transformDef1 =
new TransformDef(
"default_namespace.default_schema.table1",
"*,concat(col1,'1') as col12",
"col1 = '1' OR col1 = '999'",
"col1",
"col12",
"key1=value1",
"",
null);
TransformDef transformDef2 =
new TransformDef(
"default_namespace.default_schema.table1",
"*,concat(col1,'2') as col12",
"col1 = '2'",
null,
null,
null,
"",
null);
// Setup pipeline
Configuration pipelineConfig = new Configuration();
pipelineConfig.set(PipelineOptions.PIPELINE_PARALLELISM, 1);
pipelineConfig.set(
PipelineOptions.PIPELINE_EXECUTION_RUNTIME_MODE, RuntimeExecutionMode.BATCH);
PipelineDef pipelineDef =
new PipelineDef(
sourceDef,
sinkDef,
Collections.emptyList(),
new ArrayList<>(Arrays.asList(transformDef1, transformDef2)),
Collections.emptyList(),
pipelineConfig);

// Execute the pipeline
PipelineExecution execution = composer.compose(pipelineDef);
execution.execute();

// Check the order and content of all received events
String[] outputEvents = outCaptor.toString().trim().split("\n");
assertThat(outputEvents)
.containsExactly(
"CreateTableEvent{tableId=default_namespace.default_schema.table1, schema=columns={`col1` STRING NOT NULL,`col2` STRING,`col12` STRING}, primaryKeys=col1, partitionKeys=col12, options=({key1=value1})}",
"DataChangeEvent{tableId=default_namespace.default_schema.table1, before=[], after=[1, 1, 11], op=INSERT, meta=({op_ts=1})}",
"DataChangeEvent{tableId=default_namespace.default_schema.table1, before=[], after=[2, 2, 22], op=INSERT, meta=({op_ts=2})}");
}

@ParameterizedTest
@EnumSource
void testOneToOneRoutingInBatchMode(ValuesDataSink.SinkApi sinkApi) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,78 +423,6 @@ void testOpTypeMetadataColumn(ValuesDataSink.SinkApi sinkApi) throws Exception {
"DataChangeEvent{tableId=default_namespace.default_schema.table1, before=[2, , 20, -U, 5], after=[2, x, 20, +U, 5], op=UPDATE, meta=({op_ts=5})}");
}

@ParameterizedTest
@EnumSource
void testTransformTwice(ValuesDataSink.SinkApi sinkApi) throws Exception {
FlinkPipelineComposer composer = FlinkPipelineComposer.ofMiniCluster();

// Setup value source
Configuration sourceConfig = new Configuration();
sourceConfig.set(
ValuesDataSourceOptions.EVENT_SET_ID,
ValuesDataSourceHelper.EventSetId.TRANSFORM_TABLE);
SourceDef sourceDef =
new SourceDef(ValuesDataFactory.IDENTIFIER, "Value Source", sourceConfig);

// Setup value sink
Configuration sinkConfig = new Configuration();
sinkConfig.set(ValuesDataSinkOptions.MATERIALIZED_IN_MEMORY, true);
sinkConfig.set(ValuesDataSinkOptions.SINK_API, sinkApi);
SinkDef sinkDef = new SinkDef(ValuesDataFactory.IDENTIFIER, "Value Sink", sinkConfig);

// Setup transform
TransformDef transformDef1 =
new TransformDef(
"default_namespace.default_schema.table1",
"*,concat(col1,'1') as col12",
"col1 = '1' OR col1 = '999'",
"col1",
"col12",
"key1=value1",
"",
null);
TransformDef transformDef2 =
new TransformDef(
"default_namespace.default_schema.table1",
"*,concat(col1,'2') as col12",
"col1 = '2'",
null,
null,
null,
"",
null);
// Setup pipeline
Configuration pipelineConfig = new Configuration();
pipelineConfig.set(PipelineOptions.PIPELINE_PARALLELISM, 1);
pipelineConfig.set(
PipelineOptions.PIPELINE_SCHEMA_CHANGE_BEHAVIOR, SchemaChangeBehavior.EVOLVE);
PipelineDef pipelineDef =
new PipelineDef(
sourceDef,
sinkDef,
Collections.emptyList(),
new ArrayList<>(Arrays.asList(transformDef1, transformDef2)),
Collections.emptyList(),
pipelineConfig);

// Execute the pipeline
PipelineExecution execution = composer.compose(pipelineDef);
execution.execute();

// Check the order and content of all received events
String[] outputEvents = outCaptor.toString().trim().split("\n");
assertThat(outputEvents)
.containsExactly(
"CreateTableEvent{tableId=default_namespace.default_schema.table1, schema=columns={`col1` STRING NOT NULL,`col2` STRING,`col12` STRING}, primaryKeys=col1, partitionKeys=col12, options=({key1=value1})}",
"DataChangeEvent{tableId=default_namespace.default_schema.table1, before=[], after=[1, 1, 11], op=INSERT, meta=({op_ts=1})}",
"DataChangeEvent{tableId=default_namespace.default_schema.table1, before=[], after=[2, 2, 22], op=INSERT, meta=({op_ts=2})}",
"AddColumnEvent{tableId=default_namespace.default_schema.table1, addedColumns=[ColumnWithPosition{column=`col3` STRING, position=AFTER, existedColumnName=col2}]}",
"RenameColumnEvent{tableId=default_namespace.default_schema.table1, nameMapping={col2=newCol2, col3=newCol3}}",
"DropColumnEvent{tableId=default_namespace.default_schema.table1, droppedColumnNames=[newCol2]}",
"DataChangeEvent{tableId=default_namespace.default_schema.table1, before=[1, 1, 11], after=[], op=DELETE, meta=({op_ts=4})}",
"DataChangeEvent{tableId=default_namespace.default_schema.table1, before=[2, , 22], after=[2, x, 22], op=UPDATE, meta=({op_ts=5})}");
}

@ParameterizedTest
@EnumSource
void testOneToOneRouting(ValuesDataSink.SinkApi sinkApi) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -465,76 +465,6 @@ void testOpTypeMetadataColumn(ValuesDataSink.SinkApi sinkApi) throws Exception {
"DataChangeEvent{tableId=default_namespace.default_schema.table1, before=[2, null, 20, -U, null, null, ], after=[2, null, 20, +U, null, null, x], op=UPDATE, meta=({op_ts=5})}");
}

@ParameterizedTest
@EnumSource
void testTransformTwice(ValuesDataSink.SinkApi sinkApi) throws Exception {
FlinkPipelineComposer composer = FlinkPipelineComposer.ofMiniCluster();

// Setup value source
Configuration sourceConfig = new Configuration();
sourceConfig.set(
ValuesDataSourceOptions.EVENT_SET_ID,
ValuesDataSourceHelper.EventSetId.TRANSFORM_TABLE);
SourceDef sourceDef =
new SourceDef(ValuesDataFactory.IDENTIFIER, "Value Source", sourceConfig);

// Setup value sink
Configuration sinkConfig = new Configuration();
sinkConfig.set(ValuesDataSinkOptions.MATERIALIZED_IN_MEMORY, true);
sinkConfig.set(ValuesDataSinkOptions.SINK_API, sinkApi);
SinkDef sinkDef = new SinkDef(ValuesDataFactory.IDENTIFIER, "Value Sink", sinkConfig);

// Setup transform
TransformDef transformDef1 =
new TransformDef(
"default_namespace.default_schema.table1",
"*,concat(col1,'1') as col12",
"col1 = '1' OR col1 = '999'",
"col1",
"col12",
"key1=value1",
"",
null);
TransformDef transformDef2 =
new TransformDef(
"default_namespace.default_schema.table1",
"*,concat(col1,'2') as col12",
"col1 = '2'",
null,
null,
null,
"",
null);
// Setup pipeline
Configuration pipelineConfig = new Configuration();
pipelineConfig.set(PipelineOptions.PIPELINE_PARALLELISM, 1);

PipelineDef pipelineDef =
new PipelineDef(
sourceDef,
sinkDef,
Collections.emptyList(),
new ArrayList<>(Arrays.asList(transformDef1, transformDef2)),
Collections.emptyList(),
pipelineConfig);

// Execute the pipeline
PipelineExecution execution = composer.compose(pipelineDef);
execution.execute();

// Check the order and content of all received events
String[] outputEvents = outCaptor.toString().trim().split(LINE_SEPARATOR);
assertThat(outputEvents)
.containsExactly(
"CreateTableEvent{tableId=default_namespace.default_schema.table1, schema=columns={`col1` STRING NOT NULL,`col2` STRING,`col12` STRING}, primaryKeys=col1, partitionKeys=col12, options=({key1=value1})}",
"DataChangeEvent{tableId=default_namespace.default_schema.table1, before=[], after=[1, 1, 11], op=INSERT, meta=({op_ts=1})}",
"DataChangeEvent{tableId=default_namespace.default_schema.table1, before=[], after=[2, 2, 22], op=INSERT, meta=({op_ts=2})}",
"AddColumnEvent{tableId=default_namespace.default_schema.table1, addedColumns=[ColumnWithPosition{column=`col3` STRING, position=LAST, existedColumnName=null}]}",
"AddColumnEvent{tableId=default_namespace.default_schema.table1, addedColumns=[ColumnWithPosition{column=`newCol2` STRING, position=LAST, existedColumnName=null}, ColumnWithPosition{column=`newCol3` STRING, position=LAST, existedColumnName=null}]}",
"DataChangeEvent{tableId=default_namespace.default_schema.table1, before=[1, null, 11, null, null, 1], after=[], op=DELETE, meta=({op_ts=4})}",
"DataChangeEvent{tableId=default_namespace.default_schema.table1, before=[2, null, 22, null, null, ], after=[2, null, 22, null, null, x], op=UPDATE, meta=({op_ts=5})}");
}

@Test
void testOneToOneRouting() throws Exception {
FlinkPipelineComposer composer = FlinkPipelineComposer.ofMiniCluster();
Expand Down
Loading
Loading