Search in sources :

Example 1 with Value

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

the class ExpressionConverter method convertResolvedParameter.

private RexNode convertResolvedParameter(ResolvedParameter parameter) {
    Value value;
    switch(queryParams.getKind()) {
        case NAMED:
            value = ((Map<String, Value>) queryParams.named()).get(parameter.getName());
            break;
        case POSITIONAL:
            // parameter is 1-indexed, while parameter list is 0-indexed.
            value = ((List<Value>) queryParams.positional()).get((int) parameter.getPosition() - 1);
            break;
        default:
            throw new IllegalArgumentException("Found unexpected parameter " + parameter);
    }
    Preconditions.checkState(parameter.getType().equals(value.getType()));
    if (value.isNull()) {
        // Therefore we create a dynamic parameter placeholder here for each NULL parameter
        return rexBuilder().makeDynamicParam(ZetaSqlCalciteTranslationUtils.toCalciteType(value.getType(), true, rexBuilder()), nullParamCount++);
    } else {
        // Substitute non-NULL parameter with literal
        return ZetaSqlCalciteTranslationUtils.toRexNode(value, rexBuilder());
    }
}
Also used : Value(com.google.zetasql.Value)

Example 2 with Value

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

the class SqlOperators method createStringAggOperator.

public static SqlOperator createStringAggOperator(ResolvedNodes.ResolvedFunctionCallBase aggregateFunctionCall) {
    List<ResolvedNodes.ResolvedExpr> args = aggregateFunctionCall.getArgumentList();
    String inputType = args.get(0).getType().typeName();
    Value delimiter = null;
    if (args.size() == 2) {
        ResolvedNodes.ResolvedExpr resolvedExpr = args.get(1);
        if (resolvedExpr instanceof ResolvedNodes.ResolvedLiteral) {
            delimiter = ((ResolvedNodes.ResolvedLiteral) resolvedExpr).getValue();
        } else {
            // TODO(BEAM-13673) Add support for params
            throw new ZetaSqlException(new StatusRuntimeException(Status.INVALID_ARGUMENT.withDescription(String.format("STRING_AGG only supports ResolvedLiteral as delimiter, provided %s", resolvedExpr.getClass().getName()))));
        }
    }
    switch(inputType) {
        case "BYTES":
            return SqlOperators.createUdafOperator("string_agg", x -> SqlOperators.createTypeFactory().createSqlType(SqlTypeName.VARBINARY), new UdafImpl<>(new StringAgg.StringAggByte(delimiter == null ? ",".getBytes(StandardCharsets.UTF_8) : delimiter.getBytesValue().toByteArray())));
        case "STRING":
            return SqlOperators.createUdafOperator("string_agg", x -> SqlOperators.createTypeFactory().createSqlType(SqlTypeName.VARCHAR), new UdafImpl<>(new StringAgg.StringAggString(delimiter == null ? "," : delimiter.getStringValue())));
        default:
            throw new UnsupportedOperationException(String.format("[%s] is not supported in STRING_AGG", inputType));
    }
}
Also used : Value(com.google.zetasql.Value) StatusRuntimeException(com.google.zetasql.io.grpc.StatusRuntimeException) ResolvedNodes(com.google.zetasql.resolvedast.ResolvedNodes) ZetaSqlException(org.apache.beam.sdk.extensions.sql.zetasql.ZetaSqlException)

Example 3 with Value

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

the class ZetaSqlDialectSpecTest method testLike1.

@Test
public void testLike1() {
    String sql = "SELECT @p0 LIKE @p1 AS ColA";
    ImmutableMap<String, Value> params = ImmutableMap.of("p0", Value.createStringValue("ab%"), "p1", Value.createStringValue("ab\\%"));
    PCollection<Row> stream = execute(sql, params);
    final Schema schema = Schema.builder().addNullableField("field1", FieldType.BOOLEAN).build();
    PAssert.that(stream).containsInAnyOrder(Row.withSchema(schema).addValues(true).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)

Example 4 with Value

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

the class ZetaSqlDialectSpecTest method testLikeNullPattern.

@Test
public void testLikeNullPattern() {
    String sql = "SELECT @p0 LIKE @p1 AS ColA";
    ImmutableMap<String, Value> params = ImmutableMap.of("p0", Value.createStringValue("ab%"), "p1", Value.createSimpleNullValue(TypeKind.TYPE_STRING));
    PCollection<Row> stream = execute(sql, params);
    final Schema schema = Schema.builder().addNullableField("field1", FieldType.BOOLEAN).build();
    PAssert.that(stream).containsInAnyOrder(Row.withSchema(schema).addValues((Object) 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)

Example 5 with Value

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

the class ZetaSqlDialectSpecTest method testIfNullPositive.

@Test
public void testIfNullPositive() {
    String sql = "SELECT IFNULL(@p0, @p1) AS ColA";
    ImmutableMap<String, Value> params = ImmutableMap.of("p0", Value.createStringValue("foo"), "p1", Value.createStringValue("default"));
    PCollection<Row> stream = execute(sql, params);
    final Schema schema = Schema.builder().addNullableField("field1", FieldType.STRING).build();
    PAssert.that(stream).containsInAnyOrder(Row.withSchema(schema).addValues("foo").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