Skip to content

Commit

Permalink
replacement scan for read_gsheet
Browse files Browse the repository at this point in the history
  • Loading branch information
archiewood committed Oct 24, 2024
1 parent 7d02427 commit 82a9dd8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
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

0 comments on commit 82a9dd8

Please sign in to comment.