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_invalid_sql_raises(self, invalid_sql):
result = _translate_exception(invalid_sql)
assert isinstance(result, ProgrammingError)
assert 'Validate failed: From line 1,' in result.args[0]
import_query = import_query.replace(
"##FILE##", import_file
)
except FileNotFoundError:
logging.exception("Could not find import_query_template_file.")
exit(1)
else:
import_query = "COPY %s FROM '%s';" % (import_table_name, import_file)
logging.debug("Import query: " + import_query)
logging.info("Starting import...")
start_time = timeit.default_timer()
try:
res = con.execute(import_query)
end_time = timeit.default_timer()
logging.info("Completed import.")
except (pymapd.exceptions.ProgrammingError, pymapd.exceptions.Error):
logging.exception("Error running import query")
if no_drop_table_before:
logging.error(
'Import failed and "--no-drop-table-before" was '
+ "passed in. Make sure existing table schema matches "
+ "import .csv file schema."
)
exit(1)
# Calculate times and results values
query_elapsed_time = round(((end_time - start_time) * 1000), 1)
execution_time = res._result.execution_time_ms
connect_time = round((query_elapsed_time - execution_time), 2)
res_output = str(res.fetchall()[0])
logging.debug("Query result output: " + res_output)
rows_loaded = re.search(r"Loaded: (.*?) recs, R", res_output).group(1)
with open(kwargs["table_schema_file"], "r") as table_schema:
logging.debug(
"Reading table_schema_file: " + kwargs["table_schema_file"]
)
create_table_sql = table_schema.read().replace("\n", " ")
create_table_sql = create_table_sql.replace(
"##TAB##", kwargs["table"]
)
except FileNotFoundError:
logging.exception("Could not find destination table_schema_file.")
return False
try:
logging.debug("Executing create destination table query")
dest_con.execute(create_table_sql)
logging.debug("Destination table created.")
except (pymapd.exceptions.ProgrammingError, pymapd.exceptions.Error):
logging.exception("Error running destination table creation")
return False
logging.info("Loading results into destination db")
try:
dest_con.load_table_columnar(
kwargs["table"],
results_df,
preserve_index=False,
chunk_size_bytes=0,
col_names_from_schema=True,
)
except (pymapd.exceptions.ProgrammingError, pymapd.exceptions.Error):
logging.exception("Error loading results into destination db")
dest_con.close()
return False
dest_con.close()
def _translate_exception(e):
# type: (Exception) -> Exception
"""Translate a thrift-land exception to a DB-API 2.0
exception.
"""
# TODO: see if there's a way to get error codes, rather than relying msgs
if not isinstance(e, TMapDException):
return e
if 'Validate failed' in e.error_msg or 'Parse failed' in e.error_msg:
err = ProgrammingError
elif 'Exception occurred' in e.error_msg:
err = DatabaseError
else:
err = Error
return err(e.error_msg)