@@ -2018,30 +2018,31 @@ public RelNode visitChart(Chart node, CalcitePlanContext context) {
20182018 ArgumentMap argMap = ArgumentMap .of (node .getArguments ());
20192019 List <UnresolvedExpression > groupExprList =
20202020 Stream .of (node .getRowSplit (), node .getColumnSplit ()).filter (Objects ::nonNull ).toList ();
2021- Boolean useNull = ( Boolean ) argMap . getOrDefault ( "usenull" , Chart . DEFAULT_USE_NULL ). getValue ( );
2021+ ChartConfig config = ChartConfig . fromArguments ( argMap );
20222022 Aggregation aggregation =
20232023 new Aggregation (
2024- node .getAggregationFunctions ( ),
2024+ List . of ( node .getAggregationFunction () ),
20252025 List .of (),
20262026 groupExprList ,
20272027 null ,
2028- List .of (new Argument (Argument .BUCKET_NULLABLE , AstDSL .booleanLiteral (useNull ))));
2028+ List .of (new Argument (Argument .BUCKET_NULLABLE , AstDSL .booleanLiteral (config . useNull ))));
20292029 RelNode aggregated = visitAggregation (aggregation , context );
20302030
20312031 // If row or column split does not present or limit equals 0, this is the same as `stats agg
2032- // [group by col]`
2032+ // [group by col]` because all truncating is performed on the column split
20332033 Integer limit = (Integer ) argMap .getOrDefault ("limit" , Chart .DEFAULT_LIMIT ).getValue ();
20342034 if (node .getRowSplit () == null || node .getColumnSplit () == null || Objects .equals (limit , 0 )) {
20352035 return aggregated ;
20362036 }
20372037
2038- String aggFunctionName = getAggFunctionName (node .getAggregationFunctions ().getFirst ());
2039- Optional <BuiltinFunctionName > aggFuncNameOptional = BuiltinFunctionName .of (aggFunctionName );
2040- if (aggFuncNameOptional .isEmpty ()) {
2041- throw new IllegalArgumentException (
2042- StringUtils .format ("Unrecognized aggregation function: %s" , aggFunctionName ));
2043- }
2044- BuiltinFunctionName aggFunction = aggFuncNameOptional .get ();
2038+ String aggFunctionName = getAggFunctionName (node .getAggregationFunction ());
2039+ BuiltinFunctionName aggFunction =
2040+ BuiltinFunctionName .of (aggFunctionName )
2041+ .orElseThrow (
2042+ () ->
2043+ new IllegalArgumentException (
2044+ StringUtils .format (
2045+ "Unrecognized aggregation function: %s" , aggFunctionName )));
20452046
20462047 // Convert the column split to string if necessary: column split was supposed to be pivoted to
20472048 // column names. This guarantees that its type compatibility with useother and usenull
@@ -2058,12 +2059,6 @@ public RelNode visitChart(Chart node, CalcitePlanContext context) {
20582059 relBuilder .project (relBuilder .field (0 ), relBuilder .field (1 ), colSplit );
20592060 aggregated = relBuilder .peek ();
20602061
2061- Boolean top = (Boolean ) argMap .getOrDefault ("top" , Chart .DEFAULT_TOP ).getValue ();
2062- Boolean useOther =
2063- (Boolean ) argMap .getOrDefault ("useother" , Chart .DEFAULT_USE_OTHER ).getValue ();
2064- String otherStr = (String ) argMap .getOrDefault ("otherstr" , Chart .DEFAULT_OTHER_STR ).getValue ();
2065- String nullStr = (String ) argMap .getOrDefault ("nullstr" , Chart .DEFAULT_NULL_STR ).getValue ();
2066-
20672062 // 0: agg; 2: column-split
20682063 relBuilder .project (relBuilder .field (0 ), relBuilder .field (2 ));
20692064 // 1: column split; 0: agg
@@ -2075,7 +2070,7 @@ public RelNode visitChart(Chart node, CalcitePlanContext context) {
20752070 // Apply sorting: for MIN/EARLIEST, reverse the top/bottom logic
20762071 boolean smallestFirst =
20772072 aggFunction == BuiltinFunctionName .MIN || aggFunction == BuiltinFunctionName .EARLIEST ;
2078- if (top != smallestFirst ) {
2073+ if (config . top != smallestFirst ) {
20792074 grandTotal = relBuilder .desc (grandTotal );
20802075 }
20812076
@@ -2108,26 +2103,26 @@ public RelNode visitChart(Chart node, CalcitePlanContext context) {
21082103 relBuilder .literal (limit ));
21092104 RexNode nullCondition = relBuilder .isNull (colSplitPostJoin );
21102105 RexNode columnSplitExpr ;
2111- if (!useOther ) {
2106+ if (!config . useOther ) {
21122107 relBuilder .filter (lteCondition );
21132108 }
21142109
2115- if (useNull ) {
2110+ if (config . useNull ) {
21162111 columnSplitExpr =
21172112 relBuilder .call (
21182113 SqlStdOperatorTable .CASE ,
21192114 nullCondition ,
2120- relBuilder .literal (nullStr ),
2115+ relBuilder .literal (config . nullStr ),
21212116 lteCondition ,
21222117 relBuilder .field (2 ),
2123- relBuilder .literal (otherStr ));
2118+ relBuilder .literal (config . otherStr ));
21242119 } else {
21252120 columnSplitExpr =
21262121 relBuilder .call (
21272122 SqlStdOperatorTable .CASE ,
21282123 lteCondition ,
21292124 relBuilder .field (2 ),
2130- relBuilder .literal (otherStr ));
2125+ relBuilder .literal (config . otherStr ));
21312126 }
21322127
21332128 String aggFieldName = relBuilder .peek ().getRowType ().getFieldNames ().getFirst ();
@@ -2141,6 +2136,21 @@ public RelNode visitChart(Chart node, CalcitePlanContext context) {
21412136 return relBuilder .peek ();
21422137 }
21432138
2139+ private record ChartConfig (
2140+ int limit , boolean top , boolean useOther , boolean useNull , String otherStr , String nullStr ) {
2141+ static ChartConfig fromArguments (ArgumentMap argMap ) {
2142+ int limit = (Integer ) argMap .getOrDefault ("limit" , Chart .DEFAULT_LIMIT ).getValue ();
2143+ boolean top = (Boolean ) argMap .getOrDefault ("top" , Chart .DEFAULT_TOP ).getValue ();
2144+ boolean useOther =
2145+ (Boolean ) argMap .getOrDefault ("useother" , Chart .DEFAULT_USE_OTHER ).getValue ();
2146+ boolean useNull = (Boolean ) argMap .getOrDefault ("usenull" , Chart .DEFAULT_USE_NULL ).getValue ();
2147+ String otherStr =
2148+ (String ) argMap .getOrDefault ("otherstr" , Chart .DEFAULT_OTHER_STR ).getValue ();
2149+ String nullStr = (String ) argMap .getOrDefault ("nullstr" , Chart .DEFAULT_NULL_STR ).getValue ();
2150+ return new ChartConfig (limit , top , useOther , useNull , otherStr , nullStr );
2151+ }
2152+ }
2153+
21442154 /** Transforms timechart command into SQL-based operations. */
21452155 @ Override
21462156 public RelNode visitTimechart (
@@ -2150,7 +2160,7 @@ public RelNode visitTimechart(
21502160 // Extract parameters
21512161 UnresolvedExpression spanExpr = node .getBinExpression ();
21522162
2153- List <UnresolvedExpression > groupExprList = Arrays . asList ( spanExpr ) ;
2163+ List <UnresolvedExpression > groupExprList ;
21542164
21552165 // Handle no by field case
21562166 if (node .getByField () == null ) {
0 commit comments