Search in sources :

Example 1 with TimeZoneNotSupportedException

use of io.trino.spi.type.TimeZoneNotSupportedException in project trino by trinodb.

the class WithTimeZone method shortPrecision.

@LiteralParameters({ "x", "p" })
@SqlType("timestamp(p) with time zone")
public static long shortPrecision(@LiteralParameter("p") long precision, @SqlType("timestamp(p)") long timestamp, @SqlType("varchar(x)") Slice zoneId) {
    verify(precision <= 3, "Expected precision <= 3");
    TimeZoneKey toTimeZoneKey;
    try {
        toTimeZoneKey = getTimeZoneKey(zoneId.toStringUtf8());
    } catch (TimeZoneNotSupportedException e) {
        throw new TrinoException(INVALID_FUNCTION_ARGUMENT, format("'%s' is not a valid time zone", zoneId.toStringUtf8()));
    }
    DateTimeZone toDateTimeZone = getDateTimeZone(toTimeZoneKey);
    return packDateTimeWithZone(UTC.getMillisKeepLocal(toDateTimeZone, scaleEpochMicrosToMillis(timestamp)), toTimeZoneKey);
}
Also used : TrinoException(io.trino.spi.TrinoException) TimeZoneNotSupportedException(io.trino.spi.type.TimeZoneNotSupportedException) TimeZoneKey.getTimeZoneKey(io.trino.spi.type.TimeZoneKey.getTimeZoneKey) TimeZoneKey(io.trino.spi.type.TimeZoneKey) DateTimeZone(org.joda.time.DateTimeZone) DateTimeZoneIndex.getDateTimeZone(io.trino.util.DateTimeZoneIndex.getDateTimeZone) LiteralParameters(io.trino.spi.function.LiteralParameters) SqlType(io.trino.spi.function.SqlType)

Example 2 with TimeZoneNotSupportedException

use of io.trino.spi.type.TimeZoneNotSupportedException in project trino by trinodb.

the class TestServer method testInvalidSessionError.

