Skip to content

Enhance SQLUtils getExpressionWithoutOutsideParentheses #34804

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

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -161,18 +161,25 @@ public static String getExactlyExpression(final String value) {
*/
public static String getExpressionWithoutOutsideParentheses(final String value) {
int parenthesesOffset = getParenthesesOffset(value);
return 0 == parenthesesOffset ? value : value.substring(parenthesesOffset, value.length() - parenthesesOffset);
String result = 0 == parenthesesOffset ? value : value.substring(parenthesesOffset, value.length() - parenthesesOffset);
if (isValidParenthesis(result)) {
return result;
}
return value;
}

private static int getParenthesesOffset(final String value) {
int result = 0;
int left = 0;
if (Strings.isNullOrEmpty(value)) {
return result;
return left;
}
while (Paren.PARENTHESES.getLeftParen() == value.charAt(result)) {
result++;

int right = value.length() - 1;
while (Paren.PARENTHESES.getLeftParen() == value.charAt(left) && Paren.PARENTHESES.getRightParen() == value.charAt(right)) {
left++;
right--;
}
return result;
return left;
}

/**
Expand Down Expand Up @@ -261,4 +268,25 @@ public static String trimComment(final String sql) {
}
return result.trim();
}

/**
* Check for valid parenthesis in String.
*
* @param text to be checked for valid parenthesis
* @return true or false
*/
public static boolean isValidParenthesis(final String text) {
int count = 0;
for (char c : text.toCharArray()) {
if (Paren.PARENTHESES.getLeftParen() == c) {
count++;
} else if (Paren.PARENTHESES.getRightParen() == c) {
if (count == 0) {
return false;
}
count--;
}
}
return count == 0;
}
}
10 changes: 5 additions & 5 deletions test/it/parser/src/main/resources/case/dml/select-join.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1388,7 +1388,7 @@
<shorthand-projection name="*" start-index="86" stop-index="103">
<owner name="planForceDetails" start-index="86" stop-index="101"/>
</shorthand-projection>
<expression-projection start-index="106" stop-index="255" text="regressedPlanExecutionCount + recommendedPlanExecutionCount) * (regressedPlanCpuTimeAverage - recommendedPlanCpuTimeAverage)/100000" alias="estimated_gain">
<expression-projection start-index="106" stop-index="255" text="(regressedPlanExecutionCount + recommendedPlanExecutionCount) * (regressedPlanCpuTimeAverage - recommendedPlanCpuTimeAverage)/1000000" alias="estimated_gain">
<expr>
<binary-operation-expression start-index="123" stop-index="255">
<left>
Expand Down Expand Up @@ -2108,7 +2108,7 @@
<column-projection name="text" start-index="7" stop-index="12">
<owner name="t" start-index="7" stop-index="7"/>
</column-projection>
<expression-projection start-index="15" stop-index="83" text="qs.total_elapsed_time/1000) / qs.execution_coun" alias="avg_elapsed_time">
<expression-projection start-index="15" stop-index="83" text="(qs.total_elapsed_time/1000) / qs.execution_count" alias="avg_elapsed_time">
<expr>
<binary-operation-expression start-index="15" stop-index="63">
<left>
Expand All @@ -2133,7 +2133,7 @@
</binary-operation-expression>
</expr>
</expression-projection>
<expression-projection text="qs.total_worker_time/1000) / qs.execution_coun" start-index="86" stop-index="149" alias="avg_cpu_time">
<expression-projection text="(qs.total_worker_time/1000) / qs.execution_count" start-index="86" stop-index="149" alias="avg_cpu_time">
<expr>
<binary-operation-expression start-index="86" stop-index="133">
<left>
Expand All @@ -2158,7 +2158,7 @@
</binary-operation-expression>
</expr>
</expression-projection>
<expression-projection text="qs.total_elapsed_time/1000) / qs.execution_count ) - ((qs.total_worker_time/1000) / qs.execution_coun" start-index="152" stop-index="273" alias="avg_wait_time">
<expression-projection text="((qs.total_elapsed_time/1000) / qs.execution_count ) - ((qs.total_worker_time/1000) / qs.execution_count)" start-index="152" stop-index="273" alias="avg_wait_time">
<expr>
<binary-operation-expression start-index="152" stop-index="256">
<left>
Expand Down Expand Up @@ -2815,7 +2815,7 @@
<shorthand-projection start-index="83" stop-index="100">
<owner name="planForceDetails" start-index="83" stop-index="98" />
</shorthand-projection>
<expression-projection alias="estimated_gain" text="regressedPlanExecutionCount + recommendedPlanExecutionCount) * (regressedPlanCpuTimeAverage - recommendedPlanCpuTimeAverage) / 100000" start-index="102" stop-index="253">
<expression-projection alias="estimated_gain" text="(regressedPlanExecutionCount + recommendedPlanExecutionCount) * (regressedPlanCpuTimeAverage - recommendedPlanCpuTimeAverage) / 1000000" start-index="102" stop-index="253">
<expr>
<binary-operation-expression start-index="119" stop-index="253">
<left>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5000,7 +5000,7 @@

<select sql-case-id="select_not_in">
<projections start-index="7" stop-index="33">
<expression-projection start-index="7" stop-index="33" text="3,4) NOT IN ((1,2), (3,4)">
<expression-projection start-index="7" stop-index="33" text="(3,4) NOT IN ((1,2), (3,4))">
<expr>
<in-expression start-index="7" stop-index="33">
<left>
Expand Down
2 changes: 1 addition & 1 deletion test/it/parser/src/main/resources/case/dml/select.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7683,7 +7683,7 @@
<column-projection name="empid" start-index="75" stop-index="81" literal-start-index="75" literal-stop-index="81">`
<owner name="a" start-index="75" stop-index="75" literal-start-index="75" literal-stop-index="75" />
</column-projection>
<expression-projection text="a.enddate - term + term2 + 1) / (last_day(term) - term + 1" alias="cnt" start-index="8" stop-index="72" literal-start-index="8" literal-stop-index="72">
<expression-projection text="(a.enddate - term + term2 + 1) / (last_day(term) - term + 1)" alias="cnt" start-index="8" stop-index="72" literal-start-index="8" literal-stop-index="72">
<literalText>a.enddate - term + term2 + 1) / (last_day(term) - term + 1</literalText>
<expr>
<binary-operation-expression start-index="8" stop-index="67" literal-start-index="8" literal-stop-index="67">
Expand Down
Loading