Hi,

While reviewing patch [1], I noticed an unnecessary function parameter. Patch 
[1] mainly fixed a type dependency check for index expressions, and it also 
changed the relation OID passed to CheckTypeDependencyInSingleRelExpr():
```
-                                               if 
(CheckTypeDependencyInSingleRelExpr(expr, RelationGetRelid(rel), typeOid))
+                                               if 
(CheckTypeDependencyInSingleRelExpr(expr, indrelid, typeOid))
                                                        
report_type_used_in_expr(depobj, origRelation, origTypeName);
```

The old code passed the index's OID, but the expression refers to columns of 
the indexed table, so patch [1] changed it to pass the table's OID, which is 
semantically correct.

I was curious why passing the wrong OID didn't cause an error. After some 
debugging, I found that the walker uses the relation OID to record column 
dependencies. CheckTypeDependencyInSingleRelExpr() only looks for a dependency 
on a particular type, so column objects won’t be checked, whatever OID supplied 
doesn’t matter. I think asking callers to supply a relation OID here adds a 
maintenance burden and can leave readers wondering which OID to pass.

The attached patch removes the need to pass a range table to 
CheckTypeDependencyInExpr() or a relation OID to 
CheckTypeDependencyInSingleRelExpr(). Once those parameters were gone, the only 
remaining difference was whether to check the expression's result type. I made 
that an explicit argument to CheckTypeDependencyInExpr() and removed 
CheckTypeDependencyInSingleRelExpr(). I made the same change to 
CheckUsageOnTypesInExpr() and CheckUsageOnTypesInSingleRelExpr().

The reason CheckTypeDependencyInSingleRelExpr() used to take a relation OID, I 
think, was to build a range table for the walker. If the walker encounters a 
Var that refers to that range table, it needs the entry to resolve the Var, 
otherwise it will raise an error. I added a boolean field skipvar to 
find_expr_references_context so these type checks can tell the walker to skip 
Var nodes.

This is a pure refactoring patch. It should preserve all existing runtime 
behavior. I haven't changed any tests, and "make check-world" passed. The patch 
removes two functions and reduces the code by 42 lines, so I think it's a 
maintenance improvement.

BTW, I also wonder whether CheckTypeDependencyInExpr() and 
CheckUsageOnTypesInExpr() really need the checkResultType parameter. But I’d 
leave that for a separate discussion, as I intended to preserve the existing 
behavior in this patch.

Please see the attached patch for details. Am I missing anything?

[1] https://postgr.es/m/[email protected]

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

Attachment: v1-0001-Skip-Var-resolution-when-checking-expression-type.patch
Description: Binary data

Reply via email to