Dynamic Query Re-write or Evaluating the Query Before Executing Table Function
<p>First, I want to make it clear that the question is not about the materialized views feature.</p>

<p>Suppose, I have a table function that returns a pre-defined set of columns.</p>

<p>When a function call is submitted as</p>

<pre><code>SELECT col1, col2, col3
FROM TABLE(my_tfn(:p1))
WHERE col4 = 'X';
</code></pre>

<p>I can evaluate the parameter and choose what queries to execute.
I can either open one of the pre-defined cursors, or I can assemble my query dynamically.</p>

<p>What if instead of evaluating the parameter I want to evaluate the text of the requesting query?</p>

<p>For example, if my function returns 20 columns but the query is only requesting 4,
I can assign NULLs to remaining 16 clumns of the return type, and execute fewer joins.
Or I can push the filter down to my dynamic query.</p>

<p>Is there a way to make this happen?
More generally, is there a way to look at the requesting query before exuting the function?</p>