Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
85171de
*: plumb per-column DESC flag through index metadata (phase 1 of #2519)
takaidohigasi Apr 24, 2026
e92b461
planner: honor IndexColumn.Desc when matching sort property (phase 2 …
takaidohigasi Apr 24, 2026
a23319b
codec: add EncodeKeyWithDesc/DecodeOneWithDesc primitives (phase 3a o…
takaidohigasi Apr 24, 2026
e414040
tablecodec: encode DESC index columns with complemented bytes (phase …
takaidohigasi Apr 24, 2026
686a9dc
distsql,executor,tables: wire DESC encoding through read paths (phase…
takaidohigasi Apr 24, 2026
adb81f0
codec: auto-detect DESC flag bytes in chunk Decoder (phase 3d-A of #2…
takaidohigasi Apr 24, 2026
a2407dd
planner,model: enforce IndexInfo.IsServable at plan time (#2519)
takaidohigasi Apr 25, 2026
b05e420
ddl: gate CREATE INDEX on TiKV cluster version (#2519)
takaidohigasi Apr 25, 2026
385d13f
planner: support mixed-direction composite indexes (#2519)
takaidohigasi Apr 25, 2026
82006d0
ddl: regression test for DESC on expression-index parts (#2519)
takaidohigasi Apr 25, 2026
7f506a5
ddl: lock down sysvar create-time-only semantics for DESC indexes (#2…
takaidohigasi Apr 25, 2026
be86ba2
ddl: clarify MinTiKVVersionForDescIndex is a release-coordinated TODO…
takaidohigasi Apr 25, 2026
61a14ea
ddl: reject DESC on the columns of a clustered PRIMARY KEY (#2519)
takaidohigasi Apr 25, 2026
f40e70d
executor: propagate IndexRangesToKVRangesWithDesc error in IndexMerge…
takaidohigasi Apr 25, 2026
3f246ba
planner: extend IsServable fence to LOAD DATA and IMPORT INTO (#2519)
takaidohigasi Apr 25, 2026
fcff441
ddl: bake tidb_enable_descending_index decision at SQL frontend (#2519)
takaidohigasi Apr 25, 2026
4ce9687
ddl: delay DESC preflight until after OnExistIgnore short-circuit (#2…
takaidohigasi Apr 25, 2026
3faacae
ddl: bound PD GetAllStores call with a 5s timeout (#2519)
takaidohigasi Apr 25, 2026
ffa8638
ddl: accept pre-release TiKV at the DESC index version floor (#2519)
takaidohigasi Apr 25, 2026
f89329e
planner: extend IsServable fence to writable non-public indexes (#2519)
takaidohigasi Apr 26, 2026
e9200da
ddl: clarify checkTiKVSupportsDescIndex docstring (#2519)
takaidohigasi Apr 26, 2026
49bbaca
ddl: assert idx_a presence in TestDescendingIndexSysvarIsCreateTimeOn…
takaidohigasi Apr 26, 2026
7fb5348
ddl: skip TiKV version gate for hypothetical DESC indexes (#2519)
takaidohigasi Apr 26, 2026
37f2288
planner: run IMPORT INTO servability fence on latest schema (#2519)
takaidohigasi Apr 26, 2026
8eec79a
tests: add tiup-playground e2e smoke test for descending-order indexe…
takaidohigasi Apr 26, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions pkg/ddl/create_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,14 @@ func BuildSessionTemporaryTableInfo(ctx *metabuild.Context, store kv.Storage, is

// BuildTableInfoWithStmt builds model.TableInfo from a SQL statement without validity check
func BuildTableInfoWithStmt(ctx *metabuild.Context, s *ast.CreateTableStmt, dbCharset, dbCollate string, placementPolicyRef *model.PolicyRefInfo) (*model.TableInfo, error) {
// Apply tidb_enable_descending_index gate up front, before any constraint
// processing. See ApplyDescGateToIndexParts for the rationale: baking the
// decision in at submission time prevents a `SET GLOBAL` between statement
// submission and DDL owner replay from silently flipping the persisted
// schema.
for _, c := range s.Constraints {
ApplyDescGateToIndexParts(c.Keys)
}
colDefs := s.Cols
tableCharset, tableCollate, err := GetCharsetAndCollateInTableOption(0, s.Options, ctx.GetDefaultCollationForUTF8MB4())
if err != nil {
Expand Down Expand Up @@ -1337,6 +1345,18 @@ func BuildTableInfo(
isSingleIntPK := isSingleIntPKFromTableInfo(constr, tbInfo)

if ShouldBuildClusteredIndex(ctx.GetClusteredIndexDefMode(), constr.Option, isSingleIntPK) {
// Reject DESC on the columns of a clustered PRIMARY KEY.
// For PKIsHandle (single-int PK) the column becomes the
// row's int handle directly and BuildIndexInfo is never
// invoked, so this guard catches that case. For
// IsCommonHandle the same check fires again inside
// BuildIndexInfo for defence-in-depth. See pingcap/tidb#2519.
for _, key := range constr.Keys {
if key.Desc {
return nil, errors.Errorf(
"DESC is not supported on the columns of a clustered PRIMARY KEY; either drop the DESC keyword or declare the primary key as NONCLUSTERED")
}
}
if isSingleIntPK {
tbInfo.PKIsHandle = true
} else {
Expand Down
400 changes: 400 additions & 0 deletions pkg/ddl/db_integration_test.go

Large diffs are not rendered by default.

153 changes: 153 additions & 0 deletions pkg/ddl/desc_index_version_check_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
// Copyright 2026 PingCAP, Inc.
//
// Licensed 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 ddl

import (
"testing"

"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/tidb/pkg/ddl/placement"
"github.com/stretchr/testify/require"
)

// store builds a synthetic *metapb.Store for the version-check tests.
// Pass an empty version to simulate a store that hasn't reported one yet.
func storeAt(id uint64, version string, isTiFlash bool) *metapb.Store {
s := &metapb.Store{Id: id, Version: version, Address: "mock"}
if isTiFlash {
s.Labels = []*metapb.StoreLabel{{Key: placement.EngineLabelKey, Value: placement.EngineLabelTiFlash}}
}
return s
}

func TestCheckStoresMeetDescIndexMinVersion(t *testing.T) {
const minVer = "9.0.0"
const failClosed = false // production semantics

t.Run("all TiKV stores are new enough", func(t *testing.T) {
stores := []*metapb.Store{
storeAt(1, "9.0.0", false),
storeAt(2, "9.1.0", false),
storeAt(3, "v9.0.0", false), // leading-v normalisation
}
require.NoError(t, checkStoresMeetDescIndexMinVersion(stores, minVer, failClosed))
})

t.Run("an old TiKV store fails the gate", func(t *testing.T) {
stores := []*metapb.Store{
storeAt(1, "9.0.0", false),
storeAt(2, "8.5.0", false),
}
err := checkStoresMeetDescIndexMinVersion(stores, minVer, failClosed)
require.Error(t, err)
require.Contains(t, err.Error(), "store 2")
require.Contains(t, err.Error(), "8.5.0")
require.Contains(t, err.Error(), minVer)
require.Contains(t, err.Error(), "upgrade TiKV")
})

t.Run("TiFlash stores are excluded from the check", func(t *testing.T) {
// A TiFlash store on an old version must not block the gate; the
// check is for TiKV only because TiKV alone runs the coprocessor
// path that needs the new decoder.
stores := []*metapb.Store{
storeAt(1, "9.0.0", false),
storeAt(2, "1.0.0", true), // ancient TiFlash, ignored
}
require.NoError(t, checkStoresMeetDescIndexMinVersion(stores, minVer, failClosed))
})

t.Run("tombstone stores are skipped", func(t *testing.T) {
old := storeAt(2, "8.0.0", false)
old.State = metapb.StoreState_Tombstone
stores := []*metapb.Store{
storeAt(1, "9.0.0", false),
old,
}
require.NoError(t, checkStoresMeetDescIndexMinVersion(stores, minVer, failClosed))
})

t.Run("empty version fails closed in production", func(t *testing.T) {
// A store that hasn't reported a clean semver yet cannot prove it
// can decode descending-order keys. The gate must reject the DDL
// rather than fall through and risk silent corruption. The operator
// can retry once PD has reconciled.
stores := []*metapb.Store{
storeAt(1, "9.0.0", false),
storeAt(2, "", false),
}
err := checkStoresMeetDescIndexMinVersion(stores, minVer, failClosed)
require.Error(t, err)
require.Contains(t, err.Error(), "store 2")
require.Contains(t, err.Error(), "has not reported a version")
})

t.Run("empty version is tolerated in tests", func(t *testing.T) {
// In `intest` builds the mock store fixture reports empty versions
// for every replica, so the gate must let DDL through to allow
// integration tests to run.
stores := []*metapb.Store{
storeAt(1, "9.0.0", false),
storeAt(2, "", false),
}
require.NoError(t, checkStoresMeetDescIndexMinVersion(stores, minVer, true))
})

t.Run("garbage version always fails closed", func(t *testing.T) {
// Unparsable version strings are unambiguously a bug or a
// misconfigured store; never tolerate them, even in tests.
stores := []*metapb.Store{
storeAt(1, "9.0.0", false),
storeAt(2, "not-a-semver", false),
}
err := checkStoresMeetDescIndexMinVersion(stores, minVer, true)
require.Error(t, err)
require.Contains(t, err.Error(), "store 2")
require.Contains(t, err.Error(), "unparsable")
})

t.Run("malformed minVersion is reported", func(t *testing.T) {
err := checkStoresMeetDescIndexMinVersion(nil, "definitely-not-semver", failClosed)
require.Error(t, err)
})

t.Run("pre-release at the floor base version is accepted", func(t *testing.T) {
// Strict semver treats `9.0.0-beta.2` < `9.0.0`, but a nightly /
// release-candidate cluster cut from the branch that contains the
// feature does carry the new decoder. Reject only when the base
// (Major.Minor.Patch) is below the floor; pre-release tags at the
// floor base pass. tiup playground reports versions like
// `9.0.0-beta.2` so this is what the e2e runner relies on.
stores := []*metapb.Store{
storeAt(1, "9.0.0-beta.2", false),
storeAt(2, "9.0.0-rc.1", false),
}
require.NoError(t, checkStoresMeetDescIndexMinVersion(stores, minVer, failClosed))
})

t.Run("pre-release below the floor base still fails", func(t *testing.T) {
// `8.5.0-beta.2`'s base (8.5.0) is below the floor (9.0.0), so the
// pre-release relaxation must NOT let it through. This guards
// against accidentally degrading the check to "any pre-release
// passes".
stores := []*metapb.Store{
storeAt(1, "8.5.0-beta.2", false),
}
err := checkStoresMeetDescIndexMinVersion(stores, minVer, failClosed)
require.Error(t, err)
require.Contains(t, err.Error(), "store 1")
require.Contains(t, err.Error(), "8.5.0-beta.2")
})
}
Loading
Loading