ggjh-159 commented on code in PR #13693:
URL: https://github.com/apache/gluten/pull/13693#discussion_r4682591589
##########
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:
The translation-time type guards cover only the sort-key and partition-key
columns, while the native row storage accepts a fixed set of column kinds for
every input column (the compact row layout rejects nested types, TIME and
others when the operator is constructed). A rank query whose row carries such a
column therefore passes planning and fails at operator initialization on the
cluster, unlike the other limitations that fail at translation time. Please
extend the guard to every input column of the rank node so unsupported row
types are rejected at translation with the same kind of message.
##########
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:
Carrying the native keyed state through operator state is the wrong home for
it. The native backend's snapshot is key-group sharded by design - this PR even
computes the subtask's key-group range with Flink's own formula - but it is
persisted through a non-keyed ListState, where Flink cannot see the key groups
inside the record and can only re-distribute the records by count
(RoundRobinOperatorStateRepartitioner.repartitionSplitState); a restore with
more subtasks than records (1 to 4, 4 to 8) hands some subtasks no record, they
restore nothing, and the key groups they own are silently lost - while Flink's
own keyed state restores completely at any parallelism. The only operator-state
mode that pairs with the range-filtering native restore is union (every subtask
receives every record), which still re-implements, outside Flink, exactly the
sharding and rescaling a Flink KeyedStateBackend provides natively. Please make
the rank transformation keyed and back the native state with Fl
ink's keyed-state SPI (a KeyedStateBackend implementation over the native
backend) so Flink owns the key-group sharding, rescaling and savepoint
semantics; if that integration cannot land in this PR, keep the operator-state
path only as a documented interim in union mode with a fail-loud guard on
parallelism changes, and state the keyed-state integration as the follow-up.
##########
gluten-flink/planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/stream/StreamExecExchange.java:
##########
@@ -139,13 +134,19 @@ protected Transformation<RowData> translateToPlanInternal(
KeySelectorUtil.getRowDataSelector(
planner.getFlinkContext().getClassLoader(), keys, inputType);
// --- Begin Gluten-specific code changes ---
- // The gluten exchange optimization below only applies when the input
is
- // already a gluten operator wrapped in a OneInputTransformation; other
- // shapes (e.g. a SourceTransformation feeding the hash exchange in a
- // plain GROUP BY) must take the vanilla Flink fallback instead of
- // failing with a ClassCastException.
- if (inputTransform instanceof OneInputTransformation
- && ((OneInputTransformation) inputTransform).getOperator()
instanceof GlutenOperator) {
+ // Key-group routing is always native: the native keyed state backend
Review Comment:
Please revert the StreamExecExchange changes. This PR turns every hash
exchange of every query into a native-partitioner exchange by dropping the
gluten-input condition and the vanilla fallback - a shared path far outside the
rank scope - and the new comment states the reason: with key-group ownership
moved into the native backend (a restricted key-group range per subtask),
Flink's own partitioner would route records to a subtask whose backend does not
own their key group, and that state is dropped at the next snapshot. That
requirement exists only because of the operator-state design (see the comment
on GlutenOneInputOperator); with a keyed transformation Flink's partitioner and
Flink's key groups agree, so the exchange needs no change at all. Please
restore the previous translation: the native exchange when the input is a
gluten operator, the vanilla KeyGroupStreamPartitioner otherwise.
--
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]