pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
- operator overloading not giving expected result
7+ messages / 6 participants
[nested] [flat]

* - operator overloading not giving expected result
@ 2022-07-05 08:22  Rajesh S <rajesh.s@fincuro.com>
  0 siblings, 4 replies; 7+ messages in thread

From: Rajesh S @ 2022-07-05 08:22 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

Hi,

We are migrating our database from Oracle to Postgresql.  In oracle we 
have used this syntax "SELECT ('1999-12-30'::DATE) - 
('1999-12-11'::DATE)" to get difference between two dates as a integer 
output (ex: 19).  But in Postgres the same query returns result as "19 
days".  Because of this we are getting errors while assigning this query 
output to a numeric variable saying "ERROR: invalid input syntax for 
type numeric: "1825 days"" and "ERROR: operator does not exist: interval 
+ integer".  To avoid changing the application code in many places to 
extract the number of days alone, we tried operator overloading concept 
as below.

CREATE OR REPLACE FUNCTION public.dt_minus_dt(
     dt1 timestamp without time zone,
     dt2 timestamp without time zone)
     RETURNS integer
     LANGUAGE 'edbspl'
     COST 100
     VOLATILE SECURITY DEFINER PARALLEL UNSAFE
AS $BODY$
     days INTEGER;
BEGIN
     SELECT DATE_PART('day', dt1::timestamp - dt2::timestamp)::integer 
INTO days;
     RETURN days;
END
$BODY$;

CREATE OPERATOR public.- (
     FUNCTION = public.dt_minus_dt,
     LEFTARG = timestamp without time zone,
     RIGHTARG = timestamp without time zone
);

When we execute "SELECT ('1999-12-30'::DATE) - ('1999-12-11'::DATE)", we 
are still getting "19 days" as result and not "19" as we expect.  The 
above same function works as expected for the operator + or ===.

CREATE OPERATOR public.+ (
     FUNCTION = public.dt_minus_dt,
     LEFTARG = timestamp without time zone,
     RIGHTARG = timestamp without time zone
);

SELECT ('1999-12-30'::DATE) + ('1999-12-11'::DATE)

CREATE OPERATOR public.=== (
     FUNCTION = public.dt_minus_dt,
     LEFTARG = timestamp without time zone,
     RIGHTARG = timestamp without time zone
);

SELECT ('1999-12-30'::DATE) === ('1999-12-11'::DATE)


I really appreciate anyone's help in resolving this case.  Thanks in 
advance.


Rajesh S

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

* Re: - operator overloading not giving expected result
@ 2022-07-08 10:59  Peter J. Holzer <hjp-pgsql@hjp.at>
  parent: Rajesh S <rajesh.s@fincuro.com>
  3 siblings, 0 replies; 7+ messages in thread

From: Peter J. Holzer @ 2022-07-08 10:59 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

On 2022-07-05 13:52:32 +0530, Rajesh S wrote:
> We are migrating our database from Oracle to Postgresql.  In oracle we have
> used this syntax "SELECT ('1999-12-30'::DATE) - ('1999-12-11'::DATE)" to get
> difference between two dates as a integer output (ex: 19).  But in Postgres the
> same query returns result as "19 days".

Which PostgreSQL version is this?

I get 19 with PostgreSQL 11.16 and 14.0.

        hp

-- 
   _  | Peter J. Holzer    | Story must make more sense than reality.
|_|_) |                    |
| |   | hjp@hjp.at         |    -- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |       challenge!"

Attachments:

  signature.asc (application/pgp-signature, 832B) download

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

* Re: - operator overloading not giving expected result
@ 2022-07-08 11:38  Francisco Olarte <folarte@peoplecall.com>
  parent: Rajesh S <rajesh.s@fincuro.com>
  3 siblings, 0 replies; 7+ messages in thread

From: Francisco Olarte @ 2022-07-08 11:38 UTC (permalink / raw)
  To: Rajesh S <rajesh.s@fincuro.com>; +Cc: pgsql-general@lists.postgresql.org

Hi Rajesh:

On Fri, 8 Jul 2022 at 12:36, Rajesh S <rajesh.s@fincuro.com> wrote:
> We are migrating our database from Oracle to Postgresql.  In oracle we have used this syntax "SELECT ('1999-12-30'::DATE) - ('1999-12-11'::DATE)" to get difference between two dates as a integer output (ex: 19).  But in Postgres the same query returns result as "19 days".  Because of this we are getting errors while assigning this query output to a numeric variable saying "ERROR: invalid input syntax for type numeric: "1825 days"" and "ERROR: operator does not exist: interval + integer".  To avoid changing the application code in many places to extract the number of days alone, we tried operator overloading concept as below.

Are you posting exact code? Because current doc states:

date - date → integer
Subtract dates, producing the number of days elapsed
date '2001-10-01' - date '2001-09-28' → 3

