123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611 |
- package planner
- import (
- "fmt"
- "github.com/emqx/kuiper/common"
- "github.com/emqx/kuiper/common/kv"
- "github.com/emqx/kuiper/xsql"
- "github.com/emqx/kuiper/xstream/api"
- "path"
- "reflect"
- "strings"
- "testing"
- )
- var (
- DbDir = getDbDir()
- )
- func getDbDir() string {
- common.InitConf()
- dbDir, err := common.GetDataLoc()
- if err != nil {
- common.Log.Panic(err)
- }
- common.Log.Infof("db location is %s", dbDir)
- return dbDir
- }
- func Test_createLogicalPlan(t *testing.T) {
- store := kv.GetDefaultKVStore(path.Join(DbDir, "stream"))
- err := store.Open()
- if err != nil {
- t.Error(err)
- return
- }
- defer store.Close()
- streamSqls := map[string]string{
- "src1": `CREATE STREAM src1 (
- id1 BIGINT,
- temp BIGINT,
- name string
- ) WITH (DATASOURCE="src1", FORMAT="json", KEY="ts");`,
- "src2": `CREATE STREAM src1 (
- id2 BIGINT,
- hum BIGINT
- ) WITH (DATASOURCE="src1", FORMAT="json", KEY="ts");`,
- }
- for name, sql := range streamSqls {
- store.Set(name, sql)
- }
- streams := make(map[string]*xsql.StreamStmt)
- for n, _ := range streamSqls {
- streamStmt, err := getStream(store, n)
- if err != nil {
- t.Errorf("fail to get stream %s, please check if stream is created", n)
- return
- }
- streams[n] = streamStmt
- }
- var tests = []struct {
- sql string
- p LogicalPlan
- err string
- }{
- { // 0
- sql: `SELECT name FROM src1`,
- p: ProjectPlan{
- baseLogicalPlan: baseLogicalPlan{
- children: []LogicalPlan{
- DataSourcePlan{
- baseLogicalPlan: baseLogicalPlan{},
- name: "src1",
- streamFields: []interface{}{
- &xsql.StreamField{
- Name: "name",
- FieldType: &xsql.BasicType{Type: xsql.STRINGS},
- },
- },
- streamStmt: streams["src1"],
- metaFields: []string{},
- }.Init(),
- },
- },
- fields: []xsql.Field{
- {
- Expr: &xsql.FieldRef{Name: "name"},
- Name: "name",
- AName: ""},
- },
- isAggregate: false,
- sendMeta: false,
- }.Init(),
- }, { // 1 optimize where to data source
- sql: `SELECT temp FROM src1 WHERE name = "v1" GROUP BY TUMBLINGWINDOW(ss, 10)`,
- p: ProjectPlan{
- baseLogicalPlan: baseLogicalPlan{
- children: []LogicalPlan{
- WindowPlan{
- baseLogicalPlan: baseLogicalPlan{
- children: []LogicalPlan{
- FilterPlan{
- baseLogicalPlan: baseLogicalPlan{
- children: []LogicalPlan{
- DataSourcePlan{
- name: "src1",
- streamFields: []interface{}{
- &xsql.StreamField{
- Name: "name",
- FieldType: &xsql.BasicType{Type: xsql.STRINGS},
- },
- &xsql.StreamField{
- Name: "temp",
- FieldType: &xsql.BasicType{Type: xsql.BIGINT},
- },
- },
- streamStmt: streams["src1"],
- metaFields: []string{},
- }.Init(),
- },
- },
- condition: &xsql.BinaryExpr{
- LHS: &xsql.FieldRef{Name: "name"},
- OP: xsql.EQ,
- RHS: &xsql.StringLiteral{Val: "v1"},
- },
- }.Init(),
- },
- },
- condition: nil,
- wtype: xsql.TUMBLING_WINDOW,
- length: 10000,
- interval: 0,
- limit: 0,
- }.Init(),
- },
- },
- fields: []xsql.Field{
- {
- Expr: &xsql.FieldRef{Name: "temp"},
- Name: "temp",
- AName: ""},
- },
- isAggregate: false,
- sendMeta: false,
- }.Init(),
- }, { // 2 condition that cannot be optimized
- sql: `SELECT id1 FROM src1 INNER JOIN src2 on src1.id1 = src2.id2 WHERE src1.temp > 20 OR src2.hum > 60 GROUP BY TUMBLINGWINDOW(ss, 10)`,
- p: ProjectPlan{
- baseLogicalPlan: baseLogicalPlan{
- children: []LogicalPlan{
- JoinPlan{
- baseLogicalPlan: baseLogicalPlan{
- children: []LogicalPlan{
- WindowPlan{
- baseLogicalPlan: baseLogicalPlan{
- children: []LogicalPlan{
- DataSourcePlan{
- name: "src1",
- streamFields: []interface{}{
- &xsql.StreamField{
- Name: "id1",
- FieldType: &xsql.BasicType{Type: xsql.BIGINT},
- },
- &xsql.StreamField{
- Name: "temp",
- FieldType: &xsql.BasicType{Type: xsql.BIGINT},
- },
- },
- streamStmt: streams["src1"],
- metaFields: []string{},
- }.Init(),
- DataSourcePlan{
- name: "src2",
- streamFields: []interface{}{
- &xsql.StreamField{
- Name: "hum",
- FieldType: &xsql.BasicType{Type: xsql.BIGINT},
- },
- &xsql.StreamField{
- Name: "id2",
- FieldType: &xsql.BasicType{Type: xsql.BIGINT},
- },
- },
- streamStmt: streams["src2"],
- metaFields: []string{},
- }.Init(),
- },
- },
- condition: nil,
- wtype: xsql.TUMBLING_WINDOW,
- length: 10000,
- interval: 0,
- limit: 0,
- }.Init(),
- },
- },
- from: &xsql.Table{Name: "src1"},
- joins: xsql.Joins{xsql.Join{
- Name: "src2",
- JoinType: xsql.INNER_JOIN,
- Expr: &xsql.BinaryExpr{
- OP: xsql.AND,
- LHS: &xsql.BinaryExpr{
- LHS: &xsql.BinaryExpr{
- OP: xsql.GT,
- LHS: &xsql.FieldRef{Name: "temp", StreamName: "src1"},
- RHS: &xsql.IntegerLiteral{Val: 20},
- },
- OP: xsql.OR,
- RHS: &xsql.BinaryExpr{
- OP: xsql.GT,
- LHS: &xsql.FieldRef{Name: "hum", StreamName: "src2"},
- RHS: &xsql.IntegerLiteral{Val: 60},
- },
- },
- RHS: &xsql.BinaryExpr{
- OP: xsql.EQ,
- LHS: &xsql.FieldRef{Name: "id1", StreamName: "src1"},
- RHS: &xsql.FieldRef{Name: "id2", StreamName: "src2"},
- },
- },
- }},
- }.Init(),
- },
- },
- fields: []xsql.Field{
- {
- Expr: &xsql.FieldRef{Name: "id1"},
- Name: "id1",
- AName: ""},
- },
- isAggregate: false,
- sendMeta: false,
- }.Init(),
- }, { // 3 optimize window filter
- sql: `SELECT id1 FROM src1 WHERE name = "v1" GROUP BY TUMBLINGWINDOW(ss, 10) FILTER( WHERE temp > 2)`,
- p: ProjectPlan{
- baseLogicalPlan: baseLogicalPlan{
- children: []LogicalPlan{
- WindowPlan{
- baseLogicalPlan: baseLogicalPlan{
- children: []LogicalPlan{
- FilterPlan{
- baseLogicalPlan: baseLogicalPlan{
- children: []LogicalPlan{
- DataSourcePlan{
- name: "src1",
- streamFields: []interface{}{
- &xsql.StreamField{
- Name: "id1",
- FieldType: &xsql.BasicType{Type: xsql.BIGINT},
- },
- &xsql.StreamField{
- Name: "name",
- FieldType: &xsql.BasicType{Type: xsql.STRINGS},
- },
- &xsql.StreamField{
- Name: "temp",
- FieldType: &xsql.BasicType{Type: xsql.BIGINT},
- },
- },
- streamStmt: streams["src1"],
- metaFields: []string{},
- }.Init(),
- },
- },
- condition: &xsql.BinaryExpr{
- OP: xsql.AND,
- LHS: &xsql.BinaryExpr{
- LHS: &xsql.FieldRef{Name: "name"},
- OP: xsql.EQ,
- RHS: &xsql.StringLiteral{Val: "v1"},
- },
- RHS: &xsql.BinaryExpr{
- LHS: &xsql.FieldRef{Name: "temp"},
- OP: xsql.GT,
- RHS: &xsql.IntegerLiteral{Val: 2},
- },
- },
- }.Init(),
- },
- },
- condition: nil,
- wtype: xsql.TUMBLING_WINDOW,
- length: 10000,
- interval: 0,
- limit: 0,
- }.Init(),
- },
- },
- fields: []xsql.Field{
- {
- Expr: &xsql.FieldRef{Name: "id1"},
- Name: "id1",
- AName: ""},
- },
- isAggregate: false,
- sendMeta: false,
- }.Init(),
- }, { // 4. do not optimize count window
- sql: `SELECT * FROM src1 WHERE temp > 20 GROUP BY COUNTWINDOW(5,1) HAVING COUNT(*) > 2`,
- p: ProjectPlan{
- baseLogicalPlan: baseLogicalPlan{
- children: []LogicalPlan{
- HavingPlan{
- baseLogicalPlan: baseLogicalPlan{
- children: []LogicalPlan{
- FilterPlan{
- baseLogicalPlan: baseLogicalPlan{
- children: []LogicalPlan{
- WindowPlan{
- baseLogicalPlan: baseLogicalPlan{
- children: []LogicalPlan{
- DataSourcePlan{
- name: "src1",
- isWildCard: true,
- streamFields: []interface{}{
- &xsql.StreamField{
- Name: "id1",
- FieldType: &xsql.BasicType{Type: xsql.BIGINT},
- },
- &xsql.StreamField{
- Name: "temp",
- FieldType: &xsql.BasicType{Type: xsql.BIGINT},
- },
- &xsql.StreamField{
- Name: "name",
- FieldType: &xsql.BasicType{Type: xsql.STRINGS},
- },
- },
- streamStmt: streams["src1"],
- metaFields: []string{},
- }.Init(),
- },
- },
- condition: nil,
- wtype: xsql.COUNT_WINDOW,
- length: 5,
- interval: 1,
- limit: 0,
- }.Init(),
- },
- },
- condition: &xsql.BinaryExpr{
- LHS: &xsql.FieldRef{Name: "temp"},
- OP: xsql.GT,
- RHS: &xsql.IntegerLiteral{Val: 20},
- },
- }.Init(),
- },
- },
- condition: &xsql.BinaryExpr{
- LHS: &xsql.Call{Name: "COUNT", Args: []xsql.Expr{&xsql.StringLiteral{
- Val: "*",
- }}},
- OP: xsql.GT,
- RHS: &xsql.IntegerLiteral{Val: 2},
- },
- }.Init(),
- },
- },
- fields: []xsql.Field{
- {
- Expr: &xsql.Wildcard{Token: xsql.ASTERISK},
- Name: "",
- AName: ""},
- },
- isAggregate: false,
- sendMeta: false,
- }.Init(),
- }, { // 5. optimize join on
- sql: `SELECT id1 FROM src1 INNER JOIN src2 on src1.id1 = src2.id2 and src1.temp > 20 and src2.hum < 60 WHERE src1.id1 > 111 GROUP BY TUMBLINGWINDOW(ss, 10)`,
- p: ProjectPlan{
- baseLogicalPlan: baseLogicalPlan{
- children: []LogicalPlan{
- JoinPlan{
- baseLogicalPlan: baseLogicalPlan{
- children: []LogicalPlan{
- WindowPlan{
- baseLogicalPlan: baseLogicalPlan{
- children: []LogicalPlan{
- FilterPlan{
- baseLogicalPlan: baseLogicalPlan{
- children: []LogicalPlan{
- DataSourcePlan{
- name: "src1",
- streamFields: []interface{}{
- &xsql.StreamField{
- Name: "id1",
- FieldType: &xsql.BasicType{Type: xsql.BIGINT},
- },
- &xsql.StreamField{
- Name: "temp",
- FieldType: &xsql.BasicType{Type: xsql.BIGINT},
- },
- },
- streamStmt: streams["src1"],
- metaFields: []string{},
- }.Init(),
- },
- },
- condition: &xsql.BinaryExpr{
- RHS: &xsql.BinaryExpr{
- OP: xsql.GT,
- LHS: &xsql.FieldRef{Name: "temp", StreamName: "src1"},
- RHS: &xsql.IntegerLiteral{Val: 20},
- },
- OP: xsql.AND,
- LHS: &xsql.BinaryExpr{
- OP: xsql.GT,
- LHS: &xsql.FieldRef{Name: "id1", StreamName: "src1"},
- RHS: &xsql.IntegerLiteral{Val: 111},
- },
- },
- }.Init(),
- FilterPlan{
- baseLogicalPlan: baseLogicalPlan{
- children: []LogicalPlan{
- DataSourcePlan{
- name: "src2",
- streamFields: []interface{}{
- &xsql.StreamField{
- Name: "hum",
- FieldType: &xsql.BasicType{Type: xsql.BIGINT},
- },
- &xsql.StreamField{
- Name: "id2",
- FieldType: &xsql.BasicType{Type: xsql.BIGINT},
- },
- },
- streamStmt: streams["src2"],
- metaFields: []string{},
- }.Init(),
- },
- },
- condition: &xsql.BinaryExpr{
- OP: xsql.LT,
- LHS: &xsql.FieldRef{Name: "hum", StreamName: "src2"},
- RHS: &xsql.IntegerLiteral{Val: 60},
- },
- }.Init(),
- },
- },
- condition: nil,
- wtype: xsql.TUMBLING_WINDOW,
- length: 10000,
- interval: 0,
- limit: 0,
- }.Init(),
- },
- },
- from: &xsql.Table{
- Name: "src1",
- },
- joins: []xsql.Join{
- {
- Name: "src2",
- Alias: "",
- JoinType: xsql.INNER_JOIN,
- Expr: &xsql.BinaryExpr{
- LHS: &xsql.FieldRef{Name: "id1", StreamName: "src1"},
- OP: xsql.EQ,
- RHS: &xsql.FieldRef{Name: "id2", StreamName: "src2"},
- },
- },
- },
- }.Init(),
- },
- },
- fields: []xsql.Field{
- {
- Expr: &xsql.FieldRef{Name: "id1"},
- Name: "id1",
- AName: ""},
- },
- isAggregate: false,
- sendMeta: false,
- }.Init(),
- }, { // 6. optimize outter join on
- sql: `SELECT id1 FROM src1 FULL JOIN src2 on src1.id1 = src2.id2 and src1.temp > 20 and src2.hum < 60 WHERE src1.id > 111 GROUP BY TUMBLINGWINDOW(ss, 10)`,
- p: ProjectPlan{
- baseLogicalPlan: baseLogicalPlan{
- children: []LogicalPlan{
- JoinPlan{
- baseLogicalPlan: baseLogicalPlan{
- children: []LogicalPlan{
- WindowPlan{
- baseLogicalPlan: baseLogicalPlan{
- children: []LogicalPlan{
- FilterPlan{
- baseLogicalPlan: baseLogicalPlan{
- children: []LogicalPlan{
- DataSourcePlan{
- name: "src1",
- streamFields: []interface{}{
- &xsql.StreamField{
- Name: "id1",
- FieldType: &xsql.BasicType{Type: xsql.BIGINT},
- },
- &xsql.StreamField{
- Name: "temp",
- FieldType: &xsql.BasicType{Type: xsql.BIGINT},
- },
- },
- streamStmt: streams["src1"],
- metaFields: []string{},
- }.Init(),
- },
- },
- condition: &xsql.BinaryExpr{
- OP: xsql.GT,
- LHS: &xsql.FieldRef{Name: "id", StreamName: "src1"},
- RHS: &xsql.IntegerLiteral{Val: 111},
- },
- }.Init(),
- DataSourcePlan{
- name: "src2",
- streamFields: []interface{}{
- &xsql.StreamField{
- Name: "hum",
- FieldType: &xsql.BasicType{Type: xsql.BIGINT},
- },
- &xsql.StreamField{
- Name: "id2",
- FieldType: &xsql.BasicType{Type: xsql.BIGINT},
- },
- },
- streamStmt: streams["src2"],
- metaFields: []string{},
- }.Init(),
- },
- },
- condition: nil,
- wtype: xsql.TUMBLING_WINDOW,
- length: 10000,
- interval: 0,
- limit: 0,
- }.Init(),
- },
- },
- from: &xsql.Table{
- Name: "src1",
- },
- joins: []xsql.Join{
- {
- Name: "src2",
- Alias: "",
- JoinType: xsql.FULL_JOIN,
- Expr: &xsql.BinaryExpr{
- OP: xsql.AND,
- LHS: &xsql.BinaryExpr{
- OP: xsql.AND,
- LHS: &xsql.BinaryExpr{
- LHS: &xsql.FieldRef{Name: "id1", StreamName: "src1"},
- OP: xsql.EQ,
- RHS: &xsql.FieldRef{Name: "id2", StreamName: "src2"},
- },
- RHS: &xsql.BinaryExpr{
- OP: xsql.GT,
- LHS: &xsql.FieldRef{Name: "temp", StreamName: "src1"},
- RHS: &xsql.IntegerLiteral{Val: 20},
- },
- },
- RHS: &xsql.BinaryExpr{
- OP: xsql.LT,
- LHS: &xsql.FieldRef{Name: "hum", StreamName: "src2"},
- RHS: &xsql.IntegerLiteral{Val: 60},
- },
- },
- },
- },
- }.Init(),
- },
- },
- fields: []xsql.Field{
- {
- Expr: &xsql.FieldRef{Name: "id1"},
- Name: "id1",
- AName: ""},
- },
- isAggregate: false,
- sendMeta: false,
- }.Init(),
- },
- }
- fmt.Printf("The test bucket size is %d.\n\n", len(tests))
- for i, tt := range tests {
- stmt, err := xsql.NewParser(strings.NewReader(tt.sql)).Parse()
- if err != nil {
- t.Errorf("%d. %q: error compile sql: %s\n", i, tt.sql, err)
- continue
- }
- p, err := createLogicalPlan(stmt, &api.RuleOption{
- IsEventTime: false,
- LateTol: 0,
- Concurrency: 0,
- BufferLength: 0,
- SendMetaToSink: false,
- Qos: 0,
- CheckpointInterval: 0,
- SendError: true,
- }, store)
- if err != nil {
- t.Errorf("%d. %q\n\nerror:%v\n\n", i, tt.sql, err)
- }
- if !reflect.DeepEqual(tt.p, p) {
- t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.sql, tt.p, p)
- }
- }
- }
|