question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Help needed in adding the "timestampz" alias for "timestamp with time zone"

See original GitHub issue

Hey @oguimbal šŸ‘‹ Thanks for making this and the pg-mem repos- they’re awesome!

I’m working on adding pg-mem to a personal project and had a few migrations that used timestampz rather than the more verbose timestamp with time zone. I figured this was a good opportunity to give back to the project and add support for timestampz, but I’m afraid I’m a bit lost while trying to implement the type alias.

I’m not sure if I’m going in the right direction, but these are changes I’ve got so far

diff --git a/src/syntax/base.ne b/src/syntax/base.ne
index 486eb66..e504c04 100644
--- a/src/syntax/base.ne
+++ b/src/syntax/base.ne
@@ -147,6 +147,7 @@ kw_cache -> %word {% notReservedKw('cache')  %}
 kw_cascade -> %word {% notReservedKw('cascade')  %}
 kw_no -> %word {% notReservedKw('no')  %}
 kw_timestamp -> %word {% notReservedKw('timestamp')  %}
+kw_timestampz -> %word {% notReservedKw('timestampz')  %}
 kw_cycle -> %word {% notReservedKw('cycle')  %}
 kw_function -> %word {% notReservedKw('function')  %}
 kw_returns -> %word {% notReservedKw('returns')  %}
diff --git a/src/syntax/create-table.spec.ts b/src/syntax/create-table.spec.ts
index 9b28fe5..ad50d5c 100644
--- a/src/syntax/create-table.spec.ts
+++ b/src/syntax/create-table.spec.ts
@@ -165,6 +165,16 @@ describe('Create table', () => {
         }],
     });
 
+    checkCreateTable(['create table "test" (value timestampz)'], {
+        type: 'create table',
+        name: 'test',
+        columns: [{
+            kind: 'column',
+            name: 'value',
+            dataType: { name: 'timestamp with time zone' },
+        }],
+    });
+
     checkInvalid('create table"test"(value "timestamp" with time zone)');
     checkInvalid('create table"test"(value timestamp with "time" zone)');

With the keyword added, I’m seeing my new test failing, which makes sense considering some form of alias hasn’t been defined:

  1) Create table
       parses create table "test" (value timestampz):

      Parser has not returned the expected AST
      + expected - actual

       {
         "columns": [
           {
             "dataType": {
      -        "name": "timestampz"
      +        "name": "timestamp with time zone"
             }
             "kind": "column"
             "name": "value"
           }
      
      at Context.<anonymous> (src/syntax/spec-utils.ts:76:26)
      at processImmediate (internal/timers.js:461:21)

However I am admittedly lost now šŸ˜… I took a wild guess and added the word to the data_type_date definition, but this clearly isn’t quite right, since it results in the ā€œAmbiguous matchesā€ test failing, saying there are 2 available options.

diff --git a/src/syntax/base.ne b/src/syntax/base.ne
index 486eb66..e504c04 100644
--- a/src/syntax/base.ne
+++ b/src/syntax/base.ne
@@ -247,6 +248,7 @@ data_type_text
 #https://www.postgresql.org/docs/9.5/datatype-datetime.html
 data_type_date
      ->  (%word {% anyKw('timestamp', 'time') %}) (%kw_with | %word {% kw('without') %}) (%word {% kw('time') %}) (%word {% kw('zone') %})
+     | (%word {% kw('timestampz') %})
     # | word {% kw('date') %}
     # | word {% kw('interval') %}
     # | word {% kw('timestamp') %} 

I don’t have much experience with ASTs or with moo/nearley, but I think I can get this implemented with a little nudge in the right direction, so any help or info would be much appreciated. Thanks! šŸ™‚

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
oguimbalcommented, Feb 23, 2021

Fixed with pg-mem@1.7.7

1reaction
brahmlowercommented, Feb 22, 2021

wont change the fact that pg-mem is not timezone-aware

Oh right! I completely lost track of that part.

timestamp and timestamptz working the same is totally fine for my use case, but I could see that being an issue for other prospective users šŸ¤”

I’ll keep playing with the repo locally here- I’ll remove the type synonym and see how far I get in implementing support for time zones. I’m sure I’ll have more questions about all that, but I can ask those in another issue to avoid cluttering this one.

Read more comments on GitHub >

github_iconTop Results From Across the Web

PostgreSQL: Aliases for "timestamp with time zone" and "at ...
Now I need to compare them with respect to time zone. I can do it like: select my_timestamp::timestamp with time zone at timeĀ ......
Read more >
Timestamps, Time Zones, Time Ranges, and Date Formats
Learn how Sumo Logic manages timestamps, time zones, time ranges, and dates, and the configuration options that are available.
Read more >
TIMESTAMP / TIMESTAMPTZ | CockroachDB Docs
TIMESTAMPTZ converts TIMESTAMP values from UTC to the client's session time zone (unless another time zone is specified for the value).
Read more >
Date & Time Data Types - Snowflake Documentation
Aliases for TIMESTAMP_NTZ: TIMESTAMPNTZ. TIMESTAMP WITHOUT TIME ZONE. TIMESTAMP_TZ. TIMESTAMP_TZ internally stores UTC time together with an associated timeĀ ...
Read more >
PostgreSQL Timestamps and Timezones: What You Need to ...
When a timestamptz value is set, the system computes the local time in UTC to which this corresponds and records this, using the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found