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 2020/07/09 10:43:13 UTC

[shardingsphere] branch master updated: Fixed grammar for MATCH AGAINST expressions (#6308)

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 c74a861  Fixed grammar for MATCH AGAINST expressions (#6308)
c74a861 is described below

commit c74a861497b4950aff741346a21929d43b62f8fa
Author: Rafael Blazquez Blanco <gi...@koxalen.net>
AuthorDate: Thu Jul 9 12:43:01 2020 +0200

    Fixed grammar for MATCH AGAINST expressions (#6308)
    
    Fixes #6299
---
 .../src/main/antlr4/imports/mysql/BaseRule.g4      |  2 +-
 .../sql/parser/mysql/visitor/MySQLVisitor.java     | 10 ++++++++++
 .../src/test/resources/case/dml/select.xml         | 22 ++++++++++++++++++++++
 .../test/resources/sql/supported/dml/select.xml    |  1 +
 4 files changed, 34 insertions(+), 1 deletion(-)

diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/BaseRule.g4 b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/BaseRule.g4
index fceb5aa..a04c998 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/BaseRule.g4
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/BaseRule.g4
@@ -510,7 +510,7 @@ regularFunctionName_
     ;
 
 matchExpression_
-    : MATCH columnNames AGAINST (expr matchSearchModifier_?)
+    : MATCH columnNames AGAINST LP_ expr matchSearchModifier_? RP_
     ;
 
 matchSearchModifier_
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/MySQLVisitor.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/MySQLVisitor.java
index 0237c82..538110b 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/MySQLVisitor.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/MySQLVisitor.java
@@ -47,6 +47,7 @@ import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.Identif
 import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.IndexNameContext;
 import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.IntervalExpressionContext;
 import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.LiteralsContext;
+import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.MatchExpression_Context;
 import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.NullValueLiteralsContext;
 import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.NumberLiteralsContext;
 import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.OrderByClauseContext;
@@ -428,6 +429,9 @@ public abstract class MySQLVisitor extends MySQLStatementBaseVisitor<ASTNode> {
         if (null != ctx.columnName()) {
             return visit(ctx.columnName());
         }
+        if (null != ctx.matchExpression_()) {
+            return visit(ctx.matchExpression_());
+        }
         return visitRemainSimpleExpr(ctx);
     }
     
@@ -583,6 +587,12 @@ public abstract class MySQLVisitor extends MySQLStatementBaseVisitor<ASTNode> {
     }
     
     @Override
+    public final ASTNode visitMatchExpression_(final MatchExpression_Context ctx) {
+        visit(ctx.expr());
+        return new CommonExpressionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), ctx.getText());
+    }
+    
+    @Override
     public final ASTNode visitCaseExpression(final CaseExpressionContext ctx) {
         return new OtherLiteralValue(ctx.getText());
     }
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/test/resources/case/dml/select.xml b/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/test/resources/case/dml/select.xml
index 016becf..d1095bc 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/test/resources/case/dml/select.xml
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/test/resources/case/dml/select.xml
@@ -1864,4 +1864,26 @@
             <expression-projection start-index="7" stop-index="18" />
         </projections>
     </select>
+    <select sql-case-id="select_with_match_against" parameters="hello, 10">
+        <projections start-index="7" stop-index="7">
+            <shorthand-projection start-index="7" stop-index="7" />
+        </projections>
+        <table-reference>
+            <table-factor>
+                <table name="t_order_item" start-index="14" stop-index="25" />
+            </table-factor>
+        </table-reference>
+        <where start-index="27" stop-index="116" literal-stop-index="121">
+            <and-predicate>
+                <predicate start-index="106" stop-index="116" literal-start-index="110" literal-stop-index="121">
+                    <column-left-value name="user_id" start-index="106" stop-index="112" literal-start-index="110" literal-stop-index="116" />
+                    <operator type="=" />
+                    <compare-right-value>
+                        <literal-expression start-index="120" stop-index="121" value="10" />
+                        <parameter-marker-expression value="1" start-index="116" stop-index="116" />
+                    </compare-right-value>
+                </predicate>
+            </and-predicate>
+        </where>
+    </select>
 </sql-parser-test-cases>
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/test/resources/sql/supported/dml/select.xml b/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/test/resources/sql/supported/dml/select.xml
index eecfe84..51a063a 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/test/resources/sql/supported/dml/select.xml
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/test/resources/sql/supported/dml/select.xml
@@ -55,4 +55,5 @@
     <sql-case id="select_with_date_format_function" value="SELECT * FROM t_order WHERE DATE_FORMAT(current_date, '%Y-%m-%d') = '2019-12-18'" db-types="MySQL" />
     <sql-case id="select_with_spatial_function" value="SELECT * FROM t_order WHERE ST_DISTANCE_SPHERE(POINT(113.358772, 23.1273723), POINT(user_id,order_id)) != 0" db-types="MySQL" />
     <sql-case id="select_current_user" value="SELECT CURRENT_USER" db-types="PostgreSQL"/>
+    <sql-case id="select_with_match_against" value="SELECT * FROM t_order_item WHERE MATCH(t_order_item.description) AGAINST (? IN NATURAL LANGUAGE MODE) AND user_id = ?" db-types="MySQL" />
 </sql-cases>