analyzer.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  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. "fmt"
  17. "sort"
  18. "strings"
  19. "github.com/lf-edge/ekuiper/internal/binder/function"
  20. "github.com/lf-edge/ekuiper/internal/schema"
  21. "github.com/lf-edge/ekuiper/internal/xsql"
  22. "github.com/lf-edge/ekuiper/pkg/ast"
  23. "github.com/lf-edge/ekuiper/pkg/kv"
  24. )
  25. type streamInfo struct {
  26. stmt *ast.StreamStmt
  27. schema ast.StreamFields
  28. }
  29. // Analyze the select statement by decorating the info from stream statement.
  30. // Typically, set the correct stream name for fieldRefs
  31. func decorateStmt(s *ast.SelectStatement, store kv.KeyValue) ([]*streamInfo, []*ast.Call, []*ast.Call, error) {
  32. streamsFromStmt := xsql.GetStreams(s)
  33. streamStmts := make([]*streamInfo, len(streamsFromStmt))
  34. isSchemaless := false
  35. for i, s := range streamsFromStmt {
  36. streamStmt, err := xsql.GetDataSource(store, s)
  37. if err != nil {
  38. return nil, nil, nil, fmt.Errorf("fail to get stream %s, please check if stream is created", s)
  39. }
  40. si, err := convertStreamInfo(streamStmt)
  41. if err != nil {
  42. return nil, nil, nil, err
  43. }
  44. streamStmts[i] = si
  45. if si.schema == nil {
  46. isSchemaless = true
  47. }
  48. }
  49. if checkAliasReferenceCycle(s) {
  50. return nil, nil, nil, fmt.Errorf("select fields have cycled alias")
  51. }
  52. if !isSchemaless {
  53. aliasFieldTopoSort(s, streamStmts)
  54. }
  55. dsn := ast.DefaultStream
  56. if len(streamsFromStmt) == 1 {
  57. dsn = streamStmts[0].stmt.Name
  58. }
  59. // [fieldName][streamsName][*aliasRef] if alias, with special key alias/default. Each key has exactly one value
  60. fieldsMap := newFieldsMap(isSchemaless, dsn)
  61. if !isSchemaless {
  62. for _, streamStmt := range streamStmts {
  63. for _, field := range streamStmt.schema {
  64. fieldsMap.reserve(field.Name, streamStmt.stmt.Name)
  65. }
  66. }
  67. }
  68. var (
  69. walkErr error
  70. aliasFields []*ast.Field
  71. analyticFieldFuncs []*ast.Call
  72. analyticFuncs []*ast.Call
  73. )
  74. // Scan columns fields: bind all field refs, collect alias
  75. for i, f := range s.Fields {
  76. ast.WalkFunc(f.Expr, func(n ast.Node) bool {
  77. switch f := n.(type) {
  78. case *ast.FieldRef:
  79. walkErr = fieldsMap.bind(f)
  80. }
  81. return true
  82. })
  83. if walkErr != nil {
  84. return nil, nil, nil, walkErr
  85. }
  86. if f.AName != "" {
  87. aliasFields = append(aliasFields, &s.Fields[i])
  88. fieldsMap.bindAlias(f.AName)
  89. }
  90. }
  91. // bind alias field expressions
  92. for _, f := range aliasFields {
  93. ar, err := ast.NewAliasRef(f.Expr)
  94. if err != nil {
  95. walkErr = err
  96. } else {
  97. f.Expr = &ast.FieldRef{
  98. StreamName: ast.AliasStream,
  99. Name: f.AName,
  100. AliasRef: ar,
  101. }
  102. walkErr = fieldsMap.save(f.AName, ast.AliasStream, ar)
  103. for _, subF := range s.Fields {
  104. if f.AName == subF.AName {
  105. continue
  106. }
  107. ast.WalkFunc(&subF, func(node ast.Node) bool {
  108. switch fr := node.(type) {
  109. case *ast.FieldRef:
  110. if fr.Name == f.AName {
  111. fr.StreamName = ast.AliasStream
  112. fr.AliasRef = ar
  113. }
  114. return false
  115. }
  116. return true
  117. })
  118. }
  119. }
  120. }
  121. // Bind field ref for alias AND set StreamName for all field ref
  122. ast.WalkFunc(s, func(n ast.Node) bool {
  123. switch f := n.(type) {
  124. case ast.Fields: // do not bind selection fields, should have done above
  125. return false
  126. case *ast.FieldRef:
  127. if f.StreamName != "" && f.StreamName != ast.DefaultStream {
  128. // check if stream exists
  129. found := false
  130. for _, sn := range streamsFromStmt {
  131. if sn == string(f.StreamName) {
  132. found = true
  133. break
  134. }
  135. }
  136. if !found {
  137. walkErr = fmt.Errorf("stream %s not found", f.StreamName)
  138. return true
  139. }
  140. }
  141. walkErr = fieldsMap.bind(f)
  142. }
  143. return true
  144. })
  145. if walkErr != nil {
  146. return nil, nil, nil, walkErr
  147. }
  148. walkErr = validate(s)
  149. // Collect all analytic function calls so that we can let them run firstly
  150. ast.WalkFunc(s, func(n ast.Node) bool {
  151. switch f := n.(type) {
  152. case ast.Fields:
  153. return false
  154. case *ast.Call:
  155. if function.IsAnalyticFunc(f.Name) {
  156. f.CachedField = fmt.Sprintf("%s_%s_%d", function.AnalyticPrefix, f.Name, f.FuncId)
  157. f.Cached = true
  158. analyticFuncs = append(analyticFuncs, &ast.Call{
  159. Name: f.Name,
  160. FuncId: f.FuncId,
  161. FuncType: f.FuncType,
  162. Args: f.Args,
  163. CachedField: f.CachedField,
  164. Partition: f.Partition,
  165. WhenExpr: f.WhenExpr,
  166. })
  167. }
  168. }
  169. return true
  170. })
  171. if walkErr != nil {
  172. return nil, nil, nil, walkErr
  173. }
  174. // walk sources at last to let them run firstly
  175. // because another clause may depend on the alias defined here
  176. for _, field := range s.Fields {
  177. var calls []*ast.Call
  178. ast.WalkFunc(&field, func(n ast.Node) bool {
  179. switch f := n.(type) {
  180. case *ast.Call:
  181. if function.IsAnalyticFunc(f.Name) {
  182. f.CachedField = fmt.Sprintf("%s_%s_%d", function.AnalyticPrefix, f.Name, f.FuncId)
  183. f.Cached = true
  184. calls = append([]*ast.Call{
  185. {
  186. Name: f.Name,
  187. FuncId: f.FuncId,
  188. FuncType: f.FuncType,
  189. Args: f.Args,
  190. CachedField: f.CachedField,
  191. Partition: f.Partition,
  192. WhenExpr: f.WhenExpr,
  193. },
  194. }, calls...)
  195. }
  196. }
  197. return true
  198. })
  199. analyticFieldFuncs = append(analyticFieldFuncs, calls...)
  200. }
  201. if walkErr != nil {
  202. return nil, nil, nil, walkErr
  203. }
  204. return streamStmts, analyticFuncs, analyticFieldFuncs, walkErr
  205. }
  206. type aliasTopoDegree struct {
  207. alias string
  208. degree int
  209. field ast.Field
  210. }
  211. type aliasTopoDegrees []*aliasTopoDegree
  212. func (a aliasTopoDegrees) Len() int {
  213. return len(a)
  214. }
  215. func (a aliasTopoDegrees) Less(i, j int) bool {
  216. if a[i].degree == a[j].degree {
  217. return a[i].alias < a[j].alias
  218. }
  219. return a[i].degree < a[j].degree
  220. }
  221. func (a aliasTopoDegrees) Swap(i, j int) {
  222. a[i], a[j] = a[j], a[i]
  223. }
  224. // checkAliasReferenceCycle checks whether exists select a + 1 as b, b + 1 as a from demo;
  225. func checkAliasReferenceCycle(s *ast.SelectStatement) bool {
  226. aliasRef := make(map[string]map[string]struct{})
  227. for _, field := range s.Fields {
  228. if len(field.AName) > 0 {
  229. aliasRef[field.AName] = make(map[string]struct{})
  230. }
  231. }
  232. if len(aliasRef) < 1 {
  233. return false
  234. }
  235. hasCycleAlias := false
  236. for _, field := range s.Fields {
  237. if len(field.AName) > 0 {
  238. ast.WalkFunc(&field, func(node ast.Node) bool {
  239. switch f := node.(type) {
  240. case *ast.FieldRef:
  241. if len(f.Name) > 0 {
  242. if f.Name == field.AName {
  243. return true
  244. }
  245. _, ok := aliasRef[f.Name]
  246. if ok {
  247. aliasRef[field.AName][f.Name] = struct{}{}
  248. if dfsRef(aliasRef, map[string]struct{}{}, f.Name, field.AName) {
  249. hasCycleAlias = true
  250. return false
  251. }
  252. }
  253. }
  254. }
  255. return true
  256. })
  257. if hasCycleAlias {
  258. return true
  259. }
  260. }
  261. }
  262. return false
  263. }
  264. func dfsRef(aliasRef map[string]map[string]struct{}, walked map[string]struct{}, currentName, targetName string) bool {
  265. defer func() {
  266. walked[currentName] = struct{}{}
  267. }()
  268. for refName := range aliasRef[currentName] {
  269. if refName == targetName {
  270. return true
  271. }
  272. }
  273. for name := range aliasRef[currentName] {
  274. _, ok := walked[name]
  275. if ok {
  276. continue
  277. }
  278. if dfsRef(aliasRef, walked, name, targetName) {
  279. return true
  280. }
  281. }
  282. return false
  283. }
  284. func aliasFieldTopoSort(s *ast.SelectStatement, streamStmts []*streamInfo) {
  285. nonAliasFields := make([]ast.Field, 0)
  286. aliasDegreeMap := make(map[string]*aliasTopoDegree)
  287. for _, field := range s.Fields {
  288. if field.AName != "" {
  289. aliasDegreeMap[field.AName] = &aliasTopoDegree{
  290. alias: field.AName,
  291. degree: -1,
  292. field: field,
  293. }
  294. } else {
  295. nonAliasFields = append(nonAliasFields, field)
  296. }
  297. }
  298. for !isAliasFieldTopoSortFinish(aliasDegreeMap) {
  299. for _, field := range s.Fields {
  300. if field.AName != "" && aliasDegreeMap[field.AName].degree < 0 {
  301. skip := false
  302. degree := 0
  303. ast.WalkFunc(field.Expr, func(node ast.Node) bool {
  304. switch f := node.(type) {
  305. case *ast.FieldRef:
  306. if fDegree, ok := aliasDegreeMap[f.Name]; ok && fDegree.degree >= 0 {
  307. if degree < fDegree.degree+1 {
  308. degree = fDegree.degree + 1
  309. }
  310. return true
  311. }
  312. if !isFieldRefNameExists(f.Name, streamStmts) {
  313. skip = true
  314. return false
  315. }
  316. }
  317. return true
  318. })
  319. if !skip {
  320. aliasDegreeMap[field.AName].degree = degree
  321. }
  322. }
  323. }
  324. }
  325. as := make(aliasTopoDegrees, 0)
  326. for _, degree := range aliasDegreeMap {
  327. as = append(as, degree)
  328. }
  329. sort.Sort(as)
  330. s.Fields = make([]ast.Field, 0)
  331. for _, d := range as {
  332. s.Fields = append(s.Fields, d.field)
  333. }
  334. s.Fields = append(s.Fields, nonAliasFields...)
  335. }
  336. func isFieldRefNameExists(name string, streamStmts []*streamInfo) bool {
  337. for _, streamStmt := range streamStmts {
  338. for _, col := range streamStmt.schema {
  339. if col.Name == name {
  340. return true
  341. }
  342. }
  343. }
  344. return false
  345. }
  346. func isAliasFieldTopoSortFinish(aliasDegrees map[string]*aliasTopoDegree) bool {
  347. for _, aliasDegree := range aliasDegrees {
  348. if aliasDegree.degree < 0 {
  349. return false
  350. }
  351. }
  352. return true
  353. }
  354. func validate(s *ast.SelectStatement) (err error) {
  355. isAggStmt := false
  356. if xsql.IsAggregate(s.Condition) {
  357. return fmt.Errorf("Not allowed to call aggregate functions in WHERE clause.")
  358. }
  359. if !allAggregate(s.Having) {
  360. return fmt.Errorf("Not allowed to call non-aggregate functions in HAVING clause.")
  361. }
  362. for _, d := range s.Dimensions {
  363. isAggStmt = true
  364. if xsql.IsAggregate(d.Expr) {
  365. return fmt.Errorf("Not allowed to call aggregate functions in GROUP BY clause.")
  366. }
  367. }
  368. if s.Joins != nil {
  369. isAggStmt = true
  370. }
  371. ast.WalkFunc(s, func(n ast.Node) bool {
  372. switch f := n.(type) {
  373. case *ast.Call:
  374. // aggregate call should not have any aggregate arg
  375. if function.IsAggFunc(f.Name) {
  376. for _, arg := range f.Args {
  377. tr := xsql.IsAggregate(arg)
  378. if tr {
  379. err = fmt.Errorf("invalid argument for func %s: aggregate argument is not allowed", f.Name)
  380. return false
  381. }
  382. }
  383. }
  384. if isAggStmt && function.NoAggFunc(f.Name) {
  385. err = fmt.Errorf("function %s is not allowed in an aggregate query", f.Name)
  386. return false
  387. }
  388. case *ast.Window:
  389. // agg func check is done in dimensions.
  390. // in window trigger condition, NoAggFunc is allowed unlike normal condition so return false to skip that check
  391. return false
  392. }
  393. return true
  394. })
  395. return
  396. }
  397. // file-private functions below
  398. // allAggregate checks if all expressions of binary expression are aggregate
  399. func allAggregate(expr ast.Expr) (r bool) {
  400. r = true
  401. ast.WalkFunc(expr, func(n ast.Node) bool {
  402. switch f := expr.(type) {
  403. case *ast.BinaryExpr:
  404. switch f.OP {
  405. case ast.SUBSET, ast.ARROW:
  406. // do nothing
  407. default:
  408. r = allAggregate(f.LHS) && allAggregate(f.RHS)
  409. return false
  410. }
  411. case *ast.Call, *ast.FieldRef:
  412. if !xsql.IsAggregate(f) {
  413. r = false
  414. return false
  415. }
  416. }
  417. return true
  418. })
  419. return
  420. }
  421. func convertStreamInfo(streamStmt *ast.StreamStmt) (*streamInfo, error) {
  422. ss := streamStmt.StreamFields
  423. var err error
  424. if streamStmt.Options.SCHEMAID != "" {
  425. ss, err = schema.InferFromSchemaFile(streamStmt.Options.FORMAT, streamStmt.Options.SCHEMAID)
  426. if err != nil {
  427. return nil, err
  428. }
  429. }
  430. return &streamInfo{
  431. stmt: streamStmt,
  432. schema: ss,
  433. }, nil
  434. }
  435. type fieldsMap struct {
  436. content map[string]streamFieldStore
  437. aliasNames map[string]struct{}
  438. isSchemaless bool
  439. defaultStream ast.StreamName
  440. }
  441. func newFieldsMap(isSchemaless bool, defaultStream ast.StreamName) *fieldsMap {
  442. return &fieldsMap{content: make(map[string]streamFieldStore), aliasNames: map[string]struct{}{}, isSchemaless: isSchemaless, defaultStream: defaultStream}
  443. }
  444. func (f *fieldsMap) reserve(fieldName string, streamName ast.StreamName) {
  445. lname := strings.ToLower(fieldName)
  446. if fm, ok := f.content[lname]; ok {
  447. fm.add(streamName)
  448. } else {
  449. fm := newStreamFieldStore(f.isSchemaless, f.defaultStream)
  450. fm.add(streamName)
  451. f.content[lname] = fm
  452. }
  453. }
  454. func (f *fieldsMap) save(fieldName string, streamName ast.StreamName, field *ast.AliasRef) error {
  455. lname := strings.ToLower(fieldName)
  456. fm, ok := f.content[lname]
  457. if !ok {
  458. if streamName == ast.AliasStream || f.isSchemaless {
  459. fm = newStreamFieldStore(f.isSchemaless, f.defaultStream)
  460. f.content[lname] = fm
  461. } else {
  462. return fmt.Errorf("unknown field %s", fieldName)
  463. }
  464. }
  465. err := fm.ref(streamName, field)
  466. if err != nil {
  467. return fmt.Errorf("%s%s", err, fieldName)
  468. }
  469. return nil
  470. }
  471. func (f *fieldsMap) bindAlias(aliasName string) {
  472. f.aliasNames[aliasName] = struct{}{}
  473. }
  474. func (f *fieldsMap) bind(fr *ast.FieldRef) error {
  475. lname := strings.ToLower(fr.Name)
  476. fm, ok1 := f.content[lname]
  477. _, ok2 := f.aliasNames[lname]
  478. if !ok1 && !ok2 {
  479. if f.isSchemaless && fr.Name != "" {
  480. fm = newStreamFieldStore(f.isSchemaless, f.defaultStream)
  481. f.content[lname] = fm
  482. } else {
  483. return fmt.Errorf("unknown field %s", fr.Name)
  484. }
  485. }
  486. if fm != nil {
  487. err := fm.bindRef(fr)
  488. if err != nil {
  489. return fmt.Errorf("%s%s", err, fr.Name)
  490. }
  491. }
  492. return nil
  493. }
  494. type streamFieldStore interface {
  495. add(k ast.StreamName)
  496. ref(k ast.StreamName, v *ast.AliasRef) error
  497. bindRef(f *ast.FieldRef) error
  498. }
  499. func newStreamFieldStore(isSchemaless bool, defaultStream ast.StreamName) streamFieldStore {
  500. if !isSchemaless {
  501. return &streamFieldMap{content: make(map[ast.StreamName]*ast.AliasRef)}
  502. } else {
  503. return &streamFieldMapSchemaless{content: make(map[ast.StreamName]*ast.AliasRef), defaultStream: defaultStream}
  504. }
  505. }
  506. type streamFieldMap struct {
  507. content map[ast.StreamName]*ast.AliasRef
  508. }
  509. // add the stream name must not be default.
  510. // This is used when traversing stream schema
  511. func (s *streamFieldMap) add(k ast.StreamName) {
  512. s.content[k] = nil
  513. }
  514. // bind for schema field, all keys must be created before running bind
  515. // can bind alias & col. For alias, the stream name must be empty; For col, the field must be a col
  516. func (s *streamFieldMap) ref(k ast.StreamName, v *ast.AliasRef) error {
  517. if k == ast.AliasStream { // must not exist, save alias ref for alias
  518. _, ok := s.content[k]
  519. if ok {
  520. return fmt.Errorf("duplicate alias ")
  521. }
  522. s.content[k] = v
  523. } else { // the key must exist after the schema travers, do validation
  524. if k == ast.DefaultStream { // In schema mode, default stream won't be a key
  525. l := len(s.content)
  526. if l == 0 {
  527. return fmt.Errorf("unknow field ")
  528. } else if l == 1 {
  529. // valid, do nothing
  530. } else {
  531. return fmt.Errorf("ambiguous field ")
  532. }
  533. } else {
  534. _, ok := s.content[k]
  535. if !ok {
  536. return fmt.Errorf("unknow field %s.", k)
  537. }
  538. }
  539. }
  540. return nil
  541. }
  542. func (s *streamFieldMap) bindRef(fr *ast.FieldRef) error {
  543. l := len(s.content)
  544. if fr.StreamName == "" {
  545. fr.StreamName = ast.DefaultStream
  546. }
  547. k := fr.StreamName
  548. if k == ast.DefaultStream {
  549. switch l {
  550. case 0:
  551. return fmt.Errorf("unknown field ")
  552. case 1: // if alias, return this
  553. for sk, sv := range s.content {
  554. fr.RefSelection(sv)
  555. fr.StreamName = sk
  556. }
  557. return nil
  558. default:
  559. r, ok := s.content[ast.AliasStream] // if alias exists
  560. if ok {
  561. fr.RefSelection(r)
  562. fr.StreamName = ast.AliasStream
  563. return nil
  564. } else {
  565. return fmt.Errorf("ambiguous field ")
  566. }
  567. }
  568. } else {
  569. r, ok := s.content[k]
  570. if ok {
  571. fr.RefSelection(r)
  572. return nil
  573. } else {
  574. return fmt.Errorf("unknown field %s.", k)
  575. }
  576. }
  577. }
  578. type streamFieldMapSchemaless struct {
  579. content map[ast.StreamName]*ast.AliasRef
  580. defaultStream ast.StreamName
  581. }
  582. // add this should not be called for schemaless
  583. func (s *streamFieldMapSchemaless) add(k ast.StreamName) {
  584. s.content[k] = nil
  585. }
  586. // bind for schemaless field, create column if not exist
  587. // can bind alias & col. For alias, the stream name must be empty; For col, the field must be a col
  588. func (s *streamFieldMapSchemaless) ref(k ast.StreamName, v *ast.AliasRef) error {
  589. if k == ast.AliasStream { // must not exist
  590. _, ok := s.content[k]
  591. if ok {
  592. return fmt.Errorf("duplicate alias ")
  593. }
  594. s.content[k] = v
  595. } else { // the key may or may not exist. But always have only one default stream field.
  596. // Replace with stream name if another stream found. The key can be duplicate
  597. l := len(s.content)
  598. if k == ast.DefaultStream { // In schemaless mode, default stream can only exist when length is 1
  599. if l < 1 {
  600. // valid, do nothing
  601. } else {
  602. return fmt.Errorf("ambiguous field ")
  603. }
  604. } else {
  605. if l == 1 {
  606. for sk := range s.content {
  607. if sk == ast.DefaultStream {
  608. delete(s.content, k)
  609. }
  610. }
  611. }
  612. }
  613. }
  614. return nil
  615. }
  616. func (s *streamFieldMapSchemaless) bindRef(fr *ast.FieldRef) error {
  617. l := len(s.content)
  618. if fr.StreamName == "" || fr.StreamName == ast.DefaultStream {
  619. if l == 1 {
  620. for sk := range s.content {
  621. fr.StreamName = sk
  622. }
  623. }
  624. }
  625. k := fr.StreamName
  626. if k == ast.DefaultStream {
  627. switch l {
  628. case 0: // must be a column because alias are fields and have been traversed
  629. // reserve a hole and do nothing
  630. fr.StreamName = s.defaultStream
  631. s.content[s.defaultStream] = nil
  632. return nil
  633. case 1: // if alias or single col, return this
  634. for sk, sv := range s.content {
  635. fr.RefSelection(sv)
  636. fr.StreamName = sk
  637. }
  638. return nil
  639. default:
  640. r, ok := s.content[ast.AliasStream] // if alias exists
  641. if ok {
  642. fr.RefSelection(r)
  643. fr.StreamName = ast.AliasStream
  644. return nil
  645. } else {
  646. fr.StreamName = s.defaultStream
  647. }
  648. }
  649. }
  650. if fr.StreamName != ast.DefaultStream {
  651. r, ok := s.content[k]
  652. if !ok { // reserver a hole
  653. s.content[k] = nil
  654. } else {
  655. fr.RefSelection(r)
  656. }
  657. return nil
  658. }
  659. return fmt.Errorf("ambiguous field ")
  660. }