-
Notifications
You must be signed in to change notification settings - Fork 982
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
Add comma separated value parsing as an option in iterate_many #2016
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
#include "simdjson.h" | ||
#include "test_ondemand.h" | ||
|
||
#include <cstdint> | ||
|
||
using namespace simdjson; | ||
|
||
namespace iterate_many_csv_tests { | ||
using namespace std; | ||
|
||
bool normal() { | ||
TEST_START(); | ||
auto json = R"( 1, 2, 3, 4, "a", "b", "c", {"hello": "world"} , [1, 2, 3])"_padded; | ||
ondemand::parser parser; | ||
ondemand::document_stream doc_stream; | ||
ASSERT_SUCCESS(parser.iterate_many(json, json.size(), true).get(doc_stream)); | ||
|
||
for (auto doc : doc_stream) | ||
{ | ||
ASSERT_SUCCESS(doc); | ||
} | ||
|
||
TEST_SUCCEED(); | ||
} | ||
|
||
bool small_batch_size() { | ||
TEST_START(); | ||
auto json = R"( 1, 2, 3, 4, "a", "b", "c", {"hello": "world"} , [1, 2, 3])"_padded; | ||
ondemand::parser parser; | ||
ondemand::document_stream doc_stream; | ||
ASSERT_SUCCESS(parser.iterate_many(json, 32, true).get(doc_stream)); | ||
|
||
for (auto doc : doc_stream) | ||
{ | ||
ASSERT_SUCCESS(doc); | ||
} | ||
|
||
TEST_SUCCEED(); | ||
} | ||
|
||
bool trailing_comma() { | ||
TEST_START(); | ||
auto json = R"(1,)"_padded; | ||
ondemand::parser parser; | ||
ondemand::document_stream doc_stream; | ||
ASSERT_SUCCESS(parser.iterate_many(json, json.size(), true).get(doc_stream)); | ||
|
||
for (auto doc : doc_stream) | ||
{ | ||
ASSERT_SUCCESS(doc); | ||
} | ||
|
||
TEST_SUCCEED(); | ||
} | ||
|
||
bool check_parsed_values() { | ||
TEST_START(); | ||
|
||
auto json = R"( 1 , "a" , [100, 1] , {"hello" : "world"} , )"_padded; | ||
ondemand::parser parser; | ||
ondemand::document_stream doc_stream; | ||
ASSERT_SUCCESS(parser.iterate_many(json, json.size(), true).get(doc_stream)); | ||
|
||
auto begin = doc_stream.begin(); | ||
auto end = doc_stream.end(); | ||
int cnt = 0; | ||
auto it = begin; | ||
for (; it != end && cnt < 4; ++it, ++cnt) { | ||
auto doc = *it; | ||
switch (cnt) | ||
{ | ||
case 0: | ||
{ | ||
int64_t actual; | ||
ASSERT_SUCCESS(doc.get_int64().get(actual)); | ||
ASSERT_EQUAL(actual, 1); | ||
break; | ||
} | ||
case 1: | ||
{ | ||
std::string_view sv; | ||
ASSERT_SUCCESS(doc.get_string().get(sv)); | ||
ASSERT_EQUAL(sv, "a"); | ||
break; | ||
} | ||
case 2: | ||
{ | ||
std::vector<int64_t> expected{100, 1}; | ||
ondemand::array arr; | ||
ASSERT_SUCCESS(doc.get_array().get(arr)); | ||
size_t element_count; | ||
ASSERT_SUCCESS(arr.count_elements().get(element_count)); | ||
ASSERT_EQUAL(element_count, 2); | ||
int i = 0; | ||
for (auto a : arr) | ||
{ | ||
int64_t actual; | ||
ASSERT_SUCCESS(a.get(actual)); | ||
ASSERT_EQUAL(actual, expected[i++]); | ||
} | ||
break; | ||
} | ||
case 3: | ||
{ | ||
ondemand::object obj; | ||
ASSERT_SUCCESS(doc.get_object().get(obj)); | ||
std::string_view sv; | ||
obj.find_field("hello").get(sv); | ||
ASSERT_EQUAL(sv, "world"); | ||
break; | ||
} | ||
default: | ||
TEST_FAIL("Too many cases") | ||
} | ||
} | ||
|
||
ASSERT_EQUAL(cnt, 4); | ||
ASSERT_TRUE(!(it != end)); | ||
|
||
TEST_SUCCEED(); | ||
} | ||
|
||
#if SIMDJSON_EXCEPTIONS | ||
|
||
bool leading_comma() { | ||
TEST_START(); | ||
auto json = R"(,1)"_padded; | ||
ondemand::parser parser; | ||
ondemand::document_stream doc_stream; | ||
ASSERT_SUCCESS(parser.iterate_many(json, json.size(), true).get(doc_stream)); | ||
|
||
try { | ||
auto begin = doc_stream.begin(); | ||
auto end = doc_stream.end(); | ||
for (auto it = begin; it != end; ++it) {} | ||
} catch (simdjson_error& e) { | ||
ASSERT_ERROR(e.error(), TAPE_ERROR); | ||
} | ||
|
||
TEST_SUCCEED(); | ||
} | ||
|
||
#endif | ||
|
||
bool run() { | ||
return normal() && | ||
small_batch_size() && | ||
trailing_comma() && | ||
check_parsed_values() && | ||
#if SIMDJSON_EXCEPTIONS | ||
leading_comma() && | ||
#endif | ||
true; | ||
} | ||
|
||
} | ||
|
||
int main(int argc, char *argv[]) { | ||
return test_main(argc, argv, iterate_many_csv_tests::run); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I absolutely love that this is so confined to a single place! I'm a little surprised we couldn't use advance(), though, switching from thinking in terms of tokens (which
,
is one of) to thinking of characters does seem like a dangerous thing to me, even if it's confined to this one place where it works.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jkeiser I merged this because, as you have remarked, it is a very neat patch that is quite isolated.
We can change the design.
Are you saying that we should just skip over the token, no matter what it is? Ignoring its nature?
Can you elaborate on your concern?
(I am 100% open to changing this.)