@Test public void testGetAggregationFunction() { FunctionContext function = getFunction("CoUnT"); AggregationFunction aggregationFunction = AggregationFunctionFactory.getAggregationFunction(function, DUMMY_QUERY_CONTEXT); assertTrue(aggregationFunction instanceof CountAggregationFunction); assertEquals(aggregationFunction.getType(), AggregationFunctionType.COUNT); assertEquals(aggregationFunction.getColumnName(), "count_star"); assertEquals(aggregationFunction.getResultColumnName(), function.toString()); function = getFunction("MiN"); aggregationFunction = AggregationFunctionFactory.getAggregationFunction(function, DUMMY_QUERY_CONTEXT); assertTrue(aggregationFunction instanceof MinAggregationFunction); assertEquals(aggregationFunction.getType(), AggregationFunctionType.MIN); assertEquals(aggregationFunction.getColumnName(), "min_column"); assertEquals(aggregationFunction.getResultColumnName(), function.toString()); function = getFunction("MaX"); aggregationFunction = AggregationFunctionFactory.getAggregationFunction(function, DUMMY_QUERY_CONTEXT); assertTrue(aggregationFunction instanceof MaxAggregationFunction); assertEquals(aggregationFunction.getType(), AggregationFunctionType.MAX); assertEquals(aggregationFunction.getColumnName(), "max_column"); assertEquals(aggregationFunction.getResultColumnName(), function.toString()); function = getFunction("SuM"); aggregationFunction = AggregationFunctionFactory.getAggregationFunction(function, DUMMY_QUERY_CONTEXT); assertTrue(aggregationFunction instanceof SumAggregationFunction); assertEquals(aggregationFunction.getType(), AggregationFunctionType.SUM); assertEquals(aggregationFunction.getColumnName(), "sum_column"); assertEquals(aggregationFunction.getResultColumnName(), function.toString()); function = getFunction("SuMPreCIsiON"); aggregationFunction = AggregationFunctionFactory.getAggregationFunction(function, DUMMY_QUERY_CONTEXT); assertTrue(aggregationFunction instanceof SumPrecisionAggregationFunction); assertEquals(aggregationFunction.getType(), AggregationFunctionType.SUMPRECISION); assertEquals(aggregationFunction.getColumnName(), "sumPrecision_column"); assertEquals(aggregationFunction.getResultColumnName(), function.toString()); function = getFunction("AvG"); aggregationFunction = AggregationFunctionFactory.getAggregationFunction(function, DUMMY_QUERY_CONTEXT); assertTrue(aggregationFunction instanceof AvgAggregationFunction); assertEquals(aggregationFunction.getType(), AggregationFunctionType.AVG); assertEquals(aggregationFunction.getColumnName(), "avg_column"); assertEquals(aggregationFunction.getResultColumnName(), function.toString()); function = getFunction("MiNmAxRaNgE"); aggregationFunction = AggregationFunctionFactory.getAggregationFunction(function, DUMMY_QUERY_CONTEXT); assertTrue(aggregationFunction instanceof MinMaxRangeAggregationFunction); assertEquals(aggregationFunction.getType(), AggregationFunctionType.MINMAXRANGE); assertEquals(aggregationFunction.getColumnName(), "minMaxRange_column"); assertEquals(aggregationFunction.getResultColumnName(), function.toString()); function = getFunction("DiStInCtCoUnT"); aggregationFunction = AggregationFunctionFactory.getAggregationFunction(function, DUMMY_QUERY_CONTEXT); assertTrue(aggregationFunction instanceof DistinctCountAggregationFunction); assertEquals(aggregationFunction.getType(), AggregationFunctionType.DISTINCTCOUNT); assertEquals(aggregationFunction.getColumnName(), "distinctCount_column"); assertEquals(aggregationFunction.getResultColumnName(), function.toString()); function = getFunction("DiStInCtCoUnThLl"); aggregationFunction = AggregationFunctionFactory.getAggregationFunction(function, DUMMY_QUERY_CONTEXT); assertTrue(aggregationFunction instanceof DistinctCountHLLAggregationFunction); assertEquals(aggregationFunction.getType(), AggregationFunctionType.DISTINCTCOUNTHLL); assertEquals(aggregationFunction.getColumnName(), "distinctCountHLL_column"); assertEquals(aggregationFunction.getResultColumnName(), function.toString()); function = getFunction("DiStInCtCoUnTrAwHlL"); aggregationFunction = AggregationFunctionFactory.getAggregationFunction(function, DUMMY_QUERY_CONTEXT); assertTrue(aggregationFunction instanceof DistinctCountRawHLLAggregationFunction); assertEquals(aggregationFunction.getType(), AggregationFunctionType.DISTINCTCOUNTRAWHLL); assertEquals(aggregationFunction.getColumnName(), "distinctCountRawHLL_column"); assertEquals(aggregationFunction.getResultColumnName(), function.toString()); function = getFunction("FaStHlL"); aggregationFunction = AggregationFunctionFactory.getAggregationFunction(function, DUMMY_QUERY_CONTEXT); assertTrue(aggregationFunction instanceof FastHLLAggregationFunction); assertEquals(aggregationFunction.getType(), AggregationFunctionType.FASTHLL); assertEquals(aggregationFunction.getColumnName(), "fastHLL_column"); assertEquals(aggregationFunction.getResultColumnName(), function.toString()); function = getFunction("PeRcEnTiLe5"); aggregationFunction = AggregationFunctionFactory.getAggregationFunction(function, DUMMY_QUERY_CONTEXT); assertTrue(aggregationFunction instanceof PercentileAggregationFunction); assertEquals(aggregationFunction.getType(), AggregationFunctionType.PERCENTILE); assertEquals(aggregationFunction.getColumnName(), "percentile5_column"); assertEquals(aggregationFunction.getResultColumnName(), function.toString()); function = getFunction("PeRcEnTiLeEsT50"); aggregationFunction = AggregationFunctionFactory.getAggregationFunction(function, DUMMY_QUERY_CONTEXT); assertTrue(aggregationFunction instanceof PercentileEstAggregationFunction); assertEquals(aggregationFunction.getType(), AggregationFunctionType.PERCENTILEEST); assertEquals(aggregationFunction.getColumnName(), "percentileEst50_column"); assertEquals(aggregationFunction.getResultColumnName(), function.toString()); function = getFunction("PeRcEnTiLeTdIgEsT99"); aggregationFunction = AggregationFunctionFactory.getAggregationFunction(function, DUMMY_QUERY_CONTEXT); assertTrue(aggregationFunction instanceof PercentileTDigestAggregationFunction); assertEquals(aggregationFunction.getType(), AggregationFunctionType.PERCENTILETDIGEST); assertEquals(aggregationFunction.getColumnName(), "percentileTDigest99_column"); assertEquals(aggregationFunction.getResultColumnName(), function.toString()); function = getFunction("CoUnTmV"); aggregationFunction = AggregationFunctionFactory.getAggregationFunction(function, DUMMY_QUERY_CONTEXT); assertTrue(aggregationFunction instanceof CountMVAggregationFunction); assertEquals(aggregationFunction.getType(), AggregationFunctionType.COUNTMV); assertEquals(aggregationFunction.getColumnName(), "countMV_column"); assertEquals(aggregationFunction.getResultColumnName(), function.toString()); function = getFunction("MiNmV"); aggregationFunction = AggregationFunctionFactory.getAggregationFunction(function, DUMMY_QUERY_CONTEXT); assertTrue(aggregationFunction instanceof MinMVAggregationFunction); assertEquals(aggregationFunction.getType(), AggregationFunctionType.MINMV); assertEquals(aggregationFunction.getColumnName(), "minMV_column"); assertEquals(aggregationFunction.getResultColumnName(), function.toString()); function = getFunction("MaXmV"); aggregationFunction = AggregationFunctionFactory.getAggregationFunction(function, DUMMY_QUERY_CONTEXT); assertTrue(aggregationFunction instanceof MaxMVAggregationFunction); assertEquals(aggregationFunction.getType(), AggregationFunctionType.MAXMV); assertEquals(aggregationFunction.getColumnName(), "maxMV_column"); assertEquals(aggregationFunction.getResultColumnName(), function.toString()); function = getFunction("SuMmV"); aggregationFunction = AggregationFunctionFactory.getAggregationFunction(function, DUMMY_QUERY_CONTEXT); assertTrue(aggregationFunction instanceof SumMVAggregationFunction); assertEquals(aggregationFunction.getType(), AggregationFunctionType.SUMMV); assertEquals(aggregationFunction.getColumnName(), "sumMV_column"); assertEquals(aggregationFunction.getResultColumnName(), function.toString()); function = getFunction("AvGmV"); aggregationFunction = AggregationFunctionFactory.getAggregationFunction(function, DUMMY_QUERY_CONTEXT); assertTrue(aggregationFunction instanceof AvgMVAggregationFunction); assertEquals(aggregationFunction.getType(), AggregationFunctionType.AVGMV); assertEquals(aggregationFunction.getColumnName(), "avgMV_column"); assertEquals(aggregationFunction.getResultColumnName(), function.toString()); function = getFunction("AvG_mV"); aggregationFunction = AggregationFunctionFactory.getAggregationFunction(function, DUMMY_QUERY_CONTEXT); assertTrue(aggregationFunction instanceof AvgMVAggregationFunction); assertEquals(aggregationFunction.getType(), AggregationFunctionType.AVGMV); assertEquals(aggregationFunction.getColumnName(), "avgMV_column"); assertEquals(aggregationFunction.getResultColumnName(), "avgmv(column)"); function = getFunction("MiNmAxRaNgEmV"); aggregationFunction = AggregationFunctionFactory.getAggregationFunction(function, DUMMY_QUERY_CONTEXT); assertTrue(aggregationFunction instanceof MinMaxRangeMVAggregationFunction); assertEquals(aggregationFunction.getType(), AggregationFunctionType.MINMAXRANGEMV); assertEquals(aggregationFunction.getColumnName(), "minMaxRangeMV_column"); assertEquals(aggregationFunction.getResultColumnName(), function.toString()); function = getFunction("DiStInCtCoUnTmV"); aggregationFunction = AggregationFunctionFactory.getAggregationFunction(function, DUMMY_QUERY_CONTEXT); assertTrue(aggregationFunction instanceof DistinctCountMVAggregationFunction); assertEquals(aggregationFunction.getType(), AggregationFunctionType.DISTINCTCOUNTMV); assertEquals(aggregationFunction.getColumnName(), "distinctCountMV_column"); assertEquals(aggregationFunction.getResultColumnName(), function.toString()); function = getFunction("DiStInCtCoUnThLlMv"); aggregationFunction = AggregationFunctionFactory.getAggregationFunction(function, DUMMY_QUERY_CONTEXT); assertTrue(aggregationFunction instanceof DistinctCountHLLMVAggregationFunction); assertEquals(aggregationFunction.getType(), AggregationFunctionType.DISTINCTCOUNTHLLMV); assertEquals(aggregationFunction.getColumnName(), "distinctCountHLLMV_column"); assertEquals(aggregationFunction.getResultColumnName(), function.toString()); function = getFunction("DiStInCt_CoUnT_hLl_Mv"); aggregationFunction = AggregationFunctionFactory.getAggregationFunction(function, DUMMY_QUERY_CONTEXT); assertTrue(aggregationFunction instanceof DistinctCountHLLMVAggregationFunction); assertEquals(aggregationFunction.getType(), AggregationFunctionType.DISTINCTCOUNTHLLMV); assertEquals(aggregationFunction.getColumnName(), "distinctCountHLLMV_column"); assertEquals(aggregationFunction.getResultColumnName(), "distinctcounthllmv(column)"); function = getFunction("DiStInCtCoUnTrAwHlLmV"); aggregationFunction = AggregationFunctionFactory.getAggregationFunction(function, DUMMY_QUERY_CONTEXT); assertTrue(aggregationFunction instanceof DistinctCountRawHLLMVAggregationFunction); assertEquals(aggregationFunction.getType(), AggregationFunctionType.DISTINCTCOUNTRAWHLLMV); assertEquals(aggregationFunction.getColumnName(), "distinctCountRawHLLMV_column"); assertEquals(aggregationFunction.getResultColumnName(), function.toString()); function = getFunction("PeRcEnTiLe10Mv"); aggregationFunction = AggregationFunctionFactory.getAggregationFunction(function, DUMMY_QUERY_CONTEXT); assertTrue(aggregationFunction instanceof PercentileMVAggregationFunction); assertEquals(aggregationFunction.getType(), AggregationFunctionType.PERCENTILEMV); assertEquals(aggregationFunction.getColumnName(), "percentile10MV_column"); assertEquals(aggregationFunction.getResultColumnName(), function.toString()); function = getFunction("PeRcEnTiLeEsT90mV"); aggregationFunction = AggregationFunctionFactory.getAggregationFunction(function, DUMMY_QUERY_CONTEXT); assertTrue(aggregationFunction instanceof PercentileEstMVAggregationFunction); assertEquals(aggregationFunction.getType(), AggregationFunctionType.PERCENTILEESTMV); assertEquals(aggregationFunction.getColumnName(), "percentileEst90MV_column"); assertEquals(aggregationFunction.getResultColumnName(), function.toString()); function = getFunction("PeRcEnTiLeTdIgEsT95mV"); aggregationFunction = AggregationFunctionFactory.getAggregationFunction(function, DUMMY_QUERY_CONTEXT); assertTrue(aggregationFunction instanceof PercentileTDigestMVAggregationFunction); assertEquals(aggregationFunction.getType(), AggregationFunctionType.PERCENTILETDIGESTMV); assertEquals(aggregationFunction.getColumnName(), "percentileTDigest95MV_column"); assertEquals(aggregationFunction.getResultColumnName(), function.toString()); function = getFunction("PeRcEnTiLe_TdIgEsT_95_mV"); aggregationFunction = AggregationFunctionFactory.getAggregationFunction(function, DUMMY_QUERY_CONTEXT); assertTrue(aggregationFunction instanceof PercentileTDigestMVAggregationFunction); assertEquals(aggregationFunction.getType(), AggregationFunctionType.PERCENTILETDIGESTMV); assertEquals(aggregationFunction.getColumnName(), "percentileTDigest95MV_column"); assertEquals(aggregationFunction.getResultColumnName(), "percentiletdigest95mv(column)"); }
public static AggregationFunction getAggregationFunction(FunctionContext function, QueryContext queryContext) { try { String upperCaseFunctionName = StringUtils.remove(function.getFunctionName(), '_').toUpperCase(); List<ExpressionContext> arguments = function.getArguments(); ExpressionContext firstArgument = arguments.get(0); if (upperCaseFunctionName.startsWith("PERCENTILE")) { String remainingFunctionName = upperCaseFunctionName.substring(10); int numArguments = arguments.size(); if (numArguments == 1) { if (remainingFunctionName.matches("\\d+")) { return new PercentileAggregationFunction(firstArgument, parsePercentile(remainingFunctionName)); } else if (remainingFunctionName.matches("EST\\d+")) { String percentileString = remainingFunctionName.substring(3); return new PercentileEstAggregationFunction(firstArgument, parsePercentile(percentileString)); } else if (remainingFunctionName.matches("TDIGEST\\d+")) { String percentileString = remainingFunctionName.substring(7); return new PercentileTDigestAggregationFunction(firstArgument, parsePercentile(percentileString)); } else if (remainingFunctionName.matches("\\d+MV")) { String percentileString = remainingFunctionName.substring(0, remainingFunctionName.length() - 2); return new PercentileMVAggregationFunction(firstArgument, parsePercentile(percentileString)); } else if (remainingFunctionName.matches("EST\\d+MV")) { String percentileString = remainingFunctionName.substring(3, remainingFunctionName.length() - 2); return new PercentileEstMVAggregationFunction(firstArgument, parsePercentile(percentileString)); } else if (remainingFunctionName.matches("TDIGEST\\d+MV")) { String percentileString = remainingFunctionName.substring(7, remainingFunctionName.length() - 2); return new PercentileTDigestMVAggregationFunction(firstArgument, parsePercentile(percentileString)); } } else if (numArguments == 2) { int percentile = parsePercentile(arguments.get(1).getLiteral()); if (remainingFunctionName.isEmpty()) { return new PercentileAggregationFunction(firstArgument, percentile); } if (remainingFunctionName.equals("EST")) { return new PercentileEstAggregationFunction(firstArgument, percentile); } if (remainingFunctionName.equals("TDIGEST")) { return new PercentileTDigestAggregationFunction(firstArgument, percentile); } if (remainingFunctionName.equals("MV")) { return new PercentileMVAggregationFunction(firstArgument, percentile); } if (remainingFunctionName.equals("ESTMV")) { return new PercentileEstMVAggregationFunction(firstArgument, percentile); } if (remainingFunctionName.equals("TDIGESTMV")) { return new PercentileTDigestMVAggregationFunction(firstArgument, percentile); } } throw new IllegalArgumentException("Invalid percentile function: " + function); } else { switch (AggregationFunctionType.valueOf(upperCaseFunctionName)) { case COUNT: return new CountAggregationFunction(); case MIN: return new MinAggregationFunction(firstArgument); case MAX: return new MaxAggregationFunction(firstArgument); case SUM: return new SumAggregationFunction(firstArgument); case SUMPRECISION: return new SumPrecisionAggregationFunction(arguments); case AVG: return new AvgAggregationFunction(firstArgument); case MINMAXRANGE: return new MinMaxRangeAggregationFunction(firstArgument); case DISTINCTCOUNT: return new DistinctCountAggregationFunction(firstArgument); case DISTINCTCOUNTBITMAP: return new DistinctCountBitmapAggregationFunction(firstArgument); case SEGMENTPARTITIONEDDISTINCTCOUNT: return new SegmentPartitionedDistinctCountAggregationFunction(firstArgument); case DISTINCTCOUNTHLL: return new DistinctCountHLLAggregationFunction(arguments); case DISTINCTCOUNTRAWHLL: return new DistinctCountRawHLLAggregationFunction(arguments); case FASTHLL: return new FastHLLAggregationFunction(firstArgument); case DISTINCTCOUNTTHETASKETCH: return new DistinctCountThetaSketchAggregationFunction(arguments); case DISTINCTCOUNTRAWTHETASKETCH: return new DistinctCountRawThetaSketchAggregationFunction(arguments); case IDSET: return new IdSetAggregationFunction(arguments); case COUNTMV: return new CountMVAggregationFunction(firstArgument); case MINMV: return new MinMVAggregationFunction(firstArgument); case MAXMV: return new MaxMVAggregationFunction(firstArgument); case SUMMV: return new SumMVAggregationFunction(firstArgument); case AVGMV: return new AvgMVAggregationFunction(firstArgument); case MINMAXRANGEMV: return new MinMaxRangeMVAggregationFunction(firstArgument); case DISTINCTCOUNTMV: return new DistinctCountMVAggregationFunction(firstArgument); case DISTINCTCOUNTBITMAPMV: return new DistinctCountBitmapMVAggregationFunction(firstArgument); case DISTINCTCOUNTHLLMV: return new DistinctCountHLLMVAggregationFunction(arguments); case DISTINCTCOUNTRAWHLLMV: return new DistinctCountRawHLLMVAggregationFunction(arguments); case DISTINCT: return new DistinctAggregationFunction(arguments, queryContext.getOrderByExpressions(), queryContext.getLimit()); case STUNION: return new StUnionAggregationFunction(firstArgument); default: throw new IllegalArgumentException(); } } } catch (Exception e) { throw new BadQueryRequestException("Invalid aggregation function: " + function); } }
AggregationFunctionFactory { public static AggregationFunction getAggregationFunction(FunctionContext function, QueryContext queryContext) { try { String upperCaseFunctionName = StringUtils.remove(function.getFunctionName(), '_').toUpperCase(); List<ExpressionContext> arguments = function.getArguments(); ExpressionContext firstArgument = arguments.get(0); if (upperCaseFunctionName.startsWith("PERCENTILE")) { String remainingFunctionName = upperCaseFunctionName.substring(10); int numArguments = arguments.size(); if (numArguments == 1) { if (remainingFunctionName.matches("\\d+")) { return new PercentileAggregationFunction(firstArgument, parsePercentile(remainingFunctionName)); } else if (remainingFunctionName.matches("EST\\d+")) { String percentileString = remainingFunctionName.substring(3); return new PercentileEstAggregationFunction(firstArgument, parsePercentile(percentileString)); } else if (remainingFunctionName.matches("TDIGEST\\d+")) { String percentileString = remainingFunctionName.substring(7); return new PercentileTDigestAggregationFunction(firstArgument, parsePercentile(percentileString)); } else if (remainingFunctionName.matches("\\d+MV")) { String percentileString = remainingFunctionName.substring(0, remainingFunctionName.length() - 2); return new PercentileMVAggregationFunction(firstArgument, parsePercentile(percentileString)); } else if (remainingFunctionName.matches("EST\\d+MV")) { String percentileString = remainingFunctionName.substring(3, remainingFunctionName.length() - 2); return new PercentileEstMVAggregationFunction(firstArgument, parsePercentile(percentileString)); } else if (remainingFunctionName.matches("TDIGEST\\d+MV")) { String percentileString = remainingFunctionName.substring(7, remainingFunctionName.length() - 2); return new PercentileTDigestMVAggregationFunction(firstArgument, parsePercentile(percentileString)); } } else if (numArguments == 2) { int percentile = parsePercentile(arguments.get(1).getLiteral()); if (remainingFunctionName.isEmpty()) { return new PercentileAggregationFunction(firstArgument, percentile); } if (remainingFunctionName.equals("EST")) { return new PercentileEstAggregationFunction(firstArgument, percentile); } if (remainingFunctionName.equals("TDIGEST")) { return new PercentileTDigestAggregationFunction(firstArgument, percentile); } if (remainingFunctionName.equals("MV")) { return new PercentileMVAggregationFunction(firstArgument, percentile); } if (remainingFunctionName.equals("ESTMV")) { return new PercentileEstMVAggregationFunction(firstArgument, percentile); } if (remainingFunctionName.equals("TDIGESTMV")) { return new PercentileTDigestMVAggregationFunction(firstArgument, percentile); } } throw new IllegalArgumentException("Invalid percentile function: " + function); } else { switch (AggregationFunctionType.valueOf(upperCaseFunctionName)) { case COUNT: return new CountAggregationFunction(); case MIN: return new MinAggregationFunction(firstArgument); case MAX: return new MaxAggregationFunction(firstArgument); case SUM: return new SumAggregationFunction(firstArgument); case SUMPRECISION: return new SumPrecisionAggregationFunction(arguments); case AVG: return new AvgAggregationFunction(firstArgument); case MINMAXRANGE: return new MinMaxRangeAggregationFunction(firstArgument); case DISTINCTCOUNT: return new DistinctCountAggregationFunction(firstArgument); case DISTINCTCOUNTBITMAP: return new DistinctCountBitmapAggregationFunction(firstArgument); case SEGMENTPARTITIONEDDISTINCTCOUNT: return new SegmentPartitionedDistinctCountAggregationFunction(firstArgument); case DISTINCTCOUNTHLL: return new DistinctCountHLLAggregationFunction(arguments); case DISTINCTCOUNTRAWHLL: return new DistinctCountRawHLLAggregationFunction(arguments); case FASTHLL: return new FastHLLAggregationFunction(firstArgument); case DISTINCTCOUNTTHETASKETCH: return new DistinctCountThetaSketchAggregationFunction(arguments); case DISTINCTCOUNTRAWTHETASKETCH: return new DistinctCountRawThetaSketchAggregationFunction(arguments); case IDSET: return new IdSetAggregationFunction(arguments); case COUNTMV: return new CountMVAggregationFunction(firstArgument); case MINMV: return new MinMVAggregationFunction(firstArgument); case MAXMV: return new MaxMVAggregationFunction(firstArgument); case SUMMV: return new SumMVAggregationFunction(firstArgument); case AVGMV: return new AvgMVAggregationFunction(firstArgument); case MINMAXRANGEMV: return new MinMaxRangeMVAggregationFunction(firstArgument); case DISTINCTCOUNTMV: return new DistinctCountMVAggregationFunction(firstArgument); case DISTINCTCOUNTBITMAPMV: return new DistinctCountBitmapMVAggregationFunction(firstArgument); case DISTINCTCOUNTHLLMV: return new DistinctCountHLLMVAggregationFunction(arguments); case DISTINCTCOUNTRAWHLLMV: return new DistinctCountRawHLLMVAggregationFunction(arguments); case DISTINCT: return new DistinctAggregationFunction(arguments, queryContext.getOrderByExpressions(), queryContext.getLimit()); case STUNION: return new StUnionAggregationFunction(firstArgument); default: throw new IllegalArgumentException(); } } } catch (Exception e) { throw new BadQueryRequestException("Invalid aggregation function: " + function); } } }
AggregationFunctionFactory { public static AggregationFunction getAggregationFunction(FunctionContext function, QueryContext queryContext) { try { String upperCaseFunctionName = StringUtils.remove(function.getFunctionName(), '_').toUpperCase(); List<ExpressionContext> arguments = function.getArguments(); ExpressionContext firstArgument = arguments.get(0); if (upperCaseFunctionName.startsWith("PERCENTILE")) { String remainingFunctionName = upperCaseFunctionName.substring(10); int numArguments = arguments.size(); if (numArguments == 1) { if (remainingFunctionName.matches("\\d+")) { return new PercentileAggregationFunction(firstArgument, parsePercentile(remainingFunctionName)); } else if (remainingFunctionName.matches("EST\\d+")) { String percentileString = remainingFunctionName.substring(3); return new PercentileEstAggregationFunction(firstArgument, parsePercentile(percentileString)); } else if (remainingFunctionName.matches("TDIGEST\\d+")) { String percentileString = remainingFunctionName.substring(7); return new PercentileTDigestAggregationFunction(firstArgument, parsePercentile(percentileString)); } else if (remainingFunctionName.matches("\\d+MV")) { String percentileString = remainingFunctionName.substring(0, remainingFunctionName.length() - 2); return new PercentileMVAggregationFunction(firstArgument, parsePercentile(percentileString)); } else if (remainingFunctionName.matches("EST\\d+MV")) { String percentileString = remainingFunctionName.substring(3, remainingFunctionName.length() - 2); return new PercentileEstMVAggregationFunction(firstArgument, parsePercentile(percentileString)); } else if (remainingFunctionName.matches("TDIGEST\\d+MV")) { String percentileString = remainingFunctionName.substring(7, remainingFunctionName.length() - 2); return new PercentileTDigestMVAggregationFunction(firstArgument, parsePercentile(percentileString)); } } else if (numArguments == 2) { int percentile = parsePercentile(arguments.get(1).getLiteral()); if (remainingFunctionName.isEmpty()) { return new PercentileAggregationFunction(firstArgument, percentile); } if (remainingFunctionName.equals("EST")) { return new PercentileEstAggregationFunction(firstArgument, percentile); } if (remainingFunctionName.equals("TDIGEST")) { return new PercentileTDigestAggregationFunction(firstArgument, percentile); } if (remainingFunctionName.equals("MV")) { return new PercentileMVAggregationFunction(firstArgument, percentile); } if (remainingFunctionName.equals("ESTMV")) { return new PercentileEstMVAggregationFunction(firstArgument, percentile); } if (remainingFunctionName.equals("TDIGESTMV")) { return new PercentileTDigestMVAggregationFunction(firstArgument, percentile); } } throw new IllegalArgumentException("Invalid percentile function: " + function); } else { switch (AggregationFunctionType.valueOf(upperCaseFunctionName)) { case COUNT: return new CountAggregationFunction(); case MIN: return new MinAggregationFunction(firstArgument); case MAX: return new MaxAggregationFunction(firstArgument); case SUM: return new SumAggregationFunction(firstArgument); case SUMPRECISION: return new SumPrecisionAggregationFunction(arguments); case AVG: return new AvgAggregationFunction(firstArgument); case MINMAXRANGE: return new MinMaxRangeAggregationFunction(firstArgument); case DISTINCTCOUNT: return new DistinctCountAggregationFunction(firstArgument); case DISTINCTCOUNTBITMAP: return new DistinctCountBitmapAggregationFunction(firstArgument); case SEGMENTPARTITIONEDDISTINCTCOUNT: return new SegmentPartitionedDistinctCountAggregationFunction(firstArgument); case DISTINCTCOUNTHLL: return new DistinctCountHLLAggregationFunction(arguments); case DISTINCTCOUNTRAWHLL: return new DistinctCountRawHLLAggregationFunction(arguments); case FASTHLL: return new FastHLLAggregationFunction(firstArgument); case DISTINCTCOUNTTHETASKETCH: return new DistinctCountThetaSketchAggregationFunction(arguments); case DISTINCTCOUNTRAWTHETASKETCH: return new DistinctCountRawThetaSketchAggregationFunction(arguments); case IDSET: return new IdSetAggregationFunction(arguments); case COUNTMV: return new CountMVAggregationFunction(firstArgument); case MINMV: return new MinMVAggregationFunction(firstArgument); case MAXMV: return new MaxMVAggregationFunction(firstArgument); case SUMMV: return new SumMVAggregationFunction(firstArgument); case AVGMV: return new AvgMVAggregationFunction(firstArgument); case MINMAXRANGEMV: return new MinMaxRangeMVAggregationFunction(firstArgument); case DISTINCTCOUNTMV: return new DistinctCountMVAggregationFunction(firstArgument); case DISTINCTCOUNTBITMAPMV: return new DistinctCountBitmapMVAggregationFunction(firstArgument); case DISTINCTCOUNTHLLMV: return new DistinctCountHLLMVAggregationFunction(arguments); case DISTINCTCOUNTRAWHLLMV: return new DistinctCountRawHLLMVAggregationFunction(arguments); case DISTINCT: return new DistinctAggregationFunction(arguments, queryContext.getOrderByExpressions(), queryContext.getLimit()); case STUNION: return new StUnionAggregationFunction(firstArgument); default: throw new IllegalArgumentException(); } } } catch (Exception e) { throw new BadQueryRequestException("Invalid aggregation function: " + function); } } private  AggregationFunctionFactory(); }
AggregationFunctionFactory { public static AggregationFunction getAggregationFunction(FunctionContext function, QueryContext queryContext) { try { String upperCaseFunctionName = StringUtils.remove(function.getFunctionName(), '_').toUpperCase(); List<ExpressionContext> arguments = function.getArguments(); ExpressionContext firstArgument = arguments.get(0); if (upperCaseFunctionName.startsWith("PERCENTILE")) { String remainingFunctionName = upperCaseFunctionName.substring(10); int numArguments = arguments.size(); if (numArguments == 1) { if (remainingFunctionName.matches("\\d+")) { return new PercentileAggregationFunction(firstArgument, parsePercentile(remainingFunctionName)); } else if (remainingFunctionName.matches("EST\\d+")) { String percentileString = remainingFunctionName.substring(3); return new PercentileEstAggregationFunction(firstArgument, parsePercentile(percentileString)); } else if (remainingFunctionName.matches("TDIGEST\\d+")) { String percentileString = remainingFunctionName.substring(7); return new PercentileTDigestAggregationFunction(firstArgument, parsePercentile(percentileString)); } else if (remainingFunctionName.matches("\\d+MV")) { String percentileString = remainingFunctionName.substring(0, remainingFunctionName.length() - 2); return new PercentileMVAggregationFunction(firstArgument, parsePercentile(percentileString)); } else if (remainingFunctionName.matches("EST\\d+MV")) { String percentileString = remainingFunctionName.substring(3, remainingFunctionName.length() - 2); return new PercentileEstMVAggregationFunction(firstArgument, parsePercentile(percentileString)); } else if (remainingFunctionName.matches("TDIGEST\\d+MV")) { String percentileString = remainingFunctionName.substring(7, remainingFunctionName.length() - 2); return new PercentileTDigestMVAggregationFunction(firstArgument, parsePercentile(percentileString)); } } else if (numArguments == 2) { int percentile = parsePercentile(arguments.get(1).getLiteral()); if (remainingFunctionName.isEmpty()) { return new PercentileAggregationFunction(firstArgument, percentile); } if (remainingFunctionName.equals("EST")) { return new PercentileEstAggregationFunction(firstArgument, percentile); } if (remainingFunctionName.equals("TDIGEST")) { return new PercentileTDigestAggregationFunction(firstArgument, percentile); } if (remainingFunctionName.equals("MV")) { return new PercentileMVAggregationFunction(firstArgument, percentile); } if (remainingFunctionName.equals("ESTMV")) { return new PercentileEstMVAggregationFunction(firstArgument, percentile); } if (remainingFunctionName.equals("TDIGESTMV")) { return new PercentileTDigestMVAggregationFunction(firstArgument, percentile); } } throw new IllegalArgumentException("Invalid percentile function: " + function); } else { switch (AggregationFunctionType.valueOf(upperCaseFunctionName)) { case COUNT: return new CountAggregationFunction(); case MIN: return new MinAggregationFunction(firstArgument); case MAX: return new MaxAggregationFunction(firstArgument); case SUM: return new SumAggregationFunction(firstArgument); case SUMPRECISION: return new SumPrecisionAggregationFunction(arguments); case AVG: return new AvgAggregationFunction(firstArgument); case MINMAXRANGE: return new MinMaxRangeAggregationFunction(firstArgument); case DISTINCTCOUNT: return new DistinctCountAggregationFunction(firstArgument); case DISTINCTCOUNTBITMAP: return new DistinctCountBitmapAggregationFunction(firstArgument); case SEGMENTPARTITIONEDDISTINCTCOUNT: return new SegmentPartitionedDistinctCountAggregationFunction(firstArgument); case DISTINCTCOUNTHLL: return new DistinctCountHLLAggregationFunction(arguments); case DISTINCTCOUNTRAWHLL: return new DistinctCountRawHLLAggregationFunction(arguments); case FASTHLL: return new FastHLLAggregationFunction(firstArgument); case DISTINCTCOUNTTHETASKETCH: return new DistinctCountThetaSketchAggregationFunction(arguments); case DISTINCTCOUNTRAWTHETASKETCH: return new DistinctCountRawThetaSketchAggregationFunction(arguments); case IDSET: return new IdSetAggregationFunction(arguments); case COUNTMV: return new CountMVAggregationFunction(firstArgument); case MINMV: return new MinMVAggregationFunction(firstArgument); case MAXMV: return new MaxMVAggregationFunction(firstArgument); case SUMMV: return new SumMVAggregationFunction(firstArgument); case AVGMV: return new AvgMVAggregationFunction(firstArgument); case MINMAXRANGEMV: return new MinMaxRangeMVAggregationFunction(firstArgument); case DISTINCTCOUNTMV: return new DistinctCountMVAggregationFunction(firstArgument); case DISTINCTCOUNTBITMAPMV: return new DistinctCountBitmapMVAggregationFunction(firstArgument); case DISTINCTCOUNTHLLMV: return new DistinctCountHLLMVAggregationFunction(arguments); case DISTINCTCOUNTRAWHLLMV: return new DistinctCountRawHLLMVAggregationFunction(arguments); case DISTINCT: return new DistinctAggregationFunction(arguments, queryContext.getOrderByExpressions(), queryContext.getLimit()); case STUNION: return new StUnionAggregationFunction(firstArgument); default: throw new IllegalArgumentException(); } } } catch (Exception e) { throw new BadQueryRequestException("Invalid aggregation function: " + function); } } private  AggregationFunctionFactory(); static AggregationFunction getAggregationFunction(FunctionContext function, QueryContext queryContext); }
AggregationFunctionFactory { public static AggregationFunction getAggregationFunction(FunctionContext function, QueryContext queryContext) { try { String upperCaseFunctionName = StringUtils.remove(function.getFunctionName(), '_').toUpperCase(); List<ExpressionContext> arguments = function.getArguments(); ExpressionContext firstArgument = arguments.get(0); if (upperCaseFunctionName.startsWith("PERCENTILE")) { String remainingFunctionName = upperCaseFunctionName.substring(10); int numArguments = arguments.size(); if (numArguments == 1) { if (remainingFunctionName.matches("\\d+")) { return new PercentileAggregationFunction(firstArgument, parsePercentile(remainingFunctionName)); } else if (remainingFunctionName.matches("EST\\d+")) { String percentileString = remainingFunctionName.substring(3); return new PercentileEstAggregationFunction(firstArgument, parsePercentile(percentileString)); } else if (remainingFunctionName.matches("TDIGEST\\d+")) { String percentileString = remainingFunctionName.substring(7); return new PercentileTDigestAggregationFunction(firstArgument, parsePercentile(percentileString)); } else if (remainingFunctionName.matches("\\d+MV")) { String percentileString = remainingFunctionName.substring(0, remainingFunctionName.length() - 2); return new PercentileMVAggregationFunction(firstArgument, parsePercentile(percentileString)); } else if (remainingFunctionName.matches("EST\\d+MV")) { String percentileString = remainingFunctionName.substring(3, remainingFunctionName.length() - 2); return new PercentileEstMVAggregationFunction(firstArgument, parsePercentile(percentileString)); } else if (remainingFunctionName.matches("TDIGEST\\d+MV")) { String percentileString = remainingFunctionName.substring(7, remainingFunctionName.length() - 2); return new PercentileTDigestMVAggregationFunction(firstArgument, parsePercentile(percentileString)); } } else if (numArguments == 2) { int percentile = parsePercentile(arguments.get(1).getLiteral()); if (remainingFunctionName.isEmpty()) { return new PercentileAggregationFunction(firstArgument, percentile); } if (remainingFunctionName.equals("EST")) { return new PercentileEstAggregationFunction(firstArgument, percentile); } if (remainingFunctionName.equals("TDIGEST")) { return new PercentileTDigestAggregationFunction(firstArgument, percentile); } if (remainingFunctionName.equals("MV")) { return new PercentileMVAggregationFunction(firstArgument, percentile); } if (remainingFunctionName.equals("ESTMV")) { return new PercentileEstMVAggregationFunction(firstArgument, percentile); } if (remainingFunctionName.equals("TDIGESTMV")) { return new PercentileTDigestMVAggregationFunction(firstArgument, percentile); } } throw new IllegalArgumentException("Invalid percentile function: " + function); } else { switch (AggregationFunctionType.valueOf(upperCaseFunctionName)) { case COUNT: return new CountAggregationFunction(); case MIN: return new MinAggregationFunction(firstArgument); case MAX: return new MaxAggregationFunction(firstArgument); case SUM: return new SumAggregationFunction(firstArgument); case SUMPRECISION: return new SumPrecisionAggregationFunction(arguments); case AVG: return new AvgAggregationFunction(firstArgument); case MINMAXRANGE: return new MinMaxRangeAggregationFunction(firstArgument); case DISTINCTCOUNT: return new DistinctCountAggregationFunction(firstArgument); case DISTINCTCOUNTBITMAP: return new DistinctCountBitmapAggregationFunction(firstArgument); case SEGMENTPARTITIONEDDISTINCTCOUNT: return new SegmentPartitionedDistinctCountAggregationFunction(firstArgument); case DISTINCTCOUNTHLL: return new DistinctCountHLLAggregationFunction(arguments); case DISTINCTCOUNTRAWHLL: return new DistinctCountRawHLLAggregationFunction(arguments); case FASTHLL: return new FastHLLAggregationFunction(firstArgument); case DISTINCTCOUNTTHETASKETCH: return new DistinctCountThetaSketchAggregationFunction(arguments); case DISTINCTCOUNTRAWTHETASKETCH: return new DistinctCountRawThetaSketchAggregationFunction(arguments); case IDSET: return new IdSetAggregationFunction(arguments); case COUNTMV: return new CountMVAggregationFunction(firstArgument); case MINMV: return new MinMVAggregationFunction(firstArgument); case MAXMV: return new MaxMVAggregationFunction(firstArgument); case SUMMV: return new SumMVAggregationFunction(firstArgument); case AVGMV: return new AvgMVAggregationFunction(firstArgument); case MINMAXRANGEMV: return new MinMaxRangeMVAggregationFunction(firstArgument); case DISTINCTCOUNTMV: return new DistinctCountMVAggregationFunction(firstArgument); case DISTINCTCOUNTBITMAPMV: return new DistinctCountBitmapMVAggregationFunction(firstArgument); case DISTINCTCOUNTHLLMV: return new DistinctCountHLLMVAggregationFunction(arguments); case DISTINCTCOUNTRAWHLLMV: return new DistinctCountRawHLLMVAggregationFunction(arguments); case DISTINCT: return new DistinctAggregationFunction(arguments, queryContext.getOrderByExpressions(), queryContext.getLimit()); case STUNION: return new StUnionAggregationFunction(firstArgument); default: throw new IllegalArgumentException(); } } } catch (Exception e) { throw new BadQueryRequestException("Invalid aggregation function: " + function); } } private  AggregationFunctionFactory(); static AggregationFunction getAggregationFunction(FunctionContext function, QueryContext queryContext);  }