private static Slice dateFormat(ISOChronology chronology, Locale locale, long timestamp, Slice formatString) { DateTimeFormatter formatter = DATETIME_FORMATTER_CACHE.get(formatString) .withChronology(chronology) .withLocale(locale); return utf8Slice(formatter.print(timestamp)); }
@Test(expectedExceptions = NullPointerException.class, expectedExceptionsMessageRegExp = "loader returned null value") public void testDisallowsNulls() { new ThreadLocalCache<>(10, key -> null).get("foo"); } }
@Test(expectedExceptions = NullPointerException.class, expectedExceptionsMessageRegExp = "loader returned null value") public void testDisallowsNulls() { new ThreadLocalCache<>(10, key -> null).get("foo"); } }
@Test(expectedExceptions = NullPointerException.class, expectedExceptionsMessageRegExp = "loader returned null value") public void testDisallowsNulls() { new ThreadLocalCache<>(10, key -> null).get("foo"); } }
@ScalarFunction @LiteralParameters({"x", "y"}) @SqlType(StandardTypes.TIMESTAMP) public static long dateParse(ConnectorSession session, @SqlType("varchar(x)") Slice dateTime, @SqlType("varchar(y)") Slice formatString) { DateTimeFormatter formatter = DATETIME_FORMATTER_CACHE.get(formatString) .withChronology(session.isLegacyTimestamp() ? getChronology(session.getTimeZoneKey()) : UTC_CHRONOLOGY) .withLocale(session.getLocale()); try { return formatter.parseMillis(dateTime.toStringUtf8()); } catch (IllegalArgumentException e) { throw new PrestoException(INVALID_FUNCTION_ARGUMENT, e); } }
@Test public void testSanity() throws Exception { AtomicInteger count = new AtomicInteger(0); ThreadLocalCache<String, String> cache = new ThreadLocalCache<>(2, key -> { // Concatenate key with counter return key + count.getAndAdd(1); }); // Load first key assertEquals(cache.get("abc"), "abc0"); assertEquals(cache.get("abc"), "abc0"); // Load second key assertEquals(cache.get("def"), "def1"); // First key should still be there assertEquals(cache.get("abc"), "abc0"); // Expire first key by exceeding max size assertEquals(cache.get("ghi"), "ghi2"); // First key should now be regenerated assertEquals(cache.get("abc"), "abc3"); // TODO: add tests for multiple threads }
private static Slice dateFormat(ISOChronology chronology, Locale locale, long timestamp, Slice formatString) { DateTimeFormatter formatter = DATETIME_FORMATTER_CACHE.get(formatString) .withChronology(chronology) .withLocale(locale); return utf8Slice(formatter.print(timestamp)); }
@Test public void testSanity() throws Exception { AtomicInteger count = new AtomicInteger(0); ThreadLocalCache<String, String> cache = new ThreadLocalCache<>(2, key -> { // Concatenate key with counter return key + count.getAndAdd(1); }); // Load first key assertEquals(cache.get("abc"), "abc0"); assertEquals(cache.get("abc"), "abc0"); // Load second key assertEquals(cache.get("def"), "def1"); // First key should still be there assertEquals(cache.get("abc"), "abc0"); // Expire first key by exceeding max size assertEquals(cache.get("ghi"), "ghi2"); // First key should now be regenerated assertEquals(cache.get("abc"), "abc3"); // TODO: add tests for multiple threads }
private static Slice dateFormat(ISOChronology chronology, Locale locale, long timestamp, Slice formatString) { DateTimeFormatter formatter = DATETIME_FORMATTER_CACHE.get(formatString) .withChronology(chronology) .withLocale(locale); return utf8Slice(formatter.print(timestamp)); }
@Test public void testSanity() throws Exception { AtomicInteger count = new AtomicInteger(0); ThreadLocalCache<String, String> cache = new ThreadLocalCache<>(2, key -> { // Concatenate key with counter return key + count.getAndAdd(1); }); // Load first key assertEquals(cache.get("abc"), "abc0"); assertEquals(cache.get("abc"), "abc0"); // Load second key assertEquals(cache.get("def"), "def1"); // First key should still be there assertEquals(cache.get("abc"), "abc0"); // Expire first key by exceeding max size assertEquals(cache.get("ghi"), "ghi2"); // First key should now be regenerated assertEquals(cache.get("abc"), "abc3"); // TODO: add tests for multiple threads }
private static long parseMillis(TimeZoneKey timeZoneKey, Locale locale, Slice dateTime, Slice formatString) { DateTimeFormatter formatter = DATETIME_FORMATTER_CACHE.get(formatString) .withChronology(CHRONOLOGIES[timeZoneKey.getKey()]) .withLocale(locale); try { return formatter.parseMillis(dateTime.toString(UTF_8)); } catch (IllegalArgumentException e) { throw new PrestoException(INVALID_FUNCTION_ARGUMENT, e); } } }
@Description("Formats a timestamp") @ScalarFunction("to_char") @SqlType(StandardTypes.VARCHAR) public static Slice toChar( ConnectorSession session, @SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) long timestampWithTimeZone, @SqlType(StandardTypes.VARCHAR) Slice formatString) { DateTimeFormatter formatter = DATETIME_FORMATTER_CACHE.get(formatString) .withChronology(CHRONOLOGIES[unpackZoneKey(timestampWithTimeZone).getKey()]) .withLocale(session.getLocale()); return utf8Slice(formatter.print(unpackMillisUtc(timestampWithTimeZone))); }
@ScalarFunction @LiteralParameters({"x", "y"}) @SqlType(StandardTypes.TIMESTAMP) public static long dateParse(ConnectorSession session, @SqlType("varchar(x)") Slice dateTime, @SqlType("varchar(y)") Slice formatString) { DateTimeFormatter formatter = DATETIME_FORMATTER_CACHE.get(formatString) .withChronology(session.isLegacyTimestamp() ? getChronology(session.getTimeZoneKey()) : UTC_CHRONOLOGY) .withLocale(session.getLocale()); try { return formatter.parseMillis(dateTime.toStringUtf8()); } catch (IllegalArgumentException e) { throw new PrestoException(INVALID_FUNCTION_ARGUMENT, e); } }
@ScalarFunction @LiteralParameters({"x", "y"}) @SqlType(StandardTypes.TIMESTAMP) public static long dateParse(ConnectorSession session, @SqlType("varchar(x)") Slice dateTime, @SqlType("varchar(y)") Slice formatString) { DateTimeFormatter formatter = DATETIME_FORMATTER_CACHE.get(formatString) .withChronology(session.isLegacyTimestamp() ? getChronology(session.getTimeZoneKey()) : UTC_CHRONOLOGY) .withLocale(session.getLocale()); try { return formatter.parseMillis(dateTime.toStringUtf8()); } catch (IllegalArgumentException e) { throw new PrestoException(INVALID_FUNCTION_ARGUMENT, e); } }