Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
130 commits
Select commit Hold shift + click to select a range
4756a77
Enhance interface to provide query info.
joel-rieke Jun 6, 2022
8ba91fc
Rename to help builds.
joel-rieke Sep 20, 2022
ee98141
Support cidr block lookups.
joel-rieke Sep 20, 2022
1bc38f8
Merge branch 'main' into 09_19_2022_latest
joel-rieke Oct 3, 2022
27e8c16
Temporary workaround to bad logging.
joel-rieke Oct 4, 2022
14946c6
Try to make partitions concurrent safe.
joel-rieke Oct 4, 2022
d807f5a
Alter locking and be more comprehensive.
joel-rieke Oct 4, 2022
2fd250e
Merge tag 'v0.14.0' into 11_22_2022_latest
joel-rieke Nov 23, 2022
65881cb
Merge remote-tracking branch 'upstream/main' into 11_22_2022_latest
joel-rieke Nov 23, 2022
75bfae5
Try updating grpc and protobuf
joel-rieke Dec 8, 2023
e37d672
Bump golang.org/x/net from 0.14.0 to 0.17.0
dependabot[bot] Dec 8, 2023
540ed40
Add more refined locking.
joel-rieke Dec 12, 2023
94925e1
Merge branch 'main' into dependabot/go_modules/golang.org/x/net-0.17.0
joel-rieke Dec 12, 2023
bd7a68e
Merge pull request #5 from trimble-oss/dependabot/go_modules/golang.o…
joel-rieke Dec 12, 2023
077859a
Update to latest yaml v3
joel-rieke Dec 12, 2023
11fb790
Add missing unlock.
joel-rieke Dec 12, 2023
1cadba9
RLock for duration of copy.
joel-rieke Dec 14, 2023
70aeff6
Add some bounds checking.
joel-rieke Dec 14, 2023
ab62e2b
Git rid of unecessary dependency.
joel-rieke Dec 15, 2023
20477ea
Bump golang.org/x/crypto from 0.14.0 to 0.17.0
dependabot[bot] Dec 18, 2023
e4bf400
Merge pull request #6 from trimble-oss/dependabot/go_modules/golang.o…
joel-rieke Dec 19, 2023
cf46428
Add locking timeouts on password failures.
joel-rieke Mar 6, 2024
c16dd2e
[ga-format-pr] Run ./format_repo.sh to fix formatting
joel-rieke Mar 6, 2024
0243da8
Some improvements after testing.
joel-rieke Mar 6, 2024
8a1e8a3
Merge pull request #7 from trimble-oss/lock_on_failures
joel-rieke Mar 6, 2024
88ff4a8
Flex for super user.
joel-rieke Mar 6, 2024
204f6f9
Merge pull request #8 from trimble-oss/flex_super
joel-rieke Mar 6, 2024
894ebd3
Flex more for grant super lookups.
joel-rieke Mar 6, 2024
46971fb
Flex more for grant super local lookups
joel-rieke Mar 6, 2024
2f33f4c
Merge pull request #9 from trimble-oss/more_flex_super
joel-rieke Mar 6, 2024
402b186
Blanket check for super if host validation fails.
joel-rieke Mar 6, 2024
6bc5051
Merge pull request #10 from trimble-oss/super_flex_v2
joel-rieke Mar 6, 2024
820714b
Only check cidr in query auth scenario.
joel-rieke Mar 6, 2024
f08eaec
Merge pull request #11 from trimble-oss/cidr_check_not_grants
joel-rieke Mar 7, 2024
657755d
Skip validation unless directly involved in auth.
joel-rieke Mar 7, 2024
a6f6d95
Merge pull request #12 from trimble-oss/additional_skips
joel-rieke Mar 7, 2024
a6e07b1
Key off incoming host instead of user entry.
joel-rieke Mar 7, 2024
c3744ab
Merge pull request #13 from trimble-oss/bug_key_host
joel-rieke Mar 7, 2024
aa41b23
Adding thresh-hold rolling locking.
joel-rieke Mar 13, 2024
5a3d366
Also apply algorithm change here, too.
joel-rieke Mar 13, 2024
663421f
Merge pull request #14 from trimble-oss/rolling_locks
joel-rieke Mar 13, 2024
1e6ee63
Bump google.golang.org/protobuf from 1.31.0 to 1.33.0
dependabot[bot] Mar 13, 2024
59a4688
Bump golang.org/x/net from 0.17.0 to 0.23.0
dependabot[bot] Apr 19, 2024
b78c11a
Attempt to fix locking issue.
joel-rieke Apr 26, 2024
29fb8f7
Merge pull request #17 from trimble-oss/lock_fix
joel-rieke Apr 26, 2024
34a069c
Merge pull request #16 from trimble-oss/dependabot/go_modules/golang.…
joel-rieke Apr 26, 2024
98bbbd3
Merge pull request #15 from trimble-oss/dependabot/go_modules/google.…
joel-rieke Apr 26, 2024
163bbea
Add less to the rlock.
joel-rieke Apr 29, 2024
85618f7
Merge pull request #18 from trimble-oss/lock_fix_pt2
joel-rieke Apr 29, 2024
a9b5de7
Implement Karnveer's idea. Drop sorts, since we really don't need it.
joel-rieke Apr 30, 2024
4997a62
Actually comment out sorting as it is not needed.
joel-rieke Apr 30, 2024
cf509c5
Merge pull request #19 from trimble-oss/disable_sort
joel-rieke Apr 30, 2024
a29ee0f
Various mod upgrades
joel-rieke Jan 2, 2025
b77267f
Merge pull request #21 from trimble-oss/mod_upgrades
joel-rieke Jan 2, 2025
431f620
Bump golang.org/x/crypto from 0.31.0 to 0.35.0
dependabot[bot] Jul 14, 2025
0db5ff1
Merge pull request #22 from trimble-oss/dependabot/go_modules/golang.…
joel-rieke Jul 14, 2025
72012c4
Potential fix for code scanning alert no. 253: Incorrect conversion b…
joel-rieke Jul 15, 2025
584a5b3
[ga-format-pr] Run ./format_repo.sh to fix formatting
joel-rieke Jul 15, 2025
a440633
Merge pull request #23 from trimble-oss/alert-autofix-253
joel-rieke Jul 15, 2025
fceed0e
Potential fix for code scanning alert no. 221: Incorrect conversion b…
joel-rieke Jul 15, 2025
fff9988
[ga-format-pr] Run ./format_repo.sh to fix formatting
joel-rieke Jul 15, 2025
d6fd497
Merge pull request #24 from trimble-oss/alert-autofix-221
joel-rieke Jul 15, 2025
045728a
Potential fix for code scanning alert no. 197: Incorrect conversion b…
joel-rieke Jul 15, 2025
5e6ddc5
[ga-format-pr] Run ./format_repo.sh to fix formatting
joel-rieke Jul 15, 2025
883f390
Merge pull request #25 from trimble-oss/alert-autofix-197
joel-rieke Jul 15, 2025
1398561
Potential fix for code scanning alert no. 268: Incorrect conversion b…
joel-rieke Jul 15, 2025
4a4cc5a
Merge pull request #26 from trimble-oss/alert-autofix-268
joel-rieke Jul 15, 2025
098a8fb
Potential fix for code scanning alert no. 270: Incorrect conversion b…
joel-rieke Jul 15, 2025
fba2523
Merge pull request #27 from trimble-oss/alert-autofix-270
joel-rieke Jul 15, 2025
c69b562
Potential fix for code scanning alert no. 271: Incorrect conversion b…
joel-rieke Jul 15, 2025
35fd5cb
Potential fix for code scanning alert no. 275: Incorrect conversion b…
joel-rieke Jul 15, 2025
be1798b
Remove duplicate line. Thanks gobot
joel-rieke Jul 15, 2025
be618c2
Remove another duplicate
joel-rieke Jul 15, 2025
65a127c
Merge pull request #28 from trimble-oss/alert-autofix-271
joel-rieke Jul 15, 2025
f8d4eb1
Potential fix for code scanning alert no. 274: Incorrect conversion b…
joel-rieke Jul 15, 2025
da15274
Fix in similar fashion
joel-rieke Jul 15, 2025
2d6cfbf
[ga-format-pr] Run ./format_repo.sh to fix formatting
joel-rieke Jul 15, 2025
aeaaec9
Potential fix for code scanning alert no. 277: Incorrect conversion b…
joel-rieke Jul 15, 2025
07deb2c
Clean up some bad code. Bad gitbot
joel-rieke Jul 15, 2025
e970eaf
Potential fix for code scanning alert no. 279: Incorrect conversion b…
joel-rieke Jul 15, 2025
d19d517
[ga-format-pr] Run ./format_repo.sh to fix formatting
joel-rieke Jul 15, 2025
157e5d5
Let it fall through
joel-rieke Jul 15, 2025
d965129
Merge pull request #29 from trimble-oss/alert-autofix-274
joel-rieke Jul 15, 2025
ef60998
Go mod updates to address security
joel-rieke Jul 15, 2025
a1d1fe6
Adding fix from git security
joel-rieke Jul 15, 2025
63a11a4
Potential fix for code scanning alert no. 284: Incorrect conversion b…
joel-rieke Jul 15, 2025
0672072
Trigger code scanning rescan
joel-rieke Jul 15, 2025
baf004d
Potential fix for code scanning alert no. 405: Incorrect conversion b…
joel-rieke Jul 15, 2025
1c080e0
Potential fix for code scanning alert no. 404: Incorrect conversion b…
joel-rieke Jul 15, 2025
2467355
Address github codescanning
joel-rieke Jul 28, 2025
eb4e004
Adding more code repair
joel-rieke Jul 28, 2025
684b239
Clean up panic
joel-rieke Jul 28, 2025
dd4819f
More cleanup of panic and parsing
joel-rieke Jul 28, 2025
888eee5
Merge pull request #35 from trimble-oss/security_updates_two
joel-rieke Jul 28, 2025
332cff7
Refactor to utilize safe int16 year converter func
joel-rieke Jul 28, 2025
3f8fe68
More cleanup
joel-rieke Jul 28, 2025
0b89bf4
Merge pull request #36 from trimble-oss/security_updates_2.1
joel-rieke Jul 28, 2025
167cf51
Remove as much casting as possible
joel-rieke Jul 28, 2025
d04edf3
Address type casts
joel-rieke Jul 28, 2025
1140f1d
Potential fix for code scanning alert no. 395: Clear-text logging of …
joel-rieke Jul 28, 2025
899863a
[ga-format-pr] Run ./format_repo.sh to fix formatting
joel-rieke Jul 28, 2025
91b3e70
Make type safe conversions
joel-rieke Jul 28, 2025
a36b409
Ignore locally built .gz files
joel-rieke Jul 28, 2025
85c67e9
Merge pull request #37 from trimble-oss/security_updates_2.2
joel-rieke Jul 28, 2025
a41151c
Avoid panics. Safe conversions
joel-rieke Jul 28, 2025
6739848
Potential fix for code scanning alert no. 395: Clear-text logging of …
joel-rieke Jul 28, 2025
e2addbb
Remove unused variable
joel-rieke Jul 28, 2025
7237c7a
Merge pull request #38 from trimble-oss/security_updates_2.3
joel-rieke Jul 28, 2025
218d123
Focus fixes on two remaining lines
joel-rieke Jul 28, 2025
9c3cfc9
Address more unsafe type conversions
joel-rieke Jul 28, 2025
1988fe4
Try and get analyzer to re-analyze by putting sanitizing code closer …
joel-rieke Jul 28, 2025
2855a9f
Merge pull request #39 from trimble-oss/security_updates_2.4
joel-rieke Jul 28, 2025
ba6d920
Don't panic
joel-rieke Jul 28, 2025
0f04856
Don't panic. Safer type conversions.
joel-rieke Jul 28, 2025
de5543b
Fix unsafe type conversions
joel-rieke Jul 28, 2025
f4b96b8
Address unsafe type conversions. Don't panic!
joel-rieke Jul 28, 2025
62d5ce0
Merge pull request #40 from trimble-oss/security_updates_2.5
joel-rieke Jul 28, 2025
fa7e9be
Potential fix for code scanning alert no. 231: Incorrect conversion b…
joel-rieke Jul 28, 2025
cdae0b7
Potential fix for code scanning alert no. 276: Incorrect conversion b…
joel-rieke Jul 28, 2025
feacdec
Potential fix for code scanning alert no. 411: Clear-text logging of …
joel-rieke Jul 28, 2025
4b396bb
More sanitizing
joel-rieke Jul 28, 2025
83950fa
Merge pull request #41 from trimble-oss/security_updates_2.6
joel-rieke Jul 28, 2025
be1ec1c
Try alternate fix for these casts using strconv
joel-rieke Jul 28, 2025
5faced8
Merge pull request #42 from trimble-oss/security_updates_2.7
joel-rieke Jul 28, 2025
451ff26
Another attempt at satisfying the analyzer
joel-rieke Jul 28, 2025
cc9571f
Merge pull request #43 from trimble-oss/security_updates_2.8
joel-rieke Jul 28, 2025
8dfe288
3rd approach
joel-rieke Jul 28, 2025
1a4a456
Merge pull request #44 from trimble-oss/security_updates_2.9
joel-rieke Jul 28, 2025
b5640f6
Try logging just the message and not the args
joel-rieke Jul 28, 2025
7a39832
Need feedback on origin
joel-rieke Jul 30, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ _testmain.go
*.exe
*.test
*.prof
*.gz

