Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: data masking failed with multiple join clause in SQL #327

Merged
merged 1 commit into from
Sep 13, 2023
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 @@ -398,8 +398,7 @@ private List<LogicalColumn> extractItemFromReference(String databaseName, String
} else {
// 2. 表名不为空,从 fromTable 和 fromTable#tableList 的 columnList 进行查询
for (LogicalTable fromTable : fromTables) {
List<LogicalTable> tables = new ArrayList<>(Collections.singletonList(fromTable));
tables.addAll(fromTable.getTableList());
List<LogicalTable> tables = new ArrayList<>(retrieveLogicalTable(fromTable));
if (COLUMN_NAME_WILDCARD.equals(columnName)) {
// 2.1. 列名为 *,即只要库名和表名匹配的都输出
// 先查 fromTable,再查 fromTable#tableList
Expand Down Expand Up @@ -709,6 +708,19 @@ private List<LogicalColumn> getColumnsFromVirtualTable(String tableName) throws
throw new NotFoundException(ErrorCodes.NotFound, new Object[] {"table", "name", tableName}, null);
}

private List<LogicalTable> retrieveLogicalTable(LogicalTable table) {
List<LogicalTable> returnValue = new ArrayList<>();
if (Objects.nonNull(table)) {
returnValue.add(table);
if (CollectionUtils.isNotEmpty(table.getTableList())) {
for (LogicalTable t : table.getTableList()) {
returnValue.addAll(retrieveLogicalTable(t));
}
}
}
return returnValue;
}

private String processIdentifier(String identifier) {
if (Objects.nonNull(dialectType) && dialectType.isMysql()) {
String unquoted = StringUtils.unquoteMySqlIdentifier(identifier);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,16 @@ public void test_extract_OBMySQL_fromNaturalJoinTable() {
Assert.assertEquals(expectLabels, actualLabels);
}

@Test
public void test_extract_OBMySQL_fromMultiJoinTable() {
String sql = TestColumnExtractorUtil.getTestSql(DialectType.OB_MYSQL, "from-multi-join-table");
LogicalTable result = obMySQLColumnExtractor.extract(obMySQLParser.parse(new StringReader(sql)));
List<String> actualLabels = getResultColumnLabels(result);
List<String> expectLabels = Arrays.asList("id", "name", "birthday", "description");
Assert.assertEquals(4, result.getColumnList().size());
Assert.assertEquals(expectLabels, actualLabels);
}

@Test
public void test_extract_OBMySQL_fromSingleSubquery() {
String sql = TestColumnExtractorUtil.getTestSql(DialectType.OB_MYSQL, "from-single-subquery");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,13 @@ mysql:
FROM
test_data_masking_1 AS t1 NATURAL
JOIN test_data_masking_2 AS t2;
from-multi-join-table: |-
SELECT
t1.*
FROM
test_data_masking_1 t1
INNER JOIN test_data_masking_2 t2 ON t1.id = t2.id
INNER JOIN test_data_masking_3 t3 ON t2.id = t3.id;
from-single-subquery: |-
SELECT
*
Expand Down