diff --git a/dbms/src/Functions/FunctionsJSON.h b/dbms/src/Functions/FunctionsJSON.h index d729fc886a0..fb13c990e27 100644 --- a/dbms/src/Functions/FunctionsJSON.h +++ b/dbms/src/Functions/FunctionsJSON.h @@ -117,10 +117,10 @@ public: if (arguments.size() < 2) throw Exception{"Function " + getName() + " requires at least two arguments", ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH}; - auto col_type_const = typeid_cast(arguments[1].column.get()); + auto col_type_const = typeid_cast(arguments[arguments.size() - 1].column.get()); if (!col_type_const) - throw Exception{"Illegal non-const column " + arguments[1].column->getName() + " of argument of function " + getName(), + throw Exception{"Illegal non-const column " + arguments[arguments.size() - 1].column->getName() + " of argument of function " + getName(), ErrorCodes::ILLEGAL_COLUMN}; virtual_type = DataTypeFactory::instance().get(col_type_const->getValue()); @@ -137,7 +137,7 @@ public: actions.reserve(arguments.size() - 1 - ExtraArg); - for (const auto i : ext::range(1 + ExtraArg, arguments.size())) + for (const auto i : ext::range(1, arguments.size() - ExtraArg)) { if (isString(arguments[i].type)) actions.push_back(Action::key); @@ -194,7 +194,7 @@ public: if (!ok) break; - ok = tryMove(pjh, actions[j], (*block.getByPosition(arguments[j + 1 + ExtraArg]).column)[i]); + ok = tryMove(pjh, actions[j], (*block.getByPosition(arguments[j + 1]).column)[i]); } if (ok) diff --git a/dbms/tests/queries/0_stateless/00918_json_functions_avx2.sql b/dbms/tests/queries/0_stateless/00918_json_functions_avx2.sql index 5c5ddbd3985..438a256ecf8 100644 --- a/dbms/tests/queries/0_stateless/00918_json_functions_avx2.sql +++ b/dbms/tests/queries/0_stateless/00918_json_functions_avx2.sql @@ -14,7 +14,7 @@ SELECT JSONExtractInt('{"a": "hello", "b": [-100, 200.0, 300]}', 'b', 1); SELECT JSONExtractFloat('{"a": "hello", "b": [-100, 200.0, 300]}', 'b', 2); SELECT JSONExtractUInt('{"a": "hello", "b": [-100, 200.0, 300]}', 'b', -1); SELECT JSONExtract('{"a": "hello", "b": [-100, 200.0, 300]}', 'Tuple(String, String, String, Array(Float64))'); -SELECT JSONExtract('{"a": "hello", "b": [-100, 200.0, 300]}', 'Array(Int32)', 'b'); +SELECT JSONExtract('{"a": "hello", "b": [-100, 200.0, 300]}', 'b', 'Array(Int32)'); SELECT JSONExtract('{"a": "hello", "b": [-100, 200.0, 300]}', 'Array(String)'); SELECT JSONExtract('{"a": "hello", "b": [-100, 200.0, 300]}', 'Array(Tuple(Int16, Float32, UInt8))'); SELECT JSONExtractRaw('{"a": "hello", "b": [-100, 200.0, 300], "c":{"d":[121,144]}}'); diff --git a/docs/en/query_language/functions/json_functions.md b/docs/en/query_language/functions/json_functions.md index d20a7a38d2b..aee6512a772 100644 --- a/docs/en/query_language/functions/json_functions.md +++ b/docs/en/query_language/functions/json_functions.md @@ -143,7 +143,7 @@ select JSONExtractUInt('{"a": "hello", "b": [-100, 200.0, 300]}', 'b', -1) = 300 The usage of accessors is the same as above. -## JSONExtract(params, type[, accessors]...) +## JSONExtract(params[, accessors...], type) Parse data from JSON values with a given ClickHouse data type. @@ -152,7 +152,7 @@ If the value does not exist or has a wrong type, `null` will be returned. Examples: ``` -select JSONExtract('{"a": "hello", "b": [-100, 200.0, 300]}', 'Int8', 'b', 1) = -100 +select JSONExtract('{"a": "hello", "b": [-100, 200.0, 300]}', 'b', 1, 'Int8') = -100 select JSONExtract('{"a": "hello", "b": [-100, 200.0, 300]}', 'Tuple(String, String, String, Array(Float64))') = ('a', 'hello', 'b', [-100.0, 200.0, 300.0]) ```