Changeset: 9522e65c8959 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/9522e65c8959
Modified Files:
        monetdb5/modules/atoms/json.c
        sql/scripts/40_json.sql
        sql/server/sql_parser.y
Branch: json-extend
Log Message:

introduce new function JSON_VALUE


diffs (103 lines):

diff --git a/monetdb5/modules/atoms/json.c b/monetdb5/modules/atoms/json.c
--- a/monetdb5/modules/atoms/json.c
+++ b/monetdb5/modules/atoms/json.c
@@ -1661,7 +1661,7 @@ void* JSONrealloc(void* ctx, void *ptr, 
 }
 
 static str
-JSONfilter(json *ret, const json *js, const char *const *expr)
+JSONvalue(json *ret, const json *js, const char *const *expr)
 {
        if (strNil(*js) || strNil(*expr)) {
                if (!(*ret = GDKstrdup(str_nil)))
@@ -1713,6 +1713,17 @@ JSONfilter(json *ret, const json *js, co
        return MAL_SUCCEED;
 }
 
+static str
+JSONfilter(json *ret, const json *js, const char *const *expr)
+{
+       if (strNil(*js) || strNil(*expr)) {
+               if (!(*ret = GDKstrdup(str_nil)))
+                       throw(MAL, "json.filter", SQLSTATE(HY013) 
MAL_MALLOC_FAIL);
+               return MAL_SUCCEED;
+       }
+       return JSONfilterInternal(ret, js, expr, 0);
+}
+
 // glue all values together with an optional separator
 // The json string should be valid
 
@@ -3260,6 +3271,7 @@ static mel_func json_init_funcs[] = {
  command("json", "number", JSONjson2number, false, "Convert simple JSON values 
to a double, return nil upon error.", args(1,2, arg("",dbl),arg("j",json))),
  command("json", "integer", JSONjson2integer, false, "Convert simple JSON 
values to an integer, return nil upon error.", args(1,2, 
arg("",lng),arg("j",json))),
  pattern("json", "dump", JSONdump, false, "", args(1,2, 
batarg("",str),arg("j",json))),
+ command("json", "json_value", JSONvalue, false, "TODO", args(1,3, 
arg("",json),arg("js",json),arg("pathexpr",str))),
  command("json", "filter", JSONfilter, false, "Filter all members of an object 
by a path expression, returning an array.\nNon-matching elements are skipped.", 
args(1,3, arg("",json),arg("name",json),arg("pathexpr",str))),
  command("json", "filter", JSONfilterArray_bte, false, "", args(1,3, 
arg("",json),arg("name",json),arg("idx",bte))),
  command("json", "filter", JSONfilterArrayDefault_bte, false, "", args(1,4, 
arg("",json),arg("name",json),arg("idx",bte),arg("other",str))),
diff --git a/sql/scripts/40_json.sql b/sql/scripts/40_json.sql
--- a/sql/scripts/40_json.sql
+++ b/sql/scripts/40_json.sql
@@ -16,6 +16,11 @@ create schema json;
 create type json external name json;
 
 -- access the top level key by name, return its value
+create function sys.json_value(js json, pathexpr string)
+returns json external name json.json_value;
+GRANT EXECUTE ON FUNCTION sys.json_value(json, string) TO PUBLIC;
+
+-- access the top level key by name, return its value
 create function json.filter(js json, pathexpr string)
 returns json external name json.filter;
 GRANT EXECUTE ON FUNCTION json.filter(json, string) TO PUBLIC;
diff --git a/sql/server/sql_parser.y b/sql/server/sql_parser.y
--- a/sql/server/sql_parser.y
+++ b/sql/server/sql_parser.y
@@ -283,6 +283,7 @@ int yydebug=1;
        interval_expression
        join_spec
        joined_table
+       json_funcs
        like_exp
        like_predicate
        like_table
@@ -725,7 +726,7 @@ int yydebug=1;
 %left <operation> ALL ANY NOT_BETWEEN BETWEEN NOT_IN sqlIN NOT_EXISTS EXISTS 
NOT_LIKE LIKE NOT_ILIKE ILIKE OR SOME
 %left <operation> AND
 %left <sval> COMPARISON /* <> < > <= >= */
-%left <operation> '+' '-' '&' '|' '^' LEFT_SHIFT RIGHT_SHIFT LEFT_SHIFT_ASSIGN 
RIGHT_SHIFT_ASSIGN CONCATSTRING SUBSTRING TRIM POSITION SPLIT_PART
+%left <operation> '+' '-' '&' '|' '^' LEFT_SHIFT RIGHT_SHIFT LEFT_SHIFT_ASSIGN 
RIGHT_SHIFT_ASSIGN CONCATSTRING JSON_VALUE SUBSTRING TRIM POSITION SPLIT_PART
 %left <operation> '*' '/' '%'
 %left UMINUS
 %left <operation> '~'
@@ -4454,6 +4455,7 @@ value_exp:
  |  NEXT VALUE FOR qname                  { $$ = _symbol_create_list(SQL_NEXT, 
$4); }
  |  null
  |  param
+ |  json_funcs
  |  string_funcs
  |  XML_value_function
  |  odbc_scalar_func_escape
@@ -4665,6 +4667,21 @@ opt_trim_characters:
   | string {$$ = $1; }
   ;
 
+json_funcs:
+       JSON_VALUE '(' scalar_exp ',' scalar_exp ')'
+       {
+                dlist *l = L();
+               dlist *ops = L();
+               append_list(l,
+               append_string(L(), sa_strdup(SA, "json_value")));
+               append_int(l, FALSE); /* ignore distinct */
+               append_symbol(ops, $3);
+               append_symbol(ops, $5);
+               append_list(l, ops);
+               $$ = _symbol_create_list( SQL_NOP, l );
+       }
+
+
 string_funcs:
     SUBSTRING '(' scalar_exp FROM scalar_exp FOR scalar_exp ')'
                        { dlist *l = L();
_______________________________________________
checkin-list mailing list -- checkin-list@monetdb.org
To unsubscribe send an email to checkin-list-le...@monetdb.org

Reply via email to