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

Replacement scan for read_gsheet #16

Merged
merged 1 commit into from
Oct 24, 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
18 changes: 9 additions & 9 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# TODO

## Copy to
- header
- sheet
- types
- implicit copy to when it sees a gsheets url
- warn when more than 2048 rows
- support > 2048 rows
- [ ] header
- [ ] sheet
- [ ] types
- [ ] implicit copy to when it sees a gsheets url
- [ ] warn when more than 2048 rows
- [ ] support > 2048 rows

## read_gsheet()
- types
- large sheets
- implicit read_gsheet when it sees a gsheets url
- [ ] types
- [x] large sheets
- [x] implicit read_gsheet when it sees a gsheets url

## Tests
- Tests for read_gsheet()
Expand Down
28 changes: 28 additions & 0 deletions src/gsheets_extension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
#include "duckdb.hpp"
#include "duckdb/main/extension_util.hpp"
#include "duckdb/function/table_function.hpp"
#include "duckdb/main/config.hpp"
#include "duckdb/parser/expression/constant_expression.hpp"
#include "duckdb/parser/expression/function_expression.hpp"
#include "duckdb/parser/tableref/table_function_ref.hpp"


// Standard library
Expand Down Expand Up @@ -43,6 +47,26 @@ namespace duckdb {
#include <algorithm>


unique_ptr<TableRef> ReadSheetReplacement(ClientContext &context, ReplacementScanInput &input,
optional_ptr<ReplacementScanData> data) {
auto table_name = ReplacementScan::GetFullPath(input);
if (!StringUtil::StartsWith(table_name, "https://docs.google.com/spreadsheets/d/")) {
return nullptr;
}
auto table_function = make_uniq<TableFunctionRef>();
vector<unique_ptr<ParsedExpression>> children;
children.push_back(make_uniq<ConstantExpression>(Value(table_name)));
table_function->function = make_uniq<FunctionExpression>("read_gsheet", std::move(children));

if (!FileSystem::HasGlob(table_name)) {
auto &fs = FileSystem::GetFileSystem(context);
table_function->alias = fs.ExtractBaseName(table_name);
}

return std::move(table_function);
}



static void LoadInternal(DatabaseInstance &instance) {
// Initialize OpenSSL
Expand All @@ -63,6 +87,10 @@ static void LoadInternal(DatabaseInstance &instance) {

// Register Secret functions
CreateGsheetSecretFunctions::Register(instance);

// Register replacement scan for read_gsheet
auto &config = DBConfig::GetConfig(instance);
config.replacement_scans.emplace_back(ReadSheetReplacement);
}

void GsheetsExtension::Load(DuckDB &db) {
Expand Down
Loading