Skip to content

feat(cubestore): Add XIRR aggregate function to Cube Store #9520

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

Merged
merged 1 commit into from
May 6, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ use datafusion::{
scalar::ScalarValue,
};

// Note: A copy/pasted and minimally(?) modified version of this is in Cubestore in udf_xirr.rs, and you might
// want to update both.

pub const XIRR_UDAF_NAME: &str = "xirr";

/// Creates a XIRR Aggregate UDF.
Expand Down
117 changes: 117 additions & 0 deletions rust/cubestore/cubestore-sql-tests/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ pub fn sql_tests() -> Vec<(&'static str, TestFn)> {
t("hyperloglog_postgres", hyperloglog_postgres),
t("hyperloglog_snowflake", hyperloglog_snowflake),
t("hyperloglog_databricks", hyperloglog_databricks),
t("xirr", xirr),
t(
"aggregate_index_hll_databricks",
aggregate_index_hll_databricks,
Expand Down Expand Up @@ -2802,6 +2803,122 @@ async fn hyperloglog_databricks(service: Box<dyn SqlClient>) {
assert_eq!(to_rows(&r), rows(&[(1, 4), (2, 4), (3, 20)]));
}

async fn xirr(service: Box<dyn SqlClient>) {
// XIRR result may differ between platforms, so we truncate the results with LEFT(_, 10).
let r = service
.exec_query(
r#"
SELECT LEFT(XIRR(payment, date)::varchar, 10) AS xirr
FROM (
SELECT '2014-01-01'::date AS date, -10000.0 AS payment
UNION ALL
SELECT '2014-03-01'::date AS date, 2750.0 AS payment
UNION ALL
SELECT '2014-10-30'::date AS date, 4250.0 AS payment
UNION ALL
SELECT '2015-02-15'::date AS date, 3250.0 AS payment
UNION ALL
SELECT '2015-04-01'::date AS date, 2750.0 AS payment
) AS "t"
"#,
)
.await
.unwrap();

assert_eq!(to_rows(&r), rows(&["0.37485859"]));

let r = service
.exec_query(
r#"
SELECT LEFT(XIRR(payment, date)::varchar, 10) AS xirr
FROM (
SELECT '2014-01-01'::date AS date, -10000.0 AS payment
) AS "t"
WHERE 0 = 1
"#,
)
.await
.unwrap_err();
assert_eq!(r.elide_backtrace(), CubeError::internal("Arrow error: External error: Execution error: A result for XIRR couldn't be determined because the arguments are empty".to_owned()));

let r = service
.exec_query(
r#"
SELECT LEFT(XIRR(payment, date)::varchar, 10) AS xirr
FROM (
SELECT '2014-01-01'::date AS date, 10000.0 AS payment
) AS "t"
"#,
)
.await
.unwrap_err();
assert_eq!(r.elide_backtrace(), CubeError::internal("Arrow error: External error: Execution error: The XIRR function couldn't find a solution".to_owned()));

// --- on_error testing ---

let r = service
.exec_query(
r#"
SELECT LEFT(XIRR(payment, date, 0, NULL::double)::varchar, 10) AS xirr
FROM (
SELECT '2014-01-01'::date AS date, -10000.0 AS payment
UNION ALL
SELECT '2014-03-01'::date AS date, 2750.0 AS payment
UNION ALL
SELECT '2014-10-30'::date AS date, 4250.0 AS payment
UNION ALL
SELECT '2015-02-15'::date AS date, 3250.0 AS payment
UNION ALL
SELECT '2015-04-01'::date AS date, 2750.0 AS payment
) AS "t"
"#,
)
.await
.unwrap();

assert_eq!(to_rows(&r), rows(&["0.37485859"]));

let r = service
.exec_query(
r#"
SELECT LEFT(XIRR(payment, date, 0, NULL::double)::varchar, 10) AS xirr
FROM (
SELECT '2014-01-01'::date AS date, -10000.0 AS payment
) AS "t"
WHERE 0 = 1
"#,
)
.await
.unwrap_err();
assert_eq!(r.elide_backtrace(), CubeError::internal("Arrow error: External error: Execution error: A result for XIRR couldn't be determined because the arguments are empty".to_owned()));

let r = service
.exec_query(
r#"
SELECT LEFT(XIRR(payment, date, 0, NULL::double)::varchar, 10) AS xirr
FROM (
SELECT '2014-01-01'::date AS date, 10000.0 AS payment
) AS "t"
"#,
)
.await
.unwrap();
assert_eq!(to_rows(&r), rows(&[()]));

let r = service
.exec_query(
r#"
SELECT LEFT(XIRR(payment, date, 0, 12345)::varchar, 10) AS xirr
FROM (
SELECT '2014-01-01'::date AS date, 10000.0 AS payment
) AS "t"
"#,
)
.await
.unwrap();
assert_eq!(to_rows(&r), rows(&["12345.0"]));
}

async fn aggregate_index_hll_databricks(service: Box<dyn SqlClient>) {
service.exec_query("CREATE SCHEMA s").await.unwrap();
service
Expand Down
2 changes: 2 additions & 0 deletions rust/cubestore/cubestore/src/queryplanner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub mod now;
pub mod providers;
#[cfg(test)]
mod test_utils;
pub mod udf_xirr;
pub mod udfs;

use crate::cachestore::CacheStore;
Expand Down Expand Up @@ -429,6 +430,7 @@ impl ContextProvider for MetaStoreSchemaProvider {
// TODO: case-insensitive names.
let kind = match name {
"merge" | "MERGE" => CubeAggregateUDFKind::MergeHll,
"xirr" | "XIRR" => CubeAggregateUDFKind::Xirr,
_ => return None,
};
return Some(Arc::new(aggregate_udf_by_kind(kind).descriptor()));
Expand Down
Loading
Loading