Solr
  1. Solr
  2. SOLR-3395

FieldStreamDataSource should handle null fields

    Details

    • Type: Improvement Improvement
    • Status: Open
    • Priority: Minor Minor
    • Resolution: Unresolved
    • Affects Version/s: 3.5
    • Fix Version/s: None
    • Labels:
      None

      Description

      The FieldStreamDataSource currently throws a DataImportHandlerException if a field value is null.
      IMHO this is not appropriate: It is legal for field values to be null (like no Blob exists in a particular row).
      I suggest to return an empty InputStream rather than throwing a DataImportHandlerException.

        public InputStream getData(String query) {
          Object o = wrapper.getVariableResolver().resolve(dataField);
          if (o == null) {
      //better: return new ByteArrayInputStream(new byte[0]);
            throw new DataImportHandlerException(SEVERE, "No field available for name : " + dataField);
          }
          if (o instanceof Blob) {
            Blob blob = (Blob) o;
            try {
              //Most of the JDBC drivers have getBinaryStream defined as public
              // so let us just check it
              Method m = blob.getClass().getDeclaredMethod("getBinaryStream");
              if (Modifier.isPublic(m.getModifiers())) {
                return (InputStream) m.invoke(blob);
              } else {
                // force invoke
                m.setAccessible(true);
                return (InputStream) m.invoke(blob);
              }
            } catch (Exception e) {
              LOG.info("Unable to get data from BLOB");
              return null;
      
            }
          } else if (o instanceof byte[]) {
            byte[] bytes = (byte[]) o;
            return new ByteArrayInputStream(bytes);
          } else {
            throw new RuntimeException("unsupported type : " + o.getClass());
          } 
      
        }
      

      Maybe the best implementation would be to distinguish between a none-existing field and a null or empty field.

        Activity

        Andreas W created issue -
        Andreas W made changes -
        Field Original Value New Value
        Description The {{FieldStreamDataSource}} currently throws a {{DataImportHandlerException}} if a field value is null.
        IMHO this is not appropriate: It is legal for field values to be null (like no {{Blob}} exists in a particular row).
        I suggest to return an empty InputStream rather than throwing a {{DataImportHandlerException}}.

        {code}
          public InputStream getData(String query) {
            Object o = wrapper.getVariableResolver().resolve(dataField);
            if (o == null) {
        //better: return new ByteArrayInputStream(new byte[0]);
              throw new DataImportHandlerException(SEVERE, "No field available for name : " + dataField);
            }
            if (o instanceof Blob) {
              Blob blob = (Blob) o;
              try {
                //Most of the JDBC drivers have getBinaryStream defined as public
                // so let us just check it
                Method m = blob.getClass().getDeclaredMethod("getBinaryStream");
                if (Modifier.isPublic(m.getModifiers())) {
                  return (InputStream) m.invoke(blob);
                } else {
                  // force invoke
                  m.setAccessible(true);
                  return (InputStream) m.invoke(blob);
                }
              } catch (Exception e) {
                LOG.info("Unable to get data from BLOB");
                return null;

              }
            } else if (o instanceof byte[]) {
              byte[] bytes = (byte[]) o;
              return new ByteArrayInputStream(bytes);
            } else {
              throw new RuntimeException("unsupported type : " + o.getClass());
            }

          }
        {code}
        The {{FieldStreamDataSource}} currently throws a {{DataImportHandlerException}} if a field value is null.
        IMHO this is not appropriate: It is legal for field values to be null (like no {{Blob}} exists in a particular row).
        I suggest to return an empty InputStream rather than throwing a {{DataImportHandlerException}}.

        {code}
          public InputStream getData(String query) {
            Object o = wrapper.getVariableResolver().resolve(dataField);
            if (o == null) {
        //better: return new ByteArrayInputStream(new byte[0]);
              throw new DataImportHandlerException(SEVERE, "No field available for name : " + dataField);
            }
            if (o instanceof Blob) {
              Blob blob = (Blob) o;
              try {
                //Most of the JDBC drivers have getBinaryStream defined as public
                // so let us just check it
                Method m = blob.getClass().getDeclaredMethod("getBinaryStream");
                if (Modifier.isPublic(m.getModifiers())) {
                  return (InputStream) m.invoke(blob);
                } else {
                  // force invoke
                  m.setAccessible(true);
                  return (InputStream) m.invoke(blob);
                }
              } catch (Exception e) {
                LOG.info("Unable to get data from BLOB");
                return null;

              }
            } else if (o instanceof byte[]) {
              byte[] bytes = (byte[]) o;
              return new ByteArrayInputStream(bytes);
            } else {
              throw new RuntimeException("unsupported type : " + o.getClass());
            }

          }
        {code}

        Maybe the best implementation would be to distinguish between a none-existing field and a null or empty field.

          People

          • Assignee:
            Unassigned
            Reporter:
            Andreas W
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:

              Development