@Test public void testSeconds() throws NumericException, SqlException { StringSink sink = new StringSink(); for (int k = 0; k < 61; k++) { sink.clear(); if (k > 0) { sink.put(k).put('s'); } else { sink.put('s'); } TimestampSampler sampler = TimestampSamplerFactory.getInstance(sink, 120); Assert.assertNotNull(sampler); final long n = Timestamps.SECOND_MICROS * (k == 0 ? 1 : k); long timestamp = TimestampFormatUtils.parseTimestamp("2018-04-15T10:23:00.000000Z"); timestamp = timestamp - timestamp % n; for (int i = 0; i < n; i += 4) { long actual = sampler.round(timestamp + i); if (timestamp != actual) { Assert.fail("Failed at: " + sink + ". Expected: " + Timestamps.toString(timestamp) + ", actual: " + Timestamps.toString(actual)); } } } }
public static TimestampSampler getInstance(CharSequence cs, int position) throws SqlException { int k = -1; if (cs == null) { throw SqlException.$(position, "missing interval"); } final int len = cs.length(); for (int i = 0; i < len; i++) { char c = cs.charAt(i); if (c < '0' || c > '9') { k = i; break; } } if (k == -1) { throw SqlException.$(position + len, "expected interval qualifier"); } if (k + 1 < len) { throw SqlException.$(position + k, "expected single letter qualifier"); } try { final int n; if (k == 0) { n = 1; } else { n = Numbers.parseInt(cs, 0, k); if (n == 0) { throw SqlException.$(position, "zero is not a valid sample value"); } } switch (cs.charAt(k)) { case 'T': return new MicroTimestampSampler(Timestamps.MILLI_MICROS * n); case 's': return new MicroTimestampSampler(Timestamps.SECOND_MICROS * n); case 'm': return new MicroTimestampSampler(Timestamps.MINUTE_MICROS * n); case 'h': return new MicroTimestampSampler(Timestamps.HOUR_MICROS * n); case 'd': return new MicroTimestampSampler(Timestamps.DAY_MICROS * n); case 'M': return new MonthTimestampSampler(n); case 'y': return new YearTimestampSampler(n); default: break; } } catch (NumericException ignore) { assert false; } throw SqlException.$(position + k, "unsupported interval qualifier"); }
TimestampSamplerFactory { public static TimestampSampler getInstance(CharSequence cs, int position) throws SqlException { int k = -1; if (cs == null) { throw SqlException.$(position, "missing interval"); } final int len = cs.length(); for (int i = 0; i < len; i++) { char c = cs.charAt(i); if (c < '0' || c > '9') { k = i; break; } } if (k == -1) { throw SqlException.$(position + len, "expected interval qualifier"); } if (k + 1 < len) { throw SqlException.$(position + k, "expected single letter qualifier"); } try { final int n; if (k == 0) { n = 1; } else { n = Numbers.parseInt(cs, 0, k); if (n == 0) { throw SqlException.$(position, "zero is not a valid sample value"); } } switch (cs.charAt(k)) { case 'T': return new MicroTimestampSampler(Timestamps.MILLI_MICROS * n); case 's': return new MicroTimestampSampler(Timestamps.SECOND_MICROS * n); case 'm': return new MicroTimestampSampler(Timestamps.MINUTE_MICROS * n); case 'h': return new MicroTimestampSampler(Timestamps.HOUR_MICROS * n); case 'd': return new MicroTimestampSampler(Timestamps.DAY_MICROS * n); case 'M': return new MonthTimestampSampler(n); case 'y': return new YearTimestampSampler(n); default: break; } } catch (NumericException ignore) { assert false; } throw SqlException.$(position + k, "unsupported interval qualifier"); } }
TimestampSamplerFactory { public static TimestampSampler getInstance(CharSequence cs, int position) throws SqlException { int k = -1; if (cs == null) { throw SqlException.$(position, "missing interval"); } final int len = cs.length(); for (int i = 0; i < len; i++) { char c = cs.charAt(i); if (c < '0' || c > '9') { k = i; break; } } if (k == -1) { throw SqlException.$(position + len, "expected interval qualifier"); } if (k + 1 < len) { throw SqlException.$(position + k, "expected single letter qualifier"); } try { final int n; if (k == 0) { n = 1; } else { n = Numbers.parseInt(cs, 0, k); if (n == 0) { throw SqlException.$(position, "zero is not a valid sample value"); } } switch (cs.charAt(k)) { case 'T': return new MicroTimestampSampler(Timestamps.MILLI_MICROS * n); case 's': return new MicroTimestampSampler(Timestamps.SECOND_MICROS * n); case 'm': return new MicroTimestampSampler(Timestamps.MINUTE_MICROS * n); case 'h': return new MicroTimestampSampler(Timestamps.HOUR_MICROS * n); case 'd': return new MicroTimestampSampler(Timestamps.DAY_MICROS * n); case 'M': return new MonthTimestampSampler(n); case 'y': return new YearTimestampSampler(n); default: break; } } catch (NumericException ignore) { assert false; } throw SqlException.$(position + k, "unsupported interval qualifier"); }  }
TimestampSamplerFactory { public static TimestampSampler getInstance(CharSequence cs, int position) throws SqlException { int k = -1; if (cs == null) { throw SqlException.$(position, "missing interval"); } final int len = cs.length(); for (int i = 0; i < len; i++) { char c = cs.charAt(i); if (c < '0' || c > '9') { k = i; break; } } if (k == -1) { throw SqlException.$(position + len, "expected interval qualifier"); } if (k + 1 < len) { throw SqlException.$(position + k, "expected single letter qualifier"); } try { final int n; if (k == 0) { n = 1; } else { n = Numbers.parseInt(cs, 0, k); if (n == 0) { throw SqlException.$(position, "zero is not a valid sample value"); } } switch (cs.charAt(k)) { case 'T': return new MicroTimestampSampler(Timestamps.MILLI_MICROS * n); case 's': return new MicroTimestampSampler(Timestamps.SECOND_MICROS * n); case 'm': return new MicroTimestampSampler(Timestamps.MINUTE_MICROS * n); case 'h': return new MicroTimestampSampler(Timestamps.HOUR_MICROS * n); case 'd': return new MicroTimestampSampler(Timestamps.DAY_MICROS * n); case 'M': return new MonthTimestampSampler(n); case 'y': return new YearTimestampSampler(n); default: break; } } catch (NumericException ignore) { assert false; } throw SqlException.$(position + k, "unsupported interval qualifier"); }  static TimestampSampler getInstance(CharSequence cs, int position); }
TimestampSamplerFactory { public static TimestampSampler getInstance(CharSequence cs, int position) throws SqlException { int k = -1; if (cs == null) { throw SqlException.$(position, "missing interval"); } final int len = cs.length(); for (int i = 0; i < len; i++) { char c = cs.charAt(i); if (c < '0' || c > '9') { k = i; break; } } if (k == -1) { throw SqlException.$(position + len, "expected interval qualifier"); } if (k + 1 < len) { throw SqlException.$(position + k, "expected single letter qualifier"); } try { final int n; if (k == 0) { n = 1; } else { n = Numbers.parseInt(cs, 0, k); if (n == 0) { throw SqlException.$(position, "zero is not a valid sample value"); } } switch (cs.charAt(k)) { case 'T': return new MicroTimestampSampler(Timestamps.MILLI_MICROS * n); case 's': return new MicroTimestampSampler(Timestamps.SECOND_MICROS * n); case 'm': return new MicroTimestampSampler(Timestamps.MINUTE_MICROS * n); case 'h': return new MicroTimestampSampler(Timestamps.HOUR_MICROS * n); case 'd': return new MicroTimestampSampler(Timestamps.DAY_MICROS * n); case 'M': return new MonthTimestampSampler(n); case 'y': return new YearTimestampSampler(n); default: break; } } catch (NumericException ignore) { assert false; } throw SqlException.$(position + k, "unsupported interval qualifier"); }  static TimestampSampler getInstance(CharSequence cs, int position);  }