Skip to content

Commit 53a2704

Browse files
committed
Skip comments in keyword scan
1 parent 565b715 commit 53a2704

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

test/sql/measures.test

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,28 @@ from
5959
2023 EU 225.0
6060
2023 US 225.0
6161

62+
# FROM inside a line comment should be ignored
63+
query IIR rowsort
64+
SEMANTIC SELECT year, region, AGGREGATE(revenue) AT (ALL region) AS year_total
65+
-- from sales_v
66+
FROM sales_v;
67+
----
68+
2022 EU 150.0
69+
2022 US 150.0
70+
2023 EU 225.0
71+
2023 US 225.0
72+
73+
# FROM inside a block comment should be ignored
74+
query IIR rowsort
75+
SEMANTIC SELECT year, region, AGGREGATE(revenue) AT (ALL region) AS year_total
76+
/* from sales_v */
77+
FROM sales_v;
78+
----
79+
2022 EU 150.0
80+
2022 US 150.0
81+
2023 EU 225.0
82+
2023 US 225.0
83+
6284
# =============================================================================
6385
# Test: AT modifiers without SEMANTIC prefix
6486
# =============================================================================

yardstick-rs/src/sql/measures.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,11 +697,31 @@ fn find_top_level_keyword(sql: &str, keyword: &str, start: usize) -> Option<usiz
697697
let mut in_double = false;
698698
let mut in_backtick = false;
699699
let mut in_bracket = false;
700+
let mut in_line_comment = false;
701+
let mut in_block_comment = false;
700702

701703
let mut i = start;
702704
while i < bytes.len() {
703705
let c = bytes[i] as char;
704706

707+
if in_line_comment {
708+
if c == '\n' || c == '\r' {
709+
in_line_comment = false;
710+
}
711+
i += 1;
712+
continue;
713+
}
714+
715+
if in_block_comment {
716+
if c == '*' && i + 1 < bytes.len() && bytes[i + 1] as char == '/' {
717+
in_block_comment = false;
718+
i += 2;
719+
continue;
720+
}
721+
i += 1;
722+
continue;
723+
}
724+
705725
if in_single {
706726
if c == '\'' {
707727
if i + 1 < bytes.len() && bytes[i + 1] as char == '\'' {
@@ -738,6 +758,18 @@ fn find_top_level_keyword(sql: &str, keyword: &str, start: usize) -> Option<usiz
738758
continue;
739759
}
740760

761+
if c == '-' && i + 1 < bytes.len() && bytes[i + 1] as char == '-' {
762+
in_line_comment = true;
763+
i += 2;
764+
continue;
765+
}
766+
767+
if c == '/' && i + 1 < bytes.len() && bytes[i + 1] as char == '*' {
768+
in_block_comment = true;
769+
i += 2;
770+
continue;
771+
}
772+
741773
match c {
742774
'\'' => {
743775
in_single = true;

0 commit comments

Comments
 (0)