Skip to content
Closed
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
96 changes: 96 additions & 0 deletions docs/preview.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,99 @@ source cicd/vol/vendor-secrets/secrets.sh
);" | perl -MTime::HiRes=time -pe 'printf "%.3f ", time'

```

## Recursive CTEs over provider queries

Use `WITH RECURSIVE` with an anchor and one recursive term joined by
`UNION` or `UNION ALL`. The recursive term must reference its own CTE once.
`UNION` removes duplicate rows across iterations. `UNION ALL` retains them.
Multiple recursive definitions can appear alongside ordinary CTEs. Definitions
can read earlier CTEs. Mutual recursion is unsupported.

```sql
WITH RECURSIVE nums(n) AS (
SELECT 1 AS n
UNION ALL
SELECT n + 1 AS n FROM nums WHERE n < 4
)
SELECT n FROM nums;
```

A run allows up to 1,024 recursive iterations and examines at most 100,000
candidate rows, including duplicates. The whole main query waits for
recursion by default. When reads and effects can safely overlap, the
session can allow independent main-query work to start sooner.

```sql
SELECT $recursive_allow_independent_work AS enabled;
SET recursive_allow_independent_work = true;
UNSET recursive_allow_independent_work;
```

### Traverse Google folders and list buckets by project

Install the Google provider and use a credential with permission to list
folders, projects, and buckets in the target scope. Replace
`organizations/123` with your organization resource name. Use
`folders/123` as the seed to restrict the same query to a folder subtree.
The result includes buckets in projects directly under the seed and under
every reachable subfolder.

```sql
WITH RECURSIVE containers(parent_name) AS (
SELECT 'organizations/123' AS parent_name
UNION
SELECT f.name
FROM containers AS c
JOIN google.cloudresourcemanager.folders AS f ON f.parent = c.parent_name
), project_inventory AS (
SELECT c.parent_name, p.projectId
FROM containers AS c
JOIN google.cloudresourcemanager.projects AS p ON p.parent = c.parent_name
)
SELECT p.parent_name, p.projectId, b.name AS bucket_name
FROM project_inventory AS p
JOIN google.storage.buckets AS b ON b.project = p.projectId;
```

For AWS Organizations, configure a provider alias for each organization
used for account discovery. Configure an alias or role for each account
used for bucket listing. A SQL row containing an account ID does not
dynamically select a provider alias. Queries can combine explicitly named
account aliases with `UNION ALL`.

### Inventory buckets across two AWS organizations

Configure `org_a` and `org_b` with each organization's discovery credentials
and the Organizations request region. Configure `member_a` and `member_b`
with credentials for the two member accounts being inventoried. Substitute
the real root IDs and account IDs below. This example traverses all OUs in
each organization and lists buckets for the two explicitly bound accounts.
To cover more accounts, add a member alias and bucket branch for each one.

```sql
WITH RECURSIVE office_a(parent_id) AS (
SELECT 'r-aaaa' AS parent_id
UNION
SELECT u.Id FROM office_a AS p
JOIN org_a.organizations.organizational_units_for_parents AS u ON u.ParentId = p.parent_id
), office_b(parent_id) AS (
SELECT 'r-bbbb' AS parent_id
UNION
SELECT u.Id FROM office_b AS p
JOIN org_b.organizations.organizational_units_for_parents AS u ON u.ParentId = p.parent_id
), accounts_a AS (
SELECT a.Id AS account_id FROM office_a AS p
JOIN org_a.organizations.accounts_for_parents AS a ON a.ParentId = p.parent_id
), accounts_b AS (
SELECT a.Id AS account_id FROM office_b AS p
JOIN org_b.organizations.accounts_for_parents AS a ON a.ParentId = p.parent_id
)
SELECT 'org_a' AS org_alias, a.account_id, b.name AS bucket_name
FROM accounts_a AS a CROSS JOIN member_a.s3.buckets AS b
WHERE a.account_id = '111111111111' AND b.region = 'us-east-1'
UNION ALL
SELECT 'org_b' AS org_alias, a.account_id, b.name AS bucket_name
FROM accounts_b AS a CROSS JOIN member_b.s3.buckets AS b
WHERE a.account_id = '222222222222' AND b.region = 'us-east-1';
```
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ require (
github.com/spf13/cobra v1.10.2
github.com/spf13/pflag v1.0.10
github.com/spf13/viper v1.10.1
github.com/stackql-labs/omnisdk v0.1.15-beta04
github.com/stackql-labs/omnisdk v0.1.16-alpha01
github.com/stackql/go-suffix-map v0.0.1-alpha02
github.com/stackql/psql-wire v0.1.2-beta02
github.com/stackql/stackql-parser v0.0.17-alpha05
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,8 @@ github.com/spf13/viper v1.10.1 h1:nuJZuYpG7gTj/XqiUwg8bA0cp1+M2mC3J4g5luUYBKk=
github.com/spf13/viper v1.10.1/go.mod h1:IGlFPqhNAPKRxohIzWpI5QEy4kuI7tcl5WvR+8qy1rU=
github.com/spiffe/go-spiffe/v2 v2.7.0 h1:uXe1MflJoHw58wAUvxVlcM7WpKtijWG7I1UidcGh6g4=
github.com/spiffe/go-spiffe/v2 v2.7.0/go.mod h1:47Q0Q9/AqGha8QLHp+kxpH4Wca7X7EnOtlIJy3mxZ3U=
github.com/stackql-labs/omnisdk v0.1.15-beta04 h1:n6ad3JjC5fZx4LMp0+ejEFtqLps+YfAmfcyd1xuqSwQ=
github.com/stackql-labs/omnisdk v0.1.15-beta04/go.mod h1:76RVs6Uq5WIzFzsskvFiOToyxkELPbdNABFNgDR90d4=
github.com/stackql-labs/omnisdk v0.1.16-alpha01 h1:6x5RCF93NJf9+2VSY9vMGZ52RD+JwffeGrvNDbKI9RI=
github.com/stackql-labs/omnisdk v0.1.16-alpha01/go.mod h1:76RVs6Uq5WIzFzsskvFiOToyxkELPbdNABFNgDR90d4=
github.com/stackql/go-suffix-map v0.0.1-alpha02 h1:JI4aiq/OfHW4a/BiNqK31NbyaR94zlUOhGZof3G/OUo=
github.com/stackql/go-suffix-map v0.0.1-alpha02/go.mod h1:6BSMz4K/gIYfqdVRb7V8xL165wiPYyCjD9Kx7hLMd9Y=
github.com/stackql/psql-wire v0.1.2-beta02 h1:7DfliBpyyCaUHVJ1Jxk37q3zVwVLyh6+FCHeM0/gbAQ=
Expand Down
24 changes: 22 additions & 2 deletions internal/stackql/handler/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,12 @@ func (hc *standardHandlerContext) ResolveVariables(statement sqlparser.Statement
resolved := map[string]sqlparser.Expr{}
secretValues := map[string][]byte{}
for name, path := range paths {
if value, exists := state.variables[strings.TrimPrefix(path, "variables.")]; exists {
key := strings.TrimPrefix(path, "variables.")
value, exists := state.variables[key]
if path == "variables."+recursiveIndependentVariable && !exists {
value, exists = false, true
}
if exists {
literal, err := scalarVariableLiteral(value)
if err != nil {
return err
Expand All @@ -99,7 +104,7 @@ func (hc *standardHandlerContext) ResolveVariables(statement sqlparser.Statement
continue
}
parts := strings.Split(path, ".")
var value any = values
value = values
if parts[0] == "secrets" {
if len(parts) < 2 || state.store == nil {
return fmt.Errorf("secret variable is unavailable")
Expand Down Expand Up @@ -207,6 +212,11 @@ func (hc *standardHandlerContext) setSessionVariable(path string, rhs any) error
if err := decoder.Decode(&value); err != nil {
return err
}
if name == recursiveIndependentVariable {
if _, boolean := value.(bool); !boolean {
return fmt.Errorf("%s requires a boolean", name)
}
}
if _, err := scalarVariableLiteral(value); err != nil {
return err
}
Expand All @@ -216,3 +226,13 @@ func (hc *standardHandlerContext) setSessionVariable(path string, rhs any) error
state.variables[name] = value
return nil
}

const recursiveIndependentVariable = "recursive_allow_independent_work"

func (hc *standardHandlerContext) RecursiveIndependentWork() bool {
state := hc.providerSessionState()
state.mu.Lock()
defer state.mu.Unlock()
enabled, _ := state.variables[recursiveIndependentVariable].(bool)
return enabled
}
3 changes: 3 additions & 0 deletions internal/stackql/intrinsic/intrinsic.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ func GenerateStreamFunc(
return execute, true
}
if node, isSelect := stmt.(*sqlparser.Select); isSelect {
if recursiveClause(node) != nil {
return statementReadFunc(ctx, node), true
}
if action, handled := identitySelect(ctx, node); handled {
return action, true
}
Expand Down
Loading
Loading