planner_test.go 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696
  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 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", FuncId: 0, CachedField: "$$a_lag_0", FuncType: ast.FuncTypeScalar, Args: []ast.Expr{&ast.FieldRef{Name: "name", StreamName: "src1"}},
  1132. }, {
  1133. Name: "lag",
  1134. FuncId: 1,
  1135. CachedField: "$$a_lag_1",
  1136. Args: []ast.Expr{&ast.FieldRef{
  1137. Name: "temp",
  1138. StreamName: "src1",
  1139. }},
  1140. },
  1141. },
  1142. }.Init(),
  1143. },
  1144. },
  1145. condition: &ast.BinaryExpr{
  1146. LHS: &ast.Call{
  1147. Name: "lag",
  1148. FuncId: 1,
  1149. Args: []ast.Expr{&ast.FieldRef{
  1150. Name: "temp",
  1151. StreamName: "src1",
  1152. }},
  1153. CachedField: "$$a_lag_1",
  1154. Cached: true,
  1155. },
  1156. OP: ast.GT,
  1157. RHS: &ast.FieldRef{
  1158. Name: "temp",
  1159. StreamName: "src1",
  1160. },
  1161. },
  1162. }.Init(),
  1163. },
  1164. },
  1165. fields: []ast.Field{
  1166. {
  1167. Expr: &ast.Call{Name: "lag", FuncId: 0, FuncType: ast.FuncTypeScalar, Args: []ast.Expr{&ast.FieldRef{Name: "name", StreamName: "src1"}}, CachedField: "$$a_lag_0", Cached: true},
  1168. Name: "lag",
  1169. }, {
  1170. Expr: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  1171. Name: "id1",
  1172. },
  1173. },
  1174. isAggregate: false,
  1175. sendMeta: false,
  1176. }.Init(),
  1177. },
  1178. { // 14
  1179. sql: `SELECT name, *, meta(device) FROM src1`,
  1180. p: ProjectPlan{
  1181. baseLogicalPlan: baseLogicalPlan{
  1182. children: []LogicalPlan{
  1183. DataSourcePlan{
  1184. baseLogicalPlan: baseLogicalPlan{},
  1185. name: "src1",
  1186. streamFields: []interface{}{
  1187. &ast.StreamField{
  1188. Name: "id1",
  1189. FieldType: &ast.BasicType{Type: ast.BIGINT},
  1190. },
  1191. &ast.StreamField{
  1192. Name: "temp",
  1193. FieldType: &ast.BasicType{Type: ast.BIGINT},
  1194. },
  1195. &ast.StreamField{
  1196. Name: "name",
  1197. FieldType: &ast.BasicType{Type: ast.STRINGS},
  1198. },
  1199. &ast.StreamField{
  1200. Name: "myarray",
  1201. FieldType: &ast.ArrayType{Type: ast.STRINGS},
  1202. },
  1203. },
  1204. streamStmt: streams["src1"],
  1205. metaFields: []string{"device"},
  1206. isWildCard: true,
  1207. }.Init(),
  1208. },
  1209. },
  1210. fields: []ast.Field{
  1211. {
  1212. Expr: &ast.FieldRef{Name: "name", StreamName: "src1"},
  1213. Name: "name",
  1214. AName: "",
  1215. },
  1216. {
  1217. Name: "*",
  1218. Expr: &ast.Wildcard{
  1219. Token: ast.ASTERISK,
  1220. },
  1221. },
  1222. {
  1223. Name: "meta",
  1224. Expr: &ast.Call{
  1225. Name: "meta",
  1226. Args: []ast.Expr{
  1227. &ast.MetaRef{
  1228. StreamName: ast.DefaultStream,
  1229. Name: "device",
  1230. },
  1231. },
  1232. },
  1233. },
  1234. },
  1235. isAggregate: false,
  1236. allWildcard: true,
  1237. sendMeta: false,
  1238. }.Init(),
  1239. },
  1240. }
  1241. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  1242. for i, tt := range tests {
  1243. stmt, err := xsql.NewParser(strings.NewReader(tt.sql)).Parse()
  1244. if err != nil {
  1245. t.Errorf("%d. %q: error compile sql: %s\n", i, tt.sql, err)
  1246. continue
  1247. }
  1248. p, err := createLogicalPlan(stmt, &api.RuleOption{
  1249. IsEventTime: false,
  1250. LateTol: 0,
  1251. Concurrency: 0,
  1252. BufferLength: 0,
  1253. SendMetaToSink: false,
  1254. Qos: 0,
  1255. CheckpointInterval: 0,
  1256. SendError: true,
  1257. }, store)
  1258. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  1259. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.sql, tt.err, err)
  1260. } else if !reflect.DeepEqual(tt.p, p) {
  1261. 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))
  1262. }
  1263. }
  1264. }
  1265. func Test_createLogicalPlanSchemaless(t *testing.T) {
  1266. err, store := store.GetKV("stream")
  1267. if err != nil {
  1268. t.Error(err)
  1269. return
  1270. }
  1271. streamSqls := map[string]string{
  1272. "src1": `CREATE STREAM src1 (
  1273. ) WITH (DATASOURCE="src1", FORMAT="json", KEY="ts");`,
  1274. "src2": `CREATE STREAM src2 (
  1275. ) WITH (DATASOURCE="src2", FORMAT="json", KEY="ts");`,
  1276. "tableInPlanner": `CREATE TABLE tableInPlanner (
  1277. id BIGINT,
  1278. name STRING,
  1279. value STRING,
  1280. hum BIGINT
  1281. ) WITH (TYPE="file");`,
  1282. }
  1283. types := map[string]ast.StreamType{
  1284. "src1": ast.TypeStream,
  1285. "src2": ast.TypeStream,
  1286. "tableInPlanner": ast.TypeTable,
  1287. }
  1288. for name, sql := range streamSqls {
  1289. s, err := json.Marshal(&xsql.StreamInfo{
  1290. StreamType: types[name],
  1291. Statement: sql,
  1292. })
  1293. if err != nil {
  1294. t.Error(err)
  1295. t.Fail()
  1296. }
  1297. err = store.Set(name, string(s))
  1298. if err != nil {
  1299. t.Error(err)
  1300. t.Fail()
  1301. }
  1302. }
  1303. streams := make(map[string]*ast.StreamStmt)
  1304. for n := range streamSqls {
  1305. streamStmt, err := xsql.GetDataSource(store, n)
  1306. if err != nil {
  1307. t.Errorf("fail to get stream %s, please check if stream is created", n)
  1308. return
  1309. }
  1310. streams[n] = streamStmt
  1311. }
  1312. var (
  1313. //boolTrue = true
  1314. boolFalse = false
  1315. )
  1316. var tests = []struct {
  1317. sql string
  1318. p LogicalPlan
  1319. err string
  1320. }{
  1321. { // 0
  1322. sql: `SELECT name FROM src1`,
  1323. p: ProjectPlan{
  1324. baseLogicalPlan: baseLogicalPlan{
  1325. children: []LogicalPlan{
  1326. DataSourcePlan{
  1327. baseLogicalPlan: baseLogicalPlan{},
  1328. name: "src1",
  1329. streamFields: []interface{}{
  1330. "name",
  1331. },
  1332. streamStmt: streams["src1"],
  1333. metaFields: []string{},
  1334. }.Init(),
  1335. },
  1336. },
  1337. fields: []ast.Field{
  1338. {
  1339. Expr: &ast.FieldRef{Name: "name", StreamName: "src1"},
  1340. Name: "name",
  1341. AName: "",
  1342. },
  1343. },
  1344. isAggregate: false,
  1345. sendMeta: false,
  1346. }.Init(),
  1347. }, { // 1 optimize where to data source
  1348. sql: `SELECT temp FROM src1 WHERE name = "v1" GROUP BY TUMBLINGWINDOW(ss, 10)`,
  1349. p: ProjectPlan{
  1350. baseLogicalPlan: baseLogicalPlan{
  1351. children: []LogicalPlan{
  1352. WindowPlan{
  1353. baseLogicalPlan: baseLogicalPlan{
  1354. children: []LogicalPlan{
  1355. FilterPlan{
  1356. baseLogicalPlan: baseLogicalPlan{
  1357. children: []LogicalPlan{
  1358. DataSourcePlan{
  1359. name: "src1",
  1360. streamFields: []interface{}{
  1361. "name", "temp",
  1362. },
  1363. streamStmt: streams["src1"],
  1364. metaFields: []string{},
  1365. }.Init(),
  1366. },
  1367. },
  1368. condition: &ast.BinaryExpr{
  1369. LHS: &ast.FieldRef{Name: "name", StreamName: "src1"},
  1370. OP: ast.EQ,
  1371. RHS: &ast.StringLiteral{Val: "v1"},
  1372. },
  1373. }.Init(),
  1374. },
  1375. },
  1376. condition: nil,
  1377. wtype: ast.TUMBLING_WINDOW,
  1378. length: 10000,
  1379. interval: 0,
  1380. limit: 0,
  1381. }.Init(),
  1382. },
  1383. },
  1384. fields: []ast.Field{
  1385. {
  1386. Expr: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  1387. Name: "temp",
  1388. AName: ""},
  1389. },
  1390. isAggregate: false,
  1391. sendMeta: false,
  1392. }.Init(),
  1393. }, { // 2 condition that cannot be optimized
  1394. 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)`,
  1395. p: ProjectPlan{
  1396. baseLogicalPlan: baseLogicalPlan{
  1397. children: []LogicalPlan{
  1398. JoinPlan{
  1399. baseLogicalPlan: baseLogicalPlan{
  1400. children: []LogicalPlan{
  1401. WindowPlan{
  1402. baseLogicalPlan: baseLogicalPlan{
  1403. children: []LogicalPlan{
  1404. DataSourcePlan{
  1405. name: "src1",
  1406. streamFields: []interface{}{
  1407. "id1", "temp",
  1408. },
  1409. streamStmt: streams["src1"],
  1410. metaFields: []string{},
  1411. }.Init(),
  1412. DataSourcePlan{
  1413. name: "src2",
  1414. streamFields: []interface{}{ // can't determine where is id1 belonged to
  1415. "hum", "id1", "id2",
  1416. },
  1417. streamStmt: streams["src2"],
  1418. metaFields: []string{},
  1419. }.Init(),
  1420. },
  1421. },
  1422. condition: nil,
  1423. wtype: ast.TUMBLING_WINDOW,
  1424. length: 10000,
  1425. interval: 0,
  1426. limit: 0,
  1427. }.Init(),
  1428. },
  1429. },
  1430. from: &ast.Table{Name: "src1"},
  1431. joins: ast.Joins{ast.Join{
  1432. Name: "src2",
  1433. JoinType: ast.INNER_JOIN,
  1434. Expr: &ast.BinaryExpr{
  1435. OP: ast.AND,
  1436. LHS: &ast.BinaryExpr{
  1437. LHS: &ast.BinaryExpr{
  1438. OP: ast.GT,
  1439. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  1440. RHS: &ast.IntegerLiteral{Val: 20},
  1441. },
  1442. OP: ast.OR,
  1443. RHS: &ast.BinaryExpr{
  1444. OP: ast.GT,
  1445. LHS: &ast.FieldRef{Name: "hum", StreamName: "src2"},
  1446. RHS: &ast.IntegerLiteral{Val: 60},
  1447. },
  1448. },
  1449. RHS: &ast.BinaryExpr{
  1450. OP: ast.EQ,
  1451. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  1452. RHS: &ast.FieldRef{Name: "id2", StreamName: "src2"},
  1453. },
  1454. },
  1455. }},
  1456. }.Init(),
  1457. },
  1458. },
  1459. fields: []ast.Field{
  1460. {
  1461. Expr: &ast.FieldRef{Name: "id1", StreamName: ast.DefaultStream},
  1462. Name: "id1",
  1463. AName: ""},
  1464. },
  1465. isAggregate: false,
  1466. sendMeta: false,
  1467. }.Init(),
  1468. }, { // 3 optimize window filter
  1469. sql: `SELECT id1 FROM src1 WHERE name = "v1" GROUP BY TUMBLINGWINDOW(ss, 10) FILTER( WHERE temp > 2)`,
  1470. p: ProjectPlan{
  1471. baseLogicalPlan: baseLogicalPlan{
  1472. children: []LogicalPlan{
  1473. WindowPlan{
  1474. baseLogicalPlan: baseLogicalPlan{
  1475. children: []LogicalPlan{
  1476. FilterPlan{
  1477. baseLogicalPlan: baseLogicalPlan{
  1478. children: []LogicalPlan{
  1479. DataSourcePlan{
  1480. name: "src1",
  1481. streamFields: []interface{}{
  1482. "id1", "name", "temp",
  1483. },
  1484. streamStmt: streams["src1"],
  1485. metaFields: []string{},
  1486. }.Init(),
  1487. },
  1488. },
  1489. condition: &ast.BinaryExpr{
  1490. OP: ast.AND,
  1491. LHS: &ast.BinaryExpr{
  1492. LHS: &ast.FieldRef{Name: "name", StreamName: "src1"},
  1493. OP: ast.EQ,
  1494. RHS: &ast.StringLiteral{Val: "v1"},
  1495. },
  1496. RHS: &ast.BinaryExpr{
  1497. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  1498. OP: ast.GT,
  1499. RHS: &ast.IntegerLiteral{Val: 2},
  1500. },
  1501. },
  1502. }.Init(),
  1503. },
  1504. },
  1505. condition: nil,
  1506. wtype: ast.TUMBLING_WINDOW,
  1507. length: 10000,
  1508. interval: 0,
  1509. limit: 0,
  1510. }.Init(),
  1511. },
  1512. },
  1513. fields: []ast.Field{
  1514. {
  1515. Expr: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  1516. Name: "id1",
  1517. AName: ""},
  1518. },
  1519. isAggregate: false,
  1520. sendMeta: false,
  1521. }.Init(),
  1522. }, { // 4. do not optimize count window
  1523. sql: `SELECT * FROM src1 WHERE temp > 20 GROUP BY COUNTWINDOW(5,1) HAVING COUNT(*) > 2`,
  1524. p: ProjectPlan{
  1525. baseLogicalPlan: baseLogicalPlan{
  1526. children: []LogicalPlan{
  1527. HavingPlan{
  1528. baseLogicalPlan: baseLogicalPlan{
  1529. children: []LogicalPlan{
  1530. FilterPlan{
  1531. baseLogicalPlan: baseLogicalPlan{
  1532. children: []LogicalPlan{
  1533. WindowPlan{
  1534. baseLogicalPlan: baseLogicalPlan{
  1535. children: []LogicalPlan{
  1536. DataSourcePlan{
  1537. name: "src1",
  1538. isWildCard: true,
  1539. streamFields: nil,
  1540. streamStmt: streams["src1"],
  1541. metaFields: []string{},
  1542. }.Init(),
  1543. },
  1544. },
  1545. condition: nil,
  1546. wtype: ast.COUNT_WINDOW,
  1547. length: 5,
  1548. interval: 1,
  1549. limit: 0,
  1550. }.Init(),
  1551. },
  1552. },
  1553. condition: &ast.BinaryExpr{
  1554. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  1555. OP: ast.GT,
  1556. RHS: &ast.IntegerLiteral{Val: 20},
  1557. },
  1558. }.Init(),
  1559. },
  1560. },
  1561. condition: &ast.BinaryExpr{
  1562. LHS: &ast.Call{Name: "count", FuncId: 0, Args: []ast.Expr{&ast.Wildcard{
  1563. Token: ast.ASTERISK,
  1564. }}, FuncType: ast.FuncTypeAgg},
  1565. OP: ast.GT,
  1566. RHS: &ast.IntegerLiteral{Val: 2},
  1567. },
  1568. }.Init(),
  1569. },
  1570. },
  1571. fields: []ast.Field{
  1572. {
  1573. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  1574. Name: "*",
  1575. AName: ""},
  1576. },
  1577. isAggregate: false,
  1578. sendMeta: false,
  1579. }.Init(),
  1580. }, { // 5. optimize join on
  1581. 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)`,
  1582. p: ProjectPlan{
  1583. baseLogicalPlan: baseLogicalPlan{
  1584. children: []LogicalPlan{
  1585. JoinPlan{
  1586. baseLogicalPlan: baseLogicalPlan{
  1587. children: []LogicalPlan{
  1588. WindowPlan{
  1589. baseLogicalPlan: baseLogicalPlan{
  1590. children: []LogicalPlan{
  1591. FilterPlan{
  1592. baseLogicalPlan: baseLogicalPlan{
  1593. children: []LogicalPlan{
  1594. DataSourcePlan{
  1595. name: "src1",
  1596. streamFields: []interface{}{
  1597. "id1", "temp",
  1598. },
  1599. streamStmt: streams["src1"],
  1600. metaFields: []string{},
  1601. }.Init(),
  1602. },
  1603. },
  1604. condition: &ast.BinaryExpr{
  1605. RHS: &ast.BinaryExpr{
  1606. OP: ast.GT,
  1607. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  1608. RHS: &ast.IntegerLiteral{Val: 20},
  1609. },
  1610. OP: ast.AND,
  1611. LHS: &ast.BinaryExpr{
  1612. OP: ast.GT,
  1613. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  1614. RHS: &ast.IntegerLiteral{Val: 111},
  1615. },
  1616. },
  1617. }.Init(),
  1618. FilterPlan{
  1619. baseLogicalPlan: baseLogicalPlan{
  1620. children: []LogicalPlan{
  1621. DataSourcePlan{
  1622. name: "src2",
  1623. streamFields: []interface{}{
  1624. "hum", "id1", "id2",
  1625. },
  1626. streamStmt: streams["src2"],
  1627. metaFields: []string{},
  1628. }.Init(),
  1629. },
  1630. },
  1631. condition: &ast.BinaryExpr{
  1632. OP: ast.LT,
  1633. LHS: &ast.FieldRef{Name: "hum", StreamName: "src2"},
  1634. RHS: &ast.IntegerLiteral{Val: 60},
  1635. },
  1636. }.Init(),
  1637. },
  1638. },
  1639. condition: nil,
  1640. wtype: ast.TUMBLING_WINDOW,
  1641. length: 10000,
  1642. interval: 0,
  1643. limit: 0,
  1644. }.Init(),
  1645. },
  1646. },
  1647. from: &ast.Table{
  1648. Name: "src1",
  1649. },
  1650. joins: []ast.Join{
  1651. {
  1652. Name: "src2",
  1653. Alias: "",
  1654. JoinType: ast.INNER_JOIN,
  1655. Expr: &ast.BinaryExpr{
  1656. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  1657. OP: ast.EQ,
  1658. RHS: &ast.FieldRef{Name: "id2", StreamName: "src2"},
  1659. },
  1660. },
  1661. },
  1662. }.Init(),
  1663. },
  1664. },
  1665. fields: []ast.Field{
  1666. {
  1667. Expr: &ast.FieldRef{Name: "id1", StreamName: ast.DefaultStream},
  1668. Name: "id1",
  1669. AName: ""},
  1670. },
  1671. isAggregate: false,
  1672. sendMeta: false,
  1673. }.Init(),
  1674. }, { // 6. optimize outter join on
  1675. 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)`,
  1676. p: ProjectPlan{
  1677. baseLogicalPlan: baseLogicalPlan{
  1678. children: []LogicalPlan{
  1679. JoinPlan{
  1680. baseLogicalPlan: baseLogicalPlan{
  1681. children: []LogicalPlan{
  1682. WindowPlan{
  1683. baseLogicalPlan: baseLogicalPlan{
  1684. children: []LogicalPlan{
  1685. FilterPlan{
  1686. baseLogicalPlan: baseLogicalPlan{
  1687. children: []LogicalPlan{
  1688. DataSourcePlan{
  1689. name: "src1",
  1690. streamFields: []interface{}{
  1691. "id1", "temp",
  1692. },
  1693. streamStmt: streams["src1"],
  1694. metaFields: []string{},
  1695. }.Init(),
  1696. },
  1697. },
  1698. condition: &ast.BinaryExpr{
  1699. OP: ast.GT,
  1700. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  1701. RHS: &ast.IntegerLiteral{Val: 111},
  1702. },
  1703. }.Init(),
  1704. DataSourcePlan{
  1705. name: "src2",
  1706. streamFields: []interface{}{
  1707. "hum", "id1", "id2",
  1708. },
  1709. streamStmt: streams["src2"],
  1710. metaFields: []string{},
  1711. }.Init(),
  1712. },
  1713. },
  1714. condition: nil,
  1715. wtype: ast.TUMBLING_WINDOW,
  1716. length: 10000,
  1717. interval: 0,
  1718. limit: 0,
  1719. }.Init(),
  1720. },
  1721. },
  1722. from: &ast.Table{
  1723. Name: "src1",
  1724. },
  1725. joins: []ast.Join{
  1726. {
  1727. Name: "src2",
  1728. Alias: "",
  1729. JoinType: ast.FULL_JOIN,
  1730. Expr: &ast.BinaryExpr{
  1731. OP: ast.AND,
  1732. LHS: &ast.BinaryExpr{
  1733. OP: ast.AND,
  1734. LHS: &ast.BinaryExpr{
  1735. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  1736. OP: ast.EQ,
  1737. RHS: &ast.FieldRef{Name: "id2", StreamName: "src2"},
  1738. },
  1739. RHS: &ast.BinaryExpr{
  1740. OP: ast.GT,
  1741. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  1742. RHS: &ast.IntegerLiteral{Val: 20},
  1743. },
  1744. },
  1745. RHS: &ast.BinaryExpr{
  1746. OP: ast.LT,
  1747. LHS: &ast.FieldRef{Name: "hum", StreamName: "src2"},
  1748. RHS: &ast.IntegerLiteral{Val: 60},
  1749. },
  1750. },
  1751. },
  1752. },
  1753. }.Init(),
  1754. },
  1755. },
  1756. fields: []ast.Field{
  1757. {
  1758. Expr: &ast.FieldRef{Name: "id1", StreamName: ast.DefaultStream},
  1759. Name: "id1",
  1760. AName: ""},
  1761. },
  1762. isAggregate: false,
  1763. sendMeta: false,
  1764. }.Init(),
  1765. }, { // 7 window error for table
  1766. sql: `SELECT value FROM tableInPlanner WHERE name = "v1" GROUP BY TUMBLINGWINDOW(ss, 10)`,
  1767. p: nil,
  1768. err: "cannot run window for TABLE sources",
  1769. }, { // 8 join table without window
  1770. sql: `SELECT id1 FROM src1 INNER JOIN tableInPlanner on src1.id1 = tableInPlanner.id and src1.temp > 20 and hum < 60 WHERE src1.id1 > 111`,
  1771. p: ProjectPlan{
  1772. baseLogicalPlan: baseLogicalPlan{
  1773. children: []LogicalPlan{
  1774. JoinPlan{
  1775. baseLogicalPlan: baseLogicalPlan{
  1776. children: []LogicalPlan{
  1777. JoinAlignPlan{
  1778. baseLogicalPlan: baseLogicalPlan{
  1779. children: []LogicalPlan{
  1780. FilterPlan{
  1781. baseLogicalPlan: baseLogicalPlan{
  1782. children: []LogicalPlan{
  1783. DataSourcePlan{
  1784. name: "src1",
  1785. streamFields: []interface{}{
  1786. "hum", "id1", "temp",
  1787. },
  1788. streamStmt: streams["src1"],
  1789. metaFields: []string{},
  1790. }.Init(),
  1791. },
  1792. },
  1793. condition: &ast.BinaryExpr{
  1794. RHS: &ast.BinaryExpr{
  1795. OP: ast.GT,
  1796. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  1797. RHS: &ast.IntegerLiteral{Val: 20},
  1798. },
  1799. OP: ast.AND,
  1800. LHS: &ast.BinaryExpr{
  1801. OP: ast.GT,
  1802. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  1803. RHS: &ast.IntegerLiteral{Val: 111},
  1804. },
  1805. },
  1806. }.Init(),
  1807. DataSourcePlan{
  1808. name: "tableInPlanner",
  1809. streamFields: []interface{}{
  1810. &ast.StreamField{
  1811. Name: "hum",
  1812. FieldType: &ast.BasicType{Type: ast.BIGINT},
  1813. },
  1814. &ast.StreamField{
  1815. Name: "id",
  1816. FieldType: &ast.BasicType{Type: ast.BIGINT},
  1817. },
  1818. },
  1819. streamStmt: streams["tableInPlanner"],
  1820. metaFields: []string{},
  1821. }.Init(),
  1822. },
  1823. },
  1824. Emitters: []string{"tableInPlanner"},
  1825. }.Init(),
  1826. },
  1827. },
  1828. from: &ast.Table{
  1829. Name: "src1",
  1830. },
  1831. joins: []ast.Join{
  1832. {
  1833. Name: "tableInPlanner",
  1834. Alias: "",
  1835. JoinType: ast.INNER_JOIN,
  1836. Expr: &ast.BinaryExpr{
  1837. OP: ast.AND,
  1838. LHS: &ast.BinaryExpr{
  1839. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  1840. OP: ast.EQ,
  1841. RHS: &ast.FieldRef{Name: "id", StreamName: "tableInPlanner"},
  1842. },
  1843. RHS: &ast.BinaryExpr{
  1844. OP: ast.LT,
  1845. LHS: &ast.FieldRef{Name: "hum", StreamName: ast.DefaultStream},
  1846. RHS: &ast.IntegerLiteral{Val: 60},
  1847. },
  1848. },
  1849. },
  1850. },
  1851. }.Init(),
  1852. },
  1853. },
  1854. fields: []ast.Field{
  1855. {
  1856. Expr: &ast.FieldRef{Name: "id1", StreamName: ast.DefaultStream},
  1857. Name: "id1",
  1858. AName: ""},
  1859. },
  1860. isAggregate: false,
  1861. sendMeta: false,
  1862. }.Init(),
  1863. }, { // 9 join table with window
  1864. 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)`,
  1865. p: ProjectPlan{
  1866. baseLogicalPlan: baseLogicalPlan{
  1867. children: []LogicalPlan{
  1868. JoinPlan{
  1869. baseLogicalPlan: baseLogicalPlan{
  1870. children: []LogicalPlan{
  1871. JoinAlignPlan{
  1872. baseLogicalPlan: baseLogicalPlan{
  1873. children: []LogicalPlan{
  1874. WindowPlan{
  1875. baseLogicalPlan: baseLogicalPlan{
  1876. children: []LogicalPlan{
  1877. DataSourcePlan{
  1878. name: "src1",
  1879. streamFields: []interface{}{
  1880. "id1", "temp",
  1881. },
  1882. streamStmt: streams["src1"],
  1883. metaFields: []string{},
  1884. }.Init(),
  1885. },
  1886. },
  1887. condition: nil,
  1888. wtype: ast.TUMBLING_WINDOW,
  1889. length: 10000,
  1890. interval: 0,
  1891. limit: 0,
  1892. }.Init(),
  1893. DataSourcePlan{
  1894. name: "tableInPlanner",
  1895. streamFields: []interface{}{
  1896. &ast.StreamField{
  1897. Name: "hum",
  1898. FieldType: &ast.BasicType{Type: ast.BIGINT},
  1899. },
  1900. &ast.StreamField{
  1901. Name: "id",
  1902. FieldType: &ast.BasicType{Type: ast.BIGINT},
  1903. },
  1904. },
  1905. streamStmt: streams["tableInPlanner"],
  1906. metaFields: []string{},
  1907. }.Init(),
  1908. },
  1909. },
  1910. Emitters: []string{"tableInPlanner"},
  1911. }.Init(),
  1912. },
  1913. },
  1914. from: &ast.Table{
  1915. Name: "src1",
  1916. },
  1917. joins: []ast.Join{
  1918. {
  1919. Name: "tableInPlanner",
  1920. Alias: "",
  1921. JoinType: ast.INNER_JOIN,
  1922. Expr: &ast.BinaryExpr{
  1923. OP: ast.AND,
  1924. LHS: &ast.BinaryExpr{
  1925. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  1926. OP: ast.EQ,
  1927. RHS: &ast.FieldRef{Name: "id", StreamName: "tableInPlanner"},
  1928. },
  1929. RHS: &ast.BinaryExpr{
  1930. RHS: &ast.BinaryExpr{
  1931. OP: ast.AND,
  1932. LHS: &ast.BinaryExpr{
  1933. OP: ast.GT,
  1934. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  1935. RHS: &ast.IntegerLiteral{Val: 20},
  1936. },
  1937. RHS: &ast.BinaryExpr{
  1938. OP: ast.LT,
  1939. LHS: &ast.FieldRef{Name: "hum", StreamName: "tableInPlanner"},
  1940. RHS: &ast.IntegerLiteral{Val: 60},
  1941. },
  1942. },
  1943. OP: ast.AND,
  1944. LHS: &ast.BinaryExpr{
  1945. OP: ast.GT,
  1946. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  1947. RHS: &ast.IntegerLiteral{Val: 111},
  1948. },
  1949. },
  1950. },
  1951. },
  1952. },
  1953. }.Init(),
  1954. },
  1955. },
  1956. fields: []ast.Field{
  1957. {
  1958. Expr: &ast.FieldRef{Name: "id1", StreamName: ast.DefaultStream},
  1959. Name: "id1",
  1960. AName: ""},
  1961. },
  1962. isAggregate: false,
  1963. sendMeta: false,
  1964. }.Init(),
  1965. }, { // 10 meta
  1966. sql: `SELECT temp, meta(id) AS eid,meta(Humidity->Device) AS hdevice FROM src1 WHERE meta(device)="demo2"`,
  1967. p: ProjectPlan{
  1968. baseLogicalPlan: baseLogicalPlan{
  1969. children: []LogicalPlan{
  1970. FilterPlan{
  1971. baseLogicalPlan: baseLogicalPlan{
  1972. children: []LogicalPlan{
  1973. DataSourcePlan{
  1974. name: "src1",
  1975. streamFields: []interface{}{
  1976. "temp",
  1977. },
  1978. streamStmt: streams["src1"],
  1979. metaFields: []string{"Humidity", "device", "id"},
  1980. }.Init(),
  1981. },
  1982. },
  1983. condition: &ast.BinaryExpr{
  1984. LHS: &ast.Call{
  1985. Name: "meta",
  1986. FuncId: 2,
  1987. Args: []ast.Expr{&ast.MetaRef{
  1988. Name: "device",
  1989. StreamName: ast.DefaultStream,
  1990. }},
  1991. },
  1992. OP: ast.EQ,
  1993. RHS: &ast.StringLiteral{
  1994. Val: "demo2",
  1995. },
  1996. },
  1997. }.Init(),
  1998. },
  1999. },
  2000. fields: []ast.Field{
  2001. {
  2002. Expr: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  2003. Name: "temp",
  2004. AName: "",
  2005. }, {
  2006. Expr: &ast.FieldRef{Name: "eid", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  2007. &ast.Call{Name: "meta", FuncId: 0, Args: []ast.Expr{&ast.MetaRef{
  2008. Name: "id",
  2009. StreamName: ast.DefaultStream,
  2010. }}},
  2011. []ast.StreamName{},
  2012. nil,
  2013. )},
  2014. Name: "meta",
  2015. AName: "eid",
  2016. }, {
  2017. Expr: &ast.FieldRef{Name: "hdevice", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  2018. &ast.Call{Name: "meta", FuncId: 1, Args: []ast.Expr{
  2019. &ast.BinaryExpr{
  2020. OP: ast.ARROW,
  2021. LHS: &ast.MetaRef{Name: "Humidity", StreamName: ast.DefaultStream},
  2022. RHS: &ast.JsonFieldRef{Name: "Device"},
  2023. },
  2024. }},
  2025. []ast.StreamName{},
  2026. nil,
  2027. )},
  2028. Name: "meta",
  2029. AName: "hdevice",
  2030. },
  2031. },
  2032. isAggregate: false,
  2033. sendMeta: false,
  2034. }.Init(),
  2035. }, { // 11 join with same name field and aliased
  2036. sql: `SELECT src2.hum AS hum1, tableInPlanner.hum AS hum2 FROM src2 INNER JOIN tableInPlanner on id2 = id WHERE hum1 > hum2`,
  2037. p: ProjectPlan{
  2038. baseLogicalPlan: baseLogicalPlan{
  2039. children: []LogicalPlan{
  2040. JoinPlan{
  2041. baseLogicalPlan: baseLogicalPlan{
  2042. children: []LogicalPlan{
  2043. JoinAlignPlan{
  2044. baseLogicalPlan: baseLogicalPlan{
  2045. children: []LogicalPlan{
  2046. DataSourcePlan{
  2047. name: "src2",
  2048. streamFields: []interface{}{
  2049. "hum", "id", "id2",
  2050. },
  2051. streamStmt: streams["src2"],
  2052. metaFields: []string{},
  2053. }.Init(),
  2054. DataSourcePlan{
  2055. name: "tableInPlanner",
  2056. streamFields: []interface{}{
  2057. &ast.StreamField{
  2058. Name: "hum",
  2059. FieldType: &ast.BasicType{Type: ast.BIGINT},
  2060. },
  2061. &ast.StreamField{
  2062. Name: "id",
  2063. FieldType: &ast.BasicType{Type: ast.BIGINT},
  2064. },
  2065. },
  2066. streamStmt: streams["tableInPlanner"],
  2067. metaFields: []string{},
  2068. }.Init(),
  2069. },
  2070. },
  2071. Emitters: []string{"tableInPlanner"},
  2072. }.Init(),
  2073. },
  2074. },
  2075. from: &ast.Table{
  2076. Name: "src2",
  2077. },
  2078. joins: []ast.Join{
  2079. {
  2080. Name: "tableInPlanner",
  2081. Alias: "",
  2082. JoinType: ast.INNER_JOIN,
  2083. Expr: &ast.BinaryExpr{
  2084. RHS: &ast.BinaryExpr{
  2085. OP: ast.EQ,
  2086. LHS: &ast.FieldRef{Name: "id2", StreamName: ast.DefaultStream},
  2087. RHS: &ast.FieldRef{Name: "id", StreamName: ast.DefaultStream},
  2088. },
  2089. OP: ast.AND,
  2090. LHS: &ast.BinaryExpr{
  2091. OP: ast.GT,
  2092. LHS: &ast.FieldRef{Name: "hum1", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  2093. &ast.FieldRef{
  2094. Name: "hum",
  2095. StreamName: "src2",
  2096. },
  2097. []ast.StreamName{"src2"},
  2098. &boolFalse,
  2099. )},
  2100. RHS: &ast.FieldRef{Name: "hum2", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  2101. &ast.FieldRef{
  2102. Name: "hum",
  2103. StreamName: "tableInPlanner",
  2104. },
  2105. []ast.StreamName{"tableInPlanner"},
  2106. &boolFalse,
  2107. )},
  2108. },
  2109. },
  2110. },
  2111. },
  2112. }.Init(),
  2113. },
  2114. },
  2115. fields: []ast.Field{
  2116. {
  2117. Expr: &ast.FieldRef{Name: "hum1", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  2118. &ast.FieldRef{
  2119. Name: "hum",
  2120. StreamName: "src2",
  2121. },
  2122. []ast.StreamName{"src2"},
  2123. &boolFalse,
  2124. )},
  2125. Name: "hum",
  2126. AName: "hum1",
  2127. }, {
  2128. Expr: &ast.FieldRef{Name: "hum2", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  2129. &ast.FieldRef{
  2130. Name: "hum",
  2131. StreamName: "tableInPlanner",
  2132. },
  2133. []ast.StreamName{"tableInPlanner"},
  2134. &boolFalse,
  2135. )},
  2136. Name: "hum",
  2137. AName: "hum2",
  2138. },
  2139. },
  2140. isAggregate: false,
  2141. sendMeta: false,
  2142. }.Init(),
  2143. }, { // 12
  2144. sql: `SELECT name->first, name->last FROM src1`,
  2145. p: ProjectPlan{
  2146. baseLogicalPlan: baseLogicalPlan{
  2147. children: []LogicalPlan{
  2148. DataSourcePlan{
  2149. baseLogicalPlan: baseLogicalPlan{},
  2150. name: "src1",
  2151. streamFields: []interface{}{
  2152. "name",
  2153. },
  2154. streamStmt: streams["src1"],
  2155. metaFields: []string{},
  2156. }.Init(),
  2157. },
  2158. },
  2159. fields: []ast.Field{
  2160. {
  2161. Expr: &ast.BinaryExpr{
  2162. OP: ast.ARROW,
  2163. LHS: &ast.FieldRef{StreamName: "src1", Name: "name"},
  2164. RHS: &ast.JsonFieldRef{Name: "first"},
  2165. },
  2166. Name: "kuiper_field_0",
  2167. AName: "",
  2168. }, {
  2169. Expr: &ast.BinaryExpr{
  2170. OP: ast.ARROW,
  2171. LHS: &ast.FieldRef{StreamName: "src1", Name: "name"},
  2172. RHS: &ast.JsonFieldRef{Name: "last"},
  2173. },
  2174. Name: "kuiper_field_1",
  2175. AName: "",
  2176. },
  2177. },
  2178. isAggregate: false,
  2179. sendMeta: false,
  2180. }.Init(),
  2181. },
  2182. }
  2183. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  2184. for i, tt := range tests {
  2185. stmt, err := xsql.NewParser(strings.NewReader(tt.sql)).Parse()
  2186. if err != nil {
  2187. t.Errorf("%d. %q: error compile sql: %s\n", i, tt.sql, err)
  2188. continue
  2189. }
  2190. p, err := createLogicalPlan(stmt, &api.RuleOption{
  2191. IsEventTime: false,
  2192. LateTol: 0,
  2193. Concurrency: 0,
  2194. BufferLength: 0,
  2195. SendMetaToSink: false,
  2196. Qos: 0,
  2197. CheckpointInterval: 0,
  2198. SendError: true,
  2199. }, store)
  2200. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  2201. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.sql, tt.err, err)
  2202. } else if !reflect.DeepEqual(tt.p, p) {
  2203. 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))
  2204. }
  2205. }
  2206. }
  2207. func Test_createLogicalPlan4Lookup(t *testing.T) {
  2208. err, store := store.GetKV("stream")
  2209. if err != nil {
  2210. t.Error(err)
  2211. return
  2212. }
  2213. streamSqls := map[string]string{
  2214. "src1": `CREATE STREAM src1 () WITH (DATASOURCE="src1", FORMAT="json", KEY="ts");`,
  2215. "table1": `CREATE TABLE table1 () WITH (DATASOURCE="table1",TYPE="sql", KIND="lookup");`,
  2216. "table2": `CREATE TABLE table2 () WITH (DATASOURCE="table2",TYPE="sql", KIND="lookup");`,
  2217. }
  2218. types := map[string]ast.StreamType{
  2219. "src1": ast.TypeStream,
  2220. "table1": ast.TypeTable,
  2221. "table2": ast.TypeTable,
  2222. }
  2223. for name, sql := range streamSqls {
  2224. s, err := json.Marshal(&xsql.StreamInfo{
  2225. StreamType: types[name],
  2226. Statement: sql,
  2227. })
  2228. if err != nil {
  2229. t.Error(err)
  2230. t.Fail()
  2231. }
  2232. err = store.Set(name, string(s))
  2233. if err != nil {
  2234. t.Error(err)
  2235. t.Fail()
  2236. }
  2237. }
  2238. streams := make(map[string]*ast.StreamStmt)
  2239. for n := range streamSqls {
  2240. streamStmt, err := xsql.GetDataSource(store, n)
  2241. if err != nil {
  2242. t.Errorf("fail to get stream %s, please check if stream is created", n)
  2243. return
  2244. }
  2245. streams[n] = streamStmt
  2246. }
  2247. var tests = []struct {
  2248. sql string
  2249. p LogicalPlan
  2250. err string
  2251. }{
  2252. { // 0
  2253. sql: `SELECT src1.a, table1.b FROM src1 INNER JOIN table1 ON src1.id = table1.id`,
  2254. p: ProjectPlan{
  2255. baseLogicalPlan: baseLogicalPlan{
  2256. children: []LogicalPlan{
  2257. LookupPlan{
  2258. baseLogicalPlan: baseLogicalPlan{
  2259. children: []LogicalPlan{
  2260. DataSourcePlan{
  2261. baseLogicalPlan: baseLogicalPlan{},
  2262. name: "src1",
  2263. streamFields: []interface{}{
  2264. "a",
  2265. },
  2266. streamStmt: streams["src1"],
  2267. metaFields: []string{},
  2268. }.Init(),
  2269. },
  2270. },
  2271. joinExpr: ast.Join{
  2272. Name: "table1",
  2273. Alias: "",
  2274. JoinType: ast.INNER_JOIN,
  2275. Expr: &ast.BinaryExpr{
  2276. OP: ast.EQ,
  2277. LHS: &ast.FieldRef{
  2278. StreamName: "src1",
  2279. Name: "id",
  2280. },
  2281. RHS: &ast.FieldRef{
  2282. StreamName: "table1",
  2283. Name: "id",
  2284. },
  2285. },
  2286. },
  2287. keys: []string{"id"},
  2288. fields: []string{"b"},
  2289. valvars: []ast.Expr{
  2290. &ast.FieldRef{
  2291. StreamName: "src1",
  2292. Name: "id",
  2293. },
  2294. },
  2295. options: &ast.Options{
  2296. DATASOURCE: "table1",
  2297. TYPE: "sql",
  2298. STRICT_VALIDATION: true,
  2299. KIND: "lookup",
  2300. },
  2301. conditions: nil,
  2302. }.Init(),
  2303. },
  2304. },
  2305. fields: []ast.Field{
  2306. {
  2307. Expr: &ast.FieldRef{
  2308. StreamName: "src1",
  2309. Name: "a",
  2310. },
  2311. Name: "a",
  2312. AName: "",
  2313. },
  2314. {
  2315. Expr: &ast.FieldRef{
  2316. StreamName: "table1",
  2317. Name: "b",
  2318. },
  2319. Name: "b",
  2320. AName: "",
  2321. },
  2322. },
  2323. isAggregate: false,
  2324. sendMeta: false,
  2325. }.Init(),
  2326. },
  2327. { // 1
  2328. sql: `SELECT src1.a, table1.* FROM src1 INNER JOIN table1 ON table1.b > 20 AND src1.c < 40 AND src1.id = table1.id`,
  2329. p: ProjectPlan{
  2330. baseLogicalPlan: baseLogicalPlan{
  2331. children: []LogicalPlan{
  2332. FilterPlan{
  2333. baseLogicalPlan: baseLogicalPlan{
  2334. children: []LogicalPlan{
  2335. LookupPlan{
  2336. baseLogicalPlan: baseLogicalPlan{
  2337. children: []LogicalPlan{
  2338. FilterPlan{
  2339. baseLogicalPlan: baseLogicalPlan{
  2340. children: []LogicalPlan{
  2341. DataSourcePlan{
  2342. baseLogicalPlan: baseLogicalPlan{},
  2343. name: "src1",
  2344. streamFields: []interface{}{
  2345. "a",
  2346. },
  2347. streamStmt: streams["src1"],
  2348. metaFields: []string{},
  2349. }.Init(),
  2350. },
  2351. },
  2352. condition: &ast.BinaryExpr{
  2353. OP: ast.LT,
  2354. LHS: &ast.FieldRef{
  2355. StreamName: "src1",
  2356. Name: "c",
  2357. },
  2358. RHS: &ast.IntegerLiteral{Val: 40},
  2359. },
  2360. }.Init(),
  2361. },
  2362. },
  2363. joinExpr: ast.Join{
  2364. Name: "table1",
  2365. Alias: "",
  2366. JoinType: ast.INNER_JOIN,
  2367. Expr: &ast.BinaryExpr{
  2368. OP: ast.AND,
  2369. RHS: &ast.BinaryExpr{
  2370. OP: ast.EQ,
  2371. LHS: &ast.FieldRef{
  2372. StreamName: "src1",
  2373. Name: "id",
  2374. },
  2375. RHS: &ast.FieldRef{
  2376. StreamName: "table1",
  2377. Name: "id",
  2378. },
  2379. },
  2380. LHS: &ast.BinaryExpr{
  2381. OP: ast.AND,
  2382. LHS: &ast.BinaryExpr{
  2383. OP: ast.GT,
  2384. LHS: &ast.FieldRef{
  2385. StreamName: "table1",
  2386. Name: "b",
  2387. },
  2388. RHS: &ast.IntegerLiteral{Val: 20},
  2389. },
  2390. RHS: &ast.BinaryExpr{
  2391. OP: ast.LT,
  2392. LHS: &ast.FieldRef{
  2393. StreamName: "src1",
  2394. Name: "c",
  2395. },
  2396. RHS: &ast.IntegerLiteral{Val: 40},
  2397. },
  2398. },
  2399. },
  2400. },
  2401. keys: []string{"id"},
  2402. valvars: []ast.Expr{
  2403. &ast.FieldRef{
  2404. StreamName: "src1",
  2405. Name: "id",
  2406. },
  2407. },
  2408. options: &ast.Options{
  2409. DATASOURCE: "table1",
  2410. TYPE: "sql",
  2411. STRICT_VALIDATION: true,
  2412. KIND: "lookup",
  2413. },
  2414. conditions: &ast.BinaryExpr{
  2415. OP: ast.AND,
  2416. LHS: &ast.BinaryExpr{
  2417. OP: ast.GT,
  2418. LHS: &ast.FieldRef{
  2419. StreamName: "table1",
  2420. Name: "b",
  2421. },
  2422. RHS: &ast.IntegerLiteral{Val: 20},
  2423. },
  2424. RHS: &ast.BinaryExpr{
  2425. OP: ast.LT,
  2426. LHS: &ast.FieldRef{
  2427. StreamName: "src1",
  2428. Name: "c",
  2429. },
  2430. RHS: &ast.IntegerLiteral{Val: 40},
  2431. },
  2432. },
  2433. }.Init(),
  2434. },
  2435. },
  2436. condition: &ast.BinaryExpr{
  2437. OP: ast.GT,
  2438. LHS: &ast.FieldRef{
  2439. StreamName: "table1",
  2440. Name: "b",
  2441. },
  2442. RHS: &ast.IntegerLiteral{Val: 20},
  2443. },
  2444. }.Init(),
  2445. },
  2446. },
  2447. fields: []ast.Field{
  2448. {
  2449. Expr: &ast.FieldRef{
  2450. StreamName: "src1",
  2451. Name: "a",
  2452. },
  2453. Name: "a",
  2454. AName: "",
  2455. },
  2456. {
  2457. Expr: &ast.FieldRef{
  2458. StreamName: "table1",
  2459. Name: "*",
  2460. },
  2461. Name: "*",
  2462. AName: "",
  2463. },
  2464. },
  2465. isAggregate: false,
  2466. sendMeta: false,
  2467. }.Init(),
  2468. },
  2469. { // 2
  2470. 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`,
  2471. p: ProjectPlan{
  2472. baseLogicalPlan: baseLogicalPlan{
  2473. children: []LogicalPlan{
  2474. LookupPlan{
  2475. baseLogicalPlan: baseLogicalPlan{
  2476. children: []LogicalPlan{
  2477. LookupPlan{
  2478. baseLogicalPlan: baseLogicalPlan{
  2479. children: []LogicalPlan{
  2480. DataSourcePlan{
  2481. baseLogicalPlan: baseLogicalPlan{},
  2482. name: "src1",
  2483. streamFields: []interface{}{
  2484. "a",
  2485. },
  2486. streamStmt: streams["src1"],
  2487. metaFields: []string{},
  2488. }.Init(),
  2489. },
  2490. },
  2491. joinExpr: ast.Join{
  2492. Name: "table1",
  2493. Alias: "",
  2494. JoinType: ast.INNER_JOIN,
  2495. Expr: &ast.BinaryExpr{
  2496. OP: ast.EQ,
  2497. LHS: &ast.FieldRef{
  2498. StreamName: "src1",
  2499. Name: "id",
  2500. },
  2501. RHS: &ast.FieldRef{
  2502. StreamName: "table1",
  2503. Name: "id",
  2504. },
  2505. },
  2506. },
  2507. keys: []string{"id"},
  2508. fields: []string{"b"},
  2509. valvars: []ast.Expr{
  2510. &ast.FieldRef{
  2511. StreamName: "src1",
  2512. Name: "id",
  2513. },
  2514. },
  2515. options: &ast.Options{
  2516. DATASOURCE: "table1",
  2517. TYPE: "sql",
  2518. STRICT_VALIDATION: true,
  2519. KIND: "lookup",
  2520. },
  2521. conditions: nil,
  2522. }.Init(),
  2523. },
  2524. },
  2525. joinExpr: ast.Join{
  2526. Name: "table2",
  2527. Alias: "",
  2528. JoinType: ast.INNER_JOIN,
  2529. Expr: &ast.BinaryExpr{
  2530. OP: ast.EQ,
  2531. LHS: &ast.FieldRef{
  2532. StreamName: "table1",
  2533. Name: "id",
  2534. },
  2535. RHS: &ast.FieldRef{
  2536. StreamName: "table2",
  2537. Name: "id",
  2538. },
  2539. },
  2540. },
  2541. keys: []string{"id"},
  2542. fields: []string{"c"},
  2543. valvars: []ast.Expr{
  2544. &ast.FieldRef{
  2545. StreamName: "table1",
  2546. Name: "id",
  2547. },
  2548. },
  2549. options: &ast.Options{
  2550. DATASOURCE: "table2",
  2551. TYPE: "sql",
  2552. STRICT_VALIDATION: true,
  2553. KIND: "lookup",
  2554. },
  2555. }.Init(),
  2556. },
  2557. },
  2558. fields: []ast.Field{
  2559. {
  2560. Expr: &ast.FieldRef{
  2561. StreamName: "src1",
  2562. Name: "a",
  2563. },
  2564. Name: "a",
  2565. AName: "",
  2566. },
  2567. {
  2568. Expr: &ast.FieldRef{
  2569. StreamName: "table1",
  2570. Name: "b",
  2571. },
  2572. Name: "b",
  2573. AName: "",
  2574. },
  2575. {
  2576. Expr: &ast.FieldRef{
  2577. StreamName: "table2",
  2578. Name: "c",
  2579. },
  2580. Name: "c",
  2581. AName: "",
  2582. },
  2583. },
  2584. isAggregate: false,
  2585. sendMeta: false,
  2586. }.Init(),
  2587. },
  2588. { // 3
  2589. sql: `SELECT * FROM src1 INNER JOIN table1 ON src1.id = table1.id GROUP BY TUMBLINGWINDOW(ss, 10)`,
  2590. p: ProjectPlan{
  2591. baseLogicalPlan: baseLogicalPlan{
  2592. children: []LogicalPlan{
  2593. LookupPlan{
  2594. baseLogicalPlan: baseLogicalPlan{
  2595. children: []LogicalPlan{
  2596. WindowPlan{
  2597. baseLogicalPlan: baseLogicalPlan{
  2598. children: []LogicalPlan{
  2599. DataSourcePlan{
  2600. baseLogicalPlan: baseLogicalPlan{},
  2601. name: "src1",
  2602. streamStmt: streams["src1"],
  2603. metaFields: []string{},
  2604. isWildCard: true,
  2605. }.Init(),
  2606. },
  2607. },
  2608. condition: nil,
  2609. wtype: ast.TUMBLING_WINDOW,
  2610. length: 10000,
  2611. interval: 0,
  2612. limit: 0,
  2613. }.Init(),
  2614. },
  2615. },
  2616. joinExpr: ast.Join{
  2617. Name: "table1",
  2618. Alias: "",
  2619. JoinType: ast.INNER_JOIN,
  2620. Expr: &ast.BinaryExpr{
  2621. OP: ast.EQ,
  2622. LHS: &ast.FieldRef{
  2623. StreamName: "src1",
  2624. Name: "id",
  2625. },
  2626. RHS: &ast.FieldRef{
  2627. StreamName: "table1",
  2628. Name: "id",
  2629. },
  2630. },
  2631. },
  2632. keys: []string{"id"},
  2633. valvars: []ast.Expr{
  2634. &ast.FieldRef{
  2635. StreamName: "src1",
  2636. Name: "id",
  2637. },
  2638. },
  2639. options: &ast.Options{
  2640. DATASOURCE: "table1",
  2641. TYPE: "sql",
  2642. STRICT_VALIDATION: true,
  2643. KIND: "lookup",
  2644. },
  2645. conditions: nil,
  2646. }.Init(),
  2647. },
  2648. },
  2649. fields: []ast.Field{
  2650. {
  2651. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  2652. Name: "*",
  2653. AName: "",
  2654. },
  2655. },
  2656. isAggregate: false,
  2657. sendMeta: false,
  2658. }.Init(),
  2659. },
  2660. }
  2661. for i, tt := range tests {
  2662. stmt, err := xsql.NewParser(strings.NewReader(tt.sql)).Parse()
  2663. if err != nil {
  2664. t.Errorf("%d. %q: error compile sql: %s\n", i, tt.sql, err)
  2665. continue
  2666. }
  2667. p, err := createLogicalPlan(stmt, &api.RuleOption{
  2668. IsEventTime: false,
  2669. LateTol: 0,
  2670. Concurrency: 0,
  2671. BufferLength: 0,
  2672. SendMetaToSink: false,
  2673. Qos: 0,
  2674. CheckpointInterval: 0,
  2675. SendError: true,
  2676. }, store)
  2677. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  2678. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.sql, tt.err, err)
  2679. } else if !reflect.DeepEqual(tt.p, p) {
  2680. 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))
  2681. }
  2682. }
  2683. }