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 support for index sort order (descending) #27210

Merged
merged 7 commits into from
Jan 22, 2022
Merged

Conversation

roji
Copy link
Member

@roji roji commented Jan 19, 2022

  • Added a new IsDescending Fluent API and Data Annotation.
  • It's implemented as a property on Index and not as an annotation, since it's a core concept. One downside is that it has to be on RuntimeIndex, though it's not a runtime feature (I made it always return Array.Empty).
  • I simplified the implementation: if IsDescending is specified, it must explicitly specify all values for all properties.

/cc @AndriySvyryd would appreciate a good review as this touches upon lots of metadata/model stuff, and is non-annotation array metadata property.

Closes #4150

@roji roji marked this pull request as draft January 19, 2022 00:40
@roji roji marked this pull request as ready for review January 19, 2022 11:20
@smitpatel smitpatel mentioned this pull request Jan 19, 2022
If IsDescending is specified, it must explicitly specify all values for all properties.
@AndriySvyryd
Copy link
Member

I simplified the implementation: if IsDescending is specified, it must explicitly specify all values for all properties.

Are all indexable columns orderable? Couldn't there be some type for which specifying the order could be detrimental, like an encrypted column, for example?

@AndriySvyryd
Copy link
Member

It's implemented as a property on Index and not as an annotation, since it's a core concept. One downside is that it has to be on RuntimeIndex, though it's not a runtime feature (I made it always return Array.Empty).

Throw RuntimeModelMissingData instead

@roji
Copy link
Member Author

roji commented Jan 20, 2022

I simplified the implementation: if IsDescending is specified, it must explicitly specify all values for all properties.

Are all indexable columns orderable? Couldn't there be some type for which specifying the order could be detrimental, like an encrypted column, for example?

That's an interesting question, although note that in the migration SQL we still generate nothing (just as before this feature) unless you use the new API to specify "descending". In other words, the PR forces the user to specify true/false for all columns, but at the migration level we generate only DESC, never ASC (which is the universal default). So if there's some issue with indexes over encrypted columns, that issue already exists today.

Throw RuntimeModelMissingData instead

Yeah, I tried that but RelationalModel accesses that IIndex.IsDescending property when building TableIndex - that seems to be a difference between having it as a real property vs. annotation.

@AndriySvyryd
Copy link
Member

Yeah, I tried that but RelationalModel accesses that IIndex.IsDescending property when building TableIndex - that seems to be a difference between having it as a real property vs. annotation.

Calculate TableIndex.IsDescending lazily using ITableIndex.MappedIndexes.

@AndriySvyryd
Copy link
Member

And don't forget to update RelationalIndexExtensions.AreCompatible


if (index.IsDescending.Any(desc => desc))
{
indexBuilder.IsDescending(index.IsDescending.ToArray());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep indexBuilder updated; Fluent API doesn't guarantee that old builders remain valid.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering about that - saw some other places where it wasn't updated. May be worth doing a pass to check.

@AndriySvyryd
Copy link
Member

Add a test to ConventionDispatcherTest

@roji roji force-pushed the IndexSortOrder branch 2 times, most recently from 6012e3e to acd885d Compare January 21, 2022 16:17
/// </summary>
[DebuggerStepThrough]
public virtual string DisplayName()
=> Name is null ? Properties.Format() : $"'{Name}'";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can call ((IReadOnlyIndex)this).DisplayName() instead of overriding

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that recurses into the same call: the public DisplayName implements the interface on IReadOnlyIndex...

Compare EntityType.DisplayName which happens to be private - there it's safe to do it (and I get an IDE warning with Non-public method 'DisplayName' hides method with default implementation in interface IReadOnlyTypeBase). But with Index we want to call DisplayName() from EntityType.cs, so it needs to be public...

We could probably work around this with something fancy, but is it worth it...

Copy link
Member

@AndriySvyryd AndriySvyryd Jan 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that recurses into the same call: the public DisplayName implements the interface on IReadOnlyIndex...

That's why I said "instead of overriding", i.e. don't have this method on Index at all.

Compare EntityType.DisplayName which happens to be private

It's private by design 😃

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see... So this would have to be done here, but also in EntityType.cs where we call this twice... I dunno, overriding seems a bit nicer even if it duplicates the logic - it's very unlikely to change etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for index ordering (ASC/DESC)
2 participants