convertQueryPlanToJSON converts a Spanner QueryPlan to JSON string.
(plan *sppb.QueryPlan)
| 645 | |
| 646 | // convertQueryPlanToJSON converts a Spanner QueryPlan to JSON string. |
| 647 | func convertQueryPlanToJSON(plan *sppb.QueryPlan) (string, error) { |
| 648 | if plan == nil { |
| 649 | return "{}", nil |
| 650 | } |
| 651 | |
| 652 | queryPlan := QueryPlan{ |
| 653 | PlanNodes: make([]PlanNode, 0, len(plan.PlanNodes)), |
| 654 | } |
| 655 | |
| 656 | for _, node := range plan.PlanNodes { |
| 657 | planNode := PlanNode{ |
| 658 | Index: node.Index, |
| 659 | Kind: node.Kind.String(), |
| 660 | DisplayName: node.DisplayName, |
| 661 | } |
| 662 | |
| 663 | // Convert child links |
| 664 | if len(node.ChildLinks) > 0 { |
| 665 | planNode.ChildLinks = make([]ChildLink, 0, len(node.ChildLinks)) |
| 666 | for _, link := range node.ChildLinks { |
| 667 | planNode.ChildLinks = append(planNode.ChildLinks, ChildLink{ |
| 668 | ChildIndex: link.ChildIndex, |
| 669 | Type: link.Type, |
| 670 | Variable: link.Variable, |
| 671 | }) |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | // Convert short representation |
| 676 | if node.ShortRepresentation != nil { |
| 677 | planNode.ShortRepresentation = &ShortRepresentation{ |
| 678 | Description: node.ShortRepresentation.Description, |
| 679 | Subqueries: node.ShortRepresentation.Subqueries, |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | // Convert metadata |
| 684 | if node.Metadata != nil { |
| 685 | planNode.Metadata = node.Metadata.AsMap() |
| 686 | } |
| 687 | |
| 688 | // Convert execution stats |
| 689 | if node.ExecutionStats != nil { |
| 690 | planNode.ExecutionStats = node.ExecutionStats.AsMap() |
| 691 | } |
| 692 | |
| 693 | queryPlan.PlanNodes = append(queryPlan.PlanNodes, planNode) |
| 694 | } |
| 695 | |
| 696 | jsonBytes, err := json.Marshal(queryPlan) |
| 697 | if err != nil { |
| 698 | return "", err |
| 699 | } |
| 700 | |
| 701 | return string(jsonBytes), nil |
| 702 | } |
no test coverage detected