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

[C] Basic libpq-based driver #65

Merged
merged 1 commit into from
Aug 26, 2022
Merged

Conversation

lidavidm
Copy link
Member

The driver supports basic queries (int32 only) and toggling autocommit. It does not yet support bulk ingestion or prepared statements.

It hasn't been optimized for speed and the approach taken here will not be fast (it uses the per-row getters). In future PRs, we should set up some benchmarks and then see if DuckDB's approach makes more sense (use COPY). DuckDB also does multithreading (that might be hard for us). We may want to implement #61 first since then we will know whether it is safe to use COPY or not.

Copy link
Member

@paleolimbot paleolimbot left a comment

Choose a reason for hiding this comment

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

Just musing at ways I could make this easier to do!

}
if (schema_.release) {
schema_.release(&schema_);
schema_.release = nullptr;
Copy link
Member

Choose a reason for hiding this comment

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

This is redundant, no?

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed - thanks for catching that!


const int num_rows = PQntuples(result_);

NA_RETURN_NOT_OK(ArrowArrayInit(out, NANOARROW_TYPE_STRUCT));
Copy link
Member

Choose a reason for hiding this comment

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

I should probably add ArrowArrayInitFromSchema() (to match the recently added ArrowArrayViewInitFromSchema())

}

for (int col = 0; col < schema_.n_children; col++) {
NA_RETURN_NOT_OK(ArrowArrayFinishBuilding(out->children[col], 0));
Copy link
Member

Choose a reason for hiding this comment

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

After the last PR, calling ArrowArrayFinishBuilding() on the parent array will propagate to child arrays


std::vector<struct ArrowSchemaView> fields(schema_.n_children);

for (int col = 0; col < schema_.n_children; col++) {
Copy link
Member

Choose a reason for hiding this comment

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

I think I have enough general infrastructure now to make a generic ArrowArrayReserve() that seems like it would cut down a bit of verbosity here

@lidavidm
Copy link
Member Author

lidavidm commented Aug 23, 2022

Rebased/updated. This now tries to use the COPY command, taking advantage of the changes in #69.

Things that need to happen next:

  • Build a type mapping table on startup
  • Handle null values, more types
  • Work out some of the error handling details that were left as TODOs (and probably do some refactoring)
  • Handle bulk ingestion
  • Handle prepared statements
  • Add tests for queries that don't return result sets
  • Start adding benchmarks to help guide the rest of the design
  • Make sure concurrent statements don't interfere with each other/internal locking

result_ = nullptr;

struct ArrowError error;
// TODO: consistently release out on error (use another trampoline?)
Copy link
Member

Choose a reason for hiding this comment

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

This seems like it's going to be a common pattern...should nanoarrow provide a C++ header for this?

class ArrayHolder {
public:
  struct ArrowArray array;
  ArrayHolder() { array.release = nullptr; }
  ~ArrayHolder() { if (array.release) array.release(&array); }
}

Copy link
Member Author

Choose a reason for hiding this comment

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

Possibly, I was also going to file an issue/PR for stringifying type codes, etc. to use in error messages


std::vector<struct ArrowArrayView> views(fields.size());
for (size_t i = 0; i < views.size(); i++) {
ArrowArrayViewInit(&views[i], fields[i].data_type);
Copy link
Member

Choose a reason for hiding this comment

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

You may want to ArrowArrayViewInitFromSchema(&array_view, &schema) once on the top level and then do ArrowArrayViewSetArray(&array_view, &array) in the loop. (Then you can do array_view.children[i].stuff).

Copy link
Member

Choose a reason for hiding this comment

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

I think you're missing a ArrowArrayViewReset(), too (although it won't leak memory unless the array has children).

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, I also did a bit of templating and implemented the ArrayHolder above (slightly more generically)


schema.release(&schema);
bind_.release(&bind_);
bind_.release = nullptr;
Copy link
Member

Choose a reason for hiding this comment

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

Is this line necessary?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm mostly trying to be defensive here

Copy link
Member

Choose a reason for hiding this comment

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

I don't really mind either way, although I personally find it a bit confusing since it's a MUST in the spec that the release method has to take care of that.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, right. I'll update it then.

@lidavidm lidavidm force-pushed the lidavidm/libpq branch 2 times, most recently from e5c1751 to 9081851 Compare August 26, 2022 16:57
@lidavidm
Copy link
Member Author

I'm going to punt on most of the other tasks for now (see #81) so I can get this packaged in #57 before continuing to implement things.

@lidavidm lidavidm marked this pull request as ready for review August 26, 2022 17:07
@lidavidm lidavidm merged commit 35a9a22 into apache:main Aug 26, 2022
@lidavidm lidavidm deleted the lidavidm/libpq branch August 26, 2022 18:33
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.

None yet

2 participants