Skip to content

Commit

Permalink
test: add unit test for converting expr to scalar value
Browse files Browse the repository at this point in the history
  • Loading branch information
CookiePieWw committed Jun 16, 2024
1 parent 770204d commit 0b72305
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/servers/src/mysql/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ impl MysqlInstanceShim {
}

/// Save query and logical plan, return the unique key
fn save_plan(&self, plan: SqlPlan, stmt_key: String) -> () {
fn save_plan(&self, plan: SqlPlan, stmt_key: String) {
let mut prepared_stmts = self.prepared_stmts.write();
let _ = prepared_stmts.insert(stmt_key, plan);
}
Expand Down
41 changes: 41 additions & 0 deletions src/servers/src/mysql/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,4 +287,45 @@ mod tests {
};
assert_eq!("SELECT from AS demo WHERE host = $1 AND idc IN (SELECT idc FROM idcs WHERE name = $2) AND cpu > $3", select.inner.to_string());
}

#[test]
fn test_convert_expr_to_scalar_value() {
let expr = Expr::Value(ValueExpr::Number("123".to_string(), false));
let t = ConcreteDataType::int32_datatype();
let v = convert_expr_to_scalar_value(&expr, &t).unwrap();
assert_eq!(ScalarValue::Int32(Some(123)), v);

let expr = Expr::Value(ValueExpr::Number("123.456789".to_string(), false));
let t = ConcreteDataType::float64_datatype();
let v = convert_expr_to_scalar_value(&expr, &t).unwrap();
assert_eq!(ScalarValue::Float64(Some(123.456789)), v);

let expr = Expr::Value(ValueExpr::SingleQuotedString("2001-01-02".to_string()));
let t = ConcreteDataType::date_datatype();
let v = convert_expr_to_scalar_value(&expr, &t).unwrap();
let scalar_v = ScalarValue::Utf8(Some("2001-01-02".to_string()))
.cast_to(&arrow_schema::DataType::Date32)
.unwrap();
assert_eq!(scalar_v, v);

let expr = Expr::Value(ValueExpr::SingleQuotedString(
"2001-01-02 03:04:05".to_string(),
));
let t = ConcreteDataType::datetime_datatype();
let v = convert_expr_to_scalar_value(&expr, &t).unwrap();
let scalar_v = ScalarValue::Utf8(Some("2001-01-02 03:04:05".to_string()))
.cast_to(&arrow_schema::DataType::Date64)
.unwrap();
assert_eq!(scalar_v, v);

let expr = Expr::Value(ValueExpr::SingleQuotedString("hello".to_string()));
let t = ConcreteDataType::string_datatype();
let v = convert_expr_to_scalar_value(&expr, &t).unwrap();
assert_eq!(ScalarValue::Utf8(Some("hello".to_string())), v);

let expr = Expr::Value(ValueExpr::Null);
let t = ConcreteDataType::time_microsecond_datatype();
let v = convert_expr_to_scalar_value(&expr, &t).unwrap();
assert_eq!(ScalarValue::Time64Microsecond(None), v);
}
}

0 comments on commit 0b72305

Please sign in to comment.