Skip to content

Commit 5e6b90a

Browse files
Rework on OpenSearchDataType: parse, store and use mapping information (#180)
Rework on `OpenSearchDataType`: * Add data types for those classes are not defined in `ExprCoreType`. * Address #180 (comment) * Remove `TextKeywordValue`. * Add changes according to the PR review. #180 * Update `IndexMapping::parseMapping` function. * Add `OpenSearchDataType::resolve` function. * Add new constructor for `OpenSearchTextType`. * Make `fields` and `properties` in `OpenSearchDataType` readonly. Update tests and mapping parser. * Move `getFields` from `OpenSearchDataType` to `OpenSearchTextType`. Update tests. * Rewrite `traverseAndFlatten` according to #180 (comment) * Minor comment fix. * A fix to avoid breaking changes. * `typeName` and `legacyTypeName` to return different type names. * Change `typeof` function and corresponding tests. * Move `convertTextToKeyword` from `ScriptUtils` to `OpenSearchTextType`. Update tests. * Update UT for `typeof` function. * Make all instances of `OpenSearchDataType` and of derived types singletones as much as possible. * Make string representations of all `ExprType`s uppercase. * Remove functions from `IndexMapping` used in tests only. Signed-off-by: Yury-Fridlyand <[email protected]> Signed-off-by: Yury-Fridlyand <[email protected]>
1 parent 662a938 commit 5e6b90a

File tree

52 files changed

+1309
-596
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+1309
-596
lines changed

core/src/main/java/org/opensearch/sql/data/model/AbstractExprValue.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public int compareTo(ExprValue other) {
2121
throw new IllegalStateException(
2222
String.format("[BUG] Unreachable, Comparing with NULL or MISSING is undefined"));
2323
}
24-
if ((this.isNumber() && other.isNumber()) || this.type() == other.type()) {
24+
if ((this.isNumber() && other.isNumber()) || this.type().equals(other.type())) {
2525
return compare(other);
2626
} else {
2727
throw new ExpressionEvaluationException(

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ public enum ExprCoreType implements ExprType {
8181
*/
8282
private static final Map<ExprCoreType, String> LEGACY_TYPE_NAME_MAPPING =
8383
new ImmutableMap.Builder<ExprCoreType, String>()
84-
.put(STRUCT, "object")
85-
.put(ARRAY, "nested")
86-
.put(STRING, "keyword")
84+
.put(STRUCT, "OBJECT")
85+
.put(ARRAY, "NESTED")
86+
.put(STRING, "KEYWORD")
8787
.build();
8888

8989
private static final Set<ExprType> NUMBER_TYPES =

core/src/main/java/org/opensearch/sql/expression/system/SystemFunctions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public Pair<FunctionSignature, FunctionBuilder> resolve(
4141
arguments -> new FunctionExpression(BuiltinFunctionName.TYPEOF.getName(), arguments) {
4242
@Override
4343
public ExprValue valueOf(Environment<Expression, ExprValue> valueEnv) {
44-
return new ExprStringValue(getArguments().get(0).type().toString());
44+
return new ExprStringValue(getArguments().get(0).type().legacyTypeName());
4545
}
4646

4747
@Override

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ public void getParent() {
7676

7777
@Test
7878
void legacyName() {
79-
assertEquals("keyword", STRING.legacyTypeName());
80-
assertEquals("nested", ARRAY.legacyTypeName());
81-
assertEquals("object", STRUCT.legacyTypeName());
79+
assertEquals("KEYWORD", STRING.legacyTypeName());
80+
assertEquals("NESTED", ARRAY.legacyTypeName());
81+
assertEquals("OBJECT", STRUCT.legacyTypeName());
8282
assertEquals("integer", INTEGER.legacyTypeName().toLowerCase());
8383
}
8484

core/src/test/java/org/opensearch/sql/expression/system/SystemFunctionsTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public class SystemFunctionsTest {
4747
void typeof() {
4848
assertEquals(STRING, dsl.typeof(DSL.literal(1)).type());
4949

50-
assertEquals("ARRAY", typeofGetValue(new ExprCollectionValue(List.of())));
50+
assertEquals("NESTED", typeofGetValue(new ExprCollectionValue(List.of())));
5151
assertEquals("BOOLEAN", typeofGetValue(ExprBooleanValue.of(false)));
5252
assertEquals("BYTE", typeofGetValue(new ExprByteValue(0)));
5353
assertEquals("DATE", typeofGetValue(new ExprDateValue(LocalDate.now())));
@@ -58,8 +58,8 @@ void typeof() {
5858
assertEquals("INTERVAL", typeofGetValue(new ExprIntervalValue(Duration.ofDays(0))));
5959
assertEquals("LONG", typeofGetValue(new ExprLongValue(0)));
6060
assertEquals("SHORT", typeofGetValue(new ExprShortValue(0)));
61-
assertEquals("STRING", typeofGetValue(new ExprStringValue("")));
62-
assertEquals("STRUCT", typeofGetValue(new ExprTupleValue(new LinkedHashMap<>())));
61+
assertEquals("KEYWORD", typeofGetValue(new ExprStringValue("")));
62+
assertEquals("OBJECT", typeofGetValue(new ExprTupleValue(new LinkedHashMap<>())));
6363
assertEquals("TIME", typeofGetValue(new ExprTimeValue(LocalTime.now())));
6464
assertEquals("TIMESTAMP", typeofGetValue(new ExprTimestampValue(Instant.now())));
6565
assertEquals("UNDEFINED", typeofGetValue(ExprNullValue.of()));

docs/user/dql/functions.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3125,5 +3125,5 @@ Example::
31253125
+----------------+---------------+-----------------+------------------+
31263126
| typeof(date) | typeof(int) | typeof(now()) | typeof(column) |
31273127
|----------------+---------------+-----------------+------------------|
3128-
| DATE | INTEGER | DATETIME | STRUCT |
3128+
| DATE | INTEGER | DATETIME | OBJECT |
31293129
+----------------+---------------+-----------------+------------------+

docs/user/ppl/functions/system.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@ Example::
2727
+----------------+---------------+-----------------+------------------+
2828
| typeof(date) | typeof(int) | typeof(now()) | typeof(column) |
2929
|----------------+---------------+-----------------+------------------|
30-
| DATE | INTEGER | DATETIME | STRUCT |
30+
| DATE | INTEGER | DATETIME | OBJECT |
3131
+----------------+---------------+-----------------+------------------+

integ-test/src/test/java/org/opensearch/sql/ppl/SystemFunctionIT.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public void typeof_sql_types() throws IOException {
3333
TEST_INDEX_DATATYPE_NUMERIC));
3434
// TODO: test null in PPL
3535
verifyDataRows(response,
36-
rows("STRING", "DOUBLE", "INTEGER", "LONG", "INTERVAL"));
36+
rows("KEYWORD", "DOUBLE", "INTEGER", "LONG", "INTERVAL"));
3737

3838
response = executeQuery(String.format("source=%s | eval "
3939
+ "`timestamp` = typeof(CAST('1961-04-12 09:07:00' AS TIMESTAMP)),"
@@ -68,7 +68,7 @@ public void typeof_opensearch_types() throws IOException {
6868
+ " | fields `text`, `date`, `boolean`, `object`, `keyword`, `ip`, `binary`, `geo_point`",
6969
TEST_INDEX_DATATYPE_NONNUMERIC));
7070
verifyDataRows(response,
71-
rows("OPENSEARCH_TEXT", "TIMESTAMP", "BOOLEAN", "STRUCT", "STRING",
72-
"OPENSEARCH_IP", "OPENSEARCH_BINARY", "OPENSEARCH_GEO_POINT"));
71+
rows("TEXT", "TIMESTAMP", "BOOLEAN", "OBJECT", "KEYWORD",
72+
"IP", "BINARY", "GEO_POINT"));
7373
}
7474
}

integ-test/src/test/java/org/opensearch/sql/sql/SystemFunctionIT.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public void typeof_sql_types() {
2727
JSONObject response = executeJdbcRequest("SELECT typeof('pewpew'), typeof(NULL), typeof(1.0),"
2828
+ "typeof(12345), typeof(1234567891011), typeof(INTERVAL 2 DAY);");
2929
verifyDataRows(response,
30-
rows("STRING", "UNDEFINED", "DOUBLE", "INTEGER", "LONG", "INTERVAL"));
30+
rows("KEYWORD", "UNDEFINED", "DOUBLE", "INTEGER", "LONG", "INTERVAL"));
3131

3232
response = executeJdbcRequest("SELECT"
3333
+ " typeof(CAST('1961-04-12 09:07:00' AS TIMESTAMP)),"
@@ -54,7 +54,7 @@ public void typeof_opensearch_types() {
5454
//+ ", typeof(nested_value)"
5555
+ " from %s;", TEST_INDEX_DATATYPE_NONNUMERIC));
5656
verifyDataRows(response,
57-
rows("OPENSEARCH_TEXT", "TIMESTAMP", "BOOLEAN", "STRUCT", "STRING",
58-
"OPENSEARCH_IP", "OPENSEARCH_BINARY", "OPENSEARCH_GEO_POINT"));
57+
rows("TEXT", "TIMESTAMP", "BOOLEAN", "OBJECT", "KEYWORD",
58+
"IP", "BINARY", "GEO_POINT"));
5959
}
6060
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
7+
package org.opensearch.sql.opensearch.data.type;
8+
9+
import static org.opensearch.sql.data.type.ExprCoreType.UNKNOWN;
10+
11+
import lombok.EqualsAndHashCode;
12+
import lombok.Getter;
13+
14+
/**
15+
* The type of a binary value. See
16+
* <a href="https://opensearch.org/docs/latest/opensearch/supported-field-types/binary/">doc</a>
17+
*/
18+
@EqualsAndHashCode(callSuper = false)
19+
public class OpenSearchBinaryType extends OpenSearchDataType {
20+
21+
@Getter
22+
private static final OpenSearchBinaryType instance = new OpenSearchBinaryType();
23+
24+
private OpenSearchBinaryType() {
25+
super(MappingType.Binary);
26+
exprCoreType = UNKNOWN;
27+
}
28+
29+
@Override
30+
protected OpenSearchDataType cloneEmpty() {
31+
return instance;
32+
}
33+
}

0 commit comments

Comments
 (0)