Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import com.facebook.presto.spi.statistics.TableStatistics;
import com.facebook.presto.sql.planner.Plan;
import com.facebook.presto.testing.MaterializedResult;
import com.facebook.presto.testing.MaterializedRow;
import com.facebook.presto.testing.QueryRunner;
import com.facebook.presto.tests.AbstractTestQueryFramework;
import com.google.common.cache.CacheBuilder;
Expand Down Expand Up @@ -466,6 +467,32 @@ public void testStatisticsCachePartialEviction()
}
}

@Test
public void testShowStatsWithTimestampWithTimeZone()
{
assertQuerySucceeds("CREATE TABLE test_timestamp_tz(id BIGINT, ts TIMESTAMP WITH TIME ZONE)");
assertUpdate("INSERT INTO test_timestamp_tz VALUES " +
"(1, TIMESTAMP '2024-01-01 12:00:00 UTC'), " +
"(2, TIMESTAMP '2024-01-02 18:30:00 UTC'), " +
"(3, TIMESTAMP '2024-01-03 00:00:00 America/New_York')", 3);

MaterializedResult stats = getQueryRunner().execute("SHOW STATS FOR test_timestamp_tz");

assertStatValue(StatsSchema.LOW_VALUE, stats, ImmutableSet.of("ts"), null, true);
assertStatValue(StatsSchema.HIGH_VALUE, stats, ImmutableSet.of("ts"), null, true);

Optional<MaterializedRow> tsRow = stats.getMaterializedRows().stream()
.filter(row -> row.getField(StatsSchema.COLUMN_NAME.ordinal()) != null)
.filter(row -> row.getField(StatsSchema.COLUMN_NAME.ordinal()).equals("ts"))
.findFirst();
assertTrue(tsRow.isPresent(), "Statistics for column 'ts' not found");
MaterializedRow row = tsRow.get();
assertEquals((String) row.getField(StatsSchema.LOW_VALUE.ordinal()), "2024-01-01 12:00:00.000 UTC");
assertEquals((String) row.getField(StatsSchema.HIGH_VALUE.ordinal()), "2024-01-03 05:00:00.000 UTC");

assertQuerySucceeds("DROP TABLE test_timestamp_tz");
}

private TableStatistics getScanStatsEstimate(Session session, @Language("SQL") String sql)
{
Plan plan = plan(sql, session);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.facebook.presto.common.type.SmallintType;
import com.facebook.presto.common.type.SqlTime;
import com.facebook.presto.common.type.SqlTimestamp;
import com.facebook.presto.common.type.SqlTimestampWithTimeZone;
import com.facebook.presto.common.type.TinyintType;
import com.facebook.presto.common.type.Type;
import com.facebook.presto.metadata.Metadata;
Expand Down Expand Up @@ -82,7 +83,9 @@
import static com.facebook.presto.common.type.StandardTypes.DOUBLE;
import static com.facebook.presto.common.type.StandardTypes.VARCHAR;
import static com.facebook.presto.common.type.TimeType.TIME;
import static com.facebook.presto.common.type.TimeZoneKey.UTC_KEY;
import static com.facebook.presto.common.type.TimestampType.TIMESTAMP;
import static com.facebook.presto.common.type.TimestampWithTimeZoneType.TIMESTAMP_WITH_TIME_ZONE;
import static com.facebook.presto.metadata.MetadataUtil.createQualifiedObjectName;
import static com.facebook.presto.sql.QueryUtil.aliased;
import static com.facebook.presto.sql.QueryUtil.selectAll;
Expand Down Expand Up @@ -381,6 +384,9 @@ private Expression toStringLiteral(Type type, double value)
if (type.equals(TIME)) {
return new StringLiteral(new SqlTime(round(value)).toString());
}
if (type.equals(TIMESTAMP_WITH_TIME_ZONE)) {
return new StringLiteral(new SqlTimestampWithTimeZone(round(value) / MICROSECONDS_PER_MILLISECOND, UTC_KEY).toString());
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

So the time zone is always packed in the value? Does the value need to be unpacked before rounding and conversion to micros?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

My mistake, I didn't see the UTC_KEY was also a parameter. Looks good.

}
throw new IllegalArgumentException("Unexpected type: " + type);
}
}
Expand Down
Loading