Changeset: 637252a7de5e for MonetDB URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=637252a7de5e Modified Files: ctest/tools/monetdbe/example_append.c tools/monetdbe/monetdbe.c Branch: default Log Message:
monetdbe_append fixes - updated test diffs (275 lines): diff --git a/ctest/tools/monetdbe/example_append.c b/ctest/tools/monetdbe/example_append.c --- a/ctest/tools/monetdbe/example_append.c +++ b/ctest/tools/monetdbe/example_append.c @@ -13,6 +13,11 @@ #define error(msg) {fprintf(stderr, "Failure: %s\n", msg); return -1;} +#define date_eq(d1, d2) (d1.year == d2.year && d1.month == d2.month && d1.day == d2.day) +#define time_eq(t1, t2) (t1.hours == t2.hours && t1.minutes == t2.minutes && t1.seconds == t2.seconds && t1.ms == t2.ms) + +static char hexit[] = "0123456789ABCDEF"; + int main(void) { @@ -23,15 +28,18 @@ main(void) // second argument is a string for the db directory or NULL for in-memory mode if (monetdbe_open(&mdbe, NULL, NULL)) error("Failed to open database") - if ((err = monetdbe_query(mdbe, "CREATE TABLE test (x integer, y string)", NULL, NULL)) != NULL) + if ((err = monetdbe_query(mdbe, "CREATE TABLE test (x integer, y string, ts timestamp, dt date, t time, b blob)", NULL, NULL)) != NULL) error(err) - if ((err = monetdbe_query(mdbe, "INSERT INTO test VALUES (42, 'Hello'), (NULL, 'World')", NULL, NULL)) != NULL) + if ((err = monetdbe_query(mdbe, "INSERT INTO test VALUES (42, 'Hello', '2020-01-02 10:20:30', '2020-01-02', '10:20:30', '01020308'), \ + (NULL, 'World', NULL, NULL, NULL, NULL), \ + (NULL, 'Foo', NULL, NULL, NULL, NULL), \ + (43, 'Bar', '2021-02-03 11:21:31', '2021-02-03', '11:21:31', '01020306')", NULL, NULL)) != NULL) error(err) - if ((err = monetdbe_query(mdbe, "SELECT x, y FROM test; ", &result, NULL)) != NULL) + if ((err = monetdbe_query(mdbe, "SELECT * FROM test; ", &result, NULL)) != NULL) error(err) fprintf(stdout, "Query result with %zu cols and %"PRId64" rows\n", result->ncols, result->nrows); - monetdbe_column* rcol[2]; + monetdbe_column* rcol[result->ncols]; for (int64_t r = 0; r < result->nrows; r++) { for (size_t c = 0; c < result->ncols; c++) { if ((err = monetdbe_result_fetch(result, rcol+c, c)) != NULL) @@ -55,6 +63,48 @@ main(void) } break; } + case monetdbe_date: { + monetdbe_column_date * col = (monetdbe_column_date *) rcol[c]; + if (date_eq(col->data[r], col->null_value)) { + printf("NULL"); + } else { + printf("%d-%d-%d", col->data[r].year, col->data[r].month, col->data[r].day); + } + break; + } + case monetdbe_time: { + monetdbe_column_time * col = (monetdbe_column_time *) rcol[c]; + if (time_eq(col->data[r], col->null_value)) { + printf("NULL"); + } else { + printf("%d:%d:%d.%d", col->data[r].hours, col->data[r].minutes, col->data[r].seconds, col->data[r].ms); + } + break; + } + case monetdbe_timestamp: { + monetdbe_column_timestamp * col = (monetdbe_column_timestamp *) rcol[c]; + if (date_eq(col->data[r].date, col->null_value.date) && time_eq(col->data[r].time, col->null_value.time)) { + printf("NULL"); + } else { + printf("%d-%d-%d ", col->data[r].date.year, col->data[r].date.month, col->data[r].date.day); + printf("%d:%d:%d.%d", col->data[r].time.hours, col->data[r].time.minutes, col->data[r].time.seconds, col->data[r].time.ms); + } + break; + } + case monetdbe_blob: { + monetdbe_column_blob * col = (monetdbe_column_blob *) rcol[c]; + if (!col->data[r].data) { + printf("NULL"); + } else { + for (size_t i = 0; i < col->data[r].size; i++) { + int hval = (col->data[r].data[i] >> 4) & 15; + int lval = col->data[r].data[i] & 15; + + printf("%c%c", hexit[hval], hexit[lval]); + } + } + break; + } default: { printf("UNKNOWN"); } @@ -66,13 +116,13 @@ main(void) } printf("\n"); } - if ((err = monetdbe_append(mdbe, "sys", "test", rcol, 2)) != NULL) + if ((err = monetdbe_append(mdbe, "sys", "test", rcol, result->ncols)) != NULL) error(err) /* we can now cleanup the previous query */ if ((err = monetdbe_cleanup_result(mdbe, result)) != NULL) error(err) - if ((err = monetdbe_query(mdbe, "SELECT x, y FROM test; ", &result, NULL)) != NULL) + if ((err = monetdbe_query(mdbe, "SELECT * FROM test; ", &result, NULL)) != NULL) error(err) fprintf(stdout, "Query result after append with %zu cols and %"PRId64" rows\n", result->ncols, result->nrows); for (int64_t r = 0; r < result->nrows; r++) { @@ -98,6 +148,48 @@ main(void) } break; } + case monetdbe_date: { + monetdbe_column_date * col = (monetdbe_column_date *) rcol[c]; + if (date_eq(col->data[r], col->null_value)) { + printf("NULL"); + } else { + printf("%d-%d-%d", col->data[r].year, col->data[r].month, col->data[r].day); + } + break; + } + case monetdbe_time: { + monetdbe_column_time * col = (monetdbe_column_time *) rcol[c]; + if (time_eq(col->data[r], col->null_value)) { + printf("NULL"); + } else { + printf("%d:%d:%d.%d", col->data[r].hours, col->data[r].minutes, col->data[r].seconds, col->data[r].ms); + } + break; + } + case monetdbe_timestamp: { + monetdbe_column_timestamp * col = (monetdbe_column_timestamp *) rcol[c]; + if (date_eq(col->data[r].date, col->null_value.date) && time_eq(col->data[r].time, col->null_value.time)) { + printf("NULL"); + } else { + printf("%d-%d-%d ", col->data[r].date.year, col->data[r].date.month, col->data[r].date.day); + printf("%d:%d:%d.%d", col->data[r].time.hours, col->data[r].time.minutes, col->data[r].time.seconds, col->data[r].time.ms); + } + break; + } + case monetdbe_blob: { + monetdbe_column_blob * col = (monetdbe_column_blob *) rcol[c]; + if (!col->data[r].data) { + printf("NULL"); + } else { + for (size_t i = 0; i < col->data[r].size; i++) { + int hval = (col->data[r].data[i] >> 4) & 15; + int lval = col->data[r].data[i] & 15; + + printf("%c%c", hexit[hval], hexit[lval]); + } + } + break; + } default: { printf("UNKNOWN"); } diff --git a/tools/monetdbe/monetdbe.c b/tools/monetdbe/monetdbe.c --- a/tools/monetdbe/monetdbe.c +++ b/tools/monetdbe/monetdbe.c @@ -1103,6 +1103,7 @@ monetdbe_append(monetdbe_database dbhdl, for (i = 0, n = t->columns.set->h; i < column_count && n; i++, n = n->next) { sql_column *c = n->data; int mtype = monetdbe_type(input[i]->type); + const void* nil = ATOMnilptr(mtype); char *v = input[i]->data; int w = 1; @@ -1111,7 +1112,7 @@ monetdbe_append(monetdbe_database dbhdl, goto cleanup; } if ((mtype >= TYPE_bit && mtype <= TYPE_lng)) { - w = BATatoms[mtype].size; + w = ATOMsize(mtype); for (size_t j=0; j<cnt; j++, v+=w){ if (store_funcs.append_col(m->session->tr, c, v, mtype) != 0) { mdbe->msg = createException(SQL, "monetdbe.monetdbe_append", "Cannot append values"); @@ -1124,7 +1125,8 @@ monetdbe_append(monetdbe_database dbhdl, for (size_t j=0; j<cnt; j++){ char *s = d[j]; if (!s) - s = (char*)str_nil; + s = (char*) nil; + if (store_funcs.append_col(m->session->tr, c, s, mtype) != 0) { mdbe->msg = createException(SQL, "monetdbe.monetdbe_append", "Cannot append values"); goto cleanup; @@ -1132,45 +1134,62 @@ monetdbe_append(monetdbe_database dbhdl, } } else if (mtype == TYPE_timestamp) { monetdbe_data_timestamp* ts = (monetdbe_data_timestamp*)v; - timestamp t = timestamp_from_data(ts); - if (store_funcs.append_col(m->session->tr, c, &t, mtype) != 0) { - mdbe->msg = createException(SQL, "monetdbe.monetdbe_append", "Cannot append values"); - goto cleanup; - } - } else if (mtype == TYPE_daytime) { - monetdbe_data_time* t = (monetdbe_data_time*)v; - daytime dt = time_from_data(t); - - if (store_funcs.append_col(m->session->tr, c, &dt, mtype) != 0) { - mdbe->msg = createException(SQL, "monetdbe.monetdbe_append", "Cannot append values"); - goto cleanup; + for (size_t j=0; j<cnt; j++){ + timestamp t = *(timestamp*) nil; + if(!timestamp_is_null(ts[j])) + t = timestamp_from_data(&ts[j]); + + if (store_funcs.append_col(m->session->tr, c, &t, mtype) != 0) { + mdbe->msg = createException(SQL, "monetdbe.monetdbe_append", "Cannot append values"); + goto cleanup; + } } } else if (mtype == TYPE_date) { monetdbe_data_date* de = (monetdbe_data_date*)v; - date d = date_from_data(de); + + for (size_t j=0; j<cnt; j++){ + date d = *(date*) nil; + if(!date_is_null(de[j])) + d = date_from_data(&de[j]); - if (store_funcs.append_col(m->session->tr, c, &d, mtype) != 0) { - mdbe->msg = createException(SQL, "monetdbe.monetdbe_append", "Cannot append values"); - goto cleanup; + if (store_funcs.append_col(m->session->tr, c, &d, mtype) != 0) { + mdbe->msg = createException(SQL, "monetdbe.monetdbe_append", "Cannot append values"); + goto cleanup; + } + } + } else if (mtype == TYPE_daytime) { + monetdbe_data_time* t = (monetdbe_data_time*)v; + + for (size_t j=0; j<cnt; j++){ + daytime dt = *(daytime*) nil; + if(!time_is_null(t[j])) + dt = time_from_data(&t[j]); + + if (store_funcs.append_col(m->session->tr, c, &dt, mtype) != 0) { + mdbe->msg = createException(SQL, "monetdbe.monetdbe_append", "Cannot append values"); + goto cleanup; + } } } else if (mtype == TYPE_blob) { monetdbe_data_blob* be = (monetdbe_data_blob*)v; - blob *b = NULL; - - if (!blob_is_null(*be)) { - size_t len = be->size; - b = (blob *) GDKmalloc(blobsize(len)); - if (b == NULL) - mdbe->msg = createException(MAL, "monetdbe.monetdbe_append", MAL_MALLOC_FAIL); + + for (size_t j=0; j<cnt; j++){ + blob* b = (blob*) nil; + if (!blob_is_null(be[j])) { + size_t len = be[j].size; + b = (blob*) GDKmalloc(blobsize(len)); + if (b == NULL) + mdbe->msg = createException(MAL, "monetdbe.monetdbe_append", MAL_MALLOC_FAIL); + + b->nitems = len; + memcpy(b->data, be[j].data, len); + } - b->nitems = len; - memcpy(b->data, be->data, len); - } - - if (store_funcs.append_col(m->session->tr, c, b, mtype) != 0) { - mdbe->msg = createException(SQL, "monetdbe.monetdbe_append", "Cannot append values"); - goto cleanup; + if (store_funcs.append_col(m->session->tr, c, b, mtype) != 0) { + mdbe->msg = createException(SQL, "monetdbe.monetdbe_append", "Cannot append values"); + goto cleanup; + } } } } _______________________________________________ checkin-list mailing list checkin-list@monetdb.org https://www.monetdb.org/mailman/listinfo/checkin-list