pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Difference between CAST(v AS t) and v::t
3+ messages / 2 participants
[nested] [flat]

* Difference between CAST(v AS t) and v::t
@ 2017-11-11 11:29  Lele Gaifax <lele@metapensiero.it>
  0 siblings, 1 reply; 3+ messages in thread

From: Lele Gaifax @ 2017-11-11 11:29 UTC (permalink / raw)
  To: pgsql-general

Hi all,

while writing test cases for my SQL pretty printer tool[1], I found what seems
a discrepancy in the "Type Casts" documentation[2]: it states that the two
syntaxes are equivalent, but while

  CREATE TABLE contracts (
    ...
    company_id uuid NOT NULL,
    validity daterange NOT NULL,
    
    EXCLUDE USING gist (CAST(company_id AS text) WITH =, validity WITH &&)
  )

works, the following

  CREATE TABLE contracts (
    ...
    company_id uuid NOT NULL,
    validity daterange NOT NULL,
    
    EXCLUDE USING gist (company_id::text WITH =, validity WITH &&)
  )

is rejected with the message "syntax error at or near "::"".

Am I misreading the documentation, or is it missing some special case?

Thanks in advance for any explanation,
ciao, lele.

[1] https://github.com/lelit/pg_query
[2] https://www.postgresql.org/docs/10/static/sql-expressions.html#SQL-SYNTAX-TYPE-CASTS
-- 
nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri
real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia.
lele@metapensiero.it  |                 -- Fortunato Depero, 1929.



-- 
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Difference between CAST(v AS t) and v::t
@ 2017-11-11 16:43  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Lele Gaifax <lele@metapensiero.it>
  0 siblings, 1 reply; 3+ messages in thread

From: Tom Lane @ 2017-11-11 16:43 UTC (permalink / raw)
  To: Lele Gaifax <lele@metapensiero.it>; +Cc: pgsql-general

Lele Gaifax <lele@metapensiero.it> writes:
> while writing test cases for my SQL pretty printer tool[1], I found what seems
> a discrepancy in the "Type Casts" documentation[2]: it states that the two
> syntaxes are equivalent, but while

They are functionally equivalent ...

>     EXCLUDE USING gist (CAST(company_id AS text) WITH =, validity WITH &&)
> works, the following
>     EXCLUDE USING gist (company_id::text WITH =, validity WITH &&)
> is rejected with the message "syntax error at or near "::"".

... but expression-index syntax has the restriction that you need
parentheses around an expression unless it is, or at least looks like,
a function call.  CAST() looks enough like a function call for this
purpose, v::t does not.

I think there is relevant documentation for this near CREATE INDEX;
it doesn't seem like the province of the typecast docs to explain
the weirdnesses of index syntax.

			regards, tom lane


-- 
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Difference between CAST(v AS t) and v::t
@ 2017-11-12 07:52  Lele Gaifax <lele@metapensiero.it>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 3+ messages in thread

From: Lele Gaifax @ 2017-11-12 07:52 UTC (permalink / raw)
  To: pgsql-general

Tom Lane <tgl@sss.pgh.pa.us> writes:

> ... but expression-index syntax has the restriction that you need
> parentheses around an expression unless it is, or at least looks like,
> a function call.  CAST() looks enough like a function call for this
> purpose, v::t does not.
>
> I think there is relevant documentation for this near CREATE INDEX;
> it doesn't seem like the province of the typecast docs to explain
> the weirdnesses of index syntax.

Thank you Tom, I really missed the explanation in CREATE INDEX doc entry!

ciao, lele.
-- 
nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri
real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia.
lele@metapensiero.it  |                 -- Fortunato Depero, 1929.



-- 
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general



^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2017-11-12 07:52 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2017-11-11 11:29 Difference between CAST(v AS t) and v::t Lele Gaifax <lele@metapensiero.it>
2017-11-11 16:43 ` Tom Lane <tgl@sss.pgh.pa.us>
2017-11-12 07:52   ` Lele Gaifax <lele@metapensiero.it>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox