Skip to content

aggregate_statistics should only optimize MIN/MAX when relation is not empty - #8914

Merged
viirya merged 2 commits into
apache:mainfrom
viirya:fix_aggregate_statistics
Jan 20, 2024
Merged

viirya merged 2 commits into
apache:mainfrom
viirya:fix_aggregate_statistics

Conversation

@viirya

@viirya viirya commented Jan 19, 2024

Copy link
Copy Markdown
Member

Which issue does this PR close?

Closes #8911.

Rationale for this change

What changes are included in this PR?

Are these changes tested?

Are there any user-facing changes?

@github-actions github-actions Bot added core Core DataFusion crate sqllogictest SQL Logic Tests (.slt) labels Jan 19, 2024
.as_any()
.downcast_ref::<expressions::Column>()
if let Precision::Exact(num_rows) = &stats.num_rows {
if *num_rows > 0 {

@viirya viirya Jan 19, 2024 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could optimize MIN/MAX if the relation is empty (i.e., return NULL directly). Since it is not the issue itself, I will leave it as follow up.

@alamb alamb left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @viirya

select count(*) from (select count(*) a, count(*) b from (select 1));
----
1

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also add another test where there is a single row that doesn't actually pass the predicate?

Like

CREATE TABLE t(col0 INTEGER) as VALUES(2);
SELECT MIN(col0) FROM empty WHERE col0=1;
SELECT MAX(col0) FROM empty WHERE col0=1;

So the table is not actually empty but the filter removes it all

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. Added that one.

@viirya
viirya merged commit d0c84cc into apache:main Jan 20, 2024
@viirya

viirya commented Jan 20, 2024

Copy link
Copy Markdown
Member Author

Thank you @alamb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Core DataFusion crate sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MIN and MAX return data from WHERE clause on empty input

2 participants