Parameterized unload throws an error
See original GitHub issueDriver version
2.0.877
Redshift version
PostgreSQL 8.0.2 on i686-pc-linux-gnu, compiled by GCC gcc (GCC) 3.4.2 20041017 (Red Hat 3.4.2-6.fc3), Redshift 1.0.25109
Client Operating System
Ubuntu 20.04
Python version
3.8.5
Table schema
N/A
Problem description
- Expected behaviour: An unload is executed successfully when a parameterized expression is used.
- Actual behaviour: An error is thrown.
- Error message/stack trace:
> python3 test_unload.py
Traceback (most recent call last):
File "test_unload.py", line 16, in <module>
cursor.execute(f"""
File ".../lib/python3.8/site-packages/redshift_connector/cursor.py", line 162, in execute
self._c.execute(self, operation, args)
File ".../lib/python3.8/site-packages/redshift_connector/core.py", line 1016, in execute
self.handle_messages(cursor)
File ".../lib/python3.8/site-packages/redshift_connector/core.py", line 1172, in handle_messages
raise self.error
redshift_connector.error.ProgrammingError: {'S': 'ERROR', 'C': '42601', 'M': 'syntax error at or near "$1"', 'P': '14', 'F': '/home/ec2-user/padb/src/pg/src/backend/parser/parser_scan.l', 'L': '718', 'R': 'yyerror'}
- Any other details that can be helpful: the test code works when all parameters are hardcoded.
Python Driver trace logs
N/A
Reproduction code
test_unload.py
:
import os
import redshift_connector
conn = redshift_connector.connect(
host=os.getenv("REDSHIFT_HOST"),
database=os.getenv("REDSHIFT_DATABASE"),
user=os.getenv("REDSHIFT_USER"),
password=os.getenv("REDSHIFT_PASSWORD"),
)
destination = os.getenv("REDSHIFT_UNLOAD_DESTINATION")
role = os.getenv("REDSHIFT_UNLOAD_ROLE")
cursor = conn.cursor()
cursor.execute(f"""
UNLOAD (%s)
TO '{destination}'
IAM_ROLE '{role}'
PARALLEL OFF
""", ['SELECT id FROM mydb.accounts WHERE id = 0'])
print(cursor.fetchall())
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (5 by maintainers)
Top Results From Across the Web
Unloading dll throws an Access violation error - Stack Overflow
You're unloading the DLL while the thread is still running. That means the code it is executing no longer exists. Use FreeLibraryAndExitThread() ...
Read more >Troubleshoot UNLOAD issues in Amazon Redshift
I'm trying to unload data from my Amazon Redshift cluster to Amazon Simple Storage Service (Amazon S3). However, I'm getting an error.
Read more >Database Engine events and errors - SQL Server
Consult this MSSQL error code list to find explanations for error messages for SQL Server database engine events.
Read more >Class 10 - Redshift UNLOAD Command - YouTube
Contains demonstration of the UNLOAD command in Redshift. All the options that can be used with unload command are explained.
Read more >2 Error Codes and Descriptions - Oracle Help Center
The error is thrown by Java and TopLink only wraps the reflection exception. ... Action: Check your child descriptor and remove the field....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hey @djciregethigher ,
Thank you for sharing your findings and congrats on your first post 😃. When comparing the output from the two code blocks, it seems to be identical. Could you please help me understand?
Not sure if this applies to your use case, but redshift_connector supports parameterization without needing to use format strings which can be a bit cleaner and provide protection against sql injection. I’ve put an example of how to do this below using Redshift MONTHS_BETWEEN function.
If you continue to experience issue with this or any other redshift_connector functionality feel free to open a new issue and we can work to debug/fix any issue at hand 😃.
Hi @Brooke-white, thank you for the solution for parametrizing dates!
In reference to my findings, I’m using slightly different techniques to create/modify/parametrize query-strings of SQL that I then use as an input for the .execute() method.
I found that using conventional f-strings to create formatted strings were throwing me an error due to all the reasons discussed in this thread.
I decided to use the Python’s native .format() method for strings to build the string first, and then feed the string to the .execute() method of Redshift Connector.
To rephrase,
instead of using this:
https://docs.python.org/3/tutorial/inputoutput.html#tut-f-strings
I use this to create your strings before feeding them to .execute():
https://docs.python.org/3/library/stdtypes.html#str.format
I hope this better helps you understand my findings!
Cheers,
DJ CIRE
P.S. if not helpful, then please disregard! LOL