pnowojski commented on code in PR #29624:
URL: https://github.com/apache/flink/pull/29624#discussion_r4388641582
##########
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/common/CommonExecValues.java:
##########
@@ -69,16 +71,36 @@ protected Transformation<RowData> translateToPlanInternal(
(RowType) getOutputType(),
tuples,
getClass().getSimpleName());
- final Transformation<RowData> transformation =
- planner.getExecEnv()
- .createInput(inputFormat,
inputFormat.getProducedType())
- .getTransformation();
+ final Transformation<RowData> transformation;
+ // Without rows, VALUES keeps the legacy source: a DataGeneratorSource
emits at least one.
+ if (legacySourceEnabled() || tuples.isEmpty()) {
Review Comment:
why `tuples.isEmpty()`? Why can't the new code path handle that?
##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/runtime/stream/sql/ValuesStreamingRestoreITCase.java:
##########
@@ -0,0 +1,201 @@
+/*
+ * 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.runtime.stream.sql;
+
+import org.apache.flink.client.program.rest.RestClusterClient;
+import org.apache.flink.configuration.CheckpointingOptions;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.configuration.CoreOptions;
+import org.apache.flink.configuration.ExternalizedCheckpointRetention;
+import org.apache.flink.configuration.RestartStrategyOptions;
+import org.apache.flink.configuration.StateRecoveryOptions;
+import org.apache.flink.core.execution.CheckpointType;
+import org.apache.flink.core.execution.JobClient;
+import org.apache.flink.runtime.execution.ExecutionState;
+import org.apache.flink.runtime.minicluster.MiniCluster;
+import org.apache.flink.runtime.rest.messages.job.JobDetailsInfo;
+import org.apache.flink.runtime.testutils.MiniClusterResourceConfiguration;
+import org.apache.flink.table.api.EnvironmentSettings;
+import org.apache.flink.table.api.TableEnvironment;
+import org.apache.flink.table.planner.factories.TestValuesTableFactory;
+import org.apache.flink.test.junit5.InjectClusterClient;
+import org.apache.flink.test.junit5.InjectMiniCluster;
+import org.apache.flink.test.junit5.MiniClusterExtension;
+
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+import org.junit.jupiter.api.io.TempDir;
+
+import java.nio.file.Path;
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import static
org.apache.flink.runtime.testutils.CommonTestUtils.waitForAllTaskRunning;
+import static
org.apache.flink.runtime.testutils.CommonTestUtils.waitUntilCondition;
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * VALUES waits for one more checkpoint after emitting its rows before it
exits (FLIP-147). That
+ * checkpoint is triggered while the task is still running, so it doesn't mark
VALUES as finished. A
+ * job restored from it must not read the VALUES rows again.
+ */
+class ValuesStreamingRestoreITCase {
Review Comment:
Are those two tests failing without the fix?
##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/runtime/stream/sql/ValuesLegacyRestoreITCase.java:
##########
@@ -0,0 +1,265 @@
+/*
+ * 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.runtime.stream.sql;
+
+import org.apache.flink.api.common.JobStatus;
+import org.apache.flink.api.common.eventtime.WatermarkStrategy;
+import org.apache.flink.api.common.typeinfo.Types;
+import org.apache.flink.api.connector.sink2.Sink;
+import org.apache.flink.api.connector.sink2.SinkWriter;
+import org.apache.flink.api.connector.sink2.WriterInitContext;
+import
org.apache.flink.api.connector.source.util.ratelimit.RateLimiterStrategy;
+import org.apache.flink.client.program.rest.RestClusterClient;
+import org.apache.flink.configuration.CheckpointingOptions;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.configuration.CoreOptions;
+import org.apache.flink.configuration.ExternalizedCheckpointRetention;
+import org.apache.flink.configuration.RestartStrategyOptions;
+import org.apache.flink.configuration.StateRecoveryOptions;
+import org.apache.flink.connector.datagen.source.DataGeneratorSource;
+import org.apache.flink.core.execution.CheckpointType;
+import org.apache.flink.core.execution.JobClient;
+import org.apache.flink.runtime.execution.ExecutionState;
+import org.apache.flink.runtime.minicluster.MiniCluster;
+import org.apache.flink.runtime.rest.messages.job.JobDetailsInfo;
+import org.apache.flink.runtime.testutils.MiniClusterResourceConfiguration;
+import org.apache.flink.streaming.api.datastream.DataStream;
+import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
+import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.runtime.generated.GeneratedInput;
+import org.apache.flink.table.runtime.operators.values.ValuesInputFormat;
+import org.apache.flink.table.runtime.operators.values.ValuesSource;
+import org.apache.flink.table.runtime.typeutils.InternalTypeInfo;
+import org.apache.flink.table.types.logical.IntType;
+import org.apache.flink.table.types.logical.RowType;
+import org.apache.flink.test.junit5.InjectClusterClient;
+import org.apache.flink.test.junit5.InjectMiniCluster;
+import org.apache.flink.test.junit5.MiniClusterExtension;
+
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+import org.junit.jupiter.api.io.TempDir;
+
+import java.nio.file.Path;
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Queue;
+import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.stream.Collectors;
+
+import static
org.apache.flink.runtime.testutils.CommonTestUtils.waitForAllTaskRunning;
+import static
org.apache.flink.runtime.testutils.CommonTestUtils.waitUntilCondition;
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * Restores checkpoints of the legacy VALUES source, an {@code
InputFormatSourceFunction}, into a
+ * job with a {@link ValuesSource}. Neither job sets a UID, like a SQL job
without a compiled plan.
+ */
Review Comment:
I'm not sure what's the value of this ITCase. We are testing for a the buggy
behaviour? I think I would prefer to drop this test.
##########
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/common/CommonExecValues.java:
##########
@@ -69,16 +71,36 @@ protected Transformation<RowData> translateToPlanInternal(
(RowType) getOutputType(),
tuples,
getClass().getSimpleName());
- final Transformation<RowData> transformation =
- planner.getExecEnv()
- .createInput(inputFormat,
inputFormat.getProducedType())
- .getTransformation();
+ final Transformation<RowData> transformation;
+ // Without rows, VALUES keeps the legacy source: a DataGeneratorSource
emits at least one.
+ if (legacySourceEnabled() || tuples.isEmpty()) {
+ transformation =
+ planner.getExecEnv()
+ .createInput(inputFormat,
inputFormat.getProducedType())
+ .getTransformation();
+ } else {
+ transformation =
+ planner.getExecEnv()
+ .fromSource(
+ new ValuesSource(inputFormat,
tuples.size()),
+ WatermarkStrategy.noWatermarks(),
+ VALUES_TRANSFORMATION)
+ .getTransformation();
+ }
Review Comment:
Old legacy source was not parallel. This one is, I wonder if this can be a
breaking change or not?
##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/operators/values/ValuesGeneratorFunction.java:
##########
@@ -0,0 +1,70 @@
+/*
+ * 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.annotation.Internal;
+import org.apache.flink.api.common.io.GenericInputFormat;
+import org.apache.flink.api.connector.source.SourceReaderContext;
+import org.apache.flink.connector.datagen.source.GeneratorFunction;
+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.runtime.typeutils.RowDataSerializer;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Returns the row of a {@code VALUES} clause for a row number. The generated
format only reads
+ * forward, so {@link #open} reads all rows once.
+ */
+@Internal
+public class ValuesGeneratorFunction implements GeneratorFunction<Long,
RowData> {
+
+ private static final long serialVersionUID = 1L;
+
+ private final GeneratedInput<GenericInputFormat<RowData>> generatedInput;
+ private final InternalTypeInfo<RowData> producedType;
+
+ private transient List<RowData> rows;
+
+ public ValuesGeneratorFunction(
+ GeneratedInput<GenericInputFormat<RowData>> generatedInput,
+ InternalTypeInfo<RowData> producedType) {
+ this.generatedInput = generatedInput;
+ this.producedType = producedType;
+ }
+
+ @Override
+ public void open(SourceReaderContext readerContext) throws Exception {
+ final GenericInputFormat<RowData> format =
+
generatedInput.newInstance(readerContext.getUserCodeClassLoader().asClassLoader());
+ // The generated format returns the same row object each time.
+ final RowDataSerializer serializer = producedType.toRowSerializer();
+ rows = new ArrayList<>();
+ while (!format.reachedEnd()) {
+ rows.add(serializer.copy(format.nextRecord(null)));
+ }
Review Comment:
It's a bit strange that we are re-using `InputFormat` abstraction here. If
you take a look how `ValuesCodeGenerator`, it sounds like we could cut at least
one layer of the abstraction here. For example couldn't we code generate
`ValuesGeneratorFunction` with already materialized `rows`? Why do we need to
to generate `InputFormat` that we later iterate, to rewrite it to a
`GeneratorFunction`?
##########
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;",
+ " }",
+ "}");
Review Comment:
Hmm? This sounds like a wrong abstraction level for a unit test. It makes
the test very hard to maintain and understand. Why can't you just use
`ValuesCodeGenerator` to generate that?
##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/operators/values/ValuesGeneratorFunction.java:
##########
@@ -0,0 +1,70 @@
+/*
+ * 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.annotation.Internal;
+import org.apache.flink.api.common.io.GenericInputFormat;
+import org.apache.flink.api.connector.source.SourceReaderContext;
+import org.apache.flink.connector.datagen.source.GeneratorFunction;
+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.runtime.typeutils.RowDataSerializer;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Returns the row of a {@code VALUES} clause for a row number. The generated
format only reads
+ * forward, so {@link #open} reads all rows once.
+ */
+@Internal
+public class ValuesGeneratorFunction implements GeneratorFunction<Long,
RowData> {
+
+ private static final long serialVersionUID = 1L;
+
+ private final GeneratedInput<GenericInputFormat<RowData>> generatedInput;
+ private final InternalTypeInfo<RowData> producedType;
+
+ private transient List<RowData> rows;
Review Comment:
Does the legacy version also store values in memory? Is memory consumption
the same between new and legacy?
##########
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:
`roundTrip` seems like a bad name.
--
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]