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 SQL Server SELECT JSON_OBJECT('name':'value', sql #29160 #29740

Merged
merged 4 commits into from
Jan 19, 2024
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 @@ -311,7 +311,27 @@ distinct
;

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

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 @@ -138,6 +143,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 @@ -197,6 +203,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 @@ -665,9 +672,66 @@ public final ASTNode visitSpecialFunction(final SpecialFunctionContext ctx) {
if (null != ctx.openRowSetFunction()) {
return visit(ctx.openRowSetFunction());
}
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(), getOriginalText(ctx));
}

@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