@Test public void testTimeLiterals() throws Exception { MaterializedResult.Builder builder = resultBuilder(getSession(), DATE, TIME, TIME_WITH_TIME_ZONE, TIMESTAMP, TIMESTAMP_WITH_TIME_ZONE); DateTimeZone sessionTimeZone = DateTimeZoneIndex.getDateTimeZone(getSession().getTimeZoneKey()); DateTimeZone utcPlus6 = DateTimeZoneIndex.getDateTimeZone(TimeZoneKey.getTimeZoneKeyForOffset(6 * 60)); builder.row( new Date(new DateTime(2013, 3, 22, 0, 0, sessionTimeZone).getMillis()), new Time(new DateTime(1970, 1, 1, 3, 4, 5, sessionTimeZone).getMillisOfDay()), new Time(new DateTime(1970, 1, 1, 3, 4, 5, utcPlus6).getMillis()), // hack because java.sql.Time compares based on actual number of ms since epoch instead of ms since midnight new Timestamp(new DateTime(1960, 1, 22, 3, 4, 5, sessionTimeZone).getMillis()), new Timestamp(new DateTime(1960, 1, 22, 3, 4, 5, utcPlus6).getMillis())); MaterializedResult actual = computeActual("SELECT DATE '2013-03-22', TIME '3:04:05', TIME '3:04:05 +06:00', TIMESTAMP '1960-01-22 3:04:05', TIMESTAMP '1960-01-22 3:04:05 +06:00'"); assertEquals(actual, builder.build()); }
public FullConnectorSession( String queryId, Identity identity, TimeZoneKey timeZoneKey, Locale locale, long startTime) { this.queryId = requireNonNull(queryId, "queryId is null"); this.identity = requireNonNull(identity, "identity is null"); this.timeZoneKey = requireNonNull(timeZoneKey, "timeZoneKey is null"); this.locale = requireNonNull(locale, "locale is null"); this.startTime = startTime; this.properties = null; this.catalog = null; this.sessionPropertyManager = null; }
public FullConnectorSession( String queryId, Identity identity, TimeZoneKey timeZoneKey, Locale locale, long startTime, Map<String, String> properties, String catalog, SessionPropertyManager sessionPropertyManager) { this.queryId = requireNonNull(queryId, "queryId is null"); this.identity = requireNonNull(identity, "identity is null"); this.timeZoneKey = requireNonNull(timeZoneKey, "timeZoneKey is null"); this.locale = requireNonNull(locale, "locale is null"); this.startTime = startTime; this.properties = ImmutableMap.copyOf(requireNonNull(properties, "properties is null")); this.catalog = requireNonNull(catalog, "catalog is null"); this.sessionPropertyManager = requireNonNull(sessionPropertyManager, "sessionPropertyManager is null"); }
public TestingConnectorSession( String user, TimeZoneKey timeZoneKey, Locale locale, long startTime, List<PropertyMetadata<?>> propertyMetadatas, Map<String, Object> propertyValues) { this.queryId = queryIdGenerator.createNextQueryId().toString(); this.identity = new Identity(requireNonNull(user, "user is null"), Optional.empty()); this.timeZoneKey = requireNonNull(timeZoneKey, "timeZoneKey is null"); this.locale = requireNonNull(locale, "locale is null"); this.startTime = startTime; this.properties = Maps.uniqueIndex(propertyMetadatas, PropertyMetadata::getName); this.propertyValues = ImmutableMap.copyOf(propertyValues); }
@Override public ConnectorPageSource createPageSource(Session session, Split split, List<ColumnHandle> columns) { assertInstanceOf(split.getConnectorSplit(), FunctionAssertions.TestSplit.class); FunctionAssertions.TestSplit testSplit = (FunctionAssertions.TestSplit) split.getConnectorSplit(); if (testSplit.isRecordSet()) { RecordSet records = InMemoryRecordSet.builder(ImmutableList.<Type>of(BIGINT, VARCHAR, DOUBLE, BOOLEAN, BIGINT, VARCHAR, VARCHAR, TIMESTAMP_WITH_TIME_ZONE, VARBINARY)) .addRow( 1234L, "hello", 12.34, true, new DateTime(2001, 8, 22, 3, 4, 5, 321, DateTimeZone.UTC).getMillis(), "%el%", null, packDateTimeWithZone(new DateTime(1970, 1, 1, 0, 1, 0, 999, DateTimeZone.UTC).getMillis(), TimeZoneKey.getTimeZoneKey("Z")), Slices.wrappedBuffer((byte) 0xab)) .build(); return new RecordPageSource(records); } else { return new FixedPageSource(ImmutableList.of(SOURCE_PAGE)); } }
@Test public void testTimestampAccess() { // Test timestamp functions: TimeZoneKey tzKey = session.getTimeZoneKey(); assertEquals(tzKey.getId(), "America/Los_Angeles"); // Test a known result long result = SessionVariables.getTimestampAsLong("2016-07-10 12:03:56.124", session); assertTrue(result != 0); assertEquals(result, 1468177436124L); ConnectorSession altSession = makeSessionWithTimeZone("America/New_York"); long result2 = SessionVariables.getTimestampAsLong("2016-07-10 12:03:56.124", altSession); assertTrue(result2 != 0); assertEquals(result2, 1468166636124L); }
@Inject public ExpressionCompiler() { TransactionManager transactionManager = TransactionManager.createTestTransactionManager(); Metadata metadata = MetadataManager.createTestMetadataManager(); this.serde = metadata.getBlockEncodingSerde(); this.metadata = metadata; this.featuresConfig = new FeaturesConfig(); this.typeManager = metadata.getTypeManager(); this.session = Session.builder(new SessionPropertyManager()) .setIdentity(new Identity("user", Optional.empty())) .setTimeZoneKey(TimeZoneKey.UTC_KEY) .setLocale(Locale.ENGLISH) .setQueryId(QueryId.valueOf("row_expression_compiler")) .setTransactionId(transactionManager.beginTransaction(IsolationLevel.REPEATABLE_READ, true, true)) .build(); this.expressionOptimizer = new ExpressionOptimizer(metadata.getFunctionRegistry(), metadata.getTypeManager(), session); }
private static Function<List<Object>, MaterializedRow> dataToRow(final TimeZoneKey timeZoneKey, final List<Type> types) { return data -> { checkArgument(data.size() == types.size(), "columns size does not match types size"); List<Object> row = new ArrayList<>(); for (int i = 0; i < data.size(); i++) { Object value = data.get(i); Type type = types.get(i); row.add(convertToRowValue(type, value, timeZoneKey)); } return new MaterializedRow(DEFAULT_PRECISION, row); }; }
@JsonCreator public SessionRepresentation( @JsonProperty("queryId") String queryId, @JsonProperty("transactionId") Optional<TransactionId> transactionId, @JsonProperty("clientTransactionSupport") boolean clientTransactionSupport, @JsonProperty("user") String user, @JsonProperty("principal") Optional<String> principal, @JsonProperty("source") Optional<String> source, @JsonProperty("catalog") Optional<String> catalog, @JsonProperty("schema") Optional<String> schema, @JsonProperty("timeZoneKey") TimeZoneKey timeZoneKey, @JsonProperty("locale") Locale locale, @JsonProperty("remoteUserAddress") Optional<String> remoteUserAddress, @JsonProperty("userAgent") Optional<String> userAgent, @JsonProperty("startTime") long startTime, @JsonProperty("systemProperties") Map<String, String> systemProperties, @JsonProperty("catalogProperties") Map<String, Map<String, String>> catalogProperties) { this.queryId = requireNonNull(queryId, "queryId is null"); this.transactionId = requireNonNull(transactionId, "transactionId is null"); this.clientTransactionSupport = clientTransactionSupport; this.user = requireNonNull(user, "user is null"); this.principal = requireNonNull(principal, "principal is null"); this.source = requireNonNull(source, "source is null"); this.catalog = requireNonNull(catalog, "catalog is null"); this.schema = requireNonNull(schema, "schema is null"); this.timeZoneKey = requireNonNull(timeZoneKey, "timeZoneKey is null"); this.locale = requireNonNull(locale, "locale is null"); this.remoteUserAddress = requireNonNull(remoteUserAddress, "remoteUserAddress is null"); this.userAgent = requireNonNull(userAgent, "userAgent is null"); this.startTime = startTime; this.systemProperties = ImmutableMap.copyOf(systemProperties); ImmutableMap.Builder<String, Map<String, String>> catalogPropertiesBuilder = ImmutableMap.<String, Map<String, String>>builder(); for (Entry<String, Map<String, String>> entry : catalogProperties.entrySet()) { catalogPropertiesBuilder.put(entry.getKey(), ImmutableMap.copyOf(entry.getValue())); } this.catalogProperties = catalogPropertiesBuilder.build(); }
public Session( QueryId queryId, Optional<TransactionId> transactionId, boolean clientTransactionSupport, Identity identity, Optional<String> source, Optional<String> catalog, Optional<String> schema, TimeZoneKey timeZoneKey, Locale locale, Optional<String> remoteUserAddress, Optional<String> userAgent, long startTime, Map<String, String> systemProperties, Map<String, Map<String, String>> catalogProperties, SessionPropertyManager sessionPropertyManager) { this.queryId = requireNonNull(queryId, "queryId is null"); this.transactionId = requireNonNull(transactionId, "transactionId is null"); this.clientTransactionSupport = clientTransactionSupport; this.identity = identity; this.source = requireNonNull(source, "source is null"); this.catalog = requireNonNull(catalog, "catalog is null"); this.schema = requireNonNull(schema, "schema is null"); this.timeZoneKey = requireNonNull(timeZoneKey, "timeZoneKey is null"); this.locale = requireNonNull(locale, "locale is null"); this.remoteUserAddress = requireNonNull(remoteUserAddress, "remoteUserAddress is null"); this.userAgent = requireNonNull(userAgent, "userAgent is null"); this.startTime = startTime; this.systemProperties = ImmutableMap.copyOf(requireNonNull(systemProperties, "systemProperties is null")); this.sessionPropertyManager = requireNonNull(sessionPropertyManager, "sessionPropertyManager is null"); ImmutableMap.Builder<String, Map<String, String>> catalogPropertiesBuilder = ImmutableMap.<String, Map<String, String>>builder(); catalogProperties.entrySet().stream() .map(entry -> Maps.immutableEntry(entry.getKey(), ImmutableMap.copyOf(entry.getValue()))) .forEach(catalogPropertiesBuilder::put); this.catalogProperties = catalogPropertiesBuilder.build(); checkArgument(catalog.isPresent() || !schema.isPresent(), "schema is set but catalog is not"); }
private static TimeZoneKey getTimeZoneKey(String timeZoneId) { try { return TimeZoneKey.getTimeZoneKey(timeZoneId); } catch (TimeZoneNotSupportedException e) { throw badRequest(e.getMessage()); } }
private Visitor(FunctionKind functionKind, IdentityHashMap<Expression, Type> types, TypeManager typeManager, TimeZoneKey timeZoneKey) { this.functionKind = functionKind; this.types = types; this.typeManager = typeManager; this.timeZoneKey = timeZoneKey; }
public static long parseTimestampLiteral(TimeZoneKey timeZoneKey, String value) { try { DateTime dateTime = TIMESTAMP_WITH_TIME_ZONE_FORMATTER.parseDateTime(value); return packDateTimeWithZone(dateTime); } catch (Exception e) { return TIMESTAMP_WITHOUT_TIME_ZONE_FORMATTER.withChronology(getChronology(timeZoneKey)).parseMillis(value); } }
public static long parseTime(TimeZoneKey timeZoneKey, String value) { try { return parseTimeWithTimeZone(value); } catch (Exception e) { return parseTimeWithoutTimeZone(timeZoneKey, value); } }
@Test public void smokedTest() throws Exception { assertExecute("cast(true as boolean)", BOOLEAN, true); assertExecute("true", BOOLEAN, true); assertExecute("false", BOOLEAN, false); assertExecute("42", BIGINT, 42L); assertExecute("'foo'", VARCHAR, "foo"); assertExecute("4.2", DOUBLE, 4.2); assertExecute("1 + 1", BIGINT, 2L); assertExecute("X' 1 f'", VARBINARY, new SqlVarbinary(Slices.wrappedBuffer((byte) 0x1f).getBytes())); assertExecute("X' '", VARBINARY, new SqlVarbinary(new byte[0])); assertExecute("bound_long", BIGINT, 1234L); assertExecute("bound_string", VARCHAR, "hello"); assertExecute("bound_double", DOUBLE, 12.34); assertExecute("bound_boolean", BOOLEAN, true); assertExecute("bound_timestamp", BIGINT, new DateTime(2001, 8, 22, 3, 4, 5, 321, UTC).getMillis()); assertExecute("bound_pattern", VARCHAR, "%el%"); assertExecute("bound_null_string", VARCHAR, null); assertExecute("bound_timestamp_with_timezone", TIMESTAMP_WITH_TIME_ZONE, new SqlTimestampWithTimeZone(new DateTime(1970, 1, 1, 0, 1, 0, 999, DateTimeZone.UTC).getMillis(), TimeZoneKey.getTimeZoneKey("Z"))); assertExecute("bound_binary_literal", VARBINARY, new SqlVarbinary(new byte[]{(byte) 0xab})); // todo enable when null output type is supported // assertExecute("null", null); Futures.allAsList(futures).get(); }
protected ConnectorSession makeSessionWithTimeZone(String tzId) { return Session.builder(propManager) .setIdentity(new Identity("user", Optional.empty())) .setSource("source") .setCatalog("kinesis") .setSchema("default") .setTimeZoneKey(TimeZoneKey.getTimeZoneKey(tzId)) .setLocale(ENGLISH) .setQueryId(new QueryId("dummy")) .build().toConnectorSession(new ConnectorId("kinesis")); }
@BeforeClass public void start() { // Create dependent objects, including the minimal config needed for this test Map<String, String> properties = new ImmutableMap.Builder<String, String>() .put("kinesis.table-description-dir", "etc/kinesis") .put("kinesis.default-schema", "kinesis") .put("kinesis.hide-internal-columns", "false") .build(); KinesisPlugin kinesisPlugin = TestUtils.createPluginInstance(); KinesisConnector connector = TestUtils.createConnector(kinesisPlugin, properties, true); injector = kinesisPlugin.getInjector(); assertNotNull(injector); protoSession = Session.builder(propManager) .setIdentity(new Identity("user", Optional.empty())) .setSource("source") .setCatalog("kinesis") .setSchema("default") .setTimeZoneKey(TimeZoneKey.getTimeZoneKey("America/Los_Angeles")) .setLocale(ENGLISH) .setQueryId(new QueryId("dummy")) .build(); session = protoSession.toConnectorSession(new ConnectorId("kinesis")); // Connector needs to tell Presto about the session properties it supports propManager.addConnectorSessionProperties(new ConnectorId("kinesis"), connector.getSessionProperties()); }
private static Object convertToRowValue(Type type, Object value, TimeZoneKey timeZoneKey) { if (value == null) { return null; } if (BOOLEAN.equals(type)) { return value; } else if (BIGINT.equals(type)) { return ((Number) value).longValue(); } else if (DOUBLE.equals(type)) { return ((Number) value).doubleValue(); } else if (VARCHAR.equals(type)) { return value; } else if (VARBINARY.equals(type)) { return value; } else if (DATE.equals(type)) { int days = parseDate((String) value); return new Date(TimeUnit.DAYS.toMillis(days)); } else if (TIME.equals(type)) { return new Time(parseTime(timeZoneKey, (String) value)); } else if (TIME_WITH_TIME_ZONE.equals(type)) { return new Time(unpackMillisUtc(parseTimeWithTimeZone((String) value))); } else if (TIMESTAMP.equals(type)) { return new Timestamp(parseTimestampWithoutTimeZone(timeZoneKey, (String) value)); } else if (TIMESTAMP_WITH_TIME_ZONE.equals(type)) { return new Timestamp(unpackMillisUtc(parseTimestampWithTimeZone(timeZoneKey, (String) value))); } else if (type instanceof ArrayType) { return ((List<Object>) value).stream() .map(element -> convertToRowValue(((ArrayType) type).getElementType(), element, timeZoneKey)) .collect(toList()); } else { throw new AssertionError("unhandled type: " + type); } }
@JsonProperty public TimeZoneKey getTimeZoneKey() { return timeZoneKey; }
@Override public TimeZoneKey getTimeZoneKey() { return timeZoneKey; }
public TimeZoneKey getTimeZoneKey() { return timeZoneKey; }
public SessionBuilder setTimeZoneKey(TimeZoneKey timeZoneKey) { this.timeZoneKey = timeZoneKey; return this; }
public static ISOChronology getChronology(TimeZoneKey zoneKey) { return CHRONOLOGIES[zoneKey.getKey()]; }
public static DateTimeZone getDateTimeZone(TimeZoneKey zoneKey) { return DATE_TIME_ZONES[zoneKey.getKey()]; }
public static long parseTimestampWithTimeZone(TimeZoneKey timeZoneKey, String timestampWithTimeZone) { DateTime dateTime = TIMESTAMP_WITH_OR_WITHOUT_TIME_ZONE_FORMATTER.withChronology(getChronology(timeZoneKey)).withOffsetParsed().parseDateTime(timestampWithTimeZone); return packDateTimeWithZone(dateTime); }
public static long parseTimestampWithoutTimeZone(TimeZoneKey timeZoneKey, String value) { return TIMESTAMP_WITH_OR_WITHOUT_TIME_ZONE_FORMATTER.withChronology(getChronology(timeZoneKey)).parseMillis(value); }
public static String printTimestampWithoutTimeZone(TimeZoneKey timeZoneKey, long timestamp) { return TIMESTAMP_WITHOUT_TIME_ZONE_FORMATTER.withChronology(getChronology(timeZoneKey)).print(timestamp); }
public static long parseTimeWithoutTimeZone(TimeZoneKey timeZoneKey, String value) { return TIME_FORMATTER.withZone(getDateTimeZone(timeZoneKey)).parseMillis(value); }
public static String printTimeWithoutTimeZone(TimeZoneKey timeZoneKey, long value) { return TIME_FORMATTER.withZone(getDateTimeZone(timeZoneKey)).print(value); }
@Override public TimeZoneKey getTimeZoneKey() { return UTC_KEY; }
TimeZoneKey getTimeZoneKey();