matrixone icon indicating copy to clipboard operation
matrixone copied to clipboard

handle alter table add column issue

Open qingxinhome opened this issue 1 year ago • 1 comments
trafficstars

What type of PR is this?

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

Which issue(s) this PR fixes:

issue #16078 issue #16179 issue #16196

What this PR does / why we need it:

fix alter table ADD COLUMN NOT NULL with default value does not set null flag correctly.

qingxinhome avatar May 14 '24 12:05 qingxinhome

@qingxinhome Thanks for your contributions!

Here are review comments for file pkg/common/moerr/error.go:

Pull Request Review:

Title:

The title of the pull request is clear and concise, indicating that it addresses an issue related to handling alter table add column.

Body:

The body of the pull request provides essential information about the type of PR (BUG), the specific issue it fixes (issue #16078), and the reason for the PR, which is to fix the incorrect setting of the null flag when adding a column with a default value. The body is informative and to the point.

Changes in pkg/common/moerr/error.go:

  1. Addition of Error Code and Message for ErrBlobCantHaveDefault:

    • The pull request adds a new error code ErrBlobCantHaveDefault along with an error message for the specific scenario where a BLOB, TEXT, GEOMETRY, or JSON column cannot have a default value.
    • Issue: The error message is clear, but the error code might need to be reviewed for consistency with existing error codes.
    • Suggestion: Ensure that the new error code follows the same pattern and conventions as the existing error codes for consistency.
  2. Addition of NewErrBlobCantHaveDefault Function:

    • A new function NewErrBlobCantHaveDefault is added to create an error of type ErrBlobCantHaveDefault.
    • Issue: The function signature includes arg any, which might need clarification or validation depending on its usage.
    • Suggestion: Provide documentation or comments explaining the purpose and expected usage of the arg any parameter to avoid confusion or misuse.
  3. Overall Optimization:

    • The changes made in the error handling section seem relevant and necessary for handling the specific scenario mentioned in the pull request.
    • Suggestion: Ensure that the error codes, messages, and functions added are well-documented to aid developers in understanding and utilizing them effectively.

Security Concerns:

  • The changes in this pull request do not introduce any apparent security vulnerabilities based on the provided information.

General Suggestions:

  • Consider adding unit tests for the new error code and function to ensure proper functionality and error handling.
  • Review the naming conventions and documentation for consistency and clarity throughout the error handling code.
  • If the arg any parameter in NewErrBlobCantHaveDefault is intended for specific data types, validate and handle them appropriately to prevent potential errors.

Overall, the changes in the pull request seem relevant for addressing the issue related to altering tables and adding columns with default values. Ensuring consistency, clarity, and proper documentation will enhance the maintainability and usability of the error handling code.

Here are review comments for file pkg/sql/parsers/dialect/mysql/mysql_sql_test.go:

Pull Request Review:

Title: handle alter table add column issue

Body:

  • The PR is labeled as a bug fix.
  • It references and aims to fix issue #16078.
  • The purpose of this PR is to address the problem where "alter table ADD COLUMN NOT NULL with default value does not set null flag correctly."

Changes in pkg/sql/parsers/dialect/mysql/mysql_sql_test.go:

  • The changes made in the file involve modifying test cases related to SQL queries for MySQL dialect.
  • Specifically, the test case for "alter table t1 add column yy varchar(20) not null default 'foo'" has been added.
  • The test input and output have been adjusted to reflect the alteration in the SQL query being tested.

Feedback and Suggestions:

  1. Code Clarity and Comments:

    • The commented-out lines in the test case should be removed to maintain code cleanliness. Unnecessary commented code can lead to confusion and should be avoided.
    • Consider adding a comment above the new test case to explain its purpose and relevance to the bug fix.
  2. Test Coverage:

    • It's essential to ensure that the test cases cover various scenarios, including edge cases and potential issues related to altering tables.
    • Consider adding more test cases to cover different combinations of column additions with various constraints and default values.
  3. Security Concerns:

    • When altering tables and adding columns with default values, ensure that the default values do not introduce security vulnerabilities like SQL injection.
    • Validate and sanitize user inputs to prevent any malicious SQL injection attacks.
  4. Optimization:

    • While the current changes address the specific bug, consider optimizing the test cases to cover a broader range of scenarios related to altering tables in MySQL.
    • Ensure that the test cases are concise, focused, and provide comprehensive coverage of the functionality being tested.
  5. Consistency:

    • Maintain consistency in the naming conventions and formatting of test cases to enhance readability and maintain a standard across the test suite.
  6. Documentation:

    • Consider updating any relevant documentation or adding comments within the code to explain the behavior being tested and the expected outcomes.

By addressing the points mentioned above, the code quality, test coverage, and overall robustness of the MySQL dialect parser can be improved, ensuring that alterations to tables are handled correctly and securely.

Here are review comments for file pkg/sql/plan/build_alter_add_column.go:

Pull Request Review:

Title:

The title of the pull request, "handle alter table add column issue," is clear and concise, indicating that the PR is addressing an issue related to adding a column in an alter table operation.

Body:

The body of the pull request provides essential information about the type of PR (BUG), the specific issue it fixes (issue #16078), and the purpose of the PR, which is to fix the problem where "alter table ADD COLUMN NOT NULL with default value does not set null flag correctly."

Changes:

  1. In the buildAddColumnAndConstraint function:

    • The code related to setting the default value for a new column has been modified. The previous code block that handled *tree.AttributeDefault and *tree.AttributeNull cases has been commented out, and a new block of code has been added to handle setting the default value for the new column.
    • The new code block correctly sets the default value for the new column and updates the hasDefaultValue flag based on whether a default value is present.
    • There is a check added to ensure that if the column is auto-incremented and has a default value, it raises an error.
  2. Addition of the SetDefaultValue and related functions:

    • New functions have been added to handle setting the default value of a column and performing checks related to the default value.
    • The setDefaultValueWithBinaryPadding function handles setting the default value with binary padding for binary types.
    • The checkColumnDefaultValue function checks the default value of the column based on SQL modes and column types.
    • The IsTypeTime function checks if the type is a time type like datetime, date, or timestamp.

Feedback and Suggestions:

  1. Code Commenting:

    • It's good practice to remove commented-out code blocks entirely rather than leaving them in the codebase. This helps in maintaining a clean and understandable codebase. The commented-out code blocks related to *tree.AttributeDefault, *tree.AttributeNull, and error handling should be removed.
  2. Error Handling:

    • Ensure that error handling is consistent and thorough throughout the codebase. Make sure to handle errors appropriately and provide meaningful error messages to aid in debugging and troubleshooting.
  3. Code Optimization:

    • Consider refactoring the code to improve readability and maintainability. Break down complex functions into smaller, more manageable functions with clear responsibilities.
    • Ensure that the code follows best practices and design patterns to enhance code quality and ease of maintenance.
  4. Security Concerns:

    • When dealing with default values and SQL modes, ensure that there are no vulnerabilities related to SQL injection or other security risks. Validate and sanitize input values to prevent any potential security threats.
  5. Testing:

    • It's crucial to include comprehensive unit tests to cover the new functionality and ensure that the changes work as expected and do not introduce regressions.
  6. Documentation:

    • Update the relevant documentation to reflect the changes made in this PR, especially regarding setting default values for columns and handling column defaults based on SQL modes.

By addressing the above points and considering the suggestions provided, the codebase can be improved in terms of readability, maintainability, and security.

Here are review comments for file pkg/sql/plan/build_alter_table.go:

Pull Request Review:

Title:

The title "handle alter table add column issue" is clear and indicates the purpose of the pull request.

Body:

The body of the pull request provides essential information about the type of PR (BUG), the specific issue it fixes (issue #16078), and the reason for the changes made in the PR. It mentions fixing the issue related to setting the null flag correctly when adding a column with a default value. The body is concise and to the point.

Changes in pkg/sql/plan/build_alter_table.go:

  1. Problem 1: Code commented out but not removed:

    • In the changes, there are commented-out code blocks that are not removed. This can lead to confusion and clutter in the codebase. It is recommended to remove unnecessary commented-out code to maintain code cleanliness and readability.
  2. Problem 2: Redundant code block:

    • The logic for setting nullOrNot is duplicated and can be simplified. The code can be optimized by removing the redundant code blocks and unnecessary conditions to improve code readability and maintainability.
  3. Problem 3: Inconsistent handling of default values:

    • The handling of default values is inconsistent. There are multiple conditions and checks that can be simplified and made more consistent to avoid potential bugs and improve code clarity.
  4. Problem 4: Potential security issue - SQL Injection:

    • The current implementation does not seem to handle SQL injection vulnerabilities properly, especially when setting default values. It is crucial to ensure that user input is properly sanitized and validated to prevent SQL injection attacks.

Suggestions for Improvement:

  1. Remove commented-out code blocks to declutter the codebase and improve readability.
  2. Refactor the code to eliminate redundancy and simplify the logic for setting nullOrNot.
  3. Ensure consistent handling of default values to avoid potential bugs and improve code maintainability.
  4. Implement proper input validation and sanitization to prevent SQL injection vulnerabilities.

Overall:

The changes made in the pull request address the issue related to setting the null flag correctly when adding a column with a default value. However, there are areas for improvement in terms of code cleanliness, redundancy, consistency, and security. By addressing the mentioned problems and implementing the suggested improvements, the codebase can be enhanced in terms of readability, maintainability, and security.

Here are review comments for file pkg/sql/plan/build_show.go:

Pull Request Review:

Title:

The title of the pull request is clear and concise, indicating that the PR is addressing an issue related to handling alter table add column.

Body:

The body of the pull request provides relevant information about the type of PR (BUG), the specific issue it fixes (issue #16078), and the reason for the PR, which is to fix the problem where alter table ADD COLUMN NOT NULL with a default value does not set the null flag correctly. The body is informative and to the point.

Changes in pkg/sql/plan/build_show.go:

  1. Problem 1: Code Commented Out:

    • The original code related to setting the nullOrNot variable based on column properties has been commented out.
    • Suggestion: Instead of commenting out the code, it would be better to remove the unnecessary commented code to maintain code cleanliness and reduce confusion.
  2. Problem 2: Inefficient Code Handling:

    • The code has been modified to use a bytes.Buffer to construct the nullOrNot string, but the handling of different cases is not optimal.
    • Suggestion: Refactor the code to handle the different cases more clearly and efficiently without unnecessary complexity. Consider using conditional statements more effectively to set the nullOrNot string based on the column properties.
  3. Problem 3: Redundant Conditions:

    • There are redundant conditions and checks in the modified code that can be simplified.
    • Suggestion: Simplify the logic by removing redundant conditions and checks to improve code readability and maintainability.
  4. Problem 4: Potential Security Issue:

    • The comparison strings.EqualFold(col.Default.OriginString, "null") followed by setting buf.WriteString(" DEFAULT NULL") can lead to a security vulnerability if col.Default.OriginString is not properly sanitized.
    • Suggestion: Ensure that col.Default.OriginString is properly validated and sanitized to prevent any potential SQL injection vulnerabilities. Consider using parameterized queries or escaping user input to mitigate this risk.
  5. Optimization:

    • Instead of using a bytes.Buffer for constructing the nullOrNot string, consider using string concatenation directly if performance is not a concern. This can simplify the code and improve readability.
  6. Code Formatting:

    • Ensure consistent code formatting throughout the file to maintain code style consistency.

Overall Suggestions:

  • Remove commented-out code to declutter the file.
  • Refactor the logic to handle setting the nullOrNot string more efficiently.
  • Simplify and optimize the conditions for setting nullOrNot.
  • Validate and sanitize user input to prevent security vulnerabilities.
  • Consider simplifying the code by using direct string concatenation if performance is not a concern.
  • Ensure consistent code formatting for better readability.

By addressing these issues and suggestions, the code quality, readability, and security of the application can be improved.

matrix-meow avatar May 14 '24 12:05 matrix-meow