From 82a9dd8959a0edad078e5ec3593e7af7b2129a9e Mon Sep 17 00:00:00 2001 From: Archie Wood <58074498+archiewood@users.noreply.github.com> Date: Thu, 24 Oct 2024 03:15:05 -0400 Subject: [PATCH] replacement scan for read_gsheet --- TODO.md | 18 +++++++++--------- src/gsheets_extension.cpp | 28 ++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/TODO.md b/TODO.md index 9f6d8b5..5ef46d4 100644 --- a/TODO.md +++ b/TODO.md @@ -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() diff --git a/src/gsheets_extension.cpp b/src/gsheets_extension.cpp index d1eda0d..3d33580 100644 --- a/src/gsheets_extension.cpp +++ b/src/gsheets_extension.cpp @@ -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 @@ -43,6 +47,26 @@ namespace duckdb { #include +unique_ptr ReadSheetReplacement(ClientContext &context, ReplacementScanInput &input, + optional_ptr 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(); + vector> children; + children.push_back(make_uniq(Value(table_name))); + table_function->function = make_uniq("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 @@ -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) {