diff --git a/kernel/data-pipeline/scenario/cdc/client/src/main/java/org/apache/shardingsphere/data/pipeline/cdc/client/util/ProtobufAnyValueConverter.java b/kernel/data-pipeline/scenario/cdc/client/src/main/java/org/apache/shardingsphere/data/pipeline/cdc/client/util/ProtobufAnyValueConverter.java index 0a563cb3b0111..c6c8e10a0a206 100644 --- a/kernel/data-pipeline/scenario/cdc/client/src/main/java/org/apache/shardingsphere/data/pipeline/cdc/client/util/ProtobufAnyValueConverter.java +++ b/kernel/data-pipeline/scenario/cdc/client/src/main/java/org/apache/shardingsphere/data/pipeline/cdc/client/util/ProtobufAnyValueConverter.java @@ -65,11 +65,8 @@ public static Object convertToObject(final Any any) throws InvalidProtocolBuffer if (any.is(Int64Value.class)) { return any.unpack(Int64Value.class).getValue(); } - if (any.is(Int64Value.class)) { - return any.unpack(Int64Value.class).getValue(); - } if (any.is(UInt32Value.class)) { - return any.unpack(UInt64Value.class).getValue(); + return any.unpack(UInt32Value.class).getValue(); } if (any.is(UInt64Value.class)) { return any.unpack(UInt64Value.class).getValue(); diff --git a/kernel/data-pipeline/scenario/cdc/client/src/test/java/org/apache/shardingsphere/data/pipeline/cdc/client/util/ProtobufAnyValueConverterTest.java b/kernel/data-pipeline/scenario/cdc/client/src/test/java/org/apache/shardingsphere/data/pipeline/cdc/client/util/ProtobufAnyValueConverterTest.java index d5b460d18745b..e3f9c2ef72c85 100644 --- a/kernel/data-pipeline/scenario/cdc/client/src/test/java/org/apache/shardingsphere/data/pipeline/cdc/client/util/ProtobufAnyValueConverterTest.java +++ b/kernel/data-pipeline/scenario/cdc/client/src/test/java/org/apache/shardingsphere/data/pipeline/cdc/client/util/ProtobufAnyValueConverterTest.java @@ -32,50 +32,66 @@ import com.google.protobuf.StringValue; import com.google.protobuf.Struct; import com.google.protobuf.Struct.Builder; +import com.google.protobuf.UInt32Value; import com.google.protobuf.UInt64Value; import com.google.protobuf.Value; import com.google.protobuf.util.JsonFormat; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; import java.sql.Timestamp; -import java.time.OffsetDateTime; +import java.util.stream.Stream; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertThrows; class ProtobufAnyValueConverterTest { @Test - void assertConvertToObject() throws InvalidProtocolBufferException { - Object actual = ProtobufAnyValueConverter.convertToObject(Any.pack(Int32Value.of(123))); - assertThat(actual, is(123)); - actual = ProtobufAnyValueConverter.convertToObject(Any.pack(Int64Value.of(Long.MAX_VALUE))); - assertThat(actual, is(Long.MAX_VALUE)); - OffsetDateTime now = OffsetDateTime.now(); - actual = ProtobufAnyValueConverter.convertToObject(Any.pack(com.google.protobuf.Timestamp.newBuilder().setSeconds(now.toEpochSecond()).setNanos(now.getNano()).build())); - assertThat(actual, is(Timestamp.valueOf(now.toLocalDateTime()))); - actual = ProtobufAnyValueConverter.convertToObject(Any.pack(FloatValue.of(1.23F))); - assertThat(actual, is(1.23F)); - actual = ProtobufAnyValueConverter.convertToObject(Any.pack(DoubleValue.of(4.56D))); - assertThat(actual, is(4.56D)); - actual = ProtobufAnyValueConverter.convertToObject(Any.pack(StringValue.of("Hello"))); - assertThat(actual, is("Hello")); - actual = ProtobufAnyValueConverter.convertToObject(Any.pack(BoolValue.of(true))); - assertTrue((Boolean) actual); - actual = ProtobufAnyValueConverter.convertToObject(Any.pack(BytesValue.of(ByteString.copyFrom(new byte[]{1, 2, 3})))); - assertThat(actual, is(new byte[]{1, 2, 3})); - actual = ProtobufAnyValueConverter.convertToObject(Any.pack(UInt64Value.of(101010L))); - assertThat(actual, is(101010L)); - actual = ProtobufAnyValueConverter.convertToObject(Any.pack(Empty.getDefaultInstance())); - assertNull(actual); - actual = Struct.newBuilder().putFields("str", Value.newBuilder().setStringValue("ABC defg").build()) + void assertConvertUnsupportedTypeThrowsUnsupportedOperationException() { + Any any = Any.pack(Value.newBuilder().setStringValue("unsupported").build()); + UnsupportedOperationException actual = assertThrows(UnsupportedOperationException.class, () -> ProtobufAnyValueConverter.convertToObject(any)); + assertThat(actual.getMessage(), is(String.format("not support unpack the type %s", any.getTypeUrl()))); + } + + @ParameterizedTest + @MethodSource("supportedValues") + void assertConvertToObject(final Any any, final Object expected) throws InvalidProtocolBufferException { + Object actual = ProtobufAnyValueConverter.convertToObject(any); + if (expected instanceof Struct) { + Builder actualStruct = Struct.newBuilder(); + JsonFormat.parser().merge((String) actual, actualStruct); + assertThat(actualStruct.build().toString(), is(expected.toString())); + } else { + assertThat(actual, is(expected)); + } + } + + private static Stream supportedValues() { + long seconds = 1577830861L; + int nanos = 123456789; + Timestamp expectedTimestamp = new Timestamp(seconds * 1000); + expectedTimestamp.setNanos(nanos); + Struct struct = Struct.newBuilder().putFields("str", Value.newBuilder().setStringValue("ABC defg").build()) .putFields("null", Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build()) .putFields("number", Value.newBuilder().setNumberValue(123.45D).build()) .putFields("list", Value.newBuilder().setListValue(ListValue.newBuilder().addValues(Value.newBuilder().setNumberValue(1)).build()).build()).build(); - Builder expected = Struct.newBuilder(); - JsonFormat.parser().merge((String) ProtobufAnyValueConverter.convertToObject(Any.pack((Struct) actual)), expected); - assertThat(actual.toString(), is(expected.toString())); + return Stream.of( + Arguments.of(null, null), + Arguments.of(Any.pack(Int32Value.of(123)), 123), + Arguments.of(Any.pack(Int64Value.of(Long.MAX_VALUE)), Long.MAX_VALUE), + Arguments.of(Any.pack(UInt32Value.of(7)), 7), + Arguments.of(Any.pack(com.google.protobuf.Timestamp.newBuilder().setSeconds(seconds).setNanos(nanos).build()), expectedTimestamp), + Arguments.of(Any.pack(FloatValue.of(1.23F)), 1.23F), + Arguments.of(Any.pack(DoubleValue.of(4.56D)), 4.56D), + Arguments.of(Any.pack(StringValue.of("Hello")), "Hello"), + Arguments.of(Any.pack(BoolValue.of(true)), true), + Arguments.of(Any.pack(BytesValue.of(ByteString.copyFrom(new byte[]{1, 2, 3}))), new byte[]{1, 2, 3}), + Arguments.of(Any.pack(UInt64Value.of(101010L)), 101010L), + Arguments.of(Any.pack(Empty.getDefaultInstance()), null), + Arguments.of(Any.pack(struct), struct)); } } diff --git a/kernel/sql-federation/compiler/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/segment/expression/impl/ListExpressionConverter.java b/kernel/sql-federation/compiler/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/segment/expression/impl/ListExpressionConverter.java index 31b37df8145d2..43ec88c866916 100644 --- a/kernel/sql-federation/compiler/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/segment/expression/impl/ListExpressionConverter.java +++ b/kernel/sql-federation/compiler/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/segment/expression/impl/ListExpressionConverter.java @@ -45,8 +45,7 @@ public final class ListExpressionConverter { public static Optional convert(final ListExpression segment) { Collection sqlNodes = new LinkedList<>(); for (ExpressionSegment each : segment.getItems()) { - Optional sqlNode = ExpressionConverter.convert(each); - sqlNode.ifPresent(sqlNodes::add); + ExpressionConverter.convert(each).ifPresent(sqlNodes::add); } return sqlNodes.isEmpty() ? Optional.empty() : Optional.of(new SqlNodeList(sqlNodes, SqlParserPos.ZERO)); }