Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON single quote normalization API #14729

Merged
merged 40 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
a1120f6
single quote normalization api
shrshi Jan 9, 2024
c6b0ba3
test for normalization api
shrshi Jan 10, 2024
d9a8acf
fixes to test
shrshi Jan 11, 2024
cfe89e6
fix to tests
shrshi Jan 11, 2024
b2ce13b
pre-commit formatting fixes
shrshi Jan 11, 2024
2134cf8
finally, the test passes
shrshi Jan 11, 2024
04e9d82
try again with test stream
shrshi Jan 11, 2024
9f53d42
Merge branch 'branch-24.02' into fst-integration
shrshi Jan 11, 2024
907aba9
Merge branch 'branch-24.02' into fst-integration
shrshi Jan 11, 2024
fa11424
added option to normalize single quotes in read_json
shrshi Jan 12, 2024
adcbddf
Merge branch 'fst-integration' of github.com:shrshi/cudf into fst-int…
shrshi Jan 12, 2024
2e86d89
formatting fixes
shrshi Jan 12, 2024
9925c10
adding testing_main
shrshi Jan 13, 2024
2838c74
java bindings
shrshi Jan 13, 2024
2313955
formatting fixes
shrshi Jan 13, 2024
a5bb42e
compile fix
shrshi Jan 13, 2024
3a6f267
Merge branch 'branch-24.02' into fst-integration
shrshi Jan 13, 2024
0926a2f
Merge branch 'branch-24.02' into fst-integration
shrshi Jan 16, 2024
e63bca0
Update java/src/test/java/ai/rapids/cudf/TableTest.java
shrshi Jan 16, 2024
005b5c2
Update java/src/test/java/ai/rapids/cudf/TableTest.java
shrshi Jan 16, 2024
1a8f5f3
added an error test for when normalize quotes is not enabled
shrshi Jan 16, 2024
a999ca4
Merge branch 'fst-integration' of github.com:shrshi/cudf into fst-int…
shrshi Jan 16, 2024
6a151f5
Merge branch 'branch-24.02' into fst-integration
shrshi Jan 17, 2024
2001866
addressing PR reviews; adding comments
shrshi Jan 18, 2024
b30e130
Merge branch 'fst-integration' of github.com:shrshi/cudf into fst-int…
shrshi Jan 18, 2024
d0fefbd
moved tests; removed duplicated fst code
shrshi Jan 18, 2024
7520e03
Merge branch 'branch-24.02' into fst-integration
shrshi Jan 18, 2024
55503e3
moved preprocess step to read_json
shrshi Jan 18, 2024
85e8053
Merge branch 'fst-integration' of github.com:shrshi/cudf into fst-int…
shrshi Jan 18, 2024
a885277
PR reviews - modifiable input buffer in normalize quotes parameter
shrshi Jan 19, 2024
64135df
Merge branch 'branch-24.02' into fst-integration
shrshi Jan 19, 2024
de1f1b3
don't need fully qualified name in enclosing namespace
shrshi Jan 20, 2024
df6c0f3
Merge branch 'fst-integration' of github.com:shrshi/cudf into fst-int…
shrshi Jan 20, 2024
8441b39
header files cleanup; more fully-qualified names cleanup
shrshi Jan 22, 2024
d5b9707
alphabetizing the new file in add_library
shrshi Jan 23, 2024
4e358fd
more header file cleanup
shrshi Jan 23, 2024
a79683d
guiding the consts eastwards
shrshi Jan 23, 2024
bcc2285
Merge branch 'branch-24.02' into fst-integration
shrshi Jan 23, 2024
890d09b
formatting fix
shrshi Jan 23, 2024
ace46d3
Merge branch 'branch-24.02' into fst-integration
shrshi Jan 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ add_library(
src/io/json/legacy/json_gpu.cu
src/io/json/legacy/reader_impl.cu
src/io/json/write_json.cu
src/io/json/json_quote_normalization.cu
shrshi marked this conversation as resolved.
Show resolved Hide resolved
src/io/orc/aggregate_orc_metadata.cpp
src/io/orc/dict_enc.cu
src/io/orc/orc.cpp
Expand Down
7 changes: 6 additions & 1 deletion cpp/include/cudf/io/detail/json.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020-2023, NVIDIA CORPORATION.
* Copyright (c) 2020-2024, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -51,4 +51,9 @@ void write_json(data_sink* sink,
json_writer_options const& options,
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr);

rmm::device_uvector<char> normalize_single_quotes(const cudf::device_span<std::byte>& inbuf,
shrshi marked this conversation as resolved.
Show resolved Hide resolved
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr);

} // namespace cudf::io::json::detail
33 changes: 32 additions & 1 deletion cpp/include/cudf/io/json.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020-2023, NVIDIA CORPORATION.
* Copyright (c) 2020-2024, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -113,6 +113,9 @@ class json_reader_options {
// Whether to keep the quote characters of string values
bool _keep_quotes = false;

// Normalize single quotes
bool _normalize_single_quotes = false;

// Whether to recover after an invalid JSON line
json_recovery_mode_t _recovery_mode = json_recovery_mode_t::FAIL;

Expand Down Expand Up @@ -246,6 +249,13 @@ class json_reader_options {
*/
bool is_enabled_keep_quotes() const { return _keep_quotes; }

/**
* @brief Whether the reader should normalize single quotes around strings
*
* @returns true if the reader should normalize single quotes, false otherwise
*/
bool is_enabled_normalize_single_quotes() const { return _normalize_single_quotes; }

/**
* @brief Queries the JSON reader's behavior on invalid JSON lines.
*
Expand Down Expand Up @@ -324,6 +334,14 @@ class json_reader_options {
*/
void enable_keep_quotes(bool val) { _keep_quotes = val; }

/**
* @brief Set whether the reader should enable normalization of single quotes around strings.
shrshi marked this conversation as resolved.
Show resolved Hide resolved
*
* @param val Boolean value to indicate whether the reader should normalize single quotes around
* string
shrshi marked this conversation as resolved.
Show resolved Hide resolved
*/
void enable_normalize_single_quotes(bool val) { _normalize_single_quotes = val; }

/**
* @brief Specifies the JSON reader's behavior on invalid JSON lines.
*
Expand Down Expand Up @@ -474,6 +492,19 @@ class json_reader_options_builder {
return *this;
}

/**
* @brief Set whether the reader should normalize single quotes around string
*
* @param val Boolean value to indicate whether the reader should normalize single quotes
* of strings
* @return this for chaining
*/
json_reader_options_builder& normalize_single_quotes(bool val)
{
options._normalize_single_quotes = val;
return *this;
}

/**
* @brief Specifies the JSON reader's behavior on invalid JSON lines.
*
Expand Down
235 changes: 235 additions & 0 deletions cpp/src/io/json/json_quote_normalization.cu
vuule marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
/*
* Copyright (c) 2024, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <io/fst/lookup_tables.cuh>
#include <io/utilities/hostdevice_vector.hpp>

#include <cudf/io/detail/json.hpp>
#include <cudf/scalar/scalar_factories.hpp>
#include <cudf/strings/repeat_strings.hpp>
#include <cudf/types.hpp>

#include <rmm/cuda_stream.hpp>
#include <rmm/cuda_stream_view.hpp>
#include <rmm/device_buffer.hpp>
#include <rmm/device_uvector.hpp>

#include <thrust/iterator/discard_iterator.h>

#include <cstdlib>
#include <string>
#include <vector>

namespace cudf::io::json {

using SymbolT = char;
using StateT = char;
using SymbolOffsetT = uint32_t;

namespace normalize_quotes {

// Type sufficiently large to index symbols within the input and output (may be unsigned)
enum class dfa_states : char { TT_OOS = 0U, TT_DQS, TT_SQS, TT_DEC, TT_SEC, TT_NUM_STATES };
shrshi marked this conversation as resolved.
Show resolved Hide resolved
enum class dfa_symbol_group_id : uint32_t {
DOUBLE_QUOTE_CHAR, ///< Quote character SG: "
SINGLE_QUOTE_CHAR, ///< Quote character SG: '
ESCAPE_CHAR, ///< Escape character SG: '\'
NEWLINE_CHAR, ///< Newline character SG: '\n'
OTHER_SYMBOLS, ///< SG implicitly matching all other characters
NUM_SYMBOL_GROUPS ///< Total number of symbol groups
};

// Aliases for readability of the transition table
constexpr auto TT_OOS = dfa_states::TT_OOS;
constexpr auto TT_DQS = dfa_states::TT_DQS;
constexpr auto TT_SQS = dfa_states::TT_SQS;
constexpr auto TT_DEC = dfa_states::TT_DEC;
constexpr auto TT_SEC = dfa_states::TT_SEC;
constexpr auto TT_NUM_STATES = static_cast<char>(dfa_states::TT_NUM_STATES);
constexpr auto NUM_SYMBOL_GROUPS = static_cast<uint32_t>(dfa_symbol_group_id::NUM_SYMBOL_GROUPS);

// The i-th string representing all the characters of a symbol group
std::array<std::vector<SymbolT>, NUM_SYMBOL_GROUPS - 1> const qna_sgs{
{{'\"'}, {'\''}, {'\\'}, {'\n'}}};

// Transition table
std::array<std::array<dfa_states, NUM_SYMBOL_GROUPS>, TT_NUM_STATES> const qna_state_tt{{
/* IN_STATE " ' \ \n OTHER */
/* TT_OOS */ {{TT_DQS, TT_SQS, TT_OOS, TT_OOS, TT_OOS}},
/* TT_DQS */ {{TT_OOS, TT_DQS, TT_DEC, TT_OOS, TT_DQS}},
/* TT_SQS */ {{TT_SQS, TT_OOS, TT_SEC, TT_OOS, TT_SQS}},
/* TT_DEC */ {{TT_DQS, TT_DQS, TT_DQS, TT_OOS, TT_DQS}},
/* TT_SEC */ {{TT_SQS, TT_SQS, TT_SQS, TT_OOS, TT_SQS}},
}};

