Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support SQLServer convert function parse #29138

Merged
merged 1 commit into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -300,13 +300,17 @@ distinct
;

specialFunction
: castFunction | charFunction
: castFunction | charFunction | convertFunction
;

castFunction
: CAST LP_ expr AS dataType RP_
;

convertFunction
: CONVERT LP_ dataType COMMA_ expr (COMMA_ NUMBER_)? RP_
;

charFunction
: CHAR LP_ expr (COMMA_ expr)* (USING ignoredIdentifier)? RP_
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ IDENTIFIER_
;

DELIMITED_IDENTIFIER_
: (LBT_ | DQ_) [a-zA-Z0-9@$#_.\\/\u0080-\uFFFF ]+ (DQ_ | RBT_)
: (LBT_ | DQ_) [a-zA-Z0-9@$#_,.\\/\u0080-\uFFFF ]+ (DQ_ | RBT_)
;

STRING_
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.ColumnNamesContext;
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.ColumnNamesWithSortContext;
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.ConstraintNameContext;
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.ConvertFunctionContext;
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.CreateTableAsSelectClauseContext;
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.CteClauseContext;
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.CteClauseSetContext;
Expand Down Expand Up @@ -598,6 +599,9 @@ public final ASTNode visitSpecialFunction(final SpecialFunctionContext ctx) {
if (null != ctx.castFunction()) {
return visit(ctx.castFunction());
}
if (null != ctx.convertFunction()) {
return visit(ctx.convertFunction());
}
if (null != ctx.charFunction()) {
return visit(ctx.charFunction());
}
Expand All @@ -618,6 +622,17 @@ public final ASTNode visitCastFunction(final CastFunctionContext ctx) {
return result;
}

@Override
public ASTNode visitConvertFunction(final ConvertFunctionContext ctx) {
FunctionSegment result = new FunctionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), ctx.CONVERT().getText(), getOriginalText(ctx));
result.getParameters().add((DataTypeSegment) visit(ctx.dataType()));
result.getParameters().add((ExpressionSegment) visit(ctx.expr()));
if (null != ctx.NUMBER_()) {
result.getParameters().add(new LiteralExpressionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), ctx.NUMBER_().getText()));
}
return result;
}

