|
33 | 33 | import com.starrocks.sql.optimizer.operator.logical.LogicalOlapScanOperator;
|
34 | 34 | import com.starrocks.sql.optimizer.operator.pattern.Pattern;
|
35 | 35 | import com.starrocks.sql.optimizer.operator.scalar.CallOperator;
|
| 36 | +import com.starrocks.sql.optimizer.operator.scalar.CastOperator; |
36 | 37 | import com.starrocks.sql.optimizer.operator.scalar.ColumnRefOperator;
|
37 | 38 | import com.starrocks.sql.optimizer.operator.scalar.ScalarOperator;
|
38 | 39 | import com.starrocks.sql.optimizer.rewrite.ReplaceColumnRefRewriter;
|
| 40 | +import com.starrocks.sql.optimizer.rewrite.ScalarOperatorRewriter; |
| 41 | +import com.starrocks.sql.optimizer.rewrite.scalar.ReduceCastRule; |
39 | 42 | import com.starrocks.sql.optimizer.rule.RuleType;
|
40 | 43 |
|
41 | 44 | import java.util.List;
|
@@ -138,7 +141,23 @@ public List<OptExpression> transform(OptExpression input, OptimizerContext conte
|
138 | 141 | aggregationOperator.getGroupingKeys().forEach(g -> projectMap.put(g, g));
|
139 | 142 | for (Map.Entry<ColumnRefOperator, CallOperator> entry : aggregationOperator.getAggregations().entrySet()) {
|
140 | 143 | // in this case, CallOperator must have only one child ColumnRefOperator
|
141 |
| - projectMap.put(entry.getKey(), entry.getValue().getChild(0)); |
| 144 | + // |
| 145 | + // in AggTable,may metric definition is `col int sum`, but in query sum(col)'s type is bigint, |
| 146 | + // so after replace sum(col) wit col, we must guarantee the same ColumnRefOperator points to |
| 147 | + // the same type expr, so we must cast col as bigint, otherwise during executing, ExchangeSink would |
| 148 | + // send serialize the column as int column while the ExchangeSource would derserialize it in bigint |
| 149 | + // column. |
| 150 | + ColumnRefOperator columnRef = entry.getKey(); |
| 151 | + CallOperator aggFunc = entry.getValue(); |
| 152 | + ScalarOperator aggFuncArg = aggFunc.getChild(0); |
| 153 | + if (aggFunc.getType().equals(aggFuncArg.getType())) { |
| 154 | + projectMap.put(columnRef, aggFuncArg); |
| 155 | + } else { |
| 156 | + ScalarOperator newExpr = new ScalarOperatorRewriter().rewrite( |
| 157 | + new CastOperator(aggFunc.getType(), aggFuncArg, true), |
| 158 | + Lists.newArrayList(new ReduceCastRule())); |
| 159 | + projectMap.put(columnRef, newExpr); |
| 160 | + } |
142 | 161 | }
|
143 | 162 |
|
144 | 163 | Map<ColumnRefOperator, ScalarOperator> newProjectMap = Maps.newHashMap();
|
|
0 commit comments