odbc icon indicating copy to clipboard operation
odbc copied to clipboard

wrong column type with snowflake and writeTable

Open dcaud opened this issue 4 years ago • 1 comments

DBI::dbWriteTable doesn't use "VARCHAR(16777216)" for a field.type when the input column is a long string. I'm getting this error when writing to snowflake via DBI::dbConnect(odbc::odbc()...

Error in result_insert_dataframe(rs@ptr, values, batch_rows) : 
  nanodbc/nanodbc.cpp:1655: 00000: String 'my string here ...' is too long and would be truncated 

If I manually specify field.types then all works, but I don't think I should have to do that.

dcaud avatar Jan 26 '21 21:01 dcaud

Running into this in a data pipeline, so thought I'd put some notes from researching whether there'd be a downside to VARCHAR(max). According to the snowflake docs, there's no performance or storage difference for using "VARCHAR(16777216)". Here's the relevant sections w/ emphasis below.

A column only consumes storage for the amount of actual data stored. For example, a 1-character string in a VARCHAR(16777216) column only consumes a single character.

There is no performance difference between using the full-length VARCHAR declaration VARCHAR(16777216) or a smaller size. Note that in any relational database, SELECT statements in which a WHERE clause references VARCHAR columns or string columns are not as fast as SELECT statements filtered using a date or numeric column condition. https://docs.snowflake.com/en/sql-reference/data-types-text.html

So changing to max VARCHAR should...

  • prevent errors related to string length
  • not have a performance or storage cost

machow avatar Apr 06 '21 21:04 machow

Closed in https://github.com/r-dbi/odbc/pull/599. :)

simonpcouch avatar Dec 21 '23 19:12 simonpcouch