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 parsing Doris sql(#31507) #33316

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
15 changes: 15 additions & 0 deletions parser/sql/dialect/doris/src/main/antlr4/imports/doris/BaseRule.g4
Original file line number Diff line number Diff line change
Expand Up @@ -1046,8 +1046,23 @@ specialFunction
| groupingFunction
| timeStampDiffFunction
| bitwiseFunction
| dateFunction
| strrightFunction
| rtrimFunction
;

dateFunction
: yearWeekFunction
;
yearWeekFunction
: YEARWEEK LP_ expr (COMMA_ expr)? RP_
;
rtrimFunction
: RTRIM LP_ expr (COMMA_ expr)? RP_
;
strrightFunction
: (STRRIGHT | RIGHT) LP_ expr COMMA_ expr RP_
;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add new line after this systax.

currentUserFunction
: CURRENT_USER (LP_ RP_)?
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,13 @@ installComponent
;

installPlugin
: INSTALL PLUGIN pluginName SONAME shardLibraryName
// : INSTALL PLUGIN pluginName SONAME shardLibraryName
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this cooment.

: INSTALL PLUGIN FROM pluginSource=expr propertyClause?
;

propertyClause
: PROPERTIES LP_ expr EQ_ md5sum=expr RP_

;

uninstallComponent
Expand Down Expand Up @@ -476,3 +482,7 @@ show
| showReplicas
| showReplicaStatus
;

dbManageStatement
: ADMIN CANCEL REBALANCE DISK (ON LP_ backends+=stringLiterals (COMMA_ backends+=stringLiterals) RP_)?
;
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,10 @@ PERFORMANCE_SCHEMA
TOKUDB
: T O K U D B
;

LTRIM : L T R I M;
RTRIM : R T R I M;
CANCEL : C A N C E L;
REBALANCE : R E B A L A N C E;
STRRIGHT : S T R R I G H T;
YEARWEEK : Y E A R W E E K;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add new line at the end of file.

Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ execute
| dropTablespace
| delimiter
| startReplica
| dbManageStatement
// TODO consider refactor following sytax to SEMI_? EOF
) (SEMI_ EOF? | EOF)
| EOF
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.antlr.v4.runtime.tree.TerminalNode;
import org.apache.shardingsphere.sql.parser.api.ASTNode;
import org.apache.shardingsphere.sql.parser.autogen.DorisStatementBaseVisitor;
import org.apache.shardingsphere.sql.parser.autogen.DorisStatementParser;
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.AssignmentContext;
Expand Down Expand Up @@ -1031,9 +1032,34 @@ public final ASTNode visitSpecialFunction(final SpecialFunctionContext ctx) {
if (null != ctx.bitwiseFunction()) {
return visit(ctx.bitwiseFunction());
}
if (null != ctx.dateFunction()) {
return visit(ctx.dateFunction());
}
if (null != ctx.strrightFunction()) {
return visit(ctx.strrightFunction());
}
if (null != ctx.rtrimFunction()) {
return visit(ctx.rtrimFunction());
}
return new FunctionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), getOriginalText(ctx), getOriginalText(ctx));
}

@Override
public ASTNode visitRtrimFunction(final DorisStatementParser.RtrimFunctionContext ctx) {
FunctionSegment result = new FunctionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), ctx.RTRIM().getText(), getOriginalText(ctx));
for (ExprContext each : ctx.expr()) {
result.getParameters().add((ExpressionSegment) visit(each));
}
return result;
}

@Override
public ASTNode visitStrrightFunction(final DorisStatementParser.StrrightFunctionContext ctx) {
FunctionSegment result = new FunctionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), ctx.STRRIGHT().getText(), getOriginalText(ctx));
result.getParameters().addAll(getExpressions(ctx.expr()));
return result;
}

@Override
public final ASTNode visitGroupConcatFunction(final GroupConcatFunctionContext ctx) {
calculateParameterCount(ctx.expr());
Expand Down Expand Up @@ -1131,6 +1157,24 @@ public final ASTNode visitCharFunction(final CharFunctionContext ctx) {
return result;
}

@Override
public ASTNode visitDateFunction(final DorisStatementParser.DateFunctionContext ctx) {
if (null != ctx.yearWeekFunction()) {
return visit(ctx.yearWeekFunction());
}
return new FunctionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), getOriginalText(ctx), getOriginalText(ctx));
}

@Override
public ASTNode visitYearWeekFunction(final DorisStatementParser.YearWeekFunctionContext ctx) {
FunctionSegment result = new FunctionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), ctx.YEARWEEK().getText(), getOriginalText(ctx));
for (ExprContext each : ctx.expr()) {
result.getParameters().add((ExpressionSegment) visit(each));
}

return result;
}

