JackieTien97 commented on code in PR #19155:
URL: https://github.com/apache/iotdb/pull/19155#discussion_r4510025500


##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/planner/optimizations/ParallelizeGrouping.java:
##########
@@ -117,25 +119,33 @@ public PlanNode visitUnion(UnionNode node, Context 
context) {
               .collect(Collectors.toList()));
     }
 
-    /** We need to make sure: context#partitionKey can match the prefix of 
childOrderSchema */
+    /**
+     * We need to make sure: context#partitionKey can be satisfied by the 
prefix of childOrder.
+     *
+     * <p>The prefix is compared as a column set: partition columns are 
identified by their values,
+     * so a different writing order still clusters rows into the same 
partitions. This lets nested
+     * set-semantic table functions reuse their input clustering when the same 
PARTITION BY columns
+     * are listed in a different order.
+     *
+     * <p>The ORDER BY part of the requirement needs no check here. A group 
node sorts within every
+     * partition run by its full ordering scheme, so the input only has to be 
clustered; whatever
+     * order rows have inside a partition is re-established locally by the 
group's own sort.
+     */
     private void checkPrefixMatch(Context context, List<Symbol> childOrder) {
       if (context.canSkip()) {
         return;
       }
-      if (context.partitionKeyCount > childOrder.size()) {
+      int partitionKeyCount = context.partitionKeyCount;
+      if (partitionKeyCount > childOrder.size()) {
         context.canOptimized = TO_SORT;
         return;
       }
-      OrderingScheme prefix = context.sortKey;
-      for (int i = 0; i < context.partitionKeyCount; i++) {
-        Symbol lhs = prefix.getOrderBy().get(i);
-        Symbol rhs = childOrder.get(i);
-        if (!lhs.equals(rhs)) {
-          context.canOptimized = TO_SORT;
-          return;
-        }
-      }
-      context.canOptimized = ENABLE_PARALLEL;
+      List<Symbol> requiredPrefix = context.sortKey.getOrderBy().subList(0, 
partitionKeyCount);
+      List<Symbol> actualPrefix = childOrder.subList(0, partitionKeyCount);

Review Comment:
   `childOrder` is not always a sort order. `visitAggregation` passes 
`node.getGroupingKeys()` here, and a hash aggregation's output is not clustered 
by a prefix of those keys. With this change, `GROUP BY s2, s1, s3` with 
`PARTITION BY (s1, s2)` keeps the `GroupNode`, where the base commit used a 
`Sort`. `TableStreamSortOperator` then flushes non-contiguous input in batches, 
so one partition is cut into several runs.
   
   Repro with one device and 6000 rows, where `(s1, s2)` cycles through `(0, 
0)`, `(1, 1)`, `(2, 2)` and `s3` is unique:
   
   ```sql
   SELECT s1, s2, window_start, window_end, count(*) AS cnt FROM SESSION(DATA 
=> (
       SELECT s1, s2, s3, max(time) AS time FROM t2 WHERE d = 'd1' GROUP BY s2, 
s1, s3
   ) PARTITION BY (s1, s2) ORDER BY time, GAP => 100000ms)
   GROUP BY s1, s2, window_start, window_end ORDER BY s1, s2, window_start;
   ```
   
   The base returns 1 window per key (3 rows). This PR returns 6 windows per 
key (18 rows). With several regions, `groupingInvalidatedByCollect` falls back 
to a `Sort`, so this only happens when there is no `Collect` under the 
aggregation. The suggestion on lines 145-148 also fixes this case.



##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/planner/optimizations/ParallelizeGrouping.java:
##########
@@ -117,25 +119,33 @@ public PlanNode visitUnion(UnionNode node, Context 
context) {
               .collect(Collectors.toList()));
     }
 
-    /** We need to make sure: context#partitionKey can match the prefix of 
childOrderSchema */
+    /**
+     * We need to make sure: context#partitionKey can be satisfied by the 
prefix of childOrder.
+     *
+     * <p>The prefix is compared as a column set: partition columns are 
identified by their values,
+     * so a different writing order still clusters rows into the same 
partitions. This lets nested
+     * set-semantic table functions reuse their input clustering when the same 
PARTITION BY columns
+     * are listed in a different order.
+     *
+     * <p>The ORDER BY part of the requirement needs no check here. A group 
node sorts within every
+     * partition run by its full ordering scheme, so the input only has to be 
clustered; whatever
+     * order rows have inside a partition is re-established locally by the 
group's own sort.
+     */
     private void checkPrefixMatch(Context context, List<Symbol> childOrder) {
       if (context.canSkip()) {
         return;
       }
-      if (context.partitionKeyCount > childOrder.size()) {
+      int partitionKeyCount = context.partitionKeyCount;
+      if (partitionKeyCount > childOrder.size()) {
         context.canOptimized = TO_SORT;
         return;
       }
-      OrderingScheme prefix = context.sortKey;
-      for (int i = 0; i < context.partitionKeyCount; i++) {
-        Symbol lhs = prefix.getOrderBy().get(i);
-        Symbol rhs = childOrder.get(i);
-        if (!lhs.equals(rhs)) {
-          context.canOptimized = TO_SORT;
-          return;
-        }
-      }
-      context.canOptimized = ENABLE_PARALLEL;
+      List<Symbol> requiredPrefix = context.sortKey.getOrderBy().subList(0, 
partitionKeyCount);
+      List<Symbol> actualPrefix = childOrder.subList(0, partitionKeyCount);
+      context.canOptimized =
+          
ImmutableSet.copyOf(requiredPrefix).equals(ImmutableSet.copyOf(actualPrefix))
+              ? ENABLE_PARALLEL
+              : TO_SORT;

Review Comment:
   This accepts more than "the same PARTITION BY columns in a different order". 
Only the first `partitionKeyCount` child keys are compared, so outer `(b, a)` 
over inner `(a, b, c)` also becomes `ENABLE_PARALLEL`.
   
   That outer partition can span DataRegions. 
`TableDistributedPlanGenerator.visitGroup` still returns one node per child, 
and `canSplitTableFunctionProcessor` then splits the outer function per region, 
so each region computes its own copy of the partition. `visitWindowFunction`, 
`visitRowNumber`, `visitTopKRanking` and `visitPatternRecognition` split in the 
same way. The base commit plans this query with a single outer function above a 
`MergeSort`.
   
   Repro with 4 DataRegions and 40 devices (`a = 'A'`, `b = 'B'`, `c = 
'c00'..'c39'`, one row each at time 1..40):
   
   ```sql
   CREATE TABLE t(a STRING TAG, b STRING TAG, c STRING TAG, s1 DOUBLE FIELD);
   
   SELECT window_start, window_end, count(*) AS cnt FROM SESSION(DATA => (
       SELECT window_start AS inner_start, window_end AS inner_end, time, a, b, 
c, s1
       FROM SESSION(DATA => t PARTITION BY (a, b, c) ORDER BY time, GAP => 1ms)
   ) PARTITION BY (b, a) ORDER BY time, GAP => 100ms)
   GROUP BY window_start, window_end ORDER BY window_start;
   ```
   
   Expected, and what the base returns: one window `[1ms, 40ms]` with `cnt = 
40`. This PR returns 4 windows with `cnt = 10` each.
   
   I'd apply the set comparison only when the child exposes exactly the 
partition keys, and keep the positional rule otherwise. The Javadoc on lines 
125-128 needs the same restriction.
   
   ```suggestion
         boolean sameKeySet =
             partitionKeyCount == childOrder.size()
                 && 
ImmutableSet.copyOf(requiredPrefix).equals(ImmutableSet.copyOf(actualPrefix));
         context.canOptimized =
             sameKeySet || requiredPrefix.equals(actualPrefix) ? 
ENABLE_PARALLEL : TO_SORT;
   ```



##########
iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/relational/analyzer/TableFunctionTest.java:
##########
@@ -226,6 +227,199 @@ public void testSimpleSetSemantic() {
     assertPlan(planTester.getFragmentPlan(5), tableScan);
   }
 
+  private static final String NESTED_SESSION_SQL =
+      "SELECT * FROM SESSION(DATA => (SELECT * FROM SESSION(DATA => table1 "
+          + "PARTITION BY (tag1, tag2, tag3) ORDER BY time, GAP => 1m)) "
+          + "PARTITION BY (tag3, tag2, tag1) ORDER BY time, GAP => 2m)";
+
+  private static Consumer<TableFunctionProcessorMatcher.Builder> 
sessionMatcher(
+      String windowStart, String windowEnd, long gap) {
+    return builder ->
+        builder
+            .name("session")
+            .properOutputs(windowStart, windowEnd)
+            .requiredSymbols("time")
+            .handle(new MapTableFunctionHandle.Builder().addProperty("GAP", 
gap).build());
+  }
+
+  private static PlanMatchPattern scanAllColumns() {
+    return tableScan(
+        "testdb.table1",
+        ImmutableList.of("time", "tag1", "tag2", "tag3", "attr1", "attr2", 
"s1", "s2", "s3"),
+        ImmutableSet.of("time", "tag1", "tag2", "tag3", "attr1", "attr2", 
"s1", "s2", "s3"));
+  }
+
+  private static List<PlanMatchPattern.Ordering> orderBy(String... fields) {
+    ImmutableList.Builder<PlanMatchPattern.Ordering> result = 
ImmutableList.builder();
+    for (String field : fields) {
+      result.add(sort(field, ASCENDING, LAST));
+    }
+    return result.build();
+  }
+
+  @Test
+  public void testNestedSetSemanticPermutedPartitionsKeepBothGroups() {
+    PlanTester planTester = new PlanTester();
+    LogicalQueryPlan logicalQueryPlan = 
planTester.createPlan(NESTED_SESSION_SQL);
+    Consumer<TableFunctionProcessorMatcher.Builder> outer =
+        sessionMatcher("window_start", "window_end", 120_000L);
+    Consumer<TableFunctionProcessorMatcher.Builder> inner =
+        sessionMatcher("window_start_0", "window_end_1", 60_000L);
+    // PARTITION BY columns are the same set in a different order and ORDER BY 
matches, so both
+    // grouping nodes survive: the outer keeps its declared order, the inner 
keeps its own.
+    assertPlan(
+        logicalQueryPlan,
+        anyTree(
+            tableFunctionProcessor(
+                outer,
+                group(
+                    orderBy("tag3", "tag2", "tag1", "time"),
+                    3,
+                    tableFunctionProcessor(
+                        inner,
+                        group(orderBy("tag1", "tag2", "tag3", "time"), 3, 
scanAllColumns()))))));
+    // Both levels are split per region; only regions with several devices 
need a merged stream.
+    assertPlan(planTester.getFragmentPlan(0), output(collect(exchange(), 
exchange(), exchange())));
+    assertPlan(
+        planTester.getFragmentPlan(1),
+        tableFunctionProcessor(outer, tableFunctionProcessor(inner, 
scanAllColumns())));
+    assertPlan(
+        planTester.getFragmentPlan(2),
+        tableFunctionProcessor(outer, tableFunctionProcessor(inner, 
scanAllColumns())));
+    assertPlan(
+        planTester.getFragmentPlan(3),
+        tableFunctionProcessor(
+            outer,
+            tableFunctionProcessor(
+                inner,
+                mergeSort(orderBy("tag3", "tag2", "tag1", "time"), exchange(), 
exchange()))));
+    assertPlan(planTester.getFragmentPlan(4), scanAllColumns());
+    assertPlan(planTester.getFragmentPlan(5), scanAllColumns());
+  }
+
+  @Test
+  public void testNestedSetSemanticDifferentOrderByKeepsOuterGroup() {
+    PlanTester planTester = new PlanTester();
+    String sql =
+        "SELECT * FROM SESSION(DATA => (SELECT * FROM SESSION(DATA => table1 "
+            + "PARTITION BY (tag1, tag2, tag3) ORDER BY time, GAP => 1m)) "
+            + "PARTITION BY (tag3, tag2, tag1) ORDER BY s1, GAP => 2m)";
+    LogicalQueryPlan logicalQueryPlan = planTester.createPlan(sql);
+    Consumer<TableFunctionProcessorMatcher.Builder> outer =
+        sessionMatcher("window_start", "window_end", 120_000L);
+    Consumer<TableFunctionProcessorMatcher.Builder> inner =
+        sessionMatcher("window_start_0", "window_end_1", 60_000L);
+    // Only the partition prefix has to be clustered: the outer group 
re-establishes its s1 order
+    // inside every partition, so a different ORDER BY does not degrade it to 
a full sort.
+    assertPlan(
+        logicalQueryPlan,
+        anyTree(
+            tableFunctionProcessor(
+                outer,
+                group(
+                    orderBy("tag3", "tag2", "tag1", "s1"),
+                    3,
+                    tableFunctionProcessor(
+                        inner,
+                        group(orderBy("tag1", "tag2", "tag3", "time"), 3, 
scanAllColumns()))))));
+    // Both levels are still split per region.
+    assertPlan(planTester.getFragmentPlan(0), output(collect(exchange(), 
exchange(), exchange())));
+  }
+
+  @Test
+  public void testNestedSetSemanticGlobalOrderByDeclaredOrder() {
+    PlanTester planTester = new PlanTester();
+    LogicalQueryPlan logicalQueryPlan =
+        planTester.createPlan(NESTED_SESSION_SQL + " ORDER BY tag3, tag2, 
tag1, time");
+    Consumer<TableFunctionProcessorMatcher.Builder> outer =
+        sessionMatcher("window_start", "window_end", 120_000L);
+    Consumer<TableFunctionProcessorMatcher.Builder> inner =
+        sessionMatcher("window_start_0", "window_end_1", 60_000L);
+    // The requested global order equals the outer declared order, so no 
per-region sort is needed
+    // and the cross-region merge uses exactly that order.
+    assertPlan(
+        planTester.getFragmentPlan(0),
+        output(
+            mergeSort(
+                orderBy("tag3", "tag2", "tag1", "time"), exchange(), 
exchange(), exchange())));
+    assertPlan(
+        planTester.getFragmentPlan(1),
+        tableFunctionProcessor(outer, tableFunctionProcessor(inner, 
scanAllColumns())));
+    assertPlan(
+        planTester.getFragmentPlan(3),
+        tableFunctionProcessor(
+            outer,
+            tableFunctionProcessor(
+                inner,
+                mergeSort(orderBy("tag3", "tag2", "tag1", "time"), exchange(), 
exchange()))));
+  }
+
+  @Test
+  public void testNestedSetSemanticGlobalOrderByPermutesPartitionColumns() {
+    PlanTester planTester = new PlanTester();
+    // The global order permutes the partition columns again; the distributed 
planner must prove it
+    // by rewriting grouping/device comparators instead of reusing the TVF 
declared order.
+    LogicalQueryPlan logicalQueryPlan =
+        planTester.createPlan(NESTED_SESSION_SQL + " ORDER BY tag2, tag3, 
tag1, time");
+    Consumer<TableFunctionProcessorMatcher.Builder> outer =
+        sessionMatcher("window_start", "window_end", 120_000L);
+    Consumer<TableFunctionProcessorMatcher.Builder> inner =
+        sessionMatcher("window_start_0", "window_end_1", 60_000L);
+    assertPlan(
+        planTester.getFragmentPlan(0),
+        output(
+            mergeSort(
+                orderBy("tag2", "tag3", "tag1", "time"), exchange(), 
exchange(), exchange())));
+    assertPlan(
+        planTester.getFragmentPlan(1),
+        tableFunctionProcessor(outer, tableFunctionProcessor(inner, 
scanAllColumns())));
+    assertPlan(
+        planTester.getFragmentPlan(3),
+        tableFunctionProcessor(
+            outer,
+            tableFunctionProcessor(
+                inner,
+                mergeSort(orderBy("tag2", "tag3", "tag1", "time"), exchange(), 
exchange()))));
+  }
+
+  @Test
+  public void testThreeLevelNestedSetSemanticPermutedPartitions() {
+    PlanTester planTester = new PlanTester();
+    String sql =
+        "SELECT * FROM SESSION(DATA => (SELECT * FROM SESSION(DATA => (SELECT 
* FROM SESSION(DATA => table1 "
+            + "PARTITION BY (tag1, tag2, tag3) ORDER BY time, GAP => 1m)) "
+            + "PARTITION BY (tag3, tag2, tag1) ORDER BY time, GAP => 2m)) "
+            + "PARTITION BY (tag2, tag1, tag3) ORDER BY time, GAP => 3m)";
+    LogicalQueryPlan logicalQueryPlan = planTester.createPlan(sql);
+    Consumer<TableFunctionProcessorMatcher.Builder> outerMost =
+        sessionMatcher("window_start", "window_end", 180_000L);
+    Consumer<TableFunctionProcessorMatcher.Builder> middle =
+        sessionMatcher("window_start_0", "window_end_1", 120_000L);
+    Consumer<TableFunctionProcessorMatcher.Builder> inner =
+        sessionMatcher("window_start_2", "window_end_3", 60_000L);
+    // Every level lists the same PARTITION BY columns in a different order, 
so all three groups
+    // survive and are executed in parallel per region.

Review Comment:
   The comment says all three groups are executed in parallel per region, but 
this test only asserts the logical plan. If the distributed plan went back to a 
single stream at the root, it would still pass. Could you add `getFragmentPlan` 
assertions?



##########
integration-test/src/test/java/org/apache/iotdb/relational/it/db/it/IoTDBWindowTVFIT.java:
##########
@@ -251,6 +251,100 @@ public void testSessionFunction() {
         DATABASE_NAME);
   }
 
+  @Test
+  public void testNestedSessionWithPermutedPartitionColumns() {
+    String[] expectedHeader =
+        new String[] {
+          "window_start",
+          "window_end",
+          "inner_start",
+          "inner_end",
+          "time",
+          "factory_id",
+          "device_id",
+          "s1"
+        };
+    String[] retArray =
+        new String[] {
+          
"1970-01-01T00:00:00.001Z,1970-01-01T00:00:00.001Z,1970-01-01T00:00:00.001Z,1970-01-01T00:00:00.009Z,1970-01-01T00:00:00.001Z,F1,device_1,10.0,",
+          
"1970-01-01T00:00:00.005Z,1970-01-01T00:00:00.005Z,1970-01-01T00:00:00.001Z,1970-01-01T00:00:00.009Z,1970-01-01T00:00:00.005Z,F1,device_1,15.0,",
+          
"1970-01-01T00:00:00.009Z,1970-01-01T00:00:00.009Z,1970-01-01T00:00:00.001Z,1970-01-01T00:00:00.009Z,1970-01-01T00:00:00.009Z,F1,device_1,5.0,",
+          
"1970-01-01T00:00:00.002Z,1970-01-01T00:00:00.002Z,1970-01-01T00:00:00.002Z,1970-01-01T00:00:00.002Z,1970-01-01T00:00:00.002Z,F1,device_2,20.0,",
+          
"1970-01-01T00:00:00.011Z,1970-01-01T00:00:00.011Z,1970-01-01T00:00:00.011Z,1970-01-01T00:00:00.011Z,1970-01-01T00:00:00.011Z,F1,device_2,25.0,",
+          
"1970-01-01T00:00:00.003Z,1970-01-01T00:00:00.003Z,1970-01-01T00:00:00.003Z,1970-01-01T00:00:00.003Z,1970-01-01T00:00:00.003Z,F2,device_1,30.0,"
+        };
+    // The outer PARTITION BY lists the same columns as the inner one in a 
different order; the
+    // result must be identical to the same-order variant (next test).
+    String sql =
+        "SELECT window_start, window_end, inner_start, inner_end, time, 
factory_id, device_id, s1 "
+            + "FROM SESSION(DATA => ("
+            + "    SELECT window_start AS inner_start, window_end AS 
inner_end, time, factory_id, device_id, s1 "
+            + "    FROM SESSION(DATA => table5 PARTITION BY (factory_id, 
device_id) ORDER BY time, GAP => 5ms)"
+            + ") PARTITION BY (device_id, factory_id) ORDER BY time, GAP => 
3ms) "
+            + "ORDER BY factory_id, device_id, time";
+    tableResultSetEqualTest(sql, expectedHeader, retArray, DATABASE_NAME);
+  }
+
+  @Test
+  public void testNestedSessionWithSamePartitionColumnOrder() {
+    String[] expectedHeader =
+        new String[] {
+          "window_start",
+          "window_end",
+          "inner_start",
+          "inner_end",
+          "time",
+          "factory_id",
+          "device_id",
+          "s1"
+        };
+    String[] retArray =
+        new String[] {
+          
"1970-01-01T00:00:00.001Z,1970-01-01T00:00:00.001Z,1970-01-01T00:00:00.001Z,1970-01-01T00:00:00.009Z,1970-01-01T00:00:00.001Z,F1,device_1,10.0,",
+          
"1970-01-01T00:00:00.005Z,1970-01-01T00:00:00.005Z,1970-01-01T00:00:00.001Z,1970-01-01T00:00:00.009Z,1970-01-01T00:00:00.005Z,F1,device_1,15.0,",
+          
"1970-01-01T00:00:00.009Z,1970-01-01T00:00:00.009Z,1970-01-01T00:00:00.001Z,1970-01-01T00:00:00.009Z,1970-01-01T00:00:00.009Z,F1,device_1,5.0,",
+          
"1970-01-01T00:00:00.002Z,1970-01-01T00:00:00.002Z,1970-01-01T00:00:00.002Z,1970-01-01T00:00:00.002Z,1970-01-01T00:00:00.002Z,F1,device_2,20.0,",
+          
"1970-01-01T00:00:00.011Z,1970-01-01T00:00:00.011Z,1970-01-01T00:00:00.011Z,1970-01-01T00:00:00.011Z,1970-01-01T00:00:00.011Z,F1,device_2,25.0,",
+          
"1970-01-01T00:00:00.003Z,1970-01-01T00:00:00.003Z,1970-01-01T00:00:00.003Z,1970-01-01T00:00:00.003Z,1970-01-01T00:00:00.003Z,F2,device_1,30.0,"
+        };
+    // Reference case: both levels list the PARTITION BY columns in the same 
order.
+    String sql =
+        "SELECT window_start, window_end, inner_start, inner_end, time, 
factory_id, device_id, s1 "
+            + "FROM SESSION(DATA => ("
+            + "    SELECT window_start AS inner_start, window_end AS 
inner_end, time, factory_id, device_id, s1 "
+            + "    FROM SESSION(DATA => table5 PARTITION BY (factory_id, 
device_id) ORDER BY time, GAP => 5ms)"
+            + ") PARTITION BY (factory_id, device_id) ORDER BY time, GAP => 
3ms) "
+            + "ORDER BY factory_id, device_id, time";
+    tableResultSetEqualTest(sql, expectedHeader, retArray, DATABASE_NAME);
+  }
+
+  @Test
+  public void testThreeLevelNestedSessionWithPermutedPartitionColumns() {
+    String[] expectedHeader =
+        new String[] {"window_start", "window_end", "time", "factory_id", 
"device_id", "s1"};
+    String[] retArray =
+        new String[] {
+          
"1970-01-01T00:00:00.001Z,1970-01-01T00:00:00.009Z,1970-01-01T00:00:00.001Z,F1,device_1,10.0,",
+          
"1970-01-01T00:00:00.001Z,1970-01-01T00:00:00.009Z,1970-01-01T00:00:00.005Z,F1,device_1,15.0,",
+          
"1970-01-01T00:00:00.001Z,1970-01-01T00:00:00.009Z,1970-01-01T00:00:00.009Z,F1,device_1,5.0,",
+          
"1970-01-01T00:00:00.002Z,1970-01-01T00:00:00.002Z,1970-01-01T00:00:00.002Z,F1,device_2,20.0,",
+          
"1970-01-01T00:00:00.011Z,1970-01-01T00:00:00.011Z,1970-01-01T00:00:00.011Z,F1,device_2,25.0,",
+          
"1970-01-01T00:00:00.003Z,1970-01-01T00:00:00.003Z,1970-01-01T00:00:00.003Z,F2,device_1,30.0,"
+        };
+    // Three levels list the same PARTITION BY columns in three different 
orders.

Review Comment:
   Nit: `table5` has only two tags, so there are only two possible orders, and 
the innermost and outermost levels both use `(factory_id, device_id)`.
   
   ```suggestion
       // Adjacent levels list the same PARTITION BY columns in different 
orders.
   ```



##########
iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/relational/analyzer/TableFunctionTest.java:
##########
@@ -226,6 +227,199 @@ public void testSimpleSetSemantic() {
     assertPlan(planTester.getFragmentPlan(5), tableScan);
   }
 
+  private static final String NESTED_SESSION_SQL =
+      "SELECT * FROM SESSION(DATA => (SELECT * FROM SESSION(DATA => table1 "
+          + "PARTITION BY (tag1, tag2, tag3) ORDER BY time, GAP => 1m)) "
+          + "PARTITION BY (tag3, tag2, tag1) ORDER BY time, GAP => 2m)";
+
+  private static Consumer<TableFunctionProcessorMatcher.Builder> 
sessionMatcher(
+      String windowStart, String windowEnd, long gap) {
+    return builder ->
+        builder
+            .name("session")
+            .properOutputs(windowStart, windowEnd)
+            .requiredSymbols("time")
+            .handle(new MapTableFunctionHandle.Builder().addProperty("GAP", 
gap).build());
+  }
+
+  private static PlanMatchPattern scanAllColumns() {
+    return tableScan(
+        "testdb.table1",
+        ImmutableList.of("time", "tag1", "tag2", "tag3", "attr1", "attr2", 
"s1", "s2", "s3"),
+        ImmutableSet.of("time", "tag1", "tag2", "tag3", "attr1", "attr2", 
"s1", "s2", "s3"));
+  }
+
+  private static List<PlanMatchPattern.Ordering> orderBy(String... fields) {
+    ImmutableList.Builder<PlanMatchPattern.Ordering> result = 
ImmutableList.builder();
+    for (String field : fields) {
+      result.add(sort(field, ASCENDING, LAST));
+    }
+    return result.build();
+  }
+
+  @Test
+  public void testNestedSetSemanticPermutedPartitionsKeepBothGroups() {
+    PlanTester planTester = new PlanTester();
+    LogicalQueryPlan logicalQueryPlan = 
planTester.createPlan(NESTED_SESSION_SQL);
+    Consumer<TableFunctionProcessorMatcher.Builder> outer =
+        sessionMatcher("window_start", "window_end", 120_000L);
+    Consumer<TableFunctionProcessorMatcher.Builder> inner =
+        sessionMatcher("window_start_0", "window_end_1", 60_000L);
+    // PARTITION BY columns are the same set in a different order and ORDER BY 
matches, so both
+    // grouping nodes survive: the outer keeps its declared order, the inner 
keeps its own.
+    assertPlan(
+        logicalQueryPlan,
+        anyTree(
+            tableFunctionProcessor(
+                outer,
+                group(
+                    orderBy("tag3", "tag2", "tag1", "time"),
+                    3,
+                    tableFunctionProcessor(
+                        inner,
+                        group(orderBy("tag1", "tag2", "tag3", "time"), 3, 
scanAllColumns()))))));
+    // Both levels are split per region; only regions with several devices 
need a merged stream.
+    assertPlan(planTester.getFragmentPlan(0), output(collect(exchange(), 
exchange(), exchange())));
+    assertPlan(
+        planTester.getFragmentPlan(1),
+        tableFunctionProcessor(outer, tableFunctionProcessor(inner, 
scanAllColumns())));
+    assertPlan(
+        planTester.getFragmentPlan(2),
+        tableFunctionProcessor(outer, tableFunctionProcessor(inner, 
scanAllColumns())));
+    assertPlan(
+        planTester.getFragmentPlan(3),
+        tableFunctionProcessor(
+            outer,
+            tableFunctionProcessor(
+                inner,
+                mergeSort(orderBy("tag3", "tag2", "tag1", "time"), exchange(), 
exchange()))));
+    assertPlan(planTester.getFragmentPlan(4), scanAllColumns());
+    assertPlan(planTester.getFragmentPlan(5), scanAllColumns());
+  }
+
+  @Test
+  public void testNestedSetSemanticDifferentOrderByKeepsOuterGroup() {
+    PlanTester planTester = new PlanTester();
+    String sql =
+        "SELECT * FROM SESSION(DATA => (SELECT * FROM SESSION(DATA => table1 "
+            + "PARTITION BY (tag1, tag2, tag3) ORDER BY time, GAP => 1m)) "
+            + "PARTITION BY (tag3, tag2, tag1) ORDER BY s1, GAP => 2m)";
+    LogicalQueryPlan logicalQueryPlan = planTester.createPlan(sql);
+    Consumer<TableFunctionProcessorMatcher.Builder> outer =
+        sessionMatcher("window_start", "window_end", 120_000L);
+    Consumer<TableFunctionProcessorMatcher.Builder> inner =
+        sessionMatcher("window_start_0", "window_end_1", 60_000L);
+    // Only the partition prefix has to be clustered: the outer group 
re-establishes its s1 order
+    // inside every partition, so a different ORDER BY does not degrade it to 
a full sort.
+    assertPlan(
+        logicalQueryPlan,
+        anyTree(
+            tableFunctionProcessor(
+                outer,
+                group(
+                    orderBy("tag3", "tag2", "tag1", "s1"),
+                    3,
+                    tableFunctionProcessor(
+                        inner,
+                        group(orderBy("tag1", "tag2", "tag3", "time"), 3, 
scanAllColumns()))))));
+    // Both levels are still split per region.
+    assertPlan(planTester.getFragmentPlan(0), output(collect(exchange(), 
exchange(), exchange())));
+  }
+
+  @Test
+  public void testNestedSetSemanticGlobalOrderByDeclaredOrder() {
+    PlanTester planTester = new PlanTester();
+    LogicalQueryPlan logicalQueryPlan =

Review Comment:
   Nit: `logicalQueryPlan` is unused here and on line 362. These two tests only 
assert fragments, so calling `planTester.createPlan(...)` without the 
assignment is enough.



##########
iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/relational/analyzer/TableFunctionTest.java:
##########
@@ -226,6 +227,199 @@ public void testSimpleSetSemantic() {
     assertPlan(planTester.getFragmentPlan(5), tableScan);
   }
 
+  private static final String NESTED_SESSION_SQL =
+      "SELECT * FROM SESSION(DATA => (SELECT * FROM SESSION(DATA => table1 "
+          + "PARTITION BY (tag1, tag2, tag3) ORDER BY time, GAP => 1m)) "
+          + "PARTITION BY (tag3, tag2, tag1) ORDER BY time, GAP => 2m)";
+
+  private static Consumer<TableFunctionProcessorMatcher.Builder> 
sessionMatcher(
+      String windowStart, String windowEnd, long gap) {
+    return builder ->
+        builder
+            .name("session")
+            .properOutputs(windowStart, windowEnd)
+            .requiredSymbols("time")
+            .handle(new MapTableFunctionHandle.Builder().addProperty("GAP", 
gap).build());
+  }
+
+  private static PlanMatchPattern scanAllColumns() {
+    return tableScan(
+        "testdb.table1",
+        ImmutableList.of("time", "tag1", "tag2", "tag3", "attr1", "attr2", 
"s1", "s2", "s3"),
+        ImmutableSet.of("time", "tag1", "tag2", "tag3", "attr1", "attr2", 
"s1", "s2", "s3"));
+  }
+
+  private static List<PlanMatchPattern.Ordering> orderBy(String... fields) {
+    ImmutableList.Builder<PlanMatchPattern.Ordering> result = 
ImmutableList.builder();
+    for (String field : fields) {
+      result.add(sort(field, ASCENDING, LAST));
+    }
+    return result.build();
+  }
+
+  @Test
+  public void testNestedSetSemanticPermutedPartitionsKeepBothGroups() {

Review Comment:
   All the new cases use the complete key set with SESSION, so none of them 
catches the strict-subset regression: the relational plan tests all pass with 
it.
   
   Could you add a negative plan test? `PARTITION BY (tag2, tag1)` over `(tag1, 
tag2, tag3)` should keep the outer function in the root fragment (`Output > 
TableFunctionProcessor > MergeSort`). Since `checkPrefixMatch` is shared, a 
window-function case would help too. An IT with a fixed region count 
(`setDataRegionGroupExtensionPolicy("CUSTOM")` and 
`setDefaultDataRegionGroupNumPerDatabase(4)`) that checks window counts would 
cover the results.



##########
iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/relational/analyzer/TableFunctionTest.java:
##########
@@ -226,6 +227,199 @@ public void testSimpleSetSemantic() {
     assertPlan(planTester.getFragmentPlan(5), tableScan);
   }
 
+  private static final String NESTED_SESSION_SQL =
+      "SELECT * FROM SESSION(DATA => (SELECT * FROM SESSION(DATA => table1 "
+          + "PARTITION BY (tag1, tag2, tag3) ORDER BY time, GAP => 1m)) "
+          + "PARTITION BY (tag3, tag2, tag1) ORDER BY time, GAP => 2m)";
+
+  private static Consumer<TableFunctionProcessorMatcher.Builder> 
sessionMatcher(
+      String windowStart, String windowEnd, long gap) {
+    return builder ->
+        builder
+            .name("session")
+            .properOutputs(windowStart, windowEnd)
+            .requiredSymbols("time")
+            .handle(new MapTableFunctionHandle.Builder().addProperty("GAP", 
gap).build());
+  }
+
+  private static PlanMatchPattern scanAllColumns() {
+    return tableScan(
+        "testdb.table1",
+        ImmutableList.of("time", "tag1", "tag2", "tag3", "attr1", "attr2", 
"s1", "s2", "s3"),
+        ImmutableSet.of("time", "tag1", "tag2", "tag3", "attr1", "attr2", 
"s1", "s2", "s3"));
+  }
+
+  private static List<PlanMatchPattern.Ordering> orderBy(String... fields) {
+    ImmutableList.Builder<PlanMatchPattern.Ordering> result = 
ImmutableList.builder();
+    for (String field : fields) {
+      result.add(sort(field, ASCENDING, LAST));
+    }
+    return result.build();
+  }
+
+  @Test
+  public void testNestedSetSemanticPermutedPartitionsKeepBothGroups() {
+    PlanTester planTester = new PlanTester();
+    LogicalQueryPlan logicalQueryPlan = 
planTester.createPlan(NESTED_SESSION_SQL);
+    Consumer<TableFunctionProcessorMatcher.Builder> outer =
+        sessionMatcher("window_start", "window_end", 120_000L);
+    Consumer<TableFunctionProcessorMatcher.Builder> inner =
+        sessionMatcher("window_start_0", "window_end_1", 60_000L);
+    // PARTITION BY columns are the same set in a different order and ORDER BY 
matches, so both
+    // grouping nodes survive: the outer keeps its declared order, the inner 
keeps its own.
+    assertPlan(
+        logicalQueryPlan,
+        anyTree(
+            tableFunctionProcessor(
+                outer,
+                group(
+                    orderBy("tag3", "tag2", "tag1", "time"),
+                    3,
+                    tableFunctionProcessor(
+                        inner,
+                        group(orderBy("tag1", "tag2", "tag3", "time"), 3, 
scanAllColumns()))))));
+    // Both levels are split per region; only regions with several devices 
need a merged stream.

Review Comment:
   Nit: the `MergeSort` in fragment 3 comes from the cross-region device branch 
of `constructDeviceTableScanByTags` (the mock's `DEVICE_5` and `DEVICE_6` are 
in two regions), not from a region having several devices. The fragment for 
region 11 scans two devices without a `MergeSort`.
   
   ```suggestion
       // Both levels are split per region; only cross-region devices need a 
merged stream.
   ```



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