superset: Postgresql DB Engine Error: '>=' not supported between instances of 'datetime.timedelta' and 'int'

There seems to be a problem in retrieving and using any column that is of the format: TIMESTAMP WITH TIME ZONE or just TIMESTAMP

A relational table with a column of type TIMESTAMP WITH TIME ZONE would error when run a SELECT * ... or SELECT <column_with_timestamp_type> FROM...

The SQL editor errors even with SELECT now()

The error is:

DB engine Error

postgresql error: '>=' not supported between instances of 'datetime.timedelta' and 'int'

This may be triggered by:
Issue 1002 - The database returned an unexpected error.

Expected results

Would expect the column to return properly and be usable in exploration

Actual results & Screenshots

image

How to reproduce the bug

  1. Go to SQL Lab
  2. run the query: SELECT now()

Environment

my requirements.txt:

aiohttp==3.7.4.post0
alembic==1.6.5
amqp==2.6.1
apache-superset==1.2.0
apispec==3.3.2
async-timeout==3.0.1
attrs==21.2.0
Babel==2.9.1
backoff==1.11.1
billiard==3.6.4.0
bleach==3.3.1
Brotli==1.0.9
cachelib==0.1.1
celery==4.4.7
cffi==1.14.6
chardet==4.0.0
click==7.1.2
colorama==0.4.4
contextlib2==21.6.0
convertdate==2.3.2
cron-descriptor==1.2.24
croniter==1.0.15
cryptography==3.4.7
decorator==5.0.9
defusedxml==0.7.1
dnspython==2.1.0
email-validator==1.1.3
Flask==1.1.4
Flask-AppBuilder==3.3.1
Flask-Babel==1.0.0
Flask-Caching==1.10.1
Flask-Compress==1.10.1
Flask-JWT-Extended==3.25.1
Flask-Login==0.4.1
Flask-Migrate==3.0.1
Flask-OpenID==1.2.5
Flask-SQLAlchemy==2.5.1
flask-talisman==0.8.1
Flask-WTF==0.14.3
geographiclib==1.52
geopy==2.2.0
graphlib-backport==1.0.3
gunicorn==20.0.4
holidays==0.10.3
humanize==3.10.0
idna==3.2
isodate==0.6.0
itsdangerous==1.1.0
Jinja2==2.11.3
jsonschema==3.2.0
kombu==4.6.11
korean-lunar-calendar==0.2.1
Mako==1.1.4
Markdown==3.3.4
MarkupSafe==2.0.1
marshmallow==3.12.2
marshmallow-enum==1.5.1
marshmallow-sqlalchemy==0.23.1
msgpack==1.0.2
multidict==5.1.0
numpy==1.21.0
packaging==21.0
pandas==1.2.5
parsedatetime==2.6
pathlib2==2.3.6
pgsanity==0.2.9
polyline==1.4.0
prison==0.1.3
psycopg2==2.9.1
py==1.10.0
pyarrow==3.0.0
pycparser==2.20
PyJWT==1.7.1
PyMeeus==0.5.11
pyparsing==2.4.7
pyrsistent==0.18.0
python-dateutil==2.8.2
python-dotenv==0.18.0
python-editor==1.0.4
python-geohash==0.8.5
python3-openid==3.2.0
pytz==2021.1
PyYAML==5.4.1
redis==3.5.3
retry==0.9.2
selenium==3.141.0
simplejson==3.17.3
six==1.16.0
slackclient==2.5.0
SQLAlchemy==1.3.24
SQLAlchemy-Utils==0.36.8
sqlparse==0.3.0
typing-extensions==3.10.0.0
urllib3==1.26.6
vine==1.3.0
webencodings==0.5.1
Werkzeug==1.0.1
WTForms==2.3.3
WTForms-JSON==0.3.3
yarl==1.6.3

Checklist

Make sure to follow these steps before submitting your issue - thank you!

  • I have checked the superset logs for python stacktraces and included it here as text if there are any.
  • I have reproduced the issue with at least the latest released version of superset.
  • I have checked the issue tracker for the same issue and I haven’t found one similar.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 3
  • Comments: 36 (6 by maintainers)

Commits related to this issue

Most upvoted comments

I downgraded the version of psycopg2 and psycopg2-binary to 2.8.6 and the problem was fixed. I assume there is a problem with version 2.9.X.

Work fine after modify postgres db engine, and timezone offset problem in explore solved. Steps with docker-compose instance(version=1.3.1).

docker exec -it superset_app /bin/bash
apt-get update
apt-get install vim
vim /app/superset/db_engine_specs/postgres.py 
# delete line 171 (cursor.tzinfo_factory = FixedOffsetTimezone)
exit
# back to host machine
docker-compose -f docker-compose-non-dev.yml restart

psycopg 2.9 release note psycopg2 updated to use datetime.timezone as tz property of datetime object, we do not need pytz._FixedOffset any more.


update 2022-01-11

Should have been resolved by this https://github.com/apache/superset/pull/17713

After further experimentation, the issue seems to be timezones. If I removed the timezone offset with to_char(), my query works.

Update:

I was able to work around the issue with the following query:

SELECT (time at time zone 'CEST')::timestamp without time zone AS "time",
       ma_spo2,
       ma_bpm,
       ma_perf
FROM pulse_ox_moving_average

superser_error_workaround

This hack would allow me to create time-series charts where as using to_char would not. Setting a timezone and then localizing by removing the timezone is not ideal for the long term.

