Search in sources :

Example 76 with BeamRelNode

use of org.apache.beam.sdk.extensions.sql.impl.rel.BeamRelNode in project beam by apache.

the class ZetaSqlTimeFunctionsTest method testTimestampLiteralWithUTCTimeZone.

@Test
public void testTimestampLiteralWithUTCTimeZone() {
    String sql = "SELECT TIMESTAMP '2016-12-25 05:30:00+00'";
    ZetaSQLQueryPlanner zetaSQLQueryPlanner = new ZetaSQLQueryPlanner(config);
    BeamRelNode beamRelNode = zetaSQLQueryPlanner.convertToBeamRel(sql);
    PCollection<Row> stream = BeamSqlRelUtils.toPCollection(pipeline, beamRelNode);
    PAssert.that(stream).containsInAnyOrder(Row.withSchema(Schema.builder().addDateTimeField("field1").build()).addValues(parseTimestampWithUTCTimeZone("2016-12-25 05:30:00")).build());
    pipeline.run().waitUntilFinish(Duration.standardMinutes(PIPELINE_EXECUTION_WAITTIME_MINUTES));
}
Also used : BeamRelNode(org.apache.beam.sdk.extensions.sql.impl.rel.BeamRelNode) Row(org.apache.beam.sdk.values.Row) Test(org.junit.Test)

Example 77 with BeamRelNode

use of org.apache.beam.sdk.extensions.sql.impl.rel.BeamRelNode in project beam by apache.

the class ZetaSqlTimeFunctionsTest method testExtractDateTimeFromTimestamp.

@Test
public void testExtractDateTimeFromTimestamp() {
    String sql = "SELECT EXTRACT(DATETIME FROM TIMESTAMP '2017-05-26 12:34:56')";
    ZetaSQLQueryPlanner zetaSQLQueryPlanner = new ZetaSQLQueryPlanner(config);
    BeamRelNode beamRelNode = zetaSQLQueryPlanner.convertToBeamRel(sql);
    PCollection<Row> stream = BeamSqlRelUtils.toPCollection(pipeline, beamRelNode);
    PAssert.that(stream).containsInAnyOrder(Row.withSchema(Schema.builder().addLogicalTypeField("datetime", SqlTypes.DATETIME).build()).addValues(LocalDateTime.of(2017, 5, 26, 12, 34, 56)).build());
    pipeline.run().waitUntilFinish(Duration.standardMinutes(PIPELINE_EXECUTION_WAITTIME_MINUTES));
}
Also used : BeamRelNode(org.apache.beam.sdk.extensions.sql.impl.rel.BeamRelNode) Row(org.apache.beam.sdk.values.Row) Test(org.junit.Test)

Example 78 with BeamRelNode

use of org.apache.beam.sdk.extensions.sql.impl.rel.BeamRelNode in project beam by apache.

the class ZetaSqlTimeFunctionsTest method testExtractFromDateTime.

// TODO[BEAM-9166]: Add a test for CURRENT_DATETIME function ("SELECT CURRENT_DATETIME()")
@Test
public void testExtractFromDateTime() {
    String sql = "SELECT " + "EXTRACT(YEAR FROM DATETIME '2008-12-25 15:30:00') as year, " + "EXTRACT(QUARTER FROM DATETIME '2008-12-25 15:30:00') as quarter, " + "EXTRACT(MONTH FROM DATETIME '2008-12-25 15:30:00') as month, " + // + "EXTRACT(WEEK FROM DATETIME '2008-12-25 15:30:00') as week, "
    "EXTRACT(DAY FROM DATETIME '2008-12-25 15:30:00') as day, " + "EXTRACT(DAYOFWEEK FROM DATETIME '2008-12-25 15:30:00') as dayofweek, " + "EXTRACT(DAYOFYEAR FROM DATETIME '2008-12-25 15:30:00') as dayofyear, " + "EXTRACT(HOUR FROM DATETIME '2008-12-25 15:30:00.123456') as hour, " + "EXTRACT(MINUTE FROM DATETIME '2008-12-25 15:30:00.123456') as minute, " + "EXTRACT(SECOND FROM DATETIME '2008-12-25 15:30:00.123456') as second, " + "EXTRACT(MILLISECOND FROM DATETIME '2008-12-25 15:30:00.123456') as millisecond, " + "EXTRACT(MICROSECOND FROM DATETIME '2008-12-25 15:30:00.123456') as microsecond, " + "EXTRACT(DATE FROM DATETIME '2008-12-25 15:30:00.123456') as date, " + "EXTRACT(TIME FROM DATETIME '2008-12-25 15:30:00.123456') as time ";
    ZetaSQLQueryPlanner zetaSQLQueryPlanner = new ZetaSQLQueryPlanner(config);
    BeamRelNode beamRelNode = zetaSQLQueryPlanner.convertToBeamRel(sql);
    PCollection<Row> stream = BeamSqlRelUtils.toPCollection(pipeline, beamRelNode);
    final Schema schema = Schema.builder().addInt64Field("year").addInt64Field("quarter").addInt64Field("month").addInt64Field("day").addInt64Field("dayofweek").addInt64Field("dayofyear").addInt64Field("hour").addInt64Field("minute").addInt64Field("second").addInt64Field("millisecond").addInt64Field("microsecond").addLogicalTypeField("date", SqlTypes.DATE).addLogicalTypeField("time", SqlTypes.TIME).build();
    PAssert.that(stream).containsInAnyOrder(Row.withSchema(schema).addValues(2008L, 4L, 12L, // 52L,
    25L, 5L, 360L, 15L, 30L, 0L, 123L, 123456L, LocalDate.of(2008, 12, 25), LocalTime.of(15, 30, 0, 123456000)).build());
    pipeline.run().waitUntilFinish(Duration.standardMinutes(PIPELINE_EXECUTION_WAITTIME_MINUTES));
}
Also used : BeamRelNode(org.apache.beam.sdk.extensions.sql.impl.rel.BeamRelNode) Schema(org.apache.beam.sdk.schemas.Schema) Row(org.apache.beam.sdk.values.Row) Test(org.junit.Test)

