SUM doesn't work somehow in Redshift table
Make sure these boxes are checked before submitting your issue - thank you!
- [ ] I have checked the superset logs for python stacktraces and included it here as text if any
- [x] I have reproduced the issue with at least the latest released version of superset
- [x] I have checked the issue tracker for the same issue and I haven't found one similar
Superset version
0.26.3
Expected results
I was expecting to see daily revenue trending graph from a Redshift table and here is the SQL query:
SELECT
name AS name,
DATE_TRUNC('month', created_date) AT TIME ZONE 'UTC' AS __timestamp,
SUM(cost) AS "SUM(cost)"
FROM xxxx.yyyyyy
WHERE created_date >= '2016-07-26 00:00:00'
AND created_date <= '2018-07-26 03:32:14'
GROUP BY name,
DATE_TRUNC('month', created_date) AT TIME ZONE 'UTC'
ORDER BY "SUM(cost)" DESC
LIMIT 50000;
Actual results
I tried a few different tables but got exactly same result. I used the following connection string: postgresql+psycopg2://account:XXXXXXXXXX@redshift-host-name:5439/database
COUNT did work well.
Steps to reproduce
Hopefully the above screen and script are self-explanatory.
How about using redshift+psycopg2:// as a connection string?
I got an error from the connection string: "Can't load plugin: sqlalchemy.dialects:redshift.psycopg2"
I tried "postgres://" string and still got the same error. Not so sure whether this issue is a unique problem specific to my environment but the examples from Superset works totally fine.
I installed another Superset from the scratch in another server but got the same result :(
@mistercrunch any idea what to do to fix "Can't load plugin: sqlalchemy.dialects:redshift.psycopg2" error? I have these:
psycopg2==2.7.5 sqlalchemy-redshift==0.7.1 SQLAlchemy==1.2.10 Python 2.7.12
Do I need redshift-sqlalchemy instead? I am getting some installation error from "sudo pip install redshift-sqlalchemy" which I will try to fix soon hopefully and see
The docs are pretty clear about most of what is discussed in this issue: http://superset.apache.org/installation.html#database-dependencies
It was a matter of restarting the Superset for the connection string problem (redshift+psycopg2) I had. But the issue remains the same. SUM(cost) doesn't work somehow.
Sorry to keep bugging @mistercrunch. Any pointer or insight? If there is some related code I can take a look, that works too. Count(*) works fine BTW.
@keeyong The error looks eerily similar to what I've just been working on in #5487 . Do you have the possibility to try my branch to see if it works better?
@villebro I will be more than happy too. I know how to download your branch but not very clear how to test. Once some hint is provided, I will test for sure
After cloning the branch, you can probably follow the steps described in https://github.com/apache/incubator-superset/blob/master/CONTRIBUTING.md#setting-up-a-python-development-environment .
@villebro thank you for your help but it still doesn't solve my issue. I am seeing an error like the followings:
2018-07-27 00:02:16,101:ERROR:root:u'SUM(cost)'
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/superset/views/core.py", line 1104, in generate_json
payload = viz_obj.get_payload()
File "/usr/local/lib/python2.7/dist-packages/superset/viz.py", line 361, in get_payload
payload['data'] = self.get_data(df)
File "/usr/local/lib/python2.7/dist-packages/superset/viz.py", line 1242, in get_data
df = self.process_data(df)
File "/usr/local/lib/python2.7/dist-packages/superset/viz.py", line 1146, in process_data
values=utils.get_metric_names(fd.get('metrics')))
File "/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py", line 5303, in pivot_table
margins_name=margins_name)
File "/usr/local/lib/python2.7/dist-packages/pandas/core/reshape/pivot.py", line 61, in pivot_table
raise KeyError(i)
KeyError: u'SUM(cost)'
Maybe it is a totally different problem? The SQL query is this:
SELECT DATE_TRUNC('month', created_at) AT TIME ZONE 'UTC' AS __timestamp,
SUM(cost) AS "SUM(cost)"
FROM raw_data.payments
WHERE created_at >= '2018-07-20 00:00:00'
AND created_at <= '2018-07-27 00:05:28'
GROUP BY DATE_TRUNC('month', created_at) AT TIME ZONE 'UTC'
ORDER BY "SUM(cost)" DESC
LIMIT 50000;
@villebro I think you gave me an idea. It turns out when I used "sum(cost)" explicitly instead of "SUM(cost)" which is given by the tool. Is this issue supposed to be fixed by your change?
@keeyong Can you manually add the following print statements to row 1137 of superset/viz.py so that the section looks like the following:
def process_data(self, df, aggregate=False):
fd = self.form_data
df = df.fillna(0)
if fd.get('granularity') == 'all':
raise Exception(_('Pick a time granularity for your time series'))
print('!! Columns in df: ', df.columns)
print('!! Metric labels: ', utils.get_metric_names(fd.get('metrics')))
print('!! Metrics (raw): ', fd.get('metrics'))
if not aggregate:
df = df.pivot_table(
index=DTTM_ALIAS,
columns=fd.get('groupby'),
values=utils.get_metric_names(fd.get('metrics')))
After that, reinstall, restart and rerun with both the count metric (the one that works) and sum metric (the one that doesn't work) and paste here what the extra output looks like for both, ie. the output rows that start with !!.
@keeyong I'm thinking one is an adhocMetric and the other regular metric, and the label of one is encoded differently to what the dataframe column name is. But it should be fairly clear once you show the output.
That's great @villebro. Here is the output:
!! Columns in df: Index([u'__timestamp', u'sum(cost)'], dtype='object') !! Metric labels: [u'SUM(cost)'] !! Metrics (raw): [{u'hasCustomLabel': False, u'expressionType': u'SIMPLE', u'fromFormData': False, u'label': u'SUM(cost)', u'column': {u'optionName': u'_col_cost', u'description': None, u'filterable': False, u'expression': u'', u'is_dttm': False, u'type': u'BIGINT', u'verbose_name': None, u'groupby': False, u'column_name': u'cost'}, u'sqlExpression': None, u'aggregate': u'SUM', u'optionName': u'metric_w0umnqaijfr_7enyx6vgy83'}]
@keeyong Could you also post the same for the one that does not give the error?
@villebro sure. When I used lowercase for sum, here is the output:
!! Columns in df: Index([u'name', u'__timestamp', u'sum(cost)'], dtype='object') !! Metric labels: [u'sum(cost)'] !! Metrics (raw): [{u'hasCustomLabel': False, u'expressionType': u'SQL', u'fromFormData': True, u'label': u'sum(cost)', u'column': None, u'sqlExpression': u'sum(cost)', u'aggregate': None, u'optionName': u'metric_6bs73coem1m_iugv8q8qin'}]
@keeyong Can you confirm that you're running the preprocess_df branch, i.e. https://github.com/villebro/incubator-superset/tree/preprocess_df ? Also, what does the first part of the connection string look like, up until ://? I would expect my branch to have solved your problem for Redshift..
A good point. I might have been running the master!!! Let me double-check. I am using redshift+psycopg2://
I think I am on your branch @villebro:
$ git branch
master
* preprocess_df