2727import com .google .protobuf .Descriptors .FieldDescriptor .JavaType ;
2828import com .google .protobuf .DynamicMessage ;
2929import org .apache .commons .lang3 .StringUtils ;
30+ import org .apache .commons .lang3 .math .NumberUtils ;
3031import org .apache .flink .table .data .GenericArrayData ;
3132import org .apache .flink .table .data .GenericMapData ;
3233import org .apache .flink .table .data .GenericRowData ;
3536import org .slf4j .LoggerFactory ;
3637
3738import java .nio .charset .Charset ;
39+ import java .nio .charset .StandardCharsets ;
3840import java .util .ArrayList ;
3941import java .util .HashMap ;
42+ import java .util .IdentityHashMap ;
4043import java .util .List ;
4144import java .util .Map ;
4245import java .util .Objects ;
@@ -68,8 +71,8 @@ public class PbSourceData extends AbstractSourceData {
6871
6972 protected Charset srcCharset ;
7073
71- private Map <DynamicMessage , Map <String , Object >> nodeValueCache = new HashMap <>();
72- private Map <DynamicMessage , Map <String , Map <Object , Object >>> mapNodeCache = new HashMap <>();
74+ private Map <DynamicMessage , Map <String , Object >> nodeValueCache = new IdentityHashMap <>();
75+ private Map <DynamicMessage , Map <String , Map <Object , Object >>> mapNodeCache = new IdentityHashMap <>();
7376
7477 /**
7578 * Constructor
@@ -158,7 +161,10 @@ public Object getField(int rowNum, String fieldName) {
158161 List <Object > valueList = (List ) fieldValue ;
159162 List <Object > result = new ArrayList <>(valueList .size ());
160163 for (Object value : valueList ) {
161- result .add (this .buildFieldValue (lastNode .getFieldDesc (), value ));
164+ Object itemValue = this .buildFieldValue (lastNode .getFieldDesc (), value );
165+ if (itemValue != null ) {
166+ result .add (itemValue );
167+ }
162168 }
163169 return new GenericArrayData (result .toArray ());
164170 }
@@ -184,28 +190,56 @@ public Object buildFieldValue(FieldDescriptor fieldDesc, Object nodeValue) {
184190 }
185191 switch (fieldDesc .getJavaType ()) {
186192 case STRING :
193+ if (nodeValue instanceof BinaryStringData ) {
194+ return nodeValue ;
195+ }
196+ if (nodeValue instanceof String ) {
197+ return new BinaryStringData ((String ) nodeValue );
198+ }
199+ return new BinaryStringData (String .valueOf (nodeValue ));
187200 case INT :
201+ if (nodeValue instanceof Integer ) {
202+ return nodeValue ;
203+ }
204+ return NumberUtils .toInt (String .valueOf (nodeValue ), 0 );
188205 case LONG :
206+ if (nodeValue instanceof Long ) {
207+ return nodeValue ;
208+ }
209+ return NumberUtils .toLong (String .valueOf (nodeValue ), 0 );
189210 case FLOAT :
211+ if (nodeValue instanceof Float ) {
212+ return nodeValue ;
213+ }
214+ return NumberUtils .toFloat (String .valueOf (nodeValue ), 0 );
190215 case DOUBLE :
216+ if (nodeValue instanceof Double ) {
217+ return nodeValue ;
218+ }
219+ return NumberUtils .toDouble (String .valueOf (nodeValue ), 0 );
191220 case BOOLEAN :
192- return nodeValue ;
221+ if (nodeValue instanceof Boolean ) {
222+ return nodeValue ;
223+ }
224+ return Boolean .TRUE .toString ().equals (String .valueOf (nodeValue ));
193225 case ENUM :
194226 if (nodeValue instanceof EnumValueDescriptor ) {
195227 EnumValueDescriptor enumDesc = (EnumValueDescriptor ) nodeValue ;
196228 return enumDesc .getIndex ();
197229 }
198230 return null ;
199231 case BYTE_STRING :
200- if (nodeValue instanceof ByteString ) {
232+ if (nodeValue instanceof byte []) {
233+ return nodeValue ;
234+ } else if (nodeValue instanceof ByteString ) {
201235 return ((ByteString ) nodeValue ).toByteArray ();
202236 } else {
203- return nodeValue ;
237+ return String . valueOf ( nodeValue ). getBytes ( StandardCharsets . ISO_8859_1 ) ;
204238 }
205239 case MESSAGE :
206240 return this .buildStructData (fieldDesc .getMessageType (), nodeValue );
207241 default :
208- return String . valueOf ( nodeValue ) ;
242+ return nodeValue ;
209243 }
210244 }
211245
@@ -269,7 +303,9 @@ protected Object buildMapData(Descriptors.Descriptor messageType, Object nodeVal
269303 DynamicMessage subnodeValue = (DynamicMessage ) value ;
270304 Object keyValue = buildFieldValue (keyField , subnodeValue .getField (keyField ));
271305 Object valueValue = buildFieldValue (valueField , subnodeValue .getField (valueField ));
272- result .put (keyValue , valueValue );
306+ if (keyValue != null && valueValue != null ) {
307+ result .put (keyValue , valueValue );
308+ }
273309 }
274310 return new GenericMapData (result );
275311 }
@@ -307,7 +343,7 @@ public List<DynamicMessage> getChildRoot() {
307343 }
308344
309345 public Object findFieldNode (int rowNum , String fieldName ) {
310- Object fieldValue = "" ;
346+ Object fieldValue = null ;
311347 try {
312348 if (StringUtils .startsWith (fieldName , ROOT_KEY )) {
313349 fieldValue = this .findRootField (fieldName );
@@ -326,7 +362,7 @@ public Object findFieldNode(int rowNum, String fieldName) {
326362 }
327363 // error config
328364 if (childNodes .size () == 0 ) {
329- return "" ;
365+ return null ;
330366 }
331367 // parse other node
332368 fieldValue = this .findNodeValueByCache (childNodes , root );
@@ -377,7 +413,7 @@ private Object findRootField(String srcFieldName) {
377413
378414 private Object findChildField (int rowNum , String srcFieldName ) {
379415 if (this .childRoot == null || this .childDesc == null ) {
380- return "" ;
416+ return null ;
381417 }
382418 List <PbNode > childNodes = this .columnNodeMap .get (srcFieldName );
383419 if (childNodes == null ) {
@@ -390,7 +426,7 @@ private Object findChildField(int rowNum, String srcFieldName) {
390426 }
391427 // error config
392428 if (childNodes .size () == 0 ) {
393- return "" ;
429+ return null ;
394430 }
395431 // parse other node
396432 DynamicMessage child = childRoot .get (rowNum );
@@ -423,7 +459,47 @@ public Object findNodeValueByCache(List<PbNode> childNodes, DynamicMessage root)
423459 subNodeValue = subNodeValueCache .get (node .getCurrentPath ());
424460 if (subNodeValue != null ) {
425461 if (i == childNodes .size () - 1 ) {
426- return subNodeValue ;
462+ // primitive
463+ if (node .isPrimitiveType ()) {
464+ return subNodeValue ;
465+ }
466+ // struct
467+ if (node .isStructType ()) {
468+ return subNodeValue ;
469+ }
470+ // array
471+ if (node .isArrayType ()) {
472+ if (!node .isHasArrayIndex ()) {
473+ return subNodeValue ;
474+ }
475+ if (!(subNodeValue instanceof List )) {
476+ return null ;
477+ }
478+ List <?> nodeValueList = (List <?>) subNodeValue ;
479+ if (node .getArrayIndex () >= nodeValueList .size ()) {
480+ return null ;
481+ }
482+ Object newNode = nodeValueList .get (node .getArrayIndex ());
483+ if (!(newNode instanceof DynamicMessage )) {
484+ return null ;
485+ }
486+ return newNode ;
487+ }
488+ // map
489+ if (node .isMapType ()) {
490+ if (!node .isHasMapKey ()) {
491+ return subNodeValue ;
492+ }
493+ final Object mapNodeValue = subNodeValue ;
494+ Map <Object , Object > mapCache = subMapNodeCache .computeIfAbsent (node .getCurrentPath (),
495+ k -> parseMapNode (mapNodeValue , node ));
496+ Object fieldValue = mapCache .get (node .getMapKey ());
497+ if (fieldValue == null || !(fieldValue instanceof DynamicMessage )) {
498+ return null ;
499+ }
500+ return fieldValue ;
501+ }
502+ return null ;
427503 } else {
428504 // primitive
429505 if (node .isPrimitiveType ()) {
0 commit comments