Oracle 是否总是使用子句解析 CTE,即使它们未在结果集中使用

Does Oracle always resolve CTE with clauses even if they are not used in the result set

如果我有这样的 Oracle SQL 查询:

with
    query1 as (
        select * from animals where type = 'dog'
    ),
    query2 as (
        select * from animals where type = 'cat'
    )

select * from query1;

DBMS 是否会实际执行 resolving/running query2 的工作,或者 Oracle 是否知道最终输出不需要 query2,因此应该跳过 CTE/with 的工作?

Oracle 版本为 12c Enterprise。

我打算说“这取决于优化器”或“这很难回答”或“您需要查看执行计划”。但是想出一个代码不是 运行 的例子就足够了。

因此 here 是一个示例,证明至少一个版本的 Oracle 至少有一个示例不评估 CTE:

with query1 as (
        select * from animals where type = 'dog'
    ),
    query2 as (
        select a.*, type + 1 from animals a
        
    )
select * from query1;

如果对第二个 CTE 进行评估,则会产生错误。

当然,这并不能保证 Oracle 始终忽略未使用的 CTE。并且可能对行为有更多神秘的解释,但非评估似乎是最简单的。