@Override
public final ASTNode visitCharFunction(final CharFunctionContext ctx) {
calculateParameterCount(ctx.expr());
Expand Down
295 changes: 294 additions & 1 deletion test/it/parser/src/main/resources/case/dml/select.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3771,7 +3771,7 @@
</where>
</select>

<select sql-case-id="select_with_convert_function">
<select sql-case-id="select_with_convert_function1">
<from>
<simple-table name="t_order" start-index="70" stop-index="76" />
</from>
Expand All @@ -3797,6 +3797,299 @@
</where>
</select>

<select sql-case-id="select_with_convert_function2">
<projections start-index="7" stop-index="246">
<column-projection name="sequence_name" start-index="7" stop-index="19" />
<column-projection name="sequence_catalog" start-index="22" stop-index="37" />
<column-projection name="sequence_schema" start-index="40" stop-index="54" />
<expression-projection alias="start_value" text="convert( bigint, start_value )" start-index="57" stop-index="101">
<expr>
<function function-name="convert" text="convert( bigint, start_value )" start-index="57" stop-index="86">
<parameter>
<data-type value="bigint" start-index="66" stop-index="71" />
</parameter>
<parameter>
<column name="start_value" start-index="74" stop-index="84" />
</parameter>
</function>
</expr>
</expression-projection>
<expression-projection alias="minimum_value" text="convert( bigint, minimum_value )" start-index="104" stop-index="152">
<expr>
<function function-name="convert" text="convert( bigint, minimum_value )" start-index="104" stop-index="135">
<parameter>
<data-type value="bigint" start-index="113" stop-index="118" />
</parameter>
<parameter>
<column name="minimum_value" start-index="121" stop-index="133" />
</parameter>
</function>
</expr>
</expression-projection>
<expression-projection alias="maximum_value" text="convert( bigint, maximum_value )" start-index="155" stop-index="203">
<expr>
<function function-name="convert" text="convert( bigint, maximum_value )" start-index="155" stop-index="186">
<parameter>
<data-type value="bigint" start-index="164" stop-index="169" />
</parameter>
<parameter>
<column name="maximum_value" start-index="172" stop-index="184" />
</parameter>
</function>
</expr>
</expression-projection>
<expression-projection alias="increment" text="convert( bigint, increment )" start-index="206" stop-index="246">
<expr>
<function function-name="convert" text="convert( bigint, increment )" start-index="206" stop-index="233">
<parameter>
<data-type value="bigint" start-index="215" stop-index="220" />
</parameter>
<parameter>
<column name="increment" start-index="223" stop-index="231" />
</parameter>
</function>
</expr>
</expression-projection>
</projections>
<from>
<simple-table name="SEQUENCES" start-index="253" stop-index="280">
<owner name="INFORMATION_SCHEMA" start-index="253" stop-index="270" />
</simple-table>
</from>
</select>

<select sql-case-id="select_with_convert_function3">
<projections start-index="7" stop-index="37">
<expression-projection text="CONVERT(NVARCHAR, GETDATE(), 0)" start-index="7" stop-index="37">
<expr>
<function function-name="CONVERT" text="CONVERT(NVARCHAR, GETDATE(), 0)" start-index="7" stop-index="37">
<parameter>
<data-type value="NVARCHAR" start-index="15" stop-index="22" />
</parameter>
<parameter>
<function function-name="GETDATE" text="GETDATE()" start-index="25" stop-index="33" />
</parameter>
<parameter>
<literal-expression value="0" start-index="7" stop-index="37" />
</parameter>
</function>
</expr>
</expression-projection>
</projections>
</select>

<select sql-case-id="select_with_convert_function4">
<projections start-index="7" stop-index="61">
<expression-projection text="CONVERT(DECIMAL(10, 5), CONVERT(VARBINARY(20), @myval))" start-index="7" stop-index="61">
<expr>
<function function-name="CONVERT" text="CONVERT(DECIMAL(10, 5), CONVERT(VARBINARY(20), @myval))" start-index="7" stop-index="61">
<parameter>
<data-type value="DECIMAL" start-index="15" stop-index="28" />
</parameter>
<parameter>
<function function-name="CONVERT" text="CONVERT(VARBINARY(20), @myval)" start-index="31" stop-index="60">
<parameter>
<data-type value="VARBINARY" start-index="39" stop-index="51" />
</parameter>
<parameter>
<column name="@myval" start-index="54" stop-index="59" />
</parameter>
</function>
</parameter>
</function>
</expr>
</expression-projection>
</projections>
</select>

<select sql-case-id="select_with_convert_function5">
<projections start-index="7" stop-index="69">
<expression-projection alias="Style 0, character to binary" text="CONVERT(BINARY(8), 'Name', 0)" start-index="7" stop-index="69">
<expr>
<function function-name="CONVERT" text="CONVERT(BINARY(8), 'Name', 0)" start-index="7" stop-index="35">
<parameter>
<data-type value="BINARY" start-index="15" stop-index="23" />
</parameter>
<parameter>
<literal-expression value="Name" start-index="26" stop-index="31" />
</parameter>
<parameter>
<literal-expression value="0" start-index="7" stop-index="35" />
</parameter>
</function>
</expr>
</expression-projection>
</projections>
</select>

<select sql-case-id="select_with_convert_xml1">
<projections start-index="7" stop-index="43">
<expression-projection text="CONVERT(XML, '&lt;root&gt;&lt;child/&gt;&lt;/root&gt;')" start-index="7" stop-index="43">
<expr>
<function function-name="CONVERT" text="CONVERT(XML, '&lt;root&gt;&lt;child/&gt;&lt;/root&gt;')" start-index="7" stop-index="43">
<parameter>
<data-type value="XML" start-index="15" stop-index="17" />
</parameter>
<parameter>
<literal-expression value="&lt;root&gt;&lt;child/&gt;&lt;/root&gt;" start-index="20" stop-index="42" />
</parameter>
</function>
</expr>
</expression-projection>
</projections>
</select>

<select sql-case-id="select_with_convert_xml2">
<projections start-index="7" stop-index="65">
<expression-projection text="CONVERT(XML, '&lt;root&gt; &lt;child/&gt; &lt;/root&gt;', 1)" start-index="7" stop-index="65">
<expr>
<function function-name="CONVERT" text="CONVERT(XML, '&lt;root&gt; &lt;child/&gt; &lt;/root&gt;', 1)" start-index="7" stop-index="65">
<parameter>
<data-type value="XML" start-index="15" stop-index="17" />
</parameter>
<parameter>
<literal-expression value="&lt;root&gt; &lt;child/&gt; &lt;/root&gt;" start-index="20" stop-index="61" />
</parameter>
<parameter>
<literal-expression value="1" start-index="7" stop-index="65" />
</parameter>
</function>
</expr>
</expression-projection>
</projections>
</select>

<select sql-case-id="select_cast_convert_function">
<projections start-index="7" stop-index="149">
<expression-projection alias="UnconvertedDateTime" text="GETDATE()" start-index="7" stop-index="38">
<expr>
<function function-name="GETDATE" text="GETDATE()" start-index="7" stop-index="15" />
</expr>
</expression-projection>
<expression-projection alias="UsingCast" text="CAST(GETDATE() AS NVARCHAR(30))" start-index="41" stop-index="84">
<expr>
<function function-name="CAST" text="CAST(GETDATE() AS NVARCHAR(30))" start-index="41" stop-index="71">
<parameter>
<data-type value="NVARCHAR" start-index="59" stop-index="70" />
</parameter>
</function>
</expr>
</expression-projection>
<expression-projection alias="UsingConvertTo_ISO8601" text="CONVERT(NVARCHAR(30), GETDATE(), 126)" start-index="87" stop-index="149">
<expr>
<function function-name="CONVERT" text="CONVERT(NVARCHAR(30), GETDATE(), 126)" start-index="87" stop-index="123">
<parameter>
<data-type value="NVARCHAR" start-index="95" stop-index="106" />
</parameter>
<parameter>
<function function-name="GETDATE" text="GETDATE()" start-index="109" stop-index="117" />
</parameter>
<parameter>
<literal-expression value="126" start-index="87" stop-index="123" />
</parameter>
</function>
</expr>
</expression-projection>
</projections>
</select>

<select sql-case-id="select_cast_convert_datetime">
<projections start-index="7" stop-index="187">
<expression-projection alias="UnconvertedText" text="2006-04-25T15:50:59.997" start-index="7" stop-index="50">
<expr>
<literal-expression value="2006-04-25T15:50:59.997" start-index="7" stop-index="31" />
</expr>
</expression-projection>
<expression-projection alias="UsingCast" text="CAST('2006-04-25T15:50:59.997' AS DATETIME)" start-index="53" stop-index="108">
<expr>
<function function-name="CAST" text="CAST('2006-04-25T15:50:59.997' AS DATETIME)" start-index="53" stop-index="95">
<parameter>
<literal-expression value="2006-04-25T15:50:59.997" start-index="58" stop-index="82" />
</parameter>
<parameter>
<data-type value="DATETIME" start-index="87" stop-index="94" />
</parameter>
</function>
</expr>
</expression-projection>
<expression-projection alias="UsingConvertFrom_ISO8601" text="CONVERT(DATETIME, '2006-04-25T15:50:59.997', 126)" start-index="111" stop-index="187">
<expr>
<function function-name="CONVERT" text="CONVERT(DATETIME, '2006-04-25T15:50:59.997', 126)" start-index="111" stop-index="159">
<parameter>
<data-type value="DATETIME" start-index="119" stop-index="126" />
</parameter>
<parameter>
<literal-expression value="2006-04-25T15:50:59.997" start-index="129" stop-index="153" />
</parameter>
<parameter>
<literal-expression value="126" start-index="111" stop-index="159" />
</parameter>
</function>
</expr>
</expression-projection>
</projections>
</select>

<select sql-case-id="select_with_convert_hex1">
<projections start-index="7" stop-index="71">
<expression-projection alias="Style 0, binary to character" text="CONVERT(CHAR(8), 0x4E616d65, 0)" start-index="7" stop-index="71">
<expr>
<function function-name="CONVERT" text="CONVERT(CHAR(8), 0x4E616d65, 0)" start-index="7" stop-index="37">
<parameter>
<data-type value="CHAR" start-index="15" stop-index="21" />
</parameter>
<parameter>
<common-expression literal-text="0x4E616d65" start-index="24" stop-index="33" />
</parameter>
<parameter>
<literal-expression value="0" start-index="7" stop-index="37" />
</parameter>
</function>
</expr>
</expression-projection>
</projections>
</select>

<select sql-case-id="select_with_convert_hex2">
<projections start-index="7" stop-index="71">
<expression-projection alias="Style 1, binary to character" text="CONVERT(CHAR(8), 0x4E616d65, 1)" start-index="7" stop-index="71">
<expr>
<function function-name="CONVERT" text="CONVERT(CHAR(8), 0x4E616d65, 1)" start-index="7" stop-index="37">
<parameter>
<data-type value="CHAR" start-index="15" stop-index="21" />
</parameter>
<parameter>
<common-expression literal-text="0x4E616d65" start-index="24" stop-index="33" />
</parameter>
<parameter>
<literal-expression value="1" start-index="7" stop-index="37" />
</parameter>
</function>
</expr>
</expression-projection>
</projections>
</select>

<select sql-case-id="select_with_convert_hex3">
<projections start-index="7" stop-index="75">
<expression-projection alias="Style 1, character to binary" text="CONVERT(BINARY(4), '0x4E616D65', 1)" start-index="7" stop-index="75">
<expr>
<function function-name="CONVERT" text="CONVERT(BINARY(4), '0x4E616D65', 1)" start-index="7" stop-index="41">
<parameter>
<data-type value="BINARY" start-index="15" stop-index="23" />
</parameter>
<parameter>
<literal-expression value="0x4E616D65" start-index="26" stop-index="37" />
</parameter>
<parameter>
<literal-expression value="1" start-index="7" stop-index="41" />
</parameter>
</function>
</expr>
</expression-projection>
</projections>
</select>

<select sql-case-id="select_with_json_extract">
<from>
<simple-table name="tb_content_json" start-index="70" stop-index="84" />
Expand Down
Loading