Repository: calcite Updated Branches: refs/heads/master 4310815a8 -> 1d2929105
http://git-wip-us.apache.org/repos/asf/calcite/blob/6d9242a1/core/src/test/java/org/apache/calcite/test/SqlJsonFunctionsTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/calcite/test/SqlJsonFunctionsTest.java b/core/src/test/java/org/apache/calcite/test/SqlJsonFunctionsTest.java index 75203bd..3f3c57d 100644 --- a/core/src/test/java/org/apache/calcite/test/SqlJsonFunctionsTest.java +++ b/core/src/test/java/org/apache/calcite/test/SqlJsonFunctionsTest.java @@ -31,7 +31,6 @@ import com.jayway.jsonpath.PathNotFoundException; import org.hamcrest.BaseMatcher; import org.hamcrest.Description; import org.hamcrest.Matcher; -import org.jetbrains.annotations.NotNull; import org.junit.Test; import java.util.ArrayList; @@ -43,6 +42,7 @@ import java.util.Map; import java.util.Objects; import java.util.function.Supplier; import java.util.stream.Collectors; +import javax.annotation.Nonnull; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.nullValue; @@ -50,22 +50,23 @@ import static org.junit.Assert.assertThat; import static org.junit.Assert.fail; /** - * ` * Unit test for the methods in {@link SqlFunctions} that implement JSON processing functions. */ public class SqlJsonFunctionsTest { - - public static final String INVOC_DESC_JSON_VALUE_EXPRESSION = "jsonValueExpression"; - public static final String INVOC_DESC_JSON_STRUCTURED_VALUE_EXPRESSION - = "jsonStructuredValueExpression"; - public static final String INVOC_DESC_JSON_API_COMMON_SYNTAX = "jsonApiCommonSyntax"; + public static final String INVOC_DESC_JSON_VALUE_EXPRESSION = + "jsonValueExpression"; + public static final String INVOC_DESC_JSON_STRUCTURED_VALUE_EXPRESSION = + "jsonStructuredValueExpression"; + public static final String INVOC_DESC_JSON_API_COMMON_SYNTAX = + "jsonApiCommonSyntax"; public static final String INVOC_DESC_JSON_EXISTS = "jsonExists"; public static final String INVOC_DESC_JSON_VALUE_ANY = "jsonValueAny"; public static final String INVOC_DESC_JSON_QUERY = "jsonQuery"; public static final String INVOC_DESC_JSONIZE = "jsonize"; public static final String INVOC_DESC_DEJSONIZE = "dejsonize"; public static final String INVOC_DESC_JSON_OBJECT = "jsonObject"; - public static final String INVOC_DESC_JSON_OBJECT_AGG_ADD = "jsonObjectAggAdd"; + public static final String INVOC_DESC_JSON_OBJECT_AGG_ADD = + "jsonObjectAggAdd"; public static final String INVOC_DESC_JSON_ARRAY = "jsonArray"; public static final String INVOC_DESC_JSON_ARRAY_AGG_ADD = "jsonArrayAggAdd"; public static final String INVOC_DESC_IS_JSON_VALUE = "isJsonValue"; @@ -176,8 +177,7 @@ public class SqlJsonFunctionsTest { null, SqlJsonValueEmptyOrErrorBehavior.NULL, null, - is("bar") - ); + is("bar")); assertJsonValueAny( SqlFunctions.PathContext .withReturned(SqlFunctions.PathMode.LAX, null), @@ -185,8 +185,7 @@ public class SqlJsonFunctionsTest { null, SqlJsonValueEmptyOrErrorBehavior.NULL, null, - nullValue() - ); + nullValue()); assertJsonValueAny( SqlFunctions.PathContext .withReturned(SqlFunctions.PathMode.LAX, null), @@ -194,17 +193,17 @@ public class SqlJsonFunctionsTest { "empty", SqlJsonValueEmptyOrErrorBehavior.NULL, null, - is("empty") - ); + is("empty")); assertJsonValueAnyFailed( SqlFunctions.PathContext .withReturned(SqlFunctions.PathMode.LAX, null), SqlJsonValueEmptyOrErrorBehavior.ERROR, null, SqlJsonValueEmptyOrErrorBehavior.NULL, - null, errorMatches( - new CalciteException("Empty result of JSON_VALUE function is not allowed", null)) - ); + null, + errorMatches( + new CalciteException("Empty result of JSON_VALUE function is not " + + "allowed", null))); assertJsonValueAny( SqlFunctions.PathContext .withReturned(SqlFunctions.PathMode.LAX, Collections.emptyList()), @@ -212,8 +211,7 @@ public class SqlJsonFunctionsTest { null, SqlJsonValueEmptyOrErrorBehavior.NULL, null, - nullValue() - ); + nullValue()); assertJsonValueAny( SqlFunctions.PathContext .withReturned(SqlFunctions.PathMode.LAX, Collections.emptyList()), @@ -221,8 +219,7 @@ public class SqlJsonFunctionsTest { "empty", SqlJsonValueEmptyOrErrorBehavior.NULL, null, - is("empty") - ); + is("empty")); assertJsonValueAnyFailed( SqlFunctions.PathContext .withReturned(SqlFunctions.PathMode.LAX, Collections.emptyList()), @@ -231,8 +228,8 @@ public class SqlJsonFunctionsTest { SqlJsonValueEmptyOrErrorBehavior.NULL, null, errorMatches( - new CalciteException("Empty result of JSON_VALUE function is not allowed", null)) - ); + new CalciteException("Empty result of JSON_VALUE function is not " + + "allowed", null))); assertJsonValueAny( SqlFunctions.PathContext .withStrictException(new Exception("test message")), @@ -240,8 +237,7 @@ public class SqlJsonFunctionsTest { null, SqlJsonValueEmptyOrErrorBehavior.NULL, null, - nullValue() - ); + nullValue()); assertJsonValueAny( SqlFunctions.PathContext .withStrictException(new Exception("test message")), @@ -249,17 +245,16 @@ public class SqlJsonFunctionsTest { null, SqlJsonValueEmptyOrErrorBehavior.DEFAULT, "empty", - is("empty") - ); + is("empty")); assertJsonValueAnyFailed( - SqlFunctions.PathContext - .withStrictException(new Exception("test message")), - SqlJsonValueEmptyOrErrorBehavior.NULL, - null, - SqlJsonValueEmptyOrErrorBehavior.ERROR, - null, - errorMatches(new RuntimeException("java.lang.Exception: test message")) - ); + SqlFunctions.PathContext + .withStrictException(new Exception("test message")), + SqlJsonValueEmptyOrErrorBehavior.NULL, + null, + SqlJsonValueEmptyOrErrorBehavior.ERROR, + null, + errorMatches( + new RuntimeException("java.lang.Exception: test message"))); assertJsonValueAny( SqlFunctions.PathContext .withReturned(SqlFunctions.PathMode.STRICT, Collections.emptyList()), @@ -267,8 +262,7 @@ public class SqlJsonFunctionsTest { null, SqlJsonValueEmptyOrErrorBehavior.NULL, null, - nullValue() - ); + nullValue()); assertJsonValueAny( SqlFunctions.PathContext .withReturned(SqlFunctions.PathMode.STRICT, Collections.emptyList()), @@ -276,8 +270,7 @@ public class SqlJsonFunctionsTest { null, SqlJsonValueEmptyOrErrorBehavior.DEFAULT, "empty", - is("empty") - ); + is("empty")); assertJsonValueAnyFailed( SqlFunctions.PathContext .withReturned(SqlFunctions.PathMode.STRICT, Collections.emptyList()), @@ -286,9 +279,8 @@ public class SqlJsonFunctionsTest { SqlJsonValueEmptyOrErrorBehavior.ERROR, null, errorMatches( - new CalciteException( - "Strict jsonpath mode requires scalar value, and the actual value is: '[]'", null)) - ); + new CalciteException("Strict jsonpath mode requires scalar value, " + + "and the actual value is: '[]'", null))); } @Test @@ -299,32 +291,28 @@ public class SqlJsonFunctionsTest { SqlJsonQueryWrapperBehavior.WITHOUT_ARRAY, SqlJsonQueryEmptyOrErrorBehavior.NULL, SqlJsonQueryEmptyOrErrorBehavior.NULL, - is("[\"bar\"]") - ); + is("[\"bar\"]")); assertJsonQuery( SqlFunctions.PathContext .withReturned(SqlFunctions.PathMode.LAX, null), SqlJsonQueryWrapperBehavior.WITHOUT_ARRAY, SqlJsonQueryEmptyOrErrorBehavior.NULL, SqlJsonQueryEmptyOrErrorBehavior.NULL, - nullValue() - ); + nullValue()); assertJsonQuery( SqlFunctions.PathContext .withReturned(SqlFunctions.PathMode.LAX, null), SqlJsonQueryWrapperBehavior.WITHOUT_ARRAY, SqlJsonQueryEmptyOrErrorBehavior.EMPTY_ARRAY, SqlJsonQueryEmptyOrErrorBehavior.NULL, - is("[]") - ); + is("[]")); assertJsonQuery( SqlFunctions.PathContext .withReturned(SqlFunctions.PathMode.LAX, null), SqlJsonQueryWrapperBehavior.WITHOUT_ARRAY, SqlJsonQueryEmptyOrErrorBehavior.EMPTY_OBJECT, SqlJsonQueryEmptyOrErrorBehavior.NULL, - is("{}") - ); + is("{}")); assertJsonQueryFailed( SqlFunctions.PathContext .withReturned(SqlFunctions.PathMode.LAX, null), @@ -332,8 +320,8 @@ public class SqlJsonFunctionsTest { SqlJsonQueryEmptyOrErrorBehavior.ERROR, SqlJsonQueryEmptyOrErrorBehavior.NULL, errorMatches( - new CalciteException("Empty result of JSON_QUERY function is not allowed", null)) - ); + new CalciteException("Empty result of JSON_QUERY function is not " + + "allowed", null))); assertJsonQuery( SqlFunctions.PathContext @@ -341,24 +329,21 @@ public class SqlJsonFunctionsTest { SqlJsonQueryWrapperBehavior.WITHOUT_ARRAY, SqlJsonQueryEmptyOrErrorBehavior.NULL, SqlJsonQueryEmptyOrErrorBehavior.NULL, - nullValue() - ); + nullValue()); assertJsonQuery( SqlFunctions.PathContext .withReturned(SqlFunctions.PathMode.LAX, "bar"), SqlJsonQueryWrapperBehavior.WITHOUT_ARRAY, SqlJsonQueryEmptyOrErrorBehavior.EMPTY_ARRAY, SqlJsonQueryEmptyOrErrorBehavior.NULL, - is("[]") - ); + is("[]")); assertJsonQuery( SqlFunctions.PathContext .withReturned(SqlFunctions.PathMode.LAX, "bar"), SqlJsonQueryWrapperBehavior.WITHOUT_ARRAY, SqlJsonQueryEmptyOrErrorBehavior.EMPTY_OBJECT, SqlJsonQueryEmptyOrErrorBehavior.NULL, - is("{}") - ); + is("{}")); assertJsonQueryFailed( SqlFunctions.PathContext .withReturned(SqlFunctions.PathMode.LAX, "bar"), @@ -366,48 +351,44 @@ public class SqlJsonFunctionsTest { SqlJsonQueryEmptyOrErrorBehavior.ERROR, SqlJsonQueryEmptyOrErrorBehavior.NULL, errorMatches( - new CalciteException("Empty result of JSON_QUERY function is not allowed", null)) - ); + new CalciteException("Empty result of JSON_QUERY function is not " + + "allowed", null))); assertJsonQuery( SqlFunctions.PathContext .withStrictException(new Exception("test message")), SqlJsonQueryWrapperBehavior.WITHOUT_ARRAY, SqlJsonQueryEmptyOrErrorBehavior.NULL, SqlJsonQueryEmptyOrErrorBehavior.EMPTY_ARRAY, - is("[]") - ); + is("[]")); assertJsonQuery( SqlFunctions.PathContext .withStrictException(new Exception("test message")), SqlJsonQueryWrapperBehavior.WITHOUT_ARRAY, SqlJsonQueryEmptyOrErrorBehavior.NULL, SqlJsonQueryEmptyOrErrorBehavior.EMPTY_OBJECT, - is("{}") - ); + is("{}")); assertJsonQueryFailed( SqlFunctions.PathContext .withStrictException(new Exception("test message")), SqlJsonQueryWrapperBehavior.WITHOUT_ARRAY, SqlJsonQueryEmptyOrErrorBehavior.NULL, SqlJsonQueryEmptyOrErrorBehavior.ERROR, - errorMatches(new RuntimeException("java.lang.Exception: test message")) - ); + errorMatches( + new RuntimeException("java.lang.Exception: test message"))); assertJsonQuery( SqlFunctions.PathContext .withReturned(SqlFunctions.PathMode.STRICT, "bar"), SqlJsonQueryWrapperBehavior.WITHOUT_ARRAY, SqlJsonQueryEmptyOrErrorBehavior.NULL, SqlJsonQueryEmptyOrErrorBehavior.NULL, - nullValue() - ); + nullValue()); assertJsonQuery( SqlFunctions.PathContext .withReturned(SqlFunctions.PathMode.STRICT, "bar"), SqlJsonQueryWrapperBehavior.WITHOUT_ARRAY, SqlJsonQueryEmptyOrErrorBehavior.NULL, SqlJsonQueryEmptyOrErrorBehavior.EMPTY_ARRAY, - is("[]") - ); + is("[]")); assertJsonQueryFailed( SqlFunctions.PathContext .withReturned(SqlFunctions.PathMode.STRICT, "bar"), @@ -415,9 +396,8 @@ public class SqlJsonFunctionsTest { SqlJsonQueryEmptyOrErrorBehavior.NULL, SqlJsonQueryEmptyOrErrorBehavior.ERROR, errorMatches( - new CalciteException("Strict jsonpath mode requires array or object value, " - + "and the actual value is: 'bar'", null)) - ); + new CalciteException("Strict jsonpath mode requires array or " + + "object value, and the actual value is: 'bar'", null))); // wrapper behavior test @@ -427,8 +407,7 @@ public class SqlJsonFunctionsTest { SqlJsonQueryWrapperBehavior.WITH_UNCONDITIONAL_ARRAY, SqlJsonQueryEmptyOrErrorBehavior.NULL, SqlJsonQueryEmptyOrErrorBehavior.NULL, - is("[\"bar\"]") - ); + is("[\"bar\"]")); assertJsonQuery( SqlFunctions.PathContext @@ -436,52 +415,47 @@ public class SqlJsonFunctionsTest { SqlJsonQueryWrapperBehavior.WITH_CONDITIONAL_ARRAY, SqlJsonQueryEmptyOrErrorBehavior.NULL, SqlJsonQueryEmptyOrErrorBehavior.NULL, - is("[\"bar\"]") - ); + is("[\"bar\"]")); assertJsonQuery( SqlFunctions.PathContext - .withReturned(SqlFunctions.PathMode.STRICT, Collections.singletonList("bar")), + .withReturned(SqlFunctions.PathMode.STRICT, + Collections.singletonList("bar")), SqlJsonQueryWrapperBehavior.WITH_UNCONDITIONAL_ARRAY, SqlJsonQueryEmptyOrErrorBehavior.NULL, SqlJsonQueryEmptyOrErrorBehavior.NULL, - is("[[\"bar\"]]") - ); + is("[[\"bar\"]]")); assertJsonQuery( SqlFunctions.PathContext - .withReturned(SqlFunctions.PathMode.STRICT, Collections.singletonList("bar")), + .withReturned(SqlFunctions.PathMode.STRICT, + Collections.singletonList("bar")), SqlJsonQueryWrapperBehavior.WITH_CONDITIONAL_ARRAY, SqlJsonQueryEmptyOrErrorBehavior.NULL, SqlJsonQueryEmptyOrErrorBehavior.NULL, - is("[\"bar\"]") - ); + is("[\"bar\"]")); } @Test public void testJsonize() { assertJsonize(new HashMap<>(), - is("{}") - ); + is("{}")); } @Test public void testDejsonize() { assertDejsonize("{}", - is(Collections.emptyMap()) - ); + is(Collections.emptyMap())); assertDejsonize("[]", - is(Collections.emptyList()) - ); + is(Collections.emptyList())); // expect exception thrown + final String message = "com.fasterxml.jackson.core.JsonParseException: " + + "Unexpected close marker '}': expected ']' (for Array starting at " + + "[Source: (String)\"[}\"; line: 1, column: 1])\n at [Source: " + + "(String)\"[}\"; line: 1, column: 3]"; assertDejsonizeFailed("[}", - errorMatches( - new InvalidJsonException("com.fasterxml.jackson.core.JsonParseException: " - + "Unexpected close marker '}': expected ']' " - + "(for Array starting at [Source: (String)\"[}\"; line: 1, column: 1])\n" - + " at [Source: (String)\"[}\"; line: 1, column: 3]")) - ); + errorMatches(new InvalidJsonException(message))); } @Test @@ -494,13 +468,11 @@ public class SqlJsonFunctionsTest { assertJsonObject( is("{\"foo\":null}"), SqlJsonConstructorNullClause.NULL_ON_NULL, "foo", - null - ); + null); assertJsonObject( is("{}"), SqlJsonConstructorNullClause.ABSENT_ON_NULL, "foo", - null - ); + null); } @Test @@ -508,13 +480,13 @@ public class SqlJsonFunctionsTest { Map<String, Object> map = new HashMap<>(); Map<String, Object> expected = new HashMap<>(); expected.put("foo", "bar"); - assertJsonObjectAggAdd(map, "foo", "bar", SqlJsonConstructorNullClause.NULL_ON_NULL, - is(expected)); + assertJsonObjectAggAdd(map, "foo", "bar", + SqlJsonConstructorNullClause.NULL_ON_NULL, is(expected)); expected.put("foo1", null); - assertJsonObjectAggAdd(map, "foo1", null, SqlJsonConstructorNullClause.NULL_ON_NULL, - is(expected)); - assertJsonObjectAggAdd(map, "foo2", null, SqlJsonConstructorNullClause.ABSENT_ON_NULL, - is(expected)); + assertJsonObjectAggAdd(map, "foo1", null, + SqlJsonConstructorNullClause.NULL_ON_NULL, is(expected)); + assertJsonObjectAggAdd(map, "foo2", null, + SqlJsonConstructorNullClause.ABSENT_ON_NULL, is(expected)); } @Test @@ -538,13 +510,13 @@ public class SqlJsonFunctionsTest { List<Object> list = new ArrayList<>(); List<Object> expected = new ArrayList<>(); expected.add("foo"); - assertJsonArrayAggAdd( - list, "foo", SqlJsonConstructorNullClause.NULL_ON_NULL, is(expected)); + assertJsonArrayAggAdd(list, "foo", + SqlJsonConstructorNullClause.NULL_ON_NULL, is(expected)); expected.add(null); - assertJsonArrayAggAdd( - list, null, SqlJsonConstructorNullClause.NULL_ON_NULL, is(expected)); - assertJsonArrayAggAdd( - list, null, SqlJsonConstructorNullClause.ABSENT_ON_NULL, is(expected)); + assertJsonArrayAggAdd(list, null, + SqlJsonConstructorNullClause.NULL_ON_NULL, is(expected)); + assertJsonArrayAggAdd(list, null, + SqlJsonConstructorNullClause.ABSENT_ON_NULL, is(expected)); } @Test @@ -568,175 +540,172 @@ public class SqlJsonFunctionsTest { } private void assertJsonValueExpression(String input, - Matcher<Object> matcher) { + Matcher<Object> matcher) { assertThat( invocationDesc(INVOC_DESC_JSON_VALUE_EXPRESSION, input), SqlFunctions.jsonValueExpression(input), matcher); } private void assertJsonStructuredValueExpression(Object input, - Matcher<Object> matcher) { + Matcher<Object> matcher) { assertThat( invocationDesc(INVOC_DESC_JSON_STRUCTURED_VALUE_EXPRESSION, input), SqlFunctions.jsonStructuredValueExpression(input), matcher); } private void assertJsonApiCommonSyntax(Object input, String pathSpec, - Matcher<? super SqlFunctions.PathContext> matcher) { + Matcher<? super SqlFunctions.PathContext> matcher) { assertThat( invocationDesc(INVOC_DESC_JSON_API_COMMON_SYNTAX, input, pathSpec), SqlFunctions.jsonApiCommonSyntax(input, pathSpec), matcher); } private void assertJsonExists(Object input, SqlJsonExistsErrorBehavior errorBehavior, - Matcher<? super Boolean> matcher) { + Matcher<? super Boolean> matcher) { assertThat(invocationDesc(INVOC_DESC_JSON_EXISTS, input, errorBehavior), SqlFunctions.jsonExists(input, errorBehavior), matcher); } - private void assertJsonExistsFailed(Object input, SqlJsonExistsErrorBehavior errorBehavior, - Matcher<? super Throwable> matcher) { + private void assertJsonExistsFailed(Object input, + SqlJsonExistsErrorBehavior errorBehavior, + Matcher<? super Throwable> matcher) { assertFailed(invocationDesc(INVOC_DESC_JSON_EXISTS, input, errorBehavior), () -> SqlFunctions.jsonExists(input, errorBehavior), matcher); } private void assertJsonValueAny(Object input, - SqlJsonValueEmptyOrErrorBehavior emptyBehavior, - Object defaultValueOnEmpty, - SqlJsonValueEmptyOrErrorBehavior errorBehavior, - Object defaultValueOnError, - Matcher<Object> matcher) { + SqlJsonValueEmptyOrErrorBehavior emptyBehavior, + Object defaultValueOnEmpty, + SqlJsonValueEmptyOrErrorBehavior errorBehavior, + Object defaultValueOnError, + Matcher<Object> matcher) { assertThat( - invocationDesc(INVOC_DESC_JSON_VALUE_ANY, input, emptyBehavior, defaultValueOnEmpty, + invocationDesc(INVOC_DESC_JSON_VALUE_ANY, input, emptyBehavior, + defaultValueOnEmpty, errorBehavior, defaultValueOnError), + SqlFunctions.jsonValueAny(input, emptyBehavior, defaultValueOnEmpty, errorBehavior, defaultValueOnError), - SqlFunctions.jsonValueAny( - input, emptyBehavior, defaultValueOnEmpty, errorBehavior, defaultValueOnError), matcher); } private void assertJsonValueAnyFailed(Object input, - SqlJsonValueEmptyOrErrorBehavior emptyBehavior, - Object defaultValueOnEmpty, - SqlJsonValueEmptyOrErrorBehavior errorBehavior, - Object defaultValueOnError, - Matcher<? super Throwable> matcher) { + SqlJsonValueEmptyOrErrorBehavior emptyBehavior, + Object defaultValueOnEmpty, + SqlJsonValueEmptyOrErrorBehavior errorBehavior, + Object defaultValueOnError, + Matcher<? super Throwable> matcher) { assertFailed( - invocationDesc(INVOC_DESC_JSON_VALUE_ANY, input, emptyBehavior, defaultValueOnEmpty, - errorBehavior, defaultValueOnError), - () -> SqlFunctions.jsonValueAny( - input, emptyBehavior, defaultValueOnEmpty, errorBehavior, defaultValueOnError), + invocationDesc(INVOC_DESC_JSON_VALUE_ANY, input, emptyBehavior, + defaultValueOnEmpty, errorBehavior, defaultValueOnError), + () -> SqlFunctions.jsonValueAny(input, emptyBehavior, + defaultValueOnEmpty, errorBehavior, defaultValueOnError), matcher); } private void assertJsonQuery(Object input, - SqlJsonQueryWrapperBehavior wrapperBehavior, - SqlJsonQueryEmptyOrErrorBehavior emptyBehavior, - SqlJsonQueryEmptyOrErrorBehavior errorBehavior, - Matcher<? super String> matcher) { + SqlJsonQueryWrapperBehavior wrapperBehavior, + SqlJsonQueryEmptyOrErrorBehavior emptyBehavior, + SqlJsonQueryEmptyOrErrorBehavior errorBehavior, + Matcher<? super String> matcher) { assertThat( - invocationDesc(INVOC_DESC_JSON_QUERY, input, wrapperBehavior, emptyBehavior, + invocationDesc(INVOC_DESC_JSON_QUERY, input, wrapperBehavior, + emptyBehavior, errorBehavior), + SqlFunctions.jsonQuery(input, wrapperBehavior, emptyBehavior, errorBehavior), - SqlFunctions.jsonQuery(input, wrapperBehavior, emptyBehavior, errorBehavior), matcher); } private void assertJsonQueryFailed(Object input, - SqlJsonQueryWrapperBehavior wrapperBehavior, - SqlJsonQueryEmptyOrErrorBehavior emptyBehavior, - SqlJsonQueryEmptyOrErrorBehavior errorBehavior, - Matcher<? super Throwable> matcher) { + SqlJsonQueryWrapperBehavior wrapperBehavior, + SqlJsonQueryEmptyOrErrorBehavior emptyBehavior, + SqlJsonQueryEmptyOrErrorBehavior errorBehavior, + Matcher<? super Throwable> matcher) { assertFailed( - invocationDesc(INVOC_DESC_JSON_QUERY, input, wrapperBehavior, emptyBehavior, + invocationDesc(INVOC_DESC_JSON_QUERY, input, wrapperBehavior, + emptyBehavior, errorBehavior), + () -> SqlFunctions.jsonQuery(input, wrapperBehavior, emptyBehavior, errorBehavior), - () -> SqlFunctions.jsonQuery(input, wrapperBehavior, emptyBehavior, errorBehavior), matcher); } private void assertJsonize(Object input, - Matcher<? super String> matcher) { - assertThat( - invocationDesc(INVOC_DESC_JSONIZE, input), + Matcher<? super String> matcher) { + assertThat(invocationDesc(INVOC_DESC_JSONIZE, input), SqlFunctions.jsonize(input), matcher); } private void assertDejsonize(String input, - Matcher<Object> matcher) { - assertThat( - invocationDesc(INVOC_DESC_DEJSONIZE, input), + Matcher<Object> matcher) { + assertThat(invocationDesc(INVOC_DESC_DEJSONIZE, input), SqlFunctions.dejsonize(input), matcher); } private void assertDejsonizeFailed(String input, - Matcher<? super Throwable> matcher) { - assertFailed( - invocationDesc(INVOC_DESC_DEJSONIZE, input), + Matcher<? super Throwable> matcher) { + assertFailed(invocationDesc(INVOC_DESC_DEJSONIZE, input), () -> SqlFunctions.dejsonize(input), matcher); } private void assertJsonObject(Matcher<? super String> matcher, - SqlJsonConstructorNullClause nullClause, - Object... kvs) { - assertThat( - invocationDesc(INVOC_DESC_JSON_OBJECT, nullClause, kvs), + SqlJsonConstructorNullClause nullClause, + Object... kvs) { + assertThat(invocationDesc(INVOC_DESC_JSON_OBJECT, nullClause, kvs), SqlFunctions.jsonObject(nullClause, kvs), matcher); } private void assertJsonObjectAggAdd(Map map, String k, Object v, - SqlJsonConstructorNullClause nullClause, - Matcher<? super Map> matcher) { + SqlJsonConstructorNullClause nullClause, + Matcher<? super Map> matcher) { SqlFunctions.jsonObjectAggAdd(map, k, v, nullClause); - assertThat(invocationDesc(INVOC_DESC_JSON_OBJECT_AGG_ADD, map, k, v, nullClause), + assertThat( + invocationDesc(INVOC_DESC_JSON_OBJECT_AGG_ADD, map, k, v, nullClause), map, matcher); } private void assertJsonArray(Matcher<? super String> matcher, - SqlJsonConstructorNullClause nullClause, Object... elements) { - assertThat( - invocationDesc(INVOC_DESC_JSON_ARRAY, nullClause, elements), + SqlJsonConstructorNullClause nullClause, Object... elements) { + assertThat(invocationDesc(INVOC_DESC_JSON_ARRAY, nullClause, elements), SqlFunctions.jsonArray(nullClause, elements), matcher); } private void assertJsonArrayAggAdd(List list, Object element, - SqlJsonConstructorNullClause nullClause, - Matcher<? super List> matcher) { + SqlJsonConstructorNullClause nullClause, + Matcher<? super List> matcher) { SqlFunctions.jsonArrayAggAdd(list, element, nullClause); assertThat( - invocationDesc(INVOC_DESC_JSON_ARRAY_AGG_ADD, list, element, nullClause), list, matcher); + invocationDesc(INVOC_DESC_JSON_ARRAY_AGG_ADD, list, element, + nullClause), + list, matcher); } private void assertIsJsonValue(String input, - Matcher<? super Boolean> matcher) { - assertThat( - invocationDesc(INVOC_DESC_IS_JSON_VALUE, input), + Matcher<? super Boolean> matcher) { + assertThat(invocationDesc(INVOC_DESC_IS_JSON_VALUE, input), SqlFunctions.isJsonValue(input), matcher); } private void assertIsJsonScalar(String input, - Matcher<? super Boolean> matcher) { - assertThat( - invocationDesc(INVOC_DESC_IS_JSON_SCALAR, input), + Matcher<? super Boolean> matcher) { + assertThat(invocationDesc(INVOC_DESC_IS_JSON_SCALAR, input), SqlFunctions.isJsonScalar(input), matcher); } private void assertIsJsonArray(String input, - Matcher<? super Boolean> matcher) { - assertThat( - invocationDesc(INVOC_DESC_IS_JSON_ARRAY, input), + Matcher<? super Boolean> matcher) { + assertThat(invocationDesc(INVOC_DESC_IS_JSON_ARRAY, input), SqlFunctions.isJsonArray(input), matcher); } private void assertIsJsonObject(String input, - Matcher<? super Boolean> matcher) { - assertThat( - invocationDesc(INVOC_DESC_IS_JSON_OBJECT, input), + Matcher<? super Boolean> matcher) { + assertThat(invocationDesc(INVOC_DESC_IS_JSON_OBJECT, input), SqlFunctions.isJsonObject(input), matcher); } @@ -745,12 +714,11 @@ public class SqlJsonFunctionsTest { return methodName + "(" + String.join(", ", Arrays.stream(args) .map(Objects::toString) - .collect(Collectors.toList()) - ) + ")"; + .collect(Collectors.toList())) + ")"; } private void assertFailed(String invocationDesc, Supplier<?> supplier, - Matcher<? super Throwable> matcher) { + Matcher<? super Throwable> matcher) { try { supplier.get(); fail("expect exception, but not: " + invocationDesc); @@ -777,7 +745,7 @@ public class SqlJsonFunctionsTest { }; } - @NotNull private BaseMatcher<SqlFunctions.PathContext> contextMatches( + @Nonnull private BaseMatcher<SqlFunctions.PathContext> contextMatches( SqlFunctions.PathContext expected) { return new BaseMatcher<SqlFunctions.PathContext>() { @Override public boolean matches(Object item) { http://git-wip-us.apache.org/repos/asf/calcite/blob/6d9242a1/druid/src/test/java/org/apache/calcite/adapter/druid/DruidQueryFilterTest.java ---------------------------------------------------------------------- diff --git a/druid/src/test/java/org/apache/calcite/adapter/druid/DruidQueryFilterTest.java b/druid/src/test/java/org/apache/calcite/adapter/druid/DruidQueryFilterTest.java index 6b5b520..b42821d 100644 --- a/druid/src/test/java/org/apache/calcite/adapter/druid/DruidQueryFilterTest.java +++ b/druid/src/test/java/org/apache/calcite/adapter/druid/DruidQueryFilterTest.java @@ -59,8 +59,7 @@ public class DruidQueryFilterTest { .thenReturn( new DruidTable(Mockito.mock(DruidSchema.class), "dataSource", null, ImmutableSet.of(), "timestamp", null, null, - null - )); + null)); } @Test public void testInFilter() throws IOException { final Fixture f = new Fixture(); http://git-wip-us.apache.org/repos/asf/calcite/blob/6d9242a1/druid/src/test/java/org/apache/calcite/test/DruidAdapterIT.java ---------------------------------------------------------------------- diff --git a/druid/src/test/java/org/apache/calcite/test/DruidAdapterIT.java b/druid/src/test/java/org/apache/calcite/test/DruidAdapterIT.java index 3a572dd..50d3023 100644 --- a/druid/src/test/java/org/apache/calcite/test/DruidAdapterIT.java +++ b/druid/src/test/java/org/apache/calcite/test/DruidAdapterIT.java @@ -4528,8 +4528,7 @@ public class DruidAdapterIT { + "[{\"type\":\"default\",\"dimension\":\"vc\",\"outputName\":\"vc\",\"outputType\":\"LONG\"}," + "{\"type\":\"default\",\"dimension\":\"product_id\",\"outputName\":\"product_id\",\"outputType\":\"STRING\"}]," + "\"virtualColumns\":[{\"type\":\"expression\",\"name\":\"vc\",\"expression\":\"timestamp_extract(\\\"__time\\\",", - "QUARTER" - )); + "QUARTER")); } @Test @@ -4547,8 +4546,7 @@ public class DruidAdapterIT { "{\"queryType\":\"groupBy\",\"dataSource\":\"foodmart\",\"granularity\":\"all\"," + "\"dimensions\":[{\"type\":\"default\",\"dimension\":\"vc\",\"outputName\":\"vc\",\"outputType\":\"LONG\"}]," + "\"virtualColumns\":[{\"type\":\"expression\",\"name\":\"vc\",\"expression\":\"timestamp_extract(\\\"__time\\\",", - "QUARTER" - )); + "QUARTER")); } http://git-wip-us.apache.org/repos/asf/calcite/blob/6d9242a1/druid/src/test/java/org/apache/calcite/test/DruidAdapterIT2.java ---------------------------------------------------------------------- diff --git a/druid/src/test/java/org/apache/calcite/test/DruidAdapterIT2.java b/druid/src/test/java/org/apache/calcite/test/DruidAdapterIT2.java index aca3fa1..4414631 100644 --- a/druid/src/test/java/org/apache/calcite/test/DruidAdapterIT2.java +++ b/druid/src/test/java/org/apache/calcite/test/DruidAdapterIT2.java @@ -3919,8 +3919,7 @@ public class DruidAdapterIT2 { + "[{\"type\":\"default\",\"dimension\":\"vc\",\"outputName\":\"vc\",\"outputType\":\"LONG\"}," + "{\"type\":\"default\",\"dimension\":\"product_id\",\"outputName\":\"product_id\",\"outputType\":\"STRING\"}]," + "\"virtualColumns\":[{\"type\":\"expression\",\"name\":\"vc\",\"expression\":\"timestamp_extract(\\\"__time\\\",", - "QUARTER" - )); + "QUARTER")); } @Test @@ -3938,8 +3937,7 @@ public class DruidAdapterIT2 { "{\"queryType\":\"groupBy\",\"dataSource\":\"foodmart\",\"granularity\":\"all\"," + "\"dimensions\":[{\"type\":\"default\",\"dimension\":\"vc\",\"outputName\":\"vc\",\"outputType\":\"LONG\"}]," + "\"virtualColumns\":[{\"type\":\"expression\",\"name\":\"vc\",\"expression\":\"timestamp_extract(\\\"__time\\\",", - "QUARTER" - )); + "QUARTER")); } @Test http://git-wip-us.apache.org/repos/asf/calcite/blob/6d9242a1/elasticsearch/src/test/java/org/apache/calcite/adapter/elasticsearch/ElasticSearchAdapterTest.java ---------------------------------------------------------------------- diff --git a/elasticsearch/src/test/java/org/apache/calcite/adapter/elasticsearch/ElasticSearchAdapterTest.java b/elasticsearch/src/test/java/org/apache/calcite/adapter/elasticsearch/ElasticSearchAdapterTest.java index abcd003..4895df8 100644 --- a/elasticsearch/src/test/java/org/apache/calcite/adapter/elasticsearch/ElasticSearchAdapterTest.java +++ b/elasticsearch/src/test/java/org/apache/calcite/adapter/elasticsearch/ElasticSearchAdapterTest.java @@ -337,8 +337,7 @@ public class ElasticSearchAdapterTest { .query("select * from zips where pop in (96074, 99568)") .returnsUnordered( "city=BELL GARDENS; longitude=-118.17205; latitude=33.969177; pop=99568; state=CA; id=90201", - "city=LOS ANGELES; longitude=-118.258189; latitude=34.007856; pop=96074; state=CA; id=90011" - ) + "city=LOS ANGELES; longitude=-118.258189; latitude=34.007856; pop=96074; state=CA; id=90011") .queryContains(ElasticsearchChecker.elasticsearchChecker(searches)); } http://git-wip-us.apache.org/repos/asf/calcite/blob/6d9242a1/site/_docs/reference.md ---------------------------------------------------------------------- diff --git a/site/_docs/reference.md b/site/_docs/reference.md index e6684e6..261c1f1 100644 --- a/site/_docs/reference.md +++ b/site/_docs/reference.md @@ -1942,8 +1942,8 @@ Not implemented: Note: -* The common structure `value, path` is JSON API common syntax. **value** is a character string type json input, and **path** is a JSON path expression (in character string type too), mode flag **strict** or **lax** should be specified in the beginning of **path**. -* **ON ERROR** clause, and **ON EMPTY** clause define the fallback behavior of the function when an error is thrown or a null value is about to be returned. +* The common structure `value, path` is JSON API common syntax. **value** is a character string type json input, and **path** is a JSON path expression (in character string type too), mode flag **strict** or **lax** should be specified in the beginning of **path**. +* **ON ERROR** clause, and **ON EMPTY** clause define the fallback behavior of the function when an error is thrown or a null value is about to be returned. * **ARRAY WRAPPER** clause defines how to represent JSON array result in JSON_QUERY function. Following is a comparision to demonstrate the difference among different wrapper behaviors. Example Data: @@ -1952,11 +1952,11 @@ Example Data: { "a": "[1,2]", "b": [1,2], "c": "hi"} ``` -Comparision: +Comparison: |Operator |$.a |$.b |$.c |:-------------------------------------------|:------------|:------------|:------------ -|JSON_VALUE | [1, 2] | error | hi +|JSON_VALUE | [1, 2] | error | hi |JSON QUERY WITHOUT ARRAY WRAPPER | error | [1, 2] | error |JSON QUERY WITH UNCONDITIONAL ARRAY WRAPPER | [ "[1,2]" ] | [ [1,2] ] | [ "hi" ] |JSON QUERY WITH CONDITIONAL ARRAY WRAPPER | [ "[1,2]" ] | [1,2] | [ "hi" ] @@ -1976,7 +1976,7 @@ Not implemented: Note: -* The flag **FORMAT JSON** indicates the value is formatted as JSON character string. When **FORMAT JSON** is used, value should be de-parse from JSON character string to SQL structured value. +* The flag **FORMAT JSON** indicates the value is formatted as JSON character string. When **FORMAT JSON** is used, value should be de-parse from JSON character string to SQL structured value. * **ON NULL** clause defines how the JSON output represents null value. The default null behavior of **JSON_OBJECT** and **JSON_OBJECTAGG** is *NULL ON NULL*, and for **JSON_ARRAY** and **JSON_ARRAYAGG** it is *ABSENT ON NULL*. #### Comparison Operators
