You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by GitBox <gi...@apache.org> on 2022/08/18 08:14:14 UTC
[GitHub] [flink] yuzelin opened a new pull request, #20617: [FLINK-28936][sql-gateway] Fix REST endpoint can not serialize CHAR(0) by introducing a new serializer for LogicalType
yuzelin opened a new pull request, #20617:
URL: https://github.com/apache/flink/pull/20617
…) by introducing a new serializer for LogicalType
<!--
*Thank you very much for contributing to Apache Flink - we are happy that you want to help us improve Flink. To help the community review your contribution in the best possible way, please go through the checklist below, which will get the contribution into a shape in which it can be best reviewed.*
*Please understand that we do not do this to make contributions to Flink a hassle. In order to uphold a high standard of quality for code contributions, while at the same time managing a large number of contributions, we need contributors to prepare the contributions well, and give reviewers enough contextual information for the review. Please also understand that contributions that do not follow this guide will take longer to review and thus typically be picked up with lower priority by the community.*
## Contribution Checklist
- Make sure that the pull request corresponds to a [JIRA issue](https://issues.apache.org/jira/projects/FLINK/issues). Exceptions are made for typos in JavaDoc or documentation files, which need no JIRA issue.
- Name the pull request in the form "[FLINK-XXXX] [component] Title of the pull request", where *FLINK-XXXX* should be replaced by the actual issue number. Skip *component* if you are unsure about which is the best component.
Typo fixes that have no associated JIRA issue should be named following this pattern: `[hotfix] [docs] Fix typo in event time introduction` or `[hotfix] [javadocs] Expand JavaDoc for PuncuatedWatermarkGenerator`.
- Fill out the template below to describe the changes contributed by the pull request. That will give reviewers the context they need to do the review.
- Make sure that the change passes the automated tests, i.e., `mvn clean verify` passes. You can set up Azure Pipelines CI to do that following [this guide](https://cwiki.apache.org/confluence/display/FLINK/Azure+Pipelines#AzurePipelines-Tutorial:SettingupAzurePipelinesforaforkoftheFlinkrepository).
- Each pull request should address only one issue, not mix up code from multiple issues.
- Each commit in the pull request has a meaningful commit message (including the JIRA id)
- Once all items of the checklist are addressed, remove the above text and this checklist, leaving only the filled out template below.
**(The sections below can be removed for hotfixes of typos)**
-->
## What is the purpose of the change
Fix the absence of `LogicalType` serializer.
## Brief change log
- Add `LogicalTypeJsonSerializer` in sql-gateway-api.
- Add `LogicalTypeJsonDeserializer` in sql-gateway-api.
## Verifying this change
This change added tests and can be verified as follows:
- Added test prove that a `LogicalType` instance after serializing and deserializing is equal to original logical type.
## Does this pull request potentially affect one of the following parts:
- Dependencies (does it add or upgrade a dependency): (yes / **no**)
- The public API, i.e., is any changed class annotated with `@Public(Evolving)`: (**yes** / no)
- The serializers: (yes / **no** / don't know)
- The runtime per-record code paths (performance sensitive): (yes / **no** / don't know)
- Anything that affects deployment or recovery: JobManager (and its components), Checkpointing, Kubernetes/Yarn, ZooKeeper: (yes / **no** / don't know)
- The S3 file system connector: (yes / **no** / don't know)
## Documentation
- Does this pull request introduce a new feature? (yes / **no**)
- If yes, how is the feature documented? (not applicable / docs / JavaDocs / not documented)
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink] yuzelin commented on a diff in pull request #20617: [FLINK-28936][sql-gateway] Fix REST endpoint can not serialize CHAR(0) by introducing a new serializer for LogicalType
Posted by GitBox <gi...@apache.org>.
yuzelin commented on code in PR #20617:
URL: https://github.com/apache/flink/pull/20617#discussion_r951261804
##########
flink-table/flink-sql-gateway-api/src/main/java/org/apache/flink/table/gateway/api/results/serde/LogicalTypeJsonSerializer.java:
##########
@@ -0,0 +1,210 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.table.gateway.api.results.serde;
+
+import org.apache.flink.annotation.PublicEvolving;
+import org.apache.flink.table.api.ValidationException;
+import org.apache.flink.table.types.logical.ArrayType;
+import org.apache.flink.table.types.logical.BinaryType;
+import org.apache.flink.table.types.logical.CharType;
+import org.apache.flink.table.types.logical.DecimalType;
+import org.apache.flink.table.types.logical.LocalZonedTimestampType;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.MapType;
+import org.apache.flink.table.types.logical.MultisetType;
+import org.apache.flink.table.types.logical.RawType;
+import org.apache.flink.table.types.logical.RowType;
+import org.apache.flink.table.types.logical.TimestampType;
+import org.apache.flink.table.types.logical.VarBinaryType;
+import org.apache.flink.table.types.logical.VarCharType;
+
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonGenerator;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.SerializerProvider;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ser.std.StdSerializer;
+
+import java.io.IOException;
+
+/**
+ * Json serializer for {@link LogicalType}.
+ *
+ * @see LogicalTypeJsonDeserializer for the reverse operation.
+ */
+@PublicEvolving
+public final class LogicalTypeJsonSerializer extends StdSerializer<LogicalType> {
+
+ private static final long serialVersionUID = 1L;
+
+ // --------------------------------------------------------------------------------------------
+ // Public string constants for serializer and deserializer
+ // --------------------------------------------------------------------------------------------
+
+ // Common fields
+ public static final String FIELD_NAME_TYPE_NAME = "type";
+ public static final String FIELD_NAME_NULLABLE = "nullable";
Review Comment:
As far as I know, `description` is not a common field shared by all logical types, so I think we don't need this.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink] yuzelin commented on a diff in pull request #20617: [FLINK-28936][sql-gateway] Fix REST endpoint can not serialize CHAR(0) by introducing a new serializer for LogicalType
Posted by GitBox <gi...@apache.org>.
yuzelin commented on code in PR #20617:
URL: https://github.com/apache/flink/pull/20617#discussion_r951378375
##########
flink-table/flink-sql-gateway-api/src/test/java/org/apache/flink/table/gateway/api/results/serde/LogicalTypeJsonSerDeTest.java:
##########
@@ -0,0 +1,165 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.table.gateway.api.results.serde;
+
+import org.apache.flink.api.common.typeutils.base.LocalDateTimeSerializer;
+import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.runtime.typeutils.ExternalSerializer;
+import org.apache.flink.table.types.logical.ArrayType;
+import org.apache.flink.table.types.logical.BigIntType;
+import org.apache.flink.table.types.logical.BinaryType;
+import org.apache.flink.table.types.logical.BooleanType;
+import org.apache.flink.table.types.logical.CharType;
+import org.apache.flink.table.types.logical.DateType;
+import org.apache.flink.table.types.logical.DecimalType;
+import org.apache.flink.table.types.logical.DoubleType;
+import org.apache.flink.table.types.logical.FloatType;
+import org.apache.flink.table.types.logical.IntType;
+import org.apache.flink.table.types.logical.LocalZonedTimestampType;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.MapType;
+import org.apache.flink.table.types.logical.MultisetType;
+import org.apache.flink.table.types.logical.NullType;
+import org.apache.flink.table.types.logical.RawType;
+import org.apache.flink.table.types.logical.RowType;
+import org.apache.flink.table.types.logical.SmallIntType;
+import org.apache.flink.table.types.logical.TimestampType;
+import org.apache.flink.table.types.logical.TinyIntType;
+import org.apache.flink.table.types.logical.VarBinaryType;
+import org.apache.flink.table.types.logical.VarCharType;
+import org.apache.flink.types.Row;
+
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectMapper;
+
+import org.junit.jupiter.api.parallel.Execution;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
+
+import java.io.IOException;
+import java.time.LocalDateTime;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.jupiter.api.parallel.ExecutionMode.CONCURRENT;
+
+/** Tests for {@link LogicalType} serialization and deserialization. */
+@Execution(CONCURRENT)
+public class LogicalTypeJsonSerDeTest {
+
+ @ParameterizedTest
+ @MethodSource("generateTestData")
+ public void testLogicalTypeJsonSerDe(LogicalType logicalType) throws IOException {
+ ObjectMapper mapper = JsonSerdeUtil.getObjectMapper();
+ String json = mapper.writeValueAsString(logicalType);
+ LogicalType actualType = mapper.readValue(json, LogicalType.class);
+
+ assertThat(actualType).isEqualTo(logicalType);
+ }
+
+ // --------------------------------------------------------------------------------------------
+ // Test data
+ // --------------------------------------------------------------------------------------------
+
+ private static List<LogicalType> generateTestData() {
+ List<LogicalType> types =
+ Arrays.asList(
Review Comment:
Add necessary test case.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink] fsk119 commented on a diff in pull request #20617: [FLINK-28936][sql-gateway] Fix REST endpoint can not serialize CHAR(0) by introducing a new serializer for LogicalType
Posted by GitBox <gi...@apache.org>.
fsk119 commented on code in PR #20617:
URL: https://github.com/apache/flink/pull/20617#discussion_r950981068
##########
flink-table/flink-sql-gateway-api/src/main/java/org/apache/flink/table/gateway/api/results/serde/LogicalTypeJsonSerializer.java:
##########
@@ -0,0 +1,210 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.table.gateway.api.results.serde;
+
+import org.apache.flink.annotation.PublicEvolving;
+import org.apache.flink.table.api.ValidationException;
+import org.apache.flink.table.types.logical.ArrayType;
+import org.apache.flink.table.types.logical.BinaryType;
+import org.apache.flink.table.types.logical.CharType;
+import org.apache.flink.table.types.logical.DecimalType;
+import org.apache.flink.table.types.logical.LocalZonedTimestampType;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.MapType;
+import org.apache.flink.table.types.logical.MultisetType;
+import org.apache.flink.table.types.logical.RawType;
+import org.apache.flink.table.types.logical.RowType;
+import org.apache.flink.table.types.logical.TimestampType;
+import org.apache.flink.table.types.logical.VarBinaryType;
+import org.apache.flink.table.types.logical.VarCharType;
+
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonGenerator;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.SerializerProvider;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ser.std.StdSerializer;
+
+import java.io.IOException;
+
+/**
+ * Json serializer for {@link LogicalType}.
+ *
+ * @see LogicalTypeJsonDeserializer for the reverse operation.
+ */
+@PublicEvolving
+public final class LogicalTypeJsonSerializer extends StdSerializer<LogicalType> {
+
+ private static final long serialVersionUID = 1L;
+
+ // --------------------------------------------------------------------------------------------
+ // Public string constants for serializer and deserializer
+ // --------------------------------------------------------------------------------------------
+
+ // Common fields
+ public static final String FIELD_NAME_TYPE_NAME = "type";
+ public static final String FIELD_NAME_NULLABLE = "nullable";
Review Comment:
miss FIELD_NAME_DESCRPITION
##########
flink-table/flink-sql-gateway-api/src/test/java/org/apache/flink/table/gateway/api/results/serde/LogicalTypeJsonSerDeTest.java:
##########
@@ -0,0 +1,165 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.table.gateway.api.results.serde;
+
+import org.apache.flink.api.common.typeutils.base.LocalDateTimeSerializer;
+import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.runtime.typeutils.ExternalSerializer;
+import org.apache.flink.table.types.logical.ArrayType;
+import org.apache.flink.table.types.logical.BigIntType;
+import org.apache.flink.table.types.logical.BinaryType;
+import org.apache.flink.table.types.logical.BooleanType;
+import org.apache.flink.table.types.logical.CharType;
+import org.apache.flink.table.types.logical.DateType;
+import org.apache.flink.table.types.logical.DecimalType;
+import org.apache.flink.table.types.logical.DoubleType;
+import org.apache.flink.table.types.logical.FloatType;
+import org.apache.flink.table.types.logical.IntType;
+import org.apache.flink.table.types.logical.LocalZonedTimestampType;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.MapType;
+import org.apache.flink.table.types.logical.MultisetType;
+import org.apache.flink.table.types.logical.NullType;
+import org.apache.flink.table.types.logical.RawType;
+import org.apache.flink.table.types.logical.RowType;
+import org.apache.flink.table.types.logical.SmallIntType;
+import org.apache.flink.table.types.logical.TimestampType;
+import org.apache.flink.table.types.logical.TinyIntType;
+import org.apache.flink.table.types.logical.VarBinaryType;
+import org.apache.flink.table.types.logical.VarCharType;
+import org.apache.flink.types.Row;
+
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectMapper;
+
+import org.junit.jupiter.api.parallel.Execution;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
+
+import java.io.IOException;
+import java.time.LocalDateTime;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.jupiter.api.parallel.ExecutionMode.CONCURRENT;
+
+/** Tests for {@link LogicalType} serialization and deserialization. */
+@Execution(CONCURRENT)
+public class LogicalTypeJsonSerDeTest {
+
+ @ParameterizedTest
+ @MethodSource("generateTestData")
+ public void testLogicalTypeJsonSerDe(LogicalType logicalType) throws IOException {
+ ObjectMapper mapper = JsonSerdeUtil.getObjectMapper();
+ String json = mapper.writeValueAsString(logicalType);
+ LogicalType actualType = mapper.readValue(json, LogicalType.class);
+
+ assertThat(actualType).isEqualTo(logicalType);
+ }
+
Review Comment:
Could you add a case about failed to serialize the LogicalType?
##########
flink-table/flink-sql-gateway-api/src/main/java/org/apache/flink/table/gateway/api/results/serde/LogicalTypeJsonDeserializer.java:
##########
@@ -0,0 +1,219 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.table.gateway.api.results.serde;
+
+import org.apache.flink.annotation.PublicEvolving;
+import org.apache.flink.table.api.TableException;
+import org.apache.flink.table.types.logical.ArrayType;
+import org.apache.flink.table.types.logical.BigIntType;
+import org.apache.flink.table.types.logical.BinaryType;
+import org.apache.flink.table.types.logical.BooleanType;
+import org.apache.flink.table.types.logical.CharType;
+import org.apache.flink.table.types.logical.DateType;
+import org.apache.flink.table.types.logical.DecimalType;
+import org.apache.flink.table.types.logical.DoubleType;
+import org.apache.flink.table.types.logical.FloatType;
+import org.apache.flink.table.types.logical.IntType;
+import org.apache.flink.table.types.logical.LocalZonedTimestampType;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.LogicalTypeRoot;
+import org.apache.flink.table.types.logical.MapType;
+import org.apache.flink.table.types.logical.MultisetType;
+import org.apache.flink.table.types.logical.NullType;
+import org.apache.flink.table.types.logical.RawType;
+import org.apache.flink.table.types.logical.RowType;
+import org.apache.flink.table.types.logical.RowType.RowField;
+import org.apache.flink.table.types.logical.SmallIntType;
+import org.apache.flink.table.types.logical.TimestampType;
+import org.apache.flink.table.types.logical.TinyIntType;
+import org.apache.flink.table.types.logical.VarBinaryType;
+import org.apache.flink.table.types.logical.VarCharType;
+
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParser;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.DeserializationContext;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.deser.std.StdDeserializer;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ArrayNode;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_CLASS;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_ELEMENT_TYPE;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_FIELDS;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_FIELD_NAME;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_FIELD_TYPE;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_KEY_TYPE;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_LENGTH;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_NULLABLE;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_PRECISION;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_SCALE;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_SERIALIZER;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_TYPE_NAME;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_VALUE_TYPE;
+
+/**
+ * JSON deserializer for {@link LogicalType}.
+ *
+ * @see LogicalTypeJsonSerializer for the reverse operation.
+ */
+@PublicEvolving
Review Comment:
I think it should be `@Internal`
##########
flink-table/flink-sql-gateway-api/src/main/java/org/apache/flink/table/gateway/api/results/serde/LogicalTypeJsonSerializer.java:
##########
@@ -0,0 +1,210 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.table.gateway.api.results.serde;
+
+import org.apache.flink.annotation.PublicEvolving;
+import org.apache.flink.table.api.ValidationException;
+import org.apache.flink.table.types.logical.ArrayType;
+import org.apache.flink.table.types.logical.BinaryType;
+import org.apache.flink.table.types.logical.CharType;
+import org.apache.flink.table.types.logical.DecimalType;
+import org.apache.flink.table.types.logical.LocalZonedTimestampType;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.MapType;
+import org.apache.flink.table.types.logical.MultisetType;
+import org.apache.flink.table.types.logical.RawType;
+import org.apache.flink.table.types.logical.RowType;
+import org.apache.flink.table.types.logical.TimestampType;
+import org.apache.flink.table.types.logical.VarBinaryType;
+import org.apache.flink.table.types.logical.VarCharType;
+
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonGenerator;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.SerializerProvider;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ser.std.StdSerializer;
+
+import java.io.IOException;
+
+/**
+ * Json serializer for {@link LogicalType}.
+ *
+ * @see LogicalTypeJsonDeserializer for the reverse operation.
+ */
+@PublicEvolving
+public final class LogicalTypeJsonSerializer extends StdSerializer<LogicalType> {
+
+ private static final long serialVersionUID = 1L;
+
+ // --------------------------------------------------------------------------------------------
+ // Public string constants for serializer and deserializer
+ // --------------------------------------------------------------------------------------------
+
+ // Common fields
+ public static final String FIELD_NAME_TYPE_NAME = "type";
+ public static final String FIELD_NAME_NULLABLE = "nullable";
+
+ // CHAR, VARCHAR, BINARY, VARBINARY
+ public static final String FIELD_NAME_LENGTH = "length";
+
+ // DECIMAL, TIMESTAMP_WITHOUT_TIME_ZONE, TIMESTAMP_WITH_LOCAL_TIME_ZONE
+ public static final String FIELD_NAME_PRECISION = "precision";
+
+ // DECIMAL
+ public static final String FIELD_NAME_SCALE = "scale";
+
+ // MAP
+ public static final String FIELD_NAME_KEY_TYPE = "keyType";
+ public static final String FIELD_NAME_VALUE_TYPE = "valueType";
+
+ // ARRAY, MULTISET
+ public static final String FIELD_NAME_ELEMENT_TYPE = "elementType";
+
+ // ROW
+ public static final String FIELD_NAME_FIELDS = "fields";
+ public static final String FIELD_NAME_FIELD_NAME = "name";
+ public static final String FIELD_NAME_FIELD_TYPE = "fieldType";
+
+ // RAW
+ public static final String FIELD_NAME_CLASS = "class";
+ public static final String FIELD_NAME_SERIALIZER = "serializer";
+
+ public LogicalTypeJsonSerializer() {
+ super(LogicalType.class);
+ }
+
+ @Override
+ public void serialize(
+ LogicalType logicalType,
+ JsonGenerator jsonGenerator,
+ SerializerProvider serializerProvider)
+ throws IOException {
+ serializeInternal(logicalType, jsonGenerator);
+ }
+
+ private void serializeInternal(LogicalType logicalType, JsonGenerator jsonGenerator)
+ throws IOException {
+ jsonGenerator.writeStartObject();
+
+ // write common fields shared by all types
+ jsonGenerator.writeStringField(FIELD_NAME_TYPE_NAME, logicalType.getTypeRoot().name());
+ jsonGenerator.writeBooleanField(FIELD_NAME_NULLABLE, logicalType.isNullable());
+ // write special fields according to type root
+ switch (logicalType.getTypeRoot()) {
+ case BOOLEAN:
+ case TINYINT:
+ case SMALLINT:
+ case INTEGER:
+ case BIGINT:
+ case FLOAT:
+ case DOUBLE:
+ case DATE:
+ case NULL:
Review Comment:
NullType is alway nullable. I think we dont't need to add `"nullable": true` in the json.
##########
flink-table/flink-sql-gateway-api/src/main/java/org/apache/flink/table/gateway/api/results/serde/LogicalTypeJsonSerializer.java:
##########
@@ -0,0 +1,210 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.table.gateway.api.results.serde;
+
+import org.apache.flink.annotation.PublicEvolving;
+import org.apache.flink.table.api.ValidationException;
+import org.apache.flink.table.types.logical.ArrayType;
+import org.apache.flink.table.types.logical.BinaryType;
+import org.apache.flink.table.types.logical.CharType;
+import org.apache.flink.table.types.logical.DecimalType;
+import org.apache.flink.table.types.logical.LocalZonedTimestampType;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.MapType;
+import org.apache.flink.table.types.logical.MultisetType;
+import org.apache.flink.table.types.logical.RawType;
+import org.apache.flink.table.types.logical.RowType;
+import org.apache.flink.table.types.logical.TimestampType;
+import org.apache.flink.table.types.logical.VarBinaryType;
+import org.apache.flink.table.types.logical.VarCharType;
+
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonGenerator;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.SerializerProvider;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ser.std.StdSerializer;
+
+import java.io.IOException;
+
+/**
+ * Json serializer for {@link LogicalType}.
+ *
+ * @see LogicalTypeJsonDeserializer for the reverse operation.
+ */
+@PublicEvolving
+public final class LogicalTypeJsonSerializer extends StdSerializer<LogicalType> {
+
+ private static final long serialVersionUID = 1L;
+
+ // --------------------------------------------------------------------------------------------
+ // Public string constants for serializer and deserializer
+ // --------------------------------------------------------------------------------------------
+
+ // Common fields
+ public static final String FIELD_NAME_TYPE_NAME = "type";
+ public static final String FIELD_NAME_NULLABLE = "nullable";
+
+ // CHAR, VARCHAR, BINARY, VARBINARY
+ public static final String FIELD_NAME_LENGTH = "length";
+
+ // DECIMAL, TIMESTAMP_WITHOUT_TIME_ZONE, TIMESTAMP_WITH_LOCAL_TIME_ZONE
+ public static final String FIELD_NAME_PRECISION = "precision";
+
+ // DECIMAL
+ public static final String FIELD_NAME_SCALE = "scale";
+
+ // MAP
+ public static final String FIELD_NAME_KEY_TYPE = "keyType";
+ public static final String FIELD_NAME_VALUE_TYPE = "valueType";
+
+ // ARRAY, MULTISET
+ public static final String FIELD_NAME_ELEMENT_TYPE = "elementType";
+
+ // ROW
+ public static final String FIELD_NAME_FIELDS = "fields";
+ public static final String FIELD_NAME_FIELD_NAME = "name";
+ public static final String FIELD_NAME_FIELD_TYPE = "fieldType";
+
+ // RAW
+ public static final String FIELD_NAME_CLASS = "class";
+ public static final String FIELD_NAME_SERIALIZER = "serializer";
+
+ public LogicalTypeJsonSerializer() {
+ super(LogicalType.class);
+ }
+
+ @Override
+ public void serialize(
+ LogicalType logicalType,
+ JsonGenerator jsonGenerator,
+ SerializerProvider serializerProvider)
+ throws IOException {
+ serializeInternal(logicalType, jsonGenerator);
+ }
+
+ private void serializeInternal(LogicalType logicalType, JsonGenerator jsonGenerator)
+ throws IOException {
+ jsonGenerator.writeStartObject();
+
+ // write common fields shared by all types
+ jsonGenerator.writeStringField(FIELD_NAME_TYPE_NAME, logicalType.getTypeRoot().name());
+ jsonGenerator.writeBooleanField(FIELD_NAME_NULLABLE, logicalType.isNullable());
+ // write special fields according to type root
+ switch (logicalType.getTypeRoot()) {
+ case BOOLEAN:
+ case TINYINT:
+ case SMALLINT:
+ case INTEGER:
+ case BIGINT:
+ case FLOAT:
+ case DOUBLE:
+ case DATE:
+ case NULL:
+ break;
+ case CHAR:
+ jsonGenerator.writeNumberField(
+ FIELD_NAME_LENGTH, ((CharType) logicalType).getLength());
+ break;
+ case VARCHAR:
+ jsonGenerator.writeNumberField(
+ FIELD_NAME_LENGTH, ((VarCharType) logicalType).getLength());
+ break;
+ case BINARY:
+ jsonGenerator.writeNumberField(
+ FIELD_NAME_LENGTH, ((BinaryType) logicalType).getLength());
+ break;
+ case VARBINARY:
+ jsonGenerator.writeNumberField(
+ FIELD_NAME_LENGTH, ((VarBinaryType) logicalType).getLength());
+ break;
+ case DECIMAL:
+ jsonGenerator.writeNumberField(
+ FIELD_NAME_PRECISION, ((DecimalType) logicalType).getPrecision());
+ jsonGenerator.writeNumberField(
+ FIELD_NAME_SCALE, ((DecimalType) logicalType).getScale());
+ break;
+ case TIMESTAMP_WITHOUT_TIME_ZONE:
Review Comment:
miss `TIME_WITHOUT_TIME_ZONE` and `TIMESTAMP_WITH_TIME_ZONE`
##########
flink-table/flink-sql-gateway-api/src/main/java/org/apache/flink/table/gateway/api/results/serde/LogicalTypeJsonDeserializer.java:
##########
@@ -0,0 +1,219 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.table.gateway.api.results.serde;
+
+import org.apache.flink.annotation.PublicEvolving;
+import org.apache.flink.table.api.TableException;
+import org.apache.flink.table.types.logical.ArrayType;
+import org.apache.flink.table.types.logical.BigIntType;
+import org.apache.flink.table.types.logical.BinaryType;
+import org.apache.flink.table.types.logical.BooleanType;
+import org.apache.flink.table.types.logical.CharType;
+import org.apache.flink.table.types.logical.DateType;
+import org.apache.flink.table.types.logical.DecimalType;
+import org.apache.flink.table.types.logical.DoubleType;
+import org.apache.flink.table.types.logical.FloatType;
+import org.apache.flink.table.types.logical.IntType;
+import org.apache.flink.table.types.logical.LocalZonedTimestampType;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.LogicalTypeRoot;
+import org.apache.flink.table.types.logical.MapType;
+import org.apache.flink.table.types.logical.MultisetType;
+import org.apache.flink.table.types.logical.NullType;
+import org.apache.flink.table.types.logical.RawType;
+import org.apache.flink.table.types.logical.RowType;
+import org.apache.flink.table.types.logical.RowType.RowField;
+import org.apache.flink.table.types.logical.SmallIntType;
+import org.apache.flink.table.types.logical.TimestampType;
+import org.apache.flink.table.types.logical.TinyIntType;
+import org.apache.flink.table.types.logical.VarBinaryType;
+import org.apache.flink.table.types.logical.VarCharType;
+
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParser;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.DeserializationContext;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.deser.std.StdDeserializer;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ArrayNode;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_CLASS;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_ELEMENT_TYPE;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_FIELDS;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_FIELD_NAME;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_FIELD_TYPE;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_KEY_TYPE;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_LENGTH;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_NULLABLE;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_PRECISION;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_SCALE;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_SERIALIZER;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_TYPE_NAME;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_VALUE_TYPE;
+
+/**
+ * JSON deserializer for {@link LogicalType}.
+ *
+ * @see LogicalTypeJsonSerializer for the reverse operation.
+ */
+@PublicEvolving
+public final class LogicalTypeJsonDeserializer extends StdDeserializer<LogicalType> {
+
+ private static final long serialVersionUID = 1L;
+
+ LogicalTypeJsonDeserializer() {
+ super(LogicalType.class);
+ }
+
+ @Override
+ public LogicalType deserialize(JsonParser jsonParser, DeserializationContext ctx)
+ throws IOException {
+ return deserializeInternal(jsonParser.readValueAsTree());
+ }
+
+ /**
+ * Deserialize json according to the original type root. It's reverse operation of {@code
+ * SerializerWIP#serializeinternal}.
+ */
+ private LogicalType deserializeInternal(JsonNode logicalTypeNode) {
+ LogicalTypeRoot typeRoot =
+ LogicalTypeRoot.valueOf(logicalTypeNode.get(FIELD_NAME_TYPE_NAME).asText());
+ boolean isNullable = logicalTypeNode.get(FIELD_NAME_NULLABLE).asBoolean();
+ switch (typeRoot) {
+ case BOOLEAN:
+ return new BooleanType(isNullable);
+ case TINYINT:
+ return new TinyIntType(isNullable);
+ case SMALLINT:
+ return new SmallIntType(isNullable);
+ case INTEGER:
+ return new IntType(isNullable);
+ case BIGINT:
+ return new BigIntType(isNullable);
+ case FLOAT:
+ return new FloatType(isNullable);
+ case DOUBLE:
+ return new DoubleType(isNullable);
+ case DATE:
+ return new DateType(isNullable);
+ case NULL:
+ return new NullType();
+ case CHAR:
+ case VARCHAR:
+ case BINARY:
+ case VARBINARY:
+ return deserializeLengthFieldType(typeRoot, logicalTypeNode).copy(isNullable);
+ case DECIMAL:
+ return new DecimalType(
+ isNullable,
+ logicalTypeNode.get(FIELD_NAME_PRECISION).asInt(),
+ logicalTypeNode.get(FIELD_NAME_SCALE).asInt());
+ case TIMESTAMP_WITHOUT_TIME_ZONE:
+ case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
+ return deserializeTimestamp(typeRoot, logicalTypeNode).copy(isNullable);
+ case MAP:
+ return deserializeMap(logicalTypeNode).copy(isNullable);
+ case ARRAY:
+ case MULTISET:
+ return deserializeCollection(typeRoot, logicalTypeNode).copy(isNullable);
+ case ROW:
+ return deserializeRow(logicalTypeNode).copy(isNullable);
+ case RAW:
+ return deserializeRaw(logicalTypeNode).copy(isNullable);
+ default:
+ throw new TableException("Unsupported type root: " + typeRoot);
Review Comment:
Use`UnsupportedOperationException`.
##########
flink-table/flink-sql-gateway-api/src/test/java/org/apache/flink/table/gateway/api/results/serde/LogicalTypeJsonSerDeTest.java:
##########
@@ -0,0 +1,165 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.table.gateway.api.results.serde;
+
+import org.apache.flink.api.common.typeutils.base.LocalDateTimeSerializer;
+import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.runtime.typeutils.ExternalSerializer;
+import org.apache.flink.table.types.logical.ArrayType;
+import org.apache.flink.table.types.logical.BigIntType;
+import org.apache.flink.table.types.logical.BinaryType;
+import org.apache.flink.table.types.logical.BooleanType;
+import org.apache.flink.table.types.logical.CharType;
+import org.apache.flink.table.types.logical.DateType;
+import org.apache.flink.table.types.logical.DecimalType;
+import org.apache.flink.table.types.logical.DoubleType;
+import org.apache.flink.table.types.logical.FloatType;
+import org.apache.flink.table.types.logical.IntType;
+import org.apache.flink.table.types.logical.LocalZonedTimestampType;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.MapType;
+import org.apache.flink.table.types.logical.MultisetType;
+import org.apache.flink.table.types.logical.NullType;
+import org.apache.flink.table.types.logical.RawType;
+import org.apache.flink.table.types.logical.RowType;
+import org.apache.flink.table.types.logical.SmallIntType;
+import org.apache.flink.table.types.logical.TimestampType;
+import org.apache.flink.table.types.logical.TinyIntType;
+import org.apache.flink.table.types.logical.VarBinaryType;
+import org.apache.flink.table.types.logical.VarCharType;
+import org.apache.flink.types.Row;
+
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectMapper;
+
+import org.junit.jupiter.api.parallel.Execution;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
+
+import java.io.IOException;
+import java.time.LocalDateTime;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.jupiter.api.parallel.ExecutionMode.CONCURRENT;
+
+/** Tests for {@link LogicalType} serialization and deserialization. */
+@Execution(CONCURRENT)
+public class LogicalTypeJsonSerDeTest {
+
+ @ParameterizedTest
+ @MethodSource("generateTestData")
+ public void testLogicalTypeJsonSerDe(LogicalType logicalType) throws IOException {
+ ObjectMapper mapper = JsonSerdeUtil.getObjectMapper();
+ String json = mapper.writeValueAsString(logicalType);
+ LogicalType actualType = mapper.readValue(json, LogicalType.class);
+
+ assertThat(actualType).isEqualTo(logicalType);
+ }
+
+ // --------------------------------------------------------------------------------------------
+ // Test data
+ // --------------------------------------------------------------------------------------------
+
+ private static List<LogicalType> generateTestData() {
+ List<LogicalType> types =
+ Arrays.asList(
Review Comment:
Please also add
```
new TimeType(),
new TimeType(3),
new LocalZonedTimestampType(false, TimestampKind.PROCTIME, 3),
new TimestampType(false, TimestampKind.ROWTIME, 3),
new ZonedTimestampType(),
new ZonedTimestampType(3),
new ZonedTimestampType(false, TimestampKind.ROWTIME, 3),
new LocalZonedTimestampType(),
new LocalZonedTimestampType(3),
new LocalZonedTimestampType(false, TimestampKind.PROCTIME, 3),
new LocalZonedTimestampType(false, TimestampKind.ROWTIME, 3),
```
##########
flink-table/flink-sql-gateway-api/src/main/java/org/apache/flink/table/gateway/api/results/serde/LogicalTypeJsonDeserializer.java:
##########
@@ -0,0 +1,219 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.table.gateway.api.results.serde;
+
+import org.apache.flink.annotation.PublicEvolving;
+import org.apache.flink.table.api.TableException;
+import org.apache.flink.table.types.logical.ArrayType;
+import org.apache.flink.table.types.logical.BigIntType;
+import org.apache.flink.table.types.logical.BinaryType;
+import org.apache.flink.table.types.logical.BooleanType;
+import org.apache.flink.table.types.logical.CharType;
+import org.apache.flink.table.types.logical.DateType;
+import org.apache.flink.table.types.logical.DecimalType;
+import org.apache.flink.table.types.logical.DoubleType;
+import org.apache.flink.table.types.logical.FloatType;
+import org.apache.flink.table.types.logical.IntType;
+import org.apache.flink.table.types.logical.LocalZonedTimestampType;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.LogicalTypeRoot;
+import org.apache.flink.table.types.logical.MapType;
+import org.apache.flink.table.types.logical.MultisetType;
+import org.apache.flink.table.types.logical.NullType;
+import org.apache.flink.table.types.logical.RawType;
+import org.apache.flink.table.types.logical.RowType;
+import org.apache.flink.table.types.logical.RowType.RowField;
+import org.apache.flink.table.types.logical.SmallIntType;
+import org.apache.flink.table.types.logical.TimestampType;
+import org.apache.flink.table.types.logical.TinyIntType;
+import org.apache.flink.table.types.logical.VarBinaryType;
+import org.apache.flink.table.types.logical.VarCharType;
+
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParser;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.DeserializationContext;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.deser.std.StdDeserializer;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ArrayNode;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_CLASS;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_ELEMENT_TYPE;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_FIELDS;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_FIELD_NAME;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_FIELD_TYPE;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_KEY_TYPE;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_LENGTH;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_NULLABLE;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_PRECISION;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_SCALE;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_SERIALIZER;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_TYPE_NAME;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_VALUE_TYPE;
+
+/**
+ * JSON deserializer for {@link LogicalType}.
+ *
+ * @see LogicalTypeJsonSerializer for the reverse operation.
+ */
+@PublicEvolving
+public final class LogicalTypeJsonDeserializer extends StdDeserializer<LogicalType> {
+
+ private static final long serialVersionUID = 1L;
+
+ LogicalTypeJsonDeserializer() {
+ super(LogicalType.class);
+ }
+
+ @Override
+ public LogicalType deserialize(JsonParser jsonParser, DeserializationContext ctx)
+ throws IOException {
+ return deserializeInternal(jsonParser.readValueAsTree());
+ }
+
+ /**
+ * Deserialize json according to the original type root. It's reverse operation of {@code
+ * SerializerWIP#serializeinternal}.
+ */
+ private LogicalType deserializeInternal(JsonNode logicalTypeNode) {
+ LogicalTypeRoot typeRoot =
+ LogicalTypeRoot.valueOf(logicalTypeNode.get(FIELD_NAME_TYPE_NAME).asText());
+ boolean isNullable = logicalTypeNode.get(FIELD_NAME_NULLABLE).asBoolean();
+ switch (typeRoot) {
+ case BOOLEAN:
+ return new BooleanType(isNullable);
+ case TINYINT:
+ return new TinyIntType(isNullable);
+ case SMALLINT:
+ return new SmallIntType(isNullable);
+ case INTEGER:
+ return new IntType(isNullable);
+ case BIGINT:
+ return new BigIntType(isNullable);
+ case FLOAT:
+ return new FloatType(isNullable);
+ case DOUBLE:
+ return new DoubleType(isNullable);
+ case DATE:
+ return new DateType(isNullable);
+ case NULL:
+ return new NullType();
+ case CHAR:
+ case VARCHAR:
+ case BINARY:
+ case VARBINARY:
+ return deserializeLengthFieldType(typeRoot, logicalTypeNode).copy(isNullable);
+ case DECIMAL:
+ return new DecimalType(
+ isNullable,
+ logicalTypeNode.get(FIELD_NAME_PRECISION).asInt(),
+ logicalTypeNode.get(FIELD_NAME_SCALE).asInt());
+ case TIMESTAMP_WITHOUT_TIME_ZONE:
+ case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
+ return deserializeTimestamp(typeRoot, logicalTypeNode).copy(isNullable);
+ case MAP:
+ return deserializeMap(logicalTypeNode).copy(isNullable);
+ case ARRAY:
+ case MULTISET:
+ return deserializeCollection(typeRoot, logicalTypeNode).copy(isNullable);
+ case ROW:
+ return deserializeRow(logicalTypeNode).copy(isNullable);
+ case RAW:
+ return deserializeRaw(logicalTypeNode).copy(isNullable);
+ default:
+ throw new TableException("Unsupported type root: " + typeRoot);
+ }
+ }
+
+ // --------------------------------------------------------------------------------------------
+ // Helper methods for some complex types
+ // --------------------------------------------------------------------------------------------
+
+ private LogicalType deserializeLengthFieldType(
+ LogicalTypeRoot typeRoot, JsonNode logicalTypeNode) {
+ int length = logicalTypeNode.get(FIELD_NAME_LENGTH).asInt();
+ switch (typeRoot) {
+ case CHAR:
+ return length == 0 ? CharType.ofEmptyLiteral() : new CharType(length);
+ case VARCHAR:
+ return length == 0 ? VarCharType.ofEmptyLiteral() : new VarCharType(length);
+ case BINARY:
+ return length == 0 ? BinaryType.ofEmptyLiteral() : new BinaryType(length);
+ case VARBINARY:
+ return length == 0 ? VarBinaryType.ofEmptyLiteral() : new VarBinaryType(length);
+ default:
+ throw new TableException("Logical type with attribute 'length' expected.");
Review Comment:
"Can't convert json '%s' to the logical type %s."
BTW, I think it's better to throw SqlGatewayException here.
##########
flink-table/flink-sql-gateway-api/src/main/java/org/apache/flink/table/gateway/api/results/serde/LogicalTypeJsonDeserializer.java:
##########
@@ -0,0 +1,219 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.table.gateway.api.results.serde;
+
+import org.apache.flink.annotation.PublicEvolving;
+import org.apache.flink.table.api.TableException;
+import org.apache.flink.table.types.logical.ArrayType;
+import org.apache.flink.table.types.logical.BigIntType;
+import org.apache.flink.table.types.logical.BinaryType;
+import org.apache.flink.table.types.logical.BooleanType;
+import org.apache.flink.table.types.logical.CharType;
+import org.apache.flink.table.types.logical.DateType;
+import org.apache.flink.table.types.logical.DecimalType;
+import org.apache.flink.table.types.logical.DoubleType;
+import org.apache.flink.table.types.logical.FloatType;
+import org.apache.flink.table.types.logical.IntType;
+import org.apache.flink.table.types.logical.LocalZonedTimestampType;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.LogicalTypeRoot;
+import org.apache.flink.table.types.logical.MapType;
+import org.apache.flink.table.types.logical.MultisetType;
+import org.apache.flink.table.types.logical.NullType;
+import org.apache.flink.table.types.logical.RawType;
+import org.apache.flink.table.types.logical.RowType;
+import org.apache.flink.table.types.logical.RowType.RowField;
+import org.apache.flink.table.types.logical.SmallIntType;
+import org.apache.flink.table.types.logical.TimestampType;
+import org.apache.flink.table.types.logical.TinyIntType;
+import org.apache.flink.table.types.logical.VarBinaryType;
+import org.apache.flink.table.types.logical.VarCharType;
+
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParser;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.DeserializationContext;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.deser.std.StdDeserializer;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ArrayNode;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_CLASS;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_ELEMENT_TYPE;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_FIELDS;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_FIELD_NAME;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_FIELD_TYPE;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_KEY_TYPE;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_LENGTH;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_NULLABLE;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_PRECISION;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_SCALE;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_SERIALIZER;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_TYPE_NAME;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_VALUE_TYPE;
+
+/**
+ * JSON deserializer for {@link LogicalType}.
+ *
+ * @see LogicalTypeJsonSerializer for the reverse operation.
+ */
+@PublicEvolving
+public final class LogicalTypeJsonDeserializer extends StdDeserializer<LogicalType> {
+
+ private static final long serialVersionUID = 1L;
+
+ LogicalTypeJsonDeserializer() {
+ super(LogicalType.class);
+ }
+
+ @Override
+ public LogicalType deserialize(JsonParser jsonParser, DeserializationContext ctx)
+ throws IOException {
+ return deserializeInternal(jsonParser.readValueAsTree());
+ }
+
+ /**
+ * Deserialize json according to the original type root. It's reverse operation of {@code
+ * SerializerWIP#serializeinternal}.
+ */
+ private LogicalType deserializeInternal(JsonNode logicalTypeNode) {
+ LogicalTypeRoot typeRoot =
+ LogicalTypeRoot.valueOf(logicalTypeNode.get(FIELD_NAME_TYPE_NAME).asText());
+ boolean isNullable = logicalTypeNode.get(FIELD_NAME_NULLABLE).asBoolean();
+ switch (typeRoot) {
+ case BOOLEAN:
+ return new BooleanType(isNullable);
+ case TINYINT:
+ return new TinyIntType(isNullable);
+ case SMALLINT:
+ return new SmallIntType(isNullable);
+ case INTEGER:
+ return new IntType(isNullable);
+ case BIGINT:
+ return new BigIntType(isNullable);
+ case FLOAT:
+ return new FloatType(isNullable);
+ case DOUBLE:
+ return new DoubleType(isNullable);
+ case DATE:
+ return new DateType(isNullable);
+ case NULL:
+ return new NullType();
+ case CHAR:
+ case VARCHAR:
+ case BINARY:
+ case VARBINARY:
+ return deserializeLengthFieldType(typeRoot, logicalTypeNode).copy(isNullable);
+ case DECIMAL:
+ return new DecimalType(
+ isNullable,
+ logicalTypeNode.get(FIELD_NAME_PRECISION).asInt(),
+ logicalTypeNode.get(FIELD_NAME_SCALE).asInt());
+ case TIMESTAMP_WITHOUT_TIME_ZONE:
+ case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
+ return deserializeTimestamp(typeRoot, logicalTypeNode).copy(isNullable);
+ case MAP:
+ return deserializeMap(logicalTypeNode).copy(isNullable);
+ case ARRAY:
+ case MULTISET:
+ return deserializeCollection(typeRoot, logicalTypeNode).copy(isNullable);
+ case ROW:
+ return deserializeRow(logicalTypeNode).copy(isNullable);
+ case RAW:
+ return deserializeRaw(logicalTypeNode).copy(isNullable);
+ default:
+ throw new TableException("Unsupported type root: " + typeRoot);
+ }
+ }
+
+ // --------------------------------------------------------------------------------------------
+ // Helper methods for some complex types
+ // --------------------------------------------------------------------------------------------
+
+ private LogicalType deserializeLengthFieldType(
+ LogicalTypeRoot typeRoot, JsonNode logicalTypeNode) {
+ int length = logicalTypeNode.get(FIELD_NAME_LENGTH).asInt();
+ switch (typeRoot) {
+ case CHAR:
+ return length == 0 ? CharType.ofEmptyLiteral() : new CharType(length);
+ case VARCHAR:
+ return length == 0 ? VarCharType.ofEmptyLiteral() : new VarCharType(length);
+ case BINARY:
+ return length == 0 ? BinaryType.ofEmptyLiteral() : new BinaryType(length);
+ case VARBINARY:
+ return length == 0 ? VarBinaryType.ofEmptyLiteral() : new VarBinaryType(length);
+ default:
+ throw new TableException("Logical type with attribute 'length' expected.");
+ }
+ }
+
+ private LogicalType deserializeTimestamp(LogicalTypeRoot typeRoot, JsonNode logicalTypeNode) {
+ int precision = logicalTypeNode.get(FIELD_NAME_PRECISION).asInt();
+ switch (typeRoot) {
+ case TIMESTAMP_WITHOUT_TIME_ZONE:
+ return new TimestampType(precision);
+ case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
+ return new LocalZonedTimestampType(precision);
+ default:
+ throw new TableException("Timestamp type root expected.");
+ }
+ }
+
+ private LogicalType deserializeMap(JsonNode logicalTypeNode) {
+ JsonNode keyNode = logicalTypeNode.get(FIELD_NAME_KEY_TYPE);
+ LogicalType keyType = deserializeInternal(keyNode);
+ JsonNode valueNode = logicalTypeNode.get(FIELD_NAME_VALUE_TYPE);
+ LogicalType valueType = deserializeInternal(valueNode);
+ return new MapType(keyType, valueType);
+ }
+
+ private LogicalType deserializeCollection(LogicalTypeRoot typeRoot, JsonNode logicalTypeNode) {
+ JsonNode elementNode = logicalTypeNode.get(FIELD_NAME_ELEMENT_TYPE);
+ LogicalType elementType = deserializeInternal(elementNode);
+ switch (typeRoot) {
+ case ARRAY:
+ return new ArrayType(elementType);
+ case MULTISET:
+ return new MultisetType(elementType);
+ default:
+ throw new TableException("Collection type root expected.");
+ }
+ }
+
+ private LogicalType deserializeRow(JsonNode logicalTypeNode) {
+ ArrayNode fieldNodes = (ArrayNode) logicalTypeNode.get(FIELD_NAME_FIELDS);
+ List<RowField> fields = new ArrayList<>();
+ fieldNodes.forEach(
+ fieldNode -> {
+ String fieldName = fieldNode.get(FIELD_NAME_FIELD_NAME).asText();
+ LogicalType fieldType =
+ deserializeInternal(fieldNode.get(FIELD_NAME_FIELD_TYPE));
+ fields.add(new RowField(fieldName, fieldType));
+ });
+ return new RowType(fields);
+ }
+
+ private LogicalType deserializeRaw(JsonNode logicalTypeNode) {
+ String className = logicalTypeNode.get(FIELD_NAME_CLASS).asText();
+ String specialSerializer = logicalTypeNode.get(FIELD_NAME_SERIALIZER).asText();
Review Comment:
rename to serializer. We don't have special serializer.
##########
flink-table/flink-sql-gateway-api/src/main/java/org/apache/flink/table/gateway/api/results/serde/LogicalTypeJsonSerializer.java:
##########
@@ -0,0 +1,210 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.table.gateway.api.results.serde;
+
+import org.apache.flink.annotation.PublicEvolving;
+import org.apache.flink.table.api.ValidationException;
+import org.apache.flink.table.types.logical.ArrayType;
+import org.apache.flink.table.types.logical.BinaryType;
+import org.apache.flink.table.types.logical.CharType;
+import org.apache.flink.table.types.logical.DecimalType;
+import org.apache.flink.table.types.logical.LocalZonedTimestampType;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.MapType;
+import org.apache.flink.table.types.logical.MultisetType;
+import org.apache.flink.table.types.logical.RawType;
+import org.apache.flink.table.types.logical.RowType;
+import org.apache.flink.table.types.logical.TimestampType;
+import org.apache.flink.table.types.logical.VarBinaryType;
+import org.apache.flink.table.types.logical.VarCharType;
+
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonGenerator;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.SerializerProvider;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ser.std.StdSerializer;
+
+import java.io.IOException;
+
+/**
+ * Json serializer for {@link LogicalType}.
+ *
+ * @see LogicalTypeJsonDeserializer for the reverse operation.
+ */
+@PublicEvolving
+public final class LogicalTypeJsonSerializer extends StdSerializer<LogicalType> {
+
+ private static final long serialVersionUID = 1L;
+
+ // --------------------------------------------------------------------------------------------
+ // Public string constants for serializer and deserializer
+ // --------------------------------------------------------------------------------------------
+
+ // Common fields
+ public static final String FIELD_NAME_TYPE_NAME = "type";
+ public static final String FIELD_NAME_NULLABLE = "nullable";
+
+ // CHAR, VARCHAR, BINARY, VARBINARY
+ public static final String FIELD_NAME_LENGTH = "length";
+
+ // DECIMAL, TIMESTAMP_WITHOUT_TIME_ZONE, TIMESTAMP_WITH_LOCAL_TIME_ZONE
+ public static final String FIELD_NAME_PRECISION = "precision";
+
+ // DECIMAL
+ public static final String FIELD_NAME_SCALE = "scale";
+
+ // MAP
+ public static final String FIELD_NAME_KEY_TYPE = "keyType";
+ public static final String FIELD_NAME_VALUE_TYPE = "valueType";
+
+ // ARRAY, MULTISET
+ public static final String FIELD_NAME_ELEMENT_TYPE = "elementType";
+
+ // ROW
+ public static final String FIELD_NAME_FIELDS = "fields";
+ public static final String FIELD_NAME_FIELD_NAME = "name";
+ public static final String FIELD_NAME_FIELD_TYPE = "fieldType";
Review Comment:
missing description.
##########
flink-table/flink-sql-gateway-api/src/main/java/org/apache/flink/table/gateway/api/results/serde/LogicalTypeJsonSerializer.java:
##########
@@ -0,0 +1,210 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.table.gateway.api.results.serde;
+
+import org.apache.flink.annotation.PublicEvolving;
+import org.apache.flink.table.api.ValidationException;
+import org.apache.flink.table.types.logical.ArrayType;
+import org.apache.flink.table.types.logical.BinaryType;
+import org.apache.flink.table.types.logical.CharType;
+import org.apache.flink.table.types.logical.DecimalType;
+import org.apache.flink.table.types.logical.LocalZonedTimestampType;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.MapType;
+import org.apache.flink.table.types.logical.MultisetType;
+import org.apache.flink.table.types.logical.RawType;
+import org.apache.flink.table.types.logical.RowType;
+import org.apache.flink.table.types.logical.TimestampType;
+import org.apache.flink.table.types.logical.VarBinaryType;
+import org.apache.flink.table.types.logical.VarCharType;
+
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonGenerator;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.SerializerProvider;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ser.std.StdSerializer;
+
+import java.io.IOException;
+
+/**
+ * Json serializer for {@link LogicalType}.
+ *
+ * @see LogicalTypeJsonDeserializer for the reverse operation.
+ */
+@PublicEvolving
+public final class LogicalTypeJsonSerializer extends StdSerializer<LogicalType> {
+
+ private static final long serialVersionUID = 1L;
+
+ // --------------------------------------------------------------------------------------------
+ // Public string constants for serializer and deserializer
+ // --------------------------------------------------------------------------------------------
+
+ // Common fields
+ public static final String FIELD_NAME_TYPE_NAME = "type";
+ public static final String FIELD_NAME_NULLABLE = "nullable";
+
+ // CHAR, VARCHAR, BINARY, VARBINARY
+ public static final String FIELD_NAME_LENGTH = "length";
+
+ // DECIMAL, TIMESTAMP_WITHOUT_TIME_ZONE, TIMESTAMP_WITH_LOCAL_TIME_ZONE
+ public static final String FIELD_NAME_PRECISION = "precision";
+
+ // DECIMAL
+ public static final String FIELD_NAME_SCALE = "scale";
+
+ // MAP
+ public static final String FIELD_NAME_KEY_TYPE = "keyType";
+ public static final String FIELD_NAME_VALUE_TYPE = "valueType";
+
+ // ARRAY, MULTISET
+ public static final String FIELD_NAME_ELEMENT_TYPE = "elementType";
+
+ // ROW
+ public static final String FIELD_NAME_FIELDS = "fields";
+ public static final String FIELD_NAME_FIELD_NAME = "name";
+ public static final String FIELD_NAME_FIELD_TYPE = "fieldType";
+
+ // RAW
+ public static final String FIELD_NAME_CLASS = "class";
+ public static final String FIELD_NAME_SERIALIZER = "serializer";
+
+ public LogicalTypeJsonSerializer() {
+ super(LogicalType.class);
+ }
+
+ @Override
+ public void serialize(
+ LogicalType logicalType,
+ JsonGenerator jsonGenerator,
+ SerializerProvider serializerProvider)
+ throws IOException {
+ serializeInternal(logicalType, jsonGenerator);
+ }
+
+ private void serializeInternal(LogicalType logicalType, JsonGenerator jsonGenerator)
+ throws IOException {
+ jsonGenerator.writeStartObject();
+
+ // write common fields shared by all types
+ jsonGenerator.writeStringField(FIELD_NAME_TYPE_NAME, logicalType.getTypeRoot().name());
+ jsonGenerator.writeBooleanField(FIELD_NAME_NULLABLE, logicalType.isNullable());
+ // write special fields according to type root
+ switch (logicalType.getTypeRoot()) {
+ case BOOLEAN:
+ case TINYINT:
+ case SMALLINT:
+ case INTEGER:
+ case BIGINT:
+ case FLOAT:
+ case DOUBLE:
+ case DATE:
+ case NULL:
+ break;
+ case CHAR:
+ jsonGenerator.writeNumberField(
+ FIELD_NAME_LENGTH, ((CharType) logicalType).getLength());
+ break;
+ case VARCHAR:
+ jsonGenerator.writeNumberField(
+ FIELD_NAME_LENGTH, ((VarCharType) logicalType).getLength());
+ break;
+ case BINARY:
+ jsonGenerator.writeNumberField(
+ FIELD_NAME_LENGTH, ((BinaryType) logicalType).getLength());
+ break;
+ case VARBINARY:
+ jsonGenerator.writeNumberField(
+ FIELD_NAME_LENGTH, ((VarBinaryType) logicalType).getLength());
+ break;
+ case DECIMAL:
+ jsonGenerator.writeNumberField(
+ FIELD_NAME_PRECISION, ((DecimalType) logicalType).getPrecision());
+ jsonGenerator.writeNumberField(
+ FIELD_NAME_SCALE, ((DecimalType) logicalType).getScale());
+ break;
+ case TIMESTAMP_WITHOUT_TIME_ZONE:
+ jsonGenerator.writeNumberField(
+ FIELD_NAME_PRECISION, ((TimestampType) logicalType).getPrecision());
+ break;
+ case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
+ jsonGenerator.writeNumberField(
+ FIELD_NAME_PRECISION,
+ ((LocalZonedTimestampType) logicalType).getPrecision());
+ break;
+ case MAP:
+ serializeMap((MapType) logicalType, jsonGenerator);
+ break;
+ case ARRAY:
+ serializeCollection(((ArrayType) logicalType).getElementType(), jsonGenerator);
+ break;
+ case MULTISET:
+ serializeCollection(((MultisetType) logicalType).getElementType(), jsonGenerator);
+ break;
+ case ROW:
+ serializeRow((RowType) logicalType, jsonGenerator);
+ break;
+ case RAW:
+ if (logicalType instanceof RawType) {
+ serializeRaw((RawType<?>) logicalType, jsonGenerator);
+ break;
+ }
+ // fall through
+ default:
+ throw new ValidationException(
Review Comment:
I think it's better to use `UnsupportedException`.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink] yuzelin commented on a diff in pull request #20617: [FLINK-28936][sql-gateway] Fix REST endpoint can not serialize CHAR(0) by introducing a new serializer for LogicalType
Posted by GitBox <gi...@apache.org>.
yuzelin commented on code in PR #20617:
URL: https://github.com/apache/flink/pull/20617#discussion_r953493103
##########
flink-table/flink-sql-gateway/src/test/java/org/apache/flink/table/gateway/rest/util/SqlGatewayRestEndpointExtension.java:
##########
@@ -0,0 +1,90 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.table.gateway.rest.util;
+
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.table.gateway.api.SqlGatewayService;
+import org.apache.flink.table.gateway.api.utils.SqlGatewayException;
+import org.apache.flink.table.gateway.rest.SqlGatewayRestEndpoint;
+
+import org.junit.jupiter.api.extension.AfterAllCallback;
+import org.junit.jupiter.api.extension.BeforeAllCallback;
+import org.junit.jupiter.api.extension.Extension;
+import org.junit.jupiter.api.extension.ExtensionContext;
+
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.util.function.Supplier;
+
+import static org.apache.flink.table.gateway.rest.util.RestConfigUtils.getBaseConfig;
+import static org.apache.flink.table.gateway.rest.util.RestConfigUtils.getFlinkConfig;
+import static org.apache.flink.util.Preconditions.checkNotNull;
+
+/** A simple {@link Extension} to be used by tests that require a {@link SqlGatewayRestEndpoint}. */
+public class SqlGatewayRestEndpointExtension implements BeforeAllCallback, AfterAllCallback {
+
+ // to supply service needed when starting the rest end point
Review Comment:
After thinking about it, I think the usage of this variable is clear, so I removed the comment.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink] yuzelin commented on a diff in pull request #20617: [FLINK-28936][sql-gateway] Fix REST endpoint can not serialize CHAR(0) by introducing a new serializer for LogicalType
Posted by GitBox <gi...@apache.org>.
yuzelin commented on code in PR #20617:
URL: https://github.com/apache/flink/pull/20617#discussion_r951405303
##########
flink-table/flink-sql-gateway-api/src/main/java/org/apache/flink/table/gateway/api/results/serde/LogicalTypeJsonDeserializer.java:
##########
@@ -0,0 +1,219 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.table.gateway.api.results.serde;
+
+import org.apache.flink.annotation.PublicEvolving;
+import org.apache.flink.table.api.TableException;
+import org.apache.flink.table.types.logical.ArrayType;
+import org.apache.flink.table.types.logical.BigIntType;
+import org.apache.flink.table.types.logical.BinaryType;
+import org.apache.flink.table.types.logical.BooleanType;
+import org.apache.flink.table.types.logical.CharType;
+import org.apache.flink.table.types.logical.DateType;
+import org.apache.flink.table.types.logical.DecimalType;
+import org.apache.flink.table.types.logical.DoubleType;
+import org.apache.flink.table.types.logical.FloatType;
+import org.apache.flink.table.types.logical.IntType;
+import org.apache.flink.table.types.logical.LocalZonedTimestampType;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.LogicalTypeRoot;
+import org.apache.flink.table.types.logical.MapType;
+import org.apache.flink.table.types.logical.MultisetType;
+import org.apache.flink.table.types.logical.NullType;
+import org.apache.flink.table.types.logical.RawType;
+import org.apache.flink.table.types.logical.RowType;
+import org.apache.flink.table.types.logical.RowType.RowField;
+import org.apache.flink.table.types.logical.SmallIntType;
+import org.apache.flink.table.types.logical.TimestampType;
+import org.apache.flink.table.types.logical.TinyIntType;
+import org.apache.flink.table.types.logical.VarBinaryType;
+import org.apache.flink.table.types.logical.VarCharType;
+
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParser;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.DeserializationContext;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.deser.std.StdDeserializer;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ArrayNode;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_CLASS;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_ELEMENT_TYPE;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_FIELDS;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_FIELD_NAME;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_FIELD_TYPE;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_KEY_TYPE;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_LENGTH;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_NULLABLE;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_PRECISION;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_SCALE;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_SERIALIZER;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_TYPE_NAME;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_VALUE_TYPE;
+
+/**
+ * JSON deserializer for {@link LogicalType}.
+ *
+ * @see LogicalTypeJsonSerializer for the reverse operation.
+ */
+@PublicEvolving
+public final class LogicalTypeJsonDeserializer extends StdDeserializer<LogicalType> {
+
+ private static final long serialVersionUID = 1L;
+
+ LogicalTypeJsonDeserializer() {
+ super(LogicalType.class);
+ }
+
+ @Override
+ public LogicalType deserialize(JsonParser jsonParser, DeserializationContext ctx)
+ throws IOException {
+ return deserializeInternal(jsonParser.readValueAsTree());
+ }
+
+ /**
+ * Deserialize json according to the original type root. It's reverse operation of {@code
+ * SerializerWIP#serializeinternal}.
+ */
+ private LogicalType deserializeInternal(JsonNode logicalTypeNode) {
+ LogicalTypeRoot typeRoot =
+ LogicalTypeRoot.valueOf(logicalTypeNode.get(FIELD_NAME_TYPE_NAME).asText());
+ boolean isNullable = logicalTypeNode.get(FIELD_NAME_NULLABLE).asBoolean();
+ switch (typeRoot) {
+ case BOOLEAN:
+ return new BooleanType(isNullable);
+ case TINYINT:
+ return new TinyIntType(isNullable);
+ case SMALLINT:
+ return new SmallIntType(isNullable);
+ case INTEGER:
+ return new IntType(isNullable);
+ case BIGINT:
+ return new BigIntType(isNullable);
+ case FLOAT:
+ return new FloatType(isNullable);
+ case DOUBLE:
+ return new DoubleType(isNullable);
+ case DATE:
+ return new DateType(isNullable);
+ case NULL:
+ return new NullType();
+ case CHAR:
+ case VARCHAR:
+ case BINARY:
+ case VARBINARY:
+ return deserializeLengthFieldType(typeRoot, logicalTypeNode).copy(isNullable);
+ case DECIMAL:
+ return new DecimalType(
+ isNullable,
+ logicalTypeNode.get(FIELD_NAME_PRECISION).asInt(),
+ logicalTypeNode.get(FIELD_NAME_SCALE).asInt());
+ case TIMESTAMP_WITHOUT_TIME_ZONE:
+ case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
+ return deserializeTimestamp(typeRoot, logicalTypeNode).copy(isNullable);
+ case MAP:
+ return deserializeMap(logicalTypeNode).copy(isNullable);
+ case ARRAY:
+ case MULTISET:
+ return deserializeCollection(typeRoot, logicalTypeNode).copy(isNullable);
+ case ROW:
+ return deserializeRow(logicalTypeNode).copy(isNullable);
+ case RAW:
+ return deserializeRaw(logicalTypeNode).copy(isNullable);
+ default:
+ throw new TableException("Unsupported type root: " + typeRoot);
+ }
+ }
+
+ // --------------------------------------------------------------------------------------------
+ // Helper methods for some complex types
+ // --------------------------------------------------------------------------------------------
+
+ private LogicalType deserializeLengthFieldType(
+ LogicalTypeRoot typeRoot, JsonNode logicalTypeNode) {
+ int length = logicalTypeNode.get(FIELD_NAME_LENGTH).asInt();
+ switch (typeRoot) {
+ case CHAR:
+ return length == 0 ? CharType.ofEmptyLiteral() : new CharType(length);
+ case VARCHAR:
+ return length == 0 ? VarCharType.ofEmptyLiteral() : new VarCharType(length);
+ case BINARY:
+ return length == 0 ? BinaryType.ofEmptyLiteral() : new BinaryType(length);
+ case VARBINARY:
+ return length == 0 ? VarBinaryType.ofEmptyLiteral() : new VarBinaryType(length);
+ default:
+ throw new TableException("Logical type with attribute 'length' expected.");
+ }
+ }
+
+ private LogicalType deserializeTimestamp(LogicalTypeRoot typeRoot, JsonNode logicalTypeNode) {
+ int precision = logicalTypeNode.get(FIELD_NAME_PRECISION).asInt();
+ switch (typeRoot) {
+ case TIMESTAMP_WITHOUT_TIME_ZONE:
+ return new TimestampType(precision);
+ case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
+ return new LocalZonedTimestampType(precision);
+ default:
+ throw new TableException("Timestamp type root expected.");
+ }
+ }
+
+ private LogicalType deserializeMap(JsonNode logicalTypeNode) {
+ JsonNode keyNode = logicalTypeNode.get(FIELD_NAME_KEY_TYPE);
+ LogicalType keyType = deserializeInternal(keyNode);
+ JsonNode valueNode = logicalTypeNode.get(FIELD_NAME_VALUE_TYPE);
+ LogicalType valueType = deserializeInternal(valueNode);
+ return new MapType(keyType, valueType);
+ }
+
+ private LogicalType deserializeCollection(LogicalTypeRoot typeRoot, JsonNode logicalTypeNode) {
+ JsonNode elementNode = logicalTypeNode.get(FIELD_NAME_ELEMENT_TYPE);
+ LogicalType elementType = deserializeInternal(elementNode);
+ switch (typeRoot) {
+ case ARRAY:
+ return new ArrayType(elementType);
+ case MULTISET:
+ return new MultisetType(elementType);
+ default:
+ throw new TableException("Collection type root expected.");
+ }
+ }
+
+ private LogicalType deserializeRow(JsonNode logicalTypeNode) {
+ ArrayNode fieldNodes = (ArrayNode) logicalTypeNode.get(FIELD_NAME_FIELDS);
+ List<RowField> fields = new ArrayList<>();
+ fieldNodes.forEach(
+ fieldNode -> {
+ String fieldName = fieldNode.get(FIELD_NAME_FIELD_NAME).asText();
+ LogicalType fieldType =
+ deserializeInternal(fieldNode.get(FIELD_NAME_FIELD_TYPE));
+ fields.add(new RowField(fieldName, fieldType));
+ });
+ return new RowType(fields);
+ }
+
+ private LogicalType deserializeRaw(JsonNode logicalTypeNode) {
+ String className = logicalTypeNode.get(FIELD_NAME_CLASS).asText();
+ String specialSerializer = logicalTypeNode.get(FIELD_NAME_SERIALIZER).asText();
Review Comment:
Renamed.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink] yuzelin commented on a diff in pull request #20617: [FLINK-28936][sql-gateway] Fix REST endpoint can not serialize CHAR(0) by introducing a new serializer for LogicalType
Posted by GitBox <gi...@apache.org>.
yuzelin commented on code in PR #20617:
URL: https://github.com/apache/flink/pull/20617#discussion_r953562914
##########
flink-table/flink-sql-gateway-api/src/test/java/org/apache/flink/table/gateway/api/results/serde/LogicalTypeJsonSerDeTest.java:
##########
@@ -0,0 +1,233 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.table.gateway.api.results.serde;
+
+import org.apache.flink.api.common.typeutils.base.LocalDateTimeSerializer;
+import org.apache.flink.core.testutils.FlinkAssertions;
+import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.runtime.typeutils.ExternalSerializer;
+import org.apache.flink.table.types.logical.ArrayType;
+import org.apache.flink.table.types.logical.BigIntType;
+import org.apache.flink.table.types.logical.BinaryType;
+import org.apache.flink.table.types.logical.BooleanType;
+import org.apache.flink.table.types.logical.CharType;
+import org.apache.flink.table.types.logical.DateType;
+import org.apache.flink.table.types.logical.DayTimeIntervalType;
+import org.apache.flink.table.types.logical.DecimalType;
+import org.apache.flink.table.types.logical.DoubleType;
+import org.apache.flink.table.types.logical.FloatType;
+import org.apache.flink.table.types.logical.IntType;
+import org.apache.flink.table.types.logical.LocalZonedTimestampType;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.MapType;
+import org.apache.flink.table.types.logical.MultisetType;
+import org.apache.flink.table.types.logical.NullType;
+import org.apache.flink.table.types.logical.RawType;
+import org.apache.flink.table.types.logical.RowType;
+import org.apache.flink.table.types.logical.SmallIntType;
+import org.apache.flink.table.types.logical.TimeType;
+import org.apache.flink.table.types.logical.TimestampKind;
+import org.apache.flink.table.types.logical.TimestampType;
+import org.apache.flink.table.types.logical.TinyIntType;
+import org.apache.flink.table.types.logical.VarBinaryType;
+import org.apache.flink.table.types.logical.VarCharType;
+import org.apache.flink.table.types.logical.ZonedTimestampType;
+import org.apache.flink.types.Row;
+
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.module.SimpleModule;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.parallel.Execution;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
+
+import java.io.IOException;
+import java.time.LocalDateTime;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.junit.jupiter.api.parallel.ExecutionMode.CONCURRENT;
+
+/** Tests for {@link LogicalType} serialization and deserialization. */
+@Execution(CONCURRENT)
+public class LogicalTypeJsonSerDeTest {
+
+ private final ObjectMapper mapper = getObjectMapper();
+
+ // final constants for testing unsupported case
+ private final LogicalType unsupportedType =
+ new DayTimeIntervalType(DayTimeIntervalType.DayTimeResolution.DAY_TO_HOUR);
+ private final String serializerExceptionMessageFormat =
+ "Unable to serialize logical type '%s'. Please check the documentation for supported types.";
+ private final String unsupportedTypeString = "INTERVAL_DAY_TIME";
+ private final String json =
+ String.format(
+ "{\"%s\": \"%s\", \"%s\": %s}",
+ "type", unsupportedTypeString, "nullable", "true");
+ private final String deserializerExceptionMessageFormat =
+ "Unable to deserialize a logical type of type root '%s'. Please check the documentation for supported types.";
Review Comment:
Changed.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink] yuzelin commented on a diff in pull request #20617: [FLINK-28936][sql-gateway] Fix REST endpoint can not serialize CHAR(0) by introducing a new serializer for LogicalType
Posted by GitBox <gi...@apache.org>.
yuzelin commented on code in PR #20617:
URL: https://github.com/apache/flink/pull/20617#discussion_r953558373
##########
flink-table/flink-sql-gateway-api/src/main/java/org/apache/flink/table/gateway/api/results/serde/LogicalTypeJsonDeserializer.java:
##########
@@ -98,6 +100,10 @@ public LogicalType deserialize(JsonParser jsonParser, DeserializationContext ctx
private LogicalType deserializeInternal(JsonNode logicalTypeNode) {
LogicalTypeRoot typeRoot =
LogicalTypeRoot.valueOf(logicalTypeNode.get(FIELD_NAME_TYPE_NAME).asText());
+ // handle the special case of NullType
Review Comment:
Made it clearer.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink] yuzelin commented on a diff in pull request #20617: [FLINK-28936][sql-gateway] Fix REST endpoint can not serialize CHAR(0) by introducing a new serializer for LogicalType
Posted by GitBox <gi...@apache.org>.
yuzelin commented on code in PR #20617:
URL: https://github.com/apache/flink/pull/20617#discussion_r951367986
##########
flink-table/flink-sql-gateway-api/src/main/java/org/apache/flink/table/gateway/api/results/serde/LogicalTypeJsonDeserializer.java:
##########
@@ -0,0 +1,219 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.table.gateway.api.results.serde;
+
+import org.apache.flink.annotation.PublicEvolving;
+import org.apache.flink.table.api.TableException;
+import org.apache.flink.table.types.logical.ArrayType;
+import org.apache.flink.table.types.logical.BigIntType;
+import org.apache.flink.table.types.logical.BinaryType;
+import org.apache.flink.table.types.logical.BooleanType;
+import org.apache.flink.table.types.logical.CharType;
+import org.apache.flink.table.types.logical.DateType;
+import org.apache.flink.table.types.logical.DecimalType;
+import org.apache.flink.table.types.logical.DoubleType;
+import org.apache.flink.table.types.logical.FloatType;
+import org.apache.flink.table.types.logical.IntType;
+import org.apache.flink.table.types.logical.LocalZonedTimestampType;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.LogicalTypeRoot;
+import org.apache.flink.table.types.logical.MapType;
+import org.apache.flink.table.types.logical.MultisetType;
+import org.apache.flink.table.types.logical.NullType;
+import org.apache.flink.table.types.logical.RawType;
+import org.apache.flink.table.types.logical.RowType;
+import org.apache.flink.table.types.logical.RowType.RowField;
+import org.apache.flink.table.types.logical.SmallIntType;
+import org.apache.flink.table.types.logical.TimestampType;
+import org.apache.flink.table.types.logical.TinyIntType;
+import org.apache.flink.table.types.logical.VarBinaryType;
+import org.apache.flink.table.types.logical.VarCharType;
+
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParser;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.DeserializationContext;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.deser.std.StdDeserializer;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ArrayNode;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_CLASS;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_ELEMENT_TYPE;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_FIELDS;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_FIELD_NAME;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_FIELD_TYPE;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_KEY_TYPE;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_LENGTH;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_NULLABLE;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_PRECISION;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_SCALE;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_SERIALIZER;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_TYPE_NAME;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_VALUE_TYPE;
+
+/**
+ * JSON deserializer for {@link LogicalType}.
+ *
+ * @see LogicalTypeJsonSerializer for the reverse operation.
+ */
+@PublicEvolving
+public final class LogicalTypeJsonDeserializer extends StdDeserializer<LogicalType> {
+
+ private static final long serialVersionUID = 1L;
+
+ LogicalTypeJsonDeserializer() {
+ super(LogicalType.class);
+ }
+
+ @Override
+ public LogicalType deserialize(JsonParser jsonParser, DeserializationContext ctx)
+ throws IOException {
+ return deserializeInternal(jsonParser.readValueAsTree());
+ }
+
+ /**
+ * Deserialize json according to the original type root. It's reverse operation of {@code
+ * SerializerWIP#serializeinternal}.
+ */
+ private LogicalType deserializeInternal(JsonNode logicalTypeNode) {
+ LogicalTypeRoot typeRoot =
+ LogicalTypeRoot.valueOf(logicalTypeNode.get(FIELD_NAME_TYPE_NAME).asText());
+ boolean isNullable = logicalTypeNode.get(FIELD_NAME_NULLABLE).asBoolean();
+ switch (typeRoot) {
+ case BOOLEAN:
+ return new BooleanType(isNullable);
+ case TINYINT:
+ return new TinyIntType(isNullable);
+ case SMALLINT:
+ return new SmallIntType(isNullable);
+ case INTEGER:
+ return new IntType(isNullable);
+ case BIGINT:
+ return new BigIntType(isNullable);
+ case FLOAT:
+ return new FloatType(isNullable);
+ case DOUBLE:
+ return new DoubleType(isNullable);
+ case DATE:
+ return new DateType(isNullable);
+ case NULL:
+ return new NullType();
+ case CHAR:
+ case VARCHAR:
+ case BINARY:
+ case VARBINARY:
+ return deserializeLengthFieldType(typeRoot, logicalTypeNode).copy(isNullable);
+ case DECIMAL:
+ return new DecimalType(
+ isNullable,
+ logicalTypeNode.get(FIELD_NAME_PRECISION).asInt(),
+ logicalTypeNode.get(FIELD_NAME_SCALE).asInt());
+ case TIMESTAMP_WITHOUT_TIME_ZONE:
+ case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
+ return deserializeTimestamp(typeRoot, logicalTypeNode).copy(isNullable);
+ case MAP:
+ return deserializeMap(logicalTypeNode).copy(isNullable);
+ case ARRAY:
+ case MULTISET:
+ return deserializeCollection(typeRoot, logicalTypeNode).copy(isNullable);
+ case ROW:
+ return deserializeRow(logicalTypeNode).copy(isNullable);
+ case RAW:
+ return deserializeRaw(logicalTypeNode).copy(isNullable);
+ default:
+ throw new TableException("Unsupported type root: " + typeRoot);
Review Comment:
Changed.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink] yuzelin commented on a diff in pull request #20617: [FLINK-28936][sql-gateway] Fix REST endpoint can not serialize CHAR(0) by introducing a new serializer for LogicalType
Posted by GitBox <gi...@apache.org>.
yuzelin commented on code in PR #20617:
URL: https://github.com/apache/flink/pull/20617#discussion_r953566304
##########
flink-table/flink-sql-gateway-api/src/test/java/org/apache/flink/table/gateway/api/results/serde/LogicalTypeJsonSerDeTest.java:
##########
@@ -116,6 +151,8 @@ private static List<LogicalType> generateTestData() {
new LocalZonedTimestampType(),
new LocalZonedTimestampType(3),
new LocalZonedTimestampType(false, 3),
+ // LocalZonedTimestampType#eaquals doesn't compare TimestampKind
Review Comment:
Removed.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink] yuzelin commented on a diff in pull request #20617: [FLINK-28936][sql-gateway] Fix REST endpoint can not serialize CHAR(0) by introducing a new serializer for LogicalType
Posted by GitBox <gi...@apache.org>.
yuzelin commented on code in PR #20617:
URL: https://github.com/apache/flink/pull/20617#discussion_r953488968
##########
flink-table/flink-sql-gateway/src/test/java/org/apache/flink/table/gateway/rest/util/SqlGatewayRestEndpointExtension.java:
##########
@@ -0,0 +1,90 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.table.gateway.rest.util;
+
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.table.gateway.api.SqlGatewayService;
+import org.apache.flink.table.gateway.api.utils.SqlGatewayException;
+import org.apache.flink.table.gateway.rest.SqlGatewayRestEndpoint;
+
+import org.junit.jupiter.api.extension.AfterAllCallback;
+import org.junit.jupiter.api.extension.BeforeAllCallback;
+import org.junit.jupiter.api.extension.Extension;
+import org.junit.jupiter.api.extension.ExtensionContext;
+
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.util.function.Supplier;
+
+import static org.apache.flink.table.gateway.rest.util.RestConfigUtils.getBaseConfig;
+import static org.apache.flink.table.gateway.rest.util.RestConfigUtils.getFlinkConfig;
+import static org.apache.flink.util.Preconditions.checkNotNull;
+
+/** A simple {@link Extension} to be used by tests that require a {@link SqlGatewayRestEndpoint}. */
Review Comment:
Changed.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink] yuzelin commented on a diff in pull request #20617: [FLINK-28936][sql-gateway] Fix REST endpoint can not serialize CHAR(0) by introducing a new serializer for LogicalType
Posted by GitBox <gi...@apache.org>.
yuzelin commented on code in PR #20617:
URL: https://github.com/apache/flink/pull/20617#discussion_r951326179
##########
flink-table/flink-sql-gateway-api/src/main/java/org/apache/flink/table/gateway/api/results/serde/LogicalTypeJsonSerializer.java:
##########
@@ -0,0 +1,210 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.table.gateway.api.results.serde;
+
+import org.apache.flink.annotation.PublicEvolving;
+import org.apache.flink.table.api.ValidationException;
+import org.apache.flink.table.types.logical.ArrayType;
+import org.apache.flink.table.types.logical.BinaryType;
+import org.apache.flink.table.types.logical.CharType;
+import org.apache.flink.table.types.logical.DecimalType;
+import org.apache.flink.table.types.logical.LocalZonedTimestampType;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.MapType;
+import org.apache.flink.table.types.logical.MultisetType;
+import org.apache.flink.table.types.logical.RawType;
+import org.apache.flink.table.types.logical.RowType;
+import org.apache.flink.table.types.logical.TimestampType;
+import org.apache.flink.table.types.logical.VarBinaryType;
+import org.apache.flink.table.types.logical.VarCharType;
+
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonGenerator;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.SerializerProvider;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ser.std.StdSerializer;
+
+import java.io.IOException;
+
+/**
+ * Json serializer for {@link LogicalType}.
+ *
+ * @see LogicalTypeJsonDeserializer for the reverse operation.
+ */
+@PublicEvolving
+public final class LogicalTypeJsonSerializer extends StdSerializer<LogicalType> {
+
+ private static final long serialVersionUID = 1L;
+
+ // --------------------------------------------------------------------------------------------
+ // Public string constants for serializer and deserializer
+ // --------------------------------------------------------------------------------------------
+
+ // Common fields
+ public static final String FIELD_NAME_TYPE_NAME = "type";
+ public static final String FIELD_NAME_NULLABLE = "nullable";
+
+ // CHAR, VARCHAR, BINARY, VARBINARY
+ public static final String FIELD_NAME_LENGTH = "length";
+
+ // DECIMAL, TIMESTAMP_WITHOUT_TIME_ZONE, TIMESTAMP_WITH_LOCAL_TIME_ZONE
+ public static final String FIELD_NAME_PRECISION = "precision";
+
+ // DECIMAL
+ public static final String FIELD_NAME_SCALE = "scale";
+
+ // MAP
+ public static final String FIELD_NAME_KEY_TYPE = "keyType";
+ public static final String FIELD_NAME_VALUE_TYPE = "valueType";
+
+ // ARRAY, MULTISET
+ public static final String FIELD_NAME_ELEMENT_TYPE = "elementType";
+
+ // ROW
+ public static final String FIELD_NAME_FIELDS = "fields";
+ public static final String FIELD_NAME_FIELD_NAME = "name";
+ public static final String FIELD_NAME_FIELD_TYPE = "fieldType";
Review Comment:
Added code for serialization and deserialization for `RowType#description` and corresponding test.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink] fsk119 commented on a diff in pull request #20617: [FLINK-28936][sql-gateway] Fix REST endpoint can not serialize CHAR(0) by introducing a new serializer for LogicalType
Posted by GitBox <gi...@apache.org>.
fsk119 commented on code in PR #20617:
URL: https://github.com/apache/flink/pull/20617#discussion_r953388060
##########
flink-table/flink-sql-gateway/src/test/java/org/apache/flink/table/gateway/rest/util/SqlGatewayRestEndpointExtension.java:
##########
@@ -0,0 +1,90 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.table.gateway.rest.util;
+
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.table.gateway.api.SqlGatewayService;
+import org.apache.flink.table.gateway.api.utils.SqlGatewayException;
+import org.apache.flink.table.gateway.rest.SqlGatewayRestEndpoint;
+
+import org.junit.jupiter.api.extension.AfterAllCallback;
+import org.junit.jupiter.api.extension.BeforeAllCallback;
+import org.junit.jupiter.api.extension.Extension;
+import org.junit.jupiter.api.extension.ExtensionContext;
+
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.util.function.Supplier;
+
+import static org.apache.flink.table.gateway.rest.util.RestConfigUtils.getBaseConfig;
+import static org.apache.flink.table.gateway.rest.util.RestConfigUtils.getFlinkConfig;
+import static org.apache.flink.util.Preconditions.checkNotNull;
+
+/** A simple {@link Extension} to be used by tests that require a {@link SqlGatewayRestEndpoint}. */
Review Comment:
nit:
```
/** A simple {@link Extension} that manages the lifecycle of the {@link SqlGatewayRestEndpoint}. */
```
##########
flink-table/flink-sql-gateway/src/test/java/org/apache/flink/table/gateway/rest/util/SqlGatewayRestEndpointExtension.java:
##########
@@ -0,0 +1,90 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.table.gateway.rest.util;
+
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.table.gateway.api.SqlGatewayService;
+import org.apache.flink.table.gateway.api.utils.SqlGatewayException;
+import org.apache.flink.table.gateway.rest.SqlGatewayRestEndpoint;
+
+import org.junit.jupiter.api.extension.AfterAllCallback;
+import org.junit.jupiter.api.extension.BeforeAllCallback;
+import org.junit.jupiter.api.extension.Extension;
+import org.junit.jupiter.api.extension.ExtensionContext;
+
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.util.function.Supplier;
+
+import static org.apache.flink.table.gateway.rest.util.RestConfigUtils.getBaseConfig;
+import static org.apache.flink.table.gateway.rest.util.RestConfigUtils.getFlinkConfig;
+import static org.apache.flink.util.Preconditions.checkNotNull;
+
+/** A simple {@link Extension} to be used by tests that require a {@link SqlGatewayRestEndpoint}. */
+public class SqlGatewayRestEndpointExtension implements BeforeAllCallback, AfterAllCallback {
+
+ // to supply service needed when starting the rest end point
Review Comment:
Remove? Or use /** */ to describe fields.
##########
flink-table/flink-sql-gateway-api/src/test/java/org/apache/flink/table/gateway/api/results/serde/LogicalTypeJsonSerDeTest.java:
##########
@@ -0,0 +1,233 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.table.gateway.api.results.serde;
+
+import org.apache.flink.api.common.typeutils.base.LocalDateTimeSerializer;
+import org.apache.flink.core.testutils.FlinkAssertions;
+import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.runtime.typeutils.ExternalSerializer;
+import org.apache.flink.table.types.logical.ArrayType;
+import org.apache.flink.table.types.logical.BigIntType;
+import org.apache.flink.table.types.logical.BinaryType;
+import org.apache.flink.table.types.logical.BooleanType;
+import org.apache.flink.table.types.logical.CharType;
+import org.apache.flink.table.types.logical.DateType;
+import org.apache.flink.table.types.logical.DayTimeIntervalType;
+import org.apache.flink.table.types.logical.DecimalType;
+import org.apache.flink.table.types.logical.DoubleType;
+import org.apache.flink.table.types.logical.FloatType;
+import org.apache.flink.table.types.logical.IntType;
+import org.apache.flink.table.types.logical.LocalZonedTimestampType;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.MapType;
+import org.apache.flink.table.types.logical.MultisetType;
+import org.apache.flink.table.types.logical.NullType;
+import org.apache.flink.table.types.logical.RawType;
+import org.apache.flink.table.types.logical.RowType;
+import org.apache.flink.table.types.logical.SmallIntType;
+import org.apache.flink.table.types.logical.TimeType;
+import org.apache.flink.table.types.logical.TimestampKind;
+import org.apache.flink.table.types.logical.TimestampType;
+import org.apache.flink.table.types.logical.TinyIntType;
+import org.apache.flink.table.types.logical.VarBinaryType;
+import org.apache.flink.table.types.logical.VarCharType;
+import org.apache.flink.table.types.logical.ZonedTimestampType;
+import org.apache.flink.types.Row;
+
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.module.SimpleModule;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.parallel.Execution;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
+
+import java.io.IOException;
+import java.time.LocalDateTime;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.junit.jupiter.api.parallel.ExecutionMode.CONCURRENT;
+
+/** Tests for {@link LogicalType} serialization and deserialization. */
+@Execution(CONCURRENT)
+public class LogicalTypeJsonSerDeTest {
+
+ private final ObjectMapper mapper = getObjectMapper();
+
+ // final constants for testing unsupported case
+ private final LogicalType unsupportedType =
+ new DayTimeIntervalType(DayTimeIntervalType.DayTimeResolution.DAY_TO_HOUR);
+ private final String serializerExceptionMessageFormat =
+ "Unable to serialize logical type '%s'. Please check the documentation for supported types.";
+ private final String unsupportedTypeString = "INTERVAL_DAY_TIME";
+ private final String json =
+ String.format(
+ "{\"%s\": \"%s\", \"%s\": %s}",
+ "type", unsupportedTypeString, "nullable", "true");
+ private final String deserializerExceptionMessageFormat =
+ "Unable to deserialize a logical type of type root '%s'. Please check the documentation for supported types.";
Review Comment:
Local variables are enough. Move into the test case.
##########
flink-table/flink-sql-gateway/src/test/java/org/apache/flink/table/gateway/rest/util/SqlGatewayRestEndpointExtension.java:
##########
@@ -0,0 +1,90 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.table.gateway.rest.util;
+
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.table.gateway.api.SqlGatewayService;
+import org.apache.flink.table.gateway.api.utils.SqlGatewayException;
+import org.apache.flink.table.gateway.rest.SqlGatewayRestEndpoint;
+
+import org.junit.jupiter.api.extension.AfterAllCallback;
+import org.junit.jupiter.api.extension.BeforeAllCallback;
+import org.junit.jupiter.api.extension.Extension;
+import org.junit.jupiter.api.extension.ExtensionContext;
+
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.util.function.Supplier;
+
+import static org.apache.flink.table.gateway.rest.util.RestConfigUtils.getBaseConfig;
+import static org.apache.flink.table.gateway.rest.util.RestConfigUtils.getFlinkConfig;
+import static org.apache.flink.util.Preconditions.checkNotNull;
+
+/** A simple {@link Extension} to be used by tests that require a {@link SqlGatewayRestEndpoint}. */
+public class SqlGatewayRestEndpointExtension implements BeforeAllCallback, AfterAllCallback {
+
+ // to supply service needed when starting the rest end point
+ private final Supplier<SqlGatewayService> serviceSupplier;
+
+ private SqlGatewayRestEndpoint sqlGatewayRestEndpoint;
+ private String targetAddress;
+ private int targetPort;
+
+ public String getTargetAddress() {
+ return targetAddress;
+ }
+
+ public int getTargetPort() {
+ return targetPort;
+ }
+
+ public SqlGatewayRestEndpointExtension(Supplier<SqlGatewayService> serviceSupplier) {
+ this.serviceSupplier = serviceSupplier;
+ }
+
+ @Override
+ public void beforeAll(ExtensionContext context) {
+ String address = InetAddress.getLoopbackAddress().getHostAddress();
+ Configuration config = getBaseConfig(getFlinkConfig(address, address, "0"));
+
+ try {
+ sqlGatewayRestEndpoint = new SqlGatewayRestEndpoint(config, serviceSupplier.get());
+ sqlGatewayRestEndpoint.start();
+ } catch (Exception e) {
+ throw new SqlGatewayException(
+ "Unexpected error occurred when trying to start the rest endpoint of sql gateway.",
+ e);
+ }
+
+ InetSocketAddress serverAddress = checkNotNull(sqlGatewayRestEndpoint.getServerAddress());
+ targetAddress = serverAddress.getHostName();
+ targetPort = serverAddress.getPort();
Review Comment:
Can we use NetUtils to get a random port? If other CI runs in the same machine, it may fail to bind the port.
##########
flink-table/flink-sql-gateway-api/src/test/java/org/apache/flink/table/gateway/api/results/serde/LogicalTypeJsonSerDeTest.java:
##########
@@ -0,0 +1,233 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.table.gateway.api.results.serde;
+
+import org.apache.flink.api.common.typeutils.base.LocalDateTimeSerializer;
+import org.apache.flink.core.testutils.FlinkAssertions;
+import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.runtime.typeutils.ExternalSerializer;
+import org.apache.flink.table.types.logical.ArrayType;
+import org.apache.flink.table.types.logical.BigIntType;
+import org.apache.flink.table.types.logical.BinaryType;
+import org.apache.flink.table.types.logical.BooleanType;
+import org.apache.flink.table.types.logical.CharType;
+import org.apache.flink.table.types.logical.DateType;
+import org.apache.flink.table.types.logical.DayTimeIntervalType;
+import org.apache.flink.table.types.logical.DecimalType;
+import org.apache.flink.table.types.logical.DoubleType;
+import org.apache.flink.table.types.logical.FloatType;
+import org.apache.flink.table.types.logical.IntType;
+import org.apache.flink.table.types.logical.LocalZonedTimestampType;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.MapType;
+import org.apache.flink.table.types.logical.MultisetType;
+import org.apache.flink.table.types.logical.NullType;
+import org.apache.flink.table.types.logical.RawType;
+import org.apache.flink.table.types.logical.RowType;
+import org.apache.flink.table.types.logical.SmallIntType;
+import org.apache.flink.table.types.logical.TimeType;
+import org.apache.flink.table.types.logical.TimestampKind;
+import org.apache.flink.table.types.logical.TimestampType;
+import org.apache.flink.table.types.logical.TinyIntType;
+import org.apache.flink.table.types.logical.VarBinaryType;
+import org.apache.flink.table.types.logical.VarCharType;
+import org.apache.flink.table.types.logical.ZonedTimestampType;
+import org.apache.flink.types.Row;
+
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.module.SimpleModule;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.parallel.Execution;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
+
+import java.io.IOException;
+import java.time.LocalDateTime;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.junit.jupiter.api.parallel.ExecutionMode.CONCURRENT;
+
+/** Tests for {@link LogicalType} serialization and deserialization. */
+@Execution(CONCURRENT)
+public class LogicalTypeJsonSerDeTest {
+
+ private final ObjectMapper mapper = getObjectMapper();
+
+ // final constants for testing unsupported case
Review Comment:
Remove useless comments... I think variable names are straightforward for us.
##########
flink-table/flink-sql-gateway-api/src/main/java/org/apache/flink/table/gateway/api/results/serde/LogicalTypeJsonDeserializer.java:
##########
@@ -98,6 +100,10 @@ public LogicalType deserialize(JsonParser jsonParser, DeserializationContext ctx
private LogicalType deserializeInternal(JsonNode logicalTypeNode) {
LogicalTypeRoot typeRoot =
LogicalTypeRoot.valueOf(logicalTypeNode.get(FIELD_NAME_TYPE_NAME).asText());
+ // handle the special case of NullType
Review Comment:
Can we describe the root reason here. For example, the NullType is always nullable.
##########
flink-table/flink-sql-gateway-api/src/test/java/org/apache/flink/table/gateway/api/results/serde/LogicalTypeJsonSerDeTest.java:
##########
@@ -0,0 +1,233 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.table.gateway.api.results.serde;
+
+import org.apache.flink.api.common.typeutils.base.LocalDateTimeSerializer;
+import org.apache.flink.core.testutils.FlinkAssertions;
+import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.runtime.typeutils.ExternalSerializer;
+import org.apache.flink.table.types.logical.ArrayType;
+import org.apache.flink.table.types.logical.BigIntType;
+import org.apache.flink.table.types.logical.BinaryType;
+import org.apache.flink.table.types.logical.BooleanType;
+import org.apache.flink.table.types.logical.CharType;
+import org.apache.flink.table.types.logical.DateType;
+import org.apache.flink.table.types.logical.DayTimeIntervalType;
+import org.apache.flink.table.types.logical.DecimalType;
+import org.apache.flink.table.types.logical.DoubleType;
+import org.apache.flink.table.types.logical.FloatType;
+import org.apache.flink.table.types.logical.IntType;
+import org.apache.flink.table.types.logical.LocalZonedTimestampType;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.MapType;
+import org.apache.flink.table.types.logical.MultisetType;
+import org.apache.flink.table.types.logical.NullType;
+import org.apache.flink.table.types.logical.RawType;
+import org.apache.flink.table.types.logical.RowType;
+import org.apache.flink.table.types.logical.SmallIntType;
+import org.apache.flink.table.types.logical.TimeType;
+import org.apache.flink.table.types.logical.TimestampKind;
+import org.apache.flink.table.types.logical.TimestampType;
+import org.apache.flink.table.types.logical.TinyIntType;
+import org.apache.flink.table.types.logical.VarBinaryType;
+import org.apache.flink.table.types.logical.VarCharType;
+import org.apache.flink.table.types.logical.ZonedTimestampType;
+import org.apache.flink.types.Row;
+
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.module.SimpleModule;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.parallel.Execution;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
+
+import java.io.IOException;
+import java.time.LocalDateTime;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.junit.jupiter.api.parallel.ExecutionMode.CONCURRENT;
+
+/** Tests for {@link LogicalType} serialization and deserialization. */
+@Execution(CONCURRENT)
+public class LogicalTypeJsonSerDeTest {
+
+ private final ObjectMapper mapper = getObjectMapper();
+
+ // final constants for testing unsupported case
+ private final LogicalType unsupportedType =
+ new DayTimeIntervalType(DayTimeIntervalType.DayTimeResolution.DAY_TO_HOUR);
+ private final String serializerExceptionMessageFormat =
+ "Unable to serialize logical type '%s'. Please check the documentation for supported types.";
+ private final String unsupportedTypeString = "INTERVAL_DAY_TIME";
+ private final String json =
+ String.format(
+ "{\"%s\": \"%s\", \"%s\": %s}",
+ "type", unsupportedTypeString, "nullable", "true");
+ private final String deserializerExceptionMessageFormat =
+ "Unable to deserialize a logical type of type root '%s'. Please check the documentation for supported types.";
+
+ @ParameterizedTest
+ @MethodSource("generateTestData")
+ public void testLogicalTypeJsonSerDe(LogicalType logicalType) throws IOException {
+ String json = mapper.writeValueAsString(logicalType);
+ LogicalType actualType = mapper.readValue(json, LogicalType.class);
+
+ assertThat(actualType).isEqualTo(logicalType);
+ }
+
+ @Test
+ public void testSerDeWithUnsupportedType() {
+ // test to serialize unsupported LogicalType
+ assertThatThrownBy(() -> mapper.writeValueAsString(unsupportedType))
+ .satisfies(
+ FlinkAssertions.anyCauseMatches(
+ UnsupportedOperationException.class,
+ String.format(
+ serializerExceptionMessageFormat,
+ unsupportedType.asSummaryString())));
+
+ // test to deserialize unsupported JSON string
+ assertThatThrownBy(() -> mapper.readValue(json, LogicalType.class))
+ .satisfies(
+ FlinkAssertions.anyCauseMatches(
+ UnsupportedOperationException.class,
+ String.format(
+ deserializerExceptionMessageFormat,
+ unsupportedTypeString)));
+ }
+
+ // --------------------------------------------------------------------------------------------
+ // Test data
+ // --------------------------------------------------------------------------------------------
+
+ private static List<LogicalType> generateTestData() {
+ List<LogicalType> types =
+ Arrays.asList(
+ new BooleanType(),
+ new TinyIntType(),
+ new SmallIntType(),
+ new IntType(),
+ new BigIntType(),
+ new FloatType(),
+ new DoubleType(),
+ new DateType(),
+ CharType.ofEmptyLiteral(),
+ new CharType(),
+ new CharType(5),
+ VarCharType.ofEmptyLiteral(),
+ new VarCharType(),
+ new VarCharType(5),
+ BinaryType.ofEmptyLiteral(),
+ new BinaryType(),
+ new BinaryType(100),
+ VarBinaryType.ofEmptyLiteral(),
+ new VarBinaryType(),
+ new VarBinaryType(100),
+ new DecimalType(10),
+ new DecimalType(15, 5),
+ new TimeType(),
+ new TimeType(3),
+ new TimestampType(),
+ new TimestampType(3),
+ new TimestampType(false, 3),
+ new ZonedTimestampType(),
+ new ZonedTimestampType(3),
+ new LocalZonedTimestampType(),
+ new LocalZonedTimestampType(3),
+ new LocalZonedTimestampType(false, 3),
+ // LocalZonedTimestampType#eaquals doesn't compare TimestampKind
+ new LocalZonedTimestampType(false, TimestampKind.PROCTIME, 3),
+ new MapType(new BigIntType(), new IntType(false)),
+ new MapType(CharType.ofEmptyLiteral(), CharType.ofEmptyLiteral()),
+ new MapType(VarCharType.ofEmptyLiteral(), VarCharType.ofEmptyLiteral()),
+ new MapType(BinaryType.ofEmptyLiteral(), BinaryType.ofEmptyLiteral()),
+ new MapType(VarBinaryType.ofEmptyLiteral(), VarBinaryType.ofEmptyLiteral()),
+ new MapType(new TimestampType(false, 3), new LocalZonedTimestampType()),
+ new ArrayType(new IntType(false)),
+ new ArrayType(new TimestampType()),
+ new ArrayType(new LocalZonedTimestampType(false, 3)),
+ new ArrayType(CharType.ofEmptyLiteral()),
+ new ArrayType(VarCharType.ofEmptyLiteral()),
+ new ArrayType(BinaryType.ofEmptyLiteral()),
+ new ArrayType(VarBinaryType.ofEmptyLiteral()),
+ new MultisetType(new IntType(false)),
+ new MultisetType(new TimestampType()),
+ new MultisetType(new TimestampType(true, 3)),
+ new MultisetType(CharType.ofEmptyLiteral()),
+ new MultisetType(VarCharType.ofEmptyLiteral()),
+ new MultisetType(BinaryType.ofEmptyLiteral()),
+ new MultisetType(VarBinaryType.ofEmptyLiteral()),
+ RowType.of(new BigIntType(), new IntType(false), new VarCharType(200)),
+ RowType.of(
+ new LogicalType[] {
+ new BigIntType(), new IntType(false), new VarCharType(200)
+ },
+ new String[] {"f1", "f2", "f3"}),
+ RowType.of(
+ new TimestampType(false, 3), new LocalZonedTimestampType(false, 3)),
+ RowType.of(
+ CharType.ofEmptyLiteral(),
+ VarCharType.ofEmptyLiteral(),
+ BinaryType.ofEmptyLiteral(),
+ VarBinaryType.ofEmptyLiteral()),
+ // Row with descriptions
+ new RowType(
+ Arrays.asList(
+ new RowType.RowField("ID", new BigIntType(), "ID desc"),
+ new RowType.RowField(
+ "Name", new VarCharType(20), "Name desc"))),
+ // custom RawType
+ new RawType<>(LocalDateTime.class, LocalDateTimeSerializer.INSTANCE),
+ // external RawType
+ new RawType<>(
+ Row.class,
+ ExternalSerializer.of(
+ DataTypes.ROW(DataTypes.INT(), DataTypes.STRING()))));
+
+ List<LogicalType> testTypes =
+ Stream.concat(
+ types.stream().map(type -> type.copy(true)),
+ types.stream().map(type -> type.copy(false)))
+ .collect(Collectors.toList());
+
+ // ignore nullable for NullType
+ testTypes.add(new NullType());
+
+ return testTypes;
+ }
+
+ /**
+ * LogicalType isn't annotated with Jackson annotations, so it's necessary to register the
+ * customer serializer and deserializer when testing LogicalType Serde alone.
+ */
Review Comment:
Remove this comment. It's the Jackson's API, we don't need to tell others how to use jackson.
##########
flink-table/flink-sql-gateway-api/src/test/java/org/apache/flink/table/gateway/api/results/serde/LogicalTypeJsonSerDeTest.java:
##########
@@ -77,6 +82,36 @@ public void testLogicalTypeJsonSerDe(LogicalType logicalType) throws IOException
assertThat(actualType).isEqualTo(logicalType);
}
+ @Test
+ public void testSerDeWithUnsupportedType() {
+ ObjectMapper mapper = JsonSerdeUtil.getObjectMapper();
+
+ // test to serialize unsupported LogicalType
+ LogicalType unsupportedType =
+ new DayTimeIntervalType(DayTimeIntervalType.DayTimeResolution.DAY_TO_HOUR);
+ assertThatThrownBy(() -> mapper.writeValueAsString(unsupportedType))
+ .satisfies(
+ FlinkAssertions.anyCauseMatches(
+ UnsupportedOperationException.class,
+ String.format(
+ "Unable to serialize logical type '%s'. Please check the documentation for supported types.",
+ unsupportedType.asSummaryString())));
+
+ // test to deserialize unsupported JSON string
+ String unsupportedTypeString = "INTERVAL_DAY_TIME";
+ String json =
+ String.format(
+ "{\"%s\": \"%s\", \"%s\": %s}",
+ "type", unsupportedTypeString, "nullable", "true");
+ assertThatThrownBy(() -> mapper.readValue(json, LogicalType.class))
+ .satisfies(
+ FlinkAssertions.anyCauseMatches(
+ UnsupportedOperationException.class,
+ String.format(
+ "Unable to deserialize a logical type of type root '%s'. Please check the documentation for supported types.",
+ unsupportedTypeString)));
Review Comment:
It's better to split into two test cases.
##########
flink-table/flink-sql-gateway-api/src/test/java/org/apache/flink/table/gateway/api/results/serde/LogicalTypeJsonSerDeTest.java:
##########
@@ -116,6 +151,8 @@ private static List<LogicalType> generateTestData() {
new LocalZonedTimestampType(),
new LocalZonedTimestampType(3),
new LocalZonedTimestampType(false, 3),
+ // LocalZonedTimestampType#eaquals doesn't compare TimestampKind
Review Comment:
Remove this ...
##########
flink-table/flink-sql-gateway-api/src/test/java/org/apache/flink/table/gateway/api/results/serde/LogicalTypeJsonSerDeTest.java:
##########
@@ -0,0 +1,233 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.table.gateway.api.results.serde;
+
+import org.apache.flink.api.common.typeutils.base.LocalDateTimeSerializer;
+import org.apache.flink.core.testutils.FlinkAssertions;
+import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.runtime.typeutils.ExternalSerializer;
+import org.apache.flink.table.types.logical.ArrayType;
+import org.apache.flink.table.types.logical.BigIntType;
+import org.apache.flink.table.types.logical.BinaryType;
+import org.apache.flink.table.types.logical.BooleanType;
+import org.apache.flink.table.types.logical.CharType;
+import org.apache.flink.table.types.logical.DateType;
+import org.apache.flink.table.types.logical.DayTimeIntervalType;
+import org.apache.flink.table.types.logical.DecimalType;
+import org.apache.flink.table.types.logical.DoubleType;
+import org.apache.flink.table.types.logical.FloatType;
+import org.apache.flink.table.types.logical.IntType;
+import org.apache.flink.table.types.logical.LocalZonedTimestampType;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.MapType;
+import org.apache.flink.table.types.logical.MultisetType;
+import org.apache.flink.table.types.logical.NullType;
+import org.apache.flink.table.types.logical.RawType;
+import org.apache.flink.table.types.logical.RowType;
+import org.apache.flink.table.types.logical.SmallIntType;
+import org.apache.flink.table.types.logical.TimeType;
+import org.apache.flink.table.types.logical.TimestampKind;
+import org.apache.flink.table.types.logical.TimestampType;
+import org.apache.flink.table.types.logical.TinyIntType;
+import org.apache.flink.table.types.logical.VarBinaryType;
+import org.apache.flink.table.types.logical.VarCharType;
+import org.apache.flink.table.types.logical.ZonedTimestampType;
+import org.apache.flink.types.Row;
+
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.module.SimpleModule;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.parallel.Execution;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
+
+import java.io.IOException;
+import java.time.LocalDateTime;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.junit.jupiter.api.parallel.ExecutionMode.CONCURRENT;
+
+/** Tests for {@link LogicalType} serialization and deserialization. */
+@Execution(CONCURRENT)
+public class LogicalTypeJsonSerDeTest {
+
+ private final ObjectMapper mapper = getObjectMapper();
+
+ // final constants for testing unsupported case
+ private final LogicalType unsupportedType =
+ new DayTimeIntervalType(DayTimeIntervalType.DayTimeResolution.DAY_TO_HOUR);
+ private final String serializerExceptionMessageFormat =
+ "Unable to serialize logical type '%s'. Please check the documentation for supported types.";
+ private final String unsupportedTypeString = "INTERVAL_DAY_TIME";
+ private final String json =
+ String.format(
+ "{\"%s\": \"%s\", \"%s\": %s}",
+ "type", unsupportedTypeString, "nullable", "true");
+ private final String deserializerExceptionMessageFormat =
+ "Unable to deserialize a logical type of type root '%s'. Please check the documentation for supported types.";
+
+ @ParameterizedTest
+ @MethodSource("generateTestData")
+ public void testLogicalTypeJsonSerDe(LogicalType logicalType) throws IOException {
+ String json = mapper.writeValueAsString(logicalType);
+ LogicalType actualType = mapper.readValue(json, LogicalType.class);
+
+ assertThat(actualType).isEqualTo(logicalType);
+ }
+
+ @Test
+ public void testSerDeWithUnsupportedType() {
+ // test to serialize unsupported LogicalType
+ assertThatThrownBy(() -> mapper.writeValueAsString(unsupportedType))
+ .satisfies(
+ FlinkAssertions.anyCauseMatches(
+ UnsupportedOperationException.class,
+ String.format(
+ serializerExceptionMessageFormat,
+ unsupportedType.asSummaryString())));
+
+ // test to deserialize unsupported JSON string
+ assertThatThrownBy(() -> mapper.readValue(json, LogicalType.class))
+ .satisfies(
+ FlinkAssertions.anyCauseMatches(
+ UnsupportedOperationException.class,
+ String.format(
+ deserializerExceptionMessageFormat,
+ unsupportedTypeString)));
+ }
+
+ // --------------------------------------------------------------------------------------------
+ // Test data
+ // --------------------------------------------------------------------------------------------
+
+ private static List<LogicalType> generateTestData() {
+ List<LogicalType> types =
+ Arrays.asList(
+ new BooleanType(),
+ new TinyIntType(),
+ new SmallIntType(),
+ new IntType(),
+ new BigIntType(),
+ new FloatType(),
+ new DoubleType(),
+ new DateType(),
+ CharType.ofEmptyLiteral(),
+ new CharType(),
+ new CharType(5),
+ VarCharType.ofEmptyLiteral(),
+ new VarCharType(),
+ new VarCharType(5),
+ BinaryType.ofEmptyLiteral(),
+ new BinaryType(),
+ new BinaryType(100),
+ VarBinaryType.ofEmptyLiteral(),
+ new VarBinaryType(),
+ new VarBinaryType(100),
+ new DecimalType(10),
+ new DecimalType(15, 5),
+ new TimeType(),
+ new TimeType(3),
+ new TimestampType(),
+ new TimestampType(3),
+ new TimestampType(false, 3),
+ new ZonedTimestampType(),
+ new ZonedTimestampType(3),
+ new LocalZonedTimestampType(),
+ new LocalZonedTimestampType(3),
+ new LocalZonedTimestampType(false, 3),
+ // LocalZonedTimestampType#eaquals doesn't compare TimestampKind
+ new LocalZonedTimestampType(false, TimestampKind.PROCTIME, 3),
+ new MapType(new BigIntType(), new IntType(false)),
+ new MapType(CharType.ofEmptyLiteral(), CharType.ofEmptyLiteral()),
+ new MapType(VarCharType.ofEmptyLiteral(), VarCharType.ofEmptyLiteral()),
+ new MapType(BinaryType.ofEmptyLiteral(), BinaryType.ofEmptyLiteral()),
+ new MapType(VarBinaryType.ofEmptyLiteral(), VarBinaryType.ofEmptyLiteral()),
+ new MapType(new TimestampType(false, 3), new LocalZonedTimestampType()),
+ new ArrayType(new IntType(false)),
+ new ArrayType(new TimestampType()),
+ new ArrayType(new LocalZonedTimestampType(false, 3)),
+ new ArrayType(CharType.ofEmptyLiteral()),
+ new ArrayType(VarCharType.ofEmptyLiteral()),
+ new ArrayType(BinaryType.ofEmptyLiteral()),
+ new ArrayType(VarBinaryType.ofEmptyLiteral()),
+ new MultisetType(new IntType(false)),
+ new MultisetType(new TimestampType()),
+ new MultisetType(new TimestampType(true, 3)),
+ new MultisetType(CharType.ofEmptyLiteral()),
+ new MultisetType(VarCharType.ofEmptyLiteral()),
+ new MultisetType(BinaryType.ofEmptyLiteral()),
+ new MultisetType(VarBinaryType.ofEmptyLiteral()),
+ RowType.of(new BigIntType(), new IntType(false), new VarCharType(200)),
+ RowType.of(
+ new LogicalType[] {
+ new BigIntType(), new IntType(false), new VarCharType(200)
+ },
+ new String[] {"f1", "f2", "f3"}),
+ RowType.of(
+ new TimestampType(false, 3), new LocalZonedTimestampType(false, 3)),
+ RowType.of(
+ CharType.ofEmptyLiteral(),
+ VarCharType.ofEmptyLiteral(),
+ BinaryType.ofEmptyLiteral(),
+ VarBinaryType.ofEmptyLiteral()),
+ // Row with descriptions
+ new RowType(
+ Arrays.asList(
+ new RowType.RowField("ID", new BigIntType(), "ID desc"),
+ new RowType.RowField(
+ "Name", new VarCharType(20), "Name desc"))),
+ // custom RawType
+ new RawType<>(LocalDateTime.class, LocalDateTimeSerializer.INSTANCE),
+ // external RawType
+ new RawType<>(
+ Row.class,
+ ExternalSerializer.of(
+ DataTypes.ROW(DataTypes.INT(), DataTypes.STRING()))));
+
+ List<LogicalType> testTypes =
+ Stream.concat(
+ types.stream().map(type -> type.copy(true)),
+ types.stream().map(type -> type.copy(false)))
+ .collect(Collectors.toList());
+
+ // ignore nullable for NullType
+ testTypes.add(new NullType());
+
+ return testTypes;
+ }
+
+ /**
+ * LogicalType isn't annotated with Jackson annotations, so it's necessary to register the
+ * customer serializer and deserializer when testing LogicalType Serde alone.
+ */
+ private ObjectMapper getObjectMapper() {
Review Comment:
rename to buildObjectMapper
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink] yuzelin commented on a diff in pull request #20617: [FLINK-28936][sql-gateway] Fix REST endpoint can not serialize CHAR(0) by introducing a new serializer for LogicalType
Posted by GitBox <gi...@apache.org>.
yuzelin commented on code in PR #20617:
URL: https://github.com/apache/flink/pull/20617#discussion_r951355979
##########
flink-table/flink-sql-gateway-api/src/main/java/org/apache/flink/table/gateway/api/results/serde/LogicalTypeJsonSerializer.java:
##########
@@ -0,0 +1,210 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.table.gateway.api.results.serde;
+
+import org.apache.flink.annotation.PublicEvolving;
+import org.apache.flink.table.api.ValidationException;
+import org.apache.flink.table.types.logical.ArrayType;
+import org.apache.flink.table.types.logical.BinaryType;
+import org.apache.flink.table.types.logical.CharType;
+import org.apache.flink.table.types.logical.DecimalType;
+import org.apache.flink.table.types.logical.LocalZonedTimestampType;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.MapType;
+import org.apache.flink.table.types.logical.MultisetType;
+import org.apache.flink.table.types.logical.RawType;
+import org.apache.flink.table.types.logical.RowType;
+import org.apache.flink.table.types.logical.TimestampType;
+import org.apache.flink.table.types.logical.VarBinaryType;
+import org.apache.flink.table.types.logical.VarCharType;
+
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonGenerator;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.SerializerProvider;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ser.std.StdSerializer;
+
+import java.io.IOException;
+
+/**
+ * Json serializer for {@link LogicalType}.
+ *
+ * @see LogicalTypeJsonDeserializer for the reverse operation.
+ */
+@PublicEvolving
+public final class LogicalTypeJsonSerializer extends StdSerializer<LogicalType> {
+
+ private static final long serialVersionUID = 1L;
+
+ // --------------------------------------------------------------------------------------------
+ // Public string constants for serializer and deserializer
+ // --------------------------------------------------------------------------------------------
+
+ // Common fields
+ public static final String FIELD_NAME_TYPE_NAME = "type";
+ public static final String FIELD_NAME_NULLABLE = "nullable";
+
+ // CHAR, VARCHAR, BINARY, VARBINARY
+ public static final String FIELD_NAME_LENGTH = "length";
+
+ // DECIMAL, TIMESTAMP_WITHOUT_TIME_ZONE, TIMESTAMP_WITH_LOCAL_TIME_ZONE
+ public static final String FIELD_NAME_PRECISION = "precision";
+
+ // DECIMAL
+ public static final String FIELD_NAME_SCALE = "scale";
+
+ // MAP
+ public static final String FIELD_NAME_KEY_TYPE = "keyType";
+ public static final String FIELD_NAME_VALUE_TYPE = "valueType";
+
+ // ARRAY, MULTISET
+ public static final String FIELD_NAME_ELEMENT_TYPE = "elementType";
+
+ // ROW
+ public static final String FIELD_NAME_FIELDS = "fields";
+ public static final String FIELD_NAME_FIELD_NAME = "name";
+ public static final String FIELD_NAME_FIELD_TYPE = "fieldType";
+
+ // RAW
+ public static final String FIELD_NAME_CLASS = "class";
+ public static final String FIELD_NAME_SERIALIZER = "serializer";
+
+ public LogicalTypeJsonSerializer() {
+ super(LogicalType.class);
+ }
+
+ @Override
+ public void serialize(
+ LogicalType logicalType,
+ JsonGenerator jsonGenerator,
+ SerializerProvider serializerProvider)
+ throws IOException {
+ serializeInternal(logicalType, jsonGenerator);
+ }
+
+ private void serializeInternal(LogicalType logicalType, JsonGenerator jsonGenerator)
+ throws IOException {
+ jsonGenerator.writeStartObject();
+
+ // write common fields shared by all types
+ jsonGenerator.writeStringField(FIELD_NAME_TYPE_NAME, logicalType.getTypeRoot().name());
+ jsonGenerator.writeBooleanField(FIELD_NAME_NULLABLE, logicalType.isNullable());
+ // write special fields according to type root
+ switch (logicalType.getTypeRoot()) {
+ case BOOLEAN:
+ case TINYINT:
+ case SMALLINT:
+ case INTEGER:
+ case BIGINT:
+ case FLOAT:
+ case DOUBLE:
+ case DATE:
+ case NULL:
Review Comment:
Added codes to handle NullType.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink] yuzelin commented on a diff in pull request #20617: [FLINK-28936][sql-gateway] Fix REST endpoint can not serialize CHAR(0) by introducing a new serializer for LogicalType
Posted by GitBox <gi...@apache.org>.
yuzelin commented on code in PR #20617:
URL: https://github.com/apache/flink/pull/20617#discussion_r951326179
##########
flink-table/flink-sql-gateway-api/src/main/java/org/apache/flink/table/gateway/api/results/serde/LogicalTypeJsonSerializer.java:
##########
@@ -0,0 +1,210 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.table.gateway.api.results.serde;
+
+import org.apache.flink.annotation.PublicEvolving;
+import org.apache.flink.table.api.ValidationException;
+import org.apache.flink.table.types.logical.ArrayType;
+import org.apache.flink.table.types.logical.BinaryType;
+import org.apache.flink.table.types.logical.CharType;
+import org.apache.flink.table.types.logical.DecimalType;
+import org.apache.flink.table.types.logical.LocalZonedTimestampType;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.MapType;
+import org.apache.flink.table.types.logical.MultisetType;
+import org.apache.flink.table.types.logical.RawType;
+import org.apache.flink.table.types.logical.RowType;
+import org.apache.flink.table.types.logical.TimestampType;
+import org.apache.flink.table.types.logical.VarBinaryType;
+import org.apache.flink.table.types.logical.VarCharType;
+
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonGenerator;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.SerializerProvider;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ser.std.StdSerializer;
+
+import java.io.IOException;
+
+/**
+ * Json serializer for {@link LogicalType}.
+ *
+ * @see LogicalTypeJsonDeserializer for the reverse operation.
+ */
+@PublicEvolving
+public final class LogicalTypeJsonSerializer extends StdSerializer<LogicalType> {
+
+ private static final long serialVersionUID = 1L;
+
+ // --------------------------------------------------------------------------------------------
+ // Public string constants for serializer and deserializer
+ // --------------------------------------------------------------------------------------------
+
+ // Common fields
+ public static final String FIELD_NAME_TYPE_NAME = "type";
+ public static final String FIELD_NAME_NULLABLE = "nullable";
+
+ // CHAR, VARCHAR, BINARY, VARBINARY
+ public static final String FIELD_NAME_LENGTH = "length";
+
+ // DECIMAL, TIMESTAMP_WITHOUT_TIME_ZONE, TIMESTAMP_WITH_LOCAL_TIME_ZONE
+ public static final String FIELD_NAME_PRECISION = "precision";
+
+ // DECIMAL
+ public static final String FIELD_NAME_SCALE = "scale";
+
+ // MAP
+ public static final String FIELD_NAME_KEY_TYPE = "keyType";
+ public static final String FIELD_NAME_VALUE_TYPE = "valueType";
+
+ // ARRAY, MULTISET
+ public static final String FIELD_NAME_ELEMENT_TYPE = "elementType";
+
+ // ROW
+ public static final String FIELD_NAME_FIELDS = "fields";
+ public static final String FIELD_NAME_FIELD_NAME = "name";
+ public static final String FIELD_NAME_FIELD_TYPE = "fieldType";
Review Comment:
Added code for serialization and deserialization for `RowType#description`.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink] yuzelin commented on a diff in pull request #20617: [FLINK-28936][sql-gateway] Fix REST endpoint can not serialize CHAR(0) by introducing a new serializer for LogicalType
Posted by GitBox <gi...@apache.org>.
yuzelin commented on code in PR #20617:
URL: https://github.com/apache/flink/pull/20617#discussion_r953574075
##########
flink-table/flink-sql-gateway-api/src/test/java/org/apache/flink/table/gateway/api/results/serde/LogicalTypeJsonSerDeTest.java:
##########
@@ -77,6 +82,36 @@ public void testLogicalTypeJsonSerDe(LogicalType logicalType) throws IOException
assertThat(actualType).isEqualTo(logicalType);
}
+ @Test
+ public void testSerDeWithUnsupportedType() {
+ ObjectMapper mapper = JsonSerdeUtil.getObjectMapper();
+
+ // test to serialize unsupported LogicalType
+ LogicalType unsupportedType =
+ new DayTimeIntervalType(DayTimeIntervalType.DayTimeResolution.DAY_TO_HOUR);
+ assertThatThrownBy(() -> mapper.writeValueAsString(unsupportedType))
+ .satisfies(
+ FlinkAssertions.anyCauseMatches(
+ UnsupportedOperationException.class,
+ String.format(
+ "Unable to serialize logical type '%s'. Please check the documentation for supported types.",
+ unsupportedType.asSummaryString())));
+
+ // test to deserialize unsupported JSON string
+ String unsupportedTypeString = "INTERVAL_DAY_TIME";
+ String json =
+ String.format(
+ "{\"%s\": \"%s\", \"%s\": %s}",
+ "type", unsupportedTypeString, "nullable", "true");
+ assertThatThrownBy(() -> mapper.readValue(json, LogicalType.class))
+ .satisfies(
+ FlinkAssertions.anyCauseMatches(
+ UnsupportedOperationException.class,
+ String.format(
+ "Unable to deserialize a logical type of type root '%s'. Please check the documentation for supported types.",
+ unsupportedTypeString)));
Review Comment:
Improved.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink] yuzelin commented on a diff in pull request #20617: [FLINK-28936][sql-gateway] Fix REST endpoint can not serialize CHAR(0) by introducing a new serializer for LogicalType
Posted by GitBox <gi...@apache.org>.
yuzelin commented on code in PR #20617:
URL: https://github.com/apache/flink/pull/20617#discussion_r953565664
##########
flink-table/flink-sql-gateway-api/src/test/java/org/apache/flink/table/gateway/api/results/serde/LogicalTypeJsonSerDeTest.java:
##########
@@ -0,0 +1,233 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.table.gateway.api.results.serde;
+
+import org.apache.flink.api.common.typeutils.base.LocalDateTimeSerializer;
+import org.apache.flink.core.testutils.FlinkAssertions;
+import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.runtime.typeutils.ExternalSerializer;
+import org.apache.flink.table.types.logical.ArrayType;
+import org.apache.flink.table.types.logical.BigIntType;
+import org.apache.flink.table.types.logical.BinaryType;
+import org.apache.flink.table.types.logical.BooleanType;
+import org.apache.flink.table.types.logical.CharType;
+import org.apache.flink.table.types.logical.DateType;
+import org.apache.flink.table.types.logical.DayTimeIntervalType;
+import org.apache.flink.table.types.logical.DecimalType;
+import org.apache.flink.table.types.logical.DoubleType;
+import org.apache.flink.table.types.logical.FloatType;
+import org.apache.flink.table.types.logical.IntType;
+import org.apache.flink.table.types.logical.LocalZonedTimestampType;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.MapType;
+import org.apache.flink.table.types.logical.MultisetType;
+import org.apache.flink.table.types.logical.NullType;
+import org.apache.flink.table.types.logical.RawType;
+import org.apache.flink.table.types.logical.RowType;
+import org.apache.flink.table.types.logical.SmallIntType;
+import org.apache.flink.table.types.logical.TimeType;
+import org.apache.flink.table.types.logical.TimestampKind;
+import org.apache.flink.table.types.logical.TimestampType;
+import org.apache.flink.table.types.logical.TinyIntType;
+import org.apache.flink.table.types.logical.VarBinaryType;
+import org.apache.flink.table.types.logical.VarCharType;
+import org.apache.flink.table.types.logical.ZonedTimestampType;
+import org.apache.flink.types.Row;
+
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.module.SimpleModule;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.parallel.Execution;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
+
+import java.io.IOException;
+import java.time.LocalDateTime;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.junit.jupiter.api.parallel.ExecutionMode.CONCURRENT;
+
+/** Tests for {@link LogicalType} serialization and deserialization. */
+@Execution(CONCURRENT)
+public class LogicalTypeJsonSerDeTest {
+
+ private final ObjectMapper mapper = getObjectMapper();
+
+ // final constants for testing unsupported case
+ private final LogicalType unsupportedType =
+ new DayTimeIntervalType(DayTimeIntervalType.DayTimeResolution.DAY_TO_HOUR);
+ private final String serializerExceptionMessageFormat =
+ "Unable to serialize logical type '%s'. Please check the documentation for supported types.";
+ private final String unsupportedTypeString = "INTERVAL_DAY_TIME";
+ private final String json =
+ String.format(
+ "{\"%s\": \"%s\", \"%s\": %s}",
+ "type", unsupportedTypeString, "nullable", "true");
+ private final String deserializerExceptionMessageFormat =
+ "Unable to deserialize a logical type of type root '%s'. Please check the documentation for supported types.";
+
+ @ParameterizedTest
+ @MethodSource("generateTestData")
+ public void testLogicalTypeJsonSerDe(LogicalType logicalType) throws IOException {
+ String json = mapper.writeValueAsString(logicalType);
+ LogicalType actualType = mapper.readValue(json, LogicalType.class);
+
+ assertThat(actualType).isEqualTo(logicalType);
+ }
+
+ @Test
+ public void testSerDeWithUnsupportedType() {
+ // test to serialize unsupported LogicalType
+ assertThatThrownBy(() -> mapper.writeValueAsString(unsupportedType))
+ .satisfies(
+ FlinkAssertions.anyCauseMatches(
+ UnsupportedOperationException.class,
+ String.format(
+ serializerExceptionMessageFormat,
+ unsupportedType.asSummaryString())));
+
+ // test to deserialize unsupported JSON string
+ assertThatThrownBy(() -> mapper.readValue(json, LogicalType.class))
+ .satisfies(
+ FlinkAssertions.anyCauseMatches(
+ UnsupportedOperationException.class,
+ String.format(
+ deserializerExceptionMessageFormat,
+ unsupportedTypeString)));
+ }
+
+ // --------------------------------------------------------------------------------------------
+ // Test data
+ // --------------------------------------------------------------------------------------------
+
+ private static List<LogicalType> generateTestData() {
+ List<LogicalType> types =
+ Arrays.asList(
+ new BooleanType(),
+ new TinyIntType(),
+ new SmallIntType(),
+ new IntType(),
+ new BigIntType(),
+ new FloatType(),
+ new DoubleType(),
+ new DateType(),
+ CharType.ofEmptyLiteral(),
+ new CharType(),
+ new CharType(5),
+ VarCharType.ofEmptyLiteral(),
+ new VarCharType(),
+ new VarCharType(5),
+ BinaryType.ofEmptyLiteral(),
+ new BinaryType(),
+ new BinaryType(100),
+ VarBinaryType.ofEmptyLiteral(),
+ new VarBinaryType(),
+ new VarBinaryType(100),
+ new DecimalType(10),
+ new DecimalType(15, 5),
+ new TimeType(),
+ new TimeType(3),
+ new TimestampType(),
+ new TimestampType(3),
+ new TimestampType(false, 3),
+ new ZonedTimestampType(),
+ new ZonedTimestampType(3),
+ new LocalZonedTimestampType(),
+ new LocalZonedTimestampType(3),
+ new LocalZonedTimestampType(false, 3),
+ // LocalZonedTimestampType#eaquals doesn't compare TimestampKind
+ new LocalZonedTimestampType(false, TimestampKind.PROCTIME, 3),
+ new MapType(new BigIntType(), new IntType(false)),
+ new MapType(CharType.ofEmptyLiteral(), CharType.ofEmptyLiteral()),
+ new MapType(VarCharType.ofEmptyLiteral(), VarCharType.ofEmptyLiteral()),
+ new MapType(BinaryType.ofEmptyLiteral(), BinaryType.ofEmptyLiteral()),
+ new MapType(VarBinaryType.ofEmptyLiteral(), VarBinaryType.ofEmptyLiteral()),
+ new MapType(new TimestampType(false, 3), new LocalZonedTimestampType()),
+ new ArrayType(new IntType(false)),
+ new ArrayType(new TimestampType()),
+ new ArrayType(new LocalZonedTimestampType(false, 3)),
+ new ArrayType(CharType.ofEmptyLiteral()),
+ new ArrayType(VarCharType.ofEmptyLiteral()),
+ new ArrayType(BinaryType.ofEmptyLiteral()),
+ new ArrayType(VarBinaryType.ofEmptyLiteral()),
+ new MultisetType(new IntType(false)),
+ new MultisetType(new TimestampType()),
+ new MultisetType(new TimestampType(true, 3)),
+ new MultisetType(CharType.ofEmptyLiteral()),
+ new MultisetType(VarCharType.ofEmptyLiteral()),
+ new MultisetType(BinaryType.ofEmptyLiteral()),
+ new MultisetType(VarBinaryType.ofEmptyLiteral()),
+ RowType.of(new BigIntType(), new IntType(false), new VarCharType(200)),
+ RowType.of(
+ new LogicalType[] {
+ new BigIntType(), new IntType(false), new VarCharType(200)
+ },
+ new String[] {"f1", "f2", "f3"}),
+ RowType.of(
+ new TimestampType(false, 3), new LocalZonedTimestampType(false, 3)),
+ RowType.of(
+ CharType.ofEmptyLiteral(),
+ VarCharType.ofEmptyLiteral(),
+ BinaryType.ofEmptyLiteral(),
+ VarBinaryType.ofEmptyLiteral()),
+ // Row with descriptions
+ new RowType(
+ Arrays.asList(
+ new RowType.RowField("ID", new BigIntType(), "ID desc"),
+ new RowType.RowField(
+ "Name", new VarCharType(20), "Name desc"))),
+ // custom RawType
+ new RawType<>(LocalDateTime.class, LocalDateTimeSerializer.INSTANCE),
+ // external RawType
+ new RawType<>(
+ Row.class,
+ ExternalSerializer.of(
+ DataTypes.ROW(DataTypes.INT(), DataTypes.STRING()))));
+
+ List<LogicalType> testTypes =
+ Stream.concat(
+ types.stream().map(type -> type.copy(true)),
+ types.stream().map(type -> type.copy(false)))
+ .collect(Collectors.toList());
+
+ // ignore nullable for NullType
+ testTypes.add(new NullType());
+
+ return testTypes;
+ }
+
+ /**
+ * LogicalType isn't annotated with Jackson annotations, so it's necessary to register the
+ * customer serializer and deserializer when testing LogicalType Serde alone.
+ */
+ private ObjectMapper getObjectMapper() {
Review Comment:
Renamed.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink] yuzelin commented on a diff in pull request #20617: [FLINK-28936][sql-gateway] Fix REST endpoint can not serialize CHAR(0) by introducing a new serializer for LogicalType
Posted by GitBox <gi...@apache.org>.
yuzelin commented on code in PR #20617:
URL: https://github.com/apache/flink/pull/20617#discussion_r953552318
##########
flink-table/flink-sql-gateway/src/test/java/org/apache/flink/table/gateway/rest/util/SqlGatewayRestEndpointExtension.java:
##########
@@ -0,0 +1,90 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.table.gateway.rest.util;
+
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.table.gateway.api.SqlGatewayService;
+import org.apache.flink.table.gateway.api.utils.SqlGatewayException;
+import org.apache.flink.table.gateway.rest.SqlGatewayRestEndpoint;
+
+import org.junit.jupiter.api.extension.AfterAllCallback;
+import org.junit.jupiter.api.extension.BeforeAllCallback;
+import org.junit.jupiter.api.extension.Extension;
+import org.junit.jupiter.api.extension.ExtensionContext;
+
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.util.function.Supplier;
+
+import static org.apache.flink.table.gateway.rest.util.RestConfigUtils.getBaseConfig;
+import static org.apache.flink.table.gateway.rest.util.RestConfigUtils.getFlinkConfig;
+import static org.apache.flink.util.Preconditions.checkNotNull;
+
+/** A simple {@link Extension} to be used by tests that require a {@link SqlGatewayRestEndpoint}. */
+public class SqlGatewayRestEndpointExtension implements BeforeAllCallback, AfterAllCallback {
+
+ // to supply service needed when starting the rest end point
+ private final Supplier<SqlGatewayService> serviceSupplier;
+
+ private SqlGatewayRestEndpoint sqlGatewayRestEndpoint;
+ private String targetAddress;
+ private int targetPort;
+
+ public String getTargetAddress() {
+ return targetAddress;
+ }
+
+ public int getTargetPort() {
+ return targetPort;
+ }
+
+ public SqlGatewayRestEndpointExtension(Supplier<SqlGatewayService> serviceSupplier) {
+ this.serviceSupplier = serviceSupplier;
+ }
+
+ @Override
+ public void beforeAll(ExtensionContext context) {
+ String address = InetAddress.getLoopbackAddress().getHostAddress();
+ Configuration config = getBaseConfig(getFlinkConfig(address, address, "0"));
+
+ try {
+ sqlGatewayRestEndpoint = new SqlGatewayRestEndpoint(config, serviceSupplier.get());
+ sqlGatewayRestEndpoint.start();
+ } catch (Exception e) {
+ throw new SqlGatewayException(
+ "Unexpected error occurred when trying to start the rest endpoint of sql gateway.",
+ e);
+ }
+
+ InetSocketAddress serverAddress = checkNotNull(sqlGatewayRestEndpoint.getServerAddress());
+ targetAddress = serverAddress.getHostName();
+ targetPort = serverAddress.getPort();
Review Comment:
After check the implementation, I found the port is 0 in default configuration used by sqlGatewayRestEndpoint.start(), and that means server would bind a random port.
##########
flink-table/flink-sql-gateway/src/test/java/org/apache/flink/table/gateway/rest/util/SqlGatewayRestEndpointExtension.java:
##########
@@ -0,0 +1,90 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.table.gateway.rest.util;
+
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.table.gateway.api.SqlGatewayService;
+import org.apache.flink.table.gateway.api.utils.SqlGatewayException;
+import org.apache.flink.table.gateway.rest.SqlGatewayRestEndpoint;
+
+import org.junit.jupiter.api.extension.AfterAllCallback;
+import org.junit.jupiter.api.extension.BeforeAllCallback;
+import org.junit.jupiter.api.extension.Extension;
+import org.junit.jupiter.api.extension.ExtensionContext;
+
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.util.function.Supplier;
+
+import static org.apache.flink.table.gateway.rest.util.RestConfigUtils.getBaseConfig;
+import static org.apache.flink.table.gateway.rest.util.RestConfigUtils.getFlinkConfig;
+import static org.apache.flink.util.Preconditions.checkNotNull;
+
+/** A simple {@link Extension} to be used by tests that require a {@link SqlGatewayRestEndpoint}. */
+public class SqlGatewayRestEndpointExtension implements BeforeAllCallback, AfterAllCallback {
+
+ // to supply service needed when starting the rest end point
+ private final Supplier<SqlGatewayService> serviceSupplier;
+
+ private SqlGatewayRestEndpoint sqlGatewayRestEndpoint;
+ private String targetAddress;
+ private int targetPort;
+
+ public String getTargetAddress() {
+ return targetAddress;
+ }
+
+ public int getTargetPort() {
+ return targetPort;
+ }
+
+ public SqlGatewayRestEndpointExtension(Supplier<SqlGatewayService> serviceSupplier) {
+ this.serviceSupplier = serviceSupplier;
+ }
+
+ @Override
+ public void beforeAll(ExtensionContext context) {
+ String address = InetAddress.getLoopbackAddress().getHostAddress();
+ Configuration config = getBaseConfig(getFlinkConfig(address, address, "0"));
+
+ try {
+ sqlGatewayRestEndpoint = new SqlGatewayRestEndpoint(config, serviceSupplier.get());
+ sqlGatewayRestEndpoint.start();
+ } catch (Exception e) {
+ throw new SqlGatewayException(
+ "Unexpected error occurred when trying to start the rest endpoint of sql gateway.",
+ e);
+ }
+
+ InetSocketAddress serverAddress = checkNotNull(sqlGatewayRestEndpoint.getServerAddress());
+ targetAddress = serverAddress.getHostName();
+ targetPort = serverAddress.getPort();
Review Comment:
After checking the implementation, I found the port is 0 in default configuration used by sqlGatewayRestEndpoint.start(), and that means server would bind a random port.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink] yuzelin commented on a diff in pull request #20617: [FLINK-28936][sql-gateway] Fix REST endpoint can not serialize CHAR(0) by introducing a new serializer for LogicalType
Posted by GitBox <gi...@apache.org>.
yuzelin commented on code in PR #20617:
URL: https://github.com/apache/flink/pull/20617#discussion_r951201843
##########
flink-table/flink-sql-gateway-api/src/main/java/org/apache/flink/table/gateway/api/results/serde/LogicalTypeJsonDeserializer.java:
##########
@@ -0,0 +1,219 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.table.gateway.api.results.serde;
+
+import org.apache.flink.annotation.PublicEvolving;
+import org.apache.flink.table.api.TableException;
+import org.apache.flink.table.types.logical.ArrayType;
+import org.apache.flink.table.types.logical.BigIntType;
+import org.apache.flink.table.types.logical.BinaryType;
+import org.apache.flink.table.types.logical.BooleanType;
+import org.apache.flink.table.types.logical.CharType;
+import org.apache.flink.table.types.logical.DateType;
+import org.apache.flink.table.types.logical.DecimalType;
+import org.apache.flink.table.types.logical.DoubleType;
+import org.apache.flink.table.types.logical.FloatType;
+import org.apache.flink.table.types.logical.IntType;
+import org.apache.flink.table.types.logical.LocalZonedTimestampType;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.LogicalTypeRoot;
+import org.apache.flink.table.types.logical.MapType;
+import org.apache.flink.table.types.logical.MultisetType;
+import org.apache.flink.table.types.logical.NullType;
+import org.apache.flink.table.types.logical.RawType;
+import org.apache.flink.table.types.logical.RowType;
+import org.apache.flink.table.types.logical.RowType.RowField;
+import org.apache.flink.table.types.logical.SmallIntType;
+import org.apache.flink.table.types.logical.TimestampType;
+import org.apache.flink.table.types.logical.TinyIntType;
+import org.apache.flink.table.types.logical.VarBinaryType;
+import org.apache.flink.table.types.logical.VarCharType;
+
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParser;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.DeserializationContext;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.deser.std.StdDeserializer;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ArrayNode;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_CLASS;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_ELEMENT_TYPE;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_FIELDS;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_FIELD_NAME;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_FIELD_TYPE;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_KEY_TYPE;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_LENGTH;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_NULLABLE;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_PRECISION;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_SCALE;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_SERIALIZER;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_TYPE_NAME;
+import static org.apache.flink.table.gateway.api.results.serde.LogicalTypeJsonSerializer.FIELD_NAME_VALUE_TYPE;
+
+/**
+ * JSON deserializer for {@link LogicalType}.
+ *
+ * @see LogicalTypeJsonSerializer for the reverse operation.
+ */
+@PublicEvolving
Review Comment:
Changed.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink] flinkbot commented on pull request #20617: [FLINK-28936][sql-gateway] Fix REST endpoint can not serialize CHAR(0) by introducing a new serializer for LogicalType
Posted by GitBox <gi...@apache.org>.
flinkbot commented on PR #20617:
URL: https://github.com/apache/flink/pull/20617#issuecomment-1219184553
<!--
Meta data
{
"version" : 1,
"metaDataEntries" : [ {
"hash" : "4b858b3a3a9b7c6870c9c44bded0803f5a1a2091",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "4b858b3a3a9b7c6870c9c44bded0803f5a1a2091",
"triggerType" : "PUSH"
} ]
}-->
## CI report:
* 4b858b3a3a9b7c6870c9c44bded0803f5a1a2091 UNKNOWN
<details>
<summary>Bot commands</summary>
The @flinkbot bot supports the following commands:
- `@flinkbot run azure` re-run the last Azure build
</details>
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink] yuzelin commented on a diff in pull request #20617: [FLINK-28936][sql-gateway] Fix REST endpoint can not serialize CHAR(0) by introducing a new serializer for LogicalType
Posted by GitBox <gi...@apache.org>.
yuzelin commented on code in PR #20617:
URL: https://github.com/apache/flink/pull/20617#discussion_r953564879
##########
flink-table/flink-sql-gateway-api/src/test/java/org/apache/flink/table/gateway/api/results/serde/LogicalTypeJsonSerDeTest.java:
##########
@@ -0,0 +1,233 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.table.gateway.api.results.serde;
+
+import org.apache.flink.api.common.typeutils.base.LocalDateTimeSerializer;
+import org.apache.flink.core.testutils.FlinkAssertions;
+import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.runtime.typeutils.ExternalSerializer;
+import org.apache.flink.table.types.logical.ArrayType;
+import org.apache.flink.table.types.logical.BigIntType;
+import org.apache.flink.table.types.logical.BinaryType;
+import org.apache.flink.table.types.logical.BooleanType;
+import org.apache.flink.table.types.logical.CharType;
+import org.apache.flink.table.types.logical.DateType;
+import org.apache.flink.table.types.logical.DayTimeIntervalType;
+import org.apache.flink.table.types.logical.DecimalType;
+import org.apache.flink.table.types.logical.DoubleType;
+import org.apache.flink.table.types.logical.FloatType;
+import org.apache.flink.table.types.logical.IntType;
+import org.apache.flink.table.types.logical.LocalZonedTimestampType;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.MapType;
+import org.apache.flink.table.types.logical.MultisetType;
+import org.apache.flink.table.types.logical.NullType;
+import org.apache.flink.table.types.logical.RawType;
+import org.apache.flink.table.types.logical.RowType;
+import org.apache.flink.table.types.logical.SmallIntType;
+import org.apache.flink.table.types.logical.TimeType;
+import org.apache.flink.table.types.logical.TimestampKind;
+import org.apache.flink.table.types.logical.TimestampType;
+import org.apache.flink.table.types.logical.TinyIntType;
+import org.apache.flink.table.types.logical.VarBinaryType;
+import org.apache.flink.table.types.logical.VarCharType;
+import org.apache.flink.table.types.logical.ZonedTimestampType;
+import org.apache.flink.types.Row;
+
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.module.SimpleModule;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.parallel.Execution;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
+
+import java.io.IOException;
+import java.time.LocalDateTime;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.junit.jupiter.api.parallel.ExecutionMode.CONCURRENT;
+
+/** Tests for {@link LogicalType} serialization and deserialization. */
+@Execution(CONCURRENT)
+public class LogicalTypeJsonSerDeTest {
+
+ private final ObjectMapper mapper = getObjectMapper();
+
+ // final constants for testing unsupported case
+ private final LogicalType unsupportedType =
+ new DayTimeIntervalType(DayTimeIntervalType.DayTimeResolution.DAY_TO_HOUR);
+ private final String serializerExceptionMessageFormat =
+ "Unable to serialize logical type '%s'. Please check the documentation for supported types.";
+ private final String unsupportedTypeString = "INTERVAL_DAY_TIME";
+ private final String json =
+ String.format(
+ "{\"%s\": \"%s\", \"%s\": %s}",
+ "type", unsupportedTypeString, "nullable", "true");
+ private final String deserializerExceptionMessageFormat =
+ "Unable to deserialize a logical type of type root '%s'. Please check the documentation for supported types.";
+
+ @ParameterizedTest
+ @MethodSource("generateTestData")
+ public void testLogicalTypeJsonSerDe(LogicalType logicalType) throws IOException {
+ String json = mapper.writeValueAsString(logicalType);
+ LogicalType actualType = mapper.readValue(json, LogicalType.class);
+
+ assertThat(actualType).isEqualTo(logicalType);
+ }
+
+ @Test
+ public void testSerDeWithUnsupportedType() {
+ // test to serialize unsupported LogicalType
+ assertThatThrownBy(() -> mapper.writeValueAsString(unsupportedType))
+ .satisfies(
+ FlinkAssertions.anyCauseMatches(
+ UnsupportedOperationException.class,
+ String.format(
+ serializerExceptionMessageFormat,
+ unsupportedType.asSummaryString())));
+
+ // test to deserialize unsupported JSON string
+ assertThatThrownBy(() -> mapper.readValue(json, LogicalType.class))
+ .satisfies(
+ FlinkAssertions.anyCauseMatches(
+ UnsupportedOperationException.class,
+ String.format(
+ deserializerExceptionMessageFormat,
+ unsupportedTypeString)));
+ }
+
+ // --------------------------------------------------------------------------------------------
+ // Test data
+ // --------------------------------------------------------------------------------------------
+
+ private static List<LogicalType> generateTestData() {
+ List<LogicalType> types =
+ Arrays.asList(
+ new BooleanType(),
+ new TinyIntType(),
+ new SmallIntType(),
+ new IntType(),
+ new BigIntType(),
+ new FloatType(),
+ new DoubleType(),
+ new DateType(),
+ CharType.ofEmptyLiteral(),
+ new CharType(),
+ new CharType(5),
+ VarCharType.ofEmptyLiteral(),
+ new VarCharType(),
+ new VarCharType(5),
+ BinaryType.ofEmptyLiteral(),
+ new BinaryType(),
+ new BinaryType(100),
+ VarBinaryType.ofEmptyLiteral(),
+ new VarBinaryType(),
+ new VarBinaryType(100),
+ new DecimalType(10),
+ new DecimalType(15, 5),
+ new TimeType(),
+ new TimeType(3),
+ new TimestampType(),
+ new TimestampType(3),
+ new TimestampType(false, 3),
+ new ZonedTimestampType(),
+ new ZonedTimestampType(3),
+ new LocalZonedTimestampType(),
+ new LocalZonedTimestampType(3),
+ new LocalZonedTimestampType(false, 3),
+ // LocalZonedTimestampType#eaquals doesn't compare TimestampKind
+ new LocalZonedTimestampType(false, TimestampKind.PROCTIME, 3),
+ new MapType(new BigIntType(), new IntType(false)),
+ new MapType(CharType.ofEmptyLiteral(), CharType.ofEmptyLiteral()),
+ new MapType(VarCharType.ofEmptyLiteral(), VarCharType.ofEmptyLiteral()),
+ new MapType(BinaryType.ofEmptyLiteral(), BinaryType.ofEmptyLiteral()),
+ new MapType(VarBinaryType.ofEmptyLiteral(), VarBinaryType.ofEmptyLiteral()),
+ new MapType(new TimestampType(false, 3), new LocalZonedTimestampType()),
+ new ArrayType(new IntType(false)),
+ new ArrayType(new TimestampType()),
+ new ArrayType(new LocalZonedTimestampType(false, 3)),
+ new ArrayType(CharType.ofEmptyLiteral()),
+ new ArrayType(VarCharType.ofEmptyLiteral()),
+ new ArrayType(BinaryType.ofEmptyLiteral()),
+ new ArrayType(VarBinaryType.ofEmptyLiteral()),
+ new MultisetType(new IntType(false)),
+ new MultisetType(new TimestampType()),
+ new MultisetType(new TimestampType(true, 3)),
+ new MultisetType(CharType.ofEmptyLiteral()),
+ new MultisetType(VarCharType.ofEmptyLiteral()),
+ new MultisetType(BinaryType.ofEmptyLiteral()),
+ new MultisetType(VarBinaryType.ofEmptyLiteral()),
+ RowType.of(new BigIntType(), new IntType(false), new VarCharType(200)),
+ RowType.of(
+ new LogicalType[] {
+ new BigIntType(), new IntType(false), new VarCharType(200)
+ },
+ new String[] {"f1", "f2", "f3"}),
+ RowType.of(
+ new TimestampType(false, 3), new LocalZonedTimestampType(false, 3)),
+ RowType.of(
+ CharType.ofEmptyLiteral(),
+ VarCharType.ofEmptyLiteral(),
+ BinaryType.ofEmptyLiteral(),
+ VarBinaryType.ofEmptyLiteral()),
+ // Row with descriptions
+ new RowType(
+ Arrays.asList(
+ new RowType.RowField("ID", new BigIntType(), "ID desc"),
+ new RowType.RowField(
+ "Name", new VarCharType(20), "Name desc"))),
+ // custom RawType
+ new RawType<>(LocalDateTime.class, LocalDateTimeSerializer.INSTANCE),
+ // external RawType
+ new RawType<>(
+ Row.class,
+ ExternalSerializer.of(
+ DataTypes.ROW(DataTypes.INT(), DataTypes.STRING()))));
+
+ List<LogicalType> testTypes =
+ Stream.concat(
+ types.stream().map(type -> type.copy(true)),
+ types.stream().map(type -> type.copy(false)))
+ .collect(Collectors.toList());
+
+ // ignore nullable for NullType
+ testTypes.add(new NullType());
+
+ return testTypes;
+ }
+
+ /**
+ * LogicalType isn't annotated with Jackson annotations, so it's necessary to register the
+ * customer serializer and deserializer when testing LogicalType Serde alone.
+ */
Review Comment:
Removed.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink] fsk119 merged pull request #20617: [FLINK-28936][sql-gateway] Fix REST endpoint can not serialize CHAR(0) by introducing a new serializer for LogicalType
Posted by GitBox <gi...@apache.org>.
fsk119 merged PR #20617:
URL: https://github.com/apache/flink/pull/20617
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink] yuzelin commented on a diff in pull request #20617: [FLINK-28936][sql-gateway] Fix REST endpoint can not serialize CHAR(0) by introducing a new serializer for LogicalType
Posted by GitBox <gi...@apache.org>.
yuzelin commented on code in PR #20617:
URL: https://github.com/apache/flink/pull/20617#discussion_r951211937
##########
flink-table/flink-sql-gateway-api/src/main/java/org/apache/flink/table/gateway/api/results/serde/LogicalTypeJsonSerializer.java:
##########
@@ -0,0 +1,210 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.table.gateway.api.results.serde;
+
+import org.apache.flink.annotation.PublicEvolving;
+import org.apache.flink.table.api.ValidationException;
+import org.apache.flink.table.types.logical.ArrayType;
+import org.apache.flink.table.types.logical.BinaryType;
+import org.apache.flink.table.types.logical.CharType;
+import org.apache.flink.table.types.logical.DecimalType;
+import org.apache.flink.table.types.logical.LocalZonedTimestampType;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.MapType;
+import org.apache.flink.table.types.logical.MultisetType;
+import org.apache.flink.table.types.logical.RawType;
+import org.apache.flink.table.types.logical.RowType;
+import org.apache.flink.table.types.logical.TimestampType;
+import org.apache.flink.table.types.logical.VarBinaryType;
+import org.apache.flink.table.types.logical.VarCharType;
+
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonGenerator;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.SerializerProvider;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ser.std.StdSerializer;
+
+import java.io.IOException;
+
+/**
+ * Json serializer for {@link LogicalType}.
+ *
+ * @see LogicalTypeJsonDeserializer for the reverse operation.
+ */
+@PublicEvolving
+public final class LogicalTypeJsonSerializer extends StdSerializer<LogicalType> {
+
+ private static final long serialVersionUID = 1L;
+
+ // --------------------------------------------------------------------------------------------
+ // Public string constants for serializer and deserializer
+ // --------------------------------------------------------------------------------------------
+
+ // Common fields
+ public static final String FIELD_NAME_TYPE_NAME = "type";
+ public static final String FIELD_NAME_NULLABLE = "nullable";
Review Comment:
As far as I know, only StructuredType has field `description`, so I think we don't need serialize this because it doesn't support StructuredType until now.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink] yuzelin commented on a diff in pull request #20617: [FLINK-28936][sql-gateway] Fix REST endpoint can not serialize CHAR(0) by introducing a new serializer for LogicalType
Posted by GitBox <gi...@apache.org>.
yuzelin commented on code in PR #20617:
URL: https://github.com/apache/flink/pull/20617#discussion_r951211937
##########
flink-table/flink-sql-gateway-api/src/main/java/org/apache/flink/table/gateway/api/results/serde/LogicalTypeJsonSerializer.java:
##########
@@ -0,0 +1,210 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.table.gateway.api.results.serde;
+
+import org.apache.flink.annotation.PublicEvolving;
+import org.apache.flink.table.api.ValidationException;
+import org.apache.flink.table.types.logical.ArrayType;
+import org.apache.flink.table.types.logical.BinaryType;
+import org.apache.flink.table.types.logical.CharType;
+import org.apache.flink.table.types.logical.DecimalType;
+import org.apache.flink.table.types.logical.LocalZonedTimestampType;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.MapType;
+import org.apache.flink.table.types.logical.MultisetType;
+import org.apache.flink.table.types.logical.RawType;
+import org.apache.flink.table.types.logical.RowType;
+import org.apache.flink.table.types.logical.TimestampType;
+import org.apache.flink.table.types.logical.VarBinaryType;
+import org.apache.flink.table.types.logical.VarCharType;
+
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonGenerator;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.SerializerProvider;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ser.std.StdSerializer;
+
+import java.io.IOException;
+
+/**
+ * Json serializer for {@link LogicalType}.
+ *
+ * @see LogicalTypeJsonDeserializer for the reverse operation.
+ */
+@PublicEvolving
+public final class LogicalTypeJsonSerializer extends StdSerializer<LogicalType> {
+
+ private static final long serialVersionUID = 1L;
+
+ // --------------------------------------------------------------------------------------------
+ // Public string constants for serializer and deserializer
+ // --------------------------------------------------------------------------------------------
+
+ // Common fields
+ public static final String FIELD_NAME_TYPE_NAME = "type";
+ public static final String FIELD_NAME_NULLABLE = "nullable";
Review Comment:
As far as I know, only StructuredType has field `description`, so I think we don't need serialize this because it doesn't support StructuredType until now.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink] yuzelin commented on a diff in pull request #20617: [FLINK-28936][sql-gateway] Fix REST endpoint can not serialize CHAR(0) by introducing a new serializer for LogicalType
Posted by GitBox <gi...@apache.org>.
yuzelin commented on code in PR #20617:
URL: https://github.com/apache/flink/pull/20617#discussion_r951357533
##########
flink-table/flink-sql-gateway-api/src/main/java/org/apache/flink/table/gateway/api/results/serde/LogicalTypeJsonSerializer.java:
##########
@@ -0,0 +1,210 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.table.gateway.api.results.serde;
+
+import org.apache.flink.annotation.PublicEvolving;
+import org.apache.flink.table.api.ValidationException;
+import org.apache.flink.table.types.logical.ArrayType;
+import org.apache.flink.table.types.logical.BinaryType;
+import org.apache.flink.table.types.logical.CharType;
+import org.apache.flink.table.types.logical.DecimalType;
+import org.apache.flink.table.types.logical.LocalZonedTimestampType;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.MapType;
+import org.apache.flink.table.types.logical.MultisetType;
+import org.apache.flink.table.types.logical.RawType;
+import org.apache.flink.table.types.logical.RowType;
+import org.apache.flink.table.types.logical.TimestampType;
+import org.apache.flink.table.types.logical.VarBinaryType;
+import org.apache.flink.table.types.logical.VarCharType;
+
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonGenerator;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.SerializerProvider;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ser.std.StdSerializer;
+
+import java.io.IOException;
+
+/**
+ * Json serializer for {@link LogicalType}.
+ *
+ * @see LogicalTypeJsonDeserializer for the reverse operation.
+ */
+@PublicEvolving
+public final class LogicalTypeJsonSerializer extends StdSerializer<LogicalType> {
+
+ private static final long serialVersionUID = 1L;
+
+ // --------------------------------------------------------------------------------------------
+ // Public string constants for serializer and deserializer
+ // --------------------------------------------------------------------------------------------
+
+ // Common fields
+ public static final String FIELD_NAME_TYPE_NAME = "type";
+ public static final String FIELD_NAME_NULLABLE = "nullable";
+
+ // CHAR, VARCHAR, BINARY, VARBINARY
+ public static final String FIELD_NAME_LENGTH = "length";
+
+ // DECIMAL, TIMESTAMP_WITHOUT_TIME_ZONE, TIMESTAMP_WITH_LOCAL_TIME_ZONE
+ public static final String FIELD_NAME_PRECISION = "precision";
+
+ // DECIMAL
+ public static final String FIELD_NAME_SCALE = "scale";
+
+ // MAP
+ public static final String FIELD_NAME_KEY_TYPE = "keyType";
+ public static final String FIELD_NAME_VALUE_TYPE = "valueType";
+
+ // ARRAY, MULTISET
+ public static final String FIELD_NAME_ELEMENT_TYPE = "elementType";
+
+ // ROW
+ public static final String FIELD_NAME_FIELDS = "fields";
+ public static final String FIELD_NAME_FIELD_NAME = "name";
+ public static final String FIELD_NAME_FIELD_TYPE = "fieldType";
+
+ // RAW
+ public static final String FIELD_NAME_CLASS = "class";
+ public static final String FIELD_NAME_SERIALIZER = "serializer";
+
+ public LogicalTypeJsonSerializer() {
+ super(LogicalType.class);
+ }
+
+ @Override
+ public void serialize(
+ LogicalType logicalType,
+ JsonGenerator jsonGenerator,
+ SerializerProvider serializerProvider)
+ throws IOException {
+ serializeInternal(logicalType, jsonGenerator);
+ }
+
+ private void serializeInternal(LogicalType logicalType, JsonGenerator jsonGenerator)
+ throws IOException {
+ jsonGenerator.writeStartObject();
+
+ // write common fields shared by all types
+ jsonGenerator.writeStringField(FIELD_NAME_TYPE_NAME, logicalType.getTypeRoot().name());
+ jsonGenerator.writeBooleanField(FIELD_NAME_NULLABLE, logicalType.isNullable());
+ // write special fields according to type root
+ switch (logicalType.getTypeRoot()) {
+ case BOOLEAN:
+ case TINYINT:
+ case SMALLINT:
+ case INTEGER:
+ case BIGINT:
+ case FLOAT:
+ case DOUBLE:
+ case DATE:
+ case NULL:
+ break;
+ case CHAR:
+ jsonGenerator.writeNumberField(
+ FIELD_NAME_LENGTH, ((CharType) logicalType).getLength());
+ break;
+ case VARCHAR:
+ jsonGenerator.writeNumberField(
+ FIELD_NAME_LENGTH, ((VarCharType) logicalType).getLength());
+ break;
+ case BINARY:
+ jsonGenerator.writeNumberField(
+ FIELD_NAME_LENGTH, ((BinaryType) logicalType).getLength());
+ break;
+ case VARBINARY:
+ jsonGenerator.writeNumberField(
+ FIELD_NAME_LENGTH, ((VarBinaryType) logicalType).getLength());
+ break;
+ case DECIMAL:
+ jsonGenerator.writeNumberField(
+ FIELD_NAME_PRECISION, ((DecimalType) logicalType).getPrecision());
+ jsonGenerator.writeNumberField(
+ FIELD_NAME_SCALE, ((DecimalType) logicalType).getScale());
+ break;
+ case TIMESTAMP_WITHOUT_TIME_ZONE:
+ jsonGenerator.writeNumberField(
+ FIELD_NAME_PRECISION, ((TimestampType) logicalType).getPrecision());
+ break;
+ case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
+ jsonGenerator.writeNumberField(
+ FIELD_NAME_PRECISION,
+ ((LocalZonedTimestampType) logicalType).getPrecision());
+ break;
+ case MAP:
+ serializeMap((MapType) logicalType, jsonGenerator);
+ break;
+ case ARRAY:
+ serializeCollection(((ArrayType) logicalType).getElementType(), jsonGenerator);
+ break;
+ case MULTISET:
+ serializeCollection(((MultisetType) logicalType).getElementType(), jsonGenerator);
+ break;
+ case ROW:
+ serializeRow((RowType) logicalType, jsonGenerator);
+ break;
+ case RAW:
+ if (logicalType instanceof RawType) {
+ serializeRaw((RawType<?>) logicalType, jsonGenerator);
+ break;
+ }
+ // fall through
+ default:
+ throw new ValidationException(
Review Comment:
Changed.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [flink] yuzelin commented on a diff in pull request #20617: [FLINK-28936][sql-gateway] Fix REST endpoint can not serialize CHAR(0) by introducing a new serializer for LogicalType
Posted by GitBox <gi...@apache.org>.
yuzelin commented on code in PR #20617:
URL: https://github.com/apache/flink/pull/20617#discussion_r951378971
##########
flink-table/flink-sql-gateway-api/src/main/java/org/apache/flink/table/gateway/api/results/serde/LogicalTypeJsonSerializer.java:
##########
@@ -0,0 +1,210 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.table.gateway.api.results.serde;
+
+import org.apache.flink.annotation.PublicEvolving;
+import org.apache.flink.table.api.ValidationException;
+import org.apache.flink.table.types.logical.ArrayType;
+import org.apache.flink.table.types.logical.BinaryType;
+import org.apache.flink.table.types.logical.CharType;
+import org.apache.flink.table.types.logical.DecimalType;
+import org.apache.flink.table.types.logical.LocalZonedTimestampType;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.MapType;
+import org.apache.flink.table.types.logical.MultisetType;
+import org.apache.flink.table.types.logical.RawType;
+import org.apache.flink.table.types.logical.RowType;
+import org.apache.flink.table.types.logical.TimestampType;
+import org.apache.flink.table.types.logical.VarBinaryType;
+import org.apache.flink.table.types.logical.VarCharType;
+
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonGenerator;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.SerializerProvider;
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ser.std.StdSerializer;
+
+import java.io.IOException;
+
+/**
+ * Json serializer for {@link LogicalType}.
+ *
+ * @see LogicalTypeJsonDeserializer for the reverse operation.
+ */
+@PublicEvolving
+public final class LogicalTypeJsonSerializer extends StdSerializer<LogicalType> {
+
+ private static final long serialVersionUID = 1L;
+
+ // --------------------------------------------------------------------------------------------
+ // Public string constants for serializer and deserializer
+ // --------------------------------------------------------------------------------------------
+
+ // Common fields
+ public static final String FIELD_NAME_TYPE_NAME = "type";
+ public static final String FIELD_NAME_NULLABLE = "nullable";
+
+ // CHAR, VARCHAR, BINARY, VARBINARY
+ public static final String FIELD_NAME_LENGTH = "length";
+
+ // DECIMAL, TIMESTAMP_WITHOUT_TIME_ZONE, TIMESTAMP_WITH_LOCAL_TIME_ZONE
+ public static final String FIELD_NAME_PRECISION = "precision";
+
+ // DECIMAL
+ public static final String FIELD_NAME_SCALE = "scale";
+
+ // MAP
+ public static final String FIELD_NAME_KEY_TYPE = "keyType";
+ public static final String FIELD_NAME_VALUE_TYPE = "valueType";
+
+ // ARRAY, MULTISET
+ public static final String FIELD_NAME_ELEMENT_TYPE = "elementType";
+
+ // ROW
+ public static final String FIELD_NAME_FIELDS = "fields";
+ public static final String FIELD_NAME_FIELD_NAME = "name";
+ public static final String FIELD_NAME_FIELD_TYPE = "fieldType";
+
+ // RAW
+ public static final String FIELD_NAME_CLASS = "class";
+ public static final String FIELD_NAME_SERIALIZER = "serializer";
+
+ public LogicalTypeJsonSerializer() {
+ super(LogicalType.class);
+ }
+
+ @Override
+ public void serialize(
+ LogicalType logicalType,
+ JsonGenerator jsonGenerator,
+ SerializerProvider serializerProvider)
+ throws IOException {
+ serializeInternal(logicalType, jsonGenerator);
+ }
+
+ private void serializeInternal(LogicalType logicalType, JsonGenerator jsonGenerator)
+ throws IOException {
+ jsonGenerator.writeStartObject();
+
+ // write common fields shared by all types
+ jsonGenerator.writeStringField(FIELD_NAME_TYPE_NAME, logicalType.getTypeRoot().name());
+ jsonGenerator.writeBooleanField(FIELD_NAME_NULLABLE, logicalType.isNullable());
+ // write special fields according to type root
+ switch (logicalType.getTypeRoot()) {
+ case BOOLEAN:
+ case TINYINT:
+ case SMALLINT:
+ case INTEGER:
+ case BIGINT:
+ case FLOAT:
+ case DOUBLE:
+ case DATE:
+ case NULL:
+ break;
+ case CHAR:
+ jsonGenerator.writeNumberField(
+ FIELD_NAME_LENGTH, ((CharType) logicalType).getLength());
+ break;
+ case VARCHAR:
+ jsonGenerator.writeNumberField(
+ FIELD_NAME_LENGTH, ((VarCharType) logicalType).getLength());
+ break;
+ case BINARY:
+ jsonGenerator.writeNumberField(
+ FIELD_NAME_LENGTH, ((BinaryType) logicalType).getLength());
+ break;
+ case VARBINARY:
+ jsonGenerator.writeNumberField(
+ FIELD_NAME_LENGTH, ((VarBinaryType) logicalType).getLength());
+ break;
+ case DECIMAL:
+ jsonGenerator.writeNumberField(
+ FIELD_NAME_PRECISION, ((DecimalType) logicalType).getPrecision());
+ jsonGenerator.writeNumberField(
+ FIELD_NAME_SCALE, ((DecimalType) logicalType).getScale());
+ break;
+ case TIMESTAMP_WITHOUT_TIME_ZONE:
Review Comment:
Added.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org