Skip to content

Commit bf269ed

Browse files
committed
Fix char and varchar typename printing in EXPLAIN and error msgs
1 parent b4a2db3 commit bf269ed

File tree

8 files changed

+138
-13
lines changed

8 files changed

+138
-13
lines changed

src/adapter/src/catalog.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1672,6 +1672,14 @@ impl ExprHumanizer for ConnCatalog<'_> {
16721672
.join(",")
16731673
),
16741674
PgLegacyChar => "\"char\"".into(),
1675+
Char { length } => match length {
1676+
None => "char".into(),
1677+
Some(length) => format!("char({})", length.into_u32()),
1678+
},
1679+
VarChar { max_length } => match max_length {
1680+
None => "varchar".into(),
1681+
Some(length) => format!("varchar({})", length.into_u32()),
1682+
},
16751683
UInt16 => "uint2".into(),
16761684
UInt32 => "uint4".into(),
16771685
UInt64 => "uint8".into(),

src/repr/src/explain.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,9 +438,11 @@ pub trait ExprHumanizer: fmt::Debug {
438438
fn humanize_id_parts(&self, id: GlobalId) -> Option<Vec<String>>;
439439

440440
/// Returns a human-readable name for the specified scalar type.
441+
/// Used in, e.g., EXPLAIN and error msgs.
441442
fn humanize_scalar_type(&self, ty: &ScalarType) -> String;
442443

443444
/// Returns a human-readable name for the specified column type.
445+
/// Used in, e.g., EXPLAIN and error msgs.
444446
fn humanize_column_type(&self, typ: &ColumnType) -> String {
445447
format!(
446448
"{}{}",

test/sqllogictest/array_fill.slt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,10 @@ SELECT array_fill(LIST[1], array[3, 2])
195195
query error array_fill on integer list not yet supported
196196
SELECT array_fill(LIST[1], array[3, 2], array[2, 3])
197197

198-
query error array_fill on character not yet supported
198+
query error db error: ERROR: array_fill on char\(1\) not yet supported
199199
SELECT array_fill('c'::char, array[3, 2])
200200

201-
query error array_fill on character not yet supported
201+
query error db error: ERROR: array_fill on char\(1\) not yet supported
202202
SELECT array_fill('c'::char, array[3, 2], array[2, 3])
203203

204204
query error array_fill on map\[text=>integer\] not yet supported

test/sqllogictest/char.slt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ SELECT 'a'::character = 'a'::"char";
2323
----
2424
true
2525

26-
query error coalesce could not convert type character to "char"
26+
query error db error: ERROR: coalesce could not convert type char\(1\) to "char"
2727
SELECT pg_typeof(coalesce('1'::"char", '1'::char));
2828

2929
# Fixes database-issues#5191
@@ -90,14 +90,14 @@ SELECT pg_typeof('abc'::text::"char");
9090

9191
# Fixes database-issues#5222
9292

93-
query error db error: ERROR: coalesce could not convert type "char" to character
93+
query error db error: ERROR: coalesce could not convert type "char" to char
9494
SELECT COALESCE('a'::char, 'a'::"char");
9595

96-
query error coalesce could not convert type "char" to character varying
96+
query error db error: ERROR: coalesce could not convert type "char" to varchar
9797
SELECT COALESCE('a'::varchar, 'a'::"char");
9898

99-
query error coalesce could not convert type character to "char"
99+
query error db error: ERROR: coalesce could not convert type char\(1\) to "char"
100100
SELECT COALESCE('a'::"char", 'a'::char);
101101

102-
query error db error: ERROR: coalesce could not convert type character varying to "char"
102+
query error db error: ERROR: coalesce could not convert type varchar to "char"
103103
SELECT COALESCE('a'::"char", 'a'::varchar);

test/sqllogictest/cockroach/aggregate.slt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -698,22 +698,22 @@ SELECT avg(b), sum(b) FROM abc
698698
# ----
699699
# 3 years 5 mons 7 days 00:00:19
700700

701-
query error db error: ERROR: function sum\(character varying\) does not exist
701+
query error db error: ERROR: function sum\(varchar\) does not exist
702702
SELECT avg(a) FROM abc
703703

704704
query error db error: ERROR: function sum\(boolean\) does not exist
705705
SELECT avg(c) FROM abc
706706

707-
query error db error: ERROR: function sum\(record\(f1: character varying,f2: boolean\?\)\) does not exist
707+
query error db error: ERROR: function sum\(record\(f1: varchar,f2: boolean\?\)\) does not exist
708708
SELECT avg((a,c)) FROM abc
709709

710-
query error db error: ERROR: function sum\(character varying\) does not exist
710+
query error db error: ERROR: function sum\(varchar\) does not exist
711711
SELECT sum(a) FROM abc
712712

713713
query error db error: ERROR: function sum\(boolean\) does not exist
714714
SELECT sum(c) FROM abc
715715

716-
query error db error: ERROR: function sum\(record\(f1: character varying,f2: boolean\?\)\) does not exist
716+
query error db error: ERROR: function sum\(record\(f1: varchar,f2: boolean\?\)\) does not exist
717717
SELECT sum((a,c)) FROM abc
718718

719719
statement ok

test/sqllogictest/map.slt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ SELECT '{a=>1, b=>2}'::map[text=>int] <@ '{a=>1, b=>2, c=>3}'::map[text=>int]
524524
----
525525
true
526526

527-
query error CAST does not support casting from map\[text=>map\[text=>character\]\] to map\[text=>map\[text=>character\]\]
527+
query error db error: ERROR: CAST does not support casting from map\[text=>map\[text=>char\]\] to map\[text=>map\[text=>char\(1\)\]\]
528528
SELECT '{hello=>{world=>a}}'::map[text=>map[text=>char]] <@ '{hello=>c}'::map[text=>char]
529529
----
530530
false

test/sqllogictest/type-promotion.slt

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1662,3 +1662,117 @@ values ('2023-01-01T00:00:00.666'::timestamp(6)) union all values ('2023-01-01T0
16621662

16631663
query error numeric field overflow
16641664
SELECT '1e-307'::float8::numeric
1665+
1666+
statement ok
1667+
CREATE TABLE t1(a varchar(3));
1668+
1669+
statement ok
1670+
CREATE TABLE t2(b varchar(4));
1671+
1672+
statement error db error: ERROR: value too long for type character varying\(3\)
1673+
INSERT INTO t1 VALUES ('123456');
1674+
1675+
# From the Postgres docs: https://www.postgresql.org/docs/current/datatype-character.html
1676+
# "An attempt to store a longer string into a column of these types will result in an error, unless the excess
1677+
# characters are all spaces, in which case the string will be truncated to the maximum length. (This somewhat bizarre
1678+
# exception is required by the SQL standard.)"
1679+
statement ok
1680+
INSERT INTO t1 VALUES ('111 ');
1681+
1682+
statement ok
1683+
INSERT INTO t1 VALUES ('123');
1684+
1685+
statement ok
1686+
INSERT INTO t2 VALUES ('1234');
1687+
1688+
query T multiline
1689+
EXPLAIN WITH (TYPES)
1690+
(SELECT * FROM t1) UNION ALL (SELECT * FROM t2);
1691+
----
1692+
Explained Query:
1693+
Union // { types: "(varchar?)" }
1694+
ReadStorage materialize.public.t1 // { types: "(varchar(3)?)" }
1695+
ReadStorage materialize.public.t2 // { types: "(varchar(4)?)" }
1696+
1697+
Source materialize.public.t1
1698+
Source materialize.public.t2
1699+
1700+
Target cluster: quickstart
1701+
1702+
EOF
1703+
1704+
query T
1705+
(SELECT * FROM t1) UNION ALL (SELECT * FROM t2);
1706+
----
1707+
111
1708+
123
1709+
1234
1710+
1711+
query T
1712+
(SELECT * FROM t2) UNION ALL (SELECT * FROM t1);
1713+
----
1714+
111
1715+
123
1716+
1234
1717+
1718+
statement ok
1719+
CREATE TABLE t3(b char(2));
1720+
1721+
statement ok
1722+
INSERT INTO t3 VALUES ('ab');
1723+
1724+
query T multiline
1725+
EXPLAIN WITH (TYPES)
1726+
(SELECT * FROM t1) UNION ALL (SELECT * FROM t3);
1727+
----
1728+
Explained Query:
1729+
Union // { types: "(varchar?)" }
1730+
ReadStorage materialize.public.t1 // { types: "(varchar(3)?)" }
1731+
Project (#1) // { types: "(varchar?)" }
1732+
Map (text_to_varchar[len=None](#0)) // { types: "(char(2)?, varchar?)" }
1733+
ReadStorage materialize.public.t3 // { types: "(char(2)?)" }
1734+
1735+
Source materialize.public.t1
1736+
Source materialize.public.t3
1737+
1738+
Target cluster: quickstart
1739+
1740+
EOF
1741+
1742+
query T
1743+
(SELECT * FROM t1) UNION ALL (SELECT * FROM t3);
1744+
----
1745+
ab
1746+
111
1747+
123
1748+
1749+
statement ok
1750+
CREATE TABLE t4(b char(3));
1751+
1752+
statement ok
1753+
INSERT INTO t4 VALUES ('ab ');
1754+
1755+
query T multiline
1756+
EXPLAIN WITH (TYPES)
1757+
(SELECT * FROM t3) UNION (SELECT * FROM t4);
1758+
----
1759+
Explained Query:
1760+
Distinct project=[#0] // { types: "(char?)" }
1761+
Union // { types: "(char?)" }
1762+
ReadStorage materialize.public.t3 // { types: "(char(2)?)" }
1763+
ReadStorage materialize.public.t4 // { types: "(char(3)?)" }
1764+
1765+
Source materialize.public.t3
1766+
Source materialize.public.t4
1767+
1768+
Target cluster: quickstart
1769+
1770+
EOF
1771+
1772+
# Trailing spaces are treated as semantically insignificant in both Postgres and Materialize, so the above
1773+
# 'ab' and 'ab ' end up deduplicated.
1774+
# See also in char-varchar-distinct.td
1775+
query T
1776+
(SELECT * FROM t3) UNION (SELECT * FROM t4);
1777+
----
1778+
ab

test/testdrive/char-varchar-distinct.td

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
# by the Apache License, Version 2.0.
99

1010
#
11-
# Make sure the distinct operator inside a detaflow operates correctly
11+
# Make sure the distinct operator inside a dataflow operates correctly
1212
# with respect to CHAR/VARCHAR, especially in the presence of trailing
1313
# spaces.
1414
#
15+
# The reason for this being a td and not slt is that td has "" wrapping on the output, which is convenient here.
1516

1617
> SELECT 'a '::char(5) UNION DISTINCT SELECT 'a '::char(5);
1718
"a "

0 commit comments

Comments
 (0)