Skip to content

Commit

Permalink
Test coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Andy Kwok <[email protected]>
  • Loading branch information
andy-k-improving committed Jan 11, 2025
1 parent 302ed85 commit da7af99
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public class IPFunctions {

public void register(BuiltinFunctionRepository repository) {
repository.register(cidrmatch());
repository.register(geoIp());
}

private DefaultFunctionResolver cidrmatch() {
Expand All @@ -37,18 +36,6 @@ private DefaultFunctionResolver cidrmatch() {
impl(nullMissingHandling(IPFunctions::exprCidrMatch), BOOLEAN, IP, STRING));
}

private DefaultFunctionResolver geoIp() {
return define(
BuiltinFunctionName.GEOIP.getName(),
impl(nullMissingHandling(IPFunctions::exprGeoIp), BOOLEAN, STRING, STRING),
impl(
nullMissingHandling(IPFunctions::exprGeoIpWithOption),
BOOLEAN,
STRING,
STRING,
STRING));
}

/**
* Returns whether the given IP address is within the specified inclusive CIDR IP address range.
* Supports both IPv4 and IPv6 addresses.
Expand All @@ -71,12 +58,4 @@ private ExprValue exprCidrMatch(ExprValue addressExprValue, ExprValue rangeExprV
: ExprValueUtils.LITERAL_TRUE;
}

private ExprValue exprGeoIpWithOption(
ExprValue dataSource, ExprValue ipAddress, ExprValue option) {
return ExprValueUtils.booleanValue(true);
}

private ExprValue exprGeoIp(ExprValue dataSource, ExprValue ipAddress) {
return ExprValueUtils.booleanValue(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@

package org.opensearch.sql.expression.ip;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.opensearch.sql.data.type.ExprCoreType.BOOLEAN;
import static org.opensearch.sql.data.type.ExprCoreType.STRING;

import org.junit.jupiter.api.Test;
Expand All @@ -16,6 +18,7 @@
import org.opensearch.sql.data.model.ExprValue;
import org.opensearch.sql.expression.DSL;
import org.opensearch.sql.expression.Expression;
import org.opensearch.sql.expression.FunctionExpression;
import org.opensearch.sql.expression.env.Environment;

@ExtendWith(MockitoExtension.class)
Expand All @@ -34,4 +37,10 @@ public void geoIpDefaultImplementation() {
.valueOf(env));
assertTrue(exception.getMessage().matches(".*no default implementation available"));
}

@Test
public void testGeoipFnctionSignature() {
var geoip = DSL.geoip(DSL.literal("HARDCODED_DATASOURCE_NAME"), DSL.ref("ip_address", STRING));
assertEquals(BOOLEAN, geoip.type());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.opensearch.sql.ast.tree.Trendline;
import org.opensearch.sql.common.setting.Settings;
import org.opensearch.sql.data.model.ExprBooleanValue;
import org.opensearch.sql.data.type.ExprCoreType;
import org.opensearch.sql.expression.DSL;
import org.opensearch.sql.expression.Expression;
import org.opensearch.sql.expression.NamedExpression;
Expand All @@ -58,10 +59,12 @@
import org.opensearch.sql.expression.window.ranking.RankFunction;
import org.opensearch.sql.monitor.ResourceMonitor;
import org.opensearch.sql.opensearch.client.OpenSearchClient;
import org.opensearch.sql.opensearch.data.type.OpenSearchTextType;
import org.opensearch.sql.opensearch.data.value.OpenSearchExprValueFactory;
import org.opensearch.sql.opensearch.planner.physical.ADOperator;
import org.opensearch.sql.opensearch.planner.physical.MLCommonsOperator;
import org.opensearch.sql.opensearch.planner.physical.MLOperator;
import org.opensearch.sql.opensearch.planner.physical.OpenSearchEvalOperator;
import org.opensearch.sql.opensearch.request.OpenSearchRequest;
import org.opensearch.sql.opensearch.request.OpenSearchRequestBuilder;
import org.opensearch.sql.opensearch.setting.OpenSearchSettings;
Expand Down Expand Up @@ -339,6 +342,25 @@ public void test_visitTrendline() {
executionProtector.visitTrendline(trendlineOperator, null));
}

@Test
void test_visitOpenSearchEval() {
NodeClient nodeClient = mock(NodeClient.class);
OpenSearchEvalOperator evalOperator =
// ADOperator adOperator =
new OpenSearchEvalOperator(
values(emptyList()),
List.of(
ImmutablePair.of(
new ReferenceExpression("ageInAbs", OpenSearchTextType.of()),
DSL.abs(DSL.abs(new ReferenceExpression("age", ExprCoreType.LONG))))
),
nodeClient);

assertEquals(
executionProtector.doProtect(evalOperator), executionProtector.visitEval(evalOperator, null));
}


PhysicalPlan resourceMonitor(PhysicalPlan input) {
return new ResourceMonitorPlan(input, resourceMonitor);
}
Expand Down

0 comments on commit da7af99

Please sign in to comment.