|
| 1 | +package distributed_execution |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "math" |
| 7 | + |
| 8 | + "github.com/prometheus/prometheus/model/labels" |
| 9 | + "github.com/thanos-io/promql-engine/logicalplan" |
| 10 | +) |
| 11 | + |
| 12 | +type jsonNode struct { |
| 13 | + Type logicalplan.NodeType `json:"type"` |
| 14 | + Data json.RawMessage `json:"data"` |
| 15 | + Children []json.RawMessage `json:"children,omitempty"` |
| 16 | +} |
| 17 | + |
| 18 | +const ( |
| 19 | + nanVal = `"NaN"` |
| 20 | + infVal = `"+Inf"` |
| 21 | + negInfVal = `"-Inf"` |
| 22 | +) |
| 23 | + |
| 24 | +func Unmarshal(data []byte) (logicalplan.Node, error) { |
| 25 | + return unmarshalNode(data) |
| 26 | +} |
| 27 | + |
| 28 | +func unmarshalNode(data []byte) (logicalplan.Node, error) { |
| 29 | + t := jsonNode{} |
| 30 | + if err := json.Unmarshal(data, &t); err != nil { |
| 31 | + return nil, err |
| 32 | + } |
| 33 | + |
| 34 | + switch t.Type { |
| 35 | + case logicalplan.VectorSelectorNode: |
| 36 | + v := &logicalplan.VectorSelector{} |
| 37 | + if err := json.Unmarshal(t.Data, v); err != nil { |
| 38 | + return nil, err |
| 39 | + } |
| 40 | + var err error |
| 41 | + for i, m := range v.LabelMatchers { |
| 42 | + v.LabelMatchers[i], err = labels.NewMatcher(m.Type, m.Name, m.Value) |
| 43 | + if err != nil { |
| 44 | + return nil, err |
| 45 | + } |
| 46 | + } |
| 47 | + return v, nil |
| 48 | + case logicalplan.MatrixSelectorNode: |
| 49 | + m := &logicalplan.MatrixSelector{} |
| 50 | + if err := json.Unmarshal(t.Data, m); err != nil { |
| 51 | + return nil, err |
| 52 | + } |
| 53 | + vs, err := unmarshalNode(t.Children[0]) |
| 54 | + if err != nil { |
| 55 | + return nil, err |
| 56 | + } |
| 57 | + m.VectorSelector = vs.(*logicalplan.VectorSelector) |
| 58 | + return m, nil |
| 59 | + case logicalplan.AggregationNode: |
| 60 | + a := &logicalplan.Aggregation{} |
| 61 | + if err := json.Unmarshal(t.Data, a); err != nil { |
| 62 | + return nil, err |
| 63 | + } |
| 64 | + var err error |
| 65 | + a.Expr, err = unmarshalNode(t.Children[0]) |
| 66 | + if err != nil { |
| 67 | + return nil, err |
| 68 | + } |
| 69 | + if len(t.Children) > 1 { |
| 70 | + a.Param, err = unmarshalNode(t.Children[1]) |
| 71 | + if err != nil { |
| 72 | + return nil, err |
| 73 | + } |
| 74 | + } |
| 75 | + return a, nil |
| 76 | + case logicalplan.BinaryNode: |
| 77 | + b := &logicalplan.Binary{} |
| 78 | + if err := json.Unmarshal(t.Data, b); err != nil { |
| 79 | + return nil, err |
| 80 | + } |
| 81 | + var err error |
| 82 | + b.LHS, err = unmarshalNode(t.Children[0]) |
| 83 | + if err != nil { |
| 84 | + return nil, err |
| 85 | + } |
| 86 | + b.RHS, err = unmarshalNode(t.Children[1]) |
| 87 | + if err != nil { |
| 88 | + return nil, err |
| 89 | + } |
| 90 | + return b, nil |
| 91 | + case logicalplan.FunctionNode: |
| 92 | + f := &logicalplan.FunctionCall{} |
| 93 | + if err := json.Unmarshal(t.Data, f); err != nil { |
| 94 | + return nil, err |
| 95 | + } |
| 96 | + for _, c := range t.Children { |
| 97 | + child, err := unmarshalNode(c) |
| 98 | + if err != nil { |
| 99 | + return nil, err |
| 100 | + } |
| 101 | + f.Args = append(f.Args, child) |
| 102 | + } |
| 103 | + return f, nil |
| 104 | + case logicalplan.NumberLiteralNode: |
| 105 | + n := &logicalplan.NumberLiteral{} |
| 106 | + if bytes.Equal(t.Data, []byte(infVal)) { |
| 107 | + n.Val = math.Inf(1) |
| 108 | + } else if bytes.Equal(t.Data, []byte(negInfVal)) { |
| 109 | + n.Val = math.Inf(-1) |
| 110 | + } else if bytes.Equal(t.Data, []byte(nanVal)) { |
| 111 | + n.Val = math.NaN() |
| 112 | + } else { |
| 113 | + if err := json.Unmarshal(t.Data, n); err != nil { |
| 114 | + return nil, err |
| 115 | + } |
| 116 | + } |
| 117 | + return n, nil |
| 118 | + case logicalplan.StringLiteralNode: |
| 119 | + s := &logicalplan.StringLiteral{} |
| 120 | + if err := json.Unmarshal(t.Data, s); err != nil { |
| 121 | + return nil, err |
| 122 | + } |
| 123 | + return s, nil |
| 124 | + case logicalplan.SubqueryNode: |
| 125 | + s := &logicalplan.Subquery{} |
| 126 | + if err := json.Unmarshal(t.Data, s); err != nil { |
| 127 | + return nil, err |
| 128 | + } |
| 129 | + var err error |
| 130 | + s.Expr, err = unmarshalNode(t.Children[0]) |
| 131 | + if err != nil { |
| 132 | + return nil, err |
| 133 | + } |
| 134 | + return s, nil |
| 135 | + case logicalplan.CheckDuplicateNode: |
| 136 | + c := &logicalplan.CheckDuplicateLabels{} |
| 137 | + if err := json.Unmarshal(t.Data, c); err != nil { |
| 138 | + return nil, err |
| 139 | + } |
| 140 | + var err error |
| 141 | + c.Expr, err = unmarshalNode(t.Children[0]) |
| 142 | + if err != nil { |
| 143 | + return nil, err |
| 144 | + } |
| 145 | + return c, nil |
| 146 | + case logicalplan.StepInvariantNode: |
| 147 | + s := &logicalplan.StepInvariantExpr{} |
| 148 | + if err := json.Unmarshal(t.Data, s); err != nil { |
| 149 | + return nil, err |
| 150 | + } |
| 151 | + var err error |
| 152 | + s.Expr, err = unmarshalNode(t.Children[0]) |
| 153 | + if err != nil { |
| 154 | + return nil, err |
| 155 | + } |
| 156 | + return s, nil |
| 157 | + case logicalplan.ParensNode: |
| 158 | + p := &logicalplan.Parens{} |
| 159 | + if err := json.Unmarshal(t.Data, p); err != nil { |
| 160 | + return nil, err |
| 161 | + } |
| 162 | + var err error |
| 163 | + p.Expr, err = unmarshalNode(t.Children[0]) |
| 164 | + if err != nil { |
| 165 | + return nil, err |
| 166 | + } |
| 167 | + return p, nil |
| 168 | + case logicalplan.UnaryNode: |
| 169 | + u := &logicalplan.Unary{} |
| 170 | + if err := json.Unmarshal(t.Data, u); err != nil { |
| 171 | + return nil, err |
| 172 | + } |
| 173 | + var err error |
| 174 | + u.Expr, err = unmarshalNode(t.Children[0]) |
| 175 | + if err != nil { |
| 176 | + return nil, err |
| 177 | + } |
| 178 | + return u, nil |
| 179 | + case RemoteNode: |
| 180 | + r := &Remote{} |
| 181 | + if err := json.Unmarshal(t.Data, r); err != nil { |
| 182 | + return nil, err |
| 183 | + } |
| 184 | + var err error |
| 185 | + r.Expr, err = unmarshalNode(t.Children[0]) |
| 186 | + if err != nil { |
| 187 | + return nil, err |
| 188 | + } |
| 189 | + return r, nil |
| 190 | + } |
| 191 | + return nil, nil |
| 192 | +} |
0 commit comments