Skip to content

Commit 580d32e

Browse files
committed
update tests
Signed-off-by: Kai Huang <ahkcs@amazon.com>
1 parent 0d40418 commit 580d32e

3 files changed

Lines changed: 50 additions & 197 deletions

File tree

core/src/test/java/org/opensearch/sql/expression/function/CollectionUDF/MVFindFunctionImplTest.java

Lines changed: 11 additions & 197 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ public void testMvfindWithLastElementMatch() {
4747
assertEquals(2, result);
4848
}
4949

50+
@Test
51+
public void testMvfindReturnsFirstMatch() {
52+
List<Object> array = Arrays.asList("test1", "test2", "test3");
53+
Object result = MVFindFunctionImpl.eval(array, "test");
54+
assertEquals(0, result); // Returns first match, not all
55+
}
56+
5057
// Null handling tests
5158

5259
@Test
@@ -81,13 +88,6 @@ public void testMvfindWithNullElementInArray() {
8188
assertEquals(2, result);
8289
}
8390

84-
@Test
85-
public void testMvfindWithAllNullElements() {
86-
List<Object> array = Arrays.asList(null, null, null);
87-
Object result = MVFindFunctionImpl.eval(array, "pattern");
88-
assertNull(result);
89-
}
90-
9191
// Edge cases
9292

9393
@Test
@@ -101,14 +101,6 @@ public void testMvfindWithEmptyArray() {
101101
public void testMvfindWithEmptyStringPattern() {
102102
List<Object> array = Arrays.asList("apple", "banana");
103103
Object result = MVFindFunctionImpl.eval(array, "");
104-
// Empty pattern matches everything (finds at position 0 in "apple")
105-
assertEquals(0, result);
106-
}
107-
108-
@Test
109-
public void testMvfindWithEmptyStringElement() {
110-
List<Object> array = Arrays.asList("apple", "", "banana");
111-
Object result = MVFindFunctionImpl.eval(array, "");
112104
assertEquals(0, result); // Empty pattern matches first element
113105
}
114106

@@ -121,27 +113,13 @@ public void testMvfindWithSingleElementArray() {
121113

122114
// Regex pattern tests
123115

124-
@Test
125-
public void testMvfindWithPartialMatch() {
126-
List<Object> array = Arrays.asList("apple", "banana", "apricot");
127-
Object result = MVFindFunctionImpl.eval(array, "ban");
128-
assertEquals(1, result);
129-
}
130-
131116
@Test
132117
public void testMvfindWithWildcardPattern() {
133118
List<Object> array = Arrays.asList("apple", "banana", "apricot");
134119
Object result = MVFindFunctionImpl.eval(array, "ban.*");
135120
assertEquals(1, result);
136121
}
137122

138-
@Test
139-
public void testMvfindWithDotPlus() {
140-
List<Object> array = Arrays.asList("a", "ab", "abc");
141-
Object result = MVFindFunctionImpl.eval(array, "a.+");
142-
assertEquals(1, result); // Matches "ab"
143-
}
144-
145123
@Test
146124
public void testMvfindWithCharacterClass() {
147125
List<Object> array = Arrays.asList("error123", "info", "error456");
@@ -157,10 +135,10 @@ public void testMvfindWithDigitClass() {
157135
}
158136

159137
@Test
160-
public void testMvfindWithWordBoundary() {
161-
List<Object> array = Arrays.asList("hello world", "helloworld", "hello");
162-
Object result = MVFindFunctionImpl.eval(array, "\\bhello\\b");
163-
assertEquals(0, result);
138+
public void testMvfindWithCaseInsensitiveFlag() {
139+
List<Object> array = Arrays.asList("Apple", "Banana", "Cherry");
140+
Object result = MVFindFunctionImpl.eval(array, "(?i)banana");
141+
assertEquals(1, result);
164142
}
165143

166144
@Test
@@ -177,64 +155,6 @@ public void testMvfindWithAnchorEnd() {
177155
assertEquals(0, result);
178156
}
179157

180-
@Test
181-
public void testMvfindWithAlternation() {
182-
List<Object> array = Arrays.asList("cat", "dog", "bird");
183-
Object result = MVFindFunctionImpl.eval(array, "dog|cat");
184-
assertEquals(0, result); // Finds "cat" first
185-
}
186-
187-
@Test
188-
public void testMvfindWithOptionalQuantifier() {
189-
List<Object> array = Arrays.asList("color", "colour", "colors");
190-
Object result = MVFindFunctionImpl.eval(array, "colou?r");
191-
assertEquals(0, result);
192-
}
193-
194-
@Test
195-
public void testMvfindWithCaseInsensitiveFlag() {
196-
List<Object> array = Arrays.asList("Apple", "Banana", "Cherry");
197-
Object result = MVFindFunctionImpl.eval(array, "(?i)banana");
198-
assertEquals(1, result);
199-
}
200-
201-
@Test
202-
public void testMvfindWithNegatedCharacterClass() {
203-
List<Object> array = Arrays.asList("abc123", "abc", "123");
204-
Object result = MVFindFunctionImpl.eval(array, "[^0-9]+");
205-
assertEquals(0, result); // Matches "abc123" (has non-digits)
206-
}
207-
208-
@Test
209-
public void testMvfindWithEscapedSpecialChars() {
210-
List<Object> array = Arrays.asList("test.log", "test-log", "testlog");
211-
Object result = MVFindFunctionImpl.eval(array, "test\\.log");
212-
assertEquals(0, result);
213-
}
214-
215-
@Test
216-
public void testMvfindWithGroup() {
217-
List<Object> array = Arrays.asList("abc", "ababab", "abab");
218-
Object result = MVFindFunctionImpl.eval(array, "(ab)+");
219-
assertEquals(0, result);
220-
}
221-
222-
// Multiple matches - should return first
223-
224-
@Test
225-
public void testMvfindReturnsFirstMatch() {
226-
List<Object> array = Arrays.asList("apple", "apricot", "application");
227-
Object result = MVFindFunctionImpl.eval(array, "app");
228-
assertEquals(0, result); // Returns first match, not all
229-
}
230-
231-
@Test
232-
public void testMvfindWithMultipleMatchingElements() {
233-
List<Object> array = Arrays.asList("test1", "test2", "test3");
234-
Object result = MVFindFunctionImpl.eval(array, "test");
235-
assertEquals(0, result); // Returns first
236-
}
237-
238158
// Case sensitivity
239159

240160
@Test
@@ -244,13 +164,6 @@ public void testMvfindIsCaseSensitiveByDefault() {
244164
assertNull(result); // No match because case-sensitive
245165
}
246166

247-
@Test
248-
public void testMvfindWithExactCaseMatch() {
249-
List<Object> array = Arrays.asList("Apple", "banana", "Cherry");
250-
Object result = MVFindFunctionImpl.eval(array, "Apple");
251-
assertEquals(0, result);
252-
}
253-
254167
// Invalid regex patterns
255168

256169
@Test
@@ -263,16 +176,6 @@ public void testMvfindWithInvalidRegex() {
263176
});
264177
}
265178

266-
@Test
267-
public void testMvfindWithUnclosedGroup() {
268-
List<Object> array = Arrays.asList("test");
269-
assertThrows(
270-
RuntimeException.class,
271-
() -> {
272-
MVFindFunctionImpl.eval(array, "(unclosed");
273-
});
274-
}
275-
276179
// Type conversion tests
277180

278181
@Test
@@ -295,93 +198,4 @@ public void testMvfindWithBooleanElements() {
295198
Object result = MVFindFunctionImpl.eval(array, "false");
296199
assertEquals(1, result);
297200
}
298-
299-
@Test
300-
public void testMvfindNumericPattern() {
301-
List<Object> array = Arrays.asList("test123", "test456", "test");
302-
Object result = MVFindFunctionImpl.eval(array, "\\d{3}");
303-
assertEquals(0, result);
304-
}
305-
306-
// Special regex features
307-
308-
@Test
309-
public void testMvfindWithLookahead() {
310-
List<Object> array = Arrays.asList("test123", "test", "test456");
311-
Object result = MVFindFunctionImpl.eval(array, "test(?=\\d)");
312-
assertEquals(0, result);
313-
}
314-
315-
@Test
316-
public void testMvfindWithLookbehind() {
317-
List<Object> array = Arrays.asList("pretest", "test", "posttest");
318-
Object result = MVFindFunctionImpl.eval(array, "(?<=pre)test");
319-
assertEquals(0, result);
320-
}
321-
322-
@Test
323-
public void testMvfindWithNonCapturingGroup() {
324-
List<Object> array = Arrays.asList("apple", "application", "apply");
325-
Object result = MVFindFunctionImpl.eval(array, "app(?:le|ly)");
326-
assertEquals(0, result);
327-
}
328-
329-
// Edge cases with whitespace
330-
331-
@Test
332-
public void testMvfindWithWhitespace() {
333-
List<Object> array = Arrays.asList("hello world", "hello", "world");
334-
Object result = MVFindFunctionImpl.eval(array, "hello\\s+world");
335-
assertEquals(0, result);
336-
}
337-
338-
@Test
339-
public void testMvfindWithLeadingTrailingSpaces() {
340-
List<Object> array = Arrays.asList(" test ", "test", " test");
341-
Object result = MVFindFunctionImpl.eval(array, "^\\s+test");
342-
assertEquals(0, result);
343-
}
344-
345-
@Test
346-
public void testMvfindWithTab() {
347-
List<Object> array = Arrays.asList("hello\tworld", "hello world");
348-
Object result = MVFindFunctionImpl.eval(array, "hello\\tworld");
349-
assertEquals(0, result);
350-
}
351-
352-
// Unicode and special characters
353-
354-
@Test
355-
public void testMvfindWithUnicodeCharacters() {
356-
List<Object> array = Arrays.asList("café", "resume", "naïve");
357-
Object result = MVFindFunctionImpl.eval(array, "café");
358-
assertEquals(0, result);
359-
}
360-
361-
@Test
362-
public void testMvfindWithSpecialCharacters() {
363-
List<Object> array = Arrays.asList("hello@world", "hello#world", "hello$world");
364-
Object result = MVFindFunctionImpl.eval(array, "hello@world");
365-
assertEquals(0, result);
366-
}
367-
368-
// Boundary testing
369-
370-
@Test
371-
public void testMvfindWithVeryLongArray() {
372-
Object[] elements = new Object[1000];
373-
for (int i = 0; i < 1000; i++) {
374-
elements[i] = "element" + i;
375-
}
376-
List<Object> array = Arrays.asList(elements);
377-
Object result = MVFindFunctionImpl.eval(array, "element500");
378-
assertEquals(500, result);
379-
}
380-
381-
@Test
382-
public void testMvfindWithVeryLongPattern() {
383-
List<Object> array = Arrays.asList("a".repeat(1000));
384-
Object result = MVFindFunctionImpl.eval(array, "a{1000}");
385-
assertEquals(0, result);
386-
}
387201
}

ppl/src/test/java/org/opensearch/sql/ppl/calcite/CalcitePPLArrayFunctionTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,12 @@ public void testMvfindWithMatch() {
224224

225225
String expectedResult = "result=1\n";
226226
verifyResult(root, expectedResult);
227+
228+
String expectedSparkSql =
229+
"SELECT MVFIND(ARRAY('apple', 'banana', 'apricot'), 'ban.*') `result`\n"
230+
+ "FROM `scott`.`EMP`\n"
231+
+ "LIMIT 1";
232+
verifyPPLToSparkSQL(root, expectedSparkSql);
227233
}
228234

229235
@Test
@@ -235,6 +241,12 @@ public void testMvfindWithNoMatch() {
235241

236242
String expectedResult = "result=null\n";
237243
verifyResult(root, expectedResult);
244+
245+
String expectedSparkSql =
246+
"SELECT MVFIND(ARRAY('cat', 'dog', 'bird'), 'fish') `result`\n"
247+
+ "FROM `scott`.`EMP`\n"
248+
+ "LIMIT 1";
249+
verifyPPLToSparkSQL(root, expectedSparkSql);
238250
}
239251

240252
@Test
@@ -246,6 +258,12 @@ public void testMvfindWithFirstMatch() {
246258

247259
String expectedResult = "result=0\n";
248260
verifyResult(root, expectedResult);
261+
262+
String expectedSparkSql =
263+
"SELECT MVFIND(ARRAY('error123', 'info', 'error456'), 'err.*') `result`\n"
264+
+ "FROM `scott`.`EMP`\n"
265+
+ "LIMIT 1";
266+
verifyPPLToSparkSQL(root, expectedSparkSql);
249267
}
250268

251269
@Test
@@ -257,6 +275,12 @@ public void testMvfindWithMultipleMatches() {
257275

258276
String expectedResult = "result=0\n";
259277
verifyResult(root, expectedResult);
278+
279+
String expectedSparkSql =
280+
"SELECT MVFIND(ARRAY('test1', 'test2', 'test3'), 'test.*') `result`\n"
281+
+ "FROM `scott`.`EMP`\n"
282+
+ "LIMIT 1";
283+
verifyPPLToSparkSQL(root, expectedSparkSql);
260284
}
261285

262286
@Test
@@ -268,5 +292,11 @@ public void testMvfindWithComplexRegex() {
268292

269293
String expectedResult = "result=1\n";
270294
verifyResult(root, expectedResult);
295+
296+
String expectedSparkSql =
297+
"SELECT MVFIND(ARRAY('abc123', 'def456', 'ghi789'), 'def\\d+') `result`\n"
298+
+ "FROM `scott`.`EMP`\n"
299+
+ "LIMIT 1";
300+
verifyPPLToSparkSQL(root, expectedSparkSql);
271301
}
272302
}

ppl/src/test/java/org/opensearch/sql/ppl/utils/PPLQueryDataAnonymizerTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -891,4 +891,13 @@ public void testSpath() {
891891
anonymize(
892892
"search source=t | spath input=json_attr output=out path=foo.bar | fields id, out"));
893893
}
894+
895+
@Test
896+
public void testMvfind() {
897+
assertEquals(
898+
"source=table | eval identifier=mvfind(array(***,***,***),***) | fields + identifier",
899+
anonymize(
900+
"source=t | eval result=mvfind(array('apple', 'banana', 'apricot'), 'ban.*') | fields"
901+
+ " result"));
902+
}
894903
}

0 commit comments

Comments
 (0)