Skip to content

Commit

Permalink
for #2118, reuse ownerName instead of tableNameOrAlias
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Mar 27, 2019
1 parent bb55b5d commit b50d7f8
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,23 @@
import org.apache.shardingsphere.core.parse.antlr.extractor.OptionalSQLSegmentExtractor;
import org.apache.shardingsphere.core.parse.antlr.extractor.util.ExtractorUtils;
import org.apache.shardingsphere.core.parse.antlr.extractor.util.RuleName;
import org.apache.shardingsphere.core.parse.antlr.sql.segment.table.TableNameOrAliasSegment;
import org.apache.shardingsphere.core.parse.antlr.sql.segment.table.OwnerSegment;
import org.apache.shardingsphere.core.parse.util.SQLUtil;

/**
* Table name or alias extractor.
* Owner name extractor.
*
* @author zhangliang
*/
public final class TableNameOrAliasExtractor implements OptionalSQLSegmentExtractor {
public final class OwnerNameExtractor implements OptionalSQLSegmentExtractor {

@Override
public Optional<TableNameOrAliasSegment> extract(final ParserRuleContext ancestorNode) {
Optional<ParserRuleContext> tableNameOrAliasNode = ExtractorUtils.findFirstChildNode(ancestorNode, RuleName.TABLE_NAME_OR_ALIAS);
if (!tableNameOrAliasNode.isPresent()) {
public Optional<OwnerSegment> extract(final ParserRuleContext ancestorNode) {
Optional<ParserRuleContext> ownerNameNode = ExtractorUtils.findFirstChildNode(ancestorNode, RuleName.OWNER_NAME);
if (!ownerNameNode.isPresent()) {
return Optional.absent();
}
String name = tableNameOrAliasNode.get().getText();
return Optional.of(new TableNameOrAliasSegment(tableNameOrAliasNode.get().getStart().getStartIndex(), SQLUtil.getExactlyValue(name), QuoteCharacter.getQuoteCharacter(name)));
String name = ownerNameNode.get().getText();
return Optional.of(new OwnerSegment(ownerNameNode.get().getStart().getStartIndex(), SQLUtil.getExactlyValue(name), QuoteCharacter.getQuoteCharacter(name)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,27 @@
import org.apache.shardingsphere.core.parse.antlr.extractor.CollectionSQLSegmentExtractor;
import org.apache.shardingsphere.core.parse.antlr.extractor.util.ExtractorUtils;
import org.apache.shardingsphere.core.parse.antlr.extractor.util.RuleName;
import org.apache.shardingsphere.core.parse.antlr.sql.segment.table.TableNameOrAliasSegment;
import org.apache.shardingsphere.core.parse.antlr.sql.segment.table.OwnerSegment;

import java.util.Collection;
import java.util.LinkedList;

/**
* Table names or aliases extractor.
* Owner names extractor.
*
* @author zhangliang
*/
public final class TableNamesOrAliasesExtractor implements CollectionSQLSegmentExtractor {
public final class OwnerNamesExtractor implements CollectionSQLSegmentExtractor {

private final TableNameOrAliasExtractor tableNameOrAliasExtractor = new TableNameOrAliasExtractor();
private final OwnerNameExtractor ownerNameExtractor = new OwnerNameExtractor();

@Override
public Collection<TableNameOrAliasSegment> extract(final ParserRuleContext ancestorNode) {
Collection<TableNameOrAliasSegment> result = new LinkedList<>();
for (ParserRuleContext each : ExtractorUtils.getAllDescendantNodes(ancestorNode, RuleName.TABLE_NAME_OR_ALIAS)) {
Optional<TableNameOrAliasSegment> tableNameOrAliasSegment = tableNameOrAliasExtractor.extract(each);
if (tableNameOrAliasSegment.isPresent()) {
result.add(tableNameOrAliasSegment.get());
public Collection<OwnerSegment> extract(final ParserRuleContext ancestorNode) {
Collection<OwnerSegment> result = new LinkedList<>();
for (ParserRuleContext each : ExtractorUtils.getAllDescendantNodes(ancestorNode, RuleName.OWNER_NAME)) {
Optional<OwnerSegment> ownerSegment = ownerNameExtractor.extract(each);
if (ownerSegment.isPresent()) {
result.add(ownerSegment.get());
}
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public enum RuleName {

TABLE_NAME("TableName"),

TABLE_NAME_OR_ALIAS("TableNameOrAlias"),
OWNER_NAME("OwnerName"),

COLUMN_DEFINITION("ColumnDefinition"),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@

import org.apache.shardingsphere.core.metadata.table.ShardingTableMetaData;
import org.apache.shardingsphere.core.parse.antlr.filler.sharding.SQLSegmentShardingFiller;
import org.apache.shardingsphere.core.parse.antlr.sql.segment.table.TableNameOrAliasSegment;
import org.apache.shardingsphere.core.parse.antlr.sql.segment.table.OwnerSegment;
import org.apache.shardingsphere.core.parse.parser.sql.SQLStatement;
import org.apache.shardingsphere.core.rule.ShardingRule;

/**
* Table name or alias filler.
* Owner filler.
*
* @author zhangliang
*/
public final class TableNameOrAliasFiller implements SQLSegmentShardingFiller<TableNameOrAliasSegment> {
public final class OwnerFiller implements SQLSegmentShardingFiller<OwnerSegment> {

@Override
public void fill(final TableNameOrAliasSegment sqlSegment, final SQLStatement sqlStatement, final String sql, final ShardingRule shardingRule, final ShardingTableMetaData shardingTableMetaData) {
sqlStatement.getTables().getTableNameOrAliasSegment().add(sqlSegment);
public void fill(final OwnerSegment sqlSegment, final SQLStatement sqlStatement, final String sql, final ShardingRule shardingRule, final ShardingTableMetaData shardingTableMetaData) {
sqlStatement.getTables().getOwnerSegments().add(sqlSegment);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
import org.apache.shardingsphere.core.parse.antlr.sql.segment.SQLSegment;

/**
* Table name or alias segment.
* Owner segment.
*
* @author zhangliang
*/
@RequiredArgsConstructor
@Getter
public class TableNameOrAliasSegment implements SQLSegment {
public class OwnerSegment implements SQLSegment {

private final int startIndex;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import com.google.common.base.Preconditions;
import lombok.Getter;
import lombok.ToString;
import org.apache.shardingsphere.core.parse.antlr.sql.segment.table.TableNameOrAliasSegment;
import org.apache.shardingsphere.core.parse.antlr.sql.segment.table.OwnerSegment;

import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -42,7 +42,7 @@ public final class Tables {
private final List<Table> tables = new ArrayList<>();

@Getter
private final Collection<TableNameOrAliasSegment> tableNameOrAliasSegment = new LinkedList<>();
private final Collection<OwnerSegment> ownerSegments = new LinkedList<>();

/**
* Add table.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<extractor-rule-definition>
<extractor-rule id="tableName" extractor-class="org.apache.shardingsphere.core.parse.antlr.extractor.impl.TableNameExtractor" />
<extractor-rule id="tableNames" extractor-class="org.apache.shardingsphere.core.parse.antlr.extractor.impl.TableNamesExtractor" />
<extractor-rule id="tableNamesOrAliases" extractor-class="org.apache.shardingsphere.core.parse.antlr.extractor.impl.TableNamesOrAliasesExtractor" />
<extractor-rule id="tableNamesOrAliases" extractor-class="org.apache.shardingsphere.core.parse.antlr.extractor.impl.OwnerNamesExtractor" />
<extractor-rule id="indexName" extractor-class="org.apache.shardingsphere.core.parse.antlr.extractor.impl.ddl.index.IndexNameExtractor" />
<extractor-rule id="indexNames" extractor-class="org.apache.shardingsphere.core.parse.antlr.extractor.impl.ddl.index.IndexNamesExtractor" />
<extractor-rule id="columnDefinitions" extractor-class="org.apache.shardingsphere.core.parse.antlr.extractor.impl.ddl.column.ColumnDefinitionsExtractor" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

<filler-rule-definition>
<filler-rule sql-segment-class="org.apache.shardingsphere.core.parse.antlr.sql.segment.table.TableSegment" filler-class="org.apache.shardingsphere.core.parse.antlr.filler.sharding.segment.impl.TableFiller" />
<filler-rule sql-segment-class="org.apache.shardingsphere.core.parse.antlr.sql.segment.table.TableNameOrAliasSegment" filler-class="org.apache.shardingsphere.core.parse.antlr.filler.sharding.segment.impl.TableNameOrAliasFiller" />
<filler-rule sql-segment-class="org.apache.shardingsphere.core.parse.antlr.sql.segment.table.OwnerSegment" filler-class="org.apache.shardingsphere.core.parse.antlr.filler.sharding.segment.impl.OwnerFiller" />
<filler-rule sql-segment-class="org.apache.shardingsphere.core.parse.antlr.sql.segment.FromWhereSegment" filler-class="org.apache.shardingsphere.core.parse.antlr.filler.sharding.segment.impl.FromWhereFiller" />
<filler-rule sql-segment-class="org.apache.shardingsphere.core.parse.antlr.sql.segment.condition.SubqueryConditionSegment" filler-class="org.apache.shardingsphere.core.parse.antlr.filler.sharding.segment.impl.dql.SubqueryConditionFiller" />
</filler-rule-definition>
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ tableName
: (schemaName DOT_)? uid
;

tableNameOrAlias
: uid
;

ownerName
: uid
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ selectExprs
;

selectExpr
: (columnName | expr) AS? alias? | tableNameOrAlias DOT_ASTERISK_
: (columnName | expr) AS? alias? | ownerName DOT_ASTERISK_
;

fromClause
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.apache.shardingsphere.core.constant.AggregationType;
import org.apache.shardingsphere.core.metadata.table.ShardingTableMetaData;
import org.apache.shardingsphere.core.parse.antlr.optimizer.SQLStatementOptimizer;
import org.apache.shardingsphere.core.parse.antlr.sql.segment.table.TableNameOrAliasSegment;
import org.apache.shardingsphere.core.parse.antlr.sql.segment.table.OwnerSegment;
import org.apache.shardingsphere.core.parse.parser.constant.DerivedColumn;
import org.apache.shardingsphere.core.parse.parser.context.condition.OrCondition;
import org.apache.shardingsphere.core.parse.parser.context.orderby.OrderItem;
Expand Down Expand Up @@ -52,26 +52,26 @@ public final class MySQLSelectOptimizer implements SQLStatementOptimizer {

@Override
public void optimize(final SQLStatement sqlStatement, final ShardingTableMetaData shardingTableMetaData) {
addTableTokensIfIsNotAlias(sqlStatement);
addTableTokensIfOwnerIsNotAlias(sqlStatement);
appendDerivedColumns((SelectStatement) sqlStatement, shardingTableMetaData);
appendDerivedOrderBy((SelectStatement) sqlStatement);
postExtractInternal(sqlStatement);
}

private void addTableTokensIfIsNotAlias(final SQLStatement sqlStatement) {
private void addTableTokensIfOwnerIsNotAlias(final SQLStatement sqlStatement) {
List<SQLToken> toBeAdded = new LinkedList<>();
final Collection<String> tableNames = sqlStatement.getTables().getTableNames();
for (TableNameOrAliasSegment each : sqlStatement.getTables().getTableNameOrAliasSegment()) {
for (OwnerSegment each : sqlStatement.getTables().getOwnerSegments()) {
if (!isAlias(tableNames, each)) {
toBeAdded.add(new TableToken(each.getStartIndex(), each.getName(), each.getQuoteCharacter(), 0));
}
}
sqlStatement.getSQLTokens().addAll(toBeAdded);
}

private boolean isAlias(final Collection<String> tableNames, final TableNameOrAliasSegment tableNameOrAliasSegment) {
private boolean isAlias(final Collection<String> tableNames, final OwnerSegment ownerSegment) {
for (String each : tableNames) {
if (each.equalsIgnoreCase(tableNameOrAliasSegment.getName())) {
if (each.equalsIgnoreCase(ownerSegment.getName())) {
return false;
}
}
Expand Down

0 comments on commit b50d7f8

Please sign in to comment.