Re: Implicit cast of literal in SQL statements

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Scott Royston <scroyston71(at)yahoo(dot)com>
Cc: pgsql-hackers(at)postgreSQL(dot)org, pgsql-bugs(at)postgreSQL(dot)org
Subject: Re: Implicit cast of literal in SQL statements
Date: 2001-12-27 21:22:56
Message-ID: 26391.1009488176@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs pgsql-hackers

Scott Royston <scroyston71(at)yahoo(dot)com> writes:
> I've seen a few postings in multiple newsgroups saying that in 7.1.x and
> up, literals in SQL statements are implicitly cast to strings.

That's an oversimplification: the implicit coercion of unknown literals
only happens when looking for an operator or function to apply to them.
For an unprocessed result literal such as you describe, the type
never does get changed. Which is okay because type "unknown" does have
an output routine, which is all that's needed to emit the literal.
You may care to peruse the rules in
http://developer.postgresql.org/docs/postgres/typeconv.html

> However, in both 7.1.3, and a fresh build of 7.2b4 from cvs, (with all
> regressions passing) I get:

> mytest=# select distinct 'hello' from mytable;
> ERROR: Unable to identify an ordering operator '<' for type 'unknown'
> Use an explicit ordering operator or modify the query

This is mildly annoying but I'm not sure that fixing it wouldn't
introduce greater annoyances. As an example of the pitfalls, consider:

regression=# select 1 union select '2';
?column?
----------
1
2
(2 rows)

regression=# select 1 union select '2'::text;
ERROR: UNION types "int4" and "text" not matched

The first example works because the right-hand SELECT's result is not
coerced to "text" before UNION can get its hands on it.

Possibly DISTINCT should be allowed to type-coerce unknown inputs to
text the same way that explicit operators and functions can. Offhand
I'm not sure if that's a good solution or not. There are related
cases to consider too, eg ORDER BY and GROUP BY.

regards, tom lane

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Thomas Lockhart 2001-12-27 21:28:53 Re: Implicit cast of literal in SQL statements
Previous Message Scott Royston 2001-12-27 20:37:14 Implicit cast of literal in SQL statements

Browse pgsql-hackers by date

  From Date Subject
Next Message Thomas Lockhart 2001-12-27 21:28:53 Re: Implicit cast of literal in SQL statements
Previous Message Pavlo Baron 2001-12-27 21:09:14 TODO question