Skip to content

Commit

Permalink
Fix the combine statement cannot find the outer table when bind
Browse files Browse the repository at this point in the history
  • Loading branch information
FlyingZC committed Oct 23, 2024
1 parent f323f7b commit 8f939b1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@

package org.apache.shardingsphere.infra.binder.engine.segment.combine;

import com.cedarsoftware.util.CaseInsensitiveMap;
import com.google.common.collect.Multimap;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.infra.binder.engine.segment.from.context.TableSegmentBinderContext;
import org.apache.shardingsphere.infra.binder.engine.statement.SQLStatementBinderContext;
import org.apache.shardingsphere.infra.binder.engine.statement.dml.SelectStatementBinder;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.combine.CombineSegment;
Expand All @@ -35,19 +38,23 @@ public final class CombineSegmentBinder {
*
* @param segment table segment
* @param binderContext SQL statement binder context
* @param outerTableBinderContexts outer table binder contexts
* @return bound combine segment
*/
public static CombineSegment bind(final CombineSegment segment, final SQLStatementBinderContext binderContext) {
public static CombineSegment bind(final CombineSegment segment, final SQLStatementBinderContext binderContext,
final Multimap<CaseInsensitiveMap.CaseInsensitiveString, TableSegmentBinderContext> outerTableBinderContexts) {
return new CombineSegment(segment.getStartIndex(), segment.getStopIndex(),
bindSubquerySegment(segment.getLeft(), binderContext), segment.getCombineType(), bindSubquerySegment(segment.getRight(), binderContext));
bindSubquerySegment(segment.getLeft(), binderContext, outerTableBinderContexts), segment.getCombineType(),
bindSubquerySegment(segment.getRight(), binderContext, outerTableBinderContexts));
}

private static SubquerySegment bindSubquerySegment(final SubquerySegment segment, final SQLStatementBinderContext binderContext) {
private static SubquerySegment bindSubquerySegment(final SubquerySegment segment, final SQLStatementBinderContext binderContext,
final Multimap<CaseInsensitiveMap.CaseInsensitiveString, TableSegmentBinderContext> outerTableBinderContexts) {
SubquerySegment result = new SubquerySegment(segment.getStartIndex(), segment.getStopIndex(), segment.getText());
result.setSubqueryType(segment.getSubqueryType());
SQLStatementBinderContext subqueryBinderContext = new SQLStatementBinderContext(segment.getSelect(), binderContext.getMetaData(), binderContext.getCurrentDatabaseName());
subqueryBinderContext.getExternalTableBinderContexts().putAll(binderContext.getExternalTableBinderContexts());
result.setSelect(new SelectStatementBinder().bind(segment.getSelect(), subqueryBinderContext));
result.setSelect(new SelectStatementBinder(outerTableBinderContexts).bind(segment.getSelect(), subqueryBinderContext));
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public SelectStatement bind(final SelectStatement sqlStatement, final SQLStateme
boundTableSegment.ifPresent(result::setFrom);
result.setProjections(ProjectionsSegmentBinder.bind(sqlStatement.getProjections(), binderContext, boundTableSegment.orElse(null), tableBinderContexts, outerTableBinderContexts));
sqlStatement.getWhere().ifPresent(optional -> result.setWhere(WhereSegmentBinder.bind(optional, binderContext, tableBinderContexts, outerTableBinderContexts)));
sqlStatement.getCombine().ifPresent(optional -> result.setCombine(CombineSegmentBinder.bind(optional, binderContext)));
sqlStatement.getCombine().ifPresent(optional -> result.setCombine(CombineSegmentBinder.bind(optional, binderContext, outerTableBinderContexts)));
sqlStatement.getLock().ifPresent(optional -> result.setLock(LockSegmentBinder.bind(optional, binderContext, tableBinderContexts, outerTableBinderContexts)));
// TODO support other segment bind in select statement
return result;
Expand Down

0 comments on commit 8f939b1

Please sign in to comment.