Skip to content

Commit

Permalink
SPOTLESS
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Carbonetto <[email protected]>
  • Loading branch information
acarbonetto committed Jan 14, 2025
1 parent 16d91af commit 2ccf4ab
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,10 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.opensearch.sql.data.model.ExprValueUtils.LITERAL_FALSE;
import static org.opensearch.sql.data.model.ExprValueUtils.LITERAL_NULL;
import static org.opensearch.sql.data.model.ExprValueUtils.LITERAL_NULL;
import static org.opensearch.sql.data.model.ExprValueUtils.LITERAL_TRUE;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -32,18 +26,9 @@
import org.opensearch.sql.data.model.ExprNullValue;
import org.opensearch.sql.data.model.ExprStringValue;
import org.opensearch.sql.data.model.ExprTupleValue;
import org.opensearch.sql.data.model.ExprBooleanValue;
import org.opensearch.sql.data.model.ExprCollectionValue;
import org.opensearch.sql.data.model.ExprDoubleValue;
import org.opensearch.sql.data.model.ExprIntegerValue;
import org.opensearch.sql.data.model.ExprLongValue;
import org.opensearch.sql.data.model.ExprNullValue;
import org.opensearch.sql.data.model.ExprStringValue;
import org.opensearch.sql.data.model.ExprTupleValue;
import org.opensearch.sql.data.model.ExprValue;
import org.opensearch.sql.data.model.ExprValueUtils;
import org.opensearch.sql.exception.SemanticCheckException;
import org.opensearch.sql.exception.SemanticCheckException;
import org.opensearch.sql.expression.DSL;
import org.opensearch.sql.expression.FunctionExpression;

Expand Down Expand Up @@ -80,92 +65,92 @@ private ExprValue execute(ExprValue jsonString) {
return exp.valueOf();
}

@Test
void json_returnsJsonObject() {
FunctionExpression exp;

// Setup
final String objectJson =
"{\"foo\": \"foo\", \"fuzz\": true, \"bar\": 1234, \"bar2\": 12.34, \"baz\": null, "
+ "\"obj\": {\"internal\": \"value\"}, \"arr\": [\"string\", true, null]}";

LinkedHashMap<String, ExprValue> objectMap = new LinkedHashMap<>();
objectMap.put("foo", new ExprStringValue("foo"));
objectMap.put("fuzz", ExprBooleanValue.of(true));
objectMap.put("bar", new ExprLongValue(1234));
objectMap.put("bar2", new ExprDoubleValue(12.34));
objectMap.put("baz", ExprNullValue.of());
objectMap.put(
"obj", ExprTupleValue.fromExprValueMap(Map.of("internal", new ExprStringValue("value"))));
objectMap.put(
"arr",
new ExprCollectionValue(
List.of(new ExprStringValue("string"), ExprBooleanValue.of(true), ExprNullValue.of())));
ExprValue expectedTupleExpr = ExprTupleValue.fromExprValueMap(objectMap);

// exercise
exp = DSL.json_function(DSL.literal(objectJson));

// Verify
var value = exp.valueOf();
assertTrue(value instanceof ExprTupleValue);
assertEquals(expectedTupleExpr, value);
}

@Test
void json_returnsJsonArray() {
FunctionExpression exp;

// Setup
final String arrayJson = "[\"foo\", \"fuzz\", true, \"bar\", 1234, 12.34, null]";
ExprValue expectedArrayExpr =
new ExprCollectionValue(
List.of(
new ExprStringValue("foo"),
new ExprStringValue("fuzz"),
LITERAL_TRUE,
new ExprStringValue("bar"),
new ExprIntegerValue(1234),
new ExprDoubleValue(12.34),
LITERAL_NULL));

// exercise
exp = DSL.json_function(DSL.literal(arrayJson));

// Verify
var value = exp.valueOf();
assertTrue(value instanceof ExprCollectionValue);
assertEquals(expectedArrayExpr, value);
}

@Test
void json_returnsScalar() {
assertEquals(
new ExprStringValue("foobar"), DSL.json_function(DSL.literal("\"foobar\"")).valueOf());

assertEquals(new ExprIntegerValue(1234), DSL.json_function(DSL.literal("1234")).valueOf());

assertEquals(LITERAL_TRUE, DSL.json_function(DSL.literal("true")).valueOf());

assertEquals(LITERAL_NULL, DSL.json_function(DSL.literal("null")).valueOf());

assertEquals(LITERAL_NULL, DSL.json_function(DSL.literal("")).valueOf());

assertEquals(
ExprTupleValue.fromExprValueMap(Map.of()), DSL.json_function(DSL.literal("{}")).valueOf());
}

