From 049090acd1913061f314a8ed02dfbb06a98459ba Mon Sep 17 00:00:00 2001 From: ZhangCheng Date: Wed, 25 Dec 2024 14:13:05 +0800 Subject: [PATCH 1/5] Support extracting column name from function for postgresql --- .../extractor/DialectProjectionIdentifierExtractor.java | 8 +++++--- .../extractor/ProjectionIdentifierExtractEngine.java | 8 +++++--- .../dialect/OpenGaussProjectionIdentifierExtractor.java | 6 ++++-- .../dialect/OracleProjectionIdentifierExtractor.java | 5 +++-- .../dialect/PostgreSQLProjectionIdentifierExtractor.java | 6 ++++-- .../select/projection/impl/ExpressionProjection.java | 2 +- .../infra/binder/engine/util/SubqueryTableBindUtils.java | 2 +- .../extractor/ProjectionIdentifierExtractEngineTest.java | 3 ++- .../OpenGaussProjectionIdentifierExtractorTest.java | 9 ++++++++- .../dialect/OracleProjectionIdentifierExtractorTest.java | 3 ++- .../PostgreSQLProjectionIdentifierExtractorTest.java | 9 ++++++++- 11 files changed, 43 insertions(+), 18 deletions(-) diff --git a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/extractor/DialectProjectionIdentifierExtractor.java b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/extractor/DialectProjectionIdentifierExtractor.java index 23603c890284b..3fb98209148c3 100644 --- a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/extractor/DialectProjectionIdentifierExtractor.java +++ b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/extractor/DialectProjectionIdentifierExtractor.java @@ -19,6 +19,7 @@ import org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPI; import org.apache.shardingsphere.infra.spi.annotation.SingletonSPI; +import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.ExpressionSegment; import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.item.SubqueryProjectionSegment; import org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue; @@ -46,12 +47,13 @@ public interface DialectProjectionIdentifierExtractor extends DatabaseTypedSPI { String getColumnNameFromFunction(String functionName, String functionExpression); /** - * Get column name from expression. + * Get column name from expression segment. * - * @param expression expression + * @param expressionSegment expression segment * @return column name */ - String getColumnNameFromExpression(String expression); + + String getColumnNameFromExpression(ExpressionSegment expressionSegment); /** * Get column name from subquery segment. diff --git a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/extractor/ProjectionIdentifierExtractEngine.java b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/extractor/ProjectionIdentifierExtractEngine.java index 5d69ad1c42edf..c7f6d8d271a7c 100644 --- a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/extractor/ProjectionIdentifierExtractEngine.java +++ b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/extractor/ProjectionIdentifierExtractEngine.java @@ -21,6 +21,7 @@ import org.apache.shardingsphere.infra.database.core.metadata.database.enums.QuoteCharacter; import org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPILoader; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; +import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.ExpressionSegment; import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.item.SubqueryProjectionSegment; import org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue; @@ -61,11 +62,12 @@ public String getColumnNameFromFunction(final String functionName, final String /** * Get column name from expression. * - * @param expression expression + * @param expressionSegment expression * @return column name */ - public String getColumnNameFromExpression(final String expression) { - return DatabaseTypedSPILoader.findService(DialectProjectionIdentifierExtractor.class, databaseType).map(optional -> optional.getColumnNameFromExpression(expression)).orElse(expression); + public String getColumnNameFromExpression(final ExpressionSegment expressionSegment) { + return DatabaseTypedSPILoader.findService(DialectProjectionIdentifierExtractor.class, databaseType).map(optional -> optional.getColumnNameFromExpression(expressionSegment)) + .orElse(expressionSegment.getText()); } /** 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 c5ec97785cfe7..eba73687a5421 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 @@ -18,6 +18,8 @@ package org.apache.shardingsphere.infra.binder.context.segment.select.projection.extractor.dialect; 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.SubqueryProjectionSegment; import org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue; @@ -37,8 +39,8 @@ public String getColumnNameFromFunction(final String functionName, final String } @Override - public String getColumnNameFromExpression(final String expression) { - return "?column?"; + public String getColumnNameFromExpression(final ExpressionSegment expressionSegment) { + return expressionSegment instanceof FunctionSegment ? ((FunctionSegment) expressionSegment).getFunctionName() : "?column?"; } @Override diff --git a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/extractor/dialect/OracleProjectionIdentifierExtractor.java b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/extractor/dialect/OracleProjectionIdentifierExtractor.java index 49195645c70dd..c519cc0da2a53 100644 --- a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/extractor/dialect/OracleProjectionIdentifierExtractor.java +++ b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/extractor/dialect/OracleProjectionIdentifierExtractor.java @@ -18,6 +18,7 @@ package org.apache.shardingsphere.infra.binder.context.segment.select.projection.extractor.dialect; 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.item.SubqueryProjectionSegment; import org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue; @@ -37,8 +38,8 @@ public String getColumnNameFromFunction(final String functionName, final String } @Override - public String getColumnNameFromExpression(final String expression) { - return expression.replace(" ", "").toUpperCase(); + public String getColumnNameFromExpression(final ExpressionSegment expressionSegment) { + return expressionSegment.getText().replace(" ", "").toUpperCase(); } @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 720f7f0410b8b..d1cc8f4b44a84 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 @@ -18,6 +18,8 @@ package org.apache.shardingsphere.infra.binder.context.segment.select.projection.extractor.dialect; 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.SubqueryProjectionSegment; import org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue; @@ -37,8 +39,8 @@ public String getColumnNameFromFunction(final String functionName, final String } @Override - public String getColumnNameFromExpression(final String expression) { - return "?column?"; + public String getColumnNameFromExpression(final ExpressionSegment expressionSegment) { + return expressionSegment instanceof FunctionSegment ? ((FunctionSegment) expressionSegment).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 76f93428dc17a..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.getText())); + 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 40d640b910bd5..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.getText()); + return extractEngine.getColumnNameFromExpression(expressionSegment); } } diff --git a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/extractor/ProjectionIdentifierExtractEngineTest.java b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/extractor/ProjectionIdentifierExtractEngineTest.java index 6e51dd14c383e..ce8e96a11d07f 100644 --- a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/extractor/ProjectionIdentifierExtractEngineTest.java +++ b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/extractor/ProjectionIdentifierExtractEngineTest.java @@ -21,6 +21,7 @@ import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.subquery.SubquerySegment; +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; import org.junit.jupiter.api.Test; @@ -50,7 +51,7 @@ void assertGetColumnNameFromFunction() { @Test void assertGetColumnNameFromExpression() { - assertThat(new ProjectionIdentifierExtractEngine(databaseType).getColumnNameFromExpression("expression"), is("expression")); + assertThat(new ProjectionIdentifierExtractEngine(databaseType).getColumnNameFromExpression(new ExpressionProjectionSegment(0, 0, "expression")), is("expression")); } @Test 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 e898c4c23c4b8..0d8924d8c39a7 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 @@ -22,7 +22,9 @@ import org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPILoader; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; +import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.FunctionSegment; import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.subquery.SubquerySegment; +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; import org.junit.jupiter.api.Test; @@ -49,7 +51,12 @@ void assertGetColumnNameFromFunction() { @Test void assertGetColumnNameFromExpression() { - assertThat(extractor.getColumnNameFromExpression("expression"), is("?column?")); + assertThat(extractor.getColumnNameFromExpression(new ExpressionProjectionSegment(0, 0, "expression")), is("?column?")); + } + + @Test + void assertGetColumnNameFromFunctionExpression() { + assertThat(extractor.getColumnNameFromExpression(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/OracleProjectionIdentifierExtractorTest.java b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/extractor/dialect/OracleProjectionIdentifierExtractorTest.java index 563b1eca8d3cb..17ed34e40e0e8 100644 --- a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/extractor/dialect/OracleProjectionIdentifierExtractorTest.java +++ b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/extractor/dialect/OracleProjectionIdentifierExtractorTest.java @@ -23,6 +23,7 @@ import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.subquery.SubquerySegment; +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; import org.junit.jupiter.api.Test; @@ -49,7 +50,7 @@ void assertGetColumnNameFromFunction() { @Test void assertGetColumnNameFromExpression() { - assertThat(extractor.getColumnNameFromExpression("expression"), is("EXPRESSION")); + assertThat(extractor.getColumnNameFromExpression(new ExpressionProjectionSegment(0, 0, "expression")), is("EXPRESSION")); } @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 42f3afbb3bcd4..f838b63029aa4 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 @@ -22,7 +22,9 @@ import org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPILoader; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; +import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.FunctionSegment; import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.subquery.SubquerySegment; +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; import org.junit.jupiter.api.Test; @@ -49,7 +51,12 @@ void assertGetColumnNameFromFunction() { @Test void assertGetColumnNameFromExpression() { - assertThat(extractor.getColumnNameFromExpression("expression"), is("?column?")); + assertThat(extractor.getColumnNameFromExpression(new ExpressionProjectionSegment(0, 0, "expression")), is("?column?")); + } + + @Test + void assertGetColumnNameFromFunctionExpression() { + assertThat(extractor.getColumnNameFromExpression(new FunctionSegment(0, 0, "SUM", "SUM(ID)")), is("SUM")); } @Test From 40812081424fe9660cbee3aa2b7120753a4b1673 Mon Sep 17 00:00:00 2001 From: ZhangCheng Date: Wed, 25 Dec 2024 14:15:42 +0800 Subject: [PATCH 2/5] Support extracting column name from function for postgresql --- .../projection/extractor/ProjectionIdentifierExtractEngine.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/extractor/ProjectionIdentifierExtractEngine.java b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/extractor/ProjectionIdentifierExtractEngine.java index c7f6d8d271a7c..6ba201598ebb0 100644 --- a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/extractor/ProjectionIdentifierExtractEngine.java +++ b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/extractor/ProjectionIdentifierExtractEngine.java @@ -62,7 +62,7 @@ public String getColumnNameFromFunction(final String functionName, final String /** * Get column name from expression. * - * @param expressionSegment expression + * @param expressionSegment expression segment * @return column name */ public String getColumnNameFromExpression(final ExpressionSegment expressionSegment) { From ba855df27c55994bd082063119fe119e4245055b Mon Sep 17 00:00:00 2001 From: ZhangCheng Date: Wed, 25 Dec 2024 14:20:54 +0800 Subject: [PATCH 3/5] Support extracting column name from function for postgresql --- .../segment/select/projection/impl/ExpressionProjection.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 3c1caf1c40088..5f90e6769afae 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)); + return getAlias().map(extractEngine::getIdentifierValue).orElseGet(() -> extractEngine.getColumnNameFromExpression(expressionSegment.getExpr())); } @Override From 6c9e6a7689b73bdfe68ee63697df0aa42f659237 Mon Sep 17 00:00:00 2001 From: ZhangCheng Date: Wed, 25 Dec 2024 14:30:00 +0800 Subject: [PATCH 4/5] Support extracting column name from function for postgresql --- .../infra/binder/engine/util/SubqueryTableBindUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 cafaac7a910f0..08e713872922f 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); + return extractEngine.getColumnNameFromExpression(expressionSegment instanceof ExpressionProjectionSegment ? ((ExpressionProjectionSegment) expressionSegment).getExpr() : expressionSegment); } } From a8c37b7ecb29c3d03176176b4f5145142a272011 Mon Sep 17 00:00:00 2001 From: ZhangCheng Date: Wed, 25 Dec 2024 14:51:51 +0800 Subject: [PATCH 5/5] 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