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 36f10129517 make CONNECTION_ID() support alias (#19110)
36f10129517 is described below

commit 36f1012951702adc6258127cca0eda8d58a0b7a5
Author: csonezp <[email protected]>
AuthorDate: Thu Jul 14 11:36:08 2022 +0800

    make CONNECTION_ID() support alias (#19110)
    
    * make CONNECTION_ID() support alias
    
    * code style
---
 .../admin/mysql/MySQLAdminExecutorCreator.java     |  2 +-
 .../mysql/executor/ShowConnectionIdExecutor.java   | 22 +++++++++-
 .../executor/ShowConnectionIdExecutorTest.java     | 50 +++++++++++++++++++++-
 3 files changed, 70 insertions(+), 4 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 c835a7dbfdf..ca2f6e4e79d 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
@@ -97,7 +97,7 @@ public final class MySQLAdminExecutorCreator implements 
DatabaseAdminExecutorCre
         }
         if (sqlStatement instanceof SelectStatement) {
             if (isShowSpecialFunction((SelectStatement) sqlStatement, 
ShowConnectionIdExecutor.FUNCTION_NAME)) {
-                return Optional.of(new ShowConnectionIdExecutor());
+                return Optional.of(new 
ShowConnectionIdExecutor((SelectStatement) sqlStatement));
             }
             if (isShowSpecialFunction((SelectStatement) sqlStatement, 
ShowVersionExecutor.FUNCTION_NAME)) {
                 return Optional.of(new ShowVersionExecutor());
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/executor/ShowConnectionIdExecutor.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/executor/ShowConnectionIdExecutor.java
index 486e1c5820f..a418dacd41f 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/executor/ShowConnectionIdExecutor.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/executor/ShowConnectionIdExecutor.java
@@ -26,8 +26,12 @@ 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;
 
 /**
@@ -38,8 +42,14 @@ public final class ShowConnectionIdExecutor implements 
DatabaseAdminQueryExecuto
     
     public static final String FUNCTION_NAME = "connection_id()";
     
+    private SelectStatement sqlStatement;
+    
     private MergedResult mergedResult;
     
+    public ShowConnectionIdExecutor(final SelectStatement sqlStatement) {
+        this.sqlStatement = sqlStatement;
+    }
+    
     @Override
     public void execute(final ConnectionSession connectionSession) {
         mergedResult = new LocalDataMergedResult(Collections.singleton(new 
LocalDataQueryResultRow(connectionSession.getConnectionId())));
@@ -47,6 +57,16 @@ public final class ShowConnectionIdExecutor implements 
DatabaseAdminQueryExecuto
     
     @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/ShowConnectionIdExecutorTest.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/executor/ShowConnectionIdExecutorTest.java
index 8d09eebaf69..7f16ff9aa9f 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/executor/ShowConnectionIdExecutorTest.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/executor/ShowConnectionIdExecutorTest.java
@@ -17,12 +17,21 @@
 
 package org.apache.shardingsphere.proxy.backend.text.admin.mysql.executor;
 
+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;
@@ -34,9 +43,25 @@ public final class ShowConnectionIdExecutorTest {
     
     @Test
     public void assertExecute() throws SQLException {
-        ShowConnectionIdExecutor executor = new ShowConnectionIdExecutor();
+        ShowConnectionIdExecutor executor = new 
ShowConnectionIdExecutor(mockSelectStatement());
         executor.execute(mockConnectionSession());
-        assertThat(executor.getQueryResultMetaData().getColumnCount(), is(1));
+        QueryResultMetaData metaData = executor.getQueryResultMetaData();
+        assertThat(metaData.getColumnCount(), is(1));
+        assertThat(metaData.getColumnName(1), 
is(ShowConnectionIdExecutor.FUNCTION_NAME));
+        assertThat(metaData.getColumnLabel(1), 
is(ShowConnectionIdExecutor.FUNCTION_NAME));
+        while (executor.getMergedResult().next()) {
+            assertThat(executor.getMergedResult().getValue(1, Object.class), 
is(109));
+        }
+    }
+    
+    @Test
+    public void assertExecuteWithAlias() throws SQLException {
+        ShowConnectionIdExecutor executor = new 
ShowConnectionIdExecutor(mockSelectStatementWithAlias());
+        executor.execute(mockConnectionSession());
+        QueryResultMetaData metaData = executor.getQueryResultMetaData();
+        assertThat(metaData.getColumnCount(), is(1));
+        assertThat(metaData.getColumnName(1), 
is(ShowConnectionIdExecutor.FUNCTION_NAME));
+        assertThat(metaData.getColumnLabel(1), is("test_alias"));
         while (executor.getMergedResult().next()) {
             assertThat(executor.getMergedResult().getValue(1, Object.class), 
is(109));
         }
@@ -47,4 +72,25 @@ public final class ShowConnectionIdExecutorTest {
         when(result.getConnectionId()).thenReturn(109);
         return result;
     }
+    
+    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, "connection_id()");
+        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;
+    }
 }

Reply via email to