MCPcopy Create free account
hub / github.com/dgraph-io/dgraph / processTernary

Function processTernary

query/math.go:211–248  ·  view source on GitHub ↗

processTernary handles the ternary operand cond()

(mNode *mathTree)

Source from the content-addressed store, hash-verified

209
210// processTernary handles the ternary operand cond()
211func processTernary(mNode *mathTree) error {
212 destMap := types.NewShardedMap()
213 aggName := mNode.Fn
214 condMap := mNode.Child[0].Val
215 if condMap.IsEmpty() {
216 return errors.Errorf("Expected a value variable in %v but missing.", aggName)
217 }
218 varOne := mNode.Child[1].Val
219 varTwo := mNode.Child[2].Val
220 constOne := mNode.Child[1].Const
221 constTwo := mNode.Child[2].Const
222 condMap.Iterate(func(k uint64, val types.Val) error {
223 var res types.Val
224 v, ok := val.Value.(bool)
225 if !ok {
226 return errors.Errorf("First variable of conditional function not a bool value")
227 }
228 if v {
229 // Pick the value of first map.
230 if constOne.Value != nil {
231 res = constOne
232 } else {
233 res, _ = varOne.Get(k)
234 }
235 } else {
236 // Pick the value of second map.
237 if constTwo.Value != nil {
238 res = constTwo
239 } else {
240 res, _ = varTwo.Get(k)
241 }
242 }
243 destMap.Set(k, res)
244 return nil
245 })
246 mNode.Val = destMap
247 return nil
248}
249
250func evalMathTree(mNode *mathTree) error {
251 if mNode.Const.Value != nil {

Callers 2

evalMathTreeFunction · 0.85
TestProcessTernaryFunction · 0.85

Calls 6

SetMethod · 0.95
NewShardedMapFunction · 0.92
GetMethod · 0.65
IsEmptyMethod · 0.45
ErrorfMethod · 0.45
IterateMethod · 0.45

Tested by 1

TestProcessTernaryFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…