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());
}
}
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));
}
}
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));
}
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));
}
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));
}
Aggregations