@@ -18,18 +18,20 @@ use anyhow::anyhow;
18
18
use itertools:: Itertools ;
19
19
use risingwave_common:: catalog:: { internal_table_name_to_parts, Field , Schema , StreamJobStatus } ;
20
20
use risingwave_common:: types:: { DataType , ScalarImpl } ;
21
+ use risingwave_expr:: aggregate:: AggType ;
21
22
22
23
use super :: { ApplyResult , BoxedRule , FallibleRule } ;
23
24
use crate :: catalog:: catalog_service:: CatalogReadGuard ;
24
25
use crate :: catalog:: table_catalog:: TableType ;
25
- use crate :: expr:: { ExprImpl , InputRef , Literal , TableFunctionType } ;
26
+ use crate :: expr:: { AggCall , ExprImpl , InputRef , Literal , OrderBy , TableFunctionType } ;
26
27
use crate :: optimizer:: plan_node:: generic:: GenericPlanRef ;
27
28
use crate :: optimizer:: plan_node:: {
28
29
LogicalAgg , LogicalProject , LogicalScan , LogicalTableFunction , LogicalUnion , LogicalValues ,
29
30
} ;
30
31
use crate :: optimizer:: PlanRef ;
31
- use crate :: utils:: GroupBy ;
32
+ use crate :: utils:: { Condition , GroupBy } ;
32
33
use crate :: TableCatalog ;
34
+ pub use risingwave_pb:: expr:: agg_call:: PbKind as PbAggKind ;
33
35
34
36
/// Transform a special `TableFunction` (with `FILE_SCAN` table function type) into a `LogicalFileScan`
35
37
pub struct TableFunctionToInternalBackfillProgressRule { }
@@ -79,27 +81,66 @@ impl FallibleRule for TableFunctionToInternalBackfillProgressRule {
79
81
None ,
80
82
Default :: default ( ) ,
81
83
) ;
82
- let project = LogicalProject :: new (
83
- scan. into ( ) ,
84
- vec ! [ ExprImpl :: InputRef ( Box :: new( InputRef {
84
+ let project = {
85
+ let job_id_expr = ExprImpl :: Literal ( Box :: new ( Literal :: new (
86
+ Some ( ScalarImpl :: Int32 ( job_id. table_id as i32 ) ) ,
87
+ DataType :: Int32 ,
88
+ ) ) ) ;
89
+ let row_count_expr = ExprImpl :: InputRef ( Box :: new ( InputRef {
85
90
index : row_count_column_index,
86
91
data_type : DataType :: Int64 ,
87
- } ) ) ] ,
88
- ) ;
89
- let select_exprs = vec ! [ ExprImpl :: Literal ( Box :: new( Literal :: new(
90
- Some ( ScalarImpl :: Int32 ( job_id. table_id as i32 ) ) ,
91
- DataType :: Int32 ,
92
- ) ) ) ] ;
93
- let group_key = GroupBy :: GroupKey ( vec ! [ ExprImpl :: InputRef ( Box :: new( InputRef {
92
+ } ) ) ;
93
+ LogicalProject :: new (
94
+ scan. into ( ) ,
95
+ vec ! [ job_id_expr, row_count_expr] ,
96
+ )
97
+ } ;
98
+ counts. push ( project. into ( ) ) ;
99
+ }
100
+ let union = LogicalUnion :: new ( true , counts) ;
101
+ let select_exprs = {
102
+ let job_id = ExprImpl :: InputRef ( Box :: new ( InputRef {
94
103
index : 0 ,
95
104
data_type : DataType :: Int32 ,
96
- } ) ) ] ) ;
97
- let ( count, _rewritten_select_exprs, _) =
98
- LogicalAgg :: create ( select_exprs, group_key, None , project. into ( ) ) ?;
99
- counts. push ( count) ;
100
- }
101
- println ! ( "counts: {:?}" , counts) ;
102
- ApplyResult :: Ok ( LogicalUnion :: new ( true , counts) . into ( ) )
105
+ } ) ) ;
106
+ let sum_agg = ExprImpl :: AggCall ( Box :: new ( AggCall :: new (
107
+ AggType :: Builtin ( PbAggKind :: Sum ) ,
108
+ vec ! [
109
+ ExprImpl :: InputRef ( Box :: new( InputRef {
110
+ index: 1 ,
111
+ data_type: DataType :: Int64 ,
112
+ } ) ) ,
113
+ ] ,
114
+ false ,
115
+ OrderBy :: any ( ) ,
116
+ Condition :: true_cond ( ) ,
117
+ vec ! [ ] ,
118
+ ) ?) ) ;
119
+ vec ! [
120
+ job_id,
121
+ sum_agg,
122
+ ]
123
+ } ;
124
+ let group_key = GroupBy :: GroupKey ( vec ! [ ExprImpl :: InputRef ( Box :: new( InputRef {
125
+ index: 0 ,
126
+ data_type: DataType :: Int32 ,
127
+ } ) ) ] ) ;
128
+ let ( agg, _rewritten_select_exprs, _rewritten_having_exprs) =
129
+ LogicalAgg :: create ( select_exprs, group_key, None , union. into ( ) ) ?;
130
+ let project = LogicalProject :: new (
131
+ agg. into ( ) ,
132
+ vec ! [
133
+ ExprImpl :: InputRef ( Box :: new( InputRef {
134
+ index: 0 ,
135
+ data_type: DataType :: Int32 ,
136
+ } ) ) ,
137
+ ExprImpl :: InputRef ( Box :: new( InputRef {
138
+ index: 1 ,
139
+ data_type: DataType :: Decimal ,
140
+ } ) ) . cast_explicit( DataType :: Int64 ) ?,
141
+ ] ,
142
+ ) ;
143
+ ApplyResult :: Ok ( project. into ( ) )
103
144
}
104
145
}
105
146
0 commit comments