oguimbal / pg-mem

An in memory postgres DB instance for your unit tests
MIT License
1.95k stars 94 forks source link

Comparing timestamps: operator does not exist #312

Closed fabrice-toussaint closed 1 year ago

fabrice-toussaint commented 1 year ago

Describe the bug

Hi all,

I am using pg-mem for testing purposes and I am running into the following problem. When I want to subtract timestamps it throws the following error:

QueryFailedError: operator does not exist: timestamp without time zone - timestamp without time zone.

Is this normal behaviour, if so, how do I work around it? Thanks in advance!

To Reproduce

select now() - now()

pg-mem version

I am using version 2.6.12.

oguimbal commented 1 year ago

Hi,

pg-mem only supports a few operators. You can easily declare others like this:


import { Interval } from 'pgsql-ast-parser';

        db.public.registerOperator({
            operator: '-',
            left: DataType.timestamp,
            right: DataType.timestamp,
            returns: DataType.interval,
            implementation: (a, b): Interval => {
                // implement here the difference of two dates, returned as `Interval` type
               //    (which is the internal type in which intervals are stored)
            },
        });
oguimbal commented 1 year ago

ps: this is a general-enough operator... it could be integrated in the pg-mem codebase.

If you do implement it, dont hesitate to copy/paste it here, or create a pull request binary-operators.ts ^^