Skip to content

Commit 693d7d0

Browse files
committed
feat(cubesql): Top-down extractor for rewrites
1 parent 8149d7b commit 693d7d0

File tree

10 files changed

+650
-216
lines changed

10 files changed

+650
-216
lines changed

packages/cubejs-backend-native/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/cubenativeutils/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/cubesql/Cargo.lock

Lines changed: 8 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/cubesql/cubesql/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ minijinja = { version = "1", features = ["json", "loader"] }
5858
lru = "0.12.1"
5959
sha2 = "0.10.8"
6060
bigdecimal = "0.4.2"
61+
indexmap = "1.9.3"
6162

6263

6364
[dev-dependencies]

rust/cubesql/cubesql/src/compile/mod.rs

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14369,7 +14369,11 @@ ORDER BY "source"."str0" ASC
1436914369
.wrapped_sql
1437014370
.unwrap()
1437114371
.sql;
14372-
assert!(sql.contains("\"limit\":1000"));
14372+
if Rewriter::top_down_extractor_enabled() {
14373+
assert!(sql.contains("LIMIT 1000"));
14374+
} else {
14375+
assert!(sql.contains("\"limit\":1000"));
14376+
}
1437314377
assert!(sql.contains("% 7"));
1437414378

1437514379
let physical_plan = query_plan.as_physical_plan().await.unwrap();
@@ -16547,7 +16551,7 @@ LIMIT {{ limit }}{% endif %}"#.to_string(),
1654716551
measure(count) AS cnt,
1654816552
date_trunc('month', order_date) AS dt
1654916553
FROM KibanaSampleDataEcommerce
16550-
WHERE order_date IN (to_timestamp('2019-01-01 00:00:00.000000', 'YYYY-MM-DD HH24:MI:SS.US'))
16554+
WHERE date_trunc('month', order_date) IN (to_timestamp('2019-01-01 00:00:00.000000', 'YYYY-MM-DD HH24:MI:SS.US'))
1655116555
GROUP BY 2
1655216556
;"#
1655316557
.to_string(),
@@ -16565,10 +16569,18 @@ LIMIT {{ limit }}{% endif %}"#.to_string(),
1656516569
time_dimensions: Some(vec![V1LoadRequestQueryTimeDimension {
1656616570
dimension: "KibanaSampleDataEcommerce.order_date".to_string(),
1656716571
granularity: Some("month".to_string()),
16568-
date_range: Some(json!(vec![
16569-
"2019-01-01T00:00:00.000Z".to_string(),
16570-
"2019-01-01T00:00:00.000Z".to_string()
16571-
]))
16572+
date_range: if Rewriter::top_down_extractor_enabled() {
16573+
Some(json!(vec![
16574+
"2019-01-01T00:00:00.000Z".to_string(),
16575+
"2019-01-31T23:59:59.999Z".to_string()
16576+
]))
16577+
} else {
16578+
// Non-optimal variant with top down extractor disabled
16579+
Some(json!(vec![
16580+
"2019-01-01 00:00:00.000".to_string(),
16581+
"2019-01-31 23:59:59.999".to_string()
16582+
]))
16583+
}
1657216584
}]),
1657316585
order: Some(vec![]),
1657416586
limit: None,
@@ -16915,4 +16927,35 @@ LIMIT {{ limit }}{% endif %}"#.to_string(),
1691516927

1691616928
Ok(())
1691716929
}
16930+
16931+
#[tokio::test]
16932+
async fn test_wrapper_limit_zero() {
16933+
if !Rewriter::sql_push_down_enabled() {
16934+
return;
16935+
}
16936+
init_testing_logger();
16937+
16938+
let query_plan = convert_select_to_query_plan(
16939+
r#"
16940+
SELECT MAX(order_date) FROM KibanaSampleDataEcommerce LIMIT 0
16941+
"#
16942+
.to_string(),
16943+
DatabaseProtocol::PostgreSQL,
16944+
)
16945+
.await;
16946+
16947+
let logical_plan = query_plan.as_logical_plan();
16948+
let sql = logical_plan
16949+
.find_cube_scan_wrapper()
16950+
.wrapped_sql
16951+
.unwrap()
16952+
.sql;
16953+
assert!(sql.contains("LIMIT 0"));
16954+
16955+
let physical_plan = query_plan.as_physical_plan().await.unwrap();
16956+
println!(
16957+
"Physical plan: {}",
16958+
displayable(physical_plan.as_ref()).indent()
16959+
);
16960+
}
1691816961
}

rust/cubesql/cubesql/src/compile/query_engine.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,13 @@ pub trait QueryEngine {
190190
let mut rewriter = Rewriter::new(finalized_graph, cube_ctx.clone());
191191

192192
let result = rewriter
193-
.find_best_plan(root, state.auth_context().unwrap(), qtrace, span_id.clone())
193+
.find_best_plan(
194+
root,
195+
state.auth_context().unwrap(),
196+
qtrace,
197+
span_id.clone(),
198+
self.config_ref().top_down_extractor(),
199+
)
194200
.await
195201
.map_err(|e| match e.cause {
196202
CubeErrorCauseType::Internal(_) => CompilationError::Internal(

0 commit comments

Comments
 (0)