Re: Adding time to DATE type

Started by Bruce Momjianover 25 years ago11 messages
#1Bruce Momjian
pgman@candle.pha.pa.us

Was this resolved?

Bruce Momjian <pgman@candle.pha.pa.us> writes:

test=> SELECT date('1/1/1992') + timespan('1 year');
ERROR: No such function 'timespan' with the specified attributes

This works:

SELECT date('1/1/1992') + '1 year'::timespan;

The function parsing code has a rather half-baked attempt to interpret
function calls that match type names as casts. IIRC, it only works
when the cast is between binary-compatible types. We should probably
either rip that out or make it fully equivalent to a typecast.
If the latter, it would have to be tried *after* failing to find a
matching ordinary function --- I think it's tried first at the moment,
which is pretty bogus.

A more restricted possibility that would cover this particular example
is to treat a function call as a typecast if (a) the function name
matches a type name *and* (b) the argument is of type UNKNOWN (ie,
it is a string literal of as-yet-undetermined type).

I'm starting to get uncomfortable with the amount of syntax and
semantics rejiggering we're doing in beta phase... so I'd not recommend
trying to implement the first option now. If people like the more
restricted fix, maybe that would be reasonable to do now.

I notice that although 6.5 doesn't take the query either, it gives
a different and perhaps more appropriate error message:

play=> SELECT date('1/1/1992') + timespan('1 year');
ERROR: Function 'timespan(unknown)' does not exist
Unable to identify a function which satisfies the given argument types
You will have to retype your query using explicit typecasts

I thought I'd got rid of the nonspecific error messages for function/
operator lookup failures, but this case seems to have got worse instead
of better. Drat. Will look into that.

regards, tom lane

-- 
  Bruce Momjian                        |  http://www.op.net/~candle
  pgman@candle.pha.pa.us               |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026
