pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedIs it possible to use keywords (date units) in a function definition?
7+ messages / 4 participants
[nested] [flat]
* Is it possible to use keywords (date units) in a function definition?
@ 2020-06-08 21:21 Alistair Johnson <aewj@mit.edu>
0 siblings, 2 replies; 7+ messages in thread
From: Alistair Johnson @ 2020-06-08 21:21 UTC (permalink / raw)
To: pgsql-general
Hello,
I recently tried to write a wrapper function to calculate the difference
between two dates, mainly as a convenience. I'd essentially be emulating
EXTRACT(<dateunit> FROM date1 - date2), in various ways. I got a bit stuck
on allowing specification of the <dateunit>: is this possible in function
definitions? I'd like to be able to write something along the lines of:
CREATE OR REPLACE FUNCTION DATETIME_DIFF(end TIMESTAMP(3), start
TIMESTAMP(3), datepart UNIT) RETURNS DOUBLE PRECISION AS $$
BEGIN
RETURN EXTRACT(datepart FROM end - start);
END; $$
LANGUAGE PLPGSQL;
One option would be to treat datepart as a string, but this doesn't work
for my use case. (Background: I'm trying to refactor a bunch of SQL scripts
to work on Google BigQuery and PostgreSQL by writing PostgreSQL functions
to emulate BigQuery functions. Unfortunately BigQuery does not recognize
the third argument if it is a string (i.e. 'HOUR' does not work but HOUR
does)).
Any ideas? Is this even possible?
-Alistair
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Is it possible to use keywords (date units) in a function definition?
@ 2020-06-08 22:10 David G. Johnston <david.g.johnston@gmail.com>
parent: Alistair Johnson <aewj@mit.edu>
1 sibling, 1 reply; 7+ messages in thread
From: David G. Johnston @ 2020-06-08 22:10 UTC (permalink / raw)
To: Alistair Johnson <aewj@mit.edu>; +Cc: pgsql-general
On Mon, Jun 8, 2020 at 2:57 PM Alistair Johnson <aewj@mit.edu> wrote:
>
> RETURN EXTRACT(datepart FROM end - start);
> Any ideas? Is this even possible?
>
Use the "date_part" function.
David J.
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Is it possible to use keywords (date units) in a function definition?
@ 2020-06-08 22:40 Adrian Klaver <adrian.klaver@aklaver.com>
parent: David G. Johnston <david.g.johnston@gmail.com>
0 siblings, 0 replies; 7+ messages in thread
From: Adrian Klaver @ 2020-06-08 22:40 UTC (permalink / raw)
To: David G. Johnston <david.g.johnston@gmail.com>; Alistair Johnson <aewj@mit.edu>; +Cc: pgsql-general
On 6/8/20 3:10 PM, David G. Johnston wrote:
> On Mon, Jun 8, 2020 at 2:57 PM Alistair Johnson <aewj@mit.edu
> <mailto:aewj@mit.edu>> wrote:
>
>
> RETURN EXTRACT(datepart FROM end - start);
> Any ideas? Is this even possible?
>
>
> Use the "date_part" function.
That would work on the Postgres side, but the OP also wants this to run
on BigQuery and unfortunately it won't work there.
>
> David J.
--
Adrian Klaver
adrian.klaver@aklaver.com
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Is it possible to use keywords (date units) in a function definition?
@ 2020-06-08 23:09 David G. Johnston <david.g.johnston@gmail.com>
parent: Alistair Johnson <aewj@mit.edu>
1 sibling, 1 reply; 7+ messages in thread
From: David G. Johnston @ 2020-06-08 23:09 UTC (permalink / raw)
To: Alistair Johnson <aewj@mit.edu>; +Cc: pgsql-general
On Mon, Jun 8, 2020 at 2:57 PM Alistair Johnson <aewj@mit.edu> wrote:
> Hello,
>
> I recently tried to write a wrapper function to calculate the difference
> between two dates, mainly as a convenience. I'd essentially be emulating
> EXTRACT(<dateunit> FROM date1 - date2), in various ways. I got a bit stuck
> on allowing specification of the <dateunit>: is this possible in function
> definitions? I'd like to be able to write something along the lines of:
>
> CREATE OR REPLACE FUNCTION DATETIME_DIFF(end TIMESTAMP(3), start
> TIMESTAMP(3), datepart UNIT) RETURNS DOUBLE PRECISION AS $$
> BEGIN
> RETURN EXTRACT(datepart FROM end - start);
> END; $$
> LANGUAGE PLPGSQL;
>
> One option would be to treat datepart as a string, but this doesn't work
> for my use case. (Background: I'm trying to refactor a bunch of SQL scripts
> to work on Google BigQuery and PostgreSQL by writing PostgreSQL functions
> to emulate BigQuery functions. Unfortunately BigQuery does not recognize
> the third argument if it is a string (i.e. 'HOUR' does not work but HOUR
> does)).
>
> Any ideas? Is this even possible?
>
>
I think you need to be more specific as to what "this" means.
Looking again after Andrian's comment are you trying to write, in the
script file:
datetime_diff('start time as string'::timestamp, 'end time as
string'::timestamp, HOUR)
and get PostgreSQL to recognize the value HOUR as a custom type
value without single quotes surrounding it
If that is the question the answer is no. The only type literals that can
be written without single quotes are numbers.
The parsing of SQL can handle some standard mandated non-quoted constants
but they are basically keywords, not values.
David J.
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Is it possible to use keywords (date units) in a function definition?
@ 2020-06-08 23:22 Alistair Johnson <aewj@mit.edu>
parent: David G. Johnston <david.g.johnston@gmail.com>
0 siblings, 0 replies; 7+ messages in thread
From: Alistair Johnson @ 2020-06-08 23:22 UTC (permalink / raw)
To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: pgsql-general
>
> I think you need to be more specific as to what "this" means.
>
> Looking again after Andrian's comment are you trying to write, in the
> script file:
>
> datetime_diff('start time as string'::timestamp, 'end time as
> string'::timestamp, HOUR)
>
> and get PostgreSQL to recognize the value HOUR as a custom type
> value without single quotes surrounding it
>
> If that is the question the answer is no. The only type literals that can
> be written without single quotes are numbers.
>
> The parsing of SQL can handle some standard mandated non-quoted constants
> but they are basically keywords, not values.
>
> David J.
>
Thanks David - that was exactly my question - and it's nice to have a clear
answer even if it wrinkles my plans a bit!
-Alistair
^ permalink raw reply [nested|flat] 7+ messages in thread
* Is it possible to use keywords (date units) in a function definition?
@ 2020-06-09 00:06 Martin Gainty <mgainty@hotmail.com>
0 siblings, 1 reply; 7+ messages in thread
From: Martin Gainty @ 2020-06-09 00:06 UTC (permalink / raw)
To: pgsql-general
Hello,
I recently tried to write a wrapper function to calculate the difference between two dates, mainly as a convenience. I'd essentially be emulating EXTRACT(<dateunit> FROM date1 - date2), in various ways. I got a bit stuck on allowing specification of the <dateunit>: is this possible in function definitions? I'd like to be able to write something along the lines of:
CREATE OR REPLACE FUNCTION DATETIME_DIFF(end TIMESTAMP(3), start TIMESTAMP(3), datepart UNIT) RETURNS DOUBLE PRECISION AS $$
BEGIN
RETURN EXTRACT(datepart FROM end - start);
END; $$
LANGUAGE PLPGSQL;
One option would be to treat datepart as a string, but this doesn't work for my use case. (Background: I'm trying to refactor a bunch of SQL scripts to work on Google BigQuery and PostgreSQL by writing PostgreSQL functions to emulate BigQuery functions. Unfortunately BigQuery does not recognize the third argument if it is a string (i.e. 'HOUR' does not work but HOUR does)).
Any ideas? Is this even possible?
-Alistair
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Is it possible to use keywords (date units) in a function definition?
@ 2020-06-09 00:16 David G. Johnston <david.g.johnston@gmail.com>
parent: Martin Gainty <mgainty@hotmail.com>
0 siblings, 0 replies; 7+ messages in thread
From: David G. Johnston @ 2020-06-09 00:16 UTC (permalink / raw)
To: Martin Gainty <mgainty@hotmail.com>; +Cc: pgsql-general
On Mon, Jun 8, 2020 at 5:06 PM Martin Gainty <mgainty@hotmail.com> wrote:
> CREATE OR REPLACE FUNCTION DATETIME_DIFF(end TIMESTAMP(3), start
> TIMESTAMP(3), datepart UNIT) RETURNS DOUBLE PRECISION AS $$
>
Duplicate email from account (same sender) - already answered on the
original/correct thread.
David J.
^ permalink raw reply [nested|flat] 7+ messages in thread
end of thread, other threads:[~2020-06-09 00:16 UTC | newest]
Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2020-06-08 21:21 Is it possible to use keywords (date units) in a function definition? Alistair Johnson <aewj@mit.edu>
2020-06-08 22:10 ` David G. Johnston <david.g.johnston@gmail.com>
2020-06-08 22:40 ` Adrian Klaver <adrian.klaver@aklaver.com>
2020-06-08 23:09 ` David G. Johnston <david.g.johnston@gmail.com>
2020-06-08 23:22 ` Alistair Johnson <aewj@mit.edu>
2020-06-09 00:06 Is it possible to use keywords (date units) in a function definition? Martin Gainty <mgainty@hotmail.com>
2020-06-09 00:16 ` David G. Johnston <david.g.johnston@gmail.com>
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