|
49 | 49 | import io.trino.spi.predicate.ValueSet; |
50 | 50 | import io.trino.spi.security.TrinoPrincipal; |
51 | 51 | import io.trino.spi.statistics.ColumnStatisticMetadata; |
| 52 | +import io.trino.spi.statistics.ColumnStatistics; |
52 | 53 | import io.trino.spi.statistics.ComputedStatistics; |
| 54 | +import io.trino.spi.statistics.Estimate; |
| 55 | +import io.trino.spi.statistics.TableStatistics; |
53 | 56 | import io.trino.spi.statistics.TableStatisticsMetadata; |
54 | 57 | import io.trino.spi.type.CharType; |
55 | 58 | import io.trino.spi.type.Type; |
@@ -759,4 +762,41 @@ public FunctionDependencyDeclaration getFunctionDependencies(ConnectorSession se |
759 | 762 | { |
760 | 763 | return FunctionDependencyDeclaration.NO_DEPENDENCIES; |
761 | 764 | } |
| 765 | + |
| 766 | + @Override |
| 767 | + public synchronized TableStatistics getTableStatistics(ConnectorSession session, ConnectorTableHandle tableHandle) |
| 768 | + { |
| 769 | + FakerTableHandle fakerTableHandle = (FakerTableHandle) tableHandle; |
| 770 | + TableInfo info = tables.get(fakerTableHandle.schemaTableName()); |
| 771 | + |
| 772 | + TableStatistics.Builder tableStatisitics = TableStatistics.builder(); |
| 773 | + tableStatisitics.setRowCount(Estimate.of(fakerTableHandle.limit())); |
| 774 | + |
| 775 | + info.columns().forEach(columnInfo -> { |
| 776 | + Object min = PropertyValues.propertyValue(columnInfo.metadata(), MIN_PROPERTY); |
| 777 | + Object max = PropertyValues.propertyValue(columnInfo.metadata(), MAX_PROPERTY); |
| 778 | + Object step = PropertyValues.propertyValue(columnInfo.metadata(), STEP_PROPERTY); |
| 779 | + Collection<?> allowedValues = (Collection<?>) columnInfo.metadata().getProperties().get(ALLOWED_VALUES_PROPERTY); // skip parsing as we don't need the values |
| 780 | + |
| 781 | + checkState(allowedValues == null || (min == null && max == null), "The `%s` property cannot be set together with `%s` and `%s` properties".formatted(ALLOWED_VALUES_PROPERTY, MIN_PROPERTY, MAX_PROPERTY)); |
| 782 | + |
| 783 | + ColumnStatistics.Builder columnStatistics = ColumnStatistics.builder(); |
| 784 | + if (allowedValues != null) { |
| 785 | + columnStatistics.setDistinctValuesCount(Estimate.of(allowedValues.size())); |
| 786 | + } |
| 787 | + else { |
| 788 | + Type type = columnInfo.metadata().getType(); |
| 789 | + if (min != null && max != null && type.getJavaType() == long.class) { |
| 790 | + long distinctValuesCount = (long) max - (long) min; |
| 791 | + if (step != null) { |
| 792 | + distinctValuesCount = distinctValuesCount / (long) step; |
| 793 | + } |
| 794 | + columnStatistics.setDistinctValuesCount(Estimate.of(distinctValuesCount)); |
| 795 | + } |
| 796 | + } |
| 797 | + columnStatistics.setNullsFraction(Estimate.of(columnInfo.handle().nullProbability())); |
| 798 | + tableStatisitics.setColumnStatistics(columnInfo.handle(), columnStatistics.build()); |
| 799 | + }); |
| 800 | + return tableStatisitics.build(); |
| 801 | + } |
762 | 802 | } |
0 commit comments