This is an automated email from the ASF dual-hosted git repository.

jianglongtao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new abe6923a3b4 [Oracle SQL] Support parsing of XMLElement and XMLCdata 
Function (#28265)
abe6923a3b4 is described below

commit abe6923a3b428e2dc9f797b1116b437091c91e89
Author: Liao Lanyu <[email protected]>
AuthorDate: Mon Sep 4 16:01:30 2023 +0800

    [Oracle SQL] Support parsing of XMLElement and XMLCdata Function (#28265)
    
    * fix
    
    * fix import
---
 .../src/main/antlr4/imports/oracle/BaseRule.g4     | 20 +++++++-
 .../visitor/statement/OracleStatementVisitor.java  | 26 +++++++++++
 .../statement/type/OracleDMLStatementVisitor.java  |  4 +-
 .../dml/expr/XmlElementFunctionSegment.java        | 54 ++++++++++++++++++++++
 4 files changed, 102 insertions(+), 2 deletions(-)

diff --git 
a/parser/sql/dialect/oracle/src/main/antlr4/imports/oracle/BaseRule.g4 
b/parser/sql/dialect/oracle/src/main/antlr4/imports/oracle/BaseRule.g4
index be5bce3d07a..686c07c74fa 100644
--- a/parser/sql/dialect/oracle/src/main/antlr4/imports/oracle/BaseRule.g4
+++ b/parser/sql/dialect/oracle/src/main/antlr4/imports/oracle/BaseRule.g4
@@ -1941,7 +1941,9 @@ datetimeExpr
     ;
 
 xmlFunction
-    : xmlAggFunction
+    : xmlElementFunction
+    | xmlCdataFunction
+    | xmlAggFunction
     | xmlColattvalFunction
     | xmlExistsFunction
     | xmlForestFunction
@@ -1959,6 +1961,22 @@ xmlFunction
     | specifiedFunctionName = XMLCOMMENT LP_ stringLiterals RP_
     ;
 
+xmlElementFunction
+    : XMLELEMENT LP_ identifier (COMMA_ xmlAttributes)? (COMMA_ 
exprWithAlias)* RP_
+    ;
+
+exprWithAlias
+    : expr (AS alias)?
+    ;
+
+xmlAttributes
+    : XMLATTRIBUTES LP_ exprWithAlias (COMMA_ exprWithAlias)* RP_
+    ;
+
+xmlCdataFunction
+    : XMLCDATA LP_ stringLiterals RP_
+    ;
+
 xmlAggFunction
     : XMLAGG LP_ expr orderByClause? RP_
     ;
diff --git 
a/parser/sql/dialect/oracle/src/main/java/org/apache/shardingsphere/sql/parser/oracle/visitor/statement/OracleStatementVisitor.java
 
b/parser/sql/dialect/oracle/src/main/java/org/apache/shardingsphere/sql/parser/oracle/visitor/statement/OracleStatementVisitor.java
index 9d94caa9781..bb999a1a045 100644
--- 
a/parser/sql/dialect/oracle/src/main/java/org/apache/shardingsphere/sql/parser/oracle/visitor/statement/OracleStatementVisitor.java
+++ 
b/parser/sql/dialect/oracle/src/main/java/org/apache/shardingsphere/sql/parser/oracle/visitor/statement/OracleStatementVisitor.java
@@ -79,7 +79,9 @@ import 
org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.TypeNa
 import 
org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.UnreservedWordContext;
 import 
org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.ViewNameContext;
 import 
org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.XmlAggFunctionContext;
+import 
org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.XmlCdataFunctionContext;
 import 
org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.XmlColattvalFunctionContext;
+import 
org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.XmlElementFunctionContext;
 import 
org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.XmlExistsFunctionContext;
 import 
org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.XmlForestFunctionContext;
 import 
org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.XmlFunctionContext;
@@ -116,6 +118,7 @@ import 
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.Interval
 import 
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ListExpression;
 import 
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.MultisetExpression;
 import 
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.NotExpression;
+import 
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.XmlElementFunctionSegment;
 import 
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.XmlNameSpaceStringAsIdentifierSegment;
 import 
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.XmlNameSpacesClauseSegment;
 import 
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.XmlPiFunctionSegment;
@@ -731,11 +734,34 @@ public abstract class OracleStatementVisitor extends 
OracleStatementBaseVisitor<
         if (null != ctx.xmlTableFunction()) {
             return visit(ctx.xmlTableFunction());
         }
+        if (null != ctx.xmlElementFunction()) {
+            return visit(ctx.xmlElementFunction());
+        }
         FunctionSegment result = new 
FunctionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), 
ctx.specifiedFunctionName.getText(), getOriginalText(ctx));
         result.getParameters().addAll(getExpressions(ctx.exprList()));
         return result;
     }
     
