I've come across some code in the application I'm working on that makes a database call merely to call the ORA_HASH
function (documentation) on a UUID string. The reason it's doing this is that it needs the value to make a service call to another system that appears to use ORA_HASH
for partitioning.
I would like to know the algorithm ORA_HASH
uses so that I can re-implement it to make a similar service call for an application that won't have access to a real database, let alone Oracle. I've only been able to find what amounts to Oracle API documentation so far.
Just to be super clear: I need to clone ORA_HASH
because that's what another system that's outside of my control uses, and I need to integrate with that system. Yes, it would nice if could use a really standard algorithm, like MD5, but I can't, unless that's what ORA_HASH
is under the covers.
Answers or comments that propose the use of a hash algorithm besides ORA_HASH
are not helpful. This question is specifically about ORA_HASH
, not hashing or partitioning in general.
This doesn't answer the OP question of the actual algo behind ora_hash. This is just an example of using ora_hash in pl/sql (answering @JonHeller comment):
The function:
And using it:
Dbms Output:
You can also mess around with RESULT_CACHE or other techniques to try to speed things up even more.
Its very fast already. For example, calling the function 1 million times on a large table:
So basically 100k rows per second, that includes any context switches you may be worried about.
If you need to reproduce ORA_HASH because of performance, I would suggest that your performance bottleneck may be elsewhere.
Well, if it "appears to use" then it makes sense to do a bit of reverse engineering and check what exactly is called and disassemble code of the function.
If you, however, want to dive into Oracle internals then following may help.
First of all, you have to figure out what internal C function is called. To do that you can execute some long running code in one session. I did run this
It can be PL/SQL code as well, you just need to make sure that you constantly call ora_hash.
While it's running
If you on Windows then you can use ostackprof by TANEL PODER (https://blog.tanelpoder.com/2008/10/31/advanced-oracle-troubleshooting-guide-part-9-process-stack-profiling-from-sqlplus-using-ostackprof/)
If you on *nix then you can use dtrace (http://www.oracle.com/technetwork/articles/servers-storage-dev/dtrace-on-linux-1956556.html), Flame Graph (usage scenario https://blog.dbi-services.com/oracle-database-multilingual-engine-mle/)
I tested on Windows and looks like that ora_hash is ...->evaopn2()->evahash()->...
Now let's google for evahash. We got extremely lucky because there is a header file on official site https://oss.oracle.com/projects/ocfs-tools/src/branches/new-dir-format/libocfs/Linux/inc/ocfshash.h with link to evahash.
And finally there is page with actual C code http://burtleburtle.net/bob/hash/evahash.html
So far so good, we remember that we can use external C function in Oracle if we build it into library (DLL on Windows).
For example on my Win x64 if I change function signature to
it can be successfully executed from Oracle. But, as you see, signature a bit differs from ora_hash in Oracle. This function accepts value, its length and initval (may be seed) while signature in Oracle is ora_hash(expr, max_bucket, seed_value).
Let's try to test Oracle
C
None of the numbers matches. So what is the problem? ora_hash accepts parameters of almost any type (for example
select ora_hash(sys.odcinumberlist(1,2,3)) from dual
) while C function accepts value as array of bytes. This means that some conversion happens before function call. Thus before using mentioned C hash function you have to figure out how actual value is transformed before passing to it.You can proceed with reverse engineering of Oracle binaries using IDA PRO + hex rays but that may take days. Not to mention platform specific details.
So if you want to imitate ora_hash, the easiest option would be to install Oracle express edition and use it to call ora_hash.
I hope that was interesting. Good luck.
Update
ora_hash and dbms_utility.get_hash_value can be mapped to each other (see https://jonathanlewis.wordpress.com/2009/11/21/ora_hash-function/)
If we unwrap package body of dbms_utility we will see following declaration
and
Let's google for
icd_hash
and we can find that it's mapped to_psdhsh
(https://yurichev.com/blog/50/). Now it's time to disassemble oracle.exe and extract code for_psdhsh
from it. Maybe I'll spend some time on this next year.