Skip to content

Commit

Permalink
edgeql: Add converter functions for multiple data types.
Browse files Browse the repository at this point in the history
The `to_str` function is meant to be like a cast when called
without the format parameter.

`to_str` usage with `json` scalars is not equivalent to casting, it is
producing a string representation of JSON. The inverse function `to_json`
is therefore parsing a string, not casting either:
- `str_to_json` becomes `to_json`
- `json_to_str` becomes `to_str`

Add various date and time converter functions.
  • Loading branch information
vpetrovykh committed Nov 21, 2018
1 parent 394491a commit 19e6a23
Show file tree
Hide file tree
Showing 14 changed files with 1,004 additions and 135 deletions.
2 changes: 1 addition & 1 deletion edb/lang/graphql/translator.py
Expand Up @@ -118,7 +118,7 @@ def visit_Document(self, node):
for el in eql[0].result.elements:
# swap in the json bits
if (isinstance(el.compexpr, qlast.FunctionCall) and
el.compexpr.func == 'str_to_json'):
el.compexpr.func == 'to_json'):
name = el.expr.steps[0].ptr.name
el.compexpr.args[0].arg = qlast.StringConstant.from_python(
json.dumps(gqlresult.data[name], indent=4))
Expand Down
2 changes: 1 addition & 1 deletion edb/lang/graphql/types.py
Expand Up @@ -572,7 +572,7 @@ def get_template(self):
'''

if self.dummy:
return parse_fragment(f'''str_to_json("xxx")'''), None, None
return parse_fragment(f'''to_json("xxx")'''), None, None

eql = parse_fragment(f'''
SELECT {self.edb_base_name} {{
Expand Down
2 changes: 1 addition & 1 deletion edb/lib/std/20-genericfuncs.eql
Expand Up @@ -44,7 +44,7 @@ CREATE FUNCTION
std::len(array: array<anytype>) -> std::int64
{
FROM SQL $$
SELECT COALESCE(array_length("array", 1), 0)::bigint
SELECT cardinality("array")::bigint
$$;
};

Expand Down
18 changes: 18 additions & 0 deletions edb/lib/std/30-datetimefuncs.eql
Expand Up @@ -84,3 +84,21 @@ std::timedelta_get(dt: std::timedelta, el: std::str) -> std::float64
SELECT date_part("el", dt)
$$;
};


CREATE FUNCTION
std::datetime_trunc(dt: std::datetime, unit: std::str) -> std::datetime
{
FROM SQL $$
SELECT date_trunc(unit, dt)
$$;
};


CREATE FUNCTION
std::timedelta_trunc(dt: std::timedelta, unit: std::str) -> std::timedelta
{
FROM SQL $$
SELECT date_trunc(unit, dt)
$$;
};
18 changes: 0 additions & 18 deletions edb/lib/std/30-jsonfuncs.eql
Expand Up @@ -54,21 +54,3 @@ std::json_get(
)
$$;
};


CREATE FUNCTION
std::json_to_str(json: std::json) -> std::str
{
FROM SQL $$
SELECT "json"::text
$$;
};


CREATE FUNCTION
std::str_to_json(str: std::str) -> std::json
{
FROM SQL $$
SELECT "str"::jsonb
$$;
};

0 comments on commit 19e6a23

Please sign in to comment.