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

Fix truncate behavior #16

Merged
merged 9 commits into from
Feb 16, 2024
Merged

Fix truncate behavior #16

merged 9 commits into from
Feb 16, 2024

Conversation

elefeint
Copy link
Contributor

Two fixes for truncate behavior:

  • hard deletes are not supported yet; all truncates have to be soft-delete
  • truncate should take into account the column / value for utc_delete_before (timestamp prior to which to delete rows).

Fixes #15
Fixes #9

@elefeint elefeint requested a review from Y-- February 14, 2024 16:57
std::unique_ptr<duckdb::Connection> con =
get_connection(request->configuration(), db_name);

if (table_exists(*con, table_name)) {
truncate_table(*con, table_name);
std::chrono::nanoseconds delete_before_ts =
std::chrono::seconds(request->utc_delete_before().seconds()) +
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't it be converted to ns?

Suggested change
std::chrono::seconds(request->utc_delete_before().seconds()) +
std::chrono::seconds(request->utc_delete_before().seconds()) * 1e9 +

But also, doesn't std::chrono::nanoseconds(request->utc_delete_before().nanos()) already give you what you need? Does it just give you the fractional seconds part?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Adding seconds to nanoseconds gets seconds converted automatically (docs)

When two duration objects of different types are involved, the one with the longest period (as determined by common_type) is converted before the operation.

nanos() only contain the fractional part -- proto docs.

Copy link
Contributor

Choose a reason for hiding this comment

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

A bit too magical for me, but I guess I'm not on the C++ design committee :-)
You got to love this radically opposite approach between C++ stdlib that makes it as nice and expressive as possible vs the proto which complicate things and wants to split seconds and ns!

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 was actually grateful that for once C++ did not make me invent every basic utility from first principles, but yeah, it's not super intuitive either.
And I have no idea what proto designers were thinking here. I guess it's space saving for usecases where only second granularity is needed? But who uses second granularity anymore -- it's not 1999.

const std::string absolute_table_name = table.to_string();
std::ostringstream sql;
sql << "DELETE FROM " + absolute_table_name;

sql << "UPDATE " << absolute_table_name << " SET "
Copy link
Contributor

Choose a reason for hiding this comment

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

(sorry if I already asked that before...) we can't quote the table name?

Copy link
Contributor Author

@elefeint elefeint Feb 16, 2024

Choose a reason for hiding this comment

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

table_def::to_string() quotes all parts

Copy link
Contributor Author

Choose a reason for hiding this comment

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

perhaps I should rename it to to_quoted_string

Copy link
Contributor Author

Choose a reason for hiding this comment

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

renamed to to_escaped_string

@elefeint elefeint requested a review from Y-- February 16, 2024 16:35
Copy link
Contributor

@Y-- Y-- left a comment

Choose a reason for hiding this comment

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

LGTM!

@elefeint elefeint merged commit 2d87cde into main Feb 16, 2024
1 check passed
@elefeint elefeint deleted the fix_truncate branch February 16, 2024 16:51
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.

Truncate has to take utc_delete_before into account Update delete logic to be soft-delete
2 participants