|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.sql.opensearch.storage; |
| 7 | + |
| 8 | +import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
| 9 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 10 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 11 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 12 | + |
| 13 | +import java.util.List; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 16 | +import org.mockito.Mock; |
| 17 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 18 | +import org.opensearch.sql.common.setting.Settings; |
| 19 | +import org.opensearch.sql.data.type.ExprCoreType; |
| 20 | +import org.opensearch.sql.exception.ExpressionEvaluationException; |
| 21 | +import org.opensearch.sql.expression.DSL; |
| 22 | +import org.opensearch.sql.expression.Expression; |
| 23 | +import org.opensearch.sql.expression.function.FunctionName; |
| 24 | +import org.opensearch.sql.opensearch.client.OpenSearchClient; |
| 25 | +import org.opensearch.sql.storage.Table; |
| 26 | + |
| 27 | +@ExtendWith(MockitoExtension.class) |
| 28 | +class VectorSearchTableFunctionImplementationTest { |
| 29 | + |
| 30 | + @Mock private OpenSearchClient client; |
| 31 | + |
| 32 | + @Mock private Settings settings; |
| 33 | + |
| 34 | + @Test |
| 35 | + void testValueOfThrows() { |
| 36 | + VectorSearchTableFunctionImplementation impl = createImpl(); |
| 37 | + UnsupportedOperationException ex = |
| 38 | + assertThrows(UnsupportedOperationException.class, () -> impl.valueOf()); |
| 39 | + assertTrue(ex.getMessage().contains("only supported in FROM clause")); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + void testType() { |
| 44 | + VectorSearchTableFunctionImplementation impl = createImpl(); |
| 45 | + assertEquals(ExprCoreType.STRUCT, impl.type()); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + void testToString() { |
| 50 | + VectorSearchTableFunctionImplementation impl = createImpl(); |
| 51 | + String str = impl.toString(); |
| 52 | + assertTrue(str.contains("vectorsearch")); |
| 53 | + assertTrue(str.contains("table=")); |
| 54 | + assertTrue(str.contains("my-index")); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + void testApplyArguments() { |
| 59 | + VectorSearchTableFunctionImplementation impl = createImpl(); |
| 60 | + Table table = impl.applyArguments(); |
| 61 | + assertTrue(table instanceof VectorSearchIndex); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + void testApplyArgumentsWithBracketedVector() { |
| 66 | + VectorSearchTableFunctionImplementation impl = |
| 67 | + createImplWithArgs("my-index", "embedding", "[1.0, 2.0, 3.0]", "k=5"); |
| 68 | + Table table = impl.applyArguments(); |
| 69 | + assertTrue(table instanceof VectorSearchIndex); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void testApplyArgumentsWithUnbracketedVector() { |
| 74 | + VectorSearchTableFunctionImplementation impl = |
| 75 | + createImplWithArgs("my-index", "embedding", "1.0, 2.0, 3.0", "k=5"); |
| 76 | + Table table = impl.applyArguments(); |
| 77 | + assertTrue(table instanceof VectorSearchIndex); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + void testApplyArgumentsWithComplexOptions() { |
| 82 | + VectorSearchTableFunctionImplementation impl = |
| 83 | + createImplWithArgs("my-index", "embedding", "[1.0, 2.0]", "k=10,method.ef_search=100"); |
| 84 | + Table table = impl.applyArguments(); |
| 85 | + assertTrue(table instanceof VectorSearchIndex); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + void testMissingKOptionThrows() { |
| 90 | + VectorSearchTableFunctionImplementation impl = |
| 91 | + createImplWithArgs("my-index", "embedding", "[1.0, 2.0]", "method.ef_search=100"); |
| 92 | + ExpressionEvaluationException ex = |
| 93 | + assertThrows(ExpressionEvaluationException.class, () -> impl.applyArguments()); |
| 94 | + assertEquals("Missing required option: k", ex.getMessage()); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + void testMissingArgumentThrows() { |
| 99 | + FunctionName functionName = FunctionName.of("vectorsearch"); |
| 100 | + List<Expression> args = |
| 101 | + List.of( |
| 102 | + DSL.namedArgument("table", DSL.literal("my-index")), |
| 103 | + DSL.namedArgument("field", DSL.literal("embedding")), |
| 104 | + DSL.namedArgument("vector", DSL.literal("[1.0, 2.0]"))); |
| 105 | + VectorSearchTableFunctionImplementation impl = |
| 106 | + new VectorSearchTableFunctionImplementation(functionName, args, client, settings); |
| 107 | + ExpressionEvaluationException ex = |
| 108 | + assertThrows(ExpressionEvaluationException.class, () -> impl.applyArguments()); |
| 109 | + assertEquals("Missing required argument: option", ex.getMessage()); |
| 110 | + } |
| 111 | + |
| 112 | + private VectorSearchTableFunctionImplementation createImpl() { |
| 113 | + return createImplWithArgs("my-index", "embedding", "[1.0, 2.0, 3.0]", "k=5"); |
| 114 | + } |
| 115 | + |
| 116 | + private VectorSearchTableFunctionImplementation createImplWithArgs( |
| 117 | + String table, String field, String vector, String option) { |
| 118 | + FunctionName functionName = FunctionName.of("vectorsearch"); |
| 119 | + List<Expression> args = |
| 120 | + List.of( |
| 121 | + DSL.namedArgument("table", DSL.literal(table)), |
| 122 | + DSL.namedArgument("field", DSL.literal(field)), |
| 123 | + DSL.namedArgument("vector", DSL.literal(vector)), |
| 124 | + DSL.namedArgument("option", DSL.literal(option))); |
| 125 | + return new VectorSearchTableFunctionImplementation(functionName, args, client, settings); |
| 126 | + } |
| 127 | +} |
0 commit comments