MeihanLi commented on code in PR #17177: URL: https://github.com/apache/pinot/pull/17177#discussion_r2625629818
########## 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: Good catch! The current usage in QueryFingerprintUtils already creates a new instance for each fingerprint generation, so it's safe. I've added documentation to the class to clarify that a new instance should be created for each query fingerprint. -- 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]
