Changeset: 9fb6e52d5bef for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=9fb6e52d5bef
Modified Files:
monetdb5/extras/jaql/Tests/All
monetdb5/extras/jaql/Tests/json03.mal
monetdb5/extras/jaql/Tests/json03.stable.err
monetdb5/extras/jaql/Tests/json03.stable.out
monetdb5/extras/jaql/json.c
Branch: jacqueline
Log Message:
json: allow to print objects that don't start at 0@0
For convenience, start printing objects from the first element of the
kind BAT. This allows to print intermediates from extract with a
shifted offset, as illustrated in the json03 example, where two JSON
objects are "joined" in preparation for their final transform.
diffs (244 lines):
diff --git a/monetdb5/extras/jaql/Tests/All b/monetdb5/extras/jaql/Tests/All
--- a/monetdb5/extras/jaql/Tests/All
+++ b/monetdb5/extras/jaql/Tests/All
@@ -1,6 +1,7 @@
json00
json01
json02
+json03
expand00
filter00
diff --git a/monetdb5/extras/jaql/Tests/json03.mal
b/monetdb5/extras/jaql/Tests/json03.mal
new file mode 100644
--- /dev/null
+++ b/monetdb5/extras/jaql/Tests/json03.mal
@@ -0,0 +1,78 @@
+# use extract to semi-construct a new result
+
+s := io.stdout();
+
+(l1,l2,l3,l4,l5,l6,l7) :=
json.shred("[{\"id\":1,\"name\":\"Fabian\",\"data\":[0,2,true,null]},{\"id\":2,\"name\":\"Niels\"},{\"id\":3,\"name\":\"Martin\",\"data\":null}]");
+json.print(s, l1,l2,l3,l4,l5,l6,l7);
+
+(r1,r2,r3,r4,r5,r6,r7) :=
json.shred("[{\"userid\":2,\"prj\":\"sql\"},{\"userid\":1,\"prj\":[\"merovingian\",\"jdbc\",\"jacqueline\"]}]");
+json.print(s, r1,r2,r3,r4,r5,r6,r7);
+
+elems := bat.new(:oid, :oid);
+elems := bat.insert(elems, 1@0, 4@0);
+elems := bat.insert(elems, 9@0, 1@0);
+
+(r1,r2,r3,r4,r5,r6,r7) := json.extract(r1,r2,r3,r4,r5,r6,r7,elems,0@0);
+json.print(s, r1,r2,r3,r4,r5,r6,r7);
+
+elems := bat.reverse(elems);
+
+t := bat.reverse(r1);
+lastid := aggr.max(t);
+lastid := lastid + 1:oid;
+
+(l1,l2,l3,l4,l5,l6,l7) := json.extract(l1,l2,l3,l4,l5,l6,l7,elems,lastid);
+json.print(s, l1,l2,l3,l4,l5,l6,l7);
+
+# objects from l and r, order should match elems and be equal to elemes
+# if startoid is 0@0
+o5 := bat.reverse(l5);
+lo := algebra.select(o5, lastid);
+
+o5 := bat.reverse(r5);
+ro := algebra.select(o5, 0@0);
+
+# remove old outer arrays
+cleanup := bat.new(:oid, :oid);
+cleanup := bat.insert(cleanup, 0@0, nil:oid);
+cleanup := bat.insert(cleanup, lastid, nil:oid);
+l5 := algebra.kdifference(l5, cleanup);
+r5 := algebra.kdifference(r5, cleanup);
+
+# get next id for new objects to create
+t := bat.reverse(l1);
+lastid := aggr.max(t);
+lastid := lastid + 1:oid;
+
+# create objects
+ln := algebra.project(lo, "l");
+l7 := bat.insert(l7, ln);
+rn := algebra.project(ro, "r");
+r7 := bat.insert(r7, rn);
+
+lo := algebra.markT(lo, lastid);
+lo := bat.reverse(lo);
+l6 := bat.insert(l6, lo);
+ro := algebra.markT(ro, lastid);
+ro := bat.reverse(ro);
+r6 := bat.insert(r6, ro);
+
+# their kind entries
+kind := algebra.project(ro, 'o');
+
+# and the outermost array
+objs := algebra.project(ro, 0@0);
+objs := bat.reverse(objs);
+
+# merge everything
+r1 := bat.insert(r1, l1);
+r1 := bat.insert(r1, kind);
+r2 := bat.insert(r2, l2);
+r3 := bat.insert(r3, l3);
+r4 := bat.insert(r4, l4);
+r5 := bat.insert(r5, l5);
+r5 := bat.insert(r5, objs);
+r6 := bat.insert(r6, l6);
+r7 := bat.insert(r7, l7);
+
+json.print(s, r1,r2,r3,r4,r5,r6,r7);
diff --git a/monetdb5/extras/jaql/Tests/json03.stable.err
b/monetdb5/extras/jaql/Tests/json03.stable.err
new file mode 100644
--- /dev/null
+++ b/monetdb5/extras/jaql/Tests/json03.stable.err
@@ -0,0 +1,31 @@
+stderr of test 'json03` in directory 'extras/jaql` itself:
+
+
+# 11:50:58 >
+# 11:50:58 > "mserver5" "--debug=10" "--set" "gdk_nr_threads=0" "--set"
"gdk_dbfarm=/net/sofia.ins.cwi.nl/export/scratch1/fabian/tmp/mtest-jacqueline-sofia.ins.cwi.nl/five/dbfarm"
"--set" "mapi_open=true" "--set" "mapi_port=30430" "--set" "monet_prompt="
"--trace" "--forcemito" "--set" "mal_listing=2" "--dbname=mTests_extras_jaql"
"json03.mal"
+# 11:50:58 >
+
+# builtin opt gdk_dbname = demo
+# builtin opt gdk_dbfarm =
/ufs/fabian/scratch/ssd/monetdb/jacqueline/program-x86_64/var/lib/monetdb5/dbfarm
+# builtin opt gdk_debug = 0
+# builtin opt gdk_alloc_map = no
+# builtin opt gdk_vmtrim = yes
+# builtin opt monet_prompt = >
+# builtin opt monet_daemon = no
+# builtin opt mapi_port = 50000
+# builtin opt mapi_open = false
+# builtin opt mapi_autosense = false
+# builtin opt sql_optimizer = default_pipe
+# builtin opt sql_debug = 0
+# cmdline opt gdk_nr_threads = 0
+# cmdline opt gdk_dbfarm =
/net/sofia.ins.cwi.nl/export/scratch1/fabian/tmp/mtest-jacqueline-sofia.ins.cwi.nl/five/dbfarm
+# cmdline opt mapi_open = true
+# cmdline opt mapi_port = 30430
+# cmdline opt monet_prompt =
+# cmdline opt mal_listing = 2
+# cmdline opt gdk_dbname = mTests_extras_jaql
+
+# 11:50:58 >
+# 11:50:58 > "Done."
+# 11:50:58 >
+
diff --git a/monetdb5/extras/jaql/Tests/json03.stable.out
b/monetdb5/extras/jaql/Tests/json03.stable.out
new file mode 100644
--- /dev/null
+++ b/monetdb5/extras/jaql/Tests/json03.stable.out
@@ -0,0 +1,90 @@
+stdout of test 'json03` in directory 'extras/jaql` itself:
+
+
+# 11:50:58 >
+# 11:50:58 > "mserver5" "--debug=10" "--set" "gdk_nr_threads=0" "--set"
"gdk_dbfarm=/net/sofia.ins.cwi.nl/export/scratch1/fabian/tmp/mtest-jacqueline-sofia.ins.cwi.nl/five/dbfarm"
"--set" "mapi_open=true" "--set" "mapi_port=30430" "--set" "monet_prompt="
"--trace" "--forcemito" "--set" "mal_listing=2" "--dbname=mTests_extras_jaql"
"json03.mal"
+# 11:50:58 >
+
+# MonetDB 5 server v11.8.0 "jacqueline-0c67cb41e11b+"
+# Serving database 'mTests_extras_jaql', using 8 threads
+# Compiled for x86_64-pc-linux-gnu/64bit with 64bit OIDs dynamically linked
+# Found 15.662 GiB available main-memory.
+# Copyright (c) 1993-July 2008 CWI.
+# Copyright (c) August 2008-2012 MonetDB B.V., all rights reserved
+# Visit http://www.monetdb.org/ for further information
+# Listening for connection requests on mapi:monetdb://sofia.ins.cwi.nl:30430/
+# MonetDB/GIS module loaded
+# MonetDB/SQL module loaded
+# MonetDB/DataCell module not loaded: MALException:jaql.context:JAQL
environment not found
+function user.main():void;
+# use extract to semi-construct a new result
+ s := io.stdout();
+ (l1,l2,l3,l4,l5,l6,l7) :=
json.shred("[{\"id\":1,\"name\":\"Fabian\",\"data\":[0,2,true,null]},{\"id\":2,\"name\":\"Niels\"},{\"id\":3,\"name\":\"Martin\",\"data\":null}]");
+ json.print(s,l1,l2,l3,l4,l5,l6,l7);
+ (r1,r2,r3,r4,r5,r6,r7) :=
json.shred("[{\"userid\":2,\"prj\":\"sql\"},{\"userid\":1,\"prj\":[\"merovingian\",\"jdbc\",\"jacqueline\"]}]");
+ json.print(s,r1,r2,r3,r4,r5,r6,r7);
+ elems := bat.new(:oid,:oid);
+ elems := bat.insert(elems,1@0:oid,4@0);
+ elems := bat.insert(elems,9@0,1@0:oid);
+ (r1,r2,r3,r4,r5,r6,r7) := json.extract(r1,r2,r3,r4,r5,r6,r7,elems,0@0);
+ json.print(s,r1,r2,r3,r4,r5,r6,r7);
+ elems := bat.reverse(elems);
+ t := bat.reverse(r1);
+ lastid := aggr.max(t);
+ lastid := calc.+(lastid,1@0:oid);
+ (l1,l2,l3,l4,l5,l6,l7) := json.extract(l1,l2,l3,l4,l5,l6,l7,elems,lastid);
+ json.print(s,l1,l2,l3,l4,l5,l6,l7);
+# objects from l and r, order should match elems and be equal to elemes
+# if startoid is 0@0
+ o5 := bat.reverse(l5);
+ lo := algebra.select(o5,lastid);
+ o5 := bat.reverse(r5);
+ ro := algebra.select(o5,0@0);
+# remove old outer arrays
+ cleanup := bat.new(:oid,:oid);
+ cleanup := bat.insert(cleanup,0@0,nil:oid);
+ cleanup := bat.insert(cleanup,lastid,nil:oid);
+ l5 := algebra.kdifference(l5,cleanup);
+ r5 := algebra.kdifference(r5,cleanup);
+# get next id for new objects to create
+ t := bat.reverse(l1);
+ lastid := aggr.max(t);
+ lastid := calc.+(lastid,1@0:oid);
+# create objects
+ ln := algebra.project(lo,"l");
+ l7 := bat.insert(l7,ln);
+ rn := algebra.project(ro,"r");
+ r7 := bat.insert(r7,rn);
+ lo := algebra.markT(lo,lastid);
+ lo := bat.reverse(lo);
+ l6 := bat.insert(l6,lo);
+ ro := algebra.markT(ro,lastid);
+ ro := bat.reverse(ro);
+ r6 := bat.insert(r6,ro);
+# their kind entries
+ kind := algebra.project(ro,'o');
+# and the outermost array
+ objs := algebra.project(ro,0@0);
+ objs := bat.reverse(objs);
+# merge everything
+ r1 := bat.insert(r1,l1);
+ r1 := bat.insert(r1,kind);
+ r2 := bat.insert(r2,l2);
+ r3 := bat.insert(r3,l3);
+ r4 := bat.insert(r4,l4);
+ r5 := bat.insert(r5,l5);
+ r5 := bat.insert(r5,objs);
+ r6 := bat.insert(r6,l6);
+ r7 := bat.insert(r7,l7);
+ json.print(s,r1,r2,r3,r4,r5,r6,r7);
+end main;
+[ { "id": 1, "name": "Fabian", "data": [ 0, 2, true, null ] }, { "id": 2,
"name": "Niels" }, { "id": 3, "name": "Martin", "data": null } ]
+[ { "userid": 2, "prj": "sql" }, { "userid": 1, "prj": [ "merovingian",
"jdbc", "jacqueline" ] } ]
+[ { "userid": 1, "prj": [ "merovingian", "jdbc", "jacqueline" ] }, { "userid":
2, "prj": "sql" } ]
+[ { "id": 1, "name": "Fabian", "data": [ 0, 2, true, null ] }, { "id": 2,
"name": "Niels" } ]
+[ { "r": { "userid": 1, "prj": [ "merovingian", "jdbc", "jacqueline" ] }, "l":
{ "id": 1, "name": "Fabian", "data": [ 0, 2, true, null ] } }, { "r": {
"userid": 2, "prj": "sql" }, "l": { "id": 2, "name": "Niels" } } ]
+
+# 11:50:58 >
+# 11:50:58 > "Done."
+# 11:50:58 >
+
diff --git a/monetdb5/extras/jaql/json.c b/monetdb5/extras/jaql/json.c
--- a/monetdb5/extras/jaql/json.c
+++ b/monetdb5/extras/jaql/json.c
@@ -520,11 +520,13 @@ str
JSONprint(int *ret, stream **s, int *kind, int *string, int *integer, int
*doble, int *array, int *object, int *name)
{
jsonbat jb;
- oid first = 0;
+ BATiter bi;
loadbats();
- print_json_value(&jb, *s, first);
+ bi = bat_iterator(jb.kind);
+
+ print_json_value(&jb, *s, *(oid *)BUNhead(bi, BUNfirst(jb.kind)));
mnstr_printf(*s, "\n");
unloadbats();
_______________________________________________
Checkin-list mailing list
[email protected]
http://mail.monetdb.org/mailman/listinfo/checkin-list