/
alterschema.go
executable file
·874 lines (748 loc) · 24.2 KB
/
alterschema.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
// Copyright 2022 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package sqle
import (
"context"
"errors"
"fmt"
"strings"
"github.com/dolthub/go-mysql-server/sql"
"github.com/dolthub/dolt/go/libraries/doltcore/doltdb"
"github.com/dolthub/dolt/go/libraries/doltcore/row"
"github.com/dolthub/dolt/go/libraries/doltcore/schema"
"github.com/dolthub/dolt/go/libraries/doltcore/schema/typeinfo"
"github.com/dolthub/dolt/go/libraries/doltcore/table/editor"
"github.com/dolthub/dolt/go/store/types"
)
// renameTable renames a table with in a RootValue and returns the updated root.
func renameTable(ctx context.Context, root *doltdb.RootValue, oldName, newName string) (*doltdb.RootValue, error) {
if newName == oldName {
return root, nil
} else if root == nil {
panic("invalid parameters")
}
return root.RenameTable(ctx, oldName, newName)
}
// Nullable represents whether a column can have a null value.
type Nullable bool
const (
NotNull Nullable = false
Null Nullable = true
)
// addColumnToTable adds a new column to the schema given and returns the new table value. Non-null column additions
// rewrite the entire table, since we must write a value for each row. If the column is not nullable, a default value
// must be provided.
//
// Returns an error if the column added conflicts with the existing schema in tag or name.
func addColumnToTable(
ctx context.Context,
root *doltdb.RootValue,
tbl *doltdb.Table,
tblName string,
tag uint64,
newColName string,
typeInfo typeinfo.TypeInfo,
nullable Nullable,
defaultVal *sql.ColumnDefaultValue,
comment string,
order *sql.ColumnOrder,
) (*doltdb.Table, error) {
oldSchema, err := tbl.GetSchema(ctx)
if err != nil {
return nil, err
}
if schema.IsKeyless(oldSchema) {
return nil, ErrKeylessAltTbl
}
if err := validateNewColumn(ctx, root, tbl, tblName, tag, newColName, typeInfo); err != nil {
return nil, err
}
newCol, err := createColumn(nullable, newColName, tag, typeInfo, defaultVal.String(), comment)
if err != nil {
return nil, err
}
newSchema, err := oldSchema.AddColumn(newCol, orderToOrder(order))
if err != nil {
return nil, err
}
newTable, err := tbl.UpdateSchema(ctx, newSchema)
if err != nil {
return nil, err
}
// TODO: we do a second pass in the engine to set a default if there is one. We should only do a single table scan.
return newTable.AddColumnToRows(ctx, newColName, newSchema)
}
func orderToOrder(order *sql.ColumnOrder) *schema.ColumnOrder {
if order == nil {
return nil
}
return &schema.ColumnOrder{
First: order.First,
AfterColumn: order.AfterColumn,
}
}
func createColumn(nullable Nullable, newColName string, tag uint64, typeInfo typeinfo.TypeInfo, defaultVal, comment string) (schema.Column, error) {
if nullable {
return schema.NewColumnWithTypeInfo(newColName, tag, typeInfo, false, defaultVal, false, comment)
} else {
return schema.NewColumnWithTypeInfo(newColName, tag, typeInfo, false, defaultVal, false, comment, schema.NotNullConstraint{})
}
}
// ValidateNewColumn returns an error if the column as specified cannot be added to the schema given.
func validateNewColumn(
ctx context.Context,
root *doltdb.RootValue,
tbl *doltdb.Table,
tblName string,
tag uint64,
newColName string,
typeInfo typeinfo.TypeInfo,
) error {
if typeInfo == nil {
return fmt.Errorf(`typeinfo may not be nil`)
}
sch, err := tbl.GetSchema(ctx)
if err != nil {
return err
}
cols := sch.GetAllCols()
err = cols.Iter(func(currColTag uint64, currCol schema.Column) (stop bool, err error) {
if currColTag == tag {
return false, schema.ErrTagPrevUsed(tag, newColName, tblName)
} else if strings.ToLower(currCol.Name) == strings.ToLower(newColName) {
return true, fmt.Errorf("A column with the name %s already exists in table %s.", newColName, tblName)
}
return false, nil
})
if err != nil {
return err
}
_, tblName, found, err := root.GetTableByColTag(ctx, tag)
if err != nil {
return err
}
if found {
return schema.ErrTagPrevUsed(tag, newColName, tblName)
}
return nil
}
var ErrPrimaryKeySetsIncompatible = errors.New("primary key sets incompatible")
// modifyColumn modifies the column with the name given, replacing it with the new definition provided. A column with
// the name given must exist in the schema of the table.
func modifyColumn(
ctx context.Context,
tbl *doltdb.Table,
existingCol schema.Column,
newCol schema.Column,
order *sql.ColumnOrder,
opts editor.Options,
) (*doltdb.Table, error) {
sch, err := tbl.GetSchema(ctx)
if err != nil {
return nil, err
}
if strings.ToLower(existingCol.Name) == strings.ToLower(newCol.Name) {
newCol.Name = existingCol.Name
}
if err := validateModifyColumn(ctx, tbl, existingCol, newCol); err != nil {
return nil, err
}
// Modify statements won't include key info, so fill it in from the old column
if existingCol.IsPartOfPK {
newCol.IsPartOfPK = true
if schema.IsColSpatialType(newCol) {
return nil, fmt.Errorf("can't use Spatial Types as Primary Key for table")
}
foundNotNullConstraint := false
for _, constraint := range newCol.Constraints {
if _, ok := constraint.(schema.NotNullConstraint); ok {
foundNotNullConstraint = true
break
}
}
if !foundNotNullConstraint {
newCol.Constraints = append(newCol.Constraints, schema.NotNullConstraint{})
}
}
newSchema, err := replaceColumnInSchema(sch, existingCol, newCol, order)
if err != nil {
return nil, err
}
updatedTable, err := updateTableWithModifiedColumn(ctx, tbl, sch, newSchema, existingCol, newCol, opts)
if err != nil {
return nil, err
}
return updatedTable, nil
}
// validateModifyColumn returns an error if the column as specified cannot be added to the schema given.
func validateModifyColumn(ctx context.Context, tbl *doltdb.Table, existingCol schema.Column, modifiedCol schema.Column) error {
sch, err := tbl.GetSchema(ctx)
if err != nil {
return err
}
if existingCol.Name != modifiedCol.Name {
cols := sch.GetAllCols()
err = cols.Iter(func(currColTag uint64, currCol schema.Column) (stop bool, err error) {
if currColTag == modifiedCol.Tag {
return false, nil
} else if strings.ToLower(currCol.Name) == strings.ToLower(modifiedCol.Name) {
return true, fmt.Errorf("A column with the name %s already exists.", modifiedCol.Name)
}
return false, nil
})
if err != nil {
return err
}
}
return nil
}
// updateTableWithModifiedColumn updates the existing table with the new schema. If the column type has changed, then
// the data is updated.
func updateTableWithModifiedColumn(ctx context.Context, tbl *doltdb.Table, oldSch, newSch schema.Schema, oldCol, modifiedCol schema.Column, opts editor.Options) (*doltdb.Table, error) {
vrw := tbl.ValueReadWriter()
rowData, err := tbl.GetNomsRowData(ctx)
if err != nil {
return nil, err
}
if !oldCol.TypeInfo.Equals(modifiedCol.TypeInfo) {
if schema.IsKeyless(newSch) {
return nil, fmt.Errorf("keyless table column type alteration is not yet supported")
}
rowData, err = updateRowDataWithNewType(ctx, rowData, tbl.ValueReadWriter(), oldSch, newSch, oldCol, modifiedCol)
if err != nil {
return nil, err
}
} else if !modifiedCol.IsNullable() {
err = rowData.Iter(ctx, func(key, value types.Value) (stop bool, err error) {
r, err := row.FromNoms(newSch, key.(types.Tuple), value.(types.Tuple))
if err != nil {
return false, err
}
val, ok := r.GetColVal(modifiedCol.Tag)
if !ok || val == nil || val == types.NullValue {
return true, fmt.Errorf("cannot change column to NOT NULL when one or more values is NULL")
}
return false, nil
})
if err != nil {
return nil, err
}
}
indexData, err := tbl.GetIndexSet(ctx)
if err != nil {
return nil, err
}
var autoVal uint64
if schema.HasAutoIncrement(newSch) && schema.HasAutoIncrement(oldSch) {
autoVal, err = tbl.GetAutoIncrementValue(ctx)
if err != nil {
return nil, err
}
}
updatedTable, err := doltdb.NewNomsTable(ctx, vrw, newSch, rowData, indexData, types.Uint(autoVal))
if err != nil {
return nil, err
}
if !oldCol.TypeInfo.Equals(modifiedCol.TypeInfo) {
// If we're modifying the primary key then all indexes are affected. Otherwise we just want to update the
// touched ones.
if modifiedCol.IsPartOfPK {
for _, index := range newSch.Indexes().AllIndexes() {
indexRowData, err := editor.RebuildIndex(ctx, updatedTable, index.Name(), opts)
if err != nil {
return nil, err
}
updatedTable, err = updatedTable.SetNomsIndexRows(ctx, index.Name(), indexRowData)
if err != nil {
return nil, err
}
}
} else {
for _, index := range newSch.Indexes().IndexesWithTag(modifiedCol.Tag) {
indexRowData, err := editor.RebuildIndex(ctx, updatedTable, index.Name(), opts)
if err != nil {
return nil, err
}
updatedTable, err = updatedTable.SetNomsIndexRows(ctx, index.Name(), indexRowData)
if err != nil {
return nil, err
}
}
}
}
return updatedTable, nil
}
// updateRowDataWithNewType returns a new map of row data containing the updated rows from the changed schema column type.
func updateRowDataWithNewType(
ctx context.Context,
rowData types.Map,
vrw types.ValueReadWriter,
oldSch, newSch schema.Schema,
oldCol, newCol schema.Column,
) (types.Map, error) {
// If there are no rows then we can immediately return. All type conversions are valid for tables without rows, but
// when rows are present then it is no longer true. GetTypeConverter assumes that there are rows present, so it
// will return a failure on a type conversion that should work for the empty table.
if rowData.Len() == 0 {
return rowData, nil
}
convFunc, _, err := typeinfo.GetTypeConverter(ctx, oldCol.TypeInfo, newCol.TypeInfo)
if err != nil {
return types.EmptyMap, err
}
if !newCol.IsNullable() {
originalConvFunc := convFunc
convFunc = func(ctx context.Context, vrw types.ValueReadWriter, v types.Value) (types.Value, error) {
if v == nil || v == types.NullValue {
return nil, fmt.Errorf("cannot change column to NOT NULL when one or more values is NULL")
}
return originalConvFunc(ctx, vrw, v)
}
}
var lastKey types.Value
mapEditor := rowData.Edit()
err = rowData.Iter(ctx, func(key, value types.Value) (stop bool, err error) {
r, err := row.FromNoms(oldSch, key.(types.Tuple), value.(types.Tuple))
if err != nil {
return true, err
}
taggedVals, err := r.TaggedValues()
if err != nil {
return true, err
}
// We skip the "ok" check as nil is returned if the value does not exist, and we still want to check nil.
// The underscore is important, otherwise a missing value would result in a panic.
val, _ := taggedVals[oldCol.Tag]
delete(taggedVals, oldCol.Tag) // If there was no value then delete is a no-op so this is safe
newVal, err := convFunc(ctx, vrw, val)
if err != nil {
return true, err
}
// convFunc returns types.NullValue rather than nil so it's always safe to compare
if newVal.Equals(val) {
newRowKey, err := r.NomsMapKey(newSch).Value(ctx)
if err != nil {
return true, err
}
if newCol.IsPartOfPK && newRowKey.Equals(lastKey) {
return true, fmt.Errorf("pk violation when altering column type and rewriting values")
}
lastKey = newRowKey
return false, nil
} else if newVal != types.NullValue {
taggedVals[newCol.Tag] = newVal
}
r, err = row.New(rowData.Format(), newSch, taggedVals)
if err != nil {
return true, err
}
newRowKey, err := r.NomsMapKey(newSch).Value(ctx)
if err != nil {
return true, err
}
if newCol.IsPartOfPK {
mapEditor.Remove(key)
if newRowKey.Equals(lastKey) {
return true, fmt.Errorf("pk violation when altering column type and rewriting values")
}
}
lastKey = newRowKey
mapEditor.Set(newRowKey, r.NomsMapValue(newSch))
return false, nil
})
if err != nil {
return types.EmptyMap, err
}
return mapEditor.Map(ctx)
}
// replaceColumnInSchema replaces the column with the name given with its new definition, optionally reordering it.
func replaceColumnInSchema(sch schema.Schema, oldCol schema.Column, newCol schema.Column, order *sql.ColumnOrder) (schema.Schema, error) {
// If no order is specified, insert in the same place as the existing column
prevColumn := ""
for _, col := range sch.GetAllCols().GetColumns() {
if col.Name == oldCol.Name {
if prevColumn == "" {
if order == nil {
order = &sql.ColumnOrder{First: true}
}
}
break
} else {
prevColumn = col.Name
}
}
if order == nil {
if prevColumn != "" {
order = &sql.ColumnOrder{AfterColumn: prevColumn}
} else {
return nil, fmt.Errorf("Couldn't find column %s", oldCol.Name)
}
}
var newCols []schema.Column
if order.First {
newCols = append(newCols, newCol)
}
for _, col := range sch.GetAllCols().GetColumns() {
if col.Name != oldCol.Name {
newCols = append(newCols, col)
}
if order.AfterColumn == col.Name {
newCols = append(newCols, newCol)
}
}
collection := schema.NewColCollection(newCols...)
err := schema.ValidateForInsert(collection)
if err != nil {
return nil, err
}
newSch, err := schema.SchemaFromCols(collection)
if err != nil {
return nil, err
}
for _, index := range sch.Indexes().AllIndexes() {
tags := index.IndexedColumnTags()
for i := range tags {
if tags[i] == oldCol.Tag {
tags[i] = newCol.Tag
}
}
_, err = newSch.Indexes().AddIndexByColTags(index.Name(), tags, schema.IndexProperties{
IsUnique: index.IsUnique(),
IsUserDefined: index.IsUserDefined(),
Comment: index.Comment(),
})
if err != nil {
return nil, err
}
}
// Copy over all checks from the old schema
for _, check := range sch.Checks().AllChecks() {
_, err := newSch.Checks().AddCheck(check.Name(), check.Expression(), check.Enforced())
if err != nil {
return nil, err
}
}
pkOrds, err := modifyPkOrdinals(sch, newSch)
if err != nil {
return nil, err
}
err = newSch.SetPkOrdinals(pkOrds)
if err != nil {
return nil, err
}
return newSch, nil
}
// modifyPkOrdinals tries to create primary key ordinals for a newSch maintaining
// the relative positions of PKs from the oldSch. Return an ErrPrimaryKeySetsIncompatible
// error if the two schemas have a different number of primary keys, or a primary
// key column's tag changed between the two sets.
// TODO: move this to schema package
func modifyPkOrdinals(oldSch, newSch schema.Schema) ([]int, error) {
if newSch.GetPKCols().Size() != oldSch.GetPKCols().Size() {
return nil, ErrPrimaryKeySetsIncompatible
}
newPkOrdinals := make([]int, len(newSch.GetPkOrdinals()))
for _, newCol := range newSch.GetPKCols().GetColumns() {
// ordIdx is the relative primary key order (that stays the same)
ordIdx, ok := oldSch.GetPKCols().TagToIdx[newCol.Tag]
if !ok {
// if pk tag changed, use name to find the new newCol tag
oldCol, ok := oldSch.GetPKCols().NameToCol[newCol.Name]
if !ok {
return nil, ErrPrimaryKeySetsIncompatible
}
ordIdx = oldSch.GetPKCols().TagToIdx[oldCol.Tag]
}
// ord is the schema ordering index, which may have changed in newSch
ord := newSch.GetAllCols().TagToIdx[newCol.Tag]
newPkOrdinals[ordIdx] = ord
}
return newPkOrdinals, nil
}
func addPrimaryKeyToTable(ctx context.Context, table *doltdb.Table, tableName string, nbf *types.NomsBinFormat, columns []sql.IndexColumn, opts editor.Options) (*doltdb.Table, error) {
sch, err := table.GetSchema(ctx)
if err != nil {
return nil, err
}
if sch.GetPKCols().Size() > 0 {
return nil, sql.ErrMultiplePrimaryKeysDefined.New() // Also caught in GMS
}
if schema.IsUsingSpatialColAsKey(sch) {
return nil, schema.ErrUsingSpatialKey.New(tableName)
}
pkColOrdering := make(map[string]int, len(columns))
for i, newCol := range columns {
pkColOrdering[newCol.Name] = i
}
newColl := make([]schema.Column, sch.GetAllCols().Size())
pkOrdinals := make([]int, len(columns))
for ord, col := range sch.GetAllCols().GetColumns() {
if i, ok := pkColOrdering[col.Name]; ok {
pkOrdinals[i] = ord
// Only add NOT NULL constraint iff it doesn't exist
if col.IsNullable() {
col.Constraints = append(col.Constraints, schema.NotNullConstraint{})
}
col.IsPartOfPK = true
}
newColl[ord] = col
}
newCollection := schema.NewColCollection(newColl...)
rows, err := table.GetNomsRowData(ctx)
if err != nil {
return nil, err
}
// enforce primary key nullability
err = rows.Iter(ctx, func(key, value types.Value) (stop bool, err error) {
r, err := row.FromNoms(sch, key.(types.Tuple), value.(types.Tuple))
if err != nil {
return false, err
}
err = newCollection.Iter(func(tag uint64, col schema.Column) (stop bool, err error) {
if !col.IsPartOfPK {
return false, nil
}
val, ok := r.GetColVal(tag)
if !ok || val == nil || val == types.NullValue {
return true, fmt.Errorf("primary key cannot have NULL values")
}
return false, nil
})
if err != nil {
return true, err
}
return false, nil
})
if err != nil {
return nil, err
}
newSchema, err := schema.SchemaFromCols(newCollection)
if err != nil {
return nil, err
}
// Copy over all checks from the old schema
for _, check := range sch.Checks().AllChecks() {
_, err := newSchema.Checks().AddCheck(check.Name(), check.Expression(), check.Enforced())
if err != nil {
return nil, err
}
}
err = newSchema.SetPkOrdinals(pkOrdinals)
if err != nil {
return nil, err
}
newSchema.Indexes().AddIndex(sch.Indexes().AllIndexes()...)
// Rebuild all of the indexes now that the primary key has been changed
return insertKeyedData(ctx, nbf, table, newSchema, tableName, opts)
}
func insertKeyedData(ctx context.Context, nbf *types.NomsBinFormat, oldTable *doltdb.Table, newSchema schema.Schema, name string, opts editor.Options) (*doltdb.Table, error) {
empty, err := types.NewMap(ctx, oldTable.ValueReadWriter())
if err != nil {
return nil, err
}
// Create the new Table and rebuild all the indexes
newTable, err := doltdb.NewNomsTable(ctx, oldTable.ValueReadWriter(), newSchema, empty, nil, nil)
if err != nil {
return nil, err
}
newTable, err = editor.RebuildAllIndexes(ctx, newTable, opts)
if err != nil {
return nil, err
}
// Create the table editor and insert all of the new data into it
tableEditor, err := editor.NewTableEditor(ctx, newTable, newSchema, name, opts)
if err != nil {
return nil, err
}
oldRowData, err := oldTable.GetNomsRowData(ctx)
if err != nil {
return nil, err
}
err = oldRowData.Iter(ctx, func(key types.Value, value types.Value) (stop bool, err error) {
keyless, card, err := row.KeylessRowsFromTuples(key.(types.Tuple), value.(types.Tuple))
if err != nil {
return true, err
}
// A row that exists more than once must be a duplicate.
if card > 1 {
return true, fmtPrimaryKeyError(newSchema, keyless)
}
taggedVals, err := keyless.TaggedValues()
if err != nil {
return true, err
}
keyedRow, err := row.New(nbf, newSchema, taggedVals)
if err != nil {
return true, err
}
err = tableEditor.InsertRow(ctx, keyedRow, duplicatePkFunction)
if err != nil {
return true, err
}
return false, nil
})
if err != nil {
return nil, err
}
return tableEditor.Table(ctx)
}
func fmtPrimaryKeyError(sch schema.Schema, keylessRow row.Row) error {
pkTags := sch.GetPKCols().Tags
vals := make([]string, len(pkTags))
for i, tg := range sch.GetPKCols().Tags {
val, ok := keylessRow.GetColVal(tg)
if !ok {
panic("tag for primary key wasn't found")
}
vals[i] = val.HumanReadableString()
}
return sql.NewUniqueKeyErr(fmt.Sprintf("[%s]", strings.Join(vals, ",")), true, sql.Row{vals})
}
func duplicatePkFunction(keyString, indexName string, k, v types.Tuple, isPk bool) error {
return sql.NewUniqueKeyErr(fmt.Sprintf("%s", keyString), true, sql.Row{})
}
var ErrKeylessAltTbl = errors.New("schema alterations not supported for keyless tables")
// dropColumn drops a column from a table, and removes its associated cell values
func dropColumn(ctx context.Context, tbl *doltdb.Table, colName string) (*doltdb.Table, error) {
if tbl == nil {
panic("invalid parameters")
}
sch, err := tbl.GetSchema(ctx)
if err != nil {
return nil, err
}
if schema.IsKeyless(sch) {
return nil, ErrKeylessAltTbl
}
if col, ok := sch.GetAllCols().GetByName(colName); !ok {
return nil, schema.ErrColNotFound
} else if col.IsPartOfPK {
return nil, errors.New("Cannot drop column in primary key")
}
for _, index := range sch.Indexes().IndexesWithColumn(colName) {
_, err = sch.Indexes().RemoveIndex(index.Name())
if err != nil {
return nil, err
}
tbl, err = tbl.DeleteIndexRowData(ctx, index.Name())
if err != nil {
return nil, err
}
}
cols := make([]schema.Column, 0)
for _, col := range sch.GetAllCols().GetColumns() {
if col.Name == colName {
continue
}
cols = append(cols, col)
}
colColl := schema.NewColCollection(cols...)
newSch, err := schema.SchemaFromCols(colColl)
if err != nil {
return nil, err
}
newSch.Indexes().AddIndex(sch.Indexes().AllIndexes()...)
// Copy over all checks from the old schema
for _, check := range sch.Checks().AllChecks() {
_, err = newSch.Checks().AddCheck(check.Name(), check.Expression(), check.Enforced())
if err != nil {
return nil, err
}
}
pkOrds, err := modifyPkOrdinals(sch, newSch)
if err != nil {
return nil, err
}
err = newSch.SetPkOrdinals(pkOrds)
if err != nil {
return nil, err
}
return tbl.UpdateSchema(ctx, newSch)
}
func dropPrimaryKeyFromTable(ctx context.Context, table *doltdb.Table, nbf *types.NomsBinFormat, opts editor.Options) (*doltdb.Table, error) {
sch, err := table.GetSchema(ctx)
if err != nil {
return nil, err
}
if sch.GetPKCols().Size() == 0 {
return nil, sql.ErrCantDropFieldOrKey.New("PRIMARY")
}
// Modify the schema to convert the primary key cols into non primary key cols
newCollection := schema.MapColCollection(sch.GetAllCols(), func(col schema.Column) schema.Column {
// If it was part of primary key
if col.IsPartOfPK {
// No longer part of primary key
col.IsPartOfPK = false
// Removing PK does not remove NOT NULL constraint, so add it back if it's somehow gone
if col.IsNullable() {
col.Constraints = append(col.Constraints, schema.NotNullConstraint{})
}
}
return col
})
newSchema, err := schema.SchemaFromCols(newCollection)
if err != nil {
return nil, err
}
newSchema.Indexes().AddIndex(sch.Indexes().AllIndexes()...)
// Copy over all checks from the old schema
for _, check := range sch.Checks().AllChecks() {
_, err := newSchema.Checks().AddCheck(check.Name(), check.Expression(), check.Enforced())
if err != nil {
return nil, err
}
}
table, err = table.UpdateSchema(ctx, newSchema)
if err != nil {
return nil, err
}
// Convert all of the keyed row data to keyless row data
rowData, err := table.GetNomsRowData(ctx)
if err != nil {
return nil, err
}
newRowData, err := keyedRowDataToKeylessRowData(ctx, nbf, table.ValueReadWriter(), rowData, newSchema)
if err != nil {
return nil, err
}
table, err = table.UpdateNomsRows(ctx, newRowData)
if err != nil {
return nil, err
}
// Rebuild all of the indexes now that the primary key has been changed
return editor.RebuildAllIndexes(ctx, table, opts)
}
func keyedRowDataToKeylessRowData(ctx context.Context, nbf *types.NomsBinFormat, vrw types.ValueReadWriter, rowData types.Map, newSch schema.Schema) (types.Map, error) {
newMap, err := types.NewMap(ctx, vrw)
if err != nil {
return types.Map{}, err
}
mapEditor := newMap.Edit()
err = rowData.Iter(ctx, func(key types.Value, value types.Value) (stop bool, err error) {
taggedVals, err := row.TaggedValuesFromTupleKeyAndValue(key.(types.Tuple), value.(types.Tuple))
if err != nil {
return true, err
}
keyedRow, err := row.New(nbf, newSch, taggedVals)
if err != nil {
return true, nil
}
mapEditor = mapEditor.Set(keyedRow.NomsMapKey(newSch), keyedRow.NomsMapValue(newSch))
return false, nil
})
if err != nil {
return types.Map{}, err
}
return mapEditor.Map(ctx)
}