hanyuzheng7 commented on code in PR #29624: URL: https://github.com/apache/flink/pull/29624#discussion_r4390975591
########## flink-table/flink-table-runtime/src/test/java/org/apache/flink/table/runtime/operators/values/ValuesSourceTest.java: ########## @@ -0,0 +1,167 @@ +/* + * 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.runtime.operators.values; + +import org.apache.flink.api.connector.source.ReaderInfo; +import org.apache.flink.api.connector.source.SourceReader; +import org.apache.flink.api.connector.source.SplitEnumerator; +import org.apache.flink.api.connector.source.lib.NumberSequenceSource.NumberSequenceSplit; +import org.apache.flink.api.connector.source.mocks.MockSplitEnumeratorContext; +import org.apache.flink.configuration.Configuration; +import org.apache.flink.connector.testutils.source.reader.TestingReaderContext; +import org.apache.flink.connector.testutils.source.reader.TestingReaderOutput; +import org.apache.flink.core.io.InputStatus; +import org.apache.flink.core.io.SimpleVersionedSerializer; +import org.apache.flink.table.data.RowData; +import org.apache.flink.table.runtime.generated.GeneratedInput; +import org.apache.flink.table.runtime.typeutils.InternalTypeInfo; +import org.apache.flink.table.types.logical.RowType; +import org.apache.flink.table.types.logical.VarCharType; + +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.stream.Collectors; + +import static org.assertj.core.api.Assertions.assertThat; + +/** Tests for {@link ValuesSource}. */ +class ValuesSourceTest { + + private static final int NUM_ROWS = 5; + + private static final String FORMAT_CLASS = "TestValuesInputFormat"; + + /** Like the generated VALUES code, this format returns the same row object for every row. */ + private static final String FORMAT_CODE = + String.join( + "\n", + "public class " + FORMAT_CLASS, + " extends org.apache.flink.api.common.io.GenericInputFormat {", + " private int nextRow = 0;", + " private final org.apache.flink.table.data.GenericRowData out =", + " new org.apache.flink.table.data.GenericRowData(1);", + " public " + FORMAT_CLASS + "(Object[] references) {}", + " public boolean reachedEnd() {", + " return nextRow >= " + NUM_ROWS + ";", + " }", + " public Object nextRecord(Object reuse) {", + " out.setField(0, org.apache.flink.table.data.binary.BinaryStringData", + " .fromString(\"row-\" + nextRow++));", + " return out;", + " }", + "}"); + + /** Rows that are not copied would all be the last row, because the format reuses its row. */ + @Test + void testEmitsEachRow() throws Exception { + ValuesSource source = source(); + assertThat(read(source, assignedSplits(source), Integer.MAX_VALUE).rows) + .containsExactly("row-0", "row-1", "row-2", "row-3", "row-4"); + } + + @Test + void testRestoreEmitsOnlyTheRemainingRows() throws Exception { + ValuesSource source = source(); + Read first = read(source, assignedSplits(source), 2); + assertThat(first.rows).containsExactly("row-0", "row-1"); + + Read second = read(source, roundTrip(source, first.snapshot), Integer.MAX_VALUE); + assertThat(second.rows).containsExactly("row-2", "row-3", "row-4"); + assertThat(second.snapshot).isEmpty(); + } + + private static ValuesSource source() { + return new ValuesSource( + new ValuesInputFormat( + new GeneratedInput<>( + FORMAT_CLASS, FORMAT_CODE, new Object[0], new Configuration()), + InternalTypeInfo.of(RowType.of(VarCharType.STRING_TYPE))), + NUM_ROWS); + } + + /** The splits that the enumerator assigns to the single reader. */ + private static List<NumberSequenceSplit> assignedSplits(ValuesSource source) throws Exception { + try (MockSplitEnumeratorContext<NumberSequenceSplit> context = + new MockSplitEnumeratorContext<>(1); + SplitEnumerator<NumberSequenceSplit, Collection<NumberSequenceSplit>> enumerator = + source.createEnumerator(context)) { + enumerator.start(); + context.registerReader(new ReaderInfo(0, "localhost")); + enumerator.addReader(0); + enumerator.handleSplitRequest(0, "localhost"); + enumerator.handleSplitRequest(0, "localhost"); + assertThat(context.hasNoMoreSplits(0)).isTrue(); + return context.getSplitsAssignmentSequence().stream() + .flatMap(assignment -> assignment.assignment().get(0).stream()) + .collect(Collectors.toList()); + } + } + + /** The reader state as it comes back from a checkpoint. */ + private static List<NumberSequenceSplit> roundTrip( Review Comment: Thanks. I'll rename it. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
