Skip to content
Merged
Show file tree
Hide file tree
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 @@ -23,6 +23,7 @@
import org.apache.shardingsphere.database.connector.core.spi.DatabaseTypedSPILoader;
import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
import org.apache.shardingsphere.database.connector.core.type.DatabaseTypeRegistry;
import org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation;
import org.apache.shardingsphere.infra.binder.context.segment.select.orderby.OrderByItem;
import org.apache.shardingsphere.infra.binder.context.segment.select.pagination.PaginationContext;
import org.apache.shardingsphere.infra.binder.context.segment.select.projection.Projection;
Expand Down Expand Up @@ -84,6 +85,7 @@ private Map<String, Integer> getColumnLabelIndexMap(final QueryResult queryResul
return result;
}

@HighFrequencyInvocation
private String getColumnLabel(final QueryResultMetaData metaData, final int columnIndex, final List<Projection> expandProjections) throws SQLException {
String result = SQLUtils.getExactlyValue(metaData.getColumnLabel(columnIndex));
if (!result.isEmpty() || !"SQLServer".equals(protocolType.getType())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.shardingsphere.database.connector.core.metadata.database.enums.NullsOrderType;
import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
import org.apache.shardingsphere.database.connector.core.type.DatabaseTypeRegistry;
import org.apache.shardingsphere.infra.binder.context.segment.select.projection.impl.AggregationProjection;
import org.apache.shardingsphere.infra.binder.context.statement.type.dml.SelectStatementContext;
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.executor.sql.execute.result.query.QueryResult;
Expand Down Expand Up @@ -53,6 +54,7 @@
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.item.ShorthandProjectionSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.order.GroupBySegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.order.OrderBySegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.order.item.ColumnOrderByItemSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.order.item.IndexOrderByItemSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.pagination.limit.LimitSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.pagination.limit.NumberLiteralLimitValueSegment;
Expand All @@ -70,7 +72,9 @@
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.MockedConstruction;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -640,6 +644,88 @@ void assertBuildGroupByMemoryMergedResultWithSQLServerEmptyColumnLabel() throws
assertThat(actual, isA(GroupByMemoryMergedResult.class));
}

@Test
void assertMergeWithSQLServerShorthandEmptyColumnLabel() throws SQLException {
ShorthandProjectionSegment shorthandProjection = new ShorthandProjectionSegment(0, 0);
GroupBySegment groupBy = new GroupBySegment(0, 0, new ArrayList<>(3));
for (String each : Arrays.asList("col1", "col2", "col3")) {
ColumnSegment column = new ColumnSegment(0, 0, new IdentifierValue(each));
shorthandProjection.getActualProjectionSegments().add(new ColumnProjectionSegment(column));
groupBy.getGroupByItems().add(new ColumnOrderByItemSegment(column, OrderDirection.ASC, NullsOrderType.FIRST));
}
ProjectionsSegment projections = new ProjectionsSegment(0, 0);
projections.getProjections().add(shorthandProjection);
projections.getProjections().add(new AggregationProjectionSegment(0, 0, AggregationType.COUNT, "COUNT(1)"));
SelectStatement selectStatement = withGroupBy(withProjections(buildSelectStatement(sqlserverDatabaseType), projections), groupBy);
SelectStatementContext selectStatementContext = new SelectStatementContext(selectStatement, createShardingSphereMetaData(mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS)), "foo_db",
Collections.emptyList());
String[] columnLabels = {"col1", "col2", "col3", ""};
List<QueryResult> queryResults = Arrays.asList(createSingleRowQueryResult(columnLabels, 1, 2, 3, 2), createSingleRowQueryResult(columnLabels, 1, 2, 3, 5));
MergedResult actual = new ShardingDQLResultMerger(sqlserverDatabaseType).merge(queryResults, selectStatementContext, createSQLServerDatabase(), mock(ConnectionContext.class));
assertThat(selectStatementContext.getProjectionsContext().getAggregationProjections().get(0).getIndex(), is(4));
assertTrue(actual.next());
assertThat(actual.getValue(1, Object.class), is(1));
assertThat(actual.getValue(2, Object.class), is(2));
assertThat(actual.getValue(3, Object.class), is(3));
assertThat(actual.getValue(4, Object.class), is(new BigDecimal("7")));
assertFalse(actual.next());
}

private QueryResult createSingleRowQueryResult(final String[] columnLabels, final Object... values) throws SQLException {
QueryResult result = mock(QueryResult.class, RETURNS_DEEP_STUBS);
when(result.getMetaData().getColumnCount()).thenReturn(columnLabels.length);
for (int columnIndex = 1; columnIndex <= columnLabels.length; columnIndex++) {
when(result.getMetaData().getColumnLabel(columnIndex)).thenReturn(columnLabels[columnIndex - 1]);
when(result.getMetaData().getColumnName(columnIndex)).thenReturn(columnLabels[columnIndex - 1]);
when(result.getValue(columnIndex, Object.class)).thenReturn(values[columnIndex - 1]);
}
when(result.next()).thenReturn(true, false);
return result;
}

@Test
void assertMergeWithSQLServerAverageEmptyColumnLabels() throws SQLException {
ProjectionsSegment projections = new ProjectionsSegment(0, 0);
projections.getProjections().add(new AggregationProjectionSegment(0, 0, AggregationType.AVG, "AVG(col1)"));
projections.getProjections().add(new AggregationProjectionSegment(0, 0, AggregationType.COUNT, "COUNT(1)"));
SelectStatement selectStatement = withProjections(buildSelectStatement(sqlserverDatabaseType), projections);
SelectStatementContext selectStatementContext = new SelectStatementContext(selectStatement, createShardingSphereMetaData(mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS)), "foo_db",
Collections.emptyList());
String[] columnLabels = {"", "", "AVG_DERIVED_COUNT_0", "AVG_DERIVED_SUM_0"};
List<QueryResult> queryResults = Arrays.asList(createSingleRowQueryResult(columnLabels, 10, 2, 2, 20), createSingleRowQueryResult(columnLabels, 30, 6, 6, 180));
MergedResult actual = new ShardingDQLResultMerger(sqlserverDatabaseType).merge(queryResults, selectStatementContext, createSQLServerDatabase(), mock(ConnectionContext.class));
AggregationProjection actualAverageProjection = (AggregationProjection) selectStatementContext.getProjectionsContext().getExpandProjections().get(0);
assertThat(actualAverageProjection.getIndex(), is(1));
assertThat(actualAverageProjection.getDerivedAggregationProjections().get(0).getIndex(), is(3));
assertThat(actualAverageProjection.getDerivedAggregationProjections().get(1).getIndex(), is(4));
assertThat(((AggregationProjection) selectStatementContext.getProjectionsContext().getExpandProjections().get(1)).getIndex(), is(2));
assertTrue(actual.next());
assertThat(actual.getValue(1, Object.class), is(new BigDecimal("25.0000")));
assertThat(actual.getValue(2, Object.class), is(new BigDecimal("8")));
assertThat(actual.getValue(3, Object.class), is(new BigDecimal("8")));
assertThat(actual.getValue(4, Object.class), is(new BigDecimal("200")));
assertFalse(actual.next());
}

@Test
void assertMergeWithSQLServerMultipleAggregationEmptyColumnLabels() throws SQLException {
ProjectionsSegment projections = new ProjectionsSegment(0, 0);
projections.getProjections().add(new AggregationProjectionSegment(0, 0, AggregationType.COUNT, "COUNT(1)"));
projections.getProjections().add(new AggregationProjectionSegment(0, 0, AggregationType.SUM, "SUM(col1)"));
SelectStatement selectStatement = withProjections(buildSelectStatement(sqlserverDatabaseType), projections);
SelectStatementContext selectStatementContext = new SelectStatementContext(selectStatement, createShardingSphereMetaData(mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS)), "foo_db",
Collections.emptyList());
String[] columnLabels = {"", ""};
List<QueryResult> queryResults = Arrays.asList(createSingleRowQueryResult(columnLabels, 2, 20), createSingleRowQueryResult(columnLabels, 3, 90));
MergedResult actual = new ShardingDQLResultMerger(sqlserverDatabaseType).merge(queryResults, selectStatementContext, createSQLServerDatabase(), mock(ConnectionContext.class));
assertThat(selectStatementContext.getProjectionsContext().getAggregationProjections().get(0).getIndex(), is(1));
assertThat(selectStatementContext.getProjectionsContext().getAggregationProjections().get(1).getIndex(), is(2));
assertTrue(actual.next());
assertThat(actual.getValue(1, Object.class), is(new BigDecimal("5")));
assertThat(actual.getValue(2, Object.class), is(new BigDecimal("110")));
assertFalse(actual.next());
}

private ShardingSphereMetaData createShardingSphereMetaData(final ShardingSphereDatabase database) {
when(database.getName()).thenReturn("foo_db");
return new ShardingSphereMetaData(Collections.singleton(database), mock(), mock(), mock());
Expand Down
Loading