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

fix(tesseract): Don't generate COALESCE with single argument #9300

Merged
merged 2 commits into from
Mar 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 @@ -714,8 +714,6 @@ SELECT 1 AS revenue, cast('2024-01-01' AS timestamp) as time UNION ALL
await compiler.compile();
const query = new PostgresQuery({ joinGraph, cubeEvaluator, compiler }, q);

// console.log(query.buildSqlAndParams());

const res = await dbRunner.testQuery(query.buildSqlAndParams());
console.log(JSON.stringify(res));

Expand Down Expand Up @@ -3379,6 +3377,45 @@ SELECT 1 AS revenue, cast('2024-01-01' AS timestamp) as time UNION ALL
});
});

it('don\'t use COALESCE with single argument', async () => {
if (!getEnv('nativeSqlPlanner')) {
return;
}
await compiler.compile();

const query = new PostgresQuery({ joinGraph, cubeEvaluator, compiler }, {
measures: [
'visitors.visitor_revenue'
],
dimensions: [
'visitors.source'
],
timeDimensions: [],
timezone: 'America/Los_Angeles',
filters: [{
dimension: 'visitor_checkins.source',
operator: 'equals',
values: ['google']
}],
order: [{
id: 'visitors.source'
}]
});
const queryAndParams = query.buildSqlAndParams();
console.log(queryAndParams);
expect(queryAndParams[0]).not.toContain('COALESCE');

await dbRunner.testQuery(queryAndParams).then(res => {
console.log(JSON.stringify(res));
expect(res).toEqual(
[{
visitors__source: 'some',
visitors__visitor_revenue: '100'
}]
);
});
});

it('expression cube name cache', async () => {
await runQueryTest(
{
Expand Down
29 changes: 20 additions & 9 deletions rust/cubesqlplanner/cubesqlplanner/src/plan/builder/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::plan::{
use crate::plan::expression::FunctionExpression;
use crate::planner::sql_evaluator::sql_nodes::SqlNodesFactory;
use crate::planner::{BaseMember, VisitorContext};
use cubenativeutils::CubeError;
use std::collections::HashMap;
use std::rc::Rc;

Expand Down Expand Up @@ -79,21 +80,30 @@ impl SelectBuilder {
member: &Rc<dyn BaseMember>,
references: Vec<QualifiedColumnName>,
alias: Option<String>,
) {
) -> Result<(), CubeError> {
let alias = if let Some(alias) = alias {
alias
} else {
member.alias_name()
};

let expr = Expr::Function(FunctionExpression {
function: "COALESCE".to_string(),
arguments: references
.into_iter()
// TODO unwrap
.map(|r| Expr::Reference(r))
.collect(),
});
let expr = if references.len() > 1 {
Expr::Function(FunctionExpression {
function: "COALESCE".to_string(),
arguments: references
.into_iter()
// TODO unwrap
.map(|r| Expr::Reference(r))
.collect(),
})
} else if references.len() == 1 {
Expr::Reference(references[0].clone())
} else {
return Err(CubeError::internal(
"Cannot add coalesce projection without references".to_string(),
));
};

let aliased_expr = AliasedExpr {
expr,
alias: alias.clone(),
Expand All @@ -102,6 +112,7 @@ impl SelectBuilder {
self.projection_columns.push(aliased_expr);
self.result_schema
.add_column(SchemaColumn::new(alias.clone(), Some(member.full_name())));
Ok(())
}

pub fn set_filter(&mut self, filter: Option<Filter>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ impl FullKeyAggregateQueryPlanner {
.collect::<Result<Vec<_>, _>>()?;
let alias = references_builder
.resolve_alias_for_member(&member.full_name(), &dimensions_source);
select_builder.add_projection_coalesce_member(member, references, alias);
select_builder.add_projection_coalesce_member(member, references, alias)?;
}

for member in BaseMemberHelper::iter_as_base_member(&outer_measures) {
Expand Down
Loading