Skip to content

Commit

Permalink
Support parsing SQL Server SELECT DeptId, sql #29164 (#29906)
Browse files Browse the repository at this point in the history
  • Loading branch information
yydeng626 authored Jan 30, 2024
1 parent 8646c49 commit 7b90120
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,11 @@ distinct
;

specialFunction
: conversionFunction | charFunction | openJsonFunction | jsonFunction | openRowSetFunction | windowFunction
: conversionFunction | charFunction | openJsonFunction | jsonFunction | openRowSetFunction | windowFunction | approxFunction
;

approxFunction
: funcName = (APPROX_PERCENTILE_CONT | APPROX_PERCENTILE_DISC) LP_ expr RP_ WITHIN GROUP LP_ ORDER BY expr (ASC | DESC)? RP_
;

conversionFunction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -787,3 +787,15 @@ FIRST_VALUE
LAST_VALUE
:L A S T UL_ V A L U E
;

APPROX_PERCENTILE_CONT
: A P P R O X UL_ P E R C E N T I L E UL_ C O N T
;

APPROX_PERCENTILE_DISC
: A P P R O X UL_ P E R C E N T I L E UL_ D I S C
;

WITHIN
: W I T H I N
;
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.AggregationClauseContext;
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.AggregationFunctionContext;
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.AliasContext;
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.ApproxFunctionContext;
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.AssignmentContext;
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.AssignmentValueContext;
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.AssignmentValuesContext;
Expand Down Expand Up @@ -678,9 +679,23 @@ public final ASTNode visitSpecialFunction(final SpecialFunctionContext ctx) {
if (null != ctx.windowFunction()) {
return visit(ctx.windowFunction());
}
if (null != ctx.approxFunction()) {
return visit(ctx.approxFunction());
}
return new FunctionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), ctx.getChild(0).getChild(0).getText(), getOriginalText(ctx));
}

@Override
public final ASTNode visitApproxFunction(final ApproxFunctionContext ctx) {
FunctionSegment result = new FunctionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), ctx.funcName.getText(), getOriginalText(ctx));
if (null != ctx.expr()) {
for (ExprContext each : ctx.expr()) {
result.getParameters().add((ExpressionSegment) visit(each));
}
}
return result;
}

