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

[BugFix] fix stale histogram lead to unexpected stats #45614

Merged
merged 3 commits into from
May 15, 2024

Conversation

packy92
Copy link
Contributor

@packy92 packy92 commented May 14, 2024

Why I'm doing:

Column histogram may not updated after the first collection.
If use the stale histogram to estimate row number may have a risk of divide zero exception because the estimated histogram may return 0 row count(a empty bucket and empty mcv).

What I'm doing:

If the predicate range doesn't overlap with the histogram, we use the min/max value instead of histogram to estimate statistics.

Fixes #issue

What type of PR is this:

  • BugFix
  • Feature
  • Enhancement
  • Refactor
  • UT
  • Doc
  • Tool

Does this PR entail a change in behavior?

  • Yes, this PR will result in a change in behavior.
  • No, this PR will not result in a change in behavior.

If yes, please specify the type of change:

  • Interface/UI changes: syntax, type conversion, expression evaluation, display information
  • Parameter changes: default values, similar parameters but with different default values
  • Policy changes: use new policy to replace old one, functionality automatically enabled
  • Feature removed
  • Miscellaneous: upgrade & downgrade compatibility, etc.

Checklist:

  • I have added test cases for my bug fix or my new feature
  • This pr needs user documentation (for new or modified features or behaviors)
    • I have added documentation for my new feature or new function
  • This is a backport pr

Bugfix cherry-pick branch check:

  • I have checked the version labels which the pr will be auto-backported to the target branch
    • 3.3
    • 3.2
    • 3.1
    • 3.0
    • 2.5

Signed-off-by: packy92 <wangchao@starrocks.com>
@packy92 packy92 requested a review from a team as a code owner May 14, 2024 13:02
return Optional.empty();
}

return Optional.of(new Histogram(bucketList, estimatedMCV));
}

public static ColumnStatistic estimateColumnStatisticsWithHistogram(ColumnStatistic columnStatistic,
Copy link

Choose a reason for hiding this comment

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

The most risky bug in this code is:
There is a potential NullPointerException or unhandled empty Optional being returned from updateHistWithLessThan and updateHistWithGreaterThan, which are used without checking whether the Optional contains a value before calling .get() method. This might lead to NoSuchElementException if the Optional is empty.

You can modify the code like this:
Inside both estimateColumnLessThanConstant and estimateColumnGreaterThanConstant methods, replace the lines where Histogram estimatedHistogram = hist.get(); is used with the following:

if (!hist.isPresent()) {
    // Handle the case where histogram is not present, for example:
    return statistics; // Returning unchanged statistics as we cannot estimate
}
Histogram estimatedHistogram = hist.get();

This ensures that you handle the case where hist might be empty properly, rather than directly calling .get() on an Optional that could potentially be empty, avoiding NoSuchElementException.

kangkaisen
kangkaisen previously approved these changes May 14, 2024
Signed-off-by: packy92 <wangchao@starrocks.com>
Signed-off-by: packy92 <wangchao@starrocks.com>
Copy link

sonarcloud bot commented May 15, 2024

Copy link

[BE Incremental Coverage Report]

pass : 0 / 0 (0%)

Copy link

[FE Incremental Coverage Report]

pass : 31 / 34 (91.18%)

file detail

path covered_line new_line coverage not_covered_line_detail
🔵 com/starrocks/sql/optimizer/statistics/BinaryPredicateStatisticCalculator.java 27 30 90.00% [397, 440, 518]
🔵 com/starrocks/sql/optimizer/statistics/Histogram.java 4 4 100.00% []

@kangkaisen kangkaisen merged commit 1e5626e into StarRocks:main May 15, 2024
47 checks passed
Copy link

@Mergifyio backport branch-3.3

@github-actions github-actions bot removed the 3.3 label May 15, 2024
Copy link

@Mergifyio backport branch-3.2

@github-actions github-actions bot removed the 3.2 label May 15, 2024
Copy link

@Mergifyio backport branch-3.1

@github-actions github-actions bot removed the 3.1 label May 15, 2024
Copy link

@Mergifyio backport branch-3.0

@github-actions github-actions bot removed the 3.0 label May 15, 2024
Copy link

@Mergifyio backport branch-2.5

Copy link
Contributor

mergify bot commented May 15, 2024

backport branch-3.3

✅ Backports have been created

Copy link
Contributor

mergify bot commented May 15, 2024

backport branch-3.2

✅ Backports have been created

@github-actions github-actions bot removed the 2.5 label May 15, 2024
Copy link
Contributor

mergify bot commented May 15, 2024

backport branch-3.1

✅ Backports have been created

Copy link
Contributor

mergify bot commented May 15, 2024

backport branch-3.0

✅ Backports have been created

Copy link
Contributor

mergify bot commented May 15, 2024

backport branch-2.5

✅ Backports have been created

mergify bot pushed a commit that referenced this pull request May 15, 2024
Signed-off-by: packy92 <wangchao@starrocks.com>
(cherry picked from commit 1e5626e)
mergify bot pushed a commit that referenced this pull request May 15, 2024
Signed-off-by: packy92 <wangchao@starrocks.com>
(cherry picked from commit 1e5626e)

# Conflicts:
#	fe/fe-core/src/test/resources/sql/tpch-histogram-cost/q19.sql
mergify bot pushed a commit that referenced this pull request May 15, 2024
Signed-off-by: packy92 <wangchao@starrocks.com>
(cherry picked from commit 1e5626e)

# Conflicts:
#	fe/fe-core/src/main/java/com/starrocks/sql/optimizer/statistics/Histogram.java
#	fe/fe-core/src/test/resources/sql/tpch-histogram-cost/q19.sql
mergify bot pushed a commit that referenced this pull request May 15, 2024
Signed-off-by: packy92 <wangchao@starrocks.com>
(cherry picked from commit 1e5626e)

# Conflicts:
#	fe/fe-core/src/main/java/com/starrocks/sql/optimizer/statistics/BinaryPredicateStatisticCalculator.java
#	fe/fe-core/src/main/java/com/starrocks/sql/optimizer/statistics/Histogram.java
#	fe/fe-core/src/test/resources/sql/tpch-histogram-cost/q19.sql
mergify bot pushed a commit that referenced this pull request May 15, 2024
Signed-off-by: packy92 <wangchao@starrocks.com>
(cherry picked from commit 1e5626e)

# Conflicts:
#	fe/fe-core/src/main/java/com/starrocks/sql/optimizer/statistics/BinaryPredicateStatisticCalculator.java
#	fe/fe-core/src/main/java/com/starrocks/sql/optimizer/statistics/Histogram.java
#	fe/fe-core/src/test/resources/sql/tpch-histogram-cost/q19.sql
wanpengfei-git pushed a commit that referenced this pull request May 15, 2024
… (#45642)

Co-authored-by: packy92 <110370499+packy92@users.noreply.github.com>
wanpengfei-git pushed a commit that referenced this pull request May 15, 2024
… (#45643)

Signed-off-by: packy92 <wangchao@starrocks.com>
Co-authored-by: packy92 <110370499+packy92@users.noreply.github.com>
Co-authored-by: packy92 <wangchao@starrocks.com>
wanpengfei-git pushed a commit that referenced this pull request May 15, 2024
… (#45644)

Signed-off-by: packy92 <wangchao@starrocks.com>
Co-authored-by: packy92 <110370499+packy92@users.noreply.github.com>
Co-authored-by: packy92 <wangchao@starrocks.com>
wanpengfei-git pushed a commit that referenced this pull request May 15, 2024
… (#45645)

Signed-off-by: packy92 <wangchao@starrocks.com>
Co-authored-by: packy92 <110370499+packy92@users.noreply.github.com>
Co-authored-by: packy92 <wangchao@starrocks.com>
wanpengfei-git pushed a commit that referenced this pull request May 16, 2024
… (#45646)

Signed-off-by: packy92 <wangchao@starrocks.com>
Co-authored-by: packy92 <110370499+packy92@users.noreply.github.com>
Co-authored-by: packy92 <wangchao@starrocks.com>
node pushed a commit to vivo/starrocks that referenced this pull request May 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants