Skip to content

Add bit_cast function for binary types - #11441

Merged
aunjgr merged 11 commits into
matrixorigin:mainfrom
aunjgr:metascan
Sep 8, 2023
Merged

aunjgr merged 11 commits into
matrixorigin:mainfrom
aunjgr:metascan

Conversation

@aunjgr

@aunjgr aunjgr commented Aug 24, 2023

Copy link
Copy Markdown
Contributor

What type of PR is this?

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

Which issue(s) this PR fixes:

issue #11396

What this PR does / why we need it:

@aunjgr
aunjgr requested a review from a user August 24, 2023 07:19
@matrix-meow matrix-meow added the size/XXL Denotes a PR that changes 2000+ lines label Aug 24, 2023
@mergify mergify Bot added the kind/feature label Aug 24, 2023
@matrix-meow

Copy link
Copy Markdown
Contributor

@aunjgr Thanks for your contributions!

Here are review comments for file pkg/sql/colexec/evalExpression.go:
Review:

Title:
The title of the pull request is clear and concise. It accurately describes the purpose of the changes made.

Body:
The body of the pull request is incomplete. It only includes a checklist for the type of PR and a reference to the issue being fixed. It would be helpful to provide more information about what the PR does and why it is needed.

Changes:
The changes made in the pull request are focused on the file pkg/sql/colexec/evalExpression.go. The changes include importing the util2 package, modifying the constBinType variable to use types.T_varbinary instead of types.T_blob, and adding a decode function for binary types.

Problems:

  1. Incomplete body: The body of the pull request is missing important information about what the PR does and why it is needed. This makes it difficult to understand the purpose and impact of the changes.

Suggestions:

  1. Provide a detailed description in the body of the pull request explaining what the PR does and why it is needed. This will help reviewers and future readers understand the context and impact of the changes.

Optimizations:

  1. None identified. The changes made in the pull request seem to be necessary and focused on the specific task of adding a decode function for binary types.

Here are review comments for file pkg/sql/colexec/table_function/metadata_scan.go:
Title: Add decode function for binary types

Body:

Changes made in pkg/sql/colexec/table_function/metadata_scan.go:

  • Added a line to add the count of metaInfos to the retBat.
  • Commented out a line that adds the count of arg.Attrs to opBat.

Review:

  1. The title of the pull request is clear and concise, indicating that a decode function for binary types is being added.

  2. The body of the pull request provides relevant information about the type of PR, the issue it fixes, and the purpose of the changes.

  3. In the changes made to metadata_scan.go, there are a couple of issues that need addressing:

    • In the first change, a line is added to add the count of metaInfos to the retBat. However, it is unclear why this count is being added and what purpose it serves. The code should be commented to explain the rationale behind this addition.

    • In the second change, a line that adds the count of arg.Attrs to opBat is commented out. It is unclear why this line is commented out and what impact it has on the functionality of the code. The code should be commented to explain the reason for this change.

Optimization suggestions:

  1. It would be helpful to provide more context in the body of the pull request. Specifically, explaining why the decode function for binary types is needed and how it will be used. This will help reviewers understand the significance of the changes and provide more informed feedback.

  2. The code changes could benefit from more descriptive variable and function names. This will improve code readability and make it easier to understand the purpose of each component.

  3. It would be beneficial to include unit tests for the new decode function to ensure its correctness and prevent regressions in the future.

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

Title:
The title of the pull request is clear and concise. It accurately describes the purpose of the changes made in the pull request.

Body:
The body of the pull request is incomplete. It only includes a checklist of PR types and a reference to an issue number. It does not provide any additional information about what the PR does or why it is needed. The body should be updated to include a detailed description of the changes made and the problem they solve.

Changes:
The changes made in the pull request modify the file pkg/sql/parsers/dialect/mysql/keywords.go. Specifically, a new keyword "decode" is added to the list of keywords. This change seems to be related to the issue #11396.

