Skip to content

Commit

Permalink
Support range stats on timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
findepi committed Jul 21, 2021
1 parent 9884dba commit 3ec60e9
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
11 changes: 10 additions & 1 deletion core/trino-main/src/main/java/io/trino/cost/StatsUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
import io.trino.spi.type.DecimalType;
import io.trino.spi.type.DoubleType;
import io.trino.spi.type.IntegerType;
import io.trino.spi.type.LongTimestamp;
import io.trino.spi.type.RealType;
import io.trino.spi.type.SmallintType;
import io.trino.spi.type.TimestampType;
import io.trino.spi.type.TinyintType;
import io.trino.spi.type.Type;
import io.trino.sql.InterpretedFunctionInvoker;
Expand All @@ -46,7 +48,14 @@ static OptionalDouble toStatsRepresentation(Metadata metadata, Session session,
}

if (DateType.DATE.equals(type)) {
return OptionalDouble.of(((Long) value).doubleValue());
return OptionalDouble.of((long) value);
}

if (type instanceof TimestampType) {
if (((TimestampType) type).isShort()) {
return OptionalDouble.of((long) value);
}
return OptionalDouble.of(((LongTimestamp) value).getEpochMicros());
}

return OptionalDouble.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@
import io.trino.cost.SymbolStatsEstimate;
import io.trino.execution.warnings.WarningCollector;
import io.trino.metadata.Metadata;
import io.trino.operator.scalar.timestamp.TimestampToVarcharCast;
import io.trino.security.AccessControl;
import io.trino.spi.security.GroupProvider;
import io.trino.spi.type.BigintType;
import io.trino.spi.type.DecimalType;
import io.trino.spi.type.IntegerType;
import io.trino.spi.type.RealType;
import io.trino.spi.type.SmallintType;
import io.trino.spi.type.TimestampType;
import io.trino.spi.type.TinyintType;
import io.trino.spi.type.Type;
import io.trino.sql.QueryUtil;
Expand Down Expand Up @@ -72,6 +74,7 @@
import static io.trino.sql.QueryUtil.simpleQuery;
import static io.trino.sql.analyzer.TypeSignatureTranslator.toSqlType;
import static java.lang.Double.isFinite;
import static java.lang.Math.min;
import static java.lang.Math.round;
import static java.util.Objects.requireNonNull;

Expand Down Expand Up @@ -219,6 +222,12 @@ private static Expression toStringLiteral(Type type, double value)
if (type.equals(DATE)) {
return new StringLiteral(LocalDate.ofEpochDay(round(value)).toString());
}
if (type instanceof TimestampType) {
@SuppressWarnings("NumericCastThatLosesPrecision")
long epochMicros = (long) value;
int outputPrecision = min(((TimestampType) type).getPrecision(), TimestampType.MAX_SHORT_PRECISION);
return new StringLiteral(TimestampToVarcharCast.cast(outputPrecision, epochMicros).toStringUtf8());
}
throw new IllegalArgumentException("Unexpected type: " + type);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,45 @@ public void testShowStats()
" (null, null, null, null, 15.0, null, null)");
}

@Test
public void testShowStatsWithTimestamp()
{
// precision 0
assertQuery(
"SHOW STATS FOR (VALUES TIMESTAMP '2021-07-20 16:52:00')",
"VALUES " +
" ('_col0', null, 1, 0, null, '2021-07-20 16:52:00', '2021-07-20 16:52:00'), " +
" (null, null, null, null, 1, null, null)");

// precision 3
assertQuery(
"SHOW STATS FOR (VALUES TIMESTAMP '2021-07-20 16:52:00.123')",
"VALUES " +
" ('_col0', null, 1, 0, null, '2021-07-20 16:52:00.123', '2021-07-20 16:52:00.123'), " +
" (null, null, null, null, 1, null, null)");

// precision 6
assertQuery(
"SHOW STATS FOR (VALUES TIMESTAMP '2021-07-20 16:52:00.123456')",
"VALUES " +
" ('_col0', null, 1, 0, null, '2021-07-20 16:52:00.123456', '2021-07-20 16:52:00.123456'), " +
" (null, null, null, null, 1, null, null)");

// precision 9
assertQuery(
"SHOW STATS FOR (VALUES TIMESTAMP '2021-07-20 16:52:00.123456789')",
"VALUES " +
" ('_col0', null, 1, 0, null, '2021-07-20 16:52:00.123456', '2021-07-20 16:52:00.123456'), " +
" (null, null, null, null, 1, null, null)");

// precision 12
assertQuery(
"SHOW STATS FOR (VALUES TIMESTAMP '2021-07-20 16:52:00.123456789012')",
"VALUES " +
" ('_col0', null, 1, 0, null, '2021-07-20 16:52:00.123456', '2021-07-20 16:52:00.123456'), " +
" (null, null, null, null, 1, null, null)");
}

@Test
public void testShowStatsWithoutFrom()
{
Expand Down

0 comments on commit 3ec60e9

Please sign in to comment.