diff --git a/sharding-core/sharding-core-preprocessor/src/test/java/org/apache/shardingsphere/core/optimize/segment/select/projection/DerivedColumnTest.java b/sharding-core/sharding-core-preprocessor/src/test/java/org/apache/shardingsphere/core/optimize/segment/select/projection/DerivedColumnTest.java index 75257e14fda6b25c1397baa977d8e5f9d4550811..c8747a2b573e985d839c61562809eafb44ec843e 100644 --- a/sharding-core/sharding-core-preprocessor/src/test/java/org/apache/shardingsphere/core/optimize/segment/select/projection/DerivedColumnTest.java +++ b/sharding-core/sharding-core-preprocessor/src/test/java/org/apache/shardingsphere/core/optimize/segment/select/projection/DerivedColumnTest.java @@ -46,4 +46,18 @@ public final class DerivedColumnTest { public void assertIsNotDerivedColumn() { assertFalse(DerivedColumn.isDerivedColumn("OTHER_DERIVED_COLUMN_0")); } + + @Test + public void assertIsDerivedColumnName() { + assertTrue(DerivedColumn.isDerivedColumnName("AVG_DERIVED_COUNT_")); + assertTrue(DerivedColumn.isDerivedColumnName("AVG_DERIVED_SUM_")); + assertTrue(DerivedColumn.isDerivedColumnName("ORDER_BY_DERIVED_")); + assertTrue(DerivedColumn.isDerivedColumnName("GROUP_BY_DERIVED_")); + assertTrue(DerivedColumn.isDerivedColumnName("AGGREGATION_DISTINCT_DERIVED_")); + } + + @Test + public void assertIsNotDerivedColumnName() { + assertFalse(DerivedColumn.isDerivedColumnName("OTHER_DERIVED_COLUMN_0")); + } } diff --git a/sharding-core/sharding-core-preprocessor/src/test/java/org/apache/shardingsphere/core/optimize/segment/table/TablesContextTest.java b/sharding-core/sharding-core-preprocessor/src/test/java/org/apache/shardingsphere/core/optimize/segment/table/TablesContextTest.java index b1fff3d3deb6a770e234de18e7191eb297ff089b..25dc8d489a2b0a7823f4bcca2736d633eaab923d 100644 --- a/sharding-core/sharding-core-preprocessor/src/test/java/org/apache/shardingsphere/core/optimize/segment/table/TablesContextTest.java +++ b/sharding-core/sharding-core-preprocessor/src/test/java/org/apache/shardingsphere/core/optimize/segment/table/TablesContextTest.java @@ -19,6 +19,8 @@ package org.apache.shardingsphere.core.optimize.segment.table; import com.google.common.base.Optional; import com.google.common.collect.Sets; +import org.apache.shardingsphere.core.metadata.table.TableMetas; +import org.apache.shardingsphere.core.parse.sql.segment.dml.column.ColumnSegment; import org.apache.shardingsphere.core.parse.sql.segment.generic.TableSegment; import org.apache.shardingsphere.core.parse.sql.statement.dml.SelectStatement; import org.hamcrest.CoreMatchers; @@ -30,6 +32,9 @@ import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public final class TablesContextTest { @@ -133,6 +138,49 @@ public final class TablesContextTest { assertFalse(table.isPresent()); } + @Test + public void assertFindTableNameWhenSingleTable() { + SelectStatement selectStatement = new SelectStatement(); + selectStatement.getAllSQLSegments().add(createTableSegment("table_1", "tbl_1")); + assertTrue(new TablesContext(selectStatement).findTableName(null, null).isPresent()); + } + + @Test + public void assertFindTableNameWhenColumnSegmentOwnerPresent() { + SelectStatement selectStatement = new SelectStatement(); + selectStatement.getAllSQLSegments().add(createTableSegment("table_1", "tbl_1")); + selectStatement.getAllSQLSegments().add(createTableSegment("table_2", "tbl_2")); + TablesContext tablesContext = new TablesContext(selectStatement); + ColumnSegment columnSegment = mock(ColumnSegment.class); + when(columnSegment.getOwner()).thenReturn(Optional.of(new TableSegment(0, 10, "table_1"))); + assertTrue(tablesContext.findTableName(columnSegment, null).isPresent()); + } + + @Test + public void assertFindTableNameWhenColumnSegmentOwnerAbsent() { + SelectStatement selectStatement = new SelectStatement(); + selectStatement.getAllSQLSegments().add(createTableSegment("table_1", "tbl_1")); + selectStatement.getAllSQLSegments().add(createTableSegment("table_2", "tbl_2")); + TablesContext tablesContext = new TablesContext(selectStatement); + ColumnSegment columnSegment = mock(ColumnSegment.class); + when(columnSegment.getOwner()).thenReturn(Optional.absent()); + TableMetas tableMetas = mock(TableMetas.class); + assertFalse(tablesContext.findTableName(columnSegment, tableMetas).isPresent()); + } + + @Test + public void assertFindTableNameWhenColumnSegmentOwnerAbsentAndTableMetasContainsColumn() { + SelectStatement selectStatement = new SelectStatement(); + selectStatement.getAllSQLSegments().add(createTableSegment("table_1", "tbl_1")); + selectStatement.getAllSQLSegments().add(createTableSegment("table_2", "tbl_2")); + ColumnSegment columnSegment = mock(ColumnSegment.class); + when(columnSegment.getOwner()).thenReturn(Optional.absent()); + when(columnSegment.getName()).thenReturn("columnName"); + TableMetas tableMetas = mock(TableMetas.class); + when(tableMetas.containsColumn(anyString(), anyString())).thenReturn(true); + assertTrue(new TablesContext(selectStatement).findTableName(columnSegment, tableMetas).isPresent()); + } + private TableSegment createTableSegment(final String tableName, final String alias) { TableSegment result = new TableSegment(0, 0, tableName); result.setAlias(alias); diff --git a/sharding-core/sharding-core-preprocessor/src/test/java/org/apache/shardingsphere/core/optimize/statement/impl/InsertSQLStatementContextTest.java b/sharding-core/sharding-core-preprocessor/src/test/java/org/apache/shardingsphere/core/optimize/statement/impl/InsertSQLStatementContextTest.java index 50bf74a18390975c057d6682928b07aab8451764..e4f6229bd2d4d74348075ae231a2c3f6267e90d7 100644 --- a/sharding-core/sharding-core-preprocessor/src/test/java/org/apache/shardingsphere/core/optimize/statement/impl/InsertSQLStatementContextTest.java +++ b/sharding-core/sharding-core-preprocessor/src/test/java/org/apache/shardingsphere/core/optimize/statement/impl/InsertSQLStatementContextTest.java @@ -57,6 +57,17 @@ public final class InsertSQLStatementContextTest { assertInsertSQLStatementContext(actual); } + @Test + public void assertGetGroupedParameters() { + TableMetas tableMetas = mock(TableMetas.class); + when(tableMetas.getAllColumnNames("tbl")).thenReturn(Arrays.asList("id", "name", "status")); + InsertStatement insertStatement = new InsertStatement(); + insertStatement.getAllSQLSegments().add(new TableSegment(0, 0, "tbl")); + setUpInsertValues(insertStatement); + InsertSQLStatementContext actual = new InsertSQLStatementContext(tableMetas, Arrays.asList(1, "Tom", 2, "Jerry"), insertStatement); + assertThat(actual.getGroupedParameters().size(), is(2)); + } + private void setUpInsertValues(final InsertStatement insertStatement) { insertStatement.getValues().add(new InsertValuesSegment(0, 0, Arrays.asList( new ParameterMarkerExpressionSegment(0, 0, 1), new ParameterMarkerExpressionSegment(0, 0, 2), new LiteralExpressionSegment(0, 0, "init"))));