Skip to content
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

feat(cubesql): Top-down extractor for rewrites #8694

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions packages/cubejs-backend-native/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/cubejs-schema-compiler/src/adapter/BaseQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -3203,7 +3203,7 @@ export class BaseQuery {
'{% if filter %}\nWHERE {{ filter }}{% endif %}' +
'{% if group_by %}\nGROUP BY {{ group_by }}{% endif %}' +
'{% if order_by %}\nORDER BY {{ order_by | map(attribute=\'expr\') | join(\', \') }}{% endif %}' +
'{% if limit %}\nLIMIT {{ limit }}{% endif %}' +
'{% if limit is not none %}\nLIMIT {{ limit }}{% endif %}' +
'{% if offset %}\nOFFSET {{ offset }}{% endif %}',
group_by_exprs: '{{ group_by | map(attribute=\'index\') | join(\', \') }}',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export class PrestodbQuery extends BaseQuery {
'{% if group_by %} GROUP BY {{ group_by }}{% endif %}' +
'{% if order_by %} ORDER BY {{ order_by | map(attribute=\'expr\') | join(\', \') }}{% endif %}' +
'{% if offset %}\nOFFSET {{ offset }}{% endif %}' +
'{% if limit %}\nLIMIT {{ limit }}{% endif %}';
'{% if limit is not none %}\nLIMIT {{ limit }}{% endif %}';
templates.expressions.extract = 'EXTRACT({{ date_part }} FROM {{ expr }})';
templates.expressions.interval_single_date_part = 'INTERVAL \'{{ num }}\' {{ date_part }}';
templates.expressions.timestamp_literal = 'from_iso8601_timestamp(\'{{ value }}\')';
Expand Down
21 changes: 8 additions & 13 deletions rust/cubesql/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions rust/cubesql/cubesql/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ minijinja = { version = "1", features = ["json", "loader"] }
lru = "0.12.1"
sha2 = "0.10.8"
bigdecimal = "0.4.2"
indexmap = "1.9.3"


[dev-dependencies]
Expand Down
57 changes: 50 additions & 7 deletions rust/cubesql/cubesql/src/compile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16042,7 +16042,11 @@
.wrapped_sql
.unwrap()
.sql;
assert!(sql.contains("\"limit\":1000"));
if Rewriter::top_down_extractor_enabled() {
assert!(sql.contains("LIMIT 1000"));
} else {
assert!(sql.contains("\"limit\":1000"));
}
assert!(sql.contains("% 7"));

let physical_plan = query_plan.as_physical_plan().await.unwrap();
Expand Down Expand Up @@ -17109,7 +17113,7 @@
FROM ({{ from }}) AS {{ from_alias }}
{% if group_by %} GROUP BY {{ group_by | map(attribute='index') | join(', ') }}{% endif %}
{% if order_by %} ORDER BY {{ order_by | map(attribute='expr') | join(', ') }}{% endif %}{% if offset %}
OFFSET {{ offset }}{% endif %}{% if limit %}
OFFSET {{ offset }}{% endif %}{% if limit is not none %}
LIMIT {{ limit }}{% endif %}"#.to_string(),
),
]
Expand Down Expand Up @@ -18220,7 +18224,7 @@
measure(count) AS cnt,
date_trunc('month', order_date) AS dt
FROM KibanaSampleDataEcommerce
WHERE order_date IN (to_timestamp('2019-01-01 00:00:00.000000', 'YYYY-MM-DD HH24:MI:SS.US'))
WHERE date_trunc('month', order_date) IN (to_timestamp('2019-01-01 00:00:00.000000', 'YYYY-MM-DD HH24:MI:SS.US'))
GROUP BY 2
;"#
.to_string(),
Expand All @@ -18238,10 +18242,18 @@
time_dimensions: Some(vec![V1LoadRequestQueryTimeDimension {
dimension: "KibanaSampleDataEcommerce.order_date".to_string(),
granularity: Some("month".to_string()),
date_range: Some(json!(vec![
"2019-01-01T00:00:00.000Z".to_string(),
"2019-01-01T00:00:00.000Z".to_string()
]))
date_range: if Rewriter::top_down_extractor_enabled() {
Some(json!(vec![
"2019-01-01T00:00:00.000Z".to_string(),
"2019-01-31T23:59:59.999Z".to_string()
]))
} else {
// Non-optimal variant with top down extractor disabled
Some(json!(vec![
"2019-01-01 00:00:00.000".to_string(),
"2019-01-31 23:59:59.999".to_string()
]))

Check warning on line 18255 in rust/cubesql/cubesql/src/compile/mod.rs

View check run for this annotation

Codecov / codecov/patch

rust/cubesql/cubesql/src/compile/mod.rs#L18253-L18255

Added lines #L18253 - L18255 were not covered by tests
}
}]),
order: Some(vec![]),
limit: None,
Expand Down Expand Up @@ -18588,4 +18600,35 @@

Ok(())
}

#[tokio::test]
async fn test_wrapper_limit_zero() {
if !Rewriter::sql_push_down_enabled() {
return;
}
init_testing_logger();

let query_plan = convert_select_to_query_plan(
r#"
SELECT MAX(order_date) FROM KibanaSampleDataEcommerce LIMIT 0
"#
.to_string(),
DatabaseProtocol::PostgreSQL,
)
.await;

let logical_plan = query_plan.as_logical_plan();
let sql = logical_plan
.find_cube_scan_wrapper()
.wrapped_sql
.unwrap()
.sql;
assert!(sql.contains("LIMIT 0"));

let physical_plan = query_plan.as_physical_plan().await.unwrap();
println!(
"Physical plan: {}",
displayable(physical_plan.as_ref()).indent()
);
}
}
8 changes: 7 additions & 1 deletion rust/cubesql/cubesql/src/compile/query_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,13 @@ pub trait QueryEngine {
let mut rewriter = Rewriter::new(finalized_graph, cube_ctx.clone());

let result = rewriter
.find_best_plan(root, state.auth_context().unwrap(), qtrace, span_id.clone())
.find_best_plan(
root,
state.auth_context().unwrap(),
qtrace,
span_id.clone(),
self.config_ref().top_down_extractor(),
)
.await
.map_err(|e| match e.cause {
CubeErrorCauseType::Internal(_) => CompilationError::Internal(
Expand Down
Loading
Loading