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 Table.select() to enhance InlineDerivedTable #13066

Closed
1 task
lukaseder opened this issue Feb 14, 2022 · 1 comment
Closed
1 task

Add Table.select() to enhance InlineDerivedTable #13066

lukaseder opened this issue Feb 14, 2022 · 1 comment

Comments

@lukaseder
Copy link
Member

lukaseder commented Feb 14, 2022

The InlineDerivedTable syntax Table.where(Condition) has been implemented for convenience, to quickly create a derived table from a Table and a WHERE clause, possibly inlining the WHERE clause to the surrounding query where appropriate.

Apart from allowing users to use this feature, we're using it internalls when creating parent() or child() relationships from ForeignKey or from TableRecord references:

Result<AuthorRecord> authors = ...
ctx.selectFrom(authors.children(BOOK_AUTHOR_ID)).fetch();

Behind the scenes, that's just:

ctx.selectFrom(BOOK.where(BOOK.AUTHOR_ID.in(authors.stream().map(r -> r.get(AUTHOR.ID)).toList()))).fetch();

For any similar use-case where a Table::where clause is useful in terms of dynamic SQL (i.e. coupling the Table with a Condition), it might be equally useful to also provide:

  • select()

Other clauses also seem to be useful, but many of them enforce the derived table, rather than making it inlinable:

  • connectBy() and startWith()
  • groupBy() and having()
  • window() and qualify()
  • selectDistinct()
  • orderBy()
  • offset() and limit() and seek()
  • union() and intersect() and except()

Depending on the expressions in the select() clause, the InlineDerivedTable would still have to be enforced as a derived table, rather than remaining inlinable. Such expressions include:

  • Aggregate functions
  • Window functions
  • Whenever the expression is used in JOIN .. ON or JOIN .. USING clauses

The select() clause can be used for this MULTISET convenience feature: #13063.

@lukaseder lukaseder added this to the Version 3.17.0 milestone Feb 14, 2022
@lukaseder lukaseder changed the title Add Table.select(), orderBy(), limit() to enhance InlineDerivedTable Add Table.select() to enhance InlineDerivedTable Feb 14, 2022
@lukaseder lukaseder added this to To do in 3.19 Other improvements via automation Jun 1, 2023
@lukaseder
Copy link
Member Author

I'm closing this as won't fix. I already have mixed feelings about the InlineDerivedTable feature per se. It's a power user feature for users of dynamic SQL. It works because the WHERE clause doesn't affect the projection. It only produces a view of the same row type as the original table.

This is different with select(), where a different row type is desired. The resulting table has nothing to do with the original one. Columns can be added / removed. So the type of Table.select() would just be Table<?>, which would render the feature useless, compared to e.g. an actual derived table.

There had been other clause suggestions, where this problem wouldn't appear, including:

  • qualify()
  • orderBy()
  • offset() and limit()
  • distinct()
  • union(), except(), and intersect()

But the utility of any of these is questionable.

The original idea appeared in this feature here: #13069, which has been rejected in the meantime.

I'm closing this as won't fix.

3.19 Other improvements automation moved this from To do to Done Jun 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
Development

No branches or pull requests

1 participant