planner_test.go 85 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978
  1. // Copyright 2022 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. "github.com/gdexlab/go-render/render"
  19. "github.com/lf-edge/ekuiper/internal/pkg/store"
  20. "github.com/lf-edge/ekuiper/internal/testx"
  21. "github.com/lf-edge/ekuiper/internal/xsql"
  22. "github.com/lf-edge/ekuiper/pkg/api"
  23. "github.com/lf-edge/ekuiper/pkg/ast"
  24. "reflect"
  25. "strings"
  26. "testing"
  27. )
  28. func init() {
  29. testx.InitEnv()
  30. }
  31. func Test_createLogicalPlan(t *testing.T) {
  32. err, store := store.GetKV("stream")
  33. if err != nil {
  34. t.Error(err)
  35. return
  36. }
  37. streamSqls := map[string]string{
  38. "src1": `CREATE STREAM src1 (
  39. id1 BIGINT,
  40. temp BIGINT,
  41. name string,
  42. myarray array(string)
  43. ) WITH (DATASOURCE="src1", FORMAT="json", KEY="ts");`,
  44. "src2": `CREATE STREAM src2 (
  45. id2 BIGINT,
  46. hum BIGINT
  47. ) WITH (DATASOURCE="src2", FORMAT="json", KEY="ts", TIMESTAMP_FORMAT="YYYY-MM-dd HH:mm:ss");`,
  48. "tableInPlanner": `CREATE TABLE tableInPlanner (
  49. id BIGINT,
  50. name STRING,
  51. value STRING,
  52. hum BIGINT
  53. ) WITH (TYPE="file");`,
  54. }
  55. types := map[string]ast.StreamType{
  56. "src1": ast.TypeStream,
  57. "src2": ast.TypeStream,
  58. "tableInPlanner": ast.TypeTable,
  59. }
  60. for name, sql := range streamSqls {
  61. s, err := json.Marshal(&xsql.StreamInfo{
  62. StreamType: types[name],
  63. Statement: sql,
  64. })
  65. if err != nil {
  66. t.Error(err)
  67. t.Fail()
  68. }
  69. err = store.Set(name, string(s))
  70. if err != nil {
  71. t.Error(err)
  72. t.Fail()
  73. }
  74. }
  75. streams := make(map[string]*ast.StreamStmt)
  76. for n := range streamSqls {
  77. streamStmt, err := xsql.GetDataSource(store, n)
  78. if err != nil {
  79. t.Errorf("fail to get stream %s, please check if stream is created", n)
  80. return
  81. }
  82. streams[n] = streamStmt
  83. }
  84. var (
  85. //boolTrue = true
  86. boolFalse = false
  87. )
  88. var tests = []struct {
  89. sql string
  90. p LogicalPlan
  91. err string
  92. }{
  93. { // 0
  94. sql: `SELECT myarray[temp] FROM src1`,
  95. p: ProjectPlan{
  96. baseLogicalPlan: baseLogicalPlan{
  97. children: []LogicalPlan{
  98. DataSourcePlan{
  99. baseLogicalPlan: baseLogicalPlan{},
  100. name: "src1",
  101. streamFields: []interface{}{
  102. &ast.StreamField{
  103. Name: "myarray",
  104. FieldType: &ast.ArrayType{Type: ast.STRINGS},
  105. },
  106. &ast.StreamField{
  107. Name: "temp",
  108. FieldType: &ast.BasicType{Type: ast.BIGINT},
  109. },
  110. },
  111. streamStmt: streams["src1"],
  112. metaFields: []string{},
  113. }.Init(),
  114. },
  115. },
  116. fields: []ast.Field{
  117. {
  118. Expr: &ast.BinaryExpr{
  119. OP: ast.SUBSET,
  120. LHS: &ast.FieldRef{
  121. StreamName: "src1",
  122. Name: "myarray",
  123. },
  124. RHS: &ast.IndexExpr{Index: &ast.FieldRef{
  125. StreamName: "src1",
  126. Name: "temp",
  127. }},
  128. },
  129. Name: "kuiper_field_0",
  130. AName: "",
  131. },
  132. },
  133. isAggregate: false,
  134. sendMeta: false,
  135. }.Init(),
  136. }, { // 1 optimize where to data source
  137. sql: `SELECT temp FROM src1 WHERE name = "v1" GROUP BY TUMBLINGWINDOW(ss, 10)`,
  138. p: ProjectPlan{
  139. baseLogicalPlan: baseLogicalPlan{
  140. children: []LogicalPlan{
  141. WindowPlan{
  142. baseLogicalPlan: baseLogicalPlan{
  143. children: []LogicalPlan{
  144. FilterPlan{
  145. baseLogicalPlan: baseLogicalPlan{
  146. children: []LogicalPlan{
  147. DataSourcePlan{
  148. name: "src1",
  149. streamFields: []interface{}{
  150. &ast.StreamField{
  151. Name: "name",
  152. FieldType: &ast.BasicType{Type: ast.STRINGS},
  153. },
  154. &ast.StreamField{
  155. Name: "temp",
  156. FieldType: &ast.BasicType{Type: ast.BIGINT},
  157. },
  158. },
  159. streamStmt: streams["src1"],
  160. metaFields: []string{},
  161. }.Init(),
  162. },
  163. },
  164. condition: &ast.BinaryExpr{
  165. LHS: &ast.FieldRef{Name: "name", StreamName: "src1"},
  166. OP: ast.EQ,
  167. RHS: &ast.StringLiteral{Val: "v1"},
  168. },
  169. }.Init(),
  170. },
  171. },
  172. condition: nil,
  173. wtype: ast.TUMBLING_WINDOW,
  174. length: 10000,
  175. interval: 0,
  176. limit: 0,
  177. }.Init(),
  178. },
  179. },
  180. fields: []ast.Field{
  181. {
  182. Expr: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  183. Name: "temp",
  184. AName: ""},
  185. },
  186. isAggregate: false,
  187. sendMeta: false,
  188. }.Init(),
  189. }, { // 2 condition that cannot be optimized
  190. 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)`,
  191. p: ProjectPlan{
  192. baseLogicalPlan: baseLogicalPlan{
  193. children: []LogicalPlan{
  194. JoinPlan{
  195. baseLogicalPlan: baseLogicalPlan{
  196. children: []LogicalPlan{
  197. WindowPlan{
  198. baseLogicalPlan: baseLogicalPlan{
  199. children: []LogicalPlan{
  200. DataSourcePlan{
  201. name: "src1",
  202. streamFields: []interface{}{
  203. &ast.StreamField{
  204. Name: "id1",
  205. FieldType: &ast.BasicType{Type: ast.BIGINT},
  206. },
  207. &ast.StreamField{
  208. Name: "temp",
  209. FieldType: &ast.BasicType{Type: ast.BIGINT},
  210. },
  211. },
  212. streamStmt: streams["src1"],
  213. metaFields: []string{},
  214. }.Init(),
  215. DataSourcePlan{
  216. name: "src2",
  217. streamFields: []interface{}{
  218. &ast.StreamField{
  219. Name: "hum",
  220. FieldType: &ast.BasicType{Type: ast.BIGINT},
  221. },
  222. &ast.StreamField{
  223. Name: "id2",
  224. FieldType: &ast.BasicType{Type: ast.BIGINT},
  225. },
  226. },
  227. streamStmt: streams["src2"],
  228. metaFields: []string{},
  229. timestampFormat: "YYYY-MM-dd HH:mm:ss",
  230. }.Init(),
  231. },
  232. },
  233. condition: nil,
  234. wtype: ast.TUMBLING_WINDOW,
  235. length: 10000,
  236. interval: 0,
  237. limit: 0,
  238. }.Init(),
  239. },
  240. },
  241. from: &ast.Table{Name: "src1"},
  242. joins: ast.Joins{ast.Join{
  243. Name: "src2",
  244. JoinType: ast.INNER_JOIN,
  245. Expr: &ast.BinaryExpr{
  246. OP: ast.AND,
  247. LHS: &ast.BinaryExpr{
  248. LHS: &ast.BinaryExpr{
  249. OP: ast.GT,
  250. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  251. RHS: &ast.IntegerLiteral{Val: 20},
  252. },
  253. OP: ast.OR,
  254. RHS: &ast.BinaryExpr{
  255. OP: ast.GT,
  256. LHS: &ast.FieldRef{Name: "hum", StreamName: "src2"},
  257. RHS: &ast.IntegerLiteral{Val: 60},
  258. },
  259. },
  260. RHS: &ast.BinaryExpr{
  261. OP: ast.EQ,
  262. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  263. RHS: &ast.FieldRef{Name: "id2", StreamName: "src2"},
  264. },
  265. },
  266. }},
  267. }.Init(),
  268. },
  269. },
  270. fields: []ast.Field{
  271. {
  272. Expr: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  273. Name: "id1",
  274. AName: ""},
  275. },
  276. isAggregate: false,
  277. sendMeta: false,
  278. }.Init(),
  279. }, { // 3 optimize window filter
  280. sql: `SELECT id1 FROM src1 WHERE name = "v1" GROUP BY TUMBLINGWINDOW(ss, 10) FILTER( WHERE temp > 2)`,
  281. p: ProjectPlan{
  282. baseLogicalPlan: baseLogicalPlan{
  283. children: []LogicalPlan{
  284. WindowPlan{
  285. baseLogicalPlan: baseLogicalPlan{
  286. children: []LogicalPlan{
  287. FilterPlan{
  288. baseLogicalPlan: baseLogicalPlan{
  289. children: []LogicalPlan{
  290. DataSourcePlan{
  291. name: "src1",
  292. streamFields: []interface{}{
  293. &ast.StreamField{
  294. Name: "id1",
  295. FieldType: &ast.BasicType{Type: ast.BIGINT},
  296. },
  297. &ast.StreamField{
  298. Name: "name",
  299. FieldType: &ast.BasicType{Type: ast.STRINGS},
  300. },
  301. &ast.StreamField{
  302. Name: "temp",
  303. FieldType: &ast.BasicType{Type: ast.BIGINT},
  304. },
  305. },
  306. streamStmt: streams["src1"],
  307. metaFields: []string{},
  308. }.Init(),
  309. },
  310. },
  311. condition: &ast.BinaryExpr{
  312. OP: ast.AND,
  313. LHS: &ast.BinaryExpr{
  314. LHS: &ast.FieldRef{Name: "name", StreamName: "src1"},
  315. OP: ast.EQ,
  316. RHS: &ast.StringLiteral{Val: "v1"},
  317. },
  318. RHS: &ast.BinaryExpr{
  319. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  320. OP: ast.GT,
  321. RHS: &ast.IntegerLiteral{Val: 2},
  322. },
  323. },
  324. }.Init(),
  325. },
  326. },
  327. condition: nil,
  328. wtype: ast.TUMBLING_WINDOW,
  329. length: 10000,
  330. interval: 0,
  331. limit: 0,
  332. }.Init(),
  333. },
  334. },
  335. fields: []ast.Field{
  336. {
  337. Expr: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  338. Name: "id1",
  339. AName: ""},
  340. },
  341. isAggregate: false,
  342. sendMeta: false,
  343. }.Init(),
  344. }, { // 4. do not optimize count window
  345. sql: `SELECT * FROM src1 WHERE temp > 20 GROUP BY COUNTWINDOW(5,1) HAVING COUNT(*) > 2`,
  346. p: ProjectPlan{
  347. baseLogicalPlan: baseLogicalPlan{
  348. children: []LogicalPlan{
  349. HavingPlan{
  350. baseLogicalPlan: baseLogicalPlan{
  351. children: []LogicalPlan{
  352. FilterPlan{
  353. baseLogicalPlan: baseLogicalPlan{
  354. children: []LogicalPlan{
  355. WindowPlan{
  356. baseLogicalPlan: baseLogicalPlan{
  357. children: []LogicalPlan{
  358. DataSourcePlan{
  359. name: "src1",
  360. isWildCard: true,
  361. streamFields: []interface{}{
  362. &ast.StreamField{
  363. Name: "id1",
  364. FieldType: &ast.BasicType{Type: ast.BIGINT},
  365. },
  366. &ast.StreamField{
  367. Name: "temp",
  368. FieldType: &ast.BasicType{Type: ast.BIGINT},
  369. },
  370. &ast.StreamField{
  371. Name: "name",
  372. FieldType: &ast.BasicType{Type: ast.STRINGS},
  373. },
  374. &ast.StreamField{
  375. Name: "myarray",
  376. FieldType: &ast.ArrayType{Type: ast.STRINGS},
  377. },
  378. },
  379. streamStmt: streams["src1"],
  380. metaFields: []string{},
  381. }.Init(),
  382. },
  383. },
  384. condition: nil,
  385. wtype: ast.COUNT_WINDOW,
  386. length: 5,
  387. interval: 1,
  388. limit: 0,
  389. }.Init(),
  390. },
  391. },
  392. condition: &ast.BinaryExpr{
  393. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  394. OP: ast.GT,
  395. RHS: &ast.IntegerLiteral{Val: 20},
  396. },
  397. }.Init(),
  398. },
  399. },
  400. condition: &ast.BinaryExpr{
  401. LHS: &ast.Call{Name: "count", FuncId: 0, Args: []ast.Expr{&ast.Wildcard{
  402. Token: ast.ASTERISK,
  403. }}, FuncType: ast.FuncTypeAgg},
  404. OP: ast.GT,
  405. RHS: &ast.IntegerLiteral{Val: 2},
  406. },
  407. }.Init(),
  408. },
  409. },
  410. fields: []ast.Field{
  411. {
  412. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  413. Name: "*",
  414. AName: ""},
  415. },
  416. isAggregate: false,
  417. sendMeta: false,
  418. }.Init(),
  419. }, { // 5. optimize join on
  420. 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)`,
  421. p: ProjectPlan{
  422. baseLogicalPlan: baseLogicalPlan{
  423. children: []LogicalPlan{
  424. JoinPlan{
  425. baseLogicalPlan: baseLogicalPlan{
  426. children: []LogicalPlan{
  427. WindowPlan{
  428. baseLogicalPlan: baseLogicalPlan{
  429. children: []LogicalPlan{
  430. FilterPlan{
  431. baseLogicalPlan: baseLogicalPlan{
  432. children: []LogicalPlan{
  433. DataSourcePlan{
  434. name: "src1",
  435. streamFields: []interface{}{
  436. &ast.StreamField{
  437. Name: "id1",
  438. FieldType: &ast.BasicType{Type: ast.BIGINT},
  439. },
  440. &ast.StreamField{
  441. Name: "temp",
  442. FieldType: &ast.BasicType{Type: ast.BIGINT},
  443. },
  444. },
  445. streamStmt: streams["src1"],
  446. metaFields: []string{},
  447. }.Init(),
  448. },
  449. },
  450. condition: &ast.BinaryExpr{
  451. RHS: &ast.BinaryExpr{
  452. OP: ast.GT,
  453. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  454. RHS: &ast.IntegerLiteral{Val: 20},
  455. },
  456. OP: ast.AND,
  457. LHS: &ast.BinaryExpr{
  458. OP: ast.GT,
  459. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  460. RHS: &ast.IntegerLiteral{Val: 111},
  461. },
  462. },
  463. }.Init(),
  464. FilterPlan{
  465. baseLogicalPlan: baseLogicalPlan{
  466. children: []LogicalPlan{
  467. DataSourcePlan{
  468. name: "src2",
  469. streamFields: []interface{}{
  470. &ast.StreamField{
  471. Name: "hum",
  472. FieldType: &ast.BasicType{Type: ast.BIGINT},
  473. },
  474. &ast.StreamField{
  475. Name: "id2",
  476. FieldType: &ast.BasicType{Type: ast.BIGINT},
  477. },
  478. },
  479. streamStmt: streams["src2"],
  480. metaFields: []string{},
  481. timestampFormat: "YYYY-MM-dd HH:mm:ss",
  482. }.Init(),
  483. },
  484. },
  485. condition: &ast.BinaryExpr{
  486. OP: ast.LT,
  487. LHS: &ast.FieldRef{Name: "hum", StreamName: "src2"},
  488. RHS: &ast.IntegerLiteral{Val: 60},
  489. },
  490. }.Init(),
  491. },
  492. },
  493. condition: nil,
  494. wtype: ast.TUMBLING_WINDOW,
  495. length: 10000,
  496. interval: 0,
  497. limit: 0,
  498. }.Init(),
  499. },
  500. },
  501. from: &ast.Table{
  502. Name: "src1",
  503. },
  504. joins: []ast.Join{
  505. {
  506. Name: "src2",
  507. Alias: "",
  508. JoinType: ast.INNER_JOIN,
  509. Expr: &ast.BinaryExpr{
  510. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  511. OP: ast.EQ,
  512. RHS: &ast.FieldRef{Name: "id2", StreamName: "src2"},
  513. },
  514. },
  515. },
  516. }.Init(),
  517. },
  518. },
  519. fields: []ast.Field{
  520. {
  521. Expr: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  522. Name: "id1",
  523. AName: ""},
  524. },
  525. isAggregate: false,
  526. sendMeta: false,
  527. }.Init(),
  528. }, { // 6. optimize outter join on
  529. 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)`,
  530. p: ProjectPlan{
  531. baseLogicalPlan: baseLogicalPlan{
  532. children: []LogicalPlan{
  533. JoinPlan{
  534. baseLogicalPlan: baseLogicalPlan{
  535. children: []LogicalPlan{
  536. WindowPlan{
  537. baseLogicalPlan: baseLogicalPlan{
  538. children: []LogicalPlan{
  539. FilterPlan{
  540. baseLogicalPlan: baseLogicalPlan{
  541. children: []LogicalPlan{
  542. DataSourcePlan{
  543. name: "src1",
  544. streamFields: []interface{}{
  545. &ast.StreamField{
  546. Name: "id1",
  547. FieldType: &ast.BasicType{Type: ast.BIGINT},
  548. },
  549. &ast.StreamField{
  550. Name: "temp",
  551. FieldType: &ast.BasicType{Type: ast.BIGINT},
  552. },
  553. },
  554. streamStmt: streams["src1"],
  555. metaFields: []string{},
  556. }.Init(),
  557. },
  558. },
  559. condition: &ast.BinaryExpr{
  560. OP: ast.GT,
  561. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  562. RHS: &ast.IntegerLiteral{Val: 111},
  563. },
  564. }.Init(),
  565. DataSourcePlan{
  566. name: "src2",
  567. streamFields: []interface{}{
  568. &ast.StreamField{
  569. Name: "hum",
  570. FieldType: &ast.BasicType{Type: ast.BIGINT},
  571. },
  572. &ast.StreamField{
  573. Name: "id2",
  574. FieldType: &ast.BasicType{Type: ast.BIGINT},
  575. },
  576. },
  577. streamStmt: streams["src2"],
  578. metaFields: []string{},
  579. timestampFormat: "YYYY-MM-dd HH:mm:ss",
  580. }.Init(),
  581. },
  582. },
  583. condition: nil,
  584. wtype: ast.TUMBLING_WINDOW,
  585. length: 10000,
  586. interval: 0,
  587. limit: 0,
  588. }.Init(),
  589. },
  590. },
  591. from: &ast.Table{
  592. Name: "src1",
  593. },
  594. joins: []ast.Join{
  595. {
  596. Name: "src2",
  597. Alias: "",
  598. JoinType: ast.FULL_JOIN,
  599. Expr: &ast.BinaryExpr{
  600. OP: ast.AND,
  601. LHS: &ast.BinaryExpr{
  602. OP: ast.AND,
  603. LHS: &ast.BinaryExpr{
  604. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  605. OP: ast.EQ,
  606. RHS: &ast.FieldRef{Name: "id2", StreamName: "src2"},
  607. },
  608. RHS: &ast.BinaryExpr{
  609. OP: ast.GT,
  610. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  611. RHS: &ast.IntegerLiteral{Val: 20},
  612. },
  613. },
  614. RHS: &ast.BinaryExpr{
  615. OP: ast.LT,
  616. LHS: &ast.FieldRef{Name: "hum", StreamName: "src2"},
  617. RHS: &ast.IntegerLiteral{Val: 60},
  618. },
  619. },
  620. },
  621. },
  622. }.Init(),
  623. },
  624. },
  625. fields: []ast.Field{
  626. {
  627. Expr: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  628. Name: "id1",
  629. AName: ""},
  630. },
  631. isAggregate: false,
  632. sendMeta: false,
  633. }.Init(),
  634. }, { // 7 window error for table
  635. sql: `SELECT value FROM tableInPlanner WHERE name = "v1" GROUP BY TUMBLINGWINDOW(ss, 10)`,
  636. p: nil,
  637. err: "cannot run window for TABLE sources",
  638. }, { // 8 join table without window
  639. sql: `SELECT id1 FROM src1 INNER JOIN tableInPlanner on src1.id1 = tableInPlanner.id and src1.temp > 20 and hum < 60 WHERE src1.id1 > 111`,
  640. p: ProjectPlan{
  641. baseLogicalPlan: baseLogicalPlan{
  642. children: []LogicalPlan{
  643. JoinPlan{
  644. baseLogicalPlan: baseLogicalPlan{
  645. children: []LogicalPlan{
  646. JoinAlignPlan{
  647. baseLogicalPlan: baseLogicalPlan{
  648. children: []LogicalPlan{
  649. FilterPlan{
  650. baseLogicalPlan: baseLogicalPlan{
  651. children: []LogicalPlan{
  652. DataSourcePlan{
  653. name: "src1",
  654. streamFields: []interface{}{
  655. &ast.StreamField{
  656. Name: "id1",
  657. FieldType: &ast.BasicType{Type: ast.BIGINT},
  658. },
  659. &ast.StreamField{
  660. Name: "temp",
  661. FieldType: &ast.BasicType{Type: ast.BIGINT},
  662. },
  663. },
  664. streamStmt: streams["src1"],
  665. metaFields: []string{},
  666. }.Init(),
  667. },
  668. },
  669. condition: &ast.BinaryExpr{
  670. RHS: &ast.BinaryExpr{
  671. OP: ast.GT,
  672. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  673. RHS: &ast.IntegerLiteral{Val: 20},
  674. },
  675. OP: ast.AND,
  676. LHS: &ast.BinaryExpr{
  677. OP: ast.GT,
  678. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  679. RHS: &ast.IntegerLiteral{Val: 111},
  680. },
  681. },
  682. }.Init(),
  683. DataSourcePlan{
  684. name: "tableInPlanner",
  685. streamFields: []interface{}{
  686. &ast.StreamField{
  687. Name: "hum",
  688. FieldType: &ast.BasicType{Type: ast.BIGINT},
  689. },
  690. &ast.StreamField{
  691. Name: "id",
  692. FieldType: &ast.BasicType{Type: ast.BIGINT},
  693. },
  694. },
  695. streamStmt: streams["tableInPlanner"],
  696. metaFields: []string{},
  697. }.Init(),
  698. },
  699. },
  700. Emitters: []string{"tableInPlanner"},
  701. }.Init(),
  702. },
  703. },
  704. from: &ast.Table{
  705. Name: "src1",
  706. },
  707. joins: []ast.Join{
  708. {
  709. Name: "tableInPlanner",
  710. Alias: "",
  711. JoinType: ast.INNER_JOIN,
  712. Expr: &ast.BinaryExpr{
  713. OP: ast.AND,
  714. LHS: &ast.BinaryExpr{
  715. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  716. OP: ast.EQ,
  717. RHS: &ast.FieldRef{Name: "id", StreamName: "tableInPlanner"},
  718. },
  719. RHS: &ast.BinaryExpr{
  720. OP: ast.LT,
  721. LHS: &ast.FieldRef{Name: "hum", StreamName: "tableInPlanner"},
  722. RHS: &ast.IntegerLiteral{Val: 60},
  723. },
  724. },
  725. },
  726. },
  727. }.Init(),
  728. },
  729. },
  730. fields: []ast.Field{
  731. {
  732. Expr: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  733. Name: "id1",
  734. AName: ""},
  735. },
  736. isAggregate: false,
  737. sendMeta: false,
  738. }.Init(),
  739. }, { // 9 join table with window
  740. 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)`,
  741. p: ProjectPlan{
  742. baseLogicalPlan: baseLogicalPlan{
  743. children: []LogicalPlan{
  744. JoinPlan{
  745. baseLogicalPlan: baseLogicalPlan{
  746. children: []LogicalPlan{
  747. JoinAlignPlan{
  748. baseLogicalPlan: baseLogicalPlan{
  749. children: []LogicalPlan{
  750. WindowPlan{
  751. baseLogicalPlan: baseLogicalPlan{
  752. children: []LogicalPlan{
  753. DataSourcePlan{
  754. name: "src1",
  755. streamFields: []interface{}{
  756. &ast.StreamField{
  757. Name: "id1",
  758. FieldType: &ast.BasicType{Type: ast.BIGINT},
  759. },
  760. &ast.StreamField{
  761. Name: "temp",
  762. FieldType: &ast.BasicType{Type: ast.BIGINT},
  763. },
  764. },
  765. streamStmt: streams["src1"],
  766. metaFields: []string{},
  767. }.Init(),
  768. },
  769. },
  770. condition: nil,
  771. wtype: ast.TUMBLING_WINDOW,
  772. length: 10000,
  773. interval: 0,
  774. limit: 0,
  775. }.Init(),
  776. DataSourcePlan{
  777. name: "tableInPlanner",
  778. streamFields: []interface{}{
  779. &ast.StreamField{
  780. Name: "hum",
  781. FieldType: &ast.BasicType{Type: ast.BIGINT},
  782. },
  783. &ast.StreamField{
  784. Name: "id",
  785. FieldType: &ast.BasicType{Type: ast.BIGINT},
  786. },
  787. },
  788. streamStmt: streams["tableInPlanner"],
  789. metaFields: []string{},
  790. }.Init(),
  791. },
  792. },
  793. Emitters: []string{"tableInPlanner"},
  794. }.Init(),
  795. },
  796. },
  797. from: &ast.Table{
  798. Name: "src1",
  799. },
  800. joins: []ast.Join{
  801. {
  802. Name: "tableInPlanner",
  803. Alias: "",
  804. JoinType: ast.INNER_JOIN,
  805. Expr: &ast.BinaryExpr{
  806. OP: ast.AND,
  807. LHS: &ast.BinaryExpr{
  808. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  809. OP: ast.EQ,
  810. RHS: &ast.FieldRef{Name: "id", StreamName: "tableInPlanner"},
  811. },
  812. RHS: &ast.BinaryExpr{
  813. RHS: &ast.BinaryExpr{
  814. OP: ast.AND,
  815. LHS: &ast.BinaryExpr{
  816. OP: ast.GT,
  817. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  818. RHS: &ast.IntegerLiteral{Val: 20},
  819. },
  820. RHS: &ast.BinaryExpr{
  821. OP: ast.LT,
  822. LHS: &ast.FieldRef{Name: "hum", StreamName: "tableInPlanner"},
  823. RHS: &ast.IntegerLiteral{Val: 60},
  824. },
  825. },
  826. OP: ast.AND,
  827. LHS: &ast.BinaryExpr{
  828. OP: ast.GT,
  829. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  830. RHS: &ast.IntegerLiteral{Val: 111},
  831. },
  832. },
  833. },
  834. },
  835. },
  836. }.Init(),
  837. },
  838. },
  839. fields: []ast.Field{
  840. {
  841. Expr: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  842. Name: "id1",
  843. AName: ""},
  844. },
  845. isAggregate: false,
  846. sendMeta: false,
  847. }.Init(),
  848. }, { // 10 meta
  849. sql: `SELECT temp, meta(id) AS eid,meta(Humidity->Device) AS hdevice FROM src1 WHERE meta(device)="demo2"`,
  850. p: ProjectPlan{
  851. baseLogicalPlan: baseLogicalPlan{
  852. children: []LogicalPlan{
  853. FilterPlan{
  854. baseLogicalPlan: baseLogicalPlan{
  855. children: []LogicalPlan{
  856. DataSourcePlan{
  857. name: "src1",
  858. streamFields: []interface{}{
  859. &ast.StreamField{
  860. Name: "temp",
  861. FieldType: &ast.BasicType{Type: ast.BIGINT},
  862. },
  863. },
  864. streamStmt: streams["src1"],
  865. metaFields: []string{"Humidity", "device", "id"},
  866. }.Init(),
  867. },
  868. },
  869. condition: &ast.BinaryExpr{
  870. LHS: &ast.Call{
  871. Name: "meta",
  872. FuncId: 2,
  873. Args: []ast.Expr{&ast.MetaRef{
  874. Name: "device",
  875. StreamName: ast.DefaultStream,
  876. }},
  877. },
  878. OP: ast.EQ,
  879. RHS: &ast.StringLiteral{
  880. Val: "demo2",
  881. },
  882. },
  883. }.Init(),
  884. },
  885. },
  886. fields: []ast.Field{
  887. {
  888. Expr: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  889. Name: "temp",
  890. AName: "",
  891. }, {
  892. Expr: &ast.FieldRef{Name: "eid", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  893. &ast.Call{Name: "meta", FuncId: 0, Args: []ast.Expr{&ast.MetaRef{
  894. Name: "id",
  895. StreamName: ast.DefaultStream,
  896. }}},
  897. []ast.StreamName{},
  898. nil,
  899. )},
  900. Name: "meta",
  901. AName: "eid",
  902. }, {
  903. Expr: &ast.FieldRef{Name: "hdevice", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  904. &ast.Call{Name: "meta", FuncId: 1, Args: []ast.Expr{
  905. &ast.BinaryExpr{
  906. OP: ast.ARROW,
  907. LHS: &ast.MetaRef{Name: "Humidity", StreamName: ast.DefaultStream},
  908. RHS: &ast.JsonFieldRef{Name: "Device"},
  909. },
  910. }},
  911. []ast.StreamName{},
  912. nil,
  913. )},
  914. Name: "meta",
  915. AName: "hdevice",
  916. },
  917. },
  918. isAggregate: false,
  919. sendMeta: false,
  920. }.Init(),
  921. }, { // 11 join with same name field and aliased
  922. sql: `SELECT src2.hum AS hum1, tableInPlanner.hum AS hum2 FROM src2 INNER JOIN tableInPlanner on id2 = id WHERE hum1 > hum2`,
  923. p: ProjectPlan{
  924. baseLogicalPlan: baseLogicalPlan{
  925. children: []LogicalPlan{
  926. JoinPlan{
  927. baseLogicalPlan: baseLogicalPlan{
  928. children: []LogicalPlan{
  929. JoinAlignPlan{
  930. baseLogicalPlan: baseLogicalPlan{
  931. children: []LogicalPlan{
  932. DataSourcePlan{
  933. name: "src2",
  934. streamFields: []interface{}{
  935. &ast.StreamField{
  936. Name: "hum",
  937. FieldType: &ast.BasicType{Type: ast.BIGINT},
  938. },
  939. &ast.StreamField{
  940. Name: "id2",
  941. FieldType: &ast.BasicType{Type: ast.BIGINT},
  942. },
  943. },
  944. streamStmt: streams["src2"],
  945. metaFields: []string{},
  946. timestampFormat: "YYYY-MM-dd HH:mm:ss",
  947. }.Init(),
  948. DataSourcePlan{
  949. name: "tableInPlanner",
  950. streamFields: []interface{}{
  951. &ast.StreamField{
  952. Name: "hum",
  953. FieldType: &ast.BasicType{Type: ast.BIGINT},
  954. },
  955. &ast.StreamField{
  956. Name: "id",
  957. FieldType: &ast.BasicType{Type: ast.BIGINT},
  958. },
  959. },
  960. streamStmt: streams["tableInPlanner"],
  961. metaFields: []string{},
  962. }.Init(),
  963. },
  964. },
  965. Emitters: []string{"tableInPlanner"},
  966. }.Init(),
  967. },
  968. },
  969. from: &ast.Table{
  970. Name: "src2",
  971. },
  972. joins: []ast.Join{
  973. {
  974. Name: "tableInPlanner",
  975. Alias: "",
  976. JoinType: ast.INNER_JOIN,
  977. Expr: &ast.BinaryExpr{
  978. RHS: &ast.BinaryExpr{
  979. OP: ast.EQ,
  980. LHS: &ast.FieldRef{Name: "id2", StreamName: "src2"},
  981. RHS: &ast.FieldRef{Name: "id", StreamName: "tableInPlanner"},
  982. },
  983. OP: ast.AND,
  984. LHS: &ast.BinaryExpr{
  985. OP: ast.GT,
  986. LHS: &ast.FieldRef{Name: "hum1", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  987. &ast.FieldRef{
  988. Name: "hum",
  989. StreamName: "src2",
  990. },
  991. []ast.StreamName{"src2"},
  992. &boolFalse,
  993. )},
  994. RHS: &ast.FieldRef{Name: "hum2", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  995. &ast.FieldRef{
  996. Name: "hum",
  997. StreamName: "tableInPlanner",
  998. },
  999. []ast.StreamName{"tableInPlanner"},
  1000. &boolFalse,
  1001. )},
  1002. },
  1003. },
  1004. },
  1005. },
  1006. }.Init(),
  1007. },
  1008. },
  1009. fields: []ast.Field{
  1010. {
  1011. Expr: &ast.FieldRef{Name: "hum1", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  1012. &ast.FieldRef{
  1013. Name: "hum",
  1014. StreamName: "src2",
  1015. },
  1016. []ast.StreamName{"src2"},
  1017. &boolFalse,
  1018. )},
  1019. Name: "hum",
  1020. AName: "hum1",
  1021. }, {
  1022. Expr: &ast.FieldRef{Name: "hum2", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  1023. &ast.FieldRef{
  1024. Name: "hum",
  1025. StreamName: "tableInPlanner",
  1026. },
  1027. []ast.StreamName{"tableInPlanner"},
  1028. &boolFalse,
  1029. )},
  1030. Name: "hum",
  1031. AName: "hum2",
  1032. },
  1033. },
  1034. isAggregate: false,
  1035. sendMeta: false,
  1036. }.Init(),
  1037. }, { // 12 meta with more fields
  1038. sql: `SELECT temp, meta(*) as m FROM src1 WHERE meta(device)="demo2"`,
  1039. p: ProjectPlan{
  1040. baseLogicalPlan: baseLogicalPlan{
  1041. children: []LogicalPlan{
  1042. FilterPlan{
  1043. baseLogicalPlan: baseLogicalPlan{
  1044. children: []LogicalPlan{
  1045. DataSourcePlan{
  1046. name: "src1",
  1047. streamFields: []interface{}{
  1048. &ast.StreamField{
  1049. Name: "temp",
  1050. FieldType: &ast.BasicType{Type: ast.BIGINT},
  1051. },
  1052. },
  1053. streamStmt: streams["src1"],
  1054. metaFields: []string{},
  1055. allMeta: true,
  1056. }.Init(),
  1057. },
  1058. },
  1059. condition: &ast.BinaryExpr{
  1060. LHS: &ast.Call{
  1061. Name: "meta",
  1062. FuncId: 1,
  1063. Args: []ast.Expr{&ast.MetaRef{
  1064. Name: "device",
  1065. StreamName: ast.DefaultStream,
  1066. }},
  1067. },
  1068. OP: ast.EQ,
  1069. RHS: &ast.StringLiteral{
  1070. Val: "demo2",
  1071. },
  1072. },
  1073. }.Init(),
  1074. },
  1075. },
  1076. fields: []ast.Field{
  1077. {
  1078. Expr: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  1079. Name: "temp",
  1080. AName: "",
  1081. }, {
  1082. Expr: &ast.FieldRef{Name: "m", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  1083. &ast.Call{Name: "meta", FuncId: 0, Args: []ast.Expr{&ast.MetaRef{
  1084. Name: "*",
  1085. StreamName: ast.DefaultStream,
  1086. }}},
  1087. []ast.StreamName{},
  1088. nil,
  1089. )},
  1090. Name: "meta",
  1091. AName: "m",
  1092. },
  1093. },
  1094. isAggregate: false,
  1095. sendMeta: false,
  1096. }.Init(),
  1097. }, { // 13 analytic function plan
  1098. sql: `SELECT latest(lag(name)), id1 FROM src1 WHERE lag(temp) > temp`,
  1099. p: ProjectPlan{
  1100. baseLogicalPlan: baseLogicalPlan{
  1101. children: []LogicalPlan{
  1102. FilterPlan{
  1103. baseLogicalPlan: baseLogicalPlan{
  1104. children: []LogicalPlan{
  1105. AnalyticFuncsPlan{
  1106. baseLogicalPlan: baseLogicalPlan{
  1107. children: []LogicalPlan{
  1108. DataSourcePlan{
  1109. name: "src1",
  1110. streamFields: []interface{}{
  1111. &ast.StreamField{
  1112. Name: "id1",
  1113. FieldType: &ast.BasicType{Type: ast.BIGINT},
  1114. },
  1115. &ast.StreamField{
  1116. Name: "name",
  1117. FieldType: &ast.BasicType{Type: ast.STRINGS},
  1118. },
  1119. &ast.StreamField{
  1120. Name: "temp",
  1121. FieldType: &ast.BasicType{Type: ast.BIGINT},
  1122. },
  1123. },
  1124. streamStmt: streams["src1"],
  1125. metaFields: []string{},
  1126. }.Init(),
  1127. },
  1128. },
  1129. funcs: []*ast.Call{
  1130. {
  1131. Name: "lag",
  1132. FuncId: 2,
  1133. CachedField: "$$a_lag_2",
  1134. Args: []ast.Expr{&ast.FieldRef{
  1135. Name: "temp",
  1136. StreamName: "src1",
  1137. }},
  1138. },
  1139. {
  1140. 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"}}}},
  1141. },
  1142. {
  1143. Name: "lag", FuncId: 0, CachedField: "$$a_lag_0", FuncType: ast.FuncTypeScalar, Args: []ast.Expr{&ast.FieldRef{Name: "name", StreamName: "src1"}},
  1144. },
  1145. },
  1146. }.Init(),
  1147. },
  1148. },
  1149. condition: &ast.BinaryExpr{
  1150. LHS: &ast.Call{
  1151. Name: "lag",
  1152. FuncId: 2,
  1153. Args: []ast.Expr{&ast.FieldRef{
  1154. Name: "temp",
  1155. StreamName: "src1",
  1156. }},
  1157. CachedField: "$$a_lag_2",
  1158. Cached: true,
  1159. },
  1160. OP: ast.GT,
  1161. RHS: &ast.FieldRef{
  1162. Name: "temp",
  1163. StreamName: "src1",
  1164. },
  1165. },
  1166. }.Init(),
  1167. },
  1168. },
  1169. fields: []ast.Field{
  1170. {
  1171. Expr: &ast.Call{
  1172. Name: "latest",
  1173. FuncId: 1,
  1174. 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"}}}},
  1175. CachedField: "$$a_latest_1",
  1176. Cached: true,
  1177. },
  1178. Name: "latest",
  1179. }, {
  1180. Expr: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  1181. Name: "id1",
  1182. },
  1183. },
  1184. isAggregate: false,
  1185. sendMeta: false,
  1186. }.Init(),
  1187. },
  1188. { // 14
  1189. sql: `SELECT name, *, meta(device) FROM src1`,
  1190. p: ProjectPlan{
  1191. baseLogicalPlan: baseLogicalPlan{
  1192. children: []LogicalPlan{
  1193. DataSourcePlan{
  1194. baseLogicalPlan: baseLogicalPlan{},
  1195. name: "src1",
  1196. streamFields: []interface{}{
  1197. &ast.StreamField{
  1198. Name: "id1",
  1199. FieldType: &ast.BasicType{Type: ast.BIGINT},
  1200. },
  1201. &ast.StreamField{
  1202. Name: "temp",
  1203. FieldType: &ast.BasicType{Type: ast.BIGINT},
  1204. },
  1205. &ast.StreamField{
  1206. Name: "name",
  1207. FieldType: &ast.BasicType{Type: ast.STRINGS},
  1208. },
  1209. &ast.StreamField{
  1210. Name: "myarray",
  1211. FieldType: &ast.ArrayType{Type: ast.STRINGS},
  1212. },
  1213. },
  1214. streamStmt: streams["src1"],
  1215. metaFields: []string{"device"},
  1216. isWildCard: true,
  1217. }.Init(),
  1218. },
  1219. },
  1220. fields: []ast.Field{
  1221. {
  1222. Expr: &ast.FieldRef{Name: "name", StreamName: "src1"},
  1223. Name: "name",
  1224. AName: "",
  1225. },
  1226. {
  1227. Name: "*",
  1228. Expr: &ast.Wildcard{
  1229. Token: ast.ASTERISK,
  1230. },
  1231. },
  1232. {
  1233. Name: "meta",
  1234. Expr: &ast.Call{
  1235. Name: "meta",
  1236. Args: []ast.Expr{
  1237. &ast.MetaRef{
  1238. StreamName: ast.DefaultStream,
  1239. Name: "device",
  1240. },
  1241. },
  1242. },
  1243. },
  1244. },
  1245. isAggregate: false,
  1246. allWildcard: true,
  1247. sendMeta: false,
  1248. }.Init(),
  1249. },
  1250. { // 15 analytic function over partition plan
  1251. sql: `SELECT latest(lag(name)) OVER (PARTITION BY temp), id1 FROM src1 WHERE lag(temp) > temp`,
  1252. p: ProjectPlan{
  1253. baseLogicalPlan: baseLogicalPlan{
  1254. children: []LogicalPlan{
  1255. FilterPlan{
  1256. baseLogicalPlan: baseLogicalPlan{
  1257. children: []LogicalPlan{
  1258. AnalyticFuncsPlan{
  1259. baseLogicalPlan: baseLogicalPlan{
  1260. children: []LogicalPlan{
  1261. DataSourcePlan{
  1262. name: "src1",
  1263. streamFields: []interface{}{
  1264. &ast.StreamField{
  1265. Name: "id1",
  1266. FieldType: &ast.BasicType{Type: ast.BIGINT},
  1267. },
  1268. &ast.StreamField{
  1269. Name: "name",
  1270. FieldType: &ast.BasicType{Type: ast.STRINGS},
  1271. },
  1272. &ast.StreamField{
  1273. Name: "temp",
  1274. FieldType: &ast.BasicType{Type: ast.BIGINT},
  1275. },
  1276. },
  1277. streamStmt: streams["src1"],
  1278. metaFields: []string{},
  1279. }.Init(),
  1280. },
  1281. },
  1282. funcs: []*ast.Call{
  1283. {
  1284. Name: "lag",
  1285. FuncId: 2,
  1286. CachedField: "$$a_lag_2",
  1287. Args: []ast.Expr{&ast.FieldRef{
  1288. Name: "temp",
  1289. StreamName: "src1",
  1290. }},
  1291. },
  1292. {
  1293. 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"}}},
  1294. },
  1295. {
  1296. Name: "lag", FuncId: 0, CachedField: "$$a_lag_0", FuncType: ast.FuncTypeScalar, Args: []ast.Expr{&ast.FieldRef{Name: "name", StreamName: "src1"}},
  1297. },
  1298. },
  1299. }.Init(),
  1300. },
  1301. },
  1302. condition: &ast.BinaryExpr{
  1303. LHS: &ast.Call{
  1304. Name: "lag",
  1305. FuncId: 2,
  1306. Args: []ast.Expr{&ast.FieldRef{
  1307. Name: "temp",
  1308. StreamName: "src1",
  1309. }},
  1310. CachedField: "$$a_lag_2",
  1311. Cached: true,
  1312. },
  1313. OP: ast.GT,
  1314. RHS: &ast.FieldRef{
  1315. Name: "temp",
  1316. StreamName: "src1",
  1317. },
  1318. },
  1319. }.Init(),
  1320. },
  1321. },
  1322. fields: []ast.Field{
  1323. {
  1324. Expr: &ast.Call{
  1325. Name: "latest",
  1326. FuncId: 1,
  1327. 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"}}}},
  1328. CachedField: "$$a_latest_1",
  1329. Cached: true,
  1330. Partition: &ast.PartitionExpr{
  1331. Exprs: []ast.Expr{
  1332. &ast.FieldRef{Name: "temp", StreamName: "src1"},
  1333. },
  1334. },
  1335. },
  1336. Name: "latest",
  1337. }, {
  1338. Expr: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  1339. Name: "id1",
  1340. },
  1341. },
  1342. isAggregate: false,
  1343. sendMeta: false,
  1344. }.Init(),
  1345. },
  1346. { // 16 analytic function over partition when plan
  1347. sql: `SELECT latest(lag(name)) OVER (PARTITION BY temp WHEN temp > 12), id1 FROM src1 WHERE lag(temp) > temp`,
  1348. p: ProjectPlan{
  1349. baseLogicalPlan: baseLogicalPlan{
  1350. children: []LogicalPlan{
  1351. FilterPlan{
  1352. baseLogicalPlan: baseLogicalPlan{
  1353. children: []LogicalPlan{
  1354. AnalyticFuncsPlan{
  1355. baseLogicalPlan: baseLogicalPlan{
  1356. children: []LogicalPlan{
  1357. DataSourcePlan{
  1358. name: "src1",
  1359. streamFields: []interface{}{
  1360. &ast.StreamField{
  1361. Name: "id1",
  1362. FieldType: &ast.BasicType{Type: ast.BIGINT},
  1363. },
  1364. &ast.StreamField{
  1365. Name: "name",
  1366. FieldType: &ast.BasicType{Type: ast.STRINGS},
  1367. },
  1368. &ast.StreamField{
  1369. Name: "temp",
  1370. FieldType: &ast.BasicType{Type: ast.BIGINT},
  1371. },
  1372. },
  1373. streamStmt: streams["src1"],
  1374. metaFields: []string{},
  1375. }.Init(),
  1376. },
  1377. },
  1378. funcs: []*ast.Call{
  1379. {
  1380. Name: "lag",
  1381. FuncId: 2,
  1382. CachedField: "$$a_lag_2",
  1383. Args: []ast.Expr{&ast.FieldRef{
  1384. Name: "temp",
  1385. StreamName: "src1",
  1386. }},
  1387. },
  1388. {
  1389. 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}},
  1390. },
  1391. {
  1392. Name: "lag", FuncId: 0, CachedField: "$$a_lag_0", FuncType: ast.FuncTypeScalar, Args: []ast.Expr{&ast.FieldRef{Name: "name", StreamName: "src1"}},
  1393. },
  1394. },
  1395. }.Init(),
  1396. },
  1397. },
  1398. condition: &ast.BinaryExpr{
  1399. LHS: &ast.Call{
  1400. Name: "lag",
  1401. FuncId: 2,
  1402. Args: []ast.Expr{&ast.FieldRef{
  1403. Name: "temp",
  1404. StreamName: "src1",
  1405. }},
  1406. CachedField: "$$a_lag_2",
  1407. Cached: true,
  1408. },
  1409. OP: ast.GT,
  1410. RHS: &ast.FieldRef{
  1411. Name: "temp",
  1412. StreamName: "src1",
  1413. },
  1414. },
  1415. }.Init(),
  1416. },
  1417. },
  1418. fields: []ast.Field{
  1419. {
  1420. Expr: &ast.Call{
  1421. Name: "latest",
  1422. FuncId: 1,
  1423. 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"}}}},
  1424. CachedField: "$$a_latest_1",
  1425. Cached: true,
  1426. Partition: &ast.PartitionExpr{
  1427. Exprs: []ast.Expr{
  1428. &ast.FieldRef{Name: "temp", StreamName: "src1"},
  1429. },
  1430. },
  1431. WhenExpr: &ast.BinaryExpr{
  1432. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  1433. OP: ast.GT,
  1434. RHS: &ast.IntegerLiteral{Val: 12},
  1435. },
  1436. },
  1437. Name: "latest",
  1438. }, {
  1439. Expr: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  1440. Name: "id1",
  1441. },
  1442. },
  1443. isAggregate: false,
  1444. sendMeta: false,
  1445. }.Init(),
  1446. }, { // 17. do not optimize sliding window
  1447. sql: `SELECT * FROM src1 WHERE temp > 20 GROUP BY SLIDINGWINDOW(ss, 10) HAVING COUNT(*) > 2`,
  1448. p: ProjectPlan{
  1449. baseLogicalPlan: baseLogicalPlan{
  1450. children: []LogicalPlan{
  1451. HavingPlan{
  1452. baseLogicalPlan: baseLogicalPlan{
  1453. children: []LogicalPlan{
  1454. FilterPlan{
  1455. baseLogicalPlan: baseLogicalPlan{
  1456. children: []LogicalPlan{
  1457. WindowPlan{
  1458. baseLogicalPlan: baseLogicalPlan{
  1459. children: []LogicalPlan{
  1460. DataSourcePlan{
  1461. name: "src1",
  1462. isWildCard: true,
  1463. streamFields: []interface{}{
  1464. &ast.StreamField{
  1465. Name: "id1",
  1466. FieldType: &ast.BasicType{Type: ast.BIGINT},
  1467. },
  1468. &ast.StreamField{
  1469. Name: "temp",
  1470. FieldType: &ast.BasicType{Type: ast.BIGINT},
  1471. },
  1472. &ast.StreamField{
  1473. Name: "name",
  1474. FieldType: &ast.BasicType{Type: ast.STRINGS},
  1475. },
  1476. &ast.StreamField{
  1477. Name: "myarray",
  1478. FieldType: &ast.ArrayType{Type: ast.STRINGS},
  1479. },
  1480. },
  1481. streamStmt: streams["src1"],
  1482. metaFields: []string{},
  1483. }.Init(),
  1484. },
  1485. },
  1486. condition: nil,
  1487. wtype: ast.SLIDING_WINDOW,
  1488. length: 10000,
  1489. interval: 0,
  1490. limit: 0,
  1491. }.Init(),
  1492. },
  1493. },
  1494. condition: &ast.BinaryExpr{
  1495. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  1496. OP: ast.GT,
  1497. RHS: &ast.IntegerLiteral{Val: 20},
  1498. },
  1499. }.Init(),
  1500. },
  1501. },
  1502. condition: &ast.BinaryExpr{
  1503. LHS: &ast.Call{Name: "count", FuncId: 0, Args: []ast.Expr{&ast.Wildcard{
  1504. Token: ast.ASTERISK,
  1505. }}, FuncType: ast.FuncTypeAgg},
  1506. OP: ast.GT,
  1507. RHS: &ast.IntegerLiteral{Val: 2},
  1508. },
  1509. }.Init(),
  1510. },
  1511. },
  1512. fields: []ast.Field{
  1513. {
  1514. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  1515. Name: "*",
  1516. AName: ""},
  1517. },
  1518. isAggregate: false,
  1519. sendMeta: false,
  1520. }.Init(),
  1521. },
  1522. }
  1523. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  1524. for i, tt := range tests {
  1525. stmt, err := xsql.NewParser(strings.NewReader(tt.sql)).Parse()
  1526. if err != nil {
  1527. t.Errorf("%d. %q: error compile sql: %s\n", i, tt.sql, err)
  1528. continue
  1529. }
  1530. p, err := createLogicalPlan(stmt, &api.RuleOption{
  1531. IsEventTime: false,
  1532. LateTol: 0,
  1533. Concurrency: 0,
  1534. BufferLength: 0,
  1535. SendMetaToSink: false,
  1536. Qos: 0,
  1537. CheckpointInterval: 0,
  1538. SendError: true,
  1539. }, store)
  1540. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  1541. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.sql, tt.err, err)
  1542. } else if !reflect.DeepEqual(tt.p, p) {
  1543. 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))
  1544. }
  1545. }
  1546. }
  1547. func Test_createLogicalPlanSchemaless(t *testing.T) {
  1548. err, store := store.GetKV("stream")
  1549. if err != nil {
  1550. t.Error(err)
  1551. return
  1552. }
  1553. streamSqls := map[string]string{
  1554. "src1": `CREATE STREAM src1 (
  1555. ) WITH (DATASOURCE="src1", FORMAT="json", KEY="ts");`,
  1556. "src2": `CREATE STREAM src2 (
  1557. ) WITH (DATASOURCE="src2", FORMAT="json", KEY="ts");`,
  1558. "tableInPlanner": `CREATE TABLE tableInPlanner (
  1559. id BIGINT,
  1560. name STRING,
  1561. value STRING,
  1562. hum BIGINT
  1563. ) WITH (TYPE="file");`,
  1564. }
  1565. types := map[string]ast.StreamType{
  1566. "src1": ast.TypeStream,
  1567. "src2": ast.TypeStream,
  1568. "tableInPlanner": ast.TypeTable,
  1569. }
  1570. for name, sql := range streamSqls {
  1571. s, err := json.Marshal(&xsql.StreamInfo{
  1572. StreamType: types[name],
  1573. Statement: sql,
  1574. })
  1575. if err != nil {
  1576. t.Error(err)
  1577. t.Fail()
  1578. }
  1579. err = store.Set(name, string(s))
  1580. if err != nil {
  1581. t.Error(err)
  1582. t.Fail()
  1583. }
  1584. }
  1585. streams := make(map[string]*ast.StreamStmt)
  1586. for n := range streamSqls {
  1587. streamStmt, err := xsql.GetDataSource(store, n)
  1588. if err != nil {
  1589. t.Errorf("fail to get stream %s, please check if stream is created", n)
  1590. return
  1591. }
  1592. streams[n] = streamStmt
  1593. }
  1594. var (
  1595. //boolTrue = true
  1596. boolFalse = false
  1597. )
  1598. var tests = []struct {
  1599. sql string
  1600. p LogicalPlan
  1601. err string
  1602. }{
  1603. { // 0
  1604. sql: `SELECT name FROM src1`,
  1605. p: ProjectPlan{
  1606. baseLogicalPlan: baseLogicalPlan{
  1607. children: []LogicalPlan{
  1608. DataSourcePlan{
  1609. baseLogicalPlan: baseLogicalPlan{},
  1610. name: "src1",
  1611. streamFields: []interface{}{
  1612. "name",
  1613. },
  1614. streamStmt: streams["src1"],
  1615. metaFields: []string{},
  1616. }.Init(),
  1617. },
  1618. },
  1619. fields: []ast.Field{
  1620. {
  1621. Expr: &ast.FieldRef{Name: "name", StreamName: "src1"},
  1622. Name: "name",
  1623. AName: "",
  1624. },
  1625. },
  1626. isAggregate: false,
  1627. sendMeta: false,
  1628. }.Init(),
  1629. }, { // 1 optimize where to data source
  1630. sql: `SELECT temp FROM src1 WHERE name = "v1" GROUP BY TUMBLINGWINDOW(ss, 10)`,
  1631. p: ProjectPlan{
  1632. baseLogicalPlan: baseLogicalPlan{
  1633. children: []LogicalPlan{
  1634. WindowPlan{
  1635. baseLogicalPlan: baseLogicalPlan{
  1636. children: []LogicalPlan{
  1637. FilterPlan{
  1638. baseLogicalPlan: baseLogicalPlan{
  1639. children: []LogicalPlan{
  1640. DataSourcePlan{
  1641. name: "src1",
  1642. streamFields: []interface{}{
  1643. "name", "temp",
  1644. },
  1645. streamStmt: streams["src1"],
  1646. metaFields: []string{},
  1647. }.Init(),
  1648. },
  1649. },
  1650. condition: &ast.BinaryExpr{
  1651. LHS: &ast.FieldRef{Name: "name", StreamName: "src1"},
  1652. OP: ast.EQ,
  1653. RHS: &ast.StringLiteral{Val: "v1"},
  1654. },
  1655. }.Init(),
  1656. },
  1657. },
  1658. condition: nil,
  1659. wtype: ast.TUMBLING_WINDOW,
  1660. length: 10000,
  1661. interval: 0,
  1662. limit: 0,
  1663. }.Init(),
  1664. },
  1665. },
  1666. fields: []ast.Field{
  1667. {
  1668. Expr: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  1669. Name: "temp",
  1670. AName: ""},
  1671. },
  1672. isAggregate: false,
  1673. sendMeta: false,
  1674. }.Init(),
  1675. }, { // 2 condition that cannot be optimized
  1676. 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)`,
  1677. p: ProjectPlan{
  1678. baseLogicalPlan: baseLogicalPlan{
  1679. children: []LogicalPlan{
  1680. JoinPlan{
  1681. baseLogicalPlan: baseLogicalPlan{
  1682. children: []LogicalPlan{
  1683. WindowPlan{
  1684. baseLogicalPlan: baseLogicalPlan{
  1685. children: []LogicalPlan{
  1686. DataSourcePlan{
  1687. name: "src1",
  1688. streamFields: []interface{}{
  1689. "id1", "temp",
  1690. },
  1691. streamStmt: streams["src1"],
  1692. metaFields: []string{},
  1693. }.Init(),
  1694. DataSourcePlan{
  1695. name: "src2",
  1696. streamFields: []interface{}{ // can't determine where is id1 belonged to
  1697. "hum", "id1", "id2",
  1698. },
  1699. streamStmt: streams["src2"],
  1700. metaFields: []string{},
  1701. }.Init(),
  1702. },
  1703. },
  1704. condition: nil,
  1705. wtype: ast.TUMBLING_WINDOW,
  1706. length: 10000,
  1707. interval: 0,
  1708. limit: 0,
  1709. }.Init(),
  1710. },
  1711. },
  1712. from: &ast.Table{Name: "src1"},
  1713. joins: ast.Joins{ast.Join{
  1714. Name: "src2",
  1715. JoinType: ast.INNER_JOIN,
  1716. Expr: &ast.BinaryExpr{
  1717. OP: ast.AND,
  1718. LHS: &ast.BinaryExpr{
  1719. LHS: &ast.BinaryExpr{
  1720. OP: ast.GT,
  1721. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  1722. RHS: &ast.IntegerLiteral{Val: 20},
  1723. },
  1724. OP: ast.OR,
  1725. RHS: &ast.BinaryExpr{
  1726. OP: ast.GT,
  1727. LHS: &ast.FieldRef{Name: "hum", StreamName: "src2"},
  1728. RHS: &ast.IntegerLiteral{Val: 60},
  1729. },
  1730. },
  1731. RHS: &ast.BinaryExpr{
  1732. OP: ast.EQ,
  1733. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  1734. RHS: &ast.FieldRef{Name: "id2", StreamName: "src2"},
  1735. },
  1736. },
  1737. }},
  1738. }.Init(),
  1739. },
  1740. },
  1741. fields: []ast.Field{
  1742. {
  1743. Expr: &ast.FieldRef{Name: "id1", StreamName: ast.DefaultStream},
  1744. Name: "id1",
  1745. AName: ""},
  1746. },
  1747. isAggregate: false,
  1748. sendMeta: false,
  1749. }.Init(),
  1750. }, { // 3 optimize window filter
  1751. sql: `SELECT id1 FROM src1 WHERE name = "v1" GROUP BY TUMBLINGWINDOW(ss, 10) FILTER( WHERE temp > 2)`,
  1752. p: ProjectPlan{
  1753. baseLogicalPlan: baseLogicalPlan{
  1754. children: []LogicalPlan{
  1755. WindowPlan{
  1756. baseLogicalPlan: baseLogicalPlan{
  1757. children: []LogicalPlan{
  1758. FilterPlan{
  1759. baseLogicalPlan: baseLogicalPlan{
  1760. children: []LogicalPlan{
  1761. DataSourcePlan{
  1762. name: "src1",
  1763. streamFields: []interface{}{
  1764. "id1", "name", "temp",
  1765. },
  1766. streamStmt: streams["src1"],
  1767. metaFields: []string{},
  1768. }.Init(),
  1769. },
  1770. },
  1771. condition: &ast.BinaryExpr{
  1772. OP: ast.AND,
  1773. LHS: &ast.BinaryExpr{
  1774. LHS: &ast.FieldRef{Name: "name", StreamName: "src1"},
  1775. OP: ast.EQ,
  1776. RHS: &ast.StringLiteral{Val: "v1"},
  1777. },
  1778. RHS: &ast.BinaryExpr{
  1779. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  1780. OP: ast.GT,
  1781. RHS: &ast.IntegerLiteral{Val: 2},
  1782. },
  1783. },
  1784. }.Init(),
  1785. },
  1786. },
  1787. condition: nil,
  1788. wtype: ast.TUMBLING_WINDOW,
  1789. length: 10000,
  1790. interval: 0,
  1791. limit: 0,
  1792. }.Init(),
  1793. },
  1794. },
  1795. fields: []ast.Field{
  1796. {
  1797. Expr: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  1798. Name: "id1",
  1799. AName: ""},
  1800. },
  1801. isAggregate: false,
  1802. sendMeta: false,
  1803. }.Init(),
  1804. }, { // 4. do not optimize count window
  1805. sql: `SELECT * FROM src1 WHERE temp > 20 GROUP BY COUNTWINDOW(5,1) HAVING COUNT(*) > 2`,
  1806. p: ProjectPlan{
  1807. baseLogicalPlan: baseLogicalPlan{
  1808. children: []LogicalPlan{
  1809. HavingPlan{
  1810. baseLogicalPlan: baseLogicalPlan{
  1811. children: []LogicalPlan{
  1812. FilterPlan{
  1813. baseLogicalPlan: baseLogicalPlan{
  1814. children: []LogicalPlan{
  1815. WindowPlan{
  1816. baseLogicalPlan: baseLogicalPlan{
  1817. children: []LogicalPlan{
  1818. DataSourcePlan{
  1819. name: "src1",
  1820. isWildCard: true,
  1821. streamFields: nil,
  1822. streamStmt: streams["src1"],
  1823. metaFields: []string{},
  1824. }.Init(),
  1825. },
  1826. },
  1827. condition: nil,
  1828. wtype: ast.COUNT_WINDOW,
  1829. length: 5,
  1830. interval: 1,
  1831. limit: 0,
  1832. }.Init(),
  1833. },
  1834. },
  1835. condition: &ast.BinaryExpr{
  1836. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  1837. OP: ast.GT,
  1838. RHS: &ast.IntegerLiteral{Val: 20},
  1839. },
  1840. }.Init(),
  1841. },
  1842. },
  1843. condition: &ast.BinaryExpr{
  1844. LHS: &ast.Call{Name: "count", FuncId: 0, Args: []ast.Expr{&ast.Wildcard{
  1845. Token: ast.ASTERISK,
  1846. }}, FuncType: ast.FuncTypeAgg},
  1847. OP: ast.GT,
  1848. RHS: &ast.IntegerLiteral{Val: 2},
  1849. },
  1850. }.Init(),
  1851. },
  1852. },
  1853. fields: []ast.Field{
  1854. {
  1855. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  1856. Name: "*",
  1857. AName: ""},
  1858. },
  1859. isAggregate: false,
  1860. sendMeta: false,
  1861. }.Init(),
  1862. }, { // 5. optimize join on
  1863. 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)`,
  1864. p: ProjectPlan{
  1865. baseLogicalPlan: baseLogicalPlan{
  1866. children: []LogicalPlan{
  1867. JoinPlan{
  1868. baseLogicalPlan: baseLogicalPlan{
  1869. children: []LogicalPlan{
  1870. WindowPlan{
  1871. baseLogicalPlan: baseLogicalPlan{
  1872. children: []LogicalPlan{
  1873. FilterPlan{
  1874. baseLogicalPlan: baseLogicalPlan{
  1875. children: []LogicalPlan{
  1876. DataSourcePlan{
  1877. name: "src1",
  1878. streamFields: []interface{}{
  1879. "id1", "temp",
  1880. },
  1881. streamStmt: streams["src1"],
  1882. metaFields: []string{},
  1883. }.Init(),
  1884. },
  1885. },
  1886. condition: &ast.BinaryExpr{
  1887. RHS: &ast.BinaryExpr{
  1888. OP: ast.GT,
  1889. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  1890. RHS: &ast.IntegerLiteral{Val: 20},
  1891. },
  1892. OP: ast.AND,
  1893. LHS: &ast.BinaryExpr{
  1894. OP: ast.GT,
  1895. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  1896. RHS: &ast.IntegerLiteral{Val: 111},
  1897. },
  1898. },
  1899. }.Init(),
  1900. FilterPlan{
  1901. baseLogicalPlan: baseLogicalPlan{
  1902. children: []LogicalPlan{
  1903. DataSourcePlan{
  1904. name: "src2",
  1905. streamFields: []interface{}{
  1906. "hum", "id1", "id2",
  1907. },
  1908. streamStmt: streams["src2"],
  1909. metaFields: []string{},
  1910. }.Init(),
  1911. },
  1912. },
  1913. condition: &ast.BinaryExpr{
  1914. OP: ast.LT,
  1915. LHS: &ast.FieldRef{Name: "hum", StreamName: "src2"},
  1916. RHS: &ast.IntegerLiteral{Val: 60},
  1917. },
  1918. }.Init(),
  1919. },
  1920. },
  1921. condition: nil,
  1922. wtype: ast.TUMBLING_WINDOW,
  1923. length: 10000,
  1924. interval: 0,
  1925. limit: 0,
  1926. }.Init(),
  1927. },
  1928. },
  1929. from: &ast.Table{
  1930. Name: "src1",
  1931. },
  1932. joins: []ast.Join{
  1933. {
  1934. Name: "src2",
  1935. Alias: "",
  1936. JoinType: ast.INNER_JOIN,
  1937. Expr: &ast.BinaryExpr{
  1938. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  1939. OP: ast.EQ,
  1940. RHS: &ast.FieldRef{Name: "id2", StreamName: "src2"},
  1941. },
  1942. },
  1943. },
  1944. }.Init(),
  1945. },
  1946. },
  1947. fields: []ast.Field{
  1948. {
  1949. Expr: &ast.FieldRef{Name: "id1", StreamName: ast.DefaultStream},
  1950. Name: "id1",
  1951. AName: ""},
  1952. },
  1953. isAggregate: false,
  1954. sendMeta: false,
  1955. }.Init(),
  1956. }, { // 6. optimize outter join on
  1957. 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)`,
  1958. p: ProjectPlan{
  1959. baseLogicalPlan: baseLogicalPlan{
  1960. children: []LogicalPlan{
  1961. JoinPlan{
  1962. baseLogicalPlan: baseLogicalPlan{
  1963. children: []LogicalPlan{
  1964. WindowPlan{
  1965. baseLogicalPlan: baseLogicalPlan{
  1966. children: []LogicalPlan{
  1967. FilterPlan{
  1968. baseLogicalPlan: baseLogicalPlan{
  1969. children: []LogicalPlan{
  1970. DataSourcePlan{
  1971. name: "src1",
  1972. streamFields: []interface{}{
  1973. "id1", "temp",
  1974. },
  1975. streamStmt: streams["src1"],
  1976. metaFields: []string{},
  1977. }.Init(),
  1978. },
  1979. },
  1980. condition: &ast.BinaryExpr{
  1981. OP: ast.GT,
  1982. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  1983. RHS: &ast.IntegerLiteral{Val: 111},
  1984. },
  1985. }.Init(),
  1986. DataSourcePlan{
  1987. name: "src2",
  1988. streamFields: []interface{}{
  1989. "hum", "id1", "id2",
  1990. },
  1991. streamStmt: streams["src2"],
  1992. metaFields: []string{},
  1993. }.Init(),
  1994. },
  1995. },
  1996. condition: nil,
  1997. wtype: ast.TUMBLING_WINDOW,
  1998. length: 10000,
  1999. interval: 0,
  2000. limit: 0,
  2001. }.Init(),
  2002. },
  2003. },
  2004. from: &ast.Table{
  2005. Name: "src1",
  2006. },
  2007. joins: []ast.Join{
  2008. {
  2009. Name: "src2",
  2010. Alias: "",
  2011. JoinType: ast.FULL_JOIN,
  2012. Expr: &ast.BinaryExpr{
  2013. OP: ast.AND,
  2014. LHS: &ast.BinaryExpr{
  2015. OP: ast.AND,
  2016. LHS: &ast.BinaryExpr{
  2017. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  2018. OP: ast.EQ,
  2019. RHS: &ast.FieldRef{Name: "id2", StreamName: "src2"},
  2020. },
  2021. RHS: &ast.BinaryExpr{
  2022. OP: ast.GT,
  2023. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  2024. RHS: &ast.IntegerLiteral{Val: 20},
  2025. },
  2026. },
  2027. RHS: &ast.BinaryExpr{
  2028. OP: ast.LT,
  2029. LHS: &ast.FieldRef{Name: "hum", StreamName: "src2"},
  2030. RHS: &ast.IntegerLiteral{Val: 60},
  2031. },
  2032. },
  2033. },
  2034. },
  2035. }.Init(),
  2036. },
  2037. },
  2038. fields: []ast.Field{
  2039. {
  2040. Expr: &ast.FieldRef{Name: "id1", StreamName: ast.DefaultStream},
  2041. Name: "id1",
  2042. AName: ""},
  2043. },
  2044. isAggregate: false,
  2045. sendMeta: false,
  2046. }.Init(),
  2047. }, { // 7 window error for table
  2048. sql: `SELECT value FROM tableInPlanner WHERE name = "v1" GROUP BY TUMBLINGWINDOW(ss, 10)`,
  2049. p: nil,
  2050. err: "cannot run window for TABLE sources",
  2051. }, { // 8 join table without window
  2052. sql: `SELECT id1 FROM src1 INNER JOIN tableInPlanner on src1.id1 = tableInPlanner.id and src1.temp > 20 and hum < 60 WHERE src1.id1 > 111`,
  2053. p: ProjectPlan{
  2054. baseLogicalPlan: baseLogicalPlan{
  2055. children: []LogicalPlan{
  2056. JoinPlan{
  2057. baseLogicalPlan: baseLogicalPlan{
  2058. children: []LogicalPlan{
  2059. JoinAlignPlan{
  2060. baseLogicalPlan: baseLogicalPlan{
  2061. children: []LogicalPlan{
  2062. FilterPlan{
  2063. baseLogicalPlan: baseLogicalPlan{
  2064. children: []LogicalPlan{
  2065. DataSourcePlan{
  2066. name: "src1",
  2067. streamFields: []interface{}{
  2068. "hum", "id1", "temp",
  2069. },
  2070. streamStmt: streams["src1"],
  2071. metaFields: []string{},
  2072. }.Init(),
  2073. },
  2074. },
  2075. condition: &ast.BinaryExpr{
  2076. RHS: &ast.BinaryExpr{
  2077. OP: ast.GT,
  2078. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  2079. RHS: &ast.IntegerLiteral{Val: 20},
  2080. },
  2081. OP: ast.AND,
  2082. LHS: &ast.BinaryExpr{
  2083. OP: ast.GT,
  2084. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  2085. RHS: &ast.IntegerLiteral{Val: 111},
  2086. },
  2087. },
  2088. }.Init(),
  2089. DataSourcePlan{
  2090. name: "tableInPlanner",
  2091. streamFields: []interface{}{
  2092. &ast.StreamField{
  2093. Name: "hum",
  2094. FieldType: &ast.BasicType{Type: ast.BIGINT},
  2095. },
  2096. &ast.StreamField{
  2097. Name: "id",
  2098. FieldType: &ast.BasicType{Type: ast.BIGINT},
  2099. },
  2100. },
  2101. streamStmt: streams["tableInPlanner"],
  2102. metaFields: []string{},
  2103. }.Init(),
  2104. },
  2105. },
  2106. Emitters: []string{"tableInPlanner"},
  2107. }.Init(),
  2108. },
  2109. },
  2110. from: &ast.Table{
  2111. Name: "src1",
  2112. },
  2113. joins: []ast.Join{
  2114. {
  2115. Name: "tableInPlanner",
  2116. Alias: "",
  2117. JoinType: ast.INNER_JOIN,
  2118. Expr: &ast.BinaryExpr{
  2119. OP: ast.AND,
  2120. LHS: &ast.BinaryExpr{
  2121. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  2122. OP: ast.EQ,
  2123. RHS: &ast.FieldRef{Name: "id", StreamName: "tableInPlanner"},
  2124. },
  2125. RHS: &ast.BinaryExpr{
  2126. OP: ast.LT,
  2127. LHS: &ast.FieldRef{Name: "hum", StreamName: ast.DefaultStream},
  2128. RHS: &ast.IntegerLiteral{Val: 60},
  2129. },
  2130. },
  2131. },
  2132. },
  2133. }.Init(),
  2134. },
  2135. },
  2136. fields: []ast.Field{
  2137. {
  2138. Expr: &ast.FieldRef{Name: "id1", StreamName: ast.DefaultStream},
  2139. Name: "id1",
  2140. AName: ""},
  2141. },
  2142. isAggregate: false,
  2143. sendMeta: false,
  2144. }.Init(),
  2145. }, { // 9 join table with window
  2146. 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)`,
  2147. p: ProjectPlan{
  2148. baseLogicalPlan: baseLogicalPlan{
  2149. children: []LogicalPlan{
  2150. JoinPlan{
  2151. baseLogicalPlan: baseLogicalPlan{
  2152. children: []LogicalPlan{
  2153. JoinAlignPlan{
  2154. baseLogicalPlan: baseLogicalPlan{
  2155. children: []LogicalPlan{
  2156. WindowPlan{
  2157. baseLogicalPlan: baseLogicalPlan{
  2158. children: []LogicalPlan{
  2159. DataSourcePlan{
  2160. name: "src1",
  2161. streamFields: []interface{}{
  2162. "id1", "temp",
  2163. },
  2164. streamStmt: streams["src1"],
  2165. metaFields: []string{},
  2166. }.Init(),
  2167. },
  2168. },
  2169. condition: nil,
  2170. wtype: ast.TUMBLING_WINDOW,
  2171. length: 10000,
  2172. interval: 0,
  2173. limit: 0,
  2174. }.Init(),
  2175. DataSourcePlan{
  2176. name: "tableInPlanner",
  2177. streamFields: []interface{}{
  2178. &ast.StreamField{
  2179. Name: "hum",
  2180. FieldType: &ast.BasicType{Type: ast.BIGINT},
  2181. },
  2182. &ast.StreamField{
  2183. Name: "id",
  2184. FieldType: &ast.BasicType{Type: ast.BIGINT},
  2185. },
  2186. },
  2187. streamStmt: streams["tableInPlanner"],
  2188. metaFields: []string{},
  2189. }.Init(),
  2190. },
  2191. },
  2192. Emitters: []string{"tableInPlanner"},
  2193. }.Init(),
  2194. },
  2195. },
  2196. from: &ast.Table{
  2197. Name: "src1",
  2198. },
  2199. joins: []ast.Join{
  2200. {
  2201. Name: "tableInPlanner",
  2202. Alias: "",
  2203. JoinType: ast.INNER_JOIN,
  2204. Expr: &ast.BinaryExpr{
  2205. OP: ast.AND,
  2206. LHS: &ast.BinaryExpr{
  2207. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  2208. OP: ast.EQ,
  2209. RHS: &ast.FieldRef{Name: "id", StreamName: "tableInPlanner"},
  2210. },
  2211. RHS: &ast.BinaryExpr{
  2212. RHS: &ast.BinaryExpr{
  2213. OP: ast.AND,
  2214. LHS: &ast.BinaryExpr{
  2215. OP: ast.GT,
  2216. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  2217. RHS: &ast.IntegerLiteral{Val: 20},
  2218. },
  2219. RHS: &ast.BinaryExpr{
  2220. OP: ast.LT,
  2221. LHS: &ast.FieldRef{Name: "hum", StreamName: "tableInPlanner"},
  2222. RHS: &ast.IntegerLiteral{Val: 60},
  2223. },
  2224. },
  2225. OP: ast.AND,
  2226. LHS: &ast.BinaryExpr{
  2227. OP: ast.GT,
  2228. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  2229. RHS: &ast.IntegerLiteral{Val: 111},
  2230. },
  2231. },
  2232. },
  2233. },
  2234. },
  2235. }.Init(),
  2236. },
  2237. },
  2238. fields: []ast.Field{
  2239. {
  2240. Expr: &ast.FieldRef{Name: "id1", StreamName: ast.DefaultStream},
  2241. Name: "id1",
  2242. AName: ""},
  2243. },
  2244. isAggregate: false,
  2245. sendMeta: false,
  2246. }.Init(),
  2247. }, { // 10 meta
  2248. sql: `SELECT temp, meta(id) AS eid,meta(Humidity->Device) AS hdevice FROM src1 WHERE meta(device)="demo2"`,
  2249. p: ProjectPlan{
  2250. baseLogicalPlan: baseLogicalPlan{
  2251. children: []LogicalPlan{
  2252. FilterPlan{
  2253. baseLogicalPlan: baseLogicalPlan{
  2254. children: []LogicalPlan{
  2255. DataSourcePlan{
  2256. name: "src1",
  2257. streamFields: []interface{}{
  2258. "temp",
  2259. },
  2260. streamStmt: streams["src1"],
  2261. metaFields: []string{"Humidity", "device", "id"},
  2262. }.Init(),
  2263. },
  2264. },
  2265. condition: &ast.BinaryExpr{
  2266. LHS: &ast.Call{
  2267. Name: "meta",
  2268. FuncId: 2,
  2269. Args: []ast.Expr{&ast.MetaRef{
  2270. Name: "device",
  2271. StreamName: ast.DefaultStream,
  2272. }},
  2273. },
  2274. OP: ast.EQ,
  2275. RHS: &ast.StringLiteral{
  2276. Val: "demo2",
  2277. },
  2278. },
  2279. }.Init(),
  2280. },
  2281. },
  2282. fields: []ast.Field{
  2283. {
  2284. Expr: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  2285. Name: "temp",
  2286. AName: "",
  2287. }, {
  2288. Expr: &ast.FieldRef{Name: "eid", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  2289. &ast.Call{Name: "meta", FuncId: 0, Args: []ast.Expr{&ast.MetaRef{
  2290. Name: "id",
  2291. StreamName: ast.DefaultStream,
  2292. }}},
  2293. []ast.StreamName{},
  2294. nil,
  2295. )},
  2296. Name: "meta",
  2297. AName: "eid",
  2298. }, {
  2299. Expr: &ast.FieldRef{Name: "hdevice", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  2300. &ast.Call{Name: "meta", FuncId: 1, Args: []ast.Expr{
  2301. &ast.BinaryExpr{
  2302. OP: ast.ARROW,
  2303. LHS: &ast.MetaRef{Name: "Humidity", StreamName: ast.DefaultStream},
  2304. RHS: &ast.JsonFieldRef{Name: "Device"},
  2305. },
  2306. }},
  2307. []ast.StreamName{},
  2308. nil,
  2309. )},
  2310. Name: "meta",
  2311. AName: "hdevice",
  2312. },
  2313. },
  2314. isAggregate: false,
  2315. sendMeta: false,
  2316. }.Init(),
  2317. }, { // 11 join with same name field and aliased
  2318. sql: `SELECT src2.hum AS hum1, tableInPlanner.hum AS hum2 FROM src2 INNER JOIN tableInPlanner on id2 = id WHERE hum1 > hum2`,
  2319. p: ProjectPlan{
  2320. baseLogicalPlan: baseLogicalPlan{
  2321. children: []LogicalPlan{
  2322. JoinPlan{
  2323. baseLogicalPlan: baseLogicalPlan{
  2324. children: []LogicalPlan{
  2325. JoinAlignPlan{
  2326. baseLogicalPlan: baseLogicalPlan{
  2327. children: []LogicalPlan{
  2328. DataSourcePlan{
  2329. name: "src2",
  2330. streamFields: []interface{}{
  2331. "hum", "id", "id2",
  2332. },
  2333. streamStmt: streams["src2"],
  2334. metaFields: []string{},
  2335. }.Init(),
  2336. DataSourcePlan{
  2337. name: "tableInPlanner",
  2338. streamFields: []interface{}{
  2339. &ast.StreamField{
  2340. Name: "hum",
  2341. FieldType: &ast.BasicType{Type: ast.BIGINT},
  2342. },
  2343. &ast.StreamField{
  2344. Name: "id",
  2345. FieldType: &ast.BasicType{Type: ast.BIGINT},
  2346. },
  2347. },
  2348. streamStmt: streams["tableInPlanner"],
  2349. metaFields: []string{},
  2350. }.Init(),
  2351. },
  2352. },
  2353. Emitters: []string{"tableInPlanner"},
  2354. }.Init(),
  2355. },
  2356. },
  2357. from: &ast.Table{
  2358. Name: "src2",
  2359. },
  2360. joins: []ast.Join{
  2361. {
  2362. Name: "tableInPlanner",
  2363. Alias: "",
  2364. JoinType: ast.INNER_JOIN,
  2365. Expr: &ast.BinaryExpr{
  2366. RHS: &ast.BinaryExpr{
  2367. OP: ast.EQ,
  2368. LHS: &ast.FieldRef{Name: "id2", StreamName: ast.DefaultStream},
  2369. RHS: &ast.FieldRef{Name: "id", StreamName: ast.DefaultStream},
  2370. },
  2371. OP: ast.AND,
  2372. LHS: &ast.BinaryExpr{
  2373. OP: ast.GT,
  2374. LHS: &ast.FieldRef{Name: "hum1", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  2375. &ast.FieldRef{
  2376. Name: "hum",
  2377. StreamName: "src2",
  2378. },
  2379. []ast.StreamName{"src2"},
  2380. &boolFalse,
  2381. )},
  2382. RHS: &ast.FieldRef{Name: "hum2", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  2383. &ast.FieldRef{
  2384. Name: "hum",
  2385. StreamName: "tableInPlanner",
  2386. },
  2387. []ast.StreamName{"tableInPlanner"},
  2388. &boolFalse,
  2389. )},
  2390. },
  2391. },
  2392. },
  2393. },
  2394. }.Init(),
  2395. },
  2396. },
  2397. fields: []ast.Field{
  2398. {
  2399. Expr: &ast.FieldRef{Name: "hum1", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  2400. &ast.FieldRef{
  2401. Name: "hum",
  2402. StreamName: "src2",
  2403. },
  2404. []ast.StreamName{"src2"},
  2405. &boolFalse,
  2406. )},
  2407. Name: "hum",
  2408. AName: "hum1",
  2409. }, {
  2410. Expr: &ast.FieldRef{Name: "hum2", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  2411. &ast.FieldRef{
  2412. Name: "hum",
  2413. StreamName: "tableInPlanner",
  2414. },
  2415. []ast.StreamName{"tableInPlanner"},
  2416. &boolFalse,
  2417. )},
  2418. Name: "hum",
  2419. AName: "hum2",
  2420. },
  2421. },
  2422. isAggregate: false,
  2423. sendMeta: false,
  2424. }.Init(),
  2425. }, { // 12
  2426. sql: `SELECT name->first, name->last FROM src1`,
  2427. p: ProjectPlan{
  2428. baseLogicalPlan: baseLogicalPlan{
  2429. children: []LogicalPlan{
  2430. DataSourcePlan{
  2431. baseLogicalPlan: baseLogicalPlan{},
  2432. name: "src1",
  2433. streamFields: []interface{}{
  2434. "name",
  2435. },
  2436. streamStmt: streams["src1"],
  2437. metaFields: []string{},
  2438. }.Init(),
  2439. },
  2440. },
  2441. fields: []ast.Field{
  2442. {
  2443. Expr: &ast.BinaryExpr{
  2444. OP: ast.ARROW,
  2445. LHS: &ast.FieldRef{StreamName: "src1", Name: "name"},
  2446. RHS: &ast.JsonFieldRef{Name: "first"},
  2447. },
  2448. Name: "kuiper_field_0",
  2449. AName: "",
  2450. }, {
  2451. Expr: &ast.BinaryExpr{
  2452. OP: ast.ARROW,
  2453. LHS: &ast.FieldRef{StreamName: "src1", Name: "name"},
  2454. RHS: &ast.JsonFieldRef{Name: "last"},
  2455. },
  2456. Name: "kuiper_field_1",
  2457. AName: "",
  2458. },
  2459. },
  2460. isAggregate: false,
  2461. sendMeta: false,
  2462. }.Init(),
  2463. },
  2464. }
  2465. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  2466. for i, tt := range tests {
  2467. stmt, err := xsql.NewParser(strings.NewReader(tt.sql)).Parse()
  2468. if err != nil {
  2469. t.Errorf("%d. %q: error compile sql: %s\n", i, tt.sql, err)
  2470. continue
  2471. }
  2472. p, err := createLogicalPlan(stmt, &api.RuleOption{
  2473. IsEventTime: false,
  2474. LateTol: 0,
  2475. Concurrency: 0,
  2476. BufferLength: 0,
  2477. SendMetaToSink: false,
  2478. Qos: 0,
  2479. CheckpointInterval: 0,
  2480. SendError: true,
  2481. }, store)
  2482. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  2483. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.sql, tt.err, err)
  2484. } else if !reflect.DeepEqual(tt.p, p) {
  2485. 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))
  2486. }
  2487. }
  2488. }
  2489. func Test_createLogicalPlan4Lookup(t *testing.T) {
  2490. err, store := store.GetKV("stream")
  2491. if err != nil {
  2492. t.Error(err)
  2493. return
  2494. }
  2495. streamSqls := map[string]string{
  2496. "src1": `CREATE STREAM src1 () WITH (DATASOURCE="src1", FORMAT="json", KEY="ts");`,
  2497. "table1": `CREATE TABLE table1 () WITH (DATASOURCE="table1",TYPE="sql", KIND="lookup");`,
  2498. "table2": `CREATE TABLE table2 () WITH (DATASOURCE="table2",TYPE="sql", KIND="lookup");`,
  2499. }
  2500. types := map[string]ast.StreamType{
  2501. "src1": ast.TypeStream,
  2502. "table1": ast.TypeTable,
  2503. "table2": ast.TypeTable,
  2504. }
  2505. for name, sql := range streamSqls {
  2506. s, err := json.Marshal(&xsql.StreamInfo{
  2507. StreamType: types[name],
  2508. Statement: sql,
  2509. })
  2510. if err != nil {
  2511. t.Error(err)
  2512. t.Fail()
  2513. }
  2514. err = store.Set(name, string(s))
  2515. if err != nil {
  2516. t.Error(err)
  2517. t.Fail()
  2518. }
  2519. }
  2520. streams := make(map[string]*ast.StreamStmt)
  2521. for n := range streamSqls {
  2522. streamStmt, err := xsql.GetDataSource(store, n)
  2523. if err != nil {
  2524. t.Errorf("fail to get stream %s, please check if stream is created", n)
  2525. return
  2526. }
  2527. streams[n] = streamStmt
  2528. }
  2529. var tests = []struct {
  2530. sql string
  2531. p LogicalPlan
  2532. err string
  2533. }{
  2534. { // 0
  2535. sql: `SELECT src1.a, table1.b FROM src1 INNER JOIN table1 ON src1.id = table1.id`,
  2536. p: ProjectPlan{
  2537. baseLogicalPlan: baseLogicalPlan{
  2538. children: []LogicalPlan{
  2539. LookupPlan{
  2540. baseLogicalPlan: baseLogicalPlan{
  2541. children: []LogicalPlan{
  2542. DataSourcePlan{
  2543. baseLogicalPlan: baseLogicalPlan{},
  2544. name: "src1",
  2545. streamFields: []interface{}{
  2546. "a",
  2547. },
  2548. streamStmt: streams["src1"],
  2549. metaFields: []string{},
  2550. }.Init(),
  2551. },
  2552. },
  2553. joinExpr: ast.Join{
  2554. Name: "table1",
  2555. Alias: "",
  2556. JoinType: ast.INNER_JOIN,
  2557. Expr: &ast.BinaryExpr{
  2558. OP: ast.EQ,
  2559. LHS: &ast.FieldRef{
  2560. StreamName: "src1",
  2561. Name: "id",
  2562. },
  2563. RHS: &ast.FieldRef{
  2564. StreamName: "table1",
  2565. Name: "id",
  2566. },
  2567. },
  2568. },
  2569. keys: []string{"id"},
  2570. fields: []string{"b"},
  2571. valvars: []ast.Expr{
  2572. &ast.FieldRef{
  2573. StreamName: "src1",
  2574. Name: "id",
  2575. },
  2576. },
  2577. options: &ast.Options{
  2578. DATASOURCE: "table1",
  2579. TYPE: "sql",
  2580. STRICT_VALIDATION: true,
  2581. KIND: "lookup",
  2582. },
  2583. conditions: nil,
  2584. }.Init(),
  2585. },
  2586. },
  2587. fields: []ast.Field{
  2588. {
  2589. Expr: &ast.FieldRef{
  2590. StreamName: "src1",
  2591. Name: "a",
  2592. },
  2593. Name: "a",
  2594. AName: "",
  2595. },
  2596. {
  2597. Expr: &ast.FieldRef{
  2598. StreamName: "table1",
  2599. Name: "b",
  2600. },
  2601. Name: "b",
  2602. AName: "",
  2603. },
  2604. },
  2605. isAggregate: false,
  2606. sendMeta: false,
  2607. }.Init(),
  2608. },
  2609. { // 1
  2610. sql: `SELECT src1.a, table1.* FROM src1 INNER JOIN table1 ON table1.b > 20 AND src1.c < 40 AND src1.id = table1.id`,
  2611. p: ProjectPlan{
  2612. baseLogicalPlan: baseLogicalPlan{
  2613. children: []LogicalPlan{
  2614. FilterPlan{
  2615. baseLogicalPlan: baseLogicalPlan{
  2616. children: []LogicalPlan{
  2617. LookupPlan{
  2618. baseLogicalPlan: baseLogicalPlan{
  2619. children: []LogicalPlan{
  2620. FilterPlan{
  2621. baseLogicalPlan: baseLogicalPlan{
  2622. children: []LogicalPlan{
  2623. DataSourcePlan{
  2624. baseLogicalPlan: baseLogicalPlan{},
  2625. name: "src1",
  2626. streamFields: []interface{}{
  2627. "a",
  2628. },
  2629. streamStmt: streams["src1"],
  2630. metaFields: []string{},
  2631. }.Init(),
  2632. },
  2633. },
  2634. condition: &ast.BinaryExpr{
  2635. OP: ast.LT,
  2636. LHS: &ast.FieldRef{
  2637. StreamName: "src1",
  2638. Name: "c",
  2639. },
  2640. RHS: &ast.IntegerLiteral{Val: 40},
  2641. },
  2642. }.Init(),
  2643. },
  2644. },
  2645. joinExpr: ast.Join{
  2646. Name: "table1",
  2647. Alias: "",
  2648. JoinType: ast.INNER_JOIN,
  2649. Expr: &ast.BinaryExpr{
  2650. OP: ast.AND,
  2651. RHS: &ast.BinaryExpr{
  2652. OP: ast.EQ,
  2653. LHS: &ast.FieldRef{
  2654. StreamName: "src1",
  2655. Name: "id",
  2656. },
  2657. RHS: &ast.FieldRef{
  2658. StreamName: "table1",
  2659. Name: "id",
  2660. },
  2661. },
  2662. LHS: &ast.BinaryExpr{
  2663. OP: ast.AND,
  2664. LHS: &ast.BinaryExpr{
  2665. OP: ast.GT,
  2666. LHS: &ast.FieldRef{
  2667. StreamName: "table1",
  2668. Name: "b",
  2669. },
  2670. RHS: &ast.IntegerLiteral{Val: 20},
  2671. },
  2672. RHS: &ast.BinaryExpr{
  2673. OP: ast.LT,
  2674. LHS: &ast.FieldRef{
  2675. StreamName: "src1",
  2676. Name: "c",
  2677. },
  2678. RHS: &ast.IntegerLiteral{Val: 40},
  2679. },
  2680. },
  2681. },
  2682. },
  2683. keys: []string{"id"},
  2684. valvars: []ast.Expr{
  2685. &ast.FieldRef{
  2686. StreamName: "src1",
  2687. Name: "id",
  2688. },
  2689. },
  2690. options: &ast.Options{
  2691. DATASOURCE: "table1",
  2692. TYPE: "sql",
  2693. STRICT_VALIDATION: true,
  2694. KIND: "lookup",
  2695. },
  2696. conditions: &ast.BinaryExpr{
  2697. OP: ast.AND,
  2698. LHS: &ast.BinaryExpr{
  2699. OP: ast.GT,
  2700. LHS: &ast.FieldRef{
  2701. StreamName: "table1",
  2702. Name: "b",
  2703. },
  2704. RHS: &ast.IntegerLiteral{Val: 20},
  2705. },
  2706. RHS: &ast.BinaryExpr{
  2707. OP: ast.LT,
  2708. LHS: &ast.FieldRef{
  2709. StreamName: "src1",
  2710. Name: "c",
  2711. },
  2712. RHS: &ast.IntegerLiteral{Val: 40},
  2713. },
  2714. },
  2715. }.Init(),
  2716. },
  2717. },
  2718. condition: &ast.BinaryExpr{
  2719. OP: ast.GT,
  2720. LHS: &ast.FieldRef{
  2721. StreamName: "table1",
  2722. Name: "b",
  2723. },
  2724. RHS: &ast.IntegerLiteral{Val: 20},
  2725. },
  2726. }.Init(),
  2727. },
  2728. },
  2729. fields: []ast.Field{
  2730. {
  2731. Expr: &ast.FieldRef{
  2732. StreamName: "src1",
  2733. Name: "a",
  2734. },
  2735. Name: "a",
  2736. AName: "",
  2737. },
  2738. {
  2739. Expr: &ast.FieldRef{
  2740. StreamName: "table1",
  2741. Name: "*",
  2742. },
  2743. Name: "*",
  2744. AName: "",
  2745. },
  2746. },
  2747. isAggregate: false,
  2748. sendMeta: false,
  2749. }.Init(),
  2750. },
  2751. { // 2
  2752. 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`,
  2753. p: ProjectPlan{
  2754. baseLogicalPlan: baseLogicalPlan{
  2755. children: []LogicalPlan{
  2756. LookupPlan{
  2757. baseLogicalPlan: baseLogicalPlan{
  2758. children: []LogicalPlan{
  2759. LookupPlan{
  2760. baseLogicalPlan: baseLogicalPlan{
  2761. children: []LogicalPlan{
  2762. DataSourcePlan{
  2763. baseLogicalPlan: baseLogicalPlan{},
  2764. name: "src1",
  2765. streamFields: []interface{}{
  2766. "a",
  2767. },
  2768. streamStmt: streams["src1"],
  2769. metaFields: []string{},
  2770. }.Init(),
  2771. },
  2772. },
  2773. joinExpr: ast.Join{
  2774. Name: "table1",
  2775. Alias: "",
  2776. JoinType: ast.INNER_JOIN,
  2777. Expr: &ast.BinaryExpr{
  2778. OP: ast.EQ,
  2779. LHS: &ast.FieldRef{
  2780. StreamName: "src1",
  2781. Name: "id",
  2782. },
  2783. RHS: &ast.FieldRef{
  2784. StreamName: "table1",
  2785. Name: "id",
  2786. },
  2787. },
  2788. },
  2789. keys: []string{"id"},
  2790. fields: []string{"b"},
  2791. valvars: []ast.Expr{
  2792. &ast.FieldRef{
  2793. StreamName: "src1",
  2794. Name: "id",
  2795. },
  2796. },
  2797. options: &ast.Options{
  2798. DATASOURCE: "table1",
  2799. TYPE: "sql",
  2800. STRICT_VALIDATION: true,
  2801. KIND: "lookup",
  2802. },
  2803. conditions: nil,
  2804. }.Init(),
  2805. },
  2806. },
  2807. joinExpr: ast.Join{
  2808. Name: "table2",
  2809. Alias: "",
  2810. JoinType: ast.INNER_JOIN,
  2811. Expr: &ast.BinaryExpr{
  2812. OP: ast.EQ,
  2813. LHS: &ast.FieldRef{
  2814. StreamName: "table1",
  2815. Name: "id",
  2816. },
  2817. RHS: &ast.FieldRef{
  2818. StreamName: "table2",
  2819. Name: "id",
  2820. },
  2821. },
  2822. },
  2823. keys: []string{"id"},
  2824. fields: []string{"c"},
  2825. valvars: []ast.Expr{
  2826. &ast.FieldRef{
  2827. StreamName: "table1",
  2828. Name: "id",
  2829. },
  2830. },
  2831. options: &ast.Options{
  2832. DATASOURCE: "table2",
  2833. TYPE: "sql",
  2834. STRICT_VALIDATION: true,
  2835. KIND: "lookup",
  2836. },
  2837. }.Init(),
  2838. },
  2839. },
  2840. fields: []ast.Field{
  2841. {
  2842. Expr: &ast.FieldRef{
  2843. StreamName: "src1",
  2844. Name: "a",
  2845. },
  2846. Name: "a",
  2847. AName: "",
  2848. },
  2849. {
  2850. Expr: &ast.FieldRef{
  2851. StreamName: "table1",
  2852. Name: "b",
  2853. },
  2854. Name: "b",
  2855. AName: "",
  2856. },
  2857. {
  2858. Expr: &ast.FieldRef{
  2859. StreamName: "table2",
  2860. Name: "c",
  2861. },
  2862. Name: "c",
  2863. AName: "",
  2864. },
  2865. },
  2866. isAggregate: false,
  2867. sendMeta: false,
  2868. }.Init(),
  2869. },
  2870. { // 3
  2871. sql: `SELECT * FROM src1 INNER JOIN table1 ON src1.id = table1.id GROUP BY TUMBLINGWINDOW(ss, 10)`,
  2872. p: ProjectPlan{
  2873. baseLogicalPlan: baseLogicalPlan{
  2874. children: []LogicalPlan{
  2875. LookupPlan{
  2876. baseLogicalPlan: baseLogicalPlan{
  2877. children: []LogicalPlan{
  2878. WindowPlan{
  2879. baseLogicalPlan: baseLogicalPlan{
  2880. children: []LogicalPlan{
  2881. DataSourcePlan{
  2882. baseLogicalPlan: baseLogicalPlan{},
  2883. name: "src1",
  2884. streamStmt: streams["src1"],
  2885. metaFields: []string{},
  2886. isWildCard: true,
  2887. }.Init(),
  2888. },
  2889. },
  2890. condition: nil,
  2891. wtype: ast.TUMBLING_WINDOW,
  2892. length: 10000,
  2893. interval: 0,
  2894. limit: 0,
  2895. }.Init(),
  2896. },
  2897. },
  2898. joinExpr: ast.Join{
  2899. Name: "table1",
  2900. Alias: "",
  2901. JoinType: ast.INNER_JOIN,
  2902. Expr: &ast.BinaryExpr{
  2903. OP: ast.EQ,
  2904. LHS: &ast.FieldRef{
  2905. StreamName: "src1",
  2906. Name: "id",
  2907. },
  2908. RHS: &ast.FieldRef{
  2909. StreamName: "table1",
  2910. Name: "id",
  2911. },
  2912. },
  2913. },
  2914. keys: []string{"id"},
  2915. valvars: []ast.Expr{
  2916. &ast.FieldRef{
  2917. StreamName: "src1",
  2918. Name: "id",
  2919. },
  2920. },
  2921. options: &ast.Options{
  2922. DATASOURCE: "table1",
  2923. TYPE: "sql",
  2924. STRICT_VALIDATION: true,
  2925. KIND: "lookup",
  2926. },
  2927. conditions: nil,
  2928. }.Init(),
  2929. },
  2930. },
  2931. fields: []ast.Field{
  2932. {
  2933. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  2934. Name: "*",
  2935. AName: "",
  2936. },
  2937. },
  2938. isAggregate: false,
  2939. sendMeta: false,
  2940. }.Init(),
  2941. },
  2942. }
  2943. for i, tt := range tests {
  2944. stmt, err := xsql.NewParser(strings.NewReader(tt.sql)).Parse()
  2945. if err != nil {
  2946. t.Errorf("%d. %q: error compile sql: %s\n", i, tt.sql, err)
  2947. continue
  2948. }
  2949. p, err := createLogicalPlan(stmt, &api.RuleOption{
  2950. IsEventTime: false,
  2951. LateTol: 0,
  2952. Concurrency: 0,
  2953. BufferLength: 0,
  2954. SendMetaToSink: false,
  2955. Qos: 0,
  2956. CheckpointInterval: 0,
  2957. SendError: true,
  2958. }, store)
  2959. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  2960. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.sql, tt.err, err)
  2961. } else if !reflect.DeepEqual(tt.p, p) {
  2962. 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))
  2963. }
  2964. }
  2965. }