You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2021/11/06 23:28:04 UTC

[shardingsphere] branch master updated: fixbug: columnMatchTableAndCheckAmbiguous on same simpleTableSegment but alias (#13485)

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

duanzhengqiang 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 e436e48  fixbug: columnMatchTableAndCheckAmbiguous on same simpleTableSegment but alias (#13485)
e436e48 is described below

commit e436e48487ab1bf4aa50976965bfbfbb36b97764
Author: cheese8 <yi...@163.com>
AuthorDate: Sun Nov 7 07:27:07 2021 +0800

    fixbug: columnMatchTableAndCheckAmbiguous on same simpleTableSegment but alias (#13485)
    
    * fixbug: columnMatchTableAndCheckAmbiguous on same simpleTableSegment but alias
    
    * fix checkstyle
    
    * fix checkstyle
    
    * fix checkstyle
---
 .../impl/EncryptProjectionTokenGenerator.java      |  4 +--
 .../impl/EncryptProjectionTokenGeneratorTest.java  | 33 +++++++++++++++++++---
 2 files changed, 31 insertions(+), 6 deletions(-)

diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/impl/EncryptProjectionTokenGenerator.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/impl/EncryptProjectionTokenGenerator.java
index b43d874..385861f 100644
--- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/impl/EncryptProjectionTokenGenerator.java
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/impl/EncryptProjectionTokenGenerator.java
@@ -115,7 +115,7 @@ public final class EncryptProjectionTokenGenerator extends BaseEncryptSQLTokenGe
         if (!columnProjectionSegment.getColumn().getOwner().isPresent()) {
             return false;
         }
-        return selectStatementContext.getTablesContext().getAllUniqueTables().stream().anyMatch(table -> tableName.equals(table.getTableName().getIdentifier().getValue()) 
+        return selectStatementContext.getTablesContext().getOriginalTables().stream().anyMatch(table -> tableName.equals(table.getTableName().getIdentifier().getValue())
                 && table.getAlias().isPresent() && columnProjectionSegment.getColumn().getOwner().get().getIdentifier().getValue().equals(table.getAlias().get()));
     }
     
@@ -123,7 +123,7 @@ public final class EncryptProjectionTokenGenerator extends BaseEncryptSQLTokenGe
         if (!columnProjectionSegment.getColumn().getOwner().isPresent()) {
             return false;
         }
-        return selectStatementContext.getTablesContext().getAllUniqueTables().stream().anyMatch(table -> tableName.equals(table.getTableName().getIdentifier().getValue()) 
+        return selectStatementContext.getTablesContext().getOriginalTables().stream().anyMatch(table -> tableName.equals(table.getTableName().getIdentifier().getValue())
                 && !table.getAlias().isPresent() && columnProjectionSegment.getColumn().getOwner().get().getIdentifier().getValue().equals(tableName));
     }
     
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/impl/EncryptProjectionTokenGeneratorTest.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/impl/EncryptProjectionTokenGeneratorTest.java
index a555ea0..4f88654 100644
--- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/impl/EncryptProjectionTokenGeneratorTest.java
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/impl/EncryptProjectionTokenGeneratorTest.java
@@ -25,8 +25,10 @@ import org.apache.shardingsphere.infra.rewrite.sql.token.pojo.generic.Substituta
 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ColumnProjectionSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ProjectionsSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.AliasSegment;
 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.value.identifier.IdentifierValue;
 import org.junit.Before;
 import org.junit.Rule;
@@ -63,9 +65,31 @@ public final class EncryptProjectionTokenGeneratorTest {
         ProjectionsSegment projectionsSegment = mock(ProjectionsSegment.class);
         SelectStatementContext sqlStatementContext = mock(SelectStatementContext.class, RETURNS_DEEP_STUBS);
         when(sqlStatementContext.getSqlStatement().getProjections()).thenReturn(projectionsSegment);
+        SimpleTableSegment doctor = new SimpleTableSegment(new TableNameSegment(1, 7, new IdentifierValue("doctor")));
+        doctor.setAlias(new AliasSegment(8, 9, new IdentifierValue("a")));
+        SimpleTableSegment doctor1 = new SimpleTableSegment(new TableNameSegment(10, 17, new IdentifierValue("doctor1")));
+        when(sqlStatementContext.getTablesContext().getOriginalTables()).thenReturn(Arrays.asList(doctor, doctor1));
         when(sqlStatementContext.getTablesContext().getTableNames()).thenReturn(Arrays.asList("doctor", "doctor1"));
-        List<SimpleTableSegment> allUniqueTables = buildAllUniqueTables();
-        when(sqlStatementContext.getTablesContext().getAllUniqueTables()).thenReturn(allUniqueTables);
+        IdentifierValue identifierValue = new IdentifierValue("mobile");
+        ColumnSegment columnSegment = new ColumnSegment(0, 0, identifierValue);
+        OwnerSegment ownerSegment = new OwnerSegment(0, 0, new IdentifierValue("a"));
+        columnSegment.setOwner(ownerSegment);
+        ColumnProjectionSegment columnProjectionSegment = new ColumnProjectionSegment(columnSegment);
+        when(projectionsSegment.getProjections()).thenReturn(Collections.singletonList(columnProjectionSegment));
+        Collection<SubstitutableColumnNameToken> tokens = encryptProjectionTokenGenerator.generateSQLTokens(sqlStatementContext);
+        assertThat(tokens.size(), is(1));
+    }
+    
+    @Test
+    public void assertOwnerExistsMatchTableAliasGenerateSQLTokens2() {
+        ProjectionsSegment projectionsSegment = mock(ProjectionsSegment.class);
+        SelectStatementContext sqlStatementContext = mock(SelectStatementContext.class, RETURNS_DEEP_STUBS);
+        when(sqlStatementContext.getSqlStatement().getProjections()).thenReturn(projectionsSegment);
+        SimpleTableSegment doctor = new SimpleTableSegment(new TableNameSegment(1, 7, new IdentifierValue("doctor")));
+        doctor.setAlias(new AliasSegment(8, 9, new IdentifierValue("a")));
+        SimpleTableSegment doctor1 = new SimpleTableSegment(new TableNameSegment(10, 17, new IdentifierValue("doctor")));
+        when(sqlStatementContext.getTablesContext().getOriginalTables()).thenReturn(Arrays.asList(doctor, doctor1));
+        when(sqlStatementContext.getTablesContext().getTableNames()).thenReturn(Arrays.asList("doctor"));
         IdentifierValue identifierValue = new IdentifierValue("mobile");
         ColumnSegment columnSegment = new ColumnSegment(0, 0, identifierValue);
         OwnerSegment ownerSegment = new OwnerSegment(0, 0, new IdentifierValue("a"));
@@ -81,9 +105,10 @@ public final class EncryptProjectionTokenGeneratorTest {
         ProjectionsSegment projectionsSegment = mock(ProjectionsSegment.class);
         SelectStatementContext sqlStatementContext = mock(SelectStatementContext.class, RETURNS_DEEP_STUBS);
         when(sqlStatementContext.getSqlStatement().getProjections()).thenReturn(projectionsSegment);
+        SimpleTableSegment doctor = new SimpleTableSegment(new TableNameSegment(1, 7, new IdentifierValue("doctor")));
+        SimpleTableSegment doctor1 = new SimpleTableSegment(new TableNameSegment(10, 17, new IdentifierValue("doctor1")));
+        when(sqlStatementContext.getTablesContext().getOriginalTables()).thenReturn(Arrays.asList(doctor, doctor1));
         when(sqlStatementContext.getTablesContext().getTableNames()).thenReturn(Arrays.asList("doctor", "doctor1"));
-        List<SimpleTableSegment> allUniqueTables = buildAllUniqueTables(false);
-        when(sqlStatementContext.getTablesContext().getAllUniqueTables()).thenReturn(allUniqueTables);
         IdentifierValue identifierValue = new IdentifierValue("mobile");
         ColumnSegment columnSegment = new ColumnSegment(0, 0, identifierValue);
         OwnerSegment ownerSegment = new OwnerSegment(0, 0, new IdentifierValue("doctor"));