You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2022/07/22 02:32:02 UTC

[shardingsphere] branch master updated: Improve ParseDistSQLHandler & add parse test case for PostgreSQL. (#19451)

This is an automated email from the ASF dual-hosted git repository.

panjuan 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 9621d0d9e79 Improve ParseDistSQLHandler & add parse test case for PostgreSQL. (#19451)
9621d0d9e79 is described below

commit 9621d0d9e793c97c4787bb1dbc8b142f93961b36
Author: Raigor <ra...@gmail.com>
AuthorDate: Fri Jul 22 10:31:56 2022 +0800

    Improve ParseDistSQLHandler & add parse test case for PostgreSQL. (#19451)
---
 .../distsql/ral/advanced/ParseDistSQLHandler.java    | 13 ++-----------
 .../distsql/ral/advance/ParseDistSQLHandlerTest.java | 20 ++++++++++++++++++--
 2 files changed, 20 insertions(+), 13 deletions(-)

diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/advanced/ParseDistSQLHandler.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/advanced/ParseDistSQLHandler.java
index 246b0636e18..756255b6674 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/advanced/ParseDistSQLHandler.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/advanced/ParseDistSQLHandler.java
@@ -17,14 +17,11 @@
 
 package org.apache.shardingsphere.proxy.backend.text.distsql.ral.advanced;
 
-import com.google.common.base.Strings;
 import com.google.gson.Gson;
 import org.apache.shardingsphere.distsql.parser.statement.ral.advanced.ParseStatement;
-import org.apache.shardingsphere.infra.database.type.DatabaseType;
 import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
 import org.apache.shardingsphere.mode.manager.ContextManager;
 import org.apache.shardingsphere.parser.rule.SQLParserRule;
-import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
 import org.apache.shardingsphere.proxy.backend.text.distsql.ral.QueryableRALBackendHandler;
 import org.apache.shardingsphere.sql.parser.exception.SQLParsingException;
 import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
@@ -55,17 +52,11 @@ public final class ParseDistSQLHandler extends QueryableRALBackendHandler<ParseS
     
     private SQLStatement parseSQL(final ContextManager contextManager) {
         SQLParserRule sqlParserRule = contextManager.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getSingleRule(SQLParserRule.class);
+        String databaseType = getConnectionSession().getDatabaseType().getType();
         try {
-            return sqlParserRule.getSQLParserEngine(getStorageType().getType()).parse(getSqlStatement().getSql(), false);
+            return sqlParserRule.getSQLParserEngine(databaseType).parse(getSqlStatement().getSql(), false);
         } catch (final SQLParsingException ex) {
             throw new SQLParsingException("You have a syntax error in your parsed statement");
         }
     }
-    
-    private DatabaseType getStorageType() {
-        String databaseName = getConnectionSession().getDatabaseName();
-        return Strings.isNullOrEmpty(databaseName) || !ProxyContext.getInstance().databaseExists(databaseName)
-                ? getConnectionSession().getDatabaseType()
-                : ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getDatabase(databaseName).getResource().getDatabaseType();
-    }
 }
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/advance/ParseDistSQLHandlerTest.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/advance/ParseDistSQLHandlerTest.java
index 168e603caf1..5567ed78f39 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/advance/ParseDistSQLHandlerTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/advance/ParseDistSQLHandlerTest.java
@@ -20,6 +20,7 @@ package org.apache.shardingsphere.proxy.backend.text.distsql.ral.advance;
 import com.google.gson.Gson;
 import org.apache.shardingsphere.distsql.parser.statement.ral.advanced.ParseStatement;
 import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
+import org.apache.shardingsphere.infra.database.type.dialect.PostgreSQLDatabaseType;
 import org.apache.shardingsphere.infra.metadata.database.rule.ShardingSphereRuleMetaData;
 import org.apache.shardingsphere.mode.manager.ContextManager;
 import org.apache.shardingsphere.parser.rule.SQLParserRule;
@@ -61,13 +62,13 @@ public final class ParseDistSQLHandlerTest extends ProxyContextRestorer {
         ShardingSphereRuleMetaData globalRuleMetaData = mock(ShardingSphereRuleMetaData.class);
         when(contextManager.getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).thenReturn(globalRuleMetaData);
         when(globalRuleMetaData.getSingleRule(SQLParserRule.class)).thenReturn(sqlParserRule);
-        when(connectionSession.getDatabaseType()).thenReturn(new MySQLDatabaseType());
         ProxyContext.init(contextManager);
     }
     
     @Test
-    public void assertGetRowData() throws SQLException {
+    public void assertGetRowDataForMySQL() throws SQLException {
         String sql = "select * from t_order";
+        when(connectionSession.getDatabaseType()).thenReturn(new MySQLDatabaseType());
         ParseStatement parseStatement = new ParseStatement(sql);
         ParseDistSQLHandler parseDistSQLHandler = new ParseDistSQLHandler();
         parseDistSQLHandler.init(parseStatement, connectionSession);
@@ -78,9 +79,24 @@ public final class ParseDistSQLHandlerTest extends ProxyContextRestorer {
         assertThat(new LinkedList<>(parseDistSQLHandler.getRowData().getData()).getLast(), is(new Gson().toJson(statement)));
     }
     
+    @Test
+    public void assertGetRowDataForPostgreSQL() throws SQLException {
+        String sql = "select * from t_order";
+        when(connectionSession.getDatabaseType()).thenReturn(new PostgreSQLDatabaseType());
+        ParseStatement parseStatement = new ParseStatement(sql);
+        ParseDistSQLHandler parseDistSQLHandler = new ParseDistSQLHandler();
+        parseDistSQLHandler.init(parseStatement, connectionSession);
+        parseDistSQLHandler.execute();
+        parseDistSQLHandler.next();
+        SQLStatement statement = sqlParserRule.getSQLParserEngine("PostgreSQL").parse(sql, false);
+        assertThat(new LinkedList<>(parseDistSQLHandler.getRowData().getData()).getFirst(), is("PostgreSQLSelectStatement"));
+        assertThat(new LinkedList<>(parseDistSQLHandler.getRowData().getData()).getLast(), is(new Gson().toJson(statement)));
+    }
+    
     @Test(expected = SQLParsingException.class)
     public void assertExecute() throws SQLException {
         String sql = "wrong sql";
+        when(connectionSession.getDatabaseType()).thenReturn(new MySQLDatabaseType());
         ParseStatement parseStatement = new ParseStatement(sql);
         ParseDistSQLHandler parseDistSQLHandler = new ParseDistSQLHandler();
         parseDistSQLHandler.init(parseStatement, connectionSession);