Skip to content

[flink] Add $buckets virtual table for bucket metadata - #4379

Merged
wuchong merged 3 commits into
apache:mainfrom
fhan688:add-Flink-sys.describe_buckets-procedure
Oct 23, 2026
Merged

wuchong merged 3 commits into
apache:mainfrom
fhan688:add-Flink-sys.describe_buckets-procedure

Conversation

@fhan688

@fhan688 fhan688 commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

Purpose

Linked issue: close #3436

PR #4029 introduced the Admin API and RPC support for retrieving bucket metadata. This pull request exposes that functionality to Flink SQL users through the read-only $buckets virtual table.

For example:

SELECT *
FROM my_table$buckets;

Compared with a system procedure, the virtual table integrates naturally with standard SQL operations such as projection and filtering, and can be inspected using commands such as DESCRIBE my_table$buckets.

Brief change log

  • Add the $buckets virtual table for Fluss tables.

  • Expose the following bucket metadata columns:

    • table_path
    • table_id
    • partition_id
    • partition_name
    • bucket_id
    • leader_id
    • leader_epoch
    • bucket_epoch
    • replicas
    • isr
  • Add a bounded, insert-only Flink table source that retrieves the current bucket metadata snapshot through Admin#describeBuckets.

  • Support both partitioned and non-partitioned tables:

    • partition fields are NULL for non-partitioned tables
    • leader fields are NULL when no leader is available
    • a missing bucket epoch is represented as NULL, while -1 represents missing leader/ISR state
  • Allow standard SQL projection and filtering, for example:

    SELECT partition_name, bucket_id, leader_id, replicas, isr
    FROM orders$buckets
    WHERE partition_name = 'cn$2026-09-16';
  • Treat $buckets as a read-only virtual table and reject sink creation, table mutation, and partition mutation.
  • Preserve lake-table routing precedence for names containing the $lake separator.
  • Replace the initial sys.describe_buckets procedure implementation in this PR with the $buckets virtual-table approach.
  • Add unit, catalog integration, and multi-version factory coverage.
  • Add documentation for the new virtual table.

The current implementation retrieves the complete bucket metadata snapshot before Flink evaluates SQL predicates. Predicate pushdown is not included in this pull request.

Tests

  • BucketsFlinkTableSourceTest: passed.
  • Flink120CatalogITCase#testBucketsVirtualTable: passed.
  • Flink118TableFactoryTest: 10 tests passed.
  • Flink120TableFactoryTest: 10 tests passed.
  • Flink23TableFactoryTest: 10 tests passed.
  • Maven package with Checkstyle, Spotless, and RAT: passed.
  • git diff --check: passed.

API and Format

This pull request introduces the $buckets Flink SQL virtual-table interface.

It does not change the Java Admin API, RPC protocol, server implementation, or storage format. The virtual table delegates to the existing Admin#describeBuckets API introduced by PR #4029.

Documentation

Added $buckets to the virtual-table documentation, including:

  • supported table types
  • bounded snapshot semantics
  • result schema and nullability
  • SQL query examples
  • read-only behavior
  • the current predicate-pushdown limitation

@fhan688 fhan688 closed this Sep 17, 2026
@fhan688 fhan688 reopened this Sep 17, 2026
@fhan688

fhan688 commented Sep 21, 2026

Copy link
Copy Markdown
Contributor Author

Hi @swuferhong , this PR is the Flink procedure follow-up to #4029. It exposes the describe-buckets API through sys.describe_buckets and completes #3436. All CI checks are passing now.

Could you please take a look when you have time? Thanks!

@apache apache deleted a comment from coding-otter-bot Sep 30, 2026
@luoyuxia

Copy link
Copy Markdown
Contributor

@coding-otter-bot review

@coding-otter-bot

Copy link
Copy Markdown
Contributor

Automated code review

Problem

Expose the existing Admin#describeBuckets bucket-metadata API to Flink SQL users via a new sys.describe_buckets system procedure, supporting all buckets of a table and optional complete/partial partition-spec filtering, with input validation, documentation, and tests.

Approach

Adds DescribeBucketsProcedure (ProcedureBase subclass with two @ProcedureHint overloads: (table_path) and (table_path, partition_spec)) that parses the table path and a key=value[/key=value...] spec into a PartitionSpec, delegates to Admin#describeBuckets, and maps List to Row<table_path, table_id, partition_id, partition_name, bucket_id, leader_id, leader_epoch, bucket_epoch, replicas ARRAY, isr ARRAY>. Registers the procedure in ProcedureManager, adds unit and ITCase coverage, and a docs section.

