Skip to content

Commit 6c8564f

Browse files
authored
fix(schema-compiler): Fix BigQuery DATETIME_TRUNC() week processing (#9380)
* fix(schema-compiler): Fix BigQuery DATETIME_TRUNC() week processing * add tests
1 parent 29fdd61 commit 6c8564f

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

packages/cubejs-schema-compiler/src/adapter/BigqueryQuery.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ export class BigqueryQuery extends BaseQuery {
236236
const templates = super.sqlTemplates();
237237
templates.quotes.identifiers = '`';
238238
templates.quotes.escape = '\\`';
239-
templates.functions.DATETRUNC = 'DATETIME_TRUNC(CAST({{ args[1] }} AS DATETIME), {{ date_part }})';
239+
templates.functions.DATETRUNC = 'DATETIME_TRUNC(CAST({{ args[1] }} AS DATETIME), {% if date_part|upper == \'WEEK\' %}{{ \'WEEK(MONDAY)\' }}{% else %}{{ date_part }}{% endif %})';
240240
templates.functions.LOG = 'LOG({{ args_concat }}{% if args[1] is undefined %}, 10{% endif %})';
241241
templates.functions.BTRIM = 'TRIM({{ args_concat }})';
242242
templates.functions.STRPOS = 'STRPOS({{ args_concat }})';

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14473,6 +14473,42 @@ ORDER BY "source"."str0" ASC
1447314473
.contains("NOT ("));
1447414474
}
1447514475

14476+
#[tokio::test]
14477+
async fn test_datetrunc_push_down() {
14478+
if !Rewriter::sql_push_down_enabled() {
14479+
return;
14480+
}
14481+
init_testing_logger();
14482+
14483+
// BigQuery
14484+
let query_plan = convert_select_to_query_plan_customized(
14485+
"
14486+
SELECT DATE_TRUNC('week', k.order_date) AS d
14487+
FROM KibanaSampleDataEcommerce AS k
14488+
WHERE LOWER(k.customer_gender) = LOWER('unknown')
14489+
GROUP BY 1
14490+
ORDER BY 1 DESC
14491+
"
14492+
.to_string(),
14493+
DatabaseProtocol::PostgreSQL,
14494+
vec![
14495+
("functions/DATETRUNC".to_string(), "DATETIME_TRUNC(CAST({{ args[1] }} AS DATETIME), {% if date_part|upper == \'WEEK\' %}{{ \'WEEK(MONDAY)\' }}{% else %}{{ date_part }}{% endif %})".to_string()),
14496+
]
14497+
)
14498+
.await;
14499+
14500+
let physical_plan = query_plan.as_physical_plan().await.unwrap();
14501+
println!(
14502+
"Physical plan: {}",
14503+
displayable(physical_plan.as_ref()).indent()
14504+
);
14505+
14506+
let logical_plan = query_plan.as_logical_plan();
14507+
let sql = logical_plan.find_cube_scan_wrapped_sql().wrapped_sql.sql;
14508+
assert!(sql.contains("DATETIME_TRUNC("));
14509+
assert!(sql.contains("WEEK(MONDAY)"));
14510+
}
14511+
1447614512
#[tokio::test]
1447714513
async fn test_datediff_push_down() {
1447814514
if !Rewriter::sql_push_down_enabled() {

0 commit comments

Comments
 (0)