Skip to content

Commit

Permalink
Address PR feedback.
Browse files Browse the repository at this point in the history
* Move function definition in ANTLR grammar into a separate group;
* Move SQL integration tests outside of legacy block;
* Extend integration tests.

Signed-off-by: Yury-Fridlyand <yuryf@bitquilltech.com>
  • Loading branch information
Yury-Fridlyand committed Oct 4, 2022
1 parent 0bb6997 commit d02e16f
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,6 @@ private static DefaultFunctionResolver typeof() {
}

private static ExprValue exprTypeOf(ExprValue input) {
return new ExprStringValue(input.type().typeName());
return new ExprStringValue(input.type().typeName().toUpperCase());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -486,22 +486,6 @@ public void castStatementInWhereClauseDatetimeCastTest() {
assertEquals(response.getJSONArray("schema").get(0).toString(), schema_result);
}

@Test
public void typeof() {
JSONObject response = executeJdbcRequest("SELECT typeof('pewpew'), typeof(NULL), typeof(1.0),"
+ "typeof(12345), typeof(1234567891011), typeof(INTERVAL 2 DAY);");
verifyDataRows(response,
rows("STRING", "UNDEFINED", "DOUBLE", "INTEGER", "LONG", "INTERVAL"));

response = executeJdbcRequest("SELECT"
+ " typeof(CAST('1961-04-12 09:07:00' AS TIMESTAMP)),"
+ " typeof(CAST('09:07:00' AS TIME)),"
+ " typeof(CAST('1961-04-12' AS DATE)),"
+ " typeof(DATETIME('1961-04-12 09:07:00'))");
verifyDataRows(response,
rows("TIMESTAMP", "TIME", "DATE", "DATETIME"));
}

@Test
public void concat_ws_field_and_string() throws Exception {
//here is a bug,csv field with spa
Expand Down
26 changes: 25 additions & 1 deletion integ-test/src/test/java/org/opensearch/sql/ppl/DataTypeIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void test_long_integer_data_type() throws IOException {
}

@Test
public void typeof() throws IOException {
public void typeof_sql_types() throws IOException {
JSONObject response = executeQuery(String.format("source=%s | eval "
+ "`str` = typeof('pewpew'), `null` = typeof(1/0), `double` = typeof(1.0),"
+ "`int` = typeof(12345), `long` = typeof(1234567891011), `interval` = typeof(INTERVAL 2 DAY)"
Expand All @@ -94,4 +94,28 @@ public void typeof() throws IOException {
verifyDataRows(response,
rows("TIMESTAMP", "TIME", "DATE", "DATETIME"));
}

@Test
public void typeof_opensearch_types() throws IOException {
JSONObject response = executeQuery(String.format("source=%s | eval "
+ "`double` = typeof(double_number), `long` = typeof(long_number),"
+ "`integer` = typeof(integer_number), `byte` = typeof(byte_number),"
+ "`short` = typeof(short_number), `float` = typeof(float_number),"
+ "`half_float` = typeof(half_float_number), `scaled_float` = typeof(scaled_float_number)"
+ " | fields `double`, `long`, `integer`, `byte`, `short`, `float`, `half_float`, `scaled_float`",
TEST_INDEX_DATATYPE_NUMERIC));
verifyDataRows(response,
rows("DOUBLE", "LONG", "INTEGER", "BYTE", "SHORT", "FLOAT", "FLOAT", "DOUBLE"));

response = executeQuery(String.format("source=%s | eval "
+ "`text` = typeof(text_value), `date` = typeof(date_value),"
+ "`boolean` = typeof(boolean_value), `object` = typeof(object_value),"
+ "`keyword` = typeof(keyword_value)"
// TODO activate these tests once `typeof` fixed to recognize `OpenSearchDataType`
//+ "`ip` = typeof(ip_value), `nested` = typeof(nested_value), `binary` = typeof(binary_value)"
+ " | fields `text`, `date`, `boolean`, `object`, `keyword`",
TEST_INDEX_DATATYPE_NONNUMERIC));
verifyDataRows(response,
rows("STRING", "TIMESTAMP", "BOOLEAN", "STRUCT", "STRING"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.sql.sql;

import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_DATATYPE_NONNUMERIC;
import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_DATATYPE_NUMERIC;
import static org.opensearch.sql.util.MatcherUtils.rows;
import static org.opensearch.sql.util.MatcherUtils.verifyDataRows;

import org.json.JSONObject;
import org.junit.Test;
import org.opensearch.sql.legacy.SQLIntegTestCase;

public class SystemFunctionIT extends SQLIntegTestCase {

@Override
protected void init() throws Exception {
loadIndex(Index.DATA_TYPE_NONNUMERIC);
loadIndex(Index.DATA_TYPE_NUMERIC);
}

@Test
public void typeof_sql_types() {
JSONObject response = executeJdbcRequest("SELECT typeof('pewpew'), typeof(NULL), typeof(1.0),"
+ "typeof(12345), typeof(1234567891011), typeof(INTERVAL 2 DAY);");
verifyDataRows(response,
rows("STRING", "UNDEFINED", "DOUBLE", "INTEGER", "LONG", "INTERVAL"));

response = executeJdbcRequest("SELECT"
+ " typeof(CAST('1961-04-12 09:07:00' AS TIMESTAMP)),"
+ " typeof(CAST('09:07:00' AS TIME)),"
+ " typeof(CAST('1961-04-12' AS DATE)),"
+ " typeof(DATETIME('1961-04-12 09:07:00'))");
verifyDataRows(response,
rows("TIMESTAMP", "TIME", "DATE", "DATETIME"));
}

@Test
public void typeof_opensearch_types() {
JSONObject response = executeJdbcRequest(String.format("SELECT typeof(double_number),"
+ "typeof(long_number), typeof(integer_number), typeof(byte_number), typeof(short_number),"
+ "typeof(float_number), typeof(half_float_number), typeof(scaled_float_number)"
+ " from %s;", TEST_INDEX_DATATYPE_NUMERIC));
verifyDataRows(response,
rows("DOUBLE", "LONG", "INTEGER", "BYTE", "SHORT", "FLOAT", "FLOAT", "DOUBLE"));

response = executeJdbcRequest(String.format("SELECT typeof(text_value),"
+ "typeof(date_value), typeof(boolean_value), typeof(object_value), typeof(keyword_value)"
// TODO activate these tests once `typeof` fixed to recognize `OpenSearchDataType`
//+ "typeof(ip_value), typeof(nested_value), typeof(binary_value)"
+ " from %s;", TEST_INDEX_DATATYPE_NONNUMERIC));
verifyDataRows(response,
rows("STRING", "TIMESTAMP", "BOOLEAN", "STRUCT", "STRING"));
}
}
2 changes: 1 addition & 1 deletion integ-test/src/test/resources/datatypes.json
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
{"index":{"_id":"1"}}
{"boolean_value": true, "keyword_value": "keyword", "text_value": "text", "binary_value": "U29tZSBiaW5hcnkgYmxvYg==", "date_value": "2020-10-13 13:00:00", "ip_value": "127.0.0.0.1", "object_value": {"first": "Dale", "last": "Dale"}, "nested_value": [{"first" : "John", "last" : "Smith"}, {"first" : "Alice", "last" : "White"}}
{"boolean_value": true, "keyword_value": "keyword", "text_value": "text", "binary_value": "U29tZSBiaW5hcnkgYmxvYg==", "date_value": "2020-10-13 13:00:00", "ip_value": "127.0.0.1", "object_value": {"first": "Dale", "last": "Dale"}, "nested_value": [{"first" : "John", "last" : "Smith"}, {"first" : "Alice", "last" : "White"}]}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
"binary_value": {
"type": "binary"
},
"date_value": {
"type": "date"
"date_value": {
"type" : "date",
"format": "yyyy-MM-dd HH:mm:ss"
},
"ip_value": {
"type": "ip"
Expand Down
6 changes: 5 additions & 1 deletion ppl/src/main/antlr/OpenSearchPPLParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ evalFunctionName
| dateAndTimeFunctionBase
| textFunctionBase
| conditionFunctionBase
| systemFunctionBase
;

functionArgs
Expand Down Expand Up @@ -394,7 +395,10 @@ constantFunctionName
conditionFunctionBase
: LIKE
| IF | ISNULL | ISNOTNULL | IFNULL | NULLIF
| TYPEOF
;

systemFunctionBase
: TYPEOF
;

textFunctionBase
Expand Down
7 changes: 6 additions & 1 deletion sql/src/main/antlr/OpenSearchSQLParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ scalarFunctionName
| dateTimeFunctionName
| textFunctionName
| flowControlFunctionName
| systemFunctionName
;

specificFunction
Expand Down Expand Up @@ -413,7 +414,11 @@ textFunctionName
;

flowControlFunctionName
: IF | IFNULL | NULLIF | ISNULL | TYPEOF
: IF | IFNULL | NULLIF | ISNULL
;

systemFunctionName
: TYPEOF
;

singleFieldRelevanceFunctionName
Expand Down

0 comments on commit d02e16f

Please sign in to comment.