Review coverage

Complete: the material changed paths and review hypotheses were resolved.

Conclusion

No actionable code issues were found in this static review. From the reviewed code paths, no change is required before merge. CI results and maintainer approval still apply.

Findings

No actionable findings survived the final audit.

@coding-otter-bot

Copy link
Copy Markdown
Contributor

Automated code review could not complete

The review for commit c5db110a85263cb4f7fb358c2c10853bbd3b34cd stopped before results could be published.

Failure code: REVIEW_MODEL_MAX_TOKENS
Invocation: inv_ee956ef4-0820-4ecc-ac3e-73efe53038b6

After the problem is resolved, post a new comment to retry:

@coding-otter-bot review

@fhan688

fhan688 commented Oct 10, 2026

Copy link
Copy Markdown
Contributor Author

hi @swuferhong , could you please help review this PR when you have time? thanks!

@wuchong

wuchong commented Oct 12, 2026

Copy link
Copy Markdown
Member

Thanks @fhan688 for the contribution, what do you think about supporting this via $buckets system tables like Paimon (https://paimon.apache.org/docs/master/concepts/system-tables#buckets-table), e.g., SELECT * FROM table_name$buckets. In this way, users can easily inspect the schema of describe bucket (via DESCRIBE table_name$buckets) and using SQL to filter/transform the buckets metadata.

@fhan688

fhan688 commented Oct 13, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @fhan688 for the contribution, what do you think about supporting this via $buckets system tables like Paimon (https://paimon.apache.org/docs/master/concepts/system-tables#buckets-table), e.g., SELECT * FROM table_name$buckets. In this way, users can easily inspect the schema of describe bucket (via DESCRIBE table_name$buckets) and using SQL to filter/transform the buckets metadata.

Thanks for the suggestion. I agree that bucket metadata is a better fit for a system table because it is read-only relational metadata. table_name$buckets would make the schema discoverable through DESCRIBE and allow users to filter, project, aggregate, or join the metadata using standard SQL.

Fluss already has the virtual-table mechanism used by $changelog and $binlog, so I plan to rework this PR rather than expose both APIs:

  • remove sys.describe_buckets;
  • add a read-only, bounded table_name$buckets system table backed by Admin#describeBuckets;
  • preserve the current bucket metadata fields and nullable semantics;
  • update the catalog/source tests, integration tests, and documentation;
  • ensure table$lake$buckets continues to be delegated to the lake catalog.

Standard SQL filtering will work in this PR. Partition predicate pushdown to the partition-specific Admin API can be added as a follow-up optimization to keep the initial implementation focused.

@fhan688 fhan688 changed the title [flink] Add describe buckets procedure [flink] Add $buckets virtual table for bucket metadata Oct 13, 2026
@fhan688 fhan688 closed this Oct 13, 2026
@fhan688 fhan688 reopened this Oct 13, 2026
@wuchong

wuchong commented Oct 17, 2026

Copy link
Copy Markdown
Member

Thanks, @fhan688, for the update. I like the new API. Will take a closer look later.

Btw, I suggest that we also refactor the previously introduced sys.list_partition_infos CALL procedure (#4339) into a $partitions virtual table to align with the $buckets design (in another PR). My understanding is that CALL procedures are better suited for action-oriented operations rather than query-like ones. cc @Kaixuan-Duan @platinumhamburg, what do you think?

@fhan688

fhan688 commented Oct 19, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, @fhan688, for the update. I like the new API. Will take a closer look later.

Btw, I suggest that we also refactor the previously introduced sys.list_partition_infos CALL procedure (#4339) into a $partitions virtual table to align with the $buckets design (in another PR). My understanding is that CALL procedures are better suited for action-oriented operations rather than query-like ones. cc @Kaixuan-Duan @platinumhamburg, what do you think?

Thanks @wuchong, that makes sense. I’ll keep this PR focused on the $buckets virtual table and handle sys.list_partition_infos in a separate follow-up PR after this one lands.

The follow-up can introduce a read-only, bounded $partitions virtual table backed by Admin#listPartitionInfos, migrate the existing tests and documentation, and reuse the virtual-table routing and mutation guards introduced here.

I’ll wait for feedback from @Kaixuan-Duan and @platinumhamburg on whether the existing procedure should be removed directly or kept for a compatibility period.

@Kaixuan-Duan

Copy link
Copy Markdown
Contributor

+1 to @wuchong's suggestion. Query-like metadata fits a virtual table better than a CALL procedure: it composes naturally with standard SQL (projection, filtering, aggregation), the schema is discoverable via DESCRIBE. Aligning $partitions with the $buckets design also keeps a consistent mental model — tables for queries, procedures for actions.

Thanks @fhan688, on removing sys.list_partition_infos: since it has already shipped in v1.0.0, I'd favor deprecating it first rather than removing it in the same PR — mark it deprecated in the release that introduces $partitions, and remove it in a later one. This gives existing users a migration window instead of breaking their scripts on upgrade.

cc @platinumhamburg.

@Override
public DataStream<RowData> produceDataStream(
ProviderContext providerContext, StreamExecutionEnvironment execEnv) {
return execEnv.fromCollection(loadBucketRows(), resultTypeInfo)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we defer fetching bucket metadata until runtime? Calling loadBucketRows() here embeds the current bucket data in the source transformation. When SQL Gateway plan caching is enabled, its cached DQLCachedPlan reuses these transformations, so subsequent executions do not call describeBuckets() again and can return stale metadata.

I reproduced this with Flink 1.20.3: after adding a partition, a freshly planned query returned 4 buckets, while executing the cached plan still returned the original 2.

Paimon's $buckets implementation avoids this by creating a split containing only the filter predicates during planning and reading the metadata in BucketsRead.createReader(). Could we use a lightweight bounded source with a single split and call Admin.describeBuckets() in the reader? This would refresh the metadata on each execution and avoid serializing all bucket rows into the JobGraph. WDYT @loserwang1024 ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we defer fetching bucket metadata until runtime? Calling loadBucketRows() here embeds the current bucket data in the source transformation. When SQL Gateway plan caching is enabled, its cached DQLCachedPlan reuses these transformations, so subsequent executions do not call describeBuckets() again and can return stale metadata.

I reproduced this with Flink 1.20.3: after adding a partition, a freshly planned query returned 4 buckets, while executing the cached plan still returned the original 2.

Paimon's $buckets implementation avoids this by creating a split containing only the filter predicates during planning and reading the metadata in BucketsRead.createReader(). Could we use a lightweight bounded source with a single split and call Admin.describeBuckets() in the reader? This would refresh the metadata on each execution and avoid serializing all bucket rows into the JobGraph. WDYT @loserwang1024 ?

Thanks for catching this and for the detailed reproduction. I agree that calling loadBucketRows() from produceDataStream() materializes the metadata while building the transformations, so a cached DQLCachedPlan can reuse stale bucket rows.

I’ll rework this into a lightweight bounded source with a single split and move Admin#describeBuckets() into the source reader, so the metadata is fetched once per execution rather than during planning. The split can remain empty for now since predicate pushdown is still a follow-up optimization.

I’ll also add regression coverage that executes the same DQL with SQL Gateway plan caching enabled, updates the partitions out of band, and verifies that the second execution observes the new buckets.

@platinumhamburg

Copy link
Copy Markdown
Contributor

+1 to @wuchong's suggestion. Query-like metadata fits a virtual table better than a CALL procedure: it composes naturally with standard SQL (projection, filtering, aggregation), the schema is discoverable via DESCRIBE. Aligning $partitions with the $buckets design also keeps a consistent mental model — tables for queries, procedures for actions.

Thanks @fhan688, on removing sys.list_partition_infos: since it has already shipped in v1.0.0, I'd favor deprecating it first rather than removing it in the same PR — mark it deprecated in the release that introduces $partitions, and remove it in a later one. This gives existing users a migration window instead of breaking their scripts on upgrade.

cc @platinumhamburg.

+1 to @wuchong and @Kaixuan-Duan. A $partitions virtual table would make metadata queries consistent with $buckets and easier to compose with standard SQL.
I agree with deprecating sys.list_partition_infos first, since it has already shipped in v1.0.0.
And keeping that change in a separate follow-up PR sounds good to me. Thanks @fhan688!

@wuchong

wuchong commented Oct 23, 2026

Copy link
Copy Markdown
Member

@Kaixuan-Duan @platinumhamburg thanks, I agree. Let's deprecate sys.list_partition_infos when introducing $partitions and remove it after one or two version.

@wuchong wuchong left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

I have some concerns regarding code reuse, particularly the 400+ lines of source implementation, as introducing more virtual tables could create a significant maintenance burden. However, this can be optimized in follow-up PRs.

@wuchong
wuchong merged commit 59bb49a into apache:main Oct 23, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add "$buckets" virtual table for describing bucket metadata

7 participants