How to check if a column in a query can produce NULL? I am scheming how to implement a type generator for arbitrary SQL queries.
I found that for any query, the simplest way to get the types of every column is to create a temporary table and then inspect that table. However, this tells nothing about which columns can be NULL.
I am interested to find a solution to identifying which columns can be NULL for any arbitrary query with the goal of generating types for that query.
What’s the way to go about it?
Since SQL (with recursive CTEs) is Turing complete, figuring out if a column in the result could be NULL or not is probably as complex as the halting problem and cannot be computed.
So either run the query and examine the result or restrict yourself to very simple queries (but even then the problem will be non-trivial).
My personal recommendation is to give up.