@Test
void json_returnsSemanticCheckException() {
// invalid type
assertThrows(
SemanticCheckException.class, () -> DSL.castJson(DSL.literal("invalid")).valueOf());

// missing bracket
assertThrows(SemanticCheckException.class, () -> DSL.castJson(DSL.literal("{{[}}")).valueOf());

// mnissing quote
assertThrows(
SemanticCheckException.class, () -> DSL.castJson(DSL.literal("\"missing quote")).valueOf());
}
@Test
void json_returnsJsonObject() {
FunctionExpression exp;

// Setup
final String objectJson =
"{\"foo\": \"foo\", \"fuzz\": true, \"bar\": 1234, \"bar2\": 12.34, \"baz\": null, "
+ "\"obj\": {\"internal\": \"value\"}, \"arr\": [\"string\", true, null]}";

LinkedHashMap<String, ExprValue> objectMap = new LinkedHashMap<>();
objectMap.put("foo", new ExprStringValue("foo"));
objectMap.put("fuzz", ExprBooleanValue.of(true));
objectMap.put("bar", new ExprLongValue(1234));
objectMap.put("bar2", new ExprDoubleValue(12.34));
objectMap.put("baz", ExprNullValue.of());
objectMap.put(
"obj", ExprTupleValue.fromExprValueMap(Map.of("internal", new ExprStringValue("value"))));
objectMap.put(
"arr",
new ExprCollectionValue(
List.of(new ExprStringValue("string"), ExprBooleanValue.of(true), ExprNullValue.of())));
ExprValue expectedTupleExpr = ExprTupleValue.fromExprValueMap(objectMap);

// exercise
exp = DSL.json_function(DSL.literal(objectJson));

// Verify
var value = exp.valueOf();
assertTrue(value instanceof ExprTupleValue);
assertEquals(expectedTupleExpr, value);
}

@Test
void json_returnsJsonArray() {
FunctionExpression exp;

// Setup
final String arrayJson = "[\"foo\", \"fuzz\", true, \"bar\", 1234, 12.34, null]";
ExprValue expectedArrayExpr =
new ExprCollectionValue(
List.of(
new ExprStringValue("foo"),
new ExprStringValue("fuzz"),
LITERAL_TRUE,
new ExprStringValue("bar"),
new ExprIntegerValue(1234),
new ExprDoubleValue(12.34),
LITERAL_NULL));

// exercise
exp = DSL.json_function(DSL.literal(arrayJson));

// Verify
var value = exp.valueOf();
assertTrue(value instanceof ExprCollectionValue);
assertEquals(expectedArrayExpr, value);
}

@Test
void json_returnsScalar() {
assertEquals(
new ExprStringValue("foobar"), DSL.json_function(DSL.literal("\"foobar\"")).valueOf());

assertEquals(new ExprIntegerValue(1234), DSL.json_function(DSL.literal("1234")).valueOf());

assertEquals(LITERAL_TRUE, DSL.json_function(DSL.literal("true")).valueOf());

assertEquals(LITERAL_NULL, DSL.json_function(DSL.literal("null")).valueOf());

assertEquals(LITERAL_NULL, DSL.json_function(DSL.literal("")).valueOf());

assertEquals(
ExprTupleValue.fromExprValueMap(Map.of()), DSL.json_function(DSL.literal("{}")).valueOf());
}

@Test
void json_returnsSemanticCheckException() {
// invalid type
assertThrows(
SemanticCheckException.class, () -> DSL.castJson(DSL.literal("invalid")).valueOf());

// missing bracket
assertThrows(SemanticCheckException.class, () -> DSL.castJson(DSL.literal("{{[}}")).valueOf());

// mnissing quote
assertThrows(
SemanticCheckException.class, () -> DSL.castJson(DSL.literal("\"missing quote")).valueOf());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import static org.opensearch.sql.util.MatcherUtils.verifySchema;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
Expand Down Expand Up @@ -68,7 +67,9 @@ public void test_cast_json() throws IOException {
verifySchema(result, schema("test_name", null, "string"), schema("casted", null, "undefined"));
verifyDataRows(
result,
rows("json nested object", new JSONObject(Map.of("a", "1", "b", Map.of("c", "3"), "d", List.of(1, 2, 3)))),
rows(
"json nested object",
new JSONObject(Map.of("a", "1", "b", Map.of("c", "3"), "d", List.of(1, 2, 3)))),
rows("json object", new JSONObject(Map.of("a", "1", "b", "2"))),
rows("json array", new JSONArray(List.of(1, 2, 3, 4))),
rows("json scalar string", "abc"),
Expand All @@ -89,7 +90,9 @@ public void test_json() throws IOException {
JSONObject firstRow = new JSONObject(Map.of("c", 2));
verifyDataRows(
result,
rows("json nested object", new JSONObject(Map.of("a", "1", "b", Map.of("c", "3"), "d", List.of(1, 2, 3)))),
rows(
"json nested object",
new JSONObject(Map.of("a", "1", "b", Map.of("c", "3"), "d", List.of(1, 2, 3)))),
rows("json object", new JSONObject(Map.of("a", "1", "b", "2"))),
rows("json array", new JSONArray(List.of(1, 2, 3, 4))),
rows("json scalar string", "abc"),
Expand Down

0 comments on commit 2ccf4ab

Please sign in to comment.