elixir-explorer / adbc

Apache Arrow ADBC bindings for Elixir
https://arrow.apache.org/adbc/
Apache License 2.0
50 stars 16 forks source link

feat: avoid allocating ADBC inputs and outputs twice #97

Closed cocoa-xu closed 4 months ago

cocoa-xu commented 4 months ago

This PR should allow us to use query results as inputs (query parameters) without allocating twice.

However, by the design of ADBC, the value of ArrowArray and ArrowSchema will be moved once used as a parameter. And there is no deep copy function for ArrowArray comes with the nanoarrow library.

It's possible to write one that does a deep copy of the ArrowArray but I'm not sure if that's what we wanted to do. But if so, I'll be happy to write one and send another PR for it.

Actually, a shallow copy + setting release to nullptr seems to be fine, and we can now reuse the results multiple times.

cocoa-xu commented 4 months ago

At the moment we have something like

Current version (up til 614e4a4)

{:ok,
  results = %Adbc.Result{
  data: %Adbc.Column{
    data: [#Reference<0.351247108.3006922760.20174>],
    name: "",
    type:
      {:struct,
        [
          %Adbc.Column{
            name: "num",
            type: :s64,
            metadata: nil,
            nullable: true
          }
        ]},
    metadata: nil,
    nullable: true
  },
  num_rows: nil
  }} = Connection.query(conn, "SELECT 123 as num")

Maybe it would be easier to use if the reference data is associated with the actual column:

Result with a single column (todo)

{:ok,
  results = %Adbc.Result{
  data: %Adbc.Column{
    data: [
      %Adbc.Column{
        name: "num",
        type: :s64,
        metadata: nil,
        nullable: true,
        data: #Reference<0.351247108.3006922760.20174>
      }
    ],
    name: "",
    type: :struct,
    metadata: nil,
    nullable: true
  },
  num_rows: nil
  }} = Connection.query(conn, "SELECT 123 as num")

Result with multiple columns (todo)

{:ok,
  results = %Adbc.Result{
  data: %Adbc.Column{
    data: [
      %Adbc.Column{
        name: "num",
        type: :s64,
        metadata: nil,
        nullable: true,
        data: #Reference<0.351247108.3006922760.20174>
      },
      %Adbc.Column{
        name: "fp",
        type: :f64,
        metadata: nil,
        nullable: true,
        data: #Reference<0.351247108.3006922760.20175>
      }
    ],
    name: "",
    type: :struct,
    metadata: nil,
    nullable: true
  },
  num_rows: nil
  }} = Connection.query(conn, "SELECT 123 as num, 456.78 as fp")

This probably looks better. WDYT? @josevalim

josevalim commented 4 months ago

@cocoa-xu what does ADBC return? Several columns or a struct with multiple entries?

cocoa-xu commented 4 months ago

@cocoa-xu what does ADBC return? Several columns or a struct with multiple entries?

It's the latter. The results will always be a struct at the top-level, and it contains all the columns.

cocoa-xu commented 4 months ago

Hrmmm, msvc doesn't like some code in arrow-adbc when compiling in debug mode

D:\a\adbc\adbc\3rd_party\apache-arrow-adbc\c\driver\framework\base_driver.cc(69) : error C2220: the following warning is treated as an error
D:\a\adbc\adbc\3rd_party\apache-arrow-adbc\c\driver\framework\base_driver.cc(57) : error C2220: the following warning is treated as an error
D:\a\adbc\adbc\3rd_party\apache-arrow-adbc\c\driver\framework\base_driver.cc(57) : error C2220: the following warning is treated as an error
D:\a\adbc\adbc\3rd_party\apache-arrow-adbc\c\driver\framework\base_driver.cc(57) : warning C4702: unreachable code
D:\a\adbc\adbc\3rd_party\apache-arrow-adbc\c\driver\framework\base_driver.cc(57) : warning C4702: unreachable code
D:\a\adbc\adbc\3rd_party\apache-arrow-adbc\c\driver\framework\base_driver.cc(69) : warning C4702: unreachable code
NMAKE : fatal error U1077: '"C:\Program Files\CMake\bin\cmake.exe" -E cmake_cl_compile_depends --dep-file=CMakeFiles\adbc_driver_framework.dir\base_driver.cc.obj.d --working-dir=D:\a\adbc\adbc\_build\test\lib\adbc\cmake_adbc\driver\framework --filter-prefix="Note: including file: " -- C:\PROGRA~1\MICROS~2\2022\ENTERP~1\VC\Tools\MSVC\1440~1.338\bin\Hostx64\x64\cl.exe @C:\Users\RUNNER~1\AppData\Local\Temp\nm9629.tmp' : return code '0x2'
Stop.
cocoa-xu commented 4 months ago

CI all green and looks great, I'll go ahead and merge it!