Skip to content

Commit b04a92e

Browse files
Address PR feedback.
Signed-off-by: Yury-Fridlyand <yury.fridlyand@improving.com>
1 parent 8b0671c commit b04a92e

6 files changed

Lines changed: 28 additions & 27 deletions

File tree

core/src/main/java/org/opensearch/sql/data/type/ExprType.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ public interface ExprType {
2020
* Is compatible with other types.
2121
*/
2222
default boolean isCompatible(ExprType other) {
23-
// Do double direction check with `equals`, because a derived class may override it
24-
if (this.equals(other) || other.equals(this)) {
23+
other = other.getExprType();
24+
if (getExprType().equals(other)) {
2525
return true;
2626
} else {
2727
if (other.equals(UNKNOWN)) {
@@ -78,4 +78,11 @@ default String convertFieldForSearchQuery(String fieldName) {
7878
default Object convertValueForSearchQuery(ExprValue value) {
7979
return value.value();
8080
}
81+
82+
/**
83+
* Get a simplified type {@link ExprCoreType} if possible. Used in {@link #isCompatible}.
84+
*/
85+
default ExprType getExprType() {
86+
return this;
87+
}
8188
}

core/src/test/java/org/opensearch/sql/data/type/ExprTypeTest.java

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,6 @@ public void isCompatible() {
5050
assertTrue(DATETIME.isCompatible(STRING));
5151
}
5252

53-
@Test
54-
public void isCompatibleTwoDirectionCheck() {
55-
ExprType other = new ExprType() {
56-
@Override
57-
public String typeName() {
58-
return null;
59-
}
60-
61-
@Override
62-
public boolean equals(Object obj) {
63-
return true;
64-
}
65-
};
66-
assertTrue(UNDEFINED.isCompatible(other));
67-
}
68-
6953
@Test
7054
public void isNotCompatible() {
7155
assertFalse(INTEGER.isCompatible(DOUBLE));
2.02 KB
Loading

docs/dev/query-type-conversion.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ Compiling time:
8585
Unresolved signature: equal(BOOL, STRING)
8686
Resovled signature: equal(BOOL, BOOL)
8787
Function builder: 1) returns equal(BOOL, cast_to_bool(STRING)) impl
88-
1) returns equal(BOOL, BOOL) impl
88+
2) returns equal(BOOL, BOOL) impl
8989
Runtime:
9090
equal impl: false.equals(cast_to_bool('FALSE'))
9191
cast_to_bool impl: Boolean.valueOf('FALSE')

opensearch/src/main/java/org/opensearch/sql/opensearch/data/type/OpenSearchDataType.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.util.function.BiConsumer;
1616
import lombok.Getter;
1717
import org.apache.commons.lang3.EnumUtils;
18+
import org.opensearch.sql.data.model.ExprValue;
1819
import org.opensearch.sql.data.type.ExprCoreType;
1920
import org.opensearch.sql.data.type.ExprType;
2021
import org.opensearch.sql.data.type.WideningTypeRule;
@@ -28,6 +29,7 @@ public class OpenSearchDataType implements ExprType, Serializable {
2829
* Redefine comparison operation: class (or derived) could be compared with ExprCoreType too.
2930
* Used in {@link WideningTypeRule#distance(ExprType, ExprType)}.
3031
*/
32+
@Override
3133
public boolean equals(final Object o) {
3234
if (o == this) {
3335
return true;
@@ -45,6 +47,10 @@ public boolean equals(final Object o) {
4547
return exprCoreType.equals(other.exprCoreType);
4648
}
4749

50+
/**
51+
* Redefine hash calculation to enforce comparing using {@link #equals(Object)}.
52+
*/
53+
@Override
4854
public int hashCode() {
4955
return 42 + exprCoreType.hashCode();
5056
}
@@ -120,6 +126,7 @@ public String toString() {
120126
* To avoid returning `UNKNOWN` for `OpenSearch*Type`s, e.g. for IP, returns itself.
121127
* @return An {@link ExprType}.
122128
*/
129+
@Override
123130
public ExprType getExprType() {
124131
if (exprCoreType != ExprCoreType.UNKNOWN) {
125132
return exprCoreType;
@@ -292,7 +299,7 @@ protected OpenSearchDataType cloneEmpty() {
292299
/**
293300
* Flattens mapping tree into a single layer list of objects (pairs of name-types actually),
294301
* which don't have nested types.
295-
* See {@see OpenSearchDataTypeTest#traverseAndFlatten() test} for example.
302+
* See <em>OpenSearchDataTypeTest::traverseAndFlatten()</em> test for example.
296303
* @param tree A list of `OpenSearchDataType`s - map between field name and its type.
297304
* @return A list of all `OpenSearchDataType`s from given map on the same nesting level (1).
298305
* Nested object names are prefixed by names of their host.

opensearch/src/test/java/org/opensearch/sql/opensearch/data/type/OpenSearchDataTypeTest.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.junit.jupiter.params.provider.Arguments;
4343
import org.junit.jupiter.params.provider.EnumSource;
4444
import org.junit.jupiter.params.provider.MethodSource;
45+
import org.opensearch.sql.data.model.ExprDoubleValue;
4546
import org.opensearch.sql.data.type.ExprCoreType;
4647
import org.opensearch.sql.data.type.ExprType;
4748

@@ -450,12 +451,14 @@ private Map<String, OpenSearchDataType> getSampleMapping() {
450451

451452
@Test
452453
public void getExprType() {
453-
assertEquals(OpenSearchTextType.of(),
454-
OpenSearchDataType.of(MappingType.Text).getExprType());
455-
assertEquals(FLOAT, OpenSearchDataType.of(MappingType.Float).getExprType());
456-
assertEquals(FLOAT, OpenSearchDataType.of(MappingType.HalfFloat).getExprType());
457-
assertEquals(DOUBLE, OpenSearchDataType.of(MappingType.Double).getExprType());
458-
assertEquals(DOUBLE, OpenSearchDataType.of(MappingType.ScaledFloat).getExprType());
459-
assertEquals(TIMESTAMP, OpenSearchDataType.of(MappingType.Date).getExprType());
454+
assertAll(
455+
() -> assertEquals(OpenSearchTextType.of(),
456+
OpenSearchDataType.of(MappingType.Text).getExprType()),
457+
() -> assertEquals(FLOAT, OpenSearchDataType.of(MappingType.Float).getExprType()),
458+
() -> assertEquals(FLOAT, OpenSearchDataType.of(MappingType.HalfFloat).getExprType()),
459+
() -> assertEquals(DOUBLE, OpenSearchDataType.of(MappingType.Double).getExprType()),
460+
() -> assertEquals(DOUBLE, OpenSearchDataType.of(MappingType.ScaledFloat).getExprType()),
461+
() -> assertEquals(TIMESTAMP, OpenSearchDataType.of(MappingType.Date).getExprType())
462+
);
460463
}
461464
}

0 commit comments

Comments
 (0)