-
Notifications
You must be signed in to change notification settings - Fork 4
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 schema support for load_csv
#87
Open
gbrgr
wants to merge
6
commits into
main
Choose a base branch
from
gbrgr/feature/csv-loading-schema
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bac953f
Add schema support for `load_csv`
gbrgr 97b10fd
Adapt formatting
gbrgr 0f1f664
Change schema string composition
gbrgr 53ad4e1
Merge branch 'main' into gbrgr/feature/csv-loading-schema
gbrgr 68a93d8
autoformat using pep8
gbrgr 4ae5eef
Autoformat using pep8
gbrgr 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 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 |
---|---|---|
|
@@ -612,27 +612,48 @@ def _gen_syntax_config(syntax: dict = {}) -> str: | |
return result | ||
|
||
|
||
# `syntax`: | ||
# * header: a map from col number to name (base 1) | ||
# * header_row: row number of header, 0 means no header (default: 1) | ||
# * delim: default: , | ||
# * quotechar: default: " | ||
# * escapechar: default: \ | ||
# | ||
# Schema: a map from col name to rel type name, eg: | ||
# {'a': "int", 'b': "string"} | ||
def load_csv(ctx: Context, database: str, engine: str, relation: str, | ||
data: str or io.TextIOBase, syntax: dict = {}) -> dict: | ||
data: str or io.TextIOBase, syntax: dict = {}, schema = {}) -> dict: | ||
""" | ||
Loads CSV data present in `data` into `database` using `engine`. Upon | ||
success, parsed CSV data is stored in `relation`. | ||
|
||
Args: | ||
- `ctx` (`Context`): The RAI API context. | ||
- `database` (`str`): The target database name. | ||
- `engine` (`str`): The engine used for loading. | ||
- `relation` (`str`): Relation name used to store CSV data. | ||
- `data` (`str or or io.TextIOBase`): Data specified either as a string or as a stream of type `io.TextIOBase`. | ||
- `syntax` (`dict`, optional): Dictionary containing parsing configuration, defaults to {}. Valid entries are: | ||
- `header`: A dictionary mapping column numbers to a names. | ||
- `header_row`: the row number of the header row; 0 means no header. Defaults to `1`. | ||
- `delim`: Column delimiter used. Defaults to `,`. | ||
- `quotechar`: Quotation character used. Defaults to `"`. | ||
- `escapechar`: Escape charater used. Defaults to `\`. | ||
- `schema` (`dict`, optional): Dictionary mapping column names to Rel type names. Defaults to `{}`. | ||
Raises: | ||
`TypeError`: If `data` is neither `str` nor `io.TextIOBase`. | ||
|
||
Returns: | ||
`dict`: The response of the query action. | ||
""" | ||
if isinstance(data, str): | ||
pass # ok | ||
elif isinstance(data, io.TextIOBase): | ||
data = data.read() | ||
else: | ||
raise TypeError(f"bad type for arg 'data': {data.__class__.__name__}") | ||
|
||
inputs = {'data': data} | ||
command = _gen_syntax_config(syntax) | ||
command += "".join( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. comprehension nested in a join behind a plusop is not ideal expression complexity. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh how bad. Changed that! |
||
[f'def config:schema[:"{col}"] = "{type}"\n' for col, type in schema.items()] | ||
) | ||
command += ("def config:data = data\n" | ||
"def insert:%s = load_csv[config]" % relation) | ||
f"def insert[:{relation}] = load_csv[config]") | ||
|
||
print(command) | ||
|
||
return query(ctx, database, engine, command, inputs=inputs, readonly=False) | ||
|
||
|
||
|
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.
please format this like the surrounding code
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.
The sdk now uses flake8 for linting, so that formatting changed. I autoformatted using pep8 now.