xuyangzhong commented on a change in pull request #17537: URL: https://github.com/apache/flink/pull/17537#discussion_r735241016
########## File path: flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/factories/source/TestValuesSource.java ########## @@ -0,0 +1,393 @@ +/* + * 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.planner.factories.source; + +import org.apache.flink.api.common.typeutils.TypeSerializer; +import org.apache.flink.api.connector.source.Boundedness; +import org.apache.flink.api.connector.source.Source; +import org.apache.flink.api.connector.source.SourceReader; +import org.apache.flink.api.connector.source.SourceReaderContext; +import org.apache.flink.api.connector.source.SplitEnumerator; +import org.apache.flink.api.connector.source.SplitEnumeratorContext; +import org.apache.flink.api.connector.source.lib.util.IteratorSourceEnumerator; +import org.apache.flink.api.connector.source.lib.util.IteratorSourceReader; +import org.apache.flink.api.connector.source.lib.util.IteratorSourceSplit; +import org.apache.flink.core.io.SimpleVersionedSerializer; +import org.apache.flink.core.memory.DataInputView; +import org.apache.flink.core.memory.DataInputViewStreamWrapper; +import org.apache.flink.core.memory.DataOutputViewStreamWrapper; +import org.apache.flink.table.data.RowData; +import org.apache.flink.table.planner.factories.TestValuesTableFactory; +import org.apache.flink.util.Preconditions; +import org.apache.flink.util.SplittableIterator; + +import org.apache.commons.collections.IteratorUtils; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; + +/** A data source implementation for {@link TestValuesTableFactory}. */ +public class TestValuesSource + implements Source< + RowData, + TestValuesSource.TestValuesSplit, + Collection<TestValuesSource.TestValuesSplit>> { + + private final boolean isBounded = true; + + private transient Iterable<RowData> elements; + + private int elementNums; + + private byte[] elementsSerialized; + + private TypeSerializer<RowData> serializer; + + public TestValuesSource( + TypeSerializer<RowData> serializer, Iterable<RowData> elements, int elementNums) + throws IOException { + this.serializer = serializer; + this.elements = elements; + this.elementNums = elementNums; + serializeElements(); + } + + public TestValuesSource(Iterable<RowData> elements) throws IOException { + this.elements = elements; + serializeElements(); + } + + private void serializeElements() throws IOException { + Preconditions.checkState(serializer != null, "serializer is not set"); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + DataOutputViewStreamWrapper wrapper = new DataOutputViewStreamWrapper(baos); + try { + for (RowData element : elements) { + serializer.serialize(element, wrapper); + } + } catch (IOException e) { + throw new IOException("Serializing the source elements failed: " + e.getMessage(), e); + } + this.elementsSerialized = baos.toByteArray(); + } + + private void deserializeElements() throws IOException { + Preconditions.checkState(serializer != null, "serializer is not set"); + Preconditions.checkState( + elementsSerialized != null && elementsSerialized.length != 0, + "elementsSerialized doesn't exist"); + ByteArrayInputStream bais = new ByteArrayInputStream(elementsSerialized); + final DataInputView input = new DataInputViewStreamWrapper(bais); + + List<RowData> elements = new ArrayList<>(); + + int index = 0; + while (index < elementNums) { + try { + RowData element = serializer.deserialize(input); + elements.add(element); + index++; + } catch (IOException e) { + throw new IOException( + "Deserializing the source elements failed: " + e.getMessage(), e); + } + } + this.elements = elements; + } + + @Override + public Boundedness getBoundedness() { + return isBounded ? Boundedness.CONTINUOUS_UNBOUNDED : Boundedness.BOUNDED; + } + + @Override + public SourceReader<RowData, TestValuesSplit> createReader(SourceReaderContext readerContext) { + return new IteratorSourceReader<>(readerContext); + } + + @Override + public SplitEnumerator<TestValuesSplit, Collection<TestValuesSplit>> createEnumerator( + SplitEnumeratorContext<TestValuesSplit> enumContext) throws IOException { + final int currentParallelism = enumContext.currentParallelism(); + + if (elements == null) { Review comment: Because the elements is transient. If the elements is null, it represents that the elements have not been deserialized, and should be deserialized first. If the elements is not null, I think it's not necessary to do the same work. -- 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