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

Add schema support for load_csv #87

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
16 changes: 13 additions & 3 deletions examples/load_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ def _sansext(fname: str) -> str:


def run(database: str, engine: str, fname: str, relation: str,
syntax: dict, profile: str):
syntax: dict, schema: dict, profile: str):
data = _read(fname)
relation = relation or _sansext(fname)
cfg = config.read(profile=profile)
ctx = api.Context(**cfg)
rsp = api.load_csv(ctx, database, engine, relation, data, syntax)
rsp = api.load_csv(ctx, database, engine, relation, data, syntax, schema)
print(json.dumps(rsp, indent=2))


Expand All @@ -57,6 +57,13 @@ def run(database: str, engine: str, fname: str, relation: str,
help="relation name (default: file name)")
p.add_argument("-p", "--profile", type=str, default="default",
help="profile name")
p.add_argument(
Copy link
Collaborator

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

Copy link
Author

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.

"--schema",
type=str,
default="",
help="Comma separated list of expressions `col=type` specifying that `col` has Rel type `type`."
)

args = p.parse_args()
syntax = {} # find full list of syntax options in the RAI docs
if args.header_row is not None:
Expand All @@ -67,8 +74,11 @@ def run(database: str, engine: str, fname: str, relation: str,
syntax["escapechar"] = args.escapechar
if args.quotechar:
syntax["quotechar"] = args.quotechar

schema = {col: type for col, type in [pair.split("=") for pair in args.schema.split(",")]}

try:
run(args.database, args.engine, args.file,
args.relation, syntax, args.profile)
args.relation, syntax, schema, args.profile)
except HTTPError as e:
show.http_error(e)
43 changes: 32 additions & 11 deletions railib/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Copy link
Collaborator

Choose a reason for hiding this comment

The 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.

Copy link
Author

Choose a reason for hiding this comment

The 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)


Expand Down