Repository: mathesar-foundation/mathesar

Issue:
Support long column names in CSV imports
Breakaway task from #2634. Supporting arbitrarily long column names in CSV imports is significantly more urgent for UX than supporting them during column creations and renamings (since a user is very unlikely to submit a 63+ byte long string via the UI, as opposed to via CSV).


Discussion:

Resolving patch:
diff --git a/mathesar/imports/csv.py b/mathesar/imports/csv.py
--- a/mathesar/imports/csv.py
+++ b/mathesar/imports/csv.py
@@ -1,4 +1,5 @@
 from io import TextIOWrapper
+import hashlib
 
 import clevercsv as csv
 
@@ -118,15 +119,7 @@ def create_db_table_from_data_file(data_file, name, schema, comment=None):
     encoding = get_file_encoding(data_file.file)
     with open(sv_filename, 'rb') as sv_file:
         sv_reader = get_sv_reader(sv_file, header, dialect=dialect)
-        column_names = [column_name.strip() for column_name in sv_reader.fieldnames]
-        column_names = [
-            f"{COLUMN_NAME_TEMPLATE}{i}" if name == '' else name
-            for i, name in enumerate(column_names)
-        ]
-        column_names_alt = [
-            fieldname if fieldname != ID else ID_ORIGINAL
-            for fieldname in column_names
-        ]
+        column_names = _process_column_names(sv_reader.fieldnames)
         table = create_string_column_table(
             name=name,
             schema=schema.name,
@@ -149,6 +142,10 @@ def create_db_table_from_data_file(data_file, name, schema, comment=None):
         update_pk_sequence_to_latest(engine, table)
     except (IntegrityError, DataError):
         drop_table(name=name, schema=schema.name, engine=engine)
+        column_names_alt = [
+            column_name if column_name != ID else ID_ORIGINAL
+            for column_name in column_names
+        ]
         table = create_string_column_table(
             name=name,
             schema=schema.name,
@@ -171,6 +168,88 @@ def create_db_table_from_data_file(data_file, name, schema, comment=None):
     return table
 
 
+def _process_column_names(column_names):
+    column_names = (
+        column_name.strip()
+        for column_name
+        in column_names
+    )
+    column_names = (
+        _truncate_if_necessary(column_name)
+        for column_name
+        in column_names
+    )
+    column_names = (
+        f"{COLUMN_NAME_TEMPLATE}{i}" if name == '' else name
+        for i, name
+        in enumerate(column_names)
+    )
+    return list(column_names)
+
+
+def _truncate_if_necessary(identifier):
+    """
+    Takes an identifier and returns it, truncating it, if it is too long. The truncated version
+    will end with a hash of the passed identifier, therefore column name collision should be very
+    rare.
+
+    Iteratively removes characters from the end of the identifier, until the resulting string, with
+    the suffix hash of the identifier appended, is short enough that it doesn't need to be truncated
+    anymore. Whitespace is trimmed from the truncated identifier before appending the suffix.
+    """
+    assert type(identifier) is str
+    if not _is_truncation_necessary(identifier):
+        return identifier
+    right_side = "-" + _get_truncation_hash(identifier)
+    identifier_length = len(identifier)
+    assert len(right_side) < identifier_length  # Sanity check
+    range_of_num_of_chars_to_remove = range(1, identifier_length)
+    for num_of_chars_to_remove in range_of_num_of_chars_to_remove:
+        left_side = identifier[:num_of_chars_to_remove * -1]
+        left_side = left_side.rstrip()
+        truncated_identifier = left_side + right_side
+        if not _is_truncation_necessary(truncated_identifier):
+            return truncated_identifier
+    raise Exception(
+        "Acceptable truncation not found; should never happen."
+    )
+
+
+def _is_truncation_necessary(identifier):
+    postgres_identifier_size_limit = 63
+    size = _get_size_of_identifier_in_bytes(identifier)
+    return size > postgres_identifier_size_limit
+
+
+def _get_truncation_hash(identifier):
+    """
+    Produces an 8-character string hash of the passed identifier.
+
+    Using hash function blake2s, because it seems fairly recommended and it seems to be better
+    suited for shorter digests than blake2b. We want short digests to not take up too much of the
+    truncated identifier in whose construction this will be used.
+    """
+    h = hashlib.blake2s(digest_size=4)
+    bytes = _get_identifier_in_bytes(identifier)
+    h.update(bytes)
+    return h.hexdigest()
+
+
+def _get_size_of_identifier_in_bytes(s):
+    bytes = _get_identifier_in_bytes(s)
+    return len(bytes)
+
+
+def _get_identifier_in_bytes(s):
+    """
+    Afaict, following Postgres doc [0] says that UTF-8 supports all languages; therefore, different
+    server locale configurations should not break this.
+
+    [0] https://www.postgresql.org/docs/13/multibyte.html
+    """
+    return s.encode('utf-8')
+
+
 def create_table_from_csv(data_file, name, schema, comment=None):
     engine = create_mathesar_engine(schema.database.name)
     db_table = create_db_table_from_data_file(


Test patch:
diff --git a/mathesar/tests/data/long_column_names.csv b/mathesar/tests/data/long_column_names.csv
new file mode 100644
--- /dev/null
+++ b/mathesar/tests/data/long_column_names.csv
@@ -0,0 +1,55 @@
+"State or Nation","Cycle 1 Total Number of Health Deficiencies","Cycle 1 Total Number of Fire Safety Deficiencies","Cycle 2 Total Number of Health Deficiencies","Cycle 2 Total Number of Fire Safety Deficiencies","Cycle 3 Total Number of Health Deficiencies","Cycle 3 Total Number of Fire Safety Deficiencies","Average Number of Residents per Day","Reported Nurse Aide Staffing Hours per Resident per Day","Reported LPN Staffing Hours per Resident per Day","Reported RN Staffing Hours per Resident per Day","Reported Licensed Staffing Hours per Resident per Day","Reported Total Nurse Staffing Hours per Resident per Day","Total number of nurse staff hours per resident per day on the weekend","Registered Nurse hours per resident per day on the weekend","Reported Physical Therapist Staffing Hours per Resident Per Day","Total nursing staff turnover","Registered Nurse turnover","Number of administrators who have left the nursing home","Case-Mix RN Staffing Hours per Resident per Day","Case-Mix Total Nurse Staffing Hours per Resident per Day","Number of Fines","Fine Amount in Dollars","Percentage of long stay residents whose need for help with daily activities has increased","Percentage of long stay residents who lose too much weight","Percentage of low risk long stay residents who lose control of their bowels or bladder","Percentage of long stay residents with a catheter inserted and left in their bladder","Percentage of long stay residents with a urinary tract infection","Percentage of long stay residents who have depressive symptoms","Percentage of long stay residents who were physically restrained","Percentage of long stay residents experiencing one or more falls with major injury","Percentage of long stay residents assessed and appropriately given the pneumococcal vaccine","Percentage of long stay residents who received an antipsychotic medication","Percentage of short stay residents assessed and appropriately given the pneumococcal vaccine","Percentage of short stay residents who newly received an antipsychotic medication","Percentage of long stay residents whose ability to move independently worsened","Percentage of long stay residents who received an antianxiety or hypnotic medication","Percentage of high risk long stay residents with pressure ulcers","Percentage of long stay residents assessed and appropriately given the seasonal influenza vaccine","Percentage of short stay residents who made improvements in function","Percentage of short stay residents who were assessed and appropriately given the seasonal influenza vaccine","Percentage of short stay residents who were rehospitalized after a nursing home admission","Percentage of short stay residents who had an outpatient emergency department visit","Number of hospitalizations per 1000 long-stay resident days","Number of outpatient emergency department visits per 1000 long-stay resident days","Processing Date"
+"NATION","8.6","4.5","8.5","4.3","8.3","4.6","78.6","2.22","0.88","0.66","1.53","3.75","3.26","0.45","0.07","53.9","52.3","0.8","0.38487","3.15796","2.3","33247","14.842144","6.172333","47.158545","1.698662","2.345577","7.882694","0.145406","3.395302","92.085375","14.447634","78.873848","1.738571","16.161024","19.436701","8.145643","94.937079","74.115131","75.601680","22.073834","11.791045","1.585233","1.016932","2023-02-01"
+"AK","8.9","7.3","8.0","6.6","13.1","5.5","35.8","4.26","0.59","2.01","2.60","6.86","5.88","1.44","0.09","54.7","54.0","0.3","0.32843","3.07815","2.8","40982","11.399472","5.955443","42.734961","3.729863","3.092132","7.233612","0.214286","3.780119","94.381998","15.207048","74.994950","0.897709","16.474449","14.673754","9.103530","97.261368","77.508477","81.395239","14.130346","14.433861","1.002547","0.897472","2023-02-01"
+"AL","2.9","2.9","3.1","3.3","3.9","3.4","91.3","2.34","0.87","0.58","1.45","3.79","3.15","0.32","0.03","52.2","46.9","0.7","0.30675","2.91289","1.0","9544","12.824604","6.869793","42.223463","2.067217","3.126881","1.119971","0.627098","3.500289","92.692985","19.916009","81.288737","2.412304","14.032358","23.573902","9.394910","93.702772","72.519689","75.399412","21.907646","11.153483","1.641877","0.983719","2023-02-01"
+"AR","8.5","2.1","9.2","1.8","11.1","2.2","71.2","2.50","1.02","0.37","1.39","3.89","3.29","0.26","0.03","56.5","54.9","0.5","0.30829","2.91594","1.9","23087","12.018400","5.631477","40.901992","1.622935","2.248033","2.173623","0.229492","3.957339","94.849162","11.978126","78.050791","1.685996","11.258485","21.562726","8.438663","96.916093","77.938874","73.836436","23.566078","13.385911","1.830761","1.296204","2023-02-01"
+"AZ","7.6","3.3","7.0","3.2","8.0","3.3","76.0","2.28","1.08","0.75","1.82","4.10","3.55","0.52","0.11","55.0","55.0","0.5","0.39392","3.15730","2.0","13117","12.840441","6.067455","56.507921","1.539109","1.507067","3.595873","0.310559","2.456865","95.641211","10.356776","86.951457","1.056651","18.691552","17.508812","9.226697","94.685482","74.158771","83.771004","23.490408","11.388180","1.275856","0.956408","2023-02-01"
+"CA","15.1","7.1","14.1","6.8","13.1","6.8","80.9","2.54","1.20","0.58","1.79","4.33","3.88","0.44","0.08","47.8","51.5","0.8","0.40762","3.15304","2.2","19392","8.128644","5.245266","31.959431","1.618842","1.273445","5.524313","0.231156","1.768723","97.972113","10.004216","93.461148","1.289304","11.198966","13.731006","7.546983","98.394685","79.463056","92.036398","21.788146","10.829473","1.722919","0.832184","2023-02-01"
+"CO","7.6","6.1","7.5","6.2","9.9","6.0","66.3","2.16","0.68","0.84","1.52","3.68","3.22","0.62","0.08","58.5","56.8","1.2","0.35462","3.01904","3.6","47574","15.465715","6.301084","47.560332","1.943715","2.062230","3.872010","0.088626","3.633371","86.832813","17.676144","71.128281","1.719544","16.945294","12.513949","5.861102","93.915574","75.203028","69.259789","19.332534","12.842127","1.183063","0.924672","2023-02-01"
+"CT","7.9","3.3","8.7","2.1","9.8","2.8","96.1","2.13","0.81","0.72","1.52","3.65","3.20","0.48","0.09","43.6","45.6","0.7","0.37347","3.12293","2.1","26299","17.142969","6.602093","42.812501","1.285110","2.264424","7.452102","0.006252","3.541306","84.747229","15.665749","62.698501","1.471289","19.217666","17.268224","6.452903","93.046692","74.507476","64.082133","23.019671","11.607396","1.720229","0.869883","2023-02-01"
+"DC","14.4","2.4","16.4","2.5","16.1","4.6","117.7","2.46","0.59","1.55","2.15","4.61","3.98","1.25","0.11","46.3","44.0","0.7","0.53050","3.50084","2.8","90079","17.149334","5.072200","64.161295","1.323201","1.687977","0.829892","0.169683","1.229977","90.680541","7.962449","65.172847","1.668955","22.407608","10.783063","13.164346","95.659753","65.283356","70.271605","19.374209","6.763558","0.929341","0.325031","2023-02-01"
+"DE","11.4","0.9","13.3","0.8","15.5","1.4","84.6","2.30","0.94","1.06","2.00","4.30","3.75","0.73","0.10","46.9","44.6","0.6","0.34697","3.03371","1.5","24843","15.009389","4.850518","51.780924","0.761590","1.721781","4.803085","0.190354","4.056899","95.177121","11.092806","80.869836","1.009524","18.304275","18.201550","5.977196","97.253134","76.179021","81.259924","19.549109","12.075911","1.457310","0.730632","2023-02-01"
+"FL","5.9","1.8","5.6","2.7","6.7","3.5","100.2","2.40","0.85","0.68","1.54","3.93","3.54","0.47","0.09","54.0","58.4","0.9","0.35502","3.09968","2.1","27981","12.816814","6.365373","52.264518","1.134168","1.796867","2.448652","0.017971","2.804979","97.076653","10.386811","90.147105","1.726172","14.941135","21.530673","9.270504","96.952495","78.078477","87.700518","25.296906","9.591155","1.915698","0.691537","2023-02-01"
+"GA","4.1","1.3","4.1","2.8","3.9","3.3","80.5","1.90","1.04","0.45","1.49","3.39","2.90","0.28","0.05","54.0","52.6","0.8","0.36572","3.12253","2.1","25067","17.040645","6.798043","42.320798","1.686013","3.169320","5.132865","0.069017","3.323895","92.367287","18.235999","79.363199","2.826814","18.303896","20.802715","10.509768","95.279234","69.422210","75.692177","21.757265","11.722100","1.646802","1.041544","2023-02-01"
+"GU","20.0","8.0","13.0","5.0","13.0","10.0","15.2","4.19","1.68","4.10","5.78","9.97","9.11","3.55","0.32",,,,"0.67516","3.34519","3.0","7150",,,,,,,,,,,"97.163122","8.333332",,,,"70.833333","0.000000","91.452991","24.816053","0.000000",,,"2023-02-01"
+"HI","11.4","2.2","9.6","3.2","8.6","0.7","74.6","2.72","0.34","1.60","1.94","4.66","4.15","1.25","0.07","39.3","43.4","0.8","0.38766","3.23198","2.5","41427","12.348540","5.191199","47.941947","1.853915","2.481894","1.479999","0.345567","1.639112","96.267609","9.441430","78.896335","1.257852","19.499854","8.656161","4.854205","97.745290","77.616533","77.288302","15.798930","8.019240","1.013812","0.675099","2023-02-01"
+"IA","7.6","6.2","6.4","5.1","6.1","5.6","47.7","2.36","0.59","0.74","1.34","3.69","3.19","0.50","0.04","56.1","50.7","0.8","0.32422","2.94033","2.5","31362","17.182500","5.503345","45.826977","2.905904","3.211425","4.620689","0.234481","3.661753","94.486830","17.036856","84.754686","1.696546","17.689218","19.334326","6.198473","95.693380","78.295246","74.139313","20.062687","12.770796","1.354242","1.165986","2023-02-01"
+"ID","8.2","4.6","10.3","4.5","10.4","4.2","47.1","2.49","0.83","0.95","1.78","4.27","3.61","0.64","0.14","56.1","51.0","0.7","0.37520","3.14779","2.5","33492","12.924392","5.824486","46.843756","1.784242","2.388714","9.129729","0.366979","3.501341","96.634971","17.707444","86.341184","1.204967","16.319169","15.595508","6.164639","96.364004","74.936058","81.483404","14.573690","11.210045","0.901025","1.001252","2023-02-01"
+"IL","11.7","6.5","11.0","8.0","9.4","8.0","84.8","1.98","0.65","0.72","1.37","3.35","2.91","0.53","0.06","52.1","49.8","0.9","0.41900","3.31127","3.3","82957","13.806874","6.641194","44.309725","1.776029","2.565415","32.133572","0.101770","3.463801","85.381931","17.796274","65.381111","2.063073","15.655724","18.842671","8.756588","91.656850","70.257559","62.457472","24.553063","12.930031","1.788210","1.163074","2023-02-01"
+"IN","8.9","6.9","9.6","7.1","9.6","6.8","66.8","2.13","0.79","0.63","1.42","3.55","3.05","0.41","0.06","57.4","53.4","0.7","0.42895","3.39945","2.6","30277","16.282996","6.252093","56.243292","0.931282","1.805943","14.403894","0.076208","4.067471","90.536625","12.883658","76.996226","1.609374","13.844641","22.110261","7.300250","94.211093","73.988901","72.941627","21.401472","12.099129","1.484595","1.110387","2023-02-01"
+"KS","8.0","9.8","6.3","9.3","7.1","11.3","47.4","2.57","0.67","0.72","1.39","3.97","3.46","0.50","0.05","57.3","52.1","0.7","0.33705","2.96194","2.4","23551","17.270619","5.726627","41.596896","2.485671","3.635786","5.957237","0.059274","5.083958","88.856452","17.844800","71.643663","2.252982","18.470156","22.772910","6.972764","94.759783","74.145134","67.158045","20.934086","11.560256","1.687413","1.241434","2023-02-01"
+"KY","4.6","1.9","5.5","1.4","4.9","0.9","74.3","2.25","0.86","0.77","1.64","3.89","3.36","0.50","0.06","56.7","50.0","0.7","0.41963","3.30694","1.6","40177","16.053339","7.800501","45.536515","1.538687","3.040351","8.842789","0.227049","4.119532","91.810084","16.179459","79.047690","2.173845","18.002202","29.375386","9.274942","95.133746","71.989882","76.797540","22.417735","14.350174","1.708204","1.389017","2023-02-01"
+"LA","6.1","1.3","4.1","0.9","5.0","1.0","85.4","2.20","1.14","0.27","1.41","3.61","3.06","0.18","0.05","55.4","53.6","0.9","0.38369","3.09516","2.0","16894","18.656230","5.663137","39.565836","1.731458","2.810300","1.498618","0.270675","3.423500","93.077418","16.621426","81.679050","2.581402","14.256986","22.552108","9.601170","94.265139","65.089414","73.692261","25.552806","13.440326","2.206644","1.537023","2023-02-01"
+"MA","11.6","2.6","9.5","2.0","9.6","1.9","90.9","2.10","0.92","0.67","1.59","3.69","3.24","0.47","0.07","47.1","52.8","0.8","0.31907","3.07685","2.7","50457","15.723910","5.724830","57.595477","1.467799","2.403211","2.008722","0.170192","3.730504","89.843750","19.521781","73.777879","1.508874","16.115730","18.586347","6.565019","95.019709","71.546852","74.994018","23.636202","11.430013","1.546126","0.805229","2023-02-01"
+"MD","14.4","5.1","14.7","3.6","11.6","3.8","99.0","2.08","0.87","0.83","1.70","3.78","3.30","0.59","0.09","49.3","50.9","0.9","0.42591","3.32068","1.8","21990","20.331978","6.173061","63.941173","1.413939","2.086239","11.160903","0.122735","2.582015","89.935951","13.134876","74.321669","1.542654","25.781949","14.155775","10.105588","95.268467","73.059782","76.285525","20.975714","9.484874","1.149602","0.709799","2023-02-01"
+"ME","6.4","4.9","5.7","4.0","5.7","5.4","58.0","2.87","0.47","1.00","1.48","4.34","3.83","0.69","0.08","55.5","50.8","0.8","0.31218","3.16030","1.0","7382","13.638926","6.025590","65.142410","2.004786","3.395025","8.676855","0.052568","4.127018","90.498288","20.603068","67.179995","1.495056","20.860618","16.306428","6.147884","95.563935","70.159342","73.078954","15.817526","14.276760","1.006634","1.146582","2023-02-01"
+"MI","12.7","6.1","13.8","5.4","13.1","4.8","76.8","2.23","0.89","0.75","1.64","3.87","3.34","0.47","0.06","53.1","48.4","0.9","0.32755","3.01248","2.6","69596","12.304438","6.631126","47.835367","1.610109","2.299351","3.402342","0.146837","2.924258","92.242691","13.632260","79.105072","1.332810","15.182324","18.966396","8.849317","92.961463","77.621248","74.864522","22.459666","11.431663","1.598690","0.918488","2023-02-01"
+"MN","6.7","4.5","8.6","3.5","9.4","3.4","56.3","2.38","0.66","1.01","1.67","4.05","3.48","0.63","0.07","50.9","47.2","0.6","0.30696","2.97545","2.4","38927","13.739490","4.743802","49.695572","2.918130","2.862371","4.374829","0.076392","4.018921","96.032326","16.585568","88.092258","1.826142","16.459135","12.601455","6.810617","95.146963","77.097943","77.600065","20.614291","13.533464","1.359289","0.961614","2023-02-01"
+"MO","10.7","7.0","9.6","4.6","8.4","5.3","67.1","2.12","0.70","0.46","1.16","3.28","2.86","0.32","0.04","60.3","58.5","0.8","0.31571","2.78326","3.0","35865","15.459187","5.413499","30.087970","2.183881","3.297052","4.646042","0.118577","3.827940","86.108015","20.687883","67.949211","2.549121","13.767195","23.985574","9.445319","92.278732","72.307599","64.118590","23.853437","12.073895","1.761408","1.209387","2023-02-01"
+"MS","3.9","0.6","4.5","0.4","4.1","0.7","69.4","2.32","1.09","0.59","1.68","4.00","3.36","0.35","0.05","52.4","49.9","0.7","0.34129","3.05183","1.1","21152","19.214683","6.958171","48.472827","1.942004","2.993915","2.040307","0.467353","3.094010","96.072347","20.591187","84.781258","3.337471","19.151819","22.715151","10.231306","96.417677","68.900694","79.245312","23.818034","15.014332","2.066777","1.523809","2023-02-01"
+"MT","6.9","7.8","6.5","10.8","7.8","12.2","46.6","2.28","0.57","0.83","1.41","3.69","3.27","0.61","0.06","63.2","55.9","0.9","0.30381","2.83834","4.3","64624","15.897640","7.377114","42.449845","2.945208","2.960927","5.103359","0.228091","4.921630","90.655136","17.775999","75.340530","1.699699","17.451727","14.567369","8.471330","94.336078","74.622658","69.289414","15.661370","12.570224","1.197952","1.253237","2023-02-01"
+"NC","7.6","2.0","6.0","2.7","6.0","4.0","78.0","2.20","0.89","0.57","1.46","3.66","3.15","0.37","0.08","57.2","54.8","0.8","0.40680","3.23533","1.8","44631","19.368996","8.365626","53.842000","1.462936","3.298314","3.419275","0.056298","3.779915","88.411617","11.988600","76.066907","1.483103","22.918309","21.400159","10.276281","93.055159","72.619948","74.185894","20.928803","12.743005","1.427248","1.075012","2023-02-01"
+"ND","5.9","1.5","5.6","1.3","5.8","2.0","59.5","2.87","0.61","0.86","1.47","4.34","3.65","0.52","0.03","53.1","40.8","0.5","0.29070","2.94563","1.2","12419","16.393833","5.473313","48.268205","1.997814","3.509871","5.421254","0.193185","4.570133","98.238410","20.217778","91.943488","2.721974","15.731838","18.752673","5.869866","98.301952","77.585936","84.642634","17.287770","9.944697","1.270855","0.954934","2023-02-01"
+"NE","7.4","6.3","6.3","4.6","8.9","6.5","51.9","2.55","0.68","0.72","1.40","3.95","3.39","0.50","0.05","56.1","47.5","0.6","0.33541","3.02373","1.9","19653","14.762205","5.825405","48.168379","2.289872","3.784967","4.156021","0.174891","4.819080","91.735755","18.843531","79.508421","1.790478","17.085793","19.739101","5.876530","95.958044","78.644474","72.319867","19.872370","10.487467","1.565626","0.991274","2023-02-01"
+"NH","4.8","3.1","3.8","2.6","3.4","2.7","76.6","2.30","0.81","0.72","1.53","3.83","3.32","0.47","0.06","51.7","45.6","0.5","0.35018","3.07170","1.3","10862","17.985373","6.453707","47.445124","1.694464","2.661148","5.833451","0.225576","4.927813","93.195146","16.636614","78.343891","1.443100","19.562980","16.461981","6.487035","96.852235","79.661243","76.564362","19.780502","13.562729","1.359127","1.056185","2023-02-01"
+"NJ","4.3","2.8","3.7","1.2","4.8","1.6","110.4","2.11","0.92","0.75","1.67","3.78","3.30","0.52","0.10","48.8","49.8","1.0","0.37014","3.05687","2.2","31780","10.457140","6.003672","41.742823","1.258171","1.493034","6.499913","0.103787","2.594331","93.606364","10.538616","80.683178","1.195082","10.681691","17.429732","9.083879","96.519125","81.174676","80.212170","23.532855","8.990347","1.791158","0.720861","2023-02-01"
+"NM","15.7","3.9","12.4","4.2","10.8","4.6","74.0","2.27","0.69","0.65","1.34","3.60","3.17","0.47","0.08","61.2","64.5","0.9","0.33007","2.92979","3.4","54788","16.880726","6.974591","42.691992","2.212342","1.689994","3.268364","0.047687","3.344050","95.710454","16.752933","78.284619","1.760241","17.587717","13.044343","8.225322","95.091577","67.042592","74.459302","18.994139","16.282381","1.341497","1.503519","2023-02-01"
+"NV","13.5","12.5","14.9","11.5","15.8","14.0","82.6","2.33","1.01","0.84","1.84","4.17","3.63","0.60","0.11","53.7","52.5","0.9","0.44537","3.18521","3.9","19457","15.613568","5.810421","39.745229","2.348736","2.082659","2.638951","0.186568","2.681275","91.584943","13.595163","79.367319","1.786471","18.670537","18.029114","8.509120","91.560315","69.506874","75.616935","20.984294","9.430053","1.333119","0.654221","2023-02-01"
+"NY","4.9","4.5","5.5","3.9","4.9","4.4","157.8","2.08","0.78","0.66","1.44","3.52","2.99","0.43","0.12","45.0","47.2","0.6","0.47563","3.43071","1.5","9742","15.066503","6.417714","54.453754","1.156857","2.109533","13.099350","0.203698","3.010254","89.287753","11.614134","70.707866","1.413465","15.399499","13.317674","9.528866","95.009824","75.573860","73.840557","19.143212","9.340678","1.396627","0.742529","2023-02-01"
+"OH","10.1","6.0","10.5","5.4","7.9","4.4","68.2","1.96","0.94","0.60","1.54","3.50","3.07","0.39","0.06","58.1","54.5","0.8","0.46852","3.38465","2.3","31735","14.965475","6.857397","43.246040","0.965583","1.502662","21.593891","0.053769","3.667924","89.503534","13.075572","73.916728","1.695244","14.012745","22.485032","7.411900","92.505303","75.215028","69.647646","23.028039","12.755315","1.407392","1.061040","2023-02-01"
+"OK","7.6","4.3","8.9","4.0","8.1","4.4","55.5","2.46","0.95","0.34","1.29","3.76","3.34","0.27","0.02","61.0","60.4","0.7","0.31539","2.81726","3.2","23013","13.895305","4.488022","36.933077","2.527743","3.529558","4.213974","0.097056","4.232689","91.466497","14.176484","75.929068","2.076984","13.214458","24.398642","8.977589","95.142884","72.537256","70.803339","24.843107","16.633618","1.921517","1.510100","2023-02-01"
+"OR","9.7","5.4","13.9","5.2","9.8","4.3","47.9","3.17","0.93","0.74","1.67","4.83","4.21","0.45","0.07","54.6","57.9","0.9","0.32562","3.07921","2.3","32471","13.308442","6.389020","51.941615","2.617117","2.431452","5.745072","0.185699","2.685213","94.926349","15.629524","82.965184","1.415088","20.172390","11.943572","8.776540","93.718219","75.768330","77.416431","18.273032","15.092847","1.203002","1.385720","2023-02-01"
+"PA","8.0","5.1","8.3","5.5","8.4","5.4","96.0","2.03","0.87","0.78","1.65","3.68","3.26","0.54","0.09","50.8","48.6","0.9","0.41560","3.32079","1.8","26519","15.197922","6.622871","55.267584","1.551957","1.933698","2.951130","0.212735","3.348926","87.934847","15.708643","70.684062","1.486137","19.322636","19.718950","7.379146","94.139844","72.521877","70.032500","20.988444","9.326240","1.397721","0.672073","2023-02-01"
+"PR","4.4","6.7","5.8","5.8","3.0","10.6","23.2","0.00","1.39","2.85","4.24","4.24","3.35","2.05","0.46","46.3","46.6","0.0","0.40967","2.99292","5.8","45874",,,,,,,,,,,"92.917090","0.648318",,,,,"77.337036","97.667416",,,,,"2023-02-01"
+"RI","9.4","0.3","7.2","0.5","4.2","0.5","90.3","2.35","0.43","0.82","1.25","3.59","3.13","0.58","0.06","49.8","48.8","1.0","0.35556","3.05791","2.9","52981","16.642184","6.047845","45.177640","1.240739","2.869224","2.185293","0.087767","3.832213","91.629133","18.238520","72.640066","1.492483","18.642817","14.973592","7.519538","95.740607","76.334540","75.367531","23.154435","13.624046","1.267121","0.841478","2023-02-01"
+"SC","4.1","0.7","4.3","0.4","7.8","0.8","83.1","2.24","1.03","0.64","1.67","3.91","3.39","0.40","0.07","57.7","54.0","0.8","0.33566","3.03900","1.7","17843","14.164727","7.366458","56.441614","1.365320","2.875399","2.837575","0.241224","3.287655","92.143804","14.863037","80.608406","1.833134","18.000137","19.891573","9.662829","94.396477","72.465571","77.092520","23.314670","12.731835","1.765981","1.048664","2023-02-01"
+"SD","4.6","1.5","5.0","1.8","4.5","1.2","48.8","2.33","0.44","0.79","1.23","3.56","3.03","0.51","0.05","53.8","41.5","0.7","0.30817","2.93111","1.5","18554","16.834104","5.976653","51.417000","2.720656","3.291942","5.264478","0.143154","5.459047","95.028219","19.889467","85.487463","1.752324","18.448584","15.008498","6.788145","96.995369","80.949700","77.533005","16.528802","11.616449","1.264200","0.841901","2023-02-01"
+"TN","5.4","2.0","4.4","3.0","4.0","3.4","77.2","2.05","1.14","0.57","1.70","3.75","3.19","0.35","0.08","55.7","51.3","0.9","0.39973","3.25675","1.3","32227","16.810657","7.716838","52.765450","1.615227","3.073131","9.865683","0.185103","3.401994","88.845066","15.912567","75.656471","1.912190","21.131910","29.392660","8.691131","93.233403","73.199093","73.972533","20.181561","11.435387","1.326567","0.979845","2023-02-01"
+"TX","6.2","2.2","6.4","3.3","7.6","4.1","69.5","1.96","1.05","0.37","1.42","3.38","2.93","0.28","0.06","59.6","63.2","0.8","0.41931","3.22391","2.9","40448","17.074427","5.029710","52.196518","1.567410","1.416943","5.039136","0.043473","3.349034","95.502194","10.582499","83.231536","1.884036","14.435950","22.055441","7.508460","96.323202","66.829074","78.081775","23.965753","12.226630","1.777216","1.126636","2023-02-01"
+"UT","9.4","3.3","9.0","3.7","9.8","4.3","55.3","2.32","0.55","1.16","1.71","4.02","3.49","0.86","0.12","60.8","50.3","0.8","0.45973","3.32571","2.7","27251","13.928882","5.526550","45.600098","1.718026","2.895718","10.177801","0.060164","3.226548","95.637162","13.366906","89.303507","1.329467","16.885494","20.695680","6.323916","95.588038","71.460153","84.549547","18.248338","11.729645","0.984483","0.807143","2023-02-01"
+"VA","11.3","4.5","11.5","4.5","11.5","5.1","92.5","1.95","1.02","0.62","1.63","3.59","3.09","0.40","0.08","56.5","55.5","1.1","0.40404","3.21630","1.6","18869","17.088182","7.173330","52.658001","1.136915","2.622878","4.366234","0.135625","3.526327","88.492761","13.836493","70.953070","1.637843","19.632030","20.288508","8.688855","94.426334","74.984786","72.172020","21.472724","12.399274","1.353924","1.005582","2023-02-01"
+"VT","7.4","2.5","4.7","2.5","4.5","1.1","67.7","2.35","1.00","0.72","1.72","4.07","3.55","0.46","0.08","61.3","56.7","0.7","0.37203","3.19644","1.6","18110","19.695843","6.495890","48.690671","2.038359","2.892317","13.645847","0.109464","5.059416","91.166759","17.128111","80.112576","1.925287","22.700096","16.323132","7.682717","95.385608","79.195423","75.541526","17.119589","13.717848","1.197579","1.462406","2023-02-01"
+"WA","18.0","8.1","20.0","6.0","17.6","7.6","64.8","2.52","0.79","0.90","1.69","4.21","3.56","0.62","0.09","54.9","53.1","1.0","0.39495","3.30419","2.5","66656","14.634771","6.167716","54.567819","1.994137","2.317844","13.320128","0.328683","2.767774","95.106071","13.810903","84.581613","1.364470","19.892669","12.209977","6.936666","94.905943","73.869151","79.524549","16.574808","11.553737","1.019159","0.836404","2023-02-01"
+"WI","7.9","6.2","7.3","4.6","7.2","4.2","52.0","2.28","0.58","0.96","1.54","3.82","3.34","0.66","0.07","53.1","46.3","0.6","0.36456","3.15373","2.1","41912","13.690116","5.695565","50.198605","3.069005","3.102199","6.290265","0.079362","3.238508","95.575805","14.099089","87.490647","1.143070","18.218220","15.397054","7.371773","95.116789","75.984216","79.213494","19.847340","13.503943","1.294014","1.303517","2023-02-01"
+"WV","11.3","2.4","10.3","2.4","10.2","2.9","73.5","2.11","0.96","0.69","1.65","3.76","3.22","0.36","0.05","52.6","44.1","0.5","0.40919","3.24531","1.2","20821","16.293913","8.265190","44.432807","1.870960","3.791073","4.469620","0.143698","4.827009","95.376320","16.726475","78.814057","1.676784","20.695138","24.155520","9.562136","95.960581","71.442064","74.423412","20.816512","13.386938","1.531578","1.202551","2023-02-01"
+"WY","6.0","6.3","6.2","3.9","8.3","3.3","56.5","2.25","0.54","0.86","1.40","3.65","3.10","0.63","0.05","58.4","48.8","0.4","0.34348","2.98457","2.4","20675","15.041404","7.127500","45.861098","2.375188","3.778759","7.096262","0.253890","4.172703","96.198127","19.817071","79.418052","2.199333","15.663748","14.880130","6.360152","96.324929","79.122969","73.355561","14.338753","14.813702","1.098024","1.308557","2023-02-01"
diff --git a/mathesar/tests/imports/test_csv.py b/mathesar/tests/imports/test_csv.py
--- a/mathesar/tests/imports/test_csv.py
+++ b/mathesar/tests/imports/test_csv.py
@@ -21,6 +21,14 @@ def data_file(patents_csv_filepath):
     return data_file
 
 
+@pytest.fixture
+def long_column_data_file():
+    data_filepath = 'mathesar/tests/data/long_column_names.csv'
+    with open(data_filepath, "rb") as csv_file:
+        data_file = DataFile.objects.create(file=File(csv_file))
+    return data_file
+
+
 @pytest.fixture
 def headerless_data_file(headerless_patents_csv_filepath):
     with open(headerless_patents_csv_filepath, "rb") as csv_file:
@@ -57,7 +65,8 @@ def check_csv_upload(table, table_name, schema, num_records, row, cols):
     assert table.schema == schema
     assert table.sa_num_records() == num_records
     assert table.get_records()[0] == row
-    assert all([col in table.sa_column_names for col in cols])
+    for col in cols:
+        assert col in table.sa_column_names
 
 
 def test_csv_upload(data_file, schema):
@@ -89,6 +98,74 @@ def test_csv_upload(data_file, schema):
     )
 
 
+def test_csv_upload_long_columns(long_column_data_file, schema):
+    table_name = "long_cols"
+    table = create_table_from_csv(long_column_data_file, table_name, schema)
+
+    num_records = 54
+    expected_row = (
+        1, 'NATION', '8.6', '4.5', '8.5', '4.3', '8.3', '4.6', '78.6', '2.22',
+        '0.88', '0.66', '1.53', '3.75', '3.26', '0.45', '0.07', '53.9', '52.3',
+        '0.8', '0.38487', '3.15796', '2.3', '33247', '14.842144', '6.172333',
+        '47.158545', '1.698662', '2.345577', '7.882694', '0.145406', '3.395302',
+        '92.085375', '14.447634', '78.873848', '1.738571', '16.161024',
+        '19.436701', '8.145643', '94.937079', '74.115131', '75.601680',
+        '22.073834', '11.791045', '1.585233', '1.016932', '2023-02-01',
+    )
+    expected_cols = [
+        'id',
+        'State or Nation',
+        'Cycle 1 Total Number of Health Deficiencies',
+        'Cycle 1 Total Number of Fire Safety Deficiencies',
+        'Cycle 2 Total Number of Health Deficiencies',
+        'Cycle 2 Total Number of Fire Safety Deficiencies',
+        'Cycle 3 Total Number of Health Deficiencies',
+        'Cycle 3 Total Number of Fire Safety Deficiencies',
+        'Average Number of Residents per Day',
+        'Reported Nurse Aide Staffing Hours per Resident per Day',
+        'Reported LPN Staffing Hours per Resident per Day',
+        'Reported RN Staffing Hours per Resident per Day',
+        'Reported Licensed Staffing Hours per Resident per Day',
+        'Reported Total Nurse Staffing Hours per Resident per Day',
+        'Total number of nurse staff hours per resident per day-8cd5ab5e',
+        'Registered Nurse hours per resident per day on the weekend',
+        'Reported Physical Therapist Staffing Hours per Resident Per Day',
+        'Total nursing staff turnover',
+        'Registered Nurse turnover',
+        'Number of administrators who have left the nursing home',
+        'Case-Mix RN Staffing Hours per Resident per Day',
+        'Case-Mix Total Nurse Staffing Hours per Resident per Day',
+        'Number of Fines',
+        'Fine Amount in Dollars',
+        'Percentage of long stay residents whose need for help-5c97c88f',
+        'Percentage of long stay residents who lose too much weight',
+        'Percentage of low risk long stay residents who lose co-fc6bc241',
+        'Percentage of long stay residents with a catheter inse-ce71f22a',
+        'Percentage of long stay residents with a urinary tract-f16fbec8',
+        'Percentage of long stay residents who have depressive symptoms',
+        'Percentage of long stay residents who were physically-f30de0aa',
+        'Percentage of long stay residents experiencing one or-9f9e8f36',
+        'Percentage of long stay residents assessed and appropr-84744861',
+        'Percentage of long stay residents who received an anti-20fe5d12',
+        'Percentage of short stay residents assessed and approp-3568770f',
+        'Percentage of short stay residents who newly received-e98612b4',
+        'Percentage of long stay residents whose ability to mov-66839cb4',
+        'Percentage of long stay residents who received an anti-868593e4',
+        'Percentage of high risk long stay residents with press-b624bbba',
+        'Percentage of long stay residents assessed and appropr-999c26ef',
+        'Percentage of short stay residents who made improvemen-ebe5c21e',
+        'Percentage of short stay residents who were assessed a-26e64965',
+        'Percentage of short stay residents who were rehospital-682a4dae',
+        'Percentage of short stay residents who had an outpatie-9403ec21',
+        'Number of hospitalizations per 1000 long-stay resident days',
+        'Number of outpatient emergency department visits per 1-f0fed7b5',
+        'Processing Date'
+    ]
+    check_csv_upload(
+        table, table_name, schema, num_records, expected_row, expected_cols
+    )
+
+
 def test_headerless_csv_upload(headerless_data_file, schema):
     table_name = "NASA no headers"
     table = create_table_from_csv(headerless_data_file, table_name, schema)