Skip to content

Commit

Permalink
Fix array_join adding extra delimeter if last element is null
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinwilfong committed May 2, 2024
1 parent 902c426 commit ed10b16
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -315,15 +315,19 @@ public static void arrayJoinProvidedBlock(
Slice nullReplacement)
{
int numElements = arrayBlock.getPositionCount();
boolean hasOutput = false;

for (int i = 0; i < numElements; i++) {
if (arrayBlock.isNull(i) && nullReplacement == null) {
continue;
}

if (hasOutput) {
blockBuilder.writeBytes(delimiter, 0, delimiter.length());
}

if (arrayBlock.isNull(i)) {
if (nullReplacement != null) {
blockBuilder.writeBytes(nullReplacement, 0, nullReplacement.length());
}
else {
continue;
}
blockBuilder.writeBytes(nullReplacement, 0, nullReplacement.length());
}
else {
try {
Expand All @@ -337,9 +341,7 @@ public static void arrayJoinProvidedBlock(
}
}

if (i != numElements - 1) {
blockBuilder.writeBytes(delimiter, 0, delimiter.length());
}
hasOutput = true;
}

blockBuilder.closeEntry();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,7 @@ public void testArrayJoin()
assertFunction("array_join(ARRAY[1, NULL, 2], ',')", VARCHAR, "1,2");
assertFunction("ARRAY_JOIN(ARRAY [1, 2, 3], ';', 'N/A')", VARCHAR, "1;2;3");
assertFunction("ARRAY_JOIN(ARRAY [1, 2, null], ';', 'N/A')", VARCHAR, "1;2;N/A");
assertFunction("ARRAY_JOIN(ARRAY [1, 2, null], ';')", VARCHAR, "1;2");
assertFunction("ARRAY_JOIN(ARRAY [1, 2, 3], 'x')", VARCHAR, "1x2x3");
assertFunction("ARRAY_JOIN(ARRAY [BIGINT '1', 2, 3], 'x')", VARCHAR, "1x2x3");
assertFunction("ARRAY_JOIN(ARRAY [null], '=')", VARCHAR, "");
Expand Down

0 comments on commit ed10b16

Please sign in to comment.