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

[BugFix] Fix union isnullable error #32426

Merged
merged 1 commit into from
Oct 11, 2023
Merged

[BugFix] Fix union isnullable error #32426

merged 1 commit into from
Oct 11, 2023

Conversation

Seaven
Copy link
Contributor

@Seaven Seaven commented Oct 10, 2023

Fixes #issue

For SQL:

SELECT t1.c_1_0
FROM t1
RIGHT JOIN t0 ON t1.c_1_0 = t0.c_0_1
UNION ALL
SELECT t1.c_1_0
FROM t1
RIGHT JOIN t0 ON t1.c_1_0 = t0.c_0_1    

t1 must empty table, the optimizer will trans SQL to

SELECT NULL
FROM t0 
UNION ALL
SELECT NULL
FROM t0 

the union set output expr's isnullable error

What type of PR is this:

  • BugFix
  • Feature
  • Enhancement
  • Refactor
  • UT
  • Doc
  • Tool

Does this PR entail a change in behavior?

  • Yes, this PR will result in a change in behavior.
  • No, this PR will not result in a change in behavior.

If yes, please specify the type of change:

  • Interface/UI changes: syntax, type conversion, expression evaluation, display information
  • Parameter changes: default values, similar parameters but with different default values
  • Policy changes: use new policy to replace old one, functionality automatically enabled
  • Feature removed
  • Miscellaneous: upgrade & downgrade compatibility, etc.

Checklist:

  • I have added test cases for my bug fix or my new feature
  • This pr needs user documentation (for new or modified features or behaviors)
    • I have added documentation for my new feature or new function

Bugfix cherry-pick branch check:

  • I have checked the version labels which the pr will be auto-backported to the target branch
    • 3.2
    • 3.1
    • 3.0
    • 2.5

Expr childExpr = ScalarOperatorToExpr.buildExecExpression(childRef, formatterContext);
isNullable |= childExpr.isNullable();
}
slotDesc.setIsNullable(isNullable);
setOutputList.add(new SlotRef(String.valueOf(columnRefOperator.getId()), slotDesc));
}
setOperationTuple.computeMemLayout();
Copy link

Choose a reason for hiding this comment

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

Here are some improvement suggestions for the code patch:

Bug Risks:

  • The code does not seem to have any evident bug risks.

Naming:

  • Consider using more descriptive variable names instead of abbreviations like optExpr, optExpr, and setOutputList.

Code Style:

  • Utilize consistent indentation throughout the code.
  • Avoid mixing different styles of iteration. Stick to one style, either using a foreach loop or an indexed loop.

Best Practices:

  • Instead of using Lists.newArrayList(), consider using the diamond operator to initialize the setOutputList as new ArrayList<>().
  • Extract the logic inside the for loop into a separate method to improve the code's readability and maintainability.

Compatibility:

  • No specific compatibility concerns identified in the provided code snippet.

Simplicity:

  • Consider splitting the functionality in the for loop into separate methods or functions to improve code modularity and simplify the logic.

Optimization Points:

  • If performance is a concern, cache the size of setOperation.getOutputColumnRefOp() before the loop condition check to avoid repetitive method calls.

Overall, the code appears to be functional, but there is room for improvement in terms of code style, clarity, and naming conventions.

Expr childExpr = ScalarOperatorToExpr.buildExecExpression(childRef, formatterContext);
isNullable |= childExpr.isNullable();
}
slotDesc.setIsNullable(isNullable);
setOutputList.add(new SlotRef(String.valueOf(columnRefOperator.getId()), slotDesc));
}
setOperationTuple.computeMemLayout();
Copy link

Choose a reason for hiding this comment

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

Code Review Feedback:

  • Bug Risk: The code appears to be fine from a bug risk perspective.
  • Naming: The variable names are generally meaningful and descriptive.
  • Code Style: The code follows indentation and formatting conventions.
  • Best Practices: There are some opportunities for improvement in terms of best practices.
  • Compatibility: The code doesn't seem to have any compatibility issues.
  • Simplicity: The code could be simplified and made more readable.
  • Optimization Points: Some performance optimization points can be addressed.

