MCPcopy Index your code
hub / github.com/bytebase/bytebase / getQuerySpanImpl

Function getQuerySpanImpl

backend/plugin/parser/cosmosdb/query_span.go:24–88  ·  view source on GitHub ↗
(statement string)

Source from the content-addressed store, hash-verified

22}
23
24func getQuerySpanImpl(statement string) (*base.QuerySpan, error) {
25 parseResults, err := ParseCosmosDB(statement)
26 if err != nil {
27 return nil, err
28 }
29
30 if len(parseResults) == 0 {
31 return &base.QuerySpan{
32 Results: []base.QuerySpanResult{},
33 PredicatePaths: nil,
34 }, nil
35 }
36
37 if len(parseResults) != 1 {
38 return nil, errors.Errorf("expecting only one statement to get query span, but got %d", len(parseResults))
39 }
40
41 selectStmt, ok := parseResults[0].Node.(*ast.SelectStmt)
42 if !ok {
43 return nil, errors.Errorf("expected SelectStmt, got %T", parseResults[0].Node)
44 }
45
46 qa := analysis.Analyze(selectStmt)
47
48 // Convert projections.
49 sourceFieldPaths := make(map[string][]*base.PathAST)
50 if !qa.SelectStar {
51 for _, proj := range qa.Projections {
52 for _, fp := range proj.SourcePaths {
53 if len(fp) == 0 {
54 continue
55 }
56 sourceFieldPaths[proj.Name] = append(sourceFieldPaths[proj.Name], fieldPathToPathAST(fp))
57 }
58 }
59 }
60
61 result := base.QuerySpanResult{
62 SourceFieldPaths: sourceFieldPaths,
63 SelectAsterisk: qa.SelectStar,
64 }
65
66 // Convert predicates.
67 var predicatePaths map[string]*base.PathAST
68 if len(qa.Predicates) > 0 {
69 predicatePaths = make(map[string]*base.PathAST)
70 for _, fp := range qa.Predicates {
71 if len(fp) == 0 {
72 continue
73 }
74 pathAST := fieldPathToPathAST(fp)
75 str, err := pathAST.String()
76 if err != nil {
77 slog.Warn("failed to convert path ast to string", log.BBError(err))
78 continue
79 }
80 predicatePaths[str] = pathAST
81 }

Callers 2

TestPredicateInWhereFunction · 0.85
GetQuerySpanFunction · 0.85

Calls 5

BBErrorFunction · 0.92
ParseCosmosDBFunction · 0.85
fieldPathToPathASTFunction · 0.85
ErrorfMethod · 0.80
StringMethod · 0.65

Tested by 1

TestPredicateInWhereFunction · 0.68