Dowgrading from 2.9.1 -> 2.8.6 was also the solution in my case.

Could quite possibly be related to this change listed in the 2.9 release notes?

Use datetime.timezone objects by default in datetime objects instead of FixedOffsetTimezone

@santicomp2014,

You should just be able to:

  • Clone the repo: $ git clone https://github.com/apache/superset.git
  • $ cd superset/requirements/
  • Change both docker.txt and development.txt to psycopg2-binary==2.8.6
  • Run: $ docker-compose -f docker-compose-non-dev.yml up

Following on your suggestion, I fixed it by editing the date column expression in the dataset editor window:

image

After further experimentation, the issue seems to be timezones. If I removed the timezone offset with to_char(), my query works.

Update:

I was able to work around the issue with the following query:

SELECT (time at time zone 'CEST')::timestamp without time zone AS "time",
       ma_spo2,
       ma_bpm,
       ma_perf
FROM pulse_ox_moving_average

superser_error_workaround

This hack would allow me to create time-series charts where as using to_char would not. Setting a timezone and then localizing by removing the timezone is not ideal for the long term.

This has been fixed on master branch here: #17713 , closing

Glad to see there are workarounds possible, however I’d prefer to use the official Docker image posted to Dockerhub. Any idea when this fix can be made available there?

I can replicate this issue using a fresh install of the latest docker image per the instructions here. Screenshot below. superset_issue_15768 According to a pip freeze in the app container (superset_app), this version is using psycopg2 v. 2.9.1. Manually replacing version 2.9.1 with 2.8.6 as suggested above resolves the issue. @mail2lawi’s solution above (time at time zone ‘CEST’)… also was an effective fix, leading me to support the hypothesis that this issue is specifically related to columns which contain timestamptz values. Thank you for your help all!

Hi @jonathanStrange0 , I’m new to superset. How do you change the version, I cloned master and found ["psycopg2-binary==2.8.5" as a dependency. This version appears to be older than 2.8.6 and I get the same error.

What are the steps to change the version with docker-compose. Thank you very much.

Hi !

For docker-compose-non-dev, here is my “hack”:

    docker-compose -f docker-compose-non-dev.yml up
    docker-compose -f docker-compose-non-dev.yml exec superset pip uninstall -y psycopg2-binary
    docker-compose -f docker-compose-non-dev.yml exec superset pip install psycopg2-binary==2.8.6
    docker-compose -f docker-compose-non-dev.yml restart superset

Before the hack

image

After the hack

image

Is this an issue that will be fixed soon? In its current state, Superset charts and datasets don’t work with any Postgresql columns of type “timestamp with time zone”.

I can replicate this issue using a fresh install of the latest docker image per the instructions here.

Screenshot below.

superset_issue_15768

According to a pip freeze in the app container (superset_app), this version is using psycopg2 v. 2.9.1.

Manually replacing version 2.9.1 with 2.8.6 as suggested above resolves the issue.

@mail2lawi’s solution above (time at time zone ‘CEST’)… also was an effective fix, leading me to support the hypothesis that this issue is specifically related to columns which contain timestamptz values.

Thank you for your help all!

This library (psycopg2-binary) was not installed at all.

  1. pip uninstall -y psycopg2-binary
  2. pip install psycopg2-binary==2.8.6

@santicomp2014,

You should just be able to:

* Clone the repo:  $ git clone https://github.com/apache/superset.git

* $ cd superset/requirements/

* Change both **docker.txt** and **development.txt** to _psycopg2-binary==2.8.6_

* Run: $ docker-compose -f docker-compose-non-dev.yml up

For me, this didn’t work. I found this “hack” : https://github.com/apache/superset/issues/15768#issuecomment-987887089

    docker-compose -f docker-compose-non-dev.yml up
    docker-compose -f docker-compose-non-dev.yml exec superset pip uninstall -y psycopg2-binary
    docker-compose -f docker-compose-non-dev.yml exec superset pip install psycopg2-binary==2.8.6
    docker-compose -f docker-compose-non-dev.yml restart superset

As a work-around I convert the date to a string. So instead of…

select rfq._created_dt, ...

use…

select to_char(rfq._created_dt, 'YYY-MM-DD') as _created_dt, ...

The actual error does not seem to come from the Postgres server as indicated by the error message. I did some debugging, and it turns out that in the condition… if abs(minutes) >= 1440: … minutes is a datetime.timedelta object which can’t be evaluated against and int using the ‘>=’ operator. But exception shows up as if it were coming from postgres.

String solution does not work well, it will cause other problems with some visualizations. The real issue is that (with Postgres) dates can be missing the timezone in some way, and forcing a timezone into the output field fixes the date issue properly without making it a string. Making the main temporal date field into a string, will cause other problems.

So, for date fields, don’t use… select date_fld, ... Add the timezone to the field… select date_fld::timestamptz at time zone 'EST',

I have 2 superset’s connected in same database.

Node 1: image

Node 2: image Query error: Query 56: <class 'TypeError'> 2021-07-19 17:06:23,715:ERROR:superset.sql_lab:Query 56: <class 'TypeError'> postgresql error: '>=' not supported between instances of 'datetime.timedelta' and 'int' 2021-07-19 17:06:23,840:WARNING:superset.views.base:postgresql error: '>=' not supported between instances of 'datetime.timedelta' and 'int'

This error appears when there is a column of type date/timestamp, when I remove this column it works