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 f5bbcef9f85 Support parsing Doris COSINE_DISTANCE syntax (#38248)
f5bbcef9f85 is described below
commit f5bbcef9f85dfd1f7989be8db9c80fcb8d615681
Author: cxy <[email protected]>
AuthorDate: Fri Feb 27 23:04:13 2026 +0800
Support parsing Doris COSINE_DISTANCE syntax (#38248)
---
.../src/main/antlr4/imports/doris/BaseRule.g4 | 6 +
.../visitor/statement/DorisStatementVisitor.java | 13 ++
.../resources/case/dml/select-special-function.xml | 201 +++++++++++++++++++++
.../sql/supported/dml/select-special-function.xml | 6 +
4 files changed, 226 insertions(+)
diff --git
a/parser/sql/engine/dialect/doris/src/main/antlr4/imports/doris/BaseRule.g4
b/parser/sql/engine/dialect/doris/src/main/antlr4/imports/doris/BaseRule.g4
index bbca59c68c6..c2cbe619f1b 100644
--- a/parser/sql/engine/dialect/doris/src/main/antlr4/imports/doris/BaseRule.g4
+++ b/parser/sql/engine/dialect/doris/src/main/antlr4/imports/doris/BaseRule.g4
@@ -994,6 +994,12 @@ simpleExpr
| matchExpression
| caseExpression
| intervalExpression
+ | arrayExpression
+ ;
+
+arrayExpression
+ : LBT_ expr (COMMA_ expr)* RBT_
+ | LBT_ RBT_
;
path
diff --git
a/parser/sql/engine/dialect/doris/src/main/java/org/apache/shardingsphere/sql/parser/engine/doris/visitor/statement/DorisStatementVisitor.java
b/parser/sql/engine/dialect/doris/src/main/java/org/apache/shardingsphere/sql/parser/engine/doris/visitor/statement/DorisStatementVisitor.java
index 9152936d961..32f85f1e9e3 100644
---
a/parser/sql/engine/dialect/doris/src/main/java/org/apache/shardingsphere/sql/parser/engine/doris/visitor/statement/DorisStatementVisitor.java
+++
b/parser/sql/engine/dialect/doris/src/main/java/org/apache/shardingsphere/sql/parser/engine/doris/visitor/statement/DorisStatementVisitor.java
@@ -30,6 +30,7 @@ import org.apache.shardingsphere.sql.parser.api.ASTNode;
import org.apache.shardingsphere.sql.parser.autogen.DorisStatementBaseVisitor;
import
org.apache.shardingsphere.sql.parser.autogen.DorisStatementParser.AggregationFunctionContext;
import
org.apache.shardingsphere.sql.parser.autogen.DorisStatementParser.AliasContext;
+import
org.apache.shardingsphere.sql.parser.autogen.DorisStatementParser.ArrayExpressionContext;
import
org.apache.shardingsphere.sql.parser.autogen.DorisStatementParser.AssignmentContext;
import
org.apache.shardingsphere.sql.parser.autogen.DorisStatementParser.AssignmentValueContext;
import
org.apache.shardingsphere.sql.parser.autogen.DorisStatementParser.AssignmentValuesContext;
@@ -690,9 +691,21 @@ public abstract class DorisStatementVisitor extends
DorisStatementBaseVisitor<AS
String text = ctx.start.getInputStream().getText(new
Interval(ctx.start.getStartIndex(), ctx.stop.getStopIndex()));
return new BinaryOperationExpression(ctx.start.getStartIndex(),
ctx.stop.getStopIndex(), left, right, ctx.VERTICAL_BAR_(0).getText() +
ctx.VERTICAL_BAR_(1).getText(), text);
}
+ if (null != ctx.arrayExpression()) {
+ return visit(ctx.arrayExpression());
+ }
return visitRemainSimpleExpr(ctx);
}
+ @Override
+ public ASTNode visitArrayExpression(final ArrayExpressionContext ctx) {
+ ListExpression result = new
ListExpression(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex());
+ for (ExprContext each : ctx.expr()) {
+ result.getItems().add((ExpressionSegment) visit(each));
+ }
+ return result;
+ }
+
@Override
public ASTNode visitColumnRef(final ColumnRefContext ctx) {
int identifierCount = ctx.identifier().size();
diff --git
a/test/it/parser/src/main/resources/case/dml/select-special-function.xml
b/test/it/parser/src/main/resources/case/dml/select-special-function.xml
index ceed8ed7d4c..566e3e181f1 100644
--- a/test/it/parser/src/main/resources/case/dml/select-special-function.xml
+++ b/test/it/parser/src/main/resources/case/dml/select-special-function.xml
@@ -5024,6 +5024,207 @@
</expression-projection>
</projections>
</select>
+
+ <select sql-case-id="select_cosine_distance" db-types="Doris">
+ <projections start-index="7" stop-index="37">
+ <expression-projection start-index="7" stop-index="37"
text="COSINE_DISTANCE([1, 2], [2, 3])">
+ <expr>
+ <function function-name="COSINE_DISTANCE"
text="COSINE_DISTANCE([1, 2], [2, 3])" start-index="7" stop-index="37">
+ <parameter>
+ <list-expression start-index="23" stop-index="28">
+ <items>
+ <literal-expression value="1"
start-index="24" stop-index="24" />
+ </items>
+ <items>
+ <literal-expression value="2"
start-index="27" stop-index="27" />
+ </items>
+ </list-expression>
+ </parameter>
+ <parameter>
+ <list-expression start-index="31" stop-index="36">
+ <items>
+ <literal-expression value="2"
start-index="32" stop-index="32" />
+ </items>
+ <items>
+ <literal-expression value="3"
start-index="35" stop-index="35" />
+ </items>
+ </list-expression>
+ </parameter>
+ </function>
+ </expr>
+ </expression-projection>
+ </projections>
+ </select>
+
+ <select sql-case-id="select_cosine_distance_multiple" db-types="Doris">
+ <projections start-index="7" stop-index="69">
+ <expression-projection start-index="7" stop-index="37"
text="COSINE_DISTANCE([1, 2], [2, 3])">
+ <expr>
+ <function function-name="COSINE_DISTANCE"
text="COSINE_DISTANCE([1, 2], [2, 3])" start-index="7" stop-index="37">
+ <parameter>
+ <list-expression start-index="23" stop-index="28">
+ <items>
+ <literal-expression value="1"
start-index="24" stop-index="24" />
+ </items>
+ <items>
+ <literal-expression value="2"
start-index="27" stop-index="27" />
+ </items>
+ </list-expression>
+ </parameter>
+ <parameter>
+ <list-expression start-index="31" stop-index="36">
+ <items>
+ <literal-expression value="2"
start-index="32" stop-index="32" />
+ </items>
+ <items>
+ <literal-expression value="3"
start-index="35" stop-index="35" />
+ </items>
+ </list-expression>
+ </parameter>
+ </function>
+ </expr>
+ </expression-projection>
+ <expression-projection start-index="39" stop-index="69"
text="COSINE_DISTANCE([3, 6], [4, 7])">
+ <expr>
+ <function function-name="COSINE_DISTANCE"
text="COSINE_DISTANCE([3, 6], [4, 7])" start-index="39" stop-index="69">
+ <parameter>
+ <list-expression start-index="55" stop-index="60">
+ <items>
+ <literal-expression value="3"
start-index="56" stop-index="56" />
+ </items>
+ <items>
+ <literal-expression value="6"
start-index="59" stop-index="59" />
+ </items>
+ </list-expression>
+ </parameter>
+ <parameter>
+ <list-expression start-index="63" stop-index="68">
+ <items>
+ <literal-expression value="4"
start-index="64" stop-index="64" />
+ </items>
+ <items>
+ <literal-expression value="7"
start-index="67" stop-index="67" />
+ </items>
+ </list-expression>
+ </parameter>
+ </function>
+ </expr>
+ </expression-projection>
+ </projections>
+ </select>
+
+ <select sql-case-id="select_cosine_distance_three_elements"
db-types="Doris">
+ <projections start-index="7" stop-index="43">
+ <expression-projection start-index="7" stop-index="43"
text="COSINE_DISTANCE([1, 2, 3], [4, 5, 6])">
+ <expr>
+ <function function-name="COSINE_DISTANCE"
text="COSINE_DISTANCE([1, 2, 3], [4, 5, 6])" start-index="7" stop-index="43">
+ <parameter>
+ <list-expression start-index="23" stop-index="31">
+ <items>
+ <literal-expression value="1"
start-index="24" stop-index="24" />
+ </items>
+ <items>
+ <literal-expression value="2"
start-index="27" stop-index="27" />
+ </items>
+ <items>
+ <literal-expression value="3"
start-index="30" stop-index="30" />
+ </items>
+ </list-expression>
+ </parameter>
+ <parameter>
+ <list-expression start-index="34" stop-index="42">
+ <items>
+ <literal-expression value="4"
start-index="35" stop-index="35" />
+ </items>
+ <items>
+ <literal-expression value="5"
start-index="38" stop-index="38" />
+ </items>
+ <items>
+ <literal-expression value="6"
start-index="41" stop-index="41" />
+ </items>
+ </list-expression>
+ </parameter>
+ </function>
+ </expr>
+ </expression-projection>
+ </projections>
+ </select>
+
+ <select sql-case-id="select_cosine_distance_from_table" db-types="Doris">
+ <projections start-index="7" stop-index="37">
+ <expression-projection start-index="7" stop-index="37"
text="COSINE_DISTANCE([1, 2], [2, 3])">
+ <expr>
+ <function function-name="COSINE_DISTANCE"
text="COSINE_DISTANCE([1, 2], [2, 3])" start-index="7" stop-index="37">
+ <parameter>
+ <list-expression start-index="23" stop-index="28">
+ <items>
+ <literal-expression value="1"
start-index="24" stop-index="24" />
+ </items>
+ <items>
+ <literal-expression value="2"
start-index="27" stop-index="27" />
+ </items>
+ </list-expression>
+ </parameter>
+ <parameter>
+ <list-expression start-index="31" stop-index="36">
+ <items>
+ <literal-expression value="2"
start-index="32" stop-index="32" />
+ </items>
+ <items>
+ <literal-expression value="3"
start-index="35" stop-index="35" />
+ </items>
+ </list-expression>
+ </parameter>
+ </function>
+ </expr>
+ </expression-projection>
+ </projections>
+ <from>
+ <simple-table name="t_order" start-index="44" stop-index="50" />
+ </from>
+ </select>
+
+ <select sql-case-id="select_cosine_distance_empty_array" db-types="Doris">
+ <projections start-index="7" stop-index="29">
+ <expression-projection start-index="7" stop-index="29"
text="COSINE_DISTANCE([], [])">
+ <expr>
+ <function function-name="COSINE_DISTANCE"
text="COSINE_DISTANCE([], [])" start-index="7" stop-index="29">
+ <parameter>
+ <list-expression start-index="23" stop-index="24"
/>
+ </parameter>
+ <parameter>
+ <list-expression start-index="27" stop-index="28"
/>
+ </parameter>
+ </function>
+ </expr>
+ </expression-projection>
+ </projections>
+ </select>
+
+ <select sql-case-id="select_cosine_distance_single_element"
db-types="Doris">
+ <projections start-index="7" stop-index="31">
+ <expression-projection start-index="7" stop-index="31"
text="COSINE_DISTANCE([1], [2])">
+ <expr>
+ <function function-name="COSINE_DISTANCE"
text="COSINE_DISTANCE([1], [2])" start-index="7" stop-index="31">
+ <parameter>
+ <list-expression start-index="23" stop-index="25">
+ <items>
+ <literal-expression value="1"
start-index="24" stop-index="24" />
+ </items>
+ </list-expression>
+ </parameter>
+ <parameter>
+ <list-expression start-index="28" stop-index="30">
+ <items>
+ <literal-expression value="2"
start-index="29" stop-index="29" />
+ </items>
+ </list-expression>
+ </parameter>
+ </function>
+ </expr>
+ </expression-projection>
+ </projections>
+ </select>
<select sql-case-id="select_last_insert_id">
<projections start-index="7" stop-index="22">
diff --git
a/test/it/parser/src/main/resources/sql/supported/dml/select-special-function.xml
b/test/it/parser/src/main/resources/sql/supported/dml/select-special-function.xml
index 98b835f4592..873fef3023d 100644
---
a/test/it/parser/src/main/resources/sql/supported/dml/select-special-function.xml
+++
b/test/it/parser/src/main/resources/sql/supported/dml/select-special-function.xml
@@ -292,6 +292,12 @@
<sql-case id="select_instr" value="SELECT INSTR('foobar','bar')"
db-types="Doris" />
<sql-case id="select_strright" value="SELECT STRRIGHT('foobarbar',4)"
db-types="Doris" />
<sql-case id="select_extract_url_parameter" value="SELECT
EXTRACT_URL_PARAMETER('http://foo.com/?bar=baz','bar')" db-types="Doris" />
+ <sql-case id="select_cosine_distance" value="SELECT COSINE_DISTANCE([1,
2], [2, 3])" db-types="Doris" />
+ <sql-case id="select_cosine_distance_multiple" value="SELECT
COSINE_DISTANCE([1, 2], [2, 3]),COSINE_DISTANCE([3, 6], [4, 7])"
db-types="Doris" />
+ <sql-case id="select_cosine_distance_three_elements" value="SELECT
COSINE_DISTANCE([1, 2, 3], [4, 5, 6])" db-types="Doris" />
+ <sql-case id="select_cosine_distance_from_table" value="SELECT
COSINE_DISTANCE([1, 2], [2, 3]) FROM t_order" db-types="Doris" />
+ <sql-case id="select_cosine_distance_empty_array" value="SELECT
COSINE_DISTANCE([], [])" db-types="Doris" />
+ <sql-case id="select_cosine_distance_single_element" value="SELECT
COSINE_DISTANCE([1], [2])" db-types="Doris" />
<sql-case id="select_last_insert_id" value="SELECT LAST_INSERT_ID()"
db-types="MySQL" />
<sql-case id="select_last_value" value="SELECT LAST_VALUE(AGE) OVER
(PARTITION BY AGE ORDER BY AGE) from TEST" db-types="MySQL" />
<sql-case id="select_lcase_function" value="SELECT LCASE('QUADRATICALLY')"
db-types="MySQL" />