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(c/driver/postgresql): TimestampTz write #868

Merged
merged 8 commits into from
Jul 10, 2023

Conversation

WillAyd
Copy link
Contributor

@WillAyd WillAyd commented Jun 30, 2023

closes #867

The lifecycle of setting the session time zone in this isn't great. I think should be handled during connect somehow? But not sure if postgres even supports that reading things like:

https://stackoverflow.com/a/11779621/621736

I think there is also something awry with the release callback for timezone schemas; needs further investigation

@@ -1025,6 +1025,18 @@ AdbcStatusCode PostgresConnection::SetOption(const char* key, const char* value,
}
return ADBC_STATUS_OK;
}

if (std::strcmp(key, "TIME ZONE") == 0) {
std::string query = std::string("SET TIME ZONE '") + std::string(value) + "'";
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Could also set / unset this in the lifecycle of the statement

Copy link
Member

Choose a reason for hiding this comment

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

If we do want an option for this, it should be something like adbc.postgresql.time_zone based on other drivers

But if we just need it for ingestion, maybe we could do everything inside a transaction, and set/restore the timezone within the transaction?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yea either of those are viable; I think better to start with the latter, though I'm not sure where to manage that lifecycle. Is the BindStream struct in statement.cc supposed to be a suitable home for managing a transaction? The type introspection happens there, but I don't know that is the best place to put BEGIN/COMMIT statements.

Copy link
Member

Choose a reason for hiding this comment

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

Hmm, I think for this case it might be, but we probably are going to need some more intensive refactoring at some point

@WillAyd WillAyd marked this pull request as ready for review July 6, 2023 20:02
@WillAyd
Copy link
Contributor Author

WillAyd commented Jul 6, 2023

Think the polars issue is unrelated

Comment on lines +148 to +149
bool has_tz_field = false;
std::string tz_setting;
Copy link
Member

Choose a reason for hiding this comment

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

agh, I was going to ask for this to be std::optional...but we can't do that

(strcmp("", bind_schema_fields[col].timezone))) {
has_tz_field = true;

PGresult* begin_result = PQexec(conn, "BEGIN");
Copy link
Member

Choose a reason for hiding this comment

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

Don't we need to hoist this out of the loop? Or else, only do this if has_tz_field was previously false. Otherwise, it looks like we'll try to begin the transaction twice.

(Also: I wonder if we shouldn't always begin/end a transaction when dealing with multiple bind parameters...)

Copy link
Member

Choose a reason for hiding this comment

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

+how does this interact with auto-commit off?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have the break at the end of the loop to make sure this only happens once, but that could be more clearly signaled. Could even just set has_tz_field within the loop then check if (has_tz_field) right after before potentially starting a transaction

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As far as autocommit goes I do think you are right about that being murky. I'm not sure how to best resolve that yet either - beginning / ending the transaction here within the BindStream class is probably at odds with most of the autocommit stuff being managed via the connection.

Are there tests already for autocommit in the ADBC suite? I was looking for a reference on how those are expected to behave. Wasn't sure if I was overlooking those of if it is something that hasn't been heavily implemented yet

Copy link
Member

Choose a reason for hiding this comment

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

ah, I missed that little break at the end.

There are some simple tests that test the options, but not really the semantics.

At least here, I think you 'just' need to check if autocommit is off, if so, there's no need to BEGIN (you're already in a transaction), and you can still commit at the end (~though maybe semantically we shouldn't? This isn't a feature that's normally available in other APIs so I'm not sure what people expect)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Any ideas on how to best forward the autocommit setting to the BindStream class? Right now I only see that as a private member of the connection, so it is pretty far removed from this

Copy link
Member

Choose a reason for hiding this comment

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

A statement already holds a connection, so we can just add a getter to the PostgresConnection class

c/driver/postgresql/connection.h Outdated Show resolved Hide resolved
@lidavidm lidavidm merged commit 4b81d77 into apache:main Jul 10, 2023
65 checks passed
@lidavidm lidavidm added this to the ADBC Libraries 0.6.0 milestone Jul 10, 2023
@WillAyd WillAyd deleted the postgres-tz branch June 28, 2024 14:56
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.

c/driver/postgresql: Timezone Support
2 participants