123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931 |
- // Copyright 2021-2022 EMQ Technologies Co., Ltd.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- package xsql
- import (
- "fmt"
- "github.com/lf-edge/ekuiper/internal/testx"
- "github.com/lf-edge/ekuiper/pkg/ast"
- "math"
- "reflect"
- "regexp"
- "strings"
- "testing"
- )
- // Ensure the parser can parse strings into Statement ASTs.
- func TestParser_ParseStatement(t *testing.T) {
- re1, _ := regexp.Compile("^foo$")
- re2, _ := regexp.Compile("^fo.o.*$")
- re3, _ := regexp.Compile("^foo\\\\%$")
- var tests = []struct {
- s string
- stmt *ast.SelectStatement
- err string
- }{
- {
- s: `SELECT name FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
- Name: "name",
- AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: "SELECT `select` FROM tbl",
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.FieldRef{Name: "select", StreamName: ast.DefaultStream},
- Name: "select",
- AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT name FROM topic/sensor1`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
- Name: "name",
- AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
- },
- },
- {
- s: `SELECT t1.name FROM topic/sensor1 AS t1`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"},
- Name: "name",
- AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
- },
- },
- {
- s: "SELECT t1.name FROM topic/sensor1 AS `join`",
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"},
- Name: "name",
- AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "join"}},
- },
- },
- {
- s: `SELECT name FROM topic/sensor1 AS t1`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
- Name: "name",
- AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
- },
- },
- {
- s: `SELECT name FROM topic/sensor1/#`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
- Name: "name",
- AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "topic/sensor1/#"}},
- },
- },
- {
- s: `SELECT name FROM topic/sensor1/# AS t2 `,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
- Name: "name",
- AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "topic/sensor1/#", Alias: "t2"}},
- },
- },
- {
- s: `SELECT name FROM /topic/sensor1/#`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
- Name: "name",
- AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "/topic/sensor1/#"}},
- },
- },
- {
- s: `SELECT name FROM /topic/sensor1/#/`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
- Name: "name",
- AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "/topic/sensor1/#/"}},
- },
- },
- {
- s: `SELECT name FROM /topic/sensor1/+/temp1/`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
- Name: "name",
- AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "/topic/sensor1/+/temp1/"}},
- },
- },
- {
- s: `SELECT name FROM topic/sensor1/+/temp`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
- Name: "name",
- AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "topic/sensor1/+/temp"}},
- },
- },
- {
- s: `SELECT * FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.Wildcard{Token: ast.ASTERISK},
- Name: "*",
- AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT a,b FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {Expr: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream}, Name: "a", AName: ""},
- {Expr: &ast.FieldRef{Name: "b", StreamName: ast.DefaultStream}, Name: "b", AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT a, b,c FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {Expr: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream}, Name: "a", AName: ""},
- {Expr: &ast.FieldRef{Name: "b", StreamName: ast.DefaultStream}, Name: "b", AName: ""},
- {Expr: &ast.FieldRef{Name: "c", StreamName: ast.DefaultStream}, Name: "c", AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT a AS alias FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{{Expr: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream}, Name: "a", AName: "alias"}},
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT a AS alias1, b as Alias2 FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {Expr: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream}, Name: "a", AName: "alias1"},
- {Expr: &ast.FieldRef{Name: "b", StreamName: ast.DefaultStream}, Name: "b", AName: "Alias2"},
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT LenGth("test") FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- AName: "",
- Name: "length",
- Expr: &ast.Call{
- Name: "length",
- Args: []ast.Expr{&ast.StringLiteral{Val: "test"}},
- },
- },
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT length(test) FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- AName: "",
- Name: "length",
- Expr: &ast.Call{
- Name: "length",
- Args: []ast.Expr{&ast.FieldRef{Name: "test", StreamName: ast.DefaultStream}},
- },
- },
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT sin(123) FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- AName: "",
- Name: "sin",
- Expr: &ast.Call{
- Name: "sin",
- Args: []ast.Expr{&ast.IntegerLiteral{Val: 123}},
- },
- },
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT lpad("abc", 123) FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- AName: "",
- Name: "lpad",
- Expr: &ast.Call{
- Name: "lpad",
- Args: []ast.Expr{&ast.StringLiteral{Val: "abc"}, &ast.IntegerLiteral{Val: 123}},
- },
- },
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT newuuid() FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- AName: "",
- Name: "newuuid",
- Expr: &ast.Call{
- Name: "newuuid",
- Args: nil,
- },
- },
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT indexof("abc", field1) FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- AName: "",
- Name: "indexof",
- Expr: &ast.Call{
- Name: "indexof",
- Args: []ast.Expr{
- &ast.StringLiteral{Val: "abc"},
- &ast.FieldRef{Name: "field1", StreamName: ast.DefaultStream},
- },
- },
- },
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT lpad(lower(test),1) FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- AName: "",
- Name: "lpad",
- Expr: &ast.Call{
- Name: "lpad",
- FuncId: 1,
- Args: []ast.Expr{
- &ast.Call{
- Name: "lower",
- Args: []ast.Expr{
- &ast.FieldRef{Name: "test", StreamName: ast.DefaultStream},
- },
- },
- &ast.IntegerLiteral{Val: 1},
- },
- },
- },
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT lpad(lower(test),1) AS field1 FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- AName: "field1",
- Name: "lpad",
- Expr: &ast.Call{
- Name: "lpad",
- FuncId: 1,
- Args: []ast.Expr{
- &ast.Call{
- Name: "lower",
- Args: []ast.Expr{
- &ast.FieldRef{Name: "test", StreamName: ast.DefaultStream},
- },
- },
- &ast.IntegerLiteral{Val: 1},
- },
- },
- },
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT length(lower("test")) FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- AName: "",
- Name: "length",
- Expr: &ast.Call{
- Name: "length",
- FuncId: 1,
- Args: []ast.Expr{
- &ast.Call{
- Name: "lower",
- Args: []ast.Expr{
- &ast.StringLiteral{Val: "test"},
- },
- },
- },
- },
- },
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT count(*) FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- AName: "",
- Name: "count",
- Expr: &ast.Call{
- Name: "count",
- Args: []ast.Expr{&ast.Wildcard{Token: ast.ASTERISK}},
- FuncType: ast.FuncTypeAgg,
- },
- },
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT count(*, f1) FROM tbl`,
- stmt: nil,
- err: `Expect 1 arguments but found 2.`,
- },
- {
- s: `SELECT lag() FROM tbl`,
- stmt: nil,
- err: `expect one two or three args but got 0`,
- },
- {
- s: `SELECT lag(a, b, "default value") FROM tbl`,
- stmt: nil,
- err: `Expect int type for parameter 2`,
- },
- {
- s: `SELECT lag(a, 2, 20) FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- AName: "",
- Name: "lag",
- Expr: &ast.Call{
- Name: "lag",
- Args: []ast.Expr{&ast.FieldRef{Name: "a", StreamName: ast.DefaultStream}, &ast.IntegerLiteral{Val: 2}, &ast.IntegerLiteral{Val: 20}},
- },
- },
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT deduplicate(temperature, false) FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- AName: "",
- Name: "deduplicate",
- Expr: &ast.Call{
- Name: "deduplicate",
- Args: []ast.Expr{&ast.Wildcard{Token: ast.ASTERISK}, &ast.FieldRef{Name: "temperature", StreamName: ast.DefaultStream}, &ast.BooleanLiteral{Val: false}},
- FuncType: ast.FuncTypeAgg,
- },
- },
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT "abc" FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{{AName: "", Name: "kuiper_field_0", Expr: &ast.StringLiteral{Val: "abc"}}},
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT "abc" AS field1 FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{{AName: "field1", Name: "", Expr: &ast.StringLiteral{Val: "abc"}}},
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT field0, "abc" AS field1, field2 FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {AName: "", Name: "field0", Expr: &ast.FieldRef{Name: "field0", StreamName: ast.DefaultStream}},
- {AName: "field1", Name: "", Expr: &ast.StringLiteral{Val: "abc"}},
- {AName: "", Name: "field2", Expr: &ast.FieldRef{Name: "field2", StreamName: ast.DefaultStream}}},
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT * AS alias FROM tbl`,
- stmt: nil,
- err: `alias is not supported for *`,
- },
- {
- s: `SELECT *, FROM tbl`,
- stmt: nil,
- err: `found "FROM", expected expression.`,
- },
- {
- s: `SELECTname FROM tbl`,
- stmt: nil,
- err: `Found "SELECTname", Expected SELECT.` + "\n",
- },
- {
- s: `SELECT abc FROM tbl WHERE abc > 12 `,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{{AName: "", Name: "abc", Expr: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream}}},
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- Condition: &ast.BinaryExpr{
- LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
- OP: ast.GT,
- RHS: &ast.IntegerLiteral{Val: 12},
- },
- },
- },
- {
- s: `SELECT abc FROM tbl WHERE abc = "hello" `,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{{AName: "", Name: "abc", Expr: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream}}},
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- Condition: &ast.BinaryExpr{
- LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
- OP: ast.EQ,
- RHS: &ast.StringLiteral{Val: "hello"},
- },
- },
- },
- {
- s: `SELECT t1.abc FROM tbl AS t1 WHERE t1.abc = "hello" `,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{{AName: "", Name: "abc", Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "abc"}}},
- Sources: []ast.Source{&ast.Table{Name: "tbl", Alias: "t1"}},
- Condition: &ast.BinaryExpr{
- LHS: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "abc"},
- OP: ast.EQ,
- RHS: &ast.StringLiteral{Val: "hello"},
- },
- },
- },
- {
- s: `SELECT abc, "fff" AS fa FROM tbl WHERE fa >= 5 `,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{{AName: "", Name: "abc", Expr: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream}}, {AName: "fa", Name: "", Expr: &ast.StringLiteral{Val: "fff"}}},
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- Condition: &ast.BinaryExpr{
- LHS: &ast.FieldRef{Name: "fa", StreamName: ast.DefaultStream},
- OP: ast.GTE,
- RHS: &ast.IntegerLiteral{Val: 5},
- },
- },
- },
- {
- s: `SELECT field2 FROM tbl WHERE field2 != 5 `,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{{AName: "", Name: "field2", Expr: &ast.FieldRef{Name: "field2", StreamName: ast.DefaultStream}}},
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- Condition: &ast.BinaryExpr{
- LHS: &ast.FieldRef{Name: "field2", StreamName: ast.DefaultStream},
- OP: ast.NEQ,
- RHS: &ast.IntegerLiteral{Val: 5},
- },
- },
- },
- {
- s: `SELECT field2 FROM tbl WHERE field2 ! = 5 `, //Add space char in expression
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{{AName: "", Name: "field2", Expr: &ast.FieldRef{Name: "field2", StreamName: ast.DefaultStream}}},
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- Condition: &ast.BinaryExpr{
- LHS: &ast.FieldRef{Name: "field2", StreamName: ast.DefaultStream},
- OP: ast.NEQ,
- RHS: &ast.IntegerLiteral{Val: 5},
- },
- },
- },
- {
- s: `SELECT *f FROM tbl`,
- stmt: nil,
- err: `found "f", expected FROM.`,
- },
- ////TODO
- //{
- // s: `SELECT *from FROM tbl`,
- // stmt: nil,
- // err: `found "f", expected FROM.`,
- //},
- {
- s: `SELECT abc+2 FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- AName: "",
- Name: "kuiper_field_0",
- Expr: &ast.BinaryExpr{
- LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
- OP: ast.ADD,
- RHS: &ast.IntegerLiteral{Val: 2},
- },
- },
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT t1.abc+2 FROM tbl AS t1`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- AName: "",
- Name: "kuiper_field_0",
- Expr: &ast.BinaryExpr{
- LHS: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "abc"},
- OP: ast.ADD,
- RHS: &ast.IntegerLiteral{Val: 2},
- },
- },
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl", Alias: "t1"}},
- },
- },
- {
- s: `SELECT abc + "hello" FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- AName: "",
- Name: "kuiper_field_0",
- Expr: &ast.BinaryExpr{
- LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
- OP: ast.ADD,
- RHS: &ast.StringLiteral{Val: "hello"},
- },
- },
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT abc*2 + 3 FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- AName: "",
- Name: "kuiper_field_0",
- Expr: &ast.BinaryExpr{
- LHS: &ast.BinaryExpr{
- LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
- OP: ast.MUL,
- RHS: &ast.IntegerLiteral{Val: 2},
- },
- OP: ast.ADD,
- RHS: &ast.IntegerLiteral{Val: 3},
- },
- },
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT ln(abc*2 + 3) FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- AName: "",
- Name: "ln",
- Expr: &ast.Call{
- Name: "ln",
- Args: []ast.Expr{
- &ast.BinaryExpr{
- LHS: &ast.BinaryExpr{
- LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
- OP: ast.MUL,
- RHS: &ast.IntegerLiteral{Val: 2},
- },
- OP: ast.ADD,
- RHS: &ast.IntegerLiteral{Val: 3},
- },
- },
- },
- },
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT ln(t1.abc*2 + 3) FROM tbl AS t1`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- AName: "",
- Name: "ln",
- Expr: &ast.Call{
- Name: "ln",
- Args: []ast.Expr{
- &ast.BinaryExpr{
- LHS: &ast.BinaryExpr{
- LHS: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "abc"},
- OP: ast.MUL,
- RHS: &ast.IntegerLiteral{Val: 2},
- },
- OP: ast.ADD,
- RHS: &ast.IntegerLiteral{Val: 3},
- },
- },
- },
- },
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl", Alias: "t1"}},
- },
- },
- {
- s: `SELECT lpad("param2", abc*2 + 3) FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- AName: "",
- Name: "lpad",
- Expr: &ast.Call{
- Name: "lpad",
- Args: []ast.Expr{
- &ast.StringLiteral{Val: "param2"},
- &ast.BinaryExpr{
- LHS: &ast.BinaryExpr{
- LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
- OP: ast.MUL,
- RHS: &ast.IntegerLiteral{Val: 2},
- },
- OP: ast.ADD,
- RHS: &ast.IntegerLiteral{Val: 3},
- },
- },
- },
- },
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT 0.2 FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- AName: "",
- Name: "kuiper_field_0",
- Expr: &ast.NumberLiteral{Val: 0.2},
- },
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT .2 FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- AName: "",
- Name: "kuiper_field_0",
- Expr: &ast.NumberLiteral{Val: 0.2},
- },
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT sin(.2) FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- AName: "",
- Name: "sin",
- Expr: &ast.Call{
- Name: "sin",
- Args: []ast.Expr{&ast.NumberLiteral{Val: 0.2}},
- },
- },
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT power(.2, 4) FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- AName: "",
- Name: "power",
- Expr: &ast.Call{
- Name: "power",
- Args: []ast.Expr{&ast.NumberLiteral{Val: 0.2}, &ast.IntegerLiteral{Val: 4}},
- },
- },
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT power(.2, 4) AS f1 FROM tbl WHERE f1 > 2.2`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- AName: "f1",
- Name: "power",
- Expr: &ast.Call{
- Name: "power",
- Args: []ast.Expr{&ast.NumberLiteral{Val: 0.2}, &ast.IntegerLiteral{Val: 4}},
- },
- },
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- Condition: &ast.BinaryExpr{
- LHS: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
- OP: ast.GT,
- RHS: &ast.NumberLiteral{Val: 2.2},
- },
- },
- },
- {
- s: `SELECT power(.2, 4) AS f1 FROM tbl WHERE f1 BETWEEN 1 AND 2`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- AName: "f1",
- Name: "power",
- Expr: &ast.Call{
- Name: "power",
- Args: []ast.Expr{&ast.NumberLiteral{Val: 0.2}, &ast.IntegerLiteral{Val: 4}},
- },
- },
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- Condition: &ast.BinaryExpr{
- LHS: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
- OP: ast.BETWEEN,
- RHS: &ast.BetweenExpr{
- Lower: &ast.IntegerLiteral{Val: 1},
- Higher: &ast.IntegerLiteral{Val: 2},
- },
- },
- },
- },
- {
- s: `SELECT a FROM tbl WHERE f1 > 4 AND f2 BETWEEN 1 AND 2`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- AName: "",
- Name: "a",
- Expr: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream},
- },
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- Condition: &ast.BinaryExpr{
- OP: ast.AND,
- LHS: &ast.BinaryExpr{
- LHS: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
- OP: ast.GT,
- RHS: &ast.IntegerLiteral{Val: 4},
- },
- RHS: &ast.BinaryExpr{
- LHS: &ast.FieldRef{Name: "f2", StreamName: ast.DefaultStream},
- OP: ast.BETWEEN,
- RHS: &ast.BetweenExpr{
- Lower: &ast.IntegerLiteral{Val: 1},
- Higher: &ast.IntegerLiteral{Val: 2},
- },
- },
- },
- },
- },
- {
- s: `SELECT a FROM tbl WHERE f1 NOT BETWEEN b AND c AND f2 BETWEEN 1 AND 2 AND f3 > 4`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- AName: "",
- Name: "a",
- Expr: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream},
- },
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- Condition: &ast.BinaryExpr{
- OP: ast.AND,
- LHS: &ast.BinaryExpr{
- OP: ast.AND,
- LHS: &ast.BinaryExpr{
- LHS: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
- OP: ast.NOTBETWEEN,
- RHS: &ast.BetweenExpr{
- Lower: &ast.FieldRef{Name: "b", StreamName: ast.DefaultStream},
- Higher: &ast.FieldRef{Name: "c", StreamName: ast.DefaultStream},
- },
- },
- RHS: &ast.BinaryExpr{
- LHS: &ast.FieldRef{Name: "f2", StreamName: ast.DefaultStream},
- OP: ast.BETWEEN,
- RHS: &ast.BetweenExpr{
- Lower: &ast.IntegerLiteral{Val: 1},
- Higher: &ast.IntegerLiteral{Val: 2},
- },
- },
- },
- RHS: &ast.BinaryExpr{
- OP: ast.GT,
- LHS: &ast.FieldRef{Name: "f3", StreamName: ast.DefaultStream},
- RHS: &ast.IntegerLiteral{Val: 4},
- },
- },
- },
- },
- {
- s: `SELECT a FROM tbl WHERE f1 NOT BETWEEN b`,
- err: "expect AND expression after between but found EOF",
- },
- {
- s: `SELECT a FROM tbl WHERE f1 NOT BETWEEN 1 OR 2`,
- err: "expect AND expression after between but found OR",
- },
- {
- s: `SELECT a FROM tbl WHERE a LIKE "foo"`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- AName: "",
- Name: "a",
- Expr: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream},
- },
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- Condition: &ast.BinaryExpr{
- LHS: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream},
- OP: ast.LIKE,
- RHS: &ast.LikePattern{Expr: &ast.StringLiteral{Val: "foo"}, Pattern: re1},
- },
- },
- },
- {
- s: `SELECT a FROM tbl WHERE a NOT LIKE "fo_o%"`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- AName: "",
- Name: "a",
- Expr: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream},
- },
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- Condition: &ast.BinaryExpr{
- LHS: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream},
- OP: ast.NOTLIKE,
- RHS: &ast.LikePattern{Expr: &ast.StringLiteral{Val: "fo_o%"}, Pattern: re2},
- },
- },
- },
- {
- s: `SELECT a FROM tbl WHERE a LIKE "foo\\%"`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- AName: "",
- Name: "a",
- Expr: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream},
- },
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- Condition: &ast.BinaryExpr{
- LHS: &ast.FieldRef{Name: "a", StreamName: ast.DefaultStream},
- OP: ast.LIKE,
- RHS: &ast.LikePattern{Expr: &ast.StringLiteral{Val: "foo\\%"}, Pattern: re3},
- },
- },
- },
- {
- s: `SELECT deviceId, name FROM topic/sensor1 WHERE deviceId=1 AND name = "dname"`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {Expr: &ast.FieldRef{Name: "deviceId", StreamName: ast.DefaultStream}, Name: "deviceId", AName: ""},
- {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
- Condition: &ast.BinaryExpr{
- LHS: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "deviceId", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.IntegerLiteral{Val: 1}},
- OP: ast.AND,
- RHS: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
- },
- },
- },
- {
- s: `SELECT deviceId, name FROM topic/sensor1 AS t1 WHERE t1.deviceId=1 AND t1.name = "dname"`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {Expr: &ast.FieldRef{Name: "deviceId", StreamName: ast.DefaultStream}, Name: "deviceId", AName: ""},
- {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
- Condition: &ast.BinaryExpr{
- LHS: &ast.BinaryExpr{LHS: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "deviceId"}, OP: ast.EQ, RHS: &ast.IntegerLiteral{Val: 1}},
- OP: ast.AND,
- RHS: &ast.BinaryExpr{LHS: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
- },
- },
- },
- {
- s: `SELECT temp AS t, name FROM topic/sensor1 WHERE t> = 20.5 OR name = "dname"`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
- {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
- Condition: &ast.BinaryExpr{
- LHS: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "t", StreamName: ast.DefaultStream}, OP: ast.GTE, RHS: &ast.NumberLiteral{Val: 20.5}},
- OP: ast.OR,
- RHS: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
- },
- },
- },
- {
- s: `SELECT temp AS t, name FROM topic/sensor1 WHERE t IN arraySet OR name IN arraySet`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
- {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
- Condition: &ast.BinaryExpr{
- LHS: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "t", StreamName: ast.DefaultStream}, OP: ast.IN, RHS: &ast.FieldRef{Name: "arraySet", StreamName: ast.DefaultStream}},
- OP: ast.OR,
- RHS: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.IN, RHS: &ast.FieldRef{Name: "arraySet", StreamName: ast.DefaultStream}},
- },
- },
- },
- {
- s: `SELECT temp AS t, name FROM topic/sensor1 WHERE t NOT IN arraySet OR name NOT IN arraySet`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
- {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
- Condition: &ast.BinaryExpr{
- LHS: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "t", StreamName: ast.DefaultStream}, OP: ast.NOTIN, RHS: &ast.FieldRef{Name: "arraySet", StreamName: ast.DefaultStream}},
- OP: ast.OR,
- RHS: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.NOTIN, RHS: &ast.FieldRef{Name: "arraySet", StreamName: ast.DefaultStream}},
- },
- },
- },
- {
- s: `SELECT temp AS t, name FROM topic/sensor1 WHERE t IN (20.5, 20.4) OR name IN ("dname", "ename")`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
- {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
- Condition: &ast.BinaryExpr{
- LHS: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "t", StreamName: ast.DefaultStream}, OP: ast.IN, RHS: &ast.ValueSetExpr{LiteralExprs: []ast.Expr{&ast.NumberLiteral{Val: 20.5}, &ast.NumberLiteral{Val: 20.4}}}},
- OP: ast.OR,
- RHS: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.IN, RHS: &ast.ValueSetExpr{LiteralExprs: []ast.Expr{&ast.StringLiteral{Val: "dname"}, &ast.StringLiteral{Val: "ename"}}}},
- },
- },
- },
- {
- s: `SELECT temp AS t, name FROM topic/sensor1 WHERE t NOT IN (20.5, 20.4) OR name IN ("dname", "ename")`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
- {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
- Condition: &ast.BinaryExpr{
- LHS: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "t", StreamName: ast.DefaultStream}, OP: ast.NOTIN, RHS: &ast.ValueSetExpr{LiteralExprs: []ast.Expr{&ast.NumberLiteral{Val: 20.5}, &ast.NumberLiteral{Val: 20.4}}}},
- OP: ast.OR,
- RHS: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.IN, RHS: &ast.ValueSetExpr{LiteralExprs: []ast.Expr{&ast.StringLiteral{Val: "dname"}, &ast.StringLiteral{Val: "ename"}}}},
- },
- },
- },
- {
- s: `SELECT temp AS t, name FROM topic/sensor1 WHERE name = "dname" GROUP BY name`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
- {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
- Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
- Dimensions: ast.Dimensions{ast.Dimension{Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}}},
- },
- },
- {
- s: `SELECT temp AS t, name FROM topic/sensor1 WHERE name = "dname" GROUP BY name HAVING count(name) > 3`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
- {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
- Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
- Dimensions: ast.Dimensions{ast.Dimension{Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}}},
- Having: &ast.BinaryExpr{LHS: &ast.Call{Name: "count", Args: []ast.Expr{&ast.FieldRef{StreamName: ast.DefaultStream, Name: "name"}}, FuncType: ast.FuncTypeAgg}, OP: ast.GT, RHS: &ast.IntegerLiteral{Val: 3}},
- },
- },
- {
- s: `SELECT temp AS t, name FROM topic/sensor1 WHERE name = "dname" HAVING count(name) > 3`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
- {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
- Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
- Having: &ast.BinaryExpr{LHS: &ast.Call{Name: "count", Args: []ast.Expr{&ast.FieldRef{StreamName: ast.DefaultStream, Name: "name"}}, FuncType: ast.FuncTypeAgg}, OP: ast.GT, RHS: &ast.IntegerLiteral{Val: 3}},
- },
- },
- {
- s: `SELECT id,AVG(data) FROM t GROUP BY SUM(data)>10`,
- stmt: nil,
- err: "Not allowed to call aggregate functions in GROUP BY clause.",
- },
- {
- s: `SELECT temp AS t, name FROM topic/sensor1 WHERE count(name) = 3`,
- stmt: nil,
- err: "Not allowed to call aggregate functions in WHERE clause.",
- },
- {
- s: `SELECT s1.temp AS t, name FROM topic/sensor1 AS s1 WHERE t = "dname" GROUP BY s1.temp`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {Expr: &ast.FieldRef{StreamName: "s1", Name: "temp"}, Name: "temp", AName: "t"},
- {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "s1"}},
- Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "t", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
- Dimensions: ast.Dimensions{ast.Dimension{Expr: &ast.FieldRef{StreamName: "s1", Name: "temp"}}},
- },
- },
- {
- s: `SELECT temp AS t, name FROM topic/sensor1 WHERE name = "dname" GROUP BY lpad(name,1)`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
- {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
- Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
- Dimensions: ast.Dimensions{ast.Dimension{
- Expr: &ast.Call{Name: "lpad", Args: []ast.Expr{&ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, &ast.IntegerLiteral{Val: 1}}},
- },
- },
- },
- },
- {
- s: `SELECT temp AS t, name FROM topic/sensor1 AS s1 WHERE name = "dname" GROUP BY lpad(s1.name,1)`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
- {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "s1"}},
- Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
- Dimensions: ast.Dimensions{ast.Dimension{
- Expr: &ast.Call{Name: "lpad", Args: []ast.Expr{&ast.FieldRef{StreamName: ast.StreamName("s1"), Name: "name"}, &ast.IntegerLiteral{Val: 1}}},
- },
- },
- },
- },
- {
- s: `SELECT temp AS t, name FROM topic/sensor1 WHERE name = "dname" GROUP BY lpad(name,1) ORDER BY name`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
- {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
- Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
- Dimensions: ast.Dimensions{
- ast.Dimension{
- Expr: &ast.Call{Name: "lpad", Args: []ast.Expr{
- &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
- &ast.IntegerLiteral{Val: 1}},
- },
- },
- },
- SortFields: []ast.SortField{{Uname: "name", Name: "name", Ascending: true, FieldExpr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}}},
- },
- },
- {
- s: `SELECT temp AS t, name FROM topic/sensor1 AS s1 WHERE s1.name = "dname" GROUP BY lpad(s1.name,1) ORDER BY s1.name`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
- {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "s1"}},
- Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{StreamName: ast.StreamName("s1"), Name: "name"}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
- Dimensions: ast.Dimensions{
- ast.Dimension{
- Expr: &ast.Call{Name: "lpad", Args: []ast.Expr{
- &ast.FieldRef{StreamName: ast.StreamName("s1"), Name: "name"},
- &ast.IntegerLiteral{Val: 1}},
- },
- },
- },
- SortFields: []ast.SortField{{Uname: "s1\007name", Name: "name", StreamName: ast.StreamName("s1"), Ascending: true, FieldExpr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}}},
- },
- },
- {
- s: `SELECT temp AS t, name FROM topic/sensor1 WHERE name = "dname" GROUP BY lpad(name,1) ORDER BY name DESC`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {Expr: &ast.FieldRef{Name: "temp", StreamName: ast.DefaultStream}, Name: "temp", AName: "t"},
- {Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, Name: "name", AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
- Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.StringLiteral{Val: "dname"}},
- Dimensions: ast.Dimensions{
- ast.Dimension{
- Expr: &ast.Call{Name: "lpad", Args: []ast.Expr{
- &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
- &ast.IntegerLiteral{Val: 1}},
- },
- },
- },
- SortFields: []ast.SortField{{Uname: "name", Name: "name", Ascending: false, FieldExpr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}}},
- },
- },
- {
- s: `SELECT * FROM topic/sensor1 ORDER BY name DESC`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.Wildcard{Token: ast.ASTERISK},
- Name: "*",
- AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
- SortFields: []ast.SortField{{Uname: "name", Name: "name", Ascending: false, FieldExpr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}}},
- },
- },
- {
- s: `SELECT * FROM topic/sensor1 ORDER BY name DESC, name2 ASC`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.Wildcard{Token: ast.ASTERISK},
- Name: "*",
- AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
- SortFields: []ast.SortField{{Uname: "name", Name: "name", Ascending: false, FieldExpr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}}, {Uname: "name2", Name: "name2", Ascending: true, FieldExpr: &ast.FieldRef{Name: "name2", StreamName: ast.DefaultStream}}},
- },
- },
- {
- s: `SELECT * FROM topic/sensor1 GROUP BY name, name2,power(name3,1.8) ORDER BY name DESC, name2 ASC`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.Wildcard{Token: ast.ASTERISK},
- Name: "*",
- AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
- Dimensions: ast.Dimensions{
- ast.Dimension{Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}},
- ast.Dimension{Expr: &ast.FieldRef{Name: "name2", StreamName: ast.DefaultStream}},
- ast.Dimension{
- Expr: &ast.Call{Name: "power", Args: []ast.Expr{
- &ast.FieldRef{Name: "name3", StreamName: ast.DefaultStream},
- &ast.NumberLiteral{Val: 1.8}},
- },
- },
- },
- SortFields: []ast.SortField{{Uname: "name", Name: "name", Ascending: false, FieldExpr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}}, {Uname: "name2", Name: "name2", Ascending: true, FieldExpr: &ast.FieldRef{Name: "name2", StreamName: ast.DefaultStream}}},
- },
- },
- //{
- // s: `SELECT .2sd FROM tbl`,
- // stmt: &SelectStatement{
- // Fields: []Field{
- // Field{
- // AName:"",
- // Expr: &NumberLiteral{Val: 0.2},
- // },
- // },
- // TableName: "tbl",
- // },
- //},
- {
- s: `SELECT name FROM tbl/*SELECT comment FROM testComments*/`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
- Name: "name",
- AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `/*SELECT comment FROM testComments*/SELECT name FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
- Name: "name",
- AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT name /*SELECT comment FROM testComments*/ FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
- Name: "name",
- AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT true AS f1, FALSE as f2 FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {AName: "f1", Name: "", Expr: &ast.BooleanLiteral{Val: true}},
- {AName: "f2", Name: "", Expr: &ast.BooleanLiteral{Val: false}},
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT true AS f1 FROM tbl WHERE f2 = true`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {AName: "f1", Name: "", Expr: &ast.BooleanLiteral{Val: true}},
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "f2", StreamName: ast.DefaultStream}, OP: ast.EQ, RHS: &ast.BooleanLiteral{Val: true}},
- },
- },
- {
- s: `SELECT indexof(field1, "abc") FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- AName: "",
- Name: "indexof",
- Expr: &ast.Call{
- Name: "indexof",
- Args: []ast.Expr{&ast.FieldRef{Name: "field1", StreamName: ast.DefaultStream}, &ast.StringLiteral{Val: "abc"}},
- },
- },
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- //The negative value expression support.
- {
- s: `SELECT -3 AS t1 FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.IntegerLiteral{Val: -3},
- Name: "",
- AName: "t1"},
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT - 3 AS t1 FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.IntegerLiteral{Val: -3},
- Name: "",
- AName: "t1"},
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT -. 3 AS t1 FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.NumberLiteral{Val: -.3},
- Name: "",
- AName: "t1"},
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT -3.3 AS t1 FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.NumberLiteral{Val: -3.3},
- Name: "",
- AName: "t1"},
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT sample(-.3,) FROM tbl`,
- stmt: nil,
- err: "function sample not found",
- },
- {
- s: `select timestamp() as tp from demo`,
- stmt: nil,
- err: "function timestamp not found",
- },
- {
- s: `select tstamp() as tp from demo`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.Call{
- Name: "tstamp",
- Args: nil,
- },
- Name: "tstamp",
- AName: "tp"},
- },
- Sources: []ast.Source{&ast.Table{Name: "demo"}},
- },
- err: "",
- },
- {
- s: "SELECT `half FROM tb",
- stmt: nil,
- err: `found "EOF", expected FROM.`,
- },
- {
- s: "SELECT `space var` FROM tbl",
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.FieldRef{Name: "space var", StreamName: ast.DefaultStream},
- Name: "space var",
- AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: "SELECT `中文 Chinese` FROM tbl",
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.FieldRef{Name: "中文 Chinese", StreamName: ast.DefaultStream},
- Name: "中文 Chinese",
- AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- }, {
- s: "SELECT CASE temperature WHEN 25 THEN \"bingo\" WHEN 30 THEN \"high\" ELSE \"low\" END as label, humidity FROM tbl",
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.CaseExpr{
- Value: &ast.FieldRef{Name: "temperature", StreamName: ast.DefaultStream},
- WhenClauses: []*ast.WhenClause{
- {
- Expr: &ast.IntegerLiteral{Val: 25},
- Result: &ast.StringLiteral{Val: "bingo"},
- }, {
- Expr: &ast.IntegerLiteral{Val: 30},
- Result: &ast.StringLiteral{Val: "high"},
- },
- },
- ElseClause: &ast.StringLiteral{Val: "low"},
- },
- Name: "",
- AName: "label",
- }, {
- Expr: &ast.FieldRef{Name: "humidity", StreamName: ast.DefaultStream},
- Name: "humidity",
- AName: "",
- },
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- }, {
- s: "SELECT CASE temperature WHEN 25 THEN \"bingo\" WHEN 30 THEN \"high\" END as label, humidity FROM tbl",
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.CaseExpr{
- Value: &ast.FieldRef{Name: "temperature", StreamName: ast.DefaultStream},
- WhenClauses: []*ast.WhenClause{
- {
- Expr: &ast.IntegerLiteral{Val: 25},
- Result: &ast.StringLiteral{Val: "bingo"},
- }, {
- Expr: &ast.IntegerLiteral{Val: 30},
- Result: &ast.StringLiteral{Val: "high"},
- },
- },
- ElseClause: nil,
- },
- Name: "",
- AName: "label",
- }, {
- Expr: &ast.FieldRef{Name: "humidity", StreamName: ast.DefaultStream},
- Name: "humidity",
- AName: "",
- },
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- }, {
- s: "SELECT CASE temperature ELSE \"low\" END as label, humidity FROM tbl",
- stmt: nil,
- err: "invalid CASE expression, WHEN expected before ELSE",
- }, {
- s: "SELECT CASE WHEN temperature > 30 THEN \"high\" ELSE \"low\" END as label, humidity FROM tbl",
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.CaseExpr{
- Value: nil,
- WhenClauses: []*ast.WhenClause{
- {
- Expr: &ast.BinaryExpr{
- OP: ast.GT,
- LHS: &ast.FieldRef{Name: "temperature", StreamName: ast.DefaultStream},
- RHS: &ast.IntegerLiteral{Val: 30},
- },
- Result: &ast.StringLiteral{Val: "high"},
- },
- },
- ElseClause: &ast.StringLiteral{Val: "low"},
- },
- Name: "",
- AName: "label",
- }, {
- Expr: &ast.FieldRef{Name: "humidity", StreamName: ast.DefaultStream},
- Name: "humidity",
- AName: "",
- },
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- }, {
- s: "SELECT CASE WHEN 30 THEN \"high\" ELSE \"low\" END as label, humidity FROM tbl",
- stmt: nil,
- err: "invalid CASE expression, WHEN expression must be a bool condition",
- }, {
- s: `SELECT count(*)-10 FROM demo`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- AName: "",
- Name: "kuiper_field_0",
- Expr: &ast.BinaryExpr{
- OP: ast.SUB,
- LHS: &ast.Call{
- Name: "count",
- Args: []ast.Expr{
- &ast.Wildcard{Token: ast.ASTERISK},
- },
- FuncType: ast.FuncTypeAgg,
- },
- RHS: &ast.IntegerLiteral{Val: 10},
- },
- },
- },
- Sources: []ast.Source{&ast.Table{Name: "demo"}},
- },
- },
- {
- s: `SELECT -abc FROM demo`,
- stmt: nil,
- err: "found \"-\", expected expression.",
- },
- {
- s: `SELECT meta(*) FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- AName: "",
- Name: "meta",
- Expr: &ast.Call{
- Name: "meta",
- Args: []ast.Expr{
- &ast.MetaRef{
- Name: "*",
- StreamName: ast.DefaultStream,
- },
- },
- },
- },
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- }, {
- s: `SELECT changed_cols("",true,a,b,c) FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- AName: "",
- Name: "changed_cols",
- Expr: &ast.Call{
- Name: "changed_cols",
- Args: []ast.Expr{
- &ast.ColFuncField{
- Name: "",
- Expr: &ast.StringLiteral{Val: ""},
- },
- &ast.ColFuncField{
- Name: "",
- Expr: &ast.BooleanLiteral{Val: true},
- },
- &ast.ColFuncField{Name: "a", Expr: &ast.FieldRef{
- StreamName: ast.DefaultStream,
- Name: "a",
- }},
- &ast.ColFuncField{Name: "b", Expr: &ast.FieldRef{
- StreamName: ast.DefaultStream,
- Name: "b",
- }},
- &ast.ColFuncField{Name: "c", Expr: &ast.FieldRef{
- StreamName: ast.DefaultStream,
- Name: "c",
- }},
- },
- FuncType: ast.FuncTypeCols,
- },
- },
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- }, {
- s: `SELECT changed_cols("",true,a,*,c) FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- AName: "",
- Name: "changed_cols",
- Expr: &ast.Call{
- Name: "changed_cols",
- Args: []ast.Expr{
- &ast.ColFuncField{
- Name: "",
- Expr: &ast.StringLiteral{Val: ""},
- },
- &ast.ColFuncField{
- Name: "",
- Expr: &ast.BooleanLiteral{Val: true},
- },
- &ast.ColFuncField{Name: "a", Expr: &ast.FieldRef{
- StreamName: ast.DefaultStream,
- Name: "a",
- }},
- &ast.ColFuncField{Name: "*", Expr: &ast.Wildcard{
- Token: ast.ASTERISK,
- }},
- &ast.ColFuncField{Name: "c", Expr: &ast.FieldRef{
- StreamName: ast.DefaultStream,
- Name: "c",
- }},
- },
- FuncType: ast.FuncTypeCols,
- },
- },
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- }, {
- s: `SELECT a FROM tbl WHERE changed_cols("",true,a,b,c) > 3`,
- err: "function changed_cols can only be used inside the select clause",
- },
- {
- s: `SELECT ".*(/)(?!.*\1)" FROM topic/sensor1 AS t1`,
- err: `found "invalid string: \".*(/)(?!.*\\1)\"", expected expression.`,
- },
- {
- s: `SELECT name FROM tbl WHERE name IN ("A", "B","C")`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
- Name: "name",
- AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- Condition: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream}, OP: ast.IN, RHS: &ast.ValueSetExpr{LiteralExprs: []ast.Expr{&ast.StringLiteral{Val: "A"}, &ast.StringLiteral{Val: "B"}, &ast.StringLiteral{Val: "C"}}}},
- },
- },
- {
- s: `SELECT name FROM tbl WHERE name IN ()`,
- err: `expect elements for IN expression, but found ")", expected expression.`,
- },
- {
- s: `SELECT name FROM tbl WHERE name IN (abc,def OR name in (abc)`,
- err: `expect ) for IN expression, but got "EOF"`,
- },
- {
- s: `SELECT lag(name) OVER (PARTITION BY device) FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.Call{
- Name: "lag",
- FuncId: 0,
- Args: []ast.Expr{
- &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
- },
- Partition: &ast.PartitionExpr{
- Exprs: []ast.Expr{
- &ast.FieldRef{Name: "device", StreamName: ast.DefaultStream},
- },
- },
- },
- Name: "lag",
- AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT name OVER (PARTITION BY device) FROM tbl`,
- err: `found "OVER", expected FROM.`,
- },
- {
- s: `SELECT avg(name) OVER (PARTITION BY device) FROM tbl`,
- err: `Found OVER after non analytic function avg`,
- },
- {
- s: `SELECT name FROM tbl WHERE lag(name) OVER (PARTITION BY device, groupName) > 3`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
- Name: "name",
- AName: ""},
- },
- Condition: &ast.BinaryExpr{
- LHS: &ast.Call{
- Name: "lag",
- FuncId: 0,
- Args: []ast.Expr{
- &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
- },
- Partition: &ast.PartitionExpr{
- Exprs: []ast.Expr{
- &ast.FieldRef{Name: "device", StreamName: ast.DefaultStream},
- &ast.FieldRef{Name: "groupName", StreamName: ast.DefaultStream},
- },
- },
- },
- OP: ast.GT,
- RHS: &ast.IntegerLiteral{Val: 3},
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT lag(name) OVER (PARTITION BY device) as ll FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.Call{
- Name: "lag",
- FuncId: 0,
- Args: []ast.Expr{
- &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
- },
- Partition: &ast.PartitionExpr{
- Exprs: []ast.Expr{
- &ast.FieldRef{Name: "device", StreamName: ast.DefaultStream},
- },
- },
- },
- Name: "lag",
- AName: "ll"},
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT lag(name) OVER (PARTITION BY device WHEN abc > 12) as ll FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.Call{
- Name: "lag",
- FuncId: 0,
- Args: []ast.Expr{
- &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
- },
- Partition: &ast.PartitionExpr{
- Exprs: []ast.Expr{
- &ast.FieldRef{Name: "device", StreamName: ast.DefaultStream},
- },
- },
- WhenExpr: &ast.BinaryExpr{
- LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
- OP: ast.GT,
- RHS: &ast.IntegerLiteral{Val: 12},
- },
- },
- Name: "lag",
- AName: "ll"},
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT lag(name) OVER (PARTITION BY device WHEN had_changed(true, StatusCode)) as ll FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.Call{
- Name: "lag",
- FuncId: 0,
- Args: []ast.Expr{
- &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
- },
- Partition: &ast.PartitionExpr{
- Exprs: []ast.Expr{
- &ast.FieldRef{Name: "device", StreamName: ast.DefaultStream},
- },
- },
- WhenExpr: &ast.Call{
- Name: "had_changed",
- FuncId: 1,
- Args: []ast.Expr{&ast.BooleanLiteral{Val: true}, &ast.FieldRef{Name: "StatusCode", StreamName: ast.DefaultStream}},
- },
- },
- Name: "lag",
- AName: "ll"},
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT lag(name) OVER (WHEN had_changed(true, StatusCode)) as ll FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.Call{
- Name: "lag",
- FuncId: 0,
- Args: []ast.Expr{
- &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
- },
- WhenExpr: &ast.Call{
- Name: "had_changed",
- FuncId: 1,
- Args: []ast.Expr{&ast.BooleanLiteral{Val: true}, &ast.FieldRef{Name: "StatusCode", StreamName: ast.DefaultStream}},
- },
- },
- Name: "lag",
- AName: "ll"},
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT name OVER (WHEN a > b) FROM tbl`,
- err: `found "OVER", expected FROM.`,
- },
- {
- s: `SELECT avg(name) OVER (WHEN a > b) FROM tbl`,
- err: `Found OVER after non analytic function avg`,
- },
- {
- s: `SELECT *, name, lower(name) as ln FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.Wildcard{
- Token: ast.ASTERISK,
- },
- Name: "*",
- },
- {
- Expr: &ast.FieldRef{
- Name: "name",
- StreamName: ast.DefaultStream,
- },
- Name: "name",
- AName: "",
- },
- {
- Expr: &ast.Call{
- Name: "lower",
- FuncId: 0,
- Args: []ast.Expr{
- &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
- },
- },
- Name: "lower",
- AName: "ln",
- },
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT name, * FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.FieldRef{
- Name: "name",
- StreamName: ast.DefaultStream,
- },
- Name: "name",
- AName: "",
- },
- {
- Expr: &ast.Wildcard{
- Token: ast.ASTERISK,
- },
- Name: "*",
- },
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- {
- s: `SELECT name, * FROM tbl`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.FieldRef{
- Name: "name",
- StreamName: ast.DefaultStream,
- },
- Name: "name",
- AName: "",
- },
- {
- Expr: &ast.Wildcard{
- Token: ast.ASTERISK,
- },
- Name: "*",
- },
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- },
- }
- fmt.Printf("The test bucket size is %d.\n\n", len(tests))
- for i, tt := range tests {
- //fmt.Printf("Parsing SQL %q.\n", tt.s)
- stmt, err := NewParser(strings.NewReader(tt.s)).Parse()
- if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
- t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
- } else if tt.err == "" && !reflect.DeepEqual(tt.stmt, stmt) {
- t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmt, stmt)
- }
- }
- }
- func TestParser_ParseWindowsExpr(t *testing.T) {
- var tests = []struct {
- s string
- stmt *ast.SelectStatement
- err string
- }{
- {
- s: `SELECT f1 FROM tbl GROUP BY TUMBLINGWINDOW(ss, 10)`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
- Name: "f1",
- AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- Dimensions: ast.Dimensions{
- ast.Dimension{
- Expr: &ast.Window{
- WindowType: ast.TUMBLING_WINDOW,
- Length: &ast.IntegerLiteral{Val: 10000},
- Interval: &ast.IntegerLiteral{Val: 0},
- },
- },
- },
- },
- },
- {
- s: `SELECT f1 FROM tbl GROUP BY HOPPINGWINDOW(mi, 5, 1)`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
- Name: "f1",
- AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- Dimensions: ast.Dimensions{
- ast.Dimension{
- Expr: &ast.Window{
- WindowType: ast.HOPPING_WINDOW,
- Length: &ast.IntegerLiteral{Val: 3e5},
- Interval: &ast.IntegerLiteral{Val: 6e4},
- },
- },
- },
- },
- },
- {
- s: `SELECT f1 FROM tbl GROUP BY SESSIONWINDOW(hh, 5, 1)`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
- Name: "f1",
- AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- Dimensions: ast.Dimensions{
- ast.Dimension{
- Expr: &ast.Window{
- WindowType: ast.SESSION_WINDOW,
- Length: &ast.IntegerLiteral{Val: 1.8e7},
- Interval: &ast.IntegerLiteral{Val: 3.6e6},
- },
- },
- },
- },
- },
- {
- s: `SELECT f1 FROM tbl GROUP BY SLIDINGWINDOW(ms, 5)`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
- Name: "f1",
- AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- Dimensions: ast.Dimensions{
- ast.Dimension{
- Expr: &ast.Window{
- WindowType: ast.SLIDING_WINDOW,
- Length: &ast.IntegerLiteral{Val: 5},
- Interval: &ast.IntegerLiteral{Val: 0},
- },
- },
- },
- },
- },
- {
- s: `SELECT f1 FROM tbl GROUP BY SLIDINGWINDOW(mi, 5, 1)`,
- stmt: nil,
- err: "The arguments for slidingwindow should be 2.\n",
- },
- {
- s: `SELECT f1 FROM tbl GROUP BY SLIDINGWINDOW("mi", 5)`,
- stmt: nil,
- err: "The 1st argument for slidingwindow is expecting timer literal expression. One value of [dd|hh|mi|ss|ms].\n",
- },
- {
- s: `SELECT f1 FROM tbl GROUP BY COUNTWINDOW(10)`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
- Name: "f1",
- AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- Dimensions: ast.Dimensions{
- ast.Dimension{
- Expr: &ast.Window{
- WindowType: ast.COUNT_WINDOW,
- Length: &ast.IntegerLiteral{Val: 10},
- },
- },
- },
- },
- },
- {
- s: `SELECT f1 FROM tbl GROUP BY COUNTWINDOW(10, 5)`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.FieldRef{Name: "f1", StreamName: ast.DefaultStream},
- Name: "f1",
- AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- Dimensions: ast.Dimensions{
- ast.Dimension{
- Expr: &ast.Window{
- WindowType: ast.COUNT_WINDOW,
- Length: &ast.IntegerLiteral{Val: 10},
- Interval: &ast.IntegerLiteral{Val: 5},
- },
- },
- },
- },
- },
- {
- s: `SELECT f1 FROM tbl GROUP BY COUNTWINDOW(3, 5)`,
- stmt: nil,
- err: "The second parameter value 5 should be less than the first parameter 3.",
- },
- {
- s: `SELECT * FROM demo GROUP BY COUNTWINDOW(3,1) FILTER( where revenue > 100 )`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.Wildcard{Token: ast.ASTERISK},
- Name: "*",
- AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "demo"}},
- Dimensions: ast.Dimensions{
- ast.Dimension{
- Expr: &ast.Window{
- WindowType: ast.COUNT_WINDOW,
- Length: &ast.IntegerLiteral{Val: 3},
- Interval: &ast.IntegerLiteral{Val: 1},
- Filter: &ast.BinaryExpr{
- LHS: &ast.FieldRef{Name: "revenue", StreamName: ast.DefaultStream},
- OP: ast.GT,
- RHS: &ast.IntegerLiteral{Val: 100},
- },
- },
- },
- },
- },
- },
- {
- s: `SELECT * FROM demo GROUP BY department, COUNTWINDOW(3,1) FILTER( where revenue > 100 ), year`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.Wildcard{Token: ast.ASTERISK},
- Name: "*",
- AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "demo"}},
- Dimensions: ast.Dimensions{
- ast.Dimension{Expr: &ast.FieldRef{Name: "department", StreamName: ast.DefaultStream}},
- ast.Dimension{
- Expr: &ast.Window{
- WindowType: ast.COUNT_WINDOW,
- Length: &ast.IntegerLiteral{Val: 3},
- Interval: &ast.IntegerLiteral{Val: 1},
- Filter: &ast.BinaryExpr{
- LHS: &ast.FieldRef{Name: "revenue", StreamName: ast.DefaultStream},
- OP: ast.GT,
- RHS: &ast.IntegerLiteral{Val: 100},
- },
- },
- },
- ast.Dimension{Expr: &ast.FieldRef{Name: "year", StreamName: ast.DefaultStream}},
- },
- },
- },
- {
- s: `SELECT * FROM demo GROUP BY department, COUNTWINDOW(3,1) FILTER( where revenue IN (100, 200)), year`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.Wildcard{Token: ast.ASTERISK},
- Name: "*",
- AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "demo"}},
- Dimensions: ast.Dimensions{
- ast.Dimension{Expr: &ast.FieldRef{Name: "department", StreamName: ast.DefaultStream}},
- ast.Dimension{
- Expr: &ast.Window{
- WindowType: ast.COUNT_WINDOW,
- Length: &ast.IntegerLiteral{Val: 3},
- Interval: &ast.IntegerLiteral{Val: 1},
- Filter: &ast.BinaryExpr{
- LHS: &ast.FieldRef{Name: "revenue", StreamName: ast.DefaultStream},
- OP: ast.IN,
- RHS: &ast.ValueSetExpr{
- LiteralExprs: []ast.Expr{&ast.IntegerLiteral{Val: 100}, &ast.IntegerLiteral{Val: 200}},
- },
- },
- },
- },
- ast.Dimension{Expr: &ast.FieldRef{Name: "year", StreamName: ast.DefaultStream}},
- },
- },
- },
- //to be supported
- {
- s: `SELECT sum(f1) FILTER( where revenue > 100 ) FROM tbl GROUP BY year`,
- stmt: nil,
- err: "found \"FILTER\", expected FROM.",
- },
- {
- s: `SELECT * FROM demo GROUP BY COUNTWINDOW(3,1) FILTER where revenue > 100`,
- stmt: nil,
- err: "Found \"WHERE\" after FILTER, expect parentheses.",
- },
- {
- s: `SELECT * FROM demo GROUP BY COUNTWINDOW(3,1) where revenue > 100`,
- stmt: nil,
- err: "found \"WHERE\", expected EOF.",
- },
- }
- fmt.Printf("The test bucket size is %d.\n\n", len(tests))
- for i, tt := range tests {
- //fmt.Printf("Parsing SQL %q.\n", tt.s)
- stmt, err := NewParser(strings.NewReader(tt.s)).Parse()
- if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
- t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
- } else if tt.err == "" && !reflect.DeepEqual(tt.stmt, stmt) {
- t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmt, stmt)
- }
- }
- }
- func TestParser_ParseJsonExpr(t *testing.T) {
- var tests = []struct {
- s string
- stmt *ast.SelectStatement
- err string
- }{
- {
- s: `SELECT children[0] FROM demo`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.BinaryExpr{
- LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
- OP: ast.SUBSET,
- RHS: &ast.IndexExpr{Index: &ast.IntegerLiteral{Val: 0}},
- },
- Name: "kuiper_field_0",
- AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "demo"}},
- },
- },
- {
- s: `SELECT children[0]->first FROM demo`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.BinaryExpr{
- LHS: &ast.BinaryExpr{
- LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
- OP: ast.SUBSET,
- RHS: &ast.IndexExpr{Index: &ast.IntegerLiteral{Val: 0}},
- },
- OP: ast.ARROW,
- RHS: &ast.JsonFieldRef{Name: "first"},
- },
- Name: "kuiper_field_0",
- AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "demo"}},
- },
- },
- {
- s: `SELECT children->first[2] FROM demo`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.BinaryExpr{
- LHS: &ast.BinaryExpr{
- LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
- OP: ast.ARROW,
- RHS: &ast.JsonFieldRef{Name: "first"},
- },
- OP: ast.SUBSET,
- RHS: &ast.IndexExpr{Index: &ast.IntegerLiteral{Val: 2}},
- },
- Name: "kuiper_field_0",
- AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "demo"}},
- },
- },
- {
- s: `SELECT children->first[2]->test FROM demo`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.BinaryExpr{
- LHS: &ast.BinaryExpr{
- LHS: &ast.BinaryExpr{
- LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
- OP: ast.ARROW,
- RHS: &ast.JsonFieldRef{Name: "first"},
- },
- OP: ast.SUBSET,
- RHS: &ast.IndexExpr{Index: &ast.IntegerLiteral{Val: 2}},
- },
- OP: ast.ARROW,
- RHS: &ast.JsonFieldRef{Name: "test"},
- },
- Name: "kuiper_field_0",
- AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "demo"}},
- },
- },
- {
- s: `SELECT children[0:1] FROM demo`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.BinaryExpr{
- LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
- OP: ast.SUBSET,
- RHS: &ast.ColonExpr{Start: &ast.IntegerLiteral{Val: 0}, End: &ast.IntegerLiteral{Val: 1}},
- },
- Name: "kuiper_field_0",
- AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "demo"}},
- },
- },
- {
- s: `SELECT children[:1] FROM demo`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.BinaryExpr{
- LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
- OP: ast.SUBSET,
- RHS: &ast.ColonExpr{Start: &ast.IntegerLiteral{Val: 0}, End: &ast.IntegerLiteral{Val: 1}},
- },
- Name: "kuiper_field_0",
- AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "demo"}},
- },
- },
- {
- s: `SELECT children[:] FROM demo`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.BinaryExpr{
- LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
- OP: ast.SUBSET,
- RHS: &ast.ColonExpr{Start: &ast.IntegerLiteral{Val: 0}, End: &ast.IntegerLiteral{Val: math.MinInt32}},
- },
- Name: "kuiper_field_0",
- AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "demo"}},
- },
- },
- {
- s: `SELECT children[2:] AS c FROM demo`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.BinaryExpr{
- LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
- OP: ast.SUBSET,
- RHS: &ast.ColonExpr{Start: &ast.IntegerLiteral{Val: 2}, End: &ast.IntegerLiteral{Val: math.MinInt32}},
- },
- Name: "",
- AName: "c"},
- },
- Sources: []ast.Source{&ast.Table{Name: "demo"}},
- },
- },
- {
- s: `SELECT children[2:]->first AS c FROM demo`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.BinaryExpr{
- LHS: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream}, OP: ast.SUBSET, RHS: &ast.ColonExpr{Start: &ast.IntegerLiteral{Val: 2}, End: &ast.IntegerLiteral{Val: math.MinInt32}}},
- OP: ast.ARROW,
- RHS: &ast.JsonFieldRef{Name: "first"},
- },
- Name: "",
- AName: "c"},
- },
- Sources: []ast.Source{&ast.Table{Name: "demo"}},
- },
- },
- {
- s: `SELECT demo.* FROM demo`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.FieldRef{StreamName: ast.StreamName("demo"), Name: "*"},
- Name: "*",
- AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "demo"}},
- },
- },
- {
- s: `SELECT demo.children[2:]->first AS c FROM demo`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.BinaryExpr{
- LHS: &ast.BinaryExpr{LHS: &ast.FieldRef{StreamName: ast.StreamName("demo"), Name: "children"}, OP: ast.SUBSET, RHS: &ast.ColonExpr{Start: &ast.IntegerLiteral{Val: 2}, End: &ast.IntegerLiteral{Val: math.MinInt32}}},
- OP: ast.ARROW,
- RHS: &ast.JsonFieldRef{Name: "first"},
- },
- Name: "",
- AName: "c"},
- },
- Sources: []ast.Source{&ast.Table{Name: "demo"}},
- },
- },
- {
- s: `SELECT lower(demo.children[2:]->first) AS c FROM demo`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.Call{
- Name: "lower",
- Args: []ast.Expr{
- &ast.BinaryExpr{
- LHS: &ast.BinaryExpr{LHS: &ast.FieldRef{StreamName: ast.StreamName("demo"), Name: "children"}, OP: ast.SUBSET, RHS: &ast.ColonExpr{Start: &ast.IntegerLiteral{Val: 2}, End: &ast.IntegerLiteral{Val: math.MinInt32}}},
- OP: ast.ARROW,
- RHS: &ast.JsonFieldRef{Name: "first"},
- },
- },
- },
- Name: "lower",
- AName: "c"},
- },
- Sources: []ast.Source{&ast.Table{Name: "demo"}},
- },
- },
- {
- s: `SELECT children[:1] FROM demo WHERE abc[0] > 12`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.BinaryExpr{
- LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
- OP: ast.SUBSET,
- RHS: &ast.ColonExpr{Start: &ast.IntegerLiteral{Val: 0}, End: &ast.IntegerLiteral{Val: 1}},
- },
- Name: "kuiper_field_0",
- AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "demo"}},
- Condition: &ast.BinaryExpr{
- LHS: &ast.BinaryExpr{
- LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
- OP: ast.SUBSET,
- RHS: &ast.IndexExpr{Index: &ast.IntegerLiteral{Val: 0}},
- },
- OP: ast.GT,
- RHS: &ast.IntegerLiteral{Val: 12},
- },
- },
- },
- {
- s: `SELECT children[:1] FROM demo WHERE abc[0] IN demo.children[2:]->first`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.BinaryExpr{
- LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
- OP: ast.SUBSET,
- RHS: &ast.ColonExpr{Start: &ast.IntegerLiteral{Val: 0}, End: &ast.IntegerLiteral{Val: 1}},
- },
- Name: "kuiper_field_0",
- AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "demo"}},
- Condition: &ast.BinaryExpr{
- LHS: &ast.BinaryExpr{
- LHS: &ast.FieldRef{Name: "abc", StreamName: ast.DefaultStream},
- OP: ast.SUBSET,
- RHS: &ast.IndexExpr{Index: &ast.IntegerLiteral{Val: 0}},
- },
- OP: ast.IN,
- RHS: &ast.BinaryExpr{
- LHS: &ast.BinaryExpr{LHS: &ast.FieldRef{StreamName: ast.StreamName("demo"), Name: "children"}, OP: ast.SUBSET, RHS: &ast.ColonExpr{Start: &ast.IntegerLiteral{Val: 2}, End: &ast.IntegerLiteral{Val: math.MinInt32}}},
- OP: ast.ARROW,
- RHS: &ast.JsonFieldRef{Name: "first"},
- },
- },
- },
- },
- {
- s: `SELECT demo.children.first AS c FROM demo`,
- stmt: nil,
- err: "Too many field names. Please use -> to reference keys in struct.\n",
- },
- {
- s: `SELECT children[index] FROM demo`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.BinaryExpr{
- LHS: &ast.FieldRef{Name: "children", StreamName: ast.DefaultStream},
- OP: ast.SUBSET,
- RHS: &ast.IndexExpr{Index: &ast.FieldRef{Name: "index", StreamName: ast.DefaultStream}},
- },
- Name: "kuiper_field_0",
- AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "demo"}},
- },
- },
- }
- fmt.Printf("The test bucket size is %d.\n\n", len(tests))
- for i, tt := range tests {
- stmt, err := NewParser(strings.NewReader(tt.s)).Parse()
- if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
- t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
- } else if tt.err == "" && !reflect.DeepEqual(tt.stmt, stmt) {
- t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmt, stmt)
- }
- }
- }
- func TestParser_ParseJoins(t *testing.T) {
- var tests = []struct {
- s string
- stmt *ast.SelectStatement
- err string
- }{
- {
- s: `SELECT * FROM topic/sensor1 LEFT JOIN topic1 ON f=k`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.Wildcard{Token: ast.ASTERISK},
- Name: "*",
- AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
- Joins: []ast.Join{
- {
- Name: "topic1", Alias: "", JoinType: ast.LEFT_JOIN, Expr: &ast.BinaryExpr{
- LHS: &ast.FieldRef{Name: "f", StreamName: ast.DefaultStream},
- OP: ast.EQ,
- RHS: &ast.FieldRef{Name: "k", StreamName: ast.DefaultStream},
- },
- },
- },
- },
- },
- {
- s: `SELECT * FROM topic/sensor1 AS t1 INNER JOIN topic1 AS t2 ON f=k`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.Wildcard{Token: ast.ASTERISK},
- Name: "*",
- AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
- Joins: []ast.Join{
- {
- Name: "topic1", Alias: "t2", JoinType: ast.INNER_JOIN, Expr: &ast.BinaryExpr{
- LHS: &ast.FieldRef{Name: "f", StreamName: ast.DefaultStream},
- OP: ast.EQ,
- RHS: &ast.FieldRef{Name: "k", StreamName: ast.DefaultStream},
- },
- },
- },
- },
- },
- {
- s: `SELECT * FROM topic/sensor1 AS t1 LEFT JOIN topic1/sensor2 AS t2 ON f=k`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.Wildcard{Token: ast.ASTERISK},
- Name: "*",
- AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
- Joins: []ast.Join{
- {
- Name: "topic1/sensor2", Alias: "t2", JoinType: ast.LEFT_JOIN, Expr: &ast.BinaryExpr{
- LHS: &ast.FieldRef{Name: "f", StreamName: ast.DefaultStream},
- OP: ast.EQ,
- RHS: &ast.FieldRef{Name: "k", StreamName: ast.DefaultStream},
- },
- },
- },
- },
- },
- {
- s: `SELECT t1.name FROM topic/sensor1 AS t1 LEFT JOIN topic1/sensor2 AS t2 ON f=k`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"},
- Name: "name",
- AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
- Joins: []ast.Join{
- {
- Name: "topic1/sensor2", Alias: "t2", JoinType: ast.LEFT_JOIN, Expr: &ast.BinaryExpr{
- LHS: &ast.FieldRef{Name: "f", StreamName: ast.DefaultStream},
- OP: ast.EQ,
- RHS: &ast.FieldRef{Name: "k", StreamName: ast.DefaultStream},
- },
- },
- },
- },
- },
- {
- s: `SELECT t1.name FROM topic/sensor1 AS t1 LEFT JOIN topic1/sensor2 AS t2 ON t1.f=t2.k`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"},
- Name: "name",
- AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
- Joins: []ast.Join{
- {
- Name: "topic1/sensor2", Alias: "t2", JoinType: ast.LEFT_JOIN, Expr: &ast.BinaryExpr{
- LHS: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "f"},
- OP: ast.EQ,
- RHS: &ast.FieldRef{StreamName: ast.StreamName("t2"), Name: "k"},
- },
- },
- },
- },
- },
- {
- s: `SELECT t1.name FROM topic/sensor1 AS t1 RIGHT JOIN topic1/sensor2 AS t2 ON t1.f=t2.k`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"},
- Name: "name",
- AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
- Joins: []ast.Join{
- {
- Name: "topic1/sensor2", Alias: "t2", JoinType: ast.RIGHT_JOIN, Expr: &ast.BinaryExpr{
- LHS: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "f"},
- OP: ast.EQ,
- RHS: &ast.FieldRef{StreamName: ast.StreamName("t2"), Name: "k"},
- },
- },
- },
- },
- },
- {
- s: `SELECT t1.name FROM topic/sensor1 AS t1 FULL JOIN topic1/sensor2 AS t2 ON t1.f=t2.k`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"},
- Name: "name",
- AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
- Joins: []ast.Join{
- {
- Name: "topic1/sensor2", Alias: "t2", JoinType: ast.FULL_JOIN, Expr: &ast.BinaryExpr{
- LHS: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "f"},
- OP: ast.EQ,
- RHS: &ast.FieldRef{StreamName: ast.StreamName("t2"), Name: "k"},
- },
- },
- },
- },
- },
- {
- s: `SELECT t1.name FROM topic/sensor1 AS t1 CROSS JOIN topic1/sensor2 AS t2 ON t1.f=t2.k`,
- stmt: nil,
- err: "On expression is not required for cross join type.\n",
- },
- {
- s: `SELECT t1.name FROM topic/sensor1 AS t1 CROSS JOIN topic1/sensor2 AS t2`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.FieldRef{StreamName: ast.StreamName("t1"), Name: "name"},
- Name: "name",
- AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "topic/sensor1", Alias: "t1"}},
- Joins: []ast.Join{
- {
- Name: "topic1/sensor2", Alias: "t2", JoinType: ast.CROSS_JOIN, Expr: nil,
- },
- },
- },
- },
- {
- s: `SELECT demo.*, demo2.* FROM demo LEFT JOIN demo2 on demo.f1 = demo2.f2`,
- stmt: &ast.SelectStatement{
- Fields: []ast.Field{
- {
- Expr: &ast.FieldRef{StreamName: ast.StreamName("demo"), Name: "*"},
- Name: "*",
- AName: ""},
- {
- Expr: &ast.FieldRef{StreamName: ast.StreamName("demo2"), Name: "*"},
- Name: "*",
- AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "demo"}},
- Joins: []ast.Join{
- {
- Name: "demo2", Alias: "", JoinType: ast.LEFT_JOIN, Expr: &ast.BinaryExpr{
- LHS: &ast.FieldRef{StreamName: ast.StreamName("demo"), Name: "f1"},
- OP: ast.EQ,
- RHS: &ast.FieldRef{StreamName: ast.StreamName("demo2"), Name: "f2"},
- },
- },
- },
- },
- },
- }
- fmt.Printf("The test bucket size is %d.\n\n", len(tests))
- for i, tt := range tests {
- stmt, err := NewParser(strings.NewReader(tt.s)).Parse()
- if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
- t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
- } else if tt.err == "" && !reflect.DeepEqual(tt.stmt, stmt) {
- t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmt, stmt)
- }
- }
- }
- func TestParser_ParseStatements(t *testing.T) {
- var tests = []struct {
- s string
- stmts []ast.SelectStatement
- err string
- }{
- {s: "SELECT name FROM tbl;\nSELECT name FROM topic/sensor1\n",
- stmts: []ast.SelectStatement{
- {
- Fields: []ast.Field{
- {
- Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
- Name: "name",
- AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- {
- Fields: []ast.Field{
- {
- Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
- Name: "name",
- AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
- },
- },
- },
- {s: "SELECT name FROM tbl;\nSELECT name FROM topic/sensor1;\n--SELECT comment FROM topic/comment",
- stmts: []ast.SelectStatement{
- {
- Fields: []ast.Field{
- {
- Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
- Name: "name",
- AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "tbl"}},
- },
- {
- Fields: []ast.Field{
- {
- Expr: &ast.FieldRef{Name: "name", StreamName: ast.DefaultStream},
- Name: "name",
- AName: ""},
- },
- Sources: []ast.Source{&ast.Table{Name: "topic/sensor1"}},
- },
- },
- },
- }
- fmt.Printf("The test bucket size is %d.\n\n", len(tests))
- for i, tt := range tests {
- stmts, err := NewParser(strings.NewReader(tt.s)).ParseQueries()
- if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
- t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
- } else if tt.err == "" && !reflect.DeepEqual(tt.stmts, stmts) {
- t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmts, stmts)
- }
- }
- }
|