diff options
author | Svilen Mihaylov <svilen.mihaylov@mongodb.com> | 2023-04-03 21:29:10 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2023-04-03 23:26:27 +0000 |
commit | b6fcc56bb8f9d27cde832bcc1657c7f7379360f3 (patch) | |
tree | 870fefaeb3211ef6e2397e20769b51a50bcff568 | |
parent | 0abe5e7b8357aae4b5f324846b7bc0b53abeb442 (diff) | |
download | mongo-b6fcc56bb8f9d27cde832bcc1657c7f7379360f3.tar.gz |
SERVER-75586 Split sort_dotted_paths.js according to functionality exercised
-rw-r--r-- | jstests/core/query/sort/sort_dotted_paths.js | 59 | ||||
-rw-r--r-- | jstests/core/query/sort/sort_dotted_paths_numeric.js | 66 | ||||
-rw-r--r-- | jstests/core/query/sort/sort_dotted_paths_parallel_arrays.js | 47 |
3 files changed, 113 insertions, 59 deletions
diff --git a/jstests/core/query/sort/sort_dotted_paths.js b/jstests/core/query/sort/sort_dotted_paths.js index 2db3ba5683c..e927b58add8 100644 --- a/jstests/core/query/sort/sort_dotted_paths.js +++ b/jstests/core/query/sort/sort_dotted_paths.js @@ -1,13 +1,5 @@ /** * Test sorting with dotted field paths. - * - * This test expects some statements to error, which will cause a transaction (if one is open) - * to abort entirely. Thus, we add the "does_not_support_transactions" tag to prevent this test - * from being run in various the multi-statement passthrough testsuites. - * - * @tags: [ - * does_not_support_transactions, - * ] */ (function() { "use strict"; @@ -41,15 +33,6 @@ testSortAndSortWithLimit( {a: -1, _id: 1}, [{_id: 7}, {_id: 4}, {_id: 1}, {_id: 6}, {_id: 3}, {_id: 8}, {_id: 2}, {_id: 5}, {_id: 9}]); -// Test out sort({"a.0":1}) on a collection of documents where field 'a' is a mix of different -// types (arrays of varying size for some documents, non-arrays for other documents). -testSortAndSortWithLimit( - {"a.0": 1, _id: 1}, - [{_id: 9}, {_id: 1}, {_id: 2}, {_id: 3}, {_id: 4}, {_id: 5}, {_id: 8}, {_id: 6}, {_id: 7}]); -testSortAndSortWithLimit( - {"a.0": -1, _id: 1}, - [{_id: 7}, {_id: 6}, {_id: 1}, {_id: 2}, {_id: 3}, {_id: 4}, {_id: 5}, {_id: 8}, {_id: 9}]); - // Test out sort({a:1,b:1}) on a collection where a is an array for some documents and b is an array // for other documents. assert(coll.drop()); @@ -78,21 +61,10 @@ testSortAndSortWithLimit( {a: -1, b: -1, _id: 1}, [{_id: 5}, {_id: 6}, {_id: 8}, {_id: 9}, {_id: 2}, {_id: 4}, {_id: 7}, {_id: 1}, {_id: 3}]); -// Verify that sort({a:1,b:1}) fails with a "parallel arrays" error when there is at least one -// document where both a and b are arrays. assert(coll.drop()); assert.commandWorked( coll.insert([{_id: 1, a: [], b: 1}, {_id: 2, a: 1, b: []}, {_id: 3, a: [], b: []}])); -assert.commandFailedWithCode(db.runCommand({find: coll.getName(), sort: {a: 1, b: 1}}), - [ErrorCodes.BadValue, ErrorCodes.CannotIndexParallelArrays]); -assert.commandFailedWithCode(db.runCommand({find: coll.getName(), sort: {_id: 1, a: 1, b: 1}}), - [ErrorCodes.BadValue, ErrorCodes.CannotIndexParallelArrays]); -assert.commandFailedWithCode(db.runCommand({find: coll.getName(), sort: {a: 1, _id: 1, b: 1}}), - [ErrorCodes.BadValue, ErrorCodes.CannotIndexParallelArrays]); -assert.commandFailedWithCode(db.runCommand({find: coll.getName(), sort: {a: 1, b: 1, _id: 1}}), - [ErrorCodes.BadValue, ErrorCodes.CannotIndexParallelArrays]); - // Verify that sort({a:1,b:1}) does not fail with a "parallel arrays" error when documents where // both a and b are arrays are filtered out. const filter1 = { @@ -173,22 +145,6 @@ testSortAndSortWithLimit( {"a.c": -1, a: -1, _id: 1}, [{_id: 8}, {_id: 5}, {_id: 9}, {_id: 4}, {_id: 6}, {_id: 7}, {_id: 3}, {_id: 2}, {_id: 1}]); -// Verify that sort({"a.b":1,"a.c":1}) fails with a "parallel arrays" error when there is at least -// one document where both a.b and a.c are arrays. -assert(coll.drop()); -assert.commandWorked(coll.insert([{_id: 1, a: {b: [1, 2], c: [3, 4]}}])); - -assert.commandFailedWithCode(db.runCommand({find: coll.getName(), sort: {"a.b": 1, "a.c": 1}}), - [ErrorCodes.BadValue, ErrorCodes.CannotIndexParallelArrays]); - -// Verify that sort({"a.b":1,"c.d":1}) fails with a "parallel arrays" error when there is at least -// onw document where both a.b and c.d are arrays. -assert(coll.drop()); -assert.commandWorked(coll.insert({a: {b: [1, 2]}, c: {d: [3, 4]}})); - -assert.commandFailedWithCode(db.runCommand({find: coll.getName(), sort: {"a.b": 1, "c.d": 1}}), - [ErrorCodes.BadValue, ErrorCodes.CannotIndexParallelArrays]); - // More tests for a sort pattern that contains two paths of length 2 with a common prefix. assert(coll.drop()); assert.commandWorked(coll.insert([ @@ -231,21 +187,6 @@ testSortAndSortWithLimit( {"a.b": -1, a: -1, _id: 1}, [{_id: 3}, {_id: 9}, {_id: 8}, {_id: 5}, {_id: 1}, {_id: 4}, {_id: 6}, {_id: 2}, {_id: 7}]); -// Test out sort({"a.0.b":1}) on a collection of documents where field "a" and sub-field "b" are -// a mix of different types. -testSortAndSortWithLimit( - {"a.0.b": 1, _id: 1}, - [{_id: 7}, {_id: 1}, {_id: 2}, {_id: 5}, {_id: 8}, {_id: 3}, {_id: 4}, {_id: 6}, {_id: 9}]); -testSortAndSortWithLimit( - {"a.0.b": 1, _id: -1}, - [{_id: 7}, {_id: 1}, {_id: 5}, {_id: 2}, {_id: 8}, {_id: 4}, {_id: 3}, {_id: 6}, {_id: 9}]); -testSortAndSortWithLimit( - {"a.0.b": -1, _id: 1}, - [{_id: 9}, {_id: 3}, {_id: 6}, {_id: 1}, {_id: 4}, {_id: 5}, {_id: 8}, {_id: 2}, {_id: 7}]); -testSortAndSortWithLimit( - {"a.0.b": -1, _id: -1}, - [{_id: 9}, {_id: 3}, {_id: 6}, {_id: 4}, {_id: 1}, {_id: 8}, {_id: 5}, {_id: 2}, {_id: 7}]); - // Tests for a sort pattern that contains two paths of length 2 that do not have a common prefix. assert(coll.drop()); assert.commandWorked(coll.insert([ diff --git a/jstests/core/query/sort/sort_dotted_paths_numeric.js b/jstests/core/query/sort/sort_dotted_paths_numeric.js new file mode 100644 index 00000000000..ce1cc6eccce --- /dev/null +++ b/jstests/core/query/sort/sort_dotted_paths_numeric.js @@ -0,0 +1,66 @@ +/** + * Test sorting with dotted field paths and numeric path components. + */ +(function() { +"use strict"; + +const coll = db.sort_dotted_paths_positional; +coll.drop(); + +function testSortAndSortWithLimit(sortPattern, expectedIds) { + assert.eq(expectedIds, coll.find({}, {_id: 1}).sort(sortPattern).toArray()); + assert.eq(expectedIds, coll.find({}, {_id: 1}).sort(sortPattern).limit(500).toArray()); +} + +// Basic tests to verify that sorting deals with undefined, null, missing fields, and nested arrays +// as expected. +assert.commandWorked(coll.insert([ + {_id: 1, a: 1}, + {_id: 2, a: undefined}, + {_id: 3, a: null}, + {_id: 4, a: {}}, + {_id: 5, a: []}, + {_id: 6, a: [1]}, + {_id: 7, a: [[1]]}, + {_id: 8}, + {_id: 9, a: [undefined]} +])); + +// Test out sort({"a.0":1}) on a collection of documents where field 'a' is a mix of different +// types (arrays of varying size for some documents, non-arrays for other documents). +testSortAndSortWithLimit( + {"a.0": 1, _id: 1}, + [{_id: 9}, {_id: 1}, {_id: 2}, {_id: 3}, {_id: 4}, {_id: 5}, {_id: 8}, {_id: 6}, {_id: 7}]); +testSortAndSortWithLimit( + {"a.0": -1, _id: 1}, + [{_id: 7}, {_id: 6}, {_id: 1}, {_id: 2}, {_id: 3}, {_id: 4}, {_id: 5}, {_id: 8}, {_id: 9}]); + +// More tests for a sort pattern that contains two paths of length 2 with a common prefix. +assert(coll.drop()); +assert.commandWorked(coll.insert([ + {_id: 1, a: [{b: [4, 1], c: 1}, {b: [1, 5], c: 2}]}, + {_id: 2, a: [{b: 2, c: [1, 3]}, {b: 4, c: [2, 4]}]}, + {_id: 3, a: [{b: [6, 4], c: 1}, {b: [9, 7], c: 2}]}, + {_id: 4, a: [{b: 4, c: [1, 2]}, {b: 5, c: [3, 2]}]}, + {_id: 5, a: [{b: [2, 3], c: 1}, {b: [7, 6], c: 3}]}, + {_id: 6, a: [{b: 5, c: []}, {b: 3, c: 3}]}, + {_id: 7, a: [{b: [], c: 2}, {b: 2, c: 3}]}, + {_id: 8, a: [{b: 3, c: [2]}, {b: 8, c: [3]}]}, + {_id: 9, a: [{b: [8], c: 2}, {b: [6], c: 3}]}, +])); + +// Test out sort({"a.0.b":1}) on a collection of documents where field "a" and sub-field "b" are +// a mix of different types. +testSortAndSortWithLimit( + {"a.0.b": 1, _id: 1}, + [{_id: 7}, {_id: 1}, {_id: 2}, {_id: 5}, {_id: 8}, {_id: 3}, {_id: 4}, {_id: 6}, {_id: 9}]); +testSortAndSortWithLimit( + {"a.0.b": 1, _id: -1}, + [{_id: 7}, {_id: 1}, {_id: 5}, {_id: 2}, {_id: 8}, {_id: 4}, {_id: 3}, {_id: 6}, {_id: 9}]); +testSortAndSortWithLimit( + {"a.0.b": -1, _id: 1}, + [{_id: 9}, {_id: 3}, {_id: 6}, {_id: 1}, {_id: 4}, {_id: 5}, {_id: 8}, {_id: 2}, {_id: 7}]); +testSortAndSortWithLimit( + {"a.0.b": -1, _id: -1}, + [{_id: 9}, {_id: 3}, {_id: 6}, {_id: 4}, {_id: 1}, {_id: 8}, {_id: 5}, {_id: 2}, {_id: 7}]); +})(); diff --git a/jstests/core/query/sort/sort_dotted_paths_parallel_arrays.js b/jstests/core/query/sort/sort_dotted_paths_parallel_arrays.js new file mode 100644 index 00000000000..145f2ba3b57 --- /dev/null +++ b/jstests/core/query/sort/sort_dotted_paths_parallel_arrays.js @@ -0,0 +1,47 @@ +/** + * Test sorting with dotted field paths. + * + * This test expects some statements to error, which will cause a transaction (if one is open) + * to abort entirely. Thus, we add the "does_not_support_transactions" tag to prevent this test + * from being run in various the multi-statement passthrough testsuites. + * + * @tags: [ + * does_not_support_transactions, + * ] + */ +(function() { +"use strict"; + +const coll = db.sort_dotted_paths_parallel_arrays; +coll.drop(); + +// Verify that sort({a:1,b:1}) fails with a "parallel arrays" error when there is at least one +// document where both a and b are arrays. +assert.commandWorked( + coll.insert([{_id: 1, a: [], b: 1}, {_id: 2, a: 1, b: []}, {_id: 3, a: [], b: []}])); + +assert.commandFailedWithCode(db.runCommand({find: coll.getName(), sort: {a: 1, b: 1}}), + [ErrorCodes.BadValue, ErrorCodes.CannotIndexParallelArrays]); +assert.commandFailedWithCode(db.runCommand({find: coll.getName(), sort: {_id: 1, a: 1, b: 1}}), + [ErrorCodes.BadValue, ErrorCodes.CannotIndexParallelArrays]); +assert.commandFailedWithCode(db.runCommand({find: coll.getName(), sort: {a: 1, _id: 1, b: 1}}), + [ErrorCodes.BadValue, ErrorCodes.CannotIndexParallelArrays]); +assert.commandFailedWithCode(db.runCommand({find: coll.getName(), sort: {a: 1, b: 1, _id: 1}}), + [ErrorCodes.BadValue, ErrorCodes.CannotIndexParallelArrays]); + +// Verify that sort({"a.b":1,"a.c":1}) fails with a "parallel arrays" error when there is at least +// one document where both a.b and a.c are arrays. +assert(coll.drop()); +assert.commandWorked(coll.insert([{_id: 1, a: {b: [1, 2], c: [3, 4]}}])); + +assert.commandFailedWithCode(db.runCommand({find: coll.getName(), sort: {"a.b": 1, "a.c": 1}}), + [ErrorCodes.BadValue, ErrorCodes.CannotIndexParallelArrays]); + +// Verify that sort({"a.b":1,"c.d":1}) fails with a "parallel arrays" error when there is at least +// onw document where both a.b and c.d are arrays. +assert(coll.drop()); +assert.commandWorked(coll.insert({a: {b: [1, 2]}, c: {d: [3, 4]}})); + +assert.commandFailedWithCode(db.runCommand({find: coll.getName(), sort: {"a.b": 1, "c.d": 1}}), + [ErrorCodes.BadValue, ErrorCodes.CannotIndexParallelArrays]); +})(); |