Skip to content

Support round() function with two parameters #180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: cubesql-3-04-2022
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ jobs:
steps:
- uses: actions/checkout@v2
- name: Cache Cargo
uses: actions/cache@v2
uses: actions/cache@v3
with:
# these represent dependencies downloaded by cargo
# and thus do not depend on the OS, arch nor rust version.
path: /github/home/.cargo
key: cargo-cache-
- name: Cache Rust dependencies
uses: actions/cache@v2
uses: actions/cache@v3
with:
# these represent compiled steps of both dependencies and arrow
# and thus are specific for a particular OS, arch and rust version.
Expand Down Expand Up @@ -103,13 +103,13 @@ jobs:
with:
submodules: true
- name: Cache Cargo
uses: actions/cache@v2
uses: actions/cache@v3
with:
path: /github/home/.cargo
# this key equals the ones on `linux-build-lib` for re-use
key: cargo-cache-
- name: Cache Rust dependencies
uses: actions/cache@v2
uses: actions/cache@v3
with:
path: /github/home/target
# this key equals the ones on `linux-build-lib` for re-use
Expand Down Expand Up @@ -250,13 +250,13 @@ jobs:
# with:
# submodules: true
# - name: Cache Cargo
# uses: actions/cache@v2
# uses: actions/cache@v3
# with:
# path: /github/home/.cargo
# # this key equals the ones on `linux-build-lib` for re-use
# key: cargo-cache-
# - name: Cache Rust dependencies
# uses: actions/cache@v2
# uses: actions/cache@v3
# with:
# path: /github/home/target
# # this key equals the ones on `linux-build-lib` for re-use
Expand Down Expand Up @@ -315,13 +315,13 @@ jobs:
with:
submodules: true
- name: Cache Cargo
uses: actions/cache@v2
uses: actions/cache@v3
with:
path: /github/home/.cargo
# this key equals the ones on `linux-build-lib` for re-use
key: cargo-cache-
- name: Cache Rust dependencies
uses: actions/cache@v2
uses: actions/cache@v3
with:
path: /github/home/target
# this key equals the ones on `linux-build-lib` for re-use
Expand Down Expand Up @@ -360,13 +360,13 @@ jobs:
with:
submodules: true
- name: Cache Cargo
uses: actions/cache@v2
uses: actions/cache@v3
with:
path: /github/home/.cargo
# this key equals the ones on `linux-build-lib` for re-use
key: cargo-cache-
- name: Cache Rust dependencies
uses: actions/cache@v2
uses: actions/cache@v3
with:
path: /github/home/target
# this key equals the ones on `linux-build-lib` for re-use
Expand Down Expand Up @@ -419,13 +419,13 @@ jobs:
# with:
# submodules: true
# - name: Cache Cargo
# uses: actions/cache@v2
# uses: actions/cache@v3
# with:
# path: /github/home/.cargo
# # this key equals the ones on `linux-build-lib` for re-use
# key: cargo-cache-
# - name: Cache Rust dependencies
# uses: actions/cache@v2
# uses: actions/cache@v3
# with:
# path: /github/home/target
# # this key equals the ones on `linux-build-lib` for re-use
Expand Down Expand Up @@ -466,13 +466,13 @@ jobs:
# with:
# submodules: true
# - name: Cache Cargo
# uses: actions/cache@v2
# uses: actions/cache@v3
# with:
# path: /home/runner/.cargo
# # this key is not equal because the user is different than on a container (runner vs github)
# key: cargo-coverage-cache-
# - name: Cache Rust dependencies
# uses: actions/cache@v2
# uses: actions/cache@v3
# with:
# path: /home/runner/target
# # this key is not equal because coverage uses different compilation flags.
Expand Down
12 changes: 6 additions & 6 deletions datafusion/core/src/logical_plan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ pub use expr::{
count, count_distinct, create_udaf, create_udf, create_udtf, date_part, date_trunc,
digest, exp, exprlist_to_fields, exprlist_to_fields_from_schema, floor, in_list,
initcap, left, length, lit, lit_timestamp_nano, ln, log10, log2, lower, lpad, ltrim,
max, md5, min, now, now_expr, nullif, octet_length, or, pi, random, regexp_match,
regexp_replace, repeat, replace, reverse, right, round, rpad, rtrim, sha224, sha256,
sha384, sha512, signum, sin, split_part, sqrt, starts_with, strpos, substr, sum, tan,
to_hex, to_timestamp_micros, to_timestamp_millis, to_timestamp_seconds, translate,
trim, trunc, unalias, upper, when, Column, Expr, ExprSchema, GroupingSet, Like,
Literal,
max, md5, min, now, now_expr, nullif, octet_length, or, pi, power, random,
regexp_match, regexp_replace, repeat, replace, reverse, right, round, rpad, rtrim,
sha224, sha256, sha384, sha512, signum, sin, split_part, sqrt, starts_with, strpos,
substr, sum, tan, to_hex, to_timestamp_micros, to_timestamp_millis,
to_timestamp_seconds, translate, trim, trunc, unalias, upper, when, Column, Expr,
ExprSchema, GroupingSet, Like, Literal,
};
pub use expr_rewriter::{
normalize_col, normalize_cols, replace_col, replace_col_to_expr,
Expand Down
19 changes: 11 additions & 8 deletions datafusion/core/src/optimizer/projection_drop_out.rs
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ mod tests {
)?
.project_with_alias(
vec![
round(col("id")).alias("first"),
round(vec![col("id")]).alias("first"),
col("n").alias("second"),
lit(2).alias("third"),
],
Expand All @@ -649,7 +649,7 @@ mod tests {
// select * from (select id first, a second, 2 third from (select round(a) id, 1 num from table) a) x;
let plan = LogicalPlanBuilder::from(table_scan)
.project_with_alias(
vec![round(col("a")).alias("id"), lit(1).alias("n")],
vec![round(vec![col("a")]).alias("id"), lit(1).alias("n")],
Some("a".to_string()),
)?
.project_with_alias(
Expand Down Expand Up @@ -748,7 +748,7 @@ mod tests {
)?
.project_with_alias(
vec![
round(col("id")).alias("first"),
round(vec![col("id")]).alias("first"),
col("n").alias("second"),
lit(2).alias("third"),
],
Expand Down Expand Up @@ -826,7 +826,10 @@ mod tests {
let plan = LogicalPlanBuilder::from(table_scan)
.project_with_alias(vec![col("a").alias("id")], Some("a".to_string()))?
.project_with_alias(
vec![round(col("id")).alias("first"), lit(2).alias("second")],
vec![
round(vec![col("id")]).alias("first"),
lit(2).alias("second"),
],
Some("b".to_string()),
)?
.sort(vec![col("first")])?
Expand Down Expand Up @@ -1019,7 +1022,7 @@ mod tests {
.project_with_alias(vec![col("a").alias("num")], Some("a".to_string()))?
.project_with_alias(vec![col("num")], Some("b".to_string()))?
.filter(col("num").gt(lit(0)))?
.aggregate(vec![round(col("num"))], Vec::<Expr>::new())?
.aggregate(vec![round(vec![col("num")])], Vec::<Expr>::new())?
.project(vec![col("Round(b.num)")])?
.sort(vec![col("Round(b.num)")])?
.build()?;
Expand All @@ -1044,7 +1047,7 @@ mod tests {
let plan = LogicalPlanBuilder::from(table_scan.clone())
.project_with_alias(vec![col("a").alias("num")], Some("a".to_string()))?
.project_with_alias(vec![col("num")], Some("b".to_string()))?
.aggregate(vec![round(col("num"))], Vec::<Expr>::new())?
.aggregate(vec![round(vec![col("num")])], Vec::<Expr>::new())?
.project(vec![col("Round(b.num)")])?
.sort(vec![col("Round(b.num)")])?
.build()?;
Expand All @@ -1061,7 +1064,7 @@ mod tests {
let plan = LogicalPlanBuilder::from(table_scan)
.project_with_alias(vec![col("a").alias("num")], Some("a".to_string()))?
.project_with_alias(vec![col("num")], Some("b".to_string()))?
.aggregate(vec![round(col("num"))], Vec::<Expr>::new())?
.aggregate(vec![round(vec![col("num")])], Vec::<Expr>::new())?
.project(vec![col("Round(b.num)")])?
.sort(vec![col("Round(b.num)")])?
.project_with_alias(vec![col("Round(b.num)")], Some("x".to_string()))?
Expand Down Expand Up @@ -1099,7 +1102,7 @@ mod tests {
.project_with_alias(vec![col("num")], Some("x".to_string()))?
.join(&right, JoinType::Left, (vec!["num"], vec!["a"]))?
.project_with_alias(
vec![col("num"), col("a"), round(col("num"))],
vec![col("num"), col("a"), round(vec![col("num")])],
Some("b".to_string()),
)?
.build()?;
Expand Down
8 changes: 7 additions & 1 deletion datafusion/core/src/physical_plan/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,12 +306,18 @@ pub fn create_physical_fun(
BuiltinScalarFunction::Log10 => Arc::new(math_expressions::log10),
BuiltinScalarFunction::Log2 => Arc::new(math_expressions::log2),
BuiltinScalarFunction::Random => Arc::new(math_expressions::random),
BuiltinScalarFunction::Round => Arc::new(math_expressions::round),
BuiltinScalarFunction::Round => {
Arc::new(|args| make_scalar_function(math_expressions::round)(args))
}
BuiltinScalarFunction::Signum => Arc::new(math_expressions::signum),
BuiltinScalarFunction::Sin => Arc::new(math_expressions::sin),
BuiltinScalarFunction::Sqrt => Arc::new(math_expressions::sqrt),
BuiltinScalarFunction::Tan => Arc::new(math_expressions::tan),
BuiltinScalarFunction::Trunc => Arc::new(math_expressions::trunc),
BuiltinScalarFunction::Power => {
Arc::new(|args| make_scalar_function(math_expressions::power)(args))
}

BuiltinScalarFunction::Pi => Arc::new(math_expressions::pi),
// string functions
BuiltinScalarFunction::MakeArray => Arc::new(array_expressions::array),
Expand Down
126 changes: 126 additions & 0 deletions datafusion/core/tests/sql/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,132 @@ async fn case_builtin_math_expression() {
}
}

#[tokio::test]
async fn test_power() -> Result<()> {
let schema = Arc::new(Schema::new(vec![
Field::new("i32", DataType::Int16, true),
Field::new("i64", DataType::Int64, true),
Field::new("f32", DataType::Float32, true),
Field::new("f64", DataType::Float64, true),
]));

let data = RecordBatch::try_new(
schema.clone(),
vec![
Arc::new(Int16Array::from(vec![
Some(2),
Some(5),
Some(0),
Some(-14),
None,
])),
Arc::new(Int64Array::from(vec![
Some(2),
Some(5),
Some(0),
Some(-14),
None,
])),
Arc::new(Float32Array::from(vec![
Some(1.0),
Some(2.5),
Some(0.0),
Some(-14.5),
None,
])),
Arc::new(Float64Array::from(vec![
Some(1.0),
Some(2.5),
Some(0.0),
Some(-14.5),
None,
])),
],
)?;

let table = MemTable::try_new(schema, vec![vec![data]])?;

let ctx = SessionContext::new();
ctx.register_table("test", Arc::new(table))?;
let sql = r"SELECT power(i32, exp_i) as power_i32,
power(i64, exp_f) as power_i64,
power(f32, exp_i) as power_f32,
power(f64, exp_f) as power_f64,
power(2, 3) as power_int_scalar,
power(2.5, 3.0) as power_float_scalar
FROM (select test.*, 3 as exp_i, 3.0 as exp_f from test) a";
let actual = execute_to_batches(&ctx, sql).await;
let expected = vec![
"+-----------+-----------+-----------+-----------+------------------+--------------------+",
"| power_i32 | power_i64 | power_f32 | power_f64 | power_int_scalar | power_float_scalar |",
"+-----------+-----------+-----------+-----------+------------------+--------------------+",
"| 8 | 8 | 1 | 1 | 8 | 15.625 |",
"| 125 | 125 | 15.625 | 15.625 | 8 | 15.625 |",
"| 0 | 0 | 0 | 0 | 8 | 15.625 |",
"| -2744 | -2744 | -3048.625 | -3048.625 | 8 | 15.625 |",
"| | | | | 8 | 15.625 |",
"+-----------+-----------+-----------+-----------+------------------+--------------------+",
];
assert_batches_eq!(expected, &actual);
//dbg!(actual[0].schema().fields());
assert_eq!(
actual[0]
.schema()
.field_with_name("power_i32")
.unwrap()
.data_type()
.to_owned(),
DataType::Int64
);
assert_eq!(
actual[0]
.schema()
.field_with_name("power_i64")
.unwrap()
.data_type()
.to_owned(),
DataType::Float64
);
assert_eq!(
actual[0]
.schema()
.field_with_name("power_f32")
.unwrap()
.data_type()
.to_owned(),
DataType::Float64
);
assert_eq!(
actual[0]
.schema()
.field_with_name("power_f64")
.unwrap()
.data_type()
.to_owned(),
DataType::Float64
);
assert_eq!(
actual[0]
.schema()
.field_with_name("power_int_scalar")
.unwrap()
.data_type()
.to_owned(),
DataType::Int64
);
assert_eq!(
actual[0]
.schema()
.field_with_name("power_float_scalar")
.unwrap()
.data_type()
.to_owned(),
DataType::Float64
);

Ok(())
}

// #[tokio::test]
// async fn case_sensitive_identifiers_aggregates() {
// let ctx = SessionContext::new();
Expand Down
4 changes: 4 additions & 0 deletions datafusion/expr/src/built_in_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ pub enum BuiltinScalarFunction {
Log10,
/// log2
Log2,
/// power
Power,
/// pi
Pi,
/// round
Expand Down Expand Up @@ -196,6 +198,7 @@ impl BuiltinScalarFunction {
BuiltinScalarFunction::Log => Volatility::Immutable,
BuiltinScalarFunction::Log10 => Volatility::Immutable,
BuiltinScalarFunction::Log2 => Volatility::Immutable,
BuiltinScalarFunction::Power => Volatility::Immutable,
BuiltinScalarFunction::Pi => Volatility::Immutable,
BuiltinScalarFunction::Round => Volatility::Immutable,
BuiltinScalarFunction::Signum => Volatility::Immutable,
Expand Down Expand Up @@ -284,6 +287,7 @@ impl FromStr for BuiltinScalarFunction {
"log" => BuiltinScalarFunction::Log,
"log10" => BuiltinScalarFunction::Log10,
"log2" => BuiltinScalarFunction::Log2,
"power" => BuiltinScalarFunction::Power,
"pi" => BuiltinScalarFunction::Pi,
"round" => BuiltinScalarFunction::Round,
"signum" => BuiltinScalarFunction::Signum,
Expand Down
Loading