I remember it always working that way and 8.0 docs document it that
way too ( and I've used a lot of date arithmetic even in 7.x )

Maybe your EXACT code is not what you have pasted, but rather you
inserting dates in timestamp columns and then substracting said
columns? Because even your operations are defined in terms of
timestamptz, not dates.

Francisco Olarte.





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

* Re: - operator overloading not giving expected result
@ 2022-07-08 12:46  Christoph Moench-Tegeder <cmt@burggraben.net>
  parent: Rajesh S <rajesh.s@fincuro.com>
  3 siblings, 1 reply; 7+ messages in thread

From: Christoph Moench-Tegeder @ 2022-07-08 12:46 UTC (permalink / raw)
  To: Rajesh S <rajesh.s@fincuro.com>; +Cc: pgsql-general@lists.postgresql.org

## Rajesh S (rajesh.s@fincuro.com):

> We are migrating our database from Oracle to Postgresql.  In oracle we 
> have used this syntax "SELECT ('1999-12-30'::DATE) - 
> ('1999-12-11'::DATE)" to get difference between two dates as a integer 
> output (ex: 19).  But in Postgres the same query returns result as "19 
> days".

There's something fishy going on, as (date) - (date) returns integer
since a very long time (even the version 8.0 docs have that).
On the other hand, (timestamp) - (timestamp) gives an interval, so
first make sure you really got the data types right.

> CREATE OR REPLACE FUNCTION public.dt_minus_dt(
>      dt1 timestamp without time zone,
>      dt2 timestamp without time zone)

See? There's TIMESTAMP, not DATE.

>      LANGUAGE 'edbspl'

Is this really PostgreSQL or is it that fork - and if it's the fork,
does it behave the same way as stock PostgreSQL does? (I would be
surprised if that deviates in this place, but...).

>      SELECT DATE_PART('day', dt1::timestamp - dt2::timestamp)::integer 

And TIMESTAMP again.

Regards,
Christoph

-- 
Spare Space





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

* Re: - operator overloading not giving expected result
@ 2022-07-08 14:13  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Christoph Moench-Tegeder <cmt@burggraben.net>
  0 siblings, 0 replies; 7+ messages in thread

From: Tom Lane @ 2022-07-08 14:13 UTC (permalink / raw)
  To: Christoph Moench-Tegeder <cmt@burggraben.net>; +Cc: Rajesh S <rajesh.s@fincuro.com>; pgsql-general@lists.postgresql.org

Christoph Moench-Tegeder <cmt@burggraben.net> writes:
> ## Rajesh S (rajesh.s@fincuro.com):
>> We are migrating our database from Oracle to Postgresql.  In oracle we 
>> have used this syntax "SELECT ('1999-12-30'::DATE) - 
>> ('1999-12-11'::DATE)" to get difference between two dates as a integer 
>> output (ex: 19).  But in Postgres the same query returns result as "19 
>> days".

> There's something fishy going on, as (date) - (date) returns integer
> since a very long time (even the version 8.0 docs have that).
> On the other hand, (timestamp) - (timestamp) gives an interval, so
> first make sure you really got the data types right.

It's easy to demonstrate that that query does indeed return "19"
in stock Postgres.  Therefore, this is not stock Postgres.

>>     LANGUAGE 'edbspl'

> Is this really PostgreSQL or is it that fork - and if it's the fork,
> does it behave the same way as stock PostgreSQL does?

I'm no expert in this area, but my recollection is that Oracle's
type DATE is more nearly what we'd call TIMESTAMP (i.e., it includes
sub-day fields), and that EDB's fork follows Oracle's datatype naming.
So I'd not be at all surprised by this result if it's on EDB.

BTW, Oracle doesn't really accept "::" casting notation does it?
I always thought that was a pure Postgres-ism.

			regards, tom lane





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

* Re: - operator overloading not giving expected result
@ 2022-07-08 14:35  Ravi Krishna <s_ravikrishna@aol.com>
  parent: Rajesh S <rajesh.s@fincuro.com>
  3 siblings, 1 reply; 7+ messages in thread

From: Ravi Krishna @ 2022-07-08 14:35 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>; Rajesh S <rajesh.s@fincuro.com>

LANGUAGE 'edbspl'

This is the root cause of your issue.


You are not using PGSQL, but EDB version of it which is compatible with Oracle PL/SQL.





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

* Re: - operator overloading not giving expected result
@ 2022-07-11 11:13  Rajesh S <rajesh.s@fincuro.com>
  parent: Ravi Krishna <s_ravikrishna@aol.com>
  0 siblings, 0 replies; 7+ messages in thread

From: Rajesh S @ 2022-07-11 11:13 UTC (permalink / raw)
  To: Ravi Krishna <s_ravikrishna@aol.com>; pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

Hi All,

Thank you for your valuable comments and suggestions.  As it was pointed 
out we are using EDB Postgres and the language was mentioned as 
'edbspl'.  We made to work by setting the search_path variable as follows:

set search_path="$user", public, pg_catalog

And also changed the Operator overloading function as below. This gave 
us the expected results.  Thanks once again for all your comments.  
Consider this thread as closed.

CREATE OR REPLACE FUNCTION public.dt_minus_dt(dt1 DATE, dt2 DATE)
RETURNS INTEGER as
$$
     SELECT (to_char(dt1::date,'J'::varchar) - 
to_char(dt2::date,'J'::varchar))::integer
$$ LANGUAGE SQL IMMUTABLE STRICT PARALLEL SAFE COST 1;

CREATE OPERATOR public.- (
   PROCEDURE = dt_minus_dt,
   LEFTARG = DATE,
   RIGHTARG = DATE
);


Thanks,

Rajesh S

On 08-07-2022 08:05 pm, Ravi Krishna wrote:
> LANGUAGE 'edbspl'
>
> This is the root cause of your issue.
>
>
> You are not using PGSQL, but EDB version of it which is compatible with Oracle PL/SQL.

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


end of thread, other threads:[~2022-07-11 11:13 UTC | newest]

Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2022-07-05 08:22 - operator overloading not giving expected result Rajesh S <rajesh.s@fincuro.com>
2022-07-08 10:59 ` Peter J. Holzer <hjp-pgsql@hjp.at>
2022-07-08 11:38 ` Francisco Olarte <folarte@peoplecall.com>
2022-07-08 12:46 ` Christoph Moench-Tegeder <cmt@burggraben.net>
2022-07-08 14:13   ` Tom Lane <tgl@sss.pgh.pa.us>
2022-07-08 14:35 ` Ravi Krishna <s_ravikrishna@aol.com>
2022-07-11 11:13   ` Rajesh S <rajesh.s@fincuro.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