planner_test.go 60 KB

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