+    @Override
+    public ASTNode visitXmlElementFunction(final XmlElementFunctionContext 
ctx) {
+        XmlElementFunctionSegment result =
+                new XmlElementFunctionSegment(ctx.start.getStartIndex(), 
ctx.stop.getStopIndex(), ctx.XMLELEMENT().getText(), (IdentifierValue) 
visit(ctx.identifier()), getOriginalText(ctx));
+        Collection<ExpressionSegment> expressionSegments = 
ctx.exprWithAlias().stream().map(each -> (ExpressionSegment) 
visit(each.expr())).collect(Collectors.toList());
+        result.getParameters().addAll(expressionSegments);
+        if (null != ctx.xmlAttributes()) {
+            Collection<ExpressionSegment> xmlAttributes = 
ctx.xmlAttributes().exprWithAlias().stream().map(each -> (ExpressionSegment) 
visit(each.expr())).collect(Collectors.toList());
+            result.getXmlAttributes().addAll(xmlAttributes);
+        }
+        return result;
+    }
+    
+    @Override
+    public ASTNode visitXmlCdataFunction(final XmlCdataFunctionContext ctx) {
+        FunctionSegment result = new 
FunctionSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), 
ctx.XMLCDATA().getText(), getOriginalText(ctx));
+        result.getParameters().add((ExpressionSegment) 
visit(ctx.stringLiterals()));
+        return result;
+    }
+    
     @Override
     public ASTNode visitXmlAggFunction(final XmlAggFunctionContext ctx) {
         return new FunctionSegment(ctx.getStart().getStartIndex(), 
ctx.getStop().getStopIndex(), ctx.XMLAGG().getText(), getOriginalText(ctx));
diff --git 
a/parser/sql/dialect/oracle/src/main/java/org/apache/shardingsphere/sql/parser/oracle/visitor/statement/type/OracleDMLStatementVisitor.java
 
b/parser/sql/dialect/oracle/src/main/java/org/apache/shardingsphere/sql/parser/oracle/visitor/statement/type/OracleDMLStatementVisitor.java
index 681a7baadc6..c97e0ddf73e 100644
--- 
a/parser/sql/dialect/oracle/src/main/java/org/apache/shardingsphere/sql/parser/oracle/visitor/statement/type/OracleDMLStatementVisitor.java
+++ 
b/parser/sql/dialect/oracle/src/main/java/org/apache/shardingsphere/sql/parser/oracle/visitor/statement/type/OracleDMLStatementVisitor.java
@@ -121,6 +121,7 @@ import 
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.Expressi
 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.MultisetExpression;
+import 
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.XmlElementFunctionSegment;
 import 
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.XmlPiFunctionSegment;
 import 
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.XmlQueryAndExistsFunctionSegment;
 import 
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.XmlSerializeFunctionSegment;
@@ -741,7 +742,8 @@ public final class OracleDMLStatementVisitor extends 
OracleStatementVisitor impl
             result.setAlias(alias);
             return result;
         }
-        if (projection instanceof XmlQueryAndExistsFunctionSegment || 
projection instanceof XmlPiFunctionSegment || projection instanceof 
XmlSerializeFunctionSegment) {
+        if (projection instanceof XmlQueryAndExistsFunctionSegment || 
projection instanceof XmlPiFunctionSegment || projection instanceof 
XmlSerializeFunctionSegment
+                || projection instanceof XmlElementFunctionSegment) {
             return projection;
         }
         throw new UnsupportedOperationException("Unsupported Complex 
Expression");
diff --git 
a/parser/sql/statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/segment/dml/expr/XmlElementFunctionSegment.java
 
b/parser/sql/statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/segment/dml/expr/XmlElementFunctionSegment.java
new file mode 100644
index 00000000000..ae5b214af89
--- /dev/null
+++ 
b/parser/sql/statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/segment/dml/expr/XmlElementFunctionSegment.java
@@ -0,0 +1,54 @@
+/*
+ * 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 
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.complex.ComplexExpressionSegment;
+import 
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ProjectionSegment;
+import 
org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
+
+import java.util.Collection;
+import java.util.LinkedList;
+
+/**
+ * Xml element function segment.
+ */
+@RequiredArgsConstructor
+@Getter
+public class XmlElementFunctionSegment implements ComplexExpressionSegment, 
ProjectionSegment {
+    
+    private final int startIndex;
+    
+    private final int stopIndex;
+    
+    private final String functionName;
+    
+    private final IdentifierValue identifier;
+    
+    private final Collection<ExpressionSegment> xmlAttributes = new 
LinkedList<>();
+    
+    private final Collection<ExpressionSegment> parameters = new 
LinkedList<>();
+    
+    private final String text;
+    
+    @Override
+    public String getColumnLabel() {
+        return text;
+    }
+}

Reply via email to