How can I track down RedShift throwing errors when aggregating on an otherwise functioning query involving CTEs and UNIONS?

0

I have a query (actual query is obv larger and more complex) like:

INSERT INTO my_table
WITH a (SELECT timestamp, count FROM foo),
     b (SELECT timestamp, count FROM bar),
     c (
       SELECT * FROM foo
       UNION ALL
       SELECT * FROM bar
     )
SELECT timestamp, count FROM c;

SELECT timestamp, SUM(count) FROM my_table;

This works fine. However when I change it because I'm investigating the query results to:

WITH a (SELECT timestamp, count FROM foo),
     b (SELECT timestamp, count FROM bar),
     c  (
       SELECT * FROM foo
       UNION ALL
       SELECT * FROM bar
     )
SELECT timestamp, SUM(count)
FROM c
GROUP BY 1;

It throws a Not Implemented error with code 1001. Now this is 100% a RedShift bug because it seems to not be able to track the types across my query, but my actual question is all I can see in STL_ERROR is:

file,linenum,context,error
../src/sys/pg_utils.cpp,2218,Not implemented,'value' - Ill formed PARAM_EXEC in expression                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
../src/sys/pg_utils.cpp,2218,Not implemented,'value' - Ill formed PARAM_EXEC in expression                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

In STV_RECENT it doesn't even appear. How can I break down what RedShift is doing to track down the point where it's failing exactly? The only change to my original query is attempting to do a ... SUM ... GROUP 1 on the original query causing it to fail.

  • timestamp is a reserved word so it might be having trouble trying to associate the position alias of "1" with the reserved word "timestamp". Try using double quotes around the column name and/or explicitly using the column name in the group by rather than the position alias.

질문됨 2년 전1419회 조회
1개 답변
0

Timestamp and Count are reserved words. It is recommended to avoid using these as column names. Else use double quotes around them in your code as shown below. Note that I have removed the table "a" and table "b" as you were not using them anyways

WITH c as (
       SELECT "timestamp", "count" FROM foo
       UNION ALL
       SELECT "timestamp", "count" FROM bar
     )
SELECT "timestamp", SUM("count")
FROM c
GROUP BY 1;
profile pictureAWS
답변함 2년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인