-
Notifications
You must be signed in to change notification settings - Fork 12
Add quote-to-bracket escaping for calculated measures with multiple ad-hoc measures #50
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 |
|---|---|---|
|
|
@@ -107,7 +107,7 @@ def visit_select( | |
| from_object = select_stmt.get_final_froms()[0] | ||
| if hasattr(from_object, "element"): | ||
| query = self._get_most_inner_element(from_object.element) | ||
| (main, lets) = self._extract_let_statements(query.text) | ||
| main, lets = self._extract_let_statements(query.text) | ||
| compiled_query_lines.extend(lets) | ||
| compiled_query_lines.append( | ||
| f"let {from_object.name} = ({self._convert_schema_in_statement(main)});" | ||
|
|
@@ -142,9 +142,15 @@ def visit_select( | |
| ) | ||
| compiled_query_lines.append(f"| where {converted_where_clause}") | ||
|
|
||
| # Add summarize first if it exists | ||
| if "summarize" in projections_parts_dict: | ||
| compiled_query_lines.append(projections_parts_dict.pop("summarize")) | ||
|
|
||
| # Then add extend after summarize | ||
| if "extend" in projections_parts_dict: | ||
| compiled_query_lines.append(projections_parts_dict.pop("extend")) | ||
|
|
||
| # Add remaining parts (project, sort) | ||
| for statement_part in projections_parts_dict.values(): | ||
| if statement_part: | ||
| compiled_query_lines.append(statement_part) | ||
|
Comment on lines
+145
to
156
|
||
|
|
@@ -356,29 +362,40 @@ def _escape_and_quote_columns(name: str | None, is_alias=False) -> str: | |
| or KustoKqlCompiler._is_number_literal(name) | ||
| ) and not is_alias: | ||
| return name | ||
| if name.startswith('"') and name.endswith('"'): | ||
| name = name[1:-1] | ||
| # First, check if the name is already wrapped in ["ColumnName"] (escaped format) | ||
| if name.startswith('["') and name.endswith('"]'): | ||
| return name # Return as is if already properly escaped | ||
| # Remove surrounding spaces | ||
| # Handle mathematical operations (wrap only the column part before operators) | ||
| # Find the position of the first operator or space that separates the column name | ||
| # Handle arithmetic expressions by recursively escaping both sides | ||
| if not is_alias: | ||
| for operator in ["/", "+", "-", "*"]: | ||
| if operator in name: | ||
| # Split the name at the first operator and wrap the left part | ||
| parts = name.split(operator, 1) | ||
| # Remove quotes if they exist at the edges | ||
| col_part = parts[0].strip() | ||
| if col_part.startswith('"') and col_part.endswith('"'): | ||
| col_part = col_part[1:-1].strip() | ||
| col_part = col_part.replace('"', '\\"') | ||
| return f'["{col_part}"] {operator} {parts[1].strip()}' # Wrap the column part | ||
| # No operators found, just wrap the entire name | ||
| # Find operator that's not inside quotes | ||
| pos = KustoKqlCompiler._find_operator_outside_quotes(name, operator) | ||
| if pos != -1: | ||
| left_part = name[:pos].strip() | ||
| right_part = name[pos + 1 :].strip() | ||
| # Recursively escape both sides | ||
| left_escaped = KustoKqlCompiler._escape_and_quote_columns(left_part) | ||
| right_escaped = KustoKqlCompiler._escape_and_quote_columns( | ||
| right_part | ||
| ) | ||
| return f"{left_escaped} {operator} {right_escaped}" | ||
| # No operators found - strip surrounding quotes if present, then wrap | ||
| if name.startswith('"') and name.endswith('"'): | ||
| name = name[1:-1] | ||
| name = name.replace('"', '\\"') | ||
| return f'["{name}"]' | ||
|
|
||
| @staticmethod | ||
| def _find_operator_outside_quotes(text: str, operator: str) -> int: | ||
| """Find position of operator that's not inside quoted strings. Returns -1 if not found.""" | ||
| in_quotes = False | ||
| for i, ch in enumerate(text): | ||
| if ch == '"' and (i == 0 or text[i - 1] != "\\"): | ||
| in_quotes = not in_quotes | ||
| elif ch == operator and not in_quotes: | ||
| return i | ||
| return -1 | ||
|
|
||
| @staticmethod | ||
| def _sql_to_kql_where(where_clause: str) -> str: | ||
| where_clause = where_clause.strip().replace("\n", "") | ||
|
|
@@ -547,7 +564,7 @@ def _is_kql_function(name: str) -> bool: | |
|
|
||
| @staticmethod | ||
| def _is_number_literal(s: str) -> bool: | ||
| pattern = r"^[0-9]+$" | ||
| pattern = r"^\d+(\.\d+)?$" | ||
| return bool(re.match(pattern, s)) | ||
|
|
||
| def _get_most_inner_element(self, clause): | ||
|
|
||
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.
The PR description indicates this should be merged after PR #48, but this diff still includes the summarize/extend reordering change. To keep this PR focused on quote-to-bracket escaping (and reduce merge conflicts), please rebase onto the branch that includes #48 so those unrelated changes drop out of this PR.
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.
Will do once the other PR merges in