-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Patrick Stuedi
committed
Jul 7, 2021
1 parent
24ff537
commit e421153
Showing
4 changed files
with
197 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
ksqldb-engine/src/main/java/io/confluent/ksql/function/udf/array/ArrayConcat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 2020 Confluent Inc. | ||
* | ||
* Licensed under the Confluent Community License; you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.confluent.io/confluent-community-license | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License | ||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and limitations under the | ||
* License. | ||
*/ | ||
|
||
package io.confluent.ksql.function.udf.array; | ||
|
||
import io.confluent.ksql.function.FunctionCategory; | ||
import io.confluent.ksql.function.udf.Udf; | ||
import io.confluent.ksql.function.udf.UdfDescription; | ||
import io.confluent.ksql.function.udf.UdfParameter; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
|
||
@UdfDescription( | ||
name = "array_concat", | ||
category = FunctionCategory.ARRAY, | ||
description = "Concatenates two arrays, creating an array that contains all the elements" | ||
+ "in the first array followed by all the elements in the second array." | ||
+ " Returns NULL if both input arrays are NULL. " | ||
+ "The two arrays must be of the same type.") | ||
public class ArrayConcat { | ||
@Udf | ||
public <T> List<T> concat( | ||
@UdfParameter(description = "First array of values") final List<T> left, | ||
@UdfParameter(description = "Second array of values") final List<T> right) { | ||
if (left == null && right == null) { | ||
return null; | ||
} | ||
final List<T> result = new ArrayList(left.size() + right.size()); | ||
if (left != null) { | ||
result.addAll(left); | ||
} | ||
if (right != null) { | ||
result.addAll(right); | ||
} | ||
return result; | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
ksqldb-engine/src/test/java/io/confluent/ksql/function/udf/array/ArrayConcatTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Copyright 2020 Confluent Inc. | ||
* | ||
* Licensed under the Confluent Community License; you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.confluent.io/confluent-community-license | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License | ||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and limitations under the | ||
* License. | ||
*/ | ||
|
||
package io.confluent.ksql.function.udf.array; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.CoreMatchers.nullValue; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.contains; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Map; | ||
import org.junit.Test; | ||
|
||
public class ArrayConcatTest { | ||
|
||
private final ArrayConcat udf = new ArrayConcat(); | ||
|
||
@Test | ||
public void shouldConcatArraysOfLikeType() { | ||
final List<String> input1 = Arrays.asList("foo", " ", "bar"); | ||
final List<String> input2 = Arrays.asList("baz"); | ||
final List<String> result = udf.concat(input1, input2); | ||
assertThat(result, is(Arrays.asList("foo", " ", "bar", "baz"))); | ||
} | ||
|
||
@Test | ||
public void shouldReturnDuplicateValues() { | ||
final List<String> input1 = Arrays.asList("foo", "foo", "bar"); | ||
final List<String> input2 = Arrays.asList("baz", "foo"); | ||
final List<String> result = udf.concat(input1, input2); | ||
assertThat(result, is(Arrays.asList("foo", "foo", "bar", "baz", "foo"))); | ||
} | ||
|
||
@Test | ||
public void shouldConcatArraysContainingNulls() { | ||
final List<String> input1 = Arrays.asList(null, "bar"); | ||
final List<String> input2 = Arrays.asList("foo"); | ||
final List<String> result = udf.concat(input1, input2); | ||
assertThat(result, is(Arrays.asList(null, "bar", "foo"))); | ||
} | ||
|
||
@Test | ||
public void shouldConcatArraysBothContainingNulls() { | ||
final List<String> input1 = Arrays.asList(null, "foo", "bar"); | ||
final List<String> input2 = Arrays.asList("foo", null); | ||
final List<String> result = udf.concat(input1, input2); | ||
assertThat(result, is(Arrays.asList(null, "foo", "bar", "foo", null))); | ||
} | ||
|
||
@Test | ||
public void shouldConcatArraysOfOnlyNulls() { | ||
final List<String> input1 = Arrays.asList(null, null); | ||
final List<String> input2 = Arrays.asList(null, null, null); | ||
final List<String> result = udf.concat(input1, input2); | ||
assertThat(result, is(Arrays.asList(null, null, null, null, null))); | ||
} | ||
|
||
@Test | ||
public void shouldReturnNonNullForNullRightInput() { | ||
final List<String> input1 = Arrays.asList("foo"); | ||
final List<String> result = udf.concat(input1, null); | ||
assertThat(result, is(Arrays.asList("foo"))); | ||
} | ||
|
||
@Test | ||
public void shouldReturnNullForNullLeftInput() { | ||
final List<String> input1 = Arrays.asList("foo"); | ||
final List<String> result = udf.concat(null, input1); | ||
assertThat(result, is(Arrays.asList("foo"))); | ||
} | ||
|
||
@Test | ||
public void shouldReturnNullForAllNullInputs() { | ||
final List<Long> result = udf.concat((List<Long>) null, (List<Long>) null); | ||
assertThat(result, is(nullValue())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters