Skip to content

Commit

Permalink
Merge #48687
Browse files Browse the repository at this point in the history
48687: cli: fix `cockroach dump` with collated strings r=dt a=rohany

Fixes #48278.

Release note (bug fix): Fix a bug where `cockroach dump` on a table
with collated strings would omit the collation clause for the data
insertion statements.

Co-authored-by: Rohan Yadav <rohany@alumni.cmu.edu>
  • Loading branch information
craig[bot] and rohany committed May 13, 2020
2 parents fbc1595 + 4b36317 commit 8398f45
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
18 changes: 14 additions & 4 deletions pkg/cli/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,12 +519,12 @@ func constructTableMetadata(rows *sqlRows, md basicMetadata) (tableMetadata, err
}

// Transform the type name to an internal coltype.
sql := fmt.Sprintf("ALTER TABLE woo ALTER COLUMN woo SET DATA TYPE %s", typ)
sql := fmt.Sprintf("CREATE TABLE woo (x %s)", typ)
stmt, err := parser.ParseOne(sql)
if err != nil {
return tableMetadata{}, fmt.Errorf("type %s is not a valid CockroachDB type", typ)
}
ref := stmt.AST.(*tree.AlterTable).Cmds[0].(*tree.AlterTableAlterColumnType).ToType
ref := stmt.AST.(*tree.CreateTable).Defs[0].(*tree.ColumnTableDef).Type

coltyp, ok := tree.GetStaticallyKnownType(ref)
if !ok {
Expand Down Expand Up @@ -643,7 +643,7 @@ func dumpTableData(w io.Writer, conn *sqlConn, bmd basicMetadata) error {
if err != nil {
return err
}

var collationEnv tree.CollationEnvironment
bs := fmt.Sprintf("SELECT %s FROM %s AS OF SYSTEM TIME %s ORDER BY PRIMARY KEY %[2]s",
md.columnNames,
md.name,
Expand Down Expand Up @@ -714,7 +714,17 @@ func dumpTableData(w io.Writer, conn *sqlConn, bmd basicMetadata) error {
case float64:
d = tree.NewDFloat(tree.DFloat(t))
case string:
d = tree.NewDString(t)
switch ct := md.columnTypes[cols[si]]; ct.Family() {
case types.StringFamily:
d = tree.NewDString(t)
case types.CollatedStringFamily:
d, err = tree.NewDCollatedString(t, ct.Locale(), &collationEnv)
if err != nil {
return err
}
default:
return errors.AssertionFailedf("unknown string type %s", ct)
}
case []byte:
// TODO(knz): this approach is brittle+flawed, see #28948.
switch ct := md.columnTypes[cols[si]]; ct.Family() {
Expand Down
19 changes: 11 additions & 8 deletions pkg/cli/testdata/dump/row
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ CREATE TABLE d.t (
e1 decimal(2),
e2 decimal(2, 1),
s1 string(1),
s2 string collate en_u_ks_level2,
oi oid,
FAMILY "primary" (i, f, d, t, ts, n, o, u, ip, j, ary, tz, e1, e2, s1, oi, rowid),
FAMILY "primary" (i, f, d, t, ts, n, o, u, ip, j, ary, tz, e1, e2, s1, s2, oi, rowid),
FAMILY fam_1_s (s),
FAMILY fam_2_b (b),
FAMILY fam_3_e (e)
Expand All @@ -44,6 +45,7 @@ INSERT INTO d.t VALUES (
3.4,
4.5,
's',
'hello' COLLATE en_u_ks_level2,
6
);
INSERT INTO d.t VALUES (DEFAULT);
Expand Down Expand Up @@ -79,18 +81,19 @@ CREATE TABLE t (
e1 DECIMAL(2) NULL,
e2 DECIMAL(2,1) NULL,
s1 STRING(1) NULL,
s2 STRING COLLATE en_u_ks_level2 NULL,
oi OID NULL,
FAMILY "primary" (i, f, d, t, ts, n, o, u, ip, j, ary, tz, e1, e2, s1, oi, rowid),
FAMILY "primary" (i, f, d, t, ts, n, o, u, ip, j, ary, tz, e1, e2, s1, s2, oi, rowid),
FAMILY fam_1_s (s),
FAMILY fam_2_b (b),
FAMILY fam_3_e (e)
);

INSERT INTO t (i, f, s, b, d, t, ts, n, o, e, u, ip, j, ary, tz, e1, e2, s1, oi) VALUES
(1, 2.3, 'striiing', '\x613162326333', '2016-03-26', '01:02:03.456', '2016-01-25 10:10:10+00:00', '02:30:30', true, 1.2345, 'e9716c74-2638-443d-90ed-ffde7bea7d1d', '192.168.0.1', '{"a": "b"}', ARRAY['hello','world'], '2016-01-25 10:10:10+00:00', 3, 4.5, 's', 6),
(NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL),
(NULL, '+Inf', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'Infinity', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL),
(NULL, '-Inf', NULL, NULL, NULL, NULL, NULL, NULL, NULL, '-Infinity', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL),
(NULL, 'NaN', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'NaN', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
INSERT INTO t (i, f, s, b, d, t, ts, n, o, e, u, ip, j, ary, tz, e1, e2, s1, s2, oi) VALUES
(1, 2.3, 'striiing', '\x613162326333', '2016-03-26', '01:02:03.456', '2016-01-25 10:10:10+00:00', '02:30:30', true, 1.2345, 'e9716c74-2638-443d-90ed-ffde7bea7d1d', '192.168.0.1', '{"a": "b"}', ARRAY['hello','world'], '2016-01-25 10:10:10+00:00', 3, 4.5, 's', 'hello' COLLATE en_u_ks_level2, 6),
(NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL),
(NULL, '+Inf', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'Infinity', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL),
(NULL, '-Inf', NULL, NULL, NULL, NULL, NULL, NULL, NULL, '-Infinity', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL),
(NULL, 'NaN', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'NaN', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
----
----

0 comments on commit 8398f45

Please sign in to comment.