Skip to content
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

Add option to preserve the original ordering of columns #75

Merged
merged 2 commits into from
Nov 11, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion bigquery_schema_generator/generate_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ def __init__(
debugging_map=False,
sanitize_names=False,
ignore_invalid_lines=False,
keep_input_sort_order=False,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use preserve_input_sort_order

):
self.input_format = input_format
self.infer_mode = infer_mode
Expand Down Expand Up @@ -113,7 +114,7 @@ def __init__(
# If CSV, preserve the original ordering because 'bq load` matches the
# CSV column with the respective schema entry using the position of the
# column in the schema.
self.sorted_schema = (input_format in {'json', 'dict'})
self.sorted_schema = (input_format in {'json', 'dict'}) and not keep_input_sort_order
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you run $ make flake8 at the root of the project, you'll see that it will complain about being longer than 80 columns. Break this into smaller lines using something like:

self.sorted_schema = (
  (input_format in {'json', 'dict'})
  and not keep_input_sort_order
)


self.line_number = 0
self.error_logs = []
Expand Down Expand Up @@ -1042,6 +1043,12 @@ def main():
' This can be fetched with:'
' `bq show --schema <project_id>:<dataset>:<table_name>',
default=None)
parser.add_argument(
'--keep-input-sort-order',
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use --preserve_input_sort_order using underscore for consistency with other flags.

help='Preserve the original ordering of columns from input instead of sorting alphabetically.'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

break long line < 80 columns

' This only only impacts `input_format` of json or dict',
action='store_true'
)
args = parser.parse_args()

# Configure logging.
Expand All @@ -1056,6 +1063,7 @@ def main():
debugging_map=args.debugging_map,
sanitize_names=args.sanitize_names,
ignore_invalid_lines=args.ignore_invalid_lines,
keep_input_sort_order=args.keep_input_sort_order
)
existing_schema_map = read_existing_schema_from_file(
args.existing_schema_path)
Expand Down