planner_test.go 91 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340
  1. // Copyright 2022-2023 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. "reflect"
  19. "strings"
  20. "testing"
  21. "github.com/gdexlab/go-render/render"
  22. "github.com/lf-edge/ekuiper/internal/pkg/store"
  23. "github.com/lf-edge/ekuiper/internal/testx"
  24. "github.com/lf-edge/ekuiper/internal/topo/node"
  25. "github.com/lf-edge/ekuiper/internal/xsql"
  26. "github.com/lf-edge/ekuiper/pkg/api"
  27. "github.com/lf-edge/ekuiper/pkg/ast"
  28. )
  29. func init() {
  30. testx.InitEnv()
  31. }
  32. func Test_createLogicalPlan(t *testing.T) {
  33. store, err := store.GetKV("stream")
  34. if err != nil {
  35. t.Error(err)
  36. return
  37. }
  38. streamSqls := map[string]string{
  39. "src1": `CREATE STREAM src1 (
  40. id1 BIGINT,
  41. temp BIGINT,
  42. name string,
  43. myarray array(string)
  44. ) WITH (DATASOURCE="src1", FORMAT="json", KEY="ts");`,
  45. "src2": `CREATE STREAM src2 (
  46. id2 BIGINT,
  47. hum BIGINT
  48. ) WITH (DATASOURCE="src2", FORMAT="json", KEY="ts", TIMESTAMP_FORMAT="YYYY-MM-dd HH:mm:ss");`,
  49. "tableInPlanner": `CREATE TABLE tableInPlanner (
  50. id BIGINT,
  51. name STRING,
  52. value STRING,
  53. hum BIGINT
  54. ) WITH (TYPE="file");`,
  55. }
  56. types := map[string]ast.StreamType{
  57. "src1": ast.TypeStream,
  58. "src2": ast.TypeStream,
  59. "tableInPlanner": ast.TypeTable,
  60. }
  61. for name, sql := range streamSqls {
  62. s, err := json.Marshal(&xsql.StreamInfo{
  63. StreamType: types[name],
  64. Statement: sql,
  65. })
  66. if err != nil {
  67. t.Error(err)
  68. t.Fail()
  69. }
  70. err = store.Set(name, string(s))
  71. if err != nil {
  72. t.Error(err)
  73. t.Fail()
  74. }
  75. }
  76. streams := make(map[string]*ast.StreamStmt)
  77. for n := range streamSqls {
  78. streamStmt, err := xsql.GetDataSource(store, n)
  79. if err != nil {
  80. t.Errorf("fail to get stream %s, please check if stream is created", n)
  81. return
  82. }
  83. streams[n] = streamStmt
  84. }
  85. // boolTrue = true
  86. boolFalse := false
  87. tests := []struct {
  88. sql string
  89. p LogicalPlan
  90. err string
  91. }{
  92. {
  93. sql: "select unnest(myarray) as col from src1",
  94. p: ProjectSetPlan{
  95. SrfMapping: map[string]struct{}{
  96. "col": {},
  97. },
  98. baseLogicalPlan: baseLogicalPlan{
  99. children: []LogicalPlan{
  100. ProjectPlan{
  101. baseLogicalPlan: baseLogicalPlan{
  102. children: []LogicalPlan{
  103. DataSourcePlan{
  104. baseLogicalPlan: baseLogicalPlan{},
  105. name: "src1",
  106. streamFields: map[string]*ast.JsonStreamField{
  107. "myarray": {
  108. Type: "array",
  109. Items: &ast.JsonStreamField{
  110. Type: "string",
  111. },
  112. },
  113. },
  114. streamStmt: streams["src1"],
  115. metaFields: []string{},
  116. }.Init(),
  117. },
  118. },
  119. fields: []ast.Field{
  120. {
  121. Name: "unnest",
  122. AName: "col",
  123. Expr: func() *ast.FieldRef {
  124. fr := &ast.FieldRef{
  125. StreamName: ast.AliasStream,
  126. Name: "col",
  127. AliasRef: &ast.AliasRef{
  128. Expression: &ast.Call{
  129. Name: "unnest",
  130. FuncType: ast.FuncTypeSrf,
  131. Args: []ast.Expr{
  132. &ast.FieldRef{
  133. StreamName: "src1",
  134. Name: "myarray",
  135. },
  136. },
  137. },
  138. },
  139. }
  140. fr.SetRefSource([]ast.StreamName{"src1"})
  141. return fr
  142. }(),
  143. },
  144. },
  145. }.Init(),
  146. },
  147. },
  148. }.Init(),
  149. },
  150. { // 0
  151. sql: "SELECT unnest(myarray), name from src1",
  152. p: ProjectSetPlan{
  153. SrfMapping: map[string]struct{}{
  154. "unnest": {},
  155. },
  156. baseLogicalPlan: baseLogicalPlan{
  157. children: []LogicalPlan{
  158. ProjectPlan{
  159. baseLogicalPlan: baseLogicalPlan{
  160. children: []LogicalPlan{
  161. DataSourcePlan{
  162. baseLogicalPlan: baseLogicalPlan{},
  163. name: "src1",
  164. streamFields: map[string]*ast.JsonStreamField{
  165. "myarray": {
  166. Type: "array",
  167. Items: &ast.JsonStreamField{
  168. Type: "string",
  169. },
  170. },
  171. "name": {
  172. Type: "string",
  173. },
  174. },
  175. streamStmt: streams["src1"],
  176. metaFields: []string{},
  177. }.Init(),
  178. },
  179. },
  180. fields: []ast.Field{
  181. {
  182. Expr: &ast.Call{
  183. Name: "unnest",
  184. FuncType: ast.FuncTypeSrf,
  185. Args: []ast.Expr{
  186. &ast.FieldRef{
  187. StreamName: "src1",
  188. Name: "myarray",
  189. },
  190. },
  191. },
  192. Name: "unnest",
  193. },
  194. {
  195. Name: "name",
  196. Expr: &ast.FieldRef{
  197. StreamName: "src1",
  198. Name: "name",
  199. },
  200. },
  201. },
  202. }.Init(),
  203. },
  204. },
  205. }.Init(),
  206. },
  207. { // 0
  208. sql: `SELECT myarray[temp] FROM src1`,
  209. p: ProjectPlan{
  210. baseLogicalPlan: baseLogicalPlan{
  211. children: []LogicalPlan{
  212. DataSourcePlan{
  213. baseLogicalPlan: baseLogicalPlan{},
  214. name: "src1",
  215. streamFields: map[string]*ast.JsonStreamField{
  216. "myarray": {
  217. Type: "array",
  218. Items: &ast.JsonStreamField{
  219. Type: "string",
  220. },
  221. },
  222. "temp": {
  223. Type: "bigint",
  224. },
  225. },
  226. streamStmt: streams["src1"],
  227. metaFields: []string{},
  228. }.Init(),
  229. },
  230. },
  231. fields: []ast.Field{
  232. {
  233. Expr: &ast.BinaryExpr{
  234. OP: ast.SUBSET,
  235. LHS: &ast.FieldRef{
  236. StreamName: "src1",
  237. Name: "myarray",
  238. },
  239. RHS: &ast.IndexExpr{Index: &ast.FieldRef{
  240. StreamName: "src1",
  241. Name: "temp",
  242. }},
  243. },
  244. Name: "kuiper_field_0",
  245. AName: "",
  246. },
  247. },
  248. isAggregate: false,
  249. sendMeta: false,
  250. }.Init(),
  251. },
  252. { // 1 optimize where to data source
  253. sql: `SELECT temp FROM src1 WHERE name = "v1" GROUP BY TUMBLINGWINDOW(ss, 10)`,
  254. p: ProjectPlan{
  255. baseLogicalPlan: baseLogicalPlan{
  256. children: []LogicalPlan{
  257. WindowPlan{
  258. baseLogicalPlan: baseLogicalPlan{
  259. children: []LogicalPlan{
  260. FilterPlan{
  261. baseLogicalPlan: baseLogicalPlan{
  262. children: []LogicalPlan{
  263. DataSourcePlan{
  264. name: "src1",
  265. streamFields: map[string]*ast.JsonStreamField{
  266. "name": {
  267. Type: "string",
  268. },
  269. "temp": {
  270. Type: "bigint",
  271. },
  272. },
  273. streamStmt: streams["src1"],
  274. metaFields: []string{},
  275. }.Init(),
  276. },
  277. },
  278. condition: &ast.BinaryExpr{
  279. LHS: &ast.FieldRef{Name: "name", StreamName: "src1"},
  280. OP: ast.EQ,
  281. RHS: &ast.StringLiteral{Val: "v1"},
  282. },
  283. }.Init(),
  284. },
  285. },
  286. condition: nil,
  287. wtype: ast.TUMBLING_WINDOW,
  288. length: 10000,
  289. interval: 0,
  290. limit: 0,
  291. }.Init(),
  292. },
  293. },
  294. fields: []ast.Field{
  295. {
  296. Expr: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  297. Name: "temp",
  298. AName: "",
  299. },
  300. },
  301. isAggregate: false,
  302. sendMeta: false,
  303. }.Init(),
  304. },
  305. { // 2 condition that cannot be optimized
  306. 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)`,
  307. p: ProjectPlan{
  308. baseLogicalPlan: baseLogicalPlan{
  309. children: []LogicalPlan{
  310. JoinPlan{
  311. baseLogicalPlan: baseLogicalPlan{
  312. children: []LogicalPlan{
  313. WindowPlan{
  314. baseLogicalPlan: baseLogicalPlan{
  315. children: []LogicalPlan{
  316. DataSourcePlan{
  317. name: "src1",
  318. streamFields: map[string]*ast.JsonStreamField{
  319. "id1": {
  320. Type: "bigint",
  321. },
  322. "temp": {
  323. Type: "bigint",
  324. },
  325. },
  326. streamStmt: streams["src1"],
  327. metaFields: []string{},
  328. }.Init(),
  329. DataSourcePlan{
  330. name: "src2",
  331. streamFields: map[string]*ast.JsonStreamField{
  332. "hum": {
  333. Type: "bigint",
  334. },
  335. "id2": {
  336. Type: "bigint",
  337. },
  338. },
  339. streamStmt: streams["src2"],
  340. metaFields: []string{},
  341. timestampFormat: "YYYY-MM-dd HH:mm:ss",
  342. }.Init(),
  343. },
  344. },
  345. condition: nil,
  346. wtype: ast.TUMBLING_WINDOW,
  347. length: 10000,
  348. interval: 0,
  349. limit: 0,
  350. }.Init(),
  351. },
  352. },
  353. from: &ast.Table{Name: "src1"},
  354. joins: ast.Joins{ast.Join{
  355. Name: "src2",
  356. JoinType: ast.INNER_JOIN,
  357. Expr: &ast.BinaryExpr{
  358. OP: ast.AND,
  359. LHS: &ast.BinaryExpr{
  360. LHS: &ast.BinaryExpr{
  361. OP: ast.GT,
  362. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  363. RHS: &ast.IntegerLiteral{Val: 20},
  364. },
  365. OP: ast.OR,
  366. RHS: &ast.BinaryExpr{
  367. OP: ast.GT,
  368. LHS: &ast.FieldRef{Name: "hum", StreamName: "src2"},
  369. RHS: &ast.IntegerLiteral{Val: 60},
  370. },
  371. },
  372. RHS: &ast.BinaryExpr{
  373. OP: ast.EQ,
  374. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  375. RHS: &ast.FieldRef{Name: "id2", StreamName: "src2"},
  376. },
  377. },
  378. }},
  379. }.Init(),
  380. },
  381. },
  382. fields: []ast.Field{
  383. {
  384. Expr: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  385. Name: "id1",
  386. AName: "",
  387. },
  388. },
  389. isAggregate: false,
  390. sendMeta: false,
  391. }.Init(),
  392. },
  393. { // 3 optimize window filter
  394. sql: `SELECT id1 FROM src1 WHERE name = "v1" GROUP BY TUMBLINGWINDOW(ss, 10) FILTER( WHERE temp > 2)`,
  395. p: ProjectPlan{
  396. baseLogicalPlan: baseLogicalPlan{
  397. children: []LogicalPlan{
  398. WindowPlan{
  399. baseLogicalPlan: baseLogicalPlan{
  400. children: []LogicalPlan{
  401. FilterPlan{
  402. baseLogicalPlan: baseLogicalPlan{
  403. children: []LogicalPlan{
  404. DataSourcePlan{
  405. name: "src1",
  406. streamFields: map[string]*ast.JsonStreamField{
  407. "id1": {
  408. Type: "bigint",
  409. },
  410. "name": {
  411. Type: "string",
  412. },
  413. "temp": {
  414. Type: "bigint",
  415. },
  416. },
  417. streamStmt: streams["src1"],
  418. metaFields: []string{},
  419. }.Init(),
  420. },
  421. },
  422. condition: &ast.BinaryExpr{
  423. OP: ast.AND,
  424. LHS: &ast.BinaryExpr{
  425. LHS: &ast.FieldRef{Name: "name", StreamName: "src1"},
  426. OP: ast.EQ,
  427. RHS: &ast.StringLiteral{Val: "v1"},
  428. },
  429. RHS: &ast.BinaryExpr{
  430. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  431. OP: ast.GT,
  432. RHS: &ast.IntegerLiteral{Val: 2},
  433. },
  434. },
  435. }.Init(),
  436. },
  437. },
  438. condition: nil,
  439. wtype: ast.TUMBLING_WINDOW,
  440. length: 10000,
  441. interval: 0,
  442. limit: 0,
  443. }.Init(),
  444. },
  445. },
  446. fields: []ast.Field{
  447. {
  448. Expr: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  449. Name: "id1",
  450. AName: "",
  451. },
  452. },
  453. isAggregate: false,
  454. sendMeta: false,
  455. }.Init(),
  456. },
  457. { // 4. do not optimize count window
  458. sql: `SELECT * FROM src1 WHERE temp > 20 GROUP BY COUNTWINDOW(5,1) HAVING COUNT(*) > 2`,
  459. p: ProjectPlan{
  460. baseLogicalPlan: baseLogicalPlan{
  461. children: []LogicalPlan{
  462. HavingPlan{
  463. baseLogicalPlan: baseLogicalPlan{
  464. children: []LogicalPlan{
  465. FilterPlan{
  466. baseLogicalPlan: baseLogicalPlan{
  467. children: []LogicalPlan{
  468. WindowPlan{
  469. baseLogicalPlan: baseLogicalPlan{
  470. children: []LogicalPlan{
  471. DataSourcePlan{
  472. name: "src1",
  473. isWildCard: true,
  474. streamFields: map[string]*ast.JsonStreamField{
  475. "id1": {
  476. Type: "bigint",
  477. },
  478. "temp": {
  479. Type: "bigint",
  480. },
  481. "name": {
  482. Type: "string",
  483. },
  484. "myarray": {
  485. Type: "array",
  486. Items: &ast.JsonStreamField{
  487. Type: "string",
  488. },
  489. },
  490. },
  491. streamStmt: streams["src1"],
  492. metaFields: []string{},
  493. }.Init(),
  494. },
  495. },
  496. condition: nil,
  497. wtype: ast.COUNT_WINDOW,
  498. length: 5,
  499. interval: 1,
  500. limit: 0,
  501. }.Init(),
  502. },
  503. },
  504. condition: &ast.BinaryExpr{
  505. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  506. OP: ast.GT,
  507. RHS: &ast.IntegerLiteral{Val: 20},
  508. },
  509. }.Init(),
  510. },
  511. },
  512. condition: &ast.BinaryExpr{
  513. LHS: &ast.Call{Name: "count", FuncId: 0, Args: []ast.Expr{&ast.Wildcard{
  514. Token: ast.ASTERISK,
  515. }}, FuncType: ast.FuncTypeAgg},
  516. OP: ast.GT,
  517. RHS: &ast.IntegerLiteral{Val: 2},
  518. },
  519. }.Init(),
  520. },
  521. },
  522. fields: []ast.Field{
  523. {
  524. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  525. Name: "*",
  526. AName: "",
  527. },
  528. },
  529. isAggregate: false,
  530. sendMeta: false,
  531. }.Init(),
  532. },
  533. { // 5. optimize join on
  534. 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)`,
  535. p: ProjectPlan{
  536. baseLogicalPlan: baseLogicalPlan{
  537. children: []LogicalPlan{
  538. JoinPlan{
  539. baseLogicalPlan: baseLogicalPlan{
  540. children: []LogicalPlan{
  541. WindowPlan{
  542. baseLogicalPlan: baseLogicalPlan{
  543. children: []LogicalPlan{
  544. FilterPlan{
  545. baseLogicalPlan: baseLogicalPlan{
  546. children: []LogicalPlan{
  547. DataSourcePlan{
  548. name: "src1",
  549. streamFields: map[string]*ast.JsonStreamField{
  550. "id1": {
  551. Type: "bigint",
  552. },
  553. "temp": {
  554. Type: "bigint",
  555. },
  556. },
  557. streamStmt: streams["src1"],
  558. metaFields: []string{},
  559. }.Init(),
  560. },
  561. },
  562. condition: &ast.BinaryExpr{
  563. RHS: &ast.BinaryExpr{
  564. OP: ast.GT,
  565. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  566. RHS: &ast.IntegerLiteral{Val: 20},
  567. },
  568. OP: ast.AND,
  569. LHS: &ast.BinaryExpr{
  570. OP: ast.GT,
  571. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  572. RHS: &ast.IntegerLiteral{Val: 111},
  573. },
  574. },
  575. }.Init(),
  576. FilterPlan{
  577. baseLogicalPlan: baseLogicalPlan{
  578. children: []LogicalPlan{
  579. DataSourcePlan{
  580. name: "src2",
  581. streamFields: map[string]*ast.JsonStreamField{
  582. "hum": {
  583. Type: "bigint",
  584. },
  585. "id2": {
  586. Type: "bigint",
  587. },
  588. },
  589. streamStmt: streams["src2"],
  590. metaFields: []string{},
  591. timestampFormat: "YYYY-MM-dd HH:mm:ss",
  592. }.Init(),
  593. },
  594. },
  595. condition: &ast.BinaryExpr{
  596. OP: ast.LT,
  597. LHS: &ast.FieldRef{Name: "hum", StreamName: "src2"},
  598. RHS: &ast.IntegerLiteral{Val: 60},
  599. },
  600. }.Init(),
  601. },
  602. },
  603. condition: nil,
  604. wtype: ast.TUMBLING_WINDOW,
  605. length: 10000,
  606. interval: 0,
  607. limit: 0,
  608. }.Init(),
  609. },
  610. },
  611. from: &ast.Table{
  612. Name: "src1",
  613. },
  614. joins: []ast.Join{
  615. {
  616. Name: "src2",
  617. Alias: "",
  618. JoinType: ast.INNER_JOIN,
  619. Expr: &ast.BinaryExpr{
  620. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  621. OP: ast.EQ,
  622. RHS: &ast.FieldRef{Name: "id2", StreamName: "src2"},
  623. },
  624. },
  625. },
  626. }.Init(),
  627. },
  628. },
  629. fields: []ast.Field{
  630. {
  631. Expr: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  632. Name: "id1",
  633. AName: "",
  634. },
  635. },
  636. isAggregate: false,
  637. sendMeta: false,
  638. }.Init(),
  639. },
  640. { // 6. optimize outter join on
  641. 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)`,
  642. p: ProjectPlan{
  643. baseLogicalPlan: baseLogicalPlan{
  644. children: []LogicalPlan{
  645. JoinPlan{
  646. baseLogicalPlan: baseLogicalPlan{
  647. children: []LogicalPlan{
  648. WindowPlan{
  649. baseLogicalPlan: baseLogicalPlan{
  650. children: []LogicalPlan{
  651. FilterPlan{
  652. baseLogicalPlan: baseLogicalPlan{
  653. children: []LogicalPlan{
  654. DataSourcePlan{
  655. name: "src1",
  656. streamFields: map[string]*ast.JsonStreamField{
  657. "id1": {
  658. Type: "bigint",
  659. },
  660. "temp": {
  661. Type: "bigint",
  662. },
  663. },
  664. streamStmt: streams["src1"],
  665. metaFields: []string{},
  666. }.Init(),
  667. },
  668. },
  669. condition: &ast.BinaryExpr{
  670. OP: ast.GT,
  671. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  672. RHS: &ast.IntegerLiteral{Val: 111},
  673. },
  674. }.Init(),
  675. DataSourcePlan{
  676. name: "src2",
  677. streamFields: map[string]*ast.JsonStreamField{
  678. "hum": {
  679. Type: "bigint",
  680. },
  681. "id2": {
  682. Type: "bigint",
  683. },
  684. },
  685. streamStmt: streams["src2"],
  686. metaFields: []string{},
  687. timestampFormat: "YYYY-MM-dd HH:mm:ss",
  688. }.Init(),
  689. },
  690. },
  691. condition: nil,
  692. wtype: ast.TUMBLING_WINDOW,
  693. length: 10000,
  694. interval: 0,
  695. limit: 0,
  696. }.Init(),
  697. },
  698. },
  699. from: &ast.Table{
  700. Name: "src1",
  701. },
  702. joins: []ast.Join{
  703. {
  704. Name: "src2",
  705. Alias: "",
  706. JoinType: ast.FULL_JOIN,
  707. Expr: &ast.BinaryExpr{
  708. OP: ast.AND,
  709. LHS: &ast.BinaryExpr{
  710. OP: ast.AND,
  711. LHS: &ast.BinaryExpr{
  712. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  713. OP: ast.EQ,
  714. RHS: &ast.FieldRef{Name: "id2", StreamName: "src2"},
  715. },
  716. RHS: &ast.BinaryExpr{
  717. OP: ast.GT,
  718. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  719. RHS: &ast.IntegerLiteral{Val: 20},
  720. },
  721. },
  722. RHS: &ast.BinaryExpr{
  723. OP: ast.LT,
  724. LHS: &ast.FieldRef{Name: "hum", StreamName: "src2"},
  725. RHS: &ast.IntegerLiteral{Val: 60},
  726. },
  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. },
  740. isAggregate: false,
  741. sendMeta: false,
  742. }.Init(),
  743. },
  744. { // 7 window error for table
  745. sql: `SELECT value FROM tableInPlanner WHERE name = "v1" GROUP BY TUMBLINGWINDOW(ss, 10)`,
  746. p: nil,
  747. err: "cannot run window for TABLE sources",
  748. },
  749. { // 8 join table without window
  750. sql: `SELECT id1 FROM src1 INNER JOIN tableInPlanner on src1.id1 = tableInPlanner.id and src1.temp > 20 and hum < 60 WHERE src1.id1 > 111`,
  751. p: ProjectPlan{
  752. baseLogicalPlan: baseLogicalPlan{
  753. children: []LogicalPlan{
  754. JoinPlan{
  755. baseLogicalPlan: baseLogicalPlan{
  756. children: []LogicalPlan{
  757. JoinAlignPlan{
  758. baseLogicalPlan: baseLogicalPlan{
  759. children: []LogicalPlan{
  760. FilterPlan{
  761. baseLogicalPlan: baseLogicalPlan{
  762. children: []LogicalPlan{
  763. DataSourcePlan{
  764. name: "src1",
  765. streamFields: map[string]*ast.JsonStreamField{
  766. "id1": {
  767. Type: "bigint",
  768. },
  769. "temp": {
  770. Type: "bigint",
  771. },
  772. },
  773. streamStmt: streams["src1"],
  774. metaFields: []string{},
  775. }.Init(),
  776. },
  777. },
  778. condition: &ast.BinaryExpr{
  779. RHS: &ast.BinaryExpr{
  780. OP: ast.GT,
  781. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  782. RHS: &ast.IntegerLiteral{Val: 20},
  783. },
  784. OP: ast.AND,
  785. LHS: &ast.BinaryExpr{
  786. OP: ast.GT,
  787. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  788. RHS: &ast.IntegerLiteral{Val: 111},
  789. },
  790. },
  791. }.Init(),
  792. DataSourcePlan{
  793. name: "tableInPlanner",
  794. streamFields: map[string]*ast.JsonStreamField{
  795. "hum": {
  796. Type: "bigint",
  797. },
  798. "id": {
  799. Type: "bigint",
  800. },
  801. },
  802. streamStmt: streams["tableInPlanner"],
  803. metaFields: []string{},
  804. }.Init(),
  805. },
  806. },
  807. Emitters: []string{"tableInPlanner"},
  808. }.Init(),
  809. },
  810. },
  811. from: &ast.Table{
  812. Name: "src1",
  813. },
  814. joins: []ast.Join{
  815. {
  816. Name: "tableInPlanner",
  817. Alias: "",
  818. JoinType: ast.INNER_JOIN,
  819. Expr: &ast.BinaryExpr{
  820. OP: ast.AND,
  821. LHS: &ast.BinaryExpr{
  822. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  823. OP: ast.EQ,
  824. RHS: &ast.FieldRef{Name: "id", StreamName: "tableInPlanner"},
  825. },
  826. RHS: &ast.BinaryExpr{
  827. OP: ast.LT,
  828. LHS: &ast.FieldRef{Name: "hum", StreamName: "tableInPlanner"},
  829. RHS: &ast.IntegerLiteral{Val: 60},
  830. },
  831. },
  832. },
  833. },
  834. }.Init(),
  835. },
  836. },
  837. fields: []ast.Field{
  838. {
  839. Expr: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  840. Name: "id1",
  841. AName: "",
  842. },
  843. },
  844. isAggregate: false,
  845. sendMeta: false,
  846. }.Init(),
  847. },
  848. { // 9 join table with window
  849. 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)`,
  850. p: ProjectPlan{
  851. baseLogicalPlan: baseLogicalPlan{
  852. children: []LogicalPlan{
  853. JoinPlan{
  854. baseLogicalPlan: baseLogicalPlan{
  855. children: []LogicalPlan{
  856. JoinAlignPlan{
  857. baseLogicalPlan: baseLogicalPlan{
  858. children: []LogicalPlan{
  859. WindowPlan{
  860. baseLogicalPlan: baseLogicalPlan{
  861. children: []LogicalPlan{
  862. DataSourcePlan{
  863. name: "src1",
  864. streamFields: map[string]*ast.JsonStreamField{
  865. "id1": {
  866. Type: "bigint",
  867. },
  868. "temp": {
  869. Type: "bigint",
  870. },
  871. },
  872. streamStmt: streams["src1"],
  873. metaFields: []string{},
  874. }.Init(),
  875. },
  876. },
  877. condition: nil,
  878. wtype: ast.TUMBLING_WINDOW,
  879. length: 10000,
  880. interval: 0,
  881. limit: 0,
  882. }.Init(),
  883. DataSourcePlan{
  884. name: "tableInPlanner",
  885. streamFields: map[string]*ast.JsonStreamField{
  886. "hum": {
  887. Type: "bigint",
  888. },
  889. "id": {
  890. Type: "bigint",
  891. },
  892. },
  893. streamStmt: streams["tableInPlanner"],
  894. metaFields: []string{},
  895. }.Init(),
  896. },
  897. },
  898. Emitters: []string{"tableInPlanner"},
  899. }.Init(),
  900. },
  901. },
  902. from: &ast.Table{
  903. Name: "src1",
  904. },
  905. joins: []ast.Join{
  906. {
  907. Name: "tableInPlanner",
  908. Alias: "",
  909. JoinType: ast.INNER_JOIN,
  910. Expr: &ast.BinaryExpr{
  911. OP: ast.AND,
  912. LHS: &ast.BinaryExpr{
  913. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  914. OP: ast.EQ,
  915. RHS: &ast.FieldRef{Name: "id", StreamName: "tableInPlanner"},
  916. },
  917. RHS: &ast.BinaryExpr{
  918. RHS: &ast.BinaryExpr{
  919. OP: ast.AND,
  920. LHS: &ast.BinaryExpr{
  921. OP: ast.GT,
  922. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  923. RHS: &ast.IntegerLiteral{Val: 20},
  924. },
  925. RHS: &ast.BinaryExpr{
  926. OP: ast.LT,
  927. LHS: &ast.FieldRef{Name: "hum", StreamName: "tableInPlanner"},
  928. RHS: &ast.IntegerLiteral{Val: 60},
  929. },
  930. },
  931. OP: ast.AND,
  932. LHS: &ast.BinaryExpr{
  933. OP: ast.GT,
  934. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  935. RHS: &ast.IntegerLiteral{Val: 111},
  936. },
  937. },
  938. },
  939. },
  940. },
  941. }.Init(),
  942. },
  943. },
  944. fields: []ast.Field{
  945. {
  946. Expr: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  947. Name: "id1",
  948. AName: "",
  949. },
  950. },
  951. isAggregate: false,
  952. sendMeta: false,
  953. }.Init(),
  954. },
  955. { // 10 meta
  956. sql: `SELECT temp, meta(id) AS eid,meta(Humidity->Device) AS hdevice FROM src1 WHERE meta(device)="demo2"`,
  957. p: ProjectPlan{
  958. baseLogicalPlan: baseLogicalPlan{
  959. children: []LogicalPlan{
  960. FilterPlan{
  961. baseLogicalPlan: baseLogicalPlan{
  962. children: []LogicalPlan{
  963. DataSourcePlan{
  964. name: "src1",
  965. streamFields: map[string]*ast.JsonStreamField{
  966. "temp": {
  967. Type: "bigint",
  968. },
  969. },
  970. streamStmt: streams["src1"],
  971. metaFields: []string{"Humidity", "device", "id"},
  972. }.Init(),
  973. },
  974. },
  975. condition: &ast.BinaryExpr{
  976. LHS: &ast.Call{
  977. Name: "meta",
  978. FuncId: 2,
  979. Args: []ast.Expr{&ast.MetaRef{
  980. Name: "device",
  981. StreamName: ast.DefaultStream,
  982. }},
  983. },
  984. OP: ast.EQ,
  985. RHS: &ast.StringLiteral{
  986. Val: "demo2",
  987. },
  988. },
  989. }.Init(),
  990. },
  991. },
  992. fields: []ast.Field{
  993. {
  994. Expr: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  995. Name: "temp",
  996. AName: "",
  997. }, {
  998. Expr: &ast.FieldRef{Name: "eid", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  999. &ast.Call{Name: "meta", FuncId: 0, Args: []ast.Expr{&ast.MetaRef{
  1000. Name: "id",
  1001. StreamName: ast.DefaultStream,
  1002. }}},
  1003. []ast.StreamName{},
  1004. nil,
  1005. )},
  1006. Name: "meta",
  1007. AName: "eid",
  1008. }, {
  1009. Expr: &ast.FieldRef{Name: "hdevice", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  1010. &ast.Call{Name: "meta", FuncId: 1, Args: []ast.Expr{
  1011. &ast.BinaryExpr{
  1012. OP: ast.ARROW,
  1013. LHS: &ast.MetaRef{Name: "Humidity", StreamName: ast.DefaultStream},
  1014. RHS: &ast.JsonFieldRef{Name: "Device"},
  1015. },
  1016. }},
  1017. []ast.StreamName{},
  1018. nil,
  1019. )},
  1020. Name: "meta",
  1021. AName: "hdevice",
  1022. },
  1023. },
  1024. isAggregate: false,
  1025. sendMeta: false,
  1026. }.Init(),
  1027. },
  1028. { // 11 join with same name field and aliased
  1029. sql: `SELECT src2.hum AS hum1, tableInPlanner.hum AS hum2 FROM src2 INNER JOIN tableInPlanner on id2 = id WHERE hum1 > hum2`,
  1030. p: ProjectPlan{
  1031. baseLogicalPlan: baseLogicalPlan{
  1032. children: []LogicalPlan{
  1033. JoinPlan{
  1034. baseLogicalPlan: baseLogicalPlan{
  1035. children: []LogicalPlan{
  1036. JoinAlignPlan{
  1037. baseLogicalPlan: baseLogicalPlan{
  1038. children: []LogicalPlan{
  1039. DataSourcePlan{
  1040. name: "src2",
  1041. streamFields: map[string]*ast.JsonStreamField{
  1042. "hum": {
  1043. Type: "bigint",
  1044. },
  1045. "id2": {
  1046. Type: "bigint",
  1047. },
  1048. },
  1049. streamStmt: streams["src2"],
  1050. metaFields: []string{},
  1051. timestampFormat: "YYYY-MM-dd HH:mm:ss",
  1052. }.Init(),
  1053. DataSourcePlan{
  1054. name: "tableInPlanner",
  1055. streamFields: map[string]*ast.JsonStreamField{
  1056. "hum": {
  1057. Type: "bigint",
  1058. },
  1059. "id": {
  1060. Type: "bigint",
  1061. },
  1062. },
  1063. streamStmt: streams["tableInPlanner"],
  1064. metaFields: []string{},
  1065. }.Init(),
  1066. },
  1067. },
  1068. Emitters: []string{"tableInPlanner"},
  1069. }.Init(),
  1070. },
  1071. },
  1072. from: &ast.Table{
  1073. Name: "src2",
  1074. },
  1075. joins: []ast.Join{
  1076. {
  1077. Name: "tableInPlanner",
  1078. Alias: "",
  1079. JoinType: ast.INNER_JOIN,
  1080. Expr: &ast.BinaryExpr{
  1081. RHS: &ast.BinaryExpr{
  1082. OP: ast.EQ,
  1083. LHS: &ast.FieldRef{Name: "id2", StreamName: "src2"},
  1084. RHS: &ast.FieldRef{Name: "id", StreamName: "tableInPlanner"},
  1085. },
  1086. OP: ast.AND,
  1087. LHS: &ast.BinaryExpr{
  1088. OP: ast.GT,
  1089. LHS: &ast.FieldRef{Name: "hum1", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  1090. &ast.FieldRef{
  1091. Name: "hum",
  1092. StreamName: "src2",
  1093. },
  1094. []ast.StreamName{"src2"},
  1095. &boolFalse,
  1096. )},
  1097. RHS: &ast.FieldRef{Name: "hum2", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  1098. &ast.FieldRef{
  1099. Name: "hum",
  1100. StreamName: "tableInPlanner",
  1101. },
  1102. []ast.StreamName{"tableInPlanner"},
  1103. &boolFalse,
  1104. )},
  1105. },
  1106. },
  1107. },
  1108. },
  1109. }.Init(),
  1110. },
  1111. },
  1112. fields: []ast.Field{
  1113. {
  1114. Expr: &ast.FieldRef{Name: "hum1", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  1115. &ast.FieldRef{
  1116. Name: "hum",
  1117. StreamName: "src2",
  1118. },
  1119. []ast.StreamName{"src2"},
  1120. &boolFalse,
  1121. )},
  1122. Name: "hum",
  1123. AName: "hum1",
  1124. }, {
  1125. Expr: &ast.FieldRef{Name: "hum2", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  1126. &ast.FieldRef{
  1127. Name: "hum",
  1128. StreamName: "tableInPlanner",
  1129. },
  1130. []ast.StreamName{"tableInPlanner"},
  1131. &boolFalse,
  1132. )},
  1133. Name: "hum",
  1134. AName: "hum2",
  1135. },
  1136. },
  1137. isAggregate: false,
  1138. sendMeta: false,
  1139. }.Init(),
  1140. },
  1141. { // 12 meta with more fields
  1142. sql: `SELECT temp, meta(*) as m FROM src1 WHERE meta(device)="demo2"`,
  1143. p: ProjectPlan{
  1144. baseLogicalPlan: baseLogicalPlan{
  1145. children: []LogicalPlan{
  1146. FilterPlan{
  1147. baseLogicalPlan: baseLogicalPlan{
  1148. children: []LogicalPlan{
  1149. DataSourcePlan{
  1150. name: "src1",
  1151. streamFields: map[string]*ast.JsonStreamField{
  1152. "temp": {
  1153. Type: "bigint",
  1154. },
  1155. },
  1156. streamStmt: streams["src1"],
  1157. metaFields: []string{},
  1158. allMeta: true,
  1159. }.Init(),
  1160. },
  1161. },
  1162. condition: &ast.BinaryExpr{
  1163. LHS: &ast.Call{
  1164. Name: "meta",
  1165. FuncId: 1,
  1166. Args: []ast.Expr{&ast.MetaRef{
  1167. Name: "device",
  1168. StreamName: ast.DefaultStream,
  1169. }},
  1170. },
  1171. OP: ast.EQ,
  1172. RHS: &ast.StringLiteral{
  1173. Val: "demo2",
  1174. },
  1175. },
  1176. }.Init(),
  1177. },
  1178. },
  1179. fields: []ast.Field{
  1180. {
  1181. Expr: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  1182. Name: "temp",
  1183. AName: "",
  1184. }, {
  1185. Expr: &ast.FieldRef{Name: "m", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  1186. &ast.Call{Name: "meta", FuncId: 0, Args: []ast.Expr{&ast.MetaRef{
  1187. Name: "*",
  1188. StreamName: ast.DefaultStream,
  1189. }}},
  1190. []ast.StreamName{},
  1191. nil,
  1192. )},
  1193. Name: "meta",
  1194. AName: "m",
  1195. },
  1196. },
  1197. isAggregate: false,
  1198. sendMeta: false,
  1199. }.Init(),
  1200. },
  1201. { // 13 analytic function plan
  1202. sql: `SELECT latest(lag(name)), id1 FROM src1 WHERE lag(temp) > temp`,
  1203. p: ProjectPlan{
  1204. baseLogicalPlan: baseLogicalPlan{
  1205. children: []LogicalPlan{
  1206. FilterPlan{
  1207. baseLogicalPlan: baseLogicalPlan{
  1208. children: []LogicalPlan{
  1209. AnalyticFuncsPlan{
  1210. baseLogicalPlan: baseLogicalPlan{
  1211. children: []LogicalPlan{
  1212. DataSourcePlan{
  1213. name: "src1",
  1214. streamFields: map[string]*ast.JsonStreamField{
  1215. "id1": {
  1216. Type: "bigint",
  1217. },
  1218. "name": {
  1219. Type: "string",
  1220. },
  1221. "temp": {
  1222. Type: "bigint",
  1223. },
  1224. },
  1225. streamStmt: streams["src1"],
  1226. metaFields: []string{},
  1227. }.Init(),
  1228. },
  1229. },
  1230. funcs: []*ast.Call{
  1231. {
  1232. Name: "lag",
  1233. FuncId: 2,
  1234. CachedField: "$$a_lag_2",
  1235. Args: []ast.Expr{&ast.FieldRef{
  1236. Name: "temp",
  1237. StreamName: "src1",
  1238. }},
  1239. },
  1240. {
  1241. Name: "latest", FuncId: 1, CachedField: "$$a_latest_1", FuncType: ast.FuncTypeScalar, Args: []ast.Expr{&ast.Call{Name: "lag", FuncId: 0, Cached: true, CachedField: "$$a_lag_0", FuncType: ast.FuncTypeScalar, Args: []ast.Expr{&ast.FieldRef{Name: "name", StreamName: "src1"}}}},
  1242. },
  1243. {
  1244. Name: "lag", FuncId: 0, CachedField: "$$a_lag_0", FuncType: ast.FuncTypeScalar, Args: []ast.Expr{&ast.FieldRef{Name: "name", StreamName: "src1"}},
  1245. },
  1246. },
  1247. }.Init(),
  1248. },
  1249. },
  1250. condition: &ast.BinaryExpr{
  1251. LHS: &ast.Call{
  1252. Name: "lag",
  1253. FuncId: 2,
  1254. Args: []ast.Expr{&ast.FieldRef{
  1255. Name: "temp",
  1256. StreamName: "src1",
  1257. }},
  1258. CachedField: "$$a_lag_2",
  1259. Cached: true,
  1260. },
  1261. OP: ast.GT,
  1262. RHS: &ast.FieldRef{
  1263. Name: "temp",
  1264. StreamName: "src1",
  1265. },
  1266. },
  1267. }.Init(),
  1268. },
  1269. },
  1270. fields: []ast.Field{
  1271. {
  1272. Expr: &ast.Call{
  1273. Name: "latest",
  1274. FuncId: 1,
  1275. Args: []ast.Expr{&ast.Call{Name: "lag", FuncId: 0, Cached: true, CachedField: "$$a_lag_0", FuncType: ast.FuncTypeScalar, Args: []ast.Expr{&ast.FieldRef{Name: "name", StreamName: "src1"}}}},
  1276. CachedField: "$$a_latest_1",
  1277. Cached: true,
  1278. },
  1279. Name: "latest",
  1280. }, {
  1281. Expr: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  1282. Name: "id1",
  1283. },
  1284. },
  1285. isAggregate: false,
  1286. sendMeta: false,
  1287. }.Init(),
  1288. },
  1289. { // 14
  1290. sql: `SELECT name, *, meta(device) FROM src1`,
  1291. p: ProjectPlan{
  1292. baseLogicalPlan: baseLogicalPlan{
  1293. children: []LogicalPlan{
  1294. DataSourcePlan{
  1295. baseLogicalPlan: baseLogicalPlan{},
  1296. name: "src1",
  1297. streamFields: map[string]*ast.JsonStreamField{
  1298. "id1": {
  1299. Type: "bigint",
  1300. },
  1301. "temp": {
  1302. Type: "bigint",
  1303. },
  1304. "name": {
  1305. Type: "string",
  1306. },
  1307. "myarray": {
  1308. Type: "array",
  1309. Items: &ast.JsonStreamField{
  1310. Type: "string",
  1311. },
  1312. },
  1313. },
  1314. streamStmt: streams["src1"],
  1315. metaFields: []string{"device"},
  1316. isWildCard: true,
  1317. }.Init(),
  1318. },
  1319. },
  1320. fields: []ast.Field{
  1321. {
  1322. Expr: &ast.FieldRef{Name: "name", StreamName: "src1"},
  1323. Name: "name",
  1324. AName: "",
  1325. },
  1326. {
  1327. Name: "*",
  1328. Expr: &ast.Wildcard{
  1329. Token: ast.ASTERISK,
  1330. },
  1331. },
  1332. {
  1333. Name: "meta",
  1334. Expr: &ast.Call{
  1335. Name: "meta",
  1336. Args: []ast.Expr{
  1337. &ast.MetaRef{
  1338. StreamName: ast.DefaultStream,
  1339. Name: "device",
  1340. },
  1341. },
  1342. },
  1343. },
  1344. },
  1345. isAggregate: false,
  1346. allWildcard: true,
  1347. sendMeta: false,
  1348. }.Init(),
  1349. },
  1350. { // 15 analytic function over partition plan
  1351. sql: `SELECT latest(lag(name)) OVER (PARTITION BY temp), id1 FROM src1 WHERE lag(temp) > temp`,
  1352. p: ProjectPlan{
  1353. baseLogicalPlan: baseLogicalPlan{
  1354. children: []LogicalPlan{
  1355. FilterPlan{
  1356. baseLogicalPlan: baseLogicalPlan{
  1357. children: []LogicalPlan{
  1358. AnalyticFuncsPlan{
  1359. baseLogicalPlan: baseLogicalPlan{
  1360. children: []LogicalPlan{
  1361. DataSourcePlan{
  1362. name: "src1",
  1363. streamFields: map[string]*ast.JsonStreamField{
  1364. "id1": {
  1365. Type: "bigint",
  1366. },
  1367. "name": {
  1368. Type: "string",
  1369. },
  1370. "temp": {
  1371. Type: "bigint",
  1372. },
  1373. },
  1374. streamStmt: streams["src1"],
  1375. metaFields: []string{},
  1376. }.Init(),
  1377. },
  1378. },
  1379. funcs: []*ast.Call{
  1380. {
  1381. Name: "lag",
  1382. FuncId: 2,
  1383. CachedField: "$$a_lag_2",
  1384. Args: []ast.Expr{&ast.FieldRef{
  1385. Name: "temp",
  1386. StreamName: "src1",
  1387. }},
  1388. },
  1389. {
  1390. Name: "latest", FuncId: 1, CachedField: "$$a_latest_1", FuncType: ast.FuncTypeScalar, Args: []ast.Expr{&ast.Call{Name: "lag", FuncId: 0, Cached: true, CachedField: "$$a_lag_0", FuncType: ast.FuncTypeScalar, Args: []ast.Expr{&ast.FieldRef{Name: "name", StreamName: "src1"}}}}, Partition: &ast.PartitionExpr{Exprs: []ast.Expr{&ast.FieldRef{Name: "temp", StreamName: "src1"}}},
  1391. },
  1392. {
  1393. Name: "lag", FuncId: 0, CachedField: "$$a_lag_0", FuncType: ast.FuncTypeScalar, Args: []ast.Expr{&ast.FieldRef{Name: "name", StreamName: "src1"}},
  1394. },
  1395. },
  1396. }.Init(),
  1397. },
  1398. },
  1399. condition: &ast.BinaryExpr{
  1400. LHS: &ast.Call{
  1401. Name: "lag",
  1402. FuncId: 2,
  1403. Args: []ast.Expr{&ast.FieldRef{
  1404. Name: "temp",
  1405. StreamName: "src1",
  1406. }},
  1407. CachedField: "$$a_lag_2",
  1408. Cached: true,
  1409. },
  1410. OP: ast.GT,
  1411. RHS: &ast.FieldRef{
  1412. Name: "temp",
  1413. StreamName: "src1",
  1414. },
  1415. },
  1416. }.Init(),
  1417. },
  1418. },
  1419. fields: []ast.Field{
  1420. {
  1421. Expr: &ast.Call{
  1422. Name: "latest",
  1423. FuncId: 1,
  1424. Args: []ast.Expr{&ast.Call{Name: "lag", FuncId: 0, Cached: true, CachedField: "$$a_lag_0", FuncType: ast.FuncTypeScalar, Args: []ast.Expr{&ast.FieldRef{Name: "name", StreamName: "src1"}}}},
  1425. CachedField: "$$a_latest_1",
  1426. Cached: true,
  1427. Partition: &ast.PartitionExpr{
  1428. Exprs: []ast.Expr{
  1429. &ast.FieldRef{Name: "temp", StreamName: "src1"},
  1430. },
  1431. },
  1432. },
  1433. Name: "latest",
  1434. }, {
  1435. Expr: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  1436. Name: "id1",
  1437. },
  1438. },
  1439. isAggregate: false,
  1440. sendMeta: false,
  1441. }.Init(),
  1442. },
  1443. { // 16 analytic function over partition when plan
  1444. sql: `SELECT latest(lag(name)) OVER (PARTITION BY temp WHEN temp > 12), id1 FROM src1 WHERE lag(temp) > temp`,
  1445. p: ProjectPlan{
  1446. baseLogicalPlan: baseLogicalPlan{
  1447. children: []LogicalPlan{
  1448. FilterPlan{
  1449. baseLogicalPlan: baseLogicalPlan{
  1450. children: []LogicalPlan{
  1451. AnalyticFuncsPlan{
  1452. baseLogicalPlan: baseLogicalPlan{
  1453. children: []LogicalPlan{
  1454. DataSourcePlan{
  1455. name: "src1",
  1456. streamFields: map[string]*ast.JsonStreamField{
  1457. "id1": {
  1458. Type: "bigint",
  1459. },
  1460. "name": {
  1461. Type: "string",
  1462. },
  1463. "temp": {
  1464. Type: "bigint",
  1465. },
  1466. },
  1467. streamStmt: streams["src1"],
  1468. metaFields: []string{},
  1469. }.Init(),
  1470. },
  1471. },
  1472. funcs: []*ast.Call{
  1473. {
  1474. Name: "lag",
  1475. FuncId: 2,
  1476. CachedField: "$$a_lag_2",
  1477. Args: []ast.Expr{&ast.FieldRef{
  1478. Name: "temp",
  1479. StreamName: "src1",
  1480. }},
  1481. },
  1482. {
  1483. Name: "latest", FuncId: 1, CachedField: "$$a_latest_1", FuncType: ast.FuncTypeScalar, Args: []ast.Expr{&ast.Call{Name: "lag", FuncId: 0, Cached: true, CachedField: "$$a_lag_0", FuncType: ast.FuncTypeScalar, Args: []ast.Expr{&ast.FieldRef{Name: "name", StreamName: "src1"}}}}, Partition: &ast.PartitionExpr{Exprs: []ast.Expr{&ast.FieldRef{Name: "temp", StreamName: "src1"}}}, WhenExpr: &ast.BinaryExpr{LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"}, OP: ast.GT, RHS: &ast.IntegerLiteral{Val: 12}},
  1484. },
  1485. {
  1486. Name: "lag", FuncId: 0, CachedField: "$$a_lag_0", FuncType: ast.FuncTypeScalar, Args: []ast.Expr{&ast.FieldRef{Name: "name", StreamName: "src1"}},
  1487. },
  1488. },
  1489. }.Init(),
  1490. },
  1491. },
  1492. condition: &ast.BinaryExpr{
  1493. LHS: &ast.Call{
  1494. Name: "lag",
  1495. FuncId: 2,
  1496. Args: []ast.Expr{&ast.FieldRef{
  1497. Name: "temp",
  1498. StreamName: "src1",
  1499. }},
  1500. CachedField: "$$a_lag_2",
  1501. Cached: true,
  1502. },
  1503. OP: ast.GT,
  1504. RHS: &ast.FieldRef{
  1505. Name: "temp",
  1506. StreamName: "src1",
  1507. },
  1508. },
  1509. }.Init(),
  1510. },
  1511. },
  1512. fields: []ast.Field{
  1513. {
  1514. Expr: &ast.Call{
  1515. Name: "latest",
  1516. FuncId: 1,
  1517. Args: []ast.Expr{&ast.Call{Name: "lag", FuncId: 0, Cached: true, CachedField: "$$a_lag_0", FuncType: ast.FuncTypeScalar, Args: []ast.Expr{&ast.FieldRef{Name: "name", StreamName: "src1"}}}},
  1518. CachedField: "$$a_latest_1",
  1519. Cached: true,
  1520. Partition: &ast.PartitionExpr{
  1521. Exprs: []ast.Expr{
  1522. &ast.FieldRef{Name: "temp", StreamName: "src1"},
  1523. },
  1524. },
  1525. WhenExpr: &ast.BinaryExpr{
  1526. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  1527. OP: ast.GT,
  1528. RHS: &ast.IntegerLiteral{Val: 12},
  1529. },
  1530. },
  1531. Name: "latest",
  1532. }, {
  1533. Expr: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  1534. Name: "id1",
  1535. },
  1536. },
  1537. isAggregate: false,
  1538. sendMeta: false,
  1539. }.Init(),
  1540. },
  1541. { // 17. do not optimize sliding window
  1542. sql: `SELECT * FROM src1 WHERE temp > 20 GROUP BY SLIDINGWINDOW(ss, 10) HAVING COUNT(*) > 2`,
  1543. p: ProjectPlan{
  1544. baseLogicalPlan: baseLogicalPlan{
  1545. children: []LogicalPlan{
  1546. HavingPlan{
  1547. baseLogicalPlan: baseLogicalPlan{
  1548. children: []LogicalPlan{
  1549. FilterPlan{
  1550. baseLogicalPlan: baseLogicalPlan{
  1551. children: []LogicalPlan{
  1552. WindowPlan{
  1553. baseLogicalPlan: baseLogicalPlan{
  1554. children: []LogicalPlan{
  1555. DataSourcePlan{
  1556. name: "src1",
  1557. isWildCard: true,
  1558. streamFields: map[string]*ast.JsonStreamField{
  1559. "id1": {
  1560. Type: "bigint",
  1561. },
  1562. "temp": {
  1563. Type: "bigint",
  1564. },
  1565. "name": {
  1566. Type: "string",
  1567. },
  1568. "myarray": {
  1569. Type: "array",
  1570. Items: &ast.JsonStreamField{
  1571. Type: "string",
  1572. },
  1573. },
  1574. },
  1575. streamStmt: streams["src1"],
  1576. metaFields: []string{},
  1577. }.Init(),
  1578. },
  1579. },
  1580. condition: nil,
  1581. wtype: ast.SLIDING_WINDOW,
  1582. length: 10000,
  1583. interval: 0,
  1584. limit: 0,
  1585. }.Init(),
  1586. },
  1587. },
  1588. condition: &ast.BinaryExpr{
  1589. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  1590. OP: ast.GT,
  1591. RHS: &ast.IntegerLiteral{Val: 20},
  1592. },
  1593. }.Init(),
  1594. },
  1595. },
  1596. condition: &ast.BinaryExpr{
  1597. LHS: &ast.Call{Name: "count", FuncId: 0, Args: []ast.Expr{&ast.Wildcard{
  1598. Token: ast.ASTERISK,
  1599. }}, FuncType: ast.FuncTypeAgg},
  1600. OP: ast.GT,
  1601. RHS: &ast.IntegerLiteral{Val: 2},
  1602. },
  1603. }.Init(),
  1604. },
  1605. },
  1606. fields: []ast.Field{
  1607. {
  1608. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  1609. Name: "*",
  1610. AName: "",
  1611. },
  1612. },
  1613. isAggregate: false,
  1614. sendMeta: false,
  1615. }.Init(),
  1616. },
  1617. {
  1618. // 18 analytic function over when plan
  1619. sql: `SELECT CASE WHEN lag(temp) OVER (WHEN lag(id1) > 1) BETWEEN 0 AND 10 THEN 1 ELSE 0 END FROM src1`,
  1620. p: ProjectPlan{
  1621. baseLogicalPlan: baseLogicalPlan{
  1622. children: []LogicalPlan{
  1623. AnalyticFuncsPlan{
  1624. baseLogicalPlan: baseLogicalPlan{
  1625. children: []LogicalPlan{
  1626. DataSourcePlan{
  1627. name: "src1",
  1628. streamFields: map[string]*ast.JsonStreamField{
  1629. "id1": {
  1630. Type: "bigint",
  1631. },
  1632. "temp": {
  1633. Type: "bigint",
  1634. },
  1635. },
  1636. streamStmt: &ast.StreamStmt{
  1637. Name: "src1",
  1638. StreamFields: []ast.StreamField{
  1639. {
  1640. Name: "id1",
  1641. FieldType: &ast.BasicType{
  1642. Type: ast.DataType(1),
  1643. },
  1644. },
  1645. {
  1646. Name: "temp",
  1647. FieldType: &ast.BasicType{
  1648. Type: ast.DataType(1),
  1649. },
  1650. },
  1651. {
  1652. Name: "name",
  1653. FieldType: &ast.BasicType{
  1654. Type: ast.DataType(3),
  1655. },
  1656. },
  1657. {
  1658. Name: "myarray",
  1659. FieldType: &ast.ArrayType{
  1660. Type: ast.DataType(3),
  1661. },
  1662. },
  1663. },
  1664. Options: &ast.Options{
  1665. DATASOURCE: "src1",
  1666. KEY: "ts",
  1667. FORMAT: "json",
  1668. },
  1669. StreamType: ast.StreamType(0),
  1670. },
  1671. metaFields: []string{},
  1672. }.Init(),
  1673. },
  1674. },
  1675. funcs: []*ast.Call{
  1676. {
  1677. Name: "lag",
  1678. FuncId: 0,
  1679. FuncType: ast.FuncType(0),
  1680. Args: []ast.Expr{
  1681. &ast.FieldRef{
  1682. StreamName: "src1",
  1683. Name: "temp",
  1684. },
  1685. },
  1686. CachedField: "$$a_lag_0",
  1687. WhenExpr: &ast.BinaryExpr{
  1688. OP: ast.GT,
  1689. LHS: &ast.Call{
  1690. Name: "lag",
  1691. FuncId: 1,
  1692. FuncType: ast.FuncType(0),
  1693. Args: []ast.Expr{
  1694. &ast.FieldRef{
  1695. StreamName: "src1",
  1696. Name: "id1",
  1697. },
  1698. },
  1699. CachedField: "$$a_lag_1",
  1700. Cached: true,
  1701. },
  1702. RHS: &ast.IntegerLiteral{
  1703. Val: 1,
  1704. },
  1705. },
  1706. },
  1707. {
  1708. Name: "lag",
  1709. FuncId: 1,
  1710. FuncType: ast.FuncType(0),
  1711. Args: []ast.Expr{
  1712. &ast.FieldRef{
  1713. StreamName: "src1",
  1714. Name: "id1",
  1715. },
  1716. },
  1717. CachedField: "$$a_lag_1",
  1718. },
  1719. },
  1720. }.Init(),
  1721. },
  1722. },
  1723. fields: []ast.Field{
  1724. {
  1725. Name: "kuiper_field_0",
  1726. Expr: &ast.CaseExpr{
  1727. WhenClauses: []*ast.WhenClause{
  1728. {
  1729. Expr: &ast.BinaryExpr{
  1730. OP: ast.BETWEEN,
  1731. LHS: &ast.Call{
  1732. Name: "lag",
  1733. FuncId: 0,
  1734. FuncType: ast.FuncType(0),
  1735. Args: []ast.Expr{
  1736. &ast.FieldRef{
  1737. StreamName: "src1",
  1738. Name: "temp",
  1739. },
  1740. },
  1741. CachedField: "$$a_lag_0",
  1742. Cached: true,
  1743. WhenExpr: &ast.BinaryExpr{
  1744. OP: ast.GT,
  1745. LHS: &ast.Call{
  1746. Name: "lag",
  1747. FuncId: 1,
  1748. FuncType: ast.FuncType(0),
  1749. Args: []ast.Expr{
  1750. &ast.FieldRef{
  1751. StreamName: "src1",
  1752. Name: "id1",
  1753. },
  1754. },
  1755. CachedField: "$$a_lag_1",
  1756. Cached: true,
  1757. },
  1758. RHS: &ast.IntegerLiteral{
  1759. Val: 1,
  1760. },
  1761. },
  1762. },
  1763. RHS: &ast.BetweenExpr{
  1764. Lower: &ast.IntegerLiteral{
  1765. Val: 0,
  1766. },
  1767. Higher: &ast.IntegerLiteral{
  1768. Val: 10,
  1769. },
  1770. },
  1771. },
  1772. Result: &ast.IntegerLiteral{
  1773. Val: 1,
  1774. },
  1775. },
  1776. },
  1777. ElseClause: &ast.IntegerLiteral{
  1778. Val: 0,
  1779. },
  1780. },
  1781. },
  1782. },
  1783. }.Init(),
  1784. },
  1785. }
  1786. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  1787. for i, tt := range tests {
  1788. stmt, err := xsql.NewParser(strings.NewReader(tt.sql)).Parse()
  1789. if err != nil {
  1790. t.Errorf("%d. %q: error compile sql: %s\n", i, tt.sql, err)
  1791. continue
  1792. }
  1793. p, err := createLogicalPlan(stmt, &api.RuleOption{
  1794. IsEventTime: false,
  1795. LateTol: 0,
  1796. Concurrency: 0,
  1797. BufferLength: 0,
  1798. SendMetaToSink: false,
  1799. Qos: 0,
  1800. CheckpointInterval: 0,
  1801. SendError: true,
  1802. }, store)
  1803. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  1804. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.sql, tt.err, err)
  1805. } else if !reflect.DeepEqual(tt.p, p) {
  1806. 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))
  1807. }
  1808. }
  1809. }
  1810. func Test_createLogicalPlanSchemaless(t *testing.T) {
  1811. store, err := store.GetKV("stream")
  1812. if err != nil {
  1813. t.Error(err)
  1814. return
  1815. }
  1816. streamSqls := map[string]string{
  1817. "src1": `CREATE STREAM src1 (
  1818. ) WITH (DATASOURCE="src1", FORMAT="json", KEY="ts");`,
  1819. "src2": `CREATE STREAM src2 (
  1820. ) WITH (DATASOURCE="src2", FORMAT="json", KEY="ts");`,
  1821. "tableInPlanner": `CREATE TABLE tableInPlanner (
  1822. id BIGINT,
  1823. name STRING,
  1824. value STRING,
  1825. hum BIGINT
  1826. ) WITH (TYPE="file");`,
  1827. }
  1828. types := map[string]ast.StreamType{
  1829. "src1": ast.TypeStream,
  1830. "src2": ast.TypeStream,
  1831. "tableInPlanner": ast.TypeTable,
  1832. }
  1833. for name, sql := range streamSqls {
  1834. s, err := json.Marshal(&xsql.StreamInfo{
  1835. StreamType: types[name],
  1836. Statement: sql,
  1837. })
  1838. if err != nil {
  1839. t.Error(err)
  1840. t.Fail()
  1841. }
  1842. err = store.Set(name, string(s))
  1843. if err != nil {
  1844. t.Error(err)
  1845. t.Fail()
  1846. }
  1847. }
  1848. streams := make(map[string]*ast.StreamStmt)
  1849. for n := range streamSqls {
  1850. streamStmt, err := xsql.GetDataSource(store, n)
  1851. if err != nil {
  1852. t.Errorf("fail to get stream %s, please check if stream is created", n)
  1853. return
  1854. }
  1855. streams[n] = streamStmt
  1856. }
  1857. // boolTrue = true
  1858. boolFalse := false
  1859. tests := []struct {
  1860. sql string
  1861. p LogicalPlan
  1862. err string
  1863. }{
  1864. { // 0
  1865. sql: `SELECT name FROM src1`,
  1866. p: ProjectPlan{
  1867. baseLogicalPlan: baseLogicalPlan{
  1868. children: []LogicalPlan{
  1869. DataSourcePlan{
  1870. baseLogicalPlan: baseLogicalPlan{},
  1871. name: "src1",
  1872. streamFields: map[string]*ast.JsonStreamField{
  1873. "name": nil,
  1874. },
  1875. streamStmt: streams["src1"],
  1876. isSchemaless: true,
  1877. metaFields: []string{},
  1878. }.Init(),
  1879. },
  1880. },
  1881. fields: []ast.Field{
  1882. {
  1883. Expr: &ast.FieldRef{Name: "name", StreamName: "src1"},
  1884. Name: "name",
  1885. AName: "",
  1886. },
  1887. },
  1888. isAggregate: false,
  1889. sendMeta: false,
  1890. }.Init(),
  1891. }, { // 1 optimize where to data source
  1892. sql: `SELECT temp FROM src1 WHERE name = "v1" GROUP BY TUMBLINGWINDOW(ss, 10)`,
  1893. p: ProjectPlan{
  1894. baseLogicalPlan: baseLogicalPlan{
  1895. children: []LogicalPlan{
  1896. WindowPlan{
  1897. baseLogicalPlan: baseLogicalPlan{
  1898. children: []LogicalPlan{
  1899. FilterPlan{
  1900. baseLogicalPlan: baseLogicalPlan{
  1901. children: []LogicalPlan{
  1902. DataSourcePlan{
  1903. name: "src1",
  1904. streamFields: map[string]*ast.JsonStreamField{
  1905. "name": nil,
  1906. "temp": nil,
  1907. },
  1908. streamStmt: streams["src1"],
  1909. metaFields: []string{},
  1910. isSchemaless: true,
  1911. }.Init(),
  1912. },
  1913. },
  1914. condition: &ast.BinaryExpr{
  1915. LHS: &ast.FieldRef{Name: "name", StreamName: "src1"},
  1916. OP: ast.EQ,
  1917. RHS: &ast.StringLiteral{Val: "v1"},
  1918. },
  1919. }.Init(),
  1920. },
  1921. },
  1922. condition: nil,
  1923. wtype: ast.TUMBLING_WINDOW,
  1924. length: 10000,
  1925. interval: 0,
  1926. limit: 0,
  1927. }.Init(),
  1928. },
  1929. },
  1930. fields: []ast.Field{
  1931. {
  1932. Expr: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  1933. Name: "temp",
  1934. AName: "",
  1935. },
  1936. },
  1937. isAggregate: false,
  1938. sendMeta: false,
  1939. }.Init(),
  1940. }, { // 2 condition that cannot be optimized
  1941. 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)`,
  1942. p: ProjectPlan{
  1943. baseLogicalPlan: baseLogicalPlan{
  1944. children: []LogicalPlan{
  1945. JoinPlan{
  1946. baseLogicalPlan: baseLogicalPlan{
  1947. children: []LogicalPlan{
  1948. WindowPlan{
  1949. baseLogicalPlan: baseLogicalPlan{
  1950. children: []LogicalPlan{
  1951. DataSourcePlan{
  1952. name: "src1",
  1953. streamFields: map[string]*ast.JsonStreamField{
  1954. "id1": nil,
  1955. "temp": nil,
  1956. },
  1957. streamStmt: streams["src1"],
  1958. metaFields: []string{},
  1959. isSchemaless: true,
  1960. }.Init(),
  1961. DataSourcePlan{
  1962. name: "src2",
  1963. streamFields: map[string]*ast.JsonStreamField{ // can't determine where is id1 belonged to
  1964. "hum": nil,
  1965. "id1": nil,
  1966. "id2": nil,
  1967. },
  1968. isSchemaless: true,
  1969. streamStmt: streams["src2"],
  1970. metaFields: []string{},
  1971. }.Init(),
  1972. },
  1973. },
  1974. condition: nil,
  1975. wtype: ast.TUMBLING_WINDOW,
  1976. length: 10000,
  1977. interval: 0,
  1978. limit: 0,
  1979. }.Init(),
  1980. },
  1981. },
  1982. from: &ast.Table{Name: "src1"},
  1983. joins: ast.Joins{ast.Join{
  1984. Name: "src2",
  1985. JoinType: ast.INNER_JOIN,
  1986. Expr: &ast.BinaryExpr{
  1987. OP: ast.AND,
  1988. LHS: &ast.BinaryExpr{
  1989. LHS: &ast.BinaryExpr{
  1990. OP: ast.GT,
  1991. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  1992. RHS: &ast.IntegerLiteral{Val: 20},
  1993. },
  1994. OP: ast.OR,
  1995. RHS: &ast.BinaryExpr{
  1996. OP: ast.GT,
  1997. LHS: &ast.FieldRef{Name: "hum", StreamName: "src2"},
  1998. RHS: &ast.IntegerLiteral{Val: 60},
  1999. },
  2000. },
  2001. RHS: &ast.BinaryExpr{
  2002. OP: ast.EQ,
  2003. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  2004. RHS: &ast.FieldRef{Name: "id2", StreamName: "src2"},
  2005. },
  2006. },
  2007. }},
  2008. }.Init(),
  2009. },
  2010. },
  2011. fields: []ast.Field{
  2012. {
  2013. Expr: &ast.FieldRef{Name: "id1", StreamName: ast.DefaultStream},
  2014. Name: "id1",
  2015. AName: "",
  2016. },
  2017. },
  2018. isAggregate: false,
  2019. sendMeta: false,
  2020. }.Init(),
  2021. }, { // 3 optimize window filter
  2022. sql: `SELECT id1 FROM src1 WHERE name = "v1" GROUP BY TUMBLINGWINDOW(ss, 10) FILTER( WHERE temp > 2)`,
  2023. p: ProjectPlan{
  2024. baseLogicalPlan: baseLogicalPlan{
  2025. children: []LogicalPlan{
  2026. WindowPlan{
  2027. baseLogicalPlan: baseLogicalPlan{
  2028. children: []LogicalPlan{
  2029. FilterPlan{
  2030. baseLogicalPlan: baseLogicalPlan{
  2031. children: []LogicalPlan{
  2032. DataSourcePlan{
  2033. name: "src1",
  2034. streamFields: map[string]*ast.JsonStreamField{
  2035. "id1": nil,
  2036. "name": nil,
  2037. "temp": nil,
  2038. },
  2039. isSchemaless: true,
  2040. streamStmt: streams["src1"],
  2041. metaFields: []string{},
  2042. }.Init(),
  2043. },
  2044. },
  2045. condition: &ast.BinaryExpr{
  2046. OP: ast.AND,
  2047. LHS: &ast.BinaryExpr{
  2048. LHS: &ast.FieldRef{Name: "name", StreamName: "src1"},
  2049. OP: ast.EQ,
  2050. RHS: &ast.StringLiteral{Val: "v1"},
  2051. },
  2052. RHS: &ast.BinaryExpr{
  2053. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  2054. OP: ast.GT,
  2055. RHS: &ast.IntegerLiteral{Val: 2},
  2056. },
  2057. },
  2058. }.Init(),
  2059. },
  2060. },
  2061. condition: nil,
  2062. wtype: ast.TUMBLING_WINDOW,
  2063. length: 10000,
  2064. interval: 0,
  2065. limit: 0,
  2066. }.Init(),
  2067. },
  2068. },
  2069. fields: []ast.Field{
  2070. {
  2071. Expr: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  2072. Name: "id1",
  2073. AName: "",
  2074. },
  2075. },
  2076. isAggregate: false,
  2077. sendMeta: false,
  2078. }.Init(),
  2079. }, { // 4. do not optimize count window
  2080. sql: `SELECT * FROM src1 WHERE temp > 20 GROUP BY COUNTWINDOW(5,1) HAVING COUNT(*) > 2`,
  2081. p: ProjectPlan{
  2082. baseLogicalPlan: baseLogicalPlan{
  2083. children: []LogicalPlan{
  2084. HavingPlan{
  2085. baseLogicalPlan: baseLogicalPlan{
  2086. children: []LogicalPlan{
  2087. FilterPlan{
  2088. baseLogicalPlan: baseLogicalPlan{
  2089. children: []LogicalPlan{
  2090. WindowPlan{
  2091. baseLogicalPlan: baseLogicalPlan{
  2092. children: []LogicalPlan{
  2093. DataSourcePlan{
  2094. name: "src1",
  2095. isWildCard: true,
  2096. streamFields: map[string]*ast.JsonStreamField{},
  2097. streamStmt: streams["src1"],
  2098. metaFields: []string{},
  2099. isSchemaless: true,
  2100. }.Init(),
  2101. },
  2102. },
  2103. condition: nil,
  2104. wtype: ast.COUNT_WINDOW,
  2105. length: 5,
  2106. interval: 1,
  2107. limit: 0,
  2108. }.Init(),
  2109. },
  2110. },
  2111. condition: &ast.BinaryExpr{
  2112. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  2113. OP: ast.GT,
  2114. RHS: &ast.IntegerLiteral{Val: 20},
  2115. },
  2116. }.Init(),
  2117. },
  2118. },
  2119. condition: &ast.BinaryExpr{
  2120. LHS: &ast.Call{Name: "count", FuncId: 0, Args: []ast.Expr{&ast.Wildcard{
  2121. Token: ast.ASTERISK,
  2122. }}, FuncType: ast.FuncTypeAgg},
  2123. OP: ast.GT,
  2124. RHS: &ast.IntegerLiteral{Val: 2},
  2125. },
  2126. }.Init(),
  2127. },
  2128. },
  2129. fields: []ast.Field{
  2130. {
  2131. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  2132. Name: "*",
  2133. AName: "",
  2134. },
  2135. },
  2136. isAggregate: false,
  2137. sendMeta: false,
  2138. }.Init(),
  2139. }, { // 5. optimize join on
  2140. 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)`,
  2141. p: ProjectPlan{
  2142. baseLogicalPlan: baseLogicalPlan{
  2143. children: []LogicalPlan{
  2144. JoinPlan{
  2145. baseLogicalPlan: baseLogicalPlan{
  2146. children: []LogicalPlan{
  2147. WindowPlan{
  2148. baseLogicalPlan: baseLogicalPlan{
  2149. children: []LogicalPlan{
  2150. FilterPlan{
  2151. baseLogicalPlan: baseLogicalPlan{
  2152. children: []LogicalPlan{
  2153. DataSourcePlan{
  2154. name: "src1",
  2155. streamFields: map[string]*ast.JsonStreamField{
  2156. "id1": nil,
  2157. "temp": nil,
  2158. },
  2159. isSchemaless: true,
  2160. streamStmt: streams["src1"],
  2161. metaFields: []string{},
  2162. }.Init(),
  2163. },
  2164. },
  2165. condition: &ast.BinaryExpr{
  2166. RHS: &ast.BinaryExpr{
  2167. OP: ast.GT,
  2168. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  2169. RHS: &ast.IntegerLiteral{Val: 20},
  2170. },
  2171. OP: ast.AND,
  2172. LHS: &ast.BinaryExpr{
  2173. OP: ast.GT,
  2174. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  2175. RHS: &ast.IntegerLiteral{Val: 111},
  2176. },
  2177. },
  2178. }.Init(),
  2179. FilterPlan{
  2180. baseLogicalPlan: baseLogicalPlan{
  2181. children: []LogicalPlan{
  2182. DataSourcePlan{
  2183. name: "src2",
  2184. streamFields: map[string]*ast.JsonStreamField{
  2185. "hum": nil,
  2186. "id1": nil,
  2187. "id2": nil,
  2188. },
  2189. isSchemaless: true,
  2190. streamStmt: streams["src2"],
  2191. metaFields: []string{},
  2192. }.Init(),
  2193. },
  2194. },
  2195. condition: &ast.BinaryExpr{
  2196. OP: ast.LT,
  2197. LHS: &ast.FieldRef{Name: "hum", StreamName: "src2"},
  2198. RHS: &ast.IntegerLiteral{Val: 60},
  2199. },
  2200. }.Init(),
  2201. },
  2202. },
  2203. condition: nil,
  2204. wtype: ast.TUMBLING_WINDOW,
  2205. length: 10000,
  2206. interval: 0,
  2207. limit: 0,
  2208. }.Init(),
  2209. },
  2210. },
  2211. from: &ast.Table{
  2212. Name: "src1",
  2213. },
  2214. joins: []ast.Join{
  2215. {
  2216. Name: "src2",
  2217. Alias: "",
  2218. JoinType: ast.INNER_JOIN,
  2219. Expr: &ast.BinaryExpr{
  2220. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  2221. OP: ast.EQ,
  2222. RHS: &ast.FieldRef{Name: "id2", StreamName: "src2"},
  2223. },
  2224. },
  2225. },
  2226. }.Init(),
  2227. },
  2228. },
  2229. fields: []ast.Field{
  2230. {
  2231. Expr: &ast.FieldRef{Name: "id1", StreamName: ast.DefaultStream},
  2232. Name: "id1",
  2233. AName: "",
  2234. },
  2235. },
  2236. isAggregate: false,
  2237. sendMeta: false,
  2238. }.Init(),
  2239. }, { // 6. optimize outter join on
  2240. 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)`,
  2241. p: ProjectPlan{
  2242. baseLogicalPlan: baseLogicalPlan{
  2243. children: []LogicalPlan{
  2244. JoinPlan{
  2245. baseLogicalPlan: baseLogicalPlan{
  2246. children: []LogicalPlan{
  2247. WindowPlan{
  2248. baseLogicalPlan: baseLogicalPlan{
  2249. children: []LogicalPlan{
  2250. FilterPlan{
  2251. baseLogicalPlan: baseLogicalPlan{
  2252. children: []LogicalPlan{
  2253. DataSourcePlan{
  2254. name: "src1",
  2255. streamFields: map[string]*ast.JsonStreamField{
  2256. "id1": nil,
  2257. "temp": nil,
  2258. },
  2259. isSchemaless: true,
  2260. streamStmt: streams["src1"],
  2261. metaFields: []string{},
  2262. }.Init(),
  2263. },
  2264. },
  2265. condition: &ast.BinaryExpr{
  2266. OP: ast.GT,
  2267. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  2268. RHS: &ast.IntegerLiteral{Val: 111},
  2269. },
  2270. }.Init(),
  2271. DataSourcePlan{
  2272. name: "src2",
  2273. streamFields: map[string]*ast.JsonStreamField{
  2274. "hum": nil,
  2275. "id1": nil,
  2276. "id2": nil,
  2277. },
  2278. isSchemaless: true,
  2279. streamStmt: streams["src2"],
  2280. metaFields: []string{},
  2281. }.Init(),
  2282. },
  2283. },
  2284. condition: nil,
  2285. wtype: ast.TUMBLING_WINDOW,
  2286. length: 10000,
  2287. interval: 0,
  2288. limit: 0,
  2289. }.Init(),
  2290. },
  2291. },
  2292. from: &ast.Table{
  2293. Name: "src1",
  2294. },
  2295. joins: []ast.Join{
  2296. {
  2297. Name: "src2",
  2298. Alias: "",
  2299. JoinType: ast.FULL_JOIN,
  2300. Expr: &ast.BinaryExpr{
  2301. OP: ast.AND,
  2302. LHS: &ast.BinaryExpr{
  2303. OP: ast.AND,
  2304. LHS: &ast.BinaryExpr{
  2305. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  2306. OP: ast.EQ,
  2307. RHS: &ast.FieldRef{Name: "id2", StreamName: "src2"},
  2308. },
  2309. RHS: &ast.BinaryExpr{
  2310. OP: ast.GT,
  2311. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  2312. RHS: &ast.IntegerLiteral{Val: 20},
  2313. },
  2314. },
  2315. RHS: &ast.BinaryExpr{
  2316. OP: ast.LT,
  2317. LHS: &ast.FieldRef{Name: "hum", StreamName: "src2"},
  2318. RHS: &ast.IntegerLiteral{Val: 60},
  2319. },
  2320. },
  2321. },
  2322. },
  2323. }.Init(),
  2324. },
  2325. },
  2326. fields: []ast.Field{
  2327. {
  2328. Expr: &ast.FieldRef{Name: "id1", StreamName: ast.DefaultStream},
  2329. Name: "id1",
  2330. AName: "",
  2331. },
  2332. },
  2333. isAggregate: false,
  2334. sendMeta: false,
  2335. }.Init(),
  2336. }, { // 7 window error for table
  2337. sql: `SELECT value FROM tableInPlanner WHERE name = "v1" GROUP BY TUMBLINGWINDOW(ss, 10)`,
  2338. p: nil,
  2339. err: "cannot run window for TABLE sources",
  2340. }, { // 8 join table without window
  2341. sql: `SELECT id1 FROM src1 INNER JOIN tableInPlanner on src1.id1 = tableInPlanner.id and src1.temp > 20 and hum < 60 WHERE src1.id1 > 111`,
  2342. p: ProjectPlan{
  2343. baseLogicalPlan: baseLogicalPlan{
  2344. children: []LogicalPlan{
  2345. JoinPlan{
  2346. baseLogicalPlan: baseLogicalPlan{
  2347. children: []LogicalPlan{
  2348. JoinAlignPlan{
  2349. baseLogicalPlan: baseLogicalPlan{
  2350. children: []LogicalPlan{
  2351. FilterPlan{
  2352. baseLogicalPlan: baseLogicalPlan{
  2353. children: []LogicalPlan{
  2354. DataSourcePlan{
  2355. name: "src1",
  2356. streamFields: map[string]*ast.JsonStreamField{
  2357. "hum": nil,
  2358. "id1": nil,
  2359. "temp": nil,
  2360. },
  2361. isSchemaless: true,
  2362. streamStmt: streams["src1"],
  2363. metaFields: []string{},
  2364. }.Init(),
  2365. },
  2366. },
  2367. condition: &ast.BinaryExpr{
  2368. RHS: &ast.BinaryExpr{
  2369. OP: ast.GT,
  2370. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  2371. RHS: &ast.IntegerLiteral{Val: 20},
  2372. },
  2373. OP: ast.AND,
  2374. LHS: &ast.BinaryExpr{
  2375. OP: ast.GT,
  2376. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  2377. RHS: &ast.IntegerLiteral{Val: 111},
  2378. },
  2379. },
  2380. }.Init(),
  2381. DataSourcePlan{
  2382. name: "tableInPlanner",
  2383. streamFields: map[string]*ast.JsonStreamField{
  2384. "hum": {
  2385. Type: "bigint",
  2386. },
  2387. "id": {
  2388. Type: "bigint",
  2389. },
  2390. },
  2391. streamStmt: streams["tableInPlanner"],
  2392. metaFields: []string{},
  2393. }.Init(),
  2394. },
  2395. },
  2396. Emitters: []string{"tableInPlanner"},
  2397. }.Init(),
  2398. },
  2399. },
  2400. from: &ast.Table{
  2401. Name: "src1",
  2402. },
  2403. joins: []ast.Join{
  2404. {
  2405. Name: "tableInPlanner",
  2406. Alias: "",
  2407. JoinType: ast.INNER_JOIN,
  2408. Expr: &ast.BinaryExpr{
  2409. OP: ast.AND,
  2410. LHS: &ast.BinaryExpr{
  2411. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  2412. OP: ast.EQ,
  2413. RHS: &ast.FieldRef{Name: "id", StreamName: "tableInPlanner"},
  2414. },
  2415. RHS: &ast.BinaryExpr{
  2416. OP: ast.LT,
  2417. LHS: &ast.FieldRef{Name: "hum", StreamName: ast.DefaultStream},
  2418. RHS: &ast.IntegerLiteral{Val: 60},
  2419. },
  2420. },
  2421. },
  2422. },
  2423. }.Init(),
  2424. },
  2425. },
  2426. fields: []ast.Field{
  2427. {
  2428. Expr: &ast.FieldRef{Name: "id1", StreamName: ast.DefaultStream},
  2429. Name: "id1",
  2430. AName: "",
  2431. },
  2432. },
  2433. isAggregate: false,
  2434. sendMeta: false,
  2435. }.Init(),
  2436. }, { // 9 join table with window
  2437. 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)`,
  2438. p: ProjectPlan{
  2439. baseLogicalPlan: baseLogicalPlan{
  2440. children: []LogicalPlan{
  2441. JoinPlan{
  2442. baseLogicalPlan: baseLogicalPlan{
  2443. children: []LogicalPlan{
  2444. JoinAlignPlan{
  2445. baseLogicalPlan: baseLogicalPlan{
  2446. children: []LogicalPlan{
  2447. WindowPlan{
  2448. baseLogicalPlan: baseLogicalPlan{
  2449. children: []LogicalPlan{
  2450. DataSourcePlan{
  2451. name: "src1",
  2452. streamFields: map[string]*ast.JsonStreamField{
  2453. "id1": nil,
  2454. "temp": nil,
  2455. },
  2456. isSchemaless: true,
  2457. streamStmt: streams["src1"],
  2458. metaFields: []string{},
  2459. }.Init(),
  2460. },
  2461. },
  2462. condition: nil,
  2463. wtype: ast.TUMBLING_WINDOW,
  2464. length: 10000,
  2465. interval: 0,
  2466. limit: 0,
  2467. }.Init(),
  2468. DataSourcePlan{
  2469. name: "tableInPlanner",
  2470. streamFields: map[string]*ast.JsonStreamField{
  2471. "hum": {
  2472. Type: "bigint",
  2473. },
  2474. "id": {
  2475. Type: "bigint",
  2476. },
  2477. },
  2478. streamStmt: streams["tableInPlanner"],
  2479. metaFields: []string{},
  2480. }.Init(),
  2481. },
  2482. },
  2483. Emitters: []string{"tableInPlanner"},
  2484. }.Init(),
  2485. },
  2486. },
  2487. from: &ast.Table{
  2488. Name: "src1",
  2489. },
  2490. joins: []ast.Join{
  2491. {
  2492. Name: "tableInPlanner",
  2493. Alias: "",
  2494. JoinType: ast.INNER_JOIN,
  2495. Expr: &ast.BinaryExpr{
  2496. OP: ast.AND,
  2497. LHS: &ast.BinaryExpr{
  2498. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  2499. OP: ast.EQ,
  2500. RHS: &ast.FieldRef{Name: "id", StreamName: "tableInPlanner"},
  2501. },
  2502. RHS: &ast.BinaryExpr{
  2503. RHS: &ast.BinaryExpr{
  2504. OP: ast.AND,
  2505. LHS: &ast.BinaryExpr{
  2506. OP: ast.GT,
  2507. LHS: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  2508. RHS: &ast.IntegerLiteral{Val: 20},
  2509. },
  2510. RHS: &ast.BinaryExpr{
  2511. OP: ast.LT,
  2512. LHS: &ast.FieldRef{Name: "hum", StreamName: "tableInPlanner"},
  2513. RHS: &ast.IntegerLiteral{Val: 60},
  2514. },
  2515. },
  2516. OP: ast.AND,
  2517. LHS: &ast.BinaryExpr{
  2518. OP: ast.GT,
  2519. LHS: &ast.FieldRef{Name: "id1", StreamName: "src1"},
  2520. RHS: &ast.IntegerLiteral{Val: 111},
  2521. },
  2522. },
  2523. },
  2524. },
  2525. },
  2526. }.Init(),
  2527. },
  2528. },
  2529. fields: []ast.Field{
  2530. {
  2531. Expr: &ast.FieldRef{Name: "id1", StreamName: ast.DefaultStream},
  2532. Name: "id1",
  2533. AName: "",
  2534. },
  2535. },
  2536. isAggregate: false,
  2537. sendMeta: false,
  2538. }.Init(),
  2539. }, { // 10 meta
  2540. sql: `SELECT temp, meta(id) AS eid,meta(Humidity->Device) AS hdevice FROM src1 WHERE meta(device)="demo2"`,
  2541. p: ProjectPlan{
  2542. baseLogicalPlan: baseLogicalPlan{
  2543. children: []LogicalPlan{
  2544. FilterPlan{
  2545. baseLogicalPlan: baseLogicalPlan{
  2546. children: []LogicalPlan{
  2547. DataSourcePlan{
  2548. name: "src1",
  2549. streamFields: map[string]*ast.JsonStreamField{
  2550. "temp": nil,
  2551. },
  2552. isSchemaless: true,
  2553. streamStmt: streams["src1"],
  2554. metaFields: []string{"Humidity", "device", "id"},
  2555. }.Init(),
  2556. },
  2557. },
  2558. condition: &ast.BinaryExpr{
  2559. LHS: &ast.Call{
  2560. Name: "meta",
  2561. FuncId: 2,
  2562. Args: []ast.Expr{&ast.MetaRef{
  2563. Name: "device",
  2564. StreamName: ast.DefaultStream,
  2565. }},
  2566. },
  2567. OP: ast.EQ,
  2568. RHS: &ast.StringLiteral{
  2569. Val: "demo2",
  2570. },
  2571. },
  2572. }.Init(),
  2573. },
  2574. },
  2575. fields: []ast.Field{
  2576. {
  2577. Expr: &ast.FieldRef{Name: "temp", StreamName: "src1"},
  2578. Name: "temp",
  2579. AName: "",
  2580. }, {
  2581. Expr: &ast.FieldRef{Name: "eid", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  2582. &ast.Call{Name: "meta", FuncId: 0, Args: []ast.Expr{&ast.MetaRef{
  2583. Name: "id",
  2584. StreamName: ast.DefaultStream,
  2585. }}},
  2586. []ast.StreamName{},
  2587. nil,
  2588. )},
  2589. Name: "meta",
  2590. AName: "eid",
  2591. }, {
  2592. Expr: &ast.FieldRef{Name: "hdevice", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  2593. &ast.Call{Name: "meta", FuncId: 1, Args: []ast.Expr{
  2594. &ast.BinaryExpr{
  2595. OP: ast.ARROW,
  2596. LHS: &ast.MetaRef{Name: "Humidity", StreamName: ast.DefaultStream},
  2597. RHS: &ast.JsonFieldRef{Name: "Device"},
  2598. },
  2599. }},
  2600. []ast.StreamName{},
  2601. nil,
  2602. )},
  2603. Name: "meta",
  2604. AName: "hdevice",
  2605. },
  2606. },
  2607. isAggregate: false,
  2608. sendMeta: false,
  2609. }.Init(),
  2610. }, { // 11 join with same name field and aliased
  2611. sql: `SELECT src2.hum AS hum1, tableInPlanner.hum AS hum2 FROM src2 INNER JOIN tableInPlanner on id2 = id WHERE hum1 > hum2`,
  2612. p: ProjectPlan{
  2613. baseLogicalPlan: baseLogicalPlan{
  2614. children: []LogicalPlan{
  2615. JoinPlan{
  2616. baseLogicalPlan: baseLogicalPlan{
  2617. children: []LogicalPlan{
  2618. JoinAlignPlan{
  2619. baseLogicalPlan: baseLogicalPlan{
  2620. children: []LogicalPlan{
  2621. DataSourcePlan{
  2622. name: "src2",
  2623. streamFields: map[string]*ast.JsonStreamField{
  2624. "hum": nil,
  2625. "id": nil,
  2626. "id2": nil,
  2627. },
  2628. isSchemaless: true,
  2629. streamStmt: streams["src2"],
  2630. metaFields: []string{},
  2631. }.Init(),
  2632. DataSourcePlan{
  2633. name: "tableInPlanner",
  2634. streamFields: map[string]*ast.JsonStreamField{
  2635. "hum": {
  2636. Type: "bigint",
  2637. },
  2638. "id": {
  2639. Type: "bigint",
  2640. },
  2641. },
  2642. streamStmt: streams["tableInPlanner"],
  2643. metaFields: []string{},
  2644. }.Init(),
  2645. },
  2646. },
  2647. Emitters: []string{"tableInPlanner"},
  2648. }.Init(),
  2649. },
  2650. },
  2651. from: &ast.Table{
  2652. Name: "src2",
  2653. },
  2654. joins: []ast.Join{
  2655. {
  2656. Name: "tableInPlanner",
  2657. Alias: "",
  2658. JoinType: ast.INNER_JOIN,
  2659. Expr: &ast.BinaryExpr{
  2660. RHS: &ast.BinaryExpr{
  2661. OP: ast.EQ,
  2662. LHS: &ast.FieldRef{Name: "id2", StreamName: ast.DefaultStream},
  2663. RHS: &ast.FieldRef{Name: "id", StreamName: ast.DefaultStream},
  2664. },
  2665. OP: ast.AND,
  2666. LHS: &ast.BinaryExpr{
  2667. OP: ast.GT,
  2668. LHS: &ast.FieldRef{Name: "hum1", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  2669. &ast.FieldRef{
  2670. Name: "hum",
  2671. StreamName: "src2",
  2672. },
  2673. []ast.StreamName{"src2"},
  2674. &boolFalse,
  2675. )},
  2676. RHS: &ast.FieldRef{Name: "hum2", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  2677. &ast.FieldRef{
  2678. Name: "hum",
  2679. StreamName: "tableInPlanner",
  2680. },
  2681. []ast.StreamName{"tableInPlanner"},
  2682. &boolFalse,
  2683. )},
  2684. },
  2685. },
  2686. },
  2687. },
  2688. }.Init(),
  2689. },
  2690. },
  2691. fields: []ast.Field{
  2692. {
  2693. Expr: &ast.FieldRef{Name: "hum1", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  2694. &ast.FieldRef{
  2695. Name: "hum",
  2696. StreamName: "src2",
  2697. },
  2698. []ast.StreamName{"src2"},
  2699. &boolFalse,
  2700. )},
  2701. Name: "hum",
  2702. AName: "hum1",
  2703. }, {
  2704. Expr: &ast.FieldRef{Name: "hum2", StreamName: ast.AliasStream, AliasRef: ast.MockAliasRef(
  2705. &ast.FieldRef{
  2706. Name: "hum",
  2707. StreamName: "tableInPlanner",
  2708. },
  2709. []ast.StreamName{"tableInPlanner"},
  2710. &boolFalse,
  2711. )},
  2712. Name: "hum",
  2713. AName: "hum2",
  2714. },
  2715. },
  2716. isAggregate: false,
  2717. sendMeta: false,
  2718. }.Init(),
  2719. }, { // 12
  2720. sql: `SELECT name->first, name->last FROM src1`,
  2721. p: ProjectPlan{
  2722. baseLogicalPlan: baseLogicalPlan{
  2723. children: []LogicalPlan{
  2724. DataSourcePlan{
  2725. baseLogicalPlan: baseLogicalPlan{},
  2726. name: "src1",
  2727. streamFields: map[string]*ast.JsonStreamField{
  2728. "name": nil,
  2729. },
  2730. isSchemaless: true,
  2731. streamStmt: streams["src1"],
  2732. metaFields: []string{},
  2733. }.Init(),
  2734. },
  2735. },
  2736. fields: []ast.Field{
  2737. {
  2738. Expr: &ast.BinaryExpr{
  2739. OP: ast.ARROW,
  2740. LHS: &ast.FieldRef{StreamName: "src1", Name: "name"},
  2741. RHS: &ast.JsonFieldRef{Name: "first"},
  2742. },
  2743. Name: "kuiper_field_0",
  2744. AName: "",
  2745. }, {
  2746. Expr: &ast.BinaryExpr{
  2747. OP: ast.ARROW,
  2748. LHS: &ast.FieldRef{StreamName: "src1", Name: "name"},
  2749. RHS: &ast.JsonFieldRef{Name: "last"},
  2750. },
  2751. Name: "kuiper_field_1",
  2752. AName: "",
  2753. },
  2754. },
  2755. isAggregate: false,
  2756. sendMeta: false,
  2757. }.Init(),
  2758. },
  2759. }
  2760. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  2761. for i, tt := range tests {
  2762. stmt, err := xsql.NewParser(strings.NewReader(tt.sql)).Parse()
  2763. if err != nil {
  2764. t.Errorf("%d. %q: error compile sql: %s\n", i, tt.sql, err)
  2765. continue
  2766. }
  2767. p, err := createLogicalPlan(stmt, &api.RuleOption{
  2768. IsEventTime: false,
  2769. LateTol: 0,
  2770. Concurrency: 0,
  2771. BufferLength: 0,
  2772. SendMetaToSink: false,
  2773. Qos: 0,
  2774. CheckpointInterval: 0,
  2775. SendError: true,
  2776. }, store)
  2777. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  2778. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.sql, tt.err, err)
  2779. } else if !reflect.DeepEqual(tt.p, p) {
  2780. 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))
  2781. }
  2782. }
  2783. }
  2784. func Test_createLogicalPlan4Lookup(t *testing.T) {
  2785. store, err := store.GetKV("stream")
  2786. if err != nil {
  2787. t.Error(err)
  2788. return
  2789. }
  2790. streamSqls := map[string]string{
  2791. "src1": `CREATE STREAM src1 () WITH (DATASOURCE="src1", FORMAT="json", KEY="ts");`,
  2792. "table1": `CREATE TABLE table1 () WITH (DATASOURCE="table1",TYPE="sql", KIND="lookup");`,
  2793. "table2": `CREATE TABLE table2 () WITH (DATASOURCE="table2",TYPE="sql", KIND="lookup");`,
  2794. }
  2795. types := map[string]ast.StreamType{
  2796. "src1": ast.TypeStream,
  2797. "table1": ast.TypeTable,
  2798. "table2": ast.TypeTable,
  2799. }
  2800. for name, sql := range streamSqls {
  2801. s, err := json.Marshal(&xsql.StreamInfo{
  2802. StreamType: types[name],
  2803. Statement: sql,
  2804. })
  2805. if err != nil {
  2806. t.Error(err)
  2807. t.Fail()
  2808. }
  2809. err = store.Set(name, string(s))
  2810. if err != nil {
  2811. t.Error(err)
  2812. t.Fail()
  2813. }
  2814. }
  2815. streams := make(map[string]*ast.StreamStmt)
  2816. for n := range streamSqls {
  2817. streamStmt, err := xsql.GetDataSource(store, n)
  2818. if err != nil {
  2819. t.Errorf("fail to get stream %s, please check if stream is created", n)
  2820. return
  2821. }
  2822. streams[n] = streamStmt
  2823. }
  2824. tests := []struct {
  2825. sql string
  2826. p LogicalPlan
  2827. err string
  2828. }{
  2829. { // 0
  2830. sql: `SELECT src1.a, table1.b FROM src1 INNER JOIN table1 ON src1.id = table1.id`,
  2831. p: ProjectPlan{
  2832. baseLogicalPlan: baseLogicalPlan{
  2833. children: []LogicalPlan{
  2834. LookupPlan{
  2835. baseLogicalPlan: baseLogicalPlan{
  2836. children: []LogicalPlan{
  2837. DataSourcePlan{
  2838. baseLogicalPlan: baseLogicalPlan{},
  2839. name: "src1",
  2840. streamFields: map[string]*ast.JsonStreamField{
  2841. "a": nil,
  2842. },
  2843. isSchemaless: true,
  2844. streamStmt: streams["src1"],
  2845. metaFields: []string{},
  2846. }.Init(),
  2847. },
  2848. },
  2849. joinExpr: ast.Join{
  2850. Name: "table1",
  2851. Alias: "",
  2852. JoinType: ast.INNER_JOIN,
  2853. Expr: &ast.BinaryExpr{
  2854. OP: ast.EQ,
  2855. LHS: &ast.FieldRef{
  2856. StreamName: "src1",
  2857. Name: "id",
  2858. },
  2859. RHS: &ast.FieldRef{
  2860. StreamName: "table1",
  2861. Name: "id",
  2862. },
  2863. },
  2864. },
  2865. keys: []string{"id"},
  2866. fields: []string{"b"},
  2867. valvars: []ast.Expr{
  2868. &ast.FieldRef{
  2869. StreamName: "src1",
  2870. Name: "id",
  2871. },
  2872. },
  2873. options: &ast.Options{
  2874. DATASOURCE: "table1",
  2875. TYPE: "sql",
  2876. KIND: "lookup",
  2877. },
  2878. conditions: nil,
  2879. }.Init(),
  2880. },
  2881. },
  2882. fields: []ast.Field{
  2883. {
  2884. Expr: &ast.FieldRef{
  2885. StreamName: "src1",
  2886. Name: "a",
  2887. },
  2888. Name: "a",
  2889. AName: "",
  2890. },
  2891. {
  2892. Expr: &ast.FieldRef{
  2893. StreamName: "table1",
  2894. Name: "b",
  2895. },
  2896. Name: "b",
  2897. AName: "",
  2898. },
  2899. },
  2900. isAggregate: false,
  2901. sendMeta: false,
  2902. }.Init(),
  2903. },
  2904. { // 1
  2905. sql: `SELECT src1.a, table1.* FROM src1 INNER JOIN table1 ON table1.b > 20 AND src1.c < 40 AND src1.id = table1.id`,
  2906. p: ProjectPlan{
  2907. baseLogicalPlan: baseLogicalPlan{
  2908. children: []LogicalPlan{
  2909. FilterPlan{
  2910. baseLogicalPlan: baseLogicalPlan{
  2911. children: []LogicalPlan{
  2912. LookupPlan{
  2913. baseLogicalPlan: baseLogicalPlan{
  2914. children: []LogicalPlan{
  2915. FilterPlan{
  2916. baseLogicalPlan: baseLogicalPlan{
  2917. children: []LogicalPlan{
  2918. DataSourcePlan{
  2919. baseLogicalPlan: baseLogicalPlan{},
  2920. name: "src1",
  2921. streamFields: map[string]*ast.JsonStreamField{
  2922. "a": nil,
  2923. },
  2924. isSchemaless: true,
  2925. streamStmt: streams["src1"],
  2926. metaFields: []string{},
  2927. }.Init(),
  2928. },
  2929. },
  2930. condition: &ast.BinaryExpr{
  2931. OP: ast.LT,
  2932. LHS: &ast.FieldRef{
  2933. StreamName: "src1",
  2934. Name: "c",
  2935. },
  2936. RHS: &ast.IntegerLiteral{Val: 40},
  2937. },
  2938. }.Init(),
  2939. },
  2940. },
  2941. joinExpr: ast.Join{
  2942. Name: "table1",
  2943. Alias: "",
  2944. JoinType: ast.INNER_JOIN,
  2945. Expr: &ast.BinaryExpr{
  2946. OP: ast.AND,
  2947. RHS: &ast.BinaryExpr{
  2948. OP: ast.EQ,
  2949. LHS: &ast.FieldRef{
  2950. StreamName: "src1",
  2951. Name: "id",
  2952. },
  2953. RHS: &ast.FieldRef{
  2954. StreamName: "table1",
  2955. Name: "id",
  2956. },
  2957. },
  2958. LHS: &ast.BinaryExpr{
  2959. OP: ast.AND,
  2960. LHS: &ast.BinaryExpr{
  2961. OP: ast.GT,
  2962. LHS: &ast.FieldRef{
  2963. StreamName: "table1",
  2964. Name: "b",
  2965. },
  2966. RHS: &ast.IntegerLiteral{Val: 20},
  2967. },
  2968. RHS: &ast.BinaryExpr{
  2969. OP: ast.LT,
  2970. LHS: &ast.FieldRef{
  2971. StreamName: "src1",
  2972. Name: "c",
  2973. },
  2974. RHS: &ast.IntegerLiteral{Val: 40},
  2975. },
  2976. },
  2977. },
  2978. },
  2979. keys: []string{"id"},
  2980. valvars: []ast.Expr{
  2981. &ast.FieldRef{
  2982. StreamName: "src1",
  2983. Name: "id",
  2984. },
  2985. },
  2986. options: &ast.Options{
  2987. DATASOURCE: "table1",
  2988. TYPE: "sql",
  2989. KIND: "lookup",
  2990. },
  2991. conditions: &ast.BinaryExpr{
  2992. OP: ast.AND,
  2993. LHS: &ast.BinaryExpr{
  2994. OP: ast.GT,
  2995. LHS: &ast.FieldRef{
  2996. StreamName: "table1",
  2997. Name: "b",
  2998. },
  2999. RHS: &ast.IntegerLiteral{Val: 20},
  3000. },
  3001. RHS: &ast.BinaryExpr{
  3002. OP: ast.LT,
  3003. LHS: &ast.FieldRef{
  3004. StreamName: "src1",
  3005. Name: "c",
  3006. },
  3007. RHS: &ast.IntegerLiteral{Val: 40},
  3008. },
  3009. },
  3010. }.Init(),
  3011. },
  3012. },
  3013. condition: &ast.BinaryExpr{
  3014. OP: ast.GT,
  3015. LHS: &ast.FieldRef{
  3016. StreamName: "table1",
  3017. Name: "b",
  3018. },
  3019. RHS: &ast.IntegerLiteral{Val: 20},
  3020. },
  3021. }.Init(),
  3022. },
  3023. },
  3024. fields: []ast.Field{
  3025. {
  3026. Expr: &ast.FieldRef{
  3027. StreamName: "src1",
  3028. Name: "a",
  3029. },
  3030. Name: "a",
  3031. AName: "",
  3032. },
  3033. {
  3034. Expr: &ast.FieldRef{
  3035. StreamName: "table1",
  3036. Name: "*",
  3037. },
  3038. Name: "*",
  3039. AName: "",
  3040. },
  3041. },
  3042. isAggregate: false,
  3043. sendMeta: false,
  3044. }.Init(),
  3045. },
  3046. { // 2
  3047. sql: `SELECT src1.a, table1.b, table2.c FROM src1 INNER JOIN table1 ON src1.id = table1.id INNER JOIN table2 on table1.id = table2.id`,
  3048. p: ProjectPlan{
  3049. baseLogicalPlan: baseLogicalPlan{
  3050. children: []LogicalPlan{
  3051. LookupPlan{
  3052. baseLogicalPlan: baseLogicalPlan{
  3053. children: []LogicalPlan{
  3054. LookupPlan{
  3055. baseLogicalPlan: baseLogicalPlan{
  3056. children: []LogicalPlan{
  3057. DataSourcePlan{
  3058. baseLogicalPlan: baseLogicalPlan{},
  3059. name: "src1",
  3060. streamFields: map[string]*ast.JsonStreamField{
  3061. "a": nil,
  3062. },
  3063. isSchemaless: true,
  3064. streamStmt: streams["src1"],
  3065. metaFields: []string{},
  3066. }.Init(),
  3067. },
  3068. },
  3069. joinExpr: ast.Join{
  3070. Name: "table1",
  3071. Alias: "",
  3072. JoinType: ast.INNER_JOIN,
  3073. Expr: &ast.BinaryExpr{
  3074. OP: ast.EQ,
  3075. LHS: &ast.FieldRef{
  3076. StreamName: "src1",
  3077. Name: "id",
  3078. },
  3079. RHS: &ast.FieldRef{
  3080. StreamName: "table1",
  3081. Name: "id",
  3082. },
  3083. },
  3084. },
  3085. keys: []string{"id"},
  3086. fields: []string{"b"},
  3087. valvars: []ast.Expr{
  3088. &ast.FieldRef{
  3089. StreamName: "src1",
  3090. Name: "id",
  3091. },
  3092. },
  3093. options: &ast.Options{
  3094. DATASOURCE: "table1",
  3095. TYPE: "sql",
  3096. KIND: "lookup",
  3097. },
  3098. conditions: nil,
  3099. }.Init(),
  3100. },
  3101. },
  3102. joinExpr: ast.Join{
  3103. Name: "table2",
  3104. Alias: "",
  3105. JoinType: ast.INNER_JOIN,
  3106. Expr: &ast.BinaryExpr{
  3107. OP: ast.EQ,
  3108. LHS: &ast.FieldRef{
  3109. StreamName: "table1",
  3110. Name: "id",
  3111. },
  3112. RHS: &ast.FieldRef{
  3113. StreamName: "table2",
  3114. Name: "id",
  3115. },
  3116. },
  3117. },
  3118. keys: []string{"id"},
  3119. fields: []string{"c"},
  3120. valvars: []ast.Expr{
  3121. &ast.FieldRef{
  3122. StreamName: "table1",
  3123. Name: "id",
  3124. },
  3125. },
  3126. options: &ast.Options{
  3127. DATASOURCE: "table2",
  3128. TYPE: "sql",
  3129. KIND: "lookup",
  3130. },
  3131. }.Init(),
  3132. },
  3133. },
  3134. fields: []ast.Field{
  3135. {
  3136. Expr: &ast.FieldRef{
  3137. StreamName: "src1",
  3138. Name: "a",
  3139. },
  3140. Name: "a",
  3141. AName: "",
  3142. },
  3143. {
  3144. Expr: &ast.FieldRef{
  3145. StreamName: "table1",
  3146. Name: "b",
  3147. },
  3148. Name: "b",
  3149. AName: "",
  3150. },
  3151. {
  3152. Expr: &ast.FieldRef{
  3153. StreamName: "table2",
  3154. Name: "c",
  3155. },
  3156. Name: "c",
  3157. AName: "",
  3158. },
  3159. },
  3160. isAggregate: false,
  3161. sendMeta: false,
  3162. }.Init(),
  3163. },
  3164. { // 3
  3165. sql: `SELECT * FROM src1 INNER JOIN table1 ON src1.id = table1.id GROUP BY TUMBLINGWINDOW(ss, 10)`,
  3166. p: ProjectPlan{
  3167. baseLogicalPlan: baseLogicalPlan{
  3168. children: []LogicalPlan{
  3169. LookupPlan{
  3170. baseLogicalPlan: baseLogicalPlan{
  3171. children: []LogicalPlan{
  3172. WindowPlan{
  3173. baseLogicalPlan: baseLogicalPlan{
  3174. children: []LogicalPlan{
  3175. DataSourcePlan{
  3176. baseLogicalPlan: baseLogicalPlan{},
  3177. name: "src1",
  3178. streamStmt: streams["src1"],
  3179. streamFields: map[string]*ast.JsonStreamField{},
  3180. metaFields: []string{},
  3181. isWildCard: true,
  3182. isSchemaless: true,
  3183. }.Init(),
  3184. },
  3185. },
  3186. condition: nil,
  3187. wtype: ast.TUMBLING_WINDOW,
  3188. length: 10000,
  3189. interval: 0,
  3190. limit: 0,
  3191. }.Init(),
  3192. },
  3193. },
  3194. joinExpr: ast.Join{
  3195. Name: "table1",
  3196. Alias: "",
  3197. JoinType: ast.INNER_JOIN,
  3198. Expr: &ast.BinaryExpr{
  3199. OP: ast.EQ,
  3200. LHS: &ast.FieldRef{
  3201. StreamName: "src1",
  3202. Name: "id",
  3203. },
  3204. RHS: &ast.FieldRef{
  3205. StreamName: "table1",
  3206. Name: "id",
  3207. },
  3208. },
  3209. },
  3210. keys: []string{"id"},
  3211. valvars: []ast.Expr{
  3212. &ast.FieldRef{
  3213. StreamName: "src1",
  3214. Name: "id",
  3215. },
  3216. },
  3217. options: &ast.Options{
  3218. DATASOURCE: "table1",
  3219. TYPE: "sql",
  3220. KIND: "lookup",
  3221. },
  3222. conditions: nil,
  3223. }.Init(),
  3224. },
  3225. },
  3226. fields: []ast.Field{
  3227. {
  3228. Expr: &ast.Wildcard{Token: ast.ASTERISK},
  3229. Name: "*",
  3230. AName: "",
  3231. },
  3232. },
  3233. isAggregate: false,
  3234. sendMeta: false,
  3235. }.Init(),
  3236. },
  3237. }
  3238. for i, tt := range tests {
  3239. stmt, err := xsql.NewParser(strings.NewReader(tt.sql)).Parse()
  3240. if err != nil {
  3241. t.Errorf("%d. %q: error compile sql: %s\n", i, tt.sql, err)
  3242. continue
  3243. }
  3244. p, err := createLogicalPlan(stmt, &api.RuleOption{
  3245. IsEventTime: false,
  3246. LateTol: 0,
  3247. Concurrency: 0,
  3248. BufferLength: 0,
  3249. SendMetaToSink: false,
  3250. Qos: 0,
  3251. CheckpointInterval: 0,
  3252. SendError: true,
  3253. }, store)
  3254. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  3255. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.sql, tt.err, err)
  3256. } else if !reflect.DeepEqual(tt.p, p) {
  3257. 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))
  3258. }
  3259. }
  3260. }
  3261. func TestTransformSourceNode(t *testing.T) {
  3262. schema := map[string]*ast.JsonStreamField{
  3263. "a": {
  3264. Type: "bigint",
  3265. },
  3266. }
  3267. testCases := []struct {
  3268. name string
  3269. plan *DataSourcePlan
  3270. node *node.SourceNode
  3271. }{
  3272. {
  3273. name: "normal source node",
  3274. plan: &DataSourcePlan{
  3275. name: "test",
  3276. streamStmt: &ast.StreamStmt{
  3277. StreamType: ast.TypeStream,
  3278. Options: &ast.Options{
  3279. TYPE: "file",
  3280. },
  3281. },
  3282. streamFields: nil,
  3283. allMeta: false,
  3284. metaFields: []string{},
  3285. iet: false,
  3286. isBinary: false,
  3287. },
  3288. node: node.NewSourceNode("test", ast.TypeStream, nil, &ast.Options{
  3289. TYPE: "file",
  3290. }, false, nil),
  3291. },
  3292. {
  3293. name: "schema source node",
  3294. plan: &DataSourcePlan{
  3295. name: "test",
  3296. streamStmt: &ast.StreamStmt{
  3297. StreamType: ast.TypeStream,
  3298. Options: &ast.Options{
  3299. TYPE: "file",
  3300. },
  3301. },
  3302. streamFields: schema,
  3303. allMeta: false,
  3304. metaFields: []string{},
  3305. iet: false,
  3306. isBinary: false,
  3307. },
  3308. node: node.NewSourceNode("test", ast.TypeStream, nil, &ast.Options{
  3309. TYPE: "file",
  3310. }, false, schema),
  3311. },
  3312. }
  3313. for _, tc := range testCases {
  3314. t.Run(tc.name, func(t *testing.T) {
  3315. node, err := transformSourceNode(tc.plan, nil, &api.RuleOption{})
  3316. if err != nil {
  3317. t.Errorf("unexpected error: %v", err)
  3318. return
  3319. }
  3320. if !reflect.DeepEqual(node, tc.node) {
  3321. t.Errorf("unexpected result: got %v, want %v", node, tc.node)
  3322. }
  3323. })
  3324. }
  3325. }