mirror of
https://github.com/postgres/postgres.git
synced 2025-07-05 07:21:24 +03:00
Suggest to the user the column they may have meant to reference.
Error messages informing the user that no such column exists can sometimes provoke a perplexed response. This often happens due to a subtle typo in the column name or, perhaps less likely, in the alias name. To speed discovery of what the real issue is in such cases, we'll now search the range table for approximate matches. If there are one or two such matches that are good enough to think that they might be what the user intended to type, and better than all other approximate matches, we'll issue a hint suggesting that the user might have intended to reference those columns. Peter Geoghegan and Robert Haas
This commit is contained in:
@ -536,6 +536,7 @@ create table atacc1 ( test int );
|
||||
-- add a check constraint (fails)
|
||||
alter table atacc1 add constraint atacc_test1 check (test1>3);
|
||||
ERROR: column "test1" does not exist
|
||||
HINT: Perhaps you meant to reference the column "atacc1"."test".
|
||||
drop table atacc1;
|
||||
-- something a little more complicated
|
||||
create table atacc1 ( test int, test2 int, test3 int);
|
||||
@ -1342,6 +1343,7 @@ select f1 from c1;
|
||||
ERROR: column "f1" does not exist
|
||||
LINE 1: select f1 from c1;
|
||||
^
|
||||
HINT: Perhaps you meant to reference the column "c1"."f2".
|
||||
drop table p1 cascade;
|
||||
NOTICE: drop cascades to table c1
|
||||
create table p1 (f1 int, f2 int);
|
||||
@ -1355,6 +1357,7 @@ select f1 from c1;
|
||||
ERROR: column "f1" does not exist
|
||||
LINE 1: select f1 from c1;
|
||||
^
|
||||
HINT: Perhaps you meant to reference the column "c1"."f2".
|
||||
drop table p1 cascade;
|
||||
NOTICE: drop cascades to table c1
|
||||
create table p1 (f1 int, f2 int);
|
||||
|
@ -2222,6 +2222,12 @@ select * from t1 left join t2 on (t1.a = t2.a);
|
||||
200 | 1000 | 200 | 2001
|
||||
(5 rows)
|
||||
|
||||
-- Test matching of column name with wrong alias
|
||||
select t1.x from t1 join t3 on (t1.a = t3.x);
|
||||
ERROR: column t1.x does not exist
|
||||
LINE 1: select t1.x from t1 join t3 on (t1.a = t3.x);
|
||||
^
|
||||
HINT: Perhaps you meant to reference the column "t3"."x".
|
||||
--
|
||||
-- regression test for 8.1 merge right join bug
|
||||
--
|
||||
@ -3433,6 +3439,38 @@ select * from
|
||||
----+----+----+----
|
||||
(0 rows)
|
||||
|
||||
--
|
||||
-- Test hints given on incorrect column references are useful
|
||||
--
|
||||
select t1.uunique1 from
|
||||
tenk1 t1 join tenk2 t2 on t1.two = t2.two; -- error, prefer "t1" suggestipn
|
||||
ERROR: column t1.uunique1 does not exist
|
||||
LINE 1: select t1.uunique1 from
|
||||
^
|
||||
HINT: Perhaps you meant to reference the column "t1"."unique1".
|
||||
select t2.uunique1 from
|
||||
tenk1 t1 join tenk2 t2 on t1.two = t2.two; -- error, prefer "t2" suggestion
|
||||
ERROR: column t2.uunique1 does not exist
|
||||
LINE 1: select t2.uunique1 from
|
||||
^
|
||||
HINT: Perhaps you meant to reference the column "t2"."unique1".
|
||||
select uunique1 from
|
||||
tenk1 t1 join tenk2 t2 on t1.two = t2.two; -- error, suggest both at once
|
||||
ERROR: column "uunique1" does not exist
|
||||
LINE 1: select uunique1 from
|
||||
^
|
||||
HINT: Perhaps you meant to reference the column "t1"."unique1" or the column "t2"."unique1".
|
||||
--
|
||||
-- Take care to reference the correct RTE
|
||||
--
|
||||
select atts.relid::regclass, s.* from pg_stats s join
|
||||
pg_attribute a on s.attname = a.attname and s.tablename =
|
||||
a.attrelid::regclass::text join (select unnest(indkey) attnum,
|
||||
indexrelid from pg_index i) atts on atts.attnum = a.attnum where
|
||||
schemaname != 'pg_catalog';
|
||||
ERROR: column atts.relid does not exist
|
||||
LINE 1: select atts.relid::regclass, s.* from pg_stats s join
|
||||
^
|
||||
--
|
||||
-- Test LATERAL
|
||||
--
|
||||
|
@ -397,6 +397,10 @@ insert into t2a values (200, 2001);
|
||||
|
||||
select * from t1 left join t2 on (t1.a = t2.a);
|
||||
|
||||
-- Test matching of column name with wrong alias
|
||||
|
||||
select t1.x from t1 join t3 on (t1.a = t3.x);
|
||||
|
||||
--
|
||||
-- regression test for 8.1 merge right join bug
|
||||
--
|
||||
@ -1060,6 +1064,26 @@ select * from
|
||||
int8_tbl x join (int4_tbl x cross join int4_tbl y(ff)) j on q1 = f1; -- ok
|
||||
|
||||
--
|
||||
-- Test hints given on incorrect column references are useful
|
||||
--
|
||||
|
||||
select t1.uunique1 from
|
||||
tenk1 t1 join tenk2 t2 on t1.two = t2.two; -- error, prefer "t1" suggestipn
|
||||
select t2.uunique1 from
|
||||
tenk1 t1 join tenk2 t2 on t1.two = t2.two; -- error, prefer "t2" suggestion
|
||||
select uunique1 from
|
||||
tenk1 t1 join tenk2 t2 on t1.two = t2.two; -- error, suggest both at once
|
||||
|
||||
--
|
||||
-- Take care to reference the correct RTE
|
||||
--
|
||||
|
||||
select atts.relid::regclass, s.* from pg_stats s join
|
||||
pg_attribute a on s.attname = a.attname and s.tablename =
|
||||
a.attrelid::regclass::text join (select unnest(indkey) attnum,
|
||||
indexrelid from pg_index i) atts on atts.attnum = a.attnum where
|
||||
schemaname != 'pg_catalog';
|
||||
--
|
||||
-- Test LATERAL
|
||||
--
|
||||
|
||||
|
Reference in New Issue
Block a user