planner_test.go 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982
  1. // Copyright 2021 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/sqlkv"
  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. store, err := sqlkv.GetKVStore("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. store.Set(name, string(s))
  70. }
  71. streams := make(map[string]*ast.StreamStmt)
  72. for n := range streamSqls {
  73. streamStmt, err := xsql.GetDataSource(store, n)
  74. if err != nil {
  75. t.Errorf("fail to get stream %s, please check if stream is created", n)
  76. return
  77. }
  78. streams[n] = streamStmt
  79. }
  80. var (
  81. //boolTrue = true
  82. boolFalse = false
  83. )
  84. var tests = []struct {
  85. sql string
  86. p LogicalPlan
  87. err string
  88. }{
  89. { // 0
  90. sql: `SELECT myarray[temp] FROM src1`,
  91. p: ProjectPlan{
  92. baseLogicalPlan: baseLogicalPlan{
  93. children: []LogicalPlan{
  94. DataSourcePlan{
  95. baseLogicalPlan: baseLogicalPlan{},
  96. name: "src1",
  97. streamFields: []interface{}{
  98. &ast.StreamField{
  99. Name: "myarray",
  100. FieldType: &ast.ArrayType{Type: ast.STRINGS},
  101. },
  102. &ast.StreamField{
  103. Name: "temp",
  104. FieldType: &ast.BasicType{Type: ast.BIGINT},
  105. },
  106. },
  107. streamStmt: streams["src1"],
  108. metaFields: []string{},
  109. }.Init(),
  110. },
  111. },
  112. fields: []ast.Field{
  113. {
  114. Expr: &ast.BinaryExpr{
  115. OP: ast.SUBSET,
  116. LHS: &ast.FieldRef{
  117. StreamName: "src1",
  118. Name: "myarray",
  119. },
  120. RHS: &ast.IndexExpr{Index: &ast.FieldRef{
  121. StreamName: "src1",
  122. Name: "temp",
  123. }},
  124. },
  125. Name: "kuiper_field_0",
  126. AName: "",
  127. },
  128. },
  129. isAggregate: false,
  130. sendMeta: false,
  131. }.Init(),
  132. }, { // 1 optimize where to data source
  133. sql: `SELECT temp FROM src1 WHERE name = "v1" GROUP BY TUMBLINGWINDOW(ss, 10)`,
  134. p: ProjectPlan{
  135. baseLogicalPlan: baseLogicalPlan{
  136. children: []LogicalPlan{
  137. WindowPlan{
  138. baseLogicalPlan: baseLogicalPlan{
  139. children: []LogicalPlan{
  140. FilterPlan{
  141. baseLogicalPlan: baseLogicalPlan{
  142. children: []LogicalPlan{
  143. DataSourcePlan{
  144. name: "src1",
  145. streamFields: []interface{}{
  146. &ast.StreamField{
  147. Name: "name",
  148. FieldType: &ast.BasicType{Type: ast.STRINGS},
  149. },
  150. &ast.StreamField{
  151. Name: "temp",
  152. FieldType: &ast.BasicType{Type: ast.BIGINT},
  153. },
  154. },
  155. streamStmt: streams["src1"],
  156. metaFields: []string{},
  157. }.Init(),
  158. },
  159. },
  160. condition: &ast.BinaryExpr{
  161. LHS: &ast.FieldRef{Name: "name", StreamName: "src1"},
  162. OP: ast.EQ,
  163. RHS: &ast.StringLiteral{Val: "v1"},
  164. },
  165. }.Init(),
  166. },
  167. },
  168. condition: nil,
  169. wtype: ast.TUMBLING_WINDOW,
  170. length: 10000,
  171. interval: 0,
  172. limit: 0,
  173. }.Init(),
  174. },
  175. },
  176. fields: []ast.Field{
  177. {
  178. Expr: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  179. Name: "temp",
  180. AName: ""},
  181. },
  182. isAggregate: false,
  183. sendMeta: false,
  184. }.Init(),
  185. }, { // 2 condition that cannot be optimized
  186. 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)`,
  187. p: ProjectPlan{
  188. baseLogicalPlan: baseLogicalPlan{
  189. children: []LogicalPlan{
  190. JoinPlan{
  191. baseLogicalPlan: baseLogicalPlan{
  192. children: []LogicalPlan{
  193. WindowPlan{
  194. baseLogicalPlan: baseLogicalPlan{
  195. children: []LogicalPlan{
  196. DataSourcePlan{
  197. name: "src1",
  198. streamFields: []interface{}{
  199. &ast.StreamField{
  200. Name: "id1",
  201. FieldType: &ast.BasicType{Type: ast.BIGINT},
  202. },
  203. &ast.StreamField{
  204. Name: "temp",
  205. FieldType: &ast.BasicType{Type: ast.BIGINT},
  206. },
  207. },
  208. streamStmt: streams["src1"],
  209. metaFields: []string{},
  210. }.Init(),
  211. DataSourcePlan{
  212. name: "src2",
  213. streamFields: []interface{}{
  214. &ast.StreamField{
  215. Name: "hum",
  216. FieldType: &ast.BasicType{Type: ast.BIGINT},
  217. },
  218. &ast.StreamField{
  219. Name: "id2",
  220. FieldType: &ast.BasicType{Type: ast.BIGINT},
  221. },
  222. },
  223. streamStmt: streams["src2"],
  224. metaFields: []string{},
  225. timestampFormat: "YYYY-MM-dd HH:mm:ss",
  226. }.Init(),
  227. },
  228. },
  229. condition: nil,
  230. wtype: ast.TUMBLING_WINDOW,
  231. length: 10000,
  232. interval: 0,
  233. limit: 0,
  234. }.Init(),
  235. },
  236. },
  237. from: &ast.Table{Name: "src1"},
  238. joins: ast.Joins{ast.Join{
  239. Name: "src2",
  240. JoinType: ast.INNER_JOIN,
  241. Expr: &ast.BinaryExpr{
  242. OP: ast.AND,
  243. LHS: &ast.BinaryExpr{
  244. LHS: &ast.BinaryExpr{
  245. OP: ast.GT,
  246. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  247. RHS: &ast.IntegerLiteral{Val: 20},
  248. },
  249. OP: ast.OR,
  250. RHS: &ast.BinaryExpr{
  251. OP: ast.GT,
  252. LHS: &ast.FieldRef{Name: "hum", StreamName: "src2"},
  253. RHS: &ast.IntegerLiteral{Val: 60},
  254. },
  255. },
  256. RHS: &ast.BinaryExpr{
  257. OP: ast.EQ,
  258. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  259. RHS: &ast.FieldRef{Name: "id2", StreamName: "src2"},
  260. },
  261. },
  262. }},
  263. }.Init(),
  264. },
  265. },
  266. fields: []ast.Field{
  267. {
  268. Expr: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  269. Name: "id1",
  270. AName: ""},
  271. },
  272. isAggregate: false,
  273. sendMeta: false,
  274. }.Init(),
  275. }, { // 3 optimize window filter
  276. sql: `SELECT id1 FROM src1 WHERE name = "v1" GROUP BY TUMBLINGWINDOW(ss, 10) FILTER( WHERE temp > 2)`,
  277. p: ProjectPlan{
  278. baseLogicalPlan: baseLogicalPlan{
  279. children: []LogicalPlan{
  280. WindowPlan{
  281. baseLogicalPlan: baseLogicalPlan{
  282. children: []LogicalPlan{
  283. FilterPlan{
  284. baseLogicalPlan: baseLogicalPlan{
  285. children: []LogicalPlan{
  286. DataSourcePlan{
  287. name: "src1",
  288. streamFields: []interface{}{
  289. &ast.StreamField{
  290. Name: "id1",
  291. FieldType: &ast.BasicType{Type: ast.BIGINT},
  292. },
  293. &ast.StreamField{
  294. Name: "name",
  295. FieldType: &ast.BasicType{Type: ast.STRINGS},
  296. },
  297. &ast.StreamField{
  298. Name: "temp",
  299. FieldType: &ast.BasicType{Type: ast.BIGINT},
  300. },
  301. },
  302. streamStmt: streams["src1"],
  303. metaFields: []string{},
  304. }.Init(),
  305. },
  306. },
  307. condition: &ast.BinaryExpr{
  308. OP: ast.AND,
  309. LHS: &ast.BinaryExpr{
  310. LHS: &ast.FieldRef{Name: "name", StreamName: "src1"},
  311. OP: ast.EQ,
  312. RHS: &ast.StringLiteral{Val: "v1"},
  313. },
  314. RHS: &ast.BinaryExpr{
  315. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  316. OP: ast.GT,
  317. RHS: &ast.IntegerLiteral{Val: 2},
  318. },
  319. },
  320. }.Init(),
  321. },
  322. },
  323. condition: nil,
  324. wtype: ast.TUMBLING_WINDOW,
  325. length: 10000,
  326. interval: 0,
  327. limit: 0,
  328. }.Init(),
  329. },
  330. },
  331. fields: []ast.Field{
  332. {
  333. Expr: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  334. Name: "id1",
  335. AName: ""},
  336. },
  337. isAggregate: false,
  338. sendMeta: false,
  339. }.Init(),
  340. }, { // 4. do not optimize count window
  341. sql: `SELECT * FROM src1 WHERE temp > 20 GROUP BY COUNTWINDOW(5,1) HAVING COUNT(*) > 2`,
  342. p: ProjectPlan{
  343. baseLogicalPlan: baseLogicalPlan{
  344. children: []LogicalPlan{
  345. HavingPlan{
  346. baseLogicalPlan: baseLogicalPlan{
  347. children: []LogicalPlan{
  348. FilterPlan{
  349. baseLogicalPlan: baseLogicalPlan{
  350. children: []LogicalPlan{
  351. WindowPlan{
  352. baseLogicalPlan: baseLogicalPlan{
  353. children: []LogicalPlan{
  354. DataSourcePlan{
  355. name: "src1",
  356. isWildCard: true,
  357. streamFields: []interface{}{
  358. &ast.StreamField{
  359. Name: "id1",
  360. FieldType: &ast.BasicType{Type: ast.BIGINT},
  361. },
  362. &ast.StreamField{
  363. Name: "temp",
  364. FieldType: &ast.BasicType{Type: ast.BIGINT},
  365. },
  366. &ast.StreamField{
  367. Name: "name",
  368. FieldType: &ast.BasicType{Type: ast.STRINGS},
  369. },
  370. &ast.StreamField{
  371. Name: "myarray",
  372. FieldType: &ast.ArrayType{Type: ast.STRINGS},
  373. },
  374. },
  375. streamStmt: streams["src1"],
  376. metaFields: []string{},
  377. }.Init(),
  378. },
  379. },
  380. condition: nil,
  381. wtype: ast.COUNT_WINDOW,
  382. length: 5,
  383. interval: 1,
  384. limit: 0,
  385. }.Init(),
  386. },
  387. },
  388. condition: &ast.BinaryExpr{
  389. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  390. OP: ast.GT,
  391. RHS: &ast.IntegerLiteral{Val: 20},
  392. },
  393. }.Init(),
  394. },
  395. },
  396. condition: &ast.BinaryExpr{
  397. LHS: &ast.Call{Name: "COUNT", Args: []ast.Expr{&ast.Wildcard{
  398. Token: ast.ASTERISK,
  399. }}},
  400. OP: ast.GT,
  401. RHS: &ast.IntegerLiteral{Val: 2},
  402. },
  403. }.Init(),
  404. },
  405. },
  406. fields: []ast.Field{
  407. {
  408. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  409. Name: "kuiper_field_0",
  410. AName: ""},
  411. },
  412. isAggregate: false,
  413. sendMeta: false,
  414. }.Init(),
  415. }, { // 5. optimize join on
  416. 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)`,
  417. p: ProjectPlan{
  418. baseLogicalPlan: baseLogicalPlan{
  419. children: []LogicalPlan{
  420. JoinPlan{
  421. baseLogicalPlan: baseLogicalPlan{
  422. children: []LogicalPlan{
  423. WindowPlan{
  424. baseLogicalPlan: baseLogicalPlan{
  425. children: []LogicalPlan{
  426. FilterPlan{
  427. baseLogicalPlan: baseLogicalPlan{
  428. children: []LogicalPlan{
  429. DataSourcePlan{
  430. name: "src1",
  431. streamFields: []interface{}{
  432. &ast.StreamField{
  433. Name: "id1",
  434. FieldType: &ast.BasicType{Type: ast.BIGINT},
  435. },
  436. &ast.StreamField{
  437. Name: "temp",
  438. FieldType: &ast.BasicType{Type: ast.BIGINT},
  439. },
  440. },
  441. streamStmt: streams["src1"],
  442. metaFields: []string{},
  443. }.Init(),
  444. },
  445. },
  446. condition: &ast.BinaryExpr{
  447. RHS: &ast.BinaryExpr{
  448. OP: ast.GT,
  449. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  450. RHS: &ast.IntegerLiteral{Val: 20},
  451. },
  452. OP: ast.AND,
  453. LHS: &ast.BinaryExpr{
  454. OP: ast.GT,
  455. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  456. RHS: &ast.IntegerLiteral{Val: 111},
  457. },
  458. },
  459. }.Init(),
  460. FilterPlan{
  461. baseLogicalPlan: baseLogicalPlan{
  462. children: []LogicalPlan{
  463. DataSourcePlan{
  464. name: "src2",
  465. streamFields: []interface{}{
  466. &ast.StreamField{
  467. Name: "hum",
  468. FieldType: &ast.BasicType{Type: ast.BIGINT},
  469. },
  470. &ast.StreamField{
  471. Name: "id2",
  472. FieldType: &ast.BasicType{Type: ast.BIGINT},
  473. },
  474. },
  475. streamStmt: streams["src2"],
  476. metaFields: []string{},
  477. timestampFormat: "YYYY-MM-dd HH:mm:ss",
  478. }.Init(),
  479. },
  480. },
  481. condition: &ast.BinaryExpr{
  482. OP: ast.LT,
  483. LHS: &ast.FieldRef{Name: "hum", StreamName: "src2"},
  484. RHS: &ast.IntegerLiteral{Val: 60},
  485. },
  486. }.Init(),
  487. },
  488. },
  489. condition: nil,
  490. wtype: ast.TUMBLING_WINDOW,
  491. length: 10000,
  492. interval: 0,
  493. limit: 0,
  494. }.Init(),
  495. },
  496. },
  497. from: &ast.Table{
  498. Name: "src1",
  499. },
  500. joins: []ast.Join{
  501. {
  502. Name: "src2",
  503. Alias: "",
  504. JoinType: ast.INNER_JOIN,
  505. Expr: &ast.BinaryExpr{
  506. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  507. OP: ast.EQ,
  508. RHS: &ast.FieldRef{Name: "id2", StreamName: "src2"},
  509. },
  510. },
  511. },
  512. }.Init(),
  513. },
  514. },
  515. fields: []ast.Field{
  516. {
  517. Expr: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  518. Name: "id1",
  519. AName: ""},
  520. },
  521. isAggregate: false,
  522. sendMeta: false,
  523. }.Init(),
  524. }, { // 6. optimize outter join on
  525. 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)`,
  526. p: ProjectPlan{
  527. baseLogicalPlan: baseLogicalPlan{
  528. children: []LogicalPlan{
  529. JoinPlan{
  530. baseLogicalPlan: baseLogicalPlan{
  531. children: []LogicalPlan{
  532. WindowPlan{
  533. baseLogicalPlan: baseLogicalPlan{
  534. children: []LogicalPlan{
  535. FilterPlan{
  536. baseLogicalPlan: baseLogicalPlan{
  537. children: []LogicalPlan{
  538. DataSourcePlan{
  539. name: "src1",
  540. streamFields: []interface{}{
  541. &ast.StreamField{
  542. Name: "id1",
  543. FieldType: &ast.BasicType{Type: ast.BIGINT},
  544. },
  545. &ast.StreamField{
  546. Name: "temp",
  547. FieldType: &ast.BasicType{Type: ast.BIGINT},
  548. },
  549. },
  550. streamStmt: streams["src1"],
  551. metaFields: []string{},
  552. }.Init(),
  553. },
  554. },
  555. condition: &ast.BinaryExpr{
  556. OP: ast.GT,
  557. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  558. RHS: &ast.IntegerLiteral{Val: 111},
  559. },
  560. }.Init(),
  561. DataSourcePlan{
  562. name: "src2",
  563. streamFields: []interface{}{
  564. &ast.StreamField{
  565. Name: "hum",
  566. FieldType: &ast.BasicType{Type: ast.BIGINT},
  567. },
  568. &ast.StreamField{
  569. Name: "id2",
  570. FieldType: &ast.BasicType{Type: ast.BIGINT},
  571. },
  572. },
  573. streamStmt: streams["src2"],
  574. metaFields: []string{},
  575. timestampFormat: "YYYY-MM-dd HH:mm:ss",
  576. }.Init(),
  577. },
  578. },
  579. condition: nil,
  580. wtype: ast.TUMBLING_WINDOW,
  581. length: 10000,
  582. interval: 0,
  583. limit: 0,
  584. }.Init(),
  585. },
  586. },
  587. from: &ast.Table{
  588. Name: "src1",
  589. },
  590. joins: []ast.Join{
  591. {
  592. Name: "src2",
  593. Alias: "",
  594. JoinType: ast.FULL_JOIN,
  595. Expr: &ast.BinaryExpr{
  596. OP: ast.AND,
  597. LHS: &ast.BinaryExpr{
  598. OP: ast.AND,
  599. LHS: &ast.BinaryExpr{
  600. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  601. OP: ast.EQ,
  602. RHS: &ast.FieldRef{Name: "id2", StreamName: "src2"},
  603. },
  604. RHS: &ast.BinaryExpr{
  605. OP: ast.GT,
  606. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  607. RHS: &ast.IntegerLiteral{Val: 20},
  608. },
  609. },
  610. RHS: &ast.BinaryExpr{
  611. OP: ast.LT,
  612. LHS: &ast.FieldRef{Name: "hum", StreamName: "src2"},
  613. RHS: &ast.IntegerLiteral{Val: 60},
  614. },
  615. },
  616. },
  617. },
  618. }.Init(),
  619. },
  620. },
  621. fields: []ast.Field{
  622. {
  623. Expr: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  624. Name: "id1",
  625. AName: ""},
  626. },
  627. isAggregate: false,
  628. sendMeta: false,
  629. }.Init(),
  630. }, { // 7 window error for table
  631. sql: `SELECT value FROM tableInPlanner WHERE name = "v1" GROUP BY TUMBLINGWINDOW(ss, 10)`,
  632. p: nil,
  633. err: "cannot run window for TABLE sources",
  634. }, { // 8 join table without window
  635. sql: `SELECT id1 FROM src1 INNER JOIN tableInPlanner on src1.id1 = tableInPlanner.id and src1.temp > 20 and hum < 60 WHERE src1.id1 > 111`,
  636. p: ProjectPlan{
  637. baseLogicalPlan: baseLogicalPlan{
  638. children: []LogicalPlan{
  639. JoinPlan{
  640. baseLogicalPlan: baseLogicalPlan{
  641. children: []LogicalPlan{
  642. JoinAlignPlan{
  643. baseLogicalPlan: baseLogicalPlan{
  644. children: []LogicalPlan{
  645. FilterPlan{
  646. baseLogicalPlan: baseLogicalPlan{
  647. children: []LogicalPlan{
  648. DataSourcePlan{
  649. name: "src1",
  650. streamFields: []interface{}{
  651. &ast.StreamField{
  652. Name: "id1",
  653. FieldType: &ast.BasicType{Type: ast.BIGINT},
  654. },
  655. &ast.StreamField{
  656. Name: "temp",
  657. FieldType: &ast.BasicType{Type: ast.BIGINT},
  658. },
  659. },
  660. streamStmt: streams["src1"],
  661. metaFields: []string{},
  662. }.Init(),
  663. },
  664. },
  665. condition: &ast.BinaryExpr{
  666. RHS: &ast.BinaryExpr{
  667. OP: ast.GT,
  668. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  669. RHS: &ast.IntegerLiteral{Val: 20},
  670. },
  671. OP: ast.AND,
  672. LHS: &ast.BinaryExpr{
  673. OP: ast.GT,
  674. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  675. RHS: &ast.IntegerLiteral{Val: 111},
  676. },
  677. },
  678. }.Init(),
  679. FilterPlan{
  680. baseLogicalPlan: baseLogicalPlan{
  681. children: []LogicalPlan{
  682. DataSourcePlan{
  683. name: "tableInPlanner",
  684. streamFields: []interface{}{
  685. &ast.StreamField{
  686. Name: "hum",
  687. FieldType: &ast.BasicType{Type: ast.BIGINT},
  688. },
  689. &ast.StreamField{
  690. Name: "id",
  691. FieldType: &ast.BasicType{Type: ast.BIGINT},
  692. },
  693. },
  694. streamStmt: streams["tableInPlanner"],
  695. metaFields: []string{},
  696. }.Init(),
  697. },
  698. },
  699. condition: &ast.BinaryExpr{
  700. OP: ast.LT,
  701. LHS: &ast.FieldRef{Name: "hum", StreamName: "tableInPlanner"},
  702. RHS: &ast.IntegerLiteral{Val: 60},
  703. },
  704. }.Init(),
  705. },
  706. },
  707. Emitters: []string{"tableInPlanner"},
  708. }.Init(),
  709. },
  710. },
  711. from: &ast.Table{
  712. Name: "src1",
  713. },
  714. joins: []ast.Join{
  715. {
  716. Name: "tableInPlanner",
  717. Alias: "",
  718. JoinType: ast.INNER_JOIN,
  719. Expr: &ast.BinaryExpr{
  720. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  721. OP: ast.EQ,
  722. RHS: &ast.FieldRef{Name: "id", StreamName: "tableInPlanner"},
  723. },
  724. },
  725. },
  726. }.Init(),
  727. },
  728. },
  729. fields: []ast.Field{
  730. {
  731. Expr: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  732. Name: "id1",
  733. AName: ""},
  734. },
  735. isAggregate: false,
  736. sendMeta: false,
  737. }.Init(),
  738. }, { // 9 join table with window
  739. 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)`,
  740. p: ProjectPlan{
  741. baseLogicalPlan: baseLogicalPlan{
  742. children: []LogicalPlan{
  743. JoinPlan{
  744. baseLogicalPlan: baseLogicalPlan{
  745. children: []LogicalPlan{
  746. JoinAlignPlan{
  747. baseLogicalPlan: baseLogicalPlan{
  748. children: []LogicalPlan{
  749. WindowPlan{
  750. baseLogicalPlan: baseLogicalPlan{
  751. children: []LogicalPlan{
  752. FilterPlan{
  753. baseLogicalPlan: baseLogicalPlan{
  754. children: []LogicalPlan{
  755. DataSourcePlan{
  756. name: "src1",
  757. streamFields: []interface{}{
  758. &ast.StreamField{
  759. Name: "id1",
  760. FieldType: &ast.BasicType{Type: ast.BIGINT},
  761. },
  762. &ast.StreamField{
  763. Name: "temp",
  764. FieldType: &ast.BasicType{Type: ast.BIGINT},
  765. },
  766. },
  767. streamStmt: streams["src1"],
  768. metaFields: []string{},
  769. }.Init(),
  770. },
  771. },
  772. condition: &ast.BinaryExpr{
  773. RHS: &ast.BinaryExpr{
  774. OP: ast.GT,
  775. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  776. RHS: &ast.IntegerLiteral{Val: 20},
  777. },
  778. OP: ast.AND,
  779. LHS: &ast.BinaryExpr{
  780. OP: ast.GT,
  781. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  782. RHS: &ast.IntegerLiteral{Val: 111},
  783. },
  784. },
  785. }.Init(),
  786. },
  787. },
  788. condition: nil,
  789. wtype: ast.TUMBLING_WINDOW,
  790. length: 10000,
  791. interval: 0,
  792. limit: 0,
  793. }.Init(),
  794. FilterPlan{
  795. baseLogicalPlan: baseLogicalPlan{
  796. children: []LogicalPlan{
  797. DataSourcePlan{
  798. name: "tableInPlanner",
  799. streamFields: []interface{}{
  800. &ast.StreamField{
  801. Name: "hum",
  802. FieldType: &ast.BasicType{Type: ast.BIGINT},
  803. },
  804. &ast.StreamField{
  805. Name: "id",
  806. FieldType: &ast.BasicType{Type: ast.BIGINT},
  807. },
  808. },
  809. streamStmt: streams["tableInPlanner"],
  810. metaFields: []string{},
  811. }.Init(),
  812. },
  813. },
  814. condition: &ast.BinaryExpr{
  815. OP: ast.LT,
  816. LHS: &ast.FieldRef{Name: "hum", StreamName: "tableInPlanner"},
  817. RHS: &ast.IntegerLiteral{Val: 60},
  818. },
  819. }.Init(),
  820. },
  821. },
  822. Emitters: []string{"tableInPlanner"},
  823. }.Init(),
  824. },
  825. },
  826. from: &ast.Table{
  827. Name: "src1",
  828. },
  829. joins: []ast.Join{
  830. {
  831. Name: "tableInPlanner",
  832. Alias: "",
  833. JoinType: ast.INNER_JOIN,
  834. Expr: &ast.BinaryExpr{
  835. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  836. OP: ast.EQ,
  837. RHS: &ast.FieldRef{Name: "id", StreamName: "tableInPlanner"},
  838. },
  839. },
  840. },
  841. }.Init(),
  842. },
  843. },
  844. fields: []ast.Field{
  845. {
  846. Expr: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  847. Name: "id1",
  848. AName: ""},
  849. },
  850. isAggregate: false,
  851. sendMeta: false,
  852. }.Init(),
  853. }, { // 10 meta
  854. sql: `SELECT temp, meta(id) AS eid,meta(Humidity->Device) AS hdevice FROM src1 WHERE meta(device)="demo2"`,
  855. p: ProjectPlan{
  856. baseLogicalPlan: baseLogicalPlan{
  857. children: []LogicalPlan{
  858. FilterPlan{
  859. baseLogicalPlan: baseLogicalPlan{
  860. children: []LogicalPlan{
  861. DataSourcePlan{
  862. name: "src1",
  863. streamFields: []interface{}{
  864. &ast.StreamField{
  865. Name: "temp",
  866. FieldType: &ast.BasicType{Type: ast.BIGINT},
  867. },
  868. },
  869. streamStmt: streams["src1"],
  870. metaFields: []string{"Humidity", "device", "id"},
  871. }.Init(),
  872. },
  873. },
  874. condition: &ast.BinaryExpr{
  875. LHS: &ast.Call{
  876. Name: "meta",
  877. Args: []ast.Expr{&ast.MetaRef{
  878. Name: "device",
  879. StreamName: ast.DefaultStream,
  880. }},
  881. },
  882. OP: ast.EQ,
  883. RHS: &ast.StringLiteral{
  884. Val: "demo2",
  885. },
  886. },
  887. }.Init(),
  888. },
  889. },
  890. fields: []ast.Field{
  891. {
  892. Expr: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  893. Name: "temp",
  894. AName: "",
  895. }, {
  896. Expr: &ast.FieldRef{Name: "eid", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  897. &ast.Call{Name: "meta", Args: []ast.Expr{&ast.MetaRef{
  898. Name: "id",
  899. StreamName: ast.DefaultStream,
  900. }}},
  901. []ast.StreamName{},
  902. nil,
  903. )},
  904. Name: "meta",
  905. AName: "eid",
  906. }, {
  907. Expr: &ast.FieldRef{Name: "hdevice", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  908. &ast.Call{Name: "meta", Args: []ast.Expr{
  909. &ast.BinaryExpr{
  910. OP: ast.ARROW,
  911. LHS: &ast.MetaRef{Name: "Humidity", StreamName: ast.DefaultStream},
  912. RHS: &ast.JsonFieldRef{Name: "Device"},
  913. },
  914. }},
  915. []ast.StreamName{},
  916. nil,
  917. )},
  918. Name: "meta",
  919. AName: "hdevice",
  920. },
  921. },
  922. isAggregate: false,
  923. sendMeta: false,
  924. }.Init(),
  925. }, { // 11 join with same name field and aliased
  926. sql: `SELECT src2.hum AS hum1, tableInPlanner.hum AS hum2 FROM src2 INNER JOIN tableInPlanner on id2 = id WHERE hum1 > hum2`,
  927. p: ProjectPlan{
  928. baseLogicalPlan: baseLogicalPlan{
  929. children: []LogicalPlan{
  930. JoinPlan{
  931. baseLogicalPlan: baseLogicalPlan{
  932. children: []LogicalPlan{
  933. JoinAlignPlan{
  934. baseLogicalPlan: baseLogicalPlan{
  935. children: []LogicalPlan{
  936. DataSourcePlan{
  937. name: "src2",
  938. streamFields: []interface{}{
  939. &ast.StreamField{
  940. Name: "hum",
  941. FieldType: &ast.BasicType{Type: ast.BIGINT},
  942. },
  943. &ast.StreamField{
  944. Name: "id2",
  945. FieldType: &ast.BasicType{Type: ast.BIGINT},
  946. },
  947. },
  948. streamStmt: streams["src2"],
  949. metaFields: []string{},
  950. timestampFormat: "YYYY-MM-dd HH:mm:ss",
  951. }.Init(),
  952. DataSourcePlan{
  953. name: "tableInPlanner",
  954. streamFields: []interface{}{
  955. &ast.StreamField{
  956. Name: "hum",
  957. FieldType: &ast.BasicType{Type: ast.BIGINT},
  958. },
  959. &ast.StreamField{
  960. Name: "id",
  961. FieldType: &ast.BasicType{Type: ast.BIGINT},
  962. },
  963. },
  964. streamStmt: streams["tableInPlanner"],
  965. metaFields: []string{},
  966. }.Init(),
  967. },
  968. },
  969. Emitters: []string{"tableInPlanner"},
  970. }.Init(),
  971. },
  972. },
  973. from: &ast.Table{
  974. Name: "src2",
  975. },
  976. joins: []ast.Join{
  977. {
  978. Name: "tableInPlanner",
  979. Alias: "",
  980. JoinType: ast.INNER_JOIN,
  981. Expr: &ast.BinaryExpr{
  982. RHS: &ast.BinaryExpr{
  983. OP: ast.EQ,
  984. LHS: &ast.FieldRef{Name: "id2", StreamName: "src2"},
  985. RHS: &ast.FieldRef{Name: "id", StreamName: "tableInPlanner"},
  986. },
  987. OP: ast.AND,
  988. LHS: &ast.BinaryExpr{
  989. OP: ast.GT,
  990. LHS: &ast.FieldRef{Name: "hum1", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  991. &ast.FieldRef{
  992. Name: "hum",
  993. StreamName: "src2",
  994. },
  995. []ast.StreamName{"src2"},
  996. &boolFalse,
  997. )},
  998. RHS: &ast.FieldRef{Name: "hum2", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  999. &ast.FieldRef{
  1000. Name: "hum",
  1001. StreamName: "tableInPlanner",
  1002. },
  1003. []ast.StreamName{"tableInPlanner"},
  1004. &boolFalse,
  1005. )},
  1006. },
  1007. },
  1008. },
  1009. },
  1010. }.Init(),
  1011. },
  1012. },
  1013. fields: []ast.Field{
  1014. {
  1015. Expr: &ast.FieldRef{Name: "hum1", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  1016. &ast.FieldRef{
  1017. Name: "hum",
  1018. StreamName: "src2",
  1019. },
  1020. []ast.StreamName{"src2"},
  1021. &boolFalse,
  1022. )},
  1023. Name: "hum",
  1024. AName: "hum1",
  1025. }, {
  1026. Expr: &ast.FieldRef{Name: "hum2", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  1027. &ast.FieldRef{
  1028. Name: "hum",
  1029. StreamName: "tableInPlanner",
  1030. },
  1031. []ast.StreamName{"tableInPlanner"},
  1032. &boolFalse,
  1033. )},
  1034. Name: "hum",
  1035. AName: "hum2",
  1036. },
  1037. },
  1038. isAggregate: false,
  1039. sendMeta: false,
  1040. }.Init(),
  1041. },
  1042. }
  1043. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  1044. for i, tt := range tests {
  1045. stmt, err := xsql.NewParser(strings.NewReader(tt.sql)).Parse()
  1046. if err != nil {
  1047. t.Errorf("%d. %q: error compile sql: %s\n", i, tt.sql, err)
  1048. continue
  1049. }
  1050. p, err := createLogicalPlan(stmt, &api.RuleOption{
  1051. IsEventTime: false,
  1052. LateTol: 0,
  1053. Concurrency: 0,
  1054. BufferLength: 0,
  1055. SendMetaToSink: false,
  1056. Qos: 0,
  1057. CheckpointInterval: 0,
  1058. SendError: true,
  1059. }, store)
  1060. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  1061. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.sql, tt.err, err)
  1062. } else if !reflect.DeepEqual(tt.p, p) {
  1063. 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))
  1064. }
  1065. }
  1066. }
  1067. func Test_createLogicalPlanSchemaless(t *testing.T) {
  1068. store, err := sqlkv.GetKVStore("stream")
  1069. if err != nil {
  1070. t.Error(err)
  1071. return
  1072. }
  1073. streamSqls := map[string]string{
  1074. "src1": `CREATE STREAM src1 (
  1075. ) WITH (DATASOURCE="src1", FORMAT="json", KEY="ts");`,
  1076. "src2": `CREATE STREAM src2 (
  1077. ) WITH (DATASOURCE="src2", FORMAT="json", KEY="ts");`,
  1078. "tableInPlanner": `CREATE TABLE tableInPlanner (
  1079. id BIGINT,
  1080. name STRING,
  1081. value STRING,
  1082. hum BIGINT
  1083. ) WITH (TYPE="file");`,
  1084. }
  1085. types := map[string]ast.StreamType{
  1086. "src1": ast.TypeStream,
  1087. "src2": ast.TypeStream,
  1088. "tableInPlanner": ast.TypeTable,
  1089. }
  1090. for name, sql := range streamSqls {
  1091. s, err := json.Marshal(&xsql.StreamInfo{
  1092. StreamType: types[name],
  1093. Statement: sql,
  1094. })
  1095. if err != nil {
  1096. t.Error(err)
  1097. t.Fail()
  1098. }
  1099. store.Set(name, string(s))
  1100. }
  1101. streams := make(map[string]*ast.StreamStmt)
  1102. for n := range streamSqls {
  1103. streamStmt, err := xsql.GetDataSource(store, n)
  1104. if err != nil {
  1105. t.Errorf("fail to get stream %s, please check if stream is created", n)
  1106. return
  1107. }
  1108. streams[n] = streamStmt
  1109. }
  1110. var (
  1111. //boolTrue = true
  1112. boolFalse = false
  1113. )
  1114. var tests = []struct {
  1115. sql string
  1116. p LogicalPlan
  1117. err string
  1118. }{
  1119. { // 0
  1120. sql: `SELECT name FROM src1`,
  1121. p: ProjectPlan{
  1122. baseLogicalPlan: baseLogicalPlan{
  1123. children: []LogicalPlan{
  1124. DataSourcePlan{
  1125. baseLogicalPlan: baseLogicalPlan{},
  1126. name: "src1",
  1127. streamFields: []interface{}{
  1128. "name",
  1129. },
  1130. streamStmt: streams["src1"],
  1131. metaFields: []string{},
  1132. }.Init(),
  1133. },
  1134. },
  1135. fields: []ast.Field{
  1136. {
  1137. Expr: &ast.FieldRef{Name: "name", StreamName: "src1"},
  1138. Name: "name",
  1139. AName: "",
  1140. },
  1141. },
  1142. isAggregate: false,
  1143. sendMeta: false,
  1144. }.Init(),
  1145. }, { // 1 optimize where to data source
  1146. sql: `SELECT temp FROM src1 WHERE name = "v1" GROUP BY TUMBLINGWINDOW(ss, 10)`,
  1147. p: ProjectPlan{
  1148. baseLogicalPlan: baseLogicalPlan{
  1149. children: []LogicalPlan{
  1150. WindowPlan{
  1151. baseLogicalPlan: baseLogicalPlan{
  1152. children: []LogicalPlan{
  1153. FilterPlan{
  1154. baseLogicalPlan: baseLogicalPlan{
  1155. children: []LogicalPlan{
  1156. DataSourcePlan{
  1157. name: "src1",
  1158. streamFields: []interface{}{
  1159. "name", "temp",
  1160. },
  1161. streamStmt: streams["src1"],
  1162. metaFields: []string{},
  1163. }.Init(),
  1164. },
  1165. },
  1166. condition: &ast.BinaryExpr{
  1167. LHS: &ast.FieldRef{Name: "name", StreamName: "src1"},
  1168. OP: ast.EQ,
  1169. RHS: &ast.StringLiteral{Val: "v1"},
  1170. },
  1171. }.Init(),
  1172. },
  1173. },
  1174. condition: nil,
  1175. wtype: ast.TUMBLING_WINDOW,
  1176. length: 10000,
  1177. interval: 0,
  1178. limit: 0,
  1179. }.Init(),
  1180. },
  1181. },
  1182. fields: []ast.Field{
  1183. {
  1184. Expr: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  1185. Name: "temp",
  1186. AName: ""},
  1187. },
  1188. isAggregate: false,
  1189. sendMeta: false,
  1190. }.Init(),
  1191. }, { // 2 condition that cannot be optimized
  1192. 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)`,
  1193. p: ProjectPlan{
  1194. baseLogicalPlan: baseLogicalPlan{
  1195. children: []LogicalPlan{
  1196. JoinPlan{
  1197. baseLogicalPlan: baseLogicalPlan{
  1198. children: []LogicalPlan{
  1199. WindowPlan{
  1200. baseLogicalPlan: baseLogicalPlan{
  1201. children: []LogicalPlan{
  1202. DataSourcePlan{
  1203. name: "src1",
  1204. streamFields: []interface{}{
  1205. "id1", "temp",
  1206. },
  1207. streamStmt: streams["src1"],
  1208. metaFields: []string{},
  1209. }.Init(),
  1210. DataSourcePlan{
  1211. name: "src2",
  1212. streamFields: []interface{}{ // can't determine where is id1 belonged to
  1213. "hum", "id1", "id2",
  1214. },
  1215. streamStmt: streams["src2"],
  1216. metaFields: []string{},
  1217. }.Init(),
  1218. },
  1219. },
  1220. condition: nil,
  1221. wtype: ast.TUMBLING_WINDOW,
  1222. length: 10000,
  1223. interval: 0,
  1224. limit: 0,
  1225. }.Init(),
  1226. },
  1227. },
  1228. from: &ast.Table{Name: "src1"},
  1229. joins: ast.Joins{ast.Join{
  1230. Name: "src2",
  1231. JoinType: ast.INNER_JOIN,
  1232. Expr: &ast.BinaryExpr{
  1233. OP: ast.AND,
  1234. LHS: &ast.BinaryExpr{
  1235. LHS: &ast.BinaryExpr{
  1236. OP: ast.GT,
  1237. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  1238. RHS: &ast.IntegerLiteral{Val: 20},
  1239. },
  1240. OP: ast.OR,
  1241. RHS: &ast.BinaryExpr{
  1242. OP: ast.GT,
  1243. LHS: &ast.FieldRef{Name: "hum", StreamName: "src2"},
  1244. RHS: &ast.IntegerLiteral{Val: 60},
  1245. },
  1246. },
  1247. RHS: &ast.BinaryExpr{
  1248. OP: ast.EQ,
  1249. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  1250. RHS: &ast.FieldRef{Name: "id2", StreamName: "src2"},
  1251. },
  1252. },
  1253. }},
  1254. }.Init(),
  1255. },
  1256. },
  1257. fields: []ast.Field{
  1258. {
  1259. Expr: &ast.FieldRef{Name: "id1", StreamName: ast.DefaultStream},
  1260. Name: "id1",
  1261. AName: ""},
  1262. },
  1263. isAggregate: false,
  1264. sendMeta: false,
  1265. }.Init(),
  1266. }, { // 3 optimize window filter
  1267. sql: `SELECT id1 FROM src1 WHERE name = "v1" GROUP BY TUMBLINGWINDOW(ss, 10) FILTER( WHERE temp > 2)`,
  1268. p: ProjectPlan{
  1269. baseLogicalPlan: baseLogicalPlan{
  1270. children: []LogicalPlan{
  1271. WindowPlan{
  1272. baseLogicalPlan: baseLogicalPlan{
  1273. children: []LogicalPlan{
  1274. FilterPlan{
  1275. baseLogicalPlan: baseLogicalPlan{
  1276. children: []LogicalPlan{
  1277. DataSourcePlan{
  1278. name: "src1",
  1279. streamFields: []interface{}{
  1280. "id1", "name", "temp",
  1281. },
  1282. streamStmt: streams["src1"],
  1283. metaFields: []string{},
  1284. }.Init(),
  1285. },
  1286. },
  1287. condition: &ast.BinaryExpr{
  1288. OP: ast.AND,
  1289. LHS: &ast.BinaryExpr{
  1290. LHS: &ast.FieldRef{Name: "name", StreamName: "src1"},
  1291. OP: ast.EQ,
  1292. RHS: &ast.StringLiteral{Val: "v1"},
  1293. },
  1294. RHS: &ast.BinaryExpr{
  1295. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  1296. OP: ast.GT,
  1297. RHS: &ast.IntegerLiteral{Val: 2},
  1298. },
  1299. },
  1300. }.Init(),
  1301. },
  1302. },
  1303. condition: nil,
  1304. wtype: ast.TUMBLING_WINDOW,
  1305. length: 10000,
  1306. interval: 0,
  1307. limit: 0,
  1308. }.Init(),
  1309. },
  1310. },
  1311. fields: []ast.Field{
  1312. {
  1313. Expr: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  1314. Name: "id1",
  1315. AName: ""},
  1316. },
  1317. isAggregate: false,
  1318. sendMeta: false,
  1319. }.Init(),
  1320. }, { // 4. do not optimize count window
  1321. sql: `SELECT * FROM src1 WHERE temp > 20 GROUP BY COUNTWINDOW(5,1) HAVING COUNT(*) > 2`,
  1322. p: ProjectPlan{
  1323. baseLogicalPlan: baseLogicalPlan{
  1324. children: []LogicalPlan{
  1325. HavingPlan{
  1326. baseLogicalPlan: baseLogicalPlan{
  1327. children: []LogicalPlan{
  1328. FilterPlan{
  1329. baseLogicalPlan: baseLogicalPlan{
  1330. children: []LogicalPlan{
  1331. WindowPlan{
  1332. baseLogicalPlan: baseLogicalPlan{
  1333. children: []LogicalPlan{
  1334. DataSourcePlan{
  1335. name: "src1",
  1336. isWildCard: true,
  1337. streamFields: nil,
  1338. streamStmt: streams["src1"],
  1339. metaFields: []string{},
  1340. }.Init(),
  1341. },
  1342. },
  1343. condition: nil,
  1344. wtype: ast.COUNT_WINDOW,
  1345. length: 5,
  1346. interval: 1,
  1347. limit: 0,
  1348. }.Init(),
  1349. },
  1350. },
  1351. condition: &ast.BinaryExpr{
  1352. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  1353. OP: ast.GT,
  1354. RHS: &ast.IntegerLiteral{Val: 20},
  1355. },
  1356. }.Init(),
  1357. },
  1358. },
  1359. condition: &ast.BinaryExpr{
  1360. LHS: &ast.Call{Name: "COUNT", Args: []ast.Expr{&ast.Wildcard{
  1361. Token: ast.ASTERISK,
  1362. }}},
  1363. OP: ast.GT,
  1364. RHS: &ast.IntegerLiteral{Val: 2},
  1365. },
  1366. }.Init(),
  1367. },
  1368. },
  1369. fields: []ast.Field{
  1370. {
  1371. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  1372. Name: "kuiper_field_0",
  1373. AName: ""},
  1374. },
  1375. isAggregate: false,
  1376. sendMeta: false,
  1377. }.Init(),
  1378. }, { // 5. optimize join on
  1379. 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)`,
  1380. p: ProjectPlan{
  1381. baseLogicalPlan: baseLogicalPlan{
  1382. children: []LogicalPlan{
  1383. JoinPlan{
  1384. baseLogicalPlan: baseLogicalPlan{
  1385. children: []LogicalPlan{
  1386. WindowPlan{
  1387. baseLogicalPlan: baseLogicalPlan{
  1388. children: []LogicalPlan{
  1389. FilterPlan{
  1390. baseLogicalPlan: baseLogicalPlan{
  1391. children: []LogicalPlan{
  1392. DataSourcePlan{
  1393. name: "src1",
  1394. streamFields: []interface{}{
  1395. "id1", "temp",
  1396. },
  1397. streamStmt: streams["src1"],
  1398. metaFields: []string{},
  1399. }.Init(),
  1400. },
  1401. },
  1402. condition: &ast.BinaryExpr{
  1403. RHS: &ast.BinaryExpr{
  1404. OP: ast.GT,
  1405. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  1406. RHS: &ast.IntegerLiteral{Val: 20},
  1407. },
  1408. OP: ast.AND,
  1409. LHS: &ast.BinaryExpr{
  1410. OP: ast.GT,
  1411. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  1412. RHS: &ast.IntegerLiteral{Val: 111},
  1413. },
  1414. },
  1415. }.Init(),
  1416. FilterPlan{
  1417. baseLogicalPlan: baseLogicalPlan{
  1418. children: []LogicalPlan{
  1419. DataSourcePlan{
  1420. name: "src2",
  1421. streamFields: []interface{}{
  1422. "hum", "id1", "id2",
  1423. },
  1424. streamStmt: streams["src2"],
  1425. metaFields: []string{},
  1426. }.Init(),
  1427. },
  1428. },
  1429. condition: &ast.BinaryExpr{
  1430. OP: ast.LT,
  1431. LHS: &ast.FieldRef{Name: "hum", StreamName: "src2"},
  1432. RHS: &ast.IntegerLiteral{Val: 60},
  1433. },
  1434. }.Init(),
  1435. },
  1436. },
  1437. condition: nil,
  1438. wtype: ast.TUMBLING_WINDOW,
  1439. length: 10000,
  1440. interval: 0,
  1441. limit: 0,
  1442. }.Init(),
  1443. },
  1444. },
  1445. from: &ast.Table{
  1446. Name: "src1",
  1447. },
  1448. joins: []ast.Join{
  1449. {
  1450. Name: "src2",
  1451. Alias: "",
  1452. JoinType: ast.INNER_JOIN,
  1453. Expr: &ast.BinaryExpr{
  1454. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  1455. OP: ast.EQ,
  1456. RHS: &ast.FieldRef{Name: "id2", StreamName: "src2"},
  1457. },
  1458. },
  1459. },
  1460. }.Init(),
  1461. },
  1462. },
  1463. fields: []ast.Field{
  1464. {
  1465. Expr: &ast.FieldRef{Name: "id1", StreamName: ast.DefaultStream},
  1466. Name: "id1",
  1467. AName: ""},
  1468. },
  1469. isAggregate: false,
  1470. sendMeta: false,
  1471. }.Init(),
  1472. }, { // 6. optimize outter join on
  1473. 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)`,
  1474. p: ProjectPlan{
  1475. baseLogicalPlan: baseLogicalPlan{
  1476. children: []LogicalPlan{
  1477. JoinPlan{
  1478. baseLogicalPlan: baseLogicalPlan{
  1479. children: []LogicalPlan{
  1480. WindowPlan{
  1481. baseLogicalPlan: baseLogicalPlan{
  1482. children: []LogicalPlan{
  1483. FilterPlan{
  1484. baseLogicalPlan: baseLogicalPlan{
  1485. children: []LogicalPlan{
  1486. DataSourcePlan{
  1487. name: "src1",
  1488. streamFields: []interface{}{
  1489. "id1", "temp",
  1490. },
  1491. streamStmt: streams["src1"],
  1492. metaFields: []string{},
  1493. }.Init(),
  1494. },
  1495. },
  1496. condition: &ast.BinaryExpr{
  1497. OP: ast.GT,
  1498. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  1499. RHS: &ast.IntegerLiteral{Val: 111},
  1500. },
  1501. }.Init(),
  1502. DataSourcePlan{
  1503. name: "src2",
  1504. streamFields: []interface{}{
  1505. "hum", "id1", "id2",
  1506. },
  1507. streamStmt: streams["src2"],
  1508. metaFields: []string{},
  1509. }.Init(),
  1510. },
  1511. },
  1512. condition: nil,
  1513. wtype: ast.TUMBLING_WINDOW,
  1514. length: 10000,
  1515. interval: 0,
  1516. limit: 0,
  1517. }.Init(),
  1518. },
  1519. },
  1520. from: &ast.Table{
  1521. Name: "src1",
  1522. },
  1523. joins: []ast.Join{
  1524. {
  1525. Name: "src2",
  1526. Alias: "",
  1527. JoinType: ast.FULL_JOIN,
  1528. Expr: &ast.BinaryExpr{
  1529. OP: ast.AND,
  1530. LHS: &ast.BinaryExpr{
  1531. OP: ast.AND,
  1532. LHS: &ast.BinaryExpr{
  1533. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  1534. OP: ast.EQ,
  1535. RHS: &ast.FieldRef{Name: "id2", StreamName: "src2"},
  1536. },
  1537. RHS: &ast.BinaryExpr{
  1538. OP: ast.GT,
  1539. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  1540. RHS: &ast.IntegerLiteral{Val: 20},
  1541. },
  1542. },
  1543. RHS: &ast.BinaryExpr{
  1544. OP: ast.LT,
  1545. LHS: &ast.FieldRef{Name: "hum", StreamName: "src2"},
  1546. RHS: &ast.IntegerLiteral{Val: 60},
  1547. },
  1548. },
  1549. },
  1550. },
  1551. }.Init(),
  1552. },
  1553. },
  1554. fields: []ast.Field{
  1555. {
  1556. Expr: &ast.FieldRef{Name: "id1", StreamName: ast.DefaultStream},
  1557. Name: "id1",
  1558. AName: ""},
  1559. },
  1560. isAggregate: false,
  1561. sendMeta: false,
  1562. }.Init(),
  1563. }, { // 7 window error for table
  1564. sql: `SELECT value FROM tableInPlanner WHERE name = "v1" GROUP BY TUMBLINGWINDOW(ss, 10)`,
  1565. p: nil,
  1566. err: "cannot run window for TABLE sources",
  1567. }, { // 8 join table without window
  1568. sql: `SELECT id1 FROM src1 INNER JOIN tableInPlanner on src1.id1 = tableInPlanner.id and src1.temp > 20 and hum < 60 WHERE src1.id1 > 111`,
  1569. p: ProjectPlan{
  1570. baseLogicalPlan: baseLogicalPlan{
  1571. children: []LogicalPlan{
  1572. JoinPlan{
  1573. baseLogicalPlan: baseLogicalPlan{
  1574. children: []LogicalPlan{
  1575. JoinAlignPlan{
  1576. baseLogicalPlan: baseLogicalPlan{
  1577. children: []LogicalPlan{
  1578. FilterPlan{
  1579. baseLogicalPlan: baseLogicalPlan{
  1580. children: []LogicalPlan{
  1581. DataSourcePlan{
  1582. name: "src1",
  1583. streamFields: []interface{}{
  1584. "hum", "id1", "temp",
  1585. },
  1586. streamStmt: streams["src1"],
  1587. metaFields: []string{},
  1588. }.Init(),
  1589. },
  1590. },
  1591. condition: &ast.BinaryExpr{
  1592. RHS: &ast.BinaryExpr{
  1593. OP: ast.GT,
  1594. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  1595. RHS: &ast.IntegerLiteral{Val: 20},
  1596. },
  1597. OP: ast.AND,
  1598. LHS: &ast.BinaryExpr{
  1599. OP: ast.GT,
  1600. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  1601. RHS: &ast.IntegerLiteral{Val: 111},
  1602. },
  1603. },
  1604. }.Init(),
  1605. DataSourcePlan{
  1606. name: "tableInPlanner",
  1607. streamFields: []interface{}{
  1608. &ast.StreamField{
  1609. Name: "hum",
  1610. FieldType: &ast.BasicType{Type: ast.BIGINT},
  1611. },
  1612. &ast.StreamField{
  1613. Name: "id",
  1614. FieldType: &ast.BasicType{Type: ast.BIGINT},
  1615. },
  1616. },
  1617. streamStmt: streams["tableInPlanner"],
  1618. metaFields: []string{},
  1619. }.Init(),
  1620. },
  1621. },
  1622. Emitters: []string{"tableInPlanner"},
  1623. }.Init(),
  1624. },
  1625. },
  1626. from: &ast.Table{
  1627. Name: "src1",
  1628. },
  1629. joins: []ast.Join{
  1630. {
  1631. Name: "tableInPlanner",
  1632. Alias: "",
  1633. JoinType: ast.INNER_JOIN,
  1634. Expr: &ast.BinaryExpr{
  1635. OP: ast.AND,
  1636. LHS: &ast.BinaryExpr{
  1637. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  1638. OP: ast.EQ,
  1639. RHS: &ast.FieldRef{Name: "id", StreamName: "tableInPlanner"},
  1640. },
  1641. RHS: &ast.BinaryExpr{
  1642. OP: ast.LT,
  1643. LHS: &ast.FieldRef{Name: "hum", StreamName: ast.DefaultStream},
  1644. RHS: &ast.IntegerLiteral{Val: 60},
  1645. },
  1646. },
  1647. },
  1648. },
  1649. }.Init(),
  1650. },
  1651. },
  1652. fields: []ast.Field{
  1653. {
  1654. Expr: &ast.FieldRef{Name: "id1", StreamName: ast.DefaultStream},
  1655. Name: "id1",
  1656. AName: ""},
  1657. },
  1658. isAggregate: false,
  1659. sendMeta: false,
  1660. }.Init(),
  1661. }, { // 9 join table with window
  1662. 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)`,
  1663. p: ProjectPlan{
  1664. baseLogicalPlan: baseLogicalPlan{
  1665. children: []LogicalPlan{
  1666. JoinPlan{
  1667. baseLogicalPlan: baseLogicalPlan{
  1668. children: []LogicalPlan{
  1669. JoinAlignPlan{
  1670. baseLogicalPlan: baseLogicalPlan{
  1671. children: []LogicalPlan{
  1672. WindowPlan{
  1673. baseLogicalPlan: baseLogicalPlan{
  1674. children: []LogicalPlan{
  1675. FilterPlan{
  1676. baseLogicalPlan: baseLogicalPlan{
  1677. children: []LogicalPlan{
  1678. DataSourcePlan{
  1679. name: "src1",
  1680. streamFields: []interface{}{
  1681. "id1", "temp",
  1682. },
  1683. streamStmt: streams["src1"],
  1684. metaFields: []string{},
  1685. }.Init(),
  1686. },
  1687. },
  1688. condition: &ast.BinaryExpr{
  1689. RHS: &ast.BinaryExpr{
  1690. OP: ast.GT,
  1691. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  1692. RHS: &ast.IntegerLiteral{Val: 20},
  1693. },
  1694. OP: ast.AND,
  1695. LHS: &ast.BinaryExpr{
  1696. OP: ast.GT,
  1697. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  1698. RHS: &ast.IntegerLiteral{Val: 111},
  1699. },
  1700. },
  1701. }.Init(),
  1702. },
  1703. },
  1704. condition: nil,
  1705. wtype: ast.TUMBLING_WINDOW,
  1706. length: 10000,
  1707. interval: 0,
  1708. limit: 0,
  1709. }.Init(),
  1710. FilterPlan{
  1711. baseLogicalPlan: baseLogicalPlan{
  1712. children: []LogicalPlan{
  1713. DataSourcePlan{
  1714. name: "tableInPlanner",
  1715. streamFields: []interface{}{
  1716. &ast.StreamField{
  1717. Name: "hum",
  1718. FieldType: &ast.BasicType{Type: ast.BIGINT},
  1719. },
  1720. &ast.StreamField{
  1721. Name: "id",
  1722. FieldType: &ast.BasicType{Type: ast.BIGINT},
  1723. },
  1724. },
  1725. streamStmt: streams["tableInPlanner"],
  1726. metaFields: []string{},
  1727. }.Init(),
  1728. },
  1729. },
  1730. condition: &ast.BinaryExpr{
  1731. OP: ast.LT,
  1732. LHS: &ast.FieldRef{Name: "hum", StreamName: "tableInPlanner"},
  1733. RHS: &ast.IntegerLiteral{Val: 60},
  1734. },
  1735. }.Init(),
  1736. },
  1737. },
  1738. Emitters: []string{"tableInPlanner"},
  1739. }.Init(),
  1740. },
  1741. },
  1742. from: &ast.Table{
  1743. Name: "src1",
  1744. },
  1745. joins: []ast.Join{
  1746. {
  1747. Name: "tableInPlanner",
  1748. Alias: "",
  1749. JoinType: ast.INNER_JOIN,
  1750. Expr: &ast.BinaryExpr{
  1751. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  1752. OP: ast.EQ,
  1753. RHS: &ast.FieldRef{Name: "id", StreamName: "tableInPlanner"},
  1754. },
  1755. },
  1756. },
  1757. }.Init(),
  1758. },
  1759. },
  1760. fields: []ast.Field{
  1761. {
  1762. Expr: &ast.FieldRef{Name: "id1", StreamName: ast.DefaultStream},
  1763. Name: "id1",
  1764. AName: ""},
  1765. },
  1766. isAggregate: false,
  1767. sendMeta: false,
  1768. }.Init(),
  1769. }, { // 10 meta
  1770. sql: `SELECT temp, meta(id) AS eid,meta(Humidity->Device) AS hdevice FROM src1 WHERE meta(device)="demo2"`,
  1771. p: ProjectPlan{
  1772. baseLogicalPlan: baseLogicalPlan{
  1773. children: []LogicalPlan{
  1774. FilterPlan{
  1775. baseLogicalPlan: baseLogicalPlan{
  1776. children: []LogicalPlan{
  1777. DataSourcePlan{
  1778. name: "src1",
  1779. streamFields: []interface{}{
  1780. "temp",
  1781. },
  1782. streamStmt: streams["src1"],
  1783. metaFields: []string{"Humidity", "device", "id"},
  1784. }.Init(),
  1785. },
  1786. },
  1787. condition: &ast.BinaryExpr{
  1788. LHS: &ast.Call{
  1789. Name: "meta",
  1790. Args: []ast.Expr{&ast.MetaRef{
  1791. Name: "device",
  1792. StreamName: ast.DefaultStream,
  1793. }},
  1794. },
  1795. OP: ast.EQ,
  1796. RHS: &ast.StringLiteral{
  1797. Val: "demo2",
  1798. },
  1799. },
  1800. }.Init(),
  1801. },
  1802. },
  1803. fields: []ast.Field{
  1804. {
  1805. Expr: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  1806. Name: "temp",
  1807. AName: "",
  1808. }, {
  1809. Expr: &ast.FieldRef{Name: "eid", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  1810. &ast.Call{Name: "meta", Args: []ast.Expr{&ast.MetaRef{
  1811. Name: "id",
  1812. StreamName: ast.DefaultStream,
  1813. }}},
  1814. []ast.StreamName{},
  1815. nil,
  1816. )},
  1817. Name: "meta",
  1818. AName: "eid",
  1819. }, {
  1820. Expr: &ast.FieldRef{Name: "hdevice", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  1821. &ast.Call{Name: "meta", Args: []ast.Expr{
  1822. &ast.BinaryExpr{
  1823. OP: ast.ARROW,
  1824. LHS: &ast.MetaRef{Name: "Humidity", StreamName: ast.DefaultStream},
  1825. RHS: &ast.JsonFieldRef{Name: "Device"},
  1826. },
  1827. }},
  1828. []ast.StreamName{},
  1829. nil,
  1830. )},
  1831. Name: "meta",
  1832. AName: "hdevice",
  1833. },
  1834. },
  1835. isAggregate: false,
  1836. sendMeta: false,
  1837. }.Init(),
  1838. }, { // 11 join with same name field and aliased
  1839. sql: `SELECT src2.hum AS hum1, tableInPlanner.hum AS hum2 FROM src2 INNER JOIN tableInPlanner on id2 = id WHERE hum1 > hum2`,
  1840. p: ProjectPlan{
  1841. baseLogicalPlan: baseLogicalPlan{
  1842. children: []LogicalPlan{
  1843. JoinPlan{
  1844. baseLogicalPlan: baseLogicalPlan{
  1845. children: []LogicalPlan{
  1846. JoinAlignPlan{
  1847. baseLogicalPlan: baseLogicalPlan{
  1848. children: []LogicalPlan{
  1849. DataSourcePlan{
  1850. name: "src2",
  1851. streamFields: []interface{}{
  1852. "hum", "id", "id2",
  1853. },
  1854. streamStmt: streams["src2"],
  1855. metaFields: []string{},
  1856. }.Init(),
  1857. DataSourcePlan{
  1858. name: "tableInPlanner",
  1859. streamFields: []interface{}{
  1860. &ast.StreamField{
  1861. Name: "hum",
  1862. FieldType: &ast.BasicType{Type: ast.BIGINT},
  1863. },
  1864. &ast.StreamField{
  1865. Name: "id",
  1866. FieldType: &ast.BasicType{Type: ast.BIGINT},
  1867. },
  1868. },
  1869. streamStmt: streams["tableInPlanner"],
  1870. metaFields: []string{},
  1871. }.Init(),
  1872. },
  1873. },
  1874. Emitters: []string{"tableInPlanner"},
  1875. }.Init(),
  1876. },
  1877. },
  1878. from: &ast.Table{
  1879. Name: "src2",
  1880. },
  1881. joins: []ast.Join{
  1882. {
  1883. Name: "tableInPlanner",
  1884. Alias: "",
  1885. JoinType: ast.INNER_JOIN,
  1886. Expr: &ast.BinaryExpr{
  1887. RHS: &ast.BinaryExpr{
  1888. OP: ast.EQ,
  1889. LHS: &ast.FieldRef{Name: "id2", StreamName: ast.DefaultStream},
  1890. RHS: &ast.FieldRef{Name: "id", StreamName: ast.DefaultStream},
  1891. },
  1892. OP: ast.AND,
  1893. LHS: &ast.BinaryExpr{
  1894. OP: ast.GT,
  1895. LHS: &ast.FieldRef{Name: "hum1", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  1896. &ast.FieldRef{
  1897. Name: "hum",
  1898. StreamName: "src2",
  1899. },
  1900. []ast.StreamName{"src2"},
  1901. &boolFalse,
  1902. )},
  1903. RHS: &ast.FieldRef{Name: "hum2", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  1904. &ast.FieldRef{
  1905. Name: "hum",
  1906. StreamName: "tableInPlanner",
  1907. },
  1908. []ast.StreamName{"tableInPlanner"},
  1909. &boolFalse,
  1910. )},
  1911. },
  1912. },
  1913. },
  1914. },
  1915. }.Init(),
  1916. },
  1917. },
  1918. fields: []ast.Field{
  1919. {
  1920. Expr: &ast.FieldRef{Name: "hum1", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  1921. &ast.FieldRef{
  1922. Name: "hum",
  1923. StreamName: "src2",
  1924. },
  1925. []ast.StreamName{"src2"},
  1926. &boolFalse,
  1927. )},
  1928. Name: "hum",
  1929. AName: "hum1",
  1930. }, {
  1931. Expr: &ast.FieldRef{Name: "hum2", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  1932. &ast.FieldRef{
  1933. Name: "hum",
  1934. StreamName: "tableInPlanner",
  1935. },
  1936. []ast.StreamName{"tableInPlanner"},
  1937. &boolFalse,
  1938. )},
  1939. Name: "hum",
  1940. AName: "hum2",
  1941. },
  1942. },
  1943. isAggregate: false,
  1944. sendMeta: false,
  1945. }.Init(),
  1946. },
  1947. }
  1948. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  1949. for i, tt := range tests {
  1950. stmt, err := xsql.NewParser(strings.NewReader(tt.sql)).Parse()
  1951. if err != nil {
  1952. t.Errorf("%d. %q: error compile sql: %s\n", i, tt.sql, err)
  1953. continue
  1954. }
  1955. p, err := createLogicalPlan(stmt, &api.RuleOption{
  1956. IsEventTime: false,
  1957. LateTol: 0,
  1958. Concurrency: 0,
  1959. BufferLength: 0,
  1960. SendMetaToSink: false,
  1961. Qos: 0,
  1962. CheckpointInterval: 0,
  1963. SendError: true,
  1964. }, store)
  1965. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  1966. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.sql, tt.err, err)
  1967. } else if !reflect.DeepEqual(tt.p, p) {
  1968. 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))
  1969. }
  1970. }
  1971. }