Skip to content
Merged
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
15 changes: 15 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,21 @@
"json"
],
},
//
{
"name": "SELECT using alias... ;",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${workspaceFolder}/stackql",
"args": [
"exec",
"--auth={ \"google\": { \"keyfilepath\": \"${workspaceFolder}/keys/sa-key.json\" }, \"okta\": { \"keyfilepath\": \"${workspaceFolder}/keys/okta-token.txt\", \"keyfiletype\": \"api_key\" } }",
"select apps.id from okta.application.apps apps where subdomain = 'dev-79923018-admin';",
"-o",
"json"
],
},
{
"name": "Launch exec \"INSERT /*+ AWAIT */ logging...buckets ... ;\" -- prebuilt with cmake",
"type": "go",
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,8 @@ github.com/stackql/go-openapistackql v0.0.3-alpha8 h1:zxrnK5D7mQWVwO3sxtcHisI4D6
github.com/stackql/go-openapistackql v0.0.3-alpha8/go.mod h1:ficis807+HOpLcS6fLpPbsYY5pOsn9RuPeP0UlOqNQQ=
github.com/stackql/go-openapistackql v0.0.3 h1:/Nc7R8B6Jl/eP/eNqngbMUx8MYfxx0eIIQftDRclAbY=
github.com/stackql/go-openapistackql v0.0.3/go.mod h1:ficis807+HOpLcS6fLpPbsYY5pOsn9RuPeP0UlOqNQQ=
github.com/stackql/go-openapistackql v0.0.4-alpha1 h1:8WUV4eo+vTJSynCOXSZUFfLW4KBF5pCAgP+kxWVrGA8=
github.com/stackql/go-openapistackql v0.0.4-alpha1/go.mod h1:ficis807+HOpLcS6fLpPbsYY5pOsn9RuPeP0UlOqNQQ=
github.com/stackql/go-spew v1.1.3-alpha24 h1:yNmgWU+OKU8SvlQ0umiQWWgWS6C5U1GuRMlYfQgbmJA=
github.com/stackql/go-spew v1.1.3-alpha24/go.mod h1:o2MPqg2ee1PN4SxAh9zQSJJmKKFJjpro9pejdK7dXWk=
github.com/stackql/go-sqlite3 v0.0.1-stackqlalpha h1:Ze9rqvLF76IuI42KqffAmBBQcIgInQrGJQkmTcoVRTI=
Expand Down
6 changes: 3 additions & 3 deletions internal/stackql/docparser/doc_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func OpenapiStackQLServiceDiscoveryDocPersistor(prov *openapistackql.Provider, s
switch v.Type {
case "object":
tabulation := v.Tabulate(false)
annTab := util.NewAnnotatedTabulation(tabulation, dto.NewHeirarchyIdentifiers(prov.Name, svc.GetName(), tabulation.GetName(), ""))
annTab := util.NewAnnotatedTabulation(tabulation, dto.NewHeirarchyIdentifiers(prov.Name, svc.GetName(), tabulation.GetName(), ""), "")
tabluationsAnnotated = append(tabluationsAnnotated, annTab)
if version == "v2" {
for pr, prVal := range v.Properties {
Expand All @@ -67,7 +67,7 @@ func OpenapiStackQLServiceDiscoveryDocPersistor(prov *openapistackql.Provider, s
tb := iSc.Tabulate(false)
log.Infoln(fmt.Sprintf("tb = %v", tb))
if tb != nil {
annTab := util.NewAnnotatedTabulation(tb, dto.NewHeirarchyIdentifiers(prov.Name, svc.GetName(), tb.GetName(), ""))
annTab := util.NewAnnotatedTabulation(tb, dto.NewHeirarchyIdentifiers(prov.Name, svc.GetName(), tb.GetName(), ""), "")
tabluationsAnnotated = append(tabluationsAnnotated, annTab)
}
}
Expand All @@ -80,7 +80,7 @@ func OpenapiStackQLServiceDiscoveryDocPersistor(prov *openapistackql.Provider, s
if len(itemsSchema.Properties) > 0 {
// create "inline" table
tabulation := v.Tabulate(false)
annTab := util.NewAnnotatedTabulation(tabulation, dto.NewHeirarchyIdentifiers(prov.Name, svc.GetName(), tabulation.GetName(), ""))
annTab := util.NewAnnotatedTabulation(tabulation, dto.NewHeirarchyIdentifiers(prov.Name, svc.GetName(), tabulation.GetName(), ""), "")
tabluationsAnnotated = append(tabluationsAnnotated, annTab)
}
}
Expand Down
6 changes: 5 additions & 1 deletion internal/stackql/drm/drm.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,11 @@ func (dc *StaticDRMConfig) GenerateSelectDML(tabAnnotated util.AnnotatedTabulati
quotedWhereColNames = append(quotedWhereColNames, `"`+genIdColName+`" `)
quotedWhereColNames = append(quotedWhereColNames, `"`+txnIdColName+`" `)
quotedWhereColNames = append(quotedWhereColNames, `"`+insIdColName+`" `)
q.WriteString(fmt.Sprintf(`SELECT %s FROM "%s" WHERE `, strings.Join(quotedColNames, ", "), dc.getTableName(tabAnnotated.GetHeirarchyIdentifiers(), txnCtrlCtrs.DiscoveryGenerationId)))
aliasStr := ""
if tabAnnotated.GetAlias() != "" {
aliasStr = fmt.Sprintf(` AS "%s" `, tabAnnotated.GetAlias())
}
q.WriteString(fmt.Sprintf(`SELECT %s FROM "%s"%s WHERE `, strings.Join(quotedColNames, ", "), dc.getTableName(tabAnnotated.GetHeirarchyIdentifiers(), txnCtrlCtrs.DiscoveryGenerationId), aliasStr))
q.WriteString(fmt.Sprintf(`( "%s" = ? AND "%s" = ? AND "%s" = ? AND "%s" = ? ) `, genIdColName, sessionIDColName, txnIdColName, insIdColName))
if rewrittenWhere != nil {
q.WriteString(fmt.Sprintf(" AND ( %s ) ", astvisit.GenerateModifiedWhereClause(rewrittenWhere)))
Expand Down
4 changes: 2 additions & 2 deletions internal/stackql/planbuilder/selection.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (p *primitiveGenerator) assembleUnarySelectionBuilder(
insertTabulation *openapistackql.Tabulation,
cols []parserutil.ColumnHandle,
) error {
annotatedInsertTabulation := util.NewAnnotatedTabulation(insertTabulation, hIds)
annotatedInsertTabulation := util.NewAnnotatedTabulation(insertTabulation, hIds, "")

prov, err := tbl.GetProviderObject()
if err != nil {
Expand Down Expand Up @@ -71,7 +71,7 @@ func (p *primitiveGenerator) assembleUnarySelectionBuilder(
log.Infoln(fmt.Sprintf("schema type = %T", schema))
}

selPsc, err := p.PrimitiveBuilder.GetDRMConfig().GenerateSelectDML(util.NewAnnotatedTabulation(selectTabulation, hIds), insPsc.TxnCtrlCtrs, node, rewrittenWhere)
selPsc, err := p.PrimitiveBuilder.GetDRMConfig().GenerateSelectDML(util.NewAnnotatedTabulation(selectTabulation, hIds, tbl.GetAlias()), insPsc.TxnCtrlCtrs, node, rewrittenWhere)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/stackql/planbuilder/statement_analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ func (p *primitiveGenerator) parseComments(comments sqlparser.Comments) {
}

func (p *primitiveGenerator) persistHerarchyToBuilder(heirarchy *taxonomy.HeirarchyObjects, node sqlparser.SQLNode) {
p.PrimitiveBuilder.SetTable(node, taxonomy.NewExtendedTableMetadata(heirarchy))
p.PrimitiveBuilder.SetTable(node, taxonomy.NewExtendedTableMetadata(heirarchy, taxonomy.GetAliasFromStatement(node)))
}

func (p *primitiveGenerator) analyzeUnaryExec(handlerCtx *handler.HandlerContext, node *sqlparser.Exec, selectNode *sqlparser.Select, cols []parserutil.ColumnHandle) (*taxonomy.ExtendedTableMetadata, error) {
Expand Down
3 changes: 3 additions & 0 deletions internal/stackql/provider/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ func (gp *GenericProvider) ShowAuth(authCtx *dto.AuthCtx) (*openapistackql.AuthM
var err error
var retVal *openapistackql.AuthMetadata
var authObj openapistackql.AuthMetadata
if authCtx == nil {
return nil, errors.New(constants.NotAuthenticatedShowStr)
}
switch gp.inferAuthType(*authCtx, authCtx.Type) {
case dto.AuthServiceAccountStr:
var sa serviceAccount
Expand Down
17 changes: 16 additions & 1 deletion internal/stackql/taxonomy/taxonomy.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ type ExtendedTableMetadata struct {
IsLocallyExecutable bool
HttpArmoury *httpbuild.HTTPArmoury
SelectItemsKey string
Alias string
}

func (ex ExtendedTableMetadata) GetAlias() string {
return ex.Alias
}

func (ex ExtendedTableMetadata) GetProvider() (provider.IProvider, error) {
Expand Down Expand Up @@ -198,11 +203,12 @@ func (ex ExtendedTableMetadata) GetSelectableObjectSchema() (*openapistackql.Sch
return ex.HeirarchyObjects.GetSelectableObjectSchema()
}

func NewExtendedTableMetadata(heirarchyObjects *HeirarchyObjects) ExtendedTableMetadata {
func NewExtendedTableMetadata(heirarchyObjects *HeirarchyObjects, alias string) ExtendedTableMetadata {
return ExtendedTableMetadata{
ColsVisited: make(map[string]bool),
RequiredParameters: make(map[string]openapistackql.Parameter),
HeirarchyObjects: heirarchyObjects,
Alias: alias,
}
}

Expand Down Expand Up @@ -290,6 +296,15 @@ func getHids(handlerCtx *handler.HandlerContext, node sqlparser.SQLNode) (*dto.H
return hIds, nil
}

func GetAliasFromStatement(node sqlparser.SQLNode) string {
switch n := node.(type) {
case *sqlparser.AliasedTableExpr:
return n.As.GetRawVal()
default:
return ""
}
}

func GetHeirarchyFromStatement(handlerCtx *handler.HandlerContext, node sqlparser.SQLNode) (*HeirarchyObjects, error) {
var hIds *dto.HeirarchyIdentifiers
hIds, err := getHids(handlerCtx, node)
Expand Down
16 changes: 11 additions & 5 deletions internal/stackql/util/annotated_tabulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,27 @@ import (
)

type AnnotatedTabulation struct {
tab *openapistackql.Tabulation
hIds *dto.HeirarchyIdentifiers
tab *openapistackql.Tabulation
hIds *dto.HeirarchyIdentifiers
alias string
}

func NewAnnotatedTabulation(tab *openapistackql.Tabulation, hIds *dto.HeirarchyIdentifiers) AnnotatedTabulation {
func NewAnnotatedTabulation(tab *openapistackql.Tabulation, hIds *dto.HeirarchyIdentifiers, alias string) AnnotatedTabulation {
return AnnotatedTabulation{
tab: tab,
hIds: hIds,
tab: tab,
hIds: hIds,
alias: alias,
}
}

func (at AnnotatedTabulation) GetTabulation() *openapistackql.Tabulation {
return at.tab
}

func (at AnnotatedTabulation) GetAlias() string {
return at.alias
}

func (at AnnotatedTabulation) GetHeirarchyIdentifiers() *dto.HeirarchyIdentifiers {
return at.hIds
}
2 changes: 1 addition & 1 deletion internal/test/testobjects/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const (
SimpleDeleteExecComputeNetwork string = `EXEC /*+ AWAIT */ google.compute.networks.delete @project = 'stackql-demo', @network = 'kubernetes-the-hard-way-vpc';`
SimpleAggCountGroupedGoogleContainerSubnetworkAsc string = "select ipCidrRange, sum(5) cc from google.container.`projects.aggregated.usableSubnetworks` where projectsId = 'testing-project' group by \"ipCidrRange\" having sum(5) >= 5 order by ipCidrRange asc;"
SimpleAggCountGroupedGoogleContainerSubnetworkDesc string = "select ipCidrRange, sum(5) cc from google.container.`projects.aggregated.usableSubnetworks` where projectsId = 'testing-project' group by \"ipCidrRange\" having sum(5) >= 5 order by ipCidrRange desc;"
SelectGoogleComputeDisksOrderCreationTmstpAsc string = `select name, sizeGb, creationTimestamp from google.compute.disks where zone = 'australia-southeast1-b' AND /* */ project = 'testing-project' ORDER BY creationTimestamp asc;`
SelectGoogleComputeDisksOrderCreationTmstpAsc string = `select d1.name, d1.sizeGb, d1.creationTimestamp from google.compute.disks d1 where zone = 'australia-southeast1-b' AND /* */ project = 'testing-project' ORDER BY creationTimestamp asc;`
SelectGoogleComputeDisksOrderCreationTmstpAscPlusJsonExtract string = `select name, json_extract('{"a":2,"c":[4,5,{"f":7}]}', '$.c') as json_rendition, sizeGb, creationTimestamp from google.compute.disks where zone = 'australia-southeast1-b' AND /* */ project = 'testing-project' ORDER BY creationTimestamp asc;`
SelectGoogleComputeDisksOrderCreationTmstpAscPlusJsonExtractCoalesce string = `select name, coalesce(json_extract(labels, '$.k1'), 'dummy_value') as json_rendition, sizeGb, creationTimestamp from google.compute.disks where zone = 'australia-southeast1-b' AND /* */ project = 'testing-project' ORDER BY creationTimestamp asc;`
UnionSelectGoogleComputeDisksOrderCreationTmstpAscPlusJsonExtractCoalesce string = `select name, coalesce(json_extract(labels, '$.k1'), 'dummy_value') as json_rendition, sizeGb, creationTimestamp from google.compute.disks where zone = 'australia-southeast1-b' AND /* */ project = 'testing-project'
Expand Down