[ 
https://issues.apache.org/jira/browse/FLINK-34380?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18150612#comment-18150612
 ] 

wind.wang commented on FLINK-34380:
-----------------------------------

Adding a data point from Flink 1.20.5, because the ordering change described 
here caused silent data loss for us when the join result was consumed by a sink 
that applies the retract changelog per key without SinkUpsertMaterializer.

*Setup* (local MiniCluster, parallelism 1, all inputs insert-only with declared 
primary keys; b and d arrive first, q arrives ~3 s later so the first join 
retracts its null-padded rows):
{code:sql}
SELECT x.id, x.k, x.flag, d.attr
FROM (SELECT b.id, b.k, q.flag FROM b LEFT JOIN q ON b.k = q.k) x
LEFT JOIN d ON x.id = d.id
-- table.exec.mini-batch.enabled=true, allow-latency=5s, size=5000
{code}

*Observed changelog of the query (toChangelogStream), N = 1000 keys:*
||mini-batch||shape||per-key output (example id=1)||keys whose last message is 
a retraction||
|off|single LEFT JOIN|+I(null), -D(null), +I(F1)|0|
|on|single LEFT JOIN|+I(null), -D(null), +I(F1)|0|
|off|two chained LEFT JOINs|+I(null), -D(null), +I(F1)|0|
|on|two chained LEFT JOINs|*+I(null), +I(F1), -D(null)*|*508* (same 508 keys in 
3 runs)|

So the reordering shows up when the -D/+I pair produced by the upstream join 
lands in the same bundle of the downstream mini-batch join.

*Impact depends on the sink:*
* Writing to a table with PRIMARY KEY (or toChangelogStream with 
ChangelogMode.upsert() and a primary key): the planner adds 
SinkUpsertMaterializer (upsertMaterialize=[true]) and the result is correct, 
1000/1000. This matches the analysis in the comments above.
* A consumer that applies the retract stream in arrival order by key (+I/+U = 
upsert, -U/-D = delete by key) ends with 492/1000 rows - the new row is written 
and then deleted by the late retraction. We hit exactly this in a commercial 
data-integration product that embeds Flink 1.20 and writes CDC join results 
into Doris / SQL Server: about half of the rows that had ever been updated 
through the join silently disappeared (no errors, checkpoints fine). Disabling 
mini-batch fixed it (0 lost on identical data).

*Suggestion:* since 1.19 (FLIP-415) mini-batch also applies to regular joins 
and they do not preserve per-key order of retract/accumulate messages. It would 
help to state this explicitly in the "Performance Tuning - MiniBatch" docs: 
sinks must either declare a primary key (so SinkUpsertMaterializer is planned) 
or mini-batch should stay disabled for jobs with joins over updating inputs. A 
separate switch to disable mini-batch only for regular joins (keeping it for 
aggregations) would also be useful.

A self-contained Java reproduction (~120 lines, DataStream sources + the SQL 
above, collects the changelog and simulates a per-key retract consumer) is 
available; happy to attach it if useful.


> Strange RowKind and records about intermediate output when using minibatch 
> join
> -------------------------------------------------------------------------------
>
>                 Key: FLINK-34380
>                 URL: https://issues.apache.org/jira/browse/FLINK-34380
>             Project: Flink
>          Issue Type: Bug
>          Components: Table SQL / Runtime
>    Affects Versions: 1.19.0, 2.5.0
>            Reporter: Xuyang Zhong
>            Priority: Major
>              Labels: pull-request-available
>
> {code:java}
> // Add it in CalcItCase
> @Test
>   def test(): Unit = {
>     env.setParallelism(1)
>     val rows = Seq(
>       changelogRow("+I", java.lang.Integer.valueOf(1), "1"),
>       changelogRow("-U", java.lang.Integer.valueOf(1), "1"),
>       changelogRow("+U", java.lang.Integer.valueOf(1), "99"),
>       changelogRow("-D", java.lang.Integer.valueOf(1), "99")
>     )
>     val dataId = TestValuesTableFactory.registerData(rows)
>     val ddl =
>       s"""
>          |CREATE TABLE t1 (
>          |  a int,
>          |  b string
>          |) WITH (
>          |  'connector' = 'values',
>          |  'data-id' = '$dataId',
>          |  'bounded' = 'false'
>          |)
>        """.stripMargin
>     tEnv.executeSql(ddl)
>     val ddl2 =
>       s"""
>          |CREATE TABLE t2 (
>          |  a int,
>          |  b string
>          |) WITH (
>          |  'connector' = 'values',
>          |  'data-id' = '$dataId',
>          |  'bounded' = 'false'
>          |)
>        """.stripMargin
>     tEnv.executeSql(ddl2)
>     tEnv.getConfig.getConfiguration
>       .set(ExecutionConfigOptions.TABLE_EXEC_MINIBATCH_ENABLED, 
> Boolean.box(true))
>     tEnv.getConfig.getConfiguration
>       .set(ExecutionConfigOptions.TABLE_EXEC_MINIBATCH_ALLOW_LATENCY, 
> Duration.ofSeconds(5))
>     tEnv.getConfig.getConfiguration
>       .set(ExecutionConfigOptions.TABLE_EXEC_MINIBATCH_SIZE, Long.box(3L))
>     println(tEnv.sqlQuery("SELECT * from t1 join t2 on t1.a = 
> t2.a").explain())
>     tEnv.executeSql("SELECT * from t1 join t2 on t1.a = t2.a").print()
>   } {code}
> Output:
> {code:java}
> +----+-------------+-----------------+-------------+---------+
> | op |           a |               b |          a0 |      b0 |
> +----+-------------+-----------------+-------------+---------+
> | +U |           1 |               1 |           1 |      99 |
> | +U |           1 |              99 |           1 |      99 |
> | -U |           1 |               1 |           1 |      99 |
> | -D |           1 |              99 |           1 |      99 |
> +----+-------------+-----------------+-------------+---------+{code}



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to