Copilot commented on code in PR #17177:
URL: https://github.com/apache/pinot/pull/17177#discussion_r2599796959


##########
pinot-broker/src/main/java/org/apache/pinot/broker/requesthandler/MultiStageBrokerRequestHandler.java:
##########
@@ -297,6 +304,19 @@ protected BrokerResponse handleRequestThrowing(long 
requestId, String query, Sql
       throws QueryException, WebApplicationException {
     _queryLogger.log(requestId, query);
 
+    String queryHash = CommonConstants.Broker.DEFAULT_QUERY_HASH;
+    if (_enableQueryFingerprinting) {
+      try {
+        QueryFingerprint queryFingerprint = 
QueryFingerprintUtils.generateFingerprint(sqlNodeAndOptions);
+        if (queryFingerprint != null) {
+          requestContext.setQueryFingerprint(queryFingerprint);
+          
LoggerConstants.QUERY_HASH_KEY.registerInMdc(queryFingerprint.getQueryHash());
+        }
+      } catch (Exception e) {
+        LOGGER.warn("Failed to generate query fingerprint for request {}: {}. 
{}", requestId, query, e.getMessage());
+      }
+    }

Review Comment:
   The `queryHash` variable is initialized to `DEFAULT_QUERY_HASH` but never 
updated when fingerprinting succeeds. The variable should be set to 
`queryFingerprint.getQueryHash()` when the fingerprint is successfully 
generated so it can be passed to the `QueryExecutionContext` constructor on 
line 334. Without this, the queryHash will always be empty even when 
fingerprinting is enabled.



##########
pinot-spi/src/main/java/org/apache/pinot/spi/query/QueryThreadContext.java:
##########
@@ -129,6 +133,9 @@ public void close() {
     THREAD_LOCAL.remove();
     LoggerConstants.REQUEST_ID_KEY.unregisterFromMdc();
     LoggerConstants.CORRELATION_ID_KEY.unregisterFromMdc();
+    if (_executionContext.getQueryHash() != null) {
+      LoggerConstants.QUERY_HASH_KEY.unregisterFromMdc();
+    }

Review Comment:
   The MDC cleanup logic is inconsistent with registration. The registration on 
line 69 checks `if (queryHash != null)` before registering, but unregistration 
checks `if (_executionContext.getQueryHash() != null)`. If `getQueryHash()` 
returns an empty string (the default), it will be non-null and registered in 
MDC, but this cleanup check will still pass and attempt to unregister. Consider 
checking for empty string as well, or better yet, always unregister 
unconditionally since `MDC.remove()` is safe to call even if the key doesn't 
exist.
   ```suggestion
       LoggerConstants.QUERY_HASH_KEY.unregisterFromMdc();
   ```



##########
pinot-common/src/main/java/org/apache/pinot/common/utils/request/QueryFingerprintUtils.java:
##########
@@ -0,0 +1,72 @@
+/**
+ * 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.pinot.common.utils.request;
+
+import com.google.common.hash.Hashing;
+import java.nio.charset.StandardCharsets;
+import javax.annotation.Nullable;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.dialect.AnsiSqlDialect;
+import org.apache.pinot.spi.trace.QueryFingerprint;
+import org.apache.pinot.sql.parsers.SqlNodeAndOptions;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class QueryFingerprintUtils {
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(QueryFingerprintUtils.class);
+
+  private QueryFingerprintUtils() {
+  }
+
+  @Nullable
+  public static QueryFingerprint generateFingerprint(SqlNodeAndOptions 
sqlNodeAndOptions) throws Exception {
+    if (sqlNodeAndOptions == null) {
+      return null;
+    }
+
+    SqlNode sqlNode = sqlNodeAndOptions.getSqlNode();
+    if (sqlNode == null) {
+      return null;
+    }
+
+    QueryFingerprintVisitor visitor = new QueryFingerprintVisitor();
+    SqlNode queryFingerprintNode = sqlNode.accept(visitor);
+
+    if (queryFingerprintNode == null) {
+      return null;
+    }
+
+    String fingerprint = queryFingerprintNode.toSqlString(c -> 
c.withDialect(AnsiSqlDialect.DEFAULT)).getSql();
+
+    if (fingerprint == null) {
+      return null;
+    }
+
+    // Normalize whitespace: replace newlines with spaces, collapse multiple 
spaces into one, and trim
+    fingerprint = fingerprint.replace("\n", " ").replaceAll("\\s+", " 
").trim();
+
+    return new QueryFingerprint(hashString(fingerprint), fingerprint);
+  }
+
+  private static String hashString(String input) {
+    return Hashing.farmHashFingerprint64()
+        .hashString(input, StandardCharsets.UTF_8)
+        .toString();
+  }

Review Comment:
   The `hashString` method uses `farmHashFingerprint64()` which produces a 
64-bit hash, but the PR description states that SHA-256 is used. This 
inconsistency between the documentation and implementation should be corrected. 
Either update the implementation to use SHA-256 as documented, or update the PR 
description to reflect that FarmHash is being used.



##########
pinot-broker/src/main/java/org/apache/pinot/broker/requesthandler/BaseSingleStageBrokerRequestHandler.java:
##########
@@ -329,6 +335,19 @@ protected BrokerResponse handleRequest(long requestId, 
String query, SqlNodeAndO
       throws Exception {
     _queryLogger.log(requestId, query);
 
+    String queryHash = CommonConstants.Broker.DEFAULT_QUERY_HASH;
+    if (_enableQueryFingerprinting) {
+      try {
+        QueryFingerprint queryFingerprint = 
QueryFingerprintUtils.generateFingerprint(sqlNodeAndOptions);
+        if (queryFingerprint != null) {
+          requestContext.setQueryFingerprint(queryFingerprint);
+          
LoggerConstants.QUERY_HASH_KEY.registerInMdc(queryFingerprint.getQueryHash());
+        }
+      } catch (Exception e) {
+        LOGGER.warn("Failed to generate query fingerprint for request {}: {}. 
{}", requestId, query, e.getMessage());
+      }
+    }

Review Comment:
   The `queryHash` variable is initialized to `DEFAULT_QUERY_HASH` but never 
updated when fingerprinting succeeds. The variable should be set to 
`queryFingerprint.getQueryHash()` when the fingerprint is successfully 
generated so it can be passed to the `QueryExecutionContext` constructor on 
line 362. This is the same issue as in `MultiStageBrokerRequestHandler`.



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to