7
7
8
8
import com .yahoo .bullet .parsing .FilterClause ;
9
9
import com .yahoo .bullet .parsing .LogicalClause ;
10
+ import com .yahoo .bullet .typesystem .Type ;
10
11
import com .yahoo .bullet .typesystem .TypedObject ;
11
12
import com .yahoo .bullet .parsing .Clause ;
12
13
import com .yahoo .bullet .record .BulletRecord ;
@@ -57,6 +58,7 @@ public interface LogicalOperator extends BiPredicate<BulletRecord, Stream<Boolea
57
58
private static final Comparator <TypedObject > GE = (t , s ) -> s .anyMatch (i -> t .compareTo (i ) >= 0 );
58
59
private static final Comparator <TypedObject > LE = (t , s ) -> s .anyMatch (i -> t .compareTo (i ) <= 0 );
59
60
private static final Comparator <Pattern > RLIKE = (t , s ) -> s .map (p -> p .matcher (t .toString ())).anyMatch (Matcher ::matches );
61
+ private static final Comparator <TypedObject > SIZEOF = (t , s ) -> s .anyMatch (i -> sizeOf (t ) == i .getValue ());
60
62
private static final LogicalOperator AND = (r , s ) -> s .allMatch (Boolean ::valueOf );
61
63
private static final LogicalOperator OR = (r , s ) -> s .anyMatch (Boolean ::valueOf );
62
64
private static final LogicalOperator NOT = (r , s ) -> !s .findFirst ().get ();
@@ -71,6 +73,7 @@ public interface LogicalOperator extends BiPredicate<BulletRecord, Stream<Boolea
71
73
COMPARATORS .put (Clause .Operation .LESS_EQUALS , isNotNullAnd (LE ));
72
74
}
73
75
static final Comparator <Pattern > REGEX_LIKE = isNotNullAnd (RLIKE );
76
+ static final Comparator <TypedObject > SIZE_OF = isNotNullAnd (SIZEOF );
74
77
static final Map <Clause .Operation , LogicalOperator > LOGICAL_OPERATORS = new EnumMap <>(Clause .Operation .class );
75
78
static {
76
79
LOGICAL_OPERATORS .put (Clause .Operation .AND , AND );
@@ -95,16 +98,34 @@ private static <T> Comparator<T> isNotNullAnd(Comparator<T> comparator) {
95
98
return (t , s ) -> IS_NOT_NULL .test (t ) && comparator .compare (t , s );
96
99
}
97
100
101
+ private static Integer sizeOf (TypedObject object ) {
102
+ Object o = object .getValue ();
103
+ if (o instanceof List ) {
104
+ return List .class .cast (o ).size ();
105
+ }
106
+ if (o instanceof Map ) {
107
+ return Map .class .cast (o ).size ();
108
+ }
109
+ if (o instanceof String ) {
110
+ return String .class .cast (o ).length ();
111
+ }
112
+ return 1 ;
113
+ }
114
+
98
115
private static boolean performRelational (BulletRecord record , FilterClause clause ) {
99
116
Clause .Operation operator = clause .getOperation ();
100
117
if (isEmpty (clause .getValues ())) {
101
118
return true ;
102
119
}
103
120
TypedObject object = extractTypedObject (clause .getField (), record );
104
- if (operator == Clause .Operation .REGEX_LIKE ) {
105
- return REGEX_LIKE .compare (object , clause .getPatterns ().stream ());
121
+ switch (operator ) {
122
+ case REGEX_LIKE :
123
+ return REGEX_LIKE .compare (object , clause .getPatterns ().stream ());
124
+ case SIZE_OF :
125
+ return SIZE_OF .compare (object , cast (new TypedObject (Type .INTEGER , 0 ), clause .getValues ()));
126
+ default :
127
+ return COMPARATORS .get (operator ).compare (object , cast (object , clause .getValues ()));
106
128
}
107
- return COMPARATORS .get (operator ).compare (object , cast (object , clause .getValues ()));
108
129
}
109
130
110
131
private static boolean performLogical (BulletRecord record , LogicalClause clause ) {
0 commit comments