sunshone commented on code in PR #13693:
URL: https://github.com/apache/gluten/pull/13693#discussion_r4691113286


##########
gluten-flink/runtime/src/main/java/org/apache/gluten/table/runtime/operators/GlutenOneInputOperator.java:
##########
@@ -336,11 +352,37 @@ public void snapshotState(StateSnapshotContext context) 
throws Exception {
   }
 
   protected void snapshotNativeState(StateSnapshotContext context) throws 
Exception {
-    task.snapshotState(context.getCheckpointId());
+    // The native snapshot returns the checkpoint records of the stateful
+    // operators (typed keyed backends); persist them in the operator state
+    // so a restore can feed them back (design D4 of the rank task). Empty
+    // for operators without native state, so the path is transparent to
+    // every other user of this class.
+    byte[][] checkpointRecords = task.snapshotState(context.getCheckpointId());
+    checkpointState.clear();
+    for (byte[] checkpointRecord : checkpointRecords) {
+      checkpointState.add(checkpointRecord);
+    }
   }
 
   @Override
   public void initializeState(StateInitializationContext context) throws 
Exception {
+    // Declare the checkpoint-record store and collect the restored records
+    // before the native state initialization consumes them (the
+    // GlutenSourceFunction pattern).
+    checkpointState =
+        context
+            .getOperatorStateStore()
+            .getListState(

Review Comment:
   Agreed on the interim. The state is now registered with getUnionListState at 
this site and at the source and file-writer persistence sites; under union 
redistribution every subtask receives every writer's shard at restore, and the 
native backend picks its own key groups out of each shard (velox-side: a stream 
with no overlap is skipped instead of rejected, since under union it is an 
expected delivery). Restore correctness across rescales is pinned by the 
stateful suite (the rescale and four-way-shard tests) and by the checkpoint 
round-trip suite. The keyed-state integration - a KeyedStateBackend 
implementation over the native backend, with Flink owning the key-group 
sharding, rescaling and savepoint semantics - is documented as the follow-up in 
the code comment at this site and in the PR description. On the 
parallelism-change guard: with union distribution the restore completes for any 
rescale by construction, so a fail-loud guard would reject correct restores; if 
you prefer a gua
 rd regardless, it can be added with the follow-up work.



##########
gluten-flink/planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/stream/StreamExecRank.java:
##########
@@ -287,25 +330,56 @@ protected Transformation<RowData> translateToPlanInternal(
             inputTransform.getParallelism(),
             false);
 
-    // set KeyType and Selector for state
-    RowDataKeySelector selector =
-        KeySelectorUtil.getRowDataSelector(
-            planner.getFlinkContext().getClassLoader(),
-            partitionSpec.getFieldIndices(),
-            inputRowTypeInfo);
-    transform.setStateKeySelector(selector);
-    transform.setStateKeyType(selector.getProducedType());
+    // The transformation stays NON-keyed (implementation contract): the key
+    // belongs to the velox keyed state backend via the plan's
+    // KeyedStateSpec, and declaring a Flink key selector would conflict
+    // with the native key-group routing at parallelism > 1.
     return transform;
   }
 
-  private List<SortOrder> generateSortOrders(SortSpec sortSpec) {
-    final List<SortOrder> sortOrders = new ArrayList<>();
-    boolean[] ascendingOrders = sortSpec.getAscendingOrders();
-    boolean[] nullLasts = sortSpec.getNullsIsLast();
-    checkArgument(ascendingOrders.length == nullLasts.length);
-    for (int i = 0; i < ascendingOrders.length; i++) {
-      sortOrders.add(new SortOrder(ascendingOrders[i], !nullLasts[i]));
+  // The native keyed state backend supports fixed-width scalar and
+  // varchar/varbinary key columns only (KeyTable::checkStateKeyTypes);
+  // unsupported partition key types fail loud at translation time instead
+  // of dying inside the first probe.
+  private static void checkPartitionKeyTypeSupported(LogicalType type, int 
fieldIndex) {
+    final boolean supported =
+        (TypeCheckUtils.isNumeric(type) && !TypeCheckUtils.isDecimal(type))
+            || TypeCheckUtils.isTimestamp(type)
+            || TypeCheckUtils.isTimestampWithLocalZone(type)
+            || TypeCheckUtils.isBoolean(type)
+            || TypeCheckUtils.isCharacterString(type)
+            || TypeCheckUtils.isBinaryString(type);
+    if (!supported) {
+      throw new TableException(
+          String.format(
+              "Gluten rank does not support partition key type %s at field 
%d", type, fieldIndex));
+    }
+  }
+
+  // The native sort-key comparator (RankOperator.cpp compareScalarValues)
+  // covers exactly these velox type kinds (design limit L11): the 11 base
+  // kinds plus DECIMAL (BIGINT/HUGEINT kind by precision) and DATE (INTEGER
+  // kind); a sort key outside the set fails loud at translation time
+  // instead of on the first row comparison.
+  private static void checkSortKeyTypeSupported(Type type, int fieldIndex) {

Review Comment:
   Done. The translation now checks every input column of the rank node against 
the exact kind set the native compact row storage accepts (its 
checkColumnKind): the scalar kinds plus DECIMAL by way of BIGINT/HUGEINT and 
DATE by way of INTEGER, with TIMESTAMP_LTZ normalized to TIMESTAMP by the 
converter. A column outside the set - nested types and anything else the 
storage rejects - fails at translation with a "Gluten rank row storage does not 
support input column type" message, the same fail-loud shape as the other 
guards. Covered by a new planner test (an ARRAY column in the rank input, 
rejected at translation).



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to