Re: DOMAINs and CASTs - Mailing list pgsql-hackers

From Robert Haas
Subject Re: DOMAINs and CASTs
Date
Msg-id BANLkTinhRpX24WAah+HOUHZQEKATn93w_A@mail.gmail.com
Whole thread Raw
In response to Re: DOMAINs and CASTs  (Jaime Casanova <jaime@2ndquadrant.com>)
Responses Re: DOMAINs and CASTs  (Jaime Casanova <jaime@2ndquadrant.com>)
List pgsql-hackers
On Sun, May 15, 2011 at 3:26 AM, Jaime Casanova <jaime@2ndquadrant.com> wrote:
> Obviously it should run the cast from timestamp to int, why it will
> run a cast from a domain?

So let's think about some harder scenarios.

Given two types T1 and T2, and two domains D1 over T1 and D2 over T2,
and a cast from a value of type D1 to type D2, then:

(1) If there is an implicit cast from D1 to D2 and an implicit cast
from T1 to T2, then presumably we should use the cast from D1 to D2,
since it's more specific.  Or if none of the available casts involve
domains, but there is a cast of some sort from T1 to T2, then it seems
clear to use that one.  But what if we instead have a cast from D1 to
T2 and a cast from T1 to D2?  Which one should we prefer?  Why?

(2) What happens if there are casts T1 -> T2 and D1 -> D2, but the D1
-> D2 cast is explicit, while the T1 -> T2 cast is on-assignment?
Should we use the D1->D2 cast when the context is explicit and the
T1->T2 when the context is on-assignment?  That seems confusing.
Alternatively, we could decide that the on-assignment cast always
beats the explicit cast, even in an explicit-cast context.  But that
amounts to ignoring the D1->D2 cast altogether - is that what we want?

(3) What happens if one or both of T1 or T2 are themselves domains
over some other types T3 and T4, respectively?  Now there are nine
possible pairings of types (any of D1, T1, T3 with any of D2, T2, T4)
and in each case the available cast could have one of three contexts.
How do we decide which cast wins?  Also, we might now need 9 probes
into pg_cast to find all the relevant casts, whereas the current code
needs just one - even in the previous scenario, we might need 4 probes
instead of one.  That'll be slower - is it worth it?  What's the
performance penalty in an artificially constructed worst case?

(4) What happens if T1 is a domain over T2?  Then we have another
option - cast D1 to D2 by smashing it to its base type twice (first to
T1, then to T2), and the re-applying any domain constraint on D2.  Is
that preferable to applying a cast from D1 to T1 and then casting to
D2?  Is it preferable to applying a cast from D1 to T2 and then
re-applying the domain constraint on D2?  Is it preferable to a cast
directly from D1 to D2?  And in each case, does the context of the
cast matter?

I'm not throwing these questions out to be flip or to make life
difficult - I'm just saying they need to be thought about, and the
answers don't seem obvious (to me).

-- 
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company


pgsql-hackers by date:

Previous
From: Josh Berkus
Date:
Subject: Re: performance-test farm
Next
From: Cédric Villemain
Date:
Subject: Re: patch for new feature: Buffer Cache Hibernation