-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[core] Introduce 'pk-clustering-override' to clustering by non-primary key fields #7426
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
JingsongLi
wants to merge
3
commits into
apache:master
Choose a base branch
from
JingsongLi:pk-clustering-override
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
108 changes: 108 additions & 0 deletions
108
docs/content/primary-key-table/pk-clustering-override.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| --- | ||
| title: "PK Clustering Override" | ||
| weight: 10 | ||
| type: docs | ||
| aliases: | ||
| - /primary-key-table/pk-clustering-override.html | ||
| --- | ||
| <!-- | ||
| 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. | ||
| --> | ||
|
|
||
| # PK Clustering Override | ||
|
|
||
| By default, data files in a primary key table are physically sorted by the primary key. This is optimal for point | ||
| lookups but can hurt scan performance when queries filter on non-primary-key columns. | ||
|
|
||
| **PK Clustering Override** mode changes the physical sort order of data files from the primary key to user-specified | ||
| clustering columns. This significantly improves scan performance for queries that filter or group by clustering columns, | ||
| while still maintaining primary key uniqueness through deletion vectors. | ||
|
|
||
| ## Quick Start | ||
|
|
||
| ```sql | ||
| CREATE TABLE my_table ( | ||
| id BIGINT, | ||
| dt STRING, | ||
| city STRING, | ||
| amount DOUBLE, | ||
| PRIMARY KEY (id) NOT ENFORCED | ||
| ) WITH ( | ||
| 'pk-clustering-override' = 'true', | ||
| 'clustering.columns' = 'city', | ||
| 'deletion-vectors.enabled' = 'true', | ||
| 'bucket' = '4' | ||
| ); | ||
| ``` | ||
|
|
||
| After this, data files within each bucket will be physically sorted by `city` instead of `id`. Queries like | ||
| `SELECT * FROM my_table WHERE city = 'Beijing'` can skip irrelevant data files by checking their min/max statistics | ||
| on the clustering column. | ||
|
|
||
| ## How It Works | ||
|
|
||
| PK Clustering Override replaces the default LSM compaction with a two-phase clustering compaction: | ||
|
|
||
| **Phase 1 — Sort by Clustering Columns**: Newly flushed (level 0) files are read, sorted by the configured clustering | ||
| columns, and rewritten as sorted (level 1) files. A key index tracks each primary key's file and row position to | ||
| maintain uniqueness. | ||
|
|
||
| **Phase 2 — Merge Overlapping Sections**: Sorted files are grouped into sections based on clustering column range | ||
| overlap. Overlapping sections are merged together. Adjacent small sections are also consolidated to reduce file count | ||
| and IO amplification. Non-overlapping large files are left untouched. | ||
|
|
||
| During both phases, deduplication is handled via deletion vectors: | ||
|
|
||
| - **Deduplicate mode**: When a key already exists in an older file, the old row is marked as deleted. | ||
| - **First-row mode**: When a key already exists, the new row is marked as deleted, keeping the first-seen value. | ||
|
|
||
| When the number of files to merge exceeds `sort-spill-threshold`, smaller files are first spilled to row-based | ||
| temporary files to reduce memory consumption, preventing OOM during multi-way merge. | ||
|
|
||
| ## Requirements | ||
|
|
||
| | Option | Requirement | | ||
| |--------|-------------| | ||
| | `pk-clustering-override` | `true` | | ||
| | `clustering.columns` | Must be set (one or more non-primary-key columns) | | ||
| | `deletion-vectors.enabled` | Must be `true` | | ||
| | `merge-engine` | `deduplicate` (default) or `first-row` only | | ||
| | `sequence.fields` | Must **not** be set | | ||
| | `record-level.expire-time` | Must **not** be set | | ||
|
|
||
| ## Related Options | ||
|
|
||
| | Option | Default | Description | | ||
| |--------|---------|-------------| | ||
| | `clustering.columns` | (none) | Comma-separated column names used as the physical sort order for data files. | | ||
| | `sort-spill-threshold` | (auto) | When the number of merge readers exceeds this value, smaller files are spilled to row-based temp files to reduce memory usage. | | ||
| | `sort-spill-buffer-size` | `64 mb` | Buffer size used for external sort during Phase 1 rewrite. | | ||
|
|
||
| ## When to Use | ||
|
|
||
| PK Clustering Override is beneficial when: | ||
|
|
||
| - Analytical queries frequently filter or aggregate on non-primary-key columns (e.g., `WHERE city = 'Beijing'`). | ||
| - The table uses `deduplicate` or `first-row` merge engine. | ||
| - You want data files physically co-located by a business dimension rather than the primary key. | ||
|
|
||
| It is **not** suitable when: | ||
|
|
||
| - Point lookups by primary key are the dominant access pattern (default LSM sort is already optimal). | ||
| - You need `partial-update` or `aggregation` merge engine. | ||
| - `sequence.fields` or `record-level.expire-time` is required. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.