@Test public void testTimestampWithZeroTimezone() throws Exception { Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"), Locale.ROOT); TestTableProvider tableProvider = new TestTableProvider(); Connection connection = JdbcDriver.connect(tableProvider); // A table with one TIMESTAMP column Schema schema = Schema.builder().addDateTimeField("ts").build(); connection .createStatement() .executeUpdate("CREATE EXTERNAL TABLE test (ts TIMESTAMP) TYPE 'test'"); ReadableInstant july1 = ISODateTimeFormat.dateTimeParser().parseDateTime("2018-07-01T01:02:03Z"); tableProvider.addRows("test", Row.withSchema(schema).addValue(july1).build()); ResultSet selectResult = connection.createStatement().executeQuery(String.format("SELECT ts FROM test")); selectResult.next(); Timestamp ts = selectResult.getTimestamp(1, cal); assertThat( String.format( "Wrote %s to a table, but got back %s", ISODateTimeFormat.basicDateTime().print(july1), ISODateTimeFormat.basicDateTime().print(ts.getTime())), ts.getTime(), equalTo(july1.getMillis())); }
@Test @Ignore("https://issues.apache.org/jira/browse/CALCITE-2394") public void testTimestampWithNonzeroTimezone() throws Exception { Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Asia/Tokyo"), Locale.ROOT); TestTableProvider tableProvider = new TestTableProvider(); Connection connection = JdbcDriver.connect(tableProvider); // A table with one TIMESTAMP column Schema schema = Schema.builder().addDateTimeField("ts").build(); connection .createStatement() .executeUpdate("CREATE EXTERNAL TABLE test (ts TIMESTAMP) TYPE 'test'"); ReadableInstant july1 = ISODateTimeFormat.dateTimeParser().parseDateTime("2018-07-01T01:02:03Z"); tableProvider.addRows("test", Row.withSchema(schema).addValue(july1).build()); ResultSet selectResult = connection.createStatement().executeQuery(String.format("SELECT ts FROM test")); selectResult.next(); Timestamp ts = selectResult.getTimestamp(1, cal); assertThat( String.format( "Wrote %s to a table, but got back %s", ISODateTimeFormat.basicDateTime().print(july1), ISODateTimeFormat.basicDateTime().print(ts.getTime())), ts.getTime(), equalTo(july1.getMillis())); }
@Test public void testTimestampWithDefaultTimezone() throws Exception { TestTableProvider tableProvider = new TestTableProvider(); Connection connection = JdbcDriver.connect(tableProvider); // A table with one TIMESTAMP column Schema schema = Schema.builder().addDateTimeField("ts").build(); connection .createStatement() .executeUpdate("CREATE EXTERNAL TABLE test (ts TIMESTAMP) TYPE 'test'"); ReadableInstant july1 = ISODateTimeFormat.dateTimeParser().parseDateTime("2018-07-01T01:02:03Z"); tableProvider.addRows("test", Row.withSchema(schema).addValue(july1).build()); ResultSet selectResult = connection.createStatement().executeQuery(String.format("SELECT ts FROM test")); selectResult.next(); Timestamp ts = selectResult.getTimestamp(1); assertThat( String.format( "Wrote %s to a table, but got back %s", ISODateTimeFormat.basicDateTime().print(july1), ISODateTimeFormat.basicDateTime().print(ts.getTime())), ts.getTime(), equalTo(july1.getMillis())); }
.addDoubleField("f_double") .addStringField("f_string") .addDateTimeField("f_datetime") .addBooleanField("f_boolean") .build();
public void testTriggeredTumble() throws Exception { Schema inputSchema = Schema.builder().addInt32Field("f_int").addDateTimeField("f_timestamp").build();
.addInt32Field("f_intGroupingKey") .addInt32Field("f_intValue") .addDateTimeField("f_timestamp") .build();
.addInt32Field("f_int2") .addInt64Field("size") .addDateTimeField("window_start") .addDateTimeField("window_end") .build();
.addInt32Field("f_intGroupingKey") .addInt32Field("f_intValue") .addDateTimeField("f_timestamp") .build();
.addInt32Field("f_int2") .addInt64Field("size") .addDateTimeField("window_start") .addDateTimeField("window_end") .build();
SerializableFunctions.identity())); Schema resultType = Schema.builder().addInt32Field("f_int").addDateTimeField("f_date").build();
.addInt32Field("price") .addInt32Field("site_id") .addDateTimeField("timestamp") .build()) .addRows(
.addInt32Field("f_int2") .addInt64Field("size") .addDateTimeField("window_start") .addDateTimeField("window_end") .build();
.addDoubleField("f_double") .addStringField("f_string") .addDateTimeField("f_datetime") .addBooleanField("f_boolean") .build();
/** Test Joda time UDF/UDAF. */ @Test public void testJodaTimeUdfUdaf() throws Exception { Schema resultType = Schema.builder().addDateTimeField("jodatime").build(); Row row1 = Row.withSchema(resultType) .addValues(parseTimestampWithoutTimeZone("2017-01-01 02:04:03")) .build(); String sql1 = "SELECT MAX_JODA(f_timestamp) as jodatime FROM PCOLLECTION"; PCollection<Row> result1 = boundedInput1.apply( "testJodaUdaf", SqlTransform.query(sql1).registerUdaf("MAX_JODA", new JodaMax())); PAssert.that(result1).containsInAnyOrder(row1); Row row2 = Row.withSchema(resultType) .addValues(parseTimestampWithoutTimeZone("2016-12-31 01:01:03")) .build(); String sql2 = "SELECT PRE_DAY(f_timestamp) as jodatime FROM PCOLLECTION WHERE f_int=1"; PCollection<Row> result2 = boundedInput1.apply( "testJodaUdf", SqlTransform.query(sql2).registerUdf("PRE_DAY", JodaPreviousDay.class)); PAssert.that(result2).containsInAnyOrder(row2); pipeline.run().waitUntilFinish(); }
@Test public void testPrimitiveTypes() throws Exception { Schema schema = Schema.builder() .addByteField("f_byte") .addInt16Field("f_int16") .addInt32Field("f_int32") .addInt64Field("f_int64") .addDecimalField("f_decimal") .addFloatField("f_float") .addDoubleField("f_double") .addStringField("f_string") .addDateTimeField("f_datetime") .addBooleanField("f_boolean") .build(); DateTime dateTime = new DateTime().withDate(1979, 03, 14).withTime(1, 2, 3, 4).withZone(DateTimeZone.UTC); Row row = Row.withSchema(schema) .addValues( (byte) 0, (short) 1, 2, 3L, new BigDecimal(2.3), 1.2f, 3.0d, "str", dateTime, false) .build(); CoderProperties.coderDecodeEncodeEqual(RowCoder.of(schema), row); }
@Test public void testThrowsIfExtraFieldsExist() { PubsubJsonTableProvider provider = new PubsubJsonTableProvider(); Schema messageSchema = Schema.builder() .addDateTimeField("event_timestamp") .addMapField("attributes", VARCHAR, VARCHAR) .addStringField("someField") .addRowField("payload", Schema.builder().build()) .build(); Table tableDefinition = tableDefinition().schema(messageSchema).build(); thrown.expectMessage("Unsupported"); thrown.expectMessage("'event_timestamp'"); provider.buildBeamSqlTable(tableDefinition); }
@Test public void testCreatesTable() { PubsubJsonTableProvider provider = new PubsubJsonTableProvider(); Schema messageSchema = Schema.builder() .addDateTimeField("event_timestamp") .addMapField("attributes", VARCHAR, VARCHAR) .addRowField("payload", Schema.builder().build()) .build(); Table tableDefinition = tableDefinition().schema(messageSchema).build(); BeamSqlTable pubsubTable = provider.buildBeamSqlTable(tableDefinition); assertNotNull(pubsubTable); assertEquals(messageSchema, pubsubTable.getSchema()); }
@Test public void testThrowsIfPayloadFieldNotProvided() { PubsubJsonTableProvider provider = new PubsubJsonTableProvider(); Schema messageSchema = Schema.builder() .addDateTimeField("event_timestamp") .addMapField("attributes", VARCHAR, VARCHAR) .build(); Table tableDefinition = tableDefinition().schema(messageSchema).build(); thrown.expectMessage("Unsupported"); thrown.expectMessage("'payload'"); provider.buildBeamSqlTable(tableDefinition); }
@Test public void testThrowsIfAttributesFieldNotProvided() { PubsubJsonTableProvider provider = new PubsubJsonTableProvider(); Schema messageSchema = Schema.builder() .addDateTimeField("event_timestamp") .addRowField("payload", Schema.builder().build()) .build(); Table tableDefinition = tableDefinition().schema(messageSchema).build(); thrown.expectMessage("Unsupported"); thrown.expectMessage("'attributes'"); provider.buildBeamSqlTable(tableDefinition); }
@Test public void testThrowsForUnsupportedType() throws Exception { Schema schema = Schema.builder().addDateTimeField("f_dateTime").build(); thrown.expect(UnsupportedRowJsonException.class); thrown.expectMessage("DATETIME is not supported"); RowJsonDeserializer.forSchema(schema); }