Skip to content

[refator]move all state type variables to arg.ctr in Operator - #16951

Merged
mergify[bot] merged 3 commits into
matrixorigin:mainfrom
huby2358:t3
Jun 21, 2024
Merged

mergify[bot] merged 3 commits into
matrixorigin:mainfrom
huby2358:t3

Conversation

@huby2358

@huby2358 huby2358 commented Jun 17, 2024 •

Copy link
Copy Markdown
Contributor

User description

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

issue ##16725

What this PR does / why we need it:

There are many variables in the operator that can be changed every time they are executed.
These variables need to be moved to specific places and cannot be placed outside, nor should they be capitalized


PR Type

Enhancement, Tests


Description

  • Refactored state variables in various join operations to use arg.ctr.
  • Updated lock operation tests to align with new state variable structure.
  • Moved state variables in generate series, deletion, and table scan operations to arg.ctr.
  • Updated operator functions to use WithCtrParams and WithCtrConfigs.
  • Refactored pre-insert logic to use arg.ctr.

Changes walkthrough 📝

Relevant files
Enhancement
14 files
join.go
Refactor state variables to `arg.ctr` in left join             

pkg/sql/colexec/left/join.go

  • Refactored state variables to arg.ctr in Call and related functions.
  • Updated batch handling logic to use arg.ctr.
  • +32/-32 
    generate_series.go
    Refactor generate series state variables to `arg.ctr`       

    pkg/sql/colexec/table_function/generate_series.go

  • Moved generateSeries state variables to arg.ctr.
  • Updated function logic to use arg.ctr for state management.
  • +46/-46 
    join.go
    Refactor state variables to `arg.ctr` in right join           

    pkg/sql/colexec/right/join.go

  • Refactored state variables to arg.ctr in Call and related functions.
  • Updated batch handling logic to use arg.ctr.
  • +25/-26 
    join.go
    Refactor state variables to `arg.ctr` in anti join             

    pkg/sql/colexec/anti/join.go

  • Refactored state variables to arg.ctr in Call and related functions.
  • Updated batch handling logic to use arg.ctr.
  • +28/-28 
    deletion.go
    Refactor deletion state variables to `arg.ctr`                     

    pkg/sql/colexec/deletion/deletion.go

  • Moved source and partitionSources to arg.ctr.
  • Updated deletion logic to use arg.ctr.
  • +25/-25 
    lock_op.go
    Refactor lock operation state variables to `arg.ctr`         

    pkg/sql/colexec/lockop/lock_op.go

  • Moved state variables to arg.ctr.
  • Updated lock operation logic to use arg.ctr.
  • +40/-40 
    join.go
    Refactor state variables to `arg.ctr` in join                       

    pkg/sql/colexec/join/join.go

  • Refactored state variables to arg.ctr in Call and related functions.
  • Updated batch handling logic to use arg.ctr.
  • +21/-21 
    operator.go
    Update operator functions to use `WithCtrParams` and `WithCtrConfigs`

    pkg/sql/compile/operator.go

  • Updated various functions to use WithCtrParams and WithCtrConfigs.
  • Refactored to align with new state variable structure.
  • +15/-13 
    join.go
    Refactor state variables to `arg.ctr` in loop anti join   

    pkg/sql/colexec/loopanti/join.go

  • Refactored state variables to arg.ctr in Call and related functions.
  • Updated batch handling logic to use arg.ctr.
  • +24/-24 
    join.go
    Refactor state variables to `arg.ctr` in right anti join 

    pkg/sql/colexec/rightanti/join.go

  • Refactored state variables to arg.ctr in Call and related functions.
  • Updated batch handling logic to use arg.ctr.
  • +17/-17 
    join.go
    Refactor state variables to `arg.ctr` in right semi join 

    pkg/sql/colexec/rightsemi/join.go

  • Refactored state variables to arg.ctr in Call and related functions.
  • Updated batch handling logic to use arg.ctr.
  • +16/-16 
    join.go
    Refactor state variables to `arg.ctr` in loop semi join   

    pkg/sql/colexec/loopsemi/join.go

  • Refactored state variables to arg.ctr in Call and related functions.
  • Updated batch handling logic to use arg.ctr.
  • +15/-15 
    table_scan.go
    Refactor table scan state variables to `arg.ctr`                 

    pkg/sql/colexec/table_scan/table_scan.go

  • Moved OrderBy and msgReceiver to arg.ctr.
  • Updated table scan logic to use arg.ctr.
  • +18/-18 
    preinsert.go
    Refactor pre-insert state variables to `arg.ctr`                 

    pkg/sql/colexec/preinsert/preinsert.go

  • Moved buffer handling to arg.ctr.
  • Updated pre-insert logic to use arg.ctr.
  • +16/-16 
    Tests
    1 files
    lock_op_test.go
    Refactor lock operation tests to use `arg.ctr`                     

    pkg/sql/colexec/lockop/lock_op_test.go

  • Updated test cases to use arg.ctr for state variables.
  • Refactored lock operation tests to align with new state variable
    structure.
  • +39/-39 

    💡 PR-Agent usage:
    Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    @matrix-meow matrix-meow added the size/L Denotes a PR that changes [500,999] lines label Jun 17, 2024
    @mergify mergify Bot added the kind/refactor Code refactor label Jun 17, 2024
    @qodo-code-review

    Copy link
    Copy Markdown

    PR-Agent was enabled for this repository. To continue using it, please link your git user with your CodiumAI identity here.

    PR Reviewer Guide 🔍

    ⏱️ Estimated effort to review [1-5] 4
    🧪 Relevant tests No
    🔒 Security concerns No
    ⚡ Key issues to review Possible Bug:
    The refactoring introduces a new structure arg.ctr to hold state variables. Ensure that all previous usages of the state variables are correctly replaced with arg.ctr. This includes checking that all methods and functions that previously accessed these variables directly are updated to access them through arg.ctr.
    Performance Concern:
    The change in how state variables are stored and accessed might impact performance. Benchmark tests should be conducted to compare the performance before and after the refactoring.
    Code Clarity:
    The introduction of arg.ctr could potentially improve code clarity by encapsulating state-related data. Review if the changes make the codebase easier to understand and maintain.

    @qodo-code-review

    Copy link
    Copy Markdown

    PR-Agent was enabled for this repository. To continue using it, please link your git user with your CodiumAI identity here.

    PR Code Suggestions ✨

    CategorySuggestion                                                                                                                                    Score
    Possible bug
    Add a nil check for arg.ctr.bat before accessing its Vecs field

    To avoid potential nil pointer dereference, add a check to ensure arg.ctr.bat is not nil
    before accessing its Vecs field.

    pkg/sql/colexec/left/join.go [172]

    +if ap.ctr.bat == nil {
    +    return fmt.Errorf("batch is nil")
    +}
     typ := *ap.ctr.bat.Vecs[rp.Pos].GetType()
     
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: The suggestion correctly identifies a potential nil pointer dereference which is a critical issue that could lead to runtime panics. Adding a nil check is crucial for robustness.

    8
    Add a nil check for arg.ctr.rt before accessing its fields to prevent nil pointer dereference errors

    Consider adding a check to ensure arg.ctr.rt is not nil before accessing its fields. This
    will prevent potential nil pointer dereference errors.

    pkg/sql/colexec/lockop/lock_op_test.go [63]

    -arg.ctr.rt.hasNewVersionInRange = testFunc
    +if arg.ctr.rt != nil {
    +    arg.ctr.rt.hasNewVersionInRange = testFunc
    +}
     
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: This suggestion correctly identifies a potential nil pointer dereference issue which is critical for preventing runtime errors. The suggested code correctly adds a nil check before accessing arg.ctr.rt.

    8
    Add a nil check for arg.ctr.rt.parker before calling its methods to prevent nil pointer dereference errors

    Add a check to ensure arg.ctr.rt.parker is not nil before calling Reset and EncodeInt32
    methods to avoid potential nil pointer dereference errors.

    pkg/sql/colexec/lockop/lock_op_test.go [130-131]

    -arg.ctr.rt.parker.Reset()
    -arg.ctr.rt.parker.EncodeInt32(0)
    +if arg.ctr.rt.parker != nil {
    +    arg.ctr.rt.parker.Reset()
    +    arg.ctr.rt.parker.EncodeInt32(0)
    +}
     
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: The suggestion is valid as it addresses a potential nil pointer dereference by adding a check before accessing arg.ctr.rt.parker. This is crucial for ensuring the stability of the code.

    8
    Add a nil check for arg.ctr.rt and arg.ctr.rt.parker before calling FreeMem to prevent nil pointer dereference errors

    Add a nil check for arg.ctr.rt.parker before calling FreeMem to avoid potential nil
    pointer dereference errors.

    pkg/sql/colexec/lockop/lock_op_test.go [340-341]

    -if arg.ctr.rt.parker != nil {
    +if arg.ctr.rt != nil && arg.ctr.rt.parker != nil {
         arg.ctr.rt.parker.FreeMem()
     }
     
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: This suggestion is important as it ensures both arg.ctr.rt and arg.ctr.rt.parker are not nil before attempting to free memory, which is critical to avoid runtime errors in memory management operations.

    8
    Add a nil check to prevent potential nil pointer dereference

    Add a nil check for arg.ctr.generateSeries before accessing its state attribute to avoid
    potential nil pointer dereference.

    pkg/sql/colexec/table_function/generate_series.go [47]

    -if arg.ctr.generateSeries.state == initArg {
    +if arg.ctr.generateSeries != nil && arg.ctr.generateSeries.state == initArg {
     
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: Adding a nil check before accessing arg.ctr.generateSeries.state is crucial to prevent runtime errors due to nil pointer dereferences, especially since arg.ctr.generateSeries is newly instantiated in the PR. This is a significant improvement for robustness and stability.

    8
    Add a nil check for ctr to prevent potential nil pointer dereference

    To avoid potential nil pointer dereference, add a check to ensure ctr is not nil before
    accessing its methods or properties.

    pkg/sql/colexec/right/join.go [107]

    +if ctr == nil {
    +    return result, moerr.NewInternalErrorNoCtx("container is nil")
    +}
     if err := ctr.probe(arg, proc, analyze, arg.GetIsFirst(), arg.GetIsLast(), &result); err != nil {
     
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: Adding a nil check before accessing methods on ctr is crucial to prevent runtime errors due to nil pointer dereference, which is a significant issue.

    8
    Ensure arg2.ctr.rt is initialized before assigning values to its fields to prevent nil pointer dereference errors

    Ensure that arg.ctr.rt is initialized before assigning testFunc to hasNewVersionInRange to
    avoid potential nil pointer dereference errors.

    pkg/sql/colexec/lockop/lock_op_test.go [222-226]

    -arg2.ctr.rt = &state{}
    +if arg2.ctr.rt == nil {
    +    arg2.ctr.rt = &state{}
    +}
     arg2.ctr.rt.retryError = nil
     arg2.ctr.rt.hasNewVersionInRange = testFunc
     
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: The suggestion is relevant and improves code safety by ensuring arg2.ctr.rt is initialized before use. However, the existing code already initializes arg2.ctr.rt before use, making this suggestion slightly redundant but still enhancing clarity and safety.

    7
    Possible issue
    Add synchronization mechanisms when modifying shared state to prevent race conditions

    To prevent potential race conditions, consider adding synchronization mechanisms when
    modifying shared state like arg.ctr.bat and arg.ctr.lastrow.

    pkg/sql/colexec/left/join.go [87-88]

    +ctr.mu.Lock()
     arg.ctr.bat = bat
     arg.ctr.lastrow = 0
    +ctr.mu.Unlock()
     
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: Adding synchronization is important to prevent race conditions in a concurrent environment. This suggestion is relevant given the context of shared state modification.

    7
    Performance
    Use a buffer pool for ctr.rbat to improve performance by reducing memory allocation overhead

    To improve performance, consider using a buffer pool for ctr.rbat to reduce the overhead
    of repeatedly allocating and deallocating memory.

    pkg/sql/colexec/right/join.go [259]

    -ctr.rbat = batch.NewWithSize(len(ap.Result))
    +if ctr.rbat == nil {
    +    ctr.rbat = batch.NewWithSize(len(ap.Result))
    +} else {
    +    ctr.rbat.Reset()
    +}
     
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: Utilizing a buffer pool to manage ctr.rbat can significantly improve performance by reducing the overhead associated with frequent memory allocation and deallocation.

    7
    Reuse existing vectors instead of creating new ones to improve performance

    To improve performance, consider reusing existing vectors instead of creating new ones in
    the probe function.

    pkg/sql/colexec/left/join.go [199]

    -ctr.rbat.Vecs[i] = proc.GetVector(*ap.ctr.bat.Vecs[rp.Pos].GetType())
    +if ctr.rbat.Vecs[i] == nil {
    +    ctr.rbat.Vecs[i] = proc.GetVector(*ap.ctr.bat.Vecs[rp.Pos].GetType())
    +} else {
    +    ctr.rbat.Vecs[i].Reset(*ap.ctr.bat.Vecs[rp.Pos].GetType())
    +}
     
    • Apply this suggestion
    Suggestion importance[1-10]: 5

    Why: Reusing vectors can enhance performance by reducing memory allocations. However, the impact might be limited depending on the frequency and context of vector creation and reuse.

    5
    Best practice
    Add a defer statement to ensure the channel is closed properly in case of an early return

    To ensure proper resource management, add a defer statement to close the channel in case
    of an early return due to an error.

    pkg/sql/colexec/right/join.go [198-202]

    +defer close(ap.ctr.channel)
     for v := range ap.ctr.channel {
         ctr.matched.Or(v)
         cnt++
         if cnt == int(ap.NumCPU) {
    -        close(ap.ctr.channel)
             break
         }
     }
     
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: Using a defer statement to ensure the channel is closed in case of an early return is a best practice that prevents resource leaks, which is important for robustness.

    7
    Maintainability
    Extract repeated code for setting ctr.rbat.Vecs[i] into a helper function

    To improve readability and maintainability, consider extracting the repeated code for
    setting ctr.rbat.Vecs[i] into a helper function.

    pkg/sql/colexec/left/join.go [173-175]

    -ctr.rbat.Vecs[i] = proc.GetVector(typ)
    -if err := vector.GetUnionAllFunction(typ, proc.Mp())(ctr.rbat.Vecs[i], ap.ctr.bat.Vecs[rp.Pos]); err != nil {
    +if err := setVector(ctr.rbat.Vecs[i], ap.ctr.bat.Vecs[rp.Pos], typ, proc); err != nil {
         return err
     }
     
    +// Helper function
    +func setVector(dest, src *vector.Vector, typ types.Type, proc *process.Process) error {
    +    dest = proc.GetVector(typ)
    +    return vector.GetUnionAllFunction(typ, proc.Mp())(dest, src)
    +}
    +
    • Apply this suggestion
    Suggestion importance[1-10]: 6

    Why: The suggestion improves maintainability by reducing code duplication through a helper function. This is a good practice but not as critical as runtime errors.

    6
    Extract repeated logic for setting ctr.rbat.Vecs into a helper function

    To improve readability and maintainability, extract the repeated logic for setting
    ctr.rbat.Vecs into a helper function.

    pkg/sql/colexec/right/join.go [259-265]

    -if rp.Rel == 0 {
    -    ctr.rbat.Vecs[i] = proc.GetVector(*ap.ctr.buf.Vecs[rp.Pos].GetType())
    -} else {
    -    ctr.rbat.Vecs[i] = proc.GetVector(ap.RightTypes[rp.Pos])
    +setVector := func(rp ResultPos, i int) {
    +    if rp.Rel == 0 {
    +        ctr.rbat.Vecs[i] = proc.GetVector(*ap.ctr.buf.Vecs[rp.Pos].GetType())
    +    } else {
    +        ctr.rbat.Vecs[i] = proc.GetVector(ap.RightTypes[rp.Pos])
    +    }
    +}
    +for i, rp := range ap.Result {
    +    setVector(rp, i)
     }
     
    • Apply this suggestion
    Suggestion importance[1-10]: 6

    Why: Extracting the logic into a helper function improves code readability and maintainability, but it's a moderate improvement rather than a critical fix.

    6

    @huby2358
    huby2358 force-pushed the t3 branch 7 times, most recently from 87745b7 to 4ebc7ca Compare June 20, 2024 06:52

    @m-schen m-schen left a comment

    Copy link
    Copy Markdown
    Contributor

    Choose a reason for hiding this comment

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

    很棒🐮

    @mergify
    mergify Bot merged commit d6a3226 into matrixorigin:main Jun 21, 2024
    fengttt pushed a commit to fengttt/matrixone that referenced this pull request May 7, 2026
    …origin#16951)
    
    There are many variables in the operator that can be changed every time they are executed.
    These variables need to be moved to specific places and cannot be placed outside, nor should they be capitalized
    
    Approved by: @ouyuanning, @m-schen
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Labels

    Enhancement kind/refactor Code refactor Review effort [1-5]: 4 size/L Denotes a PR that changes [500,999] lines Tests

    Projects

    None yet

    Development

    Successfully merging this pull request may close these issues.

    4 participants