diff --git a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/table/TablesContext.java b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/table/TablesContext.java index 86f146e1c71a4..ac08b4d2269ff 100644 --- a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/table/TablesContext.java +++ b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/table/TablesContext.java @@ -35,6 +35,7 @@ import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment; import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SubqueryTableSegment; import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.TableSegment; +import org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue; import java.util.Collection; import java.util.Collections; @@ -66,6 +67,8 @@ public final class TablesContext { @Getter(AccessLevel.NONE) private final Map> subqueryTables = new HashMap<>(); + private final Map tableAliasNameMap = new HashMap<>(); + public TablesContext(final SimpleTableSegment table, final DatabaseType databaseType, final String currentDatabaseName) { this(null == table ? Collections.emptyList() : Collections.singletonList(table), databaseType, currentDatabaseName); } @@ -81,13 +84,17 @@ public TablesContext(final Collection tables, final Map< } this.tables.addAll(tables); for (TableSegment each : tables) { - if (each instanceof SimpleTableSegment && !"DUAL".equalsIgnoreCase(((SimpleTableSegment) each).getTableName().getIdentifier().getValue())) { - SimpleTableSegment simpleTableSegment = (SimpleTableSegment) each; - simpleTables.add(simpleTableSegment); - tableNames.add(simpleTableSegment.getTableName().getIdentifier().getValue()); - // TODO use sql binder result when statement which contains tables support bind logic - simpleTableSegment.getOwner().ifPresent(optional -> schemaNames.add(optional.getIdentifier().getValue())); - databaseNames.add(findDatabaseName(simpleTableSegment, databaseType).orElse(currentDatabaseName)); + if (each instanceof SimpleTableSegment) { + String tableName = ((SimpleTableSegment) each).getTableName().getIdentifier().getValue(); + if (!"DUAL".equalsIgnoreCase(tableName)) { + SimpleTableSegment simpleTableSegment = (SimpleTableSegment) each; + simpleTables.add(simpleTableSegment); + tableNames.add(tableName); + // TODO use SQL binder result when statement which contains tables support bind logic + simpleTableSegment.getOwner().ifPresent(optional -> schemaNames.add(optional.getIdentifier().getValue())); + databaseNames.add(findDatabaseName(simpleTableSegment, databaseType).orElse(currentDatabaseName)); + tableAliasNameMap.put(each.getAlias().map(IdentifierValue::getValue).orElse(tableName).toLowerCase(), tableName.toLowerCase()); + } } if (each instanceof SubqueryTableSegment) { subqueryTables.putAll(createSubqueryTables(subqueryContexts, (SubqueryTableSegment) each));