Skip to content

Commit afd6e29

Browse files
committed
Skip literals and comments in subquery_alias_from_item depth tracking
Parentheses inside string literals or comments (e.g. (SELECT ...) || ')' AS year_total) no longer corrupt the depth counter, which previously caused the alias to be missed and the ORDER BY wrapper to not apply.
1 parent 7740e27 commit afd6e29

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

yardstick-rs/src/sql/measures.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5593,12 +5593,46 @@ fn subquery_alias_from_item(item: &str) -> Option<String> {
55935593
return None;
55945594
}
55955595
// Find the last top-level alias (not inside parentheses).
5596+
// Skip string literals and comments so quoted parens don't corrupt depth.
55965597
let bytes = item.as_bytes();
55975598
let mut depth: i32 = 0;
55985599
let mut last_alias_start: Option<usize> = None;
55995600
let mut i = 0;
56005601
while i < bytes.len() {
56015602
match bytes[i] {
5603+
b'\'' => {
5604+
i += 1;
5605+
while i < bytes.len() {
5606+
if bytes[i] == b'\'' {
5607+
if i + 1 < bytes.len() && bytes[i + 1] == b'\'' {
5608+
i += 2;
5609+
} else {
5610+
i += 1;
5611+
break;
5612+
}
5613+
} else {
5614+
i += 1;
5615+
}
5616+
}
5617+
continue;
5618+
}
5619+
b'-' if i + 1 < bytes.len() && bytes[i + 1] == b'-' => {
5620+
while i < bytes.len() && bytes[i] != b'\n' {
5621+
i += 1;
5622+
}
5623+
continue;
5624+
}
5625+
b'/' if i + 1 < bytes.len() && bytes[i + 1] == b'*' => {
5626+
i += 2;
5627+
while i + 1 < bytes.len() {
5628+
if bytes[i] == b'*' && bytes[i + 1] == b'/' {
5629+
i += 2;
5630+
break;
5631+
}
5632+
i += 1;
5633+
}
5634+
continue;
5635+
}
56025636
b'(' => depth += 1,
56035637
b')' => {
56045638
depth -= 1;

0 commit comments

Comments
 (0)