@@ -252,6 +252,14 @@ public static int resolveUnionIndex(Schema unionSchema, Object datum) {
252
252
if (raw == CLS_STRING ) {
253
253
return _resolveStringIndex (unionSchema , types , (String ) datum );
254
254
}
255
+ // 26-Apr-2017, tatu: This may look odd optimization, but turns out that:
256
+ // (a) case of "null and ONE other type" is VERY common, and
257
+ // (b) cost of real lookup for POJO types is VERY expensive (due to elaborate
258
+ // caching Avro lib does
259
+ int ix = _findNotNullIndex (types );
260
+ if (ix >= 0 ) {
261
+ return ix ;
262
+ }
255
263
if (raw == CLS_BIG_DECIMAL ) {
256
264
return _resolveBigDecimalIndex (unionSchema , types , (BigDecimal ) datum );
257
265
}
@@ -280,7 +288,7 @@ public static int resolveUnionIndex(Schema unionSchema, Object datum) {
280
288
}
281
289
*/
282
290
}
283
- //System.out .println("Union type for: "+datum.getClass());
291
+ //System.err .println("Missing index for: "+datum.getClass().getName()+" ("+types.size()+") ->\n"+types);
284
292
return ReflectData .get ().resolveUnion (unionSchema , datum );
285
293
}
286
294
@@ -299,6 +307,14 @@ public static Schema resolveUnionType(Schema unionSchema, Object datum) {
299
307
if (raw == CLS_STRING ) {
300
308
return types .get (_resolveStringIndex (unionSchema , types , (String ) datum ));
301
309
}
310
+ // 26-Apr-2017, tatu: This may look odd optimization, but turns out that:
311
+ // (a) case of "null and ONE other type" is VERY common, and
312
+ // (b) cost of real lookup for POJO types is VERY expensive (due to elaborate
313
+ // caching Avro lib does
314
+ Schema sch = _findNotNull (types );
315
+ if (sch != null ) {
316
+ return sch ;
317
+ }
302
318
if (raw == CLS_BIG_DECIMAL ) {
303
319
return types .get (_resolveBigDecimalIndex (unionSchema , types , (BigDecimal ) datum ));
304
320
}
@@ -323,6 +339,7 @@ public static Schema resolveUnionType(Schema unionSchema, Object datum) {
323
339
}
324
340
*/
325
341
}
342
+ //System.err.println("Missing schema for: "+datum.getClass().getName()+" ("+types.size()+") ->\n"+types);
326
343
int ix = ReflectData .get ().resolveUnion (unionSchema , datum );
327
344
return types .get (ix );
328
345
}
@@ -359,6 +376,32 @@ private static int _resolveStringIndex(Schema unionSchema, List<Schema> types,
359
376
return ReflectData .get ().resolveUnion (unionSchema , value );
360
377
}
361
378
379
+ private static Schema _findNotNull (List <Schema > types )
380
+ {
381
+ if (types .size () == 2 ) {
382
+ if (types .get (0 ).getType () == Type .NULL ) {
383
+ return types .get (1 );
384
+ }
385
+ if (types .get (1 ).getType () == Type .NULL ) {
386
+ return types .get (0 );
387
+ }
388
+ }
389
+ return null ;
390
+ }
391
+
392
+ private static int _findNotNullIndex (List <Schema > types )
393
+ {
394
+ if (types .size () == 2 ) {
395
+ if (types .get (0 ).getType () == Type .NULL ) {
396
+ return 1 ;
397
+ }
398
+ if (types .get (1 ).getType () == Type .NULL ) {
399
+ return 0 ;
400
+ }
401
+ }
402
+ return -1 ;
403
+ }
404
+
362
405
private static int _resolveBigDecimalIndex (Schema unionSchema , List <Schema > types ,
363
406
BigDecimal value ) {
364
407
int match = -1 ;
0 commit comments