123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928 |
- package planner
- import (
- "encoding/json"
- "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 = common.GetDbDir()
- )
- 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 src2 (
- id2 BIGINT,
- hum BIGINT
- ) WITH (DATASOURCE="src2", FORMAT="json", KEY="ts");`,
- "tableInPlanner": `CREATE TABLE tableInPlanner (
- id BIGINT,
- name STRING,
- value STRING,
- hum BIGINT
- ) WITH (TYPE="file");`,
- }
- types := map[string]xsql.StreamType{
- "src1": xsql.TypeStream,
- "src2": xsql.TypeStream,
- "tableInPlanner": xsql.TypeTable,
- }
- for name, sql := range streamSqls {
- s, err := json.Marshal(&xsql.StreamInfo{
- StreamType: types[name],
- Statement: sql,
- })
- if err != nil {
- t.Error(err)
- t.Fail()
- }
- store.Set(name, string(s))
- }
- streams := make(map[string]*xsql.StreamStmt)
- for n, _ := range streamSqls {
- streamStmt, err := xsql.GetDataSource(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", StreamName: "src1"},
- 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", StreamName: "src1"},
- 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", StreamName: "src1"},
- 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", StreamName: "src1"},
- 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", StreamName: "src1"},
- OP: xsql.EQ,
- RHS: &xsql.StringLiteral{Val: "v1"},
- },
- RHS: &xsql.BinaryExpr{
- LHS: &xsql.FieldRef{Name: "temp", StreamName: "src1"},
- 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", StreamName: "src1"},
- 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", StreamName: "src1"},
- OP: xsql.GT,
- RHS: &xsql.IntegerLiteral{Val: 20},
- },
- }.Init(),
- },
- },
- condition: &xsql.BinaryExpr{
- LHS: &xsql.Call{Name: "COUNT", Args: []xsql.Expr{&xsql.Wildcard{
- Token: xsql.ASTERISK,
- }}},
- 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", StreamName: "src1"},
- 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.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{
- OP: xsql.GT,
- LHS: &xsql.FieldRef{Name: "id1", 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", StreamName: "src1"},
- Name: "id1",
- AName: ""},
- },
- isAggregate: false,
- sendMeta: false,
- }.Init(),
- }, { // 7 window error for table
- sql: `SELECT value FROM tableInPlanner WHERE name = "v1" GROUP BY TUMBLINGWINDOW(ss, 10)`,
- p: nil,
- err: "cannot run window for TABLE sources",
- }, { // 8 join table without window
- sql: `SELECT id1 FROM src1 INNER JOIN tableInPlanner on src1.id1 = tableInPlanner.id and src1.temp > 20 and hum < 60 WHERE src1.id1 > 111`,
- p: ProjectPlan{
- baseLogicalPlan: baseLogicalPlan{
- children: []LogicalPlan{
- JoinPlan{
- baseLogicalPlan: baseLogicalPlan{
- children: []LogicalPlan{
- JoinAlignPlan{
- 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: "tableInPlanner",
- streamFields: []interface{}{
- &xsql.StreamField{
- Name: "hum",
- FieldType: &xsql.BasicType{Type: xsql.BIGINT},
- },
- &xsql.StreamField{
- Name: "id",
- FieldType: &xsql.BasicType{Type: xsql.BIGINT},
- },
- },
- streamStmt: streams["tableInPlanner"],
- metaFields: []string{},
- }.Init(),
- },
- },
- condition: &xsql.BinaryExpr{
- OP: xsql.LT,
- LHS: &xsql.FieldRef{Name: "hum", StreamName: "tableInPlanner"},
- RHS: &xsql.IntegerLiteral{Val: 60},
- },
- }.Init(),
- },
- },
- Emitters: []string{"tableInPlanner"},
- }.Init(),
- },
- },
- from: &xsql.Table{
- Name: "src1",
- },
- joins: []xsql.Join{
- {
- Name: "tableInPlanner",
- Alias: "",
- JoinType: xsql.INNER_JOIN,
- Expr: &xsql.BinaryExpr{
- LHS: &xsql.FieldRef{Name: "id1", StreamName: "src1"},
- OP: xsql.EQ,
- RHS: &xsql.FieldRef{Name: "id", StreamName: "tableInPlanner"},
- },
- },
- },
- }.Init(),
- },
- },
- fields: []xsql.Field{
- {
- Expr: &xsql.FieldRef{Name: "id1", StreamName: "src1"},
- Name: "id1",
- AName: ""},
- },
- isAggregate: false,
- sendMeta: false,
- }.Init(),
- }, { // 9 join table with window
- sql: `SELECT id1 FROM src1 INNER JOIN tableInPlanner on src1.id1 = tableInPlanner.id and src1.temp > 20 and tableInPlanner.hum < 60 WHERE src1.id1 > 111 GROUP BY TUMBLINGWINDOW(ss, 10)`,
- p: ProjectPlan{
- baseLogicalPlan: baseLogicalPlan{
- children: []LogicalPlan{
- JoinPlan{
- baseLogicalPlan: baseLogicalPlan{
- children: []LogicalPlan{
- JoinAlignPlan{
- 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(),
- },
- },
- condition: nil,
- wtype: xsql.TUMBLING_WINDOW,
- length: 10000,
- interval: 0,
- limit: 0,
- }.Init(),
- FilterPlan{
- baseLogicalPlan: baseLogicalPlan{
- children: []LogicalPlan{
- DataSourcePlan{
- name: "tableInPlanner",
- streamFields: []interface{}{
- &xsql.StreamField{
- Name: "hum",
- FieldType: &xsql.BasicType{Type: xsql.BIGINT},
- },
- &xsql.StreamField{
- Name: "id",
- FieldType: &xsql.BasicType{Type: xsql.BIGINT},
- },
- },
- streamStmt: streams["tableInPlanner"],
- metaFields: []string{},
- }.Init(),
- },
- },
- condition: &xsql.BinaryExpr{
- OP: xsql.LT,
- LHS: &xsql.FieldRef{Name: "hum", StreamName: "tableInPlanner"},
- RHS: &xsql.IntegerLiteral{Val: 60},
- },
- }.Init(),
- },
- },
- Emitters: []string{"tableInPlanner"},
- }.Init(),
- },
- },
- from: &xsql.Table{
- Name: "src1",
- },
- joins: []xsql.Join{
- {
- Name: "tableInPlanner",
- Alias: "",
- JoinType: xsql.INNER_JOIN,
- Expr: &xsql.BinaryExpr{
- LHS: &xsql.FieldRef{Name: "id1", StreamName: "src1"},
- OP: xsql.EQ,
- RHS: &xsql.FieldRef{Name: "id", StreamName: "tableInPlanner"},
- },
- },
- },
- }.Init(),
- },
- },
- fields: []xsql.Field{
- {
- Expr: &xsql.FieldRef{Name: "id1", StreamName: "src1"},
- Name: "id1",
- AName: ""},
- },
- isAggregate: false,
- sendMeta: false,
- }.Init(),
- }, { // 10 meta
- sql: `SELECT temp, meta(id) AS eid,meta(Humidity->Device) AS hdevice FROM src1 WHERE meta(device)="demo2"`,
- p: ProjectPlan{
- baseLogicalPlan: baseLogicalPlan{
- children: []LogicalPlan{
- FilterPlan{
- baseLogicalPlan: baseLogicalPlan{
- children: []LogicalPlan{
- DataSourcePlan{
- name: "src1",
- streamFields: []interface{}{
- &xsql.StreamField{
- Name: "temp",
- FieldType: &xsql.BasicType{Type: xsql.BIGINT},
- },
- },
- streamStmt: streams["src1"],
- metaFields: []string{"Humidity", "device", "id"},
- alias: xsql.Fields{
- xsql.Field{
- Expr: &xsql.Call{Name: "meta", Args: []xsql.Expr{&xsql.MetaRef{
- Name: "id",
- StreamName: xsql.DEFAULT_STREAM,
- }}},
- Name: "meta",
- AName: "eid",
- },
- xsql.Field{
- Expr: &xsql.Call{Name: "meta", Args: []xsql.Expr{
- &xsql.BinaryExpr{
- OP: xsql.ARROW,
- LHS: &xsql.MetaRef{Name: "Humidity", StreamName: xsql.DEFAULT_STREAM},
- RHS: &xsql.MetaRef{Name: "Device"},
- },
- }},
- Name: "meta",
- AName: "hdevice",
- },
- },
- }.Init(),
- },
- },
- condition: &xsql.BinaryExpr{
- LHS: &xsql.Call{
- Name: "meta",
- Args: []xsql.Expr{&xsql.MetaRef{
- Name: "device",
- StreamName: xsql.DEFAULT_STREAM,
- }},
- },
- OP: xsql.EQ,
- RHS: &xsql.StringLiteral{
- Val: "demo2",
- },
- },
- }.Init(),
- },
- },
- fields: []xsql.Field{
- {
- Expr: &xsql.FieldRef{Name: "temp", StreamName: "src1"},
- Name: "temp",
- AName: "",
- }, {
- Expr: &xsql.Call{Name: "meta", Args: []xsql.Expr{&xsql.MetaRef{
- Name: "id",
- StreamName: xsql.DEFAULT_STREAM,
- }}},
- Name: "meta",
- AName: "eid",
- }, {
- Expr: &xsql.Call{Name: "meta", Args: []xsql.Expr{
- &xsql.BinaryExpr{
- OP: xsql.ARROW,
- LHS: &xsql.MetaRef{Name: "Humidity", StreamName: xsql.DEFAULT_STREAM},
- RHS: &xsql.MetaRef{Name: "Device"},
- },
- }},
- Name: "meta",
- AName: "hdevice",
- },
- },
- 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 !reflect.DeepEqual(tt.err, common.Errstring(err)) {
- t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.sql, tt.err, err)
- } else 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)
- }
- }
- }
|