Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: use slices.Sort (#16433)

p0nyc2b8e536a0ee
diff --git a/.golangci.yaml b/.golangci.yaml
index 956bb8a3b4..9ad7851893 100644
--- a/.golangci.yaml
+++ b/.golangci.yaml
@@ -23,6 +23,17 @@ linters:
msg: Please use os\.ReadDir instead
- pattern: protojson\.Unmarshal?
msg: Please use ProtojsonUnmarshaler instead
+ # Prohibit old sort functions in favor of new slices package (Go 1.21+)
+ - pattern: sort\.Slice
+ msg: Please use slices\.SortFunc instead
+ - pattern: sort\.SliceStable
+ msg: Please use slices\.SortStableFunc instead
+ - pattern: sort\.SliceIsSorted
+ msg: Please use slices\.IsSortedFunc instead
+ - pattern: sort\.Strings
+ msg: Please use slices\.Sort instead
+ - pattern: sort\.Ints
+ msg: Please use slices\.Sort instead
gocritic:
disabled-checks:
- ifElseChain
diff --git a/CLAUDE.md b/CLAUDE.md
index ca959b3c20..dd025ed0ef 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -13,6 +13,7 @@ This file provides guidance to Claude Code (claude.ai/code) when working with co
### After Go Code Changes
1. **Format**: Run `gofmt -w` on modified files
2. **Lint**: Run `golangci-lint run --allow-parallel-runners` to catch issues
+ - **Important**: Run golangci-lint repeatedly until there are no issues. The linter has a max-issues limit and may not show all issues in a single run.
3. **Auto-fix**: Use `golangci-lint run --fix --allow-parallel-runners` to fix issues automatically
4. **Test**: Run relevant tests before committing
@@ -69,6 +70,7 @@ Always follow these guidelines to avoid common linting errors:
- **Proper Import Ordering**: Maintain correct grouping and ordering of imports
- **Consistency**: Keep function signatures, naming, and patterns consistent with existing code
- **Export Rules**: Only export (capitalize) functions and types that need to be used outside the package
+- **Linting Command**: Always run `golangci-lint run --allow-parallel-runners` without appending filenames to avoid "function not defined" errors (functions are defined in other files within the package)
## Misc
@@ -76,4 +78,4 @@ Always follow these guidelines to avoid common linting errors:
## Individual Preferences
-- @~/.claude/bytebase-instructions.md
\ No newline at end of file
+- @~/.claude/bytebase-instructions.md
diff --git a/backend/api/v1/database_service.go b/backend/api/v1/database_service.go
index 3d644e7449..ee78e3b779 100644
--- a/backend/api/v1/database_service.go
+++ b/backend/api/v1/database_service.go
@@ -5,7 +5,6 @@ import (
"fmt"
"regexp"
"slices"
- "sort"
"strings"
"unicode"
@@ -915,18 +914,33 @@ func convertToChangedResources(r *storepb.ChangedResources) *v1pb.ChangedResourc
Ranges: ranges,
})
}
- sort.Slice(v1Schema.Tables, func(i, j int) bool {
- return v1Schema.Tables[i].Name < v1Schema.Tables[j].Name
+ slices.SortFunc(v1Schema.Tables, func(a, b *v1pb.ChangedResourceTable) int {
+ if a.Name < b.Name {
+ return -1
+ } else if a.Name > b.Name {
+ return 1
+ }
+ return 0
})
v1Database.Schemas = append(v1Database.Schemas, v1Schema)
}
- sort.Slice(v1Database.Schemas, func(i, j int) bool {
- return v1Database.Schemas[i].Name < v1Database.Schemas[j].Name
+ slices.SortFunc(v1Database.Schemas, func(a, b *v1pb.ChangedResourceSchema) int {
+ if a.Name < b.Name {
+ return -1
+ } else if a.Name > b.Name {
+ return 1
+ }
+ return 0
})
result.Databases = append(result.Databases, v1Database)
}
- sort.Slice(result.Databases, func(i, j int) bool {
- return result.Databases[i].Name < result.Databases[j].Name
+ slices.SortFunc(result.Databases, func(a, b *v1pb.ChangedResourceDatabase) int {
+ if a.Name < b.Name {
+ return -1
+ } else if a.Name > b.Name {
+ return 1
+ }
+ return 0
})
return result
}
diff --git a/backend/api/v1/risk_service_calculator.go b/backend/api/v1/risk_service_calculator.go
index 5ae01039e3..2df9f374e2 100644
--- a/backend/api/v1/risk_service_calculator.go
+++ b/backend/api/v1/risk_service_calculator.go
@@ -3,7 +3,7 @@ package v1
import (
"context"
"maps"
- "sort"
+ "slices"
"github.com/google/cel-go/cel"
celtypes "github.com/google/cel-go/common/types"
@@ -26,8 +26,13 @@ func CalculateRiskLevelWithSummaryReport(
}
// Sort by level DESC, higher risks go first.
- sort.Slice(risks, func(i, j int) bool {
- return risks[i].Level > risks[j].Level
+ slices.SortFunc(risks, func(a, b *store.RiskMessage) int {
+ if a.Level > b.Level {
+ return -1
+ } else if a.Level < b.Level {
+ return 1
+ }
+ return 0
})
for _, risk := range risks {
diff --git a/backend/api/v1/rollout_service.go b/backend/api/v1/rollout_service.go
index 0d80cb5245..ce342b685c 100644
--- a/backend/api/v1/rollout_service.go
+++ b/backend/api/v1/rollout_service.go
@@ -5,7 +5,6 @@ import (
"database/sql"
"log/slog"
"slices"
- "sort"
"strings"
"time"
@@ -585,8 +584,8 @@ func (s *RolloutService) BatchRunTasks(ctx context.Context, request *v1pb.BatchR
}
taskRunCreates = append(taskRunCreates, create)
}
- sort.Slice(taskRunCreates, func(i, j int) bool {
- return taskRunCreates[i].TaskUID < taskRunCreates[j].TaskUID
+ slices.SortFunc(taskRunCreates, func(a, b *store.TaskRunMessage) int {
+ return a.TaskUID - b.TaskUID
})
if err := s.store.CreatePendingTaskRuns(ctx, taskRunCreates...); err != nil {
diff --git a/backend/component/masker/masker.go b/backend/component/masker/masker.go
index 27da610e2c..bb12b063cb 100644
--- a/backend/component/masker/masker.go
+++ b/backend/component/masker/masker.go
@@ -4,7 +4,7 @@ import (
"crypto/md5"
"fmt"
"log/slog"
- "sort"
+ "slices"
"strconv"
"strings"
"time"
@@ -163,8 +163,13 @@ func (m *RangeMasker) formatRanges(maxLength int32) []*MaskRangeSlice {
}
}
- sort.SliceStable(mergedMaskRangeSlice, func(i, j int) bool {
- return mergedMaskRangeSlice[i].Start < mergedMaskRangeSlice[j].Start
+ slices.SortStableFunc(mergedMaskRangeSlice, func(a, b *MaskRangeSlice) int {
+ if a.Start < b.Start {
+ return -1
+ } else if a.Start > b.Start {
+ return 1
+ }
+ return 0
})
return mergedMaskRangeSlice
diff --git a/backend/migrator/migrator.go b/backend/migrator/migrator.go
index 5ff2e6d991..ffe09a2319 100644
--- a/backend/migrator/migrator.go
+++ b/backend/migrator/migrator.go
@@ -8,7 +8,7 @@ import (
"fmt"
"io/fs"
"log/slog"
- "sort"
+ "slices"
"strconv"
"strings"
@@ -131,8 +131,13 @@ func getSortedVersionedFiles() ([]versionedFile, error) {
}); err != nil {
return nil, err
}
- sort.Slice(files, func(i, j int) bool {
- return files[i].version.LT(*files[j].version)
+ slices.SortFunc(files, func(a, b versionedFile) int {
+ if a.version.LT(*b.version) {
+ return -1
+ } else if a.version.GT(*b.version) {
+ return 1
+ }
+ return 0
})
return files, nil
}
diff --git a/backend/plugin/advisor/catalog/walk_through_test.go b/backend/plugin/advisor/catalog/walk_through_test.go
index b80e5cc3e7..bce59636c1 100644
--- a/backend/plugin/advisor/catalog/walk_through_test.go
+++ b/backend/plugin/advisor/catalog/walk_through_test.go
@@ -4,7 +4,7 @@ import (
"io"
"os"
"path/filepath"
- "sort"
+ "slices"
"testing"
"github.com/google/go-cmp/cmp"
@@ -232,8 +232,13 @@ func (d *DatabaseState) convertToSchemaMetadataList() []*storepb.SchemaMetadata
result = append(result, schemaMeta)
}
- sort.Slice(result, func(i, j int) bool {
- return result[i].Name < result[j].Name
+ slices.SortFunc(result, func(x, y *storepb.SchemaMetadata) int {
+ if x.Name < y.Name {
+ return -1
+ } else if x.Name > y.Name {
+ return 1
+ }
+ return 0
})
return result
@@ -266,8 +271,13 @@ func (s *SchemaState) convertToTableMetadataList() []*storepb.TableMetadata {
result = append(result, tableMeta)
}
- sort.Slice(result, func(i, j int) bool {
- return result[i].Name < result[j].Name
+ slices.SortFunc(result, func(x, y *storepb.TableMetadata) int {
+ if x.Name < y.Name {
+ return -1
+ } else if x.Name > y.Name {
+ return 1
+ }
+ return 0
})
return result
@@ -300,8 +310,13 @@ func (t *TableState) convertToIndexMetadataList() []*storepb.IndexMetadata {
result = append(result, indexMeta)
}
- sort.Slice(result, func(i, j int) bool {
- return result[i].Name < result[j].Name
+ slices.SortFunc(result, func(x, y *storepb.IndexMetadata) int {
+ if x.Name < y.Name {
+ return -1
+ } else if x.Name > y.Name {
+ return 1
+ }
+ return 0
})
return result
@@ -341,11 +356,19 @@ func (t *TableState) convertToColumnMetadataList() []*storepb.ColumnMetadata {
result = append(result, columnMeta)
}
- sort.Slice(result, func(i, j int) bool {
- if result[i].Position != result[j].Position {
- return result[i].Position < result[j].Position
+ slices.SortFunc(result, func(x, y *storepb.ColumnMetadata) int {
+ if x.Position != y.Position {
+ if x.Position < y.Position {
+ return -1
+ }
+ return 1
+ }
+ if x.Name < y.Name {
+ return -1
+ } else if x.Name > y.Name {
+ return 1
}
- return result[i].Name < result[j].Name
+ return 0
})
return result
diff --git a/backend/plugin/advisor/mssql/advisor_column_require.go b/backend/plugin/advisor/mssql/advisor_column_require.go
index 5d052b4fc1..1fea4e9fe7 100644
--- a/backend/plugin/advisor/mssql/advisor_column_require.go
+++ b/backend/plugin/advisor/mssql/advisor_column_require.go
@@ -4,7 +4,7 @@ package mssql
import (
"context"
"fmt"
- "sort"
+ "slices"
"github.com/antlr4-go/antlr/v4"
parser "github.com/bytebase/tsql-parser"
@@ -111,9 +111,7 @@ func (l *columnRequireChecker) ExitCreate_table(ctx *parser.Create_tableContext)
return
}
- sort.Slice(columnNames, func(i, j int) bool {
- return columnNames[i] < columnNames[j]
- })
+ slices.Sort(columnNames)
for _, column := range columnNames {
l.adviceList = append(l.adviceList, &storepb.Advice{
Status: l.level,
diff --git a/backend/plugin/advisor/mysql/advisor_column_current_time_count_limit.go b/backend/plugin/advisor/mysql/advisor_column_current_time_count_limit.go
index 5d78159ddb..407f35b4ca 100644
--- a/backend/plugin/advisor/mysql/advisor_column_current_time_count_limit.go
+++ b/backend/plugin/advisor/mysql/advisor_column_current_time_count_limit.go
@@ -5,7 +5,7 @@ package mysql
import (
"context"
"fmt"
- "sort"
+ "slices"
"github.com/antlr4-go/antlr/v4"
"github.com/pkg/errors"
@@ -213,8 +213,14 @@ func (checker *columnCurrentTimeCountLimitChecker) generateAdvice() []*storepb.A
for _, table := range checker.tableSet {
tableList = append(tableList, table)
}
- sort.Slice(tableList, func(i, j int) bool {
- return tableList[i].line < tableList[j].line
+ slices.SortFunc(tableList, func(a, b tableData) int {
+ if a.line < b.line {
+ return -1
+ }
+ if a.line > b.line {
+ return 1
+ }
+ return 0
})
for _, table := range tableList {
if table.defaultCurrentTimeCount > maxDefaultCurrentTimeColumCount {
diff --git a/backend/plugin/advisor/mysql/advisor_column_no_null.go b/backend/plugin/advisor/mysql/advisor_column_no_null.go
index e92d0bb20e..58656bb1a7 100644
--- a/backend/plugin/advisor/mysql/advisor_column_no_null.go
+++ b/backend/plugin/advisor/mysql/advisor_column_no_null.go
@@ -3,7 +3,7 @@ package mysql
import (
"context"
"fmt"
- "sort"
+ "slices"
"github.com/antlr4-go/antlr/v4"
mysql "github.com/bytebase/mysql-parser"
@@ -82,11 +82,20 @@ func (checker *columnNoNullChecker) generateAdvice() []*storepb.Advice {
for _, column := range checker.columnSet {
columnList = append(columnList, column)
}
- sort.Slice(columnList, func(i, j int) bool {
- if columnList[i].line != columnList[j].line {
- return columnList[i].line < columnList[j].line
+ slices.SortFunc(columnList, func(a, b columnName) int {
+ if a.line != b.line {
+ if a.line < b.line {
+ return -1
+ }
+ return 1
+ }
+ if a.columnName < b.columnName {
+ return -1
+ }
+ if a.columnName > b.columnName {
+ return 1
}
- return columnList[i].columnName < columnList[j].columnName
+ return 0
})
for _, column := range columnList {
diff --git a/backend/plugin/advisor/mysql/advisor_column_required.go b/backend/plugin/advisor/mysql/advisor_column_required.go
index 3c1f0e3098..3f1b443c1a 100644
--- a/backend/plugin/advisor/mysql/advisor_column_required.go
+++ b/backend/plugin/advisor/mysql/advisor_column_required.go
@@ -3,7 +3,7 @@ package mysql
import (
"context"
"fmt"
- "sort"
+ "slices"
"strings"
"github.com/antlr4-go/antlr/v4"
@@ -178,7 +178,7 @@ func (checker *columnRequirementChecker) generateAdviceList() []*storepb.Advice
if len(missingColumns) > 0 {
// Order it cause the random iteration order in Go, see https://go.dev/blog/maps
- sort.Strings(missingColumns)
+ slices.Sort(missingColumns)
checker.adviceList = append(checker.adviceList, &storepb.Advice{
Status: checker.level,
Code: advisor.NoRequiredColumn.Int32(),
diff --git a/backend/plugin/advisor/mysql/advisor_index_total_number_limit.go b/backend/plugin/advisor/mysql/advisor_index_total_number_limit.go
index a893348321..061f09c0cb 100644
--- a/backend/plugin/advisor/mysql/advisor_index_total_number_limit.go
+++ b/backend/plugin/advisor/mysql/advisor_index_total_number_limit.go
@@ -5,7 +5,7 @@ package mysql
import (
"context"
"fmt"
- "sort"
+ "slices"
"github.com/antlr4-go/antlr/v4"
"github.com/pkg/errors"
@@ -89,8 +89,14 @@ func (checker *indexTotalNumberLimitChecker) generateAdvice() []*storepb.Advice
line: v,
})
}
- sort.Slice(tableList, func(i, j int) bool {
- return tableList[i].line < tableList[j].line
+ slices.SortFunc(tableList, func(i, j tableName) int {
+ if i.line < j.line {
+ return -1
+ }
+ if i.line > j.line {
+ return 1
+ }
+ return 0
})
for _, table := range tableList {
diff --git a/backend/plugin/advisor/mysql/advisor_naming_index_convention.go b/backend/plugin/advisor/mysql/advisor_naming_index_convention.go
index fd5c055cec..f3f894b087 100644
--- a/backend/plugin/advisor/mysql/advisor_naming_index_convention.go
+++ b/backend/plugin/advisor/mysql/advisor_naming_index_convention.go
@@ -161,9 +161,8 @@ func (checker *namingIndexConventionChecker) EnterAlterTable(ctx *mysql.AlterTab
// Unique index naming convention should in advisor_naming_unique_key_convention.go
continue
}
- columnList := indexState.ExpressionList()
metaData := map[string]string{
- advisor.ColumnListTemplateToken: strings.Join(columnList, "_"),
+ advisor.ColumnListTemplateToken: strings.Join(indexState.ExpressionList(), "_"),
advisor.TableNameTemplateToken: tableName,
}
indexData := &indexMetaData{
diff --git a/backend/plugin/advisor/mysql/advisor_statement_merge_alter_table.go b/backend/plugin/advisor/mysql/advisor_statement_merge_alter_table.go
index 7fc7a4f195..7b16bef42d 100644
--- a/backend/plugin/advisor/mysql/advisor_statement_merge_alter_table.go
+++ b/backend/plugin/advisor/mysql/advisor_statement_merge_alter_table.go
@@ -5,7 +5,7 @@ package mysql
import (
"context"
"fmt"
- "sort"
+ "slices"
"github.com/antlr4-go/antlr/v4"
"github.com/pkg/errors"
@@ -121,8 +121,14 @@ func (checker *statementMergeAlterTableChecker) generateAdvice() []*storepb.Advi
for _, table := range checker.tableMap {
tableList = append(tableList, table)
}
- sort.Slice(tableList, func(i, j int) bool {
- return tableList[i].lastLine < tableList[j].lastLine
+ slices.SortFunc(tableList, func(i, j tableStatement) int {
+ if i.lastLine < j.lastLine {
+ return -1
+ }
+ if i.lastLine > j.lastLine {
+ return 1
+ }
+ return 0
})
for _, table := range tableList {
diff --git a/backend/plugin/advisor/mysql/utils.go b/backend/plugin/advisor/mysql/utils.go
index 517cfb1d25..54d6b6f6eb 100644
--- a/backend/plugin/advisor/mysql/utils.go
+++ b/backend/plugin/advisor/mysql/utils.go
@@ -3,7 +3,7 @@ package mysql
import (
"regexp"
- "sort"
+ "slices"
"strings"
parser "github.com/bytebase/mysql-parser"
@@ -27,7 +27,7 @@ func (t tableState) tableList() []string {
for tableName := range t {
tableList = append(tableList, tableName)
}
- sort.Strings(tableList)
+ slices.Sort(tableList)
return tableList
}
@@ -39,7 +39,7 @@ func (t tablePK) tableList() []string {
for tableName := range t {
tableList = append(tableList, tableName)
}
- sort.Strings(tableList)
+ slices.Sort(tableList)
return tableList
}
diff --git a/backend/plugin/advisor/oracle/advisor_column_no_null.go b/backend/plugin/advisor/oracle/advisor_column_no_null.go
index 67e746ef6c..c6bbcf3153 100644
--- a/backend/plugin/advisor/oracle/advisor_column_no_null.go
+++ b/backend/plugin/advisor/oracle/advisor_column_no_null.go
@@ -4,7 +4,7 @@ package oracle
import (
"context"
"fmt"
- "sort"
+ "slices"
"github.com/antlr4-go/antlr/v4"
parser "github.com/bytebase/plsql-parser"
@@ -74,7 +74,7 @@ func (l *columnNoNullListener) generateAdvice() ([]*storepb.Advice, error) {
for columnID := range l.nullableColumns {
columnIDs = append(columnIDs, columnID)
}
- sort.Strings(columnIDs)
+ slices.Sort(columnIDs)
for _, columnID := range columnIDs {
line := l.nullableColumns[columnID]
advice = append(advice, &storepb.Advice{
diff --git a/backend/plugin/advisor/oracle/advisor_column_require.go b/backend/plugin/advisor/oracle/advisor_column_require.go
index 85c5f7ba18..24f8d07ef0 100644
--- a/backend/plugin/advisor/oracle/advisor_column_require.go
+++ b/backend/plugin/advisor/oracle/advisor_column_require.go
@@ -4,7 +4,7 @@ package oracle
import (
"context"
"fmt"
- "sort"
+ "slices"
"strings"
"github.com/antlr4-go/antlr/v4"
@@ -100,7 +100,7 @@ func (l *columnRequireListener) ExitCreate_table(ctx *parser.Create_tableContext
return
}
- sort.Strings(missingColumns)
+ slices.Sort(missingColumns)
tableName := normalizeIdentifier(ctx.Table_name(), l.currentDatabase)
l.adviceList = append(l.adviceList, &storepb.Advice{
Status: l.level,
@@ -137,7 +137,7 @@ func (l *columnRequireListener) ExitAlter_table(ctx *parser.Alter_tableContext)
return
}
- sort.Strings(missingColumns)
+ slices.Sort(missingColumns)
tableName := lastIdentifier(normalizeIdentifier(ctx.Tableview_name(), l.currentDatabase))
l.adviceList = append(l.adviceList, &storepb.Advice{
Status: l.level,
diff --git a/backend/plugin/advisor/oracle/advisor_column_require_default.go b/backend/plugin/advisor/oracle/advisor_column_require_default.go
index b6da61305f..0f4618d0ac 100644
--- a/backend/plugin/advisor/oracle/advisor_column_require_default.go
+++ b/backend/plugin/advisor/oracle/advisor_column_require_default.go
@@ -4,7 +4,7 @@ package oracle
import (
"context"
"fmt"
- "sort"
+ "slices"
"github.com/antlr4-go/antlr/v4"
parser "github.com/bytebase/plsql-parser"
@@ -71,7 +71,7 @@ func (l *columnRequireDefaultListener) generateAdvice() ([]*storepb.Advice, erro
for columnID := range l.noDefaultColumns {
columnIDs = append(columnIDs, columnID)
}
- sort.Strings(columnIDs)
+ slices.Sort(columnIDs)
for _, columnID := range columnIDs {
line := l.noDefaultColumns[columnID]
advice = append(advice, &storepb.Advice{
diff --git a/backend/plugin/advisor/pg/advisor_column_default_disallow_volatile.go b/backend/plugin/advisor/pg/advisor_column_default_disallow_volatile.go
index a218f816e7..5c83c46191 100644
--- a/backend/plugin/advisor/pg/advisor_column_default_disallow_volatile.go
+++ b/backend/plugin/advisor/pg/advisor_column_default_disallow_volatile.go
@@ -5,7 +5,7 @@ package pg
import (
"context"
"fmt"
- "sort"
+ "slices"
"github.com/pkg/errors"
@@ -69,8 +69,14 @@ func (checker *columnDefaultDisallowVolatileChecker) generateAdvice() []*storepb
for _, column := range checker.columnSet {
columnList = append(columnList, column)
}
- sort.Slice(columnList, func(i, j int) bool {
- return columnList[i].line < columnList[j].line
+ slices.SortFunc(columnList, func(i, j columnData) int {
+ if i.line < j.line {
+ return -1
+ }
+ if i.line > j.line {
+ return 1
+ }
+ return 0
})
for _, column := range columnList {
diff --git a/backend/plugin/advisor/pg/advisor_column_no_null.go b/backend/plugin/advisor/pg/advisor_column_no_null.go
index 7994504a64..bed8653a67 100644
--- a/backend/plugin/advisor/pg/advisor_column_no_null.go
+++ b/backend/plugin/advisor/pg/advisor_column_no_null.go
@@ -3,7 +3,7 @@ package pg
import (
"context"
"fmt"
- "sort"
+ "slices"
"github.com/pkg/errors"
@@ -68,14 +68,26 @@ func (checker *columnNoNullChecker) generateAdviceList() []*storepb.Advice {
}
if len(columnList) > 0 {
// Order it cause the random iteration order in Go, see https://go.dev/blog/maps
- sort.Slice(columnList, func(i, j int) bool {
- if columnList[i].schema != columnList[j].schema {
- return columnList[i].schema < columnList[j].schema
+ slices.SortFunc(columnList, func(i, j columnName) int {
+ if i.schema != j.schema {
+ if i.schema < j.schema {
+ return -1
+ }
+ return 1
+ }
+ if i.table != j.table {
+ if i.table < j.table {
+ return -1
+ }
+ return 1
+ }
+ if i.column < j.column {
+ return -1
}
- if columnList[i].table != columnList[j].table {
- return columnList[i].table < columnList[j].table
+ if i.column > j.column {
+ return 1
}
- return columnList[i].column < columnList[j].column
+ return 0
})
}
for _, column := range columnList {
diff --git a/backend/plugin/advisor/pg/advisor_column_require_default.go b/backend/plugin/advisor/pg/advisor_column_require_default.go
index dbc82faf3d..b1551c5541 100644
--- a/backend/plugin/advisor/pg/advisor_column_require_default.go
+++ b/backend/plugin/advisor/pg/advisor_column_require_default.go
@@ -5,7 +5,7 @@ package pg
import (
"context"
"fmt"
- "sort"
+ "slices"
"github.com/pkg/errors"
@@ -76,8 +76,14 @@ func (checker *columnRequireDefaultChecker) generateAdvice() []*storepb.Advice {
for _, column := range checker.columnSet {
columnList = append(columnList, column)
}
- sort.Slice(columnList, func(i, j int) bool {
- return columnList[i].line < columnList[j].line
+ slices.SortFunc(columnList, func(i, j columnData) int {
+ if i.line < j.line {
+ return -1
+ }
+ if i.line > j.line {
+ return 1
+ }
+ return 0
})
for _, column := range columnList {
diff --git a/backend/plugin/advisor/pg/advisor_column_required.go b/backend/plugin/advisor/pg/advisor_column_required.go
index 696e3f05d4..8381285e54 100644
--- a/backend/plugin/advisor/pg/advisor_column_required.go
+++ b/backend/plugin/advisor/pg/advisor_column_required.go
@@ -3,7 +3,7 @@ package pg
import (
"context"
"fmt"
- "sort"
+ "slices"
"strings"
"github.com/pkg/errors"
@@ -101,7 +101,7 @@ func (checker *columnRequirementChecker) Visit(node ast.Node) ast.Visitor {
if len(missingColumns) > 0 {
// Order it cause the random iteration order in Go, see https://go.dev/blog/maps
- sort.Strings(missingColumns)
+ slices.Sort(missingColumns)
checker.adviceList = append(checker.adviceList, &storepb.Advice{
Status: checker.level,
Code: advisor.NoRequiredColumn.Int32(),
diff --git a/backend/plugin/advisor/pg/advisor_index_total_number_limit.go b/backend/plugin/advisor/pg/advisor_index_total_number_limit.go
index 8a989fc585..4376588044 100644
--- a/backend/plugin/advisor/pg/advisor_index_total_number_limit.go
+++ b/backend/plugin/advisor/pg/advisor_index_total_number_limit.go
@@ -5,7 +5,7 @@ package pg
import (
"context"
"fmt"
- "sort"
+ "slices"
"github.com/pkg/errors"
@@ -94,8 +94,14 @@ func (checker *indexTotalNumberLimitChecker) generateAdvice() []*storepb.Advice
for _, table := range checker.tableLine {
tableList = append(tableList, table)
}
- sort.Slice(tableList, func(i, j int) bool {
- return tableList[i].line < tableList[j].line
+ slices.SortFunc(tableList, func(i, j tableLine) int {
+ if i.line < j.line {
+ return -1
+ }
+ if i.line > j.line {
+ return 1
+ }
+ return 0
})
for _, table := range tableList {
diff --git a/backend/plugin/advisor/pg/advisor_statement_merge_alter_table.go b/backend/plugin/advisor/pg/advisor_statement_merge_alter_table.go
index f47f5f478f..d90aa76aab 100644
--- a/backend/plugin/advisor/pg/advisor_statement_merge_alter_table.go
+++ b/backend/plugin/advisor/pg/advisor_statement_merge_alter_table.go
@@ -5,7 +5,7 @@ package pg
import (
"context"
"fmt"
- "sort"
+ "slices"
"github.com/pkg/errors"
@@ -57,8 +57,14 @@ func (checker *statementMergeAlterTableChecker) generateAdvice() []*storepb.Advi
for _, table := range checker.tableMap {
tableList = append(tableList, table)
}
- sort.Slice(tableList, func(i, j int) bool {
- return tableList[i].line < tableList[j].line
+ slices.SortFunc(tableList, func(i, j tableStatement) int {
+ if i.line < j.line {
+ return -1
+ }
+ if i.line > j.line {
+ return 1
+ }
+ return 0
})
for _, table := range tableList {
if table.count > 1 {
diff --git a/backend/plugin/advisor/snowflake/advisor_column_require.go b/backend/plugin/advisor/snowflake/advisor_column_require.go
index 9375a24173..5dccb5fded 100644
--- a/backend/plugin/advisor/snowflake/advisor_column_require.go
+++ b/backend/plugin/advisor/snowflake/advisor_column_require.go
@@ -4,7 +4,7 @@ package snowflake
import (
"context"
"fmt"
- "sort"
+ "slices"
"github.com/antlr4-go/antlr/v4"
parser "github.com/bytebase/snowsql-parser"
@@ -117,9 +117,7 @@ func (l *columnRequireChecker) ExitCreate_table(ctx *parser.Create_tableContext)
return
}
- sort.Slice(columnNames, func(i, j int) bool {
- return columnNames[i] < columnNames[j]
- })
+ slices.Sort(columnNames)
for _, column := range columnNames {
l.adviceList = append(l.adviceList, &storepb.Advice{
Status: l.level,
@@ -163,9 +161,7 @@ func (l *columnRequireChecker) ExitAlter_table(ctx *parser.Alter_tableContext) {
return
}
- sort.Slice(columnNames, func(i, j int) bool {
- return columnNames[i] < columnNames[j]
- })
+ slices.Sort(columnNames)
for _, column := range columnNames {
l.adviceList = append(l.adviceList, &storepb.Advice{
Status: l.level,
diff --git a/backend/plugin/advisor/tidb/advisor_column_current_time_count_limit.go b/backend/plugin/advisor/tidb/advisor_column_current_time_count_limit.go
index 288ad7a6fe..72746122f0 100644
--- a/backend/plugin/advisor/tidb/advisor_column_current_time_count_limit.go
+++ b/backend/plugin/advisor/tidb/advisor_column_current_time_count_limit.go
@@ -5,7 +5,7 @@ package tidb
import (
"context"
"fmt"
- "sort"
+ "slices"
"strings"
"github.com/pingcap/tidb/pkg/parser/ast"
@@ -82,8 +82,14 @@ func (checker *columnCurrentTimeCountLimitChecker) generateAdvice() []*storepb.A
for _, table := range checker.tableSet {
tableList = append(tableList, table)
}
- sort.Slice(tableList, func(i, j int) bool {
- return tableList[i].line < tableList[j].line
+ slices.SortFunc(tableList, func(i, j tableData) int {
+ if i.line < j.line {
+ return -1
+ }
+ if i.line > j.line {
+ return 1
+ }
+ return 0
})
for _, table := range tableList {
if table.defaultCurrentTimeCount > maxDefaultCurrentTimeColumCount {
diff --git a/backend/plugin/advisor/tidb/advisor_column_no_null.go b/backend/plugin/advisor/tidb/advisor_column_no_null.go
index fd33f4c689..72cd0bbf25 100644
--- a/backend/plugin/advisor/tidb/advisor_column_no_null.go
+++ b/backend/plugin/advisor/tidb/advisor_column_no_null.go
@@ -3,7 +3,7 @@ package tidb
import (
"context"
"fmt"
- "sort"
+ "slices"
"github.com/pingcap/tidb/pkg/parser/ast"
"github.com/pkg/errors"
@@ -65,11 +65,20 @@ func (checker *columnNoNullChecker) generateAdvice() []*storepb.Advice {
for _, column := range checker.columnSet {
columnList = append(columnList, column)
}
- sort.Slice(columnList, func(i, j int) bool {
- if columnList[i].line != columnList[j].line {
- return columnList[i].line < columnList[j].line
+ slices.SortFunc(columnList, func(i, j columnName) int {
+ if i.line != j.line {
+ if i.line < j.line {
+ return -1
+ }
+ return 1
+ }
+ if i.columnName < j.columnName {
+ return -1
+ }
+ if i.columnName > j.columnName {
+ return 1
}
- return columnList[i].columnName < columnList[j].columnName
+ return 0
})
for _, column := range columnList {
diff --git a/backend/plugin/advisor/tidb/advisor_column_required.go b/backend/plugin/advisor/tidb/advisor_column_required.go
index 2e3e7e4492..93b9944b5d 100644
--- a/backend/plugin/advisor/tidb/advisor_column_required.go
+++ b/backend/plugin/advisor/tidb/advisor_column_required.go
@@ -3,7 +3,7 @@ package tidb
import (
"context"
"fmt"
- "sort"
+ "slices"
"strings"
"github.com/pingcap/tidb/pkg/parser/ast"
@@ -129,7 +129,7 @@ func (v *columnRequirementChecker) generateAdviceList() []*storepb.Advice {
}
if len(missingColumns) > 0 {
// Order it cause the random iteration order in Go, see https://go.dev/blog/maps
- sort.Strings(missingColumns)
+ slices.Sort(missingColumns)
v.adviceList = append(v.adviceList, &storepb.Advice{
Status: v.level,
Code: advisor.NoRequiredColumn.Int32(),
diff --git a/backend/plugin/advisor/tidb/advisor_index_total_number_limit.go b/backend/plugin/advisor/tidb/advisor_index_total_number_limit.go
index 01468e02ed..7e497d7a14 100644
--- a/backend/plugin/advisor/tidb/advisor_index_total_number_limit.go
+++ b/backend/plugin/advisor/tidb/advisor_index_total_number_limit.go
@@ -5,7 +5,7 @@ package tidb
import (
"context"
"fmt"
- "sort"
+ "slices"
"github.com/pingcap/tidb/pkg/parser/ast"
"github.com/pkg/errors"
@@ -85,8 +85,14 @@ func (checker *indexTotalNumberLimitChecker) generateAdvice() []*storepb.Advice
line: v,
})
}
- sort.Slice(tableList, func(i, j int) bool {
- return tableList[i].line < tableList[j].line
+ slices.SortFunc(tableList, func(i, j tableName) int {
+ if i.line < j.line {
+ return -1
+ }
+ if i.line > j.line {
+ return 1
+ }
+ return 0
})
for _, table := range tableList {
diff --git a/backend/plugin/advisor/tidb/advisor_statement_merge_alter_table.go b/backend/plugin/advisor/tidb/advisor_statement_merge_alter_table.go
index f9275c8f49..d0a545d0ee 100644
--- a/backend/plugin/advisor/tidb/advisor_statement_merge_alter_table.go
+++ b/backend/plugin/advisor/tidb/advisor_statement_merge_alter_table.go
@@ -5,7 +5,7 @@ package tidb
import (
"context"
"fmt"
- "sort"
+ "slices"
"github.com/pingcap/tidb/pkg/parser/ast"
"github.com/pkg/errors"
@@ -105,8 +105,14 @@ func (checker *statementMergeAlterTableChecker) generateAdvice() []*storepb.Advi
for _, table := range checker.tableMap {
tableList = append(tableList, table)
}
- sort.Slice(tableList, func(i, j int) bool {
- return tableList[i].lastLine < tableList[j].lastLine
+ slices.SortFunc(tableList, func(i, j tableStatement) int {
+ if i.lastLine < j.lastLine {
+ return -1
+ }
+ if i.lastLine > j.lastLine {
+ return 1
+ }
+ return 0
})
for _, table := range tableList {
diff --git a/backend/plugin/advisor/tidb/utils.go b/backend/plugin/advisor/tidb/utils.go
index 5a1d997341..cc0fe0b71b 100644
--- a/backend/plugin/advisor/tidb/utils.go
+++ b/backend/plugin/advisor/tidb/utils.go
@@ -2,7 +2,7 @@
package tidb
import (
- "sort"
+ "slices"
"strings"
"github.com/pingcap/tidb/pkg/parser/ast"
@@ -29,7 +29,7 @@ func (t tableState) tableList() []string {
for tableName := range t {
tableList = append(tableList, tableName)
}
- sort.Strings(tableList)
+ slices.Sort(tableList)
return tableList
}
@@ -41,7 +41,7 @@ func (t tablePK) tableList() []string {
for tableName := range t {
tableList = append(tableList, tableName)
}
- sort.Strings(tableList)
+ slices.Sort(tableList)
return tableList
}
diff --git a/backend/plugin/advisor/utils_for_tests.go b/backend/plugin/advisor/utils_for_tests.go
index 138ab6a4fa..4f110cbdde 100644
--- a/backend/plugin/advisor/utils_for_tests.go
+++ b/backend/plugin/advisor/utils_for_tests.go
@@ -8,7 +8,7 @@ import (
"io"
"os"
"path/filepath"
- "sort"
+ "slices"
"strings"
"testing"
@@ -237,14 +237,27 @@ func RunSQLReviewRuleTest(t *testing.T, rule SQLReviewRuleType, dbType storepb.E
adviceList, err := SQLReviewCheck(t.Context(), sm, tc.Statement, ruleList, checkCtx)
// Sort adviceList by (line, content)
- sort.Slice(adviceList, func(i, j int) bool {
- if adviceList[i].GetStartPosition() == nil || adviceList[j].GetStartPosition() == nil {
- return adviceList[i].GetStartPosition() == nil
+ slices.SortFunc(adviceList, func(x, y *storepb.Advice) int {
+ if x.GetStartPosition() == nil || y.GetStartPosition() == nil {
+ if x.GetStartPosition() == nil && y.GetStartPosition() == nil {
+ return 0
+ } else if x.GetStartPosition() == nil {
+ return -1
+ }
+ return 1
}
- if adviceList[i].GetStartPosition().Line != adviceList[j].GetStartPosition().Line {
- return adviceList[i].GetStartPosition().Line < adviceList[j].GetStartPosition().Line
+ if x.GetStartPosition().Line != y.GetStartPosition().Line {
+ if x.GetStartPosition().Line < y.GetStartPosition().Line {
+ return -1
+ }
+ return 1
}
- return adviceList[i].Content < adviceList[j].Content
+ if x.Content < y.Content {
+ return -1
+ } else if x.Content > y.Content {
+ return 1
+ }
+ return 0
})
require.NoError(t, err)
diff --git a/backend/plugin/db/dynamodb/dynamodb.go b/backend/plugin/db/dynamodb/dynamodb.go
index 1cff65076a..d0b1d0764f 100644
--- a/backend/plugin/db/dynamodb/dynamodb.go
+++ b/backend/plugin/db/dynamodb/dynamodb.go
@@ -5,7 +5,7 @@ import (
"context"
"database/sql"
"encoding/json"
- "sort"
+ "slices"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
@@ -233,7 +233,7 @@ func (d *Driver) querySinglePartiQL(ctx context.Context, statement string, query
for key := range rowMap {
sortedColumnNames = append(sortedColumnNames, key)
}
- sort.Strings(sortedColumnNames)
+ slices.Sort(sortedColumnNames)
columnTypes := make([]string, 0, len(sortedColumnNames))
for _, key := range sortedColumnNames {
columnTypes = append(columnTypes, columnTypeMap[key])
diff --git a/backend/plugin/db/dynamodb/sync.go b/backend/plugin/db/dynamodb/sync.go
index ab7923377e..539ef96e46 100644
--- a/backend/plugin/db/dynamodb/sync.go
+++ b/backend/plugin/db/dynamodb/sync.go
@@ -2,7 +2,7 @@ package dynamodb
import (
"context"
- "sort"
+ "slices"
"strings"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
@@ -118,7 +118,7 @@ func (d *Driver) syncTable(ctx context.Context, tableName string) (*storepb.Tabl
for key := range columnsMap {
sortedColumns = append(sortedColumns, key)
}
- sort.Strings(sortedColumns)
+ slices.Sort(sortedColumns)
tableMetadata.Columns = make([]*storepb.ColumnMetadata, 0, len(sortedColumns))
for _, key := range sortedColumns {
tableMetadata.Columns = append(tableMetadata.Columns, &storepb.ColumnMetadata{
diff --git a/backend/plugin/db/mongodb/sync.go b/backend/plugin/db/mongodb/sync.go
index 95f9d58e41..64c29aecd7 100644
--- a/backend/plugin/db/mongodb/sync.go
+++ b/backend/plugin/db/mongodb/sync.go
@@ -4,7 +4,7 @@ import (
"context"
"encoding/json"
"log/slog"
- "sort"
+ "slices"
"strings"
"go.mongodb.org/mongo-driver/v2/bson"
@@ -136,8 +136,8 @@ func (d *Driver) SyncDBSchema(ctx context.Context) (*storepb.DatabaseSchemaMetad
if err := collectionList.Close(ctx); err != nil {
return nil, errors.Wrap(err, "failed to close collection list")
}
- sort.Strings(collectionNames)
- sort.Strings(viewNames)
+ slices.Sort(collectionNames)
+ slices.Sort(viewNames)
for _, collectionName := range collectionNames {
if systemCollection[collectionName] {
@@ -251,7 +251,7 @@ func getIndexes(ctx context.Context, collection *mongo.Collection) ([]*storepb.I
for name := range indexMap {
indexNames = append(indexNames, name)
}
- sort.Strings(indexNames)
+ slices.Sort(indexNames)
for _, name := range indexNames {
indexes = append(indexes, indexMap[name])
}
diff --git a/backend/plugin/db/mssql/sync.go b/backend/plugin/db/mssql/sync.go
index 9c6653b31d..60352e3a94 100644
--- a/backend/plugin/db/mssql/sync.go
+++ b/backend/plugin/db/mssql/sync.go
@@ -4,7 +4,7 @@ import (
"context"
"database/sql"
"fmt"
- "sort"
+ "slices"
"strings"
"github.com/pkg/errors"
@@ -360,7 +360,7 @@ func getForeignKeys(txn *sql.Tx, schemas []string) (map[db.TableKey][]*storepb.F
for _, v := range m {
foreignkeyNames = append(foreignkeyNames, v.Name)
}
- sort.Strings(foreignkeyNames)
+ slices.Sort(foreignkeyNames)
for _, fkName := range foreignkeyNames {
result[k] = append(result[k], m[fkName])
}
@@ -623,8 +623,14 @@ func getIndexes(txn *sql.Tx, schemas []string) (map[db.TableKey][]*storepb.Index
for _, v := range m {
tableIndexes[k] = append(tableIndexes[k], v)
}
- sort.Slice(tableIndexes[k], func(i, j int) bool {
- return tableIndexes[k][i].Name < tableIndexes[k][j].Name
+ slices.SortFunc(tableIndexes[k], func(a, b *storepb.IndexMetadata) int {
+ if a.Name < b.Name {
+ return -1
+ }
+ if a.Name > b.Name {
+ return 1
+ }
+ return 0
})
}
return tableIndexes, nil
@@ -713,8 +719,14 @@ func getKeys(txn *sql.Tx, schemas []string) (map[db.TableKey][]*storepb.IndexMet
for _, v := range m {
tableIndexes[k] = append(tableIndexes[k], v)
}
- sort.Slice(tableIndexes[k], func(i, j int) bool {
- return tableIndexes[k][i].Name < tableIndexes[k][j].Name
+ slices.SortFunc(tableIndexes[k], func(a, b *storepb.IndexMetadata) int {
+ if a.Name < b.Name {
+ return -1
+ }
+ if a.Name > b.Name {
+ return 1
+ }
+ return 0
})
}
return tableIndexes, nil
diff --git a/backend/plugin/db/mysql/sync.go b/backend/plugin/db/mysql/sync.go
index 621762b0ae..e9ba044d7d 100644
--- a/backend/plugin/db/mysql/sync.go
+++ b/backend/plugin/db/mysql/sync.go
@@ -6,7 +6,7 @@ import (
"fmt"
"log/slog"
"regexp"
- "sort"
+ "slices"
"strconv"
"strings"
"unicode"
@@ -577,7 +577,7 @@ func (d *Driver) SyncDBSchema(ctx context.Context) (*storepb.DatabaseSchemaMetad
for indexName := range indexes {
indexNames = append(indexNames, indexName)
}
- sort.Strings(indexNames)
+ slices.Sort(indexNames)
for _, indexName := range indexNames {
tableMetadata.Indexes = append(tableMetadata.Indexes, indexes[indexName])
}
@@ -1318,8 +1318,13 @@ func (d *Driver) getForeignKeyList(ctx context.Context, databaseName string) (ma
orderedResult := make(map[db.TableKey][]*storepb.ForeignKeyMetadata)
for key, fks := range unordered {
- sort.Slice(fks, func(i, j int) bool {
- return fks[i].Name < fks[j].Name
+ slices.SortFunc(fks, func(x, y *storepb.ForeignKeyMetadata) int {
+ if x.Name < y.Name {
+ return -1
+ } else if x.Name > y.Name {
+ return 1
+ }
+ return 0
})
orderedResult[key] = fks
}
diff --git a/backend/plugin/db/obo/dump.go b/backend/plugin/db/obo/dump.go
index 25fb7f84aa..248a6ae984 100644
--- a/backend/plugin/db/obo/dump.go
+++ b/backend/plugin/db/obo/dump.go
@@ -7,7 +7,7 @@ import (
"fmt"
"io"
"log/slog"
- "sort"
+ "slices"
"strings"
"github.com/pkg/errors"
@@ -62,8 +62,13 @@ func assembleTableStatement(tableMap map[string]*tableSchema, out io.Writer) err
}
tableList = append(tableList, table)
}
- sort.Slice(tableList, func(i, j int) bool {
- return tableList[i].meta.TableName.String < tableList[j].meta.TableName.String
+ slices.SortFunc(tableList, func(x, y *tableSchema) int {
+ if x.meta.TableName.String < y.meta.TableName.String {
+ return -1
+ } else if x.meta.TableName.String > y.meta.TableName.String {
+ return 1
+ }
+ return 0
})
for _, table := range tableList {
diff --git a/backend/plugin/db/snowflake/sync.go b/backend/plugin/db/snowflake/sync.go
index 2dabdf617c..76d079e473 100644
--- a/backend/plugin/db/snowflake/sync.go
+++ b/backend/plugin/db/snowflake/sync.go
@@ -5,7 +5,7 @@ import (
"database/sql"
"encoding/json"
"fmt"
- "sort"
+ "slices"
"strings"
"github.com/pkg/errors"
@@ -244,8 +244,14 @@ func (d *Driver) getStreamSchema(ctx context.Context, database string) (map[stri
}
for _, streamList := range streamMap {
- sort.Slice(streamList, func(i, j int) bool {
- return streamList[i].Name < streamList[j].Name
+ slices.SortFunc(streamList, func(i, j *storepb.StreamMetadata) int {
+ if i.Name < j.Name {
+ return -1
+ }
+ if i.Name > j.Name {
+ return 1
+ }
+ return 0
})
}
return streamMap, nil
@@ -380,8 +386,14 @@ func (d *Driver) getTaskSchema(ctx context.Context, database string) (map[string
}
for _, taskList := range taskMap {
- sort.Slice(taskList, func(i, j int) bool {
- return taskList[i].Name < taskList[j].Name
+ slices.SortFunc(taskList, func(i, j *storepb.TaskMetadata) int {
+ if i.Name < j.Name {
+ return -1
+ }
+ if i.Name > j.Name {
+ return 1
+ }
+ return 0
})
}
return taskMap, nil
diff --git a/backend/plugin/db/spanner/sync.go b/backend/plugin/db/spanner/sync.go
index 7868ab2fa3..50366ff49c 100644
--- a/backend/plugin/db/spanner/sync.go
+++ b/backend/plugin/db/spanner/sync.go
@@ -2,7 +2,7 @@ package spanner
import (
"context"
- "sort"
+ "slices"
"cloud.google.com/go/spanner"
"cloud.google.com/go/spanner/admin/database/apiv1/databasepb"
@@ -93,7 +93,7 @@ func (d *Driver) SyncDBSchema(ctx context.Context) (*storepb.DatabaseSchemaMetad
for schemaName := range schemaNameMap {
schemaNames = append(schemaNames, schemaName)
}
- sort.Strings(schemaNames)
+ slices.Sort(schemaNames)
for _, schemaName := range schemaNames {
databaseMetadata.Schemas = append(databaseMetadata.Schemas, &storepb.SchemaMetadata{
Name: schemaName,
diff --git a/backend/plugin/db/tidb/sync.go b/backend/plugin/db/tidb/sync.go
index f0ffb50d3a..f4bf22fb63 100644
--- a/backend/plugin/db/tidb/sync.go
+++ b/backend/plugin/db/tidb/sync.go
@@ -6,7 +6,7 @@ import (
"fmt"
"log/slog"
"regexp"
- "sort"
+ "slices"
"strconv"
"strings"
"time"
@@ -420,7 +420,7 @@ func (d *Driver) SyncDBSchema(ctx context.Context) (*storepb.DatabaseSchemaMetad
for indexName := range indexes {
indexNames = append(indexNames, indexName)
}
- sort.Strings(indexNames)
+ slices.Sort(indexNames)
for _, indexName := range indexNames {
tableMetadata.Indexes = append(tableMetadata.Indexes, indexes[indexName])
}
diff --git a/backend/plugin/parser/base/span.go b/backend/plugin/parser/base/span.go
index 6d083e5ab3..5f981e2faa 100644
--- a/backend/plugin/parser/base/span.go
+++ b/backend/plugin/parser/base/span.go
@@ -2,7 +2,7 @@ package base
import (
"context"
- "sort"
+ "slices"
"strings"
"github.com/pkg/errors"
@@ -361,9 +361,14 @@ func (s *QuerySpan) ToYaml() *YamlQuerySpan {
for k := range result.SourceColumns {
yamlResult.SourceColumns = append(yamlResult.SourceColumns, k)
}
- sort.Slice(yamlResult.SourceColumns, func(i, j int) bool {
- vi, vj := yamlResult.SourceColumns[i], yamlResult.SourceColumns[j]
- return vi.String() < vj.String()
+ slices.SortFunc(yamlResult.SourceColumns, func(i, j ColumnResource) int {
+ if i.String() < j.String() {
+ return -1
+ }
+ if i.String() > j.String() {
+ return 1
+ }
+ return 0
})
for k, v := range result.SourceFieldPaths {
s, _ := v.String()
@@ -372,25 +377,40 @@ func (s *QuerySpan) ToYaml() *YamlQuerySpan {
Path: s,
})
}
- sort.Slice(yamlResult.SourceFieldPaths, func(i, j int) bool {
- vi, vj := yamlResult.SourceFieldPaths[i], yamlResult.SourceFieldPaths[j]
- return vi.Name < vj.Name
+ slices.SortFunc(yamlResult.SourceFieldPaths, func(i, j YamlQuerySpanResultSourceFieldPaths) int {
+ if i.Name < j.Name {
+ return -1
+ }
+ if i.Name > j.Name {
+ return 1
+ }
+ return 0
})
y.Results = append(y.Results, *yamlResult)
}
for k := range s.SourceColumns {
y.SourceColumns = append(y.SourceColumns, k)
}
- sort.Slice(y.SourceColumns, func(i, j int) bool {
- vi, vj := y.SourceColumns[i], y.SourceColumns[j]
- return vi.String() < vj.String()
+ slices.SortFunc(y.SourceColumns, func(i, j ColumnResource) int {
+ if i.String() < j.String() {
+ return -1
+ }
+ if i.String() > j.String() {
+ return 1
+ }
+ return 0
})
for k := range s.PredicateColumns {
y.PredicateColumns = append(y.PredicateColumns, k)
}
- sort.Slice(y.PredicateColumns, func(i, j int) bool {
- vi, vj := y.PredicateColumns[i], y.PredicateColumns[j]
- return vi.String() < vj.String()
+ slices.SortFunc(y.PredicateColumns, func(i, j ColumnResource) int {
+ if i.String() < j.String() {
+ return -1
+ }
+ if i.String() > j.String() {
+ return 1
+ }
+ return 0
})
return y
}
diff --git a/backend/plugin/parser/base/topo_sort.go b/backend/plugin/parser/base/topo_sort.go
index aac1915a1a..49024b8b9b 100644
--- a/backend/plugin/parser/base/topo_sort.go
+++ b/backend/plugin/parser/base/topo_sort.go
@@ -1,7 +1,7 @@
package base
import (
- "sort"
+ "slices"
"github.com/pkg/errors"
)
@@ -56,7 +56,7 @@ func (g *Graph) TopologicalSort() ([]string, error) {
queue = append(queue, id)
}
}
- sort.Strings(queue)
+ slices.Sort(queue)
for len(queue) > 0 {
node := queue[0]
diff --git a/backend/plugin/parser/bigquery/query_span_extractor.go b/backend/plugin/parser/bigquery/query_span_extractor.go
index 27e6d5a2e7..609d2b7517 100644
--- a/backend/plugin/parser/bigquery/query_span_extractor.go
+++ b/backend/plugin/parser/bigquery/query_span_extractor.go
@@ -2,7 +2,7 @@ package bigquery
import (
"context"
- "sort"
+ "slices"
"strings"
"github.com/antlr4-go/antlr/v4"
@@ -536,8 +536,14 @@ func (q *querySpanExtractor) starModify(fields []base.QuerySpanResult, starModif
for _, fieldItem := range fieldItemMap {
fieldItems = append(fieldItems, fieldItem)
}
- sort.Slice(fieldItems, func(i, j int) bool {
- return fieldItems[i].id < fieldItems[j].id
+ slices.SortFunc(fieldItems, func(i, j fieldItem) int {
+ if i.id < j.id {
+ return -1
+ }
+ if i.id > j.id {
+ return 1
+ }
+ return 0
})
var result []base.QuerySpanResult
diff --git a/backend/plugin/parser/elasticsearch/parser.go b/backend/plugin/parser/elasticsearch/parser.go
index 73c3007a2e..b82978e5b3 100644
--- a/backend/plugin/parser/elasticsearch/parser.go
+++ b/backend/plugin/parser/elasticsearch/parser.go
@@ -3,7 +3,7 @@ package elasticsearch
import (
"encoding/json"
"regexp"
- "sort"
+ "slices"
"strconv"
"strings"
"unicode/utf8"
@@ -197,8 +197,14 @@ func ParseElasticsearchREST(text string) (*ParseResult, error) {
// Convert Error
var syntaxErrors []*base.SyntaxError
- sort.Slice(p.errors, func(i, j int) bool {
- return p.errors[i].byteOffset < p.errors[j].byteOffset
+ slices.SortFunc(p.errors, func(a, b *syntaxError) int {
+ if a.byteOffset < b.byteOffset {
+ return -1
+ }
+ if a.byteOffset > b.byteOffset {
+ return 1
+ }
+ return 0
})
for i, err := range p.errors {
diff --git a/backend/plugin/parser/mybatis/mapper/ast/node.go b/backend/plugin/parser/mybatis/mapper/ast/node.go
index e2bd8985ad..0afc45a992 100644
--- a/backend/plugin/parser/mybatis/mapper/ast/node.go
+++ b/backend/plugin/parser/mybatis/mapper/ast/node.go
@@ -3,7 +3,7 @@ package ast
import (
"io"
- "sort"
+ "slices"
)
// Node is the interface implemented by all AST node types.
@@ -86,8 +86,14 @@ func (n *RootNode) RestoreSQLWithLineMapping(ctx *RestoreContext, w io.Writer) (
OriginalEleLine: originalEleLine,
})
}
- sort.Slice(lineMapping, func(i, j int) bool {
- return lineMapping[i].SQLLastLine < lineMapping[j].SQLLastLine
+ slices.SortFunc(lineMapping, func(a, b *MybatisSQLLineMapping) int {
+ if a.SQLLastLine < b.SQLLastLine {
+ return -1
+ }
+ if a.SQLLastLine > b.SQLLastLine {
+ return 1
+ }
+ return 0
})
return lineMapping, nil
}
diff --git a/backend/plugin/parser/mysql/backup.go b/backend/plugin/parser/mysql/backup.go
index e7b04bec39..c7b8b8a8b1 100644
--- a/backend/plugin/parser/mysql/backup.go
+++ b/backend/plugin/parser/mysql/backup.go
@@ -4,7 +4,7 @@ import (
"context"
"fmt"
"log/slog"
- "sort"
+ "slices"
"strings"
"github.com/antlr4-go/antlr/v4"
@@ -131,14 +131,26 @@ func generateSQL(ctx context.Context, tCtx base.TransformContext, statementInfoL
result = append(result, *backupStatement)
}
- sort.Slice(result, func(i, j int) bool {
- if result[i].StartPosition.Line != result[j].StartPosition.Line {
- return result[i].StartPosition.Line < result[j].StartPosition.Line
+ slices.SortFunc(result, func(i, j base.BackupStatement) int {
+ if i.StartPosition.Line != j.StartPosition.Line {
+ if i.StartPosition.Line < j.StartPosition.Line {
+ return -1
+ }
+ return 1
+ }
+ if i.StartPosition.Column != j.StartPosition.Column {
+ if i.StartPosition.Column < j.StartPosition.Column {
+ return -1
+ }
+ return 1
+ }
+ if i.SourceTableName < j.SourceTableName {
+ return -1
}
- if result[i].StartPosition.Column != result[j].StartPosition.Column {
- return result[i].StartPosition.Column < result[j].StartPosition.Column
+ if i.SourceTableName > j.SourceTableName {
+ return 1
}
- return result[i].SourceTableName < result[j].SourceTableName
+ return 0
})
return result, nil
diff --git a/backend/plugin/parser/mysql/backup_test.go b/backend/plugin/parser/mysql/backup_test.go
index 1bbf94f3ed..2e9dcbf9a8 100644
--- a/backend/plugin/parser/mysql/backup_test.go
+++ b/backend/plugin/parser/mysql/backup_test.go
@@ -4,7 +4,7 @@ import (
"context"
"io"
"os"
- "sort"
+ "slices"
"testing"
"github.com/stretchr/testify/require"
@@ -47,11 +47,21 @@ func TestBackup(t *testing.T) {
IsCaseSensitive: false,
}, t.Input, "db", "backupDB", "_rollback")
a.NoError(err)
- sort.Slice(result, func(i, j int) bool {
- if result[i].TargetTableName == result[j].TargetTableName {
- return result[i].Statement < result[j].Statement
+ slices.SortFunc(result, func(x, y base.BackupStatement) int {
+ if x.TargetTableName == y.TargetTableName {
+ if x.Statement < y.Statement {
+ return -1
+ } else if x.Statement > y.Statement {
+ return 1
+ }
+ return 0
}
- return result[i].TargetTableName < result[j].TargetTableName
+ if x.TargetTableName < y.TargetTableName {
+ return -1
+ } else if x.TargetTableName > y.TargetTableName {
+ return 1
+ }
+ return 0
})
if record {
diff --git a/backend/plugin/parser/mysql/completion.go b/backend/plugin/parser/mysql/completion.go
index 5f9ea063c4..37e02d99ba 100644
--- a/backend/plugin/parser/mysql/completion.go
+++ b/backend/plugin/parser/mysql/completion.go
@@ -3,7 +3,7 @@ package mysql
import (
"context"
"fmt"
- "sort"
+ "slices"
"strings"
"unicode"
@@ -702,9 +702,7 @@ func (c *Completer) fetchSelectItemAliases(ruleStack []*base.RuleContext) []stri
for alias := range aliasMap {
result = append(result, alias)
}
- sort.Slice(result, func(i, j int) bool {
- return result[i] < result[j]
- })
+ slices.Sort(result)
return result
case mysql.MySQLParserRULE_orderClause, mysql.MySQLParserRULE_groupByClause, mysql.MySQLParserRULE_havingClause:
canUseAliases = true
@@ -1309,11 +1307,20 @@ func (m CompletionMap) toSLice() []base.Candidate {
for _, candidate := range m {
result = append(result, candidate)
}
- sort.Slice(result, func(i, j int) bool {
- if result[i].Type != result[j].Type {
- return result[i].Type < result[j].Type
+ slices.SortFunc(result, func(i, j base.Candidate) int {
+ if i.Type != j.Type {
+ if i.Type < j.Type {
+ return -1
+ }
+ return 1
+ }
+ if i.Text < j.Text {
+ return -1
+ }
+ if i.Text > j.Text {
+ return 1
}
- return result[i].Text < result[j].Text
+ return 0
})
return result
}
diff --git a/backend/plugin/parser/mysql/completion_test.go b/backend/plugin/parser/mysql/completion_test.go
index ced32807e6..b941361aa7 100644
--- a/backend/plugin/parser/mysql/completion_test.go
+++ b/backend/plugin/parser/mysql/completion_test.go
@@ -4,7 +4,7 @@ import (
"context"
"io"
"os"
- "sort"
+ "slices"
"testing"
"github.com/stretchr/testify/require"
@@ -57,14 +57,25 @@ func TestCompletion(t *testing.T) {
filteredResult = append(filteredResult, r)
}
}
- sort.Slice(filteredResult, func(i, j int) bool {
- if filteredResult[i].Type != filteredResult[j].Type {
- return filteredResult[i].Type < filteredResult[j].Type
+ slices.SortFunc(filteredResult, func(x, y base.Candidate) int {
+ if x.Type != y.Type {
+ if x.Type < y.Type {
+ return -1
+ }
+ return 1
}
- if filteredResult[i].Text != filteredResult[j].Text {
- return filteredResult[i].Text < filteredResult[j].Text
+ if x.Text != y.Text {
+ if x.Text < y.Text {
+ return -1
+ }
+ return 1
}
- return filteredResult[i].Definition < filteredResult[j].Definition
+ if x.Definition < y.Definition {
+ return -1
+ } else if x.Definition > y.Definition {
+ return 1
+ }
+ return 0
})
if record {
diff --git a/backend/plugin/parser/mysql/differ.go b/backend/plugin/parser/mysql/differ.go
index 119f1fcae8..3e5ed17d2f 100644
--- a/backend/plugin/parser/mysql/differ.go
+++ b/backend/plugin/parser/mysql/differ.go
@@ -5,7 +5,7 @@ import (
"fmt"
"io"
"reflect"
- "sort"
+ "slices"
"strconv"
"strings"
@@ -613,8 +613,14 @@ func convertColumnMapToSortedList(columns map[string]*columnDef) (newColumns []*
for _, column := range columns {
newColumns = append(newColumns, column)
}
- sort.Slice(newColumns, func(i, j int) bool {
- return newColumns[i].id < newColumns[j].id
+ slices.SortFunc(newColumns, func(a, b *columnDef) int {
+ if a.id < b.id {
+ return -1
+ }
+ if a.id > b.id {
+ return 1
+ }
+ return 0
})
return newColumns
}
@@ -839,8 +845,14 @@ func (diff *diffNode) deparse() (string, error) {
}
func sortAndWriteAlertTableOptionList(buf *strings.Builder, tableOptions []*tableOptionDef) error {
- sort.Slice(tableOptions, func(i, j int) bool {
- return tableOptions[i].tableName < tableOptions[j].tableName
+ slices.SortFunc(tableOptions, func(a, b *tableOptionDef) int {
+ if a.tableName < b.tableName {
+ return -1
+ }
+ if a.tableName > b.tableName {
+ return 1
+ }
+ return 0
})
for _, tableOption := range tableOptions {
@@ -859,10 +871,16 @@ func writeAlertTableOptionStatement(buf *strings.Builder, tableOption *tableOpti
}
func sortAndWriteDropCheckConstraintList(buf *strings.Builder, checks []*checkDef) error {
- sort.Slice(checks, func(i, j int) bool {
- c1 := fmt.Sprintf("%s.%s", checks[i].tableName, checks[i].name)
- c2 := fmt.Sprintf("%s.%s", checks[j].tableName, checks[j].name)
- return c1 < c2
+ slices.SortFunc(checks, func(a, b *checkDef) int {
+ c1 := fmt.Sprintf("%s.%s", a.tableName, a.name)
+ c2 := fmt.Sprintf("%s.%s", b.tableName, b.name)
+ if c1 < c2 {
+ return -1
+ }
+ if c1 > c2 {
+ return 1
+ }
+ return 0
})
for _, check := range checks {
@@ -881,10 +899,16 @@ func writeDropCheckConstraintStatement(buf *strings.Builder, check *checkDef) er
}
func sortAndWriteAddCheckConstraintList(buf *strings.Builder, checks []*checkDef) error {
- sort.Slice(checks, func(i, j int) bool {
- c1 := fmt.Sprintf("%s.%s", checks[i].tableName, checks[i].name)
- c2 := fmt.Sprintf("%s.%s", checks[j].tableName, checks[j].name)
- return c1 < c2
+ slices.SortFunc(checks, func(a, b *checkDef) int {
+ c1 := fmt.Sprintf("%s.%s", a.tableName, a.name)
+ c2 := fmt.Sprintf("%s.%s", b.tableName, b.name)
+ if c1 < c2 {
+ return -1
+ }
+ if c1 > c2 {
+ return 1
+ }
+ return 0
})
for _, check := range checks {
@@ -909,10 +933,16 @@ func writeAddCheckConstraintStatement(buf *strings.Builder, check *checkDef) err
}
func sortAndWriteAddForeignKeyList(buf *strings.Builder, fks []*foreignKeyDef) error {
- sort.Slice(fks, func(i, j int) bool {
- c1 := fmt.Sprintf("%s.%s", fks[i].tableName, fks[i].name)
- c2 := fmt.Sprintf("%s.%s", fks[j].tableName, fks[j].name)
- return c1 < c2
+ slices.SortFunc(fks, func(a, b *foreignKeyDef) int {
+ c1 := fmt.Sprintf("%s.%s", a.tableName, a.name)
+ c2 := fmt.Sprintf("%s.%s", b.tableName, b.name)
+ if c1 < c2 {
+ return -1
+ }
+ if c1 > c2 {
+ return 1
+ }
+ return 0
})
for _, fk := range fks {
@@ -937,10 +967,16 @@ func writeAddForeignKeyStatement(buf *strings.Builder, fk *foreignKeyDef) error
}
func sortAndWriteDropForeignKeyList(buf *strings.Builder, fks []*foreignKeyDef) error {
- sort.Slice(fks, func(i, j int) bool {
- c1 := fmt.Sprintf("%s.%s", fks[i].tableName, fks[i].name)
- c2 := fmt.Sprintf("%s.%s", fks[j].tableName, fks[j].name)
- return c1 < c2
+ slices.SortFunc(fks, func(a, b *foreignKeyDef) int {
+ c1 := fmt.Sprintf("%s.%s", a.tableName, a.name)
+ c2 := fmt.Sprintf("%s.%s", b.tableName, b.name)
+ if c1 < c2 {
+ return -1
+ }
+ if c1 > c2 {
+ return 1
+ }
+ return 0
})
for _, fk := range fks {
@@ -1135,10 +1171,16 @@ func writeCreateTriggerStatement(buf *strings.Builder, trigger *triggerDef) erro
}
func sortAndWriteDropIndexList(buf *strings.Builder, indexes []*indexDef) error {
- sort.Slice(indexes, func(i, j int) bool {
- c1 := fmt.Sprintf("%s.%s", indexes[i].tableName, indexes[i].name)
- c2 := fmt.Sprintf("%s.%s", indexes[j].tableName, indexes[j].name)
- return c1 < c2
+ slices.SortFunc(indexes, func(a, b *indexDef) int {
+ c1 := fmt.Sprintf("%s.%s", a.tableName, a.name)
+ c2 := fmt.Sprintf("%s.%s", b.tableName, b.name)
+ if c1 < c2 {
+ return -1
+ }
+ if c1 > c2 {
+ return 1
+ }
+ return 0
})
for _, index := range indexes {
@@ -1157,10 +1199,16 @@ func writeDropIndexStatement(buf *strings.Builder, index *indexDef) error {
}
func sortAndWriteDropIndexConstraintList(buf *strings.Builder, indexes []*indexConstraintDef) error {
- sort.Slice(indexes, func(i, j int) bool {
- c1 := fmt.Sprintf("%s.%s", indexes[i].tableName, indexes[i].name)
- c2 := fmt.Sprintf("%s.%s", indexes[j].tableName, indexes[j].name)
- return c1 < c2
+ slices.SortFunc(indexes, func(a, b *indexConstraintDef) int {
+ c1 := fmt.Sprintf("%s.%s", a.tableName, a.name)
+ c2 := fmt.Sprintf("%s.%s", b.tableName, b.name)
+ if c1 < c2 {
+ return -1
+ }
+ if c1 > c2 {
+ return 1
+ }
+ return 0
})
for _, index := range indexes {
@@ -1179,8 +1227,14 @@ func writeDropIndexConstraintStatement(buf *strings.Builder, index *indexConstra
}
func sortAndWriteDropViewList(buf *strings.Builder, views []*viewDef) error {
- sort.Slice(views, func(i, j int) bool {
- return views[i].name < views[j].name
+ slices.SortFunc(views, func(a, b *viewDef) int {
+ if a.name < b.name {
+ return -1
+ }
+ if a.name > b.name {
+ return 1
+ }
+ return 0
})
for _, view := range views {
@@ -1200,8 +1254,14 @@ func writeDropViewStatement(buf *strings.Builder, view *viewDef) error {
}
func sortAndWriteDropTableList(buf *strings.Builder, ns []*tableDef) error {
- sort.Slice(ns, func(i, j int) bool {
- return ns[i].id < ns[j].id
+ slices.SortFunc(ns, func(a, b *tableDef) int {
+ if a.id < b.id {
+ return -1
+ }
+ if a.id > b.id {
+ return 1
+ }
+ return 0
})
for _, table := range ns {
@@ -1220,8 +1280,14 @@ func writeDropTableStatement(buf *strings.Builder, table *tableDef) error {
}
func sortAndWriteCreateTableList(buf *strings.Builder, ns []*tableDef) error {
- sort.Slice(ns, func(i, j int) bool {
- return ns[i].name < ns[j].name
+ slices.SortFunc(ns, func(a, b *tableDef) int {
+ if a.name < b.name {
+ return -1
+ }
+ if a.name > b.name {
+ return 1
+ }
+ return 0
})
for _, table := range ns {
@@ -1254,10 +1320,16 @@ func writeCreateTableStatement(buf *strings.Builder, table *tableDef) error {
}
func sortAndWriteDropColumnList(buf *strings.Builder, columns []*columnDef) error {
- sort.Slice(columns, func(i, j int) bool {
- c1 := fmt.Sprintf("%s.%s", columns[i].tableName, columns[i].name)
- c2 := fmt.Sprintf("%s.%s", columns[j].tableName, columns[j].name)
- return c1 < c2
+ slices.SortFunc(columns, func(a, b *columnDef) int {
+ c1 := fmt.Sprintf("%s.%s", a.tableName, a.name)
+ c2 := fmt.Sprintf("%s.%s", b.tableName, b.name)
+ if c1 < c2 {
+ return -1
+ }
+ if c1 > c2 {
+ return 1
+ }
+ return 0
})
for _, column := range columns {
@@ -1312,8 +1384,14 @@ func writeAddColumnStatement(buf *strings.Builder, column *columnDef) error {
}
func sortAndWriteCreateTempViewList(buf *strings.Builder, views []*viewDef) error {
- sort.Slice(views, func(i, j int) bool {
- return views[i].name < views[j].name
+ slices.SortFunc(views, func(a, b *viewDef) int {
+ if a.name < b.name {
+ return -1
+ }
+ if a.name > b.name {
+ return 1
+ }
+ return 0
})
for _, view := range views {
@@ -1338,7 +1416,7 @@ func sortAndWriteAlterTablePartitionedByList(buf *strings.Builder, partitions ma
for tableName := range partitions {
tableNames = append(tableNames, tableName)
}
- sort.Strings(tableNames)
+ slices.Sort(tableNames)
for _, tableName := range tableNames {
var body strings.Builder
if _, err := body.WriteString(fmt.Sprintf("ALTER TABLE `%s` PARTITION BY ", tableName)); err != nil {
@@ -1562,8 +1640,14 @@ func (p *partitionState) toString(buf io.StringWriter) error {
for _, partition := range p.partitions {
sortedPartitions = append(sortedPartitions, partition)
}
- sort.Slice(sortedPartitions, func(i, j int) bool {
- return sortedPartitions[i].id < sortedPartitions[j].id
+ slices.SortFunc(sortedPartitions, func(a, b *partitionDefinition) int {
+ if a.id < b.id {
+ return -1
+ }
+ if a.id > b.id {
+ return 1
+ }
+ return 0
})
if _, err := buf.WriteString("\n("); err != nil {
return err
@@ -1604,8 +1688,14 @@ func (p *partitionState) toString(buf io.StringWriter) error {
for _, subPartition := range partition.subpartitions {
sortedSubpartitions = append(sortedSubpartitions, subPartition)
}
- sort.Slice(sortedSubpartitions, func(i, j int) bool {
- return sortedSubpartitions[i].id < sortedSubpartitions[j].id
+ slices.SortFunc(sortedSubpartitions, func(a, b *partitionDefinition) int {
+ if a.id < b.id {
+ return -1
+ }
+ if a.id > b.id {
+ return 1
+ }
+ return 0
})
for j, subPartition := range sortedSubpartitions {
if _, err := buf.WriteString(fmt.Sprintf("SUBPARTITION %s", subPartition.name)); err != nil {
@@ -1704,8 +1794,14 @@ func splitPartitionExprIntoFields(expr string) []string {
}
func sortAndWriteCreateViewList(buf *strings.Builder, views []*viewDef) error {
- sort.Slice(views, func(i, j int) bool {
- return views[i].name < views[j].name
+ slices.SortFunc(views, func(a, b *viewDef) int {
+ if a.name < b.name {
+ return -1
+ }
+ if a.name > b.name {
+ return 1
+ }
+ return 0
})
for _, view := range views {
@@ -1761,10 +1857,16 @@ func writeCreateViewStatement(buf *strings.Builder, view *viewDef) error {
}
func sortAndWriteModifyColumnList(buf *strings.Builder, columns []*columnDef) error {
- sort.Slice(columns, func(i, j int) bool {
- c1 := fmt.Sprintf("%s.%s", columns[i].tableName, columns[i].name)
- c2 := fmt.Sprintf("%s.%s", columns[j].tableName, columns[j].name)
- return c1 < c2
+ slices.SortFunc(columns, func(a, b *columnDef) int {
+ c1 := fmt.Sprintf("%s.%s", a.tableName, a.name)
+ c2 := fmt.Sprintf("%s.%s", b.tableName, b.name)
+ if c1 < c2 {
+ return -1
+ }
+ if c1 > c2 {
+ return 1
+ }
+ return 0
})
for _, column := range columns {
@@ -1795,10 +1897,16 @@ func writeModifyColumnStatement(buf *strings.Builder, column *columnDef) error {
}
func sortAndWriteCreateIndexList(buf *strings.Builder, indexes []*indexDef) error {
- sort.Slice(indexes, func(i, j int) bool {
- c1 := fmt.Sprintf("%s.%s", indexes[i].tableName, indexes[i].name)
- c2 := fmt.Sprintf("%s.%s", indexes[j].tableName, indexes[j].name)
- return c1 < c2
+ slices.SortFunc(indexes, func(a, b *indexDef) int {
+ c1 := fmt.Sprintf("%s.%s", a.tableName, a.name)
+ c2 := fmt.Sprintf("%s.%s", b.tableName, b.name)
+ if c1 < c2 {
+ return -1
+ }
+ if c1 > c2 {
+ return 1
+ }
+ return 0
})
for _, index := range indexes {
@@ -1823,10 +1931,16 @@ func writeCreateIndexStatement(buf *strings.Builder, index *indexDef) error {
}
func sortAndWriteCreateIndexConstraintList(buf *strings.Builder, indexes []*indexConstraintDef) error {
- sort.Slice(indexes, func(i, j int) bool {
- c1 := fmt.Sprintf("%s.%s", indexes[i].tableName, indexes[i].name)
- c2 := fmt.Sprintf("%s.%s", indexes[j].tableName, indexes[j].name)
- return c1 < c2
+ slices.SortFunc(indexes, func(a, b *indexConstraintDef) int {
+ c1 := fmt.Sprintf("%s.%s", a.tableName, a.name)
+ c2 := fmt.Sprintf("%s.%s", b.tableName, b.name)
+ if c1 < c2 {
+ return -1
+ }
+ if c1 > c2 {
+ return 1
+ }
+ return 0
})
for _, index := range indexes {
@@ -1865,8 +1979,14 @@ func writeCreateIndexConstraintStatement(buf *strings.Builder, index *indexConst
}
func sortAndWriteCreatePrimaryIndexList(buf *strings.Builder, primaryKeys []*primaryKeyDef) error {
- sort.Slice(primaryKeys, func(i, j int) bool {
- return primaryKeys[i].tableName < primaryKeys[j].tableName
+ slices.SortFunc(primaryKeys, func(a, b *primaryKeyDef) int {
+ if a.tableName < b.tableName {
+ return -1
+ }
+ if a.tableName > b.tableName {
+ return 1
+ }
+ return 0
})
for _, primaryKey := range primaryKeys {
@@ -1890,8 +2010,14 @@ func writeCreatePrimaryIndexStatement(buf *strings.Builder, primary *primaryKeyD
}
func sortAndWriteDropPrimaryIndexList(buf *strings.Builder, primaryKeys []*primaryKeyDef) error {
- sort.Slice(primaryKeys, func(i, j int) bool {
- return primaryKeys[i].tableName < primaryKeys[j].tableName
+ slices.SortFunc(primaryKeys, func(a, b *primaryKeyDef) int {
+ if a.tableName < b.tableName {
+ return -1
+ }
+ if a.tableName > b.tableName {
+ return 1
+ }
+ return 0
})
for _, primaryKey := range primaryKeys {
@@ -2754,9 +2880,7 @@ func (t *mysqlTransformer) ExitCreateView(_ *mysql.CreateViewContext) {
// the column order of createView is decided by createView statement.
// we get columns here only for create temp view.
// only for test-case.
- sort.Slice(result, func(i, j int) bool {
- return result[i] < result[j]
- })
+ slices.Sort(result)
view.columns = result
}
diff --git a/backend/plugin/parser/mysql/query_span_extractor.go b/backend/plugin/parser/mysql/query_span_extractor.go
index c27e50e2fb..1cdef6c9ec 100644
--- a/backend/plugin/parser/mysql/query_span_extractor.go
+++ b/backend/plugin/parser/mysql/query_span_extractor.go
@@ -2,7 +2,7 @@ package mysql
import (
"context"
- "sort"
+ "slices"
"strings"
"github.com/antlr4-go/antlr/v4"
@@ -1623,8 +1623,14 @@ func extractTableRefs(database string, ctx antlr.ParserRuleContext) ([]base.Sche
result = append(result, resource)
}
- sort.Slice(result, func(i, j int) bool {
- return result[i].String() < result[j].String()
+ slices.SortFunc(result, func(i, j base.SchemaResource) int {
+ if i.String() < j.String() {
+ return -1
+ }
+ if i.String() > j.String() {
+ return 1
+ }
+ return 0
})
return result, nil
diff --git a/backend/plugin/parser/partiql/completion.go b/backend/plugin/parser/partiql/completion.go
index 398064ab01..a0b666f70a 100644
--- a/backend/plugin/parser/partiql/completion.go
+++ b/backend/plugin/parser/partiql/completion.go
@@ -2,7 +2,7 @@ package partiql
import (
"context"
- "sort"
+ "slices"
"strings"
"github.com/antlr4-go/antlr/v4"
@@ -83,11 +83,20 @@ func (m CompletionMap) toSlice() []base.Candidate {
for _, candidate := range m {
result = append(result, candidate)
}
- sort.Slice(result, func(i, j int) bool {
- if result[i].Type != result[j].Type {
- return result[i].Type < result[j].Type
+ slices.SortFunc(result, func(a, b base.Candidate) int {
+ if a.Type != b.Type {
+ if a.Type < b.Type {
+ return -1
+ }
+ return 1
+ }
+ if a.Text < b.Text {
+ return -1
+ }
+ if a.Text > b.Text {
+ return 1
}
- return result[i].Text < result[j].Text
+ return 0
})
return result
}
diff --git a/backend/plugin/parser/pg/backup.go b/backend/plugin/parser/pg/backup.go
index b6b2e25803..9d99a33834 100644
--- a/backend/plugin/parser/pg/backup.go
+++ b/backend/plugin/parser/pg/backup.go
@@ -3,7 +3,7 @@ package pg
import (
"context"
"fmt"
- "sort"
+ "slices"
"strings"
"github.com/antlr4-go/antlr/v4"
@@ -99,14 +99,26 @@ func generateSQL(statementInfoList []statementInfo, targetSchema string, tablePr
result = append(result, *backupStatement)
}
- sort.Slice(result, func(i, j int) bool {
- if result[i].StartPosition.Line != result[j].StartPosition.Line {
- return result[i].StartPosition.Line < result[j].StartPosition.Line
+ slices.SortFunc(result, func(a, b base.BackupStatement) int {
+ if a.StartPosition.Line != b.StartPosition.Line {
+ if a.StartPosition.Line < b.StartPosition.Line {
+ return -1
+ }
+ return 1
+ }
+ if a.StartPosition.Column != b.StartPosition.Column {
+ if a.StartPosition.Column < b.StartPosition.Column {
+ return -1
+ }
+ return 1
+ }
+ if a.SourceTableName < b.SourceTableName {
+ return -1
}
- if result[i].StartPosition.Column != result[j].StartPosition.Column {
- return result[i].StartPosition.Column < result[j].StartPosition.Column
+ if a.SourceTableName > b.SourceTableName {
+ return 1
}
- return result[i].SourceTableName < result[j].SourceTableName
+ return 0
})
return result, nil
diff --git a/backend/plugin/parser/pg/backup_test.go b/backend/plugin/parser/pg/backup_test.go
index 3225bde1a5..a27afc47f8 100644
--- a/backend/plugin/parser/pg/backup_test.go
+++ b/backend/plugin/parser/pg/backup_test.go
@@ -4,7 +4,7 @@ import (
"context"
"io"
"os"
- "sort"
+ "slices"
"testing"
"github.com/stretchr/testify/require"
@@ -43,11 +43,23 @@ func TestBackup(t *testing.T) {
DatabaseName: "",
}, t.Input, "", "backupSchema", "rollback")
a.NoError(err)
- sort.Slice(result, func(i, j int) bool {
- if result[i].TargetTableName == result[j].TargetTableName {
- return result[i].Statement < result[j].Statement
+ slices.SortFunc(result, func(i, j base.BackupStatement) int {
+ if i.TargetTableName == j.TargetTableName {
+ if i.Statement < j.Statement {
+ return -1
+ }
+ if i.Statement > j.Statement {
+ return 1
+ }
+ return 0
}
- return result[i].TargetTableName < result[j].TargetTableName
+ if i.TargetTableName < j.TargetTableName {
+ return -1
+ }
+ if i.TargetTableName > j.TargetTableName {
+ return 1
+ }
+ return 0
})
if record {
diff --git a/backend/plugin/parser/pg/completion.go b/backend/plugin/parser/pg/completion.go
index 2576acaf00..345d2dfd15 100644
--- a/backend/plugin/parser/pg/completion.go
+++ b/backend/plugin/parser/pg/completion.go
@@ -3,7 +3,7 @@ package pg
import (
"context"
"fmt"
- "sort"
+ "slices"
"strings"
"unicode"
@@ -443,11 +443,20 @@ func (m CompletionMap) toSlice() []base.Candidate {
for _, candidate := range m {
result = append(result, candidate)
}
- sort.Slice(result, func(i, j int) bool {
- if result[i].Type != result[j].Type {
- return result[i].Type < result[j].Type
+ slices.SortFunc(result, func(a, b base.Candidate) int {
+ if a.Type != b.Type {
+ if a.Type < b.Type {
+ return -1
+ }
+ return 1
+ }
+ if a.Text < b.Text {
+ return -1
}
- return result[i].Text < result[j].Text
+ if a.Text > b.Text {
+ return 1
+ }
+ return 0
})
return result
}
@@ -759,9 +768,7 @@ func (c *Completer) fetchSelectItemAliases(ruleStack []*base.RuleContext) []stri
for alias := range aliasMap {
result = append(result, alias)
}
- sort.Slice(result, func(i, j int) bool {
- return result[i] < result[j]
- })
+ slices.Sort(result)
return result
case pg.PostgreSQLParserRULE_opt_sort_clause, pg.PostgreSQLParserRULE_group_clause, pg.PostgreSQLParserRULE_having_clause:
canUseAliases = true
diff --git a/backend/plugin/parser/pg/completion_test.go b/backend/plugin/parser/pg/completion_test.go
index 190afa4891..2e9089ca8a 100644
--- a/backend/plugin/parser/pg/completion_test.go
+++ b/backend/plugin/parser/pg/completion_test.go
@@ -4,7 +4,7 @@ import (
"context"
"io"
"os"
- "sort"
+ "slices"
"testing"
"github.com/stretchr/testify/require"
@@ -57,14 +57,25 @@ func TestCompletion(t *testing.T) {
filteredResult = append(filteredResult, r)
}
}
- sort.Slice(filteredResult, func(i, j int) bool {
- if filteredResult[i].Type != filteredResult[j].Type {
- return filteredResult[i].Type < filteredResult[j].Type
+ slices.SortFunc(filteredResult, func(x, y base.Candidate) int {
+ if x.Type != y.Type {
+ if x.Type < y.Type {
+ return -1
+ }
+ return 1
}
- if filteredResult[i].Text != filteredResult[j].Text {
- return filteredResult[i].Text < filteredResult[j].Text
+ if x.Text != y.Text {
+ if x.Text < y.Text {
+ return -1
+ }
+ return 1
}
- return filteredResult[i].Definition < filteredResult[j].Definition
+ if x.Definition < y.Definition {
+ return -1
+ } else if x.Definition > y.Definition {
+ return 1
+ }
+ return 0
})
if record {
diff --git a/backend/plugin/parser/pg/differ.go b/backend/plugin/parser/pg/differ.go
index d1c8715544..60c75697e2 100644
--- a/backend/plugin/parser/pg/differ.go
+++ b/backend/plugin/parser/pg/differ.go
@@ -7,7 +7,7 @@ import (
"fmt"
"io"
"log/slog"
- "sort"
+ "slices"
"github.com/pkg/errors"
@@ -2470,8 +2470,14 @@ func (diff *diffNode) dropConstraint(m schemaMap) {
constraintInfoList = append(constraintInfoList, constraintInfo)
}
}
- sort.Slice(constraintInfoList, func(i, j int) bool {
- return constraintInfoList[i].id < constraintInfoList[j].id
+ slices.SortFunc(constraintInfoList, func(i, j *constraintInfo) int {
+ if i.id < j.id {
+ return -1
+ }
+ if i.id > j.id {
+ return 1
+ }
+ return 0
})
var dropConstraintList []*ast.ConstraintDef
for _, constraintInfo := range constraintInfoList {
@@ -2497,8 +2503,14 @@ func dropMaterializedView(m schemaMap) []*ast.CreateMaterializedViewStmt {
if len(materializedViewList) == 0 {
return nil
}
- sort.Slice(materializedViewList, func(i, j int) bool {
- return materializedViewList[i].id < materializedViewList[j].id
+ slices.SortFunc(materializedViewList, func(i, j *materializedViewInfo) int {
+ if i.id < j.id {
+ return -1
+ }
+ if i.id > j.id {
+ return 1
+ }
+ return 0
})
var materializedViewDefList []*ast.CreateMaterializedViewStmt
@@ -2522,8 +2534,14 @@ func dropView(m schemaMap) []*ast.CreateViewStmt {
if len(viewList) == 0 {
return nil
}
- sort.Slice(viewList, func(i, j int) bool {
- return viewList[i].id < viewList[j].id
+ slices.SortFunc(viewList, func(i, j *viewInfo) int {
+ if i.id < j.id {
+ return -1
+ }
+ if i.id > j.id {
+ return 1
+ }
+ return 0
})
var viewDefList []*ast.CreateViewStmt
@@ -2547,8 +2565,14 @@ func dropTable(m schemaMap) []*ast.CreateTableStmt {
if len(tableList) == 0 {
return nil
}
- sort.Slice(tableList, func(i, j int) bool {
- return tableList[i].id < tableList[j].id
+ slices.SortFunc(tableList, func(i, j *tableInfo) int {
+ if i.id < j.id {
+ return -1
+ }
+ if i.id > j.id {
+ return 1
+ }
+ return 0
})
var result []*ast.CreateTableStmt
@@ -2569,8 +2593,14 @@ func dropSchema(m schemaMap) *ast.DropSchemaStmt {
if len(schemaList) == 0 {
return nil
}
- sort.Slice(schemaList, func(i, j int) bool {
- return schemaList[i].id < schemaList[j].id
+ slices.SortFunc(schemaList, func(i, j *schemaInfo) int {
+ if i.id < j.id {
+ return -1
+ }
+ if i.id > j.id {
+ return 1
+ }
+ return 0
})
var schemaNameList []string
@@ -2596,8 +2626,14 @@ func dropExtension(m schemaMap) *ast.DropExtensionStmt {
if len(extensionList) == 0 {
return nil
}
- sort.Slice(extensionList, func(i, j int) bool {
- return extensionList[i].id < extensionList[j].id
+ slices.SortFunc(extensionList, func(i, j *extensionInfo) int {
+ if i.id < j.id {
+ return -1
+ }
+ if i.id > j.id {
+ return 1
+ }
+ return 0
})
var extensionNameList []string
@@ -2623,8 +2659,14 @@ func dropFunction(m schemaMap) []*ast.CreateFunctionStmt {
if len(functionList) == 0 {
return nil
}
- sort.Slice(functionList, func(i, j int) bool {
- return functionList[i].id < functionList[j].id
+ slices.SortFunc(functionList, func(i, j *functionInfo) int {
+ if i.id < j.id {
+ return -1
+ }
+ if i.id > j.id {
+ return 1
+ }
+ return 0
})
var result []*ast.CreateFunctionStmt
@@ -2653,8 +2695,14 @@ func (diff *diffNode) dropComment(m schemaMap) {
views = append(views, view)
}
}
- sort.Slice(tables, func(i, j int) bool {
- return tables[i].id < tables[j].id
+ slices.SortFunc(tables, func(i, j *tableInfo) int {
+ if i.id < j.id {
+ return -1
+ }
+ if i.id > j.id {
+ return 1
+ }
+ return 0
})
for _, table := range tables {
@@ -2677,8 +2725,14 @@ func (diff *diffNode) dropComment(m schemaMap) {
comment: v,
})
}
- sort.Slice(columnCommentList, func(i, j int) bool {
- return columnCommentList[i].column < columnCommentList[j].column
+ slices.SortFunc(columnCommentList, func(i, j commentInfo) int {
+ if i.column < j.column {
+ return -1
+ }
+ if i.column > j.column {
+ return 1
+ }
+ return 0
})
for _, columnComment := range columnCommentList {
diff.setCommentList = append(diff.setCommentList, &ast.CommentStmt{
@@ -2692,8 +2746,14 @@ func (diff *diffNode) dropComment(m schemaMap) {
}
}
- sort.Slice(views, func(i, j int) bool {
- return views[i].id < views[j].id
+ slices.SortFunc(views, func(i, j *viewInfo) int {
+ if i.id < j.id {
+ return -1
+ }
+ if i.id > j.id {
+ return 1
+ }
+ return 0
})
for _, view := range views {
@@ -2716,8 +2776,14 @@ func (diff *diffNode) dropComment(m schemaMap) {
comment: v,
})
}
- sort.Slice(columnCommentList, func(i, j int) bool {
- return columnCommentList[i].column < columnCommentList[j].column
+ slices.SortFunc(columnCommentList, func(i, j commentInfo) int {
+ if i.column < j.column {
+ return -1
+ }
+ if i.column > j.column {
+ return 1
+ }
+ return 0
})
for _, columnComment := range columnCommentList {
diff.setCommentList = append(diff.setCommentList, &ast.CommentStmt{
@@ -2746,8 +2812,14 @@ func (diff *diffNode) dropTypeStmt(m schemaMap) {
if len(typeList) == 0 {
return
}
- sort.Slice(typeList, func(i, j int) bool {
- return typeList[i].id < typeList[j].id
+ slices.SortFunc(typeList, func(i, j *typeInfo) int {
+ if i.id < j.id {
+ return -1
+ }
+ if i.id > j.id {
+ return 1
+ }
+ return 0
})
for _, tp := range typeList {
@@ -2773,8 +2845,14 @@ func (diff *diffNode) dropTriggerStmt(m schemaMap) {
if len(triggerList) == 0 {
return
}
- sort.Slice(triggerList, func(i, j int) bool {
- return triggerList[i].id < triggerList[j].id
+ slices.SortFunc(triggerList, func(i, j *triggerInfo) int {
+ if i.id < j.id {
+ return -1
+ }
+ if i.id > j.id {
+ return 1
+ }
+ return 0
})
for _, trigger := range triggerList {
@@ -2807,8 +2885,14 @@ func dropIndex(m schemaMap) *ast.DropIndexStmt {
if len(indexList) == 0 {
return nil
}
- sort.Slice(indexList, func(i, j int) bool {
- return indexList[i].id < indexList[j].id
+ slices.SortFunc(indexList, func(i, j *indexInfo) int {
+ if i.id < j.id {
+ return -1
+ }
+ if i.id > j.id {
+ return 1
+ }
+ return 0
})
var indexDefList []*ast.IndexDef
@@ -2838,8 +2922,14 @@ func (diff *diffNode) dropSequenceOwnedBy(m schemaMap) {
if len(sequenceOwnedByList) == 0 {
return
}
- sort.Slice(sequenceOwnedByList, func(i, j int) bool {
- return sequenceOwnedByList[i].id < sequenceOwnedByList[j].id
+ slices.SortFunc(sequenceOwnedByList, func(i, j *sequenceOwnedByInfo) int {
+ if i.id < j.id {
+ return -1
+ }
+ if i.id > j.id {
+ return 1
+ }
+ return 0
})
for _, sequenceOwnedBy := range sequenceOwnedByList {
@@ -2865,8 +2955,14 @@ func dropSequence(m schemaMap) *ast.DropSequenceStmt {
if len(sequenceList) == 0 {
return nil
}
- sort.Slice(sequenceList, func(i, j int) bool {
- return sequenceList[i].id < sequenceList[j].id
+ slices.SortFunc(sequenceList, func(i, j *sequenceInfo) int {
+ if i.id < j.id {
+ return -1
+ }
+ if i.id > j.id {
+ return 1
+ }
+ return 0
})
var sequenceNameList []*ast.SequenceNameDef
diff --git a/backend/plugin/parser/plsql/backup.go b/backend/plugin/parser/plsql/backup.go
index 9a905bdc62..1c6a5f7e21 100644
--- a/backend/plugin/parser/plsql/backup.go
+++ b/backend/plugin/parser/plsql/backup.go
@@ -3,7 +3,7 @@ package plsql
import (
"context"
"fmt"
- "sort"
+ "slices"
"strings"
"github.com/antlr4-go/antlr/v4"
@@ -91,14 +91,26 @@ func generateSQL(ctx base.TransformContext, statementInfoList []statementInfo, t
result = append(result, *backupStatement)
}
- sort.Slice(result, func(i, j int) bool {
- if result[i].StartPosition.Line != result[j].StartPosition.Line {
- return result[i].StartPosition.Line < result[j].StartPosition.Line
+ slices.SortFunc(result, func(i, j base.BackupStatement) int {
+ if i.StartPosition.Line != j.StartPosition.Line {
+ if i.StartPosition.Line < j.StartPosition.Line {
+ return -1
+ }
+ return 1
+ }
+ if i.StartPosition.Column != j.StartPosition.Column {
+ if i.StartPosition.Column < j.StartPosition.Column {
+ return -1
+ }
+ return 1
+ }
+ if i.SourceTableName < j.SourceTableName {
+ return -1
}
- if result[i].StartPosition.Column != result[j].StartPosition.Column {
- return result[i].StartPosition.Column < result[j].StartPosition.Column
+ if i.SourceTableName > j.SourceTableName {
+ return 1
}
- return result[i].SourceTableName < result[j].SourceTableName
+ return 0
})
return result, nil
diff --git a/backend/plugin/parser/plsql/backup_test.go b/backend/plugin/parser/plsql/backup_test.go
index 60ee6a39aa..81a95fd0ed 100644
--- a/backend/plugin/parser/plsql/backup_test.go
+++ b/backend/plugin/parser/plsql/backup_test.go
@@ -4,7 +4,7 @@ import (
"context"
"io"
"os"
- "sort"
+ "slices"
"testing"
"github.com/stretchr/testify/require"
@@ -40,11 +40,23 @@ func TestBackup(t *testing.T) {
for i, t := range tests {
result, err := TransformDMLToSelect(context.Background(), base.TransformContext{}, t.Input, "DB", "backupDB", "rollback")
a.NoError(err)
- sort.Slice(result, func(i, j int) bool {
- if result[i].TargetTableName == result[j].TargetTableName {
- return result[i].Statement < result[j].Statement
+ slices.SortFunc(result, func(a, b base.BackupStatement) int {
+ if a.TargetTableName == b.TargetTableName {
+ if a.Statement < b.Statement {
+ return -1
+ }
+ if a.Statement > b.Statement {
+ return 1
+ }
+ return 0
}
- return result[i].TargetTableName < result[j].TargetTableName
+ if a.TargetTableName < b.TargetTableName {
+ return -1
+ }
+ if a.TargetTableName > b.TargetTableName {
+ return 1
+ }
+ return 0
})
if record {
diff --git a/backend/plugin/parser/plsql/completion.go b/backend/plugin/parser/plsql/completion.go
index c99bfb6995..0b99a13f64 100644
--- a/backend/plugin/parser/plsql/completion.go
+++ b/backend/plugin/parser/plsql/completion.go
@@ -3,7 +3,7 @@ package plsql
import (
"context"
"fmt"
- "sort"
+ "slices"
"strings"
"unicode"
@@ -284,11 +284,20 @@ func (m CompletionMap) toSlice() []base.Candidate {
for _, candidate := range m {
result = append(result, candidate)
}
- sort.Slice(result, func(i, j int) bool {
- if result[i].Type != result[j].Type {
- return result[i].Type < result[j].Type
+ slices.SortFunc(result, func(i, j base.Candidate) int {
+ if i.Type != j.Type {
+ if i.Type < j.Type {
+ return -1
+ }
+ return 1
+ }
+ if i.Text < j.Text {
+ return -1
}
- return result[i].Text < result[j].Text
+ if i.Text > j.Text {
+ return 1
+ }
+ return 0
})
return result
}
@@ -752,9 +761,7 @@ func (c *Completer) fetchSelectItemAliases(ruleStack []*base.RuleContext) []stri
for alias := range aliasMap {
result = append(result, alias)
}
- sort.Slice(result, func(i, j int) bool {
- return result[i] < result[j]
- })
+ slices.Sort(result)
return result
}
}
diff --git a/backend/plugin/parser/plsql/completion_test.go b/backend/plugin/parser/plsql/completion_test.go
index 6acc9c3282..0cf4a464ae 100644
--- a/backend/plugin/parser/plsql/completion_test.go
+++ b/backend/plugin/parser/plsql/completion_test.go
@@ -4,7 +4,7 @@ import (
"context"
"io"
"os"
- "sort"
+ "slices"
"testing"
"github.com/stretchr/testify/require"
@@ -57,14 +57,26 @@ func TestCompletion(t *testing.T) {
filteredResult = append(filteredResult, r)
}
}
- sort.Slice(filteredResult, func(i, j int) bool {
- if filteredResult[i].Type != filteredResult[j].Type {
- return filteredResult[i].Type < filteredResult[j].Type
+ slices.SortFunc(filteredResult, func(a, b base.Candidate) int {
+ if a.Type != b.Type {
+ if a.Type < b.Type {
+ return -1
+ }
+ return 1
}
- if filteredResult[i].Text != filteredResult[j].Text {
- return filteredResult[i].Text < filteredResult[j].Text
+ if a.Text != b.Text {
+ if a.Text < b.Text {
+ return -1
+ }
+ return 1
}
- return filteredResult[i].Definition < filteredResult[j].Definition
+ if a.Definition < b.Definition {
+ return -1
+ }
+ if a.Definition > b.Definition {
+ return 1
+ }
+ return 0
})
if record {
diff --git a/backend/plugin/parser/plsql/differ.go b/backend/plugin/parser/plsql/differ.go
index eaeafc158b..b6ccbd91c4 100644
--- a/backend/plugin/parser/plsql/differ.go
+++ b/backend/plugin/parser/plsql/differ.go
@@ -3,7 +3,7 @@ package plsql
import (
"fmt"
- "sort"
+ "slices"
"strings"
"github.com/antlr4-go/antlr/v4"
@@ -143,8 +143,14 @@ func SchemaDiff(ctx base.DiffContext, oldStmt, newStmt string) (string, error) {
for _, table := range newSchemaInfo.tableMap {
newTables = append(newTables, table)
}
- sort.Slice(newTables, func(i, j int) bool {
- return newTables[i].id < newTables[j].id
+ slices.SortFunc(newTables, func(i, j *tableInfo) int {
+ if i.id < j.id {
+ return -1
+ }
+ if i.id > j.id {
+ return 1
+ }
+ return 0
})
for _, newTable := range newTables {
tableName := newTable.name
@@ -163,8 +169,14 @@ func SchemaDiff(ctx base.DiffContext, oldStmt, newStmt string) (string, error) {
for _, table := range oldSchemaInfo.tableMap {
remainingTables = append(remainingTables, table)
}
- sort.Slice(remainingTables, func(i, j int) bool {
- return remainingTables[i].id < remainingTables[j].id
+ slices.SortFunc(remainingTables, func(i, j *tableInfo) int {
+ if i.id < j.id {
+ return -1
+ }
+ if i.id > j.id {
+ return 1
+ }
+ return 0
})
for _, table := range remainingTables {
if ctx.StrictMode {
@@ -178,8 +190,14 @@ func SchemaDiff(ctx base.DiffContext, oldStmt, newStmt string) (string, error) {
for _, index := range newSchemaInfo.indexMap {
newIndexes = append(newIndexes, index)
}
- sort.Slice(newIndexes, func(i, j int) bool {
- return newIndexes[i].pos < newIndexes[j].pos
+ slices.SortFunc(newIndexes, func(i, j *indexInfo) int {
+ if i.pos < j.pos {
+ return -1
+ }
+ if i.pos > j.pos {
+ return 1
+ }
+ return 0
})
for _, newIndex := range newIndexes {
id := newIndex.id
@@ -200,8 +218,14 @@ func SchemaDiff(ctx base.DiffContext, oldStmt, newStmt string) (string, error) {
for _, index := range oldSchemaInfo.indexMap {
remainingIndexes = append(remainingIndexes, index)
}
- sort.Slice(remainingIndexes, func(i, j int) bool {
- return remainingIndexes[i].pos < remainingIndexes[j].pos
+ slices.SortFunc(remainingIndexes, func(i, j *indexInfo) int {
+ if i.pos < j.pos {
+ return -1
+ }
+ if i.pos > j.pos {
+ return 1
+ }
+ return 0
})
for _, index := range remainingIndexes {
if ctx.StrictMode {
@@ -338,9 +362,7 @@ func (diff *diffNode) diffConstraint(oldTable, newTable *tableInfo) error {
dropConstraints = append(dropConstraints, constraintName)
}
- sort.Slice(dropConstraints, func(i, j int) bool {
- return dropConstraints[i] < dropConstraints[j]
- })
+ slices.Sort(dropConstraints)
return diff.appendConstraintDiff(newTable.name, addConstraints, dropConstraints)
}
@@ -505,9 +527,7 @@ func (diff *diffNode) diffColumn(oldTable, newTable *tableInfo) error {
dropColumns = append(dropColumns, NormalizeIdentifierContext(column.Column_name().Identifier()))
}
- sort.Slice(dropColumns, func(i, j int) bool {
- return dropColumns[i] < dropColumns[j]
- })
+ slices.Sort(dropColumns)
return diff.appendColumnDiff(oldTable.name, addColumns, modifyColumns, modifyColumnVisibility, dropColumns)
}
diff --git a/backend/plugin/parser/plsql/query_span_extractor.go b/backend/plugin/parser/plsql/query_span_extractor.go
index 22b35f9aeb..7f45f487ee 100644
--- a/backend/plugin/parser/plsql/query_span_extractor.go
+++ b/backend/plugin/parser/plsql/query_span_extractor.go
@@ -2,7 +2,7 @@ package plsql
import (
"context"
- "sort"
+ "slices"
"strings"
"github.com/antlr4-go/antlr/v4"
@@ -1681,8 +1681,14 @@ func getAccessTables(currentDatabase string, tree antlr.Tree) ([]base.SchemaReso
result = append(result, resource)
}
- sort.Slice(result, func(i, j int) bool {
- return result[i].String() < result[j].String()
+ slices.SortFunc(result, func(a, b base.SchemaResource) int {
+ if a.String() < b.String() {
+ return -1
+ }
+ if a.String() > b.String() {
+ return 1
+ }
+ return 0
})
return result, nil
diff --git a/backend/plugin/parser/plsql/strings.go b/backend/plugin/parser/plsql/strings.go
index c55dc0de3b..fe5800d9d8 100644
--- a/backend/plugin/parser/plsql/strings.go
+++ b/backend/plugin/parser/plsql/strings.go
@@ -423,14 +423,14 @@ func (s *StringsManipulator) Manipulate(actions ...base.StringsManipulatorAction
addTables = append(addTables, action)
case *StringsManipulatorActionDropIndex:
indexID := indexID{
- schema: action.GetSchemaName(),
+ schema: action.SchemaName,
table: action.GetTopLevelNaming(),
index: action.GetSecondLevelNaming(),
}
indexActions[indexID] = append(indexActions[indexID], action)
case *StringsManipulatorActionModifyIndex:
indexID := indexID{
- schema: action.GetSchemaName(),
+ schema: action.SchemaName,
table: action.GetTopLevelNaming(),
index: action.GetSecondLevelNaming(),
}
diff --git a/backend/plugin/parser/snowflake/query_span_extractor.go b/backend/plugin/parser/snowflake/query_span_extractor.go
index 6d7e038ec4..6d5f626e93 100644
--- a/backend/plugin/parser/snowflake/query_span_extractor.go
+++ b/backend/plugin/parser/snowflake/query_span_extractor.go
@@ -2,7 +2,6 @@ package snowflake
import (
"context"
- "sort"
"strconv"
"github.com/antlr4-go/antlr/v4"
@@ -1316,11 +1315,7 @@ func getAccessTables(currentNormalizedDatabase string, currentNormalizedSchema s
resourceMap: make(base.SourceColumnSet),
}
- var result []base.SchemaResource
antlr.ParseTreeWalkerDefault.Walk(l, tree)
- sort.Slice(result, func(i, j int) bool {
- return result[i].String() < result[j].String()
- })
return l.resourceMap
}
diff --git a/backend/plugin/parser/spanner/query_span_extractor.go b/backend/plugin/parser/spanner/query_span_extractor.go
index 5ebf0bdc98..b2c856a61d 100644
--- a/backend/plugin/parser/spanner/query_span_extractor.go
+++ b/backend/plugin/parser/spanner/query_span_extractor.go
@@ -2,7 +2,7 @@ package spanner
import (
"context"
- "sort"
+ "slices"
"strings"
"github.com/antlr4-go/antlr/v4"
@@ -536,8 +536,13 @@ func (q *querySpanExtractor) starModify(fields []base.QuerySpanResult, starModif
for _, fieldItem := range fieldItemMap {
fieldItems = append(fieldItems, fieldItem)
}
- sort.Slice(fieldItems, func(i, j int) bool {
- return fieldItems[i].id < fieldItems[j].id
+ slices.SortFunc(fieldItems, func(x, y fieldItem) int {
+ if x.id < y.id {
+ return -1
+ } else if x.id > y.id {
+ return 1
+ }
+ return 0
})
var result []base.QuerySpanResult
diff --git a/backend/plugin/parser/sql/transform/mysql/transform.go b/backend/plugin/parser/sql/transform/mysql/transform.go
index 8595437c7a..a2d881df13 100644
--- a/backend/plugin/parser/sql/transform/mysql/transform.go
+++ b/backend/plugin/parser/sql/transform/mysql/transform.go
@@ -3,7 +3,7 @@ package mysql
import (
"bytes"
- "sort"
+ "slices"
"strings"
"github.com/pingcap/tidb/pkg/parser"
@@ -214,8 +214,14 @@ func (t *SchemaTransformer) Normalize(schema string, standard string) (string, e
missingIndexList = append(missingIndexList, index)
}
}
- sort.Slice(missingIndexList, func(i, j int) bool {
- return missingIndexList[i].originPos < missingIndexList[j].originPos
+ slices.SortFunc(missingIndexList, func(a, b *indexInfo) int {
+ if a.originPos < b.originPos {
+ return -1
+ }
+ if a.originPos > b.originPos {
+ return 1
+ }
+ return 0
})
for _, index := range missingIndexList {
orderedList = append(orderedList, index.createIndex)
@@ -238,8 +244,14 @@ func (t *SchemaTransformer) Normalize(schema string, standard string) (string, e
missingTableList = append(missingTableList, table)
}
}
- sort.Slice(missingTableList, func(i, j int) bool {
- return missingTableList[i].originPos < missingTableList[j].originPos
+ slices.SortFunc(missingTableList, func(a, b *tableInfo) int {
+ if a.originPos < b.originPos {
+ return -1
+ }
+ if a.originPos > b.originPos {
+ return 1
+ }
+ return 0
})
for _, table := range missingTableList {
orderedList = append(orderedList, table.createTable)
@@ -249,8 +261,14 @@ func (t *SchemaTransformer) Normalize(schema string, standard string) (string, e
missingIndexList = append(missingIndexList, index)
}
}
- sort.Slice(missingIndexList, func(i, j int) bool {
- return missingIndexList[i].originPos < missingIndexList[j].originPos
+ slices.SortFunc(missingIndexList, func(a, b *indexInfo) int {
+ if a.originPos < b.originPos {
+ return -1
+ }
+ if a.originPos > b.originPos {
+ return 1
+ }
+ return 0
})
for _, index := range missingIndexList {
orderedList = append(orderedList, index.createIndex)
diff --git a/backend/plugin/parser/tidb/backup_test.go b/backend/plugin/parser/tidb/backup_test.go
index 2880117339..fe0b83d2f0 100644
--- a/backend/plugin/parser/tidb/backup_test.go
+++ b/backend/plugin/parser/tidb/backup_test.go
@@ -4,7 +4,7 @@ import (
"context"
"io"
"os"
- "sort"
+ "slices"
"testing"
"github.com/stretchr/testify/require"
@@ -47,11 +47,23 @@ func TestBackup(t *testing.T) {
IsCaseSensitive: false,
}, t.Input, "db", "backupDB", "_rollback")
a.NoError(err)
- sort.Slice(result, func(i, j int) bool {
- if result[i].TargetTableName == result[j].TargetTableName {
- return result[i].Statement < result[j].Statement
+ slices.SortFunc(result, func(a, b base.BackupStatement) int {
+ if a.TargetTableName == b.TargetTableName {
+ if a.Statement < b.Statement {
+ return -1
+ }
+ if a.Statement > b.Statement {
+ return 1
+ }
+ return 0
}
- return result[i].TargetTableName < result[j].TargetTableName
+ if a.TargetTableName < b.TargetTableName {
+ return -1
+ }
+ if a.TargetTableName > b.TargetTableName {
+ return 1
+ }
+ return 0
})
if record {
diff --git a/backend/plugin/parser/tidb/differ.go b/backend/plugin/parser/tidb/differ.go
index 7043ee36b1..1c41d7d107 100644
--- a/backend/plugin/parser/tidb/differ.go
+++ b/backend/plugin/parser/tidb/differ.go
@@ -6,7 +6,7 @@ import (
"io"
"log/slog"
"reflect"
- "sort"
+ "slices"
"strconv"
"strings"
@@ -510,7 +510,7 @@ func sortAndWriteAlterTablePartitionedByList(buf io.Writer, partitions map[strin
for tableName := range partitions {
tableNames = append(tableNames, tableName)
}
- sort.Strings(tableNames)
+ slices.Sort(tableNames)
for _, tableName := range tableNames {
var body strings.Builder
if _, err := body.WriteString(fmt.Sprintf("ALTER TABLE `%s` PARTITION BY ", tableName)); err != nil {
@@ -734,8 +734,14 @@ func (p *partitionState) toString(buf io.StringWriter) error {
for _, partition := range p.partitions {
sortedPartitions = append(sortedPartitions, partition)
}
- sort.Slice(sortedPartitions, func(i, j int) bool {
- return sortedPartitions[i].id < sortedPartitions[j].id
+ slices.SortFunc(sortedPartitions, func(a, b *partitionDefinition) int {
+ if a.id < b.id {
+ return -1
+ }
+ if a.id > b.id {
+ return 1
+ }
+ return 0
})
if _, err := buf.WriteString("\n("); err != nil {
return err
@@ -776,8 +782,14 @@ func (p *partitionState) toString(buf io.StringWriter) error {
for _, subPartition := range partition.subpartitions {
sortedSubpartitions = append(sortedSubpartitions, subPartition)
}
- sort.Slice(sortedSubpartitions, func(i, j int) bool {
- return sortedSubpartitions[i].id < sortedSubpartitions[j].id
+ slices.SortFunc(sortedSubpartitions, func(a, b *partitionDefinition) int {
+ if a.id < b.id {
+ return -1
+ }
+ if a.id > b.id {
+ return 1
+ }
+ return 0
})
for j, subPartition := range sortedSubpartitions {
if _, err := buf.WriteString(fmt.Sprintf("SUBPARTITION %s", subPartition.name)); err != nil {
@@ -950,8 +962,16 @@ func writeNodeList(w format.RestoreWriter, ns []ast.Node, flags format.RestoreFl
}
func sortAndWriteNodeList(w format.RestoreWriter, ns []ast.Node, flags format.RestoreFlags) error {
- sort.Slice(ns, func(i, j int) bool {
- return getID(ns[i]) < getID(ns[j])
+ slices.SortFunc(ns, func(a, b ast.Node) int {
+ idA := getID(a)
+ idB := getID(b)
+ if idA < idB {
+ return -1
+ }
+ if idA > idB {
+ return 1
+ }
+ return 0
})
for _, n := range ns {
@@ -1561,8 +1581,14 @@ func normalizeColumnOptions(options []*ast.ColumnOption) (*ast.ColumnOption, []*
}
retOptions = append(retOptions, option)
}
- sort.Slice(retOptions, func(i, j int) bool {
- return retOptions[i].Tp < retOptions[j].Tp
+ slices.SortFunc(retOptions, func(a, b *ast.ColumnOption) int {
+ if a.Tp < b.Tp {
+ return -1
+ }
+ if a.Tp > b.Tp {
+ return 1
+ }
+ return 0
})
return collateOption, retOptions
}
diff --git a/backend/plugin/parser/trino/completion.go b/backend/plugin/parser/trino/completion.go
index 52508a276f..0cf1b4d5b1 100644
--- a/backend/plugin/parser/trino/completion.go
+++ b/backend/plugin/parser/trino/completion.go
@@ -3,7 +3,7 @@ package trino
import (
"context"
"fmt"
- "sort"
+ "slices"
"strings"
"unicode"
@@ -102,11 +102,20 @@ func (m CompletionMap) toSlice() []base.Candidate {
for _, candidate := range m {
result = append(result, candidate)
}
- sort.Slice(result, func(i, j int) bool {
- if result[i].Type != result[j].Type {
- return result[i].Type < result[j].Type
+ slices.SortFunc(result, func(i, j base.Candidate) int {
+ if i.Type != j.Type {
+ if i.Type < j.Type {
+ return -1
+ }
+ return 1
+ }
+ if i.Text < j.Text {
+ return -1
+ }
+ if i.Text > j.Text {
+ return 1
}
- return result[i].Text < result[j].Text
+ return 0
})
return result
}
@@ -1518,9 +1527,7 @@ func (c *Completer) fetchSelectItemAliases(ruleStack []*base.RuleContext) []stri
for alias := range aliasMap {
result = append(result, alias)
}
- sort.Slice(result, func(i, j int) bool {
- return result[i] < result[j]
- })
+ slices.Sort(result)
return result
case trinoparser.TrinoParserRULE_groupBy, trinoparser.TrinoParserRULE_sortItem, trinoparser.TrinoParserRULE_booleanExpression:
// These represent ORDER BY, GROUP BY, and HAVING contexts
diff --git a/backend/plugin/parser/trino/completion_test.go b/backend/plugin/parser/trino/completion_test.go
index 463ca8282c..d545c32dce 100644
--- a/backend/plugin/parser/trino/completion_test.go
+++ b/backend/plugin/parser/trino/completion_test.go
@@ -4,7 +4,7 @@ import (
"context"
"io"
"os"
- "sort"
+ "slices"
"strings"
"testing"
@@ -74,14 +74,26 @@ func TestCompletion(t *testing.T) {
if filteredResult == nil {
filteredResult = []base.Candidate{}
}
- sort.Slice(filteredResult, func(i, j int) bool {
- if filteredResult[i].Type != filteredResult[j].Type {
- return filteredResult[i].Type < filteredResult[j].Type
+ slices.SortFunc(filteredResult, func(i, j base.Candidate) int {
+ if i.Type != j.Type {
+ if i.Type < j.Type {
+ return -1
+ }
+ return 1
}
- if filteredResult[i].Text != filteredResult[j].Text {
- return filteredResult[i].Text < filteredResult[j].Text
+ if i.Text != j.Text {
+ if i.Text < j.Text {
+ return -1
+ }
+ return 1
}
- return filteredResult[i].Definition < filteredResult[j].Definition
+ if i.Definition < j.Definition {
+ return -1
+ }
+ if i.Definition > j.Definition {
+ return 1
+ }
+ return 0
})
if record {
diff --git a/backend/plugin/parser/tsql/backup.go b/backend/plugin/parser/tsql/backup.go
index 5a0e2657b3..69de9d4427 100644
--- a/backend/plugin/parser/tsql/backup.go
+++ b/backend/plugin/parser/tsql/backup.go
@@ -4,7 +4,7 @@ import (
"context"
"fmt"
"log/slog"
- "sort"
+ "slices"
"strings"
"github.com/antlr4-go/antlr/v4"
@@ -90,14 +90,26 @@ func generateSQL(statementInfoList []statementInfo, targetDatabase string, table
result = append(result, *backupStatement)
}
- sort.Slice(result, func(i, j int) bool {
- if result[i].StartPosition.Line != result[j].StartPosition.Line {
- return result[i].StartPosition.Line < result[j].StartPosition.Line
+ slices.SortFunc(result, func(a, b base.BackupStatement) int {
+ if a.StartPosition.Line != b.StartPosition.Line {
+ if a.StartPosition.Line < b.StartPosition.Line {
+ return -1
+ }
+ return 1
+ }
+ if a.StartPosition.Column != b.StartPosition.Column {
+ if a.StartPosition.Column < b.StartPosition.Column {
+ return -1
+ }
+ return 1
+ }
+ if a.SourceTableName < b.SourceTableName {
+ return -1
}
- if result[i].StartPosition.Column != result[j].StartPosition.Column {
- return result[i].StartPosition.Column < result[j].StartPosition.Column
+ if a.SourceTableName > b.SourceTableName {
+ return 1
}
- return result[i].SourceTableName < result[j].SourceTableName
+ return 0
})
return result, nil
diff --git a/backend/plugin/parser/tsql/backup_test.go b/backend/plugin/parser/tsql/backup_test.go
index 5d569ed39f..2114ba4b90 100644
--- a/backend/plugin/parser/tsql/backup_test.go
+++ b/backend/plugin/parser/tsql/backup_test.go
@@ -4,7 +4,7 @@ import (
"context"
"io"
"os"
- "sort"
+ "slices"
"testing"
"github.com/stretchr/testify/require"
@@ -40,11 +40,23 @@ func TestBackup(t *testing.T) {
for i, t := range tests {
result, err := TransformDMLToSelect(context.Background(), base.TransformContext{}, t.Input, "db", "backupDB", "rollback")
a.NoError(err)
- sort.Slice(result, func(i, j int) bool {
- if result[i].TargetTableName == result[j].TargetTableName {
- return result[i].Statement < result[j].Statement
+ slices.SortFunc(result, func(a, b base.BackupStatement) int {
+ if a.TargetTableName == b.TargetTableName {
+ if a.Statement < b.Statement {
+ return -1
+ }
+ if a.Statement > b.Statement {
+ return 1
+ }
+ return 0
}
- return result[i].TargetTableName < result[j].TargetTableName
+ if a.TargetTableName < b.TargetTableName {
+ return -1
+ }
+ if a.TargetTableName > b.TargetTableName {
+ return 1
+ }
+ return 0
})
if record {
diff --git a/backend/plugin/parser/tsql/completion.go b/backend/plugin/parser/tsql/completion.go
index b2013e2125..a76a385f8c 100644
--- a/backend/plugin/parser/tsql/completion.go
+++ b/backend/plugin/parser/tsql/completion.go
@@ -3,7 +3,7 @@ package tsql
import (
"context"
"fmt"
- "sort"
+ "slices"
"strings"
"unicode"
"unicode/utf8"
@@ -138,11 +138,20 @@ func (m CompletionMap) toSlice() []base.Candidate {
for _, candidate := range m {
result = append(result, candidate)
}
- sort.Slice(result, func(i, j int) bool {
- if result[i].Type != result[j].Type {
- return result[i].Type < result[j].Type
+ slices.SortFunc(result, func(a, b base.Candidate) int {
+ if a.Type != b.Type {
+ if a.Type < b.Type {
+ return -1
+ }
+ return 1
+ }
+ if a.Text < b.Text {
+ return -1
+ }
+ if a.Text > b.Text {
+ return 1
}
- return result[i].Text < result[j].Text
+ return 0
})
return result
}
@@ -1672,9 +1681,7 @@ func (c *Completer) fetchSelectItemAliases(ruleStack []*base.RuleContext) []stri
for alias := range aliasMap {
result = append(result, alias)
}
- sort.Slice(result, func(i, j int) bool {
- return result[i] < result[j]
- })
+ slices.Sort(result)
return result
case tsqlparser.TSqlParserRULE_group_by_clause, tsqlparser.TSqlParserRULE_order_by_clause, tsqlparser.TSqlParserRULE_having_clause:
canUseAliases = true
diff --git a/backend/plugin/parser/tsql/completion_test.go b/backend/plugin/parser/tsql/completion_test.go
index ab514f61e2..a4d42dcbed 100644
--- a/backend/plugin/parser/tsql/completion_test.go
+++ b/backend/plugin/parser/tsql/completion_test.go
@@ -4,7 +4,7 @@ import (
"context"
"io"
"os"
- "sort"
+ "slices"
"strings"
"testing"
@@ -71,14 +71,26 @@ func TestCompletion(t *testing.T) {
filteredResult = append(filteredResult, r)
}
}
- sort.Slice(filteredResult, func(i, j int) bool {
- if filteredResult[i].Type != filteredResult[j].Type {
- return filteredResult[i].Type < filteredResult[j].Type
+ slices.SortFunc(filteredResult, func(a, b base.Candidate) int {
+ if a.Type != b.Type {
+ if a.Type < b.Type {
+ return -1
+ }
+ return 1
}
- if filteredResult[i].Text != filteredResult[j].Text {
- return filteredResult[i].Text < filteredResult[j].Text
+ if a.Text != b.Text {
+ if a.Text < b.Text {
+ return -1
+ }
+ return 1
}
- return filteredResult[i].Definition < filteredResult[j].Definition
+ if a.Definition < b.Definition {
+ return -1
+ }
+ if a.Definition > b.Definition {
+ return 1
+ }
+ return 0
})
if record {
diff --git a/backend/plugin/parser/tsql/differ.go b/backend/plugin/parser/tsql/differ.go
index 6e003aa2c6..aab6bc9b0c 100644
--- a/backend/plugin/parser/tsql/differ.go
+++ b/backend/plugin/parser/tsql/differ.go
@@ -2,7 +2,7 @@ package tsql
import (
"fmt"
- "sort"
+ "slices"
"strings"
"github.com/antlr4-go/antlr/v4"
@@ -111,8 +111,14 @@ func SchemaDiff(_ base.DiffContext, oldStmt, newStmt string) (string, error) {
for _, newSchema := range newSchemaMap {
newSchemas = append(newSchemas, newSchema)
}
- sort.Slice(newSchemas, func(i, j int) bool {
- return newSchemas[i].id < newSchemas[j].id
+ slices.SortFunc(newSchemas, func(i, j *schemaInfo) int {
+ if i.id < j.id {
+ return -1
+ }
+ if i.id > j.id {
+ return 1
+ }
+ return 0
})
for _, newSchema := range newSchemas {
lowerSchema := newSchema.lowerName
@@ -133,8 +139,14 @@ func SchemaDiff(_ base.DiffContext, oldStmt, newStmt string) (string, error) {
}
remainingSchemas = append(remainingSchemas, oldSchema)
}
- sort.Slice(remainingSchemas, func(i, j int) bool {
- return remainingSchemas[i].id < remainingSchemas[j].id
+ slices.SortFunc(remainingSchemas, func(i, j *schemaInfo) int {
+ if i.id < j.id {
+ return -1
+ }
+ if i.id > j.id {
+ return 1
+ }
+ return 0
})
for _, oldSchema := range remainingSchemas {
diff.dropFullSchema(oldSchema)
@@ -148,8 +160,14 @@ func (d *diffNode) dropFullSchema(schema *schemaInfo) {
for _, table := range schema.tableMap {
tables = append(tables, table)
}
- sort.Slice(tables, func(i, j int) bool {
- return tables[i].id < tables[j].id
+ slices.SortFunc(tables, func(i, j *tableInfo) int {
+ if i.id < j.id {
+ return -1
+ }
+ if i.id > j.id {
+ return 1
+ }
+ return 0
})
for _, table := range tables {
d.dropFullTable(schema.name, table)
@@ -162,8 +180,14 @@ func (d *diffNode) diffSchema(oldSchema, newSchema *schemaInfo) error {
for _, newTable := range newSchema.tableMap {
newTables = append(newTables, newTable)
}
- sort.Slice(newTables, func(i, j int) bool {
- return newTables[i].id < newTables[j].id
+ slices.SortFunc(newTables, func(i, j *tableInfo) int {
+ if i.id < j.id {
+ return -1
+ }
+ if i.id > j.id {
+ return 1
+ }
+ return 0
})
for _, newTable := range newTables {
lowerTable := newTable.lowerName
@@ -184,8 +208,14 @@ func (d *diffNode) diffSchema(oldSchema, newSchema *schemaInfo) error {
}
remainingTables = append(remainingTables, oldTable)
}
- sort.Slice(remainingTables, func(i, j int) bool {
- return remainingTables[i].id < remainingTables[j].id
+ slices.SortFunc(remainingTables, func(i, j *tableInfo) int {
+ if i.id < j.id {
+ return -1
+ }
+ if i.id > j.id {
+ return 1
+ }
+ return 0
})
for _, oldTable := range remainingTables {
d.dropFullTable(oldSchema.name, oldTable)
@@ -199,8 +229,14 @@ func (d *diffNode) dropFullTable(schemaName string, table *tableInfo) {
for _, index := range table.indexMap {
indexes = append(indexes, index)
}
- sort.Slice(indexes, func(i, j int) bool {
- return indexes[i].id < indexes[j].id
+ slices.SortFunc(indexes, func(i, j *indexInfo) int {
+ if i.id < j.id {
+ return -1
+ }
+ if i.id > j.id {
+ return 1
+ }
+ return 0
})
for _, index := range indexes {
d.dropIndex = append(d.dropIndex, fmt.Sprintf("DROP INDEX [%s] ON [%s].[%s]", index.name, schemaName, table.name))
@@ -220,8 +256,14 @@ func (d *diffNode) diffTable(schemaName string, oldTable, newTable *tableInfo) e
for _, newIndex := range newTable.indexMap {
newIndexes = append(newIndexes, newIndex)
}
- sort.Slice(newIndexes, func(i, j int) bool {
- return newIndexes[i].id < newIndexes[j].id
+ slices.SortFunc(newIndexes, func(i, j *indexInfo) int {
+ if i.id < j.id {
+ return -1
+ }
+ if i.id > j.id {
+ return 1
+ }
+ return 0
})
for _, newIndex := range newIndexes {
lowerIndex := newIndex.lowerName
@@ -242,8 +284,14 @@ func (d *diffNode) diffTable(schemaName string, oldTable, newTable *tableInfo) e
}
remainingIndexes = append(remainingIndexes, oldIndex)
}
- sort.Slice(remainingIndexes, func(i, j int) bool {
- return remainingIndexes[i].id < remainingIndexes[j].id
+ slices.SortFunc(remainingIndexes, func(i, j *indexInfo) int {
+ if i.id < j.id {
+ return -1
+ }
+ if i.id > j.id {
+ return 1
+ }
+ return 0
})
for _, oldIndex := range remainingIndexes {
d.dropIndex = append(d.dropIndex, fmt.Sprintf("DROP INDEX [%s] ON [%s].[%s]", oldIndex.name, schemaName, oldTable.name))
@@ -511,8 +559,14 @@ func (d *diffNode) addSchema(schema *schemaInfo) {
for _, table := range schema.tableMap {
newTables = append(newTables, table)
}
- sort.Slice(newTables, func(i, j int) bool {
- return newTables[i].id < newTables[j].id
+ slices.SortFunc(newTables, func(i, j *tableInfo) int {
+ if i.id < j.id {
+ return -1
+ }
+ if i.id > j.id {
+ return 1
+ }
+ return 0
})
for _, table := range newTables {
@@ -521,8 +575,14 @@ func (d *diffNode) addSchema(schema *schemaInfo) {
for _, index := range table.indexMap {
newIndexes = append(newIndexes, index)
}
- sort.Slice(newIndexes, func(i, j int) bool {
- return newIndexes[i].id < newIndexes[j].id
+ slices.SortFunc(newIndexes, func(i, j *indexInfo) int {
+ if i.id < j.id {
+ return -1
+ }
+ if i.id > j.id {
+ return 1
+ }
+ return 0
})
for _, index := range newIndexes {
d.addIndex = append(d.addIndex, index.node.GetParser().GetTokenStream().GetTextFromRuleContext(index.node))
diff --git a/backend/plugin/parser/tsql/query_span_extractor.go b/backend/plugin/parser/tsql/query_span_extractor.go
index 145bb85ed1..1e60c07481 100644
--- a/backend/plugin/parser/tsql/query_span_extractor.go
+++ b/backend/plugin/parser/tsql/query_span_extractor.go
@@ -4,7 +4,7 @@ import (
"context"
"fmt"
"log/slog"
- "sort"
+ "slices"
"strings"
"unicode"
@@ -3454,8 +3454,14 @@ func getAccessTables(currentNormalizedDatabase string, currentNormalizedSchema s
var result []base.SchemaResource
antlr.ParseTreeWalkerDefault.Walk(l, tree)
- sort.Slice(result, func(i, j int) bool {
- return result[i].String() < result[j].String()
+ slices.SortFunc(result, func(a, b base.SchemaResource) int {
+ if a.String() < b.String() {
+ return -1
+ }
+ if a.String() > b.String() {
+ return 1
+ }
+ return 0
})
return l.resourceMap
diff --git a/backend/plugin/schema/clickhouse/state.go b/backend/plugin/schema/clickhouse/state.go
index 6776b37270..3f963083fb 100644
--- a/backend/plugin/schema/clickhouse/state.go
+++ b/backend/plugin/schema/clickhouse/state.go
@@ -3,7 +3,7 @@ package clickhouse
import (
"fmt"
"io"
- "sort"
+ "slices"
"strings"
"github.com/bytebase/bytebase/backend/plugin/db/util"
@@ -73,8 +73,13 @@ func (t *tableState) toString(buf *strings.Builder) error {
for _, column := range t.columns {
columns = append(columns, column)
}
- sort.Slice(columns, func(i, j int) bool {
- return columns[i].id < columns[j].id
+ slices.SortFunc(columns, func(x, y *columnState) int {
+ if x.id < y.id {
+ return -1
+ } else if x.id > y.id {
+ return 1
+ }
+ return 0
})
for i, column := range columns {
if i > 0 {
diff --git a/backend/plugin/schema/mssql/generate_migration.go b/backend/plugin/schema/mssql/generate_migration.go
index e7b56b1dea..7f1720a0ad 100644
--- a/backend/plugin/schema/mssql/generate_migration.go
+++ b/backend/plugin/schema/mssql/generate_migration.go
@@ -3,7 +3,7 @@ package mssql
import (
"context"
"fmt"
- "sort"
+ "slices"
"strings"
"github.com/antlr4-go/antlr/v4"
@@ -34,7 +34,7 @@ func generateMigration(diff *schema.MetadataDiff) (string, error) {
}
}
}
- sort.Strings(schemasToCreate)
+ slices.Sort(schemasToCreate)
// Safe order for migrations:
// 1. Drop dependent objects first (in reverse dependency order)
@@ -220,10 +220,16 @@ func generateMigration(diff *schema.MetadataDiff) (string, error) {
}
}
// Sort by schema.table name for consistent output
- sort.Slice(tablesToCreate, func(i, j int) bool {
- iFullName := getObjectID(tablesToCreate[i].SchemaName, tablesToCreate[i].TableName)
- jFullName := getObjectID(tablesToCreate[j].SchemaName, tablesToCreate[j].TableName)
- return iFullName < jFullName
+ slices.SortFunc(tablesToCreate, func(i, j *schema.TableDiff) int {
+ iFullName := getObjectID(i.SchemaName, i.TableName)
+ jFullName := getObjectID(j.SchemaName, j.TableName)
+ if iFullName < jFullName {
+ return -1
+ }
+ if iFullName > jFullName {
+ return 1
+ }
+ return 0
})
for _, tableDiff := range tablesToCreate {
createTableSQL, err := generateCreateTable(tableDiff.SchemaName, tableDiff.TableName, tableDiff.NewTable)
@@ -933,8 +939,16 @@ func dropViewsInOrder(diff *schema.MetadataDiff, buf *strings.Builder) error {
viewsToProcess = append(viewsToProcess, viewDiff)
}
}
- sort.Slice(viewsToProcess, func(i, j int) bool {
- return getObjectID(viewsToProcess[i].SchemaName, viewsToProcess[i].ViewName) < getObjectID(viewsToProcess[j].SchemaName, viewsToProcess[j].ViewName)
+ slices.SortFunc(viewsToProcess, func(i, j *schema.ViewDiff) int {
+ iFullName := getObjectID(i.SchemaName, i.ViewName)
+ jFullName := getObjectID(j.SchemaName, j.ViewName)
+ if iFullName < jFullName {
+ return -1
+ }
+ if iFullName > jFullName {
+ return 1
+ }
+ return 0
})
for _, viewDiff := range viewsToProcess {
@@ -976,8 +990,16 @@ func dropViewsInOrder(diff *schema.MetadataDiff, buf *strings.Builder) error {
}
}
// Sort alphabetically for deterministic output
- sort.Slice(fallbackViews, func(i, j int) bool {
- return getObjectID(fallbackViews[i].SchemaName, fallbackViews[i].ViewName) < getObjectID(fallbackViews[j].SchemaName, fallbackViews[j].ViewName)
+ slices.SortFunc(fallbackViews, func(i, j *schema.ViewDiff) int {
+ iFullName := getObjectID(i.SchemaName, i.ViewName)
+ jFullName := getObjectID(j.SchemaName, j.ViewName)
+ if iFullName < jFullName {
+ return -1
+ }
+ if iFullName > jFullName {
+ return 1
+ }
+ return 0
})
for _, viewDiff := range fallbackViews {
_, _ = buf.WriteString("DROP VIEW [")
@@ -1017,8 +1039,16 @@ func createViewsInOrder(diff *schema.MetadataDiff, buf *strings.Builder) error {
viewsToProcess = append(viewsToProcess, viewDiff)
}
}
- sort.Slice(viewsToProcess, func(i, j int) bool {
- return getObjectID(viewsToProcess[i].SchemaName, viewsToProcess[i].ViewName) < getObjectID(viewsToProcess[j].SchemaName, viewsToProcess[j].ViewName)
+ slices.SortFunc(viewsToProcess, func(i, j *schema.ViewDiff) int {
+ iFullName := getObjectID(i.SchemaName, i.ViewName)
+ jFullName := getObjectID(j.SchemaName, j.ViewName)
+ if iFullName < jFullName {
+ return -1
+ }
+ if iFullName > jFullName {
+ return 1
+ }
+ return 0
})
for _, viewDiff := range viewsToProcess {
@@ -1061,8 +1091,16 @@ func createViewsInOrder(diff *schema.MetadataDiff, buf *strings.Builder) error {
}
}
// Sort alphabetically for deterministic output
- sort.Slice(fallbackViews, func(i, j int) bool {
- return getObjectID(fallbackViews[i].SchemaName, fallbackViews[i].ViewName) < getObjectID(fallbackViews[j].SchemaName, fallbackViews[j].ViewName)
+ slices.SortFunc(fallbackViews, func(i, j *schema.ViewDiff) int {
+ iFullName := getObjectID(i.SchemaName, i.ViewName)
+ jFullName := getObjectID(j.SchemaName, j.ViewName)
+ if iFullName < jFullName {
+ return -1
+ }
+ if iFullName > jFullName {
+ return 1
+ }
+ return 0
})
for _, viewDiff := range fallbackViews {
_, _ = buf.WriteString(viewDiff.NewView.Definition)
diff --git a/backend/plugin/schema/mssql/get_database_definition.go b/backend/plugin/schema/mssql/get_database_definition.go
index 9fbe4a6470..021b9ad715 100644
--- a/backend/plugin/schema/mssql/get_database_definition.go
+++ b/backend/plugin/schema/mssql/get_database_definition.go
@@ -2,7 +2,7 @@ package mssql
import (
"fmt"
- "sort"
+ "slices"
"strings"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
@@ -170,11 +170,20 @@ func sortViewsByDependenciesAcrossSchemas(views []*viewWithSchema) []*viewWithSc
if err != nil {
// If there's a cycle or error, return views in original order
// Sort by schema then by name for deterministic output
- sort.Slice(views, func(i, j int) bool {
- if views[i].schema != views[j].schema {
- return views[i].schema < views[j].schema
+ slices.SortFunc(views, func(a, b *viewWithSchema) int {
+ if a.schema != b.schema {
+ if a.schema < b.schema {
+ return -1
+ }
+ return 1
}
- return views[i].view.Name < views[j].view.Name
+ if a.view.Name < b.view.Name {
+ return -1
+ }
+ if a.view.Name > b.view.Name {
+ return 1
+ }
+ return 0
})
return views
}
diff --git a/backend/plugin/schema/mssql/get_database_metadata.go b/backend/plugin/schema/mssql/get_database_metadata.go
index 1d7cf60600..c0692319ed 100644
--- a/backend/plugin/schema/mssql/get_database_metadata.go
+++ b/backend/plugin/schema/mssql/get_database_metadata.go
@@ -2,7 +2,7 @@ package mssql
import (
"fmt"
- "sort"
+ "slices"
"strconv"
"strings"
@@ -54,7 +54,7 @@ func GetDatabaseMetadata(schemaText string) (*storepb.DatabaseSchemaMetadata, er
for name := range extractor.schemas {
schemaNames = append(schemaNames, name)
}
- sort.Strings(schemaNames)
+ slices.Sort(schemaNames)
for _, schemaName := range schemaNames {
schemaMetadata.Schemas = append(schemaMetadata.Schemas, extractor.schemas[schemaName])
diff --git a/backend/plugin/schema/redshift/state.go b/backend/plugin/schema/redshift/state.go
index 446a14d8b0..553adbc291 100644
--- a/backend/plugin/schema/redshift/state.go
+++ b/backend/plugin/schema/redshift/state.go
@@ -3,7 +3,7 @@ package redshift
import (
"fmt"
"io"
- "sort"
+ "slices"
"strings"
storepb "github.com/bytebase/bytebase/proto/generated-go/store"
@@ -71,8 +71,14 @@ func (t *tableState) toString(buf *strings.Builder) error {
for _, column := range t.columns {
columns = append(columns, column)
}
- sort.Slice(columns, func(i, j int) bool {
- return columns[i].id < columns[j].id
+ slices.SortFunc(columns, func(a, b *columnState) int {
+ if a.id < b.id {
+ return -1
+ }
+ if a.id > b.id {
+ return 1
+ }
+ return 0
})
for i, column := range columns {
if i > 0 {
@@ -89,14 +95,20 @@ func (t *tableState) toString(buf *strings.Builder) error {
for _, index := range t.indexes {
indexes = append(indexes, index)
}
- sort.Slice(indexes, func(i, j int) bool {
- if indexes[i].primary {
- return true
+ slices.SortFunc(indexes, func(a, b *indexState) int {
+ if a.primary && !b.primary {
+ return -1
+ }
+ if !a.primary && b.primary {
+ return 1
}
- if indexes[j].primary {
- return false
+ if a.name < b.name {
+ return -1
}
- return indexes[i].name < indexes[j].name
+ if a.name > b.name {
+ return 1
+ }
+ return 0
})
for i, index := range indexes {
@@ -114,8 +126,14 @@ func (t *tableState) toString(buf *strings.Builder) error {
for _, fk := range t.foreignKeys {
foreignKeys = append(foreignKeys, fk)
}
- sort.Slice(foreignKeys, func(i, j int) bool {
- return foreignKeys[i].name < foreignKeys[j].name
+ slices.SortFunc(foreignKeys, func(a, b *foreignKeyState) int {
+ if a.name < b.name {
+ return -1
+ }
+ if a.name > b.name {
+ return 1
+ }
+ return 0
})
for i, fk := range foreignKeys {
diff --git a/backend/resources/postgres/sample_instance.go b/backend/resources/postgres/sample_instance.go
index 662b6c1cb7..a637fe2ede 100644
--- a/backend/resources/postgres/sample_instance.go
+++ b/backend/resources/postgres/sample_instance.go
@@ -8,7 +8,7 @@ import (
"io/fs"
"log/slog"
"path"
- "sort"
+ "slices"
"strings"
"github.com/pkg/errors"
@@ -80,7 +80,7 @@ func loadSampleData() (string, error) {
if err != nil {
return "", err
}
- sort.Strings(names)
+ slices.Sort(names)
var builder strings.Builder
for _, name := range names {
diff --git a/backend/runner/approval/runner.go b/backend/runner/approval/runner.go
index bd6eeba60f..e40082541b 100644
--- a/backend/runner/approval/runner.go
+++ b/backend/runner/approval/runner.go
@@ -6,7 +6,7 @@ import (
"fmt"
"log/slog"
"math"
- "sort"
+ "slices"
"sync"
"time"
@@ -313,8 +313,13 @@ func getApprovalTemplate(approvalSetting *storepb.WorkspaceApprovalSetting, risk
func (r *Runner) getIssueRisk(ctx context.Context, issue *store.IssueMessage, risks []*store.RiskMessage) (int32, store.RiskSource, bool, error) {
// sort by level DESC, higher risks go first.
- sort.Slice(risks, func(i, j int) bool {
- return risks[i].Level > risks[j].Level
+ slices.SortFunc(risks, func(a, b *store.RiskMessage) int {
+ if a.Level > b.Level {
+ return -1
+ } else if a.Level < b.Level {
+ return 1
+ }
+ return 0
})
switch issue.Type {
diff --git a/backend/runner/monitor/monitor.go b/backend/runner/monitor/monitor.go
index 7679168ad9..db3c1d776a 100644
--- a/backend/runner/monitor/monitor.go
+++ b/backend/runner/monitor/monitor.go
@@ -11,7 +11,7 @@ import (
"sync"
"time"
- "sort"
+ "slices"
"strconv"
"strings"
@@ -158,8 +158,13 @@ func retainMostRecentFilesByPattern(dir, pattern string, retainCount int) error
}
// Sort files by timestamp in descending order (most recent first)
- sort.Slice(files, func(i, j int) bool {
- return files[i].timestamp > files[j].timestamp
+ slices.SortFunc(files, func(a, b fileInfo) int {
+ if a.timestamp > b.timestamp {
+ return -1
+ } else if a.timestamp < b.timestamp {
+ return 1
+ }
+ return 0
})
// Remove older files beyond retention count
diff --git a/backend/runner/taskrun/executor.go b/backend/runner/taskrun/executor.go
index 2729a8292e..1beb2756d8 100644
--- a/backend/runner/taskrun/executor.go
+++ b/backend/runner/taskrun/executor.go
@@ -4,7 +4,7 @@ import (
"context"
"fmt"
"log/slog"
- "sort"
+ "slices"
"time"
"github.com/pkg/errors"
@@ -165,8 +165,13 @@ func getMigrationInfo(ctx context.Context, stores *store.Store, profile *config.
if err != nil {
return nil, errors.Wrap(err, "failed to list plan check runs")
}
- sort.Slice(runs, func(i, j int) bool {
- return runs[i].UID > runs[j].UID
+ slices.SortFunc(runs, func(a, b *store.PlanCheckRunMessage) int {
+ if a.UID > b.UID {
+ return -1
+ } else if a.UID < b.UID {
+ return 1
+ }
+ return 0
})
foundChangedResources := false
for _, run := range runs {
diff --git a/backend/store/changelog.go b/backend/store/changelog.go
index 8ab8ec81a7..3c65e64579 100644
--- a/backend/store/changelog.go
+++ b/backend/store/changelog.go
@@ -3,7 +3,7 @@ package store
import (
"context"
"fmt"
- "sort"
+ "slices"
"strings"
"time"
@@ -428,18 +428,33 @@ func (r *expressionRewriter) appendDNFPart() error {
}
schema.Tables = append(schema.Tables, table)
}
- sort.Slice(schema.Tables, func(i, j int) bool {
- return schema.Tables[i].Name < schema.Tables[j].Name
+ slices.SortFunc(schema.Tables, func(a, b *storepb.ChangedResourceTable) int {
+ if a.Name < b.Name {
+ return -1
+ } else if a.Name > b.Name {
+ return 1
+ }
+ return 0
})
db.Schemas = append(db.Schemas, schema)
}
- sort.Slice(db.Schemas, func(i, j int) bool {
- return db.Schemas[i].Name < db.Schemas[j].Name
+ slices.SortFunc(db.Schemas, func(a, b *storepb.ChangedResourceSchema) int {
+ if a.Name < b.Name {
+ return -1
+ } else if a.Name > b.Name {
+ return 1
+ }
+ return 0
})
meta.Databases = append(meta.Databases, db)
}
- sort.Slice(meta.Databases, func(i, j int) bool {
- return meta.Databases[i].Name < meta.Databases[j].Name
+ slices.SortFunc(meta.Databases, func(a, b *storepb.ChangedResourceDatabase) int {
+ if a.Name < b.Name {
+ return -1
+ } else if a.Name > b.Name {
+ return 1
+ }
+ return 0
})
text, err := protojson.Marshal(&storepb.InstanceChangeHistoryPayload{
diff --git a/backend/store/model/changed_resources.go b/backend/store/model/changed_resources.go
index b09bbc0044..12769c8140 100644
--- a/backend/store/model/changed_resources.go
+++ b/backend/store/model/changed_resources.go
@@ -1,7 +1,7 @@
package model
import (
- "sort"
+ "slices"
storepb "github.com/bytebase/bytebase/proto/generated-go/store"
)
@@ -70,8 +70,13 @@ func (d *ChangedDatabase) build() *storepb.ChangedResourceDatabase {
s.Name = name
changedResourceDatabase.Schemas = append(changedResourceDatabase.Schemas, s)
}
- sort.Slice(changedResourceDatabase.Schemas, func(i, j int) bool {
- return changedResourceDatabase.Schemas[i].GetName() < changedResourceDatabase.Schemas[j].GetName()
+ slices.SortFunc(changedResourceDatabase.Schemas, func(a, b *storepb.ChangedResourceSchema) int {
+ if a.GetName() < b.GetName() {
+ return -1
+ } else if a.GetName() > b.GetName() {
+ return 1
+ }
+ return 0
})
return changedResourceDatabase
}
@@ -81,29 +86,49 @@ func (s *ChangedSchema) build() *storepb.ChangedResourceSchema {
for _, table := range s.tables {
changedResourceSchema.Tables = append(changedResourceSchema.Tables, table.table)
}
- sort.Slice(changedResourceSchema.Tables, func(i, j int) bool {
- return changedResourceSchema.Tables[i].GetName() < changedResourceSchema.Tables[j].GetName()
+ slices.SortFunc(changedResourceSchema.Tables, func(a, b *storepb.ChangedResourceTable) int {
+ if a.GetName() < b.GetName() {
+ return -1
+ } else if a.GetName() > b.GetName() {
+ return 1
+ }
+ return 0
})
for _, view := range s.views {
changedResourceSchema.Views = append(changedResourceSchema.Views, view)
}
- sort.Slice(changedResourceSchema.Views, func(i, j int) bool {
- return changedResourceSchema.Views[i].GetName() < changedResourceSchema.Views[j].GetName()
+ slices.SortFunc(changedResourceSchema.Views, func(a, b *storepb.ChangedResourceView) int {
+ if a.GetName() < b.GetName() {
+ return -1
+ } else if a.GetName() > b.GetName() {
+ return 1
+ }
+ return 0
})
for _, function := range s.functions {
changedResourceSchema.Functions = append(changedResourceSchema.Functions, function)
}
- sort.Slice(changedResourceSchema.Functions, func(i, j int) bool {
- return changedResourceSchema.Functions[i].GetName() < changedResourceSchema.Functions[j].GetName()
+ slices.SortFunc(changedResourceSchema.Functions, func(a, b *storepb.ChangedResourceFunction) int {
+ if a.GetName() < b.GetName() {
+ return -1
+ } else if a.GetName() > b.GetName() {
+ return 1
+ }
+ return 0
})
for _, procedure := range s.procedures {
changedResourceSchema.Procedures = append(changedResourceSchema.Procedures, procedure)
}
- sort.Slice(changedResourceSchema.Procedures, func(i, j int) bool {
- return changedResourceSchema.Procedures[i].GetName() < changedResourceSchema.Procedures[j].GetName()
+ slices.SortFunc(changedResourceSchema.Procedures, func(a, b *storepb.ChangedResourceProcedure) int {
+ if a.GetName() < b.GetName() {
+ return -1
+ } else if a.GetName() > b.GetName() {
+ return 1
+ }
+ return 0
})
return changedResourceSchema
diff --git a/backend/store/model/database.go b/backend/store/model/database.go
index 4c6731a34e..a588a3c26c 100644
--- a/backend/store/model/database.go
+++ b/backend/store/model/database.go
@@ -3,7 +3,7 @@ package model
import (
"bytes"
"fmt"
- "sort"
+ "slices"
"strings"
storepb "github.com/bytebase/bytebase/proto/generated-go/store"
@@ -747,7 +747,7 @@ func (s *SchemaMetadata) ListTableNames() []string {
result = append(result, table.GetProto().GetName())
}
- sort.Strings(result)
+ slices.Sort(result)
return result
}
@@ -758,7 +758,7 @@ func (s *SchemaMetadata) ListProcedureNames() []string {
result = append(result, procedure.GetProto().GetName())
}
- sort.Strings(result)
+ slices.Sort(result)
return result
}
@@ -769,7 +769,7 @@ func (s *SchemaMetadata) ListFunctionNames() []string {
result = append(result, function.GetProto().GetName())
}
- sort.Strings(result)
+ slices.Sort(result)
return result
}
@@ -780,7 +780,7 @@ func (s *SchemaMetadata) ListViewNames() []string {
result = append(result, view.GetProto().GetName())
}
- sort.Strings(result)
+ slices.Sort(result)
return result
}
@@ -791,7 +791,7 @@ func (s *SchemaMetadata) ListForeignTableNames() []string {
result = append(result, table.GetProto().GetName())
}
- sort.Strings(result)
+ slices.Sort(result)
return result
}
@@ -802,7 +802,7 @@ func (s *SchemaMetadata) ListMaterializedViewNames() []string {
result = append(result, view.GetProto().GetName())
}
- sort.Strings(result)
+ slices.Sort(result)
return result
}
diff --git a/backend/store/task.go b/backend/store/task.go
index 93746dfe5c..93525799aa 100644
--- a/backend/store/task.go
+++ b/backend/store/task.go
@@ -5,7 +5,7 @@ import (
"database/sql"
"encoding/json"
"fmt"
- "sort"
+ "slices"
"strings"
"github.com/pkg/errors"
@@ -529,9 +529,7 @@ func (s *Store) ListTasksToAutoRollout(ctx context.Context, environments []strin
}
}
- sort.Slice(ids, func(i, j int) bool {
- return ids[i] < ids[j]
- })
+ slices.Sort(ids)
return ids, nil
}
diff --git a/backend/store/task_run.go b/backend/store/task_run.go
index c3f87aaad6..8a24a4a8a7 100644
--- a/backend/store/task_run.go
+++ b/backend/store/task_run.go
@@ -4,7 +4,7 @@ import (
"context"
"database/sql"
"fmt"
- "sort"
+ "slices"
"strings"
"time"
@@ -237,8 +237,8 @@ func (s *Store) CreatePendingTaskRuns(ctx context.Context, creates ...*TaskRunMe
return nil
}
- sort.Slice(creates, func(i, j int) bool {
- return creates[i].TaskUID < creates[j].TaskUID
+ slices.SortFunc(creates, func(a, b *TaskRunMessage) int {
+ return a.TaskUID - b.TaskUID
})
var taskIDs []int
diff --git a/backend/tests/change_history_test.go b/backend/tests/change_history_test.go
index 68fce3c658..e484ab84d0 100644
--- a/backend/tests/change_history_test.go
+++ b/backend/tests/change_history_test.go
@@ -3,7 +3,7 @@ package tests
import (
"context"
"fmt"
- "sort"
+ "slices"
"testing"
"github.com/stretchr/testify/require"
@@ -124,12 +124,17 @@ func TestFilterChangeHistoryByResources(t *testing.T) {
a.Equal(len(tt.wantStatements), len(resp.Changelogs), tt.filter)
for i, wantStatement := range tt.wantStatements {
// Sort by changelog UID.
- sort.Slice(resp.Changelogs, func(i, j int) bool {
- _, _, id1, err := common.GetInstanceDatabaseChangelogUID(resp.Changelogs[i].Name)
+ slices.SortFunc(resp.Changelogs, func(x, y *v1pb.Changelog) int {
+ _, _, id1, err := common.GetInstanceDatabaseChangelogUID(x.Name)
a.NoError(err)
- _, _, id2, err := common.GetInstanceDatabaseChangelogUID(resp.Changelogs[j].Name)
+ _, _, id2, err := common.GetInstanceDatabaseChangelogUID(y.Name)
a.NoError(err)
- return id1 < id2
+ if id1 < id2 {
+ return -1
+ } else if id1 > id2 {
+ return 1
+ }
+ return 0
})
a.Equal(wantStatement, string(resp.Changelogs[i].Statement), tt.filter)
}