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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,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.3-beta03
github.com/stackql-labs/omnisdk v0.1.4-alpha05
github.com/stackql/any-sdk v0.6.0-alpha01
github.com/stackql/go-suffix-map v0.0.1-alpha01
github.com/stackql/psql-wire v0.1.3-beta01
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,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.3-beta03 h1:VAzd8cix45oiRLbexCEBfOGHrYsnmsvqjRcBqroW96o=
github.com/stackql-labs/omnisdk v0.1.3-beta03/go.mod h1:WzvNj/bVv53yGFsVJpYWCJC1xAEdmQSFJl9eVpkRpCY=
github.com/stackql-labs/omnisdk v0.1.4-alpha05 h1:Z22XGi99mjKPJEAAYv3qkW1J0iqqFRd9OANK+9cw4Xs=
github.com/stackql-labs/omnisdk v0.1.4-alpha05/go.mod h1:2P2vFi+c/wEeQ3kFgIt0QuIlefpuzIoTm6bPJAiLAvQ=
github.com/stackql/any-sdk v0.6.0-alpha01 h1:mqy0bmZ1wghr7mUgLyaexoRU6rsUZCWXU9Mf60fC8dg=
github.com/stackql/any-sdk v0.6.0-alpha01/go.mod h1:ahkRgwHHEn7RTmSfKrshPVk6i4X1BdHknSeBflSyRac=
github.com/stackql/go-suffix-map v0.0.1-alpha01 h1:TDUDS8bySu41Oo9p0eniUeCm43mnRM6zFEd6j6VUaz8=
Expand Down
27 changes: 27 additions & 0 deletions internal/stackql/intrinsic/dialect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package intrinsic

import (
"fmt"

"github.com/stackql-labs/omnisdk/pkg/sqlfn"
"github.com/stackql/any-sdk/pkg/constants"
"github.com/stackql/any-sdk/pkg/dto"
)

// backendDialect is the SQL dialect of the session's backend, which decides what each function a
// query calls means: the embedded SQLite's, or Postgres's. It is read from the backend's
// configuration; a backend omnisdk has no catalogue for is refused.
func backendDialect(ctx queryContext) (sqlfn.Dialect, error) {
cfg, err := dto.GetSQLBackendCfg(ctx.GetRuntimeContext().SQLBackendCfgRaw)
if err != nil {
return "", err
}
switch cfg.GetSQLDialect() {
case constants.SQLDialectSQLite3:
return sqlfn.SQLite, nil
case constants.SQLDialectPostgres:
return sqlfn.Postgres, nil
}
return "", fmt.Errorf("SQL backend %q has no omnisdk function catalogue (want %q or %q)",
cfg.GetSQLDialect(), constants.SQLDialectSQLite3, constants.SQLDialectPostgres)
}
37 changes: 37 additions & 0 deletions internal/stackql/intrinsic/dialect_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package intrinsic //nolint:testpackage // tests the unexported dialect selection

import (
"testing"

"github.com/stackql-labs/omnisdk/pkg/sqlfn"
"github.com/stackql/any-sdk/pkg/dto"
)

// backendCtx is a queryContext whose only state is the SQL backend's configuration.
type backendCtx struct {
queryContext
raw string
}

func (c backendCtx) GetRuntimeContext() dto.RuntimeCtx {
return dto.RuntimeCtx{SQLBackendCfgRaw: c.raw}
}