Suggestions:

  1. Update the body of the pull request to provide a detailed description of the changes made and the problem they solve. This will help reviewers understand the context and purpose of the changes.

  2. Consider providing more information about the issue [Bug]: min/max/sum of metadata_scan is not human readable #11396 in the body of the pull request. This will help reviewers understand the motivation behind the changes and how they address the issue.

  3. It is unclear from the provided information whether the addition of the "decode" keyword is necessary or if it is a new feature. If it is a new feature, consider providing more information about its functionality and use cases in the pull request body.

  4. Consider adding unit tests to ensure that the new "decode" keyword is handled correctly in the codebase.

  5. It is unclear from the provided information whether the changes made in the pull request have been optimized or if there are any potential performance improvements that can be made. Consider reviewing the code changes for any potential optimizations or improvements.

Overall, the pull request needs more information and context to be properly reviewed. Providing a detailed description of the changes made and the problem they solve, as well as addressing the suggestions mentioned above, will help improve the quality of the pull request.

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

Title: Add decode function for binary types

The title of the pull request is clear and concise. It indicates that the purpose of the pull request is to add a decode function for binary types.

Body:

The body of the pull request is incomplete and lacks important information. It only includes a checklist for the type of pull request and a reference to the issue it fixes. It would be helpful to provide more context and explanation about the changes made in the pull request.

Changes:

The changes made in the pull request are not provided. The diff shows that there are no changes made to the file pkg/sql/parsers/dialect/mysql/mysql_sql.go. It is unclear what the purpose of the pull request is and what changes were intended to be made.

Suggestions:

  1. Provide a detailed description in the body of the pull request explaining the changes made and the motivation behind them. This will help reviewers understand the purpose and impact of the changes.

  2. Include the actual changes made in the pull request. The diff should show the modifications made to the file pkg/sql/parsers/dialect/mysql/mysql_sql.go. This will allow reviewers to provide more accurate feedback on the code changes.

Optimization:

Without the actual changes, it is difficult to provide specific optimization suggestions. However, it is always a good practice to ensure that the code changes are efficient, maintainable, and follow best practices. This can include things like using appropriate data structures, avoiding unnecessary computations, and adhering to coding conventions.

Overall, the pull request needs more information and the actual changes made in the code should be included for a thorough review.

Here are review comments for file pkg/sql/parsers/dialect/mysql/mysql_sql.y:
Review:

Title:
The title of the pull request is clear and concise. It accurately describes the purpose of the changes made in the pull request.

Body:
The body of the pull request is incomplete. It starts with a section asking about the type of PR, but the checkboxes are not filled out. It would be helpful to have this information filled out to provide more context about the nature of the changes. Additionally, the body mentions an issue number, but there is no explanation of what the issue is or how this pull request addresses it. It would be beneficial to provide more information about the issue being fixed.

Changes:
The changes made in the pull request involve modifying the file pkg/sql/parsers/dialect/mysql/mysql_sql.y. The changes include adding a new token DECODE and adding a new production rule for the DECODE function. The DECODE function is used to create a new DecodeExpr object in the abstract syntax tree.

Suggestions:

  1. Complete the body of the pull request by filling out the checkboxes and providing more information about the issue being fixed.
  2. Provide a clear explanation of the issue being fixed and how this pull request addresses it.
  3. Consider adding test cases to ensure the correctness of the DECODE function.
  4. Consider providing examples or documentation on how to use the DECODE function.
  5. Consider providing a brief explanation of the purpose and functionality of the DECODE function in the code comments.

Optimizations:

  1. Consider organizing the changes in a more readable and consistent manner, such as using consistent indentation and spacing.
  2. Consider using more descriptive variable and function names to improve code readability.
  3. Consider adding error handling for invalid inputs to the DECODE function.
  4. Consider refactoring the code to improve modularity and reusability.

Here are review comments for file pkg/sql/parsers/tree/expr.go:
Review:

Title: Add decode function for binary types

Body:

