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

arrayProduct #21613

Closed
den-crane opened this issue Mar 11, 2021 · 9 comments · Fixed by #23782
Closed

arrayProduct #21613

den-crane opened this issue Mar 11, 2021 · 9 comments · Fixed by #23782
Assignees
Labels
comp-arrays Arrays / array joins / higher order easy task Good for first contributors feature

Comments

@den-crane
Copy link
Contributor

den-crane commented Mar 11, 2021

for the last year I replied 3 times with a silly snippet:

SELECT
    [2, 3, 4] AS x,
    pow(2, arraySum(i -> log2(i), x)) AS arrayProduct
┌─x───────┬─arrayProduct─┐
│ [2,3,4] │           24 │
└─────────┴──────────────┘

I think we need a function to multiply elements of an array.

@den-crane den-crane added feature easy task Good for first contributors labels Mar 11, 2021
@filimonov filimonov added the comp-arrays Arrays / array joins / higher order label Mar 11, 2021
@hexiaoting
Copy link
Contributor

hexiaoting commented Mar 12, 2021

assign to me, should I use pow(2, arraySum(i -> log2(i), x)) the algorithm to implement arrayProduct?

@UnamedRus
Copy link
Contributor

assign to me, should I use pow(2, arraySum(i -> log2(i), x)) the algorithm to implement arrayProduct?

I don't think so, it would produce Float value from multiplying integers.

SELECT
    [2, 3, 40] AS x,
    pow(2, arraySum(i -> log2(i), x)) AS arrayProduct

Query id: 1fcc62b2-fd29-4cff-b9ed-f4e434c9043a

[LAPTOP-319G0GMQ] 2021.03.12 04:55:33.309638 [ 10268 ] {1fcc62b2-fd29-4cff-b9ed-f4e434c9043a} <Debug> executeQuery: (from 127.0.0.1:38504, using production parser) SELECT [2, 3, 40] AS x, pow(2, arraySum(i -> log2(i), x)) AS arrayProduct;
┌─x────────┬───────arrayProduct─┐
│ [2,3,40] │ 240.00000000000003 │
└──────────┴────────────────────┘

There is actually arrayFold function in development, which is generalized version of arrayProduct i guess.

#21589

@den-crane
Copy link
Contributor Author

den-crane commented Mar 12, 2021

It should calculate 2*3*4

arrayProduct([2,3,4]) -> 2*3*4 = 24

@alexey-milovidov
Copy link
Member

should I use pow(2, arraySum(i -> log2(i), x)) the algorithm to implement arrayProduct?

Just simply do the multiplication.

@pp641
Copy link

pp641 commented Mar 12, 2021

It should be
accumulate(v.begin(), v.end(), 1, multiplies())
This function in c++ multiplies all the element of the array in c++
vectorarr = {1,2,3,4};
int ans = accumulate(arr.begin(), arr.end(), 1, multiplies())
ans = 24

@dmalkr
Copy link
Contributor

dmalkr commented Mar 16, 2021

Yes, you can implement product of elements via arrayFold: SELECT arrayFold(x,acc -> x*acc, [1,2,3,4,5], toInt64(1));
PR #21589 (arrayFold function) on review now.

@hexiaoting
Copy link
Contributor

hexiaoting commented Mar 26, 2021

@den-crane
When I process Array(Decimal(P, S)) , which kind of type should I return ? Decimal(P,S) or enlarge scale by multiply the array size, or Decimal128(s)?
Because I need to return the type in getReturnTypeImpl function, so I think the scale of Decimal of the result should be static not dynamic like operator * in below example.
So Can I just make the result type of DataTypeDecimal<Decimal128>(DecimalUtils::max_precision<Decimal128>, DecimalUtils::max_precision<Decimal128>-1) == Decimal128(38, 37)?

I find the type of operator * result's scale enlarged by add all the operators 8 = 4+4:

select toDecimal32(0.111, 4) as a,  toDecimal32(0.112, 4) as b, a *b as k , toTypeName(k) ,toTypeName(a), toTypeName(b);

┌──────a─┬──────b─┬──────────k─┬─toTypeName(multiply(toDecimal32(0.111, 4), toDecimal32(0.112, 4)))─┬─toTy│
peName(toDecimal32(0.111, 4))─┬─toTypeName(toDecimal32(0.112, 4))─┐                                       │
│ 0.1110 │ 0.1120 │ 0.01243200 │ Decimal(9, 8)                                                      │ Deci│
mal(9, 4)                     │ Decimal(9, 4)                     │                                       │
└────────┴────────┴────────────┴────────────────────────────────────────────────────────────────────┴─────│
──────────────────────────────┴───────────────────────────────────┘

and Decimal(3, 1)+Decimal(3, 1)=Decimal(9, 1),
Decimal(3, 1) * Decimal(3, 1) = Decimal(9, 2)

@hexiaoting
Copy link
Contributor

@den-crane Can you give me some advice?

@den-crane
Copy link
Contributor Author

When I process Array(Decimal(P, S)) , which kind of type should I return ?

The same time as the input type (Decimal(P, S)) and use float64 for calculations

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp-arrays Arrays / array joins / higher order easy task Good for first contributors feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants