|
56 | 56 | import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.AliasSegment; |
57 | 57 | import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.OwnerSegment; |
58 | 58 | import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.PivotSegment; |
| 59 | +import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.bound.ColumnSegmentBoundInfo; |
59 | 60 | import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.bound.TableSegmentBoundInfo; |
60 | 61 | import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.JoinTableSegment; |
61 | 62 | import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment; |
@@ -191,6 +192,77 @@ void assertIsSameGroupByAndOrderByItemsWithColumnOrderByAndDifferentOrderDirecti |
191 | 192 | SelectStatementContext selectStatementContext = createSelectStatementContext(selectStatement); |
192 | 193 | assertTrue(selectStatementContext.isSameGroupByAndOrderByItems()); |
193 | 194 | } |
| 195 | + |
| 196 | + @Test |
| 197 | + void assertIsSameGroupByAndOrderByItemsWithGeneratedOrderBy() { |
| 198 | + SelectStatement selectStatement = SelectStatement.builder().databaseType(databaseType).projections(new ProjectionsSegment(0, 0)) |
| 199 | + .groupBy(new GroupBySegment(0, 0, Collections.singletonList( |
| 200 | + new ExpressionOrderByItemSegment(0, 0, "LOWER(id)", OrderDirection.ASC, NullsOrderType.LAST)))) |
| 201 | + .build(); |
| 202 | + SelectStatementContext selectStatementContext = createSelectStatementContext(selectStatement); |
| 203 | + assertTrue(selectStatementContext.isSameGroupByAndOrderByItems()); |
| 204 | + } |
| 205 | + |
| 206 | + @Test |
| 207 | + void assertIsNotSameGroupByAndOrderByItemsWhenResolvedExpressionIndexesCollide() { |
| 208 | + SelectStatement selectStatement = SelectStatement.builder().databaseType(databaseType).projections(new ProjectionsSegment(0, 0)) |
| 209 | + .groupBy(new GroupBySegment(0, 0, Collections.singletonList( |
| 210 | + new ExpressionOrderByItemSegment(0, 0, "CONCAT('a,b', name, 'c')", OrderDirection.ASC, NullsOrderType.LAST)))) |
| 211 | + .orderBy(new OrderBySegment(0, 0, Collections.singletonList( |
| 212 | + new ExpressionOrderByItemSegment(0, 0, "CONCAT('a', 'b,name', 'c')", OrderDirection.DESC, NullsOrderType.LAST)))) |
| 213 | + .build(); |
| 214 | + SelectStatementContext selectStatementContext = createSelectStatementContext(selectStatement); |
| 215 | + selectStatementContext.getGroupByContext().getItems().iterator().next().setIndex(1); |
| 216 | + selectStatementContext.getOrderByContext().getItems().iterator().next().setIndex(1); |
| 217 | + assertFalse(selectStatementContext.isSameGroupByAndOrderByItems()); |
| 218 | + } |
| 219 | + |
| 220 | + @Test |
| 221 | + void assertIsNotSameGroupByAndOrderByItemsWhenExpressionsContainDifferentParameterMarkers() { |
| 222 | + SelectStatement selectStatement = SelectStatement.builder().databaseType(databaseType).projections(new ProjectionsSegment(0, 0)) |
| 223 | + .groupBy(new GroupBySegment(0, 0, Collections.singletonList(new ExpressionOrderByItemSegment( |
| 224 | + 0, 0, "MOD(id, ?)", OrderDirection.ASC, NullsOrderType.LAST, new ParameterMarkerExpressionSegment(0, 0, 0))))) |
| 225 | + .orderBy(new OrderBySegment(0, 0, Collections.singletonList(new ExpressionOrderByItemSegment( |
| 226 | + 0, 0, "MOD(id, ?)", OrderDirection.DESC, NullsOrderType.LAST, new ParameterMarkerExpressionSegment(0, 0, 1))))) |
| 227 | + .build(); |
| 228 | + SelectStatementContext selectStatementContext = createSelectStatementContext(selectStatement); |
| 229 | + assertFalse(selectStatementContext.isSameGroupByAndOrderByItems()); |
| 230 | + } |
| 231 | + |
| 232 | + @Test |
| 233 | + void assertIsNotSameGroupByAndOrderByItemsWhenQuotedColumnCaseDiffers() { |
| 234 | + SelectStatement selectStatement = SelectStatement.builder().databaseType(databaseType).projections(new ProjectionsSegment(0, 0)) |
| 235 | + .groupBy(new GroupBySegment(0, 0, Collections.singletonList( |
| 236 | + new ColumnOrderByItemSegment(new ColumnSegment(0, 0, new IdentifierValue("\"Foo\"")), OrderDirection.ASC, NullsOrderType.LAST)))) |
| 237 | + .orderBy(new OrderBySegment(0, 0, Collections.singletonList( |
| 238 | + new ColumnOrderByItemSegment(new ColumnSegment(0, 0, new IdentifierValue("\"foo\"")), OrderDirection.DESC, NullsOrderType.LAST)))) |
| 239 | + .build(); |
| 240 | + SelectStatementContext selectStatementContext = createSelectStatementContext(selectStatement); |
| 241 | + assertFalse(selectStatementContext.isSameGroupByAndOrderByItems()); |
| 242 | + } |
| 243 | + |
| 244 | + @Test |
| 245 | + void assertIsNotSameGroupByAndOrderByItemsWhenColumnMatchesDifferentProjectionAliasIgnoringCase() { |
| 246 | + ProjectionsSegment projectionsSegment = new ProjectionsSegment(0, 0); |
| 247 | + ColumnSegmentBoundInfo boundInfo = new ColumnSegmentBoundInfo(new IdentifierValue("name")); |
| 248 | + ColumnSegment projectionColumn = new ColumnSegment(0, 0, new IdentifierValue("name")); |
| 249 | + projectionColumn.setColumnBoundInfo(boundInfo); |
| 250 | + ColumnProjectionSegment projectionSegment = new ColumnProjectionSegment(projectionColumn); |
| 251 | + projectionSegment.setAlias(new AliasSegment(0, 0, new IdentifierValue("ID"))); |
| 252 | + projectionsSegment.getProjections().add(projectionSegment); |
| 253 | + ColumnSegment groupByColumn = new ColumnSegment(0, 0, new IdentifierValue("id")); |
| 254 | + groupByColumn.setColumnBoundInfo(boundInfo); |
| 255 | + ColumnSegment orderByColumn = new ColumnSegment(0, 0, new IdentifierValue("id")); |
| 256 | + orderByColumn.setColumnBoundInfo(boundInfo); |
| 257 | + SelectStatement selectStatement = SelectStatement.builder().databaseType(databaseType).projections(projectionsSegment) |
| 258 | + .groupBy(new GroupBySegment(0, 0, Collections.singletonList( |
| 259 | + new ColumnOrderByItemSegment(groupByColumn, OrderDirection.ASC, NullsOrderType.LAST)))) |
| 260 | + .orderBy(new OrderBySegment(0, 0, Collections.singletonList( |
| 261 | + new ColumnOrderByItemSegment(orderByColumn, OrderDirection.DESC, NullsOrderType.LAST)))) |
| 262 | + .build(); |
| 263 | + SelectStatementContext selectStatementContext = createSelectStatementContext(selectStatement); |
| 264 | + assertFalse(selectStatementContext.isSameGroupByAndOrderByItems()); |
| 265 | + } |
194 | 266 |
|
195 | 267 | @Test |
196 | 268 | void assertIsNotSameGroupByAndOrderByItemsWhenEmptyGroupBy() { |
|
0 commit comments