Postgres width_bucket: function width_bucket(integer, numeric[]) does not exist - postgresql

I expected results when I run a query passing an array of integers as the second argument to width_bucket such as this:
select width_bucket(5, array[0, 1, 2, 3, 4, 5, 6, 7, 8] )
But if this array contains a decimal number such as this:
select width_bucket( 5, array[0, 1.1, 2, 3, 4, 5, 6, 7, 8] )
I get the error:
ERROR: function width_bucket(integer, numeric[]) does not exist
LINE 1: select width_bucket( 5, array[0, 1.1, 2, 3, 4, 5, 6, 7, 8] )
^
My use case is I'm using this function to calculate my data's breaks, which won't always be integers (and I've seen used elsewhere used with width_bucket in that codebase so I'm not sure why it's not working here).
How can I get width_bucket to accept an array of non-integers?
[Edited to add link to the Postgres docs for the width_bucket function, which says that it accepts "anycompatiblearray" although I don't see where the list of "compatible" array types are defined.]

The error message is misleading here. The issue is not that width_bucket does not support a numeric array, it's that it does not seem to support a mismatch of types between the operand and the thresholds values.
All of these work and return the same value:
select width_bucket( 5.0, array[0, 1.1, 2, 3, 4, 5, 6, 7, 8] )
select width_bucket( 5::numeric, array[0, 1.1, 2, 3, 4, 5, 6, 7, 8] )

Related

Vs code error which is working on online compiler

freq_count.cpp: In function 'int main()':
freq_count.cpp:40:45: error: in C++98 'vec' must be initialized by constructor, not by '{...}'
vector vec = { 1, 2, 2, 3, 1, 4, 4, 5 };
^
freq_count.cpp:40:45: error: could not convert '{1, 2, 2, 3, 1, 4, 4, 5}' from '' to 'std::vector'

array_positions() with comparison, not equality

Postgres has the function array_positions:
array_positions(ARRAY[1, 4, 3, 1, 3, 4, 2, 1], 1);
-> {1,4,8}
Which checks for equality of the second argument. I can't find anywhere if it has a function for comparison. E.g. (element in array > 1) which would return {2, 3, 5, 6, 7} in this example.
Does postgres have an efficient way of doing this?
SELECT position
FROM UNNEST(ARRAY[1, 4, 3, 1, 3, 4, 2, 1]) WITH ORDINALITY a(element, position)
WHERE element > 1

Can you merge two Flux, without blocking, such that the result only contains unique elements?

Is there a way to merge two Flux such that the result only contains unique elements? I can block on the output and then convert it to a set, but is there a way that does not depend on blocking?
Source (Kotlin)
val set1 = Flux.just(1, 2, 3, 4, 5)
val set2 = Flux.just(2, 4, 6, 8, 10)
val mergedSet = set1.mergeWith(set2)
println(mergedSet.collectList().block())
Output
[1, 2, 3, 4, 5, 2, 4, 6, 8, 10]
Desired Output (order is not important)
[1, 2, 3, 4, 5, 6, 8, 10]
You can use the Flux's merge method and then apply distinct() to it.
Flux.merge (Flux.just(1, 2, 3, 4, 5), Flux.just(2, 4, 6, 8, 10)).distinct();
This way you get a flux which produces only distinct values.

Do you have any idea or documentation about why we have arc4random_stir() in swift?

I have written the program below for generating random unique numbers for several number of times by invoking the function, but it seems like I'm getting the same pattern with minimal changes.
func generateRandom(withinNumber: Int) {
var i:Int = 0
var elements = Set<Int>()
while i != withinNumber {
let num:Int = Int(arc4random())%withinNumber + 1
if elements.count <= withinNumber && elements.contains(num) == false {
elements.insert(num)
}
else {
i = i-1
}
i=i+1
}
print(elements)
elements.removeAll()
}
generateRandom(withinNumber: 10)
How does I make my program effectively run to generate several random unique numbers.
Please let me know it would be very helpful for me.
You are storing your numbers in a Set and sets are not ordered, so the order the elements are shown by print is unrelated to the order in which they were added to the set.
Rather the elements of a set are stored in some manner which enables fast checking for .contains(), and this is one reason you seeing similar sequences.
If you wish to preserve order of insertion use a collection which does this, i.e. an array. Changing to an array in your code produced the following results from 9 calls:
[8, 9, 7, 10, 5, 6, 2, 3, 1, 4]
[4, 9, 10, 3, 6, 2, 1, 7, 8, 5]
[8, 3, 5, 1, 6, 4, 9, 10, 7, 2]
[5, 7, 2, 9, 8, 1, 6, 10, 3, 4]
[2, 3, 7, 6, 9, 1, 8, 10, 5, 4]
[9, 10, 2, 4, 6, 8, 5, 7, 1, 3]
[9, 10, 2, 5, 4, 7, 3, 8, 1, 6]
[1, 6, 4, 5, 8, 2, 3, 9, 7, 10]
[6, 10, 5, 3, 2, 8, 1, 9, 7, 4]
You are also generating 10 random numbers in the range 1 to 10 and avoiding duplicates, so the results is always going to be the numbers 1 to 10 in some order.
To generate a random number in a given range do not use %, instead use the provided arc4random_uniform() which will give better a better distribution.
The function mention in your title arc4random_stir() is available in Swift.
BTW (somewhat opinion based): It is better to write !e (! being the boolean not operator) rather than e == false, and never ever write e == true which is the long form of e!
BTW (SO etiquette): Don't link to your code (or paste in images of it). Reduce to a small example which demonstrates the issue (not required in your case) and insert directly in the question. Keep tags minimal and appropriate. These edits were done for you this time by myself and others, you will know for next time.
HTH

Is there an intrinsic size limit for the static initializer in Swift?

In a class I have class variables, simulated by a struct with static members like this:
internal class DEAccountCheck : AccountCheck {
private struct Static {
static var methodParameters: [String: (UInt16, [UInt16])] = [ // Modulus + weights indexed by method id.
"00": (10, [2, 1, 2, 1, 2, 1, 2, 1, 2]),
"01": (10, [3, 7, 1, 3, 7, 1, 3, 7, 1]),
"02": (11, [2, 3, 4, 5, 6, 7, 8, 9, 2]),
"03": (10, [2, 1, 2, 1, 2, 1, 2, 1, 2]),
...
]
}
There are more than 200 lines (entries) for the methodParameter dictionary. If I run my app I get an EXC_BAD_INSTRUCTION exception with the debugger stopping in the middle of my static initializer. I checked thereon when this starts to happen and found that I can have up to 172 values. One more and: puff.
As I can successfully add all entries by normal code the question arises if there's some known limit for static intializers.
It turned out that the exception came from a duplicate key, which is not allowed. If the error messages in Swift would be more user friendly that problem would have been seen much easier. ATM error messages in Swift are simply terrible.