From 8791a44e01d96ac5bbbacb9097eb49064724e926 Mon Sep 17 00:00:00 2001 From: Joanna Hulboj Date: Fri, 27 Jan 2023 18:01:54 +0000 Subject: [PATCH] FIXUP: Added info and test for materialized array --- src/Functions/array/arrayShuffle.cpp | 18 ++++++++++----- .../0_stateless/02523_array_shuffle.reference | 22 +++++++++++++++++++ .../0_stateless/02523_array_shuffle.sql | 4 ++++ 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/src/Functions/array/arrayShuffle.cpp b/src/Functions/array/arrayShuffle.cpp index 6532fb89ae8..0c5696d1d37 100644 --- a/src/Functions/array/arrayShuffle.cpp +++ b/src/Functions/array/arrayShuffle.cpp @@ -178,6 +178,9 @@ REGISTER_FUNCTION(ArrayShuffle) Returns an array of the same size as the original array containing the elements in shuffled order. Elements are being reordered in such a way that each possible permutation of those elements has equal probability of appearance. +Note: this function will not materialize constants: +[example:materialize] + If no seed is provided a random one will be used: [example:random_seed] @@ -186,19 +189,23 @@ It is possible to override the seed to produce stable results: )", Documentation::Examples{ {"random_seed", "SELECT arrayShuffle([1, 2, 3, 4])"}, - {"explicit_seed", "SELECT arrayShuffle([1, 2, 3, 4], 41)"}}, + {"explicit_seed", "SELECT arrayShuffle([1, 2, 3, 4], 41)"}, + {"materialize", "SELECT arrayShuffle(materialize([1, 2, 3]), 42), arrayShuffle([1, 2, 3], 42) FROM numbers(10)"}}, Documentation::Categories{"Array"} }, FunctionFactory::CaseInsensitive); factory.registerFunction>( { R"( -Returns an array of the same size as the original array where elements in range [0..limit) are a random -subset of the original array. Remaining [limit..n) shall contain the elements not in [0..limit) range in undefined order. -Value of limit shall be in range [0..n]. Values outside of that range are equivalent to performing full arrayShuffle: +Returns an array of the same size as the original array where elements in range [1..limit] are a random +subset of the original array. Remaining (limit..n] shall contain the elements not in [1..limit] range in undefined order. +Value of limit shall be in range [1..n]. Values outside of that range are equivalent to performing full arrayShuffle: [example:no_limit1] [example:no_limit2] +Note: this function will not materialize constants: +[example:materialize] + If no seed is provided a random one will be used: [example:random_seed] @@ -209,7 +216,8 @@ It is possible to override the seed to produce stable results: {"no_limit1", "SELECT arrayPartialShuffle([1, 2, 3, 4], 0)"}, {"no_limit2", "SELECT arrayPartialShuffle([1, 2, 3, 4])"}, {"random_seed", "SELECT arrayPartialShuffle([1, 2, 3, 4], 2)"}, - {"explicit_seed", "SELECT arrayShuffle([1, 2, 3, 4], 2, 41)"}}, + {"explicit_seed", "SELECT arrayPartialShuffle([1, 2, 3, 4], 2, 41)"}, + {"materialize", "SELECT arrayPartialShuffle(materialize([1, 2, 3, 4]), 2, 42), arrayPartialShuffle([1, 2, 3], 2, 42) FROM numbers(10)"}}, Documentation::Categories{"Array"} }, FunctionFactory::CaseInsensitive); diff --git a/tests/queries/0_stateless/02523_array_shuffle.reference b/tests/queries/0_stateless/02523_array_shuffle.reference index db5d1b06342..0504da61f9d 100644 --- a/tests/queries/0_stateless/02523_array_shuffle.reference +++ b/tests/queries/0_stateless/02523_array_shuffle.reference @@ -3,6 +3,7 @@ [9223372036854775808] [9223372036854775808] [10,9,4,2,5,6,7,1,8,3] +[10,9,4,2,5,6,7,1,8,3] [10.1,9,4,2,5,6,7,1,8,3] [9223372036854775808,9,4,2,5,6,7,1,8,3] [NULL,9,4,2,5,6,7,1,8,3] @@ -13,6 +14,7 @@ [NULL] [NULL,NULL] [[10,20,30,40],[1,2,3,4],[100,200,300,400,500,600,700,800,900],[2,4,8,16,32,64],[-1,-2,-3,-4]] +[[10,20,30,40],[1,2,3,4],[100,200,300,400,500,600,700,800,900],[2,4,8,16,32,64],[-1,-2,-3,-4]] [[10,20,30,40],[1,2,3,4],[100,200,300,400,500,600,700,800,900],[2,4,8,16,32,64],[NULL,-2,-3,-4]] [10,72,11,18,73,76,46,71,44,35,9,0,97,53,13,32,51,30,3,68,5,48,67,90,20,27,38,19,54,21,83,84,1,22,56,81,91,77,36,63,33,39,24,40,4,99,14,23,94,29,26,96,2,28,31,57,42,88,12,47,58,8,37,82,92,34,6,60,25,43,50,74,70,52,55,62,17,79,65,93,86,7,16,41,59,75,80,45,69,89,85,87,95,64,61,98,49,78,66,15] [10,72,11,18,73,76,46,71,44,35,9,0,97,53,13,32,51,30,3,68,5,48,67,90,20,27,38,19,54,21,83,84,1,22,56,81,91,77,36,63,33,39,24,40,4,99,14,23,94,29,26,96,2,28,31,57,42,88,12,47,58,8,37,82,92,34,6,60,25,43,50,74,70,52,55,62,17,79,65,93,86,7,16,41,59,75,80,45,69,89,85,87,95,64,61,98,49,78,66,15] @@ -61,3 +63,23 @@ [10,72,11,18,73,76,46,71,44,35,9,0,97,53,13,32,51,30,3,68,20,21,22,23,24,25,26,27,28,29,17,31,15,33,34,2,36,37,38,39,40,41,42,43,8,45,6,47,48,49,50,16,52,14,54,55,56,57,58,59,60,61,62,63,64,65,66,67,19,69,70,7,1,4,74,75,5,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,12,98,99] [(3,-3),(1,-1),(99999999,-99999999)] [(3,'A'),(1,NULL),(2,'a')] +[3,2,1] +[3,2,1] +[3,2,1] +[3,2,1] +[3,2,1] +[3,2,1] +[3,2,1] +[3,2,1] +[3,2,1] +[3,2,1] +[3,2,1] +[3,1,2] +[1,3,2] +[2,1,3] +[3,2,1] +[3,2,1] +[1,2,3] +[3,2,1] +[3,2,1] +[2,1,3] diff --git a/tests/queries/0_stateless/02523_array_shuffle.sql b/tests/queries/0_stateless/02523_array_shuffle.sql index cec0b3fbd29..9138657c842 100644 --- a/tests/queries/0_stateless/02523_array_shuffle.sql +++ b/tests/queries/0_stateless/02523_array_shuffle.sql @@ -3,6 +3,7 @@ SELECT arrayShuffle([], 0xbad_cafe); SELECT arrayShuffle([9223372036854775808]); SELECT arrayShuffle([9223372036854775808], 0xbad_cafe); SELECT arrayShuffle([1,2,3,4,5,6,7,8,9,10], 0xbad_cafe); +SELECT arrayShuffle(materialize([1,2,3,4,5,6,7,8,9,10]), 0xbad_cafe); SELECT arrayShuffle([1,2,3,4,5,6,7,8,9,10.1], 0xbad_cafe); SELECT arrayShuffle([1,2,3,4,5,6,7,8,9,9223372036854775808], 0xbad_cafe); SELECT arrayShuffle([1,2,3,4,5,6,7,8,9,NULL], 0xbad_cafe); @@ -13,6 +14,7 @@ SELECT arrayShuffle(['storage','tiger',NULL,'terminal','uniform','sensation'], 0 SELECT arrayShuffle([NULL]); SELECT arrayShuffle([NULL,NULL]); SELECT arrayShuffle([[1,2,3,4],[-1,-2,-3,-4],[10,20,30,40],[100,200,300,400,500,600,700,800,900],[2,4,8,16,32,64]], 0xbad_cafe); +SELECT arrayShuffle(materialize([[1,2,3,4],[-1,-2,-3,-4],[10,20,30,40],[100,200,300,400,500,600,700,800,900],[2,4,8,16,32,64]]), 0xbad_cafe); SELECT arrayShuffle([[1,2,3,4],[NULL,-2,-3,-4],[10,20,30,40],[100,200,300,400,500,600,700,800,900],[2,4,8,16,32,64]], 0xbad_cafe); SELECT arrayShuffle(groupArray(x),0xbad_cafe) FROM (SELECT number as x from system.numbers LIMIT 100); SELECT arrayShuffle(groupArray(toUInt64(x)),0xbad_cafe) FROM (SELECT number as x from system.numbers LIMIT 100); @@ -61,6 +63,8 @@ SELECT arrayPartialShuffle(groupArray(x),20,0xbad_cafe) FROM (SELECT number as x SELECT arrayPartialShuffle(groupArray(toUInt64(x)),20,0xbad_cafe) FROM (SELECT number as x from system.numbers LIMIT 100); SELECT arrayPartialShuffle([tuple(1, -1), tuple(99999999, -99999999), tuple(3, -3)], 2, 0xbad_cafe); SELECT arrayPartialShuffle([tuple(1, NULL), tuple(2, 'a'), tuple(3, 'A')], 2, 0xbad_cafe); +SELECT arrayShuffle([1, 2, 3], 42) FROM numbers(10); -- for constant array we don not materialize it and each row gets the same permutation +SELECT arrayShuffle(materialize([1, 2, 3]), 42) FROM numbers(10); SELECT arrayShuffle(1); -- { serverError ILLEGAL_TYPE_OF_ARGUMENT } SELECT arrayShuffle([1], 'a'); -- { serverError ILLEGAL_TYPE_OF_ARGUMENT } SELECT arrayShuffle([1], 1.1); -- { serverError ILLEGAL_TYPE_OF_ARGUMENT }