Kikyou1997 commented on code in PR #13883: URL: https://github.com/apache/doris/pull/13883#discussion_r1011097672
########## fe/fe-core/src/main/java/org/apache/doris/statistics/StatisticUtil.java: ########## @@ -0,0 +1,209 @@ +// 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.doris.statistics; + +import org.apache.doris.analysis.Analyzer; +import org.apache.doris.analysis.BoolLiteral; +import org.apache.doris.analysis.DateLiteral; +import org.apache.doris.analysis.DecimalLiteral; +import org.apache.doris.analysis.FloatLiteral; +import org.apache.doris.analysis.IntLiteral; +import org.apache.doris.analysis.LargeIntLiteral; +import org.apache.doris.analysis.LiteralExpr; +import org.apache.doris.analysis.StatementBase; +import org.apache.doris.analysis.StringLiteral; +import org.apache.doris.analysis.UserIdentity; +import org.apache.doris.catalog.Env; +import org.apache.doris.catalog.PrimitiveType; +import org.apache.doris.catalog.ScalarType; +import org.apache.doris.catalog.Type; +import org.apache.doris.common.AnalysisException; +import org.apache.doris.common.Config; +import org.apache.doris.common.UserException; +import org.apache.doris.qe.ConnectContext; +import org.apache.doris.qe.SessionVariable; +import org.apache.doris.qe.StmtExecutor; +import org.apache.doris.statistics.util.InternalQueryResult.ResultRow; +import org.apache.doris.system.SystemInfoService; +import org.apache.doris.thrift.TUniqueId; + +import com.google.common.base.Preconditions; +import org.apache.thrift.TException; + +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; +import java.util.stream.Collectors; + +public class StatisticUtil { + + public static List<ResultRow> execStatisticQuery(String sql) { + ConnectContext connectContext = StatisticUtil.buildConnectContext(); + StmtExecutor stmtExecutor = new StmtExecutor(connectContext, sql); + connectContext.setExecutor(stmtExecutor); + return stmtExecutor.executeInternalQuery(); + } + + public static void execUpdate(String sql) throws Exception { + ConnectContext connectContext = StatisticUtil.buildConnectContext(); + StmtExecutor stmtExecutor = new StmtExecutor(connectContext, sql); + connectContext.setExecutor(stmtExecutor); + stmtExecutor.execute(); + } + + // TODO: finish this. + public static List<AnalysisJobInfo> deserializeToAnalysisJob(List<ResultRow> resultBatches) throws TException { + return new ArrayList<>(); + } + + public static List<ColumnStatistic> deserializeToColumnStatistics(List<ResultRow> resultBatches) + throws Exception { + return resultBatches.stream().map(ColumnStatistic::fromResultRow).collect(Collectors.toList()); + } + + public static ConnectContext buildConnectContext() { + ConnectContext connectContext = new ConnectContext(); + SessionVariable sessionVariable = connectContext.getSessionVariable(); + sessionVariable.internalSession = true; + sessionVariable.setMaxExecMemByte(Config.statistics_max_mem_per_query_in_bytes); + sessionVariable.setEnableInsertStrict(true); + sessionVariable.parallelExecInstanceNum = Config.statistic_parallel_exec_instance_num; + connectContext.setEnv(Env.getCurrentEnv()); + connectContext.setDatabase(StatisticConstants.STATISTIC_DB_NAME); + connectContext.setQualifiedUser(UserIdentity.ROOT.getQualifiedUser()); + connectContext.setCurrentUserIdentity(UserIdentity.ROOT); + UUID uuid = UUID.randomUUID(); + TUniqueId queryId = new TUniqueId(uuid.getMostSignificantBits(), uuid.getLeastSignificantBits()); + connectContext.setQueryId(queryId); + connectContext.setThreadLocalInfo(); + connectContext.setStartTime(); + connectContext.setCluster(SystemInfoService.DEFAULT_CLUSTER); + return connectContext; + } + + public static void analyze(StatementBase statementBase) throws UserException { + Analyzer analyzer = new Analyzer(Env.getCurrentEnv(), buildConnectContext()); + statementBase.analyze(analyzer); + } + + public static double convertToDouble(Type type, String columnValue) throws AnalysisException { Review Comment: This logic is completely same as the original definition in the ColumnStat -- 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: commits-unsubscr...@doris.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org