@Override
public final ASTNode visitTrimFunction(final TrimFunctionContext ctx) {
FunctionSegment result = new FunctionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), ctx.TRIM().getText(), getOriginalText(ctx));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,10 @@ public ASTNode visitLoadTableIndexList(final LoadTableIndexListContext ctx) {
@Override
public ASTNode visitInstallPlugin(final InstallPluginContext ctx) {
DorisInstallPluginStatement result = new DorisInstallPluginStatement();
result.setPluginName(((IdentifierValue) visit(ctx.pluginName())).getValue());
result.setSource(getOriginalText(ctx.pluginSource));
if (null != ctx.propertyClause()) {
result.setMd5sum(getOriginalText(ctx.propertyClause().md5sum));
}
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,19 @@

package org.apache.shardingsphere.sql.parser.statement.doris.dal;

import lombok.Getter;
import lombok.Setter;
import org.apache.shardingsphere.sql.parser.statement.core.statement.dal.InstallPluginStatement;
import org.apache.shardingsphere.sql.parser.statement.doris.DorisStatement;

/**
* Doris install plugin statement.
*/
@Setter
@Getter
public final class DorisInstallPluginStatement extends InstallPluginStatement implements DorisStatement {

private String source;

private String md5sum;
}
4 changes: 4 additions & 0 deletions test/it/parser/src/main/resources/case/dal/install.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@
<install-plugin sql-case-id="install_plugin">
<plugin name="binlog" start-index="15" stop-index="20" />
</install-plugin>
<install-plugin sql-case-id="install_plugin_doris">
<plugin source="http://mywebsite.com/plugin.zip" start-index="20" stop-index="52" />
<plugin md5sum="73877f6029216f4314d712086a146570" start-index="76" stop-index="109" />
</install-plugin>
</sql-parser-test-cases>
46 changes: 46 additions & 0 deletions test/it/parser/src/main/resources/case/dml/select.xml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,52 @@
</expression-projection>
</projections>
</select>
<select sql-case-id="select_rtrim_function">
<projections start-index="7" stop-index="29">
<expression-projection text="rtrim('ababccaab','ab')" start-index="7" stop-index="29">
<expr>
<function start-index="7" stop-index="29" text="rtrim('ababccaab','ab')" function-name="rtrim">
<parameter>
<literal-expression start-index="13" stop-index="23" value="ababccaab" />
</parameter>
<parameter>
<literal-expression start-index="25" stop-index="28" value="ab" />
</parameter>
</function>
</expr>
</expression-projection>
</projections>
</select>
<select sql-case-id="select_strright_function">
<projections start-index="7" stop-index="31">
<expression-projection text="strright(&quot;Hello doris&quot;,5)" start-index="7" stop-index="31">
<expr>
<function start-index="7" stop-index="31" text="strright(&quot;Hello doris&quot;,5)" function-name="strright">
<parameter>
<literal-expression start-index="16" stop-index="28" value="Hello doris" />
</parameter>
<parameter>
<literal-expression start-index="30" stop-index="30" value="5" />
</parameter>
</function>
</expr>
</expression-projection>
</projections>
</select>

<select sql-case-id="select_yearweek_function">
<projections start-index="7" stop-index="26">
<expression-projection text="yearweek('2021-1-1')" start-index="7" stop-index="26">
<expr>
<function start-index="7" stop-index="26" text="yearweek('2021-1-1')" function-name="yearweek">
<parameter>
<literal-expression start-index="16" stop-index="25" value="2021-1-1" />
</parameter>
</function>
</expr>
</expression-projection>
</projections>
</select>

<select sql-case-id="select_with_latin1">
<projections start-index="7" stop-index="62">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@
<sql-case id="install_component" value="INSTALL COMPONENT 'file://component1'" db-types="MySQL" />
<sql-case id="install_components" value="INSTALL COMPONENT 'file://component1', 'file://component2'" db-types="MySQL" />
<sql-case id="install_plugin" value="INSTALL PLUGIN binlog SONAME 'shared_library_name'" db-types="MySQL" />

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove useless blank line.

<sql-case id="install_plugin_doris" value="INSTALL PLUGIN FROM &quot;http://mywebsite.com/plugin.zip&quot; PROPERTIES( &quot;md5sum &quot; = &quot;73877f6029216f4314d712086a146570 &quot;)" db-types="Doris" />

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove useless blank line.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your review. I made some optimizations.

</sql-cases>
Original file line number Diff line number Diff line change
Expand Up @@ -285,4 +285,7 @@
<sql-case id="select_with_nvl_function_and_interval_hour" value="SELECT * FROM t_order t WHERE t.CREATE_TIME &lt;= nvl(END_TIME, sysdate) - INTERVAL ? HOUR AND t.STATUS = 'FAILURE'" db-types="Oracle"/>
<sql-case id="select_with_not_operator_number" value="SELECT NOT 0, NOT 1, NOT 2" db-types="MySQL" />
<sql-case id="select_with_not_operator_boolean" value="SELECT NOT TRUE, NOT FALSE" db-types="MySQL" />
<sql-case id="select_yearweek_function" value="select yearweek('2021-1-1')" db-types="Doris" />
<sql-case id="select_rtrim_function" value="SELECT rtrim('ababccaab','ab')" db-types="Doris" />
<sql-case id="select_strright_function" value="select strright(&quot;Hello doris&quot;,5)" db-types="Doris" />
</sql-cases>