Skip to content

Commit

Permalink
Support parsing SQL Server SELECT JSON_OBJECT('name':'value', sql apa…
Browse files Browse the repository at this point in the history
  • Loading branch information
yydeng626 committed Jan 16, 2024
1 parent 9d21e57 commit 3a6ad1f
Show file tree
Hide file tree
Showing 11 changed files with 529 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,27 @@ distinct
;

specialFunction
: castFunction | charFunction | convertFunction | openJsonFunction
: castFunction | charFunction | convertFunction | openJsonFunction | jsonFunction
;

jsonFunction
: jsonObjectFunction | jsonArrayFunction
;

jsonObjectFunction
: JSON_OBJECT LP_ (jsonKeyValue (COMMA_ jsonKeyValue)* jsonNullClause?)? RP_
;

jsonArrayFunction
: JSON_ARRAY LP_ expr (COMMA_ expr)* jsonNullClause? RP_
;

jsonKeyValue
: expr COLON_ expr
;

jsonNullClause
: NULL ON NULL | ABSENT ON NULL
;

castFunction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -728,50 +728,54 @@ NOLOCK
: N O L O C K
;

NOLOCK
: N O L O C K
;

NOWAIT
: N O W A I T
;
: N O W A I T
;

PAGLOCK
: P A G L O C K
;
: P A G L O C K
;

READCOMMITTED
: R E A D C O M M I T T E D
;
: R E A D C O M M I T T E D
;

READCOMMITTEDLOCK
: R E A D C O M M I T T E D L O C K
;
: R E A D C O M M I T T E D L O C K
;

READPAST
: R E A D P A S T
;
: R E A D P A S T
;

REPEATABLEREAD
: R E P E A T A B L E R E A D
;
: R E P E A T A B L E R E A D
;

ROWLOCK
: R O W L O C K
;
: R O W L O C K
;

TABLOCK
: T A B L O C K
;
: T A B L O C K
;

TABLOCKX
: T A B L O C K X
;
: T A B L O C K X
;

UPDLOCK
: U P D L O C K
;
: U P D L O C K
;

XLOCK
: X L O C K
;
: X L O C K
;

JSON_OBJECT
: J S O N UL_ O B J E C T
;

JSON_ARRAY
: J S O N UL_ A R R A Y
;
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.IndexNameContext;
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.InsertContext;
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.InsertDefaultValueContext;
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.JsonArrayFunctionContext;
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.JsonFunctionContext;
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.JsonKeyValueContext;
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.JsonNullClauseContext;
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.JsonObjectFunctionContext;
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.WithTableHintContext;
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.InsertSelectClauseContext;
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.InsertValuesClauseContext;
Expand Down Expand Up @@ -137,6 +142,7 @@
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.FunctionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.InExpression;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.KeyValueSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ListExpression;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.NotExpression;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.complex.CommonExpressionSegment;
Expand Down Expand Up @@ -196,6 +202,7 @@
import org.apache.shardingsphere.sql.parser.sql.common.value.literal.impl.StringLiteralValue;
import org.apache.shardingsphere.sql.parser.sql.common.value.parametermarker.ParameterMarkerValue;
import org.apache.shardingsphere.sql.parser.sql.dialect.segment.sqlserver.exec.ExecSegment;
import org.apache.shardingsphere.sql.parser.sql.dialect.segment.sqlserver.json.JsonNullClauseSegment;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.sqlserver.ddl.SQLServerCreateTableStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.sqlserver.ddl.SQLServerUpdateStatisticsStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.sqlserver.dml.SQLServerDeleteStatement;
Expand Down Expand Up @@ -661,9 +668,66 @@ public final ASTNode visitSpecialFunction(final SpecialFunctionContext ctx) {
if (null != ctx.openJsonFunction()) {
return visit(ctx.openJsonFunction());
}
if (null != ctx.jsonFunction()) {
return visit(ctx.jsonFunction());
}
return new FunctionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), ctx.getChild(0).getChild(0).getText(), getOriginalText(ctx));
}

@Override
public final ASTNode visitJsonFunction(final JsonFunctionContext ctx) {
if (null != ctx.jsonArrayFunction()) {
return visit(ctx.jsonArrayFunction());
}
if (null != ctx.jsonObjectFunction()) {
return visit(ctx.jsonObjectFunction());
}
return new FunctionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), ctx.getText(), getOriginalText(ctx));
}

@Override
public final ASTNode visitJsonArrayFunction(final JsonArrayFunctionContext ctx) {
FunctionSegment result = new FunctionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), ctx.JSON_ARRAY().getText(), getOriginalText(ctx));
if (null != ctx.expr()) {
for (ExprContext each : ctx.expr()) {
result.getParameters().add((ExpressionSegment) visit(each));
}
}
if (null != ctx.jsonNullClause()) {
result.getParameters().add(new LiteralExpressionSegment(ctx.jsonNullClause().start.getStartIndex(), ctx.jsonNullClause().stop.getStopIndex(), ctx.jsonNullClause().getText()));
}
return result;
}

@Override
public final ASTNode visitJsonObjectFunction(final JsonObjectFunctionContext ctx) {
FunctionSegment result = new FunctionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), ctx.JSON_OBJECT().getText(), getOriginalText(ctx));
if (null != ctx.jsonKeyValue()) {
for (JsonKeyValueContext each : ctx.jsonKeyValue()) {
result.getParameters().add((ExpressionSegment) visit(each));
}
}
if (null != ctx.jsonNullClause()) {
result.getParameters().add((ExpressionSegment) visit(ctx.jsonNullClause()));
}
return result;
}

@Override
public final ASTNode visitJsonNullClause(final JsonNullClauseContext ctx) {
return new JsonNullClauseSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), ctx.getText());
}

@Override
public final ASTNode visitJsonKeyValue(final JsonKeyValueContext ctx) {
KeyValueSegment result = new KeyValueSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), ctx.getText());
if (null != ctx.expr()) {
result.setKey((ExpressionSegment) visit(ctx.expr(0)));
result.setValue((ExpressionSegment) visit(ctx.expr(1)));
}
return result;
}

@Override
public final ASTNode visitCastFunction(final CastFunctionContext ctx) {
calculateParameterCount(Collections.singleton(ctx.expr()));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;

/**
* Key value segment.
**/
@RequiredArgsConstructor
@Getter
public final class KeyValueSegment implements ExpressionSegment {

private final int startIndex;

private final int stopIndex;

@Setter
private ExpressionSegment key;

@Setter
private ExpressionSegment value;

private final String text;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.shardingsphere.sql.parser.sql.dialect.segment.sqlserver.json;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionSegment;

/**
* Json null clause segment.
**/
@RequiredArgsConstructor
@Getter
public final class JsonNullClauseSegment implements ExpressionSegment {

private final int startIndex;

private final int stopIndex;

private final String text;
}
Loading

0 comments on commit 3a6ad1f

Please sign in to comment.