Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add EF.Functions.Random() #16141

Closed
Tracked by #22950
max619 opened this issue Jun 18, 2019 · 8 comments · Fixed by #23145
Closed
Tracked by #22950

Add EF.Functions.Random() #16141

max619 opened this issue Jun 18, 2019 · 8 comments · Fixed by #23145
Assignees
Labels
area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. community-contribution customer-reported good first issue This issue should be relatively straightforward to fix. type-enhancement
Milestone

Comments

@max619
Copy link

max619 commented Jun 18, 2019

Is it possible to retrive random sequence of entities using mysql rnd() function and how to do it?

Obviously OrderBy(x=>Guid.NewGuid()) is not working as referenced here

@divega
Copy link
Contributor

divega commented Jun 20, 2019

@max619, According to the message @yukozh wrote when that issue was closed, Pomelo provider would need to support translating some method to the rand() function on the server. I am not if this is supported but it would be better to ask in Pomelo.EntityFrameworkCore.MySql.

@divega
Copy link
Contributor

divega commented Jun 20, 2019

@maumar I had a note in triage to ask you to find out if we had a spec test for this.

However, based on PomeloFoundation/Pomelo.EntityFrameworkCore.MySql#265, it seems that OrderBy(x=>Guid.NewGuid()) not working to randomize the order on MySQL is by design, and you need to write a different query. Not sure if there is a way we could write a spec test that accommodates this. Thoughts?

@divega
Copy link
Contributor

divega commented Jun 24, 2019

Talked to @maumar offline. We don't have spec test for this. in principle, it is probably not a good candidate for a spec test given that:

  1. Obtaining randomized order is apparently going to require different queries for different databases
  2. There is no good way to verify that the order obtained is random

That said, if we think of spec tests as a check list of high level functionality a provider should supply, then it seems ok to have an abstract test and let providers override it. Let's decide what to do with this on next triage.

@timia2109
Copy link

Why not add the Method DbContext.Functions.Random()?

In my mind this should be included there are many use-cases you need this.

@bricelam
Copy link
Contributor

@timia2109 Agreed. I had the same thought in bricelam/EFCore.SqliteEx#10

@bricelam
Copy link
Contributor

Re-opening to track adding adding a standard Random function for providers to implement. Nearly all of them return a floating-point value between 0 and 1, so I think we should use that as the spec.

Provider SQL
SQL Server RAND()
SQLite abs(random() / 9223372036854775807.0)
Cosmos RAND()
PostgreSQL random()
MySQL RAND()
Oracle DBMS_RANDOM.VALUE
DB2 RAND()
Firebird RAND()

@bricelam bricelam reopened this Jul 30, 2020
@bricelam bricelam changed the title OrderBy rnd() Add EF.Functions.Random() Jul 30, 2020
@bricelam bricelam added good first issue This issue should be relatively straightforward to fix. type-enhancement and removed closed-external labels Jul 30, 2020
@roji
Copy link
Member

roji commented Jul 31, 2020

Opened npgsql/efcore.pg#1457 to track on the Npgsql side.

@ajcvickers ajcvickers added this to the Backlog milestone Jul 31, 2020
@ajcvickers ajcvickers modified the milestones: Backlog, 6.0.0 Nov 2, 2020
smitpatel pushed a commit to RaymondHuy/efcore that referenced this issue Dec 22, 2020
@ghost ghost closed this as completed in #23145 Dec 22, 2020
ghost pushed a commit that referenced this issue Dec 22, 2020
@smitpatel smitpatel added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Dec 22, 2020
@dbrownems
Copy link

Note that mapping to RAND for SQL Server will not work for ordering, as RAND() is evaluated only once per query, not on each row (unless RAND() appears in the SELECT clause). There are only a few TSQL functions are evaluated for each row if they don't take an argument from the row. You could use something rand(checksum(newid())) generate a random(ish?) value that can be used for sorting.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. community-contribution customer-reported good first issue This issue should be relatively straightforward to fix. type-enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants