Skip to content

Commit 1fb4f44

Browse files
committed
Return statistics for faker tables
1 parent 2268c32 commit 1fb4f44

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

plugin/trino-faker/src/main/java/io/trino/plugin/faker/FakerMetadata.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@
4949
import io.trino.spi.predicate.ValueSet;
5050
import io.trino.spi.security.TrinoPrincipal;
5151
import io.trino.spi.statistics.ColumnStatisticMetadata;
52+
import io.trino.spi.statistics.ColumnStatistics;
5253
import io.trino.spi.statistics.ComputedStatistics;
54+
import io.trino.spi.statistics.Estimate;
55+
import io.trino.spi.statistics.TableStatistics;
5356
import io.trino.spi.statistics.TableStatisticsMetadata;
5457
import io.trino.spi.type.CharType;
5558
import io.trino.spi.type.Type;
@@ -759,4 +762,41 @@ public FunctionDependencyDeclaration getFunctionDependencies(ConnectorSession se
759762
{
760763
return FunctionDependencyDeclaration.NO_DEPENDENCIES;
761764
}
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+
}
762802
}

0 commit comments

Comments
 (0)