Commit 8398f454 authored by craig[bot]'s avatar craig[bot]

Merge #48687

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: default avatarRohan Yadav <[email protected]>
parents fbc15950 4b363173
......@@ -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 {
......@@ -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,
......@@ -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() {
......
......@@ -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)
......@@ -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);
......@@ -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);
----
----
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment