Skip to content

Commit b413d38

Browse files
committed
Tighten AGGREGATE token check
1 parent 50ce569 commit b413d38

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

yardstick-rs/src/sql/measures.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,18 @@ pub fn has_aggregate_function(sql: &str) -> bool {
206206
let sql_upper = sql.to_uppercase();
207207
let mut search_pos = 0;
208208

209+
let is_ident_char = |c: char| c.is_alphanumeric() || c == '_';
210+
209211
while let Some(offset) = sql_upper[search_pos..].find("AGGREGATE") {
210212
let start = search_pos + offset;
213+
if start > 0 {
214+
if let Some(prev) = sql_upper[..start].chars().last() {
215+
if is_ident_char(prev) {
216+
search_pos = start + 1;
217+
continue;
218+
}
219+
}
220+
}
211221
let after = &sql_upper[start + "AGGREGATE".len()..];
212222
if after.trim_start().starts_with('(') {
213223
return true;
@@ -3808,6 +3818,8 @@ mod tests {
38083818
fn test_has_aggregate_function() {
38093819
assert!(has_aggregate_function("SELECT AGGREGATE(revenue) FROM foo"));
38103820
assert!(has_aggregate_function("SELECT AGGREGATE (revenue) FROM foo"));
3821+
assert!(!has_aggregate_function("SELECT TOTAL_AGGREGATE(revenue) FROM foo"));
3822+
assert!(!has_aggregate_function("SELECT myaggregate(revenue) FROM foo"));
38113823
assert!(!has_aggregate_function("SELECT SUM(amount) FROM foo"));
38123824
}
38133825

@@ -3829,6 +3841,17 @@ mod tests {
38293841
assert!(cols.is_empty());
38303842
}
38313843

3844+
#[test]
3845+
fn test_extract_dimension_columns_keeps_non_aggregate_suffix() {
3846+
let cols = extract_dimension_columns_from_select(
3847+
"SELECT region, TOTAL_AGGREGATE(revenue) FROM sales_v",
3848+
);
3849+
assert_eq!(
3850+
cols,
3851+
vec!["region".to_string(), "TOTAL_AGGREGATE(revenue)".to_string()]
3852+
);
3853+
}
3854+
38323855
#[test]
38333856
#[serial]
38343857
fn test_process_create_view_basic() {

0 commit comments

Comments
 (0)