planner_test.go 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903
  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. },
  1447. }
  1448. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  1449. for i, tt := range tests {
  1450. stmt, err := xsql.NewParser(strings.NewReader(tt.sql)).Parse()
  1451. if err != nil {
  1452. t.Errorf("%d. %q: error compile sql: %s\n", i, tt.sql, err)
  1453. continue
  1454. }
  1455. p, err := createLogicalPlan(stmt, &api.RuleOption{
  1456. IsEventTime: false,
  1457. LateTol: 0,
  1458. Concurrency: 0,
  1459. BufferLength: 0,
  1460. SendMetaToSink: false,
  1461. Qos: 0,
  1462. CheckpointInterval: 0,
  1463. SendError: true,
  1464. }, store)
  1465. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  1466. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.sql, tt.err, err)
  1467. } else if !reflect.DeepEqual(tt.p, p) {
  1468. 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))
  1469. }
  1470. }
  1471. }
  1472. func Test_createLogicalPlanSchemaless(t *testing.T) {
  1473. err, store := store.GetKV("stream")
  1474. if err != nil {
  1475. t.Error(err)
  1476. return
  1477. }
  1478. streamSqls := map[string]string{
  1479. "src1": `CREATE STREAM src1 (
  1480. ) WITH (DATASOURCE="src1", FORMAT="json", KEY="ts");`,
  1481. "src2": `CREATE STREAM src2 (
  1482. ) WITH (DATASOURCE="src2", FORMAT="json", KEY="ts");`,
  1483. "tableInPlanner": `CREATE TABLE tableInPlanner (
  1484. id BIGINT,
  1485. name STRING,
  1486. value STRING,
  1487. hum BIGINT
  1488. ) WITH (TYPE="file");`,
  1489. }
  1490. types := map[string]ast.StreamType{
  1491. "src1": ast.TypeStream,
  1492. "src2": ast.TypeStream,
  1493. "tableInPlanner": ast.TypeTable,
  1494. }
  1495. for name, sql := range streamSqls {
  1496. s, err := json.Marshal(&xsql.StreamInfo{
  1497. StreamType: types[name],
  1498. Statement: sql,
  1499. })
  1500. if err != nil {
  1501. t.Error(err)
  1502. t.Fail()
  1503. }
  1504. err = store.Set(name, string(s))
  1505. if err != nil {
  1506. t.Error(err)
  1507. t.Fail()
  1508. }
  1509. }
  1510. streams := make(map[string]*ast.StreamStmt)
  1511. for n := range streamSqls {
  1512. streamStmt, err := xsql.GetDataSource(store, n)
  1513. if err != nil {
  1514. t.Errorf("fail to get stream %s, please check if stream is created", n)
  1515. return
  1516. }
  1517. streams[n] = streamStmt
  1518. }
  1519. var (
  1520. //boolTrue = true
  1521. boolFalse = false
  1522. )
  1523. var tests = []struct {
  1524. sql string
  1525. p LogicalPlan
  1526. err string
  1527. }{
  1528. { // 0
  1529. sql: `SELECT name FROM src1`,
  1530. p: ProjectPlan{
  1531. baseLogicalPlan: baseLogicalPlan{
  1532. children: []LogicalPlan{
  1533. DataSourcePlan{
  1534. baseLogicalPlan: baseLogicalPlan{},
  1535. name: "src1",
  1536. streamFields: []interface{}{
  1537. "name",
  1538. },
  1539. streamStmt: streams["src1"],
  1540. metaFields: []string{},
  1541. }.Init(),
  1542. },
  1543. },
  1544. fields: []ast.Field{
  1545. {
  1546. Expr: &ast.FieldRef{Name: "name", StreamName: "src1"},
  1547. Name: "name",
  1548. AName: "",
  1549. },
  1550. },
  1551. isAggregate: false,
  1552. sendMeta: false,
  1553. }.Init(),
  1554. }, { // 1 optimize where to data source
  1555. sql: `SELECT temp FROM src1 WHERE name = "v1" GROUP BY TUMBLINGWINDOW(ss, 10)`,
  1556. p: ProjectPlan{
  1557. baseLogicalPlan: baseLogicalPlan{
  1558. children: []LogicalPlan{
  1559. WindowPlan{
  1560. baseLogicalPlan: baseLogicalPlan{
  1561. children: []LogicalPlan{
  1562. FilterPlan{
  1563. baseLogicalPlan: baseLogicalPlan{
  1564. children: []LogicalPlan{
  1565. DataSourcePlan{
  1566. name: "src1",
  1567. streamFields: []interface{}{
  1568. "name", "temp",
  1569. },
  1570. streamStmt: streams["src1"],
  1571. metaFields: []string{},
  1572. }.Init(),
  1573. },
  1574. },
  1575. condition: &ast.BinaryExpr{
  1576. LHS: &ast.FieldRef{Name: "name", StreamName: "src1"},
  1577. OP: ast.EQ,
  1578. RHS: &ast.StringLiteral{Val: "v1"},
  1579. },
  1580. }.Init(),
  1581. },
  1582. },
  1583. condition: nil,
  1584. wtype: ast.TUMBLING_WINDOW,
  1585. length: 10000,
  1586. interval: 0,
  1587. limit: 0,
  1588. }.Init(),
  1589. },
  1590. },
  1591. fields: []ast.Field{
  1592. {
  1593. Expr: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  1594. Name: "temp",
  1595. AName: ""},
  1596. },
  1597. isAggregate: false,
  1598. sendMeta: false,
  1599. }.Init(),
  1600. }, { // 2 condition that cannot be optimized
  1601. 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)`,
  1602. p: ProjectPlan{
  1603. baseLogicalPlan: baseLogicalPlan{
  1604. children: []LogicalPlan{
  1605. JoinPlan{
  1606. baseLogicalPlan: baseLogicalPlan{
  1607. children: []LogicalPlan{
  1608. WindowPlan{
  1609. baseLogicalPlan: baseLogicalPlan{
  1610. children: []LogicalPlan{
  1611. DataSourcePlan{
  1612. name: "src1",
  1613. streamFields: []interface{}{
  1614. "id1", "temp",
  1615. },
  1616. streamStmt: streams["src1"],
  1617. metaFields: []string{},
  1618. }.Init(),
  1619. DataSourcePlan{
  1620. name: "src2",
  1621. streamFields: []interface{}{ // can't determine where is id1 belonged to
  1622. "hum", "id1", "id2",
  1623. },
  1624. streamStmt: streams["src2"],
  1625. metaFields: []string{},
  1626. }.Init(),
  1627. },
  1628. },
  1629. condition: nil,
  1630. wtype: ast.TUMBLING_WINDOW,
  1631. length: 10000,
  1632. interval: 0,
  1633. limit: 0,
  1634. }.Init(),
  1635. },
  1636. },
  1637. from: &ast.Table{Name: "src1"},
  1638. joins: ast.Joins{ast.Join{
  1639. Name: "src2",
  1640. JoinType: ast.INNER_JOIN,
  1641. Expr: &ast.BinaryExpr{
  1642. OP: ast.AND,
  1643. LHS: &ast.BinaryExpr{
  1644. LHS: &ast.BinaryExpr{
  1645. OP: ast.GT,
  1646. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  1647. RHS: &ast.IntegerLiteral{Val: 20},
  1648. },
  1649. OP: ast.OR,
  1650. RHS: &ast.BinaryExpr{
  1651. OP: ast.GT,
  1652. LHS: &ast.FieldRef{Name: "hum", StreamName: "src2"},
  1653. RHS: &ast.IntegerLiteral{Val: 60},
  1654. },
  1655. },
  1656. RHS: &ast.BinaryExpr{
  1657. OP: ast.EQ,
  1658. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  1659. RHS: &ast.FieldRef{Name: "id2", StreamName: "src2"},
  1660. },
  1661. },
  1662. }},
  1663. }.Init(),
  1664. },
  1665. },
  1666. fields: []ast.Field{
  1667. {
  1668. Expr: &ast.FieldRef{Name: "id1", StreamName: ast.DefaultStream},
  1669. Name: "id1",
  1670. AName: ""},
  1671. },
  1672. isAggregate: false,
  1673. sendMeta: false,
  1674. }.Init(),
  1675. }, { // 3 optimize window filter
  1676. sql: `SELECT id1 FROM src1 WHERE name = "v1" GROUP BY TUMBLINGWINDOW(ss, 10) FILTER( WHERE temp > 2)`,
  1677. p: ProjectPlan{
  1678. baseLogicalPlan: baseLogicalPlan{
  1679. children: []LogicalPlan{
  1680. WindowPlan{
  1681. baseLogicalPlan: baseLogicalPlan{
  1682. children: []LogicalPlan{
  1683. FilterPlan{
  1684. baseLogicalPlan: baseLogicalPlan{
  1685. children: []LogicalPlan{
  1686. DataSourcePlan{
  1687. name: "src1",
  1688. streamFields: []interface{}{
  1689. "id1", "name", "temp",
  1690. },
  1691. streamStmt: streams["src1"],
  1692. metaFields: []string{},
  1693. }.Init(),
  1694. },
  1695. },
  1696. condition: &ast.BinaryExpr{
  1697. OP: ast.AND,
  1698. LHS: &ast.BinaryExpr{
  1699. LHS: &ast.FieldRef{Name: "name", StreamName: "src1"},
  1700. OP: ast.EQ,
  1701. RHS: &ast.StringLiteral{Val: "v1"},
  1702. },
  1703. RHS: &ast.BinaryExpr{
  1704. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  1705. OP: ast.GT,
  1706. RHS: &ast.IntegerLiteral{Val: 2},
  1707. },
  1708. },
  1709. }.Init(),
  1710. },
  1711. },
  1712. condition: nil,
  1713. wtype: ast.TUMBLING_WINDOW,
  1714. length: 10000,
  1715. interval: 0,
  1716. limit: 0,
  1717. }.Init(),
  1718. },
  1719. },
  1720. fields: []ast.Field{
  1721. {
  1722. Expr: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  1723. Name: "id1",
  1724. AName: ""},
  1725. },
  1726. isAggregate: false,
  1727. sendMeta: false,
  1728. }.Init(),
  1729. }, { // 4. do not optimize count window
  1730. sql: `SELECT * FROM src1 WHERE temp > 20 GROUP BY COUNTWINDOW(5,1) HAVING COUNT(*) > 2`,
  1731. p: ProjectPlan{
  1732. baseLogicalPlan: baseLogicalPlan{
  1733. children: []LogicalPlan{
  1734. HavingPlan{
  1735. baseLogicalPlan: baseLogicalPlan{
  1736. children: []LogicalPlan{
  1737. FilterPlan{
  1738. baseLogicalPlan: baseLogicalPlan{
  1739. children: []LogicalPlan{
  1740. WindowPlan{
  1741. baseLogicalPlan: baseLogicalPlan{
  1742. children: []LogicalPlan{
  1743. DataSourcePlan{
  1744. name: "src1",
  1745. isWildCard: true,
  1746. streamFields: nil,
  1747. streamStmt: streams["src1"],
  1748. metaFields: []string{},
  1749. }.Init(),
  1750. },
  1751. },
  1752. condition: nil,
  1753. wtype: ast.COUNT_WINDOW,
  1754. length: 5,
  1755. interval: 1,
  1756. limit: 0,
  1757. }.Init(),
  1758. },
  1759. },
  1760. condition: &ast.BinaryExpr{
  1761. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  1762. OP: ast.GT,
  1763. RHS: &ast.IntegerLiteral{Val: 20},
  1764. },
  1765. }.Init(),
  1766. },
  1767. },
  1768. condition: &ast.BinaryExpr{
  1769. LHS: &ast.Call{Name: "count", FuncId: 0, Args: []ast.Expr{&ast.Wildcard{
  1770. Token: ast.ASTERISK,
  1771. }}, FuncType: ast.FuncTypeAgg},
  1772. OP: ast.GT,
  1773. RHS: &ast.IntegerLiteral{Val: 2},
  1774. },
  1775. }.Init(),
  1776. },
  1777. },
  1778. fields: []ast.Field{
  1779. {
  1780. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  1781. Name: "*",
  1782. AName: ""},
  1783. },
  1784. isAggregate: false,
  1785. sendMeta: false,
  1786. }.Init(),
  1787. }, { // 5. optimize join on
  1788. 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)`,
  1789. p: ProjectPlan{
  1790. baseLogicalPlan: baseLogicalPlan{
  1791. children: []LogicalPlan{
  1792. JoinPlan{
  1793. baseLogicalPlan: baseLogicalPlan{
  1794. children: []LogicalPlan{
  1795. WindowPlan{
  1796. baseLogicalPlan: baseLogicalPlan{
  1797. children: []LogicalPlan{
  1798. FilterPlan{
  1799. baseLogicalPlan: baseLogicalPlan{
  1800. children: []LogicalPlan{
  1801. DataSourcePlan{
  1802. name: "src1",
  1803. streamFields: []interface{}{
  1804. "id1", "temp",
  1805. },
  1806. streamStmt: streams["src1"],
  1807. metaFields: []string{},
  1808. }.Init(),
  1809. },
  1810. },
  1811. condition: &ast.BinaryExpr{
  1812. RHS: &ast.BinaryExpr{
  1813. OP: ast.GT,
  1814. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  1815. RHS: &ast.IntegerLiteral{Val: 20},
  1816. },
  1817. OP: ast.AND,
  1818. LHS: &ast.BinaryExpr{
  1819. OP: ast.GT,
  1820. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  1821. RHS: &ast.IntegerLiteral{Val: 111},
  1822. },
  1823. },
  1824. }.Init(),
  1825. FilterPlan{
  1826. baseLogicalPlan: baseLogicalPlan{
  1827. children: []LogicalPlan{
  1828. DataSourcePlan{
  1829. name: "src2",
  1830. streamFields: []interface{}{
  1831. "hum", "id1", "id2",
  1832. },
  1833. streamStmt: streams["src2"],
  1834. metaFields: []string{},
  1835. }.Init(),
  1836. },
  1837. },
  1838. condition: &ast.BinaryExpr{
  1839. OP: ast.LT,
  1840. LHS: &ast.FieldRef{Name: "hum", StreamName: "src2"},
  1841. RHS: &ast.IntegerLiteral{Val: 60},
  1842. },
  1843. }.Init(),
  1844. },
  1845. },
  1846. condition: nil,
  1847. wtype: ast.TUMBLING_WINDOW,
  1848. length: 10000,
  1849. interval: 0,
  1850. limit: 0,
  1851. }.Init(),
  1852. },
  1853. },
  1854. from: &ast.Table{
  1855. Name: "src1",
  1856. },
  1857. joins: []ast.Join{
  1858. {
  1859. Name: "src2",
  1860. Alias: "",
  1861. JoinType: ast.INNER_JOIN,
  1862. Expr: &ast.BinaryExpr{
  1863. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  1864. OP: ast.EQ,
  1865. RHS: &ast.FieldRef{Name: "id2", StreamName: "src2"},
  1866. },
  1867. },
  1868. },
  1869. }.Init(),
  1870. },
  1871. },
  1872. fields: []ast.Field{
  1873. {
  1874. Expr: &ast.FieldRef{Name: "id1", StreamName: ast.DefaultStream},
  1875. Name: "id1",
  1876. AName: ""},
  1877. },
  1878. isAggregate: false,
  1879. sendMeta: false,
  1880. }.Init(),
  1881. }, { // 6. optimize outter join on
  1882. 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)`,
  1883. p: ProjectPlan{
  1884. baseLogicalPlan: baseLogicalPlan{
  1885. children: []LogicalPlan{
  1886. JoinPlan{
  1887. baseLogicalPlan: baseLogicalPlan{
  1888. children: []LogicalPlan{
  1889. WindowPlan{
  1890. baseLogicalPlan: baseLogicalPlan{
  1891. children: []LogicalPlan{
  1892. FilterPlan{
  1893. baseLogicalPlan: baseLogicalPlan{
  1894. children: []LogicalPlan{
  1895. DataSourcePlan{
  1896. name: "src1",
  1897. streamFields: []interface{}{
  1898. "id1", "temp",
  1899. },
  1900. streamStmt: streams["src1"],
  1901. metaFields: []string{},
  1902. }.Init(),
  1903. },
  1904. },
  1905. condition: &ast.BinaryExpr{
  1906. OP: ast.GT,
  1907. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  1908. RHS: &ast.IntegerLiteral{Val: 111},
  1909. },
  1910. }.Init(),
  1911. DataSourcePlan{
  1912. name: "src2",
  1913. streamFields: []interface{}{
  1914. "hum", "id1", "id2",
  1915. },
  1916. streamStmt: streams["src2"],
  1917. metaFields: []string{},
  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.FULL_JOIN,
  1937. Expr: &ast.BinaryExpr{
  1938. OP: ast.AND,
  1939. LHS: &ast.BinaryExpr{
  1940. OP: ast.AND,
  1941. LHS: &ast.BinaryExpr{
  1942. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  1943. OP: ast.EQ,
  1944. RHS: &ast.FieldRef{Name: "id2", StreamName: "src2"},
  1945. },
  1946. RHS: &ast.BinaryExpr{
  1947. OP: ast.GT,
  1948. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  1949. RHS: &ast.IntegerLiteral{Val: 20},
  1950. },
  1951. },
  1952. RHS: &ast.BinaryExpr{
  1953. OP: ast.LT,
  1954. LHS: &ast.FieldRef{Name: "hum", StreamName: "src2"},
  1955. RHS: &ast.IntegerLiteral{Val: 60},
  1956. },
  1957. },
  1958. },
  1959. },
  1960. }.Init(),
  1961. },
  1962. },
  1963. fields: []ast.Field{
  1964. {
  1965. Expr: &ast.FieldRef{Name: "id1", StreamName: ast.DefaultStream},
  1966. Name: "id1",
  1967. AName: ""},
  1968. },
  1969. isAggregate: false,
  1970. sendMeta: false,
  1971. }.Init(),
  1972. }, { // 7 window error for table
  1973. sql: `SELECT value FROM tableInPlanner WHERE name = "v1" GROUP BY TUMBLINGWINDOW(ss, 10)`,
  1974. p: nil,
  1975. err: "cannot run window for TABLE sources",
  1976. }, { // 8 join table without window
  1977. sql: `SELECT id1 FROM src1 INNER JOIN tableInPlanner on src1.id1 = tableInPlanner.id and src1.temp > 20 and hum < 60 WHERE src1.id1 > 111`,
  1978. p: ProjectPlan{
  1979. baseLogicalPlan: baseLogicalPlan{
  1980. children: []LogicalPlan{
  1981. JoinPlan{
  1982. baseLogicalPlan: baseLogicalPlan{
  1983. children: []LogicalPlan{
  1984. JoinAlignPlan{
  1985. baseLogicalPlan: baseLogicalPlan{
  1986. children: []LogicalPlan{
  1987. FilterPlan{
  1988. baseLogicalPlan: baseLogicalPlan{
  1989. children: []LogicalPlan{
  1990. DataSourcePlan{
  1991. name: "src1",
  1992. streamFields: []interface{}{
  1993. "hum", "id1", "temp",
  1994. },
  1995. streamStmt: streams["src1"],
  1996. metaFields: []string{},
  1997. }.Init(),
  1998. },
  1999. },
  2000. condition: &ast.BinaryExpr{
  2001. RHS: &ast.BinaryExpr{
  2002. OP: ast.GT,
  2003. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  2004. RHS: &ast.IntegerLiteral{Val: 20},
  2005. },
  2006. OP: ast.AND,
  2007. LHS: &ast.BinaryExpr{
  2008. OP: ast.GT,
  2009. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  2010. RHS: &ast.IntegerLiteral{Val: 111},
  2011. },
  2012. },
  2013. }.Init(),
  2014. DataSourcePlan{
  2015. name: "tableInPlanner",
  2016. streamFields: []interface{}{
  2017. &ast.StreamField{
  2018. Name: "hum",
  2019. FieldType: &ast.BasicType{Type: ast.BIGINT},
  2020. },
  2021. &ast.StreamField{
  2022. Name: "id",
  2023. FieldType: &ast.BasicType{Type: ast.BIGINT},
  2024. },
  2025. },
  2026. streamStmt: streams["tableInPlanner"],
  2027. metaFields: []string{},
  2028. }.Init(),
  2029. },
  2030. },
  2031. Emitters: []string{"tableInPlanner"},
  2032. }.Init(),
  2033. },
  2034. },
  2035. from: &ast.Table{
  2036. Name: "src1",
  2037. },
  2038. joins: []ast.Join{
  2039. {
  2040. Name: "tableInPlanner",
  2041. Alias: "",
  2042. JoinType: ast.INNER_JOIN,
  2043. Expr: &ast.BinaryExpr{
  2044. OP: ast.AND,
  2045. LHS: &ast.BinaryExpr{
  2046. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  2047. OP: ast.EQ,
  2048. RHS: &ast.FieldRef{Name: "id", StreamName: "tableInPlanner"},
  2049. },
  2050. RHS: &ast.BinaryExpr{
  2051. OP: ast.LT,
  2052. LHS: &ast.FieldRef{Name: "hum", StreamName: ast.DefaultStream},
  2053. RHS: &ast.IntegerLiteral{Val: 60},
  2054. },
  2055. },
  2056. },
  2057. },
  2058. }.Init(),
  2059. },
  2060. },
  2061. fields: []ast.Field{
  2062. {
  2063. Expr: &ast.FieldRef{Name: "id1", StreamName: ast.DefaultStream},
  2064. Name: "id1",
  2065. AName: ""},
  2066. },
  2067. isAggregate: false,
  2068. sendMeta: false,
  2069. }.Init(),
  2070. }, { // 9 join table with window
  2071. 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)`,
  2072. p: ProjectPlan{
  2073. baseLogicalPlan: baseLogicalPlan{
  2074. children: []LogicalPlan{
  2075. JoinPlan{
  2076. baseLogicalPlan: baseLogicalPlan{
  2077. children: []LogicalPlan{
  2078. JoinAlignPlan{
  2079. baseLogicalPlan: baseLogicalPlan{
  2080. children: []LogicalPlan{
  2081. WindowPlan{
  2082. baseLogicalPlan: baseLogicalPlan{
  2083. children: []LogicalPlan{
  2084. DataSourcePlan{
  2085. name: "src1",
  2086. streamFields: []interface{}{
  2087. "id1", "temp",
  2088. },
  2089. streamStmt: streams["src1"],
  2090. metaFields: []string{},
  2091. }.Init(),
  2092. },
  2093. },
  2094. condition: nil,
  2095. wtype: ast.TUMBLING_WINDOW,
  2096. length: 10000,
  2097. interval: 0,
  2098. limit: 0,
  2099. }.Init(),
  2100. DataSourcePlan{
  2101. name: "tableInPlanner",
  2102. streamFields: []interface{}{
  2103. &ast.StreamField{
  2104. Name: "hum",
  2105. FieldType: &ast.BasicType{Type: ast.BIGINT},
  2106. },
  2107. &ast.StreamField{
  2108. Name: "id",
  2109. FieldType: &ast.BasicType{Type: ast.BIGINT},
  2110. },
  2111. },
  2112. streamStmt: streams["tableInPlanner"],
  2113. metaFields: []string{},
  2114. }.Init(),
  2115. },
  2116. },
  2117. Emitters: []string{"tableInPlanner"},
  2118. }.Init(),
  2119. },
  2120. },
  2121. from: &ast.Table{
  2122. Name: "src1",
  2123. },
  2124. joins: []ast.Join{
  2125. {
  2126. Name: "tableInPlanner",
  2127. Alias: "",
  2128. JoinType: ast.INNER_JOIN,
  2129. Expr: &ast.BinaryExpr{
  2130. OP: ast.AND,
  2131. LHS: &ast.BinaryExpr{
  2132. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  2133. OP: ast.EQ,
  2134. RHS: &ast.FieldRef{Name: "id", StreamName: "tableInPlanner"},
  2135. },
  2136. RHS: &ast.BinaryExpr{
  2137. RHS: &ast.BinaryExpr{
  2138. OP: ast.AND,
  2139. LHS: &ast.BinaryExpr{
  2140. OP: ast.GT,
  2141. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  2142. RHS: &ast.IntegerLiteral{Val: 20},
  2143. },
  2144. RHS: &ast.BinaryExpr{
  2145. OP: ast.LT,
  2146. LHS: &ast.FieldRef{Name: "hum", StreamName: "tableInPlanner"},
  2147. RHS: &ast.IntegerLiteral{Val: 60},
  2148. },
  2149. },
  2150. OP: ast.AND,
  2151. LHS: &ast.BinaryExpr{
  2152. OP: ast.GT,
  2153. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  2154. RHS: &ast.IntegerLiteral{Val: 111},
  2155. },
  2156. },
  2157. },
  2158. },
  2159. },
  2160. }.Init(),
  2161. },
  2162. },
  2163. fields: []ast.Field{
  2164. {
  2165. Expr: &ast.FieldRef{Name: "id1", StreamName: ast.DefaultStream},
  2166. Name: "id1",
  2167. AName: ""},
  2168. },
  2169. isAggregate: false,
  2170. sendMeta: false,
  2171. }.Init(),
  2172. }, { // 10 meta
  2173. sql: `SELECT temp, meta(id) AS eid,meta(Humidity->Device) AS hdevice FROM src1 WHERE meta(device)="demo2"`,
  2174. p: ProjectPlan{
  2175. baseLogicalPlan: baseLogicalPlan{
  2176. children: []LogicalPlan{
  2177. FilterPlan{
  2178. baseLogicalPlan: baseLogicalPlan{
  2179. children: []LogicalPlan{
  2180. DataSourcePlan{
  2181. name: "src1",
  2182. streamFields: []interface{}{
  2183. "temp",
  2184. },
  2185. streamStmt: streams["src1"],
  2186. metaFields: []string{"Humidity", "device", "id"},
  2187. }.Init(),
  2188. },
  2189. },
  2190. condition: &ast.BinaryExpr{
  2191. LHS: &ast.Call{
  2192. Name: "meta",
  2193. FuncId: 2,
  2194. Args: []ast.Expr{&ast.MetaRef{
  2195. Name: "device",
  2196. StreamName: ast.DefaultStream,
  2197. }},
  2198. },
  2199. OP: ast.EQ,
  2200. RHS: &ast.StringLiteral{
  2201. Val: "demo2",
  2202. },
  2203. },
  2204. }.Init(),
  2205. },
  2206. },
  2207. fields: []ast.Field{
  2208. {
  2209. Expr: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  2210. Name: "temp",
  2211. AName: "",
  2212. }, {
  2213. Expr: &ast.FieldRef{Name: "eid", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  2214. &ast.Call{Name: "meta", FuncId: 0, Args: []ast.Expr{&ast.MetaRef{
  2215. Name: "id",
  2216. StreamName: ast.DefaultStream,
  2217. }}},
  2218. []ast.StreamName{},
  2219. nil,
  2220. )},
  2221. Name: "meta",
  2222. AName: "eid",
  2223. }, {
  2224. Expr: &ast.FieldRef{Name: "hdevice", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  2225. &ast.Call{Name: "meta", FuncId: 1, Args: []ast.Expr{
  2226. &ast.BinaryExpr{
  2227. OP: ast.ARROW,
  2228. LHS: &ast.MetaRef{Name: "Humidity", StreamName: ast.DefaultStream},
  2229. RHS: &ast.JsonFieldRef{Name: "Device"},
  2230. },
  2231. }},
  2232. []ast.StreamName{},
  2233. nil,
  2234. )},
  2235. Name: "meta",
  2236. AName: "hdevice",
  2237. },
  2238. },
  2239. isAggregate: false,
  2240. sendMeta: false,
  2241. }.Init(),
  2242. }, { // 11 join with same name field and aliased
  2243. sql: `SELECT src2.hum AS hum1, tableInPlanner.hum AS hum2 FROM src2 INNER JOIN tableInPlanner on id2 = id WHERE hum1 > hum2`,
  2244. p: ProjectPlan{
  2245. baseLogicalPlan: baseLogicalPlan{
  2246. children: []LogicalPlan{
  2247. JoinPlan{
  2248. baseLogicalPlan: baseLogicalPlan{
  2249. children: []LogicalPlan{
  2250. JoinAlignPlan{
  2251. baseLogicalPlan: baseLogicalPlan{
  2252. children: []LogicalPlan{
  2253. DataSourcePlan{
  2254. name: "src2",
  2255. streamFields: []interface{}{
  2256. "hum", "id", "id2",
  2257. },
  2258. streamStmt: streams["src2"],
  2259. metaFields: []string{},
  2260. }.Init(),
  2261. DataSourcePlan{
  2262. name: "tableInPlanner",
  2263. streamFields: []interface{}{
  2264. &ast.StreamField{
  2265. Name: "hum",
  2266. FieldType: &ast.BasicType{Type: ast.BIGINT},
  2267. },
  2268. &ast.StreamField{
  2269. Name: "id",
  2270. FieldType: &ast.BasicType{Type: ast.BIGINT},
  2271. },
  2272. },
  2273. streamStmt: streams["tableInPlanner"],
  2274. metaFields: []string{},
  2275. }.Init(),
  2276. },
  2277. },
  2278. Emitters: []string{"tableInPlanner"},
  2279. }.Init(),
  2280. },
  2281. },
  2282. from: &ast.Table{
  2283. Name: "src2",
  2284. },
  2285. joins: []ast.Join{
  2286. {
  2287. Name: "tableInPlanner",
  2288. Alias: "",
  2289. JoinType: ast.INNER_JOIN,
  2290. Expr: &ast.BinaryExpr{
  2291. RHS: &ast.BinaryExpr{
  2292. OP: ast.EQ,
  2293. LHS: &ast.FieldRef{Name: "id2", StreamName: ast.DefaultStream},
  2294. RHS: &ast.FieldRef{Name: "id", StreamName: ast.DefaultStream},
  2295. },
  2296. OP: ast.AND,
  2297. LHS: &ast.BinaryExpr{
  2298. OP: ast.GT,
  2299. LHS: &ast.FieldRef{Name: "hum1", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  2300. &ast.FieldRef{
  2301. Name: "hum",
  2302. StreamName: "src2",
  2303. },
  2304. []ast.StreamName{"src2"},
  2305. &boolFalse,
  2306. )},
  2307. RHS: &ast.FieldRef{Name: "hum2", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  2308. &ast.FieldRef{
  2309. Name: "hum",
  2310. StreamName: "tableInPlanner",
  2311. },
  2312. []ast.StreamName{"tableInPlanner"},
  2313. &boolFalse,
  2314. )},
  2315. },
  2316. },
  2317. },
  2318. },
  2319. }.Init(),
  2320. },
  2321. },
  2322. fields: []ast.Field{
  2323. {
  2324. Expr: &ast.FieldRef{Name: "hum1", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  2325. &ast.FieldRef{
  2326. Name: "hum",
  2327. StreamName: "src2",
  2328. },
  2329. []ast.StreamName{"src2"},
  2330. &boolFalse,
  2331. )},
  2332. Name: "hum",
  2333. AName: "hum1",
  2334. }, {
  2335. Expr: &ast.FieldRef{Name: "hum2", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  2336. &ast.FieldRef{
  2337. Name: "hum",
  2338. StreamName: "tableInPlanner",
  2339. },
  2340. []ast.StreamName{"tableInPlanner"},
  2341. &boolFalse,
  2342. )},
  2343. Name: "hum",
  2344. AName: "hum2",
  2345. },
  2346. },
  2347. isAggregate: false,
  2348. sendMeta: false,
  2349. }.Init(),
  2350. }, { // 12
  2351. sql: `SELECT name->first, name->last FROM src1`,
  2352. p: ProjectPlan{
  2353. baseLogicalPlan: baseLogicalPlan{
  2354. children: []LogicalPlan{
  2355. DataSourcePlan{
  2356. baseLogicalPlan: baseLogicalPlan{},
  2357. name: "src1",
  2358. streamFields: []interface{}{
  2359. "name",
  2360. },
  2361. streamStmt: streams["src1"],
  2362. metaFields: []string{},
  2363. }.Init(),
  2364. },
  2365. },
  2366. fields: []ast.Field{
  2367. {
  2368. Expr: &ast.BinaryExpr{
  2369. OP: ast.ARROW,
  2370. LHS: &ast.FieldRef{StreamName: "src1", Name: "name"},
  2371. RHS: &ast.JsonFieldRef{Name: "first"},
  2372. },
  2373. Name: "kuiper_field_0",
  2374. AName: "",
  2375. }, {
  2376. Expr: &ast.BinaryExpr{
  2377. OP: ast.ARROW,
  2378. LHS: &ast.FieldRef{StreamName: "src1", Name: "name"},
  2379. RHS: &ast.JsonFieldRef{Name: "last"},
  2380. },
  2381. Name: "kuiper_field_1",
  2382. AName: "",
  2383. },
  2384. },
  2385. isAggregate: false,
  2386. sendMeta: false,
  2387. }.Init(),
  2388. },
  2389. }
  2390. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  2391. for i, tt := range tests {
  2392. stmt, err := xsql.NewParser(strings.NewReader(tt.sql)).Parse()
  2393. if err != nil {
  2394. t.Errorf("%d. %q: error compile sql: %s\n", i, tt.sql, err)
  2395. continue
  2396. }
  2397. p, err := createLogicalPlan(stmt, &api.RuleOption{
  2398. IsEventTime: false,
  2399. LateTol: 0,
  2400. Concurrency: 0,
  2401. BufferLength: 0,
  2402. SendMetaToSink: false,
  2403. Qos: 0,
  2404. CheckpointInterval: 0,
  2405. SendError: true,
  2406. }, store)
  2407. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  2408. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.sql, tt.err, err)
  2409. } else if !reflect.DeepEqual(tt.p, p) {
  2410. 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))
  2411. }
  2412. }
  2413. }
  2414. func Test_createLogicalPlan4Lookup(t *testing.T) {
  2415. err, store := store.GetKV("stream")
  2416. if err != nil {
  2417. t.Error(err)
  2418. return
  2419. }
  2420. streamSqls := map[string]string{
  2421. "src1": `CREATE STREAM src1 () WITH (DATASOURCE="src1", FORMAT="json", KEY="ts");`,
  2422. "table1": `CREATE TABLE table1 () WITH (DATASOURCE="table1",TYPE="sql", KIND="lookup");`,
  2423. "table2": `CREATE TABLE table2 () WITH (DATASOURCE="table2",TYPE="sql", KIND="lookup");`,
  2424. }
  2425. types := map[string]ast.StreamType{
  2426. "src1": ast.TypeStream,
  2427. "table1": ast.TypeTable,
  2428. "table2": ast.TypeTable,
  2429. }
  2430. for name, sql := range streamSqls {
  2431. s, err := json.Marshal(&xsql.StreamInfo{
  2432. StreamType: types[name],
  2433. Statement: sql,
  2434. })
  2435. if err != nil {
  2436. t.Error(err)
  2437. t.Fail()
  2438. }
  2439. err = store.Set(name, string(s))
  2440. if err != nil {
  2441. t.Error(err)
  2442. t.Fail()
  2443. }
  2444. }
  2445. streams := make(map[string]*ast.StreamStmt)
  2446. for n := range streamSqls {
  2447. streamStmt, err := xsql.GetDataSource(store, n)
  2448. if err != nil {
  2449. t.Errorf("fail to get stream %s, please check if stream is created", n)
  2450. return
  2451. }
  2452. streams[n] = streamStmt
  2453. }
  2454. var tests = []struct {
  2455. sql string
  2456. p LogicalPlan
  2457. err string
  2458. }{
  2459. { // 0
  2460. sql: `SELECT src1.a, table1.b FROM src1 INNER JOIN table1 ON src1.id = table1.id`,
  2461. p: ProjectPlan{
  2462. baseLogicalPlan: baseLogicalPlan{
  2463. children: []LogicalPlan{
  2464. LookupPlan{
  2465. baseLogicalPlan: baseLogicalPlan{
  2466. children: []LogicalPlan{
  2467. DataSourcePlan{
  2468. baseLogicalPlan: baseLogicalPlan{},
  2469. name: "src1",
  2470. streamFields: []interface{}{
  2471. "a",
  2472. },
  2473. streamStmt: streams["src1"],
  2474. metaFields: []string{},
  2475. }.Init(),
  2476. },
  2477. },
  2478. joinExpr: ast.Join{
  2479. Name: "table1",
  2480. Alias: "",
  2481. JoinType: ast.INNER_JOIN,
  2482. Expr: &ast.BinaryExpr{
  2483. OP: ast.EQ,
  2484. LHS: &ast.FieldRef{
  2485. StreamName: "src1",
  2486. Name: "id",
  2487. },
  2488. RHS: &ast.FieldRef{
  2489. StreamName: "table1",
  2490. Name: "id",
  2491. },
  2492. },
  2493. },
  2494. keys: []string{"id"},
  2495. fields: []string{"b"},
  2496. valvars: []ast.Expr{
  2497. &ast.FieldRef{
  2498. StreamName: "src1",
  2499. Name: "id",
  2500. },
  2501. },
  2502. options: &ast.Options{
  2503. DATASOURCE: "table1",
  2504. TYPE: "sql",
  2505. STRICT_VALIDATION: true,
  2506. KIND: "lookup",
  2507. },
  2508. conditions: nil,
  2509. }.Init(),
  2510. },
  2511. },
  2512. fields: []ast.Field{
  2513. {
  2514. Expr: &ast.FieldRef{
  2515. StreamName: "src1",
  2516. Name: "a",
  2517. },
  2518. Name: "a",
  2519. AName: "",
  2520. },
  2521. {
  2522. Expr: &ast.FieldRef{
  2523. StreamName: "table1",
  2524. Name: "b",
  2525. },
  2526. Name: "b",
  2527. AName: "",
  2528. },
  2529. },
  2530. isAggregate: false,
  2531. sendMeta: false,
  2532. }.Init(),
  2533. },
  2534. { // 1
  2535. sql: `SELECT src1.a, table1.* FROM src1 INNER JOIN table1 ON table1.b > 20 AND src1.c < 40 AND src1.id = table1.id`,
  2536. p: ProjectPlan{
  2537. baseLogicalPlan: baseLogicalPlan{
  2538. children: []LogicalPlan{
  2539. FilterPlan{
  2540. baseLogicalPlan: baseLogicalPlan{
  2541. children: []LogicalPlan{
  2542. LookupPlan{
  2543. baseLogicalPlan: baseLogicalPlan{
  2544. children: []LogicalPlan{
  2545. FilterPlan{
  2546. baseLogicalPlan: baseLogicalPlan{
  2547. children: []LogicalPlan{
  2548. DataSourcePlan{
  2549. baseLogicalPlan: baseLogicalPlan{},
  2550. name: "src1",
  2551. streamFields: []interface{}{
  2552. "a",
  2553. },
  2554. streamStmt: streams["src1"],
  2555. metaFields: []string{},
  2556. }.Init(),
  2557. },
  2558. },
  2559. condition: &ast.BinaryExpr{
  2560. OP: ast.LT,
  2561. LHS: &ast.FieldRef{
  2562. StreamName: "src1",
  2563. Name: "c",
  2564. },
  2565. RHS: &ast.IntegerLiteral{Val: 40},
  2566. },
  2567. }.Init(),
  2568. },
  2569. },
  2570. joinExpr: ast.Join{
  2571. Name: "table1",
  2572. Alias: "",
  2573. JoinType: ast.INNER_JOIN,
  2574. Expr: &ast.BinaryExpr{
  2575. OP: ast.AND,
  2576. RHS: &ast.BinaryExpr{
  2577. OP: ast.EQ,
  2578. LHS: &ast.FieldRef{
  2579. StreamName: "src1",
  2580. Name: "id",
  2581. },
  2582. RHS: &ast.FieldRef{
  2583. StreamName: "table1",
  2584. Name: "id",
  2585. },
  2586. },
  2587. LHS: &ast.BinaryExpr{
  2588. OP: ast.AND,
  2589. LHS: &ast.BinaryExpr{
  2590. OP: ast.GT,
  2591. LHS: &ast.FieldRef{
  2592. StreamName: "table1",
  2593. Name: "b",
  2594. },
  2595. RHS: &ast.IntegerLiteral{Val: 20},
  2596. },
  2597. RHS: &ast.BinaryExpr{
  2598. OP: ast.LT,
  2599. LHS: &ast.FieldRef{
  2600. StreamName: "src1",
  2601. Name: "c",
  2602. },
  2603. RHS: &ast.IntegerLiteral{Val: 40},
  2604. },
  2605. },
  2606. },
  2607. },
  2608. keys: []string{"id"},
  2609. valvars: []ast.Expr{
  2610. &ast.FieldRef{
  2611. StreamName: "src1",
  2612. Name: "id",
  2613. },
  2614. },
  2615. options: &ast.Options{
  2616. DATASOURCE: "table1",
  2617. TYPE: "sql",
  2618. STRICT_VALIDATION: true,
  2619. KIND: "lookup",
  2620. },
  2621. conditions: &ast.BinaryExpr{
  2622. OP: ast.AND,
  2623. LHS: &ast.BinaryExpr{
  2624. OP: ast.GT,
  2625. LHS: &ast.FieldRef{
  2626. StreamName: "table1",
  2627. Name: "b",
  2628. },
  2629. RHS: &ast.IntegerLiteral{Val: 20},
  2630. },
  2631. RHS: &ast.BinaryExpr{
  2632. OP: ast.LT,
  2633. LHS: &ast.FieldRef{
  2634. StreamName: "src1",
  2635. Name: "c",
  2636. },
  2637. RHS: &ast.IntegerLiteral{Val: 40},
  2638. },
  2639. },
  2640. }.Init(),
  2641. },
  2642. },
  2643. condition: &ast.BinaryExpr{
  2644. OP: ast.GT,
  2645. LHS: &ast.FieldRef{
  2646. StreamName: "table1",
  2647. Name: "b",
  2648. },
  2649. RHS: &ast.IntegerLiteral{Val: 20},
  2650. },
  2651. }.Init(),
  2652. },
  2653. },
  2654. fields: []ast.Field{
  2655. {
  2656. Expr: &ast.FieldRef{
  2657. StreamName: "src1",
  2658. Name: "a",
  2659. },
  2660. Name: "a",
  2661. AName: "",
  2662. },
  2663. {
  2664. Expr: &ast.FieldRef{
  2665. StreamName: "table1",
  2666. Name: "*",
  2667. },
  2668. Name: "*",
  2669. AName: "",
  2670. },
  2671. },
  2672. isAggregate: false,
  2673. sendMeta: false,
  2674. }.Init(),
  2675. },
  2676. { // 2
  2677. 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`,
  2678. p: ProjectPlan{
  2679. baseLogicalPlan: baseLogicalPlan{
  2680. children: []LogicalPlan{
  2681. LookupPlan{
  2682. baseLogicalPlan: baseLogicalPlan{
  2683. children: []LogicalPlan{
  2684. LookupPlan{
  2685. baseLogicalPlan: baseLogicalPlan{
  2686. children: []LogicalPlan{
  2687. DataSourcePlan{
  2688. baseLogicalPlan: baseLogicalPlan{},
  2689. name: "src1",
  2690. streamFields: []interface{}{
  2691. "a",
  2692. },
  2693. streamStmt: streams["src1"],
  2694. metaFields: []string{},
  2695. }.Init(),
  2696. },
  2697. },
  2698. joinExpr: ast.Join{
  2699. Name: "table1",
  2700. Alias: "",
  2701. JoinType: ast.INNER_JOIN,
  2702. Expr: &ast.BinaryExpr{
  2703. OP: ast.EQ,
  2704. LHS: &ast.FieldRef{
  2705. StreamName: "src1",
  2706. Name: "id",
  2707. },
  2708. RHS: &ast.FieldRef{
  2709. StreamName: "table1",
  2710. Name: "id",
  2711. },
  2712. },
  2713. },
  2714. keys: []string{"id"},
  2715. fields: []string{"b"},
  2716. valvars: []ast.Expr{
  2717. &ast.FieldRef{
  2718. StreamName: "src1",
  2719. Name: "id",
  2720. },
  2721. },
  2722. options: &ast.Options{
  2723. DATASOURCE: "table1",
  2724. TYPE: "sql",
  2725. STRICT_VALIDATION: true,
  2726. KIND: "lookup",
  2727. },
  2728. conditions: nil,
  2729. }.Init(),
  2730. },
  2731. },
  2732. joinExpr: ast.Join{
  2733. Name: "table2",
  2734. Alias: "",
  2735. JoinType: ast.INNER_JOIN,
  2736. Expr: &ast.BinaryExpr{
  2737. OP: ast.EQ,
  2738. LHS: &ast.FieldRef{
  2739. StreamName: "table1",
  2740. Name: "id",
  2741. },
  2742. RHS: &ast.FieldRef{
  2743. StreamName: "table2",
  2744. Name: "id",
  2745. },
  2746. },
  2747. },
  2748. keys: []string{"id"},
  2749. fields: []string{"c"},
  2750. valvars: []ast.Expr{
  2751. &ast.FieldRef{
  2752. StreamName: "table1",
  2753. Name: "id",
  2754. },
  2755. },
  2756. options: &ast.Options{
  2757. DATASOURCE: "table2",
  2758. TYPE: "sql",
  2759. STRICT_VALIDATION: true,
  2760. KIND: "lookup",
  2761. },
  2762. }.Init(),
  2763. },
  2764. },
  2765. fields: []ast.Field{
  2766. {
  2767. Expr: &ast.FieldRef{
  2768. StreamName: "src1",
  2769. Name: "a",
  2770. },
  2771. Name: "a",
  2772. AName: "",
  2773. },
  2774. {
  2775. Expr: &ast.FieldRef{
  2776. StreamName: "table1",
  2777. Name: "b",
  2778. },
  2779. Name: "b",
  2780. AName: "",
  2781. },
  2782. {
  2783. Expr: &ast.FieldRef{
  2784. StreamName: "table2",
  2785. Name: "c",
  2786. },
  2787. Name: "c",
  2788. AName: "",
  2789. },
  2790. },
  2791. isAggregate: false,
  2792. sendMeta: false,
  2793. }.Init(),
  2794. },
  2795. { // 3
  2796. sql: `SELECT * FROM src1 INNER JOIN table1 ON src1.id = table1.id GROUP BY TUMBLINGWINDOW(ss, 10)`,
  2797. p: ProjectPlan{
  2798. baseLogicalPlan: baseLogicalPlan{
  2799. children: []LogicalPlan{
  2800. LookupPlan{
  2801. baseLogicalPlan: baseLogicalPlan{
  2802. children: []LogicalPlan{
  2803. WindowPlan{
  2804. baseLogicalPlan: baseLogicalPlan{
  2805. children: []LogicalPlan{
  2806. DataSourcePlan{
  2807. baseLogicalPlan: baseLogicalPlan{},
  2808. name: "src1",
  2809. streamStmt: streams["src1"],
  2810. metaFields: []string{},
  2811. isWildCard: true,
  2812. }.Init(),
  2813. },
  2814. },
  2815. condition: nil,
  2816. wtype: ast.TUMBLING_WINDOW,
  2817. length: 10000,
  2818. interval: 0,
  2819. limit: 0,
  2820. }.Init(),
  2821. },
  2822. },
  2823. joinExpr: ast.Join{
  2824. Name: "table1",
  2825. Alias: "",
  2826. JoinType: ast.INNER_JOIN,
  2827. Expr: &ast.BinaryExpr{
  2828. OP: ast.EQ,
  2829. LHS: &ast.FieldRef{
  2830. StreamName: "src1",
  2831. Name: "id",
  2832. },
  2833. RHS: &ast.FieldRef{
  2834. StreamName: "table1",
  2835. Name: "id",
  2836. },
  2837. },
  2838. },
  2839. keys: []string{"id"},
  2840. valvars: []ast.Expr{
  2841. &ast.FieldRef{
  2842. StreamName: "src1",
  2843. Name: "id",
  2844. },
  2845. },
  2846. options: &ast.Options{
  2847. DATASOURCE: "table1",
  2848. TYPE: "sql",
  2849. STRICT_VALIDATION: true,
  2850. KIND: "lookup",
  2851. },
  2852. conditions: nil,
  2853. }.Init(),
  2854. },
  2855. },
  2856. fields: []ast.Field{
  2857. {
  2858. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  2859. Name: "*",
  2860. AName: "",
  2861. },
  2862. },
  2863. isAggregate: false,
  2864. sendMeta: false,
  2865. }.Init(),
  2866. },
  2867. }
  2868. for i, tt := range tests {
  2869. stmt, err := xsql.NewParser(strings.NewReader(tt.sql)).Parse()
  2870. if err != nil {
  2871. t.Errorf("%d. %q: error compile sql: %s\n", i, tt.sql, err)
  2872. continue
  2873. }
  2874. p, err := createLogicalPlan(stmt, &api.RuleOption{
  2875. IsEventTime: false,
  2876. LateTol: 0,
  2877. Concurrency: 0,
  2878. BufferLength: 0,
  2879. SendMetaToSink: false,
  2880. Qos: 0,
  2881. CheckpointInterval: 0,
  2882. SendError: true,
  2883. }, store)
  2884. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  2885. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.sql, tt.err, err)
  2886. } else if !reflect.DeepEqual(tt.p, p) {
  2887. 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))
  2888. }
  2889. }
  2890. }