# Tpc-h tbl files
benchmark/*tbl
Expand Down
File renamed without changes.
13 changes: 12 additions & 1 deletion enginetest/mysqlshim/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
package mysqlshim

import (
"errors"
"fmt"
"math"
"math/rand"
"strings"

Expand Down Expand Up @@ -143,7 +145,16 @@ func (t Table) Truncate(ctx *sql.Context) (int, error) {
return 0, err
}
err = t.db.shim.Exec("", fmt.Sprintf("TRUNCATE TABLE `%s`;", t.name))
return int(rowCount.(int64)), err
if int64RowCount, ok := rowCount.(int64); ok {
if int64RowCount >= math.MinInt && int64RowCount <= math.MaxInt {
return int(int64RowCount), err
}
} else if uint64RowCount, ok := rowCount.(uint64); ok {
if uint64RowCount <= math.MaxInt {
return int(uint64RowCount), err
}
}
return 0, errors.New("unable to convers row count")
}

// AddColumn implements the interface sql.AlterableTable.
Expand Down
File renamed without changes.
30 changes: 17 additions & 13 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/go-sql-driver/mysql v1.6.0
github.com/gocraft/dbr/v2 v2.7.2
github.com/google/flatbuffers v2.0.6+incompatible
github.com/google/uuid v1.2.0
github.com/google/uuid v1.3.1
github.com/hashicorp/golang-lru v0.5.4
github.com/lestrrat-go/strftime v1.0.4
github.com/mitchellh/hashstructure v1.1.0
Expand All @@ -20,27 +20,31 @@ require (
github.com/stretchr/testify v1.7.1
go.opentelemetry.io/otel v1.7.0
go.opentelemetry.io/otel/trace v1.7.0
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
golang.org/x/text v0.3.7
golang.org/x/tools v0.1.10
golang.org/x/sync v0.14.0
golang.org/x/text v0.25.0
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d
gopkg.in/src-d/go-errors.v1 v1.0.0
)

require (
golang.org/x/crypto v0.38.0 // indirect
golang.org/x/net v0.40.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d // indirect
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3 // indirect
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/genproto v0.0.0-20210506142907-4a47615972c2 // indirect
google.golang.org/grpc v1.37.0 // indirect
google.golang.org/protobuf v1.27.1 // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/sys v0.33.0 // indirect
google.golang.org/grpc v1.59.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b // indirect
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 // indirect
gopkg.in/yaml.v3 v3.0.0 // indirect
)

replace github.com/oliveagle/jsonpath => github.com/dolthub/jsonpath v0.0.0-20210609232853-d49537a30474

go 1.19
go 1.23.0
101 changes: 28 additions & 73 deletions go.sum
100755 → 100644

Large diffs are not rendered by default.

67 changes: 59 additions & 8 deletions memory/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"sort"
"strconv"
"strings"
"sync"
"time"

"github.com/dolthub/vitess/go/sqltypes"
Expand Down Expand Up @@ -87,6 +88,7 @@ type Table struct {
columns []int

// Data storage
partitionMux sync.RWMutex
partitions map[string][]sql.Row
partitionKeys [][]byte

Expand Down Expand Up @@ -170,6 +172,7 @@ func NewPartitionedTableWithCollation(name string, schema sql.PrimaryKeySchema,
schema: schema,
fkColl: fkColl,
collation: collation,
partitionMux: sync.RWMutex{},
partitions: partitions,
partitionKeys: keys,
autoIncVal: autoIncVal,
Expand All @@ -178,7 +181,7 @@ func NewPartitionedTableWithCollation(name string, schema sql.PrimaryKeySchema,
}

// Name implements the sql.Table interface.
func (t Table) Name() string {
func (t *Table) Name() string {
return t.name
}

Expand All @@ -196,7 +199,9 @@ func (t *Table) Collation() sql.CollationID {
}

func (t *Table) GetPartition(key string) []sql.Row {
t.partitionMux.RLock()
rows, ok := t.partitions[string(key)]
t.partitionMux.RUnlock()
if ok {
return rows
}
Expand All @@ -208,9 +213,11 @@ func (t *Table) GetPartition(key string) []sql.Row {
func (t *Table) Partitions(ctx *sql.Context) (sql.PartitionIter, error) {
var keys [][]byte
for _, k := range t.partitionKeys {
t.partitionMux.RLock()
if rows, ok := t.partitions[string(k)]; ok && len(rows) > 0 {
keys = append(keys, k)
}
t.partitionMux.RUnlock()
}
return &partitionIter{keys: keys}, nil
}
Expand Down Expand Up @@ -245,7 +252,11 @@ type rangePartition struct {

// PartitionCount implements the sql.PartitionCounter interface.
func (t *Table) PartitionCount(ctx *sql.Context) (int64, error) {
return int64(len(t.partitions)), nil
t.partitionMux.RLock()
partLen := len(t.partitions)
t.partitionMux.RUnlock()

return int64(partLen), nil
}

// PartitionRows implements the sql.PartitionRows interface.
Expand All @@ -256,14 +267,17 @@ func (t *Table) PartitionRows(ctx *sql.Context, partition sql.Partition) (sql.Ro
filters = append(t.filters, r.rang)
}

t.partitionMux.RLock()
rows, ok := t.partitions[string(partition.Key())]
if !ok {
t.partitionMux.RUnlock()
return nil, sql.ErrPartitionNotFound.New(partition.Key())
}
// The slice could be altered by other operations taking place during iteration (such as deletion or insertion), so
// make a copy of the values as they exist when execution begins.
rowsCopy := make([]sql.Row, len(rows))
copy(rowsCopy, rows)
t.partitionMux.RUnlock()

return &tableIter{
rows: rowsCopy,
Expand All @@ -274,9 +288,11 @@ func (t *Table) PartitionRows(ctx *sql.Context, partition sql.Partition) (sql.Ro

func (t *Table) numRows(ctx *sql.Context) (uint64, error) {
var count uint64 = 0
t.partitionMux.RLock()
for _, rows := range t.partitions {
count += uint64(len(rows))
}
t.partitionMux.RUnlock()

return count, nil
}
Expand Down Expand Up @@ -570,10 +586,12 @@ func (t *Table) newTableEditor() *tableEditor {

func (t *Table) Truncate(ctx *sql.Context) (int, error) {
count := 0
t.partitionMux.Lock()
for key := range t.partitions {
count += len(t.partitions[key])
t.partitions[key] = nil
}
t.partitionMux.Unlock()
return count, nil
}

Expand Down Expand Up @@ -715,6 +733,7 @@ func (t *Table) addColumnToSchema(ctx *sql.Context, newCol *sql.Column, order *s
t.autoIncVal = 0

if newColIdx < len(t.schema.Schema) {
t.partitionMux.RLock()
for _, p := range t.partitions {
for _, row := range p {
if row[newColIdx] == nil {
Expand All @@ -736,6 +755,7 @@ func (t *Table) addColumnToSchema(ctx *sql.Context, newCol *sql.Column, order *s
}
}
}
t.partitionMux.RUnlock()
} else {
t.autoIncVal = 0
}
Expand All @@ -758,8 +778,10 @@ func (t *Table) addColumnToSchema(ctx *sql.Context, newCol *sql.Column, order *s
}

func (t *Table) insertValueInRows(ctx *sql.Context, idx int, colDefault *sql.ColumnDefaultValue) error {
t.partitionMux.RLock()
for k, p := range t.partitions {
newP := make([]sql.Row, len(p))
partLen := len(p)
newP := make([]sql.Row, partLen)
for i, row := range p {
var newRow sql.Row
newRow = append(newRow, row[:idx]...)
Expand All @@ -776,13 +798,19 @@ func (t *Table) insertValueInRows(ctx *sql.Context, idx int, colDefault *sql.Col
}
newP[i] = newRow
}
t.partitionMux.RUnlock()
t.partitionMux.Lock()
t.partitions[k] = newP
t.partitionMux.Unlock()
t.partitionMux.RLock()
}
t.partitionMux.RUnlock()
return nil
}

func (t *Table) DropColumn(ctx *sql.Context, columnName string) error {
droppedCol := t.dropColumnFromSchema(ctx, columnName)
t.partitionMux.RLock()
for k, p := range t.partitions {
newP := make([]sql.Row, len(p))
for i, row := range p {
Expand All @@ -791,8 +819,13 @@ func (t *Table) DropColumn(ctx *sql.Context, columnName string) error {
newRow = append(newRow, row[droppedCol+1:]...)
newP[i] = newRow
}
t.partitionMux.RUnlock()
t.partitionMux.Lock()
t.partitions[k] = newP
t.partitionMux.Unlock()
t.partitionMux.RLock()
}
t.partitionMux.RUnlock()
return nil
}

Expand Down Expand Up @@ -857,6 +890,7 @@ func (t *Table) ModifyColumn(ctx *sql.Context, columnName string, column *sql.Co
}
}

t.partitionMux.RLock()
for k, p := range t.partitions {
newP := make([]sql.Row, len(p))
for i, row := range p {
Expand All @@ -876,8 +910,14 @@ func (t *Table) ModifyColumn(ctx *sql.Context, columnName string, column *sql.Co
newRow = append(newRow, oldRowWithoutVal[newIdx:]...)
newP[i] = newRow
}

t.partitionMux.RUnlock()
t.partitionMux.Lock()
t.partitions[k] = newP
t.partitionMux.Unlock()
t.partitionMux.RLock()
}
t.partitionMux.RUnlock()

pkNameToOrdIdx := make(map[string]int)
for i, ord := range t.schema.PkOrdinals {
Expand Down Expand Up @@ -1306,6 +1346,8 @@ func (t *Table) errIfDuplicateEntryExist(cols []string, idxName string) error {
return err
}
unique := make(map[uint64]struct{})
t.partitionMux.RLock()
defer t.partitionMux.RUnlock()
for _, partition := range t.partitions {
for _, row := range partition {
idxPrefixKey := projectOnRow(columnMapping, row)
Expand Down Expand Up @@ -1516,14 +1558,18 @@ func (t *Table) sortRows() {
}

var idx []partidx
t.partitionMux.RLock()
for _, k := range t.partitionKeys {
p := t.partitions[string(k)]
for i := 0; i < len(p); i++ {
idx = append(idx, partidx{string(k), i})
}
}
t.partitionMux.RUnlock()

sort.Sort(partitionssort{t.partitions, idx, less})
t.partitionMux.Lock()
defer t.partitionMux.Unlock()
sort.Sort(partitionssort{&t.partitionMux, t.partitions, idx, less})
}

type partidx struct {
Expand All @@ -1532,9 +1578,10 @@ type partidx struct {
}

type partitionssort struct {
ps map[string][]sql.Row
idx []partidx
less func(l, r sql.Row) bool
partitionMux *sync.RWMutex
ps map[string][]sql.Row
idx []partidx
less func(l, r sql.Row) bool
}

func (ps partitionssort) Len() int {
Expand Down Expand Up @@ -1576,10 +1623,14 @@ func copyschema(sch sql.Schema) sql.Schema {
}

func newTable(t *Table, newSch sql.PrimaryKeySchema) (*Table, error) {
newTable := NewPartitionedTableWithCollation(t.name, newSch, t.fkColl, len(t.partitions), t.collation)
partitionCount, _ := t.PartitionCount(nil)

newTable := NewPartitionedTableWithCollation(t.name, newSch, t.fkColl, int(partitionCount), t.collation)
for _, partition := range t.partitions {
for _, partitionRow := range partition {
t.partitionMux.Lock()
err := newTable.Insert(sql.NewEmptyContext(), partitionRow)
t.partitionMux.Unlock()
if err != nil {
return nil, err
}
Expand Down
Loading