Changeset: 75b51dfbbf77 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/75b51dfbbf77
Modified Files:
monetdb5/modules/mal/tablet.c
sql/backends/monet5/rel_bin.c
sql/backends/monet5/sql.c
sql/backends/monet5/sql_statement.c
sql/backends/monet5/sql_statement.h
sql/storage/store_sequence.c
sql/storage/store_sequence.h
Branch: nested
Log Message:
Supports insert of empty json array
diffs (277 lines):
diff --git a/monetdb5/modules/mal/tablet.c b/monetdb5/modules/mal/tablet.c
--- a/monetdb5/modules/mal/tablet.c
+++ b/monetdb5/modules/mal/tablet.c
@@ -347,7 +347,6 @@ output_composite(char **buf, size_t *len
* rowid column sorted
* else
*/
- assert(rowid->c->tsorted);
if (rowid->c->tkey)
fill = output_multiset_dense(buf, len, fill,
localbuf, locallen, fmt + j + 1, nr_attrs-1, f->multiset, f->composite, true,
*(int*)p);
else
diff --git a/sql/backends/monet5/rel_bin.c b/sql/backends/monet5/rel_bin.c
--- a/sql/backends/monet5/rel_bin.c
+++ b/sql/backends/monet5/rel_bin.c
@@ -6106,19 +6106,14 @@ insert_ms(backend *be, sql_table *st, sq
stmt *msid = updates[len-1 -((ct->multiset == MS_ARRAY)?1:0)];
/* nrowids = next_value_for(rowids, "schema?", st->base.name) */
- InstrPtr q = newStmt(be->mb, batsqlRef, "next_value_ms");
- q = pushArgument(be->mb, q, rowids->nr);
- q = pushStr(be->mb, q, st->s->base.name); /* sequence schema name */
- q = pushStr(be->mb, q, st->base.name); /* sequence number name */
- pushInstruction(be->mb, q);
-
- /* nrowids = batcalc.int(nrowids) */
- InstrPtr r = newStmt(be->mb, batcalcRef, "int");
- r = pushArgument(be->mb, r, getArg(q, 0));
+ InstrPtr r = newStmt(be->mb, batsqlRef, "next_value_ms");
+ r = pushArgument(be->mb, r, rowids->nr);
+ r = pushStr(be->mb, r, st->s->base.name); /* sequence schema name */
+ r = pushStr(be->mb, r, st->base.name); /* sequence number name */
pushInstruction(be->mb, r);
/* msid = renumber(msid, rowids, nrowids); */
- q = newStmt(be->mb, batsqlRef, "renumber");
+ InstrPtr q = newStmt(be->mb, batsqlRef, "renumber");
q = pushArgument(be->mb, q, msid->nr);
q = pushArgument(be->mb, q, rowids->nr);
q = pushArgument(be->mb, q, getArg(r, 0));
diff --git a/sql/backends/monet5/sql.c b/sql/backends/monet5/sql.c
--- a/sql/backends/monet5/sql.c
+++ b/sql/backends/monet5/sql.c
@@ -1095,17 +1095,7 @@ mvc_next_value_bulk(Client cntxt, MalBlk
sql_schema *s;
sql_sequence *seq;
bat *res = getArgReference_bat(stk, pci, 0);
- BUN card = 0;
- if (getArgType(mb, pci,1) == TYPE_lng)
- card = (BUN)*getArgReference_lng(stk, pci, 1);
- else {
- bat bid = *getArgReference_bat(stk, pci, 1);
- BAT *b = BATdescriptor(bid);
- if (!b)
- return createException(SQL, "sql.next_value",
SQLSTATE(HY002) RUNTIME_OBJECT_MISSING);
- card = BATcount(b);
- BBPreclaim(b);
- }
+ BUN card = (BUN)*getArgReference_lng(stk, pci, 1);
const char *sname = *getArgReference_str(stk, pci, 2);
const char *seqname = *getArgReference_str(stk, pci, 3);
BAT *r = NULL;
@@ -1140,6 +1130,71 @@ mvc_next_value_bulk(Client cntxt, MalBlk
throw(SQL, "sql.next_value", SQLSTATE(HY050) "Cannot generate next
sequence value %s.%s", sname, seqname);
}
+static str
+mvc_next_value_ms(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
+{
+ backend *be = NULL;
+ str msg;
+ sql_schema *s;
+ sql_sequence *seq;
+ bat *res = getArgReference_bat(stk, pci, 0);
+ bat bid = *getArgReference_bat(stk, pci, 1);
+ BAT *b = BATdescriptor(bid);
+ if (!b)
+ return createException(SQL, "sql.next_value", SQLSTATE(HY002)
RUNTIME_OBJECT_MISSING);
+ BUN card = BATcount(b);
+ const char *sname = *getArgReference_str(stk, pci, 2);
+ const char *seqname = *getArgReference_str(stk, pci, 3);
+ BAT *r = NULL;
+
+ (void)mb;
+ if ((msg = getBackendContext(cntxt, &be)) != NULL)
+ return msg;
+ if (!(s = mvc_bind_schema(be->mvc, sname)))
+ throw(SQL, "sql.next_value", SQLSTATE(3F000) "Cannot find the
schema %s", sname);
+ if (!mvc_schema_privs(be->mvc, s))
+ throw(SQL, "sql.next_value", SQLSTATE(42000) "Access denied for
%s to schema '%s'", get_string_global_var(be->mvc, "current_user"),
s->base.name);
+ if (!(seq = find_sql_sequence(be->mvc->session->tr, s, seqname)))
+ throw(SQL, "sql.next_value", SQLSTATE(HY050) "Cannot find the
sequence %s.%s", sname, seqname);
+ if (!(r = COLnew(0, TYPE_int, card, TRANSIENT)))
+ throw(SQL, "sql.next_value", SQLSTATE(HY013) MAL_MALLOC_FAIL);
+
+ lng rb = 0;
+
+ if (seqbulk_claim_next_values(be->mvc->session->tr->store, seq, card,
&rb)) {
+ int *ip = Tloc(b,0);
+ int *op = Tloc(r, 0);
+ int sn = (int)rb;
+ bool nil_val = false;
+ for (BUN i = 0; i < card; i++) {
+ if (ip[i] < 0) {
+ if (ip[i] == int_nil)
+ nil_val = true;
+ op[i] = ip[i];
+ } else {
+ op[i] = sn++;
+ }
+ }
+ be->last_id = sn;
+ sqlvar_set_number(find_global_var(be->mvc,
mvc_bind_schema(be->mvc, "sys"), "last_id"), be->last_id);
+ BATsetcount(r, card);
+ r->tnonil = !nil_val;
+ r->tnil = nil_val;
+ r->trevsorted = false;
+ if ((sn - rb) == 0)
+ r->tsorted = r->tkey = true;
+ else
+ r->tsorted = r->tkey = false;
+ *res = r->batCacheid;
+ BBPkeepref(r);
+ BBPreclaim(b);
+ return MAL_SUCCEED;
+ }
+ BBPreclaim(r);
+ BBPreclaim(b);
+ throw(SQL, "sql.next_value", SQLSTATE(HY050) "Cannot generate next
sequence value %s.%s", sname, seqname);
+}
+
/* str mvc_get_value(lng *res, str *sname, str *seqname); */
str
mvc_get_value(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
@@ -5911,7 +5966,7 @@ insert_json_array(char **msg, JSON *js,
*msg = "missing array start";
return -1;
}
- int id = 0, anr = 1;
+ int id = -1, anr = 1;
for (; elm < tail; elm=ja->next) { /* array begin, comma, end */
ja = js->elm+elm;
if (bat_offset > nr)
@@ -5941,10 +5996,12 @@ insert_json_array(char **msg, JSON *js,
}
if (bat_offset > nr)
return -10;
- if (elm > 0 && anr > 1 && BUNappend(bats[bat_offset++], &id, false) !=
GDK_SUCCEED)
+ if (id == -1)
+ bat_offset += composite_type_resultsize(t) - 1;
+ if (elm > 0 && BUNappend(bats[bat_offset++], &id, false) != GDK_SUCCEED)
elm = -2;
*BO = bat_offset;
- return elm-1;//+1;
+ return (tail == 0)?elm:elm-1;//+1;
}
static str
@@ -6112,9 +6169,9 @@ static mel_func sql_init_funcs[] = {
pattern("batsql", "renumber", mvc_renumber_bulk, false, "return the input b
renumbered using values from base", args(1,4,
batarg("res",int),batarg("input",int),batarg("mapping_oid",int),batarg("mapping_nid",int))),
pattern("sql", "renumber", mvc_renumber, false, "return the input b
renumbered using values from base", args(1,4,
arg("res",int),arg("input",int),arg("mapping_oid",int),arg("mapping_nid",int))),
pattern("sql", "next_value", mvc_next_value, true, "return the next value of
the sequence", args(1,3, arg("",lng),arg("sname",str),arg("sequence",str))),
- pattern("sql", "next_value_ms", mvc_next_value, false, "return the next value
of the sequence", args(1,4, arg("",lng),argany("card",1),
arg("sname",str),arg("sequence",str))),
+ pattern("sql", "next_value_ms", mvc_next_value_ms, false, "return the next
value of the sequence", args(1,4, arg("",int),argany("card",1),
arg("sname",str),arg("sequence",str))),
pattern("batsql", "next_value", mvc_next_value_bulk, true, "return the next
value of the sequence", args(1,4, batarg("",lng),arg("card",lng),
arg("sname",str),arg("sequence",str))),
- pattern("batsql", "next_value_ms", mvc_next_value_bulk, false, "return the
next value of the sequence", args(1,4, batarg("",lng),batargany("card",1),
arg("sname",str),arg("sequence",str))),
+ pattern("batsql", "next_value_ms", mvc_next_value_ms, false, "return the next
value of the sequence", args(1,4, batarg("",int),batargany("card",1),
arg("sname",str),arg("sequence",str))),
//pattern("batsql", "next_value_ms", mvc_next_value_bulk, false, "return the
next value of the sequence", args(1,5, batarg("",lng),batargany("card",1),
arg("sname",str),arg("sequence",str), batarg("cand",oid))),
pattern("sql", "get_value", mvc_get_value, false, "return the current value
of the sequence (ie the next to be used value)", args(1,3,
arg("",lng),arg("sname",str),arg("sequence",str))),
pattern("batsql", "get_value", mvc_get_value_bulk, false, "return the current
value of the sequence (ie the next to be used value)", args(1,3,
batarg("",lng),batarg("sname",str),batarg("sequence",str))),
diff --git a/sql/backends/monet5/sql_statement.c
b/sql/backends/monet5/sql_statement.c
--- a/sql/backends/monet5/sql_statement.c
+++ b/sql/backends/monet5/sql_statement.c
@@ -4114,7 +4114,7 @@ temporal_convert(backend *be, stmt *v, s
return NULL;
}
-static int
+int
composite_type_resultsize(sql_subtype *t)
{
int nr = 0;
diff --git a/sql/backends/monet5/sql_statement.h
b/sql/backends/monet5/sql_statement.h
--- a/sql/backends/monet5/sql_statement.h
+++ b/sql/backends/monet5/sql_statement.h
@@ -289,5 +289,6 @@ sql_export InstrPtr pushPtr(MalBlkPtr mb
typedef stmt *(*fstmt)(backend *be, stmt *op1, stmt *op2);
extern stmt *stmt_nest(backend *be, stmt *op1, stmt *op2, fstmt call);
+extern int composite_type_resultsize(sql_subtype *t);
#endif /* _SQL_STATEMENT_H_ */
diff --git a/sql/storage/store_sequence.c b/sql/storage/store_sequence.c
--- a/sql/storage/store_sequence.c
+++ b/sql/storage/store_sequence.c
@@ -161,8 +161,8 @@ seq_restart(sql_store Store, sql_sequenc
return 1;
}
-int
-seqbulk_next_value(sql_store Store, sql_sequence *seq, lng cnt, lng* dest)
+static int
+seqbulk_next_value_intern(sql_store Store, sql_sequence *seq, lng cnt, lng*
dest, bool dest_array)
{
store_sequence *s;
sqlstore *store = Store;
@@ -213,13 +213,14 @@ seqbulk_next_value(sql_store Store, sql_
return 0;
}
}
- for(lng i = 0; i < cnt; i++) {
- dest[i] = cur;
- if ((GDK_lng_max - inc < cur) || ((cur += inc) > max)) {
- // overflow
- cur = (seq->cycle)?min:lng_nil;
+ if (!dest_array)
+ for(lng i = 0; i < cnt; i++) {
+ dest[i] = cur;
+ if ((GDK_lng_max - inc < cur) || ((cur += inc)
> max)) {
+ // overflow
+ cur = (seq->cycle)?min:lng_nil;
+ }
}
- }
} else { // seq->increment < 0
lng inc = -seq->increment; // new value = old value - inc;
@@ -240,13 +241,14 @@ seqbulk_next_value(sql_store Store, sql_
return 0;
}
}
- for(lng i = 0; i < cnt; i++) {
- dest[i] = cur;
- if ((-GDK_lng_max + inc > cur) || ((cur -= inc) <
min)) {
- // underflow
- cur = (seq->cycle)?max:lng_nil;
+ if (!dest_array)
+ for(lng i = 0; i < cnt; i++) {
+ dest[i] = cur;
+ if ((-GDK_lng_max + inc > cur) || ((cur -= inc)
< min)) {
+ // underflow
+ cur = (seq->cycle)?max:lng_nil;
+ }
}
- }
}
if (!store_unlocked) {
@@ -264,6 +266,18 @@ seqbulk_next_value(sql_store Store, sql_
}
int
+seqbulk_claim_next_values(sql_store Store, sql_sequence *seq, lng cnt, lng*
dest)
+{
+ return seqbulk_next_value_intern(Store, seq, cnt, dest, true);
+}
+
+int
+seqbulk_next_value(sql_store Store, sql_sequence *seq, lng cnt, lng* dest)
+{
+ return seqbulk_next_value_intern(Store, seq, cnt, dest, false);
+}
+
+int
seq_next_value(sql_store store, sql_sequence *seq, lng *val)
{
return seqbulk_next_value(store, seq, 1, val);
diff --git a/sql/storage/store_sequence.h b/sql/storage/store_sequence.h
--- a/sql/storage/store_sequence.h
+++ b/sql/storage/store_sequence.h
@@ -26,6 +26,7 @@ extern int seq_restart(sql_store store,
extern void log_store_sequence(sql_store store, void *seq); /* called locked */
extern int seqbulk_next_value(sql_store store, sql_sequence *seq, lng cnt,
lng* dest);
+extern int seqbulk_claim_next_values(sql_store store, sql_sequence *seq, lng
cnt, lng* dest);
extern void sequences_lock(sql_store store);
extern void sequences_unlock(sql_store store);
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]