Changes made in pkg/sql/parsers/tree/expr.go:

  • Added a new struct called DecodeExpr with fields Expr and Type.
  • Added a Format method to format the DecodeExpr struct.
  • Added an Accept method to implement the NodeChecker interface.
  • Added a NewDecodeExpr function to create a new DecodeExpr struct.

Problems:

  1. Inconsistent naming: The function NewDecodeExpr should follow the naming convention used in the file, which is NewCastExpr. It should be renamed to NewDecodeExprExpr to be consistent.

Suggestions:

  1. Rename the function NewDecodeExpr to NewDecodeExprExpr for consistency.

Optimizations:
No optimizations needed.

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

Title: Add decode function for binary types

The title of the pull request is clear and concise. It accurately describes the purpose of the changes made in the pull request.

Body:
The body of the pull request provides some information about the type of the pull request (Feature) and the issue it fixes (issue #11396). However, it does not provide any additional context or explanation about why the changes are necessary or how they address the issue.

Changes:
The changes made in the pull request are focused on the file pkg/sql/plan/base_binder.go. The changes include adding a new case for the DecodeExpr type in the baseBindExpr function and modifying the bindFuncExprImplByAstExpr function to handle the new case.

  1. In the baseBindExpr function, a new case is added for the DecodeExpr type. However, there is no explanation or comment about what the DecodeExpr type represents or how it should be handled. This lack of documentation makes it difficult for other developers to understand the purpose and behavior of the new code.

  2. In the bindFuncExprImplByAstExpr function, there are commented out lines of code for the extract case. It is unclear why these lines are commented out and whether they should be included in the final code. It would be helpful to provide an explanation or comment about the purpose of these lines and why they are commented out.

  3. In the bindFuncExprImplByAstExpr function, there is a block of code for the decode case that is not optimized. Instead of creating a new Expr for the typeExpr, it would be more efficient to reuse the existing typ variable and assign it directly to the Typ field of the binExpr. This would eliminate the need for creating a new Expr and copying the type.

Suggestions:

  1. Add documentation or comments to explain the purpose and behavior of the new DecodeExpr case in the baseBindExpr function. This will help other developers understand the code and its intended usage.

  2. Provide an explanation or comment about the commented out lines of code for the extract case in the bindFuncExprImplByAstExpr function. Clarify whether these lines should be included in the final code and why they are commented out.

  3. Optimize the code in the bindFuncExprImplByAstExpr function by reusing the existing typ variable instead of creating a new Expr for the typeExpr. This will improve efficiency and reduce unnecessary code duplication.

Overall, the changes made in the pull request address the issue and add support for the DecodeExpr type. However, the lack of documentation and optimization in the code could be improved to enhance readability and performance.

Here are review comments for file pkg/sql/plan/explain/explain_expr.go:
Title: Add decode function for binary types

Body: This pull request adds a new decode function for binary types. It fixes issue #11396.

Changes made in pkg/sql/plan/explain/explain_expr.go:

  • Line 232: The code now writes funcName before opening parentheses.
  • Line 282: The code now adds a space before funcExpr.F.Func.GetObjName() and adds a space before opening parentheses.

Review:

  1. The title of the pull request is clear and concise, indicating the purpose of the changes.
  2. The body of the pull request provides relevant information, including the type of PR, the issue it fixes, and a brief description of what the PR does and why it is needed.
  3. The changes made in pkg/sql/plan/explain/explain_expr.go are straightforward and seem to address the issue mentioned in the body of the PR.

Suggestions:

  1. It would be helpful to provide more context about the funcName variable in the code changes. Is it a parameter or a variable declared elsewhere? Adding a comment or a brief explanation would improve code readability.
  2. Consider using consistent spacing around parentheses. In line 232, there is no space before opening parentheses, while in line 282, there is a space before opening parentheses. It would be better to have consistent spacing for better code consistency.

Optimization:

  1. The changes made in the pull request seem to be minimal and focused on fixing the issue. There are no obvious optimization opportunities in the provided code changes.

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

Review:

Title

The title of the pull request is clear and concise. It accurately describes the purpose of the changes made in the pull request.

Body

The body of the pull request provides some context about the type of pull request and the issue it fixes. However, it could be improved by providing more information about the specific problem or feature being addressed.

Changes

The changes made in the pull request include the addition of a new function called binaryDecodeToFixed and the implementation of the BinaryDecode function. The binaryDecodeToFixed function decodes binary data to a fixed-size type, while the BinaryDecode function handles the decoding of binary data based on the target type.

Problems:

  1. Inconsistent formatting: The formatting of the code is inconsistent. Some lines have extra spaces, while others do not. It would be better to have consistent formatting throughout the codebase to improve readability.

  2. Lack of comments: The code lacks comments to explain the purpose and functionality of the functions and variables. Adding comments would make the code easier to understand and maintain.

  3. Magic numbers: The code contains several magic numbers, such as 1, 2, 4, 8, etc., which are used to specify the byte length of different types. It would be better to define these numbers as constants or variables with meaningful names to improve code readability and maintainability.

  4. Error handling: The code does not handle errors properly. For example, when the length of the binary value is greater than the specified byte length, an error is returned, but it is not clear how this error is handled by the caller. It would be better to handle errors consistently and provide clear error messages or return values to the caller.

  5. Code duplication: The binaryDecodeToFixed function is duplicated for each target type. This could be refactored to reduce code duplication and improve maintainability.

Suggestions:

  1. Format the code consistently by removing extra spaces and aligning code blocks properly.

  2. Add comments to explain the purpose and functionality of the functions and variables.

  3. Replace magic numbers with constants or variables with meaningful names.

  4. Improve error handling by providing clear error messages or return values to the caller.

  5. Refactor the code to reduce code duplication and improve maintainability. Consider using generics or a more generic approach to handle different target types in the binaryDecodeToFixed function.

  6. Consider adding unit tests to ensure the correctness of the implemented functions.

  7. Consider optimizing the code by identifying any unnecessary operations or redundant code and removing them.

Overall, the changes made in the pull request address the need for a decode function for binary types. However, there are several areas that need improvement in terms of code quality, readability, and maintainability. By addressing the suggested problems and implementing the suggested optimizations, the codebase can be improved.

Here are review comments for file pkg/sql/plan/function/function_id.go:
Review:

Title: Add decode function for binary types

Body:

Changes made in pkg/sql/plan/function/function_id.go:

  • A new constant DECODE is added.
  • The functionIdRegister map is updated to include the decode function.

Overall, the changes made in this pull request seem to be straightforward and address the specific issue mentioned. However, there are a few areas that could be improved:

  1. Title: The title could be more specific and descriptive. Instead of just mentioning the addition of a decode function, it could include the specific type of binary that the function is for. For example, "Add decode function for binary types: base64".

  2. Body: The body of the pull request is incomplete. It includes a checklist for the type of PR, but it is not filled out. It would be helpful to provide more context on why this PR is needed and how it solves the issue.

  3. Code changes: The code changes themselves look fine, but it would be helpful to include a comment or documentation explaining what the decode function does and how it should be used.

Suggestions for improvement:

  1. Title: Update the title to be more specific and descriptive. For example, "Add decode function for base64 binary types".

  2. Body: Provide a more detailed explanation of why this PR is needed and how it solves the issue. This could include examples or use cases for the decode function.

  3. Code changes: Add a comment or documentation explaining the purpose and usage of the decode function. This will make it easier for other developers to understand and use the function correctly.

Optimization suggestions:

  1. Consider adding unit tests for the decode function to ensure its correctness and to provide examples of how to use it.

  2. If there are any other related functions or changes that could be made in the same file or related files, consider including them in this pull request to minimize the number of separate PRs and improve code organization.

Overall, the changes made in this pull request are necessary and straightforward. With the suggested improvements and optimizations, the codebase will be more robust and easier to understand and maintain.

Here are review comments for file pkg/sql/plan/function/list_operator.go:
Review:

Title: Add decode function for binary types

Body:

Changes made in pkg/sql/plan/function/list_operator.go:

  • Added a new function for the "decode" operator.
  • The function checks if the input types are valid for the "decode" operator.
  • The function has one overload that returns the second parameter type.
  • The function calls the BinaryDecode function to perform the decoding.

Overall, the changes in this pull request seem to be well-implemented and address the specific issue mentioned. However, there are a few suggestions for improvement:

  1. Title: The title could be more specific and descriptive. Instead of "Add decode function for binary types," it could be "Add decode function for binary and varbinary types."

  2. Body: The body of the pull request is missing some important information. It would be helpful to include a brief explanation of what the "decode" function does and why it is needed. Additionally, it would be good to mention any potential impact or dependencies this change may have on other parts of the codebase.

  3. Comments: The comments within the code could be improved for clarity and readability. For example, the comment for the "decode" operator could provide a brief description of what the operator does.

  4. Error Handling: The code does not currently handle any errors that may occur during the decoding process. It would be beneficial to add proper error handling and return meaningful error messages to the caller.

  5. Testing: It would be valuable to include unit tests for the new "decode" function to ensure its correctness and to cover different scenarios and edge cases.

  6. Code Organization: Consider separating the new function into its own file or section within the file to improve code organization and maintainability.

  7. Documentation: It would be helpful to update the documentation to include information about the new "decode" function, including its purpose, usage, and any specific considerations or limitations.

By addressing these suggestions, the pull request can be further improved in terms of clarity, functionality, and maintainability.

Here are review comments for file pkg/sql/plan/metadata_scan.go:
Title: Add decode function for binary types

Body:

Changes made in pkg/sql/plan/metadata_scan.go:

  • Line 71: The code initializes a variable tp based on a switch case.
  • Line 73: The code changes the type from types.T_varchar to types.T_varbinary.
  • Line 75: The code changes the type from types.T_varchar to types.T_varbinary.
  • Line 77: The code changes the type from types.T_varchar to types.T_varbinary.

Review:

  1. In the pull request title, it would be helpful to mention the specific binary types that are being added. For example, "Add decode function for varbinary types".

  2. The pull request body is concise and provides relevant information about the purpose of the changes and the issue being fixed.

  3. In the code changes, it seems that the types T_varchar are being replaced with T_varbinary in the switch case. However, the comment "TODO: find a way to show this info" suggests that there might be some incomplete functionality or missing implementation. It would be helpful to provide more context or explanation in the pull request body about why these changes are necessary and what the expected behavior is.

  4. The code changes themselves look fine, as they correctly update the types based on the switch case. However, it would be beneficial to add comments explaining the purpose of each case and why T_varbinary is being used instead of T_varchar. This would improve code readability and make it easier for future developers to understand the code.

Optimization suggestions:

  1. Consider refactoring the switch case into a separate function. This would improve code modularity and make it easier to test and maintain.

  2. Add unit tests for the new decode function to ensure its correctness and to catch any potential bugs or edge cases.

Overall, the changes made in this pull request seem reasonable, but some additional context and comments would be helpful for better understanding the purpose and expected behavior of the code changes.

Here are review comments for file test/distributed/cases/function/table_func_metadata_scan.result:
Title: Add decode function for binary types

Body: This pull request adds a decode function for binary types. It addresses issue #11396.

Changes made in the file test/distributed/cases/function/table_func_metadata_scan.result:

  • Added a new select statement that uses the decode function to calculate the minimum, maximum, and sum of the 'min', 'max', and 'sum' columns from the 'metadata_scan' table.
  • The decode function is used to convert the values of the 'min', 'max', and 'sum' columns to integers before performing the calculations.
  • The results of the new select statement are displayed in the result set.

Review:

  1. The title of the pull request is clear and concise, indicating that a decode function for binary types has been added.
  2. The body of the pull request provides relevant information, including the type of PR, the issue it fixes, and the purpose of the changes.
  3. The changes made in the file test/distributed/cases/function/table_func_metadata_scan.result appear to be correct and aligned with the purpose of the pull request.
  4. The new select statement using the decode function seems to be functioning as intended, calculating the minimum, maximum, and sum of the specified columns.
  5. However, the column aliases in the new select statement are not formatted correctly. The aliases should not include the data types. Instead of "min(decode(min int))", it should be "min(decode(min))". The same applies to the other aliases.
  6. It would be helpful to include some comments or documentation explaining the purpose and usage of the decode function in the code.

Suggestions:

  1. Update the column aliases in the new select statement to remove the data types. For example, change "min(decode(min int))" to "min(decode(min))".
  2. Add comments or documentation explaining the purpose and usage of the decode function in the code. This will help other developers understand the functionality and make future maintenance easier.

Optimization:

  1. Consider adding unit tests for the new decode function to ensure its correctness and prevent regressions in the future.
  2. If applicable, consider refactoring the code to improve readability and maintainability. For example, extracting the decode function into a separate helper function or using more descriptive variable names.

Here are review comments for file test/distributed/cases/function/table_func_metadata_scan.sql:
Review:

Title: Add decode function for binary types

The title of the pull request is clear and concise. It accurately describes the purpose of the changes made in the pull request.

Body:

The body of the pull request is incomplete. It starts with a checklist of different types of pull requests, but none of the options are selected. It would be helpful to select the appropriate type of pull request to provide more context for the reviewers.

The body also mentions that the pull request fixes issue #11396, which is good information to include.

However, the body does not provide any explanation of what the changes in the pull request actually do or why they are necessary. It would be beneficial to provide a brief description of the changes and their purpose.

Changes:

The changes made in the pull request are focused on the file "test/distributed/cases/function/table_func_metadata_scan.sql".

The changes include adding a new SQL query that uses the "decode" function to perform calculations on specific columns in the "metadata_scan" table.

Problem 1: Incomplete body

The body of the pull request is incomplete and does not provide enough information about the changes made. It would be helpful to provide a brief description of the changes and their purpose.

Suggestion 1: Provide a description of the changes in the body of the pull request.

Problem 2: Lack of context in the changes

The changes made in the pull request are focused on a specific SQL file, but there is no explanation of why these changes are necessary or how they fit into the larger codebase.

Suggestion 2: Provide some context or explanation of why these changes are necessary and how they fit into the larger codebase.

Optimization:

The changes made in the pull request seem to be focused on adding a new SQL query. It is difficult to optimize this specific change without more information about the purpose and requirements of the query.

However, in general, it is always a good idea to ensure that new code is properly formatted and follows the existing code style guidelines. It would be beneficial to review the changes and ensure that the formatting and style are consistent with the rest of the codebase.

Additionally, it would be helpful to include relevant test cases for the new functionality to ensure that it is working as expected and does not introduce any regressions.

Overall, the changes made in the pull request seem to be focused on adding a new SQL query. However, the body of the pull request is incomplete and does not provide enough information about the changes. It would be beneficial to provide a description of the changes and their purpose, as well as some context or explanation of why these changes are necessary and how they fit into the larger codebase. Additionally, it is important to ensure that the new code follows the existing code style guidelines and includes relevant test cases.

@fengttt fengttt 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.

This decode function is very MO specific. Please use a different name, such as
mo_decode_meta

Or, use decode(data, 'mo_meta'), see arjun, who has wrote a decode(data, 'hex'),

@aunjgr aunjgr changed the title Add decode function for binary types Add bit_cast function for binary types Sep 8, 2023
@aunjgr

aunjgr commented Sep 8, 2023

Copy link
Copy Markdown
Contributor Author

@fengttt I renamed it to bit_cast, mimicking C++'s std::bit_cast

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/feature size/XXL Denotes a PR that changes 2000+ lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants