Orientdb: Nice to have feature: support RAND() function

Created on 9 Jan 2014  路  21Comments  路  Source: orientechnologies/orientdb

Hello,

What do you think about supporting the RAND function?
It's absolutely not critical, just a nice to have feature, especially for random requests.

Select a random vertex:
SELECT FROM X ORDER BY RAND() LIMIT 1

Select randomly one vertex from two:
SELECT FROM X WHERE RAND() > 0.5

Generate a random value:
SELECT RAND()*100

enhancement

All 21 comments

I agree, I was surprised when I didn't find this feature

Very easy to implement, unfortunately we're in RC stage, so we can't add new features now. This will go for 2.1

I'm currently evaluating OrientDB and this feature is a blocker to me. Any chance this could make into v2.1?

@Ivca if it's very easy to implement, maybe you could could give me a hint on how to do it and I would do it myself.

Cheers

@tsouza I think this could be considered a duplicate of #2573 I am currently trying to bind all the Java Math methods into sql functions. I think I will create a PR within the weekend.

@fabriziofortino extending orientdb function is a great feature! But what I am really looking for is a way to randomly select an element.

Going into details, what I am looking for is a way to randomly select an element from an index with a key range constraint

@fabriziofortino I really love you ;-)

Status? Does exist a way to select random records?

In the meantime, what is the suggested way to get random vertex of specific class?

What is the current situation? I also need this feature too. Thanks !

Any chance we get the RAND() method?

Already available by using math_rand() function and ordering by it. Example:

select a, b, math_rand() as c from X order by c

The function name is math_random()

math_random() or math_rand() doesn't work my suggestion to everybody just make a simple function for generating random number and you are good to go

This is best of both world

Oh wait that's not correct is take a eternity with a db function,

Is there solution of rthis problem out there yet ?

@DarkKnight1992 what do you mean with doesn't work?

@Ivca sorry for the late reply i didn't saw your comment all this time.

but when i said it doesn't work i mean when i tried this it gives me an error all the time

No function with name 'math_random', available names are : [date,encode,dijkstra,traversedelement,decode,bothv,uuid,GETCITYFORESTA,mode,sysdate,GETITEM,LAST_30_DAYS,WHERECOND,shortestpath,UPEVT,symmetricdifference,traversedvertex,if,bothe,unionall,JOINSTR,last,in,coalesce,count,format,concat,list,both,sequence,gremlin,eval,percentile,median,difference,decimal,astar,distance,document,distinct,CONVERTTOARR,sum,out,CREATE_RANDOM,avg,min,traversededge,ifnull,stddev,outv,map,ine,set,oute,intersect,max,UPCOMINGEVENTS,CHECKSTR,label,inv,abs,variance,FNGEODISTANCE,first]
DB name="iconoland"

screenshot 17

Hi @julien-lafont

I guess we missed a little point here... math_random() was implemented in v 3.0, I guess you are trying with v 2.2.x

Thanks

Luigi

Hi @luigidellaquila,

Its nice to know this feature will be available in 3.0 but considering the fact that 3.0 is not production ready yet we can't use this which is extremely important to us before launch. Any estimates when will be released ?

Hi @DarkKnight1992

if you need it in v 2.2, you can easily implement it in javascript:

schermata 2017-11-22 alle 10 44 28

and use it like follows:

select expand(rid) from (
   select @rid, random() as c from v order by c
)

V 3.0 will be released between the end of this year and the begin of next year

Thanks

Luiig

i tired something similar making a random function but that was taking too long for the query to work.
screenshot 18

Well, that's pretty obvious, the query has to apply a random function for all the records in the target and then do a sorting, on a big class it will take time.

If you need a single random record (or a small set of records) maybe you can write a function that:

  • gets the maximum clusterId/s for your target
  • generates a random number
  • loads the record by RID (generated as <clusterId>:<randomNumber>
  • retries in case the record does not exist

For sure this will be much more efficient

thanks

Luigi

I understand but the whole purpose of random function is kinda lost like this since the database will keep growing as of now currently this was applied for class with 75k records and will keep growing exponentially with time.
yeah that will be more efficient for sure but this approach has also one downside only the selected data will be randomize which again looses the whole idea of randomizing records.

Was this page helpful?
0 / 5 - 0 ratings