Skip to content

Commit b99913f

Browse files
authored
fix: handle proper SQL variable type for operators (#1379)
Handle proper SQL variable type for operators. We have two-stage variable type checks- - With the help of String::parse() to see if it is a float or a string - Based on operator used. For operators like begins with, contains, etc we typecast to String and add proper regex symbols
1 parent f9a07ca commit b99913f

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

src/alerts/alerts_utils.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -439,8 +439,14 @@ fn match_alert_operator(expr: &ConditionConfig) -> Expr {
439439
// if it can be parsed as a number, then parse it
440440
// else keep it as a string
441441
if expr.value.as_ref().is_some_and(|v| !v.is_empty()) {
442-
let value = expr.value.as_ref().unwrap();
443-
let value = NumberOrString::from_string(value.clone());
442+
let string_value = expr
443+
.value
444+
.as_ref()
445+
.unwrap()
446+
.replace("'", "\\'")
447+
.replace('%', "\\%")
448+
.replace('_', "\\_");
449+
let value = NumberOrString::from_string(string_value.clone());
444450

445451
// for maintaining column case
446452
let column = format!(r#""{}""#, expr.column);
@@ -451,28 +457,30 @@ fn match_alert_operator(expr: &ConditionConfig) -> Expr {
451457
WhereConfigOperator::GreaterThan => col(column).gt(lit(value)),
452458
WhereConfigOperator::LessThanOrEqual => col(column).lt_eq(lit(value)),
453459
WhereConfigOperator::GreaterThanOrEqual => col(column).gt_eq(lit(value)),
454-
WhereConfigOperator::ILike => col(column).ilike(lit(value)),
455-
WhereConfigOperator::Contains => col(column).like(lit(value)),
460+
WhereConfigOperator::ILike => col(column).ilike(lit(string_value)),
461+
WhereConfigOperator::Contains => {
462+
col(column).like(lit(format!("%{string_value}%")))
463+
},
456464
WhereConfigOperator::BeginsWith => Expr::BinaryExpr(BinaryExpr::new(
457465
Box::new(col(column)),
458466
Operator::RegexIMatch,
459-
Box::new(lit(format!("^{value}"))),
467+
Box::new(lit(format!("^{string_value}"))),
460468
)),
461469
WhereConfigOperator::EndsWith => Expr::BinaryExpr(BinaryExpr::new(
462470
Box::new(col(column)),
463471
Operator::RegexIMatch,
464-
Box::new(lit(format!("{value}$"))),
472+
Box::new(lit(format!("{string_value}$"))),
465473
)),
466-
WhereConfigOperator::DoesNotContain => col(column).not_ilike(lit(value)),
474+
WhereConfigOperator::DoesNotContain => col(column).not_ilike(lit(string_value)),
467475
WhereConfigOperator::DoesNotBeginWith => Expr::BinaryExpr(BinaryExpr::new(
468476
Box::new(col(column)),
469477
Operator::RegexNotIMatch,
470-
Box::new(lit(format!("^{value}"))),
478+
Box::new(lit(format!("^{string_value}"))),
471479
)),
472480
WhereConfigOperator::DoesNotEndWith => Expr::BinaryExpr(BinaryExpr::new(
473481
Box::new(col(column)),
474482
Operator::RegexNotIMatch,
475-
Box::new(lit(format!("{value}$"))),
483+
Box::new(lit(format!("{string_value}$"))),
476484
)),
477485
_ => unreachable!("value must not be null for operators other than `is null` and `is not null`. Should've been caught in validation")
478486
}

0 commit comments

Comments
 (0)