Copilot commented on code in PR #12624:
URL: https://github.com/apache/gravitino/pull/12624#discussion_r3861562504


##########
common/src/test/java/org/apache/gravitino/dto/semantic/TestSemanticModelDefinitionDTO.java:
##########
@@ -0,0 +1,350 @@
+/*
+ * 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.gravitino.dto.semantic;
+
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import org.apache.gravitino.NameIdentifier;
+import org.apache.gravitino.json.JsonUtils;
+import org.apache.gravitino.semantic.AIContext;
+import org.apache.gravitino.semantic.AIContextObject;
+import org.apache.gravitino.semantic.CustomExtension;
+import org.apache.gravitino.semantic.DataType;
+import org.apache.gravitino.semantic.Dataset;
+import org.apache.gravitino.semantic.DialectExpression;
+import org.apache.gravitino.semantic.Dialects;
+import org.apache.gravitino.semantic.Dimension;
+import org.apache.gravitino.semantic.Expression;
+import org.apache.gravitino.semantic.Field;
+import org.apache.gravitino.semantic.Metric;
+import org.apache.gravitino.semantic.Relationship;
+import org.apache.gravitino.semantic.SemanticModelDefinition;
+import org.junit.jupiter.api.Test;
+
+public class TestSemanticModelDefinitionDTO {
+
+  private final ObjectMapper objectMapper = JsonUtils.objectMapper();
+  private final ObjectMapper persistenceMapper = JsonUtils.anyFieldMapper();
+
+  @Test
+  public void testDefinitionConversionAndJsonRoundTrip() throws 
JsonProcessingException {
+    SemanticModelDefinition definition = definition();
+    SemanticModelDefinitionDTO dto = 
SemanticModelDefinitionDTO.fromDefinition(definition);
+
+    String json = persistenceMapper.writeValueAsString(dto);
+    JsonNode root = persistenceMapper.readTree(json);
+    JsonNode dataset = root.path("datasets").get(0);
+    JsonNode field = dataset.path("fields").get(0);
+    JsonNode relationship = root.path("relationships").get(0);
+
+    assertTrue(root.has("ai_context"));
+    assertTrue(root.has("custom_extensions"));
+    assertFalse(root.has("aiContext"));
+    assertFalse(json.contains("customExtensions"));
+    assertEquals(List.of("sales", "mart"), 
stringValues(dataset.path("source").path("namespace")));
+    assertEquals("orders", dataset.path("source").path("name").textValue());
+    assertTrue(dataset.has("primary_key"));
+    assertTrue(dataset.has("unique_keys"));
+    assertTrue(dataset.has("ai_context"));
+    assertEquals("DateTimeTz", field.path("datatype").textValue());
+    assertTrue(field.path("dimension").path("is_time").booleanValue());
+    assertEquals(
+        "ANSI_SQL", 
field.path("expression").path("dialects").get(0).path("dialect").textValue());
+    assertTrue(relationship.has("from_columns"));
+    assertTrue(relationship.has("to_columns"));
+    assertEquals("example", 
root.path("custom_extensions").get(0).path("vendor_name").textValue());
+
+    JsonNode aiContext = root.path("ai_context");
+    assertEquals(
+        List.of(
+            "instructions", "synonyms", "examples", "priority", 
"semantic_hints", "nullable_hint"),
+        fieldNames(aiContext));
+    assertEquals(List.of("first", "second"), 
fieldNames(aiContext.path("semantic_hints")));
+    assertTrue(aiContext.path("nullable_hint").isNull());
+
+    SemanticModelDefinitionDTO deserialized =
+        persistenceMapper.readValue(json, SemanticModelDefinitionDTO.class);
+    assertEquals(dto, deserialized);
+    assertEquals(definition, deserialized.toDefinition());
+    assertEquals(json, persistenceMapper.writeValueAsString(deserialized));
+  }
+
+  @Test
+  public void testAIContextUnionAndUnknownPropertyOrder() throws 
JsonProcessingException {
+    AIContextDTO textContext = AIContextDTO.fromAIContext(AIContext.of(""));
+    String textJson = objectMapper.writeValueAsString(textContext);
+    assertEquals("\"\"", textJson);
+    assertEquals(
+        AIContext.of(""), objectMapper.readValue(textJson, 
AIContextDTO.class).toAIContext());
+
+    String objectJson =
+        "{\"instructions\":\"Use governed data\","
+            + "\"first_unknown\":{\"alpha\":1,\"beta\":2},"
+            + "\"synonyms\":[\"governed\"],"
+            + "\"second_unknown\":[true,null],"
+            + "\"precise\":0.123456789012345678901234567890}";
+    AIContextDTO objectContext = objectMapper.readValue(objectJson, 
AIContextDTO.class);
+
+    assertNull(objectContext.getText());
+    assertEquals(
+        List.of("first_unknown", "second_unknown", "precise"),
+        new 
ArrayList<>(objectContext.getObject().getAdditionalProperties().keySet()));
+    assertEquals(
+        new BigDecimal("0.123456789012345678901234567890"),
+        objectContext.getObject().getAdditionalProperties().get("precise"));
+    assertEquals(
+        List.of("alpha", "beta"),
+        new ArrayList<>(
+            ((Map<?, ?>) 
objectContext.getObject().getAdditionalProperties().get("first_unknown"))
+                .keySet()));
+    assertEquals(
+        BigInteger.ONE,
+        ((Map<?, ?>) 
objectContext.getObject().getAdditionalProperties().get("first_unknown"))
+            .get("alpha"));
+
+    AIContextDTO converted = 
AIContextDTO.fromAIContext(objectContext.toAIContext());
+    assertEquals(
+        List.of("first_unknown", "second_unknown", "precise"),
+        new 
ArrayList<>(converted.getObject().getAdditionalProperties().keySet()));
+    assertEquals(
+        List.of("instructions", "synonyms", "first_unknown", "second_unknown", 
"precise"),
+        
fieldNames(objectMapper.readTree(objectMapper.writeValueAsString(converted))));
+    assertEquals(
+        objectContext.getObject().getAdditionalProperties(),
+        converted.getObject().getAdditionalProperties());
+    assertTrue(
+        
objectMapper.writeValueAsString(converted).contains("0.123456789012345678901234567890"));
+
+    assertThrows(
+        JsonProcessingException.class, () -> objectMapper.readValue("42", 
AIContextDTO.class));
+  }
+
+  @Test
+  public void testAbsentAndEmptyArraysRemainDistinct() throws 
JsonProcessingException {
+    Dataset absentDataset =
+        Dataset.builder()
+            .withName("absent")
+            .withSource(NameIdentifier.of("sales", "mart", "absent"))
+            .build();
+    SemanticModelDefinition absentDefinition =
+        SemanticModelDefinition.builder().withDatasets(new Dataset[] 
{absentDataset}).build();
+    SemanticModelDefinitionDTO absent = 
SemanticModelDefinitionDTO.fromDefinition(absentDefinition);
+    String absentJson = objectMapper.writeValueAsString(absent);
+
+    assertFalse(absentJson.contains("fields"));
+    assertFalse(absentJson.contains("relationships"));

Review Comment:
   The tests use substring checks on the raw JSON (`String.contains(...)`) to 
verify field naming/absence, which is brittle (it can produce false positives 
if the substring appears in data values, and it’s sensitive to formatting). 
Prefer asserting on parsed JSON structure (e.g., `JsonNode.has(...)`, 
`path(...).isMissingNode()`, or checking exact field names via 
`fieldNames(...)`) to make the tests robust and intention-revealing.



##########
common/src/main/java/org/apache/gravitino/dto/semantic/AIContextObjectDTO.java:
##########
@@ -0,0 +1,222 @@
+/*
+ * 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.gravitino.dto.semantic;
+
+import com.fasterxml.jackson.annotation.JsonAnyGetter;
+import com.fasterxml.jackson.annotation.JsonIgnore;
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.annotation.JsonPropertyOrder;
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.core.JsonToken;
+import com.fasterxml.jackson.databind.DeserializationContext;
+import com.fasterxml.jackson.databind.JsonDeserializer;
+import com.fasterxml.jackson.databind.JsonMappingException;
+import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import javax.annotation.Nullable;
+import lombok.AccessLevel;
+import lombok.AllArgsConstructor;
+import lombok.Builder;
+import lombok.EqualsAndHashCode;
+import lombok.Getter;
+import lombok.NoArgsConstructor;
+import org.apache.gravitino.semantic.AIContextObject;
+
+/** DTO for structured Semantic Model AI context. */
+@Getter
+@EqualsAndHashCode
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+@AllArgsConstructor(access = AccessLevel.PRIVATE)
+@Builder(setterPrefix = "with")
+@JsonInclude(JsonInclude.Include.NON_NULL)
+@JsonPropertyOrder({"instructions", "synonyms", "examples"})
+@JsonDeserialize(using = AIContextObjectDTO.Deserializer.class)
+public class AIContextObjectDTO {
+
+  @Nullable
+  @JsonProperty("instructions")
+  private String instructions;
+
+  @Nullable
+  @JsonProperty("synonyms")
+  private String[] synonyms;
+
+  @Nullable
+  @JsonProperty("examples")
+  private String[] examples;
+
+  @JsonIgnore
+  @Getter(AccessLevel.NONE)
+  @Builder.Default
+  private Map<String, Object> additionalProperties = new LinkedHashMap<>();
+
+  /**
+   * Creates a structured AI context DTO from an API model.
+   *
+   * @param aiContextObject The API AI context object.
+   * @return The structured AI context DTO.
+   */
+  public static AIContextObjectDTO fromAIContextObject(AIContextObject 
aiContextObject) {
+    return builder()
+        .withInstructions(aiContextObject.instructions())
+        .withSynonyms(aiContextObject.synonyms())
+        .withExamples(aiContextObject.examples())
+        .withAdditionalProperties(new 
LinkedHashMap<>(aiContextObject.additionalProperties()))
+        .build();
+  }
+
+  /**
+   * Converts this DTO to an API AI context object.
+   *
+   * @return The API AI context object.
+   */
+  public AIContextObject toAIContextObject() {
+    return AIContextObject.builder()
+        .withInstructions(instructions)
+        .withSynonyms(synonyms)
+        .withExamples(examples)
+        .withAdditionalProperties(
+            additionalProperties == null ? Collections.emptyMap() : 
additionalProperties)
+        .build();
+  }
+
+  /**
+   * Returns unknown AI-context properties in their input order.
+   *
+   * @return The additional properties.
+   */
+  @JsonAnyGetter
+  public Map<String, Object> getAdditionalProperties() {
+    return additionalProperties == null ? Collections.emptyMap() : 
additionalProperties;
+  }
+
+  /** Deserializes structured AI context while retaining unknown JSON values 
and their order. */
+  public static final class Deserializer extends 
JsonDeserializer<AIContextObjectDTO> {
+
+    @Override
+    public AIContextObjectDTO deserialize(JsonParser parser, 
DeserializationContext context)
+        throws IOException {
+      if (!parser.hasToken(JsonToken.START_OBJECT)) {
+        throw JsonMappingException.from(parser, "Structured AI context must be 
an object");
+      }

Review Comment:
   The new deserialization errors omit the actual token/value encountered 
(e.g., what was found instead of `START_OBJECT` / `FIELD_NAME`). Including 
`parser.currentToken()` (and potentially `parser.getText()` where safe) would 
make these failures significantly easier to debug, especially when AI context 
is embedded in larger documents.



##########
common/src/main/java/org/apache/gravitino/dto/semantic/AIContextObjectDTO.java:
##########
@@ -0,0 +1,222 @@
+/*
+ * 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.gravitino.dto.semantic;
+
+import com.fasterxml.jackson.annotation.JsonAnyGetter;
+import com.fasterxml.jackson.annotation.JsonIgnore;
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.annotation.JsonPropertyOrder;
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.core.JsonToken;
+import com.fasterxml.jackson.databind.DeserializationContext;
+import com.fasterxml.jackson.databind.JsonDeserializer;
+import com.fasterxml.jackson.databind.JsonMappingException;
+import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import javax.annotation.Nullable;
+import lombok.AccessLevel;
+import lombok.AllArgsConstructor;
+import lombok.Builder;
+import lombok.EqualsAndHashCode;
+import lombok.Getter;
+import lombok.NoArgsConstructor;
+import org.apache.gravitino.semantic.AIContextObject;
+
+/** DTO for structured Semantic Model AI context. */
+@Getter
+@EqualsAndHashCode
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+@AllArgsConstructor(access = AccessLevel.PRIVATE)
+@Builder(setterPrefix = "with")
+@JsonInclude(JsonInclude.Include.NON_NULL)
+@JsonPropertyOrder({"instructions", "synonyms", "examples"})
+@JsonDeserialize(using = AIContextObjectDTO.Deserializer.class)
+public class AIContextObjectDTO {
+
+  @Nullable
+  @JsonProperty("instructions")
+  private String instructions;
+
+  @Nullable
+  @JsonProperty("synonyms")
+  private String[] synonyms;
+
+  @Nullable
+  @JsonProperty("examples")
+  private String[] examples;
+
+  @JsonIgnore
+  @Getter(AccessLevel.NONE)
+  @Builder.Default
+  private Map<String, Object> additionalProperties = new LinkedHashMap<>();
+
+  /**
+   * Creates a structured AI context DTO from an API model.
+   *
+   * @param aiContextObject The API AI context object.
+   * @return The structured AI context DTO.
+   */
+  public static AIContextObjectDTO fromAIContextObject(AIContextObject 
aiContextObject) {
+    return builder()
+        .withInstructions(aiContextObject.instructions())
+        .withSynonyms(aiContextObject.synonyms())
+        .withExamples(aiContextObject.examples())
+        .withAdditionalProperties(new 
LinkedHashMap<>(aiContextObject.additionalProperties()))
+        .build();
+  }
+
+  /**
+   * Converts this DTO to an API AI context object.
+   *
+   * @return The API AI context object.
+   */
+  public AIContextObject toAIContextObject() {
+    return AIContextObject.builder()
+        .withInstructions(instructions)
+        .withSynonyms(synonyms)
+        .withExamples(examples)
+        .withAdditionalProperties(
+            additionalProperties == null ? Collections.emptyMap() : 
additionalProperties)
+        .build();
+  }
+
+  /**
+   * Returns unknown AI-context properties in their input order.
+   *
+   * @return The additional properties.
+   */
+  @JsonAnyGetter
+  public Map<String, Object> getAdditionalProperties() {
+    return additionalProperties == null ? Collections.emptyMap() : 
additionalProperties;
+  }
+
+  /** Deserializes structured AI context while retaining unknown JSON values 
and their order. */
+  public static final class Deserializer extends 
JsonDeserializer<AIContextObjectDTO> {
+
+    @Override
+    public AIContextObjectDTO deserialize(JsonParser parser, 
DeserializationContext context)
+        throws IOException {
+      if (!parser.hasToken(JsonToken.START_OBJECT)) {
+        throw JsonMappingException.from(parser, "Structured AI context must be 
an object");
+      }
+
+      String instructions = null;
+      String[] synonyms = null;
+      String[] examples = null;
+      Map<String, Object> additionalProperties = new LinkedHashMap<>();
+      while (parser.nextToken() != JsonToken.END_OBJECT) {
+        if (!parser.hasToken(JsonToken.FIELD_NAME)) {
+          throw JsonMappingException.from(parser, "Expected an AI context 
property name");
+        }

Review Comment:
   The new deserialization errors omit the actual token/value encountered 
(e.g., what was found instead of `START_OBJECT` / `FIELD_NAME`). Including 
`parser.currentToken()` (and potentially `parser.getText()` where safe) would 
make these failures significantly easier to debug, especially when AI context 
is embedded in larger documents.



##########
common/src/test/java/org/apache/gravitino/dto/semantic/TestSemanticModelDefinitionDTO.java:
##########
@@ -0,0 +1,350 @@
+/*
+ * 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.gravitino.dto.semantic;
+
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import org.apache.gravitino.NameIdentifier;
+import org.apache.gravitino.json.JsonUtils;
+import org.apache.gravitino.semantic.AIContext;
+import org.apache.gravitino.semantic.AIContextObject;
+import org.apache.gravitino.semantic.CustomExtension;
+import org.apache.gravitino.semantic.DataType;
+import org.apache.gravitino.semantic.Dataset;
+import org.apache.gravitino.semantic.DialectExpression;
+import org.apache.gravitino.semantic.Dialects;
+import org.apache.gravitino.semantic.Dimension;
+import org.apache.gravitino.semantic.Expression;
+import org.apache.gravitino.semantic.Field;
+import org.apache.gravitino.semantic.Metric;
+import org.apache.gravitino.semantic.Relationship;
+import org.apache.gravitino.semantic.SemanticModelDefinition;
+import org.junit.jupiter.api.Test;
+
+public class TestSemanticModelDefinitionDTO {
+
+  private final ObjectMapper objectMapper = JsonUtils.objectMapper();
+  private final ObjectMapper persistenceMapper = JsonUtils.anyFieldMapper();
+
+  @Test
+  public void testDefinitionConversionAndJsonRoundTrip() throws 
JsonProcessingException {
+    SemanticModelDefinition definition = definition();
+    SemanticModelDefinitionDTO dto = 
SemanticModelDefinitionDTO.fromDefinition(definition);
+
+    String json = persistenceMapper.writeValueAsString(dto);
+    JsonNode root = persistenceMapper.readTree(json);
+    JsonNode dataset = root.path("datasets").get(0);
+    JsonNode field = dataset.path("fields").get(0);
+    JsonNode relationship = root.path("relationships").get(0);
+
+    assertTrue(root.has("ai_context"));
+    assertTrue(root.has("custom_extensions"));
+    assertFalse(root.has("aiContext"));
+    assertFalse(json.contains("customExtensions"));

Review Comment:
   The tests use substring checks on the raw JSON (`String.contains(...)`) to 
verify field naming/absence, which is brittle (it can produce false positives 
if the substring appears in data values, and it’s sensitive to formatting). 
Prefer asserting on parsed JSON structure (e.g., `JsonNode.has(...)`, 
`path(...).isMissingNode()`, or checking exact field names via 
`fieldNames(...)`) to make the tests robust and intention-revealing.



##########
common/src/main/java/org/apache/gravitino/dto/semantic/SemanticDTOUtils.java:
##########
@@ -0,0 +1,129 @@
+/*
+ * 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.gravitino.dto.semantic;
+
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.core.JsonToken;
+import com.fasterxml.jackson.databind.DeserializationContext;
+import com.fasterxml.jackson.databind.JsonDeserializer;
+import com.fasterxml.jackson.databind.JsonMappingException;
+import com.fasterxml.jackson.databind.JsonSerializer;
+import com.fasterxml.jackson.databind.SerializerProvider;
+import java.io.IOException;
+import java.util.function.Function;
+import java.util.function.IntFunction;
+import javax.annotation.Nullable;
+import org.apache.gravitino.semantic.DataType;
+
+final class SemanticDTOUtils {
+
+  private SemanticDTOUtils() {}
+
+  @Nullable
+  static <S, T> T[] convertArray(
+      @Nullable S[] values, Function<S, T> converter, IntFunction<T[]> 
arrayFactory) {
+    if (values == null) {
+      return null;
+    }
+
+    T[] converted = arrayFactory.apply(values.length);
+    for (int index = 0; index < values.length; index++) {
+      converted[index] = values[index] == null ? null : 
converter.apply(values[index]);
+    }
+    return converted;
+  }
+
+  static final class DataTypeSerializer extends JsonSerializer<DataType> {
+
+    @Override
+    public void serialize(DataType value, JsonGenerator generator, 
SerializerProvider serializers)
+        throws IOException {
+      generator.writeString(dataTypeName(value));
+    }
+  }
+
+  static final class DataTypeDeserializer extends JsonDeserializer<DataType> {
+
+    @Override
+    public DataType deserialize(JsonParser parser, DeserializationContext 
context)
+        throws IOException {
+      String value = requireString(parser, "DataType");
+      switch (value) {
+        case "String":
+          return DataType.STRING;
+        case "Integer":
+          return DataType.INTEGER;
+        case "Decimal":
+          return DataType.DECIMAL;
+        case "Float":
+          return DataType.FLOAT;
+        case "Boolean":
+          return DataType.BOOLEAN;
+        case "Date":
+          return DataType.DATE;
+        case "Time":
+          return DataType.TIME;
+        case "DateTime":
+          return DataType.DATE_TIME;
+        case "DateTimeTz":
+          return DataType.DATE_TIME_TZ;
+        case "Opaque":
+          return DataType.OPAQUE;
+        default:
+          throw JsonMappingException.from(parser, "Unknown Semantic Model data 
type: " + value);

Review Comment:
   When rejecting an unknown `DataType`, the message currently doesn’t help the 
caller recover. Consider including the list of supported values (e.g., `String, 
Integer, Decimal, ...`) in the exception message to improve usability of the 
persisted JSON format and reduce support/debug time.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to