gino icon indicating copy to clipboard operation
gino copied to clipboard

TupleLoader distinct on all included loaders

Open jekel opened this issue 7 years ago • 6 comments

When using TupleLoader with complex query like: (ModelOne.distinct(ModelOne.id).load(...=ModelTwo.distinct...), CustomLoader(query.column_name)) result is incorrect, becase, it assumes that all rows results from each loader are distinct which is not always true

jekel avatar Sep 03 '18 17:09 jekel

Pull Request Test Coverage Report for Build 1148

  • 10 of 10 (100.0%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.004%) to 98.26%

Totals Coverage Status
Change from base Build 1144: 0.004%
Covered Lines: 3954
Relevant Lines: 4024

💛 - Coveralls

coveralls avatar Sep 03 '18 17:09 coveralls

Thanks for the PR!

As you said, it's not always true that all results in sub-loaders are distinct. I also understand from the added test case. But it's also not always true that all results have to be distinct to make this tuple distinct, depending on use cases. For example, I want to iterate through (company, team) pairs instead of company.teams which needs two levels of for-loops. As a tuple loader, each tuple represents a row in the raw result, so the job for it should be to simply convert elements in the raw result to models, and users need to decide how to use them.

wwwjfy avatar Sep 09 '18 15:09 wwwjfy

Hm... i have an idea, TupleLoader must also relay on group by columns inside query to correctly handle all cases

jekel avatar Sep 09 '18 16:09 jekel

By group by, do you mean the group by in the SQL? If so, there won't be duplicated rows. Say group by company.id, we won't get two rows of the same company.id, right?

One way to get what you want may be to check each returned tuple, and save it to context, like ModelLoader does. I'll explore and see if it makes sense.

wwwjfy avatar Sep 16 '18 14:09 wwwjfy

I mean, when you have query result like that, with group by User:

User Company
User_1 Company_1
User_1 Company_2
User2 Company_1

we can automaticaly find it in group by columns and call company attribute to load Companies list, to not explicitly add User.Distinct in query

jekel avatar Sep 20 '18 11:09 jekel

I'm not sure I get what you mean. If it's implemented, what would the syntax be like?

Like this?

TupleLoader(
    (
        User.load(add_company=Company),
        ColumnLoader(query.c.test_column),
    ),
    group_by=User,
)

wwwjfy avatar Sep 20 '18 13:09 wwwjfy