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

feat: Allow field indexing by name in PatchSchema #1810

Merged

Conversation

AndrewSisley
Copy link
Contributor

Relevant issue(s)

Resolves #1169

Description

Allow field indexing by name in PatchSchema.

Looks like I got distracted part way through adding this test and forgot to finish it :)
No point doing the same work twice, and it is about to gain an extra usage
@AndrewSisley AndrewSisley added feature New feature or request area/schema Related to the schema system action/no-benchmark Skips the action that runs the benchmark. labels Aug 23, 2023
@AndrewSisley AndrewSisley added this to the DefraDB v0.7 milestone Aug 23, 2023
@AndrewSisley AndrewSisley requested a review from a team August 23, 2023 18:33
@AndrewSisley AndrewSisley self-assigned this Aug 23, 2023
@codecov
Copy link

codecov bot commented Aug 23, 2023

Codecov Report

Patch coverage: 100.00% and project coverage change: -0.03% ⚠️

Comparison is base (34329fd) 75.72% compared to head (fae2602) 75.69%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop    #1810      +/-   ##
===========================================
- Coverage    75.72%   75.69%   -0.03%     
===========================================
  Files          209      209              
  Lines        22211    22263      +52     
===========================================
+ Hits         16819    16851      +32     
- Misses        4227     4242      +15     
- Partials      1165     1170       +5     
Flag Coverage Δ
all-tests 75.69% <100.00%> (-0.03%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Changed Coverage Δ
db/errors.go 81.18% <100.00%> (+0.43%) ⬆️
db/schema.go 82.86% <100.00%> (+4.81%) ⬆️

... and 6 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 34329fd...fae2602. Read the comment docs.

db/schema.go Outdated

desc := collectionsByName[splitPath[schemaNamePathIndex]]
var index string
if index == "" {
Copy link
Collaborator

Choose a reason for hiding this comment

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

todo: This if will always be true. You should probably remove it.

Copy link
Contributor Author

@AndrewSisley AndrewSisley Aug 24, 2023

Choose a reason for hiding this comment

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

Cheers, I moved this block around a bit and missed this silliness.

  • Remove extra check

db/schema.go Outdated
@@ -171,23 +181,82 @@ func substituteSchemaPatch(
patch jsonpatch.Patch,
collectionsByName map[string]client.CollectionDescription,
) (jsonpatch.Patch, error) {
fieldIndexesByName := make(map[string]map[string]int, len(collectionsByName))
Copy link
Collaborator

Choose a reason for hiding this comment

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

suggestion: The naming of this and colFieldsIndexesByName makes understanding the added code bellow confusing. I would suggest a rename.

fieldIndexesByName -> fieldIndexesByCollection

colFieldIndexesByName -> fieldIndexesByName

Copy link
Contributor Author

@AndrewSisley AndrewSisley Aug 24, 2023

Choose a reason for hiding this comment

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

I like that, will rename throughout - thanks Fred.

  • Rename map

db/schema.go Outdated
Comment on lines 185 to 191
for colName, col := range collectionsByName {
colFieldIndexesByName := make(map[string]int, len(col.Schema.Fields))
fieldIndexesByName[colName] = colFieldIndexesByName
for i, field := range col.Schema.Fields {
colFieldIndexesByName[field.Name] = i
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

suggestion: This can be simplified to

for colName, col := range collectionsByName {
    fieldIndexesByCollection[colName] = make(map[string]int, len(col.Schema.Fields))
    for i, field := range col.Schema.Fields {
        fieldIndexesByCollection[colName][field.Name] = i
    }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is one less line, but an extra call. I prefer the current, there is actually less to read even though it takes up more eye-space.

db/schema.go Outdated
Comment on lines 238 to 243
} else {
fieldIndexesByName[desc.Name] = map[string]int{
// The DocKey field is always at the zero index and we need to account for it
request.KeyFieldName: 0,
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

question: If the provided collection doesn't exist, shouldn't we return an error here?

Copy link
Contributor Author

@AndrewSisley AndrewSisley Aug 24, 2023

Choose a reason for hiding this comment

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

This is me falling into the trap of coding for the future and tolerating dead code to do so.

I was concerned about when PatchSchema gains the ability to add new collections, however this block sits within an isFieldOrInner check, and so when the time comes it would be a poor and surprising location to do this.

I will just delete the else block.

  • remove dead code

Copy link
Collaborator

Choose a reason for hiding this comment

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

What about the option to return an error in this isFieldOrInner block? I understand potentially adding schemas from a patch operation but adding a field or changing the kind of a field in a schema that doesn't exist should probably be an error. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Returning an error here would be dead code, there would be no way to reach it. If/when we allow new schema to be added via PatchSchema then we can add some logic somewhere to handle it, it would likely be located somewhere else.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Makes sense.

Copy link
Collaborator

@fredcarle fredcarle left a comment

Choose a reason for hiding this comment

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

LGTM!

@AndrewSisley AndrewSisley merged commit 5240086 into sourcenetwork:develop Aug 24, 2023
12 checks passed
@AndrewSisley AndrewSisley deleted the 1169-field-index-substitution branch August 24, 2023 17:45
shahzadlone pushed a commit to shahzadlone/defradb that referenced this pull request Feb 23, 2024
## Relevant issue(s)

Resolves sourcenetwork#1169

## Description

Allow field indexing by name in PatchSchema.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
action/no-benchmark Skips the action that runs the benchmark. area/schema Related to the schema system feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow string/named substitution of field indexes
2 participants