multicloud365
  • Home
  • Cloud Architecture
    • OCI
    • GCP
    • Azure
    • AWS
    • IAC
    • Cloud Networking
    • Cloud Trends and Innovations
    • Cloud Security
    • Cloud Platforms
  • Data Management
  • DevOps and Automation
    • Tutorials and How-Tos
  • Case Studies and Industry Insights
    • AI and Machine Learning in the Cloud
No Result
View All Result
  • Home
  • Cloud Architecture
    • OCI
    • GCP
    • Azure
    • AWS
    • IAC
    • Cloud Networking
    • Cloud Trends and Innovations
    • Cloud Security
    • Cloud Platforms
  • Data Management
  • DevOps and Automation
    • Tutorials and How-Tos
  • Case Studies and Industry Insights
    • AI and Machine Learning in the Cloud
No Result
View All Result
multicloud365
No Result
View All Result

Attention-grabbing bug with nondeterministic capabilities

admin by admin
May 15, 2025
in OCI
0
Creation of Code 2024 – Day 1
399
SHARES
2.3k
VIEWS
Share on FacebookShare on Twitter


I used to be engaged on a bit simulation utilizing random numbers and located I used to be getting odd outcomes from my question. After spending some time dissecting it looking for my error I discovered the issue was within the database itself. I used to be capable of replicate the issue on Stay SQL utilizing a easy take a look at case with each 19c and 23ai.

The instance code is meant to roll 8 cube two occasions every. If the primary die is under 4 then I’ll use the results of the second die. In any other case, I’ll preserve the primary roll. The issue is that the CASE assertion at all times returns the second die, whatever the first die consequence. The 2 hyperlinks above will allow you to run the instance your self, however I’ll embody the code and output right here.

First, I set a seed. The seed worth itself isn’t vital; however selecting one permits everybody to get the identical outcomes from the pseudo-random quantity generator in DBMS_RANDOM.

Then, my question generates eight rows of two random values every. The rows are numbered so as by the results of the primary die. The CASE assertion ought to resolve the primary vs second die outcomes. Nonetheless, you will note on line 7, the roll was a 5, which ought to be stored; however the CASE returned the reroll worth as an alternative. Then, on line 8, the primary roll was a 6, and once more, the CASE returned the reroll as an alternative of utilizing that.

SQL> BEGIN
  2      DBMS_RANDOM.seed(0);
  3  END;
  4  /

PL/SQL process efficiently accomplished.

SQL>   SELECT ROW_NUMBER() OVER (ORDER BY first_roll) rn,
  2           first_roll,
  3           reroll,
  4           CASE WHEN first_roll 



I posted this to an Oracle ACE chat to see if anybody else had ever seen something like this. The suggestions I obtained was that it appeared just like the SQL transformation step was "over-zealous" in making an attempt to simplify the expression.

Trying on the inside question, first_roll and reroll are an identical.
They're each the expression FLOOR(DBMS_RANDOM.VALUE * 6) + 1.

So, if we name that expression X, the CASE may then be interpreted as
case when X

For the reason that case returns X it doesn't matter what the situation is, then it is sensible to simplify to simply return X. On this case the X is the reroll.

With out understanding precise the internals of how that transfomation works, I'll assume the reroll worth is at all times used as a result of it's first return worth of the case assertion. To check that principle, I'll invert the logic. As a substitute of checking to make use of the reroll if the first_roll is lower than 4, I'll verify to maintain the first_roll whether it is higher than or equal to 4. Functionally it ought to be the identical if there is no such thing as a bug; however on this case I am making an attempt to check the bug itself.

