|
16 | 16 | *
|
17 | 17 | */
|
18 | 18 |
|
| 19 | +use std::fmt::Display; |
| 20 | + |
19 | 21 | use arrow_array::{Float64Array, Int64Array, RecordBatch};
|
20 | 22 | use datafusion::{
|
21 | 23 | functions_aggregate::{
|
@@ -339,46 +341,97 @@ fn get_filter_expr(where_clause: &Conditions) -> Expr {
|
339 | 341 | }
|
340 | 342 | }
|
341 | 343 |
|
| 344 | +pub fn get_filter_string(where_clause: &Conditions) -> Result<String, String> { |
| 345 | + match &where_clause.operator { |
| 346 | + Some(op) => match op { |
| 347 | + LogicalOperator::And => { |
| 348 | + let mut exprs = vec![]; |
| 349 | + for condition in &where_clause.condition_config { |
| 350 | + if let Some(value) = &condition.value { |
| 351 | + // ad-hoc error check in case value is some and operator is either `is null` or `is not null` |
| 352 | + if condition.operator.eq(&WhereConfigOperator::IsNull) |
| 353 | + || condition.operator.eq(&WhereConfigOperator::IsNotNull) |
| 354 | + { |
| 355 | + return Err("value must be null when operator is either `is null` or `is not null`" |
| 356 | + .into()); |
| 357 | + } |
| 358 | + let value = NumberOrString::from_string(value.to_owned()); |
| 359 | + match value { |
| 360 | + NumberOrString::Number(val) => exprs.push(format!( |
| 361 | + "\"{}\" {} {}", |
| 362 | + condition.column, condition.operator, val |
| 363 | + )), |
| 364 | + NumberOrString::String(val) => exprs.push(format!( |
| 365 | + "\"{}\" {} '{}'", |
| 366 | + condition.column, |
| 367 | + condition.operator, |
| 368 | + val.replace('\'', "''") |
| 369 | + )), |
| 370 | + } |
| 371 | + } else { |
| 372 | + exprs.push(format!("\"{}\" {}", condition.column, condition.operator)) |
| 373 | + } |
| 374 | + } |
| 375 | + |
| 376 | + Ok(exprs.join(" AND ")) |
| 377 | + } |
| 378 | + _ => Err(String::from("Invalid option 'or', only 'and' is supported")), |
| 379 | + }, |
| 380 | + _ => Err(String::from( |
| 381 | + "Invalid option 'null', only 'and' is supported", |
| 382 | + )), |
| 383 | + } |
| 384 | +} |
| 385 | + |
342 | 386 | fn match_alert_operator(expr: &ConditionConfig) -> Expr {
|
343 | 387 | // the form accepts value as a string
|
344 | 388 | // if it can be parsed as a number, then parse it
|
345 | 389 | // else keep it as a string
|
346 |
| - let value = NumberOrString::from_string(expr.value.clone()); |
347 |
| - |
348 |
| - // for maintaining column case |
349 |
| - let column = format!(r#""{}""#, expr.column); |
350 |
| - match expr.operator { |
351 |
| - WhereConfigOperator::Equal => col(column).eq(lit(value)), |
352 |
| - WhereConfigOperator::NotEqual => col(column).not_eq(lit(value)), |
353 |
| - WhereConfigOperator::LessThan => col(column).lt(lit(value)), |
354 |
| - WhereConfigOperator::GreaterThan => col(column).gt(lit(value)), |
355 |
| - WhereConfigOperator::LessThanOrEqual => col(column).lt_eq(lit(value)), |
356 |
| - WhereConfigOperator::GreaterThanOrEqual => col(column).gt_eq(lit(value)), |
357 |
| - WhereConfigOperator::IsNull => col(column).is_null(), |
358 |
| - WhereConfigOperator::IsNotNull => col(column).is_not_null(), |
359 |
| - WhereConfigOperator::ILike => col(column).ilike(lit(&expr.value)), |
360 |
| - WhereConfigOperator::Contains => col(column).like(lit(&expr.value)), |
361 |
| - WhereConfigOperator::BeginsWith => Expr::BinaryExpr(BinaryExpr::new( |
362 |
| - Box::new(col(column)), |
363 |
| - Operator::RegexIMatch, |
364 |
| - Box::new(lit(format!("^{}", expr.value))), |
365 |
| - )), |
366 |
| - WhereConfigOperator::EndsWith => Expr::BinaryExpr(BinaryExpr::new( |
367 |
| - Box::new(col(column)), |
368 |
| - Operator::RegexIMatch, |
369 |
| - Box::new(lit(format!("{}$", expr.value))), |
370 |
| - )), |
371 |
| - WhereConfigOperator::DoesNotContain => col(column).not_ilike(lit(&expr.value)), |
372 |
| - WhereConfigOperator::DoesNotBeginWith => Expr::BinaryExpr(BinaryExpr::new( |
373 |
| - Box::new(col(column)), |
374 |
| - Operator::RegexNotIMatch, |
375 |
| - Box::new(lit(format!("^{}", expr.value))), |
376 |
| - )), |
377 |
| - WhereConfigOperator::DoesNotEndWith => Expr::BinaryExpr(BinaryExpr::new( |
378 |
| - Box::new(col(column)), |
379 |
| - Operator::RegexNotIMatch, |
380 |
| - Box::new(lit(format!("{}$", expr.value))), |
381 |
| - )), |
| 390 | + if let Some(value) = &expr.value { |
| 391 | + let value = NumberOrString::from_string(value.clone()); |
| 392 | + |
| 393 | + // for maintaining column case |
| 394 | + let column = format!(r#""{}""#, expr.column); |
| 395 | + match expr.operator { |
| 396 | + WhereConfigOperator::Equal => col(column).eq(lit(value)), |
| 397 | + WhereConfigOperator::NotEqual => col(column).not_eq(lit(value)), |
| 398 | + WhereConfigOperator::LessThan => col(column).lt(lit(value)), |
| 399 | + WhereConfigOperator::GreaterThan => col(column).gt(lit(value)), |
| 400 | + WhereConfigOperator::LessThanOrEqual => col(column).lt_eq(lit(value)), |
| 401 | + WhereConfigOperator::GreaterThanOrEqual => col(column).gt_eq(lit(value)), |
| 402 | + WhereConfigOperator::ILike => col(column).ilike(lit(value)), |
| 403 | + WhereConfigOperator::Contains => col(column).like(lit(value)), |
| 404 | + WhereConfigOperator::BeginsWith => Expr::BinaryExpr(BinaryExpr::new( |
| 405 | + Box::new(col(column)), |
| 406 | + Operator::RegexIMatch, |
| 407 | + Box::new(lit(format!("^{}", value))), |
| 408 | + )), |
| 409 | + WhereConfigOperator::EndsWith => Expr::BinaryExpr(BinaryExpr::new( |
| 410 | + Box::new(col(column)), |
| 411 | + Operator::RegexIMatch, |
| 412 | + Box::new(lit(format!("{}$", value))), |
| 413 | + )), |
| 414 | + WhereConfigOperator::DoesNotContain => col(column).not_ilike(lit(value)), |
| 415 | + WhereConfigOperator::DoesNotBeginWith => Expr::BinaryExpr(BinaryExpr::new( |
| 416 | + Box::new(col(column)), |
| 417 | + Operator::RegexNotIMatch, |
| 418 | + Box::new(lit(format!("^{}", value))), |
| 419 | + )), |
| 420 | + WhereConfigOperator::DoesNotEndWith => Expr::BinaryExpr(BinaryExpr::new( |
| 421 | + Box::new(col(column)), |
| 422 | + Operator::RegexNotIMatch, |
| 423 | + Box::new(lit(format!("{}$", value))), |
| 424 | + )), |
| 425 | + _ => unreachable!("value must not be null for operators other than `is null` and `is not null`. Should've been caught in validation") |
| 426 | + } |
| 427 | + } else { |
| 428 | + // for maintaining column case |
| 429 | + let column = format!(r#""{}""#, expr.column); |
| 430 | + match expr.operator { |
| 431 | + WhereConfigOperator::IsNull => col(column).is_null(), |
| 432 | + WhereConfigOperator::IsNotNull => col(column).is_not_null(), |
| 433 | + _ => unreachable!("value must be null for `is null` and `is not null`. Should've been caught in validation") |
| 434 | + } |
382 | 435 | }
|
383 | 436 | }
|
384 | 437 |
|
@@ -417,3 +470,12 @@ impl NumberOrString {
|
417 | 470 | }
|
418 | 471 | }
|
419 | 472 | }
|
| 473 | + |
| 474 | +impl Display for NumberOrString { |
| 475 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 476 | + match self { |
| 477 | + NumberOrString::Number(v) => write!(f, "{v}"), |
| 478 | + NumberOrString::String(v) => write!(f, "{v}"), |
| 479 | + } |
| 480 | + } |
| 481 | +} |
0 commit comments