okumin commented on code in PR #6244: URL: https://github.com/apache/hive/pull/6244#discussion_r2771829902
########## ql/src/java/org/apache/hadoop/hive/ql/stats/estimator/BranchingStatEstimator.java: ########## @@ -0,0 +1,50 @@ +/* + * 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.hadoop.hive.ql.stats.estimator; + +import java.util.List; +import java.util.Optional; + +import org.apache.hadoop.hive.ql.plan.ColStatistics; + +/** + * Base class for StatEstimators that handle branching expressions (CASE/WHEN, IF, COALESCE). + * Combines branch statistics using PessimisticStatCombiner and accounts for distinct constants. + */ +public abstract class BranchingStatEstimator implements StatEstimator { + protected final int numberOfDistinctConstants; + + protected BranchingStatEstimator(int numberOfDistinctConstants) { + this.numberOfDistinctConstants = numberOfDistinctConstants; + } + + @Override + public Optional<ColStatistics> estimate(List<ColStatistics> argStats) { + PessimisticStatCombiner combiner = new PessimisticStatCombiner(); + addBranchStats(combiner, argStats); + if (numberOfDistinctConstants > 1) { + ColStatistics constantsStat = new ColStatistics("_constants", "string"); + constantsStat.setCountDistint(numberOfDistinctConstants); + combiner.add(constantsStat); Review Comment: Can we make this a bit more explicit? Let's say Alice will update `PessimisticStatCombiner#add` in 1 year. It is not easy for her to guess that something might add a dummy ColStatistics instance. I guess we can either implement the logic directly in each UDF or add a utility method to PessimisticStatCombiner. ########## ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFCoalesce.java: ########## @@ -45,13 +48,16 @@ public class GenericUDFCoalesce extends GenericUDF implements StatEstimatorProvider { Review Comment: I'm guessing we don't need to update this UDF. That's because the number of distinct values of `coalesce(col_2, 'a', 'b', 'c', 'd')` should be 2 or 3, since the result is either `col_2` or 'a'. The original implementation might be more correct. -- 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]
