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

Method for reading query results during upgrade modification #18731

Merged
merged 3 commits into from
Sep 12, 2024

Conversation

qingxinhome
Copy link
Contributor

@qingxinhome qingxinhome commented Sep 12, 2024

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 #https://github.com/matrixorigin/MO-Cloud/issues/4067

What this PR does / why we need it:

Method for reading query results during upgrade modification
Change from UnsafeStringAt() to call GetStringAt()


PR Type

Bug fix


Description

  • Replaced UnsafeGetStringAt with GetStringAt across multiple files for safer string retrieval.
  • Improved safety in handling query results during upgrade modifications.
  • Addressed issue Update readme #4067 by ensuring safer string operations in various functions.

Changes walkthrough 📝

Relevant files
Bug fix
upgrade_strategy.go
Replace Unsafe String Retrieval with Safe Method                 

pkg/bootstrap/versions/upgrade_strategy.go

  • Replaced UnsafeGetStringAt with GetStringAt for safer string
    retrieval.
  • Updated multiple instances within CheckTableColumn,
    CheckViewDefinition, and CheckTableComment functions.
  • +6/-6     
    upgrade_tenant_task.go
    Improve String Handling Safety in Tenant Tasks                     

    pkg/bootstrap/versions/upgrade_tenant_task.go

  • Changed UnsafeGetStringAt to GetStringAt for safer string handling.
  • Applied changes in GetUpgradeTenantTasks,
    GetTenantCreateVersionForUpdate, and GetTenantVersion.
  • +3/-3     
    version.go
    Enhance Version Retrieval Safety                                                 

    pkg/bootstrap/versions/version.go

  • Updated UnsafeGetStringAt to GetStringAt for safer operations.
  • Modified in GetLatestVersion, GetLatestUpgradeVersion, and
    MustGetLatestReadyVersion.
  • +3/-3     

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

    Copy link

    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: 2 🔵🔵⚪⚪⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Key issues to review

    Potential Error Handling
    The change from UnsafeGetStringAt to GetStringAt may require additional error handling, as GetStringAt can return an error.

    @mergify mergify bot requested a review from sukki37 September 12, 2024 03:46
    @mergify mergify bot added the kind/bug Something isn't working label Sep 12, 2024
    Copy link

    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
    Performance
    Optimize slice appending for better performance

    Consider using append with multiple arguments instead of appending in a loop to
    improve performance when adding elements to the tenants and versions slices.

    pkg/bootstrap/versions/upgrade_tenant_task.go [111-117]

     res.ReadRows(func(rows int, cols []*vector.Vector) bool {
    +	newTenants := make([]int32, rows)
    +	newVersions := make([]string, rows)
     	for i := 0; i < rows; i++ {
    -		tenants = append(tenants, vector.GetFixedAt[int32](cols[0], i))
    -		versions = append(versions, cols[1].GetStringAt(i))
    +		newTenants[i] = vector.GetFixedAt[int32](cols[0], i)
    +		newVersions[i] = cols[1].GetStringAt(i)
     	}
    +	tenants = append(tenants, newTenants...)
    +	versions = append(versions, newVersions...)
     	return true
     })
     
    • Apply this suggestion
    Suggestion importance[1-10]: 9

    Why: This suggestion optimizes the code by reducing the number of append operations, which can significantly improve performance, especially with large datasets.

    9
    Enhancement
    Use a switch statement for cleaner and more efficient type-based logic

    Consider using a switch statement instead of multiple if-else conditions when
    checking the data type and setting the column length, precision, and scale.

    pkg/bootstrap/versions/upgrade_strategy.go [253-256]

     colInfo.ColType = data_type
     colInfo.Nullable = checkInput(is_nullable)
    -if data_type == "varchar" || data_type == "char" {
    +switch data_type {
    +case "varchar", "char":
     	colInfo.Length = int(character_length)
    -} else if data_type == "decimal" {
    +case "decimal":
     	colInfo.Precision = int(numeric_precision)
     	colInfo.Scale = int(numeric_scale)
    -} else if data_type == "datetime" || data_type == "timestamp" {
    +case "datetime", "timestamp":
     	colInfo.Precision = int(datetime_precision)
     }
     
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: Switching to a switch statement can make the code cleaner and more efficient, improving readability and maintainability, which is beneficial for future modifications.

    8
    Maintainability
    Provide more informative panic messages for easier debugging

    Consider using a more descriptive error message when panicking due to a missing
    latest ready version. Include information about the query or context that led to
    this situation.

    pkg/bootstrap/versions/version.go [130-131]

     if version == "" {
    -	panic("missing latest ready version")
    +	panic("Failed to retrieve the latest ready version from the database. This may indicate a data integrity issue or a problem with the version management system.")
     }
     
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: Providing a more informative panic message can greatly aid in debugging and understanding the context of the error, enhancing maintainability and troubleshooting.

    8
    Best practice
    Use named return values for better code clarity and safety

    Consider using a named return value for colInfo to improve code readability and
    reduce the risk of forgetting to return the updated value.

    pkg/bootstrap/versions/upgrade_strategy.go [237]

     func CheckTableColumn(txn executor.TxnExecutor,
    -	accountId uint32, schema string, table string, column string) (ColInfo, error) {
    +	accountId uint32, schema string, table string, column string) (colInfo ColInfo, err error) {
     	...
    -	colInfo := ColInfo{}
    -	...
    -	return colInfo, err
    +	return
     
    Suggestion importance[1-10]: 7

    Why: Using named return values can improve code readability and reduce the risk of errors by ensuring that the correct values are returned, but it is not a critical change.

    7

    @matrix-meow matrix-meow added the size/S Denotes a PR that changes [10,99] lines label Sep 12, 2024
    @mergify mergify bot merged commit 60bdf4f into matrixorigin:1.2-dev Sep 12, 2024
    16 of 18 checks passed
    @qingxinhome qingxinhome deleted the UpdateUpgradeCode-1.2-dev branch September 14, 2024 02:24
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    Bug fix kind/bug Something isn't working Review effort [1-5]: 2 size/S Denotes a PR that changes [10,99] lines
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    5 participants