File size: 40,747 Bytes
5a9e79d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 | diff --git a/flink-python/pyflink/table/tests/test_table_completeness.py b/flink-python/pyflink/table/tests/test_table_completeness.py
--- a/flink-python/pyflink/table/tests/test_table_completeness.py
+++ b/flink-python/pyflink/table/tests/test_table_completeness.py
@@ -42,6 +42,7 @@ def excluded_methods(cls):
'asArgument',
'process',
'partitionBy',
+ 'fromChangelog',
}
@classmethod
diff --git a/flink-table/flink-table-common/src/test/java/org/apache/flink/table/types/inference/strategies/FromChangelogInputTypeStrategyTest.java b/flink-table/flink-table-common/src/test/java/org/apache/flink/table/types/inference/strategies/FromChangelogInputTypeStrategyTest.java
new file mode 100644
--- /dev/null
+++ b/flink-table/flink-table-common/src/test/java/org/apache/flink/table/types/inference/strategies/FromChangelogInputTypeStrategyTest.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.types.inference.strategies;
+
+import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.types.DataType;
+import org.apache.flink.table.types.inference.InputTypeStrategiesTestBase;
+import org.apache.flink.table.types.inference.utils.TableSemanticsMock;
+import org.apache.flink.types.ColumnList;
+
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Stream;
+
+import static org.apache.flink.table.types.inference.strategies.SpecificInputTypeStrategies.FROM_CHANGELOG_INPUT_TYPE_STRATEGY;
+
+/** Tests for {@link FromChangelogTypeStrategy#INPUT_TYPE_STRATEGY}. */
+class FromChangelogInputTypeStrategyTest extends InputTypeStrategiesTestBase {
+
+ private static final DataType TABLE_TYPE =
+ DataTypes.ROW(
+ DataTypes.FIELD("id", DataTypes.INT()),
+ DataTypes.FIELD("op", DataTypes.STRING()),
+ DataTypes.FIELD("name", DataTypes.STRING()));
+
+ private static final DataType DESCRIPTOR_TYPE = DataTypes.DESCRIPTOR();
+
+ private static final DataType MAP_TYPE = DataTypes.MAP(DataTypes.STRING(), DataTypes.STRING());
+
+ @Override
+ protected Stream<TestSpec> testData() {
+ return Stream.of(
+ // Valid: custom mapping with all change operations
+ TestSpec.forStrategy(
+ "Valid with custom mapping", FROM_CHANGELOG_INPUT_TYPE_STRATEGY)
+ .calledWithArgumentTypes(TABLE_TYPE, DESCRIPTOR_TYPE, MAP_TYPE)
+ .calledWithTableSemanticsAt(0, new TableSemanticsMock(TABLE_TYPE))
+ .calledWithLiteralAt(1, ColumnList.of(List.of("op")))
+ .calledWithLiteralAt(
+ 2,
+ Map.of(
+ "c", "INSERT",
+ "ub", "UPDATE_BEFORE",
+ "ua", "UPDATE_AFTER",
+ "d", "DELETE"))
+ .expectArgumentTypes(TABLE_TYPE, DESCRIPTOR_TYPE, MAP_TYPE),
+
+ // Valid: retract-style mapping with UPDATE_BEFORE
+ TestSpec.forStrategy("Valid with UPDATE_BEFORE", FROM_CHANGELOG_INPUT_TYPE_STRATEGY)
+ .calledWithArgumentTypes(TABLE_TYPE, DESCRIPTOR_TYPE, MAP_TYPE)
+ .calledWithTableSemanticsAt(0, new TableSemanticsMock(TABLE_TYPE))
+ .calledWithLiteralAt(1, ColumnList.of(List.of("op")))
+ .calledWithLiteralAt(
+ 2,
+ Map.of(
+ "c", "INSERT",
+ "ub", "UPDATE_BEFORE",
+ "ua", "UPDATE_AFTER",
+ "d", "DELETE"))
+ .expectArgumentTypes(TABLE_TYPE, DESCRIPTOR_TYPE, MAP_TYPE),
+
+ // Error: op column not found
+ TestSpec.forStrategy(
+ "Op column not found in schema", FROM_CHANGELOG_INPUT_TYPE_STRATEGY)
+ .calledWithArgumentTypes(TABLE_TYPE, DESCRIPTOR_TYPE)
+ .calledWithTableSemanticsAt(0, new TableSemanticsMock(TABLE_TYPE))
+ .calledWithLiteralAt(1, ColumnList.of(List.of("nonexistent")))
+ .expectErrorMessage("The op column 'nonexistent' does not exist"),
+
+ // Error: op column is not STRING
+ TestSpec.forStrategy("Op column wrong type", FROM_CHANGELOG_INPUT_TYPE_STRATEGY)
+ .calledWithArgumentTypes(
+ DataTypes.ROW(
+ DataTypes.FIELD("id", DataTypes.INT()),
+ DataTypes.FIELD("op", DataTypes.INT()),
+ DataTypes.FIELD("name", DataTypes.STRING())),
+ DESCRIPTOR_TYPE)
+ .calledWithTableSemanticsAt(
+ 0,
+ new TableSemanticsMock(
+ DataTypes.ROW(
+ DataTypes.FIELD("id", DataTypes.INT()),
+ DataTypes.FIELD("op", DataTypes.INT()),
+ DataTypes.FIELD("name", DataTypes.STRING()))))
+ .calledWithLiteralAt(1, ColumnList.of(List.of("op")))
+ .expectErrorMessage("must be of STRING type"),
+
+ // Error: multi-column descriptor
+ TestSpec.forStrategy(
+ "Descriptor with multiple columns",
+ FROM_CHANGELOG_INPUT_TYPE_STRATEGY)
+ .calledWithArgumentTypes(TABLE_TYPE, DESCRIPTOR_TYPE)
+ .calledWithTableSemanticsAt(0, new TableSemanticsMock(TABLE_TYPE))
+ .calledWithLiteralAt(1, ColumnList.of(List.of("a", "b")))
+ .expectErrorMessage("must contain exactly one column name"),
+
+ // Error: invalid RowKind in op_mapping value
+ TestSpec.forStrategy(
+ "Invalid RowKind in mapping value",
+ FROM_CHANGELOG_INPUT_TYPE_STRATEGY)
+ .calledWithArgumentTypes(TABLE_TYPE, DESCRIPTOR_TYPE, MAP_TYPE)
+ .calledWithTableSemanticsAt(0, new TableSemanticsMock(TABLE_TYPE))
+ .calledWithLiteralAt(1, ColumnList.of(List.of("op")))
+ .calledWithLiteralAt(2, Map.of("c", "INVALID_KIND"))
+ .expectErrorMessage("Unknown change operation: 'INVALID_KIND'"),
+
+ // Error: duplicate RowKind across entries
+ TestSpec.forStrategy(
+ "Duplicate RowKind in mapping values",
+ FROM_CHANGELOG_INPUT_TYPE_STRATEGY)
+ .calledWithArgumentTypes(TABLE_TYPE, DESCRIPTOR_TYPE, MAP_TYPE)
+ .calledWithTableSemanticsAt(0, new TableSemanticsMock(TABLE_TYPE))
+ .calledWithLiteralAt(1, ColumnList.of(List.of("op")))
+ .calledWithLiteralAt(2, Map.of("c", "INSERT", "r", "INSERT"))
+ .expectErrorMessage("Duplicate change operation: 'INSERT'"),
+
+ // Valid: INSERT-only mapping (append mode, no updates)
+ TestSpec.forStrategy(
+ "Valid INSERT-only mapping", FROM_CHANGELOG_INPUT_TYPE_STRATEGY)
+ .calledWithArgumentTypes(TABLE_TYPE, DESCRIPTOR_TYPE, MAP_TYPE)
+ .calledWithTableSemanticsAt(0, new TableSemanticsMock(TABLE_TYPE))
+ .calledWithLiteralAt(1, ColumnList.of(List.of("op")))
+ .calledWithLiteralAt(2, Map.of("c, r", "INSERT"))
+ .expectArgumentTypes(TABLE_TYPE, DESCRIPTOR_TYPE, MAP_TYPE),
+
+ // Valid: INSERT + DELETE mapping (no updates)
+ TestSpec.forStrategy(
+ "Valid INSERT and DELETE mapping",
+ FROM_CHANGELOG_INPUT_TYPE_STRATEGY)
+ .calledWithArgumentTypes(TABLE_TYPE, DESCRIPTOR_TYPE, MAP_TYPE)
+ .calledWithTableSemanticsAt(0, new TableSemanticsMock(TABLE_TYPE))
+ .calledWithLiteralAt(1, ColumnList.of(List.of("op")))
+ .calledWithLiteralAt(2, Map.of("c", "INSERT", "d", "DELETE"))
+ .expectArgumentTypes(TABLE_TYPE, DESCRIPTOR_TYPE, MAP_TYPE),
+
+ // Error: UPDATE_AFTER without UPDATE_BEFORE not supported
+ TestSpec.forStrategy(
+ "UPDATE_AFTER requires UPDATE_BEFORE",
+ FROM_CHANGELOG_INPUT_TYPE_STRATEGY)
+ .calledWithArgumentTypes(TABLE_TYPE, DESCRIPTOR_TYPE, MAP_TYPE)
+ .calledWithTableSemanticsAt(0, new TableSemanticsMock(TABLE_TYPE))
+ .calledWithLiteralAt(1, ColumnList.of(List.of("op")))
+ .calledWithLiteralAt(
+ 2,
+ Map.of(
+ "c", "INSERT",
+ "u", "UPDATE_AFTER",
+ "d", "DELETE"))
+ .expectErrorMessage("must include UPDATE_BEFORE"));
+ }
+}
diff --git a/flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/plan/nodes/exec/stream/FromChangelogSemanticTests.java b/flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/plan/nodes/exec/stream/FromChangelogSemanticTests.java
new file mode 100644
--- /dev/null
+++ b/flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/plan/nodes/exec/stream/FromChangelogSemanticTests.java
@@ -0,0 +1,49 @@
+/*
+ * 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.plan.nodes.exec.stream;
+
+import org.apache.flink.table.api.TableConfig;
+import org.apache.flink.table.api.config.OptimizerConfigOptions;
+import org.apache.flink.table.planner.plan.nodes.exec.testutils.SemanticTestBase;
+import org.apache.flink.table.test.program.TableTestProgram;
+
+import java.util.List;
+
+/** Semantic tests for the built-in FROM_CHANGELOG process table function. */
+public class FromChangelogSemanticTests extends SemanticTestBase {
+
+ @Override
+ protected void applyDefaultEnvironmentOptions(TableConfig config) {
+ super.applyDefaultEnvironmentOptions(config);
+ config.set(
+ OptimizerConfigOptions.TABLE_OPTIMIZER_NONDETERMINISTIC_UPDATE_STRATEGY,
+ OptimizerConfigOptions.NonDeterministicUpdateStrategy.IGNORE);
+ }
+
+ @Override
+ public List<TableTestProgram> programs() {
+ return List.of(
+ FromChangelogTestPrograms.DEFAULT_OP_MAPPING,
+ FromChangelogTestPrograms.CUSTOM_OP_MAPPING,
+ FromChangelogTestPrograms.UNMAPPED_CODES_DROPPED,
+ FromChangelogTestPrograms.CUSTOM_OP_NAME,
+ FromChangelogTestPrograms.TABLE_API_DEFAULT,
+ FromChangelogTestPrograms.ROUND_TRIP);
+ }
+}
diff --git a/flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/plan/nodes/exec/stream/FromChangelogTestPrograms.java b/flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/plan/nodes/exec/stream/FromChangelogTestPrograms.java
new file mode 100644
--- /dev/null
+++ b/flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/plan/nodes/exec/stream/FromChangelogTestPrograms.java
@@ -0,0 +1,210 @@
+/*
+ * 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.plan.nodes.exec.stream;
+
+import org.apache.flink.table.connector.ChangelogMode;
+import org.apache.flink.table.test.program.SinkTestStep;
+import org.apache.flink.table.test.program.SourceTestStep;
+import org.apache.flink.table.test.program.TableTestProgram;
+import org.apache.flink.types.Row;
+import org.apache.flink.types.RowKind;
+
+/** {@link TableTestProgram} definitions for testing the built-in FROM_CHANGELOG PTF. */
+public class FromChangelogTestPrograms {
+
+ private static final String[] SIMPLE_CDC_SCHEMA = {"id INT", "op STRING", "name STRING"};
+
+ // --------------------------------------------------------------------------------------------
+ // SQL tests
+ // --------------------------------------------------------------------------------------------
+
+ public static final TableTestProgram DEFAULT_OP_MAPPING =
+ TableTestProgram.of(
+ "from-changelog-default-op-mapping",
+ "default mapping with standard op names")
+ .setupTableSource(
+ SourceTestStep.newBuilder("cdc_stream")
+ .addSchema(SIMPLE_CDC_SCHEMA)
+ .producedValues(
+ Row.of(1, "INSERT", "Alice"),
+ Row.of(2, "INSERT", "Bob"),
+ Row.of(1, "UPDATE_BEFORE", "Alice"),
+ Row.of(1, "UPDATE_AFTER", "Alice2"),
+ Row.of(2, "DELETE", "Bob"))
+ .build())
+ .setupTableSink(
+ SinkTestStep.newBuilder("sink")
+ .addSchema("id INT", "name STRING")
+ .consumedValues(
+ Row.ofKind(RowKind.INSERT, 1, "Alice"),
+ Row.ofKind(RowKind.INSERT, 2, "Bob"),
+ Row.ofKind(RowKind.UPDATE_BEFORE, 1, "Alice"),
+ Row.ofKind(RowKind.UPDATE_AFTER, 1, "Alice2"),
+ Row.ofKind(RowKind.DELETE, 2, "Bob"))
+ .build())
+ .runSql(
+ "INSERT INTO sink SELECT * FROM FROM_CHANGELOG("
+ + "input => TABLE cdc_stream)")
+ .build();
+
+ public static final TableTestProgram CUSTOM_OP_MAPPING =
+ TableTestProgram.of(
+ "from-changelog-custom-op-mapping",
+ "custom op_mapping with comma-separated keys")
+ .setupTableSource(
+ SourceTestStep.newBuilder("cdc_stream")
+ .addSchema(SIMPLE_CDC_SCHEMA)
+ .producedValues(
+ Row.of(1, "c", "Alice"),
+ Row.of(2, "r", "Bob"),
+ Row.of(1, "ub", "Alice"),
+ Row.of(1, "ua", "Alice2"),
+ Row.of(2, "d", "Bob"))
+ .build())
+ .setupTableSink(
+ SinkTestStep.newBuilder("sink")
+ .addSchema("id INT", "name STRING")
+ .consumedValues(
+ Row.ofKind(RowKind.INSERT, 1, "Alice"),
+ Row.ofKind(RowKind.INSERT, 2, "Bob"),
+ Row.ofKind(RowKind.UPDATE_BEFORE, 1, "Alice"),
+ Row.ofKind(RowKind.UPDATE_AFTER, 1, "Alice2"),
+ Row.ofKind(RowKind.DELETE, 2, "Bob"))
+ .build())
+ .runSql(
+ "INSERT INTO sink SELECT * FROM FROM_CHANGELOG("
+ + "input => TABLE cdc_stream, "
+ + "op_mapping => MAP['c, r', 'INSERT', 'ub', 'UPDATE_BEFORE', 'ua', 'UPDATE_AFTER', 'd', 'DELETE'])")
+ .build();
+
+ public static final TableTestProgram UNMAPPED_CODES_DROPPED =
+ TableTestProgram.of(
+ "from-changelog-unmapped-codes-dropped",
+ "unmapped op codes are silently dropped")
+ .setupTableSource(
+ SourceTestStep.newBuilder("cdc_stream")
+ .addSchema(SIMPLE_CDC_SCHEMA)
+ .producedValues(
+ Row.of(1, "INSERT", "Alice"),
+ Row.of(2, "INSERT", "Bob"),
+ Row.of(1, "UNKNOWN", "Alice2"),
+ Row.of(2, "DELETE", "Bob"))
+ .build())
+ .setupTableSink(
+ SinkTestStep.newBuilder("sink")
+ .addSchema("id INT", "name STRING")
+ .consumedValues(
+ Row.ofKind(RowKind.INSERT, 1, "Alice"),
+ Row.ofKind(RowKind.INSERT, 2, "Bob"),
+ Row.ofKind(RowKind.DELETE, 2, "Bob"))
+ .build())
+ .runSql(
+ "INSERT INTO sink SELECT * FROM FROM_CHANGELOG("
+ + "input => TABLE cdc_stream)")
+ .build();
+
+ /** Custom op column name via DESCRIPTOR. */
+ public static final TableTestProgram CUSTOM_OP_NAME =
+ TableTestProgram.of(
+ "from-changelog-custom-op-name", "custom op column name via DESCRIPTOR")
+ .setupTableSource(
+ SourceTestStep.newBuilder("cdc_stream")
+ .addSchema("id INT", "operation STRING", "name STRING")
+ .producedValues(
+ Row.of(1, "INSERT", "Alice"),
+ Row.of(1, "UPDATE_BEFORE", "Alice"),
+ Row.of(1, "UPDATE_AFTER", "Alice2"))
+ .build())
+ .setupTableSink(
+ SinkTestStep.newBuilder("sink")
+ .addSchema("id INT", "name STRING")
+ .consumedValues(
+ Row.ofKind(RowKind.INSERT, 1, "Alice"),
+ Row.ofKind(RowKind.UPDATE_BEFORE, 1, "Alice"),
+ Row.ofKind(RowKind.UPDATE_AFTER, 1, "Alice2"))
+ .build())
+ .runSql(
+ "INSERT INTO sink SELECT * FROM FROM_CHANGELOG("
+ + "input => TABLE cdc_stream, "
+ + "op => DESCRIPTOR(operation))")
+ .build();
+
+ // --------------------------------------------------------------------------------------------
+ // Table API test
+ // --------------------------------------------------------------------------------------------
+
+ public static final TableTestProgram TABLE_API_DEFAULT =
+ TableTestProgram.of(
+ "from-changelog-table-api-default",
+ "Table.fromChangelog() convenience method")
+ .setupTableSource(
+ SourceTestStep.newBuilder("cdc_stream")
+ .addSchema(SIMPLE_CDC_SCHEMA)
+ .producedValues(
+ Row.of(1, "INSERT", "Alice"),
+ Row.of(2, "INSERT", "Bob"))
+ .build())
+ .setupTableSink(
+ SinkTestStep.newBuilder("sink")
+ .addSchema("id INT", "name STRING")
+ .consumedValues(
+ Row.ofKind(RowKind.INSERT, 1, "Alice"),
+ Row.ofKind(RowKind.INSERT, 2, "Bob"))
+ .build())
+ .runTableApi(env -> env.from("cdc_stream").fromChangelog(), "sink")
+ .build();
+
+ // --------------------------------------------------------------------------------------------
+ // Round-trip test: FROM_CHANGELOG(TO_CHANGELOG(table))
+ // --------------------------------------------------------------------------------------------
+
+ /** Verifies that FROM_CHANGELOG(TO_CHANGELOG(table)) recovers the original dynamic table. */
+ public static final TableTestProgram ROUND_TRIP =
+ TableTestProgram.of(
+ "from-changelog-round-trip",
+ "FROM_CHANGELOG(TO_CHANGELOG(table)) recovers original table")
+ .setupTableSource(
+ SourceTestStep.newBuilder("orders")
+ .addSchema("id INT", "name STRING")
+ .addMode(ChangelogMode.all())
+ .producedValues(
+ Row.ofKind(RowKind.INSERT, 1, "Alice"),
+ Row.ofKind(RowKind.INSERT, 2, "Bob"),
+ Row.ofKind(RowKind.UPDATE_BEFORE, 1, "Alice"),
+ Row.ofKind(RowKind.UPDATE_AFTER, 1, "Alice2"),
+ Row.ofKind(RowKind.DELETE, 2, "Bob"))
+ .build())
+ .setupTableSink(
+ SinkTestStep.newBuilder("sink")
+ .addSchema("id INT", "name STRING")
+ .consumedValues(
+ Row.ofKind(RowKind.INSERT, 1, "Alice"),
+ Row.ofKind(RowKind.INSERT, 2, "Bob"),
+ Row.ofKind(RowKind.UPDATE_BEFORE, 1, "Alice"),
+ Row.ofKind(RowKind.UPDATE_AFTER, 1, "Alice2"),
+ Row.ofKind(RowKind.DELETE, 2, "Bob"))
+ .build())
+ .setupSql(
+ "CREATE VIEW changelog_view AS "
+ + "SELECT * FROM TO_CHANGELOG(input => TABLE orders)")
+ .runSql(
+ "INSERT INTO sink SELECT * FROM FROM_CHANGELOG("
+ + "input => TABLE changelog_view)")
+ .build();
+}
diff --git a/flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/plan/nodes/exec/stream/ProcessTableFunctionTestUtils.java b/flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/plan/nodes/exec/stream/ProcessTableFunctionTestUtils.java
--- a/flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/plan/nodes/exec/stream/ProcessTableFunctionTestUtils.java
+++ b/flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/plan/nodes/exec/stream/ProcessTableFunctionTestUtils.java
@@ -953,6 +953,21 @@ public void eval(Context ctx, @ArgumentHint({ROW_SEMANTIC_TABLE, SUPPORT_UPDATES
collectUpdate(ctx, r);
}
+ @Override
+ public ChangelogMode getChangelogMode(ChangelogContext changelogContext) {
+ return ChangelogMode.upsert(false);
+ }
+ }
+
+ /** Testing function that uses row semantics with retract mode (valid). */
+ public static class UpdatingRetractRowSemanticFunction
+ extends ChangelogProcessTableFunctionBase {
+ public void eval(
+ Context ctx,
+ @ArgumentHint({ROW_SEMANTIC_TABLE, SUPPORT_UPDATES, REQUIRE_UPDATE_BEFORE}) Row r) {
+ collectUpdate(ctx, r);
+ }
+
@Override
public ChangelogMode getChangelogMode(ChangelogContext changelogContext) {
return ChangelogMode.all();
diff --git a/flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/plan/stream/sql/FromChangelogTest.java b/flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/plan/stream/sql/FromChangelogTest.java
new file mode 100644
--- /dev/null
+++ b/flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/plan/stream/sql/FromChangelogTest.java
@@ -0,0 +1,77 @@
+/*
+ * 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.plan.stream.sql;
+
+import org.apache.flink.table.api.ExplainDetail;
+import org.apache.flink.table.api.TableConfig;
+import org.apache.flink.table.planner.utils.TableTestBase;
+import org.apache.flink.table.planner.utils.TableTestUtil;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * Plan tests for the FROM_CHANGELOG built-in process table function. Uses {@link
+ * ExplainDetail#CHANGELOG_MODE} to verify changelog mode propagation through the plan.
+ */
+public class FromChangelogTest extends TableTestBase {
+
+ private static final List<ExplainDetail> CHANGELOG_MODE =
+ Collections.singletonList(ExplainDetail.CHANGELOG_MODE);
+
+ private TableTestUtil util;
+
+ @BeforeEach
+ void setup() {
+ util = streamTestUtil(TableConfig.getDefault());
+ }
+
+ @Test
+ void testInsertOnlySource() {
+ util.tableEnv()
+ .executeSql(
+ "CREATE TABLE cdc_stream ("
+ + " id INT,"
+ + " op STRING,"
+ + " name STRING"
+ + ") WITH ('connector' = 'values')");
+ util.verifyRelPlan(
+ "SELECT * FROM FROM_CHANGELOG(input => TABLE cdc_stream)", CHANGELOG_MODE);
+ }
+
+ @Test
+ void testCustomOpMapping() {
+ util.tableEnv()
+ .executeSql(
+ "CREATE TABLE cdc_stream ("
+ + " id INT,"
+ + " __op STRING,"
+ + " name STRING"
+ + ") WITH ('connector' = 'values')");
+ util.verifyRelPlan(
+ "SELECT * FROM FROM_CHANGELOG("
+ + "input => TABLE cdc_stream, "
+ + "op => DESCRIPTOR(__op), "
+ + "op_mapping => MAP['c, r', 'INSERT', 'ub', 'UPDATE_BEFORE', 'ua', 'UPDATE_AFTER', 'd', 'DELETE'])",
+ CHANGELOG_MODE);
+ }
+}
diff --git a/flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/plan/stream/sql/ProcessTableFunctionTest.java b/flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/plan/stream/sql/ProcessTableFunctionTest.java
--- a/flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/plan/stream/sql/ProcessTableFunctionTest.java
+++ b/flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/plan/stream/sql/ProcessTableFunctionTest.java
@@ -20,6 +20,7 @@
import org.apache.flink.table.annotation.ArgumentHint;
import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.ExplainDetail;
import org.apache.flink.table.api.TableConfig;
import org.apache.flink.table.catalog.DataTypeFactory;
import org.apache.flink.table.functions.ProcessTableFunction;
@@ -42,6 +43,7 @@
import org.apache.flink.table.planner.plan.nodes.exec.stream.ProcessTableFunctionTestUtils.SetSemanticTablePassThroughFunction;
import org.apache.flink.table.planner.plan.nodes.exec.stream.ProcessTableFunctionTestUtils.TypedRowSemanticTableFunction;
import org.apache.flink.table.planner.plan.nodes.exec.stream.ProcessTableFunctionTestUtils.TypedSetSemanticTableFunction;
+import org.apache.flink.table.planner.plan.nodes.exec.stream.ProcessTableFunctionTestUtils.UpdatingRetractRowSemanticFunction;
import org.apache.flink.table.planner.plan.nodes.exec.stream.ProcessTableFunctionTestUtils.UpdatingUpsertFunction;
import org.apache.flink.table.planner.plan.nodes.exec.stream.ProcessTableFunctionTestUtils.User;
import org.apache.flink.table.planner.utils.TableTestBase;
@@ -60,6 +62,7 @@
import java.util.Optional;
import java.util.stream.Stream;
+import static java.util.Collections.singletonList;
import static org.apache.flink.core.testutils.FlinkAssertions.anyCauseMatches;
import static org.apache.flink.table.annotation.ArgumentTrait.OPTIONAL_PARTITION_BY;
import static org.apache.flink.table.annotation.ArgumentTrait.PASS_COLUMNS_THROUGH;
@@ -282,6 +285,13 @@ void testRowSemanticTableOptionalUid() {
.addInsertSql("INSERT INTO t_sink SELECT * FROM f(r => TABLE t, i => 42)"));
}
+ @Test
+ void testRetractModeWithRowSemantics() {
+ util.addTemporarySystemFunction("f", UpdatingRetractRowSemanticFunction.class);
+ util.verifyRelPlan(
+ "SELECT * FROM f(r => TABLE t)", singletonList(ExplainDetail.CHANGELOG_MODE));
+ }
+
@ParameterizedTest
@MethodSource("errorSpecs")
void testErrorBehavior(ErrorSpec spec) {
@@ -455,10 +465,10 @@ private static Stream<ErrorSpec> errorSpecs() {
"SELECT * FROM f()",
"Table arguments must not be optional."),
ErrorSpec.ofSelect(
- "no changelog support for tables with row semantics",
+ "no upsert support for tables with row semantics",
InvalidUpdatingSemanticsFunction.class,
"SELECT * FROM f(r => TABLE t)",
- "PTFs that take table arguments with row semantics don't support updating output. "
+ "PTFs that take table arguments with row semantics don't support upsert output. "
+ "Table argument 'r' of function 'f' must use set semantics."),
ErrorSpec.ofSelect(
"on_time is not supported on updating output",
diff --git a/flink-table/flink-table-planner/src/test/resources/org/apache/flink/table/planner/plan/stream/sql/FromChangelogTest.xml b/flink-table/flink-table-planner/src/test/resources/org/apache/flink/table/planner/plan/stream/sql/FromChangelogTest.xml
new file mode 100644
--- /dev/null
+++ b/flink-table/flink-table-planner/src/test/resources/org/apache/flink/table/planner/plan/stream/sql/FromChangelogTest.xml
@@ -0,0 +1,57 @@
+<?xml version="1.0" ?>
+<!--
+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.
+-->
+<Root>
+ <TestCase name="testCustomOpMapping">
+ <Resource name="sql">
+ <![CDATA[SELECT * FROM FROM_CHANGELOG(input => TABLE cdc_stream, op => DESCRIPTOR(__op), op_mapping => MAP['c, r', 'INSERT', 'ub', 'UPDATE_BEFORE', 'ua', 'UPDATE_AFTER', 'd', 'DELETE'])]]>
+ </Resource>
+ <Resource name="ast">
+ <![CDATA[
+LogicalProject(id=[$0], name=[$1])
++- LogicalTableFunctionScan(invocation=[FROM_CHANGELOG(TABLE(#0), DESCRIPTOR(_UTF-16LE'__op'), MAP(_UTF-16LE'c, r':VARCHAR(4) CHARACTER SET "UTF-16LE", _UTF-16LE'INSERT':VARCHAR(13) CHARACTER SET "UTF-16LE", _UTF-16LE'ub':VARCHAR(4) CHARACTER SET "UTF-16LE", _UTF-16LE'UPDATE_BEFORE':VARCHAR(13) CHARACTER SET "UTF-16LE", _UTF-16LE'ua':VARCHAR(4) CHARACTER SET "UTF-16LE", _UTF-16LE'UPDATE_AFTER':VARCHAR(13) CHARACTER SET "UTF-16LE", _UTF-16LE'd':VARCHAR(4) CHARACTER SET "UTF-16LE", _UTF-16LE'DELETE':VARCHAR(13) CHARACTER SET "UTF-16LE"), DEFAULT(), DEFAULT())], rowType=[RecordType(INTEGER id, VARCHAR(2147483647) name)])
+ +- LogicalProject(id=[$0], __op=[$1], name=[$2])
+ +- LogicalTableScan(table=[[default_catalog, default_database, cdc_stream]])
+]]>
+ </Resource>
+ <Resource name="optimized rel plan">
+ <![CDATA[
+ProcessTableFunction(invocation=[FROM_CHANGELOG(TABLE(#0), DESCRIPTOR(_UTF-16LE'__op'), MAP(_UTF-16LE'c, r':VARCHAR(4) CHARACTER SET "UTF-16LE", _UTF-16LE'INSERT':VARCHAR(13) CHARACTER SET "UTF-16LE", _UTF-16LE'ub':VARCHAR(4) CHARACTER SET "UTF-16LE", _UTF-16LE'UPDATE_BEFORE':VARCHAR(13) CHARACTER SET "UTF-16LE", _UTF-16LE'ua':VARCHAR(4) CHARACTER SET "UTF-16LE", _UTF-16LE'UPDATE_AFTER':VARCHAR(13) CHARACTER SET "UTF-16LE", _UTF-16LE'd':VARCHAR(4) CHARACTER SET "UTF-16LE", _UTF-16LE'DELETE':VARCHAR(13) CHARACTER SET "UTF-16LE"), DEFAULT(), DEFAULT())], uid=[null], select=[id,name], rowType=[RecordType(INTEGER id, VARCHAR(2147483647) name)], changelogMode=[I,UB,UA,D])
++- TableSourceScan(table=[[default_catalog, default_database, cdc_stream]], fields=[id, __op, name], changelogMode=[I])
+]]>
+ </Resource>
+ </TestCase>
+ <TestCase name="testInsertOnlySource">
+ <Resource name="sql">
+ <![CDATA[SELECT * FROM FROM_CHANGELOG(input => TABLE cdc_stream)]]>
+ </Resource>
+ <Resource name="ast">
+ <![CDATA[
+LogicalProject(id=[$0], name=[$1])
++- LogicalTableFunctionScan(invocation=[FROM_CHANGELOG(TABLE(#0), DEFAULT(), DEFAULT(), DEFAULT(), DEFAULT())], rowType=[RecordType(INTEGER id, VARCHAR(2147483647) name)])
+ +- LogicalProject(id=[$0], op=[$1], name=[$2])
+ +- LogicalTableScan(table=[[default_catalog, default_database, cdc_stream]])
+]]>
+ </Resource>
+ <Resource name="optimized rel plan">
+ <![CDATA[
+ProcessTableFunction(invocation=[FROM_CHANGELOG(TABLE(#0), DEFAULT(), DEFAULT(), DEFAULT(), DEFAULT())], uid=[null], select=[id,name], rowType=[RecordType(INTEGER id, VARCHAR(2147483647) name)], changelogMode=[I,UB,UA,D])
++- TableSourceScan(table=[[default_catalog, default_database, cdc_stream]], fields=[id, op, name], changelogMode=[I])
+]]>
+ </Resource>
+ </TestCase>
+</Root>
diff --git a/flink-table/flink-table-planner/src/test/resources/org/apache/flink/table/planner/plan/stream/sql/ProcessTableFunctionTest.xml b/flink-table/flink-table-planner/src/test/resources/org/apache/flink/table/planner/plan/stream/sql/ProcessTableFunctionTest.xml
--- a/flink-table/flink-table-planner/src/test/resources/org/apache/flink/table/planner/plan/stream/sql/ProcessTableFunctionTest.xml
+++ b/flink-table/flink-table-planner/src/test/resources/org/apache/flink/table/planner/plan/stream/sql/ProcessTableFunctionTest.xml
@@ -146,6 +146,26 @@ Calc(select=[out, rowtime])
+- Exchange(distribution=[hash[name]])
+- WatermarkAssigner(rowtime=[ts], watermark=[ts])
+- TableSourceScan(table=[[default_catalog, default_database, t_watermarked]], fields=[name, score, ts])
+]]>
+ </Resource>
+ </TestCase>
+ <TestCase name="testRetractModeWithRowSemantics">
+ <Resource name="sql">
+ <![CDATA[SELECT * FROM f(r => TABLE t)]]>
+ </Resource>
+ <Resource name="ast">
+ <![CDATA[
+LogicalProject(name=[$0], count=[$1], mode=[$2])
++- LogicalTableFunctionScan(invocation=[f(TABLE(#0), DEFAULT(), DEFAULT())], rowType=[RecordType(VARCHAR(2147483647) name, BIGINT count, VARCHAR(2147483647) mode)])
+ +- LogicalProject(name=[$0], score=[$1])
+ +- LogicalProject(name=[$0], score=[$1])
+ +- LogicalValues(tuples=[[{ _UTF-16LE'Bob', 12 }, { _UTF-16LE'Alice', 42 }]])
+]]>
+ </Resource>
+ <Resource name="optimized rel plan">
+ <![CDATA[
+ProcessTableFunction(invocation=[f(TABLE(#0), DEFAULT(), DEFAULT())], uid=[null], select=[name,count,mode], rowType=[RecordType(VARCHAR(2147483647) name, BIGINT count, VARCHAR(2147483647) mode)], changelogMode=[I,UB,UA,D])
++- Values(tuples=[[{ _UTF-16LE'Bob', 12 }, { _UTF-16LE'Alice', 42 }]], changelogMode=[I])
]]>
</Resource>
</TestCase>
|