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

Added mysql case in cleaner #220

Merged
merged 3 commits into from
Sep 18, 2024
Merged

Added mysql case in cleaner #220

merged 3 commits into from
Sep 18, 2024

Conversation

Muhammad18557
Copy link
Contributor

The eval I ran earlier had a lot of errors because of this space before and after ( and ).
This is something that is completely fine with postgres but erroneous in mysql. We do some post cleaning of the query to make sure we get rid of any such spaces. A case with spaces both before and after a bracket was handled but a more popular case is when there is a space after the bracket. Including this in the clean_generated_query for clarity.

# remove extra spaces around brackets especially for MySQL
query = query.replace(" ( ", "(").replace(" )", ")")
query = query.replace(" (", "(").replace(") ", ")")
query = query.replace("( ", "(").replace(" )", ")")
Copy link
Member

@rishsriv rishsriv Sep 17, 2024

Choose a reason for hiding this comment

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

Hello! Could we use regex here for cleaner code that handles edge cases better? The code above would fail in some cases. For example, if the query generated is

query = "SELECT  SUM    (      a      ) from table;"

The the code above would fail.

However, the code below would work fine.

query = re.sub(r'\s*\(\s*', '(', query)  # Remove spaces before and after '('
query = re.sub(r'\s*\)\s*', ') ', query)        # Remove spaces before and after ')', and only leaves a single space for syntactic correctness

Copy link
Contributor Author

@Muhammad18557 Muhammad18557 Sep 17, 2024

Choose a reason for hiding this comment

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

Thanks, fixed- also realized we do not want to remove space after a closing brace because that is valid- eg: COUNT(t.sbTxStatus) , 0) AS success_rate. So, its this now:

# remove extra spaces around brackets especially for MySQL
query = re.sub(r"\s*\(\s*", "(", query)  # Remove spaces before and after '('
query = re.sub(r"\s*\)", ")", query)  # Remove spaces before ')'

Copy link
Member

Choose a reason for hiding this comment

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

Cool, looks good to to me!

Copy link
Contributor

Choose a reason for hiding this comment

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

looks good to me too! thanks for this change

@rishsriv rishsriv merged commit f66bd5b into main Sep 18, 2024
2 checks passed
@rishsriv rishsriv deleted the abdullah/mysql-clean-query branch September 18, 2024 02:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants