forked from opensearch-project/sql
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Andy Kwok <[email protected]>
- Loading branch information
1 parent
a135f1d
commit 6abfd6d
Showing
23 changed files
with
669 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
core/src/main/java/org/opensearch/sql/expression/ip/GeoIPFunctions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.sql.expression.ip; | ||
|
||
import static org.opensearch.sql.data.type.ExprCoreType.BOOLEAN; | ||
import static org.opensearch.sql.data.type.ExprCoreType.STRING; | ||
import static org.opensearch.sql.expression.function.FunctionDSL.define; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import lombok.experimental.UtilityClass; | ||
import org.apache.commons.lang3.tuple.Pair; | ||
import org.opensearch.sql.data.type.ExprType; | ||
import org.opensearch.sql.expression.function.BuiltinFunctionName; | ||
import org.opensearch.sql.expression.function.BuiltinFunctionRepository; | ||
import org.opensearch.sql.expression.function.DefaultFunctionResolver; | ||
import org.opensearch.sql.expression.function.FunctionBuilder; | ||
import org.opensearch.sql.expression.function.FunctionName; | ||
import org.opensearch.sql.expression.function.FunctionSignature; | ||
import org.opensearch.sql.expression.function.SerializableFunction; | ||
|
||
/** | ||
* Utility class to register the method signature for geoip( ) expression, concreted reallocated to | ||
* `opensearch` module, as this Ip location require GeoSpatial Plugin runtime support. | ||
*/ | ||
@UtilityClass | ||
public class GeoIPFunctions { | ||
|
||
public void register(BuiltinFunctionRepository repository) { | ||
repository.register(geoIp()); | ||
} | ||
|
||
/** | ||
* To register all method signatures related to geoip( ) expression under eval. | ||
* | ||
* @return Resolver for geoip( ) expression. | ||
*/ | ||
private DefaultFunctionResolver geoIp() { | ||
return define( | ||
BuiltinFunctionName.GEOIP.getName(), | ||
openSearchImpl(BOOLEAN, Arrays.asList(STRING, STRING)), | ||
openSearchImpl(BOOLEAN, Arrays.asList(STRING, STRING, STRING))); | ||
} | ||
|
||
/** | ||
* Util method to generate probe implementation with given list of argument types, with marker | ||
* class `OpenSearchFunctionExpression` to annotate this is an OpenSearch specific expression. | ||
* | ||
* @param returnType return type. | ||
* @return Binary Function Implementation. | ||
*/ | ||
public static SerializableFunction<FunctionName, Pair<FunctionSignature, FunctionBuilder>> | ||
openSearchImpl(ExprType returnType, List<ExprType> args) { | ||
return functionName -> { | ||
FunctionSignature functionSignature = new FunctionSignature(functionName, args); | ||
FunctionBuilder functionBuilder = | ||
(functionProperties, arguments) -> | ||
new OpenSearchFunctionExpression(functionName, arguments, returnType); | ||
return Pair.of(functionSignature, functionBuilder); | ||
}; | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
core/src/main/java/org/opensearch/sql/expression/ip/OpenSearchFunctionExpression.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
*/ | ||
|
||
package org.opensearch.sql.expression.ip; | ||
|
||
import java.util.List; | ||
import org.opensearch.sql.data.model.ExprValue; | ||
import org.opensearch.sql.data.type.ExprType; | ||
import org.opensearch.sql.expression.Expression; | ||
import org.opensearch.sql.expression.FunctionExpression; | ||
import org.opensearch.sql.expression.env.Environment; | ||
import org.opensearch.sql.expression.function.FunctionName; | ||
|
||
/** | ||
* Marker class to identify functions only compatible with OpenSearch storage engine. Any attempt to | ||
* invoke the method different from OpenSearch will result in UnsupportedOperationException. | ||
*/ | ||
public class OpenSearchFunctionExpression extends FunctionExpression { | ||
|
||
private final ExprType returnType; | ||
|
||
public OpenSearchFunctionExpression( | ||
FunctionName functionName, List<Expression> arguments, ExprType returnType) { | ||
super(functionName, arguments); | ||
this.returnType = returnType; | ||
} | ||
|
||
@Override | ||
public ExprValue valueOf() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public ExprValue valueOf(Environment<Expression, ExprValue> valueEnv) { | ||
throw new UnsupportedOperationException( | ||
"OpenSearch runtime specific function, no default implementation available"); | ||
} | ||
|
||
@Override | ||
public ExprType type() { | ||
return returnType; | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
core/src/test/java/org/opensearch/sql/expression/ip/GeoIPFunctionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.sql.expression.ip; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
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; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.opensearch.sql.data.model.ExprValue; | ||
import org.opensearch.sql.expression.DSL; | ||
import org.opensearch.sql.expression.Expression; | ||
import org.opensearch.sql.expression.env.Environment; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
public class GeoIPFunctionTest { | ||
|
||
// Mock value environment for testing. | ||
@Mock private Environment<Expression, ExprValue> env; | ||
|
||
@Test | ||
public void geoIpDefaultImplementation() { | ||
UnsupportedOperationException exception = | ||
assertThrows( | ||
UnsupportedOperationException.class, | ||
() -> | ||
DSL.geoip(DSL.literal("HARDCODED_DATASOURCE_NAME"), DSL.ref("ip_address", STRING)) | ||
.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()); | ||
} | ||
|
||
/** To make sure no logic being evaluated when no environment being passed. */ | ||
@Test | ||
public void testDefaultValueOf() { | ||
var geoip = DSL.geoip(DSL.literal("HARDCODED_DATASOURCE_NAME"), DSL.ref("ip_address", STRING)); | ||
assertNull(geoip.valueOf()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.