-
Notifications
You must be signed in to change notification settings - Fork 4
support json type #62
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,39 @@ | ||
from .base import Column | ||
from .stringcolumn import String | ||
from ..reader import read_binary_uint8, read_binary_str | ||
from ..util.compat import json | ||
from ..writer import write_binary_uint8 | ||
|
||
|
||
class JsonColumn(Column): | ||
py_types = (dict, ) | ||
|
||
# No NULL value actually | ||
null_value = {} | ||
|
||
def __init__(self, column_by_spec_getter, **kwargs): | ||
self.column_by_spec_getter = column_by_spec_getter | ||
self.string_column = String(**kwargs) | ||
super(JsonColumn, self).__init__(**kwargs) | ||
|
||
def write_state_prefix(self, buf): | ||
# Read in binary format. | ||
# Write in text format. | ||
write_binary_uint8(1, buf) | ||
|
||
def read_items(self, n_items, buf): | ||
read_binary_uint8(buf) | ||
spec = read_binary_str(buf) | ||
col = self.column_by_spec_getter( | ||
spec, dict(namedtuple_as_json=True) | ||
) | ||
col.read_state_prefix(buf) | ||
return col.read_data(n_items, buf) | ||
|
||
def write_items(self, items, buf): | ||
items = [x if isinstance(x, str) else json.dumps(x) for x in items] | ||
self.string_column.write_items(items, buf) | ||
|
||
|
||
def create_json_column(spec, column_by_spec_getter, column_options): | ||
return JsonColumn(column_by_spec_getter, **column_options) |
This file contains hidden or 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 |
---|---|---|
@@ -1,73 +1,10 @@ | ||
|
||
from .arraycolumn import create_array_column | ||
from .util import get_inner_spec | ||
|
||
|
||
def create_nested_column(spec, column_by_spec_getter): | ||
def create_nested_column(spec, column_by_spec_getter, column_options): | ||
return create_array_column( | ||
'array(tuple({}))'.format(','.join(get_nested_columns(spec))), | ||
column_by_spec_getter=column_by_spec_getter | ||
'array(tuple({}))'.format(get_inner_spec('nested', spec)), | ||
column_by_spec_getter, column_options | ||
) | ||
|
||
|
||
def get_nested_columns(spec): | ||
brackets = 0 | ||
column_begin = 0 | ||
|
||
inner_spec = get_inner_spec(spec) | ||
nested_columns = [] | ||
for i, x in enumerate(inner_spec + ','): | ||
if x == ',': | ||
if brackets == 0: | ||
nested_columns.append(inner_spec[column_begin:i]) | ||
column_begin = i + 1 | ||
elif x == '(': | ||
brackets += 1 | ||
elif x == ')': | ||
brackets -= 1 | ||
elif x == ' ': | ||
if brackets == 0: | ||
column_begin = i + 1 | ||
return nested_columns | ||
|
||
|
||
def get_columns_with_types(spec): | ||
brackets = 0 | ||
prev_comma = 0 | ||
prev_space = 0 | ||
|
||
inner_spec = get_inner_spec(spec) | ||
columns_with_types = [] | ||
|
||
for i, x in enumerate(inner_spec + ','): | ||
if x == ',': | ||
if brackets == 0: | ||
columns_with_types.append(( | ||
inner_spec[prev_comma:prev_space].strip(), | ||
inner_spec[prev_space:i] | ||
)) | ||
prev_comma = i + 1 | ||
elif x == '(': | ||
brackets += 1 | ||
elif x == ')': | ||
brackets -= 1 | ||
elif x == ' ': | ||
if brackets == 0: | ||
prev_space = i + 1 | ||
return columns_with_types | ||
|
||
|
||
def get_inner_spec(spec): | ||
brackets = 0 | ||
offset = len('nested') | ||
i = offset | ||
for i, ch in enumerate(spec[offset:], offset): | ||
if ch == '(': | ||
brackets += 1 | ||
|
||
elif ch == ')': | ||
brackets -= 1 | ||
|
||
if brackets == 0: | ||
break | ||
|
||
return spec[offset + 1:i] |
This file contains hidden or 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 hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.