Skip to content

Commit

Permalink
Moved escaping wildcard test to common/utils
Browse files Browse the repository at this point in the history
Signed-off-by: Guian Gumpac <[email protected]>
  • Loading branch information
Guian Gumpac committed Nov 25, 2022
1 parent 86fa737 commit b54a934
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -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("\\%\\*\\_\\?"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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<Expression> arguments) {
super(WildcardQueryTest.this.wildcardQueryFunc, arguments);
Expand Down

0 comments on commit b54a934

Please sign in to comment.