planner_test.go 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090
  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. }}},
  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. FilterPlan{
  684. baseLogicalPlan: baseLogicalPlan{
  685. children: []LogicalPlan{
  686. DataSourcePlan{
  687. name: "tableInPlanner",
  688. streamFields: []interface{}{
  689. &ast.StreamField{
  690. Name: "hum",
  691. FieldType: &ast.BasicType{Type: ast.BIGINT},
  692. },
  693. &ast.StreamField{
  694. Name: "id",
  695. FieldType: &ast.BasicType{Type: ast.BIGINT},
  696. },
  697. },
  698. streamStmt: streams["tableInPlanner"],
  699. metaFields: []string{},
  700. }.Init(),
  701. },
  702. },
  703. condition: &ast.BinaryExpr{
  704. OP: ast.LT,
  705. LHS: &ast.FieldRef{Name: "hum", StreamName: "tableInPlanner"},
  706. RHS: &ast.IntegerLiteral{Val: 60},
  707. },
  708. }.Init(),
  709. },
  710. },
  711. Emitters: []string{"tableInPlanner"},
  712. }.Init(),
  713. },
  714. },
  715. from: &ast.Table{
  716. Name: "src1",
  717. },
  718. joins: []ast.Join{
  719. {
  720. Name: "tableInPlanner",
  721. Alias: "",
  722. JoinType: ast.INNER_JOIN,
  723. Expr: &ast.BinaryExpr{
  724. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  725. OP: ast.EQ,
  726. RHS: &ast.FieldRef{Name: "id", StreamName: "tableInPlanner"},
  727. },
  728. },
  729. },
  730. }.Init(),
  731. },
  732. },
  733. fields: []ast.Field{
  734. {
  735. Expr: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  736. Name: "id1",
  737. AName: ""},
  738. },
  739. isAggregate: false,
  740. sendMeta: false,
  741. }.Init(),
  742. }, { // 9 join table with window
  743. 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)`,
  744. p: ProjectPlan{
  745. baseLogicalPlan: baseLogicalPlan{
  746. children: []LogicalPlan{
  747. JoinPlan{
  748. baseLogicalPlan: baseLogicalPlan{
  749. children: []LogicalPlan{
  750. JoinAlignPlan{
  751. baseLogicalPlan: baseLogicalPlan{
  752. children: []LogicalPlan{
  753. WindowPlan{
  754. baseLogicalPlan: baseLogicalPlan{
  755. children: []LogicalPlan{
  756. FilterPlan{
  757. baseLogicalPlan: baseLogicalPlan{
  758. children: []LogicalPlan{
  759. DataSourcePlan{
  760. name: "src1",
  761. streamFields: []interface{}{
  762. &ast.StreamField{
  763. Name: "id1",
  764. FieldType: &ast.BasicType{Type: ast.BIGINT},
  765. },
  766. &ast.StreamField{
  767. Name: "temp",
  768. FieldType: &ast.BasicType{Type: ast.BIGINT},
  769. },
  770. },
  771. streamStmt: streams["src1"],
  772. metaFields: []string{},
  773. }.Init(),
  774. },
  775. },
  776. condition: &ast.BinaryExpr{
  777. RHS: &ast.BinaryExpr{
  778. OP: ast.GT,
  779. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  780. RHS: &ast.IntegerLiteral{Val: 20},
  781. },
  782. OP: ast.AND,
  783. LHS: &ast.BinaryExpr{
  784. OP: ast.GT,
  785. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  786. RHS: &ast.IntegerLiteral{Val: 111},
  787. },
  788. },
  789. }.Init(),
  790. },
  791. },
  792. condition: nil,
  793. wtype: ast.TUMBLING_WINDOW,
  794. length: 10000,
  795. interval: 0,
  796. limit: 0,
  797. }.Init(),
  798. FilterPlan{
  799. baseLogicalPlan: baseLogicalPlan{
  800. children: []LogicalPlan{
  801. DataSourcePlan{
  802. name: "tableInPlanner",
  803. streamFields: []interface{}{
  804. &ast.StreamField{
  805. Name: "hum",
  806. FieldType: &ast.BasicType{Type: ast.BIGINT},
  807. },
  808. &ast.StreamField{
  809. Name: "id",
  810. FieldType: &ast.BasicType{Type: ast.BIGINT},
  811. },
  812. },
  813. streamStmt: streams["tableInPlanner"],
  814. metaFields: []string{},
  815. }.Init(),
  816. },
  817. },
  818. condition: &ast.BinaryExpr{
  819. OP: ast.LT,
  820. LHS: &ast.FieldRef{Name: "hum", StreamName: "tableInPlanner"},
  821. RHS: &ast.IntegerLiteral{Val: 60},
  822. },
  823. }.Init(),
  824. },
  825. },
  826. Emitters: []string{"tableInPlanner"},
  827. }.Init(),
  828. },
  829. },
  830. from: &ast.Table{
  831. Name: "src1",
  832. },
  833. joins: []ast.Join{
  834. {
  835. Name: "tableInPlanner",
  836. Alias: "",
  837. JoinType: ast.INNER_JOIN,
  838. Expr: &ast.BinaryExpr{
  839. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  840. OP: ast.EQ,
  841. RHS: &ast.FieldRef{Name: "id", StreamName: "tableInPlanner"},
  842. },
  843. },
  844. },
  845. }.Init(),
  846. },
  847. },
  848. fields: []ast.Field{
  849. {
  850. Expr: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  851. Name: "id1",
  852. AName: ""},
  853. },
  854. isAggregate: false,
  855. sendMeta: false,
  856. }.Init(),
  857. }, { // 10 meta
  858. sql: `SELECT temp, meta(id) AS eid,meta(Humidity->Device) AS hdevice FROM src1 WHERE meta(device)="demo2"`,
  859. p: ProjectPlan{
  860. baseLogicalPlan: baseLogicalPlan{
  861. children: []LogicalPlan{
  862. FilterPlan{
  863. baseLogicalPlan: baseLogicalPlan{
  864. children: []LogicalPlan{
  865. DataSourcePlan{
  866. name: "src1",
  867. streamFields: []interface{}{
  868. &ast.StreamField{
  869. Name: "temp",
  870. FieldType: &ast.BasicType{Type: ast.BIGINT},
  871. },
  872. },
  873. streamStmt: streams["src1"],
  874. metaFields: []string{"Humidity", "device", "id"},
  875. }.Init(),
  876. },
  877. },
  878. condition: &ast.BinaryExpr{
  879. LHS: &ast.Call{
  880. Name: "meta",
  881. FuncId: 2,
  882. Args: []ast.Expr{&ast.MetaRef{
  883. Name: "device",
  884. StreamName: ast.DefaultStream,
  885. }},
  886. },
  887. OP: ast.EQ,
  888. RHS: &ast.StringLiteral{
  889. Val: "demo2",
  890. },
  891. },
  892. }.Init(),
  893. },
  894. },
  895. fields: []ast.Field{
  896. {
  897. Expr: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  898. Name: "temp",
  899. AName: "",
  900. }, {
  901. Expr: &ast.FieldRef{Name: "eid", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  902. &ast.Call{Name: "meta", FuncId: 0, Args: []ast.Expr{&ast.MetaRef{
  903. Name: "id",
  904. StreamName: ast.DefaultStream,
  905. }}},
  906. []ast.StreamName{},
  907. nil,
  908. )},
  909. Name: "meta",
  910. AName: "eid",
  911. }, {
  912. Expr: &ast.FieldRef{Name: "hdevice", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  913. &ast.Call{Name: "meta", FuncId: 1, Args: []ast.Expr{
  914. &ast.BinaryExpr{
  915. OP: ast.ARROW,
  916. LHS: &ast.MetaRef{Name: "Humidity", StreamName: ast.DefaultStream},
  917. RHS: &ast.JsonFieldRef{Name: "Device"},
  918. },
  919. }},
  920. []ast.StreamName{},
  921. nil,
  922. )},
  923. Name: "meta",
  924. AName: "hdevice",
  925. },
  926. },
  927. isAggregate: false,
  928. sendMeta: false,
  929. }.Init(),
  930. }, { // 11 join with same name field and aliased
  931. sql: `SELECT src2.hum AS hum1, tableInPlanner.hum AS hum2 FROM src2 INNER JOIN tableInPlanner on id2 = id WHERE hum1 > hum2`,
  932. p: ProjectPlan{
  933. baseLogicalPlan: baseLogicalPlan{
  934. children: []LogicalPlan{
  935. JoinPlan{
  936. baseLogicalPlan: baseLogicalPlan{
  937. children: []LogicalPlan{
  938. JoinAlignPlan{
  939. baseLogicalPlan: baseLogicalPlan{
  940. children: []LogicalPlan{
  941. DataSourcePlan{
  942. name: "src2",
  943. streamFields: []interface{}{
  944. &ast.StreamField{
  945. Name: "hum",
  946. FieldType: &ast.BasicType{Type: ast.BIGINT},
  947. },
  948. &ast.StreamField{
  949. Name: "id2",
  950. FieldType: &ast.BasicType{Type: ast.BIGINT},
  951. },
  952. },
  953. streamStmt: streams["src2"],
  954. metaFields: []string{},
  955. timestampFormat: "YYYY-MM-dd HH:mm:ss",
  956. }.Init(),
  957. DataSourcePlan{
  958. name: "tableInPlanner",
  959. streamFields: []interface{}{
  960. &ast.StreamField{
  961. Name: "hum",
  962. FieldType: &ast.BasicType{Type: ast.BIGINT},
  963. },
  964. &ast.StreamField{
  965. Name: "id",
  966. FieldType: &ast.BasicType{Type: ast.BIGINT},
  967. },
  968. },
  969. streamStmt: streams["tableInPlanner"],
  970. metaFields: []string{},
  971. }.Init(),
  972. },
  973. },
  974. Emitters: []string{"tableInPlanner"},
  975. }.Init(),
  976. },
  977. },
  978. from: &ast.Table{
  979. Name: "src2",
  980. },
  981. joins: []ast.Join{
  982. {
  983. Name: "tableInPlanner",
  984. Alias: "",
  985. JoinType: ast.INNER_JOIN,
  986. Expr: &ast.BinaryExpr{
  987. RHS: &ast.BinaryExpr{
  988. OP: ast.EQ,
  989. LHS: &ast.FieldRef{Name: "id2", StreamName: "src2"},
  990. RHS: &ast.FieldRef{Name: "id", StreamName: "tableInPlanner"},
  991. },
  992. OP: ast.AND,
  993. LHS: &ast.BinaryExpr{
  994. OP: ast.GT,
  995. LHS: &ast.FieldRef{Name: "hum1", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  996. &ast.FieldRef{
  997. Name: "hum",
  998. StreamName: "src2",
  999. },
  1000. []ast.StreamName{"src2"},
  1001. &boolFalse,
  1002. )},
  1003. RHS: &ast.FieldRef{Name: "hum2", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  1004. &ast.FieldRef{
  1005. Name: "hum",
  1006. StreamName: "tableInPlanner",
  1007. },
  1008. []ast.StreamName{"tableInPlanner"},
  1009. &boolFalse,
  1010. )},
  1011. },
  1012. },
  1013. },
  1014. },
  1015. }.Init(),
  1016. },
  1017. },
  1018. fields: []ast.Field{
  1019. {
  1020. Expr: &ast.FieldRef{Name: "hum1", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  1021. &ast.FieldRef{
  1022. Name: "hum",
  1023. StreamName: "src2",
  1024. },
  1025. []ast.StreamName{"src2"},
  1026. &boolFalse,
  1027. )},
  1028. Name: "hum",
  1029. AName: "hum1",
  1030. }, {
  1031. Expr: &ast.FieldRef{Name: "hum2", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  1032. &ast.FieldRef{
  1033. Name: "hum",
  1034. StreamName: "tableInPlanner",
  1035. },
  1036. []ast.StreamName{"tableInPlanner"},
  1037. &boolFalse,
  1038. )},
  1039. Name: "hum",
  1040. AName: "hum2",
  1041. },
  1042. },
  1043. isAggregate: false,
  1044. sendMeta: false,
  1045. }.Init(),
  1046. }, { // 12 meta with more fields
  1047. sql: `SELECT temp, meta(*) as m FROM src1 WHERE meta(device)="demo2"`,
  1048. p: ProjectPlan{
  1049. baseLogicalPlan: baseLogicalPlan{
  1050. children: []LogicalPlan{
  1051. FilterPlan{
  1052. baseLogicalPlan: baseLogicalPlan{
  1053. children: []LogicalPlan{
  1054. DataSourcePlan{
  1055. name: "src1",
  1056. streamFields: []interface{}{
  1057. &ast.StreamField{
  1058. Name: "temp",
  1059. FieldType: &ast.BasicType{Type: ast.BIGINT},
  1060. },
  1061. },
  1062. streamStmt: streams["src1"],
  1063. metaFields: []string{},
  1064. allMeta: true,
  1065. }.Init(),
  1066. },
  1067. },
  1068. condition: &ast.BinaryExpr{
  1069. LHS: &ast.Call{
  1070. Name: "meta",
  1071. FuncId: 1,
  1072. Args: []ast.Expr{&ast.MetaRef{
  1073. Name: "device",
  1074. StreamName: ast.DefaultStream,
  1075. }},
  1076. },
  1077. OP: ast.EQ,
  1078. RHS: &ast.StringLiteral{
  1079. Val: "demo2",
  1080. },
  1081. },
  1082. }.Init(),
  1083. },
  1084. },
  1085. fields: []ast.Field{
  1086. {
  1087. Expr: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  1088. Name: "temp",
  1089. AName: "",
  1090. }, {
  1091. Expr: &ast.FieldRef{Name: "m", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  1092. &ast.Call{Name: "meta", FuncId: 0, Args: []ast.Expr{&ast.MetaRef{
  1093. Name: "*",
  1094. StreamName: ast.DefaultStream,
  1095. }}},
  1096. []ast.StreamName{},
  1097. nil,
  1098. )},
  1099. Name: "meta",
  1100. AName: "m",
  1101. },
  1102. },
  1103. isAggregate: false,
  1104. sendMeta: false,
  1105. }.Init(),
  1106. },
  1107. }
  1108. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  1109. for i, tt := range tests {
  1110. stmt, err := xsql.NewParser(strings.NewReader(tt.sql)).Parse()
  1111. if err != nil {
  1112. t.Errorf("%d. %q: error compile sql: %s\n", i, tt.sql, err)
  1113. continue
  1114. }
  1115. p, err := createLogicalPlan(stmt, &api.RuleOption{
  1116. IsEventTime: false,
  1117. LateTol: 0,
  1118. Concurrency: 0,
  1119. BufferLength: 0,
  1120. SendMetaToSink: false,
  1121. Qos: 0,
  1122. CheckpointInterval: 0,
  1123. SendError: true,
  1124. }, store)
  1125. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  1126. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.sql, tt.err, err)
  1127. } else if !reflect.DeepEqual(tt.p, p) {
  1128. 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))
  1129. }
  1130. }
  1131. }
  1132. func Test_createLogicalPlanSchemaless(t *testing.T) {
  1133. err, store := store.GetKV("stream")
  1134. if err != nil {
  1135. t.Error(err)
  1136. return
  1137. }
  1138. streamSqls := map[string]string{
  1139. "src1": `CREATE STREAM src1 (
  1140. ) WITH (DATASOURCE="src1", FORMAT="json", KEY="ts");`,
  1141. "src2": `CREATE STREAM src2 (
  1142. ) WITH (DATASOURCE="src2", FORMAT="json", KEY="ts");`,
  1143. "tableInPlanner": `CREATE TABLE tableInPlanner (
  1144. id BIGINT,
  1145. name STRING,
  1146. value STRING,
  1147. hum BIGINT
  1148. ) WITH (TYPE="file");`,
  1149. }
  1150. types := map[string]ast.StreamType{
  1151. "src1": ast.TypeStream,
  1152. "src2": ast.TypeStream,
  1153. "tableInPlanner": ast.TypeTable,
  1154. }
  1155. for name, sql := range streamSqls {
  1156. s, err := json.Marshal(&xsql.StreamInfo{
  1157. StreamType: types[name],
  1158. Statement: sql,
  1159. })
  1160. if err != nil {
  1161. t.Error(err)
  1162. t.Fail()
  1163. }
  1164. err = store.Set(name, string(s))
  1165. if err != nil {
  1166. t.Error(err)
  1167. t.Fail()
  1168. }
  1169. }
  1170. streams := make(map[string]*ast.StreamStmt)
  1171. for n := range streamSqls {
  1172. streamStmt, err := xsql.GetDataSource(store, n)
  1173. if err != nil {
  1174. t.Errorf("fail to get stream %s, please check if stream is created", n)
  1175. return
  1176. }
  1177. streams[n] = streamStmt
  1178. }
  1179. var (
  1180. //boolTrue = true
  1181. boolFalse = false
  1182. )
  1183. var tests = []struct {
  1184. sql string
  1185. p LogicalPlan
  1186. err string
  1187. }{
  1188. { // 0
  1189. sql: `SELECT name FROM src1`,
  1190. p: ProjectPlan{
  1191. baseLogicalPlan: baseLogicalPlan{
  1192. children: []LogicalPlan{
  1193. DataSourcePlan{
  1194. baseLogicalPlan: baseLogicalPlan{},
  1195. name: "src1",
  1196. streamFields: []interface{}{
  1197. "name",
  1198. },
  1199. streamStmt: streams["src1"],
  1200. metaFields: []string{},
  1201. }.Init(),
  1202. },
  1203. },
  1204. fields: []ast.Field{
  1205. {
  1206. Expr: &ast.FieldRef{Name: "name", StreamName: "src1"},
  1207. Name: "name",
  1208. AName: "",
  1209. },
  1210. },
  1211. isAggregate: false,
  1212. sendMeta: false,
  1213. }.Init(),
  1214. }, { // 1 optimize where to data source
  1215. sql: `SELECT temp FROM src1 WHERE name = "v1" GROUP BY TUMBLINGWINDOW(ss, 10)`,
  1216. p: ProjectPlan{
  1217. baseLogicalPlan: baseLogicalPlan{
  1218. children: []LogicalPlan{
  1219. WindowPlan{
  1220. baseLogicalPlan: baseLogicalPlan{
  1221. children: []LogicalPlan{
  1222. FilterPlan{
  1223. baseLogicalPlan: baseLogicalPlan{
  1224. children: []LogicalPlan{
  1225. DataSourcePlan{
  1226. name: "src1",
  1227. streamFields: []interface{}{
  1228. "name", "temp",
  1229. },
  1230. streamStmt: streams["src1"],
  1231. metaFields: []string{},
  1232. }.Init(),
  1233. },
  1234. },
  1235. condition: &ast.BinaryExpr{
  1236. LHS: &ast.FieldRef{Name: "name", StreamName: "src1"},
  1237. OP: ast.EQ,
  1238. RHS: &ast.StringLiteral{Val: "v1"},
  1239. },
  1240. }.Init(),
  1241. },
  1242. },
  1243. condition: nil,
  1244. wtype: ast.TUMBLING_WINDOW,
  1245. length: 10000,
  1246. interval: 0,
  1247. limit: 0,
  1248. }.Init(),
  1249. },
  1250. },
  1251. fields: []ast.Field{
  1252. {
  1253. Expr: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  1254. Name: "temp",
  1255. AName: ""},
  1256. },
  1257. isAggregate: false,
  1258. sendMeta: false,
  1259. }.Init(),
  1260. }, { // 2 condition that cannot be optimized
  1261. 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)`,
  1262. p: ProjectPlan{
  1263. baseLogicalPlan: baseLogicalPlan{
  1264. children: []LogicalPlan{
  1265. JoinPlan{
  1266. baseLogicalPlan: baseLogicalPlan{
  1267. children: []LogicalPlan{
  1268. WindowPlan{
  1269. baseLogicalPlan: baseLogicalPlan{
  1270. children: []LogicalPlan{
  1271. DataSourcePlan{
  1272. name: "src1",
  1273. streamFields: []interface{}{
  1274. "id1", "temp",
  1275. },
  1276. streamStmt: streams["src1"],
  1277. metaFields: []string{},
  1278. }.Init(),
  1279. DataSourcePlan{
  1280. name: "src2",
  1281. streamFields: []interface{}{ // can't determine where is id1 belonged to
  1282. "hum", "id1", "id2",
  1283. },
  1284. streamStmt: streams["src2"],
  1285. metaFields: []string{},
  1286. }.Init(),
  1287. },
  1288. },
  1289. condition: nil,
  1290. wtype: ast.TUMBLING_WINDOW,
  1291. length: 10000,
  1292. interval: 0,
  1293. limit: 0,
  1294. }.Init(),
  1295. },
  1296. },
  1297. from: &ast.Table{Name: "src1"},
  1298. joins: ast.Joins{ast.Join{
  1299. Name: "src2",
  1300. JoinType: ast.INNER_JOIN,
  1301. Expr: &ast.BinaryExpr{
  1302. OP: ast.AND,
  1303. LHS: &ast.BinaryExpr{
  1304. LHS: &ast.BinaryExpr{
  1305. OP: ast.GT,
  1306. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  1307. RHS: &ast.IntegerLiteral{Val: 20},
  1308. },
  1309. OP: ast.OR,
  1310. RHS: &ast.BinaryExpr{
  1311. OP: ast.GT,
  1312. LHS: &ast.FieldRef{Name: "hum", StreamName: "src2"},
  1313. RHS: &ast.IntegerLiteral{Val: 60},
  1314. },
  1315. },
  1316. RHS: &ast.BinaryExpr{
  1317. OP: ast.EQ,
  1318. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  1319. RHS: &ast.FieldRef{Name: "id2", StreamName: "src2"},
  1320. },
  1321. },
  1322. }},
  1323. }.Init(),
  1324. },
  1325. },
  1326. fields: []ast.Field{
  1327. {
  1328. Expr: &ast.FieldRef{Name: "id1", StreamName: ast.DefaultStream},
  1329. Name: "id1",
  1330. AName: ""},
  1331. },
  1332. isAggregate: false,
  1333. sendMeta: false,
  1334. }.Init(),
  1335. }, { // 3 optimize window filter
  1336. sql: `SELECT id1 FROM src1 WHERE name = "v1" GROUP BY TUMBLINGWINDOW(ss, 10) FILTER( WHERE temp > 2)`,
  1337. p: ProjectPlan{
  1338. baseLogicalPlan: baseLogicalPlan{
  1339. children: []LogicalPlan{
  1340. WindowPlan{
  1341. baseLogicalPlan: baseLogicalPlan{
  1342. children: []LogicalPlan{
  1343. FilterPlan{
  1344. baseLogicalPlan: baseLogicalPlan{
  1345. children: []LogicalPlan{
  1346. DataSourcePlan{
  1347. name: "src1",
  1348. streamFields: []interface{}{
  1349. "id1", "name", "temp",
  1350. },
  1351. streamStmt: streams["src1"],
  1352. metaFields: []string{},
  1353. }.Init(),
  1354. },
  1355. },
  1356. condition: &ast.BinaryExpr{
  1357. OP: ast.AND,
  1358. LHS: &ast.BinaryExpr{
  1359. LHS: &ast.FieldRef{Name: "name", StreamName: "src1"},
  1360. OP: ast.EQ,
  1361. RHS: &ast.StringLiteral{Val: "v1"},
  1362. },
  1363. RHS: &ast.BinaryExpr{
  1364. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  1365. OP: ast.GT,
  1366. RHS: &ast.IntegerLiteral{Val: 2},
  1367. },
  1368. },
  1369. }.Init(),
  1370. },
  1371. },
  1372. condition: nil,
  1373. wtype: ast.TUMBLING_WINDOW,
  1374. length: 10000,
  1375. interval: 0,
  1376. limit: 0,
  1377. }.Init(),
  1378. },
  1379. },
  1380. fields: []ast.Field{
  1381. {
  1382. Expr: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  1383. Name: "id1",
  1384. AName: ""},
  1385. },
  1386. isAggregate: false,
  1387. sendMeta: false,
  1388. }.Init(),
  1389. }, { // 4. do not optimize count window
  1390. sql: `SELECT * FROM src1 WHERE temp > 20 GROUP BY COUNTWINDOW(5,1) HAVING COUNT(*) > 2`,
  1391. p: ProjectPlan{
  1392. baseLogicalPlan: baseLogicalPlan{
  1393. children: []LogicalPlan{
  1394. HavingPlan{
  1395. baseLogicalPlan: baseLogicalPlan{
  1396. children: []LogicalPlan{
  1397. FilterPlan{
  1398. baseLogicalPlan: baseLogicalPlan{
  1399. children: []LogicalPlan{
  1400. WindowPlan{
  1401. baseLogicalPlan: baseLogicalPlan{
  1402. children: []LogicalPlan{
  1403. DataSourcePlan{
  1404. name: "src1",
  1405. isWildCard: true,
  1406. streamFields: nil,
  1407. streamStmt: streams["src1"],
  1408. metaFields: []string{},
  1409. }.Init(),
  1410. },
  1411. },
  1412. condition: nil,
  1413. wtype: ast.COUNT_WINDOW,
  1414. length: 5,
  1415. interval: 1,
  1416. limit: 0,
  1417. }.Init(),
  1418. },
  1419. },
  1420. condition: &ast.BinaryExpr{
  1421. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  1422. OP: ast.GT,
  1423. RHS: &ast.IntegerLiteral{Val: 20},
  1424. },
  1425. }.Init(),
  1426. },
  1427. },
  1428. condition: &ast.BinaryExpr{
  1429. LHS: &ast.Call{Name: "count", FuncId: 0, Args: []ast.Expr{&ast.Wildcard{
  1430. Token: ast.ASTERISK,
  1431. }}},
  1432. OP: ast.GT,
  1433. RHS: &ast.IntegerLiteral{Val: 2},
  1434. },
  1435. }.Init(),
  1436. },
  1437. },
  1438. fields: []ast.Field{
  1439. {
  1440. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  1441. Name: "",
  1442. AName: ""},
  1443. },
  1444. isAggregate: false,
  1445. sendMeta: false,
  1446. }.Init(),
  1447. }, { // 5. optimize join on
  1448. 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)`,
  1449. p: ProjectPlan{
  1450. baseLogicalPlan: baseLogicalPlan{
  1451. children: []LogicalPlan{
  1452. JoinPlan{
  1453. baseLogicalPlan: baseLogicalPlan{
  1454. children: []LogicalPlan{
  1455. WindowPlan{
  1456. baseLogicalPlan: baseLogicalPlan{
  1457. children: []LogicalPlan{
  1458. FilterPlan{
  1459. baseLogicalPlan: baseLogicalPlan{
  1460. children: []LogicalPlan{
  1461. DataSourcePlan{
  1462. name: "src1",
  1463. streamFields: []interface{}{
  1464. "id1", "temp",
  1465. },
  1466. streamStmt: streams["src1"],
  1467. metaFields: []string{},
  1468. }.Init(),
  1469. },
  1470. },
  1471. condition: &ast.BinaryExpr{
  1472. RHS: &ast.BinaryExpr{
  1473. OP: ast.GT,
  1474. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  1475. RHS: &ast.IntegerLiteral{Val: 20},
  1476. },
  1477. OP: ast.AND,
  1478. LHS: &ast.BinaryExpr{
  1479. OP: ast.GT,
  1480. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  1481. RHS: &ast.IntegerLiteral{Val: 111},
  1482. },
  1483. },
  1484. }.Init(),
  1485. FilterPlan{
  1486. baseLogicalPlan: baseLogicalPlan{
  1487. children: []LogicalPlan{
  1488. DataSourcePlan{
  1489. name: "src2",
  1490. streamFields: []interface{}{
  1491. "hum", "id1", "id2",
  1492. },
  1493. streamStmt: streams["src2"],
  1494. metaFields: []string{},
  1495. }.Init(),
  1496. },
  1497. },
  1498. condition: &ast.BinaryExpr{
  1499. OP: ast.LT,
  1500. LHS: &ast.FieldRef{Name: "hum", StreamName: "src2"},
  1501. RHS: &ast.IntegerLiteral{Val: 60},
  1502. },
  1503. }.Init(),
  1504. },
  1505. },
  1506. condition: nil,
  1507. wtype: ast.TUMBLING_WINDOW,
  1508. length: 10000,
  1509. interval: 0,
  1510. limit: 0,
  1511. }.Init(),
  1512. },
  1513. },
  1514. from: &ast.Table{
  1515. Name: "src1",
  1516. },
  1517. joins: []ast.Join{
  1518. {
  1519. Name: "src2",
  1520. Alias: "",
  1521. JoinType: ast.INNER_JOIN,
  1522. Expr: &ast.BinaryExpr{
  1523. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  1524. OP: ast.EQ,
  1525. RHS: &ast.FieldRef{Name: "id2", StreamName: "src2"},
  1526. },
  1527. },
  1528. },
  1529. }.Init(),
  1530. },
  1531. },
  1532. fields: []ast.Field{
  1533. {
  1534. Expr: &ast.FieldRef{Name: "id1", StreamName: ast.DefaultStream},
  1535. Name: "id1",
  1536. AName: ""},
  1537. },
  1538. isAggregate: false,
  1539. sendMeta: false,
  1540. }.Init(),
  1541. }, { // 6. optimize outter join on
  1542. 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)`,
  1543. p: ProjectPlan{
  1544. baseLogicalPlan: baseLogicalPlan{
  1545. children: []LogicalPlan{
  1546. JoinPlan{
  1547. baseLogicalPlan: baseLogicalPlan{
  1548. children: []LogicalPlan{
  1549. WindowPlan{
  1550. baseLogicalPlan: baseLogicalPlan{
  1551. children: []LogicalPlan{
  1552. FilterPlan{
  1553. baseLogicalPlan: baseLogicalPlan{
  1554. children: []LogicalPlan{
  1555. DataSourcePlan{
  1556. name: "src1",
  1557. streamFields: []interface{}{
  1558. "id1", "temp",
  1559. },
  1560. streamStmt: streams["src1"],
  1561. metaFields: []string{},
  1562. }.Init(),
  1563. },
  1564. },
  1565. condition: &ast.BinaryExpr{
  1566. OP: ast.GT,
  1567. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  1568. RHS: &ast.IntegerLiteral{Val: 111},
  1569. },
  1570. }.Init(),
  1571. DataSourcePlan{
  1572. name: "src2",
  1573. streamFields: []interface{}{
  1574. "hum", "id1", "id2",
  1575. },
  1576. streamStmt: streams["src2"],
  1577. metaFields: []string{},
  1578. }.Init(),
  1579. },
  1580. },
  1581. condition: nil,
  1582. wtype: ast.TUMBLING_WINDOW,
  1583. length: 10000,
  1584. interval: 0,
  1585. limit: 0,
  1586. }.Init(),
  1587. },
  1588. },
  1589. from: &ast.Table{
  1590. Name: "src1",
  1591. },
  1592. joins: []ast.Join{
  1593. {
  1594. Name: "src2",
  1595. Alias: "",
  1596. JoinType: ast.FULL_JOIN,
  1597. Expr: &ast.BinaryExpr{
  1598. OP: ast.AND,
  1599. LHS: &ast.BinaryExpr{
  1600. OP: ast.AND,
  1601. LHS: &ast.BinaryExpr{
  1602. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  1603. OP: ast.EQ,
  1604. RHS: &ast.FieldRef{Name: "id2", StreamName: "src2"},
  1605. },
  1606. RHS: &ast.BinaryExpr{
  1607. OP: ast.GT,
  1608. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  1609. RHS: &ast.IntegerLiteral{Val: 20},
  1610. },
  1611. },
  1612. RHS: &ast.BinaryExpr{
  1613. OP: ast.LT,
  1614. LHS: &ast.FieldRef{Name: "hum", StreamName: "src2"},
  1615. RHS: &ast.IntegerLiteral{Val: 60},
  1616. },
  1617. },
  1618. },
  1619. },
  1620. }.Init(),
  1621. },
  1622. },
  1623. fields: []ast.Field{
  1624. {
  1625. Expr: &ast.FieldRef{Name: "id1", StreamName: ast.DefaultStream},
  1626. Name: "id1",
  1627. AName: ""},
  1628. },
  1629. isAggregate: false,
  1630. sendMeta: false,
  1631. }.Init(),
  1632. }, { // 7 window error for table
  1633. sql: `SELECT value FROM tableInPlanner WHERE name = "v1" GROUP BY TUMBLINGWINDOW(ss, 10)`,
  1634. p: nil,
  1635. err: "cannot run window for TABLE sources",
  1636. }, { // 8 join table without window
  1637. sql: `SELECT id1 FROM src1 INNER JOIN tableInPlanner on src1.id1 = tableInPlanner.id and src1.temp > 20 and hum < 60 WHERE src1.id1 > 111`,
  1638. p: ProjectPlan{
  1639. baseLogicalPlan: baseLogicalPlan{
  1640. children: []LogicalPlan{
  1641. JoinPlan{
  1642. baseLogicalPlan: baseLogicalPlan{
  1643. children: []LogicalPlan{
  1644. JoinAlignPlan{
  1645. baseLogicalPlan: baseLogicalPlan{
  1646. children: []LogicalPlan{
  1647. FilterPlan{
  1648. baseLogicalPlan: baseLogicalPlan{
  1649. children: []LogicalPlan{
  1650. DataSourcePlan{
  1651. name: "src1",
  1652. streamFields: []interface{}{
  1653. "hum", "id1", "temp",
  1654. },
  1655. streamStmt: streams["src1"],
  1656. metaFields: []string{},
  1657. }.Init(),
  1658. },
  1659. },
  1660. condition: &ast.BinaryExpr{
  1661. RHS: &ast.BinaryExpr{
  1662. OP: ast.GT,
  1663. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  1664. RHS: &ast.IntegerLiteral{Val: 20},
  1665. },
  1666. OP: ast.AND,
  1667. LHS: &ast.BinaryExpr{
  1668. OP: ast.GT,
  1669. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  1670. RHS: &ast.IntegerLiteral{Val: 111},
  1671. },
  1672. },
  1673. }.Init(),
  1674. DataSourcePlan{
  1675. name: "tableInPlanner",
  1676. streamFields: []interface{}{
  1677. &ast.StreamField{
  1678. Name: "hum",
  1679. FieldType: &ast.BasicType{Type: ast.BIGINT},
  1680. },
  1681. &ast.StreamField{
  1682. Name: "id",
  1683. FieldType: &ast.BasicType{Type: ast.BIGINT},
  1684. },
  1685. },
  1686. streamStmt: streams["tableInPlanner"],
  1687. metaFields: []string{},
  1688. }.Init(),
  1689. },
  1690. },
  1691. Emitters: []string{"tableInPlanner"},
  1692. }.Init(),
  1693. },
  1694. },
  1695. from: &ast.Table{
  1696. Name: "src1",
  1697. },
  1698. joins: []ast.Join{
  1699. {
  1700. Name: "tableInPlanner",
  1701. Alias: "",
  1702. JoinType: ast.INNER_JOIN,
  1703. Expr: &ast.BinaryExpr{
  1704. OP: ast.AND,
  1705. LHS: &ast.BinaryExpr{
  1706. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  1707. OP: ast.EQ,
  1708. RHS: &ast.FieldRef{Name: "id", StreamName: "tableInPlanner"},
  1709. },
  1710. RHS: &ast.BinaryExpr{
  1711. OP: ast.LT,
  1712. LHS: &ast.FieldRef{Name: "hum", StreamName: ast.DefaultStream},
  1713. RHS: &ast.IntegerLiteral{Val: 60},
  1714. },
  1715. },
  1716. },
  1717. },
  1718. }.Init(),
  1719. },
  1720. },
  1721. fields: []ast.Field{
  1722. {
  1723. Expr: &ast.FieldRef{Name: "id1", StreamName: ast.DefaultStream},
  1724. Name: "id1",
  1725. AName: ""},
  1726. },
  1727. isAggregate: false,
  1728. sendMeta: false,
  1729. }.Init(),
  1730. }, { // 9 join table with window
  1731. 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)`,
  1732. p: ProjectPlan{
  1733. baseLogicalPlan: baseLogicalPlan{
  1734. children: []LogicalPlan{
  1735. JoinPlan{
  1736. baseLogicalPlan: baseLogicalPlan{
  1737. children: []LogicalPlan{
  1738. JoinAlignPlan{
  1739. baseLogicalPlan: baseLogicalPlan{
  1740. children: []LogicalPlan{
  1741. WindowPlan{
  1742. baseLogicalPlan: baseLogicalPlan{
  1743. children: []LogicalPlan{
  1744. FilterPlan{
  1745. baseLogicalPlan: baseLogicalPlan{
  1746. children: []LogicalPlan{
  1747. DataSourcePlan{
  1748. name: "src1",
  1749. streamFields: []interface{}{
  1750. "id1", "temp",
  1751. },
  1752. streamStmt: streams["src1"],
  1753. metaFields: []string{},
  1754. }.Init(),
  1755. },
  1756. },
  1757. condition: &ast.BinaryExpr{
  1758. RHS: &ast.BinaryExpr{
  1759. OP: ast.GT,
  1760. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  1761. RHS: &ast.IntegerLiteral{Val: 20},
  1762. },
  1763. OP: ast.AND,
  1764. LHS: &ast.BinaryExpr{
  1765. OP: ast.GT,
  1766. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  1767. RHS: &ast.IntegerLiteral{Val: 111},
  1768. },
  1769. },
  1770. }.Init(),
  1771. },
  1772. },
  1773. condition: nil,
  1774. wtype: ast.TUMBLING_WINDOW,
  1775. length: 10000,
  1776. interval: 0,
  1777. limit: 0,
  1778. }.Init(),
  1779. FilterPlan{
  1780. baseLogicalPlan: baseLogicalPlan{
  1781. children: []LogicalPlan{
  1782. DataSourcePlan{
  1783. name: "tableInPlanner",
  1784. streamFields: []interface{}{
  1785. &ast.StreamField{
  1786. Name: "hum",
  1787. FieldType: &ast.BasicType{Type: ast.BIGINT},
  1788. },
  1789. &ast.StreamField{
  1790. Name: "id",
  1791. FieldType: &ast.BasicType{Type: ast.BIGINT},
  1792. },
  1793. },
  1794. streamStmt: streams["tableInPlanner"],
  1795. metaFields: []string{},
  1796. }.Init(),
  1797. },
  1798. },
  1799. condition: &ast.BinaryExpr{
  1800. OP: ast.LT,
  1801. LHS: &ast.FieldRef{Name: "hum", StreamName: "tableInPlanner"},
  1802. RHS: &ast.IntegerLiteral{Val: 60},
  1803. },
  1804. }.Init(),
  1805. },
  1806. },
  1807. Emitters: []string{"tableInPlanner"},
  1808. }.Init(),
  1809. },
  1810. },
  1811. from: &ast.Table{
  1812. Name: "src1",
  1813. },
  1814. joins: []ast.Join{
  1815. {
  1816. Name: "tableInPlanner",
  1817. Alias: "",
  1818. JoinType: ast.INNER_JOIN,
  1819. Expr: &ast.BinaryExpr{
  1820. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  1821. OP: ast.EQ,
  1822. RHS: &ast.FieldRef{Name: "id", StreamName: "tableInPlanner"},
  1823. },
  1824. },
  1825. },
  1826. }.Init(),
  1827. },
  1828. },
  1829. fields: []ast.Field{
  1830. {
  1831. Expr: &ast.FieldRef{Name: "id1", StreamName: ast.DefaultStream},
  1832. Name: "id1",
  1833. AName: ""},
  1834. },
  1835. isAggregate: false,
  1836. sendMeta: false,
  1837. }.Init(),
  1838. }, { // 10 meta
  1839. sql: `SELECT temp, meta(id) AS eid,meta(Humidity->Device) AS hdevice FROM src1 WHERE meta(device)="demo2"`,
  1840. p: ProjectPlan{
  1841. baseLogicalPlan: baseLogicalPlan{
  1842. children: []LogicalPlan{
  1843. FilterPlan{
  1844. baseLogicalPlan: baseLogicalPlan{
  1845. children: []LogicalPlan{
  1846. DataSourcePlan{
  1847. name: "src1",
  1848. streamFields: []interface{}{
  1849. "temp",
  1850. },
  1851. streamStmt: streams["src1"],
  1852. metaFields: []string{"Humidity", "device", "id"},
  1853. }.Init(),
  1854. },
  1855. },
  1856. condition: &ast.BinaryExpr{
  1857. LHS: &ast.Call{
  1858. Name: "meta",
  1859. FuncId: 2,
  1860. Args: []ast.Expr{&ast.MetaRef{
  1861. Name: "device",
  1862. StreamName: ast.DefaultStream,
  1863. }},
  1864. },
  1865. OP: ast.EQ,
  1866. RHS: &ast.StringLiteral{
  1867. Val: "demo2",
  1868. },
  1869. },
  1870. }.Init(),
  1871. },
  1872. },
  1873. fields: []ast.Field{
  1874. {
  1875. Expr: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  1876. Name: "temp",
  1877. AName: "",
  1878. }, {
  1879. Expr: &ast.FieldRef{Name: "eid", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  1880. &ast.Call{Name: "meta", FuncId: 0, Args: []ast.Expr{&ast.MetaRef{
  1881. Name: "id",
  1882. StreamName: ast.DefaultStream,
  1883. }}},
  1884. []ast.StreamName{},
  1885. nil,
  1886. )},
  1887. Name: "meta",
  1888. AName: "eid",
  1889. }, {
  1890. Expr: &ast.FieldRef{Name: "hdevice", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  1891. &ast.Call{Name: "meta", FuncId: 1, Args: []ast.Expr{
  1892. &ast.BinaryExpr{
  1893. OP: ast.ARROW,
  1894. LHS: &ast.MetaRef{Name: "Humidity", StreamName: ast.DefaultStream},
  1895. RHS: &ast.JsonFieldRef{Name: "Device"},
  1896. },
  1897. }},
  1898. []ast.StreamName{},
  1899. nil,
  1900. )},
  1901. Name: "meta",
  1902. AName: "hdevice",
  1903. },
  1904. },
  1905. isAggregate: false,
  1906. sendMeta: false,
  1907. }.Init(),
  1908. }, { // 11 join with same name field and aliased
  1909. sql: `SELECT src2.hum AS hum1, tableInPlanner.hum AS hum2 FROM src2 INNER JOIN tableInPlanner on id2 = id WHERE hum1 > hum2`,
  1910. p: ProjectPlan{
  1911. baseLogicalPlan: baseLogicalPlan{
  1912. children: []LogicalPlan{
  1913. JoinPlan{
  1914. baseLogicalPlan: baseLogicalPlan{
  1915. children: []LogicalPlan{
  1916. JoinAlignPlan{
  1917. baseLogicalPlan: baseLogicalPlan{
  1918. children: []LogicalPlan{
  1919. DataSourcePlan{
  1920. name: "src2",
  1921. streamFields: []interface{}{
  1922. "hum", "id", "id2",
  1923. },
  1924. streamStmt: streams["src2"],
  1925. metaFields: []string{},
  1926. }.Init(),
  1927. DataSourcePlan{
  1928. name: "tableInPlanner",
  1929. streamFields: []interface{}{
  1930. &ast.StreamField{
  1931. Name: "hum",
  1932. FieldType: &ast.BasicType{Type: ast.BIGINT},
  1933. },
  1934. &ast.StreamField{
  1935. Name: "id",
  1936. FieldType: &ast.BasicType{Type: ast.BIGINT},
  1937. },
  1938. },
  1939. streamStmt: streams["tableInPlanner"],
  1940. metaFields: []string{},
  1941. }.Init(),
  1942. },
  1943. },
  1944. Emitters: []string{"tableInPlanner"},
  1945. }.Init(),
  1946. },
  1947. },
  1948. from: &ast.Table{
  1949. Name: "src2",
  1950. },
  1951. joins: []ast.Join{
  1952. {
  1953. Name: "tableInPlanner",
  1954. Alias: "",
  1955. JoinType: ast.INNER_JOIN,
  1956. Expr: &ast.BinaryExpr{
  1957. RHS: &ast.BinaryExpr{
  1958. OP: ast.EQ,
  1959. LHS: &ast.FieldRef{Name: "id2", StreamName: ast.DefaultStream},
  1960. RHS: &ast.FieldRef{Name: "id", StreamName: ast.DefaultStream},
  1961. },
  1962. OP: ast.AND,
  1963. LHS: &ast.BinaryExpr{
  1964. OP: ast.GT,
  1965. LHS: &ast.FieldRef{Name: "hum1", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  1966. &ast.FieldRef{
  1967. Name: "hum",
  1968. StreamName: "src2",
  1969. },
  1970. []ast.StreamName{"src2"},
  1971. &boolFalse,
  1972. )},
  1973. RHS: &ast.FieldRef{Name: "hum2", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  1974. &ast.FieldRef{
  1975. Name: "hum",
  1976. StreamName: "tableInPlanner",
  1977. },
  1978. []ast.StreamName{"tableInPlanner"},
  1979. &boolFalse,
  1980. )},
  1981. },
  1982. },
  1983. },
  1984. },
  1985. }.Init(),
  1986. },
  1987. },
  1988. fields: []ast.Field{
  1989. {
  1990. Expr: &ast.FieldRef{Name: "hum1", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  1991. &ast.FieldRef{
  1992. Name: "hum",
  1993. StreamName: "src2",
  1994. },
  1995. []ast.StreamName{"src2"},
  1996. &boolFalse,
  1997. )},
  1998. Name: "hum",
  1999. AName: "hum1",
  2000. }, {
  2001. Expr: &ast.FieldRef{Name: "hum2", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  2002. &ast.FieldRef{
  2003. Name: "hum",
  2004. StreamName: "tableInPlanner",
  2005. },
  2006. []ast.StreamName{"tableInPlanner"},
  2007. &boolFalse,
  2008. )},
  2009. Name: "hum",
  2010. AName: "hum2",
  2011. },
  2012. },
  2013. isAggregate: false,
  2014. sendMeta: false,
  2015. }.Init(),
  2016. }, { // 12
  2017. sql: `SELECT name->first, name->last FROM src1`,
  2018. p: ProjectPlan{
  2019. baseLogicalPlan: baseLogicalPlan{
  2020. children: []LogicalPlan{
  2021. DataSourcePlan{
  2022. baseLogicalPlan: baseLogicalPlan{},
  2023. name: "src1",
  2024. streamFields: []interface{}{
  2025. "name",
  2026. },
  2027. streamStmt: streams["src1"],
  2028. metaFields: []string{},
  2029. }.Init(),
  2030. },
  2031. },
  2032. fields: []ast.Field{
  2033. {
  2034. Expr: &ast.BinaryExpr{
  2035. OP: ast.ARROW,
  2036. LHS: &ast.FieldRef{StreamName: "src1", Name: "name"},
  2037. RHS: &ast.JsonFieldRef{Name: "first"},
  2038. },
  2039. Name: "kuiper_field_0",
  2040. AName: "",
  2041. }, {
  2042. Expr: &ast.BinaryExpr{
  2043. OP: ast.ARROW,
  2044. LHS: &ast.FieldRef{StreamName: "src1", Name: "name"},
  2045. RHS: &ast.JsonFieldRef{Name: "last"},
  2046. },
  2047. Name: "kuiper_field_1",
  2048. AName: "",
  2049. },
  2050. },
  2051. isAggregate: false,
  2052. sendMeta: false,
  2053. }.Init(),
  2054. },
  2055. }
  2056. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  2057. for i, tt := range tests {
  2058. stmt, err := xsql.NewParser(strings.NewReader(tt.sql)).Parse()
  2059. if err != nil {
  2060. t.Errorf("%d. %q: error compile sql: %s\n", i, tt.sql, err)
  2061. continue
  2062. }
  2063. p, err := createLogicalPlan(stmt, &api.RuleOption{
  2064. IsEventTime: false,
  2065. LateTol: 0,
  2066. Concurrency: 0,
  2067. BufferLength: 0,
  2068. SendMetaToSink: false,
  2069. Qos: 0,
  2070. CheckpointInterval: 0,
  2071. SendError: true,
  2072. }, store)
  2073. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  2074. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.sql, tt.err, err)
  2075. } else if !reflect.DeepEqual(tt.p, p) {
  2076. 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))
  2077. }
  2078. }
  2079. }