Skip to content

Commit

Permalink
Merge pull request #41 from evidence-dev/once-again-into-the-types-de…
Browse files Browse the repository at this point in the history
…ar-friends

Fix types
  • Loading branch information
archiewood authored Nov 3, 2024
2 parents 843d261 + 01bdf28 commit f51bf5b
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 46 deletions.
23 changes: 0 additions & 23 deletions TODO.md

This file was deleted.

6 changes: 5 additions & 1 deletion docs/pages/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ INSTALL gsheets FROM community;
LOAD gsheets;
```

The latest version of DuckDB (currently 1.1.2) is required.
The latest version of [DuckDB](https://duckdb.org/docs/installation) (currently 1.1.2) is required.

## Usage

Expand Down Expand Up @@ -109,3 +109,7 @@ This token will periodically expire - you can re-run the above command again to
- Reading sheets where data does not start in A1 is not yet supported.
- Writing data to a sheet starting from a cell other than A1 is not yet supported.
- Sheets must already exist to COPY TO them.

## Support

If you are having problems, find a bug, or have an idea for an improvement, please [file an issue on GitHub](https://github.com/evidence-dev/duckdb_gsheets).
24 changes: 5 additions & 19 deletions src/gsheets_copy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,25 +103,11 @@ namespace duckdb
for (idx_t c = 0; c < input.ColumnCount(); c++)
{
auto &col = input.data[c];
switch (col.GetType().id()) {
case LogicalTypeId::VARCHAR:
row.push_back(FlatVector::GetData<string_t>(col)[r].GetString());
break;
case LogicalTypeId::INTEGER:
row.push_back(to_string(FlatVector::GetData<int32_t>(col)[r]));
break;
case LogicalTypeId::BIGINT:
row.push_back(to_string(FlatVector::GetData<int64_t>(col)[r]));
break;
case LogicalTypeId::DOUBLE:
row.push_back(to_string(FlatVector::GetData<double>(col)[r]));
break;
case LogicalTypeId::BOOLEAN:
row.push_back(FlatVector::GetData<bool>(col)[r] ? "TRUE" : "FALSE");
break;
default:
row.push_back("Type not implemented");
break;
Value val = col.GetValue(r);
if (val.IsNull()) {
row.push_back("");
} else {
row.push_back(val.ToString());
}
}
values.push_back(row);
Expand Down
20 changes: 18 additions & 2 deletions src/gsheets_read.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,22 @@ ReadSheetBindData::ReadSheetBindData(string spreadsheet_id, string token, bool h
response = call_sheets_api(spreadsheet_id, token, sheet_name, HttpMethod::GET);
}

bool IsValidNumber(const string& value) {
// Skip empty strings
if (value.empty()) {
return false;
}

try {
// Try to parse as double
size_t processed;
std::stod(value, &processed);
// Ensure the entire string was processed
return processed == value.length();
} catch (...) {
return false;
}
}

void ReadSheetFunction(ClientContext &context, TableFunctionInput &data_p, DataChunk &output) {
auto &bind_data = const_cast<ReadSheetBindData&>(data_p.bind_data->Cast<ReadSheetBindData>());
Expand All @@ -40,7 +56,7 @@ void ReadSheetFunction(ClientContext &context, TableFunctionInput &data_p, DataC
const string& value = first_data_row[col];
if (value == "true" || value == "false") {
column_types[col] = LogicalType::BOOLEAN;
} else if (value.find_first_not_of("0123456789.+-eE") == string::npos) {
} else if (IsValidNumber(value)) {
column_types[col] = LogicalType::DOUBLE;
}
}
Expand Down Expand Up @@ -155,7 +171,7 @@ unique_ptr<FunctionData> ReadSheetBind(ClientContext &context, TableFunctionBind
const string& value = first_data_row[i];
if (value == "true" || value == "false") {
return_types.push_back(LogicalType::BOOLEAN);
} else if (value.find_first_not_of("0123456789.+-eE") == string::npos) {
} else if (IsValidNumber(value)) {
return_types.push_back(LogicalType::DOUBLE);
} else {
return_types.push_back(LogicalType::VARCHAR);
Expand Down
2 changes: 1 addition & 1 deletion src/gsheets_requests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ namespace duckdb

if (method == HttpMethod::POST) {
path += ":append";
path += "?valueInputOption=RAW";
path += "?valueInputOption=USER_ENTERED";
}

return perform_https_request(host, path, token, method, body);
Expand Down
52 changes: 52 additions & 0 deletions test/sql/types.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# name: test/sql/types.test
# description: test types
# group: [gsheets]

require-env TOKEN

# Require statement will ensure this test is run with this extension loaded
require gsheets

# Create a secret NB must substitute a token, do not commit!
statement ok
create secret test_secret (
type gsheet,
provider access_token,
token '${TOKEN}'
);

# Test the easy types
# TODO: add the other types
# bool,tinyint,smallint,int,bigint,hugeint,uhugeint,utinyint,usmallint,uint,ubigint,varint,date,time,timestamp,timestamp_s,timestamp_ms,timestamp_ns,time_tz,timestamp_tz,float,double,dec_4_1,dec_9_4,dec_18_6,dec38_10,uuid,interval,varchar,blob,bit,small_enum,medium_enum,large_enum,int_array,double_array,date_array,timestamp_array,timestamptz_array,varchar_array,nested_int_array,struct,struct_of_arrays,array_of_structs,map,union,fixed_int_array,fixed_varchar_array,fixed_nested_int_array,fixed_nested_varchar_array,fixed_struct_array,struct_of_fixed_array,fixed_array_of_int_list,list_of_fixed_int_array
statement ok
copy (select
bool,
tinyint,
smallint,
int,
bigint,
hugeint,
uhugeint,
utinyint,
usmallint,
uint,
ubigint,
varint,
date,
time,
timestamp,
timestamp_s,
timestamp_ms,
timestamp_ns,
time_tz,
timestamp_tz,
float,
double
from test_all_types()) to 'https://docs.google.com/spreadsheets/d/11QdEasMWbETbFVxry-SsD8jVcdYIT1zBQszcF84MdE8/edit?gid=1295634987#gid=1295634987' (format gsheet);

# Read the types back
query IIIIIIIIIIIIIIIIIIIIII
from 'https://docs.google.com/spreadsheets/d/11QdEasMWbETbFVxry-SsD8jVcdYIT1zBQszcF84MdE8/edit?gid=1295634987#gid=1295634987';
----
FALSE -128.0 -32768.0 -2147483648.0 -9.22337e+18 -1.70141e+38 0.0 0.0 0.0 0.0 0.0 -1.79769e+308 5877642-06-25 (BC) 0:00:00 290309-12-22 (BC) 00:00:00 290309-12-22 (BC) 00:00:00 290309-12-22 (BC) 00:00:00 1677-09-22 0:00:00 00:00:00+15:59:59 290309-12-22 (BC) 00:00:00+00 -3.4e+38 -1.80E+308
TRUE 127.0 32767.0 2147483647.0 9.223372036854776e+18 1.7014118346046923e+38 3.402823669209385e+38 255.0 65535.0 4294967295.0 1.8446744073709552e+19 1.7976931348623157e+308 5881580-07-10 24:00:00 294247-01-10 04:00:54.775806 294247-01-10 04:00:54 294247-01-10 04:00:54.775 2262-04-11 23:47:17 24:00:00-15:59:59 294247-01-10 04:00:54.775806+00 3.4e+38 1.80E+308

0 comments on commit f51bf5b

Please sign in to comment.