This is an automated email from the ASF dual-hosted git repository.
sunnianjun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new a90806aa959 Rename ProjectionUtils to
DialectProjectionIdentifierExtractor (#32326)
a90806aa959 is described below
commit a90806aa9592ca9aea7d30c157058fba0925103c
Author: Liang Zhang <[email protected]>
AuthorDate: Tue Jul 30 12:28:15 2024 +0800
Rename ProjectionUtils to DialectProjectionIdentifierExtractor (#32326)
---
.../DialectProjectionIdentifierExtractor.java} | 25 +++++-----
.../projection/impl/AggregationProjection.java | 8 ++--
.../select/projection/impl/ColumnProjection.java | 8 ++--
.../projection/impl/ExpressionProjection.java | 8 ++--
.../select/projection/impl/SubqueryProjection.java | 5 +-
.../binder/engine/util/SubqueryTableBindUtils.java | 7 +--
.../DialectProjectionIdentifierExtractorTest.java | 54 ++++++++++++++++++++++
.../projection/util/ProjectionUtilsTest.java | 52 ---------------------
8 files changed, 87 insertions(+), 80 deletions(-)
diff --git
a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/util/ProjectionUtils.java
b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/extractor/DialectProjectionIdentifierExtractor.java
similarity index 80%
rename from
infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/util/ProjectionUtils.java
rename to
infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/extractor/DialectProjectionIdentifierExtractor.java
index 59630e294f8..41e5502597d 100644
---
a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/util/ProjectionUtils.java
+++
b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/extractor/DialectProjectionIdentifierExtractor.java
@@ -15,10 +15,9 @@
* limitations under the License.
*/
-package
org.apache.shardingsphere.infra.binder.context.segment.select.projection.util;
+package
org.apache.shardingsphere.infra.binder.context.segment.select.projection.extractor;
-import lombok.AccessLevel;
-import lombok.NoArgsConstructor;
+import lombok.RequiredArgsConstructor;
import
org.apache.shardingsphere.infra.database.core.metadata.database.enums.QuoteCharacter;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import
org.apache.shardingsphere.infra.database.opengauss.type.OpenGaussDatabaseType;
@@ -28,19 +27,20 @@ import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.item.Subq
import
org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
/**
- * Projection utility class.
+ * Dialect projection identifier extractor.
*/
-@NoArgsConstructor(access = AccessLevel.PRIVATE)
-public final class ProjectionUtils {
+@RequiredArgsConstructor
+public final class DialectProjectionIdentifierExtractor {
+
+ private final DatabaseType databaseType;
/**
* Get identifier value.
*
* @param identifierValue identifier value
- * @param databaseType database type
* @return identifier value
*/
- public static String getIdentifierValue(final IdentifierValue
identifierValue, final DatabaseType databaseType) {
+ public String getIdentifierValue(final IdentifierValue identifierValue) {
if (QuoteCharacter.NONE != identifierValue.getQuoteCharacter()) {
return identifierValue.getValue();
}
@@ -58,10 +58,9 @@ public final class ProjectionUtils {
*
* @param functionName function name
* @param functionExpression function expression
- * @param databaseType database type
* @return column name
*/
- public static String getColumnNameFromFunction(final String functionName,
final String functionExpression, final DatabaseType databaseType) {
+ public String getColumnNameFromFunction(final String functionName, final
String functionExpression) {
if (databaseType instanceof PostgreSQLDatabaseType || databaseType
instanceof OpenGaussDatabaseType) {
return functionName.toLowerCase();
}
@@ -75,10 +74,9 @@ public final class ProjectionUtils {
* Get column name from expression.
*
* @param expression expression
- * @param databaseType database type
* @return column name
*/
- public static String getColumnNameFromExpression(final String expression,
final DatabaseType databaseType) {
+ public String getColumnNameFromExpression(final String expression) {
if (databaseType instanceof PostgreSQLDatabaseType || databaseType
instanceof OpenGaussDatabaseType) {
return "?column?";
}
@@ -92,10 +90,9 @@ public final class ProjectionUtils {
* Get column name from subquery segment.
*
* @param subquerySegment subquery segment
- * @param databaseType database type
* @return column name
*/
- public static String getColumnNameFromSubquery(final
SubqueryProjectionSegment subquerySegment, final DatabaseType databaseType) {
+ public String getColumnNameFromSubquery(final SubqueryProjectionSegment
subquerySegment) {
// TODO support postgresql subquery projection
if (databaseType instanceof OracleDatabaseType) {
return subquerySegment.getText().replace(" ", "").toUpperCase();
diff --git
a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/impl/AggregationProjection.java
b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/impl/AggregationProjection.java
index e236d67f143..1e2796e427e 100644
---
a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/impl/AggregationProjection.java
+++
b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/impl/AggregationProjection.java
@@ -24,7 +24,7 @@ import lombok.Setter;
import lombok.ToString;
import
org.apache.shardingsphere.infra.binder.context.segment.select.projection.DerivedColumn;
import
org.apache.shardingsphere.infra.binder.context.segment.select.projection.Projection;
-import
org.apache.shardingsphere.infra.binder.context.segment.select.projection.util.ProjectionUtils;
+import
org.apache.shardingsphere.infra.binder.context.segment.select.projection.extractor.DialectProjectionIdentifierExtractor;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import
org.apache.shardingsphere.sql.parser.statement.core.enums.AggregationType;
import
org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
@@ -62,8 +62,10 @@ public class AggregationProjection implements Projection {
@Override
public String getColumnLabel() {
- return getAlias().isPresent() &&
!DerivedColumn.isDerivedColumnName(getAlias().get().getValueWithQuoteCharacters())
? ProjectionUtils.getIdentifierValue(getAlias().get(), databaseType)
- : ProjectionUtils.getColumnNameFromFunction(type.name(),
expression, databaseType);
+ DialectProjectionIdentifierExtractor identifierExtractor = new
DialectProjectionIdentifierExtractor(databaseType);
+ return getAlias().isPresent() &&
!DerivedColumn.isDerivedColumnName(getAlias().get().getValueWithQuoteCharacters())
+ ? identifierExtractor.getIdentifierValue(getAlias().get())
+ : identifierExtractor.getColumnNameFromFunction(type.name(),
expression);
}
@Override
diff --git
a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/impl/ColumnProjection.java
b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/impl/ColumnProjection.java
index 54ad62a1a3c..43d95f488ad 100644
---
a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/impl/ColumnProjection.java
+++
b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/impl/ColumnProjection.java
@@ -24,7 +24,7 @@ import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import
org.apache.shardingsphere.infra.binder.context.segment.select.projection.Projection;
-import
org.apache.shardingsphere.infra.binder.context.segment.select.projection.util.ProjectionUtils;
+import
org.apache.shardingsphere.infra.binder.context.segment.select.projection.extractor.DialectProjectionIdentifierExtractor;
import
org.apache.shardingsphere.infra.database.core.metadata.database.enums.QuoteCharacter;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.database.mysql.type.MySQLDatabaseType;
@@ -61,12 +61,14 @@ public final class ColumnProjection implements Projection {
@Override
public String getColumnName() {
- return databaseType instanceof MySQLDatabaseType ?
ProjectionUtils.getIdentifierValue(name, databaseType) : getColumnLabel();
+ DialectProjectionIdentifierExtractor identifierExtractor = new
DialectProjectionIdentifierExtractor(databaseType);
+ return databaseType instanceof MySQLDatabaseType ?
identifierExtractor.getIdentifierValue(name) : getColumnLabel();
}
@Override
public String getColumnLabel() {
- return getAlias().isPresent() ?
ProjectionUtils.getIdentifierValue(getAlias().get(), databaseType) :
ProjectionUtils.getIdentifierValue(name, databaseType);
+ DialectProjectionIdentifierExtractor identifierExtractor = new
DialectProjectionIdentifierExtractor(databaseType);
+ return getAlias().isPresent() ?
identifierExtractor.getIdentifierValue(getAlias().get()) :
identifierExtractor.getIdentifierValue(name);
}
@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 87febb332b5..2554d787c26 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
@@ -22,7 +22,7 @@ import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
import
org.apache.shardingsphere.infra.binder.context.segment.select.projection.Projection;
-import
org.apache.shardingsphere.infra.binder.context.segment.select.projection.util.ProjectionUtils;
+import
org.apache.shardingsphere.infra.binder.context.segment.select.projection.extractor.DialectProjectionIdentifierExtractor;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.item.ExpressionProjectionSegment;
import
org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
@@ -51,8 +51,10 @@ public final class ExpressionProjection implements
Projection {
@Override
public String getColumnLabel() {
- return getAlias().isPresent() ?
ProjectionUtils.getIdentifierValue(getAlias().get(), databaseType)
- :
ProjectionUtils.getColumnNameFromExpression(expressionSegment.getText(),
databaseType);
+ DialectProjectionIdentifierExtractor identifierExtractor = new
DialectProjectionIdentifierExtractor(databaseType);
+ return getAlias().isPresent()
+ ? identifierExtractor.getIdentifierValue(getAlias().get())
+ :
identifierExtractor.getColumnNameFromExpression(expressionSegment.getText());
}
@Override
diff --git
a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/impl/SubqueryProjection.java
b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/impl/SubqueryProjection.java
index 1a89e8878f4..2a4f938d16c 100644
---
a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/impl/SubqueryProjection.java
+++
b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/impl/SubqueryProjection.java
@@ -22,7 +22,7 @@ import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
import
org.apache.shardingsphere.infra.binder.context.segment.select.projection.Projection;
-import
org.apache.shardingsphere.infra.binder.context.segment.select.projection.util.ProjectionUtils;
+import
org.apache.shardingsphere.infra.binder.context.segment.select.projection.extractor.DialectProjectionIdentifierExtractor;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.item.SubqueryProjectionSegment;
import
org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
@@ -53,7 +53,8 @@ public final class SubqueryProjection implements Projection {
@Override
public String getColumnLabel() {
- return getAlias().isPresent() ?
ProjectionUtils.getIdentifierValue(getAlias().get(), databaseType) :
ProjectionUtils.getColumnNameFromSubquery(subquerySegment, databaseType);
+ DialectProjectionIdentifierExtractor identifierExtractor = new
DialectProjectionIdentifierExtractor(databaseType);
+ return getAlias().isPresent() ?
identifierExtractor.getIdentifierValue(getAlias().get()) :
identifierExtractor.getColumnNameFromSubquery(subquerySegment);
}
@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 5afb4d5da87..6b29f6cca07 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
@@ -20,7 +20,7 @@ package org.apache.shardingsphere.infra.binder.engine.util;
import com.google.common.base.Strings;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
-import
org.apache.shardingsphere.infra.binder.context.segment.select.projection.util.ProjectionUtils;
+import
org.apache.shardingsphere.infra.binder.context.segment.select.projection.extractor.DialectProjectionIdentifierExtractor;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeRegistry;
import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.column.ColumnSegment;
@@ -96,10 +96,11 @@ public final class SubqueryTableBindUtils {
private static String getColumnNameFromExpression(final ExpressionSegment
expressionSegment, final DatabaseType databaseType) {
String result;
+ DialectProjectionIdentifierExtractor identifierExtractor = new
DialectProjectionIdentifierExtractor(databaseType);
if (expressionSegment instanceof AliasAvailable && ((AliasAvailable)
expressionSegment).getAlias().isPresent()) {
- result = ProjectionUtils.getIdentifierValue(((AliasAvailable)
expressionSegment).getAlias().get(), databaseType);
+ result = identifierExtractor.getIdentifierValue(((AliasAvailable)
expressionSegment).getAlias().get());
} else {
- result =
ProjectionUtils.getColumnNameFromExpression(expressionSegment.getText(),
databaseType);
+ result =
identifierExtractor.getColumnNameFromExpression(expressionSegment.getText());
}
return result;
}
diff --git
a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/engine/segment/select/projection/extractor/DialectProjectionIdentifierExtractorTest.java
b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/engine/segment/select/projection/extractor/DialectProjectionIdentifierExtractorTest.java
new file mode 100644
index 00000000000..85dbbc61948
--- /dev/null
+++
b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/engine/segment/select/projection/extractor/DialectProjectionIdentifierExtractorTest.java
@@ -0,0 +1,54 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package
org.apache.shardingsphere.infra.binder.engine.segment.select.projection.extractor;
+
+import
org.apache.shardingsphere.infra.binder.context.segment.select.projection.extractor.DialectProjectionIdentifierExtractor;
+import
org.apache.shardingsphere.infra.database.core.metadata.database.enums.QuoteCharacter;
+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.value.identifier.IdentifierValue;
+import org.junit.jupiter.api.Test;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+
+class DialectProjectionIdentifierExtractorTest {
+
+ @Test
+ void assertGetIdentifierValue() {
+ assertThat(new
DialectProjectionIdentifierExtractor(TypedSPILoader.getService(DatabaseType.class,
"PostgreSQL")).getIdentifierValue(new IdentifierValue("Data",
QuoteCharacter.QUOTE)),
+ is("Data"));
+ IdentifierValue identifierValue = new IdentifierValue("Data",
QuoteCharacter.NONE);
+ assertThat(new
DialectProjectionIdentifierExtractor(TypedSPILoader.getService(DatabaseType.class,
"PostgreSQL")).getIdentifierValue(identifierValue), is("data"));
+ assertThat(new
DialectProjectionIdentifierExtractor(TypedSPILoader.getService(DatabaseType.class,
"openGauss")).getIdentifierValue(identifierValue), is("data"));
+ assertThat(new
DialectProjectionIdentifierExtractor(TypedSPILoader.getService(DatabaseType.class,
"Oracle")).getIdentifierValue(identifierValue), is("DATA"));
+ assertThat(new
DialectProjectionIdentifierExtractor(TypedSPILoader.getService(DatabaseType.class,
"MySQL")).getIdentifierValue(identifierValue), is("Data"));
+ }
+
+ @Test
+ void assertGetColumnNameFromFunction() {
+ String functionName = "Function";
+ String functionExpression = "FunctionExpression";
+ assertThat(new
DialectProjectionIdentifierExtractor(TypedSPILoader.getService(DatabaseType.class,
"PostgreSQL")).getColumnNameFromFunction(functionName, functionExpression),
is("function"));
+ assertThat(new
DialectProjectionIdentifierExtractor(TypedSPILoader.getService(DatabaseType.class,
"openGauss")).getColumnNameFromFunction(functionName, functionExpression),
is("function"));
+ assertThat(new
DialectProjectionIdentifierExtractor(TypedSPILoader.getService(DatabaseType.class,
"Oracle")).getColumnNameFromFunction(functionName, functionExpression),
+ is("FUNCTIONEXPRESSION"));
+ assertThat(new
DialectProjectionIdentifierExtractor(TypedSPILoader.getService(DatabaseType.class,
"MySQL")).getColumnNameFromFunction(functionName, functionExpression),
+ is("FunctionExpression"));
+ }
+}
diff --git
a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/engine/segment/select/projection/util/ProjectionUtilsTest.java
b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/engine/segment/select/projection/util/ProjectionUtilsTest.java
deleted file mode 100644
index b8591a1bcda..00000000000
---
a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/engine/segment/select/projection/util/ProjectionUtilsTest.java
+++ /dev/null
@@ -1,52 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package
org.apache.shardingsphere.infra.binder.engine.segment.select.projection.util;
-
-import
org.apache.shardingsphere.infra.binder.context.segment.select.projection.util.ProjectionUtils;
-import
org.apache.shardingsphere.infra.database.core.metadata.database.enums.QuoteCharacter;
-import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
-import
org.apache.shardingsphere.infra.database.postgresql.type.PostgreSQLDatabaseType;
-import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
-import
org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
-import org.junit.jupiter.api.Test;
-
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.is;
-
-class ProjectionUtilsTest {
-
- @Test
- void assertGetIdentifierValue() {
- assertThat(ProjectionUtils.getIdentifierValue(new
IdentifierValue("Data", QuoteCharacter.QUOTE), new PostgreSQLDatabaseType()),
is("Data"));
- IdentifierValue identifierValue = new IdentifierValue("Data",
QuoteCharacter.NONE);
- assertThat(ProjectionUtils.getIdentifierValue(identifierValue,
TypedSPILoader.getService(DatabaseType.class, "PostgreSQL")), is("data"));
- assertThat(ProjectionUtils.getIdentifierValue(identifierValue,
TypedSPILoader.getService(DatabaseType.class, "openGauss")), is("data"));
- assertThat(ProjectionUtils.getIdentifierValue(identifierValue,
TypedSPILoader.getService(DatabaseType.class, "Oracle")), is("DATA"));
- assertThat(ProjectionUtils.getIdentifierValue(identifierValue,
TypedSPILoader.getService(DatabaseType.class, "MySQL")), is("Data"));
- }
-
- @Test
- void assertGetColumnNameFromFunction() {
- String functionName = "Function";
- String functionExpression = "FunctionExpression";
- assertThat(ProjectionUtils.getColumnNameFromFunction(functionName,
functionExpression, TypedSPILoader.getService(DatabaseType.class,
"PostgreSQL")), is("function"));
- assertThat(ProjectionUtils.getColumnNameFromFunction(functionName,
functionExpression, TypedSPILoader.getService(DatabaseType.class,
"openGauss")), is("function"));
- assertThat(ProjectionUtils.getColumnNameFromFunction(functionName,
functionExpression, TypedSPILoader.getService(DatabaseType.class, "Oracle")),
is("FUNCTIONEXPRESSION"));
- assertThat(ProjectionUtils.getColumnNameFromFunction(functionName,
functionExpression, TypedSPILoader.getService(DatabaseType.class, "MySQL")),
is("FunctionExpression"));
- }
-}