-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Add AS OF SYSTEM TIME support for CockroachDB temporal queries #7566
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
Conversation
|
Add CockroachDB AS OF SYSTEM TIME Temporal Query Support in GORM This PR introduces explicit support for CockroachDB's Key Changes• Added new file Affected Areas• This summary was automatically generated by @propel-code-bot |
…p output in clause/as_of_system_time.go
…matting in chainable_api.go and related tests
|
@jinzhu who should i speak with about getting this merged in? Is there anything I can do to be helpful in that process? |
Fixes warning like this: assignment copies lock value to relationships: gorm.io/gorm/schema.Relationships contains sync.RWMutex
… retries This new method allows users to query data as it exists at the time of the query, specifically optimized for CockroachDB. It sets the system time to be as close to now as possible, enhancing data retrieval efficiency.
This commit modifies the AsOfSystemTime clause to sanitize the input by removing semicolons, ensuring safer SQL generation. Additionally, the test cases have been updated to reflect the correct formatting of the AS OF SYSTEM TIME string.
…lity This commit updates the AsOfSystemTimeNow method to ensure the timestamp string is properly formatted with quotes, enhancing compatibility with SQL syntax in CockroachDB.
|
Hi @mackinleysmith, Thank you for your PR! I believe a better approach would be to add an AsOfSystemTime method in the CockroachDB driver. This method could return a clause.Builder, similar to how clause.From is implemented. The driver would then be responsible for incorporating this clause into the final SQL during statement building. You can refer to this implementation as an example: https://github.com/go-gorm/postgres/blob/c65a34eaf8229cd55f5a0febaf69c30250be3c2a/postgres.go#L85 Let me know what you think! |
|
@jinzhu as far as I understand, CockroachDB does not have its own driver for Gorm. It seems the recommended approach is to use the postgres driver, since Cockroach implements the postgres dialect. Please let me know if I'm missing something -- I'd be glad to take a stab at it if I am. |
|
I think it would be more reasonable to create a dedicated GORM CockroachDB driver based on the PostgreSQL driver. |
CockroachDB AS OF SYSTEM TIME Support
This pull request implements support for CockroachDB's "AS OF SYSTEM TIME" feature in GORM.
Docs can be found on Cockroach's website at: https://www.cockroachlabs.com/docs/stable/as-of-system-time.
Overview
The "AS OF SYSTEM TIME" feature allows you to query data as it existed at a specific point in time, which is useful for:
Usage
Basic Usage
Chaining with Other Methods
Supported Time Formats
The method accepts two types of input:
Time strings (raw SQL expressions):
Go time.Time values:
Database Support
This feature is only supported by CockroachDB. If you try to use it with other databases, GORM will return an error.
Generated SQL
The feature generates SQL like this:
Important: The "AS OF SYSTEM TIME" clause is always positioned at the end of the FROM clause, after any JOINs. This is the correct SQL syntax for CockroachDB.
Error Handling
The method will return an error if:
Implementation Details
The feature is implemented through:
AsOfSystemTimeclause type in theclausepackageFROMclauseAsOfSystemTime()method in the chainable APIExamples
Complete Example
Notes