Replies: 1 comment 3 replies
-
|
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
njcstreet
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
|
Beta Was this translation helpful? Give feedback.
-
Hi,
We have a requirement to implement a function which behaves differently based on the types of the arguments passed in. We are eventually planning to enhance the application layer to bypass the need to do this entirely, but it would be useful for a quick proof of concept. As an example we want to create a function which accepts two arguments, If both the arguments are numerical, it should add them together. If either one of the arguments is a string, it should concatenate the results. Taking the example function below:
CREATE OR REPLACE FUNCTION add_or_concat AS (x, y) -> if(toTypeName(x) = 'String' OR toTypeName(y) = 'String', concat(x, y), x + y); SELECT add_or_concat ('a', 'b')
I would expect this to evaluate the left hand side of the if statement, figure out that at least one of the arguments is a String, and therefore return 'ab'. However instead we receive the error:
"Illegal types String and String of arguments of function plus" which implies that the right hand side of the statement has also been evaluated." even though short_circuit_function_evaluation is set to true.
I did some digging and I can see that in https://github.com/ClickHouse/ClickHouse/blob/master/src/Functions/toTypeName.cpp isSuitableForShortCircuitArgumentsExecution is set to false, so I wonder if this is causing the right-hand side to be executed? However we get the same issue even if we replace the entire function with:
CREATE OR REPLACE FUNCTION add_or_concat AS (x, y) -> if(1 = 1, concat(x, y), x + y);
Can someone explain what is going on here and provide a possible alternative? Possibly this is related to #40622
Thanks.
Beta Was this translation helpful? Give feedback.
All reactions