GaussDB(DWS) conditional expression function returns wrong result set troubleshooting

Abstract: When the result set inconsistency occurs in the conditional expression function, we must first consider whether the inconsistency of the input parameter data type leads to the inconsistency of the output parameter.

This article is shared from the HUAWEI CLOUD community " GaussDB (DWS) Conditional Expression Function Returns Error Result Set Troubleshooting ", author: yd_211369925.

(1) Case background

The customer uses greatest to obtain and return the value of the expression with the largest value in the parameter list, and the query result in the subquery is inconsistent with the results of the subquery sticking out and executing the result set separately.

select greatest(1,2,100,-1,0,nvl(null,0)) --结果为2,select nvl(null,0)的结果为0
select greatest(1,2,100,-1,0,0) --结果为100

(2) Troubleshooting

First of all, we need to understand the usage of the two functions greatest and nvl

The version used by the customer is dws820 and the environment is in mysql compatibility mode, and the result type of nvl(null,0) is unknown, which is text;

There are int and text in the first greatest(1,2,100,-1,0,nvl(null,0)) parameter, that is, sorting by character is the largest;

The parameters of the second greatest(1,2,100,-1,0) are all ints, and the output is sorted by value 100 is the largest;

expand:

For some non-conditional expression functions, you can use \df+ function name (min is used here as an example) to find the data types of input and output parameters

Or first use select proname, proisstrict, provolatile, prorettype, proargtypes, prosrc, proshippable from pg_proc where proname = 'function name';

Then use select oid, typname from pg_type where oid ='xxx'; (here oid is queried according to the above prorettype, proargtypes)

Specific function important attribute reference

GaussDB (DWS) function pushdown attribute introduction
https://bbs.huaweicloud.com/blogs/250351

(3) Solutions

From the above investigation, it can be seen that the first statement should be rewritten, and nvl(null, 0) should be replaced with nvl(null,0)::int so that all data types in the greatest function are int.

SELECT greatest(1,2,100,-1,0,nvl(null,0)::int)

The result at this time is that 100 meets the customer's expected result.

 

Click to follow and learn about Huawei Cloud's fresh technologies for the first time~

{{o.name}}
{{m.name}}

Guess you like

Origin my.oschina.net/u/4526289/blog/9210584