Skip to content

Commit

Permalink
Support extracting column name from function for postgresql
Browse files Browse the repository at this point in the history
  • Loading branch information
FlyingZC committed Dec 25, 2024
1 parent 6c9e6a7 commit a8c37b7
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.shardingsphere.infra.binder.context.segment.select.projection.extractor.DialectProjectionIdentifierExtractor;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.ExpressionSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.FunctionSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.item.ExpressionProjectionSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.item.SubqueryProjectionSegment;
import org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;

Expand All @@ -40,7 +41,9 @@ public String getColumnNameFromFunction(final String functionName, final String

@Override
public String getColumnNameFromExpression(final ExpressionSegment expressionSegment) {
return expressionSegment instanceof FunctionSegment ? ((FunctionSegment) expressionSegment).getFunctionName() : "?column?";
return expressionSegment instanceof ExpressionProjectionSegment && ((ExpressionProjectionSegment) expressionSegment).getExpr() instanceof FunctionSegment
? ((FunctionSegment) ((ExpressionProjectionSegment) expressionSegment).getExpr()).getFunctionName()
: "?column?";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.shardingsphere.infra.binder.context.segment.select.projection.extractor.DialectProjectionIdentifierExtractor;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.ExpressionSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.FunctionSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.item.ExpressionProjectionSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.item.SubqueryProjectionSegment;
import org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;

Expand All @@ -40,7 +41,9 @@ public String getColumnNameFromFunction(final String functionName, final String

@Override
public String getColumnNameFromExpression(final ExpressionSegment expressionSegment) {
return expressionSegment instanceof FunctionSegment ? ((FunctionSegment) expressionSegment).getFunctionName() : "?column?";
return expressionSegment instanceof ExpressionProjectionSegment && ((ExpressionProjectionSegment) expressionSegment).getExpr() instanceof FunctionSegment
? ((FunctionSegment) ((ExpressionProjectionSegment) expressionSegment).getExpr()).getFunctionName()
: "?column?";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public String getColumnName() {
@Override
public String getColumnLabel() {
ProjectionIdentifierExtractEngine extractEngine = new ProjectionIdentifierExtractEngine(databaseType);
return getAlias().map(extractEngine::getIdentifierValue).orElseGet(() -> extractEngine.getColumnNameFromExpression(expressionSegment.getExpr()));
return getAlias().map(extractEngine::getIdentifierValue).orElseGet(() -> extractEngine.getColumnNameFromExpression(expressionSegment));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,6 @@ private static String getColumnNameFromExpression(final ExpressionSegment expres
if (expressionSegment instanceof AliasAvailable && ((AliasAvailable) expressionSegment).getAlias().isPresent()) {
return extractEngine.getIdentifierValue(((AliasAvailable) expressionSegment).getAlias().get());
}
return extractEngine.getColumnNameFromExpression(expressionSegment instanceof ExpressionProjectionSegment ? ((ExpressionProjectionSegment) expressionSegment).getExpr() : expressionSegment);
return extractEngine.getColumnNameFromExpression(expressionSegment);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void assertGetColumnNameFromExpression() {

@Test
void assertGetColumnNameFromFunctionExpression() {
assertThat(extractor.getColumnNameFromExpression(new FunctionSegment(0, 0, "SUM", "SUM(ID)")), is("SUM"));
assertThat(extractor.getColumnNameFromExpression(new ExpressionProjectionSegment(0, 0, "SUM(ID)", new FunctionSegment(0, 0, "SUM", "SUM(ID)"))), is("SUM"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void assertGetColumnNameFromExpression() {

@Test
void assertGetColumnNameFromFunctionExpression() {
assertThat(extractor.getColumnNameFromExpression(new FunctionSegment(0, 0, "SUM", "SUM(ID)")), is("SUM"));
assertThat(extractor.getColumnNameFromExpression(new ExpressionProjectionSegment(0, 0, "SUM(ID)", new FunctionSegment(0, 0, "SUM", "SUM(ID)"))), is("SUM"));
}

@Test
Expand Down

0 comments on commit a8c37b7

Please sign in to comment.