|
| 1 | +package knowledge |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | +) |
| 7 | + |
| 8 | +// AndOrExpression represent a AND or OR expression |
| 9 | +type AndOrExpression struct { |
| 10 | + And bool // true for And and false for Or |
| 11 | + Children []AndOrExpression |
| 12 | + Expression string |
| 13 | +} |
| 14 | + |
| 15 | +func (aoe AndOrExpression) String() string { |
| 16 | + if aoe.Expression != "" { |
| 17 | + return aoe.Expression |
| 18 | + } |
| 19 | + |
| 20 | + if len(aoe.Children) > 0 { |
| 21 | + op := " AND " |
| 22 | + if !aoe.And { |
| 23 | + op = " OR " |
| 24 | + } |
| 25 | + exprs := make([]string, 0) |
| 26 | + for _, e := range aoe.Children { |
| 27 | + exprs = append(exprs, e.String()) |
| 28 | + } |
| 29 | + return strings.Join(exprs, op) |
| 30 | + } |
| 31 | + return "(empty)" |
| 32 | +} |
| 33 | + |
| 34 | +func BuildAndOrExpression(tree AndOrExpression) (string, error) { |
| 35 | + if tree.Expression != "" { |
| 36 | + return tree.Expression, nil |
| 37 | + } else if tree.And { |
| 38 | + exprs := make([]string, 0) |
| 39 | + for i := range tree.Children { |
| 40 | + expr, err := BuildAndOrExpression(tree.Children[i]) |
| 41 | + if err != nil { |
| 42 | + return "", err |
| 43 | + } |
| 44 | + if expr != "" { |
| 45 | + exprs = append(exprs, expr) |
| 46 | + } |
| 47 | + } |
| 48 | + if len(exprs) > 1 { |
| 49 | + return fmt.Sprintf("(%s)", strings.Join(exprs, " AND ")), nil |
| 50 | + } |
| 51 | + return strings.Join(exprs, " AND "), nil |
| 52 | + } else if !tree.And { |
| 53 | + exprs := make([]string, 0) |
| 54 | + for i := range tree.Children { |
| 55 | + expr, err := BuildAndOrExpression(tree.Children[i]) |
| 56 | + if err != nil { |
| 57 | + return "", err |
| 58 | + } |
| 59 | + |
| 60 | + if expr != "" { |
| 61 | + exprs = append(exprs, expr) |
| 62 | + } |
| 63 | + } |
| 64 | + if len(exprs) > 1 { |
| 65 | + return fmt.Sprintf("(%s)", strings.Join(exprs, " OR ")), nil |
| 66 | + } |
| 67 | + return strings.Join(exprs, " OR "), nil |
| 68 | + } |
| 69 | + return "", nil |
| 70 | +} |
| 71 | + |
| 72 | +func CrossProductExpressions(and1 []AndOrExpression, and2 []AndOrExpression) []AndOrExpression { |
| 73 | + outExpr := []AndOrExpression{} |
| 74 | + for i := range and1 { |
| 75 | + for j := range and2 { |
| 76 | + children := AndOrExpression{ |
| 77 | + And: true, |
| 78 | + Children: []AndOrExpression{and1[i], and2[j]}, |
| 79 | + } |
| 80 | + outExpr = append(outExpr, children) |
| 81 | + } |
| 82 | + } |
| 83 | + return outExpr |
| 84 | +} |
| 85 | + |
| 86 | +// UnwindOrExpressions in order to transform query with or relations into a union |
| 87 | +// query which is more performant, an AndOrExpression is transformed into a list of AndExpressions |
| 88 | +func UnwindOrExpressions(tree AndOrExpression) ([]AndOrExpression, error) { |
| 89 | + if tree.Expression != "" { |
| 90 | + child := AndOrExpression{Children: []AndOrExpression{tree}, And: true} |
| 91 | + return []AndOrExpression{child}, nil |
| 92 | + } else if !tree.And { |
| 93 | + exprs := []AndOrExpression{} |
| 94 | + for i := range tree.Children { |
| 95 | + nestedExpressions, err := UnwindOrExpressions(tree.Children[i]) |
| 96 | + if err != nil { |
| 97 | + return nil, err |
| 98 | + } |
| 99 | + exprs = append(exprs, nestedExpressions...) |
| 100 | + } |
| 101 | + return exprs, nil |
| 102 | + } else if tree.And { |
| 103 | + exprs := []AndOrExpression{} |
| 104 | + for i := range tree.Children { |
| 105 | + expr, err := UnwindOrExpressions(tree.Children[i]) |
| 106 | + if err != nil { |
| 107 | + return nil, err |
| 108 | + } |
| 109 | + |
| 110 | + if len(exprs) == 0 { |
| 111 | + exprs = append(exprs, expr...) |
| 112 | + } else { |
| 113 | + exprs = CrossProductExpressions(exprs, expr) |
| 114 | + } |
| 115 | + } |
| 116 | + return exprs, nil |
| 117 | + } |
| 118 | + return nil, fmt.Errorf("Unable to detect kind of node") |
| 119 | +} |
0 commit comments