|
| 1 | +package dialect |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/dosco/graphjin/core/v3/internal/qcode" |
| 8 | + "github.com/dosco/graphjin/core/v3/internal/sdata" |
| 9 | +) |
| 10 | + |
| 11 | +func TestClickHouseCollectFieldsGroupsCompilerBaseColumns(t *testing.T) { |
| 12 | + nameCol := sdata.DBColumn{Name: "name"} |
| 13 | + sel := &qcode.Select{ |
| 14 | + GroupCols: true, |
| 15 | + Fields: []qcode.Field{ |
| 16 | + {Type: qcode.FieldTypeCol, Col: nameCol, FieldName: "name"}, |
| 17 | + {Type: qcode.FieldTypeFunc, Func: sdata.DBFunction{Name: "count", Agg: true}, FieldName: "count_id"}, |
| 18 | + }, |
| 19 | + BCols: []qcode.Column{{Col: nameCol}}, |
| 20 | + } |
| 21 | + |
| 22 | + node := &chNode{} |
| 23 | + if err := (&chBuilder{}).collectFields(sel, node); err != nil { |
| 24 | + t.Fatal(err) |
| 25 | + } |
| 26 | + if want := []string{"name"}; !reflect.DeepEqual(node.GroupBy, want) { |
| 27 | + t.Fatalf("group_by = %v, want %v", node.GroupBy, want) |
| 28 | + } |
| 29 | + if len(node.Aggregates) != 1 || node.Aggregates[0].Alias != "count_id" { |
| 30 | + t.Fatalf("aggregates = %+v", node.Aggregates) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +func TestClickHouseCollectFieldsKeepsGlobalAggregateUngrouped(t *testing.T) { |
| 35 | + sel := &qcode.Select{ |
| 36 | + GroupCols: true, |
| 37 | + GlobalAgg: true, |
| 38 | + Fields: []qcode.Field{ |
| 39 | + {Type: qcode.FieldTypeFunc, Func: sdata.DBFunction{Name: "count", Agg: true}, FieldName: "count_id"}, |
| 40 | + }, |
| 41 | + } |
| 42 | + |
| 43 | + node := &chNode{} |
| 44 | + if err := (&chBuilder{}).collectFields(sel, node); err != nil { |
| 45 | + t.Fatal(err) |
| 46 | + } |
| 47 | + if len(node.GroupBy) != 0 { |
| 48 | + t.Fatalf("global aggregate group_by = %v, want none", node.GroupBy) |
| 49 | + } |
| 50 | +} |
0 commit comments