// The DFA's starting state
constexpr char start_state = static_cast<char>(TT_OOS);

struct TransduceToNormalizedQuotes {
/**
* @brief Returns the <relative_offset>-th output symbol on the transition (state_id, match_id).
*/
template <typename StateT, typename SymbolGroupT, typename RelativeOffsetT, typename SymbolT>
constexpr CUDF_HOST_DEVICE SymbolT operator()(StateT const state_id,
SymbolGroupT const match_id,
RelativeOffsetT const relative_offset,
SymbolT const read_symbol) const
{
// -------- TRANSLATION TABLE ------------
// Let the alphabet set be Sigma
// ---------------------------------------
// ---------- NON-SPECIAL CASES: ----------
// Output symbol same as input symbol <s>
// state | read_symbol <s> -> output_symbol <s>
// DQS | Sigma -> Sigma
// DEC | Sigma -> Sigma
// OOS | Sigma\{'} -> Sigma\{'}
// SQS | Sigma\{', "} -> Sigma\{', "}
// ---------- SPECIAL CASES: --------------
// Input symbol translates to output symbol
// OOS | {'} -> {"}
// SQS | {'} -> {"}
// SQS | {"} -> {\"}
// SQS | {\} -> <nop>
// SEC | {'} -> {'}
// SEC | Sigma\{'} -> {\*}

// Whether this transition translates to the escape sequence: \"
const bool outputs_escape_sequence =
shrshi marked this conversation as resolved.
Show resolved Hide resolved
(state_id == static_cast<StateT>(dfa_states::TT_SQS)) &&
(match_id == static_cast<SymbolGroupT>(dfa_symbol_group_id::DOUBLE_QUOTE_CHAR));
// Case when a double quote needs to be replaced by the escape sequence: \"
if (outputs_escape_sequence) { return (relative_offset == 0) ? '\\' : '"'; }
// Case when a single quote needs to be replaced by a double quote
if ((match_id == static_cast<SymbolGroupT>(dfa_symbol_group_id::SINGLE_QUOTE_CHAR)) &&
((state_id == static_cast<StateT>(dfa_states::TT_SQS)) ||
(state_id == static_cast<StateT>(dfa_states::TT_OOS)))) {
return '"';
}
// Case when the read symbol is an escape character - the actual translation for \<s> for some
// symbol <s> is handled by transitions from SEC. For now, there is no output for this
// transition
if ((match_id == static_cast<SymbolGroupT>(dfa_symbol_group_id::ESCAPE_CHAR)) &&
((state_id == static_cast<StateT>(dfa_states::TT_SQS)))) {
return 0;
}
// Case when an escaped single quote in an input single-quoted string needs to be replaced by an
// unescaped single quote
if ((match_id == static_cast<SymbolGroupT>(dfa_symbol_group_id::SINGLE_QUOTE_CHAR)) &&
((state_id == static_cast<StateT>(dfa_states::TT_SEC)))) {
return '\'';
}
// Case when an escaped symbol <s> that is not a single-quote needs to be replaced with \<s>
if (state_id == static_cast<StateT>(dfa_states::TT_SEC)) {
return (relative_offset == 0) ? '\\' : read_symbol;
}
// In all other cases we simply output the input symbol
return read_symbol;
}

/**
* @brief Returns the number of output characters for a given transition. During quote
* normalization, we always emit one output character (i.e., either the input character or the
* single quote-input replaced by a double quote), except when we need to escape a double quote
* that was previously inside a single-quoted string.
*/
template <typename StateT, typename SymbolGroupT, typename SymbolT>
constexpr CUDF_HOST_DEVICE int32_t operator()(StateT const state_id,
SymbolGroupT const match_id,
SymbolT const read_symbol) const
{
// Whether this transition translates to the escape sequence: \"
const bool sqs_outputs_escape_sequence =
(state_id == static_cast<StateT>(dfa_states::TT_SQS)) &&
(match_id == static_cast<SymbolGroupT>(dfa_symbol_group_id::DOUBLE_QUOTE_CHAR));
// Number of characters to output on this transition
if (sqs_outputs_escape_sequence) { return 2; }
// Whether this transition translates to the escape sequence \<s> or unescaped '
const bool sec_outputs_escape_sequence =
(state_id == static_cast<StateT>(dfa_states::TT_SEC)) &&
(match_id != static_cast<SymbolGroupT>(dfa_symbol_group_id::SINGLE_QUOTE_CHAR));
// Number of characters to output on this transition
if (sec_outputs_escape_sequence) { return 2; }
// Whether this transition translates to no output <nop>
const bool sqs_outputs_nop =
(state_id == static_cast<StateT>(dfa_states::TT_SQS)) &&
(match_id == static_cast<SymbolGroupT>(dfa_symbol_group_id::ESCAPE_CHAR));
// Number of characters to output on this transition
if (sqs_outputs_nop) { return 0; }
return 1;
}
};

} // namespace normalize_quotes

namespace detail {

/*
std::unique_ptr<rmm::device_uvector<char>> normalize_quotes(
const cudf::device_span<std::byte>& inbuf,
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr)
{
auto parser = cudf::io::fst::detail::make_fst(
cudf::io::fst::detail::make_symbol_group_lut(cudf::io::json::normalize_quotes::qna_sgs),
cudf::io::fst::detail::make_transition_table(cudf::io::json::normalize_quotes::qna_state_tt),
cudf::io::fst::detail::make_translation_functor(
cudf::io::json::normalize_quotes::TransduceToNormalizedQuotes{}),
stream);

std::unique_ptr<rmm::device_uvector<char>> outbuf_ptr =
std::make_unique<rmm::device_uvector<char>>(inbuf.size() * 2, stream, mr);
rmm::device_scalar<SymbolOffsetT> outbuf_size(stream, mr);
parser.Transduce(reinterpret_cast<char*>(inbuf.data()),
static_cast<SymbolOffsetT>(inbuf.size()),
outbuf_ptr->data(),
thrust::make_discard_iterator(),
outbuf_size.data(),
cudf::io::json::normalize_quotes::start_state,
stream);

outbuf_ptr->resize(outbuf_size.value(stream), stream);
return outbuf_ptr;
}
*/

rmm::device_uvector<SymbolT> normalize_single_quotes(const cudf::device_span<std::byte>& inbuf,
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr)
{
auto parser = cudf::io::fst::detail::make_fst(
cudf::io::fst::detail::make_symbol_group_lut(cudf::io::json::normalize_quotes::qna_sgs),
cudf::io::fst::detail::make_transition_table(cudf::io::json::normalize_quotes::qna_state_tt),
cudf::io::fst::detail::make_translation_functor(
cudf::io::json::normalize_quotes::TransduceToNormalizedQuotes{}),
stream);

rmm::device_uvector<SymbolT> outbuf(inbuf.size() * 2, stream, mr);
rmm::device_scalar<SymbolOffsetT> outbuf_size(stream, mr);
parser.Transduce(reinterpret_cast<SymbolT*>(inbuf.data()),
static_cast<SymbolOffsetT>(inbuf.size()),
outbuf.data(),
thrust::make_discard_iterator(),
outbuf_size.data(),
cudf::io::json::normalize_quotes::start_state,
stream);

outbuf.resize(outbuf_size.value(stream), stream);
return outbuf;
}

} // namespace detail
} // namespace cudf::io::json
Loading
Loading