Code Improvement Suggestions:

  1. Use enhanced for loop: Instead of using a traditional for loop with an index, you can use an enhanced for loop to iterate over the setOperation.getOutputColumnRefOp() list. This will simplify the loop and make it more readable.

    for (ColumnRefOperator columnRefOperator : setOperation.getOutputColumnRefOp()) {
        // Rest of the loop body
    }
  2. Combine bitwise OR operations: In the line boolean isNullable = slotDesc.getIsNullable() | setOperationNode.isHasNullableGenerateChild();, you can combine the bitwise OR operation with the assignment in a single statement. This will make the code more concise.

    boolean isNullable = slotDesc.getIsNullable() || setOperationNode.isHasNullableGenerateChild();
  3. Simplify nullable check inside the loop: Instead of calculating the isNullable value in a separate loop, you can move that logic inside the main loop by accessing the child output columns directly. This will eliminate the need for an additional loop.

    for (ColumnRefOperator columnRefOperator : setOperation.getOutputColumnRefOp()) {
        // Existing code
        // ...
        for (List<ColumnRefOperator> childOutputColumn : setOperation.getChildOutputColumns()) {
            ColumnRefOperator childRef = childOutputColumn.get(index);
            Expr childExpr = ScalarOperatorToExpr.buildExecExpression(childRef, formatterContext);
            isNullable |= childExpr.isNullable();
        }
        // Existing code
        // ...
    }
  4. Use StringBuilder for SlotId conversion: Instead of creating a new String object every time in new SlotRef(String.valueOf(columnRefOperator.getId()), slotDesc), you can use a StringBuilder to improve efficiency.

    new SlotRef(new StringBuilder().append(columnRefOperator.getId()).toString(), slotDesc)
  5. Optimize computeMemLayout(): Consider optimizing the computeMemLayout() call after adding elements to setOutputList. If computeMemLayout() is an expensive operation, you can delay the call until after the loop, reducing redundant calculations if applicable.

Note: These suggestions focus on the provided code patch and may not capture potential issues or optimizations in the larger context of the codebase.

@sonarcloud
Copy link

sonarcloud bot commented Oct 10, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 2 Code Smells

0.0% 0.0% Coverage
0.0% 0.0% Duplication

@wanpengfei-git
Copy link
Collaborator

[FE Incremental Coverage Report]

😍 pass : 9 / 9 (100.00%)

file detail

path covered_line new_line coverage not_covered_line_detail
🔵 com/starrocks/sql/plan/PlanFragmentBuilder.java 9 9 100.00% []

@wanpengfei-git
Copy link
Collaborator

[BE Incremental Coverage Report]

😍 pass : 0 / 0 (0%)

@packy92 packy92 merged commit c39da42 into StarRocks:main Oct 11, 2023
47 checks passed
@wanpengfei-git
Copy link
Collaborator

@Mergifyio backport branch-3.2

@github-actions github-actions bot removed the 3.2 label Oct 11, 2023
@mergify
Copy link
Contributor

mergify bot commented Oct 11, 2023

backport branch-3.2

✅ Backports have been created

mergify bot pushed a commit that referenced this pull request Oct 11, 2023
Signed-off-by: Seaven <[email protected]>
(cherry picked from commit c39da42)
wanpengfei-git pushed a commit that referenced this pull request Oct 11, 2023
Signed-off-by: Seaven <[email protected]>
(cherry picked from commit c39da42)
@Seaven
Copy link
Contributor Author

Seaven commented Mar 14, 2024

@mergify backport branch-3.1 branch-3.0 branch-2.5

@github-actions github-actions bot added the 2.5 label Mar 14, 2024
Copy link
Contributor

mergify bot commented Mar 14, 2024

backport branch-3.1 branch-3.0 branch-2.5

✅ Backports have been created

mergify bot pushed a commit that referenced this pull request Mar 14, 2024
Signed-off-by: Seaven <[email protected]>
(cherry picked from commit c39da42)

# Conflicts:
#	fe/fe-core/src/test/java/com/starrocks/sql/plan/EmptyValueTest.java
mergify bot pushed a commit that referenced this pull request Mar 14, 2024
Signed-off-by: Seaven <[email protected]>
(cherry picked from commit c39da42)

# Conflicts:
#	fe/fe-core/src/test/java/com/starrocks/sql/plan/EmptyValueTest.java
mergify bot pushed a commit that referenced this pull request Mar 14, 2024
Signed-off-by: Seaven <[email protected]>
(cherry picked from commit c39da42)

# Conflicts:
#	fe/fe-core/src/main/java/com/starrocks/sql/plan/PlanFragmentBuilder.java
#	fe/fe-core/src/test/java/com/starrocks/sql/plan/EmptyValueTest.java
Seaven added a commit to Seaven/starrocks that referenced this pull request Mar 14, 2024
Seaven added a commit to Seaven/starrocks that referenced this pull request Mar 14, 2024
Seaven added a commit to Seaven/starrocks that referenced this pull request Mar 14, 2024
packy92 pushed a commit that referenced this pull request Mar 14, 2024
packy92 pushed a commit that referenced this pull request Mar 14, 2024
Seaven added a commit that referenced this pull request Mar 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants