rdblue commented on code in PR #18629:
URL: https://github.com/apache/iceberg/pull/18629#discussion_r4778202057
##########
core/src/main/java/org/apache/iceberg/expressions/ExpressionParser.java:
##########
@@ -257,6 +325,203 @@ private void transform(String transform, String name)
throws IOException {
gen.writeStringField(TERM, name);
gen.writeEndObject();
}
+
+ private <T> void unboundPredicate(UnboundPredicate<T> pred) throws
IOException {
+ gen.writeStartObject();
+ gen.writeStringField(TYPE, operationType(pred.op()));
+ switch (pred.op()) {
+ case IS_NULL, NOT_NULL, IS_NAN, NOT_NAN -> field(CHILD, pred.child());
+ case IN, NOT_IN -> {
+ field(CHILD, pred.child());
+ gen.writeFieldName(VALUES);
+ literals(pred.literals());
+ }
+ default -> {
+ field(LEFT, pred.left());
+ field(RIGHT, pred.right());
+ }
+ }
+
+ gen.writeEndObject();
+ }
+
+ private <T> void boundPredicate(BoundPredicate<T> pred) throws IOException
{
+ gen.writeStartObject();
+ gen.writeStringField(TYPE, operationType(pred.op()));
+ if (pred.isUnaryPredicate()) {
+ field(CHILD, pred.asUnaryPredicate().child());
+ } else if (pred.isLiteralPredicate()) {
+ BoundLiteralPredicate<T> literalPred = pred.asLiteralPredicate();
+ field(LEFT, literalPred.left());
+ field(RIGHT, literalPred.right());
+ } else if (pred.isSetPredicate()) {
+ BoundSetPredicate<T> setPred = pred.asSetPredicate();
+ field(CHILD, setPred.child());
+ gen.writeFieldName(VALUES);
+ literalSet(valueType(setPred.child()), setPred.literalSet());
+ } else {
+ throw new UnsupportedOperationException("Cannot write unsupported
predicate: " + pred);
+ }
+
+ gen.writeEndObject();
+ }
+
+ private void field(String name, Expression expr) throws IOException {
+ gen.writeFieldName(name);
+ valueExpression(expr);
+ }
+
+ private void valueExpression(Expression expr) throws IOException {
+ if (expr instanceof Literal<?> literal) {
+ literal(literal);
+ } else if (expr instanceof NamedReference<?> ref) {
+ reference(NAME, () -> gen.writeString(ref.name()));
+ } else if (expr instanceof IdReference<?> ref) {
+ reference(ID, () -> gen.writeNumber(ref.fieldId()));
+ } else if (expr instanceof BoundReference<?> ref) {
+ reference(ID, () -> gen.writeNumber(ref.fieldId()));
+ } else if (expr instanceof UnboundTransform<?, ?> transform) {
+ transformFunction(transform.transform(), transform.ref());
+ } else if (expr instanceof BoundTransform<?, ?> transform) {
+ transformFunction(transform.transform(), transform.ref());
+ } else if (expr instanceof UnboundApply<?> apply) {
+ apply(apply.function().catalog(), apply.function().identifier(),
apply.arguments());
+ } else if (expr instanceof BoundApply<?> apply) {
+ apply(apply.function().catalog(), apply.function().identifier(),
apply.arguments());
+ } else {
+ throw new UnsupportedOperationException(
+ "Cannot write unsupported value expression: " + expr);
+ }
+ }
+
+ private void reference(String field, Task value) throws IOException {
+ gen.writeStartObject();
+ gen.writeStringField(TYPE, REFERENCE);
+ gen.writeFieldName(field);
+ value.run();
+ gen.writeEndObject();
+ }
+
+ private void literal(Literal<?> literal) throws IOException {
+ Type type = literal.type();
+ gen.writeStartObject();
+ gen.writeStringField(TYPE, LITERAL);
+ gen.writeFieldName(VALUE);
+ value(type, literal.value());
+ gen.writeStringField(DATA_TYPE, type.toString());
+ gen.writeEndObject();
+ }
+
+ private <T> void literals(List<Literal<T>> literals) throws IOException {
+ Type type = literals.isEmpty() ? null : literals.get(0).type();
+ if (type == null || literals.stream().anyMatch(lit ->
!type.equals(lit.type()))) {
+ gen.writeStartArray();
+ for (Literal<T> lit : literals) {
+ literal(lit);
+ }
+
+ gen.writeEndArray();
+ } else {
+ literalSet(type, literals.stream().map(Literal::value).toList());
+ }
+ }
+
+ private void literalSet(Type type, Iterable<?> values) throws IOException {
+ gen.writeStartObject();
+ gen.writeStringField(TYPE, LITERALS);
+ gen.writeArrayFieldStart(VALUES);
+ for (Object value : values) {
+ value(type, value);
+ }
+
+ gen.writeEndArray();
+ gen.writeStringField(DATA_TYPE, type.toString());
+ gen.writeEndObject();
+ }
+
+ private void value(Type type, Object value) throws IOException {
+ // decimal types use non-negative scales, so align values like 1E+3
before writing
+ if (value instanceof BigDecimal decimal
+ && type instanceof Types.DecimalType decimalType
+ && decimal.scale() != decimalType.scale()) {
+ SingleValueParser.toJson(type, decimal.setScale(decimalType.scale()),
gen);
Review Comment:
Why is this modifying the decimal value? Shouldn't this just use
`SingleValueParser.toJson`?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]