@@ -37,6 +37,65 @@ func init() {
3737 testx .InitEnv ("node_test" )
3838}
3939
40+ func TestStateWindowStatePartition (t * testing.T ) {
41+ conf .IsTesting = true
42+ ctx , cancel := mockContext .NewMockContext ("1" , "2" ).WithCancel ()
43+ now := time .Now ()
44+ o := & def.RuleOption {
45+ BufferLength : 10 ,
46+ }
47+ kv , err := store .GetKV ("stream" )
48+ require .NoError (t , err )
49+ require .NoError (t , prepareStream ())
50+ sql := "select count(*) from stream group by statewindow(a =1 , a = 2) over (partition by b)"
51+ stmt , err := xsql .NewParser (strings .NewReader (sql )).Parse ()
52+ require .NoError (t , err )
53+ p , err := planner .CreateLogicalPlan (stmt , o , kv )
54+ require .NoError (t , err )
55+ require .NotNil (t , p )
56+ windowPlan := extractWindowPlan (p )
57+ require .NotNil (t , windowPlan )
58+ op , err := node .NewWindowV2Op ("window" , node.WindowConfig {
59+ Type : windowPlan .WindowType (),
60+ BeginCondition : windowPlan .GetBeginCondition (),
61+ EmitCondition : windowPlan .GetEmitCondition (),
62+ PartitionExpr : windowPlan .GetPartitionExpr (),
63+ }, o )
64+ require .NoError (t , err )
65+ require .NotNil (t , op )
66+ input , _ := op .GetInput ()
67+ output := make (chan any , 10 )
68+ op .AddOutput (output , "output" )
69+ errCh := make (chan error , 10 )
70+ op .Exec (ctx , errCh )
71+ waitExecute ()
72+ input <- & xsql.Tuple {Message : map [string ]any {"a" : true }, Timestamp : now }
73+ input <- & xsql.Tuple {Message : map [string ]any {"a" : false }, Timestamp : now .Add (500 * time .Millisecond )}
74+ waitExecute ()
75+ input <- & xsql.Tuple {Message : map [string ]any {"a" : int64 (1 ), "b" : int64 (1 )}, Timestamp : now }
76+ input <- & xsql.Tuple {Message : map [string ]any {"a" : int64 (1 ), "b" : int64 (2 )}, Timestamp : now }
77+ input <- & xsql.Tuple {Message : map [string ]any {"a" : int64 (2 ), "b" : int64 (1 )}, Timestamp : now .Add (500 * time .Millisecond )}
78+ waitExecute ()
79+ got := <- output
80+ wt , ok := got .(* xsql.WindowTuples )
81+ require .True (t , ok )
82+ require .NotNil (t , wt )
83+ d := wt .ToMaps ()
84+ require .Equal (t , []map [string ]any {
85+ {
86+ "a" : int64 (1 ),
87+ "b" : int64 (1 ),
88+ },
89+ {
90+ "a" : int64 (2 ),
91+ "b" : int64 (1 ),
92+ },
93+ }, d )
94+ cancel ()
95+ waitExecute ()
96+ op .Close ()
97+ }
98+
4099func TestStateWindowState (t * testing.T ) {
41100 conf .IsTesting = true
42101 ctx , cancel := mockContext .NewMockContext ("1" , "2" ).WithCancel ()
0 commit comments