Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TablesContext.tableAliasNameMap #33562

Merged
merged 2 commits into from
Nov 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -66,6 +67,8 @@ public final class TablesContext {
@Getter(AccessLevel.NONE)
private final Map<String, Collection<SubqueryTableContext>> subqueryTables = new HashMap<>();

private final Map<String, String> tableAliasNameMap = new HashMap<>();

public TablesContext(final SimpleTableSegment table, final DatabaseType databaseType, final String currentDatabaseName) {
this(null == table ? Collections.emptyList() : Collections.singletonList(table), databaseType, currentDatabaseName);
}
Expand All @@ -81,13 +84,17 @@ public TablesContext(final Collection<? extends TableSegment> 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));
Expand Down