1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042 |
- 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"
- "github.com/gdexlab/go-render/render"
- "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(),
- }, { // 11 join with same name field and aliased
- sql: `SELECT src2.hum AS hum1, tableInPlanner.hum AS hum2 FROM src2 INNER JOIN tableInPlanner on id2 = id WHERE hum1 > hum2`,
- p: ProjectPlan{
- baseLogicalPlan: baseLogicalPlan{
- children: []LogicalPlan{
- JoinPlan{
- baseLogicalPlan: baseLogicalPlan{
- children: []LogicalPlan{
- JoinAlignPlan{
- 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"],
- alias: xsql.Fields{
- xsql.Field{
- Expr: &xsql.FieldRef{
- Name: "hum",
- StreamName: "src2",
- },
- Name: "hum",
- AName: "hum1",
- },
- },
- metaFields: []string{},
- }.Init(),
- 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"],
- alias: xsql.Fields{
- xsql.Field{
- Expr: &xsql.FieldRef{
- Name: "hum",
- StreamName: "tableInPlanner",
- },
- Name: "hum",
- AName: "hum2",
- },
- },
- metaFields: []string{},
- }.Init(),
- },
- },
- Emitters: []string{"tableInPlanner"},
- }.Init(),
- },
- },
- from: &xsql.Table{
- Name: "src2",
- },
- joins: []xsql.Join{
- {
- Name: "tableInPlanner",
- Alias: "",
- JoinType: xsql.INNER_JOIN,
- Expr: &xsql.BinaryExpr{
- RHS: &xsql.BinaryExpr{
- OP: xsql.EQ,
- LHS: &xsql.FieldRef{Name: "id2", StreamName: "src2"},
- RHS: &xsql.FieldRef{Name: "id", StreamName: "tableInPlanner"},
- },
- OP: xsql.AND,
- LHS: &xsql.BinaryExpr{
- OP: xsql.GT,
- LHS: &xsql.FieldRef{Name: "hum1", StreamName: "src2"},
- RHS: &xsql.FieldRef{Name: "hum2", StreamName: "tableInPlanner"},
- },
- },
- },
- },
- }.Init(),
- },
- },
- fields: []xsql.Field{
- {
- Expr: &xsql.FieldRef{
- Name: "hum",
- StreamName: "src2",
- },
- Name: "hum",
- AName: "hum1",
- }, {
- Expr: &xsql.FieldRef{
- Name: "hum",
- StreamName: "tableInPlanner",
- },
- Name: "hum",
- AName: "hum2",
- },
- },
- 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, render.AsCode(tt.p), render.AsCode(p))
- }
- }
- }
|