When editing a data source template, inconsistent results can be seen due to database query #3350
Describe the bug
In the screenshot below the template is missing its name, data input method and has the wrong data source profile.
Desktop (please complete the following information):
Where this query should return the template information for template ID 41, it returns an empty set on the post-upgrade database, but the expected data on the pre-upgrade database:
The reason for this is that the query can only return one row due to the SUM(CASE WHEN dl.data_template_id = '41' THEN 1 ELSE 0 END) AS data_sources portion of the query. Without any type of forced ORDERING this will cause the query to return the first chronologically inserted row in the database which matches. Under normal circumstances this will always be the template and the query will work. However, if the database is changed in such a way where the rows could be inserted in a different chronological order - such as during an dump/import operation - this query can break and return nothing.
For example, in the pre-upgrade database the slightly-modified version of this query returns:
This is expected, and when the HAVING filter is applied to this result it will match and the row is returned.
In the post-upgrade/import database the same query returns a different row:
This is a data source - not a template. When the HAVING filter is applied this returns an empty set and the template is broken.
The text was updated successfully, but these errors were encountered:
Thanks for the response. I have not considered a pull request because all my attempts thus far to compose a working equivalent SQL query have failed. My SQL abilities may not be good enough to come up with a graceful solution to this problem on my own, so I was hoping for some help with that. Even if you try to add some ordering to the query it seems to break the SUM() portion of the query which tries to sum up the data sources using the template.
I've also tried variations of the query using SUM(IF(dl.data_template_id = '41', 1, 0)) instead but I run into the same problem. I can't seem to make it work without reverting to two queries.
I commissioned some SQL expertise from within my company here and we came up with this as a possible alternative: