Skip to content

Commit

Permalink
Fix comment
Browse files Browse the repository at this point in the history
  • Loading branch information
PHILO-HE committed Dec 10, 2024
1 parent 2ca3533 commit 5926631
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
15 changes: 8 additions & 7 deletions velox/functions/sparksql/GetJsonObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,11 @@ struct GetJsonObjectFunction {
if (simdjsonParse(paddedJson).get(jsonDoc)) {
return false;
}
auto formattedJsonPath = jsonPath_.has_value()
const auto formattedJsonPath = jsonPath_.has_value()
? jsonPath_.value()
: removeSingleQuotes(jsonPath);
try {
// Can return error result or throw exception possibly.
auto rawResult = jsonDoc.at_path(formattedJsonPath);
if (rawResult.error()) {
return false;
Expand All @@ -81,15 +82,16 @@ struct GetJsonObjectFunction {
private:
FOLLY_ALWAYS_INLINE bool checkJsonPath(StringView jsonPath) {
// Spark requires the first char in jsonPath is '$'.
if (jsonPath.size() < 1 || jsonPath.data()[0] != '$') {
if (jsonPath.empty() || jsonPath.data()[0] != '$') {
return false;
}
return true;
}

// Spark's json path requires field name surrounded by single quotes if it is
// specified in "[]". But simdjson lib requires not. This method just removes
// such single quotes, e.g., converts "['a']['b']" to "[a][b]".
// such single quotes to adapt to simdjson lib, e.g., converts "['a']['b']" to
// "[a][b]".
std::string removeSingleQuotes(StringView jsonPath) {
// Skip the initial "$".
std::string result(jsonPath.data() + 1, jsonPath.size() - 1);
Expand All @@ -100,6 +102,7 @@ struct GetJsonObjectFunction {
break;
}
pairEnd = result.find("]", pairBegin);
// If expected pattern, like ['a'], is not found.
if (pairEnd == std::string::npos || result[pairEnd - 1] != '\'') {
return "-1";
}
Expand All @@ -112,8 +115,7 @@ struct GetJsonObjectFunction {

// Extracts a string representation from a simdjson result. Handles various
// JSON types including numbers, booleans, strings, objects, and arrays.
// Returns true if the conversion wes successful, false
// otherwise.
// Returns true if the conversion is successful. Otherwise, returns false.
bool extractStringResult(
simdjson::simdjson_result<simdjson::ondemand::value> rawResult,
out_type<Varchar>& result) {
Expand Down Expand Up @@ -198,8 +200,7 @@ struct GetJsonObjectFunction {
if (endingChar == ',' || endingChar == '}' || endingChar == ']') {
return true;
}
// These chars can be prior to a valid ending char.
// See reference:
// These chars can be prior to a valid ending char. See reference:
// https://github.com/simdjson/simdjson/blob/v3.9.0/dependencies/jsoncppdist/jsoncpp.cpp
if (endingChar == ' ' || endingChar == '\r' || endingChar == '\n' ||
endingChar == '\t') {
Expand Down
3 changes: 1 addition & 2 deletions velox/functions/sparksql/tests/GetJsonObjectTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdint.h>
#include "velox/functions/sparksql/tests/SparkFunctionBaseTest.h"
#include "velox/type/Type.h"

#include <stdint.h>

namespace facebook::velox::functions::sparksql::test {
namespace {

Expand Down

0 comments on commit 5926631

Please sign in to comment.