Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: avoid allocating ADBC inputs and outputs twice #97

Merged
merged 23 commits into from
Jun 23, 2024
Merged

Conversation

cocoa-xu
Copy link
Member

@cocoa-xu cocoa-xu commented Jun 22, 2024

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 cocoa-xu requested a review from josevalim June 22, 2024 23:09
@cocoa-xu
Copy link
Member Author

cocoa-xu commented Jun 23, 2024

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
Copy link
Member

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

lib/adbc_column.ex Outdated Show resolved Hide resolved
lib/adbc_connection.ex Outdated Show resolved Hide resolved
@cocoa-xu
Copy link
Member Author

@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
Copy link
Member Author

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.

lib/adbc_connection.ex Outdated Show resolved Hide resolved
Copy link
Member

@josevalim josevalim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great! One comment to migrate to a list of refs and we can ship it!!

@cocoa-xu
Copy link
Member Author

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

@cocoa-xu cocoa-xu merged commit 10f74c8 into main Jun 23, 2024
3 checks passed
@cocoa-xu cocoa-xu deleted the cx-data-ref branch June 23, 2024 18:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants