Skip to content

Commit

Permalink
Add ShardingSphereMetaDataIdentifier and try to use in ShardingSphere…
Browse files Browse the repository at this point in the history
…Table.columns (#33776)

* Add ShardingSphereMetaDataIdentifier and try to use in ShardingSphereTable.columns

* Add ShardingSphereMetaDataIdentifier and try to use in ShardingSphereTable.columns
  • Loading branch information
terrymanu authored Nov 24, 2024
1 parent 30f63c9 commit 6baa244
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
package org.apache.shardingsphere.infra.metadata.database.schema.model;

import com.cedarsoftware.util.CaseInsensitiveMap;
import lombok.AccessLevel;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;
import org.apache.shardingsphere.infra.database.core.metadata.database.enums.TableType;
import org.apache.shardingsphere.infra.metadata.identifier.ShardingSphereMetaDataIdentifier;

import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -39,7 +41,8 @@ public final class ShardingSphereTable {

private final String name;

private final Map<String, ShardingSphereColumn> columns;
@Getter(AccessLevel.NONE)
private final Map<ShardingSphereMetaDataIdentifier, ShardingSphereColumn> columns;

private final Map<String, ShardingSphereIndex> indexes;

Expand Down Expand Up @@ -77,11 +80,11 @@ public ShardingSphereTable(final String name, final Collection<ShardingSphereCol
this.type = type;
}

private Map<String, ShardingSphereColumn> createColumns(final Collection<ShardingSphereColumn> columns) {
Map<String, ShardingSphereColumn> result = new CaseInsensitiveMap<>(columns.size(), 1F);
private Map<ShardingSphereMetaDataIdentifier, ShardingSphereColumn> createColumns(final Collection<ShardingSphereColumn> columns) {
Map<ShardingSphereMetaDataIdentifier, ShardingSphereColumn> result = new CaseInsensitiveMap<>(columns.size(), 1F);
int index = 0;
for (ShardingSphereColumn each : columns) {
result.put(each.getName(), each);
result.put(new ShardingSphereMetaDataIdentifier(each.getName()), each);
columnNames.add(each.getName());
if (each.isPrimaryKey()) {
primaryKeyColumns.add(each.getName());
Expand Down Expand Up @@ -116,7 +119,7 @@ private Map<String, ShardingSphereConstraint> createConstraints(final Collection
* @param column column meta data
*/
public void putColumn(final ShardingSphereColumn column) {
columns.put(column.getName(), column);
columns.put(new ShardingSphereMetaDataIdentifier(column.getName()), column);
}

/**
Expand All @@ -126,13 +129,13 @@ public void putColumn(final ShardingSphereColumn column) {
* @return column meta data
*/
public ShardingSphereColumn getColumn(final String columnName) {
return columns.get(columnName);
return columns.get(new ShardingSphereMetaDataIdentifier(columnName));
}

/**
* Get column meta data collection.
* Get column meta data list.
*
* @return column meta data collection
* @return column meta data list
*/
public Collection<ShardingSphereColumn> getColumnValues() {
return columns.values();
Expand All @@ -145,7 +148,7 @@ public Collection<ShardingSphereColumn> getColumnValues() {
* @return whether contains column or not
*/
public boolean containsColumn(final String columnName) {
return null != columnName && columns.containsKey(columnName);
return null != columnName && columns.containsKey(new ShardingSphereMetaDataIdentifier(columnName));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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.metadata.identifier;

import org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPI;
import org.apache.shardingsphere.infra.spi.annotation.SingletonSPI;

/**
* Database dialect meta data identifier handler.
*/
@SingletonSPI
public interface DatabaseDialectMetaDataIdentifierHandler extends DatabaseTypedSPI {

/**
* Whether identifier is case-sensitive.
*
* @return is case-sensitive or insensitive
*/
boolean isCaseSensitive();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* 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.metadata.identifier;

import com.cedarsoftware.util.CaseInsensitiveMap.CaseInsensitiveString;
import org.apache.shardingsphere.infra.database.core.metadata.database.enums.QuoteCharacter;
import org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPILoader;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;

/**
* ShardingSphere meta data identifier.
*/
public final class ShardingSphereMetaDataIdentifier {

private final boolean isCaseSensitive;

private final CaseInsensitiveString value;

public ShardingSphereMetaDataIdentifier(final String value) {
isCaseSensitive = false;
this.value = new CaseInsensitiveString(value);
}

public ShardingSphereMetaDataIdentifier(final String value, final DatabaseType databaseType) {
isCaseSensitive = DatabaseTypedSPILoader.findService(DatabaseDialectMetaDataIdentifierHandler.class, databaseType)
.map(DatabaseDialectMetaDataIdentifierHandler::isCaseSensitive).orElse(false);
this.value = new CaseInsensitiveString(value);
}

public ShardingSphereMetaDataIdentifier(final IdentifierValue value) {
isCaseSensitive = QuoteCharacter.NONE != value.getQuoteCharacter();
this.value = new CaseInsensitiveString(value.getValue());
}

public ShardingSphereMetaDataIdentifier(final IdentifierValue value, final DatabaseType databaseType) {
isCaseSensitive = QuoteCharacter.NONE != value.getQuoteCharacter()
&& DatabaseTypedSPILoader.findService(DatabaseDialectMetaDataIdentifierHandler.class, databaseType).map(DatabaseDialectMetaDataIdentifierHandler::isCaseSensitive).orElse(false);
this.value = new CaseInsensitiveString(value.getValue());
}

/**
* Get identifier value.
*
* @return identifier value
*/
public String getValue() {
return value.toString();
}

@Override
public boolean equals(final Object obj) {
if (!(obj instanceof ShardingSphereMetaDataIdentifier)) {
return false;
}
return isCaseSensitive ? value.toString().equals(((ShardingSphereMetaDataIdentifier) obj).value.toString()) : value.equals(((ShardingSphereMetaDataIdentifier) obj).value);
}

@Override
public int hashCode() {
return isCaseSensitive ? value.toString().hashCode() : value.hashCode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ private Enumerable<Object> createMemoryEnumerator(final ShardingSphereTableData

@Override
public Enumerator<Object> enumerator() {
return new MemoryRowEnumerator(tableData.getRows(), table.getColumns().values(), databaseType);
return new MemoryRowEnumerator(tableData.getRows(), table.getColumnValues(), databaseType);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void assertExecuteWithStatistics() {
when(schemaData.getTable("test")).thenReturn(tableData);
ShardingSphereTable table = mock(ShardingSphereTable.class, RETURNS_DEEP_STUBS);
when(table.getName()).thenReturn("test");
when(table.getColumns().values()).thenReturn(Collections.singleton(new ShardingSphereColumn("id", Types.INTEGER, true, false, false, false, true, false)));
when(table.getColumnValues()).thenReturn(Collections.singleton(new ShardingSphereColumn("id", Types.INTEGER, true, false, false, false, true, false)));
Enumerable<Object> enumerable = new EnumerableScanExecutor(null, null, null, optimizerContext, executorContext, null, null, statistics)
.execute(table, mock(ScanExecutorContext.class));
try (Enumerator<Object> actual = enumerable.enumerator()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ private Collection<MySQLPacket> createProjectionColumnDefinition41Packets(final
// TODO Calculate column definition flag for other projection types
if (each instanceof ColumnProjection) {
result.add(Optional.ofNullable(schema.getTable(((ColumnProjection) each).getOriginalTable().getValue()))
.map(table -> table.getColumns().get(((ColumnProjection) each).getOriginalColumn().getValue()))
.map(table -> table.getColumn(((ColumnProjection) each).getOriginalColumn().getValue()))
.map(column -> createMySQLColumnDefinition41Packet(characterSet, calculateColumnDefinitionFlag(column), MySQLBinaryColumnType.valueOfJDBCType(column.getDataType())))
.orElseGet(() -> createMySQLColumnDefinition41Packet(characterSet, 0, MySQLBinaryColumnType.VAR_STRING)));
} else {
Expand Down

0 comments on commit 6baa244

Please sign in to comment.