You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@fluss.apache.org by "loserwang1024 (via GitHub)" <gi...@apache.org> on 2026/08/27 06:57:37 UTC
Re: [PR] [flink] Support custom lookup shuffle by bucket for Flink 2.x [fluss]
loserwang1024 commented on code in PR #3675:
URL: https://github.com/apache/fluss/pull/3675#discussion_r3869364515
##########
fluss-flink/fluss-flink-2.2/src/main/java/org/apache/fluss/flink/adapter/LookupShuffleSourceAdapter.java:
##########
@@ -0,0 +1,96 @@
+/*
+ * 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.fluss.flink.adapter;
+
+import org.apache.fluss.flink.FlinkConnectorOptions;
+import org.apache.fluss.flink.source.FlinkLookupShuffleTableSource;
+import org.apache.fluss.flink.source.FlinkTableSource;
+
+import org.apache.flink.configuration.ReadableConfig;
+import org.apache.flink.table.api.ValidationException;
+
+/**
+ * Flink 2.x implementation: wraps the source so it implements {@code SupportsLookupCustomShuffle},
+ * but only when the table exposes the metadata required to reproduce Fluss bucket routing. Shadows
+ * the common (no-op) class of the same fully-qualified name at package time.
+ *
+ * <p>Eligibility is decided here, at planning time, rather than inside {@code getPartitioner()}:
+ *
+ * <ul>
+ * <li>the table is not lookup-shuffle eligible, or {@code bucket.num} is not configured ->
+ * leave the source unwrapped so that an explicitly requested lookup shuffle falls back to
+ * Flink's default hash distribution;
+ * <li>{@code bucket.num} configured but not a positive integer -> fail fast with a {@link
+ * ValidationException} (a real misconfiguration);
+ * <li>otherwise wrap with the validated bucket count, so the wrapped source can always return a
+ * partitioner.
+ * </ul>
+ *
+ * <p>The unwrapped (hash-fallback) branch is a defensive safety net rather than a reachable state
+ * for a Fluss table: a Fluss table can only be created through the {@code FlussCatalog}, which
+ * always defaults {@code bucket.num} and derives the bucket key from the primary key, while Flink
+ * rejects creating a Fluss source as a temporary/{@code connector}-based table. It is therefore
+ * covered by adapter unit tests rather than an end-to-end planner test.
+ */
+public class LookupShuffleSourceAdapter {
Review Comment:
The current `LookupShuffleSourceAdapter` seems more complicated than necessary. It performs capability gating in the factory, wraps `FlinkTableSource` with an additional subtype, and requires extra copy/state-preservation logic.
Could we follow the existing version-specific superclass adapter pattern instead? For example, let `FlinkTableSource` extend a same-FQN `SupportsLookupCustomShuffleAdapter`.
* The Flink 2.2 implementation can implement `SupportsLookupCustomShuffle` and provide `getPartitioner()`
```java
public interface SupportsLookupCustomShuffleAdapter extends SupportsLookupCustomShuffle {
@Override
default Optional<InputDataPartitioner> getPartitioner() {
Optional<InputDataPartitionerAdapter> partitionerAdapter = getPartitionerAdapter();
Optional<InputDataPartitioner> inputDataPartitioner = partitionerAdapter.isPresent() ? Optional.of(partitionerAdapter.get()) : Optional.empty();
return inputDataPartitioner;
}
Optional<InputDataPartitionerAdapter> getPartitionerAdapter();
interface InputDataPartitionerAdapter extends InputDataPartitioner{}
}
```
* the lower-version implementation :
```java
public interface SupportsLookupCustomShuffleAdapter {
Optional<InputDataPartitionerAdapter> getPartitionerAdapter();
interface InputDataPartitionerAdapter {
int partition(RowData joinKeys, int numPartitions);
default boolean isDeterministic() {
return true;
}
}
}
```
##########
fluss-flink/fluss-flink-2.2/src/test/java/org/apache/fluss/flink/source/FlinkLookupShuffleTableSourceTest.java:
##########
@@ -0,0 +1,247 @@
+/*
+ * 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.fluss.flink.source;
+
+import org.apache.fluss.bucketing.BucketingFunction;
+import org.apache.fluss.config.ConfigOptions;
+import org.apache.fluss.config.Configuration;
+import org.apache.fluss.config.TableConfig;
+import org.apache.fluss.flink.FlinkConnectorOptions;
+import org.apache.fluss.flink.row.FlinkAsFlussRow;
+import org.apache.fluss.flink.utils.FlinkConnectorOptionsUtils;
+import org.apache.fluss.flink.utils.FlinkConversions;
+import org.apache.fluss.metadata.DataLakeFormat;
+import org.apache.fluss.metadata.TablePath;
+import org.apache.fluss.row.encode.KeyEncoder;
+import org.apache.fluss.testutils.common.MultiVersionTest;
+
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.connector.source.DynamicTableSource;
+import org.apache.flink.table.connector.source.LookupTableSource;
+import org.apache.flink.table.connector.source.abilities.SupportsLookupCustomShuffle.InputDataPartitioner;
+import org.apache.flink.table.data.GenericRowData;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.data.StringData;
+import org.apache.flink.table.types.DataType;
+import org.apache.flink.table.types.logical.IntType;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.RowType;
+import org.junit.jupiter.api.Test;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Optional;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+/**
+ * Unit tests for {@link FlinkLookupShuffleTableSource}, focusing on the invariant that a wrapped
+ * source (which has already advertised {@code SupportsLookupCustomShuffle}, so Flink suppresses its
+ * own hash shuffle) must always return a partitioner, and that a copy made between {@code
+ * getLookupRuntimeProvider()} and {@code getPartitioner()} keeps the stashed lookup normalizer.
+ */
+@MultiVersionTest
+class FlinkLookupShuffleTableSourceTest {
+
+ private static FlinkTableSource baseSource(int[] primaryKeys, int[] bucketKeys) {
+ RowType tableOutputType =
+ (RowType)
+ DataTypes.ROW(
+ DataTypes.FIELD("id", DataTypes.INT()),
+ DataTypes.FIELD("name", DataTypes.STRING()),
+ DataTypes.FIELD("address", DataTypes.STRING()))
+ .getLogicalType();
+ return baseSource(
+ tableOutputType,
+ primaryKeys,
+ bucketKeys,
+ new int[] {},
+ new TableConfig(new Configuration()));
+ }
+
+ private static FlinkTableSource baseSource(
+ RowType tableOutputType,
+ int[] primaryKeys,
+ int[] bucketKeys,
+ int[] partitionKeys,
+ TableConfig tableConfig) {
+ Configuration flussConfig = new Configuration();
+ flussConfig.setString(FlinkConnectorOptions.BOOTSTRAP_SERVERS.key(), "localhost:9092");
+ FlinkConnectorOptionsUtils.StartupOptions startupOptions =
+ new FlinkConnectorOptionsUtils.StartupOptions();
+ startupOptions.startupMode = FlinkConnectorOptions.ScanStartupMode.EARLIEST;
+ return new FlinkTableSource(
+ TablePath.of("test_db", "dim"),
+ flussConfig,
+ tableConfig,
+ tableOutputType,
+ primaryKeys,
+ bucketKeys,
+ partitionKeys,
+ true, // streaming
+ startupOptions,
+ false, // lookup async (sync avoids any async runtime setup)
+ false, // insert if not exists
+ null, // cache
+ 1000L, // scan partition discovery interval
+ false, // is data lake enabled
+ null, // merge engine type
+ new HashMap<>(),
+ null); // lease context
+ }
+
+ @Test
+ void testGetPartitionerPresentForFullPkLookup() {
+ // primary key == bucket key == id; lookup on the full primary key.
+ FlinkLookupShuffleTableSource source =
+ new FlinkLookupShuffleTableSource(baseSource(new int[] {0}, new int[] {0}), 3);
+ source.getLookupRuntimeProvider(new TestLookupContext(new int[][] {{0}}));
+ assertThat(source.getPartitioner()).isPresent();
+ }
+
+ @Test
+ void testPartitionedLakeFormatBuildsUsablePartitioner() {
+ RowType tableOutputType =
+ (RowType)
+ DataTypes.ROW(
+ DataTypes.FIELD("id", DataTypes.INT().notNull()),
+ DataTypes.FIELD("region", DataTypes.STRING().notNull()),
+ DataTypes.FIELD("p_date", DataTypes.STRING().notNull()))
+ .getLogicalType();
+ Configuration tableConfiguration = new Configuration();
+ tableConfiguration.set(ConfigOptions.TABLE_DATALAKE_FORMAT, DataLakeFormat.ICEBERG);
+ FlinkLookupShuffleTableSource source =
+ new FlinkLookupShuffleTableSource(
+ baseSource(
+ tableOutputType,
+ new int[] {0, 1, 2},
+ new int[] {0},
+ new int[] {1, 2},
+ new TableConfig(tableConfiguration)),
+ 16);
+ source.getLookupRuntimeProvider(new TestLookupContext(new int[][] {{0}, {1}, {2}}));
+
+ InputDataPartitioner partitioner = source.getPartitioner().get();
+ RowData first =
+ GenericRowData.of(
+ 1, StringData.fromString("cn"), StringData.fromString("2026-08-20"));
+ RowData second =
+ GenericRowData.of(
+ 2, StringData.fromString("cn"), StringData.fromString("2026-08-20"));
+
+ // The golden bucket vectors pin both ids to Iceberg bucket 4.
+ assertThat(partitioner.partition(second, 4)).isEqualTo(partitioner.partition(first, 4));
+ }
+
+ @Test
+ void testGetPartitionerPresentForPrefixLookup() {
Review Comment:
I don't like this test.
1. ItCase need to be add to flink-common module.
2. An IT case test means end to end, please add flink sql tests for ITCase(Adding test case in FlinkTableSourceITCase is enough, referring org.apache.fluss.flink.source.FlinkTableSourceITCase#testLookup1PkTable)
```sql
SELECT /*+ LOOKUP('table'='Customers', 'shuffle'='true') */ o.order_id, o.total, c.country, c.zip
FROM Orders AS o
JOIN Customers FOR SYSTEM_TIME AS OF o.proc_time AS c
ON o.customer_id = [c.id](http://c.id/);
```
3. To verify shuffle is enbaled, check the result of tevn.explainSql("xxx")
--
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: issues-unsubscribe@fluss.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org