-
Notifications
You must be signed in to change notification settings - Fork 12
fix: ensure logical operators do not modify string literal casing #49
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
base: main
Are you sure you want to change the base?
Changes from all commits
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 | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3,7 +3,6 @@ | |||||||||||||||||||
|
|
||||||||||||||||||||
| from sqlalchemy import Column, exc, sql | ||||||||||||||||||||
| from sqlalchemy.sql import compiler, operators, selectable | ||||||||||||||||||||
| from sqlalchemy.sql.compiler import OPERATORS | ||||||||||||||||||||
|
|
||||||||||||||||||||
| from sqlalchemy_kusto.dialect_base import KustoBaseDialect | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
|
@@ -82,7 +81,6 @@ def __init__(self, dialect, **kw): | |||||||||||||||||||
|
|
||||||||||||||||||||
|
|
||||||||||||||||||||
| class KustoKqlCompiler(compiler.SQLCompiler): | ||||||||||||||||||||
| OPERATORS[operators.and_] = " and " | ||||||||||||||||||||
| delete_extra_from_clause = None | ||||||||||||||||||||
| update_from_clause = None | ||||||||||||||||||||
| visit_empty_set_expr = None | ||||||||||||||||||||
|
|
@@ -162,6 +160,18 @@ def visit_select( | |||||||||||||||||||
| def limit_clause(self, select, **kw): | ||||||||||||||||||||
| return "" | ||||||||||||||||||||
|
|
||||||||||||||||||||
| def visit_clauselist(self, clauselist, **kw): | ||||||||||||||||||||
| kql_operators = { | ||||||||||||||||||||
| operators.and_: " and ", | ||||||||||||||||||||
| operators.or_: " or ", | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| if clauselist.operator in kql_operators: | ||||||||||||||||||||
| sep = kql_operators[clauselist.operator] | ||||||||||||||||||||
| return self._generate_delimited_list(clauselist.clauses, sep, **kw) | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
Comment on lines
+168
to
+172
|
||||||||||||||||||||
| if clauselist.operator in kql_operators: | |
| sep = kql_operators[clauselist.operator] | |
| return self._generate_delimited_list(clauselist.clauses, sep, **kw) | |
| if clauselist.operator in kql_operators: | |
| sep = kql_operators[clauselist.operator] | |
| return self._generate_delimited_list(clauselist.clauses, sep, **kw) |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -576,3 +576,51 @@ def test_schema_from_query(query_table_name: str, expected_table_name: str): | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| query_expected = f"let inner_qry = ({expected_table_name});inner_qry| take 5" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert query_compiled == query_expected | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def test_logical_operators_and_literals_precedence(): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| val_and = "DATA AND ANALYTICS" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| val_or = "OPEN OR CLOSED" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| condition = sa.and_( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| sa.or_(column("Field1") == val_and, column("Field2") == val_or), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| column("Status") == "ACTIVE", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| query = select([column("Field1")]).select_from(text("logs")).where(condition) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| query_compiled = str( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| query.compile(engine, compile_kwargs={"literal_binds": True}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ).replace("\n", " ") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expected_full = ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| '["logs"] ' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| f"| where ([\"Field1\"] == '{val_and}' or [\"Field2\"] == '{val_or}') and [\"Status\"] == 'ACTIVE' " | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| '| project ["Field1"]' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert query_compiled == expected_full | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def test_logical_operators_precedence_and_casing(): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Test that 'or' inside 'and' gets parentheses (required), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| but 'and' inside 'or' does not (not required by precedence). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cond_nested_or = sa.and_( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| sa.or_(column("A") == 1, column("B") == 2), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| column("C") == 3 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| query1 = select([column("Field")]).select_from(text("logs")).where(cond_nested_or) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| compiled1 = str(query1.compile(engine, compile_kwargs={"literal_binds": True})).replace("\n", " ") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert '(["A"] == 1 or ["B"] == 2) and ["C"] == 3' in compiled1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cond_nested_and = sa.or_( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| column("A") == 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| sa.and_(column("B") == 2, column("C") == 3) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| query2 = select([column("Field")]).select_from(text("logs")).where(cond_nested_and) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| compiled2 = str(query2.compile(engine, compile_kwargs={"literal_binds": True})).replace("\n", " ") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+612
to
+624
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| column("C") == 3 | |
| ) | |
| query1 = select([column("Field")]).select_from(text("logs")).where(cond_nested_or) | |
| compiled1 = str(query1.compile(engine, compile_kwargs={"literal_binds": True})).replace("\n", " ") | |
| assert '(["A"] == 1 or ["B"] == 2) and ["C"] == 3' in compiled1 | |
| cond_nested_and = sa.or_( | |
| column("A") == 1, | |
| sa.and_(column("B") == 2, column("C") == 3) | |
| ) | |
| query2 = select([column("Field")]).select_from(text("logs")).where(cond_nested_and) | |
| compiled2 = str(query2.compile(engine, compile_kwargs={"literal_binds": True})).replace("\n", " ") | |
| column("C") == 3, | |
| ) | |
| query1 = ( | |
| select([column("Field")]) | |
| .select_from(text("logs")) | |
| .where(cond_nested_or) | |
| ) | |
| compiled1 = ( | |
| str(query1.compile(engine, compile_kwargs={"literal_binds": True})) | |
| .replace("\n", " ") | |
| ) | |
| assert '(["A"] == 1 or ["B"] == 2) and ["C"] == 3' in compiled1 | |
| cond_nested_and = sa.or_( | |
| column("A") == 1, | |
| sa.and_(column("B") == 2, column("C") == 3), | |
| ) | |
| query2 = ( | |
| select([column("Field")]) | |
| .select_from(text("logs")) | |
| .where(cond_nested_and) | |
| ) | |
| compiled2 = ( | |
| str(query2.compile(engine, compile_kwargs={"literal_binds": True})) | |
| .replace("\n", " ") | |
| ) |
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.
visit_clauselist()currently ignoresclauselist.group/ grouping semantics and always returns the raw_generate_delimited_list(...)output for AND/OR. SQLAlchemy uses grouping to emit parentheses for nested boolean expressions (e.g.and_(or_(...), ...)), so this override should preserve that behavior (e.g. wrap the rendered list when grouping is requested) to avoid changing operator precedence in the emitted KQL.