Search in sources :

Example 36 with Value

use of com.google.zetasql.Value in project beam by apache.

the class ZetaSqlTimeFunctionsTest method testDateTimeAddWithParameter.

@Test
public void testDateTimeAddWithParameter() {
    String sql = "SELECT DATETIME_ADD(@p0, INTERVAL @p1 HOUR)";
    LocalDateTime datetime = LocalDateTime.of(2008, 12, 25, 15, 30, 0).withNano(123456000);
    ImmutableMap<String, Value> params = ImmutableMap.of("p0", Value.createDatetimeValue(datetime), "p1", Value.createInt64Value(3L));
    ZetaSQLQueryPlanner zetaSQLQueryPlanner = new ZetaSQLQueryPlanner(config);
    BeamRelNode beamRelNode = zetaSQLQueryPlanner.convertToBeamRel(sql, params);
    PCollection<Row> stream = BeamSqlRelUtils.toPCollection(pipeline, beamRelNode);
    PAssert.that(stream).containsInAnyOrder(Row.withSchema(Schema.builder().addLogicalTypeField("f_datetime", SqlTypes.DATETIME).build()).addValues(LocalDateTime.of(2008, 12, 25, 18, 30, 0).withNano(123456000)).build());
    pipeline.run().waitUntilFinish(Duration.standardMinutes(PIPELINE_EXECUTION_WAITTIME_MINUTES));
}
Also used : LocalDateTime(java.time.LocalDateTime) BeamRelNode(org.apache.beam.sdk.extensions.sql.impl.rel.BeamRelNode) DateTimeUtils.parseTimestampWithTZToValue(org.apache.beam.sdk.extensions.sql.zetasql.DateTimeUtils.parseTimestampWithTZToValue) DateTimeUtils.parseDateToValue(org.apache.beam.sdk.extensions.sql.zetasql.DateTimeUtils.parseDateToValue) Value(com.google.zetasql.Value) DateTimeUtils.parseTimeToValue(org.apache.beam.sdk.extensions.sql.zetasql.DateTimeUtils.parseTimeToValue) Row(org.apache.beam.sdk.values.Row) Test(org.junit.Test)

Example 37 with Value

use of com.google.zetasql.Value in project beam by apache.

the class ZetaSqlTimeFunctionsTest method testTimestampAddWithParameter1.

@Test
public void testTimestampAddWithParameter1() {
    String sql = "SELECT TIMESTAMP_ADD(@p0, INTERVAL @p1 MILLISECOND)";
    ImmutableMap<String, Value> params = ImmutableMap.of("p0", parseTimestampWithTZToValue("2001-01-01 00:00:00+00"), "p1", Value.createInt64Value(1L));
    ZetaSQLQueryPlanner zetaSQLQueryPlanner = new ZetaSQLQueryPlanner(config);
    BeamRelNode beamRelNode = zetaSQLQueryPlanner.convertToBeamRel(sql, params);
    PCollection<Row> stream = BeamSqlRelUtils.toPCollection(pipeline, beamRelNode);
    final Schema schema = Schema.builder().addDateTimeField("field1").build();
    PAssert.that(stream).containsInAnyOrder(Row.withSchema(schema).addValues(parseTimestampWithTimeZone("2001-01-01 00:00:00.001+00")).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) DateTimeUtils.parseTimestampWithTZToValue(org.apache.beam.sdk.extensions.sql.zetasql.DateTimeUtils.parseTimestampWithTZToValue) DateTimeUtils.parseDateToValue(org.apache.beam.sdk.extensions.sql.zetasql.DateTimeUtils.parseDateToValue) Value(com.google.zetasql.Value) DateTimeUtils.parseTimeToValue(org.apache.beam.sdk.extensions.sql.zetasql.DateTimeUtils.parseTimeToValue) Row(org.apache.beam.sdk.values.Row) Test(org.junit.Test)

Example 38 with Value

use of com.google.zetasql.Value in project beam by apache.

the class ZetaSqlTimeFunctionsTest method testDateAddWithParameter.

@Test
public void testDateAddWithParameter() {
    String sql = "SELECT " + "DATE_ADD(@p0, INTERVAL @p1 DAY), " + "DATE_ADD(@p2, INTERVAL @p3 DAY), " + "DATE_ADD(@p4, INTERVAL @p5 YEAR), " + "DATE_ADD(@p6, INTERVAL @p7 DAY), " + "DATE_ADD(@p8, INTERVAL @p9 MONTH)";
    ImmutableMap<String, Value> params = ImmutableMap.<String, Value>builder().put("p0", // 1970-01-01
    Value.createDateValue(0)).put("p1", Value.createInt64Value(2L)).put("p2", parseDateToValue("2019-01-01")).put("p3", Value.createInt64Value(2L)).put("p4", Value.createSimpleNullValue(TypeKind.TYPE_DATE)).put("p5", Value.createInt64Value(1L)).put("p6", parseDateToValue("2000-02-29")).put("p7", Value.createInt64Value(-365L)).put("p8", parseDateToValue("1999-03-31")).put("p9", Value.createInt64Value(-1L)).build();
    ZetaSQLQueryPlanner zetaSQLQueryPlanner = new ZetaSQLQueryPlanner(config);
    BeamRelNode beamRelNode = zetaSQLQueryPlanner.convertToBeamRel(sql, params);
    PCollection<Row> stream = BeamSqlRelUtils.toPCollection(pipeline, beamRelNode);
    final Schema schema = Schema.builder().addLogicalTypeField("f_date1", SqlTypes.DATE).addLogicalTypeField("f_date2", SqlTypes.DATE).addNullableField("f_date3", FieldType.logicalType(SqlTypes.DATE)).addLogicalTypeField("f_date4", SqlTypes.DATE).addLogicalTypeField("f_date5", SqlTypes.DATE).build();
    PAssert.that(stream).containsInAnyOrder(Row.withSchema(schema).addValues(LocalDate.of(1970, 1, 3), LocalDate.of(2019, 1, 3), null, LocalDate.of(1999, 3, 1), LocalDate.of(1999, 2, 28)).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) DateTimeUtils.parseTimestampWithTZToValue(org.apache.beam.sdk.extensions.sql.zetasql.DateTimeUtils.parseTimestampWithTZToValue) DateTimeUtils.parseDateToValue(org.apache.beam.sdk.extensions.sql.zetasql.DateTimeUtils.parseDateToValue) Value(com.google.zetasql.Value) DateTimeUtils.parseTimeToValue(org.apache.beam.sdk.extensions.sql.zetasql.DateTimeUtils.parseTimeToValue) Row(org.apache.beam.sdk.values.Row) Test(org.junit.Test)

Example 39 with Value

use of com.google.zetasql.Value in project beam by apache.

the class ZetaSqlTimeFunctionsTest method testTimeAddWithParameter.

@Test
public void testTimeAddWithParameter() {
    String sql = "SELECT TIME_ADD(@p0, INTERVAL @p1 SECOND)";
    ImmutableMap<String, Value> params = ImmutableMap.of("p0", parseTimeToValue("12:13:14.123"), "p1", Value.createInt64Value(1L));
    ZetaSQLQueryPlanner zetaSQLQueryPlanner = new ZetaSQLQueryPlanner(config);
    BeamRelNode beamRelNode = zetaSQLQueryPlanner.convertToBeamRel(sql, params);
    PCollection<Row> stream = BeamSqlRelUtils.toPCollection(pipeline, beamRelNode);
    PAssert.that(stream).containsInAnyOrder(Row.withSchema(Schema.builder().addLogicalTypeField("f_time", SqlTypes.TIME).build()).addValues(LocalTime.of(12, 13, 15, 123000000)).build());
    pipeline.run().waitUntilFinish(Duration.standardMinutes(PIPELINE_EXECUTION_WAITTIME_MINUTES));
}
Also used : BeamRelNode(org.apache.beam.sdk.extensions.sql.impl.rel.BeamRelNode) DateTimeUtils.parseTimestampWithTZToValue(org.apache.beam.sdk.extensions.sql.zetasql.DateTimeUtils.parseTimestampWithTZToValue) DateTimeUtils.parseDateToValue(org.apache.beam.sdk.extensions.sql.zetasql.DateTimeUtils.parseDateToValue) Value(com.google.zetasql.Value) DateTimeUtils.parseTimeToValue(org.apache.beam.sdk.extensions.sql.zetasql.DateTimeUtils.parseTimeToValue) Row(org.apache.beam.sdk.values.Row) Test(org.junit.Test)

Example 40 with Value

use of com.google.zetasql.Value in project beam by apache.

the class ZetaSqlDialectSpecTest method testNullIfPositive.

@Test
public void testNullIfPositive() {
    String sql = "SELECT NULLIF(@p0, @p1) AS ColA";
    ImmutableMap<String, Value> params = ImmutableMap.of("p0", Value.createStringValue("null"), "p1", Value.createStringValue("null"));
    PCollection<Row> stream = execute(sql, params);
    final Schema schema = Schema.builder().addNullableField("field1", FieldType.STRING).build();
    PAssert.that(stream).containsInAnyOrder(Row.withSchema(schema).addValue(null).build());
    pipeline.run().waitUntilFinish(Duration.standardMinutes(PIPELINE_EXECUTION_WAITTIME_MINUTES));
}
Also used : Schema(org.apache.beam.sdk.schemas.Schema) Value(com.google.zetasql.Value) ByteString(com.google.protobuf.ByteString) Row(org.apache.beam.sdk.values.Row) Test(org.junit.Test)

Aggregations

Value (com.google.zetasql.Value)73 Test (org.junit.Test)69 Row (org.apache.beam.sdk.values.Row)66 ByteString (com.google.protobuf.ByteString)64 Schema (org.apache.beam.sdk.schemas.Schema)63 BeamRelNode (org.apache.beam.sdk.extensions.sql.impl.rel.BeamRelNode)9 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 StructField (com.google.zetasql.StructType.StructField)3 AnalyzerOptions (com.google.zetasql.AnalyzerOptions)1 StructType (com.google.zetasql.StructType)1 StatusRuntimeException (com.google.zetasql.io.grpc.StatusRuntimeException)1 ResolvedNodes (com.google.zetasql.resolvedast.ResolvedNodes)1 LocalDateTime (java.time.LocalDateTime)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 ZetaSqlException (org.apache.beam.sdk.extensions.sql.zetasql.ZetaSqlException)1 Ignore (org.junit.Ignore)1