LadyForest commented on a change in pull request #18394: URL: https://github.com/apache/flink/pull/18394#discussion_r787562855
########## File path: flink-table/flink-table-common/src/test/java/org/apache/flink/table/connector/source/TestManagedTableSink.java ########## @@ -0,0 +1,468 @@ +/* + * 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.table.connector.source; + +import org.apache.flink.api.common.serialization.Encoder; +import org.apache.flink.api.connector.sink.Committer; +import org.apache.flink.api.connector.sink.GlobalCommitter; +import org.apache.flink.api.connector.sink.Sink; +import org.apache.flink.api.connector.sink.SinkWriter; +import org.apache.flink.core.fs.FSDataOutputStream; +import org.apache.flink.core.fs.FileSystem; +import org.apache.flink.core.fs.Path; +import org.apache.flink.core.io.SimpleVersionedSerializer; +import org.apache.flink.core.memory.DataInputDeserializer; +import org.apache.flink.core.memory.DataOutputSerializer; +import org.apache.flink.table.catalog.CatalogPartitionSpec; +import org.apache.flink.table.catalog.ObjectIdentifier; +import org.apache.flink.table.connector.ChangelogMode; +import org.apache.flink.table.connector.sink.DynamicTableSink; +import org.apache.flink.table.connector.sink.SinkProvider; +import org.apache.flink.table.connector.sink.abilities.SupportsOverwrite; +import org.apache.flink.table.connector.sink.abilities.SupportsPartitioning; +import org.apache.flink.table.data.GenericRowData; +import org.apache.flink.table.data.RowData; +import org.apache.flink.table.data.StringData; +import org.apache.flink.table.factories.DynamicTableFactory; +import org.apache.flink.table.factories.TestManagedTableFactory; +import org.apache.flink.table.utils.PartitionPathUtils; + +import java.io.IOException; +import java.io.OutputStream; +import java.io.Serializable; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.UUID; +import java.util.concurrent.atomic.AtomicReference; + +/** Managed {@link DynamicTableSink} for testing. */ +public class TestManagedTableSink + implements DynamicTableSink, SupportsOverwrite, SupportsPartitioning { + + private final DynamicTableFactory.Context context; + private final Path basePath; + + private LinkedHashMap<String, String> staticPartitionSpecs = new LinkedHashMap<>(); + private boolean overwrite = false; + + public TestManagedTableSink(DynamicTableFactory.Context context, Path basePath) { + this.context = context; + this.basePath = basePath; + } + + @Override + public ChangelogMode getChangelogMode(ChangelogMode requestedMode) { + return ChangelogMode.insertOnly(); + } + + @Override + public SinkRuntimeProvider getSinkRuntimeProvider(Context context) { + return SinkProvider.of(new TestManagedSink(this.context.getObjectIdentifier(), basePath)); + } + + @Override + public DynamicTableSink copy() { + TestManagedTableSink copied = new TestManagedTableSink(context, basePath); + copied.overwrite = this.overwrite; + copied.staticPartitionSpecs = this.staticPartitionSpecs; + return copied; + } + + @Override + public String asSummaryString() { + return "TestManagedSink"; + } + + @Override + public void applyOverwrite(boolean overwrite) { + this.overwrite = overwrite; + } + + @Override + public void applyStaticPartition(Map<String, String> partition) { + List<String> partitionKeys = context.getCatalogTable().getPartitionKeys(); + for (String partitionKey : partitionKeys) { + if (partition.containsKey(partitionKey)) { + staticPartitionSpecs.put(partitionKey, partition.get(partitionKey)); + } + } + } + + /** Managed {@link Sink} for testing compaction. */ + public static class TestManagedSink + implements Sink<RowData, TestManagedCommittable, Void, Void> { + + private final ObjectIdentifier tableIdentifier; + private final Path basePath; + + public TestManagedSink(ObjectIdentifier tableIdentifier, Path basePath) { + this.tableIdentifier = tableIdentifier; + this.basePath = basePath; + } + + @Override + public SinkWriter<RowData, TestManagedCommittable, Void> createWriter( + InitContext context, List<Void> states) throws IOException { + return new TestManagedSinkWriter(); + } + + @Override + public Optional<Committer<TestManagedCommittable>> createCommitter() { + return Optional.of(new TestManagedSinkCommitter(tableIdentifier, basePath)); + } + + @Override + public Optional<SimpleVersionedSerializer<TestManagedCommittable>> + getCommittableSerializer() { + return Optional.of(new TestManagedSinkCommittableSerializer()); + } + + @Override + public Optional<SimpleVersionedSerializer<Void>> getWriterStateSerializer() { + return Optional.empty(); + } + + @Override + public Optional<GlobalCommitter<TestManagedCommittable, Void>> createGlobalCommitter() { + return Optional.empty(); + } + + @Override + public Optional<SimpleVersionedSerializer<Void>> getGlobalCommittableSerializer() { + return Optional.empty(); + } + + @Override + public Collection<String> getCompatibleStateNames() { + return Collections.emptyList(); + } + } + + /** + * Committable which contains the generated compact files to be created and the old files to be + * deleted. + */ + public static class TestManagedCommittable implements Serializable { + private static final long serialVersionUID = 1L; + + private final Map<CatalogPartitionSpec, List<RowData>> toAdd; + private final Map<CatalogPartitionSpec, Set<Path>> toDelete; Review comment: > `CatalogPartitionSpec` is not `Serializable` `TestManagedSinkCommittableSerializer` takes care of `CatalogPartitionSpec` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@flink.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org