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

fix: Handle multiple nil values on unique indexed fields #2178

Merged
merged 4 commits into from
Jan 10, 2024

Conversation

islamaliev
Copy link
Contributor

Relevant issue(s)

Resolves #2174 #2175

Description

Forbid nil values for fields that are unique-indexed.

Tasks

  • I made sure the code is well commented, particularly hard-to-understand areas.
  • I made sure the repository-held documentation is changed accordingly.
  • I made sure the pull request title adheres to the conventional commit style (the subset used in the project can be found in tools/configs/chglog/config.yml).
  • I made sure to discuss its limitations such as threats to validity, vulnerability to mistake and misuse, robustness to invalidation of assumptions, resource requirements, ...

How has this been tested?

Integration and unit tests

Specify the platform(s) on which this was tested:

  • MacOS

@islamaliev islamaliev added the bug Something isn't working label Jan 9, 2024
@islamaliev islamaliev added this to the DefraDB v0.9 milestone Jan 9, 2024
@islamaliev islamaliev self-assigned this Jan 9, 2024
Copy link

codecov bot commented Jan 9, 2024

Codecov Report

Attention: 5 lines in your changes are missing coverage. Please review.

Comparison is base (eea7699) 74.13% compared to head (a21a95c) 74.16%.

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##           develop    #2178      +/-   ##
===========================================
+ Coverage    74.13%   74.16%   +0.02%     
===========================================
  Files          256      256              
  Lines        25368    25372       +4     
===========================================
+ Hits         18806    18815       +9     
+ Misses        5279     5276       -3     
+ Partials      1283     1281       -2     
Flag Coverage Δ
all-tests 74.16% <66.67%> (+0.02%) ⬆️

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

Files Coverage Δ
db/collection_index.go 92.59% <80.00%> (-0.07%) ⬇️
db/errors.go 67.48% <0.00%> (ø)
db/index.go 62.83% <66.67%> (+1.41%) ⬆️

... and 5 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 eea7699...a21a95c. Read the comment docs.

@islamaliev islamaliev changed the title Forbid nil values on unique indexed fields fix: Forbid nil values on unique indexed fields Jan 9, 2024
Copy link
Contributor

@AndrewSisley AndrewSisley left a comment

Choose a reason for hiding this comment

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

This does feel like the wrong approach. I think users should still be allowed nil values in indexed fields, especially considering that the GQL types are nillable (Int vs Int!).

The SQL standards for this allow multiple nil values in a column with a unique constraint. Microsoft SQL Server naturally ignores this, and only permits a single nil value.

https://stackoverflow.com/questions/767657/how-do-i-create-a-unique-constraint-that-also-allows-nulls (check top answer).

@islamaliev
Copy link
Contributor Author

added a task for multiple nil values #2180

Copy link
Contributor

@AndrewSisley AndrewSisley left a comment

Choose a reason for hiding this comment

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

Looks good, and I'm glad not much production code was required. I do think more tests are required though.

testUtils.ExecuteTestCase(t, test)
}

func TestUniqueIndexCreate_UponAddingDocWithExistingNilValue_ReturnError(t *testing.T) {
Copy link
Contributor

@AndrewSisley AndrewSisley Jan 9, 2024

Choose a reason for hiding this comment

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

todo: Can you please add a test that shows a single document with a nil indexed value succeeding? This way if this (multiple nil values) test is deleted (e.g. when switching to allowing multiple nils) we do not lose coverage of the single nil happy-case.

todo: Can you please add a test with a filter eq nil?

todo: Can you please add some tests for composite indexes with all-nil and mixed nil/non-nil values?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

test for a single nil value was always there

func TestUnique_IfIndexedFieldIsNil_StoreItAsNil(t *testing.T) {

Copy link
Contributor

@AndrewSisley AndrewSisley Jan 9, 2024

Choose a reason for hiding this comment

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

Could you please add it as an integration test please? It will make it much easier to discover, and it will be able to take advantage of all our integration test related perks (such as mutation and client types, change detection, etc)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the current test already includes a successful addition of a document with a nil value:

testUtils.CreateDoc{
	CollectionID: 0,
	Doc: `
		{
			"name":	"Keenan"
		}`,
	},
testUtils.CreateDoc{
	CollectionID: 0,
	Doc: `
		{
			"name":	"Andy"
		}`,
	ExpectedError: db.NewErrCanNotIndexNonUniqueField("bae-2159860f-3cd1-59de-9440-71331e77cbb8", "age", nil).Error(),
},

Adding another one would be redundant.

I will add a test with filtering on nil value.

todo: Can you please add some tests for composite indexes with all-nil and mixed nil/non-nil values?

composite indexes is WIP on another branch.

Copy link
Contributor

Choose a reason for hiding this comment

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

the current test already includes a successful addition of a document with a nil value:

Yes I saw that, but an independent successful test would be useful. If this (multiple nil values) test is deleted (e.g. when switching to allowing multiple nils) we do not lose coverage of the single nil happy-case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added an explicit test

db/index.go Show resolved Hide resolved
db/errors.go Show resolved Hide resolved
@islamaliev islamaliev force-pushed the fix/unique-index-on-nil-field branch from 91fa97d to a21a95c Compare January 9, 2024 21:46
@islamaliev islamaliev changed the title fix: Forbid nil values on unique indexed fields fix: Handle multiple nil values on unique indexed fields Jan 9, 2024
Copy link
Contributor

@AndrewSisley AndrewSisley left a comment

Choose a reason for hiding this comment

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

LGTM :)

@islamaliev islamaliev merged commit ffd7f0b into develop Jan 10, 2024
27 of 29 checks passed
@islamaliev islamaliev deleted the fix/unique-index-on-nil-field branch January 10, 2024 14:42
@islamaliev islamaliev linked an issue Jan 10, 2024 that may be closed by this pull request
shahzadlone pushed a commit to shahzadlone/defradb that referenced this pull request Jan 22, 2024
…rk#2178)

## Relevant issue(s)

Resolves sourcenetwork#2174 sourcenetwork#2175

## Description

This change fixes how nil values are handled on unique indexes
shahzadlone pushed a commit to shahzadlone/defradb that referenced this pull request Feb 23, 2024
…rk#2178)

## Relevant issue(s)

Resolves sourcenetwork#2174 sourcenetwork#2175

## Description

This change fixes how nil values are handled on unique indexes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
2 participants