Skip to content
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 @@ -236,7 +236,7 @@ export class BigqueryQuery extends BaseQuery {
const templates = super.sqlTemplates();
templates.quotes.identifiers = '`';
templates.quotes.escape = '\\`';
templates.functions.DATETRUNC = 'DATETIME_TRUNC(CAST({{ args[1] }} AS DATETIME), {{ date_part }})';
templates.functions.DATETRUNC = 'DATETIME_TRUNC(CAST({{ args[1] }} AS DATETIME), {% if date_part|upper == \'WEEK\' %}{{ \'WEEK(MONDAY)\' }}{% else %}{{ date_part }}{% endif %})';
templates.functions.LOG = 'LOG({{ args_concat }}{% if args[1] is undefined %}, 10{% endif %})';
templates.functions.BTRIM = 'TRIM({{ args_concat }})';
templates.functions.STRPOS = 'STRPOS({{ args_concat }})';
Expand Down
36 changes: 36 additions & 0 deletions rust/cubesql/cubesql/src/compile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14473,6 +14473,42 @@ ORDER BY "source"."str0" ASC
.contains("NOT ("));
}

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

// BigQuery
let query_plan = convert_select_to_query_plan_customized(
"
SELECT DATE_TRUNC('week', k.order_date) AS d
FROM KibanaSampleDataEcommerce AS k
WHERE LOWER(k.customer_gender) = LOWER('unknown')
GROUP BY 1
ORDER BY 1 DESC
"
.to_string(),
DatabaseProtocol::PostgreSQL,
vec![
("functions/DATETRUNC".to_string(), "DATETIME_TRUNC(CAST({{ args[1] }} AS DATETIME), {% if date_part|upper == \'WEEK\' %}{{ \'WEEK(MONDAY)\' }}{% else %}{{ date_part }}{% endif %})".to_string()),
]
)
.await;

let physical_plan = query_plan.as_physical_plan().await.unwrap();
println!(
"Physical plan: {}",
displayable(physical_plan.as_ref()).indent()
);

let logical_plan = query_plan.as_logical_plan();
let sql = logical_plan.find_cube_scan_wrapped_sql().wrapped_sql.sql;
assert!(sql.contains("DATETIME_TRUNC("));
assert!(sql.contains("WEEK(MONDAY)"));
}

#[tokio::test]
async fn test_datediff_push_down() {
if !Rewriter::sql_push_down_enabled() {
Expand Down
Loading