Skip to content

Commit fd7cd5f

Browse files
authored
feat: add FailureBehavior enum to Cast immutable (#115)
1 parent 0a8d8f1 commit fd7cd5f

File tree

4 files changed

+47
-5
lines changed

4 files changed

+47
-5
lines changed

core/src/main/java/io/substrait/expression/Expression.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,8 @@ abstract static class Cast implements Expression {
532532

533533
public abstract Expression input();
534534

535+
public abstract FailureBehavior failureBehavior();
536+
535537
public Type getType() {
536538
return type();
537539
}
@@ -770,7 +772,7 @@ public static AggregationPhase fromProto(io.substrait.proto.AggregationPhase pro
770772
}
771773
}
772774

773-
public enum SortDirection {
775+
enum SortDirection {
774776
ASC_NULLS_FIRST(io.substrait.proto.SortField.SortDirection.SORT_DIRECTION_ASC_NULLS_FIRST),
775777
ASC_NULLS_LAST(io.substrait.proto.SortField.SortDirection.SORT_DIRECTION_ASC_NULLS_LAST),
776778
DESC_NULLS_FIRST(io.substrait.proto.SortField.SortDirection.SORT_DIRECTION_DESC_NULLS_FIRST),
@@ -797,4 +799,32 @@ public static SortDirection fromProto(io.substrait.proto.SortField.SortDirection
797799
throw new IllegalArgumentException("Unknown type: " + proto);
798800
}
799801
}
802+
803+
enum FailureBehavior {
804+
UNSPECIFIED(io.substrait.proto.Expression.Cast.FailureBehavior.FAILURE_BEHAVIOR_UNSPECIFIED),
805+
RETURN_NULL(io.substrait.proto.Expression.Cast.FailureBehavior.FAILURE_BEHAVIOR_RETURN_NULL),
806+
THROW_EXCEPTION(
807+
io.substrait.proto.Expression.Cast.FailureBehavior.FAILURE_BEHAVIOR_THROW_EXCEPTION);
808+
809+
private final io.substrait.proto.Expression.Cast.FailureBehavior proto;
810+
811+
FailureBehavior(io.substrait.proto.Expression.Cast.FailureBehavior proto) {
812+
this.proto = proto;
813+
}
814+
815+
public io.substrait.proto.Expression.Cast.FailureBehavior toProto() {
816+
return proto;
817+
}
818+
819+
public static FailureBehavior fromProto(
820+
io.substrait.proto.Expression.Cast.FailureBehavior proto) {
821+
for (var v : values()) {
822+
if (v.proto == proto) {
823+
return v;
824+
}
825+
}
826+
827+
throw new IllegalArgumentException("Unknown type: " + proto);
828+
}
829+
}
800830
}

core/src/main/java/io/substrait/expression/ExpressionCreator.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
import java.time.Instant;
1010
import java.time.LocalDateTime;
1111
import java.time.ZoneOffset;
12-
import java.util.*;
12+
import java.util.List;
13+
import java.util.Map;
14+
import java.util.UUID;
1315
import java.util.concurrent.TimeUnit;
1416

1517
public class ExpressionCreator {
@@ -341,7 +343,16 @@ public static WindowFunctionInvocation windowFunction(
341343
}
342344

343345
public static Expression cast(Type type, Expression expression) {
344-
return Expression.Cast.builder().type(type).input(expression).build();
346+
return cast(type, expression, Expression.FailureBehavior.UNSPECIFIED);
347+
}
348+
349+
public static Expression cast(
350+
Type type, Expression expression, Expression.FailureBehavior failureBehavior) {
351+
return Expression.Cast.builder()
352+
.type(type)
353+
.input(expression)
354+
.failureBehavior(failureBehavior)
355+
.build();
345356
}
346357

347358
private static ByteString padLeftIfNeeded(byte[] value, int length) {

core/src/main/java/io/substrait/expression/proto/ExpressionProtoConverter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,8 @@ public Expression visit(io.substrait.expression.Expression.Cast expr) {
276276
.setCast(
277277
Expression.Cast.newBuilder()
278278
.setInput(expr.input().accept(this))
279-
.setType(expr.getType().accept(TypeProtoConverter.INSTANCE)))
279+
.setType(expr.getType().accept(TypeProtoConverter.INSTANCE))
280+
.setFailureBehavior(expr.failureBehavior().toProto()))
280281
.build();
281282
}
282283

core/src/main/java/io/substrait/function/SimpleExtension.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ public abstract static class ScalarFunction {
379379

380380
public abstract List<ScalarFunctionVariant> impls();
381381

382-
Stream<ScalarFunctionVariant> resolve(String uri) {
382+
public Stream<ScalarFunctionVariant> resolve(String uri) {
383383
return impls().stream().map(f -> f.resolve(uri, name(), description()));
384384
}
385385
}

0 commit comments

Comments
 (0)