This is an automated email from the ASF dual-hosted git repository.
tuichenchuxin 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 85727ad8907 Fix sql binder exception when tables join without owner
(#28244)
85727ad8907 is described below
commit 85727ad89072f1a1f4273b2aeb826cc556f3edfd
Author: Zhengqiang Duan <[email protected]>
AuthorDate: Thu Aug 24 10:00:38 2023 +0800
Fix sql binder exception when tables join without owner (#28244)
---
.../expression/impl/ColumnSegmentBinder.java | 9 ++-
.../expression/impl/ColumnSegmentBinderTest.java | 67 ++++++++++++++++++++++
2 files changed, 73 insertions(+), 3 deletions(-)
diff --git
a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/expression/impl/ColumnSegmentBinder.java
b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/expression/impl/ColumnSegmentBinder.java
index bf809ff0de2..64d7156befe 100644
---
a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/expression/impl/ColumnSegmentBinder.java
+++
b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/expression/impl/ColumnSegmentBinder.java
@@ -108,17 +108,20 @@ public final class ColumnSegmentBinder {
}
private static Optional<ColumnSegment> findInputColumnSegment(final
ColumnSegment segment, final SegmentType parentSegmentType, final
Collection<TableSegmentBinderContext> tableBinderContexts) {
- ProjectionSegment projectionSegment = null;
ColumnSegment result = null;
+ boolean isFindInputColumn = false;
for (TableSegmentBinderContext each : tableBinderContexts) {
- projectionSegment =
each.getProjectionSegmentByColumnLabel(segment.getIdentifier().getValue());
+ ProjectionSegment projectionSegment =
each.getProjectionSegmentByColumnLabel(segment.getIdentifier().getValue());
if (projectionSegment instanceof ColumnProjectionSegment) {
ShardingSpherePreconditions.checkState(null == result,
() -> new
AmbiguousColumnException(segment.getExpression(),
SEGMENT_TYPE_MESSAGES.getOrDefault(parentSegmentType,
UNKNOWN_SEGMENT_TYPE_MESSAGE)));
result = ((ColumnProjectionSegment)
projectionSegment).getColumn();
}
+ if (!isFindInputColumn && null != projectionSegment) {
+ isFindInputColumn = true;
+ }
}
- ShardingSpherePreconditions.checkState(null != projectionSegment,
+ ShardingSpherePreconditions.checkState(isFindInputColumn,
() -> new UnknownColumnException(segment.getExpression(),
SEGMENT_TYPE_MESSAGES.getOrDefault(parentSegmentType,
UNKNOWN_SEGMENT_TYPE_MESSAGE)));
return Optional.ofNullable(result);
}
diff --git
a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/expression/impl/ColumnSegmentBinderTest.java
b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/expression/impl/ColumnSegmentBinderTest.java
new file mode 100644
index 00000000000..2a6286996b7
--- /dev/null
+++
b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/expression/impl/ColumnSegmentBinderTest.java
@@ -0,0 +1,67 @@
+/*
+ * 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.infra.binder.segment.expression.impl;
+
+import org.apache.commons.collections4.map.CaseInsensitiveMap;
+import org.apache.shardingsphere.infra.binder.enums.SegmentType;
+import
org.apache.shardingsphere.infra.binder.segment.from.TableSegmentBinderContext;
+import
org.apache.shardingsphere.infra.binder.statement.SQLStatementBinderContext;
+import org.apache.shardingsphere.infra.database.core.DefaultDatabase;
+import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
+import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
+import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ColumnProjectionSegment;
+import
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.bounded.ColumnSegmentBoundedInfo;
+import
org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
+import org.junit.jupiter.api.Test;
+
+import java.util.Collections;
+import java.util.Map;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.mockito.Mockito.mock;
+
+class ColumnSegmentBinderTest {
+
+ @Test
+ void assertBindWithMultiTablesJoinAndNoOwner() {
+ Map<String, TableSegmentBinderContext> tableBinderContexts = new
CaseInsensitiveMap<>();
+ ColumnSegment boundedOrderIdColumn = new ColumnSegment(0, 0, new
IdentifierValue("order_id"));
+ boundedOrderIdColumn.setColumnBoundedInfo(new
ColumnSegmentBoundedInfo(new IdentifierValue(DefaultDatabase.LOGIC_NAME), new
IdentifierValue(DefaultDatabase.LOGIC_NAME),
+ new IdentifierValue("t_order"), new
IdentifierValue("order_id")));
+ tableBinderContexts.put("t_order", new
TableSegmentBinderContext(Collections.singleton(new
ColumnProjectionSegment(boundedOrderIdColumn))));
+ ColumnSegment boundedItemIdColumn = new ColumnSegment(0, 0, new
IdentifierValue("item_id"));
+ boundedItemIdColumn.setColumnBoundedInfo(new
ColumnSegmentBoundedInfo(new IdentifierValue(DefaultDatabase.LOGIC_NAME), new
IdentifierValue(DefaultDatabase.LOGIC_NAME),
+ new IdentifierValue("t_order_item"), new
IdentifierValue("item_id")));
+ tableBinderContexts.put("t_order_item", new
TableSegmentBinderContext(Collections.singleton(new
ColumnProjectionSegment(boundedItemIdColumn))));
+ ColumnSegment columnSegment = new ColumnSegment(0, 0, new
IdentifierValue("order_id"));
+ SQLStatementBinderContext statementBinderContext =
+ new
SQLStatementBinderContext(mock(ShardingSphereMetaData.class),
DefaultDatabase.LOGIC_NAME, TypedSPILoader.getService(DatabaseType.class,
"FIXTURE"));
+ ColumnSegment actual = ColumnSegmentBinder.bind(columnSegment,
SegmentType.JOIN_ON, statementBinderContext, tableBinderContexts,
Collections.emptyMap());
+ assertNotNull(actual.getColumnBoundedInfo());
+ assertNull(actual.getOtherUsingColumnBoundedInfo());
+
assertThat(actual.getColumnBoundedInfo().getOriginalDatabase().getValue(),
is(DefaultDatabase.LOGIC_NAME));
+
assertThat(actual.getColumnBoundedInfo().getOriginalSchema().getValue(),
is(DefaultDatabase.LOGIC_NAME));
+
assertThat(actual.getColumnBoundedInfo().getOriginalTable().getValue(),
is("t_order"));
+
assertThat(actual.getColumnBoundedInfo().getOriginalColumn().getValue(),
is("order_id"));
+ }
+}