jerryshao commented on code in PR #14145:
URL: https://github.com/apache/gravitino/pull/14145#discussion_r4408564378


##########
clients/client-java/src/main/java/org/apache/gravitino/client/GenericSemanticModelMetric.java:
##########
@@ -0,0 +1,44 @@
+/*
+ * 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.client;
+
+import org.apache.gravitino.Namespace;
+import org.apache.gravitino.semantic.Metric;
+import org.apache.gravitino.semantic.SemanticModelMetric;
+
+/** A decoded metric with a catalog-relative namespace. */
+final class GenericSemanticModelMetric implements SemanticModelMetric {

Review Comment:
   [Important] `GenericSemanticModelMetric` has no `equals`, `hashCode` or 
`toString`, unlike every other client-side value type: `GenericColumn`, 
`GenericFileset`, `GenericFunction`, `GenericModel`, `GenericModelVersion`, 
`GenericPolicy`, `GenericSchema`, `GenericSemanticModel` (equals at 
GenericSemanticModel.java:187), `GenericTag`, `GenericTopic` and `GenericView` 
all define them, and the wire type `SemanticModelMetricDTO` carries Lombok 
`@EqualsAndHashCode`/`@ToString` 
(common/src/main/java/org/apache/gravitino/dto/semantic/SemanticModelMetricDTO.java:31-32).
   
   Because this is the object users get back from the public `listMetricInfos`, 
two calls that return the same metric produce objects that are not equal: 
`Arrays.asList(infos).contains(expected)`, deduplicating into a `Set`, or a 
plain `assertEquals` on the returned element all fall back to identity, and a 
log line prints `GenericSemanticModelMetric@1f2b3c`. The PR's own tests have to 
work around it by comparing `namespace()` and `metric()` separately 
(SemanticModelIT.java:1344-1357 and TestSemanticModelCatalog.java:190-192).
   
   Suggestion: add `equals`/`hashCode`/`toString` over the two fields, or drop 
this class and return `new 
SemanticModelMetricDTO(relativeMetricNamespace(info.namespace()), 
MetricDTO.fromMetric(info.metric()))`, whose constructor already takes 
`(Namespace, MetricDTO)` and brings value semantics with it.
   
   Verified by: read the new file in full in this run; `grep -ln "public 
boolean equals" 
clients/client-java/src/main/java/org/apache/gravitino/client/Generic*.java` 
lists all the siblings above and not this one.



##########
clients/client-java/src/test/java/org/apache/gravitino/client/TestSemanticModelCatalog.java:
##########
@@ -148,6 +154,146 @@ public void 
testListSemanticModelsUsesShortClientIdentifiers() throws JsonProces
         actual);
   }
 
+  @ParameterizedTest
+  @ValueSource(booleans = {false, true})
+  void testMetricDiscoveryPreservesNamespacesDefinitionsAndOrder(boolean 
details)
+      throws JsonProcessingException {
+    String schema = "schema /%? 收入";
+    String path = metricPath(schema);
+    Metric metric = definition().metrics()[0];
+    NameIdentifier[] fullIdentifiers = {
+      NameIdentifier.of(Namespace.of(METALAKE_NAME, CATALOG_NAME, schema, 
"a"), metric.name()),
+      NameIdentifier.of(Namespace.of(METALAKE_NAME, CATALOG_NAME, schema, 
"a-b"), metric.name())
+    };
+    SemanticModelMetricDTO[] infos =
+        Arrays.stream(fullIdentifiers)
+            .map(
+                ident ->
+                    new SemanticModelMetricDTO(ident.namespace(), 
MetricDTO.fromMetric(metric)))
+            .toArray(SemanticModelMetricDTO[]::new);
+    buildMockResource(
+        Method.GET,
+        path,
+        Map.of("details", String.valueOf(details)),
+        null,
+        details ? new MetricInfoListResponse(infos) : new 
EntityListResponse(fullIdentifiers),
+        SC_OK);
+    int requestsBefore = mockServer.retrieveRecordedRequests(request()).length;
+    NameIdentifier[] expected = {
+      NameIdentifier.of(Namespace.of(schema, "a"), metric.name()),
+      NameIdentifier.of(Namespace.of(schema, "a-b"), metric.name())
+    };
+    if (details) {
+      SemanticModelMetric[] actual =
+          
catalog.asSemanticModelCatalog().listMetricInfos(Namespace.of(schema));
+      assertEquals(expected.length, actual.length);
+      for (int i = 0; i < actual.length; i++) {
+        assertEquals(expected[i].namespace(), actual[i].namespace());
+        assertEquals(metric, actual[i].metric());
+        String[] levels = actual[i].namespace().levels();
+        levels[0] = "changed";

Review Comment:
   [Nit] This immutability assertion cannot fail, whatever the client does. 
`Namespace.levels()` returns `Arrays.copyOf(levels, levels.length)` 
(api/src/main/java/org/apache/gravitino/Namespace.java:98-100), so `levels[0] = 
"changed"` mutates a throwaway copy and the following `assertEquals` re-reads 
an untouched `Namespace`. It would still pass if `GenericSemanticModelMetric` 
leaked mutable state.
   
   Either drop these three lines, or, if the intent is that the client cannot 
be corrupted by the caller, assert something the client controls, for example 
that `listMetricInfos` returns a fresh array on each call, or that the 
per-element `metric()` is not the same instance the response DTO holds.
   
   Verified by: read `Namespace.levels()` in this run; the copy is 
unconditional.



-- 
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