AWS Redshift Decimal字段查询返回错误结果求助(Python redshift-connector)
问题排查与解决思路
1. 升级redshift-connector版本
旧版本的redshift-connector对高精度DECIMAL类型的解析可能存在bug,尤其是处理DECIMAL(38,20)这种超宽范围的数值时,容易出现符号错误。先升级到最新稳定版:
pip install --upgrade redshift-connector
2. 查询时显式转换字段类型
在SQL语句里强制转换精度,绕过驱动自动解析的问题:
import redshift_connector from decimal import Decimal conn = redshift_connector.connect( host='你的Redshift地址', database='目标数据库', user='账号', password='密码' ) cursor = conn.cursor() # 把字段转成Python Decimal能稳定处理的精度,比如DECIMAL(18,8) cursor.execute('SELECT CAST("price" AS DECIMAL(18,8)) FROM deal_history;') results = cursor.fetchall() for row in results: print(row[0])
3. 禁用驱动自动Decimal转换
让驱动直接返回字符串,再手动转成Decimal,避免自动解析出错:
import redshift_connector from decimal import Decimal conn = redshift_connector.connect( host='你的Redshift地址', database='目标数据库', user='账号', password='密码', convert_decimal=False # 关闭自动转换,返回原始字符串 ) cursor = conn.cursor() cursor.execute('SELECT "price" FROM deal_history;') results = cursor.fetchall() for row in results: price = Decimal(row[0]) print(price)
4. 调整Python Decimal精度上下文
Python默认的Decimal精度可能不足以支撑DECIMAL(38,20)的超大位数,导致解析溢出出现负数。手动调整精度:
from decimal import getcontext import redshift_connector # 设置足够覆盖38位整数+20位小数的精度 getcontext().prec = 40 conn = redshift_connector.connect(...) cursor.execute('SELECT "price" FROM deal_history;') results = cursor.fetchall() for row in results: print(row[0])
5. 验证数据真实状态
虽然psql查询正常,但还是可以在psql里执行以下语句,确认Redshift中确实没有负数:
-- 统计负数条数 SELECT COUNT(*) FROM deal_history WHERE "price" < 0; -- 查看疑似异常值 SELECT "price" FROM deal_history WHERE "price" < 0 LIMIT 10;
内容的提问来源于stack exchange,提问作者Winston He
相关产品推荐
相关产品推荐

