analyzer.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  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. v, ok1 := aliasRef[f.Name]
  249. if ok1 {
  250. _, ok2 := v[field.AName]
  251. if ok2 {
  252. hasCycleAlias = true
  253. return false
  254. }
  255. }
  256. }
  257. }
  258. }
  259. return true
  260. })
  261. if hasCycleAlias {
  262. return true
  263. }
  264. }
  265. }
  266. return false
  267. }
  268. func aliasFieldTopoSort(s *ast.SelectStatement, streamStmts []*streamInfo) {
  269. nonAliasFields := make([]ast.Field, 0)
  270. aliasDegreeMap := make(map[string]*aliasTopoDegree)
  271. for _, field := range s.Fields {
  272. if field.AName != "" {
  273. aliasDegreeMap[field.AName] = &aliasTopoDegree{
  274. alias: field.AName,
  275. degree: -1,
  276. field: field,
  277. }
  278. } else {
  279. nonAliasFields = append(nonAliasFields, field)
  280. }
  281. }
  282. for !isAliasFieldTopoSortFinish(aliasDegreeMap) {
  283. for _, field := range s.Fields {
  284. if field.AName != "" && aliasDegreeMap[field.AName].degree < 0 {
  285. skip := false
  286. degree := 0
  287. ast.WalkFunc(field.Expr, func(node ast.Node) bool {
  288. switch f := node.(type) {
  289. case *ast.FieldRef:
  290. if fDegree, ok := aliasDegreeMap[f.Name]; ok && fDegree.degree >= 0 {
  291. if degree < fDegree.degree+1 {
  292. degree = fDegree.degree + 1
  293. }
  294. return true
  295. }
  296. if !isFieldRefNameExists(f.Name, streamStmts) {
  297. skip = true
  298. return false
  299. }
  300. }
  301. return true
  302. })
  303. if !skip {
  304. aliasDegreeMap[field.AName].degree = degree
  305. }
  306. }
  307. }
  308. }
  309. as := make(aliasTopoDegrees, 0)
  310. for _, degree := range aliasDegreeMap {
  311. as = append(as, degree)
  312. }
  313. sort.Sort(as)
  314. s.Fields = make([]ast.Field, 0)
  315. for _, d := range as {
  316. s.Fields = append(s.Fields, d.field)
  317. }
  318. s.Fields = append(s.Fields, nonAliasFields...)
  319. }
  320. func isFieldRefNameExists(name string, streamStmts []*streamInfo) bool {
  321. for _, streamStmt := range streamStmts {
  322. for _, col := range streamStmt.schema {
  323. if col.Name == name {
  324. return true
  325. }
  326. }
  327. }
  328. return false
  329. }
  330. func isAliasFieldTopoSortFinish(aliasDegrees map[string]*aliasTopoDegree) bool {
  331. for _, aliasDegree := range aliasDegrees {
  332. if aliasDegree.degree < 0 {
  333. return false
  334. }
  335. }
  336. return true
  337. }
  338. func validate(s *ast.SelectStatement) (err error) {
  339. isAggStmt := false
  340. if xsql.IsAggregate(s.Condition) {
  341. return fmt.Errorf("Not allowed to call aggregate functions in WHERE clause.")
  342. }
  343. if !allAggregate(s.Having) {
  344. return fmt.Errorf("Not allowed to call non-aggregate functions in HAVING clause.")
  345. }
  346. for _, d := range s.Dimensions {
  347. isAggStmt = true
  348. if xsql.IsAggregate(d.Expr) {
  349. return fmt.Errorf("Not allowed to call aggregate functions in GROUP BY clause.")
  350. }
  351. }
  352. if s.Joins != nil {
  353. isAggStmt = true
  354. }
  355. ast.WalkFunc(s, func(n ast.Node) bool {
  356. switch f := n.(type) {
  357. case *ast.Call:
  358. // aggregate call should not have any aggregate arg
  359. if function.IsAggFunc(f.Name) {
  360. for _, arg := range f.Args {
  361. tr := xsql.IsAggregate(arg)
  362. if tr {
  363. err = fmt.Errorf("invalid argument for func %s: aggregate argument is not allowed", f.Name)
  364. return false
  365. }
  366. }
  367. }
  368. if isAggStmt && function.NoAggFunc(f.Name) {
  369. err = fmt.Errorf("function %s is not allowed in an aggregate query", f.Name)
  370. return false
  371. }
  372. case *ast.Window:
  373. // agg func check is done in dimensions.
  374. // in window trigger condition, NoAggFunc is allowed unlike normal condition so return false to skip that check
  375. return false
  376. }
  377. return true
  378. })
  379. return
  380. }
  381. // file-private functions below
  382. // allAggregate checks if all expressions of binary expression are aggregate
  383. func allAggregate(expr ast.Expr) (r bool) {
  384. r = true
  385. ast.WalkFunc(expr, func(n ast.Node) bool {
  386. switch f := expr.(type) {
  387. case *ast.BinaryExpr:
  388. switch f.OP {
  389. case ast.SUBSET, ast.ARROW:
  390. // do nothing
  391. default:
  392. r = allAggregate(f.LHS) && allAggregate(f.RHS)
  393. return false
  394. }
  395. case *ast.Call, *ast.FieldRef:
  396. if !xsql.IsAggregate(f) {
  397. r = false
  398. return false
  399. }
  400. }
  401. return true
  402. })
  403. return
  404. }
  405. func convertStreamInfo(streamStmt *ast.StreamStmt) (*streamInfo, error) {
  406. ss := streamStmt.StreamFields
  407. var err error
  408. if streamStmt.Options.SCHEMAID != "" {
  409. ss, err = schema.InferFromSchemaFile(streamStmt.Options.FORMAT, streamStmt.Options.SCHEMAID)
  410. if err != nil {
  411. return nil, err
  412. }
  413. }
  414. return &streamInfo{
  415. stmt: streamStmt,
  416. schema: ss,
  417. }, nil
  418. }
  419. type fieldsMap struct {
  420. content map[string]streamFieldStore
  421. aliasNames map[string]struct{}
  422. isSchemaless bool
  423. defaultStream ast.StreamName
  424. }
  425. func newFieldsMap(isSchemaless bool, defaultStream ast.StreamName) *fieldsMap {
  426. return &fieldsMap{content: make(map[string]streamFieldStore), aliasNames: map[string]struct{}{}, isSchemaless: isSchemaless, defaultStream: defaultStream}
  427. }
  428. func (f *fieldsMap) reserve(fieldName string, streamName ast.StreamName) {
  429. lname := strings.ToLower(fieldName)
  430. if fm, ok := f.content[lname]; ok {
  431. fm.add(streamName)
  432. } else {
  433. fm := newStreamFieldStore(f.isSchemaless, f.defaultStream)
  434. fm.add(streamName)
  435. f.content[lname] = fm
  436. }
  437. }
  438. func (f *fieldsMap) save(fieldName string, streamName ast.StreamName, field *ast.AliasRef) error {
  439. lname := strings.ToLower(fieldName)
  440. fm, ok := f.content[lname]
  441. if !ok {
  442. if streamName == ast.AliasStream || f.isSchemaless {
  443. fm = newStreamFieldStore(f.isSchemaless, f.defaultStream)
  444. f.content[lname] = fm
  445. } else {
  446. return fmt.Errorf("unknown field %s", fieldName)
  447. }
  448. }
  449. err := fm.ref(streamName, field)
  450. if err != nil {
  451. return fmt.Errorf("%s%s", err, fieldName)
  452. }
  453. return nil
  454. }
  455. func (f *fieldsMap) bindAlias(aliasName string) {
  456. f.aliasNames[aliasName] = struct{}{}
  457. }
  458. func (f *fieldsMap) bind(fr *ast.FieldRef) error {
  459. lname := strings.ToLower(fr.Name)
  460. fm, ok1 := f.content[lname]
  461. _, ok2 := f.aliasNames[lname]
  462. if !ok1 && !ok2 {
  463. if f.isSchemaless && fr.Name != "" {
  464. fm = newStreamFieldStore(f.isSchemaless, f.defaultStream)
  465. f.content[lname] = fm
  466. } else {
  467. return fmt.Errorf("unknown field %s", fr.Name)
  468. }
  469. }
  470. if fm != nil {
  471. err := fm.bindRef(fr)
  472. if err != nil {
  473. return fmt.Errorf("%s%s", err, fr.Name)
  474. }
  475. }
  476. return nil
  477. }
  478. type streamFieldStore interface {
  479. add(k ast.StreamName)
  480. ref(k ast.StreamName, v *ast.AliasRef) error
  481. bindRef(f *ast.FieldRef) error
  482. }
  483. func newStreamFieldStore(isSchemaless bool, defaultStream ast.StreamName) streamFieldStore {
  484. if !isSchemaless {
  485. return &streamFieldMap{content: make(map[ast.StreamName]*ast.AliasRef)}
  486. } else {
  487. return &streamFieldMapSchemaless{content: make(map[ast.StreamName]*ast.AliasRef), defaultStream: defaultStream}
  488. }
  489. }
  490. type streamFieldMap struct {
  491. content map[ast.StreamName]*ast.AliasRef
  492. }
  493. // add the stream name must not be default.
  494. // This is used when traversing stream schema
  495. func (s *streamFieldMap) add(k ast.StreamName) {
  496. s.content[k] = nil
  497. }
  498. // bind for schema field, all keys must be created before running bind
  499. // can bind alias & col. For alias, the stream name must be empty; For col, the field must be a col
  500. func (s *streamFieldMap) ref(k ast.StreamName, v *ast.AliasRef) error {
  501. if k == ast.AliasStream { // must not exist, save alias ref for alias
  502. _, ok := s.content[k]
  503. if ok {
  504. return fmt.Errorf("duplicate alias ")
  505. }
  506. s.content[k] = v
  507. } else { // the key must exist after the schema travers, do validation
  508. if k == ast.DefaultStream { // In schema mode, default stream won't be a key
  509. l := len(s.content)
  510. if l == 0 {
  511. return fmt.Errorf("unknow field ")
  512. } else if l == 1 {
  513. // valid, do nothing
  514. } else {
  515. return fmt.Errorf("ambiguous field ")
  516. }
  517. } else {
  518. _, ok := s.content[k]
  519. if !ok {
  520. return fmt.Errorf("unknow field %s.", k)
  521. }
  522. }
  523. }
  524. return nil
  525. }
  526. func (s *streamFieldMap) bindRef(fr *ast.FieldRef) error {
  527. l := len(s.content)
  528. if fr.StreamName == "" {
  529. fr.StreamName = ast.DefaultStream
  530. }
  531. k := fr.StreamName
  532. if k == ast.DefaultStream {
  533. switch l {
  534. case 0:
  535. return fmt.Errorf("unknown field ")
  536. case 1: // if alias, return this
  537. for sk, sv := range s.content {
  538. fr.RefSelection(sv)
  539. fr.StreamName = sk
  540. }
  541. return nil
  542. default:
  543. r, ok := s.content[ast.AliasStream] // if alias exists
  544. if ok {
  545. fr.RefSelection(r)
  546. fr.StreamName = ast.AliasStream
  547. return nil
  548. } else {
  549. return fmt.Errorf("ambiguous field ")
  550. }
  551. }
  552. } else {
  553. r, ok := s.content[k]
  554. if ok {
  555. fr.RefSelection(r)
  556. return nil
  557. } else {
  558. return fmt.Errorf("unknown field %s.", k)
  559. }
  560. }
  561. }
  562. type streamFieldMapSchemaless struct {
  563. content map[ast.StreamName]*ast.AliasRef
  564. defaultStream ast.StreamName
  565. }
  566. // add this should not be called for schemaless
  567. func (s *streamFieldMapSchemaless) add(k ast.StreamName) {
  568. s.content[k] = nil
  569. }
  570. // bind for schemaless field, create column if not exist
  571. // can bind alias & col. For alias, the stream name must be empty; For col, the field must be a col
  572. func (s *streamFieldMapSchemaless) ref(k ast.StreamName, v *ast.AliasRef) error {
  573. if k == ast.AliasStream { // must not exist
  574. _, ok := s.content[k]
  575. if ok {
  576. return fmt.Errorf("duplicate alias ")
  577. }
  578. s.content[k] = v
  579. } else { // the key may or may not exist. But always have only one default stream field.
  580. // Replace with stream name if another stream found. The key can be duplicate
  581. l := len(s.content)
  582. if k == ast.DefaultStream { // In schemaless mode, default stream can only exist when length is 1
  583. if l < 1 {
  584. // valid, do nothing
  585. } else {
  586. return fmt.Errorf("ambiguous field ")
  587. }
  588. } else {
  589. if l == 1 {
  590. for sk := range s.content {
  591. if sk == ast.DefaultStream {
  592. delete(s.content, k)
  593. }
  594. }
  595. }
  596. }
  597. }
  598. return nil
  599. }
  600. func (s *streamFieldMapSchemaless) bindRef(fr *ast.FieldRef) error {
  601. l := len(s.content)
  602. if fr.StreamName == "" || fr.StreamName == ast.DefaultStream {
  603. if l == 1 {
  604. for sk := range s.content {
  605. fr.StreamName = sk
  606. }
  607. }
  608. }
  609. k := fr.StreamName
  610. if k == ast.DefaultStream {
  611. switch l {
  612. case 0: // must be a column because alias are fields and have been traversed
  613. // reserve a hole and do nothing
  614. fr.StreamName = s.defaultStream
  615. s.content[s.defaultStream] = nil
  616. return nil
  617. case 1: // if alias or single col, return this
  618. for sk, sv := range s.content {
  619. fr.RefSelection(sv)
  620. fr.StreamName = sk
  621. }
  622. return nil
  623. default:
  624. r, ok := s.content[ast.AliasStream] // if alias exists
  625. if ok {
  626. fr.RefSelection(r)
  627. fr.StreamName = ast.AliasStream
  628. return nil
  629. } else {
  630. fr.StreamName = s.defaultStream
  631. }
  632. }
  633. }
  634. if fr.StreamName != ast.DefaultStream {
  635. r, ok := s.content[k]
  636. if !ok { // reserver a hole
  637. s.content[k] = nil
  638. } else {
  639. fr.RefSelection(r)
  640. }
  641. return nil
  642. }
  643. return fmt.Errorf("ambiguous field ")
  644. }