SQL>   SELECT ROW_NUMBER() OVER (ORDER BY first_roll) rn,
  2           first_roll,
  3           reroll,
  4           CASE WHEN first_roll >= 4 THEN first_roll else reroll  END closing
  5      FROM (
  6                   SELECT FLOOR(DBMS_RANDOM.VALUE * 6) + 1 first_roll, FLOOR(DBMS_RANDOM.VALUE * 6) + 1 reroll
  7                     FROM DUAL
  8               CONNECT BY LEVEL 



It appears like my assumption was right. With the primary roll being the returned worth of the primary WHEN situation - that finally ends up getting used, no matter whether or not the WHEN situation is true or not.

A bug report is being filed internally with Oracle to look into this; however till a repair is made obtainable I rewrote the question like this:

SQL>   SELECT rn, first_roll, reroll, NVL(reroll, first_roll) closing
  2      FROM (SELECT ROW_NUMBER() OVER (ORDER BY first_roll) rn,
  3                   first_roll,
  4                   CASE WHEN first_roll 



Now, as an alternative of rolling each cube each time. I solely reroll if the primary roll warrants it. Thus I've NULL for a reroll if the first_roll is 4 or increased and a reroll worth if the first_roll is 3 or decrease.
Then within the outer-most question, I take advantage of NVL to resolve the distinction. Now, there may be by no means a degree within the question the place transformation logic might be taking a look at two values which might be outlined by the identical expression.

I had some preliminary doubts of whether or not this was a bug or just a misunderstanding. If my expression is deterministic, then it'd make sense for the transformation to exchange my case with a single worth; as a result of a deterministic expression will at all times return the identical factor, it will not matter which worth of two an identical expressions is used, as a result of they're assured to return the identical worth.

However, with non-deterministic capabilities (which dbms_random.worth definitely qualifies as) that transformation is not legitimate. The syntax of every expression is identical; however as a result of first_roll and reroll are nondeterministic they have to be handled as distinct entities. Thus the transformation's simplification is misguided.

At first look, failing a easy less-than verify might appear to be an egregious mistake with the parsing/remodeling steps. Upon evaluation although, I believe it is a uncommon state of affairs the place a question can be constructed as I did. That's, querying a case assertion over a sub-query of an identical, non-deterministic expressions. Whereas it made sense for the needs of my simulation, most enterprise instances will probably be completely different.

I hope this helps if another person runs into an identical transformation error. It was an attention-grabbing train to dig via and I am glad I've a community of individuals to bounce concepts off. Thanks for studying. Questions and feedback, as at all times, are welcome.

Tags: bugFunctionsInterestingnondeterministic
Previous Post

The Cyber Danger Tides Are Turning: RSAC ‘25 And Past

Next Post

Mastering Pandas: Superior Capabilities That Make Information Evaluation Approach Simpler | by Abhinav Kumar N A | Might, 2025

Next Post
Mastering Pandas: Superior Capabilities That Make Information Evaluation Approach Simpler | by Abhinav Kumar N A | Might, 2025

Mastering Pandas: Superior Capabilities That Make Information Evaluation Approach Simpler | by Abhinav Kumar N A | Might, 2025

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Trending

INTEGRATE 2025 Day 2 Highlights

INTEGRATE 2025 Day 2 Highlights

June 11, 2025
Matthew Warner On How MSPs Can Evolve, Scale, And Safe In 2025

Matthew Warner On How MSPs Can Evolve, Scale, And Safe In 2025

May 22, 2025
Azure VM Pricing Defined

Azure VM Pricing Defined

March 24, 2025
Principal Monetary Group will increase Voice Digital Assistant efficiency utilizing Genesys, Amazon Lex, and Amazon QuickSight

Principal Monetary Group will increase Voice Digital Assistant efficiency utilizing Genesys, Amazon Lex, and Amazon QuickSight

May 24, 2025
Empowering YouTube creators with generative AI

Empowering YouTube creators with generative AI

May 17, 2025
Mastering GitHub Actions: Step-by-Step Information to Utilizing a Self-Hosted Runner | by Jack Roper | Jan, 2025

Mastering GitHub Actions: Step-by-Step Information to Utilizing a Self-Hosted Runner | by Jack Roper | Jan, 2025

January 25, 2025

MultiCloud365

Welcome to MultiCloud365 — your go-to resource for all things cloud! Our mission is to empower IT professionals, developers, and businesses with the knowledge and tools to navigate the ever-evolving landscape of cloud technology.

Category

  • AI and Machine Learning in the Cloud
  • AWS
  • Azure
  • Case Studies and Industry Insights
  • Cloud Architecture
  • Cloud Networking
  • Cloud Platforms
  • Cloud Security
  • Cloud Trends and Innovations
  • Data Management
  • DevOps and Automation
  • GCP
  • IAC
  • OCI

Recent News

PowerAutomate to GITLab Pipelines | Tech Wizard

PowerAutomate to GITLab Pipelines | Tech Wizard

June 13, 2025
Runtime is the actual protection, not simply posture

Runtime is the actual protection, not simply posture

June 13, 2025
  • About Us
  • Privacy Policy
  • Disclaimer
  • Contact

© 2025- https://multicloud365.com/ - All Rights Reserved

No Result
View All Result
  • Home
  • Cloud Architecture
    • OCI
    • GCP
    • Azure
    • AWS
    • IAC
    • Cloud Networking
    • Cloud Trends and Innovations
    • Cloud Security
    • Cloud Platforms
  • Data Management
  • DevOps and Automation
    • Tutorials and How-Tos
  • Case Studies and Industry Insights
    • AI and Machine Learning in the Cloud

© 2025- https://multicloud365.com/ - All Rights Reserved