Example 79 with BeamRelNode

use of org.apache.beam.sdk.extensions.sql.impl.rel.BeamRelNode in project beam by apache.

the class ZetaSqlTimeFunctionsTest method testAggregateOnDate.

@Test
public void testAggregateOnDate() {
    String sql = "SELECT MAX(date_field) FROM table_with_date GROUP BY str_field";
    ZetaSQLQueryPlanner zetaSQLQueryPlanner = new ZetaSQLQueryPlanner(config);
    BeamRelNode beamRelNode = zetaSQLQueryPlanner.convertToBeamRel(sql);
    PCollection<Row> stream = BeamSqlRelUtils.toPCollection(pipeline, beamRelNode);
    PAssert.that(stream).containsInAnyOrder(Row.withSchema(Schema.builder().addLogicalTypeField("date_field", SqlTypes.DATE).build()).addValues(LocalDate.of(2020, 4, 7)).build());
    pipeline.run().waitUntilFinish(Duration.standardMinutes(PIPELINE_EXECUTION_WAITTIME_MINUTES));
}
Also used : BeamRelNode(org.apache.beam.sdk.extensions.sql.impl.rel.BeamRelNode) Row(org.apache.beam.sdk.values.Row) Test(org.junit.Test)

Example 80 with BeamRelNode

use of org.apache.beam.sdk.extensions.sql.impl.rel.BeamRelNode in project beam by apache.

the class ZetaSqlTimeFunctionsTest method testExtractFromTime.

// TODO[BEAM-9166]: Add a test for CURRENT_TIME function ("SELECT CURRENT_TIME()")
@Test
public void testExtractFromTime() {
    String sql = "SELECT " + "EXTRACT(HOUR FROM TIME '15:30:35.123456') as hour, " + "EXTRACT(MINUTE FROM TIME '15:30:35.123456') as minute, " + "EXTRACT(SECOND FROM TIME '15:30:35.123456') as second, " + "EXTRACT(MILLISECOND FROM TIME '15:30:35.123456') as millisecond, " + "EXTRACT(MICROSECOND FROM TIME '15:30:35.123456') as microsecond ";
    ZetaSQLQueryPlanner zetaSQLQueryPlanner = new ZetaSQLQueryPlanner(config);
    BeamRelNode beamRelNode = zetaSQLQueryPlanner.convertToBeamRel(sql);
    PCollection<Row> stream = BeamSqlRelUtils.toPCollection(pipeline, beamRelNode);
    final Schema schema = Schema.builder().addInt64Field("hour").addInt64Field("minute").addInt64Field("second").addInt64Field("millisecond").addInt64Field("microsecond").build();
    PAssert.that(stream).containsInAnyOrder(Row.withSchema(schema).addValues(15L, 30L, 35L, 123L, 123456L).build());
    pipeline.run().waitUntilFinish(Duration.standardMinutes(PIPELINE_EXECUTION_WAITTIME_MINUTES));
}
Also used : BeamRelNode(org.apache.beam.sdk.extensions.sql.impl.rel.BeamRelNode) Schema(org.apache.beam.sdk.schemas.Schema) Row(org.apache.beam.sdk.values.Row) Test(org.junit.Test)

Aggregations

BeamRelNode (org.apache.beam.sdk.extensions.sql.impl.rel.BeamRelNode)246 Test (org.junit.Test)241 Row (org.apache.beam.sdk.values.Row)207 Schema (org.apache.beam.sdk.schemas.Schema)54 BeamIOSourceRel (org.apache.beam.sdk.extensions.sql.impl.rel.BeamIOSourceRel)38 ByteString (com.google.protobuf.ByteString)24 BeamCalcRel (org.apache.beam.sdk.extensions.sql.impl.rel.BeamCalcRel)22 Matchers.containsString (org.hamcrest.Matchers.containsString)13 Value (com.google.zetasql.Value)9 DateTime (org.joda.time.DateTime)8 BeamSqlEnv (org.apache.beam.sdk.extensions.sql.impl.BeamSqlEnv)7 BeamPushDownIOSourceRel (org.apache.beam.sdk.extensions.sql.impl.rel.BeamPushDownIOSourceRel)6 Calc (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.core.Calc)6 PipelineResult (org.apache.beam.sdk.PipelineResult)5 TestTableProvider (org.apache.beam.sdk.extensions.sql.meta.provider.test.TestTableProvider)5 DateTimeUtils.parseDateToValue (org.apache.beam.sdk.extensions.sql.zetasql.DateTimeUtils.parseDateToValue)5 DateTimeUtils.parseTimeToValue (org.apache.beam.sdk.extensions.sql.zetasql.DateTimeUtils.parseTimeToValue)5 DateTimeUtils.parseTimestampWithTZToValue (org.apache.beam.sdk.extensions.sql.zetasql.DateTimeUtils.parseTimestampWithTZToValue)5 Ignore (org.junit.Ignore)5 BeamAggregationRel (org.apache.beam.sdk.extensions.sql.impl.rel.BeamAggregationRel)4