Changeset: 71f4c04e46bb for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/71f4c04e46bb
Modified Files:
        common/stream/fwf.c
        sql/backends/monet5/sql.c
        sql/backends/monet5/sql_result.c
Branch: Jul2021
Log Message:

Cleanup existing stream on error and cleaned error propagation for table import


diffs (186 lines):

diff --git a/common/stream/fwf.c b/common/stream/fwf.c
--- a/common/stream/fwf.c
+++ b/common/stream/fwf.c
@@ -149,21 +149,18 @@ stream_fwf_create(stream *restrict s, si
        }
        fsd->in_buf = malloc(fsd->line_len);
        if (fsd->in_buf == NULL) {
-               close_stream(fsd->s);
                free(fsd);
                mnstr_set_open_error(STREAM_FWF_NAME, errno, NULL);
                return NULL;
        }
        fsd->out_buf = malloc(fsd->line_len * 3);
        if (fsd->out_buf == NULL) {
-               close_stream(fsd->s);
                free(fsd->in_buf);
                free(fsd);
                mnstr_set_open_error(STREAM_FWF_NAME, errno, NULL);
                return NULL;
        }
        if ((ns = create_stream(STREAM_FWF_NAME)) == NULL) {
-               close_stream(fsd->s);
                free(fsd->in_buf);
                free(fsd->out_buf);
                free(fsd);
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
@@ -3086,9 +3086,8 @@ mvc_import_table_wrap(Client cntxt, MalB
        (void) mb;              /* NOT USED */
        if ((msg = checkSQLContext(cntxt)) != NULL)
                return msg;
-       if (onclient && !cntxt->filetrans) {
-               throw(MAL, "sql.copy_from", "cannot transfer files from 
client");
-       }
+       if (onclient && !cntxt->filetrans)
+               throw(MAL, "sql.copy_from", SQLSTATE(42000) "Cannot transfer 
files from client");
 
        be = cntxt->sqlcontext;
        /* The CSV parser expects ssep to have the value 0 if the user does not
@@ -3145,6 +3144,8 @@ mvc_import_table_wrap(Client cntxt, MalB
                        size_t *widths;
                        char* val_start = fixed_widths;
                        size_t width_len = strlen(fixed_widths);
+                       stream *ns;
+
                        for (i = 0; i < width_len; i++) {
                                if (fixed_widths[i] == '|') {
                                        ncol++;
@@ -3166,30 +3167,37 @@ mvc_import_table_wrap(Client cntxt, MalB
                        tsep = fwftsep;
                        rsep = fwfrsep;
 
-                       ss = stream_fwf_create(ss, ncol, widths, 
STREAM_FWF_FILLER);
+                       ns = stream_fwf_create(ss, ncol, widths, 
STREAM_FWF_FILLER);
+                       if (ns == NULL || mnstr_errnr(ns)) {
+                               msg = createException(IO, "sql.copy_from", 
SQLSTATE(42000) "%s", mnstr_peek_error(NULL));
+                               close_stream(ss);
+                               free(widths);
+                               return msg;
+                       }
+                       ss = ns;
                }
 #if SIZEOF_VOID_P == 4
                s = bstream_create(ss, 0x20000);
 #else
                s = bstream_create(ss, 0x200000);
 #endif
-               if (s != NULL) {
-                       msg = mvc_import_table(cntxt, &b, be->mvc, s, t, tsep, 
rsep, ssep, ns, sz, offset, besteffort, false, escape);
-                       if (onclient) {
-                               mnstr_write(be->mvc->scanner.ws, PROMPT3, 
sizeof(PROMPT3)-1, 1);
-                               mnstr_flush(be->mvc->scanner.ws, 
MNSTR_FLUSH_DATA);
-                               be->mvc->scanner.rs->eof = s->eof;
-                               s->s = NULL;
-                       }
-                       bstream_destroy(s);
+               if (s == NULL) {
+                       close_stream(ss);
+                       throw(MAL, "sql.copy_from", SQLSTATE(HY013) 
MAL_MALLOC_FAIL);
                }
-       }
-       if (fname && s == NULL)
-               throw(IO, "bstreams.create", SQLSTATE(42000) "Failed to create 
block stream");
-       if (b == NULL)
-               throw(SQL, "importTable", SQLSTATE(42000) "Failed to import 
table '%s', %s", t->base.name, be->mvc->errstr);
-       bat2return(stk, pci, b);
-       GDKfree(b);
+               msg = mvc_import_table(cntxt, &b, be->mvc, s, t, tsep, rsep, 
ssep, ns, sz, offset, besteffort, false, escape);
+               if (onclient) {
+                       mnstr_write(be->mvc->scanner.ws, PROMPT3, 
sizeof(PROMPT3)-1, 1);
+                       mnstr_flush(be->mvc->scanner.ws, MNSTR_FLUSH_DATA);
+                       be->mvc->scanner.rs->eof = s->eof;
+                       s->s = NULL;
+               }
+               bstream_destroy(s);
+       }
+       if (b) {
+               bat2return(stk, pci, b);
+               GDKfree(b);
+       }
        return msg;
 }
 
diff --git a/sql/backends/monet5/sql_result.c b/sql/backends/monet5/sql_result.c
--- a/sql/backends/monet5/sql_result.c
+++ b/sql/backends/monet5/sql_result.c
@@ -699,21 +699,17 @@ mvc_import_table(Client cntxt, BAT ***ba
 
        *bats =0;       // initialize the receiver
 
-       if (!bs) {
-               sql_error(m, 500, "no stream (pointer) provided");
-               return NULL;
-       }
+       if (!bs)
+               throw(IO, "sql.copy_from", SQLSTATE(42000) "No stream (pointer) 
provided");
        if (mnstr_errnr(bs->s)) {
                mnstr_error_kind errnr = mnstr_errnr(bs->s);
-               char *msg = mnstr_error(bs->s);
-               sql_error(m, 500, "stream not open %s: %s", 
mnstr_error_kind_name(errnr), msg ? msg : "unknown error");
-               free(msg);
-               return NULL;
+               char *stream_msg = mnstr_error(bs->s);
+               msg = createException(IO, "sql.copy_from", SQLSTATE(42000) 
"Stream not open %s: %s", mnstr_error_kind_name(errnr), stream_msg ? stream_msg 
: "unknown error");
+               free(stream_msg);
+               return msg;
        }
-       if (offset < 0 || offset > (lng) BUN_MAX) {
-               sql_error(m, 500, "offset out of range");
-               return NULL;
-       }
+       if (offset < 0 || offset > (lng) BUN_MAX)
+               throw(IO, "sql.copy_from", SQLSTATE(42000) "Offset out of 
range");
 
        if (offset > 0)
                offset--;
@@ -730,10 +726,8 @@ mvc_import_table(Client cntxt, BAT ***ba
                        .filename = m->scanner.rs == bs ? NULL : "",
                };
                fmt = GDKzalloc(sizeof(Column) * (as.nr_attrs + 1));
-               if (fmt == NULL) {
-                       sql_error(m, 500, SQLSTATE(HY013) MAL_MALLOC_FAIL);
-                       return NULL;
-               }
+               if (fmt == NULL)
+                       throw(IO, "sql.copy_from", SQLSTATE(HY013) 
MAL_MALLOC_FAIL);
                as.format = fmt;
                if (!isa_block_stream(bs->s))
                        out = NULL;
@@ -758,8 +752,7 @@ mvc_import_table(Client cntxt, BAT ***ba
                                        BBPunfix(fmt[j].c->batCacheid);
                                }
                                GDKfree(fmt[i].data);
-                               sql_error(m, 500, SQLSTATE(HY013) "failed to 
allocate space for column");
-                               return NULL;
+                               throw(IO, "sql.copy_from", SQLSTATE(HY013) 
MAL_MALLOC_FAIL);
                        }
                        fmt[i].c = NULL;
                        fmt[i].ws = !has_whitespace(fmt[i].sep);
@@ -782,15 +775,14 @@ mvc_import_table(Client cntxt, BAT ***ba
                                (best || !as.error))) {
                                *bats = (BAT**) GDKzalloc(sizeof(BAT *) * 
as.nr_attrs);
                                if ( *bats == NULL){
-                                       sql_error(m, 500, SQLSTATE(HY013) 
"failed to allocate space for column");
                                        TABLETdestroy_format(&as);
-                                       return NULL;
+                                       throw(IO, "sql.copy_from", 
SQLSTATE(HY013) MAL_MALLOC_FAIL);
                                }
                                msg = TABLETcollect(*bats,&as);
                        }
                }
                if (as.error) {
-                       if( !best) sql_error(m, 500, "%s", 
getExceptionMessage(as.error));
+                       if( !best) msg = createException(SQL, "sql.copy_from", 
SQLSTATE(42000) "Failed to import table '%s', %s", t->base.name, 
getExceptionMessage(as.error));
                        freeException(as.error);
                        as.error = NULL;
                }
@@ -1377,7 +1369,6 @@ mvc_export_table(backend *b, stream *s, 
        if(fmt == NULL || tres == NULL) {
                GDKfree(fmt);
                GDKfree(tres);
-               sql_error(m, 500, SQLSTATE(HY013) MAL_MALLOC_FAIL);
                return -1;
        }
 
_______________________________________________
checkin-list mailing list
checkin-list@monetdb.org
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to