forked from duckdb/duckdb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request duckdb#11726 from motherduckdb/which_secret_tf
feat: rewrite which_secret() into a table function
- Loading branch information
Showing
13 changed files
with
154 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
From e8e6c286376d97e0a695284fc32b3b67a77e35af Mon Sep 17 00:00:00 2001 | ||
From: stephaniewang <qw79@cornell.edu> | ||
Date: Thu, 18 Apr 2024 21:41:29 -0400 | ||
Subject: [PATCH] update tests | ||
|
||
--- | ||
test/sql/aws_minio_secret.test | 2 +- | ||
test/sql/aws_secret_gcs.test | 2 +- | ||
test/sql/aws_secret_r2.test | 2 +- | ||
3 files changed, 3 insertions(+), 3 deletions(-) | ||
|
||
diff --git a/test/sql/aws_minio_secret.test b/test/sql/aws_minio_secret.test | ||
index 2ddc29f..34c4c92 100644 | ||
--- a/test/sql/aws_minio_secret.test | ||
+++ b/test/sql/aws_minio_secret.test | ||
@@ -28,7 +28,7 @@ CREATE SECRET my_aws_secret ( | ||
); | ||
|
||
query I | ||
-SELECT which_secret('s3://test-bucket/aws_minio_secret/secret1/test.csv', 's3') | ||
+SELECT name FROM which_secret('s3://test-bucket/aws_minio_secret/secret1/test.csv', 's3') | ||
---- | ||
my_aws_secret | ||
|
||
diff --git a/test/sql/aws_secret_gcs.test b/test/sql/aws_secret_gcs.test | ||
index 0b1fd40..cbed048 100644 | ||
--- a/test/sql/aws_secret_gcs.test | ||
+++ b/test/sql/aws_secret_gcs.test | ||
@@ -18,7 +18,7 @@ CREATE SECRET s1 ( | ||
); | ||
|
||
query I | ||
-SELECT which_secret('gcs://haha/hoehoe.parkoe', 'gcs') | ||
+SELECT name FROM which_secret('gcs://haha/hoehoe.parkoe', 'gcs') | ||
---- | ||
s1 | ||
|
||
diff --git a/test/sql/aws_secret_r2.test b/test/sql/aws_secret_r2.test | ||
index 01be38b..19ebd1e 100644 | ||
--- a/test/sql/aws_secret_r2.test | ||
+++ b/test/sql/aws_secret_r2.test | ||
@@ -19,7 +19,7 @@ CREATE SECRET s1 ( | ||
); | ||
|
||
query I | ||
-SELECT which_secret('r2://haha/hoehoe.parkoe', 'r2') | ||
+SELECT name FROM which_secret('r2://haha/hoehoe.parkoe', 'r2') | ||
---- | ||
s1 | ||
|
||
-- | ||
2.39.2 (Apple Git-143) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,6 @@ | |
'math', | ||
'operators', | ||
'random', | ||
'secret', | ||
'string', | ||
'debug', | ||
'struct', | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#include "duckdb/function/table/system_functions.hpp" | ||
|
||
#include "duckdb/common/file_system.hpp" | ||
#include "duckdb/common/map.hpp" | ||
#include "duckdb/common/string_util.hpp" | ||
#include "duckdb/common/multi_file_reader.hpp" | ||
#include "duckdb/function/function_set.hpp" | ||
#include "duckdb/main/client_context.hpp" | ||
#include "duckdb/main/database.hpp" | ||
#include "duckdb/main/extension_helper.hpp" | ||
#include "duckdb/main/secret/secret_manager.hpp" | ||
|
||
namespace duckdb { | ||
|
||
struct DuckDBWhichSecretData : public GlobalTableFunctionState { | ||
DuckDBWhichSecretData() : finished(false) { | ||
} | ||
bool finished; | ||
}; | ||
|
||
struct DuckDBWhichSecretBindData : public TableFunctionData { | ||
explicit DuckDBWhichSecretBindData(TableFunctionBindInput &tf_input) : inputs(tf_input.inputs) {}; | ||
|
||
duckdb::vector<duckdb::Value> inputs; | ||
}; | ||
|
||
static unique_ptr<FunctionData> DuckDBWhichSecretBind(ClientContext &context, TableFunctionBindInput &input, | ||
vector<LogicalType> &return_types, vector<string> &names) { | ||
names.emplace_back("name"); | ||
return_types.emplace_back(LogicalType::VARCHAR); | ||
|
||
names.emplace_back("persistent"); | ||
return_types.emplace_back(LogicalType::VARCHAR); | ||
|
||
names.emplace_back("storage"); | ||
return_types.emplace_back(LogicalType::VARCHAR); | ||
|
||
return make_uniq<DuckDBWhichSecretBindData>(input); | ||
} | ||
|
||
unique_ptr<GlobalTableFunctionState> DuckDBWhichSecretInit(ClientContext &context, TableFunctionInitInput &input) { | ||
return make_uniq<DuckDBWhichSecretData>(); | ||
} | ||
|
||
void DuckDBWhichSecretFunction(ClientContext &context, TableFunctionInput &data_p, DataChunk &output) { | ||
auto &data = data_p.global_state->Cast<DuckDBWhichSecretData>(); | ||
if (data.finished) { | ||
// finished returning values | ||
return; | ||
} | ||
auto &bind_data = data_p.bind_data->Cast<DuckDBWhichSecretBindData>(); | ||
|
||
auto &secret_manager = SecretManager::Get(context); | ||
auto transaction = CatalogTransaction::GetSystemCatalogTransaction(context); | ||
|
||
auto &inputs = bind_data.inputs; | ||
auto path = inputs[0].ToString(); | ||
auto type = inputs[1].ToString(); | ||
auto secret_match = secret_manager.LookupSecret(transaction, path, type); | ||
if (secret_match.HasMatch()) { | ||
auto &secret_entry = *secret_match.secret_entry; | ||
output.SetCardinality(1); | ||
output.SetValue(0, 0, secret_entry.secret->GetName()); | ||
output.SetValue(1, 0, EnumUtil::ToString(secret_entry.persist_type)); | ||
output.SetValue(2, 0, secret_entry.storage_mode); | ||
} | ||
data.finished = true; | ||
} | ||
|
||
void DuckDBWhichSecretFun::RegisterFunction(BuiltinFunctions &set) { | ||
set.AddFunction(TableFunction("which_secret", {duckdb::LogicalType::VARCHAR, duckdb::LogicalType::VARCHAR}, | ||
DuckDBWhichSecretFunction, DuckDBWhichSecretBind, DuckDBWhichSecretInit)); | ||
} | ||
|
||
} // namespace duckdb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters