Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat: support correlated subquery in select clause (#6987)

h3n4l1a5038e445e0
diff --git a/backend/plugin/db/util/driverutil_test.go b/backend/plugin/db/util/driverutil_test.go
index fa36ce648c..6e329ac47a 100644
--- a/backend/plugin/db/util/driverutil_test.go
+++ b/backend/plugin/db/util/driverutil_test.go
@@ -1819,6 +1819,21 @@ func TestSnowSQLExtractSensitiveField(t *testing.T) {
schemaInfo *db.SensitiveSchemaInfo
fieldList []db.SensitiveField
}{
+ {
+ // Test for correlated sub-query.
+ statement: `SELECT A, (SELECT MAX(B) > Y.A FROM T1 X) FROM T1 Y`,
+ schemaInfo: defaultDatabaseSchema,
+ fieldList: []db.SensitiveField{
+ {
+ Name: "A",
+ Sensitive: true,
+ },
+ {
+ Name: "(SELECTMAX(B)>Y.AFROMT1X)",
+ Sensitive: true,
+ },
+ },
+ },
{
// Test for CTE in CTE.
statement: `WITH TT1 (T1_COL1, T1_COL2) AS (
diff --git a/backend/plugin/db/util/mask_sensitive_data_snowsql.go b/backend/plugin/db/util/mask_sensitive_data_snowsql.go
index dec9facd57..f918796df1 100644
--- a/backend/plugin/db/util/mask_sensitive_data_snowsql.go
+++ b/backend/plugin/db/util/mask_sensitive_data_snowsql.go
@@ -141,10 +141,10 @@ func (extractor *sensitiveFieldExtractor) extractSnowsqlSensitiveFieldsSelectSta
if err != nil {
return nil, err
}
- originalFromFields := extractor.fromFieldList
- extractor.fromFieldList = fromFieldList
+ originalFromFieldsLength := len(extractor.fromFieldList)
+ extractor.fromFieldList = append(extractor.fromFieldList, fromFieldList...)
defer func() {
- extractor.fromFieldList = originalFromFields
+ extractor.fromFieldList = extractor.fromFieldList[:originalFromFieldsLength]
}()
}
@@ -829,6 +829,12 @@ func (extractor *sensitiveFieldExtractor) extractSnowsqlSensitiveFieldsObjectRef
aliasName := parser.NormalizeObjectNamePart(id)
for i := 0; i < len(result); i++ {
result[i].table = aliasName
+ // We can safely set the database and schema to empty string because the
+ // user cannot use the original table name to access the column.
+ // For example, the following query is illegal:
+ // SELECT T1.A FROM T1 AS T2;
+ result[i].schema = ""
+ result[i].database = ""
}
}