planner_test.go 115 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300
  1. // Copyright 2022-2023 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package planner
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "reflect"
  19. "strings"
  20. "testing"
  21. "github.com/gdexlab/go-render/render"
  22. "github.com/lf-edge/ekuiper/internal/pkg/store"
  23. "github.com/lf-edge/ekuiper/internal/testx"
  24. "github.com/lf-edge/ekuiper/internal/topo/node"
  25. "github.com/lf-edge/ekuiper/internal/xsql"
  26. "github.com/lf-edge/ekuiper/pkg/api"
  27. "github.com/lf-edge/ekuiper/pkg/ast"
  28. )
  29. func init() {
  30. testx.InitEnv()
  31. }
  32. func Test_createLogicalPlan(t *testing.T) {
  33. kv, err := store.GetKV("stream")
  34. if err != nil {
  35. t.Error(err)
  36. return
  37. }
  38. streamSqls := map[string]string{
  39. "src1": `CREATE STREAM src1 (
  40. id1 BIGINT,
  41. temp BIGINT,
  42. name string,
  43. myarray array(string)
  44. ) WITH (DATASOURCE="src1", FORMAT="json", KEY="ts");`,
  45. "src2": `CREATE STREAM src2 (
  46. id2 BIGINT,
  47. hum BIGINT
  48. ) WITH (DATASOURCE="src2", FORMAT="json", KEY="ts", TIMESTAMP_FORMAT="YYYY-MM-dd HH:mm:ss");`,
  49. "tableInPlanner": `CREATE TABLE tableInPlanner (
  50. id BIGINT,
  51. name STRING,
  52. value STRING,
  53. hum BIGINT
  54. ) WITH (TYPE="file");`,
  55. }
  56. types := map[string]ast.StreamType{
  57. "src1": ast.TypeStream,
  58. "src2": ast.TypeStream,
  59. "tableInPlanner": ast.TypeTable,
  60. }
  61. for name, sql := range streamSqls {
  62. s, err := json.Marshal(&xsql.StreamInfo{
  63. StreamType: types[name],
  64. Statement: sql,
  65. })
  66. if err != nil {
  67. t.Error(err)
  68. t.Fail()
  69. }
  70. err = kv.Set(name, string(s))
  71. if err != nil {
  72. t.Error(err)
  73. t.Fail()
  74. }
  75. }
  76. streams := make(map[string]*ast.StreamStmt)
  77. for n := range streamSqls {
  78. streamStmt, err := xsql.GetDataSource(kv, n)
  79. if err != nil {
  80. t.Errorf("fail to get stream %s, please check if stream is created", n)
  81. return
  82. }
  83. streams[n] = streamStmt
  84. }
  85. // boolTrue = true
  86. boolFalse := false
  87. tests := []struct {
  88. sql string
  89. p LogicalPlan
  90. err string
  91. }{
  92. {
  93. sql: "select name from src1 where true limit 1",
  94. p: ProjectPlan{
  95. baseLogicalPlan: baseLogicalPlan{
  96. children: []LogicalPlan{
  97. FilterPlan{
  98. baseLogicalPlan: baseLogicalPlan{
  99. children: []LogicalPlan{
  100. DataSourcePlan{
  101. baseLogicalPlan: baseLogicalPlan{},
  102. name: "src1",
  103. streamFields: map[string]*ast.JsonStreamField{
  104. "name": {
  105. Type: "string",
  106. },
  107. },
  108. streamStmt: streams["src1"],
  109. metaFields: []string{},
  110. pruneFields: []string{},
  111. }.Init(),
  112. },
  113. },
  114. condition: &ast.BooleanLiteral{
  115. Val: true,
  116. },
  117. }.Init(),
  118. },
  119. },
  120. fields: []ast.Field{
  121. {
  122. Name: "name",
  123. Expr: &ast.FieldRef{
  124. StreamName: "src1",
  125. Name: "name",
  126. },
  127. },
  128. },
  129. limitCount: 1,
  130. enableLimit: true,
  131. }.Init(),
  132. },
  133. {
  134. sql: "select name from src1 limit 1",
  135. p: ProjectPlan{
  136. baseLogicalPlan: baseLogicalPlan{
  137. children: []LogicalPlan{
  138. DataSourcePlan{
  139. baseLogicalPlan: baseLogicalPlan{},
  140. name: "src1",
  141. streamFields: map[string]*ast.JsonStreamField{
  142. "name": {
  143. Type: "string",
  144. },
  145. },
  146. streamStmt: streams["src1"],
  147. metaFields: []string{},
  148. pruneFields: []string{},
  149. }.Init(),
  150. },
  151. },
  152. fields: []ast.Field{
  153. {
  154. Name: "name",
  155. Expr: &ast.FieldRef{
  156. StreamName: "src1",
  157. Name: "name",
  158. },
  159. },
  160. },
  161. limitCount: 1,
  162. enableLimit: true,
  163. }.Init(),
  164. },
  165. {
  166. sql: "select unnest(myarray) as col from src1 limit 1",
  167. p: ProjectSetPlan{
  168. SrfMapping: map[string]struct{}{
  169. "col": {},
  170. },
  171. limitCount: 1,
  172. enableLimit: true,
  173. baseLogicalPlan: baseLogicalPlan{
  174. children: []LogicalPlan{
  175. ProjectPlan{
  176. baseLogicalPlan: baseLogicalPlan{
  177. children: []LogicalPlan{
  178. DataSourcePlan{
  179. baseLogicalPlan: baseLogicalPlan{},
  180. name: "src1",
  181. streamFields: map[string]*ast.JsonStreamField{
  182. "myarray": {
  183. Type: "array",
  184. Items: &ast.JsonStreamField{
  185. Type: "string",
  186. },
  187. },
  188. },
  189. streamStmt: streams["src1"],
  190. metaFields: []string{},
  191. pruneFields: []string{},
  192. }.Init(),
  193. },
  194. },
  195. fields: []ast.Field{
  196. {
  197. Name: "unnest",
  198. AName: "col",
  199. Expr: func() *ast.FieldRef {
  200. fr := &ast.FieldRef{
  201. StreamName: ast.AliasStream,
  202. Name: "col",
  203. AliasRef: &ast.AliasRef{
  204. Expression: &ast.Call{
  205. Name: "unnest",
  206. FuncType: ast.FuncTypeSrf,
  207. Args: []ast.Expr{
  208. &ast.FieldRef{
  209. StreamName: "src1",
  210. Name: "myarray",
  211. },
  212. },
  213. },
  214. },
  215. }
  216. fr.SetRefSource([]ast.StreamName{"src1"})
  217. return fr
  218. }(),
  219. },
  220. },
  221. }.Init(),
  222. },
  223. },
  224. }.Init(),
  225. },
  226. { // 0
  227. sql: "SELECT unnest(myarray), name from src1",
  228. p: ProjectSetPlan{
  229. SrfMapping: map[string]struct{}{
  230. "unnest": {},
  231. },
  232. baseLogicalPlan: baseLogicalPlan{
  233. children: []LogicalPlan{
  234. ProjectPlan{
  235. baseLogicalPlan: baseLogicalPlan{
  236. children: []LogicalPlan{
  237. DataSourcePlan{
  238. baseLogicalPlan: baseLogicalPlan{},
  239. name: "src1",
  240. streamFields: map[string]*ast.JsonStreamField{
  241. "myarray": {
  242. Type: "array",
  243. Items: &ast.JsonStreamField{
  244. Type: "string",
  245. },
  246. },
  247. "name": {
  248. Type: "string",
  249. },
  250. },
  251. streamStmt: streams["src1"],
  252. metaFields: []string{},
  253. pruneFields: []string{},
  254. }.Init(),
  255. },
  256. },
  257. fields: []ast.Field{
  258. {
  259. Expr: &ast.Call{
  260. Name: "unnest",
  261. FuncType: ast.FuncTypeSrf,
  262. Args: []ast.Expr{
  263. &ast.FieldRef{
  264. StreamName: "src1",
  265. Name: "myarray",
  266. },
  267. },
  268. },
  269. Name: "unnest",
  270. },
  271. {
  272. Name: "name",
  273. Expr: &ast.FieldRef{
  274. StreamName: "src1",
  275. Name: "name",
  276. },
  277. },
  278. },
  279. }.Init(),
  280. },
  281. },
  282. }.Init(),
  283. },
  284. { // 0
  285. sql: `SELECT myarray[temp] FROM src1`,
  286. p: ProjectPlan{
  287. baseLogicalPlan: baseLogicalPlan{
  288. children: []LogicalPlan{
  289. DataSourcePlan{
  290. baseLogicalPlan: baseLogicalPlan{},
  291. name: "src1",
  292. streamFields: map[string]*ast.JsonStreamField{
  293. "myarray": {
  294. Type: "array",
  295. Items: &ast.JsonStreamField{
  296. Type: "string",
  297. },
  298. },
  299. "temp": {
  300. Type: "bigint",
  301. },
  302. },
  303. streamStmt: streams["src1"],
  304. metaFields: []string{},
  305. pruneFields: []string{},
  306. }.Init(),
  307. },
  308. },
  309. fields: []ast.Field{
  310. {
  311. Expr: &ast.BinaryExpr{
  312. OP: ast.SUBSET,
  313. LHS: &ast.FieldRef{
  314. StreamName: "src1",
  315. Name: "myarray",
  316. },
  317. RHS: &ast.IndexExpr{Index: &ast.FieldRef{
  318. StreamName: "src1",
  319. Name: "temp",
  320. }},
  321. },
  322. Name: "kuiper_field_0",
  323. AName: "",
  324. },
  325. },
  326. isAggregate: false,
  327. sendMeta: false,
  328. }.Init(),
  329. },
  330. { // 1 optimize where to data source
  331. sql: `SELECT temp FROM src1 WHERE name = "v1" GROUP BY TUMBLINGWINDOW(ss, 10)`,
  332. p: ProjectPlan{
  333. baseLogicalPlan: baseLogicalPlan{
  334. children: []LogicalPlan{
  335. WindowPlan{
  336. baseLogicalPlan: baseLogicalPlan{
  337. children: []LogicalPlan{
  338. FilterPlan{
  339. baseLogicalPlan: baseLogicalPlan{
  340. children: []LogicalPlan{
  341. DataSourcePlan{
  342. name: "src1",
  343. streamFields: map[string]*ast.JsonStreamField{
  344. "name": {
  345. Type: "string",
  346. },
  347. "temp": {
  348. Type: "bigint",
  349. },
  350. },
  351. streamStmt: streams["src1"],
  352. metaFields: []string{},
  353. pruneFields: []string{},
  354. }.Init(),
  355. },
  356. },
  357. condition: &ast.BinaryExpr{
  358. LHS: &ast.FieldRef{Name: "name", StreamName: "src1"},
  359. OP: ast.EQ,
  360. RHS: &ast.StringLiteral{Val: "v1"},
  361. },
  362. }.Init(),
  363. },
  364. },
  365. condition: nil,
  366. wtype: ast.TUMBLING_WINDOW,
  367. length: 10,
  368. timeUnit: ast.SS,
  369. interval: 0,
  370. limit: 0,
  371. }.Init(),
  372. },
  373. },
  374. fields: []ast.Field{
  375. {
  376. Expr: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  377. Name: "temp",
  378. AName: "",
  379. },
  380. },
  381. isAggregate: false,
  382. sendMeta: false,
  383. }.Init(),
  384. },
  385. { // 2 condition that cannot be optimized
  386. 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)`,
  387. p: ProjectPlan{
  388. baseLogicalPlan: baseLogicalPlan{
  389. children: []LogicalPlan{
  390. JoinPlan{
  391. baseLogicalPlan: baseLogicalPlan{
  392. children: []LogicalPlan{
  393. WindowPlan{
  394. baseLogicalPlan: baseLogicalPlan{
  395. children: []LogicalPlan{
  396. DataSourcePlan{
  397. name: "src1",
  398. streamFields: map[string]*ast.JsonStreamField{
  399. "id1": {
  400. Type: "bigint",
  401. },
  402. "temp": {
  403. Type: "bigint",
  404. },
  405. },
  406. streamStmt: streams["src1"],
  407. metaFields: []string{},
  408. pruneFields: []string{},
  409. }.Init(),
  410. DataSourcePlan{
  411. name: "src2",
  412. streamFields: map[string]*ast.JsonStreamField{
  413. "hum": {
  414. Type: "bigint",
  415. },
  416. "id2": {
  417. Type: "bigint",
  418. },
  419. },
  420. streamStmt: streams["src2"],
  421. metaFields: []string{},
  422. timestampFormat: "YYYY-MM-dd HH:mm:ss",
  423. pruneFields: []string{},
  424. }.Init(),
  425. },
  426. },
  427. condition: nil,
  428. wtype: ast.TUMBLING_WINDOW,
  429. length: 10,
  430. timeUnit: ast.SS,
  431. interval: 0,
  432. limit: 0,
  433. }.Init(),
  434. },
  435. },
  436. from: &ast.Table{Name: "src1"},
  437. joins: ast.Joins{ast.Join{
  438. Name: "src2",
  439. JoinType: ast.INNER_JOIN,
  440. Expr: &ast.BinaryExpr{
  441. OP: ast.AND,
  442. LHS: &ast.BinaryExpr{
  443. LHS: &ast.BinaryExpr{
  444. OP: ast.GT,
  445. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  446. RHS: &ast.IntegerLiteral{Val: 20},
  447. },
  448. OP: ast.OR,
  449. RHS: &ast.BinaryExpr{
  450. OP: ast.GT,
  451. LHS: &ast.FieldRef{Name: "hum", StreamName: "src2"},
  452. RHS: &ast.IntegerLiteral{Val: 60},
  453. },
  454. },
  455. RHS: &ast.BinaryExpr{
  456. OP: ast.EQ,
  457. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  458. RHS: &ast.FieldRef{Name: "id2", StreamName: "src2"},
  459. },
  460. },
  461. }},
  462. }.Init(),
  463. },
  464. },
  465. fields: []ast.Field{
  466. {
  467. Expr: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  468. Name: "id1",
  469. AName: "",
  470. },
  471. },
  472. isAggregate: false,
  473. sendMeta: false,
  474. }.Init(),
  475. },
  476. { // 3 optimize window filter
  477. sql: `SELECT id1 FROM src1 WHERE name = "v1" GROUP BY TUMBLINGWINDOW(ss, 10) FILTER( WHERE temp > 2)`,
  478. p: ProjectPlan{
  479. baseLogicalPlan: baseLogicalPlan{
  480. children: []LogicalPlan{
  481. WindowPlan{
  482. baseLogicalPlan: baseLogicalPlan{
  483. children: []LogicalPlan{
  484. FilterPlan{
  485. baseLogicalPlan: baseLogicalPlan{
  486. children: []LogicalPlan{
  487. DataSourcePlan{
  488. name: "src1",
  489. streamFields: map[string]*ast.JsonStreamField{
  490. "id1": {
  491. Type: "bigint",
  492. },
  493. "name": {
  494. Type: "string",
  495. },
  496. "temp": {
  497. Type: "bigint",
  498. },
  499. },
  500. streamStmt: streams["src1"],
  501. metaFields: []string{},
  502. pruneFields: []string{},
  503. }.Init(),
  504. },
  505. },
  506. condition: &ast.BinaryExpr{
  507. OP: ast.AND,
  508. LHS: &ast.BinaryExpr{
  509. LHS: &ast.FieldRef{Name: "name", StreamName: "src1"},
  510. OP: ast.EQ,
  511. RHS: &ast.StringLiteral{Val: "v1"},
  512. },
  513. RHS: &ast.BinaryExpr{
  514. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  515. OP: ast.GT,
  516. RHS: &ast.IntegerLiteral{Val: 2},
  517. },
  518. },
  519. }.Init(),
  520. },
  521. },
  522. condition: nil,
  523. wtype: ast.TUMBLING_WINDOW,
  524. length: 10,
  525. timeUnit: ast.SS,
  526. interval: 0,
  527. limit: 0,
  528. }.Init(),
  529. },
  530. },
  531. fields: []ast.Field{
  532. {
  533. Expr: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  534. Name: "id1",
  535. AName: "",
  536. },
  537. },
  538. isAggregate: false,
  539. sendMeta: false,
  540. }.Init(),
  541. },
  542. { // 4. do not optimize count window
  543. sql: `SELECT * FROM src1 WHERE temp > 20 GROUP BY COUNTWINDOW(5,1) HAVING COUNT(*) > 2`,
  544. p: ProjectPlan{
  545. baseLogicalPlan: baseLogicalPlan{
  546. children: []LogicalPlan{
  547. HavingPlan{
  548. baseLogicalPlan: baseLogicalPlan{
  549. children: []LogicalPlan{
  550. FilterPlan{
  551. baseLogicalPlan: baseLogicalPlan{
  552. children: []LogicalPlan{
  553. WindowPlan{
  554. baseLogicalPlan: baseLogicalPlan{
  555. children: []LogicalPlan{
  556. DataSourcePlan{
  557. name: "src1",
  558. isWildCard: true,
  559. streamFields: map[string]*ast.JsonStreamField{
  560. "id1": {
  561. Type: "bigint",
  562. },
  563. "temp": {
  564. Type: "bigint",
  565. },
  566. "name": {
  567. Type: "string",
  568. },
  569. "myarray": {
  570. Type: "array",
  571. Items: &ast.JsonStreamField{
  572. Type: "string",
  573. },
  574. },
  575. },
  576. streamStmt: streams["src1"],
  577. metaFields: []string{},
  578. pruneFields: []string{},
  579. }.Init(),
  580. },
  581. },
  582. condition: nil,
  583. wtype: ast.COUNT_WINDOW,
  584. length: 5,
  585. interval: 1,
  586. limit: 0,
  587. }.Init(),
  588. },
  589. },
  590. condition: &ast.BinaryExpr{
  591. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  592. OP: ast.GT,
  593. RHS: &ast.IntegerLiteral{Val: 20},
  594. },
  595. }.Init(),
  596. },
  597. },
  598. condition: &ast.BinaryExpr{
  599. LHS: &ast.Call{Name: "count", FuncId: 0, Args: []ast.Expr{&ast.Wildcard{
  600. Token: ast.ASTERISK,
  601. }}, FuncType: ast.FuncTypeAgg},
  602. OP: ast.GT,
  603. RHS: &ast.IntegerLiteral{Val: 2},
  604. },
  605. }.Init(),
  606. },
  607. },
  608. fields: []ast.Field{
  609. {
  610. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  611. Name: "*",
  612. AName: "",
  613. },
  614. },
  615. isAggregate: false,
  616. sendMeta: false,
  617. }.Init(),
  618. },
  619. { // 5. optimize join on
  620. 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)`,
  621. p: ProjectPlan{
  622. baseLogicalPlan: baseLogicalPlan{
  623. children: []LogicalPlan{
  624. JoinPlan{
  625. baseLogicalPlan: baseLogicalPlan{
  626. children: []LogicalPlan{
  627. WindowPlan{
  628. baseLogicalPlan: baseLogicalPlan{
  629. children: []LogicalPlan{
  630. FilterPlan{
  631. baseLogicalPlan: baseLogicalPlan{
  632. children: []LogicalPlan{
  633. DataSourcePlan{
  634. name: "src1",
  635. streamFields: map[string]*ast.JsonStreamField{
  636. "id1": {
  637. Type: "bigint",
  638. },
  639. "temp": {
  640. Type: "bigint",
  641. },
  642. },
  643. streamStmt: streams["src1"],
  644. metaFields: []string{},
  645. pruneFields: []string{},
  646. }.Init(),
  647. },
  648. },
  649. condition: &ast.BinaryExpr{
  650. RHS: &ast.BinaryExpr{
  651. OP: ast.GT,
  652. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  653. RHS: &ast.IntegerLiteral{Val: 20},
  654. },
  655. OP: ast.AND,
  656. LHS: &ast.BinaryExpr{
  657. OP: ast.GT,
  658. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  659. RHS: &ast.IntegerLiteral{Val: 111},
  660. },
  661. },
  662. }.Init(),
  663. FilterPlan{
  664. baseLogicalPlan: baseLogicalPlan{
  665. children: []LogicalPlan{
  666. DataSourcePlan{
  667. name: "src2",
  668. streamFields: map[string]*ast.JsonStreamField{
  669. "hum": {
  670. Type: "bigint",
  671. },
  672. "id2": {
  673. Type: "bigint",
  674. },
  675. },
  676. streamStmt: streams["src2"],
  677. metaFields: []string{},
  678. timestampFormat: "YYYY-MM-dd HH:mm:ss",
  679. pruneFields: []string{},
  680. }.Init(),
  681. },
  682. },
  683. condition: &ast.BinaryExpr{
  684. OP: ast.LT,
  685. LHS: &ast.FieldRef{Name: "hum", StreamName: "src2"},
  686. RHS: &ast.IntegerLiteral{Val: 60},
  687. },
  688. }.Init(),
  689. },
  690. },
  691. condition: nil,
  692. wtype: ast.TUMBLING_WINDOW,
  693. length: 10,
  694. timeUnit: ast.SS,
  695. interval: 0,
  696. limit: 0,
  697. }.Init(),
  698. },
  699. },
  700. from: &ast.Table{
  701. Name: "src1",
  702. },
  703. joins: []ast.Join{
  704. {
  705. Name: "src2",
  706. Alias: "",
  707. JoinType: ast.INNER_JOIN,
  708. Expr: &ast.BinaryExpr{
  709. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  710. OP: ast.EQ,
  711. RHS: &ast.FieldRef{Name: "id2", StreamName: "src2"},
  712. },
  713. },
  714. },
  715. }.Init(),
  716. },
  717. },
  718. fields: []ast.Field{
  719. {
  720. Expr: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  721. Name: "id1",
  722. AName: "",
  723. },
  724. },
  725. isAggregate: false,
  726. sendMeta: false,
  727. }.Init(),
  728. },
  729. { // 6. optimize outter join on
  730. 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)`,
  731. p: ProjectPlan{
  732. baseLogicalPlan: baseLogicalPlan{
  733. children: []LogicalPlan{
  734. JoinPlan{
  735. baseLogicalPlan: baseLogicalPlan{
  736. children: []LogicalPlan{
  737. WindowPlan{
  738. baseLogicalPlan: baseLogicalPlan{
  739. children: []LogicalPlan{
  740. FilterPlan{
  741. baseLogicalPlan: baseLogicalPlan{
  742. children: []LogicalPlan{
  743. DataSourcePlan{
  744. name: "src1",
  745. streamFields: map[string]*ast.JsonStreamField{
  746. "id1": {
  747. Type: "bigint",
  748. },
  749. "temp": {
  750. Type: "bigint",
  751. },
  752. },
  753. streamStmt: streams["src1"],
  754. metaFields: []string{},
  755. pruneFields: []string{},
  756. }.Init(),
  757. },
  758. },
  759. condition: &ast.BinaryExpr{
  760. OP: ast.GT,
  761. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  762. RHS: &ast.IntegerLiteral{Val: 111},
  763. },
  764. }.Init(),
  765. DataSourcePlan{
  766. name: "src2",
  767. streamFields: map[string]*ast.JsonStreamField{
  768. "hum": {
  769. Type: "bigint",
  770. },
  771. "id2": {
  772. Type: "bigint",
  773. },
  774. },
  775. streamStmt: streams["src2"],
  776. metaFields: []string{},
  777. pruneFields: []string{},
  778. timestampFormat: "YYYY-MM-dd HH:mm:ss",
  779. }.Init(),
  780. },
  781. },
  782. condition: nil,
  783. wtype: ast.TUMBLING_WINDOW,
  784. length: 10,
  785. timeUnit: ast.SS,
  786. interval: 0,
  787. limit: 0,
  788. }.Init(),
  789. },
  790. },
  791. from: &ast.Table{
  792. Name: "src1",
  793. },
  794. joins: []ast.Join{
  795. {
  796. Name: "src2",
  797. Alias: "",
  798. JoinType: ast.FULL_JOIN,
  799. Expr: &ast.BinaryExpr{
  800. OP: ast.AND,
  801. LHS: &ast.BinaryExpr{
  802. OP: ast.AND,
  803. LHS: &ast.BinaryExpr{
  804. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  805. OP: ast.EQ,
  806. RHS: &ast.FieldRef{Name: "id2", StreamName: "src2"},
  807. },
  808. RHS: &ast.BinaryExpr{
  809. OP: ast.GT,
  810. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  811. RHS: &ast.IntegerLiteral{Val: 20},
  812. },
  813. },
  814. RHS: &ast.BinaryExpr{
  815. OP: ast.LT,
  816. LHS: &ast.FieldRef{Name: "hum", StreamName: "src2"},
  817. RHS: &ast.IntegerLiteral{Val: 60},
  818. },
  819. },
  820. },
  821. },
  822. }.Init(),
  823. },
  824. },
  825. fields: []ast.Field{
  826. {
  827. Expr: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  828. Name: "id1",
  829. AName: "",
  830. },
  831. },
  832. isAggregate: false,
  833. sendMeta: false,
  834. }.Init(),
  835. },
  836. { // 7 window error for table
  837. sql: `SELECT value FROM tableInPlanner WHERE name = "v1" GROUP BY TUMBLINGWINDOW(ss, 10)`,
  838. p: nil,
  839. err: "cannot run window for TABLE sources",
  840. },
  841. { // 8 join table without window
  842. sql: `SELECT id1 FROM src1 INNER JOIN tableInPlanner on src1.id1 = tableInPlanner.id and src1.temp > 20 and hum < 60 WHERE src1.id1 > 111`,
  843. p: ProjectPlan{
  844. baseLogicalPlan: baseLogicalPlan{
  845. children: []LogicalPlan{
  846. JoinPlan{
  847. baseLogicalPlan: baseLogicalPlan{
  848. children: []LogicalPlan{
  849. JoinAlignPlan{
  850. baseLogicalPlan: baseLogicalPlan{
  851. children: []LogicalPlan{
  852. FilterPlan{
  853. baseLogicalPlan: baseLogicalPlan{
  854. children: []LogicalPlan{
  855. DataSourcePlan{
  856. name: "src1",
  857. streamFields: map[string]*ast.JsonStreamField{
  858. "id1": {
  859. Type: "bigint",
  860. },
  861. "temp": {
  862. Type: "bigint",
  863. },
  864. },
  865. streamStmt: streams["src1"],
  866. metaFields: []string{},
  867. pruneFields: []string{},
  868. }.Init(),
  869. },
  870. },
  871. condition: &ast.BinaryExpr{
  872. RHS: &ast.BinaryExpr{
  873. OP: ast.GT,
  874. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  875. RHS: &ast.IntegerLiteral{Val: 20},
  876. },
  877. OP: ast.AND,
  878. LHS: &ast.BinaryExpr{
  879. OP: ast.GT,
  880. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  881. RHS: &ast.IntegerLiteral{Val: 111},
  882. },
  883. },
  884. }.Init(),
  885. DataSourcePlan{
  886. name: "tableInPlanner",
  887. streamFields: map[string]*ast.JsonStreamField{
  888. "hum": {
  889. Type: "bigint",
  890. },
  891. "id": {
  892. Type: "bigint",
  893. },
  894. },
  895. streamStmt: streams["tableInPlanner"],
  896. metaFields: []string{},
  897. pruneFields: []string{},
  898. }.Init(),
  899. },
  900. },
  901. Emitters: []string{"tableInPlanner"},
  902. }.Init(),
  903. },
  904. },
  905. from: &ast.Table{
  906. Name: "src1",
  907. },
  908. joins: []ast.Join{
  909. {
  910. Name: "tableInPlanner",
  911. Alias: "",
  912. JoinType: ast.INNER_JOIN,
  913. Expr: &ast.BinaryExpr{
  914. OP: ast.AND,
  915. LHS: &ast.BinaryExpr{
  916. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  917. OP: ast.EQ,
  918. RHS: &ast.FieldRef{Name: "id", StreamName: "tableInPlanner"},
  919. },
  920. RHS: &ast.BinaryExpr{
  921. OP: ast.LT,
  922. LHS: &ast.FieldRef{Name: "hum", StreamName: "tableInPlanner"},
  923. RHS: &ast.IntegerLiteral{Val: 60},
  924. },
  925. },
  926. },
  927. },
  928. }.Init(),
  929. },
  930. },
  931. fields: []ast.Field{
  932. {
  933. Expr: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  934. Name: "id1",
  935. AName: "",
  936. },
  937. },
  938. isAggregate: false,
  939. sendMeta: false,
  940. }.Init(),
  941. },
  942. { // 9 join table with window
  943. 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)`,
  944. p: ProjectPlan{
  945. baseLogicalPlan: baseLogicalPlan{
  946. children: []LogicalPlan{
  947. JoinPlan{
  948. baseLogicalPlan: baseLogicalPlan{
  949. children: []LogicalPlan{
  950. JoinAlignPlan{
  951. baseLogicalPlan: baseLogicalPlan{
  952. children: []LogicalPlan{
  953. WindowPlan{
  954. baseLogicalPlan: baseLogicalPlan{
  955. children: []LogicalPlan{
  956. DataSourcePlan{
  957. name: "src1",
  958. streamFields: map[string]*ast.JsonStreamField{
  959. "id1": {
  960. Type: "bigint",
  961. },
  962. "temp": {
  963. Type: "bigint",
  964. },
  965. },
  966. streamStmt: streams["src1"],
  967. metaFields: []string{},
  968. pruneFields: []string{},
  969. }.Init(),
  970. },
  971. },
  972. condition: nil,
  973. wtype: ast.TUMBLING_WINDOW,
  974. length: 10,
  975. timeUnit: ast.SS,
  976. interval: 0,
  977. limit: 0,
  978. }.Init(),
  979. DataSourcePlan{
  980. name: "tableInPlanner",
  981. streamFields: map[string]*ast.JsonStreamField{
  982. "hum": {
  983. Type: "bigint",
  984. },
  985. "id": {
  986. Type: "bigint",
  987. },
  988. },
  989. streamStmt: streams["tableInPlanner"],
  990. metaFields: []string{},
  991. pruneFields: []string{},
  992. }.Init(),
  993. },
  994. },
  995. Emitters: []string{"tableInPlanner"},
  996. }.Init(),
  997. },
  998. },
  999. from: &ast.Table{
  1000. Name: "src1",
  1001. },
  1002. joins: []ast.Join{
  1003. {
  1004. Name: "tableInPlanner",
  1005. Alias: "",
  1006. JoinType: ast.INNER_JOIN,
  1007. Expr: &ast.BinaryExpr{
  1008. OP: ast.AND,
  1009. LHS: &ast.BinaryExpr{
  1010. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  1011. OP: ast.EQ,
  1012. RHS: &ast.FieldRef{Name: "id", StreamName: "tableInPlanner"},
  1013. },
  1014. RHS: &ast.BinaryExpr{
  1015. RHS: &ast.BinaryExpr{
  1016. OP: ast.AND,
  1017. LHS: &ast.BinaryExpr{
  1018. OP: ast.GT,
  1019. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  1020. RHS: &ast.IntegerLiteral{Val: 20},
  1021. },
  1022. RHS: &ast.BinaryExpr{
  1023. OP: ast.LT,
  1024. LHS: &ast.FieldRef{Name: "hum", StreamName: "tableInPlanner"},
  1025. RHS: &ast.IntegerLiteral{Val: 60},
  1026. },
  1027. },
  1028. OP: ast.AND,
  1029. LHS: &ast.BinaryExpr{
  1030. OP: ast.GT,
  1031. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  1032. RHS: &ast.IntegerLiteral{Val: 111},
  1033. },
  1034. },
  1035. },
  1036. },
  1037. },
  1038. }.Init(),
  1039. },
  1040. },
  1041. fields: []ast.Field{
  1042. {
  1043. Expr: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  1044. Name: "id1",
  1045. AName: "",
  1046. },
  1047. },
  1048. isAggregate: false,
  1049. sendMeta: false,
  1050. }.Init(),
  1051. },
  1052. { // 10 meta
  1053. sql: `SELECT temp, meta(id) AS eid,meta(Humidity->Device) AS hdevice FROM src1 WHERE meta(device)="demo2"`,
  1054. p: ProjectPlan{
  1055. baseLogicalPlan: baseLogicalPlan{
  1056. children: []LogicalPlan{
  1057. FilterPlan{
  1058. baseLogicalPlan: baseLogicalPlan{
  1059. children: []LogicalPlan{
  1060. DataSourcePlan{
  1061. name: "src1",
  1062. streamFields: map[string]*ast.JsonStreamField{
  1063. "temp": {
  1064. Type: "bigint",
  1065. },
  1066. },
  1067. streamStmt: streams["src1"],
  1068. metaFields: []string{"Humidity", "device", "id"},
  1069. pruneFields: []string{},
  1070. }.Init(),
  1071. },
  1072. },
  1073. condition: &ast.BinaryExpr{
  1074. LHS: &ast.Call{
  1075. Name: "meta",
  1076. FuncId: 2,
  1077. Args: []ast.Expr{&ast.MetaRef{
  1078. Name: "device",
  1079. StreamName: ast.DefaultStream,
  1080. }},
  1081. },
  1082. OP: ast.EQ,
  1083. RHS: &ast.StringLiteral{
  1084. Val: "demo2",
  1085. },
  1086. },
  1087. }.Init(),
  1088. },
  1089. },
  1090. fields: []ast.Field{
  1091. {
  1092. Expr: &ast.FieldRef{Name: "eid", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  1093. &ast.Call{Name: "meta", FuncId: 0, Args: []ast.Expr{&ast.MetaRef{
  1094. Name: "id",
  1095. StreamName: ast.DefaultStream,
  1096. }}},
  1097. []ast.StreamName{},
  1098. nil,
  1099. )},
  1100. Name: "meta",
  1101. AName: "eid",
  1102. },
  1103. {
  1104. Expr: &ast.FieldRef{Name: "hdevice", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  1105. &ast.Call{Name: "meta", FuncId: 1, Args: []ast.Expr{
  1106. &ast.BinaryExpr{
  1107. OP: ast.ARROW,
  1108. LHS: &ast.MetaRef{Name: "Humidity", StreamName: ast.DefaultStream},
  1109. RHS: &ast.JsonFieldRef{Name: "Device"},
  1110. },
  1111. }},
  1112. []ast.StreamName{},
  1113. nil,
  1114. )},
  1115. Name: "meta",
  1116. AName: "hdevice",
  1117. },
  1118. {
  1119. Expr: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  1120. Name: "temp",
  1121. AName: "",
  1122. },
  1123. },
  1124. isAggregate: false,
  1125. sendMeta: false,
  1126. }.Init(),
  1127. },
  1128. { // 11 join with same name field and aliased
  1129. sql: `SELECT src2.hum AS hum1, tableInPlanner.hum AS hum2 FROM src2 INNER JOIN tableInPlanner on id2 = id WHERE hum1 > hum2`,
  1130. p: ProjectPlan{
  1131. baseLogicalPlan: baseLogicalPlan{
  1132. children: []LogicalPlan{
  1133. JoinPlan{
  1134. baseLogicalPlan: baseLogicalPlan{
  1135. children: []LogicalPlan{
  1136. JoinAlignPlan{
  1137. baseLogicalPlan: baseLogicalPlan{
  1138. children: []LogicalPlan{
  1139. DataSourcePlan{
  1140. name: "src2",
  1141. streamFields: map[string]*ast.JsonStreamField{
  1142. "hum": {
  1143. Type: "bigint",
  1144. },
  1145. "id2": {
  1146. Type: "bigint",
  1147. },
  1148. },
  1149. streamStmt: streams["src2"],
  1150. metaFields: []string{},
  1151. timestampFormat: "YYYY-MM-dd HH:mm:ss",
  1152. pruneFields: []string{},
  1153. }.Init(),
  1154. DataSourcePlan{
  1155. name: "tableInPlanner",
  1156. streamFields: map[string]*ast.JsonStreamField{
  1157. "hum": {
  1158. Type: "bigint",
  1159. },
  1160. "id": {
  1161. Type: "bigint",
  1162. },
  1163. },
  1164. streamStmt: streams["tableInPlanner"],
  1165. metaFields: []string{},
  1166. pruneFields: []string{},
  1167. }.Init(),
  1168. },
  1169. },
  1170. Emitters: []string{"tableInPlanner"},
  1171. }.Init(),
  1172. },
  1173. },
  1174. from: &ast.Table{
  1175. Name: "src2",
  1176. },
  1177. joins: []ast.Join{
  1178. {
  1179. Name: "tableInPlanner",
  1180. Alias: "",
  1181. JoinType: ast.INNER_JOIN,
  1182. Expr: &ast.BinaryExpr{
  1183. RHS: &ast.BinaryExpr{
  1184. OP: ast.EQ,
  1185. LHS: &ast.FieldRef{Name: "id2", StreamName: "src2"},
  1186. RHS: &ast.FieldRef{Name: "id", StreamName: "tableInPlanner"},
  1187. },
  1188. OP: ast.AND,
  1189. LHS: &ast.BinaryExpr{
  1190. OP: ast.GT,
  1191. LHS: &ast.FieldRef{Name: "hum1", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  1192. &ast.FieldRef{
  1193. Name: "hum",
  1194. StreamName: "src2",
  1195. },
  1196. []ast.StreamName{"src2"},
  1197. &boolFalse,
  1198. )},
  1199. RHS: &ast.FieldRef{Name: "hum2", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  1200. &ast.FieldRef{
  1201. Name: "hum",
  1202. StreamName: "tableInPlanner",
  1203. },
  1204. []ast.StreamName{"tableInPlanner"},
  1205. &boolFalse,
  1206. )},
  1207. },
  1208. },
  1209. },
  1210. },
  1211. }.Init(),
  1212. },
  1213. },
  1214. fields: []ast.Field{
  1215. {
  1216. Expr: &ast.FieldRef{Name: "hum1", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  1217. &ast.FieldRef{
  1218. Name: "hum",
  1219. StreamName: "src2",
  1220. },
  1221. []ast.StreamName{"src2"},
  1222. &boolFalse,
  1223. )},
  1224. Name: "hum",
  1225. AName: "hum1",
  1226. }, {
  1227. Expr: &ast.FieldRef{Name: "hum2", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  1228. &ast.FieldRef{
  1229. Name: "hum",
  1230. StreamName: "tableInPlanner",
  1231. },
  1232. []ast.StreamName{"tableInPlanner"},
  1233. &boolFalse,
  1234. )},
  1235. Name: "hum",
  1236. AName: "hum2",
  1237. },
  1238. },
  1239. isAggregate: false,
  1240. sendMeta: false,
  1241. }.Init(),
  1242. },
  1243. { // 12 meta with more fields
  1244. sql: `SELECT temp, meta(*) as m FROM src1 WHERE meta(device)="demo2"`,
  1245. p: ProjectPlan{
  1246. baseLogicalPlan: baseLogicalPlan{
  1247. children: []LogicalPlan{
  1248. FilterPlan{
  1249. baseLogicalPlan: baseLogicalPlan{
  1250. children: []LogicalPlan{
  1251. DataSourcePlan{
  1252. name: "src1",
  1253. streamFields: map[string]*ast.JsonStreamField{
  1254. "temp": {
  1255. Type: "bigint",
  1256. },
  1257. },
  1258. streamStmt: streams["src1"],
  1259. metaFields: []string{},
  1260. allMeta: true,
  1261. pruneFields: []string{},
  1262. }.Init(),
  1263. },
  1264. },
  1265. condition: &ast.BinaryExpr{
  1266. LHS: &ast.Call{
  1267. Name: "meta",
  1268. FuncId: 1,
  1269. Args: []ast.Expr{&ast.MetaRef{
  1270. Name: "device",
  1271. StreamName: ast.DefaultStream,
  1272. }},
  1273. },
  1274. OP: ast.EQ,
  1275. RHS: &ast.StringLiteral{
  1276. Val: "demo2",
  1277. },
  1278. },
  1279. }.Init(),
  1280. },
  1281. },
  1282. fields: []ast.Field{
  1283. {
  1284. Expr: &ast.FieldRef{Name: "m", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  1285. &ast.Call{Name: "meta", FuncId: 0, Args: []ast.Expr{&ast.MetaRef{
  1286. Name: "*",
  1287. StreamName: ast.DefaultStream,
  1288. }}},
  1289. []ast.StreamName{},
  1290. nil,
  1291. )},
  1292. Name: "meta",
  1293. AName: "m",
  1294. },
  1295. {
  1296. Expr: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  1297. Name: "temp",
  1298. AName: "",
  1299. },
  1300. },
  1301. isAggregate: false,
  1302. sendMeta: false,
  1303. }.Init(),
  1304. },
  1305. { // 13 analytic function plan
  1306. sql: `SELECT latest(lag(name)), id1 FROM src1 WHERE lag(temp) > temp`,
  1307. p: ProjectPlan{
  1308. baseLogicalPlan: baseLogicalPlan{
  1309. children: []LogicalPlan{
  1310. FilterPlan{
  1311. baseLogicalPlan: baseLogicalPlan{
  1312. children: []LogicalPlan{
  1313. AnalyticFuncsPlan{
  1314. baseLogicalPlan: baseLogicalPlan{
  1315. children: []LogicalPlan{
  1316. DataSourcePlan{
  1317. name: "src1",
  1318. streamFields: map[string]*ast.JsonStreamField{
  1319. "id1": {
  1320. Type: "bigint",
  1321. },
  1322. "name": {
  1323. Type: "string",
  1324. },
  1325. "temp": {
  1326. Type: "bigint",
  1327. },
  1328. },
  1329. streamStmt: streams["src1"],
  1330. metaFields: []string{},
  1331. pruneFields: []string{},
  1332. }.Init(),
  1333. },
  1334. },
  1335. funcs: []*ast.Call{
  1336. {
  1337. Name: "lag",
  1338. FuncId: 2,
  1339. CachedField: "$$a_lag_2",
  1340. Args: []ast.Expr{&ast.FieldRef{
  1341. Name: "temp",
  1342. StreamName: "src1",
  1343. }},
  1344. },
  1345. },
  1346. fieldFuncs: []*ast.Call{
  1347. {
  1348. Name: "lag", FuncId: 0, CachedField: "$$a_lag_0", FuncType: ast.FuncTypeScalar, Args: []ast.Expr{&ast.FieldRef{Name: "name", StreamName: "src1"}},
  1349. },
  1350. {
  1351. Name: "latest", FuncId: 1, CachedField: "$$a_latest_1", FuncType: ast.FuncTypeScalar, Args: []ast.Expr{&ast.Call{Name: "lag", FuncId: 0, Cached: true, CachedField: "$$a_lag_0", FuncType: ast.FuncTypeScalar, Args: []ast.Expr{&ast.FieldRef{Name: "name", StreamName: "src1"}}}},
  1352. },
  1353. },
  1354. }.Init(),
  1355. },
  1356. },
  1357. condition: &ast.BinaryExpr{
  1358. LHS: &ast.Call{
  1359. Name: "lag",
  1360. FuncId: 2,
  1361. Args: []ast.Expr{&ast.FieldRef{
  1362. Name: "temp",
  1363. StreamName: "src1",
  1364. }},
  1365. CachedField: "$$a_lag_2",
  1366. Cached: true,
  1367. },
  1368. OP: ast.GT,
  1369. RHS: &ast.FieldRef{
  1370. Name: "temp",
  1371. StreamName: "src1",
  1372. },
  1373. },
  1374. }.Init(),
  1375. },
  1376. },
  1377. fields: []ast.Field{
  1378. {
  1379. Expr: &ast.Call{
  1380. Name: "latest",
  1381. FuncId: 1,
  1382. Args: []ast.Expr{&ast.Call{Name: "lag", FuncId: 0, Cached: true, CachedField: "$$a_lag_0", FuncType: ast.FuncTypeScalar, Args: []ast.Expr{&ast.FieldRef{Name: "name", StreamName: "src1"}}}},
  1383. CachedField: "$$a_latest_1",
  1384. Cached: true,
  1385. },
  1386. Name: "latest",
  1387. }, {
  1388. Expr: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  1389. Name: "id1",
  1390. },
  1391. },
  1392. isAggregate: false,
  1393. sendMeta: false,
  1394. }.Init(),
  1395. },
  1396. { // 14
  1397. sql: `SELECT name, *, meta(device) FROM src1`,
  1398. p: ProjectPlan{
  1399. baseLogicalPlan: baseLogicalPlan{
  1400. children: []LogicalPlan{
  1401. DataSourcePlan{
  1402. baseLogicalPlan: baseLogicalPlan{},
  1403. name: "src1",
  1404. streamFields: map[string]*ast.JsonStreamField{
  1405. "id1": {
  1406. Type: "bigint",
  1407. },
  1408. "temp": {
  1409. Type: "bigint",
  1410. },
  1411. "name": {
  1412. Type: "string",
  1413. },
  1414. "myarray": {
  1415. Type: "array",
  1416. Items: &ast.JsonStreamField{
  1417. Type: "string",
  1418. },
  1419. },
  1420. },
  1421. streamStmt: streams["src1"],
  1422. metaFields: []string{"device"},
  1423. isWildCard: true,
  1424. pruneFields: []string{},
  1425. }.Init(),
  1426. },
  1427. },
  1428. fields: []ast.Field{
  1429. {
  1430. Expr: &ast.FieldRef{Name: "name", StreamName: "src1"},
  1431. Name: "name",
  1432. AName: "",
  1433. },
  1434. {
  1435. Name: "*",
  1436. Expr: &ast.Wildcard{
  1437. Token: ast.ASTERISK,
  1438. },
  1439. },
  1440. {
  1441. Name: "meta",
  1442. Expr: &ast.Call{
  1443. Name: "meta",
  1444. Args: []ast.Expr{
  1445. &ast.MetaRef{
  1446. StreamName: ast.DefaultStream,
  1447. Name: "device",
  1448. },
  1449. },
  1450. },
  1451. },
  1452. },
  1453. isAggregate: false,
  1454. allWildcard: true,
  1455. sendMeta: false,
  1456. }.Init(),
  1457. },
  1458. { // 15 analytic function over partition plan
  1459. sql: `SELECT latest(lag(name)) OVER (PARTITION BY temp), id1 FROM src1 WHERE lag(temp) > temp`,
  1460. p: ProjectPlan{
  1461. baseLogicalPlan: baseLogicalPlan{
  1462. children: []LogicalPlan{
  1463. FilterPlan{
  1464. baseLogicalPlan: baseLogicalPlan{
  1465. children: []LogicalPlan{
  1466. AnalyticFuncsPlan{
  1467. baseLogicalPlan: baseLogicalPlan{
  1468. children: []LogicalPlan{
  1469. DataSourcePlan{
  1470. name: "src1",
  1471. streamFields: map[string]*ast.JsonStreamField{
  1472. "id1": {
  1473. Type: "bigint",
  1474. },
  1475. "name": {
  1476. Type: "string",
  1477. },
  1478. "temp": {
  1479. Type: "bigint",
  1480. },
  1481. },
  1482. streamStmt: streams["src1"],
  1483. metaFields: []string{},
  1484. pruneFields: []string{},
  1485. }.Init(),
  1486. },
  1487. },
  1488. funcs: []*ast.Call{
  1489. {
  1490. Name: "lag",
  1491. FuncId: 2,
  1492. CachedField: "$$a_lag_2",
  1493. Args: []ast.Expr{&ast.FieldRef{
  1494. Name: "temp",
  1495. StreamName: "src1",
  1496. }},
  1497. },
  1498. },
  1499. fieldFuncs: []*ast.Call{
  1500. {
  1501. Name: "lag", FuncId: 0, CachedField: "$$a_lag_0", FuncType: ast.FuncTypeScalar, Args: []ast.Expr{&ast.FieldRef{Name: "name", StreamName: "src1"}},
  1502. },
  1503. {
  1504. Name: "latest", FuncId: 1, CachedField: "$$a_latest_1", FuncType: ast.FuncTypeScalar, Args: []ast.Expr{&ast.Call{Name: "lag", FuncId: 0, Cached: true, CachedField: "$$a_lag_0", FuncType: ast.FuncTypeScalar, Args: []ast.Expr{&ast.FieldRef{Name: "name", StreamName: "src1"}}}}, Partition: &ast.PartitionExpr{Exprs: []ast.Expr{&ast.FieldRef{Name: "temp", StreamName: "src1"}}},
  1505. },
  1506. },
  1507. }.Init(),
  1508. },
  1509. },
  1510. condition: &ast.BinaryExpr{
  1511. LHS: &ast.Call{
  1512. Name: "lag",
  1513. FuncId: 2,
  1514. Args: []ast.Expr{&ast.FieldRef{
  1515. Name: "temp",
  1516. StreamName: "src1",
  1517. }},
  1518. CachedField: "$$a_lag_2",
  1519. Cached: true,
  1520. },
  1521. OP: ast.GT,
  1522. RHS: &ast.FieldRef{
  1523. Name: "temp",
  1524. StreamName: "src1",
  1525. },
  1526. },
  1527. }.Init(),
  1528. },
  1529. },
  1530. fields: []ast.Field{
  1531. {
  1532. Expr: &ast.Call{
  1533. Name: "latest",
  1534. FuncId: 1,
  1535. Args: []ast.Expr{&ast.Call{Name: "lag", FuncId: 0, Cached: true, CachedField: "$$a_lag_0", FuncType: ast.FuncTypeScalar, Args: []ast.Expr{&ast.FieldRef{Name: "name", StreamName: "src1"}}}},
  1536. CachedField: "$$a_latest_1",
  1537. Cached: true,
  1538. Partition: &ast.PartitionExpr{
  1539. Exprs: []ast.Expr{
  1540. &ast.FieldRef{Name: "temp", StreamName: "src1"},
  1541. },
  1542. },
  1543. },
  1544. Name: "latest",
  1545. }, {
  1546. Expr: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  1547. Name: "id1",
  1548. },
  1549. },
  1550. isAggregate: false,
  1551. sendMeta: false,
  1552. }.Init(),
  1553. },
  1554. { // 16 analytic function over partition when plan
  1555. sql: `SELECT latest(lag(name)) OVER (PARTITION BY temp WHEN temp > 12), id1 FROM src1 WHERE lag(temp) > temp`,
  1556. p: ProjectPlan{
  1557. baseLogicalPlan: baseLogicalPlan{
  1558. children: []LogicalPlan{
  1559. FilterPlan{
  1560. baseLogicalPlan: baseLogicalPlan{
  1561. children: []LogicalPlan{
  1562. AnalyticFuncsPlan{
  1563. baseLogicalPlan: baseLogicalPlan{
  1564. children: []LogicalPlan{
  1565. DataSourcePlan{
  1566. name: "src1",
  1567. streamFields: map[string]*ast.JsonStreamField{
  1568. "id1": {
  1569. Type: "bigint",
  1570. },
  1571. "name": {
  1572. Type: "string",
  1573. },
  1574. "temp": {
  1575. Type: "bigint",
  1576. },
  1577. },
  1578. streamStmt: streams["src1"],
  1579. metaFields: []string{},
  1580. pruneFields: []string{},
  1581. }.Init(),
  1582. },
  1583. },
  1584. funcs: []*ast.Call{
  1585. {
  1586. Name: "lag",
  1587. FuncId: 2,
  1588. CachedField: "$$a_lag_2",
  1589. Args: []ast.Expr{&ast.FieldRef{
  1590. Name: "temp",
  1591. StreamName: "src1",
  1592. }},
  1593. },
  1594. },
  1595. fieldFuncs: []*ast.Call{
  1596. {
  1597. Name: "lag", FuncId: 0, CachedField: "$$a_lag_0", FuncType: ast.FuncTypeScalar, Args: []ast.Expr{&ast.FieldRef{Name: "name", StreamName: "src1"}},
  1598. },
  1599. {
  1600. Name: "latest", FuncId: 1, CachedField: "$$a_latest_1", FuncType: ast.FuncTypeScalar, Args: []ast.Expr{&ast.Call{Name: "lag", FuncId: 0, Cached: true, CachedField: "$$a_lag_0", FuncType: ast.FuncTypeScalar, Args: []ast.Expr{&ast.FieldRef{Name: "name", StreamName: "src1"}}}}, Partition: &ast.PartitionExpr{Exprs: []ast.Expr{&ast.FieldRef{Name: "temp", StreamName: "src1"}}}, WhenExpr: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"}, OP: ast.GT, RHS: &ast.IntegerLiteral{Val: 12}},
  1601. },
  1602. },
  1603. }.Init(),
  1604. },
  1605. },
  1606. condition: &ast.BinaryExpr{
  1607. LHS: &ast.Call{
  1608. Name: "lag",
  1609. FuncId: 2,
  1610. Args: []ast.Expr{&ast.FieldRef{
  1611. Name: "temp",
  1612. StreamName: "src1",
  1613. }},
  1614. CachedField: "$$a_lag_2",
  1615. Cached: true,
  1616. },
  1617. OP: ast.GT,
  1618. RHS: &ast.FieldRef{
  1619. Name: "temp",
  1620. StreamName: "src1",
  1621. },
  1622. },
  1623. }.Init(),
  1624. },
  1625. },
  1626. fields: []ast.Field{
  1627. {
  1628. Expr: &ast.Call{
  1629. Name: "latest",
  1630. FuncId: 1,
  1631. Args: []ast.Expr{&ast.Call{Name: "lag", FuncId: 0, Cached: true, CachedField: "$$a_lag_0", FuncType: ast.FuncTypeScalar, Args: []ast.Expr{&ast.FieldRef{Name: "name", StreamName: "src1"}}}},
  1632. CachedField: "$$a_latest_1",
  1633. Cached: true,
  1634. Partition: &ast.PartitionExpr{
  1635. Exprs: []ast.Expr{
  1636. &ast.FieldRef{Name: "temp", StreamName: "src1"},
  1637. },
  1638. },
  1639. WhenExpr: &ast.BinaryExpr{
  1640. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  1641. OP: ast.GT,
  1642. RHS: &ast.IntegerLiteral{Val: 12},
  1643. },
  1644. },
  1645. Name: "latest",
  1646. }, {
  1647. Expr: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  1648. Name: "id1",
  1649. },
  1650. },
  1651. isAggregate: false,
  1652. sendMeta: false,
  1653. }.Init(),
  1654. },
  1655. { // 17. do not optimize sliding window
  1656. sql: `SELECT * FROM src1 WHERE temp > 20 GROUP BY SLIDINGWINDOW(ss, 10) HAVING COUNT(*) > 2`,
  1657. p: ProjectPlan{
  1658. baseLogicalPlan: baseLogicalPlan{
  1659. children: []LogicalPlan{
  1660. HavingPlan{
  1661. baseLogicalPlan: baseLogicalPlan{
  1662. children: []LogicalPlan{
  1663. FilterPlan{
  1664. baseLogicalPlan: baseLogicalPlan{
  1665. children: []LogicalPlan{
  1666. WindowPlan{
  1667. baseLogicalPlan: baseLogicalPlan{
  1668. children: []LogicalPlan{
  1669. DataSourcePlan{
  1670. name: "src1",
  1671. isWildCard: true,
  1672. streamFields: map[string]*ast.JsonStreamField{
  1673. "id1": {
  1674. Type: "bigint",
  1675. },
  1676. "temp": {
  1677. Type: "bigint",
  1678. },
  1679. "name": {
  1680. Type: "string",
  1681. },
  1682. "myarray": {
  1683. Type: "array",
  1684. Items: &ast.JsonStreamField{
  1685. Type: "string",
  1686. },
  1687. },
  1688. },
  1689. streamStmt: streams["src1"],
  1690. metaFields: []string{},
  1691. pruneFields: []string{},
  1692. }.Init(),
  1693. },
  1694. },
  1695. condition: nil,
  1696. wtype: ast.SLIDING_WINDOW,
  1697. length: 10,
  1698. timeUnit: ast.SS,
  1699. interval: 0,
  1700. limit: 0,
  1701. }.Init(),
  1702. },
  1703. },
  1704. condition: &ast.BinaryExpr{
  1705. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  1706. OP: ast.GT,
  1707. RHS: &ast.IntegerLiteral{Val: 20},
  1708. },
  1709. }.Init(),
  1710. },
  1711. },
  1712. condition: &ast.BinaryExpr{
  1713. LHS: &ast.Call{Name: "count", FuncId: 0, Args: []ast.Expr{&ast.Wildcard{
  1714. Token: ast.ASTERISK,
  1715. }}, FuncType: ast.FuncTypeAgg},
  1716. OP: ast.GT,
  1717. RHS: &ast.IntegerLiteral{Val: 2},
  1718. },
  1719. }.Init(),
  1720. },
  1721. },
  1722. fields: []ast.Field{
  1723. {
  1724. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  1725. Name: "*",
  1726. AName: "",
  1727. },
  1728. },
  1729. isAggregate: false,
  1730. sendMeta: false,
  1731. }.Init(),
  1732. },
  1733. {
  1734. // 18 analytic function over when plan
  1735. sql: `SELECT CASE WHEN lag(temp) OVER (WHEN lag(id1) > 1) BETWEEN 0 AND 10 THEN 1 ELSE 0 END FROM src1`,
  1736. p: ProjectPlan{
  1737. baseLogicalPlan: baseLogicalPlan{
  1738. children: []LogicalPlan{
  1739. AnalyticFuncsPlan{
  1740. baseLogicalPlan: baseLogicalPlan{
  1741. children: []LogicalPlan{
  1742. DataSourcePlan{
  1743. name: "src1",
  1744. streamFields: map[string]*ast.JsonStreamField{
  1745. "id1": {
  1746. Type: "bigint",
  1747. },
  1748. "temp": {
  1749. Type: "bigint",
  1750. },
  1751. },
  1752. streamStmt: &ast.StreamStmt{
  1753. Name: "src1",
  1754. StreamFields: []ast.StreamField{
  1755. {
  1756. Name: "id1",
  1757. FieldType: &ast.BasicType{
  1758. Type: ast.DataType(1),
  1759. },
  1760. },
  1761. {
  1762. Name: "temp",
  1763. FieldType: &ast.BasicType{
  1764. Type: ast.DataType(1),
  1765. },
  1766. },
  1767. {
  1768. Name: "name",
  1769. FieldType: &ast.BasicType{
  1770. Type: ast.DataType(3),
  1771. },
  1772. },
  1773. {
  1774. Name: "myarray",
  1775. FieldType: &ast.ArrayType{
  1776. Type: ast.DataType(3),
  1777. },
  1778. },
  1779. },
  1780. Options: &ast.Options{
  1781. DATASOURCE: "src1",
  1782. KEY: "ts",
  1783. FORMAT: "json",
  1784. },
  1785. StreamType: ast.StreamType(0),
  1786. },
  1787. metaFields: []string{},
  1788. pruneFields: []string{},
  1789. }.Init(),
  1790. },
  1791. },
  1792. fieldFuncs: []*ast.Call{
  1793. {
  1794. Name: "lag",
  1795. FuncId: 1,
  1796. FuncType: ast.FuncType(0),
  1797. Args: []ast.Expr{
  1798. &ast.FieldRef{
  1799. StreamName: "src1",
  1800. Name: "id1",
  1801. },
  1802. },
  1803. CachedField: "$$a_lag_1",
  1804. },
  1805. {
  1806. Name: "lag",
  1807. FuncId: 0,
  1808. FuncType: ast.FuncType(0),
  1809. Args: []ast.Expr{
  1810. &ast.FieldRef{
  1811. StreamName: "src1",
  1812. Name: "temp",
  1813. },
  1814. },
  1815. CachedField: "$$a_lag_0",
  1816. WhenExpr: &ast.BinaryExpr{
  1817. OP: ast.GT,
  1818. LHS: &ast.Call{
  1819. Name: "lag",
  1820. FuncId: 1,
  1821. FuncType: ast.FuncType(0),
  1822. Args: []ast.Expr{
  1823. &ast.FieldRef{
  1824. StreamName: "src1",
  1825. Name: "id1",
  1826. },
  1827. },
  1828. CachedField: "$$a_lag_1",
  1829. Cached: true,
  1830. },
  1831. RHS: &ast.IntegerLiteral{
  1832. Val: 1,
  1833. },
  1834. },
  1835. },
  1836. },
  1837. }.Init(),
  1838. },
  1839. },
  1840. fields: []ast.Field{
  1841. {
  1842. Name: "kuiper_field_0",
  1843. Expr: &ast.CaseExpr{
  1844. WhenClauses: []*ast.WhenClause{
  1845. {
  1846. Expr: &ast.BinaryExpr{
  1847. OP: ast.BETWEEN,
  1848. LHS: &ast.Call{
  1849. Name: "lag",
  1850. FuncId: 0,
  1851. FuncType: ast.FuncType(0),
  1852. Args: []ast.Expr{
  1853. &ast.FieldRef{
  1854. StreamName: "src1",
  1855. Name: "temp",
  1856. },
  1857. },
  1858. CachedField: "$$a_lag_0",
  1859. Cached: true,
  1860. WhenExpr: &ast.BinaryExpr{
  1861. OP: ast.GT,
  1862. LHS: &ast.Call{
  1863. Name: "lag",
  1864. FuncId: 1,
  1865. FuncType: ast.FuncType(0),
  1866. Args: []ast.Expr{
  1867. &ast.FieldRef{
  1868. StreamName: "src1",
  1869. Name: "id1",
  1870. },
  1871. },
  1872. CachedField: "$$a_lag_1",
  1873. Cached: true,
  1874. },
  1875. RHS: &ast.IntegerLiteral{
  1876. Val: 1,
  1877. },
  1878. },
  1879. },
  1880. RHS: &ast.BetweenExpr{
  1881. Lower: &ast.IntegerLiteral{
  1882. Val: 0,
  1883. },
  1884. Higher: &ast.IntegerLiteral{
  1885. Val: 10,
  1886. },
  1887. },
  1888. },
  1889. Result: &ast.IntegerLiteral{
  1890. Val: 1,
  1891. },
  1892. },
  1893. },
  1894. ElseClause: &ast.IntegerLiteral{
  1895. Val: 0,
  1896. },
  1897. },
  1898. },
  1899. },
  1900. }.Init(),
  1901. },
  1902. { // 19
  1903. sql: `SELECT * EXCEPT(id1, name), meta(device) FROM src1`,
  1904. p: ProjectPlan{
  1905. baseLogicalPlan: baseLogicalPlan{
  1906. children: []LogicalPlan{
  1907. DataSourcePlan{
  1908. baseLogicalPlan: baseLogicalPlan{},
  1909. name: "src1",
  1910. streamFields: map[string]*ast.JsonStreamField{
  1911. "temp": {
  1912. Type: "bigint",
  1913. },
  1914. "myarray": {
  1915. Type: "array",
  1916. Items: &ast.JsonStreamField{
  1917. Type: "string",
  1918. },
  1919. },
  1920. },
  1921. streamStmt: streams["src1"],
  1922. metaFields: []string{"device"},
  1923. isWildCard: false,
  1924. pruneFields: []string{"id1", "name"},
  1925. }.Init(),
  1926. },
  1927. },
  1928. fields: []ast.Field{
  1929. {
  1930. Name: "*",
  1931. Expr: &ast.Wildcard{
  1932. Token: ast.ASTERISK,
  1933. Except: []string{"id1", "name"},
  1934. },
  1935. },
  1936. {
  1937. Name: "meta",
  1938. Expr: &ast.Call{
  1939. Name: "meta",
  1940. Args: []ast.Expr{
  1941. &ast.MetaRef{
  1942. StreamName: ast.DefaultStream,
  1943. Name: "device",
  1944. },
  1945. },
  1946. },
  1947. },
  1948. },
  1949. isAggregate: false,
  1950. allWildcard: true,
  1951. sendMeta: false,
  1952. }.Init(),
  1953. },
  1954. { // 20
  1955. sql: `SELECT * REPLACE(temp * 2 AS id1, myarray * 2 AS name), meta(device) FROM src1`,
  1956. p: ProjectPlan{
  1957. baseLogicalPlan: baseLogicalPlan{
  1958. children: []LogicalPlan{
  1959. DataSourcePlan{
  1960. baseLogicalPlan: baseLogicalPlan{},
  1961. name: "src1",
  1962. streamFields: map[string]*ast.JsonStreamField{
  1963. "temp": {
  1964. Type: "bigint",
  1965. },
  1966. "myarray": {
  1967. Type: "array",
  1968. Items: &ast.JsonStreamField{
  1969. Type: "string",
  1970. },
  1971. },
  1972. },
  1973. streamStmt: streams["src1"],
  1974. metaFields: []string{"device"},
  1975. isWildCard: false,
  1976. pruneFields: []string{"id1", "name"},
  1977. }.Init(),
  1978. },
  1979. },
  1980. fields: []ast.Field{
  1981. {
  1982. Name: "*",
  1983. Expr: &ast.Wildcard{
  1984. Token: ast.ASTERISK,
  1985. Replace: []ast.Field{
  1986. {
  1987. AName: "id1",
  1988. Expr: &ast.BinaryExpr{
  1989. OP: ast.MUL,
  1990. LHS: &ast.FieldRef{
  1991. Name: "temp",
  1992. StreamName: "src1",
  1993. },
  1994. RHS: &ast.IntegerLiteral{Val: 2},
  1995. },
  1996. },
  1997. {
  1998. AName: "name",
  1999. Expr: &ast.BinaryExpr{
  2000. OP: ast.MUL,
  2001. LHS: &ast.FieldRef{
  2002. Name: "myarray",
  2003. StreamName: "src1",
  2004. },
  2005. RHS: &ast.IntegerLiteral{Val: 2},
  2006. },
  2007. },
  2008. },
  2009. },
  2010. },
  2011. {
  2012. Name: "meta",
  2013. Expr: &ast.Call{
  2014. Name: "meta",
  2015. Args: []ast.Expr{
  2016. &ast.MetaRef{
  2017. StreamName: ast.DefaultStream,
  2018. Name: "device",
  2019. },
  2020. },
  2021. },
  2022. },
  2023. },
  2024. isAggregate: false,
  2025. allWildcard: true,
  2026. sendMeta: false,
  2027. }.Init(),
  2028. },
  2029. { // 21
  2030. sql: `SELECT collect( * EXCEPT(id1, name)) FROM src1 GROUP BY TUMBLINGWINDOW(ss, 10)`,
  2031. p: ProjectPlan{
  2032. baseLogicalPlan: baseLogicalPlan{
  2033. children: []LogicalPlan{
  2034. WindowPlan{
  2035. baseLogicalPlan: baseLogicalPlan{
  2036. children: []LogicalPlan{
  2037. DataSourcePlan{
  2038. baseLogicalPlan: baseLogicalPlan{},
  2039. name: "src1",
  2040. streamFields: map[string]*ast.JsonStreamField{
  2041. "temp": {
  2042. Type: "bigint",
  2043. },
  2044. "myarray": {
  2045. Type: "array",
  2046. Items: &ast.JsonStreamField{
  2047. Type: "string",
  2048. },
  2049. },
  2050. },
  2051. streamStmt: streams["src1"],
  2052. metaFields: []string{},
  2053. isWildCard: false,
  2054. pruneFields: []string{"id1", "name"},
  2055. }.Init(),
  2056. },
  2057. },
  2058. condition: nil,
  2059. wtype: ast.TUMBLING_WINDOW,
  2060. length: 10,
  2061. timeUnit: ast.SS,
  2062. interval: 0,
  2063. limit: 0,
  2064. }.Init(),
  2065. },
  2066. },
  2067. fields: []ast.Field{
  2068. {
  2069. Name: "collect",
  2070. Expr: &ast.Call{
  2071. Name: "collect",
  2072. FuncType: ast.FuncTypeAgg,
  2073. Args: []ast.Expr{
  2074. &ast.Wildcard{
  2075. Token: ast.ASTERISK,
  2076. Except: []string{"id1", "name"},
  2077. },
  2078. },
  2079. },
  2080. },
  2081. },
  2082. isAggregate: true,
  2083. allWildcard: false,
  2084. sendMeta: false,
  2085. }.Init(),
  2086. },
  2087. { // 22
  2088. sql: `SELECT collect( * REPLACE(temp * 2 AS id1, myarray * 2 AS name)) FROM src1 GROUP BY TUMBLINGWINDOW(ss, 10)`,
  2089. p: ProjectPlan{
  2090. baseLogicalPlan: baseLogicalPlan{
  2091. children: []LogicalPlan{
  2092. WindowPlan{
  2093. baseLogicalPlan: baseLogicalPlan{
  2094. children: []LogicalPlan{
  2095. DataSourcePlan{
  2096. baseLogicalPlan: baseLogicalPlan{},
  2097. name: "src1",
  2098. streamFields: map[string]*ast.JsonStreamField{
  2099. "temp": {
  2100. Type: "bigint",
  2101. },
  2102. "myarray": {
  2103. Type: "array",
  2104. Items: &ast.JsonStreamField{
  2105. Type: "string",
  2106. },
  2107. },
  2108. },
  2109. streamStmt: streams["src1"],
  2110. metaFields: []string{},
  2111. isWildCard: false,
  2112. pruneFields: []string{"id1", "name"},
  2113. }.Init(),
  2114. },
  2115. },
  2116. condition: nil,
  2117. wtype: ast.TUMBLING_WINDOW,
  2118. length: 10,
  2119. timeUnit: ast.SS,
  2120. interval: 0,
  2121. limit: 0,
  2122. }.Init(),
  2123. },
  2124. },
  2125. fields: []ast.Field{
  2126. {
  2127. Name: "collect",
  2128. Expr: &ast.Call{
  2129. Name: "collect",
  2130. FuncType: ast.FuncTypeAgg,
  2131. Args: []ast.Expr{
  2132. &ast.Wildcard{
  2133. Token: ast.ASTERISK,
  2134. Replace: []ast.Field{
  2135. {
  2136. AName: "id1",
  2137. Expr: &ast.BinaryExpr{
  2138. OP: ast.MUL,
  2139. LHS: &ast.FieldRef{
  2140. Name: "temp",
  2141. StreamName: "src1",
  2142. },
  2143. RHS: &ast.IntegerLiteral{Val: 2},
  2144. },
  2145. },
  2146. {
  2147. AName: "name",
  2148. Expr: &ast.BinaryExpr{
  2149. OP: ast.MUL,
  2150. LHS: &ast.FieldRef{
  2151. Name: "myarray",
  2152. StreamName: "src1",
  2153. },
  2154. RHS: &ast.IntegerLiteral{Val: 2},
  2155. },
  2156. },
  2157. },
  2158. },
  2159. },
  2160. },
  2161. },
  2162. },
  2163. isAggregate: true,
  2164. allWildcard: false,
  2165. sendMeta: false,
  2166. }.Init(),
  2167. },
  2168. { // 23
  2169. sql: `SELECT id1 FROM src1 GROUP BY TUMBLINGWINDOW(ss, 10) HAVING count(* EXCEPT(id1, name)) > 0`,
  2170. p: ProjectPlan{
  2171. baseLogicalPlan: baseLogicalPlan{
  2172. children: []LogicalPlan{
  2173. HavingPlan{
  2174. baseLogicalPlan: baseLogicalPlan{
  2175. children: []LogicalPlan{
  2176. WindowPlan{
  2177. baseLogicalPlan: baseLogicalPlan{
  2178. children: []LogicalPlan{
  2179. DataSourcePlan{
  2180. baseLogicalPlan: baseLogicalPlan{},
  2181. name: "src1",
  2182. streamFields: map[string]*ast.JsonStreamField{
  2183. "id1": {
  2184. Type: "bigint",
  2185. },
  2186. "temp": {
  2187. Type: "bigint",
  2188. },
  2189. "myarray": {
  2190. Type: "array",
  2191. Items: &ast.JsonStreamField{
  2192. Type: "string",
  2193. },
  2194. },
  2195. },
  2196. streamStmt: streams["src1"],
  2197. metaFields: []string{},
  2198. isWildCard: false,
  2199. pruneFields: []string{"id1", "name"},
  2200. }.Init(),
  2201. },
  2202. },
  2203. condition: nil,
  2204. wtype: ast.TUMBLING_WINDOW,
  2205. length: 10,
  2206. timeUnit: ast.SS,
  2207. interval: 0,
  2208. limit: 0,
  2209. }.Init(),
  2210. },
  2211. },
  2212. condition: &ast.BinaryExpr{
  2213. LHS: &ast.Call{
  2214. Name: "count",
  2215. FuncId: 0,
  2216. Args: []ast.Expr{
  2217. &ast.Wildcard{
  2218. Token: ast.ASTERISK,
  2219. Except: []string{"id1", "name"},
  2220. },
  2221. },
  2222. FuncType: ast.FuncTypeAgg,
  2223. },
  2224. OP: ast.GT,
  2225. RHS: &ast.IntegerLiteral{Val: 0},
  2226. },
  2227. }.Init(),
  2228. },
  2229. },
  2230. fields: []ast.Field{
  2231. {
  2232. Name: "id1",
  2233. Expr: &ast.FieldRef{
  2234. Name: "id1",
  2235. StreamName: "src1",
  2236. },
  2237. },
  2238. },
  2239. isAggregate: false,
  2240. allWildcard: false,
  2241. sendMeta: false,
  2242. }.Init(),
  2243. },
  2244. { // 24
  2245. sql: `SELECT temp FROM src1 GROUP BY TUMBLINGWINDOW(ss, 10) HAVING count(* REPLACE(temp * 2 AS id1, myarray * 2 AS name)) > 0`,
  2246. p: ProjectPlan{
  2247. baseLogicalPlan: baseLogicalPlan{
  2248. children: []LogicalPlan{
  2249. HavingPlan{
  2250. baseLogicalPlan: baseLogicalPlan{
  2251. children: []LogicalPlan{
  2252. WindowPlan{
  2253. baseLogicalPlan: baseLogicalPlan{
  2254. children: []LogicalPlan{
  2255. DataSourcePlan{
  2256. baseLogicalPlan: baseLogicalPlan{},
  2257. name: "src1",
  2258. streamFields: map[string]*ast.JsonStreamField{
  2259. "temp": {
  2260. Type: "bigint",
  2261. },
  2262. "myarray": {
  2263. Type: "array",
  2264. Items: &ast.JsonStreamField{
  2265. Type: "string",
  2266. },
  2267. },
  2268. },
  2269. streamStmt: streams["src1"],
  2270. metaFields: []string{},
  2271. isWildCard: false,
  2272. pruneFields: []string{"id1", "name"},
  2273. }.Init(),
  2274. },
  2275. },
  2276. condition: nil,
  2277. wtype: ast.TUMBLING_WINDOW,
  2278. length: 10,
  2279. timeUnit: ast.SS,
  2280. interval: 0,
  2281. limit: 0,
  2282. }.Init(),
  2283. },
  2284. },
  2285. condition: &ast.BinaryExpr{
  2286. LHS: &ast.Call{
  2287. Name: "count",
  2288. FuncId: 0,
  2289. Args: []ast.Expr{
  2290. &ast.Wildcard{
  2291. Token: ast.ASTERISK,
  2292. Replace: []ast.Field{
  2293. {
  2294. AName: "id1",
  2295. Expr: &ast.BinaryExpr{
  2296. OP: ast.MUL,
  2297. LHS: &ast.FieldRef{
  2298. Name: "temp",
  2299. StreamName: "src1",
  2300. },
  2301. RHS: &ast.IntegerLiteral{Val: 2},
  2302. },
  2303. },
  2304. {
  2305. AName: "name",
  2306. Expr: &ast.BinaryExpr{
  2307. OP: ast.MUL,
  2308. LHS: &ast.FieldRef{
  2309. Name: "myarray",
  2310. StreamName: "src1",
  2311. },
  2312. RHS: &ast.IntegerLiteral{Val: 2},
  2313. },
  2314. },
  2315. },
  2316. },
  2317. },
  2318. FuncType: ast.FuncTypeAgg,
  2319. },
  2320. OP: ast.GT,
  2321. RHS: &ast.IntegerLiteral{Val: 0},
  2322. },
  2323. }.Init(),
  2324. },
  2325. },
  2326. fields: []ast.Field{
  2327. {
  2328. Name: "temp",
  2329. Expr: &ast.FieldRef{
  2330. Name: "temp",
  2331. StreamName: "src1",
  2332. },
  2333. },
  2334. },
  2335. isAggregate: false,
  2336. allWildcard: false,
  2337. sendMeta: false,
  2338. }.Init(),
  2339. },
  2340. }
  2341. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  2342. for i, tt := range tests {
  2343. stmt, err := xsql.NewParser(strings.NewReader(tt.sql)).Parse()
  2344. if err != nil {
  2345. t.Errorf("%d. %q: error compile sql: %s\n", i, tt.sql, err)
  2346. continue
  2347. }
  2348. p, err := createLogicalPlan(stmt, &api.RuleOption{
  2349. IsEventTime: false,
  2350. LateTol: 0,
  2351. Concurrency: 0,
  2352. BufferLength: 0,
  2353. SendMetaToSink: false,
  2354. Qos: 0,
  2355. CheckpointInterval: 0,
  2356. SendError: true,
  2357. }, kv)
  2358. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  2359. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.sql, tt.err, err)
  2360. } else if !reflect.DeepEqual(tt.p, p) {
  2361. 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))
  2362. }
  2363. }
  2364. }
  2365. func Test_createLogicalPlanSchemaless(t *testing.T) {
  2366. kv, err := store.GetKV("stream")
  2367. if err != nil {
  2368. t.Error(err)
  2369. return
  2370. }
  2371. streamSqls := map[string]string{
  2372. "src1": `CREATE STREAM src1 (
  2373. ) WITH (DATASOURCE="src1", FORMAT="json", KEY="ts");`,
  2374. "src2": `CREATE STREAM src2 (
  2375. ) WITH (DATASOURCE="src2", FORMAT="json", KEY="ts");`,
  2376. "tableInPlanner": `CREATE TABLE tableInPlanner (
  2377. id BIGINT,
  2378. name STRING,
  2379. value STRING,
  2380. hum BIGINT
  2381. ) WITH (TYPE="file");`,
  2382. }
  2383. types := map[string]ast.StreamType{
  2384. "src1": ast.TypeStream,
  2385. "src2": ast.TypeStream,
  2386. "tableInPlanner": ast.TypeTable,
  2387. }
  2388. for name, sql := range streamSqls {
  2389. s, err := json.Marshal(&xsql.StreamInfo{
  2390. StreamType: types[name],
  2391. Statement: sql,
  2392. })
  2393. if err != nil {
  2394. t.Error(err)
  2395. t.Fail()
  2396. }
  2397. err = kv.Set(name, string(s))
  2398. if err != nil {
  2399. t.Error(err)
  2400. t.Fail()
  2401. }
  2402. }
  2403. streams := make(map[string]*ast.StreamStmt)
  2404. for n := range streamSqls {
  2405. streamStmt, err := xsql.GetDataSource(kv, n)
  2406. if err != nil {
  2407. t.Errorf("fail to get stream %s, please check if stream is created", n)
  2408. return
  2409. }
  2410. streams[n] = streamStmt
  2411. }
  2412. // boolTrue = true
  2413. boolFalse := false
  2414. tests := []struct {
  2415. sql string
  2416. p LogicalPlan
  2417. err string
  2418. }{
  2419. { // 0
  2420. sql: `SELECT name FROM src1`,
  2421. p: ProjectPlan{
  2422. baseLogicalPlan: baseLogicalPlan{
  2423. children: []LogicalPlan{
  2424. DataSourcePlan{
  2425. baseLogicalPlan: baseLogicalPlan{},
  2426. name: "src1",
  2427. streamFields: map[string]*ast.JsonStreamField{
  2428. "name": nil,
  2429. },
  2430. streamStmt: streams["src1"],
  2431. isSchemaless: true,
  2432. metaFields: []string{},
  2433. pruneFields: []string{},
  2434. }.Init(),
  2435. },
  2436. },
  2437. fields: []ast.Field{
  2438. {
  2439. Expr: &ast.FieldRef{Name: "name", StreamName: "src1"},
  2440. Name: "name",
  2441. AName: "",
  2442. },
  2443. },
  2444. isAggregate: false,
  2445. sendMeta: false,
  2446. }.Init(),
  2447. }, { // 1 optimize where to data source
  2448. sql: `SELECT temp FROM src1 WHERE name = "v1" GROUP BY TUMBLINGWINDOW(ss, 10)`,
  2449. p: ProjectPlan{
  2450. baseLogicalPlan: baseLogicalPlan{
  2451. children: []LogicalPlan{
  2452. WindowPlan{
  2453. baseLogicalPlan: baseLogicalPlan{
  2454. children: []LogicalPlan{
  2455. FilterPlan{
  2456. baseLogicalPlan: baseLogicalPlan{
  2457. children: []LogicalPlan{
  2458. DataSourcePlan{
  2459. name: "src1",
  2460. streamFields: map[string]*ast.JsonStreamField{
  2461. "name": nil,
  2462. "temp": nil,
  2463. },
  2464. streamStmt: streams["src1"],
  2465. metaFields: []string{},
  2466. isSchemaless: true,
  2467. pruneFields: []string{},
  2468. }.Init(),
  2469. },
  2470. },
  2471. condition: &ast.BinaryExpr{
  2472. LHS: &ast.FieldRef{Name: "name", StreamName: "src1"},
  2473. OP: ast.EQ,
  2474. RHS: &ast.StringLiteral{Val: "v1"},
  2475. },
  2476. }.Init(),
  2477. },
  2478. },
  2479. condition: nil,
  2480. wtype: ast.TUMBLING_WINDOW,
  2481. length: 10,
  2482. timeUnit: ast.SS,
  2483. interval: 0,
  2484. limit: 0,
  2485. }.Init(),
  2486. },
  2487. },
  2488. fields: []ast.Field{
  2489. {
  2490. Expr: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  2491. Name: "temp",
  2492. AName: "",
  2493. },
  2494. },
  2495. isAggregate: false,
  2496. sendMeta: false,
  2497. }.Init(),
  2498. }, { // 2 condition that cannot be optimized
  2499. 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)`,
  2500. p: ProjectPlan{
  2501. baseLogicalPlan: baseLogicalPlan{
  2502. children: []LogicalPlan{
  2503. JoinPlan{
  2504. baseLogicalPlan: baseLogicalPlan{
  2505. children: []LogicalPlan{
  2506. WindowPlan{
  2507. baseLogicalPlan: baseLogicalPlan{
  2508. children: []LogicalPlan{
  2509. DataSourcePlan{
  2510. name: "src1",
  2511. streamFields: map[string]*ast.JsonStreamField{
  2512. "id1": nil,
  2513. "temp": nil,
  2514. },
  2515. streamStmt: streams["src1"],
  2516. metaFields: []string{},
  2517. isSchemaless: true,
  2518. pruneFields: []string{},
  2519. }.Init(),
  2520. DataSourcePlan{
  2521. name: "src2",
  2522. streamFields: map[string]*ast.JsonStreamField{ // can't determine where is id1 belonged to
  2523. "hum": nil,
  2524. "id1": nil,
  2525. "id2": nil,
  2526. },
  2527. isSchemaless: true,
  2528. streamStmt: streams["src2"],
  2529. metaFields: []string{},
  2530. pruneFields: []string{},
  2531. }.Init(),
  2532. },
  2533. },
  2534. condition: nil,
  2535. wtype: ast.TUMBLING_WINDOW,
  2536. length: 10,
  2537. timeUnit: ast.SS,
  2538. interval: 0,
  2539. limit: 0,
  2540. }.Init(),
  2541. },
  2542. },
  2543. from: &ast.Table{Name: "src1"},
  2544. joins: ast.Joins{ast.Join{
  2545. Name: "src2",
  2546. JoinType: ast.INNER_JOIN,
  2547. Expr: &ast.BinaryExpr{
  2548. OP: ast.AND,
  2549. LHS: &ast.BinaryExpr{
  2550. LHS: &ast.BinaryExpr{
  2551. OP: ast.GT,
  2552. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  2553. RHS: &ast.IntegerLiteral{Val: 20},
  2554. },
  2555. OP: ast.OR,
  2556. RHS: &ast.BinaryExpr{
  2557. OP: ast.GT,
  2558. LHS: &ast.FieldRef{Name: "hum", StreamName: "src2"},
  2559. RHS: &ast.IntegerLiteral{Val: 60},
  2560. },
  2561. },
  2562. RHS: &ast.BinaryExpr{
  2563. OP: ast.EQ,
  2564. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  2565. RHS: &ast.FieldRef{Name: "id2", StreamName: "src2"},
  2566. },
  2567. },
  2568. }},
  2569. }.Init(),
  2570. },
  2571. },
  2572. fields: []ast.Field{
  2573. {
  2574. Expr: &ast.FieldRef{Name: "id1", StreamName: ast.DefaultStream},
  2575. Name: "id1",
  2576. AName: "",
  2577. },
  2578. },
  2579. isAggregate: false,
  2580. sendMeta: false,
  2581. }.Init(),
  2582. }, { // 3 optimize window filter
  2583. sql: `SELECT id1 FROM src1 WHERE name = "v1" GROUP BY TUMBLINGWINDOW(ss, 10) FILTER( WHERE temp > 2)`,
  2584. p: ProjectPlan{
  2585. baseLogicalPlan: baseLogicalPlan{
  2586. children: []LogicalPlan{
  2587. WindowPlan{
  2588. baseLogicalPlan: baseLogicalPlan{
  2589. children: []LogicalPlan{
  2590. FilterPlan{
  2591. baseLogicalPlan: baseLogicalPlan{
  2592. children: []LogicalPlan{
  2593. DataSourcePlan{
  2594. name: "src1",
  2595. streamFields: map[string]*ast.JsonStreamField{
  2596. "id1": nil,
  2597. "name": nil,
  2598. "temp": nil,
  2599. },
  2600. isSchemaless: true,
  2601. streamStmt: streams["src1"],
  2602. metaFields: []string{},
  2603. pruneFields: []string{},
  2604. }.Init(),
  2605. },
  2606. },
  2607. condition: &ast.BinaryExpr{
  2608. OP: ast.AND,
  2609. LHS: &ast.BinaryExpr{
  2610. LHS: &ast.FieldRef{Name: "name", StreamName: "src1"},
  2611. OP: ast.EQ,
  2612. RHS: &ast.StringLiteral{Val: "v1"},
  2613. },
  2614. RHS: &ast.BinaryExpr{
  2615. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  2616. OP: ast.GT,
  2617. RHS: &ast.IntegerLiteral{Val: 2},
  2618. },
  2619. },
  2620. }.Init(),
  2621. },
  2622. },
  2623. condition: nil,
  2624. wtype: ast.TUMBLING_WINDOW,
  2625. length: 10,
  2626. timeUnit: ast.SS,
  2627. interval: 0,
  2628. limit: 0,
  2629. }.Init(),
  2630. },
  2631. },
  2632. fields: []ast.Field{
  2633. {
  2634. Expr: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  2635. Name: "id1",
  2636. AName: "",
  2637. },
  2638. },
  2639. isAggregate: false,
  2640. sendMeta: false,
  2641. }.Init(),
  2642. }, { // 4. do not optimize count window
  2643. sql: `SELECT * FROM src1 WHERE temp > 20 GROUP BY COUNTWINDOW(5,1) HAVING COUNT(*) > 2`,
  2644. p: ProjectPlan{
  2645. baseLogicalPlan: baseLogicalPlan{
  2646. children: []LogicalPlan{
  2647. HavingPlan{
  2648. baseLogicalPlan: baseLogicalPlan{
  2649. children: []LogicalPlan{
  2650. FilterPlan{
  2651. baseLogicalPlan: baseLogicalPlan{
  2652. children: []LogicalPlan{
  2653. WindowPlan{
  2654. baseLogicalPlan: baseLogicalPlan{
  2655. children: []LogicalPlan{
  2656. DataSourcePlan{
  2657. name: "src1",
  2658. isWildCard: true,
  2659. streamFields: map[string]*ast.JsonStreamField{},
  2660. streamStmt: streams["src1"],
  2661. metaFields: []string{},
  2662. isSchemaless: true,
  2663. pruneFields: []string{},
  2664. }.Init(),
  2665. },
  2666. },
  2667. condition: nil,
  2668. wtype: ast.COUNT_WINDOW,
  2669. length: 5,
  2670. interval: 1,
  2671. limit: 0,
  2672. }.Init(),
  2673. },
  2674. },
  2675. condition: &ast.BinaryExpr{
  2676. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  2677. OP: ast.GT,
  2678. RHS: &ast.IntegerLiteral{Val: 20},
  2679. },
  2680. }.Init(),
  2681. },
  2682. },
  2683. condition: &ast.BinaryExpr{
  2684. LHS: &ast.Call{Name: "count", FuncId: 0, Args: []ast.Expr{&ast.Wildcard{
  2685. Token: ast.ASTERISK,
  2686. }}, FuncType: ast.FuncTypeAgg},
  2687. OP: ast.GT,
  2688. RHS: &ast.IntegerLiteral{Val: 2},
  2689. },
  2690. }.Init(),
  2691. },
  2692. },
  2693. fields: []ast.Field{
  2694. {
  2695. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  2696. Name: "*",
  2697. AName: "",
  2698. },
  2699. },
  2700. isAggregate: false,
  2701. sendMeta: false,
  2702. }.Init(),
  2703. }, { // 5. optimize join on
  2704. 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)`,
  2705. p: ProjectPlan{
  2706. baseLogicalPlan: baseLogicalPlan{
  2707. children: []LogicalPlan{
  2708. JoinPlan{
  2709. baseLogicalPlan: baseLogicalPlan{
  2710. children: []LogicalPlan{
  2711. WindowPlan{
  2712. baseLogicalPlan: baseLogicalPlan{
  2713. children: []LogicalPlan{
  2714. FilterPlan{
  2715. baseLogicalPlan: baseLogicalPlan{
  2716. children: []LogicalPlan{
  2717. DataSourcePlan{
  2718. name: "src1",
  2719. streamFields: map[string]*ast.JsonStreamField{
  2720. "id1": nil,
  2721. "temp": nil,
  2722. },
  2723. isSchemaless: true,
  2724. streamStmt: streams["src1"],
  2725. metaFields: []string{},
  2726. pruneFields: []string{},
  2727. }.Init(),
  2728. },
  2729. },
  2730. condition: &ast.BinaryExpr{
  2731. RHS: &ast.BinaryExpr{
  2732. OP: ast.GT,
  2733. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  2734. RHS: &ast.IntegerLiteral{Val: 20},
  2735. },
  2736. OP: ast.AND,
  2737. LHS: &ast.BinaryExpr{
  2738. OP: ast.GT,
  2739. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  2740. RHS: &ast.IntegerLiteral{Val: 111},
  2741. },
  2742. },
  2743. }.Init(),
  2744. FilterPlan{
  2745. baseLogicalPlan: baseLogicalPlan{
  2746. children: []LogicalPlan{
  2747. DataSourcePlan{
  2748. name: "src2",
  2749. streamFields: map[string]*ast.JsonStreamField{
  2750. "hum": nil,
  2751. "id1": nil,
  2752. "id2": nil,
  2753. },
  2754. isSchemaless: true,
  2755. streamStmt: streams["src2"],
  2756. metaFields: []string{},
  2757. pruneFields: []string{},
  2758. }.Init(),
  2759. },
  2760. },
  2761. condition: &ast.BinaryExpr{
  2762. OP: ast.LT,
  2763. LHS: &ast.FieldRef{Name: "hum", StreamName: "src2"},
  2764. RHS: &ast.IntegerLiteral{Val: 60},
  2765. },
  2766. }.Init(),
  2767. },
  2768. },
  2769. condition: nil,
  2770. wtype: ast.TUMBLING_WINDOW,
  2771. length: 10,
  2772. timeUnit: ast.SS,
  2773. interval: 0,
  2774. limit: 0,
  2775. }.Init(),
  2776. },
  2777. },
  2778. from: &ast.Table{
  2779. Name: "src1",
  2780. },
  2781. joins: []ast.Join{
  2782. {
  2783. Name: "src2",
  2784. Alias: "",
  2785. JoinType: ast.INNER_JOIN,
  2786. Expr: &ast.BinaryExpr{
  2787. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  2788. OP: ast.EQ,
  2789. RHS: &ast.FieldRef{Name: "id2", StreamName: "src2"},
  2790. },
  2791. },
  2792. },
  2793. }.Init(),
  2794. },
  2795. },
  2796. fields: []ast.Field{
  2797. {
  2798. Expr: &ast.FieldRef{Name: "id1", StreamName: ast.DefaultStream},
  2799. Name: "id1",
  2800. AName: "",
  2801. },
  2802. },
  2803. isAggregate: false,
  2804. sendMeta: false,
  2805. }.Init(),
  2806. }, { // 6. optimize outter join on
  2807. 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)`,
  2808. p: ProjectPlan{
  2809. baseLogicalPlan: baseLogicalPlan{
  2810. children: []LogicalPlan{
  2811. JoinPlan{
  2812. baseLogicalPlan: baseLogicalPlan{
  2813. children: []LogicalPlan{
  2814. WindowPlan{
  2815. baseLogicalPlan: baseLogicalPlan{
  2816. children: []LogicalPlan{
  2817. FilterPlan{
  2818. baseLogicalPlan: baseLogicalPlan{
  2819. children: []LogicalPlan{
  2820. DataSourcePlan{
  2821. name: "src1",
  2822. streamFields: map[string]*ast.JsonStreamField{
  2823. "id1": nil,
  2824. "temp": nil,
  2825. },
  2826. isSchemaless: true,
  2827. streamStmt: streams["src1"],
  2828. metaFields: []string{},
  2829. pruneFields: []string{},
  2830. }.Init(),
  2831. },
  2832. },
  2833. condition: &ast.BinaryExpr{
  2834. OP: ast.GT,
  2835. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  2836. RHS: &ast.IntegerLiteral{Val: 111},
  2837. },
  2838. }.Init(),
  2839. DataSourcePlan{
  2840. name: "src2",
  2841. streamFields: map[string]*ast.JsonStreamField{
  2842. "hum": nil,
  2843. "id1": nil,
  2844. "id2": nil,
  2845. },
  2846. isSchemaless: true,
  2847. streamStmt: streams["src2"],
  2848. metaFields: []string{},
  2849. pruneFields: []string{},
  2850. }.Init(),
  2851. },
  2852. },
  2853. condition: nil,
  2854. wtype: ast.TUMBLING_WINDOW,
  2855. length: 10,
  2856. timeUnit: ast.SS,
  2857. interval: 0,
  2858. limit: 0,
  2859. }.Init(),
  2860. },
  2861. },
  2862. from: &ast.Table{
  2863. Name: "src1",
  2864. },
  2865. joins: []ast.Join{
  2866. {
  2867. Name: "src2",
  2868. Alias: "",
  2869. JoinType: ast.FULL_JOIN,
  2870. Expr: &ast.BinaryExpr{
  2871. OP: ast.AND,
  2872. LHS: &ast.BinaryExpr{
  2873. OP: ast.AND,
  2874. LHS: &ast.BinaryExpr{
  2875. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  2876. OP: ast.EQ,
  2877. RHS: &ast.FieldRef{Name: "id2", StreamName: "src2"},
  2878. },
  2879. RHS: &ast.BinaryExpr{
  2880. OP: ast.GT,
  2881. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  2882. RHS: &ast.IntegerLiteral{Val: 20},
  2883. },
  2884. },
  2885. RHS: &ast.BinaryExpr{
  2886. OP: ast.LT,
  2887. LHS: &ast.FieldRef{Name: "hum", StreamName: "src2"},
  2888. RHS: &ast.IntegerLiteral{Val: 60},
  2889. },
  2890. },
  2891. },
  2892. },
  2893. }.Init(),
  2894. },
  2895. },
  2896. fields: []ast.Field{
  2897. {
  2898. Expr: &ast.FieldRef{Name: "id1", StreamName: ast.DefaultStream},
  2899. Name: "id1",
  2900. AName: "",
  2901. },
  2902. },
  2903. isAggregate: false,
  2904. sendMeta: false,
  2905. }.Init(),
  2906. }, { // 7 window error for table
  2907. sql: `SELECT value FROM tableInPlanner WHERE name = "v1" GROUP BY TUMBLINGWINDOW(ss, 10)`,
  2908. p: nil,
  2909. err: "cannot run window for TABLE sources",
  2910. }, { // 8 join table without window
  2911. sql: `SELECT id1 FROM src1 INNER JOIN tableInPlanner on src1.id1 = tableInPlanner.id and src1.temp > 20 and hum < 60 WHERE src1.id1 > 111`,
  2912. p: ProjectPlan{
  2913. baseLogicalPlan: baseLogicalPlan{
  2914. children: []LogicalPlan{
  2915. JoinPlan{
  2916. baseLogicalPlan: baseLogicalPlan{
  2917. children: []LogicalPlan{
  2918. JoinAlignPlan{
  2919. baseLogicalPlan: baseLogicalPlan{
  2920. children: []LogicalPlan{
  2921. FilterPlan{
  2922. baseLogicalPlan: baseLogicalPlan{
  2923. children: []LogicalPlan{
  2924. DataSourcePlan{
  2925. name: "src1",
  2926. streamFields: map[string]*ast.JsonStreamField{
  2927. "hum": nil,
  2928. "id1": nil,
  2929. "temp": nil,
  2930. },
  2931. isSchemaless: true,
  2932. streamStmt: streams["src1"],
  2933. metaFields: []string{},
  2934. pruneFields: []string{},
  2935. }.Init(),
  2936. },
  2937. },
  2938. condition: &ast.BinaryExpr{
  2939. RHS: &ast.BinaryExpr{
  2940. OP: ast.GT,
  2941. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  2942. RHS: &ast.IntegerLiteral{Val: 20},
  2943. },
  2944. OP: ast.AND,
  2945. LHS: &ast.BinaryExpr{
  2946. OP: ast.GT,
  2947. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  2948. RHS: &ast.IntegerLiteral{Val: 111},
  2949. },
  2950. },
  2951. }.Init(),
  2952. DataSourcePlan{
  2953. name: "tableInPlanner",
  2954. streamFields: map[string]*ast.JsonStreamField{
  2955. "hum": {
  2956. Type: "bigint",
  2957. },
  2958. "id": {
  2959. Type: "bigint",
  2960. },
  2961. },
  2962. streamStmt: streams["tableInPlanner"],
  2963. metaFields: []string{},
  2964. pruneFields: []string{},
  2965. }.Init(),
  2966. },
  2967. },
  2968. Emitters: []string{"tableInPlanner"},
  2969. }.Init(),
  2970. },
  2971. },
  2972. from: &ast.Table{
  2973. Name: "src1",
  2974. },
  2975. joins: []ast.Join{
  2976. {
  2977. Name: "tableInPlanner",
  2978. Alias: "",
  2979. JoinType: ast.INNER_JOIN,
  2980. Expr: &ast.BinaryExpr{
  2981. OP: ast.AND,
  2982. LHS: &ast.BinaryExpr{
  2983. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  2984. OP: ast.EQ,
  2985. RHS: &ast.FieldRef{Name: "id", StreamName: "tableInPlanner"},
  2986. },
  2987. RHS: &ast.BinaryExpr{
  2988. OP: ast.LT,
  2989. LHS: &ast.FieldRef{Name: "hum", StreamName: ast.DefaultStream},
  2990. RHS: &ast.IntegerLiteral{Val: 60},
  2991. },
  2992. },
  2993. },
  2994. },
  2995. }.Init(),
  2996. },
  2997. },
  2998. fields: []ast.Field{
  2999. {
  3000. Expr: &ast.FieldRef{Name: "id1", StreamName: ast.DefaultStream},
  3001. Name: "id1",
  3002. AName: "",
  3003. },
  3004. },
  3005. isAggregate: false,
  3006. sendMeta: false,
  3007. }.Init(),
  3008. }, { // 9 join table with window
  3009. 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)`,
  3010. p: ProjectPlan{
  3011. baseLogicalPlan: baseLogicalPlan{
  3012. children: []LogicalPlan{
  3013. JoinPlan{
  3014. baseLogicalPlan: baseLogicalPlan{
  3015. children: []LogicalPlan{
  3016. JoinAlignPlan{
  3017. baseLogicalPlan: baseLogicalPlan{
  3018. children: []LogicalPlan{
  3019. WindowPlan{
  3020. baseLogicalPlan: baseLogicalPlan{
  3021. children: []LogicalPlan{
  3022. DataSourcePlan{
  3023. name: "src1",
  3024. streamFields: map[string]*ast.JsonStreamField{
  3025. "id1": nil,
  3026. "temp": nil,
  3027. },
  3028. isSchemaless: true,
  3029. streamStmt: streams["src1"],
  3030. metaFields: []string{},
  3031. pruneFields: []string{},
  3032. }.Init(),
  3033. },
  3034. },
  3035. condition: nil,
  3036. wtype: ast.TUMBLING_WINDOW,
  3037. length: 10,
  3038. timeUnit: ast.SS,
  3039. interval: 0,
  3040. limit: 0,
  3041. }.Init(),
  3042. DataSourcePlan{
  3043. name: "tableInPlanner",
  3044. streamFields: map[string]*ast.JsonStreamField{
  3045. "hum": {
  3046. Type: "bigint",
  3047. },
  3048. "id": {
  3049. Type: "bigint",
  3050. },
  3051. },
  3052. streamStmt: streams["tableInPlanner"],
  3053. metaFields: []string{},
  3054. pruneFields: []string{},
  3055. }.Init(),
  3056. },
  3057. },
  3058. Emitters: []string{"tableInPlanner"},
  3059. }.Init(),
  3060. },
  3061. },
  3062. from: &ast.Table{
  3063. Name: "src1",
  3064. },
  3065. joins: []ast.Join{
  3066. {
  3067. Name: "tableInPlanner",
  3068. Alias: "",
  3069. JoinType: ast.INNER_JOIN,
  3070. Expr: &ast.BinaryExpr{
  3071. OP: ast.AND,
  3072. LHS: &ast.BinaryExpr{
  3073. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  3074. OP: ast.EQ,
  3075. RHS: &ast.FieldRef{Name: "id", StreamName: "tableInPlanner"},
  3076. },
  3077. RHS: &ast.BinaryExpr{
  3078. RHS: &ast.BinaryExpr{
  3079. OP: ast.AND,
  3080. LHS: &ast.BinaryExpr{
  3081. OP: ast.GT,
  3082. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  3083. RHS: &ast.IntegerLiteral{Val: 20},
  3084. },
  3085. RHS: &ast.BinaryExpr{
  3086. OP: ast.LT,
  3087. LHS: &ast.FieldRef{Name: "hum", StreamName: "tableInPlanner"},
  3088. RHS: &ast.IntegerLiteral{Val: 60},
  3089. },
  3090. },
  3091. OP: ast.AND,
  3092. LHS: &ast.BinaryExpr{
  3093. OP: ast.GT,
  3094. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  3095. RHS: &ast.IntegerLiteral{Val: 111},
  3096. },
  3097. },
  3098. },
  3099. },
  3100. },
  3101. }.Init(),
  3102. },
  3103. },
  3104. fields: []ast.Field{
  3105. {
  3106. Expr: &ast.FieldRef{Name: "id1", StreamName: ast.DefaultStream},
  3107. Name: "id1",
  3108. AName: "",
  3109. },
  3110. },
  3111. isAggregate: false,
  3112. sendMeta: false,
  3113. }.Init(),
  3114. }, { // 10 meta
  3115. sql: `SELECT temp, meta(id) AS eid,meta(Humidity->Device) AS hdevice FROM src1 WHERE meta(device)="demo2"`,
  3116. p: ProjectPlan{
  3117. baseLogicalPlan: baseLogicalPlan{
  3118. children: []LogicalPlan{
  3119. FilterPlan{
  3120. baseLogicalPlan: baseLogicalPlan{
  3121. children: []LogicalPlan{
  3122. DataSourcePlan{
  3123. name: "src1",
  3124. streamFields: map[string]*ast.JsonStreamField{
  3125. "temp": nil,
  3126. },
  3127. isSchemaless: true,
  3128. streamStmt: streams["src1"],
  3129. metaFields: []string{"Humidity", "device", "id"},
  3130. pruneFields: []string{},
  3131. }.Init(),
  3132. },
  3133. },
  3134. condition: &ast.BinaryExpr{
  3135. LHS: &ast.Call{
  3136. Name: "meta",
  3137. FuncId: 2,
  3138. Args: []ast.Expr{&ast.MetaRef{
  3139. Name: "device",
  3140. StreamName: ast.DefaultStream,
  3141. }},
  3142. },
  3143. OP: ast.EQ,
  3144. RHS: &ast.StringLiteral{
  3145. Val: "demo2",
  3146. },
  3147. },
  3148. }.Init(),
  3149. },
  3150. },
  3151. fields: []ast.Field{
  3152. {
  3153. Expr: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  3154. Name: "temp",
  3155. AName: "",
  3156. }, {
  3157. Expr: &ast.FieldRef{Name: "eid", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  3158. &ast.Call{Name: "meta", FuncId: 0, Args: []ast.Expr{&ast.MetaRef{
  3159. Name: "id",
  3160. StreamName: ast.DefaultStream,
  3161. }}},
  3162. []ast.StreamName{},
  3163. nil,
  3164. )},
  3165. Name: "meta",
  3166. AName: "eid",
  3167. }, {
  3168. Expr: &ast.FieldRef{Name: "hdevice", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  3169. &ast.Call{Name: "meta", FuncId: 1, Args: []ast.Expr{
  3170. &ast.BinaryExpr{
  3171. OP: ast.ARROW,
  3172. LHS: &ast.MetaRef{Name: "Humidity", StreamName: ast.DefaultStream},
  3173. RHS: &ast.JsonFieldRef{Name: "Device"},
  3174. },
  3175. }},
  3176. []ast.StreamName{},
  3177. nil,
  3178. )},
  3179. Name: "meta",
  3180. AName: "hdevice",
  3181. },
  3182. },
  3183. isAggregate: false,
  3184. sendMeta: false,
  3185. }.Init(),
  3186. }, { // 11 join with same name field and aliased
  3187. sql: `SELECT src2.hum AS hum1, tableInPlanner.hum AS hum2 FROM src2 INNER JOIN tableInPlanner on id2 = id WHERE hum1 > hum2`,
  3188. p: ProjectPlan{
  3189. baseLogicalPlan: baseLogicalPlan{
  3190. children: []LogicalPlan{
  3191. JoinPlan{
  3192. baseLogicalPlan: baseLogicalPlan{
  3193. children: []LogicalPlan{
  3194. JoinAlignPlan{
  3195. baseLogicalPlan: baseLogicalPlan{
  3196. children: []LogicalPlan{
  3197. DataSourcePlan{
  3198. name: "src2",
  3199. streamFields: map[string]*ast.JsonStreamField{
  3200. "hum": nil,
  3201. "id": nil,
  3202. "id2": nil,
  3203. },
  3204. isSchemaless: true,
  3205. streamStmt: streams["src2"],
  3206. metaFields: []string{},
  3207. pruneFields: []string{},
  3208. }.Init(),
  3209. DataSourcePlan{
  3210. name: "tableInPlanner",
  3211. streamFields: map[string]*ast.JsonStreamField{
  3212. "hum": {
  3213. Type: "bigint",
  3214. },
  3215. "id": {
  3216. Type: "bigint",
  3217. },
  3218. },
  3219. streamStmt: streams["tableInPlanner"],
  3220. metaFields: []string{},
  3221. pruneFields: []string{},
  3222. }.Init(),
  3223. },
  3224. },
  3225. Emitters: []string{"tableInPlanner"},
  3226. }.Init(),
  3227. },
  3228. },
  3229. from: &ast.Table{
  3230. Name: "src2",
  3231. },
  3232. joins: []ast.Join{
  3233. {
  3234. Name: "tableInPlanner",
  3235. Alias: "",
  3236. JoinType: ast.INNER_JOIN,
  3237. Expr: &ast.BinaryExpr{
  3238. RHS: &ast.BinaryExpr{
  3239. OP: ast.EQ,
  3240. LHS: &ast.FieldRef{Name: "id2", StreamName: ast.DefaultStream},
  3241. RHS: &ast.FieldRef{Name: "id", StreamName: ast.DefaultStream},
  3242. },
  3243. OP: ast.AND,
  3244. LHS: &ast.BinaryExpr{
  3245. OP: ast.GT,
  3246. LHS: &ast.FieldRef{Name: "hum1", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  3247. &ast.FieldRef{
  3248. Name: "hum",
  3249. StreamName: "src2",
  3250. },
  3251. []ast.StreamName{"src2"},
  3252. &boolFalse,
  3253. )},
  3254. RHS: &ast.FieldRef{Name: "hum2", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  3255. &ast.FieldRef{
  3256. Name: "hum",
  3257. StreamName: "tableInPlanner",
  3258. },
  3259. []ast.StreamName{"tableInPlanner"},
  3260. &boolFalse,
  3261. )},
  3262. },
  3263. },
  3264. },
  3265. },
  3266. }.Init(),
  3267. },
  3268. },
  3269. fields: []ast.Field{
  3270. {
  3271. Expr: &ast.FieldRef{Name: "hum1", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  3272. &ast.FieldRef{
  3273. Name: "hum",
  3274. StreamName: "src2",
  3275. },
  3276. []ast.StreamName{"src2"},
  3277. &boolFalse,
  3278. )},
  3279. Name: "hum",
  3280. AName: "hum1",
  3281. },
  3282. {
  3283. Expr: &ast.FieldRef{Name: "hum2", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  3284. &ast.FieldRef{
  3285. Name: "hum",
  3286. StreamName: "tableInPlanner",
  3287. },
  3288. []ast.StreamName{"tableInPlanner"},
  3289. &boolFalse,
  3290. )},
  3291. Name: "hum",
  3292. AName: "hum2",
  3293. },
  3294. },
  3295. isAggregate: false,
  3296. sendMeta: false,
  3297. }.Init(),
  3298. }, { // 12
  3299. sql: `SELECT name->first, name->last FROM src1`,
  3300. p: ProjectPlan{
  3301. baseLogicalPlan: baseLogicalPlan{
  3302. children: []LogicalPlan{
  3303. DataSourcePlan{
  3304. baseLogicalPlan: baseLogicalPlan{},
  3305. name: "src1",
  3306. streamFields: map[string]*ast.JsonStreamField{
  3307. "name": nil,
  3308. },
  3309. isSchemaless: true,
  3310. streamStmt: streams["src1"],
  3311. metaFields: []string{},
  3312. pruneFields: []string{},
  3313. }.Init(),
  3314. },
  3315. },
  3316. fields: []ast.Field{
  3317. {
  3318. Expr: &ast.BinaryExpr{
  3319. OP: ast.ARROW,
  3320. LHS: &ast.FieldRef{StreamName: "src1", Name: "name"},
  3321. RHS: &ast.JsonFieldRef{Name: "first"},
  3322. },
  3323. Name: "kuiper_field_0",
  3324. AName: "",
  3325. }, {
  3326. Expr: &ast.BinaryExpr{
  3327. OP: ast.ARROW,
  3328. LHS: &ast.FieldRef{StreamName: "src1", Name: "name"},
  3329. RHS: &ast.JsonFieldRef{Name: "last"},
  3330. },
  3331. Name: "kuiper_field_1",
  3332. AName: "",
  3333. },
  3334. },
  3335. isAggregate: false,
  3336. sendMeta: false,
  3337. }.Init(),
  3338. }, { // 13
  3339. sql: `SELECT * EXCEPT(id1, name), meta(device) FROM src1`,
  3340. p: ProjectPlan{
  3341. baseLogicalPlan: baseLogicalPlan{
  3342. children: []LogicalPlan{
  3343. DataSourcePlan{
  3344. baseLogicalPlan: baseLogicalPlan{},
  3345. name: "src1",
  3346. streamFields: map[string]*ast.JsonStreamField{},
  3347. streamStmt: streams["src1"],
  3348. metaFields: []string{"device"},
  3349. isWildCard: false,
  3350. pruneFields: []string{"id1", "name"},
  3351. isSchemaless: true,
  3352. }.Init(),
  3353. },
  3354. },
  3355. fields: []ast.Field{
  3356. {
  3357. Name: "*",
  3358. Expr: &ast.Wildcard{
  3359. Token: ast.ASTERISK,
  3360. Except: []string{"id1", "name"},
  3361. },
  3362. },
  3363. {
  3364. Name: "meta",
  3365. Expr: &ast.Call{
  3366. Name: "meta",
  3367. Args: []ast.Expr{
  3368. &ast.MetaRef{
  3369. StreamName: ast.DefaultStream,
  3370. Name: "device",
  3371. },
  3372. },
  3373. },
  3374. },
  3375. },
  3376. isAggregate: false,
  3377. allWildcard: true,
  3378. sendMeta: false,
  3379. }.Init(),
  3380. }, { // 14
  3381. sql: `SELECT * REPLACE(temp * 2 AS id1, myarray * 2 AS name), meta(device) FROM src1`,
  3382. p: ProjectPlan{
  3383. baseLogicalPlan: baseLogicalPlan{
  3384. children: []LogicalPlan{
  3385. DataSourcePlan{
  3386. baseLogicalPlan: baseLogicalPlan{},
  3387. name: "src1",
  3388. streamFields: map[string]*ast.JsonStreamField{},
  3389. streamStmt: streams["src1"],
  3390. metaFields: []string{"device"},
  3391. isWildCard: false,
  3392. pruneFields: []string{"id1", "name"},
  3393. isSchemaless: true,
  3394. }.Init(),
  3395. },
  3396. },
  3397. fields: []ast.Field{
  3398. {
  3399. Name: "*",
  3400. Expr: &ast.Wildcard{
  3401. Token: ast.ASTERISK,
  3402. Replace: []ast.Field{
  3403. {
  3404. AName: "id1",
  3405. Expr: &ast.BinaryExpr{
  3406. OP: ast.MUL,
  3407. LHS: &ast.FieldRef{
  3408. Name: "temp",
  3409. StreamName: "src1",
  3410. },
  3411. RHS: &ast.IntegerLiteral{Val: 2},
  3412. },
  3413. },
  3414. {
  3415. AName: "name",
  3416. Expr: &ast.BinaryExpr{
  3417. OP: ast.MUL,
  3418. LHS: &ast.FieldRef{
  3419. Name: "myarray",
  3420. StreamName: "src1",
  3421. },
  3422. RHS: &ast.IntegerLiteral{Val: 2},
  3423. },
  3424. },
  3425. },
  3426. },
  3427. },
  3428. {
  3429. Name: "meta",
  3430. Expr: &ast.Call{
  3431. Name: "meta",
  3432. Args: []ast.Expr{
  3433. &ast.MetaRef{
  3434. StreamName: ast.DefaultStream,
  3435. Name: "device",
  3436. },
  3437. },
  3438. },
  3439. },
  3440. },
  3441. isAggregate: false,
  3442. allWildcard: true,
  3443. sendMeta: false,
  3444. }.Init(),
  3445. }, { // 15
  3446. sql: `SELECT collect( * EXCEPT(id1, name)) FROM src1 GROUP BY TUMBLINGWINDOW(ss, 10)`,
  3447. p: ProjectPlan{
  3448. baseLogicalPlan: baseLogicalPlan{
  3449. children: []LogicalPlan{
  3450. WindowPlan{
  3451. baseLogicalPlan: baseLogicalPlan{
  3452. children: []LogicalPlan{
  3453. DataSourcePlan{
  3454. baseLogicalPlan: baseLogicalPlan{},
  3455. name: "src1",
  3456. streamFields: map[string]*ast.JsonStreamField{},
  3457. streamStmt: streams["src1"],
  3458. metaFields: []string{},
  3459. isWildCard: false,
  3460. pruneFields: []string{"id1", "name"},
  3461. isSchemaless: true,
  3462. }.Init(),
  3463. },
  3464. },
  3465. condition: nil,
  3466. wtype: ast.TUMBLING_WINDOW,
  3467. length: 10,
  3468. timeUnit: ast.SS,
  3469. interval: 0,
  3470. limit: 0,
  3471. }.Init(),
  3472. },
  3473. },
  3474. fields: []ast.Field{
  3475. {
  3476. Name: "collect",
  3477. Expr: &ast.Call{
  3478. Name: "collect",
  3479. FuncType: ast.FuncTypeAgg,
  3480. Args: []ast.Expr{
  3481. &ast.Wildcard{
  3482. Token: ast.ASTERISK,
  3483. Except: []string{"id1", "name"},
  3484. },
  3485. },
  3486. },
  3487. },
  3488. },
  3489. isAggregate: true,
  3490. allWildcard: false,
  3491. sendMeta: false,
  3492. }.Init(),
  3493. }, { // 16
  3494. sql: `SELECT collect( * REPLACE(temp * 2 AS id1, myarray * 2 AS name)) FROM src1 GROUP BY TUMBLINGWINDOW(ss, 10)`,
  3495. p: ProjectPlan{
  3496. baseLogicalPlan: baseLogicalPlan{
  3497. children: []LogicalPlan{
  3498. WindowPlan{
  3499. baseLogicalPlan: baseLogicalPlan{
  3500. children: []LogicalPlan{
  3501. DataSourcePlan{
  3502. baseLogicalPlan: baseLogicalPlan{},
  3503. name: "src1",
  3504. streamFields: map[string]*ast.JsonStreamField{},
  3505. streamStmt: streams["src1"],
  3506. metaFields: []string{},
  3507. isWildCard: false,
  3508. pruneFields: []string{"id1", "name"},
  3509. isSchemaless: true,
  3510. }.Init(),
  3511. },
  3512. },
  3513. condition: nil,
  3514. wtype: ast.TUMBLING_WINDOW,
  3515. length: 10,
  3516. timeUnit: ast.SS,
  3517. interval: 0,
  3518. limit: 0,
  3519. }.Init(),
  3520. },
  3521. },
  3522. fields: []ast.Field{
  3523. {
  3524. Name: "collect",
  3525. Expr: &ast.Call{
  3526. Name: "collect",
  3527. FuncType: ast.FuncTypeAgg,
  3528. Args: []ast.Expr{
  3529. &ast.Wildcard{
  3530. Token: ast.ASTERISK,
  3531. Replace: []ast.Field{
  3532. {
  3533. AName: "id1",
  3534. Expr: &ast.BinaryExpr{
  3535. OP: ast.MUL,
  3536. LHS: &ast.FieldRef{
  3537. Name: "temp",
  3538. StreamName: "src1",
  3539. },
  3540. RHS: &ast.IntegerLiteral{Val: 2},
  3541. },
  3542. },
  3543. {
  3544. AName: "name",
  3545. Expr: &ast.BinaryExpr{
  3546. OP: ast.MUL,
  3547. LHS: &ast.FieldRef{
  3548. Name: "myarray",
  3549. StreamName: "src1",
  3550. },
  3551. RHS: &ast.IntegerLiteral{Val: 2},
  3552. },
  3553. },
  3554. },
  3555. },
  3556. },
  3557. },
  3558. },
  3559. },
  3560. isAggregate: true,
  3561. allWildcard: false,
  3562. sendMeta: false,
  3563. }.Init(),
  3564. }, { // 17
  3565. sql: `SELECT id1 FROM src1 GROUP BY TUMBLINGWINDOW(ss, 10) HAVING count(* EXCEPT(id1, name)) > 0`,
  3566. p: ProjectPlan{
  3567. baseLogicalPlan: baseLogicalPlan{
  3568. children: []LogicalPlan{
  3569. HavingPlan{
  3570. baseLogicalPlan: baseLogicalPlan{
  3571. children: []LogicalPlan{
  3572. WindowPlan{
  3573. baseLogicalPlan: baseLogicalPlan{
  3574. children: []LogicalPlan{
  3575. DataSourcePlan{
  3576. baseLogicalPlan: baseLogicalPlan{},
  3577. name: "src1",
  3578. streamFields: map[string]*ast.JsonStreamField{},
  3579. streamStmt: streams["src1"],
  3580. metaFields: []string{},
  3581. isWildCard: false,
  3582. pruneFields: []string{"id1", "name"},
  3583. isSchemaless: true,
  3584. }.Init(),
  3585. },
  3586. },
  3587. condition: nil,
  3588. wtype: ast.TUMBLING_WINDOW,
  3589. length: 10,
  3590. timeUnit: ast.SS,
  3591. interval: 0,
  3592. limit: 0,
  3593. }.Init(),
  3594. },
  3595. },
  3596. condition: &ast.BinaryExpr{
  3597. LHS: &ast.Call{
  3598. Name: "count",
  3599. FuncId: 0,
  3600. Args: []ast.Expr{
  3601. &ast.Wildcard{
  3602. Token: ast.ASTERISK,
  3603. Except: []string{"id1", "name"},
  3604. },
  3605. },
  3606. FuncType: ast.FuncTypeAgg,
  3607. },
  3608. OP: ast.GT,
  3609. RHS: &ast.IntegerLiteral{Val: 0},
  3610. },
  3611. }.Init(),
  3612. },
  3613. },
  3614. fields: []ast.Field{
  3615. {
  3616. Name: "id1",
  3617. Expr: &ast.FieldRef{
  3618. Name: "id1",
  3619. StreamName: "src1",
  3620. },
  3621. },
  3622. },
  3623. isAggregate: false,
  3624. allWildcard: false,
  3625. sendMeta: false,
  3626. }.Init(),
  3627. }, { // 18
  3628. sql: `SELECT temp FROM src1 GROUP BY TUMBLINGWINDOW(ss, 10) HAVING count(* REPLACE(temp * 2 AS id1, myarray * 2 AS name)) > 0`,
  3629. p: ProjectPlan{
  3630. baseLogicalPlan: baseLogicalPlan{
  3631. children: []LogicalPlan{
  3632. HavingPlan{
  3633. baseLogicalPlan: baseLogicalPlan{
  3634. children: []LogicalPlan{
  3635. WindowPlan{
  3636. baseLogicalPlan: baseLogicalPlan{
  3637. children: []LogicalPlan{
  3638. DataSourcePlan{
  3639. baseLogicalPlan: baseLogicalPlan{},
  3640. name: "src1",
  3641. streamFields: map[string]*ast.JsonStreamField{},
  3642. streamStmt: streams["src1"],
  3643. metaFields: []string{},
  3644. isWildCard: false,
  3645. pruneFields: []string{"id1", "name"},
  3646. isSchemaless: true,
  3647. }.Init(),
  3648. },
  3649. },
  3650. condition: nil,
  3651. wtype: ast.TUMBLING_WINDOW,
  3652. length: 10,
  3653. timeUnit: ast.SS,
  3654. interval: 0,
  3655. limit: 0,
  3656. }.Init(),
  3657. },
  3658. },
  3659. condition: &ast.BinaryExpr{
  3660. LHS: &ast.Call{
  3661. Name: "count",
  3662. FuncId: 0,
  3663. Args: []ast.Expr{
  3664. &ast.Wildcard{
  3665. Token: ast.ASTERISK,
  3666. Replace: []ast.Field{
  3667. {
  3668. AName: "id1",
  3669. Expr: &ast.BinaryExpr{
  3670. OP: ast.MUL,
  3671. LHS: &ast.FieldRef{
  3672. Name: "temp",
  3673. StreamName: "src1",
  3674. },
  3675. RHS: &ast.IntegerLiteral{Val: 2},
  3676. },
  3677. },
  3678. {
  3679. AName: "name",
  3680. Expr: &ast.BinaryExpr{
  3681. OP: ast.MUL,
  3682. LHS: &ast.FieldRef{
  3683. Name: "myarray",
  3684. StreamName: "src1",
  3685. },
  3686. RHS: &ast.IntegerLiteral{Val: 2},
  3687. },
  3688. },
  3689. },
  3690. },
  3691. },
  3692. FuncType: ast.FuncTypeAgg,
  3693. },
  3694. OP: ast.GT,
  3695. RHS: &ast.IntegerLiteral{Val: 0},
  3696. },
  3697. }.Init(),
  3698. },
  3699. },
  3700. fields: []ast.Field{
  3701. {
  3702. Name: "temp",
  3703. Expr: &ast.FieldRef{
  3704. Name: "temp",
  3705. StreamName: "src1",
  3706. },
  3707. },
  3708. },
  3709. isAggregate: false,
  3710. allWildcard: false,
  3711. sendMeta: false,
  3712. }.Init(),
  3713. },
  3714. }
  3715. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  3716. for i, tt := range tests {
  3717. stmt, err := xsql.NewParser(strings.NewReader(tt.sql)).Parse()
  3718. if err != nil {
  3719. t.Errorf("%d. %q: error compile sql: %s\n", i, tt.sql, err)
  3720. continue
  3721. }
  3722. p, err := createLogicalPlan(stmt, &api.RuleOption{
  3723. IsEventTime: false,
  3724. LateTol: 0,
  3725. Concurrency: 0,
  3726. BufferLength: 0,
  3727. SendMetaToSink: false,
  3728. Qos: 0,
  3729. CheckpointInterval: 0,
  3730. SendError: true,
  3731. }, kv)
  3732. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  3733. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.sql, tt.err, err)
  3734. } else if !reflect.DeepEqual(tt.p, p) {
  3735. 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))
  3736. }
  3737. }
  3738. }
  3739. func Test_createLogicalPlan4Lookup(t *testing.T) {
  3740. kv, err := store.GetKV("stream")
  3741. if err != nil {
  3742. t.Error(err)
  3743. return
  3744. }
  3745. streamSqls := map[string]string{
  3746. "src1": `CREATE STREAM src1 () WITH (DATASOURCE="src1", FORMAT="json", KEY="ts");`,
  3747. "table1": `CREATE TABLE table1 () WITH (DATASOURCE="table1",TYPE="sql", KIND="lookup");`,
  3748. "table2": `CREATE TABLE table2 () WITH (DATASOURCE="table2",TYPE="sql", KIND="lookup");`,
  3749. }
  3750. types := map[string]ast.StreamType{
  3751. "src1": ast.TypeStream,
  3752. "table1": ast.TypeTable,
  3753. "table2": ast.TypeTable,
  3754. }
  3755. for name, sql := range streamSqls {
  3756. s, err := json.Marshal(&xsql.StreamInfo{
  3757. StreamType: types[name],
  3758. Statement: sql,
  3759. })
  3760. if err != nil {
  3761. t.Error(err)
  3762. t.Fail()
  3763. }
  3764. err = kv.Set(name, string(s))
  3765. if err != nil {
  3766. t.Error(err)
  3767. t.Fail()
  3768. }
  3769. }
  3770. streams := make(map[string]*ast.StreamStmt)
  3771. for n := range streamSqls {
  3772. streamStmt, err := xsql.GetDataSource(kv, n)
  3773. if err != nil {
  3774. t.Errorf("fail to get stream %s, please check if stream is created", n)
  3775. return
  3776. }
  3777. streams[n] = streamStmt
  3778. }
  3779. tests := []struct {
  3780. sql string
  3781. p LogicalPlan
  3782. err string
  3783. }{
  3784. { // 0
  3785. sql: `SELECT src1.a, table1.b FROM src1 INNER JOIN table1 ON src1.id = table1.id`,
  3786. p: ProjectPlan{
  3787. baseLogicalPlan: baseLogicalPlan{
  3788. children: []LogicalPlan{
  3789. LookupPlan{
  3790. baseLogicalPlan: baseLogicalPlan{
  3791. children: []LogicalPlan{
  3792. DataSourcePlan{
  3793. baseLogicalPlan: baseLogicalPlan{},
  3794. name: "src1",
  3795. streamFields: map[string]*ast.JsonStreamField{
  3796. "a": nil,
  3797. },
  3798. isSchemaless: true,
  3799. streamStmt: streams["src1"],
  3800. metaFields: []string{},
  3801. pruneFields: []string{},
  3802. }.Init(),
  3803. },
  3804. },
  3805. joinExpr: ast.Join{
  3806. Name: "table1",
  3807. Alias: "",
  3808. JoinType: ast.INNER_JOIN,
  3809. Expr: &ast.BinaryExpr{
  3810. OP: ast.EQ,
  3811. LHS: &ast.FieldRef{
  3812. StreamName: "src1",
  3813. Name: "id",
  3814. },
  3815. RHS: &ast.FieldRef{
  3816. StreamName: "table1",
  3817. Name: "id",
  3818. },
  3819. },
  3820. },
  3821. keys: []string{"id"},
  3822. fields: []string{"b"},
  3823. valvars: []ast.Expr{
  3824. &ast.FieldRef{
  3825. StreamName: "src1",
  3826. Name: "id",
  3827. },
  3828. },
  3829. options: &ast.Options{
  3830. DATASOURCE: "table1",
  3831. TYPE: "sql",
  3832. KIND: "lookup",
  3833. },
  3834. conditions: nil,
  3835. }.Init(),
  3836. },
  3837. },
  3838. fields: []ast.Field{
  3839. {
  3840. Expr: &ast.FieldRef{
  3841. StreamName: "src1",
  3842. Name: "a",
  3843. },
  3844. Name: "a",
  3845. AName: "",
  3846. },
  3847. {
  3848. Expr: &ast.FieldRef{
  3849. StreamName: "table1",
  3850. Name: "b",
  3851. },
  3852. Name: "b",
  3853. AName: "",
  3854. },
  3855. },
  3856. isAggregate: false,
  3857. sendMeta: false,
  3858. }.Init(),
  3859. },
  3860. { // 1
  3861. sql: `SELECT src1.a, table1.* FROM src1 INNER JOIN table1 ON table1.b > 20 AND src1.c < 40 AND src1.id = table1.id`,
  3862. p: ProjectPlan{
  3863. baseLogicalPlan: baseLogicalPlan{
  3864. children: []LogicalPlan{
  3865. FilterPlan{
  3866. baseLogicalPlan: baseLogicalPlan{
  3867. children: []LogicalPlan{
  3868. LookupPlan{
  3869. baseLogicalPlan: baseLogicalPlan{
  3870. children: []LogicalPlan{
  3871. FilterPlan{
  3872. baseLogicalPlan: baseLogicalPlan{
  3873. children: []LogicalPlan{
  3874. DataSourcePlan{
  3875. baseLogicalPlan: baseLogicalPlan{},
  3876. name: "src1",
  3877. streamFields: map[string]*ast.JsonStreamField{
  3878. "a": nil,
  3879. },
  3880. isSchemaless: true,
  3881. streamStmt: streams["src1"],
  3882. metaFields: []string{},
  3883. pruneFields: []string{},
  3884. }.Init(),
  3885. },
  3886. },
  3887. condition: &ast.BinaryExpr{
  3888. OP: ast.LT,
  3889. LHS: &ast.FieldRef{
  3890. StreamName: "src1",
  3891. Name: "c",
  3892. },
  3893. RHS: &ast.IntegerLiteral{Val: 40},
  3894. },
  3895. }.Init(),
  3896. },
  3897. },
  3898. joinExpr: ast.Join{
  3899. Name: "table1",
  3900. Alias: "",
  3901. JoinType: ast.INNER_JOIN,
  3902. Expr: &ast.BinaryExpr{
  3903. OP: ast.AND,
  3904. RHS: &ast.BinaryExpr{
  3905. OP: ast.EQ,
  3906. LHS: &ast.FieldRef{
  3907. StreamName: "src1",
  3908. Name: "id",
  3909. },
  3910. RHS: &ast.FieldRef{
  3911. StreamName: "table1",
  3912. Name: "id",
  3913. },
  3914. },
  3915. LHS: &ast.BinaryExpr{
  3916. OP: ast.AND,
  3917. LHS: &ast.BinaryExpr{
  3918. OP: ast.GT,
  3919. LHS: &ast.FieldRef{
  3920. StreamName: "table1",
  3921. Name: "b",
  3922. },
  3923. RHS: &ast.IntegerLiteral{Val: 20},
  3924. },
  3925. RHS: &ast.BinaryExpr{
  3926. OP: ast.LT,
  3927. LHS: &ast.FieldRef{
  3928. StreamName: "src1",
  3929. Name: "c",
  3930. },
  3931. RHS: &ast.IntegerLiteral{Val: 40},
  3932. },
  3933. },
  3934. },
  3935. },
  3936. keys: []string{"id"},
  3937. valvars: []ast.Expr{
  3938. &ast.FieldRef{
  3939. StreamName: "src1",
  3940. Name: "id",
  3941. },
  3942. },
  3943. options: &ast.Options{
  3944. DATASOURCE: "table1",
  3945. TYPE: "sql",
  3946. KIND: "lookup",
  3947. },
  3948. conditions: &ast.BinaryExpr{
  3949. OP: ast.AND,
  3950. LHS: &ast.BinaryExpr{
  3951. OP: ast.GT,
  3952. LHS: &ast.FieldRef{
  3953. StreamName: "table1",
  3954. Name: "b",
  3955. },
  3956. RHS: &ast.IntegerLiteral{Val: 20},
  3957. },
  3958. RHS: &ast.BinaryExpr{
  3959. OP: ast.LT,
  3960. LHS: &ast.FieldRef{
  3961. StreamName: "src1",
  3962. Name: "c",
  3963. },
  3964. RHS: &ast.IntegerLiteral{Val: 40},
  3965. },
  3966. },
  3967. }.Init(),
  3968. },
  3969. },
  3970. condition: &ast.BinaryExpr{
  3971. OP: ast.GT,
  3972. LHS: &ast.FieldRef{
  3973. StreamName: "table1",
  3974. Name: "b",
  3975. },
  3976. RHS: &ast.IntegerLiteral{Val: 20},
  3977. },
  3978. }.Init(),
  3979. },
  3980. },
  3981. fields: []ast.Field{
  3982. {
  3983. Expr: &ast.FieldRef{
  3984. StreamName: "src1",
  3985. Name: "a",
  3986. },
  3987. Name: "a",
  3988. AName: "",
  3989. },
  3990. {
  3991. Expr: &ast.FieldRef{
  3992. StreamName: "table1",
  3993. Name: "*",
  3994. },
  3995. Name: "*",
  3996. AName: "",
  3997. },
  3998. },
  3999. isAggregate: false,
  4000. sendMeta: false,
  4001. }.Init(),
  4002. },
  4003. { // 2
  4004. sql: `SELECT src1.a, table1.b, table2.c FROM src1 INNER JOIN table1 ON src1.id = table1.id INNER JOIN table2 on table1.id = table2.id`,
  4005. p: ProjectPlan{
  4006. baseLogicalPlan: baseLogicalPlan{
  4007. children: []LogicalPlan{
  4008. LookupPlan{
  4009. baseLogicalPlan: baseLogicalPlan{
  4010. children: []LogicalPlan{
  4011. LookupPlan{
  4012. baseLogicalPlan: baseLogicalPlan{
  4013. children: []LogicalPlan{
  4014. DataSourcePlan{
  4015. baseLogicalPlan: baseLogicalPlan{},
  4016. name: "src1",
  4017. streamFields: map[string]*ast.JsonStreamField{
  4018. "a": nil,
  4019. },
  4020. isSchemaless: true,
  4021. streamStmt: streams["src1"],
  4022. metaFields: []string{},
  4023. pruneFields: []string{},
  4024. }.Init(),
  4025. },
  4026. },
  4027. joinExpr: ast.Join{
  4028. Name: "table1",
  4029. Alias: "",
  4030. JoinType: ast.INNER_JOIN,
  4031. Expr: &ast.BinaryExpr{
  4032. OP: ast.EQ,
  4033. LHS: &ast.FieldRef{
  4034. StreamName: "src1",
  4035. Name: "id",
  4036. },
  4037. RHS: &ast.FieldRef{
  4038. StreamName: "table1",
  4039. Name: "id",
  4040. },
  4041. },
  4042. },
  4043. keys: []string{"id"},
  4044. fields: []string{"b"},
  4045. valvars: []ast.Expr{
  4046. &ast.FieldRef{
  4047. StreamName: "src1",
  4048. Name: "id",
  4049. },
  4050. },
  4051. options: &ast.Options{
  4052. DATASOURCE: "table1",
  4053. TYPE: "sql",
  4054. KIND: "lookup",
  4055. },
  4056. conditions: nil,
  4057. }.Init(),
  4058. },
  4059. },
  4060. joinExpr: ast.Join{
  4061. Name: "table2",
  4062. Alias: "",
  4063. JoinType: ast.INNER_JOIN,
  4064. Expr: &ast.BinaryExpr{
  4065. OP: ast.EQ,
  4066. LHS: &ast.FieldRef{
  4067. StreamName: "table1",
  4068. Name: "id",
  4069. },
  4070. RHS: &ast.FieldRef{
  4071. StreamName: "table2",
  4072. Name: "id",
  4073. },
  4074. },
  4075. },
  4076. keys: []string{"id"},
  4077. fields: []string{"c"},
  4078. valvars: []ast.Expr{
  4079. &ast.FieldRef{
  4080. StreamName: "table1",
  4081. Name: "id",
  4082. },
  4083. },
  4084. options: &ast.Options{
  4085. DATASOURCE: "table2",
  4086. TYPE: "sql",
  4087. KIND: "lookup",
  4088. },
  4089. }.Init(),
  4090. },
  4091. },
  4092. fields: []ast.Field{
  4093. {
  4094. Expr: &ast.FieldRef{
  4095. StreamName: "src1",
  4096. Name: "a",
  4097. },
  4098. Name: "a",
  4099. AName: "",
  4100. },
  4101. {
  4102. Expr: &ast.FieldRef{
  4103. StreamName: "table1",
  4104. Name: "b",
  4105. },
  4106. Name: "b",
  4107. AName: "",
  4108. },
  4109. {
  4110. Expr: &ast.FieldRef{
  4111. StreamName: "table2",
  4112. Name: "c",
  4113. },
  4114. Name: "c",
  4115. AName: "",
  4116. },
  4117. },
  4118. isAggregate: false,
  4119. sendMeta: false,
  4120. }.Init(),
  4121. },
  4122. { // 3
  4123. sql: `SELECT * FROM src1 INNER JOIN table1 ON src1.id = table1.id GROUP BY TUMBLINGWINDOW(ss, 10)`,
  4124. p: ProjectPlan{
  4125. baseLogicalPlan: baseLogicalPlan{
  4126. children: []LogicalPlan{
  4127. LookupPlan{
  4128. baseLogicalPlan: baseLogicalPlan{
  4129. children: []LogicalPlan{
  4130. WindowPlan{
  4131. baseLogicalPlan: baseLogicalPlan{
  4132. children: []LogicalPlan{
  4133. DataSourcePlan{
  4134. baseLogicalPlan: baseLogicalPlan{},
  4135. name: "src1",
  4136. streamStmt: streams["src1"],
  4137. streamFields: map[string]*ast.JsonStreamField{},
  4138. metaFields: []string{},
  4139. isWildCard: true,
  4140. isSchemaless: true,
  4141. pruneFields: []string{},
  4142. }.Init(),
  4143. },
  4144. },
  4145. condition: nil,
  4146. wtype: ast.TUMBLING_WINDOW,
  4147. length: 10,
  4148. timeUnit: ast.SS,
  4149. interval: 0,
  4150. limit: 0,
  4151. }.Init(),
  4152. },
  4153. },
  4154. joinExpr: ast.Join{
  4155. Name: "table1",
  4156. Alias: "",
  4157. JoinType: ast.INNER_JOIN,
  4158. Expr: &ast.BinaryExpr{
  4159. OP: ast.EQ,
  4160. LHS: &ast.FieldRef{
  4161. StreamName: "src1",
  4162. Name: "id",
  4163. },
  4164. RHS: &ast.FieldRef{
  4165. StreamName: "table1",
  4166. Name: "id",
  4167. },
  4168. },
  4169. },
  4170. keys: []string{"id"},
  4171. valvars: []ast.Expr{
  4172. &ast.FieldRef{
  4173. StreamName: "src1",
  4174. Name: "id",
  4175. },
  4176. },
  4177. options: &ast.Options{
  4178. DATASOURCE: "table1",
  4179. TYPE: "sql",
  4180. KIND: "lookup",
  4181. },
  4182. conditions: nil,
  4183. }.Init(),
  4184. },
  4185. },
  4186. fields: []ast.Field{
  4187. {
  4188. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  4189. Name: "*",
  4190. AName: "",
  4191. },
  4192. },
  4193. isAggregate: false,
  4194. sendMeta: false,
  4195. }.Init(),
  4196. },
  4197. }
  4198. for i, tt := range tests {
  4199. stmt, err := xsql.NewParser(strings.NewReader(tt.sql)).Parse()
  4200. if err != nil {
  4201. t.Errorf("%d. %q: error compile sql: %s\n", i, tt.sql, err)
  4202. continue
  4203. }
  4204. p, err := createLogicalPlan(stmt, &api.RuleOption{
  4205. IsEventTime: false,
  4206. LateTol: 0,
  4207. Concurrency: 0,
  4208. BufferLength: 0,
  4209. SendMetaToSink: false,
  4210. Qos: 0,
  4211. CheckpointInterval: 0,
  4212. SendError: true,
  4213. }, kv)
  4214. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  4215. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.sql, tt.err, err)
  4216. } else if !reflect.DeepEqual(tt.p, p) {
  4217. 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))
  4218. }
  4219. }
  4220. }
  4221. func TestTransformSourceNode(t *testing.T) {
  4222. schema := map[string]*ast.JsonStreamField{
  4223. "a": {
  4224. Type: "bigint",
  4225. },
  4226. }
  4227. testCases := []struct {
  4228. name string
  4229. plan *DataSourcePlan
  4230. node *node.SourceNode
  4231. }{
  4232. {
  4233. name: "normal source node",
  4234. plan: &DataSourcePlan{
  4235. name: "test",
  4236. streamStmt: &ast.StreamStmt{
  4237. StreamType: ast.TypeStream,
  4238. Options: &ast.Options{
  4239. TYPE: "file",
  4240. },
  4241. },
  4242. streamFields: nil,
  4243. allMeta: false,
  4244. metaFields: []string{},
  4245. iet: false,
  4246. isBinary: false,
  4247. },
  4248. node: node.NewSourceNode("test", ast.TypeStream, nil, &ast.Options{
  4249. TYPE: "file",
  4250. }, false, nil),
  4251. },
  4252. {
  4253. name: "schema source node",
  4254. plan: &DataSourcePlan{
  4255. name: "test",
  4256. streamStmt: &ast.StreamStmt{
  4257. StreamType: ast.TypeStream,
  4258. Options: &ast.Options{
  4259. TYPE: "file",
  4260. },
  4261. },
  4262. streamFields: schema,
  4263. allMeta: false,
  4264. metaFields: []string{},
  4265. iet: false,
  4266. isBinary: false,
  4267. },
  4268. node: node.NewSourceNode("test", ast.TypeStream, nil, &ast.Options{
  4269. TYPE: "file",
  4270. }, false, schema),
  4271. },
  4272. }
  4273. for _, tc := range testCases {
  4274. t.Run(tc.name, func(t *testing.T) {
  4275. sourceNode, err := transformSourceNode(tc.plan, nil, &api.RuleOption{})
  4276. if err != nil {
  4277. t.Errorf("unexpected error: %v", err)
  4278. return
  4279. }
  4280. if !reflect.DeepEqual(sourceNode, tc.node) {
  4281. t.Errorf("unexpected result: got %v, want %v", sourceNode, tc.node)
  4282. }
  4283. })
  4284. }
  4285. }