diff --git a/core/src/main/java/org/opensearch/sql/data/model/ExprIpValue.java b/core/src/main/java/org/opensearch/sql/data/model/ExprIpValue.java index 6207796fcb..37d0d1a955 100644 --- a/core/src/main/java/org/opensearch/sql/data/model/ExprIpValue.java +++ b/core/src/main/java/org/opensearch/sql/data/model/ExprIpValue.java @@ -34,7 +34,9 @@ public ExprIpValue(String s) { IPAddress address = new IPAddressString(s, validationOptions).toAddress(); // Convert IPv6 mapped IPv4 addresses to IPv4 - if (address.isIPv4Convertible()) address = address.toIPv4(); + if (address.isIPv4Convertible()) { + address = address.toIPv4(); + } value = address; } catch (AddressStringException e) { @@ -55,7 +57,6 @@ public ExprType type() { @Override public int compare(ExprValue other) { - assert (other instanceof ExprIpValue); // Map IPv4 addresses to IPv6 for comparison IPv6Address ipv6Value = toIPv6Address(value); diff --git a/core/src/test/java/org/opensearch/sql/data/model/ExprIpValueTest.java b/core/src/test/java/org/opensearch/sql/data/model/ExprIpValueTest.java index 0e248cf05f..8a2f517920 100644 --- a/core/src/test/java/org/opensearch/sql/data/model/ExprIpValueTest.java +++ b/core/src/test/java/org/opensearch/sql/data/model/ExprIpValueTest.java @@ -6,12 +6,14 @@ package org.opensearch.sql.data.model; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.List; import org.junit.jupiter.api.Test; import org.opensearch.sql.data.type.ExprCoreType; +import org.opensearch.sql.exception.ExpressionEvaluationException; import org.opensearch.sql.exception.SemanticCheckException; public class ExprIpValueTest { @@ -74,6 +76,9 @@ public void testType() { @Test public void testCompare() { + Exception ex; + + // Compare to IP address. ipv4LesserStrings.forEach( (s) -> assertTrue(exprIpv4Value.compareTo(ExprValueUtils.ipValue(s)) > 0)); ipv4EqualStrings.forEach( @@ -86,12 +91,40 @@ public void testCompare() { (s) -> assertEquals(0, exprIpv6Value.compareTo(ExprValueUtils.ipValue(s)))); ipv6GreaterStrings.forEach( (s) -> assertTrue(exprIpv6Value.compareTo(ExprValueUtils.ipValue(s)) < 0)); + + // Compare to null/missing value. + ex = + assertThrows( + IllegalStateException.class, + () -> exprIpv4Value.compareTo(ExprValueUtils.LITERAL_NULL)); + assertEquals("[BUG] Unreachable, Comparing with NULL or MISSING is undefined", ex.getMessage()); + + ex = + assertThrows( + IllegalStateException.class, + () -> exprIpv4Value.compareTo(ExprValueUtils.LITERAL_MISSING)); + assertEquals("[BUG] Unreachable, Comparing with NULL or MISSING is undefined", ex.getMessage()); + + // Compare to other data type. + ex = + assertThrows( + ExpressionEvaluationException.class, + () -> exprIpv4Value.compareTo(ExprValueUtils.LITERAL_TRUE)); + assertEquals("compare expected value have same type, but with [IP, BOOLEAN]", ex.getMessage()); } @Test public void testEquals() { + assertEquals(exprIpv4Value, exprIpv4Value); + assertNotEquals(exprIpv4Value, new Object()); + assertNotEquals(exprIpv4Value, ExprValueUtils.LITERAL_NULL); + assertNotEquals(exprIpv4Value, ExprValueUtils.LITERAL_MISSING); + ipv4EqualStrings.forEach((s) -> assertEquals(exprIpv4Value, ExprValueUtils.ipValue(s))); ipv6EqualStrings.forEach((s) -> assertEquals(exprIpv6Value, ExprValueUtils.ipValue(s))); + + ipv4LesserStrings.forEach((s) -> assertNotEquals(exprIpv4Value, ExprValueUtils.ipValue(s))); + ipv6GreaterStrings.forEach((s) -> assertNotEquals(exprIpv6Value, ExprValueUtils.ipValue(s))); } @Test