DuckDB CSV identifier joins with mixed key types
In DuckDB 2.0.1, three unique site codes and three fictional payments worth £150 produced five join rows worth £250 after one input switched to integer keys. The original row count and total came back after a late cast, but £25 still went to the wrong site.
Why a unique lookup can produce two matches
The CSV has three text codes, 001, 1, and 010. DuckDB’s automatic import kept them as text in this test. The SQL below pins the type and uses three fictional payments with IDs 001, 1, and 999. An integer conversion on the payment side simulates an upstream feed that has already parsed the codes as numbers.
DuckDB applies combination casting when comparing different types. In the mixed join, each numeric 1 matches both 001 and 1 in the site table. Casting the payment IDs back to text at the join makes both match 1. The original leading zeros are already gone.
| Payment and site keys | Rows | Total | Matches for £25 payment |
|---|---|---|---|
| Original text on both sides | 3 | £150 | North workshop |
| Integer payments, text sites | 5 | £250 | East branch and North workshop |
| Integer payments cast back to text | 3 | £150 | East branch |
Run the queries in a fresh DuckDB session
Save csv-identifier-check.csv in your working directory. Open DuckDB 2.0.1 in that directory and run this block once in a new in-memory database. The payments are created by the script. No account or existing data is needed.
CREATE VIEW sites AS
SELECT * FROM read_csv('csv-identifier-check.csv', types = {'ExternalID': 'VARCHAR'});
CREATE TABLE payments (
PaymentID VARCHAR,
ExternalID VARCHAR,
Amount INTEGER
);
INSERT INTO payments VALUES
('P-01', '001', 25),
('P-02', '1', 75),
('P-03', '999', 50);
CREATE VIEW payments_numeric AS
SELECT PaymentID, CAST(ExternalID AS BIGINT) AS ExternalID, Amount
FROM payments;
SELECT COUNT(*) AS SiteRows, COUNT(DISTINCT ExternalID) AS UniqueSiteIDs
FROM sites;
CREATE VIEW joined_payments AS
SELECT 'text keys' AS Scenario, p.PaymentID, p.Amount, s.Site
FROM payments p LEFT JOIN sites s ON p.ExternalID = s.ExternalID
UNION ALL
SELECT 'mixed types', p.PaymentID, p.Amount, s.Site
FROM payments_numeric p LEFT JOIN sites s ON p.ExternalID = s.ExternalID
UNION ALL
SELECT 'late cast', p.PaymentID, p.Amount, s.Site
FROM payments_numeric p LEFT JOIN sites s ON CAST(p.ExternalID AS VARCHAR) = s.ExternalID;
SELECT Scenario, COUNT(*) AS JoinedRows, SUM(Amount) AS Total,
STRING_AGG(Site, ' and ' ORDER BY Site) FILTER (WHERE PaymentID = 'P-01') AS P01Sites
FROM joined_payments
GROUP BY Scenario
ORDER BY Scenario;
SELECT Scenario, PaymentID, Site, Amount
FROM joined_payments
ORDER BY Scenario, PaymentID, Site;
SELECT CAST(ExternalID AS BIGINT) AS NumericID,
LIST(ExternalID ORDER BY ExternalID) AS OriginalIDs
FROM sites
GROUP BY NumericID
HAVING COUNT(*) > 1;Verify each payment reaches its intended site
Keep the original IDs as text when importing both sides. The CSV type override fixes the type before a value can change. If a feed already stripped the zeros, recover the source ID from the original export or a verified mapping.
Before publishing an app or report, check for duplicate IDs in the values the join actually uses, preserve unmatched records such as 999, and compare individual assignments with the source. The same row count and total can hide a payment sent to the wrong site. A pandas exercise using the same site CSV shows what happens when the conversion takes place at import.
Tested by Zuch Huang with fictional data.
Appsmith CSV merge and DuckDB on the same three records
In Appsmith’s public merge demo, the result displayed four records when both CSVs ended in a line feed (LF). The table showed three populated rows. With one or neither file ending in LF, the demo displayed three records. DuckDB 2.0.1 returned three rows for all four pairs.
| Sites file ending | Measurements file ending | Appsmith displayed | Appsmith populated | DuckDB rows |
|---|---|---|---|---|
| LF | LF | 4 | 3 | 3 |
| LF | No LF | 3 | 3 | 3 |
| No LF | LF | 3 | 3 | 3 |
| No LF | No LF | 3 | 3 | 3 |
Open the public Appsmith demo in a signed-out browser. Under Select CSV1, upload the sites CSV with final LF or the sites CSV without final LF. Under Select CSV2, upload the measurements CSV with final LF or the measurements CSV without final LF. Use a fresh session for each pair.
Read the displayed count and the ID, City and LatD columns in the merged table. All four pairs matched 001 to North workshop (25), 1 to East branch (75), and 010 to South kiosk (50). Each pair of downloadable versions differs by one final byte.
To reproduce the DuckDB control, save the files together and run this query from that directory. Substitute the filenames without the final LF to check the other pairs.
SELECT s.id, s.City, m.LatD
FROM read_csv('sites-with-final-lf.csv', all_varchar=true) s
LEFT JOIN read_csv('measurements-with-final-lf.csv', all_varchar=true) m USING (id)
ORDER BY s.id;Spreadsheet to App ran these checks on November 29, 2026, with Codex assistance and fictional data. Browser observations cover the public demo’s table and count.
Three output rows can hide two wrong site matches
Keep the same measurements for 001, 1, and 010. Replace the sites file with a two-row lookup that contains 1 (East branch) and 10(Overflow site). Only 1 matches by exact text. A left join should keep all three measurements and leave two sites blank.
| Measurement ID | DuckDB automatic import | DuckDB text keys and TableWorkbench |
|---|---|---|
001 | East branch | Unmatched |
1 | East branch | East branch |
010 | Overflow site | Unmatched |
Download the replacement lookup with final LF or the replacement lookup without final LF. Save it with one of the measurements files in the original test above. Run these queries from that folder. Substitute either filename to repeat the check with or without a final LF.
SELECT 'automatic import' AS test, m.id, s.City
FROM read_csv('measurements-with-final-lf.csv') m
LEFT JOIN read_csv('lookup-with-final-lf.csv') s USING (id)
UNION ALL
SELECT 'text keys' AS test, m.id, s.City
FROM read_csv('measurements-with-final-lf.csv', types = {'id':'VARCHAR'}) m
LEFT JOIN read_csv('lookup-with-final-lf.csv', types = {'id':'VARCHAR'}) s USING (id)
ORDER BY test, id;DuckDB inferred VARCHAR for the measurement IDs and BIGINT for the lookup IDs. Comparing the mixed types made 001 match 1 and 010 match 10. Setting both ID types to text preserved the three rows and left the two padded IDs unmatched.
For the browser check, open the public TableWorkbench CSV join. Paste measurements first, then the replacement lookup. Confirm parsing after each paste. Select id on both sides and Keep every record from A. Run once to see the projection, confirm it, then run again. Check Only in A for two measurement IDs and Only in B for the lookup ID 10.
Spreadsheet to App ran the four line-ending combinations in TableWorkbench and DuckDB on November 29, 2026, with Codex assistance and fictional data. The public TableWorkbench also matched all three original site assignments for all four input pairs. The Appsmith observations above cover the complete original lookup.
Check that the Excel export keeps identifiers as text
CSV stores field characters. An Excel workbook can also mark each cell as text or numeric. If another team needs an Excel file before it runs the join, verify the downloaded cell types and values at that handoff.
- Download the five-row CSV and open it in the public TableMender CSV to Excel tool. Preview and download with the default settings. All cells are stored as text, including the quantities.
- Expand Excel column types. Set
ExternalIDto Number. The tool flags three source records and disables the XLSX download. Two IDs have leading zeros and one has 16 digits. - Return
ExternalIDto Text and setQuantityto Number. Download again. Check for five data rows with IDs001,1,010,10and9876543210987654. The numeric quantities are 6, 14, 6, 14 and 8, totaling 48.
Spreadsheet to App ran the public converter in 390- and 1280-pixel browser viewports on November 29, 2026, with Codex assistance and fictional data. We read back all four downloaded XLSX files. Both export modes preserved all five text IDs on both screens. The explicit quantity setting saved five numeric cells. For the next CSV join, set both join keys to text before comparing the values again.
Compare the default Deskbench export
Open the same five-row CSV in Deskbench CSV to Excel. Choose Save as Excel (.xlsx) with the default settings. On November 30, 2026, we downloaded the workbook at 390 and 1280 pixels and checked the stored cells. Both exports kept the five IDs intact as text. All five quantities were stored as text too.
In Apple Numbers 15.2.1, add a blank column to each imported workbook and enter =COUNT(C2:C6) and =SUM(C2:C6). Both formulas returned 0 on the Deskbench export. The TableMender export with numeric quantities returned 5 and 48.
If the recipient needs numeric quantities, repeat the TableMender steps above with ExternalID set to Text and Quantity set to Number. Check the five exact IDs and the quantity total of 48 before joining the file. Spreadsheet to App ran the comparison with Codex on fictional branch records.
Check both CSV join keys in csvkit
This check uses the same site CSV and a different set of fictional payments. It uses 1 for £75, 10 for £25, and 999 for £50. Only 1 should match a site. The lookup has 010, which is a different ID from 10 in this fixture.
| Import option | csvsql | csvjoin |
|---|---|---|
| Defaults | 4 rows, £225, 3 matched rows | 4 rows, £225, 3 matched rows |
--no-leading-zeroes | 4 rows, £225, 3 matched rows | 3 rows, £150, no match |
--no-inference | 3 rows, £150, only ID 1 matches | 3 rows, £150, only ID 1 matches |
With --no-leading-zeroes, the csvsql run kept the site keys as text. The other file’s payment keys were numeric. SQLite’s comparison rules made numeric 1 match two site IDs and made 10 match 010. The same flag in csvjoin left every payment unmatched. Disabling inference kept both inputs as text and restored the single correct match to East branch.
With csvkit 2.2.0 installed, save the site CSV in your working directory and run this block in Bash. It creates and removes a temporary payment file. When checking the totals with type inference disabled, treat the amounts as whole pounds and keep the identifiers as text. Follow the source data dictionary to decide which keys should match.
(
set -eu
work_dir=$(mktemp -d)
trap 'rm -rf "$work_dir"' EXIT
cat > "$work_dir/payments.csv" <<'CSV'
ExternalID,Amount
1,75
10,25
999,50
CSV
query='SELECT p.ExternalID, p.Amount, s.Site
FROM payments p LEFT JOIN sites s ON p.ExternalID = s.ExternalID'
for mode in default --no-leading-zeroes --no-inference; do
set --
[ "$mode" = default ] || set -- "$mode"
printf '\n%s — csvsql\n' "$mode"
csvsql "$@" --tables sites,payments --query "$query" \
csv-identifier-check.csv "$work_dir/payments.csv"
printf '\n%s — csvjoin\n' "$mode"
csvjoin "$@" --left -c ExternalID \
"$work_dir/payments.csv" csv-identifier-check.csv
done
)Check archive identifiers against a transfer log
An audiovisual inventory may use a year and a sequence separated by a period. The Digital Readiness Toolkit recommends using the identifier system already in place. In this fictional example, the archive retained two different legacy labels, 2018.0450 and 2018.045. Each points to a different physical carrier and transfer action.
Try all three ways of treating the identifier below. Watch how each transfer action pairs with the printed labels. Review unmatched carriers and log entries before publishing a view that staff may use to prepare a transfer.
| Carrier | Log identifier | Transfer action | Match |
|---|---|---|---|
Interview master 2018.0450 | 2018.0450 | Transfer from original | Exact match |
Grant meeting 2018.045 | 2018.045 | Review physical condition | Exact match |
Unreviewed tape 2018.1250 | — | — | Unmatched |
| No carrier | 2018.9999 | Check for missing carrier | Unmatched |
With both CSVs imported as text, DuckDB 2.0.1 returned four rows, including two exact matches and two unmatched records. An import as DOUBLE produced six rows. Two rows assigned a transfer action to the wrong carrier. Casting the stored numbers back to text produced the same six rows, because both distinct labels had already become the number 2018.045.
Download the carrier inventory, transfer log and DuckDB script to one directory. Run .read archive-accession-check.sql in a new DuckDB session. The SQL stores each import in a separate table so the cast-back check starts from the stored numeric values.
Check the assignments after deduplication
A numeric join returns two candidate actions for each carrier with a colliding ID. In a separate SQLite test, we kept the first action alphabetically for each original carrier ID. Both the exact join and the deduplicated numeric join produced four rows with two matched carriers. The numeric join assigned Review physical condition to both carriers. Interview master lost its expected Transfer from original action.
Check the expected action for each original text ID before accepting the migration. Include the unmatched carrier and unmatched transfer in the check so you can see whether any records disappear.
Save the Python check beside the two CSVs above. It uses Python’s built-in SQLite module and requires SQLite 3.39 or newer. Run python3 archive-count-check.py to see the counts and the incorrect assignment.