You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2022/10/18 12:16:31 UTC

[shardingsphere] branch master updated: Asserting tables that should not exit from routine body segment (#21366)

This is an automated email from the ASF dual-hosted git repository.

zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 19011e8c3bb Asserting tables that should not exit from routine body segment (#21366)
19011e8c3bb is described below

commit 19011e8c3bb5ac6f351bd19c4a81519260b4ffb2
Author: Gabriel Cunha <cu...@gmail.com>
AuthorDate: Tue Oct 18 09:16:23 2022 -0300

    Asserting tables that should not exit from routine body segment (#21366)
---
 .../sql/common/extractor/TableExtractorTest.java   | 37 ++++++++++++++++------
 1 file changed, 27 insertions(+), 10 deletions(-)

diff --git a/sql-parser/statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/common/extractor/TableExtractorTest.java b/sql-parser/statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/common/extractor/TableExtractorTest.java
index 2c347c65895..760377aec3e 100644
--- a/sql-parser/statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/common/extractor/TableExtractorTest.java
+++ b/sql-parser/statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/common/extractor/TableExtractorTest.java
@@ -17,7 +17,20 @@
 
 package org.apache.shardingsphere.sql.parser.sql.common.extractor;
 
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.hasSize;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.Optional;
 import org.apache.shardingsphere.sql.parser.sql.common.constant.AggregationType;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.routine.RoutineBodySegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.routine.ValidStatementSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.assignment.AssignmentSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.assignment.ColumnAssignmentSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
@@ -29,21 +42,12 @@ import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.predicate.Loc
 import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.OwnerSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.TableNameSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.CreateTableStatement;
 import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
 import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dml.MySQLInsertStatement;
 import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dml.MySQLSelectStatement;
 import org.junit.Test;
 
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Iterator;
-import java.util.Optional;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.junit.Assert.assertTrue;
-
 public final class TableExtractorTest {
     
     private final TableExtractor tableExtractor = new TableExtractor();
@@ -93,6 +97,19 @@ public final class TableExtractorTest {
         assertTableSegment(tableSegmentIterator.next(), 122, 128, "t_order");
         assertTableSegment(tableSegmentIterator.next(), 130, 132, "t_order");
     }
+
+    @Test
+    public void assertNotExistTableFromRoutineBody() {
+        RoutineBodySegment routineBodySegment = new RoutineBodySegment(0, 3);
+        ValidStatementSegment validStatement = new ValidStatementSegment(0, 1);
+        validStatement.setSqlStatement(() -> 0);
+        routineBodySegment.getValidStatements().add(validStatement);
+        ValidStatementSegment newValidStatement = new ValidStatementSegment(0, 1);
+        validStatement.setSqlStatement(mock(CreateTableStatement.class));
+        routineBodySegment.getValidStatements().add(newValidStatement);
+        Collection<SimpleTableSegment> nonExistingTables = tableExtractor.extractNotExistTableFromRoutineBody(routineBodySegment);
+        assertThat(nonExistingTables, hasSize(1));
+    }
     
     private void assertTableSegment(final SimpleTableSegment actual, final int expectedStartIndex, final int expectedStopIndex, final String expectedTableName) {
         assertThat(actual.getStartIndex(), is(expectedStartIndex));