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 c12b815  Improve  parser exception handling (#9540)
c12b815 is described below

commit c12b815b4548d362992ffbed2088ba53908bfd5d
Author: JingShang Lu <[email protected]>
AuthorDate: Mon Mar 1 18:06:28 2021 +0800

    Improve  parser exception handling (#9540)
    
    * fix Oracle and SQLServer parser warning
    
    * Improve  parser exception handling
    
    * add UT
---
 .../distsql/parser/api/DistSQLStatementParserEngine.java  |  6 +++++-
 .../backend/TextProtocolBackendHandlerFactoryTest.java    | 15 +++++++++++++++
 .../sql/parser/core/parser/SQLParserExecutor.java         |  6 +++++-
 3 files changed, 25 insertions(+), 2 deletions(-)

diff --git 
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/api/DistSQLStatementParserEngine.java
 
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/api/DistSQLStatementParserEngine.java
index bfc2ae7..33d861c 100644
--- 
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/api/DistSQLStatementParserEngine.java
+++ 
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/api/DistSQLStatementParserEngine.java
@@ -56,7 +56,11 @@ public final class DistSQLStatementParserEngine {
         } catch (final ParseCancellationException ex) {
             ((Parser) sqlParser).reset();
             setPredictionMode((Parser) sqlParser, PredictionMode.LL);
-            return (ParseASTNode) sqlParser.parse();
+            try {
+                return (ParseASTNode) sqlParser.parse();
+            } catch (final ParseCancellationException e) {
+                throw new SQLParsingException("You have an error in your SQL 
syntax");
+            }
         }
     }
     
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/TextProtocolBackendHandlerFactoryTest.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/TextProtocolBackendHandlerFactoryTest.java
index 8154bd9..93cea02 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/TextProtocolBackendHandlerFactoryTest.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/TextProtocolBackendHandlerFactoryTest.java
@@ -36,6 +36,7 @@ import 
org.apache.shardingsphere.proxy.backend.text.sctl.set.ShardingCTLSetBacke
 import 
org.apache.shardingsphere.proxy.backend.text.sctl.show.ShardingCTLShowBackendHandler;
 import org.apache.shardingsphere.proxy.backend.text.skip.SkipBackendHandler;
 import 
org.apache.shardingsphere.proxy.backend.text.transaction.TransactionBackendHandler;
+import org.apache.shardingsphere.sql.parser.exception.SQLParsingException;
 import org.apache.shardingsphere.transaction.ShardingTransactionManagerEngine;
 import org.apache.shardingsphere.transaction.context.TransactionContexts;
 import org.apache.shardingsphere.transaction.core.TransactionType;
@@ -206,4 +207,18 @@ public final class TextProtocolBackendHandlerFactoryTest {
         TextProtocolBackendHandler actual = 
TextProtocolBackendHandlerFactory.newInstance(databaseType, sql, 
backendConnection);
         assertThat(actual, instanceOf(SkipBackendHandler.class));
     }
+    
+    @Test(expected = SQLParsingException.class)
+    public void assertNewInstanceWithErrorSQL() throws SQLException {
+        String sql = "SELECT";
+        TextProtocolBackendHandler actual = 
TextProtocolBackendHandlerFactory.newInstance(databaseType, sql, 
backendConnection);
+        assertThat(actual, instanceOf(SkipBackendHandler.class));
+    }
+    
+    @Test(expected = SQLParsingException.class)
+    public void assertNewInstanceWithErrorRDL() throws SQLException {
+        String sql = "CREATE SHARDING";
+        TextProtocolBackendHandler actual = 
TextProtocolBackendHandlerFactory.newInstance(databaseType, sql, 
backendConnection);
+        assertThat(actual, instanceOf(SkipBackendHandler.class));
+    }
 }
diff --git 
a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/core/parser/SQLParserExecutor.java
 
b/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/core/parser/SQLParserExecutor.java
index be85a65..fa5fbfa 100644
--- 
a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/core/parser/SQLParserExecutor.java
+++ 
b/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/core/parser/SQLParserExecutor.java
@@ -57,7 +57,11 @@ public final class SQLParserExecutor {
         } catch (final ParseCancellationException ex) {
             ((Parser) sqlParser).reset();
             setPredictionMode((Parser) sqlParser, PredictionMode.LL);
-            return (ParseASTNode) sqlParser.parse();
+            try {
+                return (ParseASTNode) sqlParser.parse();
+            } catch (final ParseCancellationException e) {
+                throw new SQLParsingException("You have an error in your SQL 
syntax");
+            }
         }
     }
     

Reply via email to