// The dialect is the backend's: the default and sqlite3 are SQLite, postgres is Postgres, and a
// backend with no catalogue is refused rather than given another's functions.
func TestBackendDialect(t *testing.T) {
cases := map[string]sqlfn.Dialect{
``: sqlfn.SQLite,
`{"sqlDialect": "sqlite3"}`: sqlfn.SQLite,
`{"dbEngine": "postgres_tcp", "sqlDialect": "postgres", "dsn": "postgres://u:p@h:5432/db"}`: sqlfn.Postgres,
}
for raw, want := range cases {
got, err := backendDialect(backendCtx{raw: raw})
if err != nil || got != want {
t.Errorf("%s: got %q, %v; want %q", raw, got, err, want)
}
}
if _, err := backendDialect(backendCtx{raw: `{"sqlDialect": "snowflake"}`}); err == nil {
t.Error("snowflake: want a refusal, got none")
}
}
23 changes: 19 additions & 4 deletions internal/stackql/intrinsic/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,11 @@ func docSelectFunc(
if previewCfg.getStagingEnabled() && needsStaging(node) {
return stagedSelectFunc(ctx, node, currentProvider), true
}
translated, err := translateSelect(node, currentProvider)
dialect, err := backendDialect(ctx)
if err != nil {
return refuse(err), true
}
translated, err := translateSelect(node, currentProvider, dialect)
if err != nil {
return refuse(err), true
}
Expand All @@ -190,7 +194,11 @@ func docMutationFunc(
} else if err != nil {
return refuse(err), true
}
translated, err := translateMutation(stmt, currentProvider)
dialect, err := backendDialect(ctx)
if err != nil {
return refuse(err), true
}
translated, err := translateMutation(stmt, currentProvider, dialect)
if err != nil {
return refuse(err), true
}
Expand Down Expand Up @@ -224,14 +232,21 @@ func openDocQuery(ctx queryContext, translated docQuery) (omnisdk.Rows, string,
} else {
relation = q.From()[0].Resource().Alias()
}
res, resolveErr := omnisdk.Resolve(q, tables)
dialect, dialectErr := backendDialect(ctx)
if dialectErr != nil {
return nil, "", dialectErr
}
res, resolveErr := omnisdk.ResolveIn(q, tables, dialect)
if resolveErr != nil {
return nil, "", resolveErr
}
// omnisdk takes one credential per run: the first relation's cloud - a
// mutation's target - leaving the rest to the canonical environment
// variables.
args := previewArgs(ctx, translated.getBundles()[0], res.Params())
args, argsErr := previewArgs(ctx, translated.getBundles()[0], res.Params())
if argsErr != nil {
return nil, "", argsErr
}
args.Tuning.Limit = translated.getLimit()
plan, planErr := omnisdk.NewGraphSelectQuery(registry, res.Graph(), args)
if planErr != nil {
Expand Down
7 changes: 5 additions & 2 deletions internal/stackql/intrinsic/dynamic.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,11 @@ func dynamicSelectFunc(
if err != nil {
return internaldto.NewErroneousExecutorOutput(err)
}
plan, planErr := omnisdk.NewGraphQuery(
registryRoot(ctx), graph, previewArgs(ctx, graphCloud(graph.Addresses()), params))
args, argsErr := previewArgs(ctx, graphCloud(graph.Addresses()), params)
if argsErr != nil {
return internaldto.NewErroneousExecutorOutput(argsErr)
}
plan, planErr := omnisdk.NewGraphQuery(registryRoot(ctx), graph, args)
if planErr != nil {
return internaldto.NewErroneousExecutorOutput(planErr)
}
Expand Down
8 changes: 5 additions & 3 deletions internal/stackql/intrinsic/iac.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,11 @@ func iacSelectFunc(
if err != nil {
return internaldto.NewErroneousExecutorOutput(err)
}
plan, planErr := omnisdk.Converge(
registryRoot(ctx), collection, state, runID, resources,
previewArgs(ctx, convergeCloud(resources), params))
args, argsErr := previewArgs(ctx, convergeCloud(resources), params)
if argsErr != nil {
return internaldto.NewErroneousExecutorOutput(argsErr)
}
plan, planErr := omnisdk.Converge(registryRoot(ctx), collection, state, runID, resources, args)
if planErr != nil {
return internaldto.NewErroneousExecutorOutput(planErr)
}
Expand Down
13 changes: 9 additions & 4 deletions internal/stackql/intrinsic/preview_services.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,21 @@ func streamPlan(
}

// previewArgs assembles the SDK arguments both services need: the scope left
// over after the control predicates, the credential for the cloud in play, and
// the backend tuning.
func previewArgs(ctx queryContext, cloud string, params map[string]string) omnisdk.Args {
// over after the control predicates, the credential for the cloud in play, the
// backend tuning, and the backend's SQL dialect.
func previewArgs(ctx queryContext, cloud string, params map[string]string) (omnisdk.Args, error) {
dialect, err := backendDialect(ctx)
if err != nil {
return omnisdk.Args{}, err
}
input := previewCfg
return omnisdk.Args{
Params: params,
AuthByProvider: providerAuthByProvider(ctx, cloud),
Endpoint: input.getEndpoint(),
InsecureSkipTLSVerify: input.getInsecureSkipTLSVerify(),
}
Dialect: dialect,
}, nil
}

// previewSelectFunc routes a SELECT over an extended preview relation.
Expand Down
10 changes: 8 additions & 2 deletions internal/stackql/intrinsic/staged.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

"github.com/stackql-labs/omnisdk/pkg/omnisdk"
"github.com/stackql-labs/omnisdk/pkg/query"
"github.com/stackql-labs/omnisdk/pkg/sqlfn"
"github.com/stackql/any-sdk/pkg/dto"
"github.com/stackql/stackql/internal/stackql/internal_data_transfer/internaldto"
"github.com/stackql/stackql/internal/stackql/omnistaging"
Expand Down Expand Up @@ -139,7 +140,11 @@ func stagedSelectFunc(
node *sqlparser.Select,
currentProvider string,
) func() internaldto.ExecutorOutput {
staged, err := planStagedSelect(node, currentProvider, ctx.GetASTFormatter())
dialect, err := backendDialect(ctx)
if err != nil {
return refuse(err)
}
staged, err := planStagedSelect(node, currentProvider, dialect, ctx.GetASTFormatter())
if err != nil {
return refuse(err)
}
Expand Down Expand Up @@ -248,9 +253,10 @@ func (r *stagedRefs) collectSelectExprs(
func planStagedSelect(
node *sqlparser.Select,
currentProvider string,
dialect sqlfn.Dialect,
formatter sqlparser.NodeFormatter,
) (stagedSelect, error) {
t, where, err := translateSource(node, currentProvider)
t, where, err := translateSource(node, currentProvider, dialect)
if err != nil {
return nil, err
}
Expand Down
5 changes: 3 additions & 2 deletions internal/stackql/intrinsic/staged_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"path/filepath"
"testing"

"github.com/stackql-labs/omnisdk/pkg/sqlfn"
"github.com/stackql/any-sdk/pkg/dto"
"github.com/stackql/any-sdk/public/sqlengine"
"github.com/stackql/stackql/internal/stackql/internal_data_transfer/internaldto"
Expand Down Expand Up @@ -84,7 +85,7 @@ func TestPlanStagedSelectOuterStatement(t *testing.T) {
},
}
for _, tc := range cases {
staged, err := planStagedSelect(sel, "", tc.formatter)
staged, err := planStagedSelect(sel, "", sqlfn.SQLite, tc.formatter)
if err != nil {
t.Fatal(err)
}
Expand All @@ -102,7 +103,7 @@ func TestPlanStagedSelectOuterStatement(t *testing.T) {
}
}
if _, err := planStagedSelect(parseSelect(t,
"select * from stackql_unstable_github.orgs.members order by login"), "",
"select * from stackql_unstable_github.orgs.members order by login"), "", sqlfn.SQLite,
astformat.SQLiteSelectExprsFormatter); err == nil ||
err.Error() != "'*' cannot be staged for stackql_unstable_* relations; name the columns" {
t.Fatalf("star refusal: got %v", err)
Expand Down
Loading
Loading