1fanwang opened a new pull request, #29977:
URL: https://github.com/apache/flink/pull/29977

   <!--
   *Thank you very much for contributing to Apache Flink - we are happy that 
you want to help us improve Flink. To help the community review your 
contribution in the best possible way, please go through the checklist below, 
which will get the contribution into a shape in which it can be best reviewed.*
   
   *Please understand that we do not do this to make contributions to Flink a 
hassle. In order to uphold a high standard of quality for code contributions, 
while at the same time managing a large number of contributions, we need 
contributors to prepare the contributions well, and give reviewers enough 
contextual information for the review. Please also understand that 
contributions that do not follow this guide will take longer to review and thus 
typically be picked up with lower priority by the community.*
   
   ## Contribution Checklist
   
     - Make sure that the pull request corresponds to a [JIRA 
issue](https://issues.apache.org/jira/projects/FLINK/issues). Exceptions are 
made for typos in JavaDoc or documentation files, which need no JIRA issue.
     
     - Name the pull request in the form "[FLINK-XXXX] [component] Title of the 
pull request", where *FLINK-XXXX* should be replaced by the actual issue 
number. Skip *component* if you are unsure about which is the best component.
     Typo fixes that have no associated JIRA issue should be named following 
this pattern: `[hotfix] [docs] Fix typo in event time introduction` or 
`[hotfix] [javadocs] Expand JavaDoc for PuncuatedWatermarkGenerator`.
   
     - Fill out the template below to describe the changes contributed by the 
pull request. That will give reviewers the context they need to do the review.
     
     - Make sure that the change passes the automated tests, i.e., `mvn clean 
verify` passes. You can set up Azure Pipelines CI to do that following [this 
guide](https://cwiki.apache.org/confluence/display/FLINK/Azure+Pipelines#AzurePipelines-Tutorial:SettingupAzurePipelinesforaforkoftheFlinkrepository).
   
     - Each pull request should address only one issue, not mix up code from 
multiple issues.
     
     - Each commit in the pull request has a meaningful commit message 
(including the JIRA id)
   
     - Once all items of the checklist are addressed, remove the above text and 
this checklist, leaving only the filled out template below.
   
   
   **(The sections below can be removed for hotfixes of typos)**
   -->
   
   ## What is the purpose of the change
   
   Streaming outer joins can retract initially unmatched rows when a match 
arrives. MAX and MIN above a filter on those rows can keep the retracted 
values, even while COUNT and SUM reflect the correct rows.
   
   https://issues.apache.org/jira/browse/FLINK-41507
   
   ## Brief change log
   
   Tell the planner that outer joins can remove rows. It can then choose 
aggregates that discard retracted values, so MAX and MIN reflect the remaining 
rows.
   
   ## Verifying this change
   
   Please make sure both new and modified tests in this PR follow [the 
conventions for tests defined in our code quality 
guide](https://flink.apache.org/how-to-contribute/code-style-and-quality-common/#7-testing).
   
   ### Testing done
   
   The reproducer runs a streaming SQL join on a local mini cluster. It waits 
until the sink has received the three unmatched rows before emitting the 
matching rows. Set JAVA_HOME to JDK 17 and save the source below as 
flink-table/flink-table-planner/target/repro/OuterJoinRepro.java.
   
   The pre-fix planner JAR was built at base commit 
7b436bcf6cff64cdd47c13674ca72e9462fe584f with JDK 17:
   
   ```bash
   git worktree add --detach 
flink-table/flink-table-planner/target/repro/baseline 
7b436bcf6cff64cdd47c13674ca72e9462fe584f
   JAVA_HOME=$(/usr/libexec/java_home -v 17) ./mvnw -f 
flink-table/flink-table-planner/target/repro/baseline/pom.xml -pl 
flink-table/flink-table-planner -DskipTests -Dfast -Pskip-webui-build package
   ```
   
   ```text
   [INFO] BUILD SUCCESS
   ```
   
   On the fixed checkout, compile the planner and test fixtures, generate the 
classpath, and compile the reproducer:
   
   ```bash
   REPRO=flink-table/flink-table-planner/target/repro
   JAVA_HOME=$(/usr/libexec/java_home -v 17) ./mvnw -pl 
flink-table/flink-table-planner -DskipTests -Dfast -Pskip-webui-build 
test-compile
   ./mvnw -q -pl flink-table/flink-table-planner -DincludeScope=test 
-Dmdep.outputFile=target/repro/planner-classpath dependency:build-classpath
   
CP="flink-table/flink-table-planner/target/classes:flink-table/flink-table-planner/target/test-classes:$(cat
 "$REPRO/planner-classpath")"
   "$JAVA_HOME/bin/javac" -cp "$CP" "$REPRO/OuterJoinRepro.java"
   ```
   
   Before the fix, using the planner JAR built from the unmodified checkout:
   
   ```bash
   REPRO=flink-table/flink-table-planner/target/repro; 
CP="$REPRO/baseline/flink-table/flink-table-planner/target/flink-table-planner_2.12-2.5-SNAPSHOT.jar:flink-table/flink-table-planner/target/test-classes:$(cat
 "$REPRO/planner-classpath"):$REPRO"; "$JAVA_HOME/bin/java" 
--add-opens=java.base/java.lang=ALL-UNNAMED 
--add-opens=java.base/java.util=ALL-UNNAMED 
--add-opens=java.base/java.io=ALL-UNNAMED -cp "$CP" OuterJoinRepro > 
"$REPRO/before.log" 2>&1; result=$?; tail -4 "$REPRO/before.log"; exit "$result"
   ```
   
   ```text
   Raw: [+I[3, 3, 1, 3], -U[3, 3, 1, 3], +U[3, -9, 2, -6], -U[3, -9, 2, -6], 
+U[3, -9, 3, -10], -U[3, -9, 3, -10], +U[3, -9, 2, -13], -U[3, -9, 2, -13], 
+U[3, -9, 1, -4]]
   Final: [+I[3, -9, 1, -4]]
   Exception in thread "main" java.lang.AssertionError: Expected [-4,-4,1,-4]
        at OuterJoinRepro.main(OuterJoinRepro.java:45)
   ```
   
   After the fix, using the recompiled planner classes:
   
   ```bash
   REPRO=flink-table/flink-table-planner/target/repro; 
CP="flink-table/flink-table-planner/target/classes:flink-table/flink-table-planner/target/test-classes:$(cat
 "$REPRO/planner-classpath"):$REPRO"; "$JAVA_HOME/bin/java" 
--add-opens=java.base/java.lang=ALL-UNNAMED 
--add-opens=java.base/java.util=ALL-UNNAMED 
--add-opens=java.base/java.io=ALL-UNNAMED -cp "$CP" OuterJoinRepro > 
"$REPRO/after.log" 2>&1; result=$?; tail -2 "$REPRO/after.log"; exit "$result"
   ```
   
   ```text
   Raw: [+I[3, 3, 1, 3], -U[3, 3, 1, 3], +U[3, -9, 2, -6], -U[3, -9, 2, -6], 
+U[3, -9, 3, -10], -U[3, -9, 3, -10], +U[-4, -9, 2, -13], -U[-4, -9, 2, -13], 
+U[-4, -4, 1, -4]]
   Final: [+I[-4, -4, 1, -4]]
   ```
   
   <details>
   <summary>Reproducer source: OuterJoinRepro.java</summary>
   
   ```java
   import org.apache.flink.api.common.eventtime.WatermarkStrategy;
   import org.apache.flink.api.common.typeinfo.Types;
   import org.apache.flink.connector.datagen.source.DataGeneratorSource;
   import org.apache.flink.connector.datagen.source.GeneratorFunction;
   import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
   import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
   import org.apache.flink.table.planner.factories.TestValuesTableFactory;
   import org.apache.flink.types.Row;
   
   import java.util.Arrays;
   import java.util.concurrent.TimeUnit;
   
   public class OuterJoinRepro {
       public static final class MatchesAfterInitialAggregate implements 
GeneratorFunction<Long, Row> {
           @Override
           public Row map(Long index) throws Exception {
               long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(60);
               while 
(TestValuesTableFactory.getRawResults("result_sink").stream()
                       .noneMatch(row -> 
Long.valueOf(3).equals(row.getField(2)))) {
                   if (System.nanoTime() >= deadline) {
                       throw new IllegalStateException("Initial aggregate did 
not reach sink");
                   }
                   Thread.sleep(10);
               }
               return Row.of(index == 0 ? 3 : -9);
           }
       }
   
       public static void main(String[] args) throws Exception {
           StreamExecutionEnvironment env = 
StreamExecutionEnvironment.getExecutionEnvironment();
           env.setParallelism(1);
           StreamTableEnvironment table = StreamTableEnvironment.create(env);
           String id = 
TestValuesTableFactory.registerData(Arrays.asList(Row.of(3), Row.of(-9), 
Row.of(-4)));
           table.executeSql("CREATE TABLE a (x INT) WITH ('connector'='values', 
'data-id'='" + id + "', 'bounded'='true')");
           table.createTemporaryView("b", table.fromDataStream(env.fromSource(
                   new DataGeneratorSource<>(new 
MatchesAfterInitialAggregate(), 2, Types.ROW_NAMED(new String[]{"y"}, 
Types.INT)),
                   WatermarkStrategy.noWatermarks(), "matches")));
           table.executeSql("CREATE TABLE result_sink (max_x INT, min_x INT, 
cnt BIGINT, sum_x INT) WITH ('connector'='values', 
'sink-insert-only'='false')");
           String sql = "INSERT INTO result_sink SELECT MAX(x), MIN(x), 
COUNT(*), SUM(x) FROM a LEFT JOIN b ON x = y WHERE y IS NULL";
           System.out.println(table.explainSql(sql));
           table.executeSql(sql).await();
           System.out.println("Raw: " + 
TestValuesTableFactory.getRawResultsAsStrings("result_sink"));
           System.out.println("Final: " + 
TestValuesTableFactory.getResults("result_sink"));
           if 
(!TestValuesTableFactory.getResults("result_sink").equals(Arrays.asList(Row.of(-4,
 -4, 1L, -4)))) {
               throw new AssertionError("Expected [-4,-4,1,-4]");
           }
       }
   }
   ```
   </details>
   
   Recompiling affected queries selects different accumulator state. Restoring 
pre-fix savepoints has not been verified. The full clean verify was not run.
   
   ## Does this pull request potentially affect one of the following parts:
   
     - Dependencies (does it add or upgrade a dependency): no
     - The public API, i.e., is any changed class annotated with 
`@Public(Evolving)`: no
     - The serializers: yes, affected aggregates use different accumulator 
state; restore compatibility has not been verified
     - The runtime per-record code paths (performance sensitive): yes, affected 
aggregates use retraction-aware state
     - Anything that affects deployment or recovery: JobManager (and its 
components), Checkpointing, Kubernetes/Yarn, ZooKeeper: possible effect on 
savepoint restore; not verified
     - The S3 file system connector: no
   
   ## Documentation
   
     - Does this pull request introduce a new feature? no
     - If yes, how is the feature documented? not applicable
   
   ---
   
   ##### Was generative AI tooling used to co-author this PR?
   
   <!--
   If generative AI tooling has been used in the process of authoring this PR, 
please
   change the checkbox below to `[X]` and replace the placeholder in the 
"Generated-by"
   line with the tool name and version. Otherwise remove the "Generated-by" 
line.
   See the ASF Generative Tooling Guidance for details:
   https://www.apache.org/legal/generative-tooling.html
   
   You are responsible for the quality and correctness of every change in this 
PR
   regardless of the tooling used. Low-effort AI-generated PRs will be closed. 
See
   AGENTS.md for the full guidance.
   -->
   
   - [X] Yes
   
   Generated-by: GitHub Copilot CLI 1.0.112 (Auto)


-- 
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]

Reply via email to