Uploaded image for project: 'Apache Arrow'
  1. Apache Arrow
  2. ARROW-17430

[Java] ListBinder to bind Arrow List type to DB column

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • 10.0.0
    • 10.0.0
    • Java

    Description

      Typical real life Arrow datasets contain List type vectors of primitive type. Looks logically implement default JDBC parameter binders for such vector type or implement some kind on binder code extensibility of core framework via MetaINF/ServiceLocator

      Current implementation just throws UnsupportedOperationException]

       

        @Override
              
              
                  public ColumnBinder visit(ArrowType.List type) {
              
              
                    throw new UnsupportedOperationException("No column binder implemented for type " + type);
              
              
                  } 

       

      My current implementation patch ColumnBinderArrowTypeVisitor in classpath (to leverage Builder functionality instead of manual vector mapping in code) to return ListBinder

      @Override
      public ColumnBinder visit(ArrowType.List type) {
          return new ListBinder((ListVector) vector);
      } 

      and following code works for me with H2 database and in java stored PostgreSQL function in PL/Java to bind List parameter to JDBC:

      package org.apache.arrow.adapter.jdbc.binder;
      
      import org.apache.arrow.vector.complex.ListVector;
      import org.apache.arrow.vector.complex.impl.UnionListReader;
      
      import java.sql.PreparedStatement;
      import java.sql.SQLException;
      import java.sql.Types;
      import java.util.ArrayList;
      import java.util.Arrays;
      
      public class ListBinder extends BaseColumnBinder<ListVector>{
      
          private UnionListReader reader;
      
          public ListBinder(ListVector vector) {
              this(vector, Types.ARRAY);
          }
      
          public ListBinder(ListVector vector, int jdbcType) {
              super(vector, jdbcType);
              reader = vector.getReader();
          }
      
          @Override
          public void bind(PreparedStatement statement, int parameterIndex, int rowIndex) throws SQLException {
              reader.setPosition(rowIndex);
              ArrayList sourceArray = (ArrayList) reader.readObject();
              Class aClass = sourceArray.get(0).getClass();
              if(aClass.isAssignableFrom(Long.class)){
                  Long[] res = new Long[sourceArray.size()];
                  Arrays.setAll(res, sourceArray::get);
                  statement.setObject(parameterIndex, res);
              } else
              if(aClass.isAssignableFrom(Integer.class)){
                  Integer[] res = new Integer[sourceArray.size()];
                  Arrays.setAll(res, sourceArray::get);
                  statement.setObject(parameterIndex, res);
              } else
              if(aClass.isAssignableFrom(Short.class)){
                  Short[] res = new Short[sourceArray.size()];
                  Arrays.setAll(res, sourceArray::get);
                  statement.setObject(parameterIndex, res);
              } else
              if(aClass.isAssignableFrom(String.class)){
                  String[] res = new String[sourceArray.size()];
                  Arrays.setAll(res, sourceArray::get);
                  statement.setObject(parameterIndex, res);
              }
          }
      }
       

      Attachments

        Issue Links

          Activity

            People

              igor.suhorukov Igor Suhorukov
              igor.suhorukov Igor Suhorukov
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0h
                  0h
                  Logged:
                  Time Spent - 2.5h
                  2.5h