Skip to content
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
1 change: 1 addition & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
1. SQL Parser: Fix SQL parser error when SQL contains subquery with alias for Oracle - [#35239](https://github.com/apache/shardingsphere/pull/35239)
1. SQL Binder: Fix unable to find the outer table in the NotExpressionBinder - [36135](https://github.com/apache/shardingsphere/pull/36135)
1. SQL Binder: Fix unable to find the outer table in the ExistsSubqueryExpressionBinder - [#36068](https://github.com/apache/shardingsphere/pull/36068)
1. SQL Binder: Fix column bind exception caused by oracle XMLELEMENT function first parameter without quote - [#36963](https://github.com/apache/shardingsphere/pull/36963)
1. Transaction: Fix conflicting dependencies of BASE transaction integration module - [#35142](https://github.com/apache/shardingsphere/pull/35142)
1. Transaction: Alleviate connection leaks caused by SEATA client throwing exceptions - [#34463](https://github.com/apache/shardingsphere/pull/34463)
1. SQL Federation: Fix Operation not allowed after ResultSet closed exception when use SQL federation - [#35206](https://github.com/apache/shardingsphere/pull/35206)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.shardingsphere.infra.binder.engine.segment.dml.expression.type;

import com.cedarsoftware.util.CaseInsensitiveMap;
import com.cedarsoftware.util.CaseInsensitiveMap.CaseInsensitiveString;
import com.google.common.collect.Multimap;
import lombok.AccessLevel;
Expand All @@ -25,16 +26,23 @@
import org.apache.shardingsphere.infra.binder.engine.segment.dml.expression.ExpressionSegmentBinder;
import org.apache.shardingsphere.infra.binder.engine.segment.dml.from.context.TableSegmentBinderContext;
import org.apache.shardingsphere.infra.binder.engine.statement.SQLStatementBinderContext;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.ExpressionSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.FunctionSegment;

import java.util.stream.Collectors;
import java.util.Map;

/**
* Function expression binder.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class FunctionExpressionSegmentBinder {

private static final Map<String, Integer> SKIP_BIND_FUNCTION_PARAMETER_INDEXES = new CaseInsensitiveMap<>();

static {
SKIP_BIND_FUNCTION_PARAMETER_INDEXES.put("XMLELEMENT", 0);
}

/**
* Bind function expression.
*
Expand All @@ -50,8 +58,15 @@ public static FunctionSegment bind(final FunctionSegment segment, final SegmentT
final Multimap<CaseInsensitiveString, TableSegmentBinderContext> outerTableBinderContexts) {
FunctionSegment result = new FunctionSegment(segment.getStartIndex(), segment.getStopIndex(), segment.getFunctionName(), segment.getText());
result.setOwner(segment.getOwner());
result.getParameters().addAll(segment.getParameters().stream()
.map(each -> ExpressionSegmentBinder.bind(each, parentSegmentType, binderContext, tableBinderContexts, outerTableBinderContexts)).collect(Collectors.toList()));
int index = 0;
Integer skipParameterIndex = SKIP_BIND_FUNCTION_PARAMETER_INDEXES.getOrDefault(segment.getFunctionName(), -1);
for (ExpressionSegment each : segment.getParameters()) {
if (skipParameterIndex >= 0 && index++ == skipParameterIndex) {
result.getParameters().add(each);
continue;
}
result.getParameters().add(ExpressionSegmentBinder.bind(each, parentSegmentType, binderContext, tableBinderContexts, outerTableBinderContexts));
}
segment.getWindow().ifPresent(result::setWindow);
return result;
}
Expand Down
125 changes: 125 additions & 0 deletions test/it/binder/src/test/resources/cases/dml/select-function.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
-->

<sql-parser-test-cases>
<select sql-case-id="select_with_xml_function">
<projections start-index="7" stop-index="144">
<expression-projection text="to_char(XMLAGGG(XMLELEMENT(E, ( SELECT i.item_id FROM t_order_item i WHERE i.item_id = 1 ) OR ',')).EXTRACT('//text()').getclobval())" alias="n" start-index="7" stop-index="144">
<expr>
<function function-name="to_char" text="to_char(XMLAGGG(XMLELEMENT(E, ( SELECT i.item_id FROM t_order_item i WHERE i.item_id = 1 ) OR ',')).EXTRACT('//text()').getclobval())" start-index="7" stop-index="139">
<parameter>
<binary-operation-expression start-index="15" stop-index="138">
<left>
<binary-operation-expression start-index="15" stop-index="125">
<left>
<function function-name="XMLAGGG" text="XMLAGGG(XMLELEMENT(E, ( SELECT i.item_id FROM t_order_item i WHERE i.item_id = 1 ) OR ','))" start-index="15" stop-index="105">
<parameter>
<function function-name="XMLELEMENT" text="XMLELEMENT(E, ( SELECT i.item_id FROM t_order_item i WHERE i.item_id = 1 ) OR ',')" start-index="23" stop-index="104">
<parameter>
<column name="E" start-index="34" stop-index="34" />
</parameter>
<parameter>
<binary-operation-expression start-index="37" stop-index="103">
<left>
<subquery start-index="37" stop-index="96">
<select>
<projections start-index="46" stop-index="54">
<column-projection name="item_id" start-index="46" stop-index="54">
<owner name="i" start-index="46" stop-index="46" />
<column-bound>
<original-database name="foo_db_1" />
<original-schema name="foo_db_1" />
<original-table name="t_order_item" />
<original-column name="item_id" start-delimiter="&quot;" end-delimiter="&quot;" />
<table-source-type name="PHYSICAL_TABLE"/>
</column-bound>
</column-projection>
</projections>
<from>
<simple-table name="t_order_item" alias="i" start-index="61" stop-index="74">
<table-bound>
<original-database name="foo_db_1" />
<original-schema name="foo_db_1" />
</table-bound>
</simple-table>
</from>
<where start-index="76" stop-index="94">
<expr>
<binary-operation-expression start-index="82" stop-index="94">
<left>
<column name="item_id" start-index="82" stop-index="90" >
<owner name="i" start-index="82" stop-index="82" />
<column-bound>
<original-database name="foo_db_1" />
<original-schema name="foo_db_1" />
<original-table name="t_order_item" />
<original-column name="item_id" start-delimiter="&quot;" end-delimiter="&quot;" />
<table-source-type name="PHYSICAL_TABLE"/>
</column-bound>
</column>
</left>
<operator>=</operator>
<right>
<literal-expression value="1" start-index="94" stop-index="94" />
</right>
</binary-operation-expression>
</expr>
</where>
</select>
</subquery>
</left>
<operator>OR</operator>
<right>
<literal-expression value="," start-index="101" stop-index="103" />
</right>
</binary-operation-expression>
</parameter>
</function>
</parameter>
</function>
</left>
<operator>.</operator>
<right>
<function function-name="EXTRACT" text="EXTRACT('//text()')" start-index="107" stop-index="125">
<parameter>
<literal-expression value="//text()" start-index="115" stop-index="124" />
</parameter>
</function>
</right>
</binary-operation-expression>
</left>
<operator>.</operator>
<right>
<function function-name="getclobval" text="getclobval()" start-index="127" stop-index="138" />
</right>
</binary-operation-expression>
</parameter>
</function>
</expr>
</expression-projection>
</projections>
<from>
<simple-table name="DUAL" start-index="151" stop-index="154">
<table-bound>
<original-database name="foo_db_1" />
<original-schema name="foo_db_1" />
</table-bound>
</simple-table>
</from>
</select>
</sql-parser-test-cases>
21 changes: 21 additions & 0 deletions test/it/binder/src/test/resources/sqls/dml/select-function.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
-->

<sql-cases>
<sql-case id="select_with_xml_function" value="SELECT to_char(XMLAGGG(XMLELEMENT(E, ( SELECT i.item_id FROM t_order_item i WHERE i.item_id = 1 ) OR ',')).EXTRACT('//text()').getclobval()) AS n FROM DUAL" db-types="Oracle"/>
</sql-cases>