#2Bruce Momjian
pgman@candle.pha.pa.us
In reply to: Bruce Momjian (#1)

Can someone give me a TODO summary for this issue?

[ Charset ISO-8859-1 unsupported, converting... ]

On Fri, 17 Mar 2000, Bruce Momjian wrote:

test=> SELECT date('1/1/1992') + timespan('1 year');

If I may point something out here, the correct syntax for this in SQL92 is

SELECT DATE '1/1/1992' + INTERVAL '1 year'

(Ignoring the fact that neither the date nor the interval strings have the
correct format.)

This converts to a cast in PostgreSQL, which is fine, but the standard
makes a semantic distinction:

CAST('2000-02-29' AS DATE)

converts a character literal to date

DATE '2000-02-29'

*is* a date literal. Furthermore, just

'2000-02-29'

is not a date literal.

I've been doing some lobbying to get rid of the "unknown" type because SQL
is perfectly clear about what "quote-stuff-quote" means (character type)
and in absence of any evidence to the contrary (such as a function only
taking date arguments, inserting it into a date field) it should be
treated as such. That will get rid of such embarrassments as

SELECT 'a' LIKE 'a' -- try it

Tom believes that this will create a pain for the odd data type crowd but
I don't think that this is so (or at least has to be so) whereas the
current behavior creates a pain for the normal data type crowd.

Just my ideas.

--
Peter Eisentraut Sernanders v?g 10:115
peter_e@gmx.net 75262 Uppsala
http://yi.org/peter-e/ Sweden

-- 
  Bruce Momjian                        |  http://www.op.net/~candle
  pgman@candle.pha.pa.us               |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026
#3Tom Lane
tgl@sss.pgh.pa.us
In reply to: Bruce Momjian (#1)

Bruce Momjian <pgman@candle.pha.pa.us> writes:

Was this resolved?

Yeah, I think it's working fairly well now. The current code first
tries to look up an actual function matching the name + arguments,
and only if that fails does it try to interpret the construct as a
binary-compatible type coercion.

regards, tom lane

Show quoted text

Bruce Momjian <pgman@candle.pha.pa.us> writes:

test=> SELECT date('1/1/1992') + timespan('1 year');
ERROR: No such function 'timespan' with the specified attributes

This works:

SELECT date('1/1/1992') + '1 year'::timespan;

The function parsing code has a rather half-baked attempt to interpret
function calls that match type names as casts. IIRC, it only works
when the cast is between binary-compatible types. We should probably
either rip that out or make it fully equivalent to a typecast.
If the latter, it would have to be tried *after* failing to find a
matching ordinary function --- I think it's tried first at the moment,
which is pretty bogus.

A more restricted possibility that would cover this particular example
is to treat a function call as a typecast if (a) the function name
matches a type name *and* (b) the argument is of type UNKNOWN (ie,
it is a string literal of as-yet-undetermined type).

I'm starting to get uncomfortable with the amount of syntax and
semantics rejiggering we're doing in beta phase... so I'd not recommend
trying to implement the first option now. If people like the more
restricted fix, maybe that would be reasonable to do now.

I notice that although 6.5 doesn't take the query either, it gives
a different and perhaps more appropriate error message:

play=> SELECT date('1/1/1992') + timespan('1 year');
ERROR: Function 'timespan(unknown)' does not exist
Unable to identify a function which satisfies the given argument types
You will have to retype your query using explicit typecasts

I thought I'd got rid of the nonspecific error messages for function/
operator lookup failures, but this case seems to have got worse instead
of better. Drat. Will look into that.

regards, tom lane

-- 
Bruce Momjian                        |  http://www.op.net/~candle
pgman@candle.pha.pa.us               |  (610) 853-3000
+  If your life is a hard drive,     |  830 Blythe Avenue
+  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026
#4Peter Eisentraut
peter_e@gmx.net
In reply to: Bruce Momjian (#2)

Bruce Momjian writes:

Can someone give me a TODO summary for this issue?

* make 'text' constants default to text type (not unknown)

(I think not everyone's completely convinced on this issue, but I don't
recall anyone being firmly opposed to it.)

* add SQL interval syntax

On Fri, 17 Mar 2000, Bruce Momjian wrote:

test=> SELECT date('1/1/1992') + timespan('1 year');

If I may point something out here, the correct syntax for this in SQL92 is

SELECT DATE '1/1/1992' + INTERVAL '1 year'

(Ignoring the fact that neither the date nor the interval strings have the
correct format.)

This converts to a cast in PostgreSQL, which is fine, but the standard
makes a semantic distinction:

CAST('2000-02-29' AS DATE)

converts a character literal to date

DATE '2000-02-29'

*is* a date literal. Furthermore, just

'2000-02-29'

is not a date literal.

I've been doing some lobbying to get rid of the "unknown" type because SQL
is perfectly clear about what "quote-stuff-quote" means (character type)
and in absence of any evidence to the contrary (such as a function only
taking date arguments, inserting it into a date field) it should be
treated as such. That will get rid of such embarrassments as

SELECT 'a' LIKE 'a' -- try it

Tom believes that this will create a pain for the odd data type crowd but
I don't think that this is so (or at least has to be so) whereas the
current behavior creates a pain for the normal data type crowd.

Just my ideas.

--
Peter Eisentraut Sernanders v?g 10:115
peter_e@gmx.net 75262 Uppsala
http://yi.org/peter-e/ Sweden

--
Peter Eisentraut Sernanders v�g 10:115
peter_e@gmx.net 75262 Uppsala
http://yi.org/peter-e/ Sweden

#5Bruce Momjian
pgman@candle.pha.pa.us
In reply to: Peter Eisentraut (#4)

[ Charset ISO-8859-1 unsupported, converting... ]

Bruce Momjian writes:

Can someone give me a TODO summary for this issue?

* make 'text' constants default to text type (not unknown)

(I think not everyone's completely convinced on this issue, but I don't
recall anyone being firmly opposed to it.)

I don't know but I know it came up in the last month. Something about
character strings not being considered TEXT, and they had to be cast to
TEXT to be used.

* add SQL interval syntax

These must be your own items. I don't see them on the main TODO list.

-- 
  Bruce Momjian                        |  http://www.op.net/~candle
  pgman@candle.pha.pa.us               |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026
#6Peter Eisentraut
e99re41@DoCS.UU.SE
In reply to: Bruce Momjian (#5)

On Sun, 11 Jun 2000, Bruce Momjian wrote:

Can someone give me a TODO summary for this issue?

* make 'text' constants default to text type (not unknown)

(I think not everyone's completely convinced on this issue, but I don't
recall anyone being firmly opposed to it.)

I don't know but I know it came up in the last month. Something about
character strings not being considered TEXT, and they had to be cast to
TEXT to be used.

* add SQL interval syntax

These must be your own items. I don't see them on the main TODO list.

?? You asked for a TODO summary, and these are the things that would need
TO be DOne in order to address the issue originally at hand.

--
Peter Eisentraut Sernanders v�g 10:115
peter_e@gmx.net 75262 Uppsala
http://yi.org/peter-e/ Sweden

#7Tom Lane
tgl@sss.pgh.pa.us
In reply to: Peter Eisentraut (#4)

Peter Eisentraut <peter_e@gmx.net> writes:

Bruce Momjian writes:

Can someone give me a TODO summary for this issue?

* make 'text' constants default to text type (not unknown)

(I think not everyone's completely convinced on this issue, but I don't
recall anyone being firmly opposed to it.)

It would be a mistake to eliminate the distinction between unknown and
text. See for example my just-posted response to John Cochran on
pgsql-general about why 'BOULEVARD'::text behaves differently from
'BOULEVARD'::char. If string literals are immediately assigned type
text then we will have serious problems with char(n) fields.

I think it's fine to assign string literals a type of 'unknown'
initially. What we need to do is add a phase of type resolution that
considers treating them as text, but only after the existing logic fails
to deduce a type.

(BTW it might be better to treat string literals as defaulting to char(n)
instead of text, allowing the normal promotion rules to replace char(n)
with text if necessary. Not sure if that would make things more or less
confusing for operations that intermix fixed- and variable-width char
types.)

regards, tom lane

#8Peter Eisentraut
e99re41@DoCS.UU.SE
In reply to: Tom Lane (#7)

On Mon, 12 Jun 2000, Tom Lane wrote:

* make 'text' constants default to text type (not unknown)

I think it's fine to assign string literals a type of 'unknown'
initially. What we need to do is add a phase of type resolution that
considers treating them as text, but only after the existing logic fails
to deduce a type.

Hence "default to"

--
Peter Eisentraut Sernanders v�g 10:115
peter_e@gmx.net 75262 Uppsala
http://yi.org/peter-e/ Sweden

#9Bruce Momjian
pgman@candle.pha.pa.us
In reply to: Peter Eisentraut (#6)

Oh, OK.

[ Charset ISO-8859-1 unsupported, converting... ]

On Sun, 11 Jun 2000, Bruce Momjian wrote:

Can someone give me a TODO summary for this issue?

* make 'text' constants default to text type (not unknown)

(I think not everyone's completely convinced on this issue, but I don't
recall anyone being firmly opposed to it.)

I don't know but I know it came up in the last month. Something about
character strings not being considered TEXT, and they had to be cast to
TEXT to be used.

* add SQL interval syntax

These must be your own items. I don't see them on the main TODO list.

?? You asked for a TODO summary, and these are the things that would need
TO be DOne in order to address the issue originally at hand.

--
Peter Eisentraut Sernanders v?g 10:115
peter_e@gmx.net 75262 Uppsala
http://yi.org/peter-e/ Sweden

-- 
  Bruce Momjian                        |  http://www.op.net/~candle
  pgman@candle.pha.pa.us               |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026
#10Bruce Momjian
pgman@candle.pha.pa.us
In reply to: Tom Lane (#7)

Is this something worth addressing?

Peter Eisentraut <peter_e@gmx.net> writes:

Bruce Momjian writes:

Can someone give me a TODO summary for this issue?

* make 'text' constants default to text type (not unknown)

(I think not everyone's completely convinced on this issue, but I don't
recall anyone being firmly opposed to it.)

It would be a mistake to eliminate the distinction between unknown and
text. See for example my just-posted response to John Cochran on
pgsql-general about why 'BOULEVARD'::text behaves differently from
'BOULEVARD'::char. If string literals are immediately assigned type
text then we will have serious problems with char(n) fields.

I think it's fine to assign string literals a type of 'unknown'
initially. What we need to do is add a phase of type resolution that
considers treating them as text, but only after the existing logic fails
to deduce a type.

(BTW it might be better to treat string literals as defaulting to char(n)
instead of text, allowing the normal promotion rules to replace char(n)
with text if necessary. Not sure if that would make things more or less
confusing for operations that intermix fixed- and variable-width char
types.)

regards, tom lane

-- 
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026
#11Tom Lane
tgl@sss.pgh.pa.us
In reply to: Bruce Momjian (#10)

Bruce Momjian <pgman@candle.pha.pa.us> writes:

Is this something worth addressing?

Yes, but not when we're already overdue for beta. We've been around
on the question of type promotion rules several times, and no one has
yet put forward a solution that everyone else liked. I don't expect
to see a usable solution both proposed and implemented in the next
three weeks...

regards, tom lane