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

multi-column group by in e.g. .classify #501

Open
jonathanstowe opened this issue Jul 31, 2021 · 9 comments
Open

multi-column group by in e.g. .classify #501

jonathanstowe opened this issue Jul 31, 2021 · 9 comments
Assignees
Labels
bug Something isn't working test needed

Comments

@jonathanstowe
Copy link
Contributor

I might be missing something quite obvious but it doesn't seen possible to create a group by with more than one column using .classify.

It's probably implement .reduce as per #61 as .classify doesn't seem the right place to do this.

@FCO
Copy link
Owner

FCO commented Jul 31, 2021

Sorry, I'm on vacation without my computer... but isn't it just a case of returning a list os columns from the classify block?

@jonathanstowe
Copy link
Contributor Author

I thought so too, but no it doesn't work,

Enjoy your holiday.

@FCO
Copy link
Owner

FCO commented Sep 19, 2021

I'm fixing it, I hadn't enough time yet today, but .classify(...).keys is already working on my implementation, now I'm fixing the rest. I hope I have some time tomorrow to play with that.

@FCO FCO self-assigned this Sep 19, 2021
@FCO FCO added bug Something isn't working test needed labels Sep 19, 2021
FCO added a commit that referenced this issue Sep 19, 2021
@FCO
Copy link
Owner

FCO commented Sep 19, 2021

@jonathanstowe could you see if something like this would solve the problem? I still have to add some tests and resolve how Bag and Set should work,

@jonathanstowe
Copy link
Contributor Author

I'll take a look in the morning 👍

@jonathanstowe
Copy link
Contributor Author

I couldn't get it to work, do you have an example?

What I'm really trying to do is illustrated by the SQL:

select a.foo, a.bar, count(*)
from a
join b on b.a_id = a.id
group by a.foo, a.bar;

And I can't for the life of me think of a way to express it.

@FCO FCO added this to To do in Beta Release via automation Oct 15, 2021
@FCO
Copy link
Owner

FCO commented Dec 24, 2022

Now it accepts multiple columns for .classify, but I don't think it will solve your issue... still working on that...

FCO added a commit that referenced this issue Dec 24, 2022
@FCO
Copy link
Owner

FCO commented Dec 24, 2022

@jonathanstowe I think that might be closer to what you want now (but not there yet). .classify now accepts a :&reduce (it still needs to use what-does-it-do (as the other functions)). Here is an example of usage:

➜  Red git:(master) raku -I. -MRed -e '

model Bla { has $.id is serial; has $.value is column }

my $*RED-DB = database "SQLite";
Bla.^create-table; Bla.^create(:value(<test1 test2>.pick)) xx 10;
my $*RED-DEBUG = True;

say Bla.^all.classify({ .value }, :reduce{ .elems })

'
SQL : SELECT
   DISTINCT("bla".value) as "data_1"
FROM
   "bla"
BIND: []
SQL : SELECT
   count('*') as "data_1"
FROM
   "bla"
WHERE
   "bla".value = ?
LIMIT 1
BIND: ["test1"]
SQL : SELECT
   count('*') as "data_1"
FROM
   "bla"
WHERE
   "bla".value = ?
LIMIT 1
BIND: ["test2"]
{test1 => 7, test2 => 3}

@FCO
Copy link
Owner

FCO commented Jan 10, 2023

Maybe we could try something like:

Model.^all.classify({ MyClassifiableClass.new: .foo, .bar }).Bag

(https://glot.io/snippets/gh56566qcg)

and create that class specifically for Red. That class could also be used to make map return objects, maybe something like:

say a: { .a }, b: { .b } for Model.^all.map: { MyNewClass.new: .a, .b }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working test needed
Projects
Development

No branches or pull requests

2 participants