6262import org .apache .calcite .sql .fun .SqlStdOperatorTable ;
6363import org .apache .calcite .sql .type .SqlTypeFamily ;
6464import org .apache .calcite .sql .type .SqlTypeName ;
65+ import org .apache .calcite .sql .type .SqlTypeUtil ;
6566import org .apache .calcite .tools .RelBuilder ;
6667import org .apache .calcite .tools .RelBuilder .AggCall ;
6768import org .apache .calcite .util .Holder ;
@@ -2039,7 +2040,22 @@ public RelNode visitChart(Chart node, CalcitePlanContext context) {
20392040 groupExprList ,
20402041 span ,
20412042 List .of (new Argument (Argument .BUCKET_NULLABLE , AstDSL .booleanLiteral (useNull ))));
2042- RelNode aggregated = visitAggregation (aggregation , context );
2043+ visitAggregation (aggregation , context );
2044+
2045+ // Convert the column split to string if necessary: column split was supposed to be pivoted to
2046+ // column names. This guarantees that its type being compatible with useother and usenull
2047+ RelBuilder relBuilder = context .relBuilder ;
2048+ RexNode colSplit = relBuilder .field (2 );
2049+ String columSplitName = relBuilder .peek ().getRowType ().getFieldNames ().getLast ();
2050+ if (!SqlTypeUtil .isCharacter (colSplit .getType ())) {
2051+ colSplit =
2052+ relBuilder .alias (
2053+ context .rexBuilder .makeCast (
2054+ UserDefinedFunctionUtils .NULLABLE_STRING , colSplit , true , true ),
2055+ columSplitName );
2056+ }
2057+ relBuilder .project (relBuilder .field (0 ), relBuilder .field (1 ), colSplit );
2058+ RelNode aggregated = relBuilder .peek ();
20432059
20442060 // If row or column split does not present or limit equals 0, this is the same as `stats agg
20452061 // [group by col]`
@@ -2054,8 +2070,6 @@ public RelNode visitChart(Chart node, CalcitePlanContext context) {
20542070 String otherStr = (String ) argMap .getOrDefault ("otherstr" , Chart .DEFAULT_OTHER_STR ).getValue ();
20552071 String nullStr = (String ) argMap .getOrDefault ("nullstr" , Chart .DEFAULT_NULL_STR ).getValue ();
20562072
2057- String columSplitName = aggregated .getRowType ().getFieldNames ().getLast ();
2058- RelBuilder relBuilder = context .relBuilder ;
20592073 // 0: agg; 2: column-split
20602074 relBuilder .project (relBuilder .field (0 ), relBuilder .field (2 ));
20612075 // 1: column split; 0: agg
0 commit comments