Goal | Find derived tables (views and materialized views) that apparently concatenate values (by using || operator or use Concat function or use Format function) by putting a space between these but do not use Trim function to get rid of the extra space at the beginning or the end of the string. |
Notes | In PostgreSQL NULL || 'text' => NULL. To replace NULL with an empty string one can use Coalesce function. Thus Coalesce(NULL,'') || 'text' => 'text'. Coalesce(NULL,'') || ' ' || 'text' => ' text'. We can use Trim function to get rid of the space at the beginning of the concatenation result like that: Trim(Coalesce(NULL,'') || ' ' || 'text'))=> 'text'. Similarly, Concat (NULL, ' ', 'text') )=>' text' and Trim should be used to get rid of the space at the beginning of the result. Instead of SELECT Concat(surname, ' ', given_name) AS person_name FROM Person; write SELECT Trim(Concat(surname, ' ', given_name)) AS person_name FROM Person; |
Type | Problem detection (Each row in the result could represent a flaw in the design) |
Reliability | Low (Many false-positive results) |
License | MIT License |
Fixing Suggestion | Change the subquery of the derived table. Use Trim function or use Concat_ws function to concatenate the values. concat(NULL, ' ', 'a')=> " a" |
Data Source | system catalog only |
SQL Query |
|
This query is classified under the following categories:
Name | Description |
---|---|
Derived tables | Queries of this category provide information about the derived tables (views, materialized views), which are used to implement virtual data layer. |
Further reading and related materials: