Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Wrong result on a simple SQL statement

cadwinvalOct 2 2020

20201002-bug-reproducer.txt (1.44 KB)Hello
I think that I've found a bug in Oracle 19 (probably reproducible on Oracle 12 but I haven't tested).
Please find enclosed a self-contained reproducer which creates a single table, insert 4 lines and runs a select query.
It gives the expected result if and if I use the optimizer hint "NO_QUERY_TRANSFORMATION" or if I drive "r_m_s_id" non nullable or if I drive the select query much more complicated by duplicating some code and using a coalesce or if I remove one inner join but those workarounds aren't satisfying (performance considerations).
I assume that Oracle SQL transforms the query to optimize the execution plan but it seems to treat a nullable column as always null.
By the way, PostgreSQL 9.6 always returns the expected result.
Has someone else experienced the same problem? Does it work in Oracle 20?

Comments

tlokweng
Answer

クエリトランスフォーメーションのビューマージで2階層のクエリが1階層に置換され、結果として各行でプロシジャが2回づつ呼ばれているということですね。なので単純にプロシジャを呼んでいるインラインビューがマージされないようにすれば解決します。素直にNO_MERGEヒントでOKでしょう。テーブルサイズが大きくなければWITHにしてファクタリングするのもアリですね。

> create table test_table(n) as select 1 from dual union select 2 from dual;

> create function test_func return varchar2 as begin RETURN DBMS_RANDOM.STRING('x', 10); end;

> select n, r r1, r r2 from (select n, test_func r from test_table);

        N R1          R2

--------- ------------ ------------

        1 AJ1LNZ30HD  LAMQV4CQUY

        2 F911DGQDYZ  ID8FDREGKF

> select n, r r1, r r2 from (select /*+ no_merge */ n, test_func r from test_table);

--------- ------------ ------------

        1 K5MPMUR3DQ  K5MPMUR3DQ

        2 DD2Y5NUWPM  DD2Y5NUWPM

> with t as (select /*+ materialize */ n, test_func r from test_table) select n, r r1, r r2 from t;

--------- ------------ ------------

        1 0F2RE7Y8C3  0F2RE7Y8C3

        2 O6C4WCBAFP  O6C4WCBAFP

Marked as Answer by 988825 · Sep 27 2020
988825

tlokweng様、お礼が遅くなってしまい、申し訳ありません。サンプルまでお示しくださいまして、誠にありがとうございます。

クエリトランスフォーメーションがこのようなSQLに対し作用しているとは全く一度も想像すらつきませんでした。

頂戴したサンプルで動作も確認出来ましたので、本来の組み込み先に入れてみようと思います。

ありがとうございました。

1 - 2

Post Details

Added on Oct 2 2020
3 comments
764 views