Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include headers with COPY TO #28

Merged
merged 3 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# TODO

## Copy to
- [ ] header
- [x] header
- [ ] sheet
- [ ] types
- [ ] implicit copy to when it sees a gsheets url
Expand Down
24 changes: 6 additions & 18 deletions src/gsheets_copy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,12 @@ namespace duckdb
copy_to_sink = GSheetWriteSink;
}

struct GSheetCopyGlobalState : public GlobalFunctionData
{
explicit GSheetCopyGlobalState(ClientContext &context, const string &sheet_id, const string &token, const string &sheet_name)
: sheet_id(sheet_id), token(token), sheet_name(sheet_name)
{
}

public:
string sheet_id;
string token;
string sheet_name;
};

struct GSheetWriteBindData : public TableFunctionData
{
};

unique_ptr<FunctionData> GSheetCopyFunction::GSheetWriteBind(ClientContext &context, CopyFunctionBindInput &input, const vector<string> &names, const vector<LogicalType> &sql_types)
{
return make_uniq<GSheetWriteBindData>();
string file_path = input.info.file_path;

return make_uniq<GSheetWriteBindData>(file_path, sql_types, names);
}

unique_ptr<GlobalFunctionData> GSheetCopyFunction::GSheetWriteInitializeGlobal(ClientContext &context, FunctionData &bind_data, const string &file_path)
Expand Down Expand Up @@ -96,9 +82,11 @@ namespace duckdb
sheet_data["range"] = "Sheet1";
sheet_data["majorDimension"] = "ROWS";

// TODO: Add column headers
vector<string> headers = bind_data_p.Cast<GSheetWriteBindData>().options.name_list;

vector<vector<string>> values;
values.push_back(headers);

for (idx_t r = 0; r < input.size(); r++)
{
vector<string> row;
Expand Down
31 changes: 31 additions & 0 deletions src/include/gsheets_copy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,37 @@

namespace duckdb
{
struct GSheetCopyGlobalState : public GlobalFunctionData
{
explicit GSheetCopyGlobalState(ClientContext &context, const string &sheet_id, const string &token, const string &sheet_name)
: sheet_id(sheet_id), token(token), sheet_name(sheet_name)
{
}

public:
string sheet_id;
string token;
string sheet_name;
};

struct GSheetWriteOptions
{
vector<string> name_list;
};

struct GSheetWriteBindData : public TableFunctionData
{
vector<string> files;
GSheetWriteOptions options;
vector<LogicalType> sql_types;

GSheetWriteBindData(string file_path, vector<LogicalType> sql_types, vector<string> names)
: sql_types(std::move(sql_types))
{
files.push_back(std::move(file_path));
options.name_list = std::move(names);
}
};

class GSheetCopyFunction : public CopyFunction
{
Expand Down
35 changes: 35 additions & 0 deletions test/sql/copyto.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# name: test/sql/copyto.test
# description: test COPY TO function
# group: [gsheets]

require-env TOKEN

require gsheets

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

# Create a table to copy to Google Sheet
statement ok
create table spreadsheets as
select 'Microsoft' as company, 'Excel' as product, 1985 as year_founded
union all
select 'Google', 'Google Sheets', 2006
union all
select 'Apple', 'Numbers', 1984
union all
select 'LibreOffice', 'Calc', 2000;

# Copy the table to Google Sheet
statement ok
copy spreadsheets to 'https://docs.google.com/spreadsheets/d/11QdEasMWbETbFVxry-SsD8jVcdYIT1zBQszcF84MdE8/edit#gid=0' (format gsheet);

# Read the table from Google Sheet
query III
from read_gsheet('https://docs.google.com/spreadsheets/d/11QdEasMWbETbFVxry-SsD8jVcdYIT1zBQszcF84MdE8/edit#gid=0');
----
Microsoft Excel 1985
Google Google Sheets 2006
Apple Numbers 1984
LibreOffice Calc 2000
Loading