Skip to content

Commit

Permalink
Add binding to owner table (#33533)
Browse files Browse the repository at this point in the history
* Add binding to owner table

* Add binding to owner table
  • Loading branch information
FlyingZC authored Nov 4, 2024
1 parent 83101c7 commit 92bc780
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 2 deletions.
1 change: 1 addition & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
1. Kernel: Add index columns not empty judgement for IndexColumnTokenGenerator - [#33384](https://github.com/apache/shardingsphere/pull/33384)
1. SQL Parser: Support parsing Doris STRRIGHT - [#33393](https://github.com/apache/shardingsphere/pull/33393)
1. JDBC: Add show database name for JDBC when execute SHOW COMPUTE NODES - [#33437](https://github.com/apache/shardingsphere/pull/33437)
1. Kernel: Add binding to owner table - [#33533](https://github.com/apache/shardingsphere/pull/33533)

### Bug Fixes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.column.ColumnSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.item.ColumnProjectionSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.item.ProjectionSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.OwnerSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.bound.ColumnSegmentBoundInfo;
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.bound.TableSegmentBoundInfo;
import org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;

import java.util.ArrayList;
Expand Down Expand Up @@ -82,18 +84,27 @@ public static ColumnSegment bind(final ColumnSegment segment, final SegmentType
Collection<TableSegmentBinderContext> tableSegmentBinderContexts = getTableSegmentBinderContexts(segment, parentSegmentType, binderContext, tableBinderContexts, outerTableBinderContexts);
Optional<ColumnSegment> inputColumnSegment = findInputColumnSegment(segment, parentSegmentType, tableSegmentBinderContexts, outerTableBinderContexts, binderContext);
inputColumnSegment.ifPresent(optional -> result.setVariable(optional.isVariable()));
segment.getOwner().ifPresent(optional -> result.setOwner(bindOwnerTableContext(optional, inputColumnSegment.orElse(null))));
result.setColumnBoundInfo(createColumnSegmentBoundInfo(segment, inputColumnSegment.orElse(null)));
return result;
}

private static ColumnSegment copy(final ColumnSegment segment) {
ColumnSegment result = new ColumnSegment(segment.getStartIndex(), segment.getStopIndex(), segment.getIdentifier());
segment.getOwner().ifPresent(result::setOwner);
segment.getLeftParentheses().ifPresent(result::setLeftParentheses);
segment.getRightParentheses().ifPresent(result::setRightParentheses);
return result;
}

private static OwnerSegment bindOwnerTableContext(final OwnerSegment owner, final ColumnSegment inputColumnSegment) {
IdentifierValue originalDatabase = null == inputColumnSegment ? null : inputColumnSegment.getColumnBoundInfo().getOriginalDatabase();
IdentifierValue originalSchema = null == inputColumnSegment ? null : inputColumnSegment.getColumnBoundInfo().getOriginalSchema();
if (originalDatabase != null && originalSchema != null) {
owner.setTableBoundInfo(new TableSegmentBoundInfo(originalDatabase, originalSchema));
}
return owner;
}

private static Collection<TableSegmentBinderContext> getTableSegmentBinderContexts(final ColumnSegment segment, final SegmentType parentSegmentType,
final SQLStatementBinderContext binderContext,
final Multimap<CaseInsensitiveString, TableSegmentBinderContext> tableBinderContexts,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.item.ColumnProjectionSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.OwnerSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.bound.ColumnSegmentBoundInfo;
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.bound.TableSegmentBoundInfo;
import org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
import org.junit.jupiter.api.Test;

Expand All @@ -43,6 +44,7 @@
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;

class ColumnSegmentBinderTest {
Expand Down Expand Up @@ -134,4 +136,33 @@ void assertBindWithSameTableAliasAndDifferentProjection() {
assertThat(actual.getColumnBoundInfo().getOriginalTable().getValue(), is("t_order_item"));
assertThat(actual.getColumnBoundInfo().getOriginalColumn().getValue(), is("status"));
}

@Test
void assertBindOwner() {
Multimap<CaseInsensitiveString, TableSegmentBinderContext> tableBinderContexts = LinkedHashMultimap.create();
ColumnSegment boundOrderIdColumn = new ColumnSegment(0, 0, new IdentifierValue("order_id"));
boundOrderIdColumn.setColumnBoundInfo(new ColumnSegmentBoundInfo(new IdentifierValue(DefaultDatabase.LOGIC_NAME), new IdentifierValue(DefaultDatabase.LOGIC_NAME),
new IdentifierValue("t_order"), new IdentifierValue("order_id")));
tableBinderContexts.put(new CaseInsensitiveString("t_order"), new SimpleTableSegmentBinderContext(Collections.singleton(new ColumnProjectionSegment(boundOrderIdColumn))));
ColumnSegment boundItemIdColumn = new ColumnSegment(0, 0, new IdentifierValue("item_id"));
boundItemIdColumn.setColumnBoundInfo(new ColumnSegmentBoundInfo(new IdentifierValue(DefaultDatabase.LOGIC_NAME), new IdentifierValue(DefaultDatabase.LOGIC_NAME),
new IdentifierValue("t_order_item"), new IdentifierValue("item_id")));
tableBinderContexts.put(new CaseInsensitiveString("t_order_item"), new SimpleTableSegmentBinderContext(Collections.singleton(new ColumnProjectionSegment(boundItemIdColumn))));
ColumnSegment columnSegment = new ColumnSegment(0, 0, new IdentifierValue("order_id"));
columnSegment.setOwner(new OwnerSegment(0, 0, new IdentifierValue("t_order")));
SQLStatementBinderContext binderContext =
new SQLStatementBinderContext(mock(ShardingSphereMetaData.class), DefaultDatabase.LOGIC_NAME, TypedSPILoader.getService(DatabaseType.class, "FIXTURE"), Collections.emptySet());
ColumnSegment actual = ColumnSegmentBinder.bind(columnSegment, SegmentType.JOIN_ON, binderContext, tableBinderContexts, LinkedHashMultimap.create());
assertTrue(actual.getOwner().isPresent());
assertTrue(actual.getOwner().get().getTableBoundInfo().isPresent());
TableSegmentBoundInfo actualTableBoundInfo = actual.getOwner().get().getTableBoundInfo().get();
assertThat(actualTableBoundInfo.getOriginalDatabase().getValue(), is(DefaultDatabase.LOGIC_NAME));
assertThat(actualTableBoundInfo.getOriginalSchema().getValue(), is(DefaultDatabase.LOGIC_NAME));
assertNotNull(actual.getColumnBoundInfo());
assertNull(actual.getOtherUsingColumnBoundInfo());
assertThat(actual.getColumnBoundInfo().getOriginalDatabase().getValue(), is(DefaultDatabase.LOGIC_NAME));
assertThat(actual.getColumnBoundInfo().getOriginalSchema().getValue(), is(DefaultDatabase.LOGIC_NAME));
assertThat(actual.getColumnBoundInfo().getOriginalTable().getValue(), is("t_order"));
assertThat(actual.getColumnBoundInfo().getOriginalColumn().getValue(), is("order_id"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import org.apache.shardingsphere.sql.parser.statement.core.segment.SQLSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.bound.TableSegmentBoundInfo;
import org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;

import java.util.Optional;
Expand All @@ -41,6 +42,8 @@ public final class OwnerSegment implements SQLSegment {

private OwnerSegment owner;

private TableSegmentBoundInfo tableBoundInfo;

/**
* Get owner.
*
Expand All @@ -49,4 +52,13 @@ public final class OwnerSegment implements SQLSegment {
public Optional<OwnerSegment> getOwner() {
return Optional.ofNullable(owner);
}

/**
* Get table bound info.
*
* @return table bound info
*/
public Optional<TableSegmentBoundInfo> getTableBoundInfo() {
return Optional.ofNullable(tableBoundInfo);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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.statement.core.segment.generic.bound;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;

/**
* Table name bound info.
*/
@RequiredArgsConstructor
@Getter
public final class TableSegmentBoundInfo {

private final IdentifierValue originalDatabase;

private final IdentifierValue originalSchema;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import org.apache.shardingsphere.sql.parser.statement.core.segment.SQLSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.bound.TableSegmentBoundInfo;
import org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;

/**
Expand All @@ -36,4 +37,6 @@ public final class TableNameSegment implements SQLSegment {
private final int stopIndex;

private final IdentifierValue identifier;

private TableSegmentBoundInfo tableBoundInfo;
}
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,9 @@ private void extractTablesFromColumnSegments(final Collection<ColumnSegment> col
for (ColumnSegment each : columnSegments) {
if (each.getOwner().isPresent() && needRewrite(each.getOwner().get())) {
OwnerSegment ownerSegment = each.getOwner().get();
rewriteTables.add(new SimpleTableSegment(new TableNameSegment(ownerSegment.getStartIndex(), ownerSegment.getStopIndex(), ownerSegment.getIdentifier())));
TableNameSegment tableSegment = new TableNameSegment(ownerSegment.getStartIndex(), ownerSegment.getStopIndex(), ownerSegment.getIdentifier());
ownerSegment.getTableBoundInfo().ifPresent(tableSegment::setTableBoundInfo);
rewriteTables.add(new SimpleTableSegment(tableSegment));
}
}
}
Expand Down

0 comments on commit 92bc780

Please sign in to comment.