- 
          
 - 
                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
          
     Closed
      
      
    
      
        
          +177
        
        
          −2
        
        
          
        
      
    
  
  
     Closed
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            16 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      9495265
              
                Implement AS OF SYSTEM TIME feature for CockroachDB with correspondin…
              
              
                mackinleysmith afc5aac
              
                Remove dialect check for AS OF SYSTEM TIME in CockroachDB implementation
              
              
                mackinleysmith 829ccab
              
                Refactor AS OF SYSTEM TIME formatting to use fmt.Sprintf for timestam…
              
              
                mackinleysmith 8dfa319
              
                Update AS OF SYSTEM TIME clause to standardize raw SQL expression for…
              
              
                mackinleysmith bbe4f91
              
                Merge branch 'master' into master
              
              
                mackinleysmith a3fac44
              
                avoid copying structures with embedded mutexs (#7571)
              
              
                drakkan 8dbd45e
              
                fix(generics): resolve CurrentTable in Raw/Exec
              
              
                jinzhu 94811fa
              
                fix slogLogger to support ParameterizedQueries Config (#7574)
              
              
                EricWvi 08124fa
              
                fix: build failure on Go versions below 1.21, add build constraint fo…
              
              
                iTanken 1f011fb
              
                Add Set-based Create and Update support to Generics API (#7578)
              
              
                jinzhu 9754050
              
                Set accepts Assigner for Generics API
              
              
                jinzhu 539e048
              
                Add AsOfSystemTimeNow method to retrieve the most recent data without…
              
              
                mackinleysmith 5c19e7c
              
                Merge branch 'master' into master
              
              
                mackinleysmith 5251385
              
                feat: add Count method to CreateInterface for row counting functionality
              
              
                mackinleysmith 5e266dd
              
                fix: sanitize input for AsOfSystemTime clause and update test cases
              
              
                mackinleysmith 55942c6
              
                fix: correct formatting in AsOfSystemTimeNow method for SQL compatibi…
              
              
                mackinleysmith File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package clause | ||
| 
     | 
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
| "time" | ||
| ) | ||
| 
     | 
||
| // AsOfSystemTime represents CockroachDB's "AS OF SYSTEM TIME" clause | ||
| // This allows querying data as it existed at a specific point in time | ||
| type AsOfSystemTime struct { | ||
| Timestamp time.Time | ||
| Raw string // For raw SQL expressions like "AS OF SYSTEM TIME '-1h'" | ||
| } | ||
| 
     | 
||
| // Name returns the clause name | ||
| func (a AsOfSystemTime) Name() string { | ||
| return "AS OF SYSTEM TIME" | ||
| } | ||
| 
     | 
||
| // Build builds the "AS OF SYSTEM TIME" clause | ||
| func (a AsOfSystemTime) Build(builder Builder) { | ||
| if a.Raw != "" { | ||
| sanitizedRaw := strings.ReplaceAll(a.Raw, ";", "") | ||
| builder.WriteString(fmt.Sprintf("AS OF SYSTEM TIME %s", sanitizedRaw)) | ||
| } else if !a.Timestamp.IsZero() { | ||
| builder.WriteString(fmt.Sprintf("AS OF SYSTEM TIME '%s'", a.Timestamp.Format("2006-01-02 15:04:05.000000"))) | ||
                
      
                  mackinleysmith marked this conversation as resolved.
               
          
            Show resolved
            Hide resolved
         | 
||
| } | ||
| } | ||
| 
     | 
||
| // MergeClause merges the "AS OF SYSTEM TIME" clause | ||
| func (a AsOfSystemTime) MergeClause(clause *Clause) { | ||
| clause.Expression = a | ||
| } | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| package clause_test | ||
| 
     | 
||
| import ( | ||
| "fmt" | ||
| "testing" | ||
| "time" | ||
| 
     | 
||
| "gorm.io/gorm/clause" | ||
| ) | ||
| 
     | 
||
| func TestAsOfSystemTime(t *testing.T) { | ||
| results := []struct { | ||
| Clauses []clause.Interface | ||
| Result string | ||
| Vars []interface{} | ||
| }{ | ||
| { | ||
| []clause.Interface{ | ||
| clause.Select{}, | ||
| clause.From{ | ||
| Tables: []clause.Table{{Name: "users"}}, | ||
| AsOfSystemTime: &clause.AsOfSystemTime{Timestamp: time.Date(2023, 1, 1, 12, 0, 0, 0, time.UTC)}, | ||
| }, | ||
| }, | ||
| "SELECT * FROM `users` AS OF SYSTEM TIME '2023-01-01 12:00:00.000000'", | ||
| nil, | ||
| }, | ||
| { | ||
| []clause.Interface{ | ||
| clause.Select{}, | ||
| clause.From{ | ||
| Tables: []clause.Table{{Name: "users"}}, | ||
| AsOfSystemTime: &clause.AsOfSystemTime{Raw: "'-1h'"}, | ||
| }, | ||
| }, | ||
| "SELECT * FROM `users` AS OF SYSTEM TIME '-1h'", | ||
| nil, | ||
| }, | ||
| { | ||
| []clause.Interface{ | ||
| clause.Select{}, | ||
| clause.From{ | ||
| Tables: []clause.Table{{Name: "users"}}, | ||
| Joins: []clause.Join{ | ||
| { | ||
| Type: clause.InnerJoin, | ||
| Table: clause.Table{Name: "companies"}, | ||
| ON: clause.Where{ | ||
| []clause.Expression{clause.Eq{clause.Column{Table: "companies", Name: "id"}, clause.Column{Table: "users", Name: "company_id"}}}, | ||
| }, | ||
| }, | ||
| }, | ||
| AsOfSystemTime: &clause.AsOfSystemTime{Raw: "'-1h'"}, | ||
| }, | ||
| }, | ||
| "SELECT * FROM `users` INNER JOIN `companies` ON `companies`.`id` = `users`.`company_id` AS OF SYSTEM TIME '-1h'", | ||
| nil, | ||
| }, | ||
| } | ||
| 
     | 
||
| for idx, result := range results { | ||
| t.Run(fmt.Sprintf("case #%v", idx), func(t *testing.T) { | ||
| checkBuildClauses(t, result.Clauses, result.Result, result.Vars) | ||
| }) | ||
| } | ||
| } | ||
| 
     | 
||
| func TestAsOfSystemTimeName(t *testing.T) { | ||
| asOfClause := clause.AsOfSystemTime{} | ||
| if asOfClause.Name() != "AS OF SYSTEM TIME" { | ||
| t.Errorf("expected name 'AS OF SYSTEM TIME', got %q", asOfClause.Name()) | ||
| } | ||
| } | ||
| 
     | 
||
| func TestAsOfSystemTimeMergeClause(t *testing.T) { | ||
| asOfClause := clause.AsOfSystemTime{Timestamp: time.Now()} | ||
| var c clause.Clause | ||
| asOfClause.MergeClause(&c) | ||
| 
     | 
||
| if c.Expression != asOfClause { | ||
| t.Error("expected expression to be set to the clause") | ||
| } | ||
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.