001package gudusoft.gsqlparser.dlineage.statistics; 002 003import gudusoft.gsqlparser.nodes.TParseTreeVisitor; 004import gudusoft.gsqlparser.nodes.TFunctionCall; 005 006import java.util.Arrays; 007import java.util.HashSet; 008import java.util.Set; 009 010/** 011 * 聚合函数访问者 012 */ 013public class AggregateFunctionVisitor extends TParseTreeVisitor { 014 private int aggregateFunctionCount = 0; 015 private static final Set<String> AGGREGATE_FUNCTIONS = new HashSet<>(Arrays.asList( 016 // 基本聚合函数 017 "AVG", "COUNT", "MAX", "MIN", "SUM", 018 019 // 统计聚合函数 020 "CORR", "COVAR_POP", "COVAR_SAMP", "MEDIAN", "STDDEV", "STDDEV_POP", "STDDEV_SAMP", 021 "VAR_POP", "VAR_SAMP", "VARIANCE", "STATS_MODE", 022 023 // 位聚合函数 024 "BIT_AND", "BIT_OR", "BIT_XOR", 025 026 // 字符串聚合函数 027 "LISTAGG", "STRING_AGG", 028 029 // JSON聚合函数 030 "JSON_ARRAYAGG", "JSON_OBJECTAGG", 031 032 // XML聚合函数 033 "XMLAGG", "SYS_XMLAGG", 034 035 // 数组聚合函数 036 "ARRAY_AGG", "ARRAY_UNIQUE_AGG", "COLLECT", 037 038 // 布尔聚合函数 039 "ANY", "SOME", "EVERY", "BOOL_AND", "BOOL_OR", 040 041 // 近似聚合函数 042 "APPROX_COUNT_DISTINCT", "HLL_COUNT", "HLL_COUNT_DISTINCT", "APPROX_PERCENTILE" 043 )); 044 045 public void preVisit(TFunctionCall node) { 046 if (isAggregateFunction(node)) { 047 aggregateFunctionCount++; 048 } 049 } 050 051 public boolean isAggregateFunction(TFunctionCall func) { 052 if (func == null || func.getFunctionName() == null) { 053 return false; 054 } 055 return AGGREGATE_FUNCTIONS.contains(func.getFunctionName().toString().toUpperCase()); 056 } 057 058 public int getAggregateFunctionCount() { 059 return aggregateFunctionCount; 060 } 061 062 public void reset() { 063 aggregateFunctionCount = 0; 064 } 065}