diff --git a/common/src/test/java/org/opensearch/sql/common/utils/ConvertSQLWildcardTest.java b/common/src/test/java/org/opensearch/sql/common/utils/ConvertSQLWildcardTest.java new file mode 100644 index 0000000000..bad877780d --- /dev/null +++ b/common/src/test/java/org/opensearch/sql/common/utils/ConvertSQLWildcardTest.java @@ -0,0 +1,29 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.sql.common.utils; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class ConvertSQLWildcardTest { + @Test + public void test_escaping_sql_wildcards() { + assertEquals("%", StringUtils.convertSqlWildcardToLucene("\\%")); + assertEquals("\\*", StringUtils.convertSqlWildcardToLucene("\\*")); + assertEquals("_", StringUtils.convertSqlWildcardToLucene("\\_")); + assertEquals("\\?", StringUtils.convertSqlWildcardToLucene("\\?")); + assertEquals("%*", StringUtils.convertSqlWildcardToLucene("\\%%")); + assertEquals("*%", StringUtils.convertSqlWildcardToLucene("%\\%")); + assertEquals("%*%", StringUtils.convertSqlWildcardToLucene("\\%%\\%")); + assertEquals("*%*", StringUtils.convertSqlWildcardToLucene("%\\%%")); + assertEquals("_?", StringUtils.convertSqlWildcardToLucene("\\__")); + assertEquals("?_", StringUtils.convertSqlWildcardToLucene("_\\_")); + assertEquals("_?_", StringUtils.convertSqlWildcardToLucene("\\__\\_")); + assertEquals("?_?", StringUtils.convertSqlWildcardToLucene("_\\__")); + assertEquals("%\\*_\\?", StringUtils.convertSqlWildcardToLucene("\\%\\*\\_\\?")); + } +} diff --git a/opensearch/src/main/java/org/opensearch/sql/opensearch/storage/script/filter/lucene/LikeQuery.java b/opensearch/src/main/java/org/opensearch/sql/opensearch/storage/script/filter/lucene/LikeQuery.java index b887d92314..ed4d48f71e 100644 --- a/opensearch/src/main/java/org/opensearch/sql/opensearch/storage/script/filter/lucene/LikeQuery.java +++ b/opensearch/src/main/java/org/opensearch/sql/opensearch/storage/script/filter/lucene/LikeQuery.java @@ -18,7 +18,7 @@ public class LikeQuery extends LuceneQuery { @Override public QueryBuilder build(FunctionExpression func) { ReferenceExpression ref = (ReferenceExpression) func.getArguments().get(0); - String field = convertTextToKeyword(ref.toString(), ref.type()); + String field = convertTextToKeyword(ref.getAttr(), ref.type()); Expression expr = func.getArguments().get(1); ExprValue literalValue = expr.valueOf(); return createBuilder(field, literalValue.stringValue()); diff --git a/opensearch/src/test/java/org/opensearch/sql/opensearch/storage/script/filter/lucene/WildcardQueryTest.java b/opensearch/src/test/java/org/opensearch/sql/opensearch/storage/script/filter/lucene/WildcardQueryTest.java index 287c1557e4..ce7a39d91a 100644 --- a/opensearch/src/test/java/org/opensearch/sql/opensearch/storage/script/filter/lucene/WildcardQueryTest.java +++ b/opensearch/src/test/java/org/opensearch/sql/opensearch/storage/script/filter/lucene/WildcardQueryTest.java @@ -5,7 +5,6 @@ package org.opensearch.sql.opensearch.storage.script.filter.lucene; -import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.opensearch.sql.expression.DSL.namedArgument; @@ -18,7 +17,6 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; import org.opensearch.sql.common.antlr.SyntaxCheckException; -import org.opensearch.sql.common.utils.StringUtils; import org.opensearch.sql.data.model.ExprValue; import org.opensearch.sql.data.type.ExprType; import org.opensearch.sql.exception.SemanticCheckException; @@ -76,23 +74,6 @@ public void test_SemanticCheckException_when_invalid_parameter() { () -> wildcardQueryQuery.build(new WildcardQueryExpression(arguments))); } - @Test - public void test_escaping_sql_wildcards() { - assertEquals("%", StringUtils.convertSqlWildcardToLucene("\\%")); - assertEquals("\\*", StringUtils.convertSqlWildcardToLucene("\\*")); - assertEquals("_", StringUtils.convertSqlWildcardToLucene("\\_")); - assertEquals("\\?", StringUtils.convertSqlWildcardToLucene("\\?")); - assertEquals("%*", StringUtils.convertSqlWildcardToLucene("\\%%")); - assertEquals("*%", StringUtils.convertSqlWildcardToLucene("%\\%")); - assertEquals("%*%", StringUtils.convertSqlWildcardToLucene("\\%%\\%")); - assertEquals("*%*", StringUtils.convertSqlWildcardToLucene("%\\%%")); - assertEquals("_?", StringUtils.convertSqlWildcardToLucene("\\__")); - assertEquals("?_", StringUtils.convertSqlWildcardToLucene("_\\_")); - assertEquals("_?_", StringUtils.convertSqlWildcardToLucene("\\__\\_")); - assertEquals("?_?", StringUtils.convertSqlWildcardToLucene("_\\__")); - assertEquals("%\\*_\\?", StringUtils.convertSqlWildcardToLucene("\\%\\*\\_\\?")); - } - private class WildcardQueryExpression extends FunctionExpression { public WildcardQueryExpression(List arguments) { super(WildcardQueryTest.this.wildcardQueryFunc, arguments);