This is an automated email from the ASF dual-hosted git repository.
zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new ebd9618dbf4 Refactor ProtobufAnyValueConverter (#37370)
ebd9618dbf4 is described below
commit ebd9618dbf4fb270686bd20d6f6a961237cf1101
Author: Liang Zhang <[email protected]>
AuthorDate: Sat Dec 13 01:16:12 2025 +0800
Refactor ProtobufAnyValueConverter (#37370)
* Refactor ProtobufAnyValueConverter
* Refactor ProtobufAnyValueConverter
---
.../cdc/client/util/ProtobufAnyValueConverter.java | 5 +-
.../client/util/ProtobufAnyValueConverterTest.java | 74 +++++++++++++---------
.../expression/impl/ListExpressionConverter.java | 3 +-
3 files changed, 47 insertions(+), 35 deletions(-)
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 0a563cb3b01..c6c8e10a0a2 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 final class ProtobufAnyValueConverter {
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 d5b460d1874..e3f9c2ef72c 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.NullValue;
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<Arguments> 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 31b37df8145..43ec88c8669 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<SqlNode> convert(final ListExpression segment) {
Collection<SqlNode> sqlNodes = new LinkedList<>();
for (ExpressionSegment each : segment.getItems()) {
- Optional<SqlNode> 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));
}