|
| 1 | +package pointers |
| 2 | + |
| 3 | +import "github.com/apache/arrow-go/v18/arrow/scalar" |
| 4 | + |
| 5 | +// Predicate is an expression used to filter column values in a [Reader]. |
| 6 | +type Predicate interface{ isPredicate() } |
| 7 | + |
| 8 | +// Supported predicates. |
| 9 | +type ( |
| 10 | + // An AndPredicate is a [Predicate] which asserts that a row may only be |
| 11 | + // included if both the Left and Right Predicate are true. |
| 12 | + AndPredicate struct{ Left, Right Predicate } |
| 13 | + |
| 14 | + // An OrPredicate is a [Predicate] which asserts that a row may only be |
| 15 | + // included if either the Left or Right Predicate are true. |
| 16 | + OrPredicate struct{ Left, Right Predicate } |
| 17 | + |
| 18 | + // A NotePredicate is a [Predicate] which asserts that a row may only be |
| 19 | + // included if the inner Predicate is false. |
| 20 | + NotPredicate struct{ Inner Predicate } |
| 21 | + |
| 22 | + // TruePredicate is a [Predicate] which always returns true. |
| 23 | + TruePredicate struct{} |
| 24 | + |
| 25 | + // FalsePredicate is a [Predicate] which always returns false. |
| 26 | + FalsePredicate struct{} |
| 27 | + |
| 28 | + // An EqualPredicate is a [Predicate] which asserts that a row may only be |
| 29 | + // included if the Value of the Column is equal to the Value. |
| 30 | + EqualPredicate struct { |
| 31 | + Column *Column // Column to check. |
| 32 | + Value scalar.Scalar // Value to check equality for. |
| 33 | + } |
| 34 | + |
| 35 | + // An InPredicate is a [Predicate] which asserts that a row may only be |
| 36 | + // included if the Value of the Column is present in the provided Values. |
| 37 | + InPredicate struct { |
| 38 | + Column *Column // Column to check. |
| 39 | + Values []scalar.Scalar // Values to check for inclusion. |
| 40 | + } |
| 41 | + |
| 42 | + // A GreaterThanPredicate is a [Predicate] which asserts that a row may only |
| 43 | + // be included if the Value of the Column is greater than the provided Value. |
| 44 | + GreaterThanPredicate struct { |
| 45 | + Column *Column // Column to check. |
| 46 | + Value scalar.Scalar // Value for which rows in Column must be greater than. |
| 47 | + } |
| 48 | + |
| 49 | + // A LessThanPredicate is a [Predicate] which asserts that a row may only be |
| 50 | + // included if the Value of the Column is less than the provided Value. |
| 51 | + LessThanPredicate struct { |
| 52 | + Column *Column // Column to check. |
| 53 | + Value scalar.Scalar // Value for which rows in Column must be less than. |
| 54 | + } |
| 55 | + |
| 56 | + // FuncPredicate is a [Predicate] which asserts that a row may only be |
| 57 | + // included if the Value of the Column passes the Keep function. |
| 58 | + // |
| 59 | + // Instances of FuncPredicate are ineligible for page filtering and should |
| 60 | + // only be used when there isn't a more explicit Predicate implementation. |
| 61 | + FuncPredicate struct { |
| 62 | + Column *Column // Column to check. |
| 63 | + |
| 64 | + // Keep is invoked with the column and value pair to check. Keep is given |
| 65 | + // the Column instance to allow for reusing the same function across |
| 66 | + // multiple columns, if necessary. |
| 67 | + // |
| 68 | + // If Keep returns true, the row is kept. |
| 69 | + Keep func(column *Column, value scalar.Scalar) bool |
| 70 | + } |
| 71 | +) |
| 72 | + |
| 73 | +func (AndPredicate) isPredicate() {} |
| 74 | +func (OrPredicate) isPredicate() {} |
| 75 | +func (NotPredicate) isPredicate() {} |
| 76 | +func (TruePredicate) isPredicate() {} |
| 77 | +func (FalsePredicate) isPredicate() {} |
| 78 | +func (EqualPredicate) isPredicate() {} |
| 79 | +func (InPredicate) isPredicate() {} |
| 80 | +func (GreaterThanPredicate) isPredicate() {} |
| 81 | +func (LessThanPredicate) isPredicate() {} |
| 82 | +func (FuncPredicate) isPredicate() {} |
| 83 | + |
| 84 | +// walkPredicate traverses a predicate in depth-first order: it starts by |
| 85 | +// calling fn(p). If fn(p) returns true, walkPredicate is invoked recursively |
| 86 | +// with fn for each of the non-nil children of p, followed by a call of |
| 87 | +// fn(nil). |
| 88 | +func walkPredicate(p Predicate, fn func(Predicate) bool) { |
| 89 | + if p == nil || !fn(p) { |
| 90 | + return |
| 91 | + } |
| 92 | + |
| 93 | + switch p := p.(type) { |
| 94 | + case AndPredicate: |
| 95 | + walkPredicate(p.Left, fn) |
| 96 | + walkPredicate(p.Right, fn) |
| 97 | + |
| 98 | + case OrPredicate: |
| 99 | + walkPredicate(p.Left, fn) |
| 100 | + walkPredicate(p.Right, fn) |
| 101 | + |
| 102 | + case NotPredicate: |
| 103 | + walkPredicate(p.Inner, fn) |
| 104 | + |
| 105 | + case TruePredicate: // No children. |
| 106 | + case FalsePredicate: // No children. |
| 107 | + case EqualPredicate: // No children. |
| 108 | + case InPredicate: // No children. |
| 109 | + case GreaterThanPredicate: // No children. |
| 110 | + case LessThanPredicate: // No children. |
| 111 | + case FuncPredicate: // No children. |
| 112 | + |
| 113 | + default: |
| 114 | + panic("streams.walkPredicate: unsupported predicate type") |
| 115 | + } |
| 116 | + |
| 117 | + fn(nil) |
| 118 | +} |
0 commit comments