Copilot commented on code in PR #17177: URL: https://github.com/apache/pinot/pull/17177#discussion_r2618392546
########## pinot-common/src/main/java/org/apache/pinot/common/utils/request/QueryFingerprintVisitor.java: ########## @@ -0,0 +1,307 @@ +/** + * 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 java.util.ArrayList; +import java.util.List; +import javax.annotation.Nullable; +import org.apache.calcite.sql.SqlCall; +import org.apache.calcite.sql.SqlDynamicParam; +import org.apache.calcite.sql.SqlJoin; +import org.apache.calcite.sql.SqlLiteral; +import org.apache.calcite.sql.SqlNode; +import org.apache.calcite.sql.SqlNodeList; +import org.apache.calcite.sql.SqlOrderBy; +import org.apache.calcite.sql.SqlSelect; +import org.apache.calcite.sql.SqlWith; +import org.apache.calcite.sql.SqlWithItem; +import org.apache.calcite.sql.fun.SqlCase; +import org.apache.calcite.sql.type.SqlTypeName; +import org.apache.calcite.sql.util.SqlShuttle; + + +/** + * QueryFingerprintVisitor traverses the Calcite SqlNode AST and produces a normalized query fingerprint. + * Implementation is based on Calcite 1.40.0 version. It may change in future versions of Calcite. + * + * <p> + * <ul> + * <li>All data literals are replaced with dynamic parameters (?)</li> + * <li>IN/NOT IN clauses with multiple constants are squashed to a single parameter.</li> + * <li>EXPLAIN PLAN FOR is NOT preserved.</li> + * <li>Symbolic keywords (DISTINCT, ASC, DESC, etc.) and NULL literals are preserved.</li> + * <li>Hints are preserved.</li> + * <li>Window functions: window specification (operand 1) are preserved.</li> + * </ul> + * </p> + */ +public class QueryFingerprintVisitor extends SqlShuttle { + // SqlSelect operand index for hints, see {@link org.apache.calcite.sql.SqlSelect}. + private static final int SQLSELECT_HINTS_OPERAND_INDEX = 11; + + // SqlJoin operand indices, see {@link org.apache.calcite.sql.SqlJoin}. + private static final int SQLJOIN_NATURAL_OPERAND_INDEX = 1; + private static final int SQLJOIN_JOIN_TYPE_OPERAND_INDEX = 2; + private static final int SQLJOIN_CONDITION_TYPE_OPERAND_INDEX = 4; + + private int _dynamicParamIndex = 0; + + @Override Review Comment: The `_dynamicParamIndex` field is never reset between query fingerprints. If the same visitor instance is reused for multiple queries, the dynamic parameter indices will continue incrementing. Consider either documenting that each visitor instance should only be used once, or resetting `_dynamicParamIndex` at the start of the visit process. ```suggestion @Override public SqlNode visit(SqlNode node) { _dynamicParamIndex = 0; return super.visit(node); } @Override ``` ########## pinot-broker/src/main/java/org/apache/pinot/broker/requesthandler/MultiStageBrokerRequestHandler.java: ########## @@ -311,11 +331,11 @@ protected BrokerResponse handleRequestThrowing(long requestId, String query, Sql QueryExecutionContext executionContext = new QueryExecutionContext(QueryExecutionContext.QueryType.MSE, requestId, cid, workloadName, startTimeMs, - activeDeadlineMs, passiveDeadlineMs, _brokerId, _brokerId); + activeDeadlineMs, passiveDeadlineMs, _brokerId, _brokerId, queryHash); QueryThreadContext.MseWorkerInfo mseWorkerInfo = new QueryThreadContext.MseWorkerInfo(0, 0); try (QueryThreadContext ignore = QueryThreadContext.open(executionContext, mseWorkerInfo, _threadAccountant); Review Comment: The queryHash registered in MDC at line 313 is never unregistered after the request completes. This creates a resource leak where MDC entries accumulate. Since QueryThreadContext is responsible for MDC cleanup (as seen in its close() method), and executionContext now contains the queryHash, the MDC registration should occur inside the try-with-resources block where QueryThreadContext.open is called, not before it. ########## pinot-broker/src/main/java/org/apache/pinot/broker/requesthandler/BaseSingleStageBrokerRequestHandler.java: ########## @@ -340,7 +359,8 @@ protected BrokerResponse handleRequest(long requestId, String query, SqlNodeAndO // TODO: Revisit whether we should set deadline here QueryExecutionContext executionContext = new QueryExecutionContext(QueryExecutionContext.QueryType.SSE, requestId, cid, workloadName, - requestContext.getRequestArrivalTimeMillis(), Long.MAX_VALUE, Long.MAX_VALUE, _brokerId, _brokerId); + requestContext.getRequestArrivalTimeMillis(), Long.MAX_VALUE, Long.MAX_VALUE, _brokerId, _brokerId, + queryHash); try (QueryThreadContext ignore = QueryThreadContext.open(executionContext, _threadAccountant)) { Review Comment: The queryHash registered in MDC at line 344 is never unregistered after the request completes. This creates a resource leak where MDC entries accumulate. Since QueryThreadContext is responsible for MDC cleanup (as seen in its close() method), and executionContext now contains the queryHash, the MDC registration should occur inside the try-with-resources block where QueryThreadContext.open is called, not before it. -- 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]