@Override
public final ASTNode visitConversionFunction(final ConversionFunctionContext ctx) {
if (null != ctx.castFunction()) {
Expand Down
60 changes: 60 additions & 0 deletions test/it/parser/src/main/resources/case/dml/insert.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3457,4 +3457,64 @@
</columns>
<exec name="predict_per_partition" start-index="129" stop-index="159" start-delimiter="[" end-delimiter="]" />
</insert>

<insert sql-case-id="insert_into_sample_temp_table">
<table name="#SampleTempTable" start-index="12" stop-index="27" />
<columns start-index="28" stop-index="28" />
<values>
<value>
<assignment-value>
<literal-expression value="null" start-index="37" stop-index="40" />
</assignment-value>
<assignment-value>
<literal-expression value="hello" start-index="43" stop-index="49" />
</assignment-value>
</value>
</values>
</insert>

<insert sql-case-id="insert_into_sample_temp_table_2">
<table name="#SampleTempTable" start-index="12" stop-index="27" />
<columns start-index="28" stop-index="28" />
<values>
<value>
<assignment-value>
<literal-expression value="10" start-index="37" stop-index="38" />
</assignment-value>
<assignment-value>
<literal-expression value="null" start-index="41" stop-index="44" />
</assignment-value>
</value>
</values>
</insert>

<insert sql-case-id="insert_into_sample_temp_table_3">
<table name="#SampleTempTable" start-index="12" stop-index="27" />
<columns start-index="28" stop-index="28" />
<values>
<value>
<assignment-value>
<literal-expression value="17" start-index="37" stop-index="38" />
</assignment-value>
<assignment-value>
<literal-expression value="abc" start-index="41" stop-index="45" />
</assignment-value>
</value>
</values>
</insert>

<insert sql-case-id="insert_into_sample_temp_table_4">
<table name="#SampleTempTable" start-index="12" stop-index="27" />
<columns start-index="28" stop-index="28" />
<values>
<value>
<assignment-value>
<literal-expression value="17" start-index="37" stop-index="38" />
</assignment-value>
<assignment-value>
<literal-expression value="yes" start-index="41" stop-index="45" />
</assignment-value>
</value>
</values>
</insert>
</sql-parser-test-cases>
Original file line number Diff line number Diff line change
Expand Up @@ -1095,4 +1095,40 @@
<column-item name="Quarter" order-direction="ASC" start-index="638" stop-index="644" />
</order-by>
</select>

<select sql-case-id="select_approx_percentile_cont_function">
<projections start-index="7" stop-index="148">
<column-projection start-index="7" stop-index="12" name="DeptId" />
<expression-projection start-index="14" stop-index="80" text="APPROX_PERCENTILE_CONT(0.10) WITHIN GROUP(ORDER BY Salary)" alias="P10">
<expr>
<function function-name="APPROX_PERCENTILE_CONT" text="APPROX_PERCENTILE_CONT(0.10) WITHIN GROUP(ORDER BY Salary)" start-index="14" stop-index="71">
<parameter>
<literal-expression value="0.10" start-index="37" stop-index="40" />
</parameter>
<parameter>
<column name="Salary" start-index="65" stop-index="70" />
</parameter>
</function>
</expr>
</expression-projection>
<expression-projection text="APPROX_PERCENTILE_CONT(0.90) WITHIN GROUP(ORDER BY Salary)" start-index="82" stop-index="148" alias="P90">
<expr>
<function function-name="APPROX_PERCENTILE_CONT" text="APPROX_PERCENTILE_CONT(0.90) WITHIN GROUP(ORDER BY Salary)" start-index="82" stop-index="139">
<parameter>
<literal-expression value="0.90" start-index="105" stop-index="108" />
</parameter>
<parameter>
<column name="Salary" start-index="133" stop-index="138" />
</parameter>
</function>
</expr>
</expression-projection>
</projections>
<from>
<simple-table name="tblEmployee" start-index="155" stop-index="165" />
</from>
<group-by start-index="167" stop-index="181">
<column-item name="DeptId" order-direction="ASC" start-index="176" stop-index="181" />
</group-by>
</select>
</sql-parser-test-cases>
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,8 @@
<sql-case id="insert_into_table_from_table" value="INSERT INTO #Test SELECT * FROM Production.UnitMeasure" db-types="SQLServer"/>
<sql-case id="insert_into_production_location" value="INSERT INTO AdventureWorks2022.Production.Location(Name, CostRate, Availability, ModifiedDate) VALUES (NEWID(), .5, 5.2, GETDATE())" db-types="SQLServer"/>
<sql-case id="insert_with_exec_prediction_results" value="INSERT INTO prediction_results (tipped_Pred,payment_type,tipped,passenger_count,trip_distance,trip_time_in_secs,direct_distance) EXECUTE [predict_per_partition]" db-types="SQLServer"/>
<sql-case id="insert_into_sample_temp_table" value="INSERT INTO #SampleTempTable VALUES (null, 'hello')" db-types="SQLServer"/>
<sql-case id="insert_into_sample_temp_table_2" value="INSERT INTO #SampleTempTable VALUES (10, null)" db-types="SQLServer"/>
<sql-case id="insert_into_sample_temp_table_3" value="INSERT INTO #SampleTempTable VALUES (17, 'abc')" db-types="SQLServer"/>
<sql-case id="insert_into_sample_temp_table_4" value="INSERT INTO #SampleTempTable VALUES (17, 'yes')" db-types="SQLServer"/>
</sql-cases>
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,5 @@
<sql-case id="select_json_object_with_subquery" value="SELECT JSON_OBJECT('user_name':USER_NAME(), @id_key:@id_value, 'sid':(SELECT @@SPID))" db-types="SQLServer" />
<sql-case id="select_dm_exec_sessions_with_json_object_function" value="SELECT s.session_id, JSON_OBJECT('security_id':s.security_id, 'login':s.login_name, 'status':s.status) as info FROM sys.dm_exec_sessions AS s WHERE s.is_user_process = 1" db-types="SQLServer" />
<sql-case id="select_first_last_value_function" value="SELECT BusinessEntityID, DATEPART(QUARTER, QuotaDate) AS Quarter, YEAR(QuotaDate) AS SalesYear, SalesQuota AS QuotaThisQuarter, SalesQuota - FIRST_VALUE(SalesQuota) OVER (PARTITION BY BusinessEntityID, YEAR(QuotaDate) ORDER BY DATEPART(QUARTER, QuotaDate)) AS DifferenceFromFirstQuarter, SalesQuota - LAST_VALUE(SalesQuota) OVER (PARTITION BY BusinessEntityID, YEAR(QuotaDate) ORDER BY DATEPART(QUARTER, QuotaDate) RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) AS DifferenceFromLastQuarter FROM Sales.SalesPersonQuotaHistory WHERE YEAR(QuotaDate) > 2005 AND BusinessEntityID BETWEEN 274 AND 275 ORDER BY BusinessEntityID, SalesYear, Quarter" db-types="SQLServer" />
<sql-case id="select_approx_percentile_cont_function" value="SELECT DeptId,APPROX_PERCENTILE_CONT(0.10) WITHIN GROUP(ORDER BY Salary) AS 'P10',APPROX_PERCENTILE_CONT(0.90) WITHIN GROUP(ORDER BY Salary) AS 'P90' FROM tblEmployee GROUP BY DeptId" db-types="SQLServer" />
</sql-cases>

0 comments on commit 7b90120

Please sign in to comment.