From a8c37b7ecb29c3d03176176b4f5145142a272011 Mon Sep 17 00:00:00 2001 From: ZhangCheng Date: Wed, 25 Dec 2024 14:51:51 +0800 Subject: [PATCH] Support extracting column name from function for postgresql --- .../dialect/OpenGaussProjectionIdentifierExtractor.java | 5 ++++- .../dialect/PostgreSQLProjectionIdentifierExtractor.java | 5 ++++- .../segment/select/projection/impl/ExpressionProjection.java | 2 +- .../infra/binder/engine/util/SubqueryTableBindUtils.java | 2 +- .../dialect/OpenGaussProjectionIdentifierExtractorTest.java | 2 +- .../dialect/PostgreSQLProjectionIdentifierExtractorTest.java | 2 +- 6 files changed, 12 insertions(+), 6 deletions(-) diff --git a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/extractor/dialect/OpenGaussProjectionIdentifierExtractor.java b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/extractor/dialect/OpenGaussProjectionIdentifierExtractor.java index eba73687a5421..9471b600a5cb3 100644 --- a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/extractor/dialect/OpenGaussProjectionIdentifierExtractor.java +++ b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/extractor/dialect/OpenGaussProjectionIdentifierExtractor.java @@ -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; @@ -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 diff --git a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/extractor/dialect/PostgreSQLProjectionIdentifierExtractor.java b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/extractor/dialect/PostgreSQLProjectionIdentifierExtractor.java index d1cc8f4b44a84..fa641d5cc74fa 100644 --- a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/extractor/dialect/PostgreSQLProjectionIdentifierExtractor.java +++ b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/extractor/dialect/PostgreSQLProjectionIdentifierExtractor.java @@ -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; @@ -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 diff --git a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/impl/ExpressionProjection.java b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/impl/ExpressionProjection.java index 5f90e6769afae..3c1caf1c40088 100644 --- a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/impl/ExpressionProjection.java +++ b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/impl/ExpressionProjection.java @@ -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 diff --git a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/engine/util/SubqueryTableBindUtils.java b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/engine/util/SubqueryTableBindUtils.java index 08e713872922f..cafaac7a910f0 100644 --- a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/engine/util/SubqueryTableBindUtils.java +++ b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/engine/util/SubqueryTableBindUtils.java @@ -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); } } diff --git a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/extractor/dialect/OpenGaussProjectionIdentifierExtractorTest.java b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/extractor/dialect/OpenGaussProjectionIdentifierExtractorTest.java index 0d8924d8c39a7..8afc81ef87d70 100644 --- a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/extractor/dialect/OpenGaussProjectionIdentifierExtractorTest.java +++ b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/extractor/dialect/OpenGaussProjectionIdentifierExtractorTest.java @@ -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 diff --git a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/extractor/dialect/PostgreSQLProjectionIdentifierExtractorTest.java b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/extractor/dialect/PostgreSQLProjectionIdentifierExtractorTest.java index f838b63029aa4..1bdfcd2b863c5 100644 --- a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/extractor/dialect/PostgreSQLProjectionIdentifierExtractorTest.java +++ b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/extractor/dialect/PostgreSQLProjectionIdentifierExtractorTest.java @@ -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