forked from trinodb/trino
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Calculate statistics for TABLESAMPLE
- Loading branch information
Showing
4 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
core/trino-main/src/main/java/io/trino/cost/SampleStatsRule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.trino.cost; | ||
|
||
import io.trino.Session; | ||
import io.trino.matching.Pattern; | ||
import io.trino.sql.planner.TypeProvider; | ||
import io.trino.sql.planner.iterative.Lookup; | ||
import io.trino.sql.planner.plan.SampleNode; | ||
|
||
import java.util.Optional; | ||
|
||
import static io.trino.sql.planner.plan.Patterns.sample; | ||
|
||
public class SampleStatsRule | ||
extends SimpleStatsRule<SampleNode> | ||
{ | ||
private static final Pattern<SampleNode> PATTERN = sample(); | ||
|
||
public SampleStatsRule(StatsNormalizer normalizer) | ||
{ | ||
super(normalizer); | ||
} | ||
|
||
@Override | ||
public Pattern<SampleNode> getPattern() | ||
{ | ||
return PATTERN; | ||
} | ||
|
||
@Override | ||
protected Optional<PlanNodeStatsEstimate> doCalculate(SampleNode node, StatsProvider statsProvider, Lookup lookup, Session session, TypeProvider types) | ||
{ | ||
PlanNodeStatsEstimate sourceStats = statsProvider.getStats(node.getSource()); | ||
PlanNodeStatsEstimate calculatedStats = sourceStats.mapOutputRowCount(outputRowCount -> outputRowCount * node.getSampleRatio()); | ||
return Optional.of(calculatedStats); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
core/trino-main/src/test/java/io/trino/cost/TestSampleStatsRule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.trino.cost; | ||
|
||
import io.trino.sql.planner.Symbol; | ||
import io.trino.sql.planner.plan.SampleNode; | ||
import org.testng.annotations.Test; | ||
|
||
import static io.trino.spi.type.BigintType.BIGINT; | ||
import static io.trino.spi.type.DoubleType.DOUBLE; | ||
import static java.lang.Double.POSITIVE_INFINITY; | ||
|
||
public class TestSampleStatsRule | ||
extends BaseStatsCalculatorTest | ||
{ | ||
@Test | ||
public void testStatsForSampleNode() | ||
{ | ||
tester() | ||
.assertStatsFor(pb -> { | ||
Symbol a = pb.symbol("a", BIGINT); | ||
Symbol b = pb.symbol("b", DOUBLE); | ||
return pb.sample(.33, SampleNode.Type.BERNOULLI, pb.values(a, b)); | ||
}) | ||
.withSourceStats(PlanNodeStatsEstimate.builder() | ||
.setOutputRowCount(100) | ||
.addSymbolStatistics( | ||
new Symbol("a"), | ||
SymbolStatsEstimate.builder() | ||
.setDistinctValuesCount(20) | ||
.setNullsFraction(0.3) | ||
.setLowValue(1) | ||
.setHighValue(30) | ||
.build()) | ||
.addSymbolStatistics( | ||
new Symbol("b"), | ||
SymbolStatsEstimate.builder() | ||
.setDistinctValuesCount(40) | ||
.setNullsFraction(0.6) | ||
.setLowValue(13.5) | ||
.setHighValue(POSITIVE_INFINITY) | ||
.build()) | ||
.build()) | ||
.check(check -> check | ||
.outputRowsCount(33) | ||
.symbolStats("a", assertion -> assertion | ||
.dataSizeUnknown() | ||
.distinctValuesCount(20) | ||
.nullsFraction(0.3) | ||
.lowValue(1) | ||
.highValue(30)) | ||
.symbolStats("b", assertion -> assertion | ||
.dataSizeUnknown() | ||
.distinctValuesCount(23.1) | ||
.nullsFraction(0.3) | ||
.lowValue(13.5) | ||
.highValueUnknown())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters