This is an automated email from the ASF dual-hosted git repository.

fhueske pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/flink.git


The following commit(s) were added to refs/heads/master by this push:
     new 3f5129b8767 [FLINK-39784][table] Forbid SNAPSHOT outside a LATERAL 
context (#28674)
3f5129b8767 is described below

commit 3f5129b87672fe91d4725d019b3e0b833c45ccc1
Author: Fabian Hueske <[email protected]>
AuthorDate: Wed Jul 15 10:15:19 2026 +0200

    [FLINK-39784][table] Forbid SNAPSHOT outside a LATERAL context (#28674)
    
    Adds ForbidSnapshotOutsideLateralRule, which rejects any SNAPSHOT scan
    that survives the LATERAL SNAPSHOT rewrite with a clear message instead
    of failing later in the generic PTF translation.
    
    Generated-By: Claude Opus 4.8 (1M context)
---
 .../logical/ForbidSnapshotOutsideLateralRule.java  | 85 ++++++++++++++++++++++
 .../planner/plan/rules/FlinkStreamRuleSets.scala   |  3 +
 .../plan/stream/sql/SnapshotTableFunctionTest.java | 12 +--
 3 files changed, 95 insertions(+), 5 deletions(-)

diff --git 
a/flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/rules/logical/ForbidSnapshotOutsideLateralRule.java
 
b/flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/rules/logical/ForbidSnapshotOutsideLateralRule.java
new file mode 100644
index 00000000000..95db7221528
--- /dev/null
+++ 
b/flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/rules/logical/ForbidSnapshotOutsideLateralRule.java
@@ -0,0 +1,85 @@
+/*
+ * 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.rules.logical;
+
+import org.apache.flink.table.api.ValidationException;
+import 
org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalTableFunctionScan;
+import org.apache.flink.table.planner.plan.utils.LateralSnapshotJoinUtil;
+
+import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.plan.RelRule;
+import org.apache.calcite.rex.RexCall;
+import org.immutables.value.Value;
+
+/**
+ * Rejects any {@link FlinkLogicalTableFunctionScan} that is still backed by 
the built-in {@code
+ * SNAPSHOT} function, with a clear error message.
+ *
+ * <p>{@code SNAPSHOT} is a planner placeholder that is only valid as the 
build side of a {@code
+ * LATERAL} join, where {@link LogicalJoinToLateralSnapshotJoinRule} rewrites 
the surrounding join
+ * into a dedicated {@link
+ * 
org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalLateralSnapshotJoin}
 and removes
+ * the SNAPSHOT scan. This rule must therefore run <em>after</em> that rewrite 
(see {@code
+ * FlinkStreamRuleSets.LOGICAL_REWRITE}).
+ */
[email protected]
+public class ForbidSnapshotOutsideLateralRule
+        extends 
RelRule<ForbidSnapshotOutsideLateralRule.ForbidSnapshotOutsideLateralRuleConfig>
 {
+
+    public static final ForbidSnapshotOutsideLateralRule INSTANCE =
+            
ForbidSnapshotOutsideLateralRule.ForbidSnapshotOutsideLateralRuleConfig.DEFAULT
+                    .toRule();
+
+    private 
ForbidSnapshotOutsideLateralRule(ForbidSnapshotOutsideLateralRuleConfig config) 
{
+        super(config);
+    }
+
+    @Override
+    public boolean matches(RelOptRuleCall call) {
+        final FlinkLogicalTableFunctionScan scan = call.rel(0);
+        return scan.getCall() instanceof RexCall
+                && LateralSnapshotJoinUtil.isSnapshotCall((RexCall) 
scan.getCall());
+    }
+
+    @Override
+    public void onMatch(RelOptRuleCall call) {
+        throw new ValidationException(
+                "The SNAPSHOT function can only be used as the build side 
(right-hand side) of a "
+                        + "LATERAL join. It cannot be used as a standalone 
table function or "
+                        + "outside of a LATERAL context.");
+    }
+
+    /** Rule configuration. */
+    @Value.Immutable(singleton = false)
+    public interface ForbidSnapshotOutsideLateralRuleConfig extends 
RelRule.Config {
+
+        
ForbidSnapshotOutsideLateralRule.ForbidSnapshotOutsideLateralRuleConfig DEFAULT 
=
+                
ImmutableForbidSnapshotOutsideLateralRule.ForbidSnapshotOutsideLateralRuleConfig
+                        .builder()
+                        .build()
+                        .withOperandSupplier(
+                                b0 -> 
b0.operand(FlinkLogicalTableFunctionScan.class).anyInputs())
+                        .withDescription("ForbidSnapshotOutsideLateralRule");
+
+        @Override
+        default ForbidSnapshotOutsideLateralRule toRule() {
+            return new ForbidSnapshotOutsideLateralRule(this);
+        }
+    }
+}
diff --git 
a/flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/planner/plan/rules/FlinkStreamRuleSets.scala
 
b/flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/planner/plan/rules/FlinkStreamRuleSets.scala
index 0617400e4ae..de637d07a24 100644
--- 
a/flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/planner/plan/rules/FlinkStreamRuleSets.scala
+++ 
b/flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/planner/plan/rules/FlinkStreamRuleSets.scala
@@ -406,6 +406,9 @@ object FlinkStreamRuleSets {
     // Rewrites a join over a SNAPSHOT table function call into a dedicated
     // FlinkLogicalLateralSnapshotJoin for the LATERAL SNAPSHOT operator.
     LogicalJoinToLateralSnapshotJoinRule.INSTANCE,
+    // Rejects SNAPSHOT scans that survived the rewrite above, i.e. SNAPSHOT 
calls used outside a
+    // LATERAL context. Must run after LogicalJoinToLateralSnapshotJoinRule.
+    ForbidSnapshotOutsideLateralRule.INSTANCE,
     // Avoids accessing a field from the result (condition).
     PythonCalcSplitRule.SPLIT_CONDITION_REX_FIELD,
     // Avoids accessing a field from the result (projection).
diff --git 
a/flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/plan/stream/sql/SnapshotTableFunctionTest.java
 
b/flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/plan/stream/sql/SnapshotTableFunctionTest.java
index 12511c4ae49..6d254b220b8 100644
--- 
a/flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/plan/stream/sql/SnapshotTableFunctionTest.java
+++ 
b/flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/plan/stream/sql/SnapshotTableFunctionTest.java
@@ -19,6 +19,7 @@
 package org.apache.flink.table.planner.plan.stream.sql;
 
 import org.apache.flink.table.api.TableConfig;
+import org.apache.flink.table.api.ValidationException;
 import org.apache.flink.table.planner.utils.TableTestBase;
 import org.apache.flink.table.planner.utils.TableTestUtil;
 
@@ -153,13 +154,14 @@ public class SnapshotTableFunctionTest extends 
TableTestBase {
 
     @Test
     void testFromContextRejectedOutsideLateral() {
-        // SNAPSHOT used outside a LATERAL context is not rewritten by the 
LATERAL SNAPSHOT rule and
-        // reaches the regular PTF physical rule. Because SNAPSHOT disables 
system arguments, that
-        // rule rejects it. FLINK-39784 will replace this with a clearer 
message
-        // ("SNAPSHOT can only be used inside a LATERAL clause").
+        // SNAPSHOT used outside a LATERAL context is not rewritten by the 
LATERAL SNAPSHOT rule.
+        // ForbidSnapshotOutsideLateralRule intercepts the surviving SNAPSHOT 
scan and rejects it
+        // with a clear message before it reaches the generic PTF translation.
         assertThatThrownBy(() -> util.verifyRelPlan("SELECT * FROM 
SNAPSHOT(input => TABLE Rates)"))
                 .satisfies(
                         anyCauseMatches(
-                                "Disabling system arguments is not supported 
for user-defined PTF."));
+                                ValidationException.class,
+                                "The SNAPSHOT function can only be used as the 
build side "
+                                        + "(right-hand side) of a LATERAL 
join"));
     }
 }

Reply via email to