@Test
public void testInvalidSessionError() {
    String invalidTimeZone = "this_is_an_invalid_time_zone";
    QueryResults queryResults = postQuery(request -> request.setBodyGenerator(createStaticBodyGenerator("show catalogs", UTF_8)).setHeader(TRINO_HEADERS.requestCatalog(), "catalog").setHeader(TRINO_HEADERS.requestSchema(), "schema").setHeader(TRINO_HEADERS.requestPath(), "path").setHeader(TRINO_HEADERS.requestTimeZone(), invalidTimeZone)).map(JsonResponse::getValue).peek(result -> checkState((result.getError() == null) != (result.getNextUri() == null))).collect(last());
    QueryError queryError = queryResults.getError();
    assertNotNull(queryError);
    TimeZoneNotSupportedException expected = new TimeZoneNotSupportedException(invalidTimeZone);
    assertEquals(queryError.getErrorCode(), expected.getErrorCode().getCode());
    assertEquals(queryError.getErrorName(), expected.getErrorCode().getName());
    assertEquals(queryError.getErrorType(), expected.getErrorCode().getType().name());
    assertEquals(queryError.getMessage(), expected.getMessage());
}
Also used : QueryId(io.trino.spi.QueryId) QueryError(io.trino.client.QueryError) X_FORWARDED_PORT(com.google.common.net.HttpHeaders.X_FORWARDED_PORT) BasicQueryInfo(io.trino.server.BasicQueryInfo) FullJsonResponseHandler.createFullJsonResponseHandler(io.airlift.http.client.FullJsonResponseHandler.createFullJsonResponseHandler) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.testng.annotations.Test) Closeables.closeAll(io.airlift.testing.Closeables.closeAll) QueryResults(io.trino.client.QueryResults) URI(java.net.URI) Collector(java.util.stream.Collector) TestingTrinoServer(io.trino.server.testing.TestingTrinoServer) APPLICATION_JSON(javax.ws.rs.core.MediaType.APPLICATION_JSON) Splitter(com.google.common.base.Splitter) X_FORWARDED_PROTO(com.google.common.net.HttpHeaders.X_FORWARDED_PROTO) HttpUriBuilder(io.airlift.http.client.HttpUriBuilder) SEE_OTHER(javax.ws.rs.core.Response.Status.SEE_OTHER) JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) Builder.prepareHead(io.airlift.http.client.Request.Builder.prepareHead) QUERY_MAX_MEMORY(io.trino.SystemSessionProperties.QUERY_MAX_MEMORY) ImmutableMap(com.google.common.collect.ImmutableMap) OK(javax.ws.rs.core.Response.Status.OK) MemoryPlugin(io.trino.plugin.memory.MemoryPlugin) BeforeClass(org.testng.annotations.BeforeClass) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Assert.assertNotNull(org.testng.Assert.assertNotNull) Streams(com.google.common.collect.Streams) TestingTrinoClient(io.trino.testing.TestingTrinoClient) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) Collectors.joining(java.util.stream.Collectors.joining) Preconditions.checkState(com.google.common.base.Preconditions.checkState) List(java.util.List) TestingSession.testSessionBuilder(io.trino.testing.TestingSession.testSessionBuilder) Stream(java.util.stream.Stream) Optional(java.util.Optional) StaticBodyGenerator.createStaticBodyGenerator(io.airlift.http.client.StaticBodyGenerator.createStaticBodyGenerator) JsonCodec(io.airlift.json.JsonCodec) HttpClient(io.airlift.http.client.HttpClient) IntStream(java.util.stream.IntStream) Assert.assertNull(org.testng.Assert.assertNull) Assert.assertEquals(org.testng.Assert.assertEquals) Function(java.util.function.Function) CONTENT_TYPE(com.google.common.net.HttpHeaders.CONTENT_TYPE) AbstractSequentialIterator(com.google.common.collect.AbstractSequentialIterator) INCOMPATIBLE_CLIENT(io.trino.spi.StandardErrorCode.INCOMPATIBLE_CLIENT) JsonResponse(io.airlift.http.client.FullJsonResponseHandler.JsonResponse) ImmutableList(com.google.common.collect.ImmutableList) Objects.requireNonNull(java.util.Objects.requireNonNull) Request(io.airlift.http.client.Request) X_FORWARDED_HOST(com.google.common.net.HttpHeaders.X_FORWARDED_HOST) AfterClass(org.testng.annotations.AfterClass) Builder.preparePost(io.airlift.http.client.Request.Builder.preparePost) Builder.prepareGet(io.airlift.http.client.Request.Builder.prepareGet) Language(org.intellij.lang.annotations.Language) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Assert.fail(org.testng.Assert.fail) HASH_PARTITION_COUNT(io.trino.SystemSessionProperties.HASH_PARTITION_COUNT) Throwables.getStackTraceAsString(com.google.common.base.Throwables.getStackTraceAsString) JsonCodec.jsonCodec(io.airlift.json.JsonCodec.jsonCodec) StatusResponseHandler.createStatusResponseHandler(io.airlift.http.client.StatusResponseHandler.createStatusResponseHandler) JOIN_DISTRIBUTION_TYPE(io.trino.SystemSessionProperties.JOIN_DISTRIBUTION_TYPE) TimeZoneNotSupportedException(io.trino.spi.type.TimeZoneNotSupportedException) StatusResponse(io.airlift.http.client.StatusResponseHandler.StatusResponse) Collections(java.util.Collections) TRINO_HEADERS(io.trino.client.ProtocolHeaders.TRINO_HEADERS) Throwables.getStackTraceAsString(com.google.common.base.Throwables.getStackTraceAsString) QueryError(io.trino.client.QueryError) TimeZoneNotSupportedException(io.trino.spi.type.TimeZoneNotSupportedException) QueryResults(io.trino.client.QueryResults) JsonResponse(io.airlift.http.client.FullJsonResponseHandler.JsonResponse) Test(org.testng.annotations.Test)

Example 3 with TimeZoneNotSupportedException

use of io.trino.spi.type.TimeZoneNotSupportedException in project trino by trinodb.

the class AtTimeZone method atTimeZone.

@LiteralParameters({ "x", "p" })
@SqlType("time(p) with time zone")
public static LongTimeWithTimeZone atTimeZone(ConnectorSession session, @SqlType("time(p) with time zone") LongTimeWithTimeZone time, @SqlType("varchar(x)") Slice zoneId) {
    TimeZoneKey zoneKey;
    try {
        zoneKey = getTimeZoneKey(zoneId.toStringUtf8());
    } catch (TimeZoneNotSupportedException e) {
        throw new TrinoException(INVALID_FUNCTION_ARGUMENT, format("'%s' is not a valid time zone", zoneId));
    }
    int offsetMinutes = getOffsetMinutes(session.getStart(), zoneKey);
    long picos = time.getPicoseconds() - (time.getOffsetMinutes() - offsetMinutes) * PICOSECONDS_PER_MINUTE;
    return new LongTimeWithTimeZone(floorMod(picos, PICOSECONDS_PER_DAY), offsetMinutes);
}
Also used : LongTimeWithTimeZone(io.trino.spi.type.LongTimeWithTimeZone) TrinoException(io.trino.spi.TrinoException) TimeZoneNotSupportedException(io.trino.spi.type.TimeZoneNotSupportedException) TimeZoneKey.getTimeZoneKey(io.trino.spi.type.TimeZoneKey.getTimeZoneKey) TimeZoneKey(io.trino.spi.type.TimeZoneKey) LiteralParameters(io.trino.spi.function.LiteralParameters) SqlType(io.trino.spi.function.SqlType)

Example 4 with TimeZoneNotSupportedException

use of io.trino.spi.type.TimeZoneNotSupportedException in project trino by trinodb.

the class AtTimeZone method atTimeZone.

@LiteralParameters({ "x", "p" })
@SqlType("time(p) with time zone")
public static long atTimeZone(ConnectorSession session, @SqlType("time(p) with time zone") long packedTime, @SqlType("varchar(x)") Slice zoneId) {
    TimeZoneKey zoneKey;
    try {
        zoneKey = getTimeZoneKey(zoneId.toStringUtf8());
    } catch (TimeZoneNotSupportedException e) {
        throw new TrinoException(INVALID_FUNCTION_ARGUMENT, format("'%s' is not a valid time zone", zoneId));
    }
    int offsetMinutes = getOffsetMinutes(session.getStart(), zoneKey);
    long nanos = unpackTimeNanos(packedTime) - (unpackOffsetMinutes(packedTime) - offsetMinutes) * NANOSECONDS_PER_MINUTE;
    return packTimeWithTimeZone(floorMod(nanos, NANOSECONDS_PER_DAY), offsetMinutes);
}
Also used : TrinoException(io.trino.spi.TrinoException) TimeZoneNotSupportedException(io.trino.spi.type.TimeZoneNotSupportedException) TimeZoneKey.getTimeZoneKey(io.trino.spi.type.TimeZoneKey.getTimeZoneKey) TimeZoneKey(io.trino.spi.type.TimeZoneKey) LiteralParameters(io.trino.spi.function.LiteralParameters) SqlType(io.trino.spi.function.SqlType)

Example 5 with TimeZoneNotSupportedException

use of io.trino.spi.type.TimeZoneNotSupportedException in project trino by trinodb.

the class WithTimeZone method toLong.

private static LongTimestampWithTimeZone toLong(long epochMicros, int picosOfMicro, Slice zoneId) {
    TimeZoneKey toTimeZoneKey;
    try {
        toTimeZoneKey = getTimeZoneKey(zoneId.toStringUtf8());
    } catch (TimeZoneNotSupportedException e) {
        throw new TrinoException(INVALID_FUNCTION_ARGUMENT, format("'%s' is not a valid time zone", zoneId.toStringUtf8()));
    }
    DateTimeZone toDateTimeZone = getDateTimeZone(toTimeZoneKey);
    long epochMillis = scaleEpochMicrosToMillis(epochMicros);
    epochMillis = UTC.getMillisKeepLocal(toDateTimeZone, epochMillis);
    int picosOfMilli = getMicrosOfMilli(epochMicros) * PICOSECONDS_PER_MICROSECOND + picosOfMicro;
    return LongTimestampWithTimeZone.fromEpochMillisAndFraction(epochMillis, picosOfMilli, toTimeZoneKey);
}
Also used : TrinoException(io.trino.spi.TrinoException) TimeZoneNotSupportedException(io.trino.spi.type.TimeZoneNotSupportedException) TimeZoneKey.getTimeZoneKey(io.trino.spi.type.TimeZoneKey.getTimeZoneKey) TimeZoneKey(io.trino.spi.type.TimeZoneKey) DateTimeZone(org.joda.time.DateTimeZone) DateTimeZoneIndex.getDateTimeZone(io.trino.util.DateTimeZoneIndex.getDateTimeZone)

Aggregations

TimeZoneNotSupportedException (io.trino.spi.type.TimeZoneNotSupportedException)5 TrinoException (io.trino.spi.TrinoException)4 TimeZoneKey (io.trino.spi.type.TimeZoneKey)3 TimeZoneKey.getTimeZoneKey (io.trino.spi.type.TimeZoneKey.getTimeZoneKey)3 LiteralParameters (io.trino.spi.function.LiteralParameters)2 SqlType (io.trino.spi.function.SqlType)2 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1 Splitter (com.google.common.base.Splitter)1 Throwables.getStackTraceAsString (com.google.common.base.Throwables.getStackTraceAsString)1 AbstractSequentialIterator (com.google.common.collect.AbstractSequentialIterator)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Streams (com.google.common.collect.Streams)1 CONTENT_TYPE (com.google.common.net.HttpHeaders.CONTENT_TYPE)1 X_FORWARDED_HOST (com.google.common.net.HttpHeaders.X_FORWARDED_HOST)1 X_FORWARDED_PORT (com.google.common.net.HttpHeaders.X_FORWARDED_PORT)1 X_FORWARDED_PROTO (com.google.common.net.HttpHeaders.X_FORWARDED_PROTO)1 JsonResponse (io.airlift.http.client.FullJsonResponseHandler.JsonResponse)1 FullJsonResponseHandler.createFullJsonResponseHandler (io.airlift.http.client.FullJsonResponseHandler.createFullJsonResponseHandler)1