Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_validator_db_error(self, flask_g):
flask_g.user.username = "nobody"
sql = "SELECT 1 FROM default.notarealtable"
schema = "default"
fetch_fn = self.database.db_engine_spec.fetch_data
fetch_fn.side_effect = DatabaseError("dummy db error")
with self.assertRaises(PrestoSQLValidationError):
self.validator.validate(sql, schema, self.database)
cursor = connection.cursor()
try:
cursor.execute(query)
column_tuples = [
(i[0], PRESTO_TYPES_MAPPING.get(i[1], None)) for i in cursor.description
]
columns = self.fetch_columns(column_tuples)
rows = [
dict(zip(([column["name"] for column in columns]), r))
for i, r in enumerate(cursor.fetchall())
]
data = {"columns": columns, "rows": rows}
json_data = json_dumps(data)
error = None
except DatabaseError as db:
json_data = None
default_message = "Unspecified DatabaseError: {0}".format(str(db))
if isinstance(db.args[0], dict):
message = db.args[0].get("failureInfo", {"message", None}).get(
"message"
)
else:
message = None
error = default_message if message is None else message
except (KeyboardInterrupt, InterruptException) as e:
cursor.cancel()
error = "Query cancelled by user."
json_data = None
except Exception as ex:
json_data = None
error = str(ex)
"friendly_name": column_name,
"type": types_map.get(column[COLUMN_TYPE], None),
}
)
rows = [dict(zip(column_names, row)) for row in cursor]
data = {"columns": columns, "rows": rows}
json_data = json_dumps(data)
error = None
except KeyboardInterrupt:
if connection:
connection.cancel()
error = "Query cancelled by user."
json_data = None
except DatabaseError as e:
try:
error = e.args[0].status.errorMessage
except AttributeError:
error = str(e)
json_data = None
finally:
if connection:
connection.close()
return json_data, error
poll_interval=1, source=self.source, protocol=self.server_protocol
)
cursor.execute(statement)
status = cursor.poll()
if wait:
logger.progress(0)
# status None means command executed successfully
# See https://github.com/dropbox/PyHive/blob/master/pyhive/presto.py#L234
while status is not None and status['stats']['state'] != "FINISHED":
if status['stats'].get('totalSplits', 0) > 0:
pct_complete = round(status['stats']['completedSplits'] / float(status['stats']['totalSplits']), 4)
logger.progress(pct_complete * 100)
status = cursor.poll()
logger.progress(100, complete=True)
return cursor
except (DatabaseError, pandas.io.sql.DatabaseError) as e:
# Attempt to parse database error, before ultimately reraising the same
# exception, maintaining the full stacktrace.
exception, exception_args, traceback = sys.exc_info()
try:
message = e.args[0]
if isinstance(message, six.string_types):
message = ast.literal_eval(re.match("[^{]*({.*})[^}]*$", message).group(1))
linenumber = message['errorLocation']['lineNumber'] - 1
splt = statement.splitlines()
splt[linenumber] += ' <-- {errorType} ({errorName}) occurred. {message} '.format(**message)
context = '\n\n[Error Context]\n{}\n'.format('\n'.join([splt[l] for l in range(max(linenumber - 1, 0),
min(linenumber + 2, len(splt)))]))
class ErrContext(object):
"""
pass
class DatabaseError(Error):
"""Exception raised for errors that are related to the database."""
pass
class InternalError(DatabaseError):
"""Exception raised when the database encounters an internal error, e.g. the cursor is not valid
anymore, the transaction is out of sync, etc."""
pass
class OperationalError(DatabaseError):
"""Exception raised for errors that are related to the database's operation and not necessarily
under the control of the programmer, e.g. an unexpected disconnect occurs, the data source name
is not found, a transaction could not be processed, a memory allocation error occurred during
processing, etc.
"""
pass
class ProgrammingError(DatabaseError):
"""Exception raised for programming errors, e.g. table not found or already exists, syntax error
in the SQL statement, wrong number of parameters specified, etc.
"""
pass
class DataError(DatabaseError):
username=self.configuration.get('username', 'redash'),
catalog=self.configuration.get('catalog', 'hive'),
schema=self.configuration.get('schema', 'default'))
cursor = connection.cursor()
try:
cursor.execute(query)
column_tuples = [(i[0], PRESTO_TYPES_MAPPING.get(i[1], None)) for i in cursor.description]
columns = self.fetch_columns(column_tuples)
rows = [dict(zip(([c['name'] for c in columns]), r)) for i, r in enumerate(cursor.fetchall())]
data = {'columns': columns, 'rows': rows}
json_data = json.dumps(data, cls=JSONEncoder)
error = None
except DatabaseError as db:
json_data = None
default_message = 'Unspecified DatabaseError: {0}'.format(db.message)
message = db.message.get('failureInfo', {'message', None}).get('message')
error = default_message if message is None else message
except (KeyboardInterrupt, InterruptException) as e:
cursor.cancel()
error = "Query cancelled by user."
json_data = None
except Exception as ex:
json_data = None
error = ex.message
if not isinstance(error, basestring):
error = unicode(error)
return json_data, error
pass
class InterfaceError(Error):
"""Exception raised for errors that are related to the database interface rather than the
database itself.
"""
pass
class DatabaseError(Error):
"""Exception raised for errors that are related to the database."""
pass
class InternalError(DatabaseError):
"""Exception raised when the database encounters an internal error, e.g. the cursor is not valid
anymore, the transaction is out of sync, etc."""
pass
class OperationalError(DatabaseError):
"""Exception raised for errors that are related to the database's operation and not necessarily
under the control of the programmer, e.g. an unexpected disconnect occurs, the data source name
is not found, a transaction could not be processed, a memory allocation error occurred during
processing, etc.
"""
pass
class ProgrammingError(DatabaseError):
"""Exception raised for programming errors, e.g. table not found or already exists, syntax error
"""Exception raised for errors that are related to the database's operation and not necessarily
under the control of the programmer, e.g. an unexpected disconnect occurs, the data source name
is not found, a transaction could not be processed, a memory allocation error occurred during
processing, etc.
"""
pass
class ProgrammingError(DatabaseError):
"""Exception raised for programming errors, e.g. table not found or already exists, syntax error
in the SQL statement, wrong number of parameters specified, etc.
"""
pass
class DataError(DatabaseError):
"""Exception raised for errors that are due to problems with the processed data like division by
zero, numeric value out of range, etc.
"""
pass
class NotSupportedError(DatabaseError):
"""Exception raised in case a method or database API was used which is not supported by the
database, e.g. requesting a ``.rollback()`` on a connection that does not support transaction or
has transactions turned off.
"""
pass
class InternalError(DatabaseError):
"""Exception raised when the database encounters an internal error, e.g. the cursor is not valid
anymore, the transaction is out of sync, etc."""
pass
class OperationalError(DatabaseError):
"""Exception raised for errors that are related to the database's operation and not necessarily
under the control of the programmer, e.g. an unexpected disconnect occurs, the data source name
is not found, a transaction could not be processed, a memory allocation error occurred during
processing, etc.
"""
pass
class ProgrammingError(DatabaseError):
"""Exception raised for programming errors, e.g. table not found or already exists, syntax error
in the SQL statement, wrong number of parameters specified, etc.
"""
pass
class DataError(DatabaseError):
"""Exception raised for errors that are due to problems with the processed data like division by
zero, numeric value out of range, etc.
"""
pass
class NotSupportedError(DatabaseError):
"""Exception raised in case a method or database API was used which is not supported by the
database, e.g. requesting a ``.rollback()`` on a connection that does not support transaction or
:param table: Table (view) name
"""
from pyhive.exc import DatabaseError
engine = cls.get_engine(database, schema)
with closing(engine.raw_connection()) as conn:
with closing(conn.cursor()) as cursor:
sql = f"SHOW CREATE VIEW {schema}.{table}"
try:
cls.execute(cursor, sql)
polled = cursor.poll()
while polled:
time.sleep(0.2)
polled = cursor.poll()
except DatabaseError: # not a VIEW
return None
rows = cls.fetch_data(cursor, 1)
return rows[0][0]