Changeset: 1abb034531f1 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/1abb034531f1
Added Files:
        sql/test/bincopy/Tests/bincopy_filecount_mismatch.py
Modified Files:
        sql/server/rel_updates.c
        sql/test/bincopy/Tests/All
Branch: Mar2025
Log Message:

Check number of files given to COPY ... INTO BINARY

Fixes #7660


diffs (101 lines):

diff --git a/sql/server/rel_updates.c b/sql/server/rel_updates.c
--- a/sql/server/rel_updates.c
+++ b/sql/server/rel_updates.c
@@ -1884,7 +1884,7 @@ bincopyfrom(sql_query *query, dlist *qna
        int file_count = dlist_length(files);
        if (column_count != file_count) {
                return sql_error(sql, 02, SQLSTATE(42000) "COPY BINARY INTO: "
-                       "number of files does not match number of columns: "
+                       "number of files _umber of columns: "
                        "%d files, %d columns",
                        file_count, column_count);
        }
@@ -2047,6 +2047,13 @@ bincopyto(sql_query *query, symbol *qry,
           but we don't care about that here. */
        sub = rel_project(sql->sa, sub, rel_projections(sql, sub, NULL, 1, 0));
 
+       int nrcolumns = sub->nrcols;
+       int nrfilenames = filenames->cnt;
+       if (nrcolumns != nrfilenames) {
+               return sql_error(sql, 02, "COPY INTO BINARY: need %d file 
names, got %d",
+                       nrcolumns, nrfilenames);
+       }
+
        sql_rel *rel = rel_create(sql->sa);
        list *exps = new_exp_list(sql->sa);
        if (!rel || !exps)
diff --git a/sql/test/bincopy/Tests/All b/sql/test/bincopy/Tests/All
--- a/sql/test/bincopy/Tests/All
+++ b/sql/test/bincopy/Tests/All
@@ -1,5 +1,8 @@
 bincopy_test_the_testdata
 
+bincopy_filecount_mismatch
+
+
 bincopy_ints_on_client
 bincopy_ints_on_server
 bincopy_more_ints_on_client
diff --git a/sql/test/bincopy/Tests/bincopy_filecount_mismatch.py 
b/sql/test/bincopy/Tests/bincopy_filecount_mismatch.py
new file mode 100644
--- /dev/null
+++ b/sql/test/bincopy/Tests/bincopy_filecount_mismatch.py
@@ -0,0 +1,58 @@
+#!/usr/bin/env python3
+
+import os
+import struct
+from MonetDBtesting import tpymonetdb as pymonetdb
+
+
+class MyDownloader(pymonetdb.Downloader):
+    def __init__(self):
+        self.data = {}
+
+    def handle_download(self, download, filename, text_mode):
+        assert text_mode == False
+        br = download.binary_reader()
+        self.data[filename] = br.read(1_000_000)
+
+
+NROWS = 10
+
+conn = pymonetdb.connect(
+    database=os.getenv("TSTDB", 'demo'),
+    port=int(os.getenv("MAPIPORT", '44001')))
+downloader = MyDownloader()
+conn.set_downloader(downloader)
+conn.set_autocommit(True)
+
+c = conn.cursor()
+c.execute("DROP TABLE IF EXISTS foo")
+c.execute("CREATE TABLE foo(i TINYINT, j TINYINT)")
+c.execute("INSERT INTO foo SELECT value AS i, 10 * value AS j FROM 
sys.generate_series(%s, %s)", [1, NROWS + 1])
+
+# This should work
+downloader.data.clear()
+c.execute("COPY SELECT i, j FROM foo INTO BINARY 'i', 'j' ON CLIENT")
+expected_i = bytes([1,2,3,4,5,6,7,8,9,10])
+actual_i = downloader.data['i']
+assert actual_i == expected_i, "expected {expected_i!r}, found {value_i!r}"
+expected_j = bytes([10,20,30,40,50,60,70,80,90,100])
+actual_j = downloader.data['j']
+assert actual_j == expected_j, "expected {expected_j!r}, found {value_j!r}"
+
+# Too many file names
+try:
+    c.execute("COPY SELECT i, j FROM foo INTO BINARY 'i', 'j', 'x' ON CLIENT")
+    assert False, "should have complained about the extra file name"
+except pymonetdb.Error as e:
+    if 'need 2 file names, got 3' not in str(e):
+        raise
+
+# Too few file names
+try:
+    c.execute("COPY SELECT i, j FROM foo INTO BINARY 'i' ON CLIENT")
+    assert False, "should have complained about the missing file name"
+except pymonetdb.Error as e:
+    if 'need 2 file names, got 1' not in str(e):
+        raise
+
+conn.execute("DROP TABLE foo")
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to