-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve implementation of storage of transpiled files #1409
base: main
Are you sure you want to change the base?
Changes from all commits
7beeaff
879407c
b9e663e
3bc5fb2
8cb310f
8ac675a
819aad5
e8b7b3c
1c36787
203573e
47e6227
8a58089
40b1eff
6f38994
e301186
b02795f
5ee7117
c4836f3
f7ad038
8803594
ee18e9c
b54e627
39cf86d
561515f
0bbfd08
ae30790
f4eddda
8010899
eef26a8
ea0bf62
94783de
5baeb69
ea9ff6f
499cc8d
82a7bcb
5a4ba11
46a41af
f840530
7dabd9a
0170b1f
cda3c47
c44a146
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,7 +33,7 @@ | |
logger = logging.getLogger(__name__) | ||
|
||
|
||
async def _process_file( | ||
async def _process_one_file( | ||
config: TranspileConfig, | ||
validator: Validator | None, | ||
transpiler: TranspileEngine, | ||
|
@@ -71,29 +71,23 @@ async def _process_file( | |
return transpile_result.success_count, error_list | ||
|
||
|
||
async def _process_directory( | ||
async def _process_many_files( | ||
config: TranspileConfig, | ||
validator: Validator | None, | ||
transpiler: TranspileEngine, | ||
root: Path, | ||
base_root: Path, | ||
output_folder: Path, | ||
files: list[Path], | ||
) -> tuple[int, list[TranspileError]]: | ||
|
||
output_folder = config.output_folder | ||
output_folder_base = root / ("transpiled" if output_folder is None else base_root) | ||
make_dir(output_folder_base) | ||
|
||
counter = 0 | ||
all_errors: list[TranspileError] = [] | ||
|
||
for file in files: | ||
logger.info(f"Processing file :{file}") | ||
if not is_sql_file(file): | ||
continue | ||
|
||
output_file_name = output_folder_base / file.name | ||
success_count, error_list = await _process_file(config, validator, transpiler, file, output_file_name) | ||
output_file_name = output_folder / file.name | ||
success_count, error_list = await _process_one_file(config, validator, transpiler, file, output_file_name) | ||
counter = counter + success_count | ||
all_errors.extend(error_list) | ||
|
||
|
@@ -104,15 +98,17 @@ async def _process_input_dir(config: TranspileConfig, validator: Validator | Non | |
error_list = [] | ||
file_list = [] | ||
counter = 0 | ||
input_source = str(config.input_source) | ||
input_path = Path(input_source) | ||
for root, _, files in dir_walk(input_path): | ||
base_root = Path(str(root).replace(input_source, "")) | ||
folder = str(input_path.resolve().joinpath(base_root)) | ||
msg = f"Processing for sqls under this folder: {folder}" | ||
logger.info(msg) | ||
input_path = config.input_path | ||
output_folder = config.output_path | ||
if output_folder is None: | ||
output_folder = input_path.parent / "transpiled" | ||
make_dir(output_folder) | ||
for source_dir, _, files in dir_walk(input_path): | ||
relative_path = cast(Path, source_dir).relative_to(input_path) | ||
transpiled_dir = output_folder / relative_path | ||
logger.info(f"Transpiling sql files from folder: {source_dir!s} into {transpiled_dir!s}") | ||
file_list.extend(files) | ||
no_of_sqls, errors = await _process_directory(config, validator, transpiler, root, base_root, files) | ||
no_of_sqls, errors = await _process_many_files(config, validator, transpiler, transpiled_dir, files) | ||
counter = counter + no_of_sqls | ||
error_list.extend(errors) | ||
return TranspileStatus(file_list, counter, error_list) | ||
|
@@ -126,23 +122,21 @@ async def _process_input_file( | |
logger.warning(msg) | ||
# silently ignore non-sql files | ||
return TranspileStatus([], 0, []) | ||
msg = f"Processing sql from this file: {config.input_source}" | ||
msg = f"Transpiling sql file: {config.input_path!s}" | ||
logger.info(msg) | ||
if config.output_path is None: | ||
output_path = config.output_path | ||
if output_path is None: | ||
output_path = config.input_path.parent / "transpiled" | ||
else: | ||
output_path = config.output_path | ||
|
||
make_dir(output_path) | ||
output_file = output_path / config.input_path.name | ||
no_of_sqls, error_list = await _process_file(config, validator, transpiler, config.input_path, output_file) | ||
no_of_sqls, error_list = await _process_one_file(config, validator, transpiler, config.input_path, output_file) | ||
return TranspileStatus([config.input_path], no_of_sqls, error_list) | ||
|
||
|
||
@timeit | ||
async def transpile( | ||
workspace_client: WorkspaceClient, engine: TranspileEngine, config: TranspileConfig | ||
) -> tuple[list[dict[str, Any]], list[TranspileError]]: | ||
) -> tuple[dict[str, Any], list[TranspileError]]: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. return a dict, we never have less or more than 1 status item |
||
await engine.initialize(config) | ||
status, errors = await _do_transpile(workspace_client, engine, config) | ||
await engine.shutdown() | ||
|
@@ -151,7 +145,7 @@ async def transpile( | |
|
||
async def _do_transpile( | ||
workspace_client: WorkspaceClient, engine: TranspileEngine, config: TranspileConfig | ||
) -> tuple[list[dict[str, Any]], list[TranspileError]]: | ||
) -> tuple[dict[str, Any], list[TranspileError]]: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. return a dict, we never have less or more than 1 status item |
||
""" | ||
[Experimental] Transpiles the SQL queries from one dialect to another. | ||
|
||
|
@@ -163,8 +157,6 @@ async def _do_transpile( | |
logger.error("Input SQL path is not provided.") | ||
raise ValueError("Input SQL path is not provided.") | ||
|
||
status = [] | ||
|
||
validator = None | ||
if not config.skip_validation: | ||
sql_backend = db_sql.get_sql_backend(workspace_client) | ||
|
@@ -194,17 +186,16 @@ async def _do_transpile( | |
with cast(Path, error_log_path).open("a", encoding="utf-8") as e: | ||
e.writelines(f"{err!s}\n" for err in result.error_list) | ||
|
||
status.append( | ||
{ | ||
"total_files_processed": len(result.file_list), | ||
"total_queries_processed": result.no_of_transpiled_queries, | ||
"no_of_sql_failed_while_analysing": result.analysis_error_count, | ||
"no_of_sql_failed_while_parsing": result.parsing_error_count, | ||
"no_of_sql_failed_while_generating": result.generation_error_count, | ||
"no_of_sql_failed_while_validating": result.validation_error_count, | ||
"error_log_file": str(error_log_path), | ||
} | ||
) | ||
status = { | ||
"total_files_processed": len(result.file_list), | ||
"total_queries_processed": result.no_of_transpiled_queries, | ||
"analysis_error_count": result.analysis_error_count, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. align names |
||
"parsing_error_count": result.parsing_error_count, | ||
"validation_error_count": result.validation_error_count, | ||
"generation_error_count": result.generation_error_count, | ||
"error_log_file": str(error_log_path), | ||
} | ||
|
||
return status, result.error_list | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -80,7 +80,7 @@ async def transpile( | |
read_dialect = get_dialect(source_dialect) | ||
error: TranspileError | None = self._check_supported(read_dialect, source_code, file_path) | ||
if error: | ||
return TranspileResult(str(file_path), 1, [error]) | ||
return TranspileResult(source_code, 1, [error]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fix silent bug |
||
write_dialect = get_dialect(target_dialect) | ||
try: | ||
transpiled_expressions = transpile( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
moved this logic upwards