Skip to content

Commit 00b8e28

Browse files
authored
Add null-safety unit tests for UDFs (#191)
1 parent be482e7 commit 00b8e28

File tree

12 files changed

+356
-11
lines changed

12 files changed

+356
-11
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright © 2025 DataSQRL ([email protected])
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.datasqrl.flinkrunner.stdlib.commons;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
20+
import org.junit.jupiter.api.Test;
21+
22+
class SerializeToBytesTest {
23+
24+
@Test
25+
public void testNullInput() {
26+
var function = new serialize_to_bytes();
27+
var result = function.eval(null);
28+
29+
assertThat(result).isNull();
30+
}
31+
}

stdlib/stdlib-vector/src/main/java/com/datasqrl/flinkrunner/stdlib/vector/VectorFunctions.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class VectorFunctions {
2525

2626
public static final euclidean_distance EUCLIDEAN_DISTANCE = new euclidean_distance();
2727

28-
public static final vector_to_double VEC_TO_DOUBLE = new vector_to_double();
28+
public static final vector_to_double VECTOR_TO_DOUBLE = new vector_to_double();
2929

3030
public static final double_to_vector DOUBLE_TO_VECTOR = new double_to_vector();
3131

@@ -38,7 +38,7 @@ public class VectorFunctions {
3838
COSINE_SIMILARITY,
3939
COSINE_DISTANCE,
4040
EUCLIDEAN_DISTANCE,
41-
VEC_TO_DOUBLE,
41+
VECTOR_TO_DOUBLE,
4242
DOUBLE_TO_VECTOR,
4343
ASCII_TEXT_TEST_EMBED,
4444
CENTER);

stdlib/stdlib-vector/src/main/java/com/datasqrl/flinkrunner/stdlib/vector/center.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package com.datasqrl.flinkrunner.stdlib.vector;
1717

18-
import static com.datasqrl.flinkrunner.stdlib.vector.VectorFunctions.VEC_TO_DOUBLE;
18+
import static com.datasqrl.flinkrunner.stdlib.vector.VectorFunctions.VECTOR_TO_DOUBLE;
1919
import static com.datasqrl.flinkrunner.stdlib.vector.VectorFunctions.convert;
2020

2121
import com.datasqrl.flinkrunner.stdlib.utils.AutoRegisterSystemFunction;
@@ -47,11 +47,11 @@ public FlinkVectorType getValue(CenterAccumulator acc) {
4747
}
4848

4949
public void accumulate(CenterAccumulator acc, FlinkVectorType vector) {
50-
acc.add(VEC_TO_DOUBLE.eval(vector));
50+
acc.add(VECTOR_TO_DOUBLE.eval(vector));
5151
}
5252

5353
public void retract(CenterAccumulator acc, FlinkVectorType vector) {
54-
acc.substract(VEC_TO_DOUBLE.eval(vector));
54+
acc.substract(VECTOR_TO_DOUBLE.eval(vector));
5555
}
5656

5757
public void merge(CenterAccumulator acc, Iterable<CenterAccumulator> iter) {

stdlib/stdlib-vector/src/main/java/com/datasqrl/flinkrunner/stdlib/vector/cosine_similarity.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package com.datasqrl.flinkrunner.stdlib.vector;
1717

18-
import static com.datasqrl.flinkrunner.stdlib.vector.VectorFunctions.VEC_TO_DOUBLE;
18+
import static com.datasqrl.flinkrunner.stdlib.vector.VectorFunctions.VECTOR_TO_DOUBLE;
1919

2020
import com.datasqrl.flinkrunner.stdlib.utils.AutoRegisterSystemFunction;
2121
import com.google.auto.service.AutoService;
@@ -35,8 +35,8 @@ public Double eval(FlinkVectorType vectorA, FlinkVectorType vectorB) {
3535
}
3636

3737
// Create RealVectors from the input arrays
38-
RealVector vA = new ArrayRealVector(VEC_TO_DOUBLE.eval(vectorA), false);
39-
RealVector vB = new ArrayRealVector(VEC_TO_DOUBLE.eval(vectorB), false);
38+
RealVector vA = new ArrayRealVector(VECTOR_TO_DOUBLE.eval(vectorA), false);
39+
RealVector vB = new ArrayRealVector(VECTOR_TO_DOUBLE.eval(vectorB), false);
4040

4141
// Calculate the cosine similarity
4242
var dotProduct = vA.dotProduct(vB);

stdlib/stdlib-vector/src/main/java/com/datasqrl/flinkrunner/stdlib/vector/euclidean_distance.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package com.datasqrl.flinkrunner.stdlib.vector;
1717

18-
import static com.datasqrl.flinkrunner.stdlib.vector.VectorFunctions.VEC_TO_DOUBLE;
18+
import static com.datasqrl.flinkrunner.stdlib.vector.VectorFunctions.VECTOR_TO_DOUBLE;
1919

2020
import com.datasqrl.flinkrunner.stdlib.utils.AutoRegisterSystemFunction;
2121
import com.google.auto.service.AutoService;
@@ -35,8 +35,8 @@ public Double eval(FlinkVectorType vectorA, FlinkVectorType vectorB) {
3535
}
3636

3737
// Create RealVectors from the input arrays
38-
RealVector vA = new ArrayRealVector(VEC_TO_DOUBLE.eval(vectorA), false);
39-
RealVector vB = new ArrayRealVector(VEC_TO_DOUBLE.eval(vectorB), false);
38+
RealVector vA = new ArrayRealVector(VECTOR_TO_DOUBLE.eval(vectorA), false);
39+
RealVector vB = new ArrayRealVector(VECTOR_TO_DOUBLE.eval(vectorB), false);
4040

4141
return vA.getDistance(vB);
4242
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright © 2025 DataSQRL ([email protected])
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.datasqrl.flinkrunner.stdlib.vector;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
20+
import org.junit.jupiter.api.Test;
21+
22+
class AsciiTextTestEmbedTest {
23+
24+
@Test
25+
public void testNullInputReturnsNull() {
26+
var function = new ascii_text_test_embed();
27+
var result = function.eval(null);
28+
29+
assertThat(result).isNull();
30+
}
31+
32+
@Test
33+
public void testValidInputReturnsVector() {
34+
var function = new ascii_text_test_embed();
35+
var result = function.eval("test");
36+
37+
assertThat(result).isNotNull();
38+
assertThat(result.getValue()).hasSize(256);
39+
}
40+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright © 2025 DataSQRL ([email protected])
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.datasqrl.flinkrunner.stdlib.vector;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
import static org.assertj.core.api.Assertions.within;
20+
21+
import org.junit.jupiter.api.Test;
22+
23+
class CosineDistanceTest {
24+
25+
@Test
26+
public void testNullInputsReturnNull() {
27+
var function = new cosine_distance();
28+
29+
assertThat(function.eval(null, null)).isNull();
30+
31+
var vector = new FlinkVectorType(new double[] {1.0, 2.0, 3.0});
32+
assertThat(function.eval(null, vector)).isNull();
33+
assertThat(function.eval(vector, null)).isNull();
34+
}
35+
36+
@Test
37+
public void testValidInputsReturnDistance() {
38+
var function = new cosine_distance();
39+
40+
var vector1 = new FlinkVectorType(new double[] {1.0, 0.0, 0.0});
41+
var vector2 = new FlinkVectorType(new double[] {1.0, 0.0, 0.0});
42+
43+
var result = function.eval(vector1, vector2);
44+
assertThat(result).isNotNull();
45+
assertThat(result).isCloseTo(0.0, within(1e-7));
46+
}
47+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright © 2025 DataSQRL ([email protected])
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.datasqrl.flinkrunner.stdlib.vector;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
import static org.assertj.core.api.Assertions.within;
20+
21+
import org.junit.jupiter.api.Test;
22+
23+
class CosineSimilarityTest {
24+
25+
@Test
26+
public void testNullInputsReturnNull() {
27+
var function = new cosine_similarity();
28+
29+
assertThat(function.eval(null, null)).isNull();
30+
31+
var vector = new FlinkVectorType(new double[] {1.0, 2.0, 3.0});
32+
assertThat(function.eval(null, vector)).isNull();
33+
assertThat(function.eval(vector, null)).isNull();
34+
}
35+
36+
@Test
37+
public void testValidInputsReturnSimilarity() {
38+
var function = new cosine_similarity();
39+
40+
var vector1 = new FlinkVectorType(new double[] {1.0, 0.0, 0.0});
41+
var vector2 = new FlinkVectorType(new double[] {1.0, 0.0, 0.0});
42+
43+
var result = function.eval(vector1, vector2);
44+
assertThat(result).isNotNull();
45+
assertThat(result).isCloseTo(1.0, within(1e-7));
46+
}
47+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright © 2025 DataSQRL ([email protected])
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.datasqrl.flinkrunner.stdlib.vector;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
20+
import org.junit.jupiter.api.Test;
21+
22+
class DoubleToVectorTest {
23+
24+
@Test
25+
public void testNullInputReturnsNull() {
26+
var function = new double_to_vector();
27+
var result = function.eval(null);
28+
29+
assertThat(result).isNull();
30+
}
31+
32+
@Test
33+
public void testValidInputReturnsVector() {
34+
var function = new double_to_vector();
35+
var input = new double[] {1.0, 2.0, 3.0};
36+
var result = function.eval(input);
37+
38+
assertThat(result).isNotNull();
39+
assertThat(result.getValue()).containsExactly(1.0, 2.0, 3.0);
40+
}
41+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright © 2025 DataSQRL ([email protected])
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.datasqrl.flinkrunner.stdlib.vector;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
import static org.assertj.core.api.Assertions.within;
20+
21+
import org.junit.jupiter.api.Test;
22+
23+
class EuclideanDistanceTest {
24+
25+
@Test
26+
public void testNullInputsReturnNull() {
27+
var function = new euclidean_distance();
28+
29+
assertThat(function.eval(null, null)).isNull();
30+
31+
var vector = new FlinkVectorType(new double[] {1.0, 2.0, 3.0});
32+
assertThat(function.eval(null, vector)).isNull();
33+
assertThat(function.eval(vector, null)).isNull();
34+
}
35+
36+
@Test
37+
public void testValidInputsReturnDistance() {
38+
var function = new euclidean_distance();
39+
40+
var vector1 = new FlinkVectorType(new double[] {1.0, 0.0, 0.0});
41+
var vector2 = new FlinkVectorType(new double[] {0.0, 0.0, 0.0});
42+
43+
var result = function.eval(vector1, vector2);
44+
assertThat(result).isNotNull();
45+
assertThat(result).isCloseTo(1.0, within(1e-7));
46+
}
47+
}

0 commit comments

Comments
 (0)