-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support select with statement sql bind and add bind test case
- Loading branch information
1 parent
5a96091
commit ff550f8
Showing
10 changed files
with
414 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
...e/shardingsphere/infra/binder/engine/segment/with/CommonTableExpressionSegmentBinder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* 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.engine.segment.with; | ||
|
||
import com.cedarsoftware.util.CaseInsensitiveMap.CaseInsensitiveString; | ||
import com.google.common.collect.LinkedHashMultimap; | ||
import lombok.AccessLevel; | ||
import lombok.NoArgsConstructor; | ||
import org.apache.shardingsphere.infra.binder.engine.segment.from.context.type.SimpleTableSegmentBinderContext; | ||
import org.apache.shardingsphere.infra.binder.engine.segment.from.type.SubqueryTableSegmentBinder; | ||
import org.apache.shardingsphere.infra.binder.engine.statement.SQLStatementBinderContext; | ||
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.complex.CommonTableExpressionSegment; | ||
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.item.ColumnProjectionSegment; | ||
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SubqueryTableSegment; | ||
|
||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Common table expression segment binder. | ||
*/ | ||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public final class CommonTableExpressionSegmentBinder { | ||
|
||
/** | ||
* Bind common table expression segment. | ||
* | ||
* @param segment common table expression segment | ||
* @param binderContext SQL statement binder context | ||
* @param recursive recursive | ||
* @return bound common table expression segment | ||
*/ | ||
public static CommonTableExpressionSegment bind(final CommonTableExpressionSegment segment, final SQLStatementBinderContext binderContext, final boolean recursive) { | ||
if (recursive && segment.getAliasName().isPresent()) { | ||
binderContext.getExternalTableBinderContexts().put(new CaseInsensitiveString(segment.getAliasName().get()), | ||
new SimpleTableSegmentBinderContext(segment.getColumns().stream().map(ColumnProjectionSegment::new).collect(Collectors.toList()))); | ||
} | ||
SubqueryTableSegment subqueryTableSegment = new SubqueryTableSegment(segment.getStartIndex(), segment.getStopIndex(), segment.getSubquery()); | ||
subqueryTableSegment.setAlias(segment.getAliasSegment()); | ||
SubqueryTableSegment boundSubquerySegment = | ||
SubqueryTableSegmentBinder.bind(subqueryTableSegment, binderContext, LinkedHashMultimap.create(), binderContext.getExternalTableBinderContexts()); | ||
CommonTableExpressionSegment result = new CommonTableExpressionSegment( | ||
segment.getStartIndex(), segment.getStopIndex(), boundSubquerySegment.getAliasSegment().orElse(null), boundSubquerySegment.getSubquery()); | ||
// TODO bind with columns | ||
result.getColumns().addAll(segment.getColumns()); | ||
return result; | ||
} | ||
} |
125 changes: 125 additions & 0 deletions
125
...in/java/org/apache/shardingsphere/infra/binder/engine/segment/with/WithSegmentBinder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
* 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.engine.segment.with; | ||
|
||
import com.cedarsoftware.util.CaseInsensitiveMap; | ||
import com.cedarsoftware.util.CaseInsensitiveMap.CaseInsensitiveString; | ||
import com.google.common.base.Strings; | ||
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.segment.from.context.type.SimpleTableSegmentBinderContext; | ||
import org.apache.shardingsphere.infra.binder.engine.statement.SQLStatementBinderContext; | ||
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.column.ColumnSegment; | ||
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.complex.CommonTableExpressionSegment; | ||
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.dml.item.ShorthandProjectionSegment; | ||
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.WithSegment; | ||
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.Collection; | ||
import java.util.LinkedList; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
/** | ||
* With segment binder. | ||
*/ | ||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public final class WithSegmentBinder { | ||
|
||
/** | ||
* Bind with segment. | ||
* | ||
* @param segment with segment | ||
* @param binderContext SQL statement binder context | ||
* @param externalTableBinderContexts external table binder contexts | ||
* @return bound with segment | ||
*/ | ||
public static WithSegment bind(final WithSegment segment, final SQLStatementBinderContext binderContext, | ||
final Multimap<CaseInsensitiveString, TableSegmentBinderContext> externalTableBinderContexts) { | ||
Collection<CommonTableExpressionSegment> boundCommonTableExpressions = new LinkedList<>(); | ||
for (CommonTableExpressionSegment each : segment.getCommonTableExpressions()) { | ||
CommonTableExpressionSegment boundCommonTableExpression = CommonTableExpressionSegmentBinder.bind(each, binderContext, segment.isRecursive()); | ||
boundCommonTableExpressions.add(boundCommonTableExpression); | ||
if (segment.isRecursive() && each.getAliasName().isPresent()) { | ||
externalTableBinderContexts.removeAll(new CaseInsensitiveString(each.getAliasName().get())); | ||
} | ||
bindWithColumns(each.getColumns(), boundCommonTableExpression); | ||
each.getAliasName().ifPresent(optional -> externalTableBinderContexts.put(new CaseInsensitiveString(optional), createWithTableBinderContext(boundCommonTableExpression))); | ||
} | ||
return new WithSegment(segment.getStartIndex(), segment.getStopIndex(), boundCommonTableExpressions); | ||
} | ||
|
||
private static SimpleTableSegmentBinderContext createWithTableBinderContext(final CommonTableExpressionSegment commonTableExpressionSegment) { | ||
return new SimpleTableSegmentBinderContext(commonTableExpressionSegment.getSubquery().getSelect().getProjections().getProjections()); | ||
} | ||
|
||
private static void bindWithColumns(final Collection<ColumnSegment> columns, final CommonTableExpressionSegment boundCommonTableExpression) { | ||
if (columns.isEmpty()) { | ||
return; | ||
} | ||
Map<String, ColumnProjectionSegment> columnProjections = extractWithSubqueryColumnProjections(boundCommonTableExpression); | ||
columns.forEach(each -> { | ||
ColumnProjectionSegment projectionSegment = columnProjections.get(each.getIdentifier().getValue()); | ||
if (null != projectionSegment) { | ||
each.setColumnBoundInfo(createColumnSegmentBoundInfo(each, projectionSegment.getColumn())); | ||
} | ||
}); | ||
} | ||
|
||
private static Map<String, ColumnProjectionSegment> extractWithSubqueryColumnProjections(final CommonTableExpressionSegment boundCommonTableExpression) { | ||
Map<String, ColumnProjectionSegment> result = new CaseInsensitiveMap<>(); | ||
Collection<ProjectionSegment> projections = boundCommonTableExpression.getSubquery().getSelect().getProjections().getProjections(); | ||
projections.forEach(each -> extractWithSubqueryColumnProjections(each, result)); | ||
return result; | ||
} | ||
|
||
private static void extractWithSubqueryColumnProjections(final ProjectionSegment projectionSegment, final Map<String, ColumnProjectionSegment> result) { | ||
if (projectionSegment instanceof ColumnProjectionSegment) { | ||
result.put(getColumnName((ColumnProjectionSegment) projectionSegment), (ColumnProjectionSegment) projectionSegment); | ||
} | ||
if (projectionSegment instanceof ShorthandProjectionSegment) { | ||
((ShorthandProjectionSegment) projectionSegment).getActualProjectionSegments().forEach(eachProjection -> { | ||
if (eachProjection instanceof ColumnProjectionSegment) { | ||
result.put(getColumnName((ColumnProjectionSegment) eachProjection), (ColumnProjectionSegment) eachProjection); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
private static String getColumnName(final ColumnProjectionSegment columnProjection) { | ||
return columnProjection.getAliasName().orElse(columnProjection.getColumn().getIdentifier().getValue()); | ||
} | ||
|
||
private static ColumnSegmentBoundInfo createColumnSegmentBoundInfo(final ColumnSegment segment, final ColumnSegment inputColumnSegment) { | ||
IdentifierValue originalDatabase = null == inputColumnSegment ? null : inputColumnSegment.getColumnBoundInfo().getOriginalDatabase(); | ||
IdentifierValue originalSchema = null == inputColumnSegment ? null : inputColumnSegment.getColumnBoundInfo().getOriginalSchema(); | ||
IdentifierValue segmentOriginalTable = segment.getColumnBoundInfo().getOriginalTable(); | ||
IdentifierValue originalTable = Strings.isNullOrEmpty(segmentOriginalTable.getValue()) | ||
? Optional.ofNullable(inputColumnSegment).map(optional -> optional.getColumnBoundInfo().getOriginalTable()).orElse(segmentOriginalTable) | ||
: segmentOriginalTable; | ||
IdentifierValue segmentOriginalColumn = segment.getColumnBoundInfo().getOriginalColumn(); | ||
IdentifierValue originalColumn = Optional.ofNullable(inputColumnSegment).map(optional -> optional.getColumnBoundInfo().getOriginalColumn()).orElse(segmentOriginalColumn); | ||
return new ColumnSegmentBoundInfo(new TableSegmentBoundInfo(originalDatabase, originalSchema), originalTable, originalColumn); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.