You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2023/04/24 00:38:25 UTC
[shardingsphere] branch master updated: Support for substring, dual, spatial function (#25268)
This is an automated email from the ASF dual-hosted git repository.
duanzhengqiang 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 6e52731df27 Support for substring, dual, spatial function (#25268)
6e52731df27 is described below
commit 6e52731df278c2fdf59feefd1196bd5635326eb7
Author: kanha gupta <92...@users.noreply.github.com>
AuthorDate: Mon Apr 24 06:08:09 2023 +0530
Support for substring, dual, spatial function (#25268)
* support for Char function
* support for Weight string SQL
* Support for Char & weight string Function
* null
* Revert "Support for Substring function"
This reverts commit 9af038be0f4be54affd801c9ac2f6750db883377.
* support for spatial function
* support for DUAL function
* support for Substring function
* support for Char function
* support for Weight string SQL
* Support for Char & weight string Function
* null
* Revert "Support for Substring function"
This reverts commit 9af038be0f4be54affd801c9ac2f6750db883377.
* refactor node to each
---
.../impl/LiteralExpressionConverter.java | 3 ++-
.../visitor/statement/MySQLStatementVisitor.java | 22 +++++++++++++++++-----
.../test/it/optimize/SQLNodeConverterEngineIT.java | 3 +++
.../parser/src/main/resources/case/dml/insert.xml | 20 ++++++++++++++++++--
.../resources/case/dml/select-special-function.xml | 9 ++++++++-
.../parser/src/main/resources/case/dml/select.xml | 3 +++
6 files changed, 51 insertions(+), 9 deletions(-)
diff --git a/kernel/sql-federation/optimizer/src/main/java/org/apache/shardingsphere/sqlfederation/optimizer/converter/segment/expression/impl/LiteralExpressionConverter.java b/kernel/sql-federation/optimizer/src/main/java/org/apache/shardingsphere/sqlfederation/optimizer/converter/segment/expression/impl/LiteralExpressionConverter.java
index c7849e00d78..5b2708a8319 100644
--- a/kernel/sql-federation/optimizer/src/main/java/org/apache/shardingsphere/sqlfederation/optimizer/converter/segment/expression/impl/LiteralExpressionConverter.java
+++ b/kernel/sql-federation/optimizer/src/main/java/org/apache/shardingsphere/sqlfederation/optimizer/converter/segment/expression/impl/LiteralExpressionConverter.java
@@ -29,6 +29,7 @@ import org.apache.shardingsphere.sqlfederation.optimizer.converter.segment.SQLSe
import java.util.Collection;
import java.util.HashSet;
import java.util.Optional;
+import java.math.BigDecimal;
/**
* Literal expression converter.
@@ -51,7 +52,7 @@ public final class LiteralExpressionConverter implements SQLSegmentConverter<Lit
if ("YEAR".equals(segment.getLiterals())) {
return Optional.of(new SqlIntervalQualifier(TimeUnit.YEAR, null, SqlParserPos.ZERO));
}
- if (segment.getLiterals() instanceof Integer) {
+ if (segment.getLiterals() instanceof Integer || segment.getLiterals() instanceof BigDecimal) {
return Optional.of(SqlLiteral.createExactNumeric(String.valueOf(segment.getLiterals()), SqlParserPos.ZERO));
}
if (TRIM_FUNCTION_FLAGS.contains(String.valueOf(segment.getLiterals()))) {
diff --git a/sql-parser/dialect/mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/MySQLStatementVisitor.java b/sql-parser/dialect/mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/MySQLStatementVisitor.java
index cc9f5cff6a7..cebb1317013 100644
--- a/sql-parser/dialect/mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/MySQLStatementVisitor.java
+++ b/sql-parser/dialect/mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/MySQLStatementVisitor.java
@@ -719,9 +719,16 @@ public abstract class MySQLStatementVisitor extends MySQLStatementBaseVisitor<AS
if (null != ctx.selectSpecification()) {
result.getProjections().setDistinctRow(isDistinct(ctx));
}
- if (null != ctx.fromClause() && null != ctx.fromClause().tableReferences()) {
- TableSegment tableSource = (TableSegment) visit(ctx.fromClause().tableReferences());
- result.setFrom(tableSource);
+ if (null != ctx.fromClause()) {
+ if (null != ctx.fromClause().tableReferences()) {
+ TableSegment tableSource = (TableSegment) visit(ctx.fromClause().tableReferences());
+ result.setFrom(tableSource);
+ }
+ if (null != ctx.fromClause().DUAL()) {
+ TableSegment tableSource = new SimpleTableSegment(new TableNameSegment(ctx.fromClause().DUAL().getSymbol().getStartIndex(),
+ ctx.fromClause().DUAL().getSymbol().getStopIndex(), new IdentifierValue(ctx.fromClause().DUAL().getText())));
+ result.setFrom(tableSource);
+ }
}
if (null != ctx.whereClause()) {
result.setWhere((WhereSegment) visit(ctx.whereClause()));
@@ -939,8 +946,13 @@ public abstract class MySQLStatementVisitor extends MySQLStatementBaseVisitor<AS
@Override
public final ASTNode visitSubstringFunction(final SubstringFunctionContext ctx) {
- calculateParameterCount(Collections.singleton(ctx.expr()));
- return new FunctionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), null == ctx.SUBSTR() ? ctx.SUBSTRING().getText() : ctx.SUBSTR().getText(), getOriginalText(ctx));
+ FunctionSegment result = new FunctionSegment(
+ ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), null == ctx.SUBSTR() ? ctx.SUBSTRING().getText() : ctx.SUBSTR().getText(), getOriginalText(ctx));
+ result.getParameters().add((ExpressionSegment) visit(ctx.expr()));
+ for (TerminalNode each : ctx.NUMBER_()) {
+ result.getParameters().add(new LiteralExpressionSegment(each.getSymbol().getStartIndex(), each.getSymbol().getStopIndex(), new NumberLiteralValue(each.getText()).getValue()));
+ }
+ return result;
}
@Override
diff --git a/test/it/optimizer/src/test/java/org/apache/shardingsphere/test/it/optimize/SQLNodeConverterEngineIT.java b/test/it/optimizer/src/test/java/org/apache/shardingsphere/test/it/optimize/SQLNodeConverterEngineIT.java
index 0aec536b1c2..a0655fdf5b1 100644
--- a/test/it/optimizer/src/test/java/org/apache/shardingsphere/test/it/optimize/SQLNodeConverterEngineIT.java
+++ b/test/it/optimizer/src/test/java/org/apache/shardingsphere/test/it/optimize/SQLNodeConverterEngineIT.java
@@ -200,6 +200,9 @@ class SQLNodeConverterEngineIT {
result.add("select_with_trim_expr_from_expr_and_both");
result.add("select_extract");
result.add("select_where_with_bit_expr_with_mod_sign");
+ result.add("select_with_spatial_function");
+ result.add("select_from_dual");
+ result.add("select_substring");
return result;
}
// CHECKSTYLE:ON
diff --git a/test/it/parser/src/main/resources/case/dml/insert.xml b/test/it/parser/src/main/resources/case/dml/insert.xml
index 1ee8d5339d5..9c990f8165c 100644
--- a/test/it/parser/src/main/resources/case/dml/insert.xml
+++ b/test/it/parser/src/main/resources/case/dml/insert.xml
@@ -794,7 +794,15 @@
<literal-expression value="1" start-index="59" stop-index="59" />
</assignment-value>
<assignment-value>
- <function function-name="SUBSTR" text="SUBSTR(?, 1)" literal-text="SUBSTR('init', 1)" literal-start-index="62" literal-stop-index="78" start-index="62" stop-index="73" />
+ <function function-name="SUBSTR" text="SUBSTR(?, 1)" literal-text="SUBSTR('init', 1)" literal-start-index="62" literal-stop-index="78" start-index="62" stop-index="73" >
+ <parameter>
+ <parameter-marker-expression parameter-index="2" start-index="69" stop-index="69" />
+ <literal-expression value="init" literal-start-index="69" literal-stop-index="74" />
+ </parameter>
+ <parameter>
+ <literal-expression value="1" start-index="72" stop-index="72" literal-start-index="77" literal-stop-index="77" />
+ </parameter>
+ </function>
</assignment-value>
</value>
<value>
@@ -807,7 +815,15 @@
<literal-expression value="2" literal-start-index="86" literal-stop-index="86" />
</assignment-value>
<assignment-value>
- <function function-name="SUBSTR" text="SUBSTR(?, 1)" literal-text="SUBSTR('init', 1)" start-index="84" stop-index="95" literal-start-index="89" literal-stop-index="105" />
+ <function function-name="SUBSTR" text="SUBSTR(?, 1)" literal-text="SUBSTR('init', 1)" start-index="84" stop-index="95" literal-start-index="89" literal-stop-index="105" >
+ <parameter>
+ <parameter-marker-expression parameter-index="5" start-index="91" stop-index="91" />
+ <literal-expression value="init" literal-start-index="96" literal-stop-index="101" />
+ </parameter>
+ <parameter>
+ <literal-expression value="1" start-index="94" stop-index="94" literal-start-index="104" literal-stop-index="104" />
+ </parameter>
+ </function>
</assignment-value>
</value>
</values>
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 daa3b4a3dce..94035b3803f 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
@@ -110,7 +110,14 @@
<projections start-index="7" stop-index="35">
<expression-projection text="SUBSTRING('foobarbar' from 4)" start-index="7" stop-index="35">
<expr>
- <function function-name="SUBSTRING" start-index="7" stop-index="35" text="SUBSTRING('foobarbar' from 4)" />
+ <function function-name="SUBSTRING" start-index="7" stop-index="35" text="SUBSTRING('foobarbar' from 4)" >
+ <parameter>
+ <literal-expression value="foobarbar" start-index="17" stop-index="27" />
+ </parameter>
+ <parameter>
+ <literal-expression value="4" start-index="34" stop-index="34" />
+ </parameter>
+ </function>
</expr>
</expression-projection>
</projections>
diff --git a/test/it/parser/src/main/resources/case/dml/select.xml b/test/it/parser/src/main/resources/case/dml/select.xml
index dc3634cf070..f15cea7633c 100644
--- a/test/it/parser/src/main/resources/case/dml/select.xml
+++ b/test/it/parser/src/main/resources/case/dml/select.xml
@@ -3349,6 +3349,9 @@
</select>
<select sql-case-id="select_from_dual" >
+ <from>
+ <simple-table name="DUAL" start-index="14" stop-index="17" />
+ </from>
<projections start-index="7" stop-index="7">
<expression-projection text="1" start-index="7" stop-index="7" />
</projections>