-
Notifications
You must be signed in to change notification settings - Fork 125
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
model executor for s3/gcs/azure to duckdb #6353
Open
k-anshul
wants to merge
4
commits into
main
Choose a base branch
from
s3_duckdb_model
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
167 changes: 167 additions & 0 deletions
167
runtime/drivers/duckdb/model_executor_objectstore_self.go
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,167 @@ | ||
package duckdb | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"maps" | ||
"strings" | ||
|
||
"github.com/mitchellh/mapstructure" | ||
"github.com/rilldata/rill/runtime/drivers" | ||
"github.com/rilldata/rill/runtime/drivers/azure" | ||
"github.com/rilldata/rill/runtime/drivers/gcs" | ||
"github.com/rilldata/rill/runtime/drivers/s3" | ||
"github.com/rilldata/rill/runtime/pkg/fileutil" | ||
) | ||
|
||
type s3InputProps struct { | ||
Path string `mapstructure:"path"` | ||
Format drivers.FileFormat `mapstructure:"format"` | ||
DuckDB map[string]any `mapstructure:"duckdb"` | ||
} | ||
|
||
func (p *s3InputProps) Validate() error { | ||
if p.Path == "" { | ||
return fmt.Errorf("missing property `path`") | ||
} | ||
return nil | ||
} | ||
|
||
type objectStoreToSelfExecutor struct { | ||
c *connection | ||
} | ||
|
||
var _ drivers.ModelExecutor = &objectStoreToSelfExecutor{} | ||
|
||
func (e *objectStoreToSelfExecutor) Concurrency(desired int) (int, bool) { | ||
if desired > 1 { | ||
return 0, false | ||
} | ||
return 1, true | ||
} | ||
|
||
func (e *objectStoreToSelfExecutor) Execute(ctx context.Context, opts *drivers.ModelExecuteOptions) (*drivers.ModelResult, error) { | ||
// Build the model executor options with updated input properties | ||
clone := *opts | ||
newInputProps, err := e.modelInputProperties(opts.ModelName, opts.InputConnector, opts.InputHandle, opts.InputProperties) | ||
if err != nil { | ||
return nil, err | ||
} | ||
clone.InputProperties = newInputProps | ||
newOpts := &clone | ||
|
||
// execute | ||
executor := &selfToSelfExecutor{c: e.c} | ||
return executor.Execute(ctx, newOpts) | ||
} | ||
|
||
func (e *objectStoreToSelfExecutor) modelInputProperties(model, inputConnector string, inputHandle drivers.Handle, inputProps map[string]any) (map[string]any, error) { | ||
parsed := &s3InputProps{} | ||
if err := mapstructure.WeakDecode(inputProps, parsed); err != nil { | ||
return nil, fmt.Errorf("failed to parse input properties: %w", err) | ||
} | ||
if err := parsed.Validate(); err != nil { | ||
return nil, fmt.Errorf("invalid input properties: %w", err) | ||
} | ||
|
||
m := &ModelInputProperties{} | ||
var format string | ||
if parsed.Format != "" { | ||
format = fmt.Sprintf(".%s", parsed.Format) | ||
} else { | ||
format = fileutil.FullExt(parsed.Path) | ||
} | ||
|
||
config := inputHandle.Config() | ||
// config properties can also be set as input properties | ||
maps.Copy(config, inputProps) | ||
|
||
// Generate secret SQL to access the service and set as pre_exec_query | ||
safeSecretName := safeName(fmt.Sprintf("%s__%s__secret", model, inputConnector)) | ||
switch inputHandle.Driver() { | ||
case "s3": | ||
s3Config := &s3.ConfigProperties{} | ||
err := mapstructure.WeakDecode(config, s3Config) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to parse s3 config properties: %w", err) | ||
} | ||
var sb strings.Builder | ||
sb.WriteString("CREATE OR REPLACE TEMPORARY SECRET ") | ||
sb.WriteString(safeSecretName) | ||
sb.WriteString(" (TYPE S3") | ||
if s3Config.AllowHostAccess { | ||
sb.WriteString(", PROVIDER CREDENTIAL_CHAIN") | ||
} | ||
if s3Config.AccessKeyID != "" { | ||
fmt.Fprintf(&sb, ", KEY_ID %s, SECRET %s", safeSQLString(s3Config.AccessKeyID), safeSQLString(s3Config.SecretAccessKey)) | ||
} | ||
if s3Config.SessionToken != "" { | ||
fmt.Fprintf(&sb, ", SESSION_TOKEN %s", safeSQLString(s3Config.SessionToken)) | ||
} | ||
if s3Config.Endpoint != "" { | ||
sb.WriteString(", ENDPOINT ") | ||
sb.WriteString(safeSQLString(s3Config.Endpoint)) | ||
} | ||
if s3Config.Region != "" { | ||
sb.WriteString(", REGION ") | ||
sb.WriteString(safeSQLString(s3Config.Region)) | ||
} | ||
sb.WriteRune(')') | ||
m.PreExec = sb.String() | ||
case "gcs": | ||
// GCS works via S3 compatibility mode | ||
gcsConfig := &gcs.ConfigProperties{} | ||
err := mapstructure.WeakDecode(config, gcsConfig) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to parse s3 config properties: %w", err) | ||
} | ||
var sb strings.Builder | ||
sb.WriteString("CREATE OR REPLACE TEMPORARY SECRET ") | ||
sb.WriteString(safeSecretName) | ||
sb.WriteString(" (TYPE GCS") | ||
if gcsConfig.AllowHostAccess { | ||
sb.WriteString(", PROVIDER CREDENTIAL_CHAIN") | ||
} | ||
if gcsConfig.KeyID != "" { | ||
fmt.Fprintf(&sb, ", KEY_ID %s, SECRET %s", safeSQLString(gcsConfig.KeyID), safeSQLString(gcsConfig.Secret)) | ||
} | ||
sb.WriteRune(')') | ||
m.PreExec = sb.String() | ||
case "azure": | ||
azureConfig := &azure.ConfigProperties{} | ||
err := mapstructure.WeakDecode(config, azureConfig) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to parse s3 config properties: %w", err) | ||
} | ||
var sb strings.Builder | ||
sb.WriteString("CREATE OR REPLACE TEMPORARY SECRET ") | ||
sb.WriteString(safeSecretName) | ||
sb.WriteString(" (TYPE AZURE") | ||
if azureConfig.AllowHostAccess { | ||
sb.WriteString(", PROVIDER CREDENTIAL_CHAIN") | ||
} | ||
if azureConfig.ConnectionString != "" { | ||
fmt.Fprintf(&sb, ", CONNECTION_STRING %s", safeSQLString(azureConfig.ConnectionString)) | ||
} | ||
if azureConfig.Account != "" { | ||
fmt.Fprintf(&sb, ", ACCOUNT_NAME %s", safeSQLString(azureConfig.Account)) | ||
} | ||
sb.WriteRune(')') | ||
m.PreExec = sb.String() | ||
default: | ||
return nil, fmt.Errorf("internal error: unsupported object store: %s", inputHandle.Driver()) | ||
} | ||
|
||
// Set SQL to read from the external source | ||
from, err := sourceReader([]string{parsed.Path}, format, parsed.DuckDB) | ||
if err != nil { | ||
return nil, err | ||
} | ||
m.SQL = "SELECT * FROM " + from | ||
|
||
propsMap := make(map[string]any) | ||
if err := mapstructure.Decode(m, &propsMap); err != nil { | ||
return nil, err | ||
} | ||
return propsMap, nil | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gcsConfig.SecretJSON
is set, but notgcsConfig.KeyID
, should we perhaps return an error here?rill env configure
to requestkey_id
andsecret
instead ofgoogle_application_credentials
for GCS? I guess that would be more appropriate now, right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.