12
12
import com .yahoo .bullet .typesystem .TypedObject ;
13
13
14
14
import java .io .Serializable ;
15
+ import java .util .Set ;
15
16
16
17
/**
17
18
* An evaluator that extracts a given field from a {@link BulletRecord}. This is the only evaluator that directly takes a
@@ -45,36 +46,37 @@ private static FieldExtractor getFieldExtractor(FieldExpression fieldExpression)
45
46
final String field = fieldExpression .getField ();
46
47
final Serializable key = fieldExpression .getKey ();
47
48
final Serializable subKey = fieldExpression .getSubKey ();
49
+ final Type fieldType = fieldExpression .getType () != null ? fieldExpression .getType () : Type .UNKNOWN ;
48
50
49
51
if (key instanceof String ) {
50
52
if (subKey instanceof String ) {
51
- return record -> record .typedGet (field , (String ) key , (String ) subKey );
53
+ return record -> record .typedGet (field , (String ) key , (String ) subKey , getSuperSuperType ( Type . COMPLEX_MAPS , fieldType ) );
52
54
} else if (subKey instanceof Expression ) {
53
55
final Evaluator subKeyEvaluator = ((Expression ) subKey ).getEvaluator ();
54
56
return record -> {
55
57
TypedObject subKeyArg = subKeyEvaluator .evaluate (record );
56
58
if (subKeyArg .isNull ()) {
57
59
return TypedObject .NULL ;
58
60
}
59
- return record .typedGet (field , (String ) key , (String ) subKeyArg .getValue ());
61
+ return record .typedGet (field , (String ) key , (String ) subKeyArg .getValue (), getSuperSuperType ( Type . COMPLEX_MAPS , fieldType ) );
60
62
};
61
63
} else {
62
- return record -> record .typedGet (field , (String ) key );
64
+ return record -> record .typedGet (field , (String ) key , getSuperType ( Type . MAPS , fieldType ) );
63
65
}
64
66
} else if (key instanceof Integer ) {
65
67
if (subKey instanceof String ) {
66
- return record -> record .typedGet (field , (Integer ) key , (String ) subKey );
68
+ return record -> record .typedGet (field , (Integer ) key , (String ) subKey , getSuperSuperType ( Type . COMPLEX_LISTS , fieldType ) );
67
69
} else if (subKey instanceof Expression ) {
68
70
final Evaluator subKeyEvaluator = ((Expression ) subKey ).getEvaluator ();
69
71
return record -> {
70
72
TypedObject subKeyArg = subKeyEvaluator .evaluate (record );
71
73
if (subKeyArg .isNull ()) {
72
74
return TypedObject .NULL ;
73
75
}
74
- return record .typedGet (field , (Integer ) key , (String ) subKeyArg .getValue ());
76
+ return record .typedGet (field , (Integer ) key , (String ) subKeyArg .getValue (), getSuperSuperType ( Type . COMPLEX_LISTS , fieldType ) );
75
77
};
76
78
} else {
77
- return record -> record .typedGet (field , (Integer ) key );
79
+ return record -> record .typedGet (field , (Integer ) key , getSuperType ( Type . LISTS , fieldType ) );
78
80
}
79
81
} else if (key instanceof Expression ) {
80
82
final Evaluator keyEvaluator = ((Expression ) key ).getEvaluator ();
@@ -86,9 +88,9 @@ private static FieldExtractor getFieldExtractor(FieldExpression fieldExpression)
86
88
}
87
89
Type type = keyArg .getType ();
88
90
if (Type .isNumeric (type )) {
89
- return record .typedGet (field , ((Number ) keyArg .getValue ()).intValue (), (String ) subKey );
91
+ return record .typedGet (field , ((Number ) keyArg .getValue ()).intValue (), (String ) subKey , getSuperSuperType ( Type . COMPLEX_LISTS , fieldType ) );
90
92
} else {
91
- return record .typedGet (field , (String ) keyArg .getValue (), (String ) subKey );
93
+ return record .typedGet (field , (String ) keyArg .getValue (), (String ) subKey , getSuperSuperType ( Type . COMPLEX_MAPS , fieldType ) );
92
94
}
93
95
};
94
96
} else if (subKey instanceof Expression ) {
@@ -104,9 +106,9 @@ private static FieldExtractor getFieldExtractor(FieldExpression fieldExpression)
104
106
}
105
107
Type type = keyArg .getType ();
106
108
if (Type .isNumeric (type )) {
107
- return record .typedGet (field , ((Number ) keyArg .getValue ()).intValue (), (String ) subKeyArg .getValue ());
109
+ return record .typedGet (field , ((Number ) keyArg .getValue ()).intValue (), (String ) subKeyArg .getValue (), getSuperSuperType ( Type . COMPLEX_LISTS , fieldType ) );
108
110
} else {
109
- return record .typedGet (field , (String ) keyArg .getValue (), (String ) subKeyArg .getValue ());
111
+ return record .typedGet (field , (String ) keyArg .getValue (), (String ) subKeyArg .getValue (), getSuperSuperType ( Type . COMPLEX_MAPS , fieldType ) );
110
112
}
111
113
};
112
114
} else {
@@ -117,14 +119,22 @@ private static FieldExtractor getFieldExtractor(FieldExpression fieldExpression)
117
119
}
118
120
Type type = keyArg .getType ();
119
121
if (Type .isNumeric (type )) {
120
- return record .typedGet (field , ((Number ) keyArg .getValue ()).intValue ());
122
+ return record .typedGet (field , ((Number ) keyArg .getValue ()).intValue (), getSuperType ( Type . LISTS , fieldType ) );
121
123
} else {
122
- return record .typedGet (field , (String ) keyArg .getValue ());
124
+ return record .typedGet (field , (String ) keyArg .getValue (), getSuperType ( Type . MAPS , fieldType ) );
123
125
}
124
126
};
125
127
}
126
128
} else {
127
- return record -> record .typedGet (field );
129
+ return record -> record .typedGet (field , fieldType );
128
130
}
129
131
}
132
+
133
+ private static Type getSuperType (Set <Type > types , Type type ) {
134
+ return types .stream ().filter (t -> t .getSubType () == type ).findFirst ().orElse (Type .UNKNOWN );
135
+ }
136
+
137
+ private static Type getSuperSuperType (Set <Type > types , Type type ) {
138
+ return types .stream ().filter (t -> t .getSubType ().getSubType () == type ).findFirst ().orElse (Type .UNKNOWN );
139
+ }
130
140
}
0 commit comments