This is an automated email from the ASF dual-hosted git repository.
zhangliang 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 066d575fa79 make VERSION() support alias (#19188)
066d575fa79 is described below
commit 066d575fa7966bd622839aa76f5161b6985d98e3
Author: csonezp <[email protected]>
AuthorDate: Sat Jul 16 00:06:39 2022 +0800
make VERSION() support alias (#19188)
* make VERSION() support alias
* make filed final
---
.../admin/mysql/MySQLAdminExecutorCreator.java | 2 +-
.../admin/mysql/executor/ShowVersionExecutor.java | 22 ++++++++-
.../mysql/executor/ShowVersionExecutorTest.java | 54 +++++++++++++++++++++-
3 files changed, 73 insertions(+), 5 deletions(-)
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/MySQLAdminExecutorCreator.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/MySQLAdminExecutorCreator.java
index ca2f6e4e79d..a4311987a3e 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/MySQLAdminExecutorCreator.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/MySQLAdminExecutorCreator.java
@@ -100,7 +100,7 @@ public final class MySQLAdminExecutorCreator implements
DatabaseAdminExecutorCre
return Optional.of(new
ShowConnectionIdExecutor((SelectStatement) sqlStatement));
}
if (isShowSpecialFunction((SelectStatement) sqlStatement,
ShowVersionExecutor.FUNCTION_NAME)) {
- return Optional.of(new ShowVersionExecutor());
+ return Optional.of(new ShowVersionExecutor((SelectStatement)
sqlStatement));
}
if (isShowSpecialFunction((SelectStatement) sqlStatement,
ShowCurrentUserExecutor.FUNCTION_NAME)
|| isShowSpecialFunction((SelectStatement) sqlStatement,
ShowCurrentUserExecutor.FUNCTION_NAME_ALIAS)) {
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/executor/ShowVersionExecutor.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/executor/ShowVersionExecutor.java
index 75b56387a92..465817d7a74 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/executor/ShowVersionExecutor.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/executor/ShowVersionExecutor.java
@@ -18,6 +18,7 @@
package org.apache.shardingsphere.proxy.backend.text.admin.mysql.executor;
import lombok.Getter;
+import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLServerInfo;
import
org.apache.shardingsphere.infra.executor.sql.execute.result.query.QueryResultMetaData;
import
org.apache.shardingsphere.infra.executor.sql.execute.result.query.impl.raw.metadata.RawQueryResultColumnMetaData;
@@ -27,20 +28,27 @@ import
org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataMergedRe
import
org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
import
org.apache.shardingsphere.proxy.backend.text.admin.executor.DatabaseAdminQueryExecutor;
+import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ExpressionProjectionSegment;
+import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ProjectionSegment;
+import
org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectStatement;
import java.sql.Types;
+import java.util.Collection;
import java.util.Collections;
/**
* Show version executor.
*/
@Getter
+@RequiredArgsConstructor
public final class ShowVersionExecutor implements DatabaseAdminQueryExecutor {
public static final String FUNCTION_NAME = "version()";
- private MergedResult mergedResult;
+ private final SelectStatement sqlStatement;
+ private MergedResult mergedResult;
+
@Override
public void execute(final ConnectionSession connectionSession) {
mergedResult = new LocalDataMergedResult(Collections.singleton(new
LocalDataQueryResultRow(MySQLServerInfo.getServerVersion(connectionSession.getDatabaseName()))));
@@ -48,6 +56,16 @@ public final class ShowVersionExecutor implements
DatabaseAdminQueryExecutor {
@Override
public QueryResultMetaData getQueryResultMetaData() {
- return new RawQueryResultMetaData(Collections.singletonList(new
RawQueryResultColumnMetaData("", FUNCTION_NAME, FUNCTION_NAME, Types.VARCHAR,
"VARCHAR", 100, 0)));
+ return new RawQueryResultMetaData(Collections.singletonList(new
RawQueryResultColumnMetaData("", FUNCTION_NAME, getLabel(), Types.VARCHAR,
"VARCHAR", 100, 0)));
+ }
+
+ private String getLabel() {
+ Collection<ProjectionSegment> projections =
sqlStatement.getProjections().getProjections();
+ for (ProjectionSegment each : projections) {
+ if (each instanceof ExpressionProjectionSegment) {
+ return ((ExpressionProjectionSegment)
each).getAlias().orElse(FUNCTION_NAME);
+ }
+ }
+ return FUNCTION_NAME;
}
}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/executor/ShowVersionExecutorTest.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/executor/ShowVersionExecutorTest.java
index 2d75f128644..530b6656938 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/executor/ShowVersionExecutorTest.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/executor/ShowVersionExecutorTest.java
@@ -18,12 +18,21 @@
package org.apache.shardingsphere.proxy.backend.text.admin.mysql.executor;
import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLServerInfo;
+import
org.apache.shardingsphere.infra.executor.sql.execute.result.query.QueryResultMetaData;
import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
+import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ExpressionProjectionSegment;
+import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ProjectionSegment;
+import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ProjectionsSegment;
+import
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.AliasSegment;
+import
org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectStatement;
+import
org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
import java.sql.SQLException;
+import java.util.Collection;
+import java.util.LinkedList;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@@ -37,13 +46,54 @@ public final class ShowVersionExecutorTest {
public void assertExecute() throws SQLException {
String databaseName = "test";
MySQLServerInfo.setServerVersion(databaseName, "8.0.26");
- ShowVersionExecutor executor = new ShowVersionExecutor();
+ ShowVersionExecutor executor = new
ShowVersionExecutor(mockSelectStatement());
ConnectionSession connectionSession = mock(ConnectionSession.class);
when(connectionSession.getDatabaseName()).thenReturn(databaseName);
executor.execute(connectionSession);
- assertThat(executor.getQueryResultMetaData().getColumnCount(), is(1));
+ QueryResultMetaData metaData = executor.getQueryResultMetaData();
+ assertThat(metaData.getColumnCount(), is(1));
+ assertThat(metaData.getColumnName(1),
is(ShowVersionExecutor.FUNCTION_NAME));
+ assertThat(metaData.getColumnLabel(1),
is(ShowVersionExecutor.FUNCTION_NAME));
while (executor.getMergedResult().next()) {
assertThat(executor.getMergedResult().getValue(1, Object.class),
is(MySQLServerInfo.getServerVersion(databaseName)));
}
}
+
+ @Test
+ public void assertExecuteWithAlias() throws SQLException {
+ String databaseName = "test";
+ MySQLServerInfo.setServerVersion(databaseName, "8.0.26");
+ ShowVersionExecutor executor = new
ShowVersionExecutor(mockSelectStatementWithAlias());
+ ConnectionSession connectionSession = mock(ConnectionSession.class);
+ when(connectionSession.getDatabaseName()).thenReturn(databaseName);
+ executor.execute(connectionSession);
+ QueryResultMetaData metaData = executor.getQueryResultMetaData();
+ assertThat(metaData.getColumnCount(), is(1));
+ assertThat(metaData.getColumnName(1),
is(ShowVersionExecutor.FUNCTION_NAME));
+ assertThat(metaData.getColumnLabel(1), is("test_alias"));
+ while (executor.getMergedResult().next()) {
+ assertThat(executor.getMergedResult().getValue(1, Object.class),
is(MySQLServerInfo.getServerVersion(databaseName)));
+ }
+ }
+
+ private SelectStatement mockSelectStatement() {
+ Collection<ProjectionSegment> projections = new LinkedList<>();
+ ProjectionsSegment segment = mock(ProjectionsSegment.class);
+ when(segment.getProjections()).thenReturn(projections);
+ SelectStatement result = mock(SelectStatement.class);
+ when(result.getProjections()).thenReturn(segment);
+ return result;
+ }
+
+ private SelectStatement mockSelectStatementWithAlias() {
+ Collection<ProjectionSegment> projections = new LinkedList<>();
+ ExpressionProjectionSegment projectionSegment = new
ExpressionProjectionSegment(0, 0, "version()");
+ projectionSegment.setAlias(new AliasSegment(0, 0, new
IdentifierValue("test_alias")));
+ projections.add(projectionSegment);
+ ProjectionsSegment segment = mock(ProjectionsSegment.class);
+ when(segment.getProjections()).thenReturn(projections);
+ SelectStatement result = mock(SelectStatement.class);
+ when(result.getProjections()).thenReturn(segment);
+ return result;
+ }
}