Merge pull request #2428 from toru-takahashi/master

Improve a query failure message for Treasure Data Runner
This commit is contained in:
Arik Fraimovich 2018-04-12 15:14:24 +03:00 committed by GitHub
commit 7465c74392
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -8,6 +8,7 @@ logger = logging.getLogger(__name__)
try:
import tdclient
from tdclient import errors
enabled = True
except ImportError:
@ -102,22 +103,24 @@ class TreasureData(BaseQueryRunner):
db=self.configuration.get('db'))
cursor = connection.cursor()
try:
cursor.execute(query)
columns_data = [(row[0], cursor.show_job()['hive_result_schema'][i][1]) for i,row in enumerate(cursor.description)]
cursor.execute(query)
columns_data = [(row[0], cursor.show_job()['hive_result_schema'][i][1]) for i,row in enumerate(cursor.description)]
columns = [{'name': col[0],
'friendly_name': col[0],
'type': TD_TYPES_MAPPING.get(col[1], None)} for col in columns_data]
if cursor.rowcount == 0:
rows = []
else:
rows = [dict(zip(([c[0] for c in columns_data]), r)) for i, r in enumerate(cursor.fetchall())]
data = {'columns': columns, 'rows': rows}
json_data = json.dumps(data, cls=JSONEncoder)
error = None
columns = [{'name': col[0],
'friendly_name': col[0],
'type': TD_TYPES_MAPPING.get(col[1], None)} for col in columns_data]
if cursor.rowcount == 0:
rows = []
else:
rows = [dict(zip(([c[0] for c in columns_data]), r)) for i, r in enumerate(cursor.fetchall())]
data = {'columns': columns, 'rows': rows}
json_data = json.dumps(data, cls=JSONEncoder)
error = None
except errors.InternalError as e:
json_data = None
error = "%s: %s" % (e.message, cursor.show_job().get('debug', {}).get('stderr', 'No stderr message in the response')
return json_data, error
register(TreasureData)