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

Fix concat expression #178

Open
wants to merge 5 commits into
base: development
Choose a base branch
from
Open

Conversation

rlaiola
Copy link
Contributor

@rlaiola rlaiola commented Oct 18, 2022

Reference issue (#177)

The current implementation of the concat function (expression) may present an inconsistent behavior. It seems that it is possible to have a non-string argument only if it is listed as the very first one. If not, an error message is displayed.

What does this implement/fix?

This PR aims at providing a more consistent behavior to the concat expression based on the SQL standard and RDBMS implementations such as MySQL, PostgreSQL and MS SQL Server. In summary, each non-string argument is implicitly converted to string and then concatenated with the other arguments.

Note: To run the tests refer to http://dbis-uibk.github.io/relax/calc/local/uibk/local/0

  • Test 1: concat expression with only one non-string argument (same behavior as before)
pi concat(a)->x (R)

or

SELECT concat(a) AS x FROM R;

results in

x
'1'
'3'
'4'
'5'
'6'
  • Example 2: concat expression with multiple non-string arguments (now it works!)
pi concat(a, a)->x (R)

or

SELECT concat(a, a) AS x FROM R;

results in

x
'11'
'33'
'44'
'55'
'66'
  • Example 3: concatenate non-string with string arguments (non-string as the first listed) (same behavior as before)
pi concat(a, b, c)->x (R)

or

SELECT concat(a, b, c) AS x FROM R;

results in

x
'1ad'
'3cc'
'4df'
'5db'
'6ef'
  • Example 4: concatenate string with non-string argument (the last not as the first one) (now it works!)
pi concat(b, a)->x (R)

or

SELECT concat(b, a) AS x FROM R;

results in

x
'a1'
'c3'
'd4'
'd5'
'e6'

NULL values

It is important to mention that this behavior of any null argument leading to a null return does not seem to be adopted in all current RDBMS, but MySQL. See below:

  • MySQL (since version 5.1)

CONCAT(str1,str2,...): Returns the string that results from concatenating the arguments. May have one or more arguments. If all arguments are nonbinary strings, the result is a nonbinary string. If the arguments include any binary strings, the result is a binary string. A numeric argument is converted to its equivalent nonbinary string form. CONCAT() returns NULL if any argument is NULL.

Examples:

SELECT CONCAT('MyS', NULL, 'QL'); -- NULL
SELECT CONCAT('abcde', 2, NULL, 22); -- NULL
  • PosgreSQL (since version 9.1)

concat ( val1 "any" [, val2 "any" [, ...] ] ) → text: Concatenates the text representations of all the arguments. NULL arguments are ignored.

Examples:

SELECT CONCAT('PostgreS', NULL, 'QL'); -- PostgreSQL
SELECT CONCAT('abcde', 2, NULL, 22); -- abcde222
  • SQL Server (since version 2012)

CONCAT ( string_value1, string_value2 [, string_valueN ] ): CONCAT takes a variable number of string arguments and concatenates them into a single string. It requires a minimum of two input values; otherwise, an error is raised. All arguments are implicitly converted to string types and then concatenated. Null values are implicitly converted to an empty string. If all the arguments are null, an empty string of type varchar(1) is returned.

Examples:

SELECT CONCAT( 'S', NULL, 'QL Server'); -- SQL Server
SELECT CONCAT( 'abcde', 2, NULL, 22); -- abcde222

NOTE: SQL Server has the system setting CONCAT_NULL_YIELDS_NULL, which can be set to alter the behavior when NULL values are used in the concatenation of string values.

How to test this PR?

Test it live at https://rlaiola.github.io/relax/calc/local/uibk/local/0. For automated tests visit https://rlaiola.github.io/relax/test.html

References

@rlaiola rlaiola mentioned this pull request Oct 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant