Skip to content

Commit a939236

Browse files
- More await pass through.
1 parent 6d4e6d0 commit a939236

File tree

9 files changed

+99
-28
lines changed

9 files changed

+99
-28
lines changed

internal/stackql/astanalysis/annotatedast/annotated_ast.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func NewAnnotatedAst(parent AnnotatedAst, ast sqlparser.Statement) (AnnotatedAst
2727

2828
type AnnotatedAst interface {
2929
GetAST() sqlparser.Statement
30+
IsAwait(sqlparser.SQLNode) bool
3031
GetIndirect(sqlparser.SQLNode) (astindirect.Indirect, bool)
3132
GetIndirects() map[string]astindirect.Indirect
3233
GetMaterializedView(sqlparser.SQLNode) (astindirect.Indirect, bool)
@@ -65,6 +66,31 @@ type standardAnnotatedAst struct {
6566
execIndirectCache map[*sqlparser.Exec]astindirect.Indirect
6667
}
6768

69+
func hasAwaitComment(comments sqlparser.Comments) bool {
70+
if comments != nil {
71+
commentDirectives := sqlparser.ExtractCommentDirectives(comments)
72+
if commentDirectives.IsSet("AWAIT") {
73+
return true
74+
}
75+
}
76+
return false
77+
}
78+
79+
func (aa *standardAnnotatedAst) IsAwait(node sqlparser.SQLNode) bool {
80+
switch n := node.(type) {
81+
case *sqlparser.Exec:
82+
return hasAwaitComment(n.Comments)
83+
case *sqlparser.Insert:
84+
return hasAwaitComment(n.Comments)
85+
case *sqlparser.Update:
86+
return hasAwaitComment(n.Comments)
87+
case *sqlparser.Delete:
88+
return hasAwaitComment(n.Comments)
89+
default:
90+
return false
91+
}
92+
}
93+
6894
func (aa *standardAnnotatedAst) GetExecIndirect(selNode *sqlparser.Exec) (astindirect.Indirect, bool) {
6995
rv, ok := aa.execIndirectCache[selNode]
7096
return rv, ok

internal/stackql/astanalysis/routeanalysis/select_route_analysis.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ func (sp *standardSelectRoutePass) RoutePass() error {
153153

154154
// TODO: Do the proper SOLID treatment on router, etc.
155155
// Might need to split into multiple passes.
156-
v := router.NewTableRouteAstVisitor(pbi.GetHandlerCtx(), paramRouter)
156+
v := router.NewTableRouteAstVisitor(pbi.GetHandlerCtx(), paramRouter, false)
157157

158158
err := v.Visit(node)
159159

internal/stackql/dependencyplanner/dependencyplanner.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ func (dp *standardDependencyPlanner) orchestrate(
497497
return idx, err
498498
}
499499

500-
//nolint:gocognit // live with it
500+
//nolint:gocognit,funlen // live with it
501501
func (dp *standardDependencyPlanner) processAcquire(
502502
sqlNode sqlparser.SQLNode,
503503
annotationCtx taxonomy.AnnotationCtx,
@@ -571,7 +571,7 @@ func (dp *standardDependencyPlanner) processAcquire(
571571
svc,
572572
resource,
573573
m,
574-
false, // TODO: wire up isAwait
574+
annotationCtx.IsAwait(),
575575
[]util.AnnotatedTabulation{anTab},
576576
dp.primitiveComposer.GetSQLEngine(),
577577
prov.GetName(),

internal/stackql/parserutil/parser_util.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,20 @@ const (
1717
FloatBitSize int = 64
1818
)
1919

20+
func hasAwaitComment(comments sqlparser.Comments) bool {
21+
if comments != nil {
22+
commentDirectives := sqlparser.ExtractCommentDirectives(comments)
23+
if commentDirectives.IsSet("AWAIT") {
24+
return true
25+
}
26+
}
27+
return false
28+
}
29+
30+
func IsAwait(comments sqlparser.Comments) bool {
31+
return hasAwaitComment(comments)
32+
}
33+
2034
//nolint:gocritic,staticcheck // TODO: clean up this and other tech debt
2135
func ExtractLHSTable(sqlFunc *sqlparser.FuncExpr) (*sqlparser.ColName, bool) {
2236
if sqlFunc == nil {

internal/stackql/primitivegenerator/statement_analyzer.go

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -518,12 +518,22 @@ func extractVarDefFromExec(node *sqlparser.Exec, argName string) (*sqlparser.Exe
518518

519519
func (pb *standardPrimitiveGenerator) parseComments(comments sqlparser.Comments) {
520520
if comments != nil {
521-
pb.PrimitiveComposer.SetCommentDirectives(sqlparser.ExtractCommentDirectives(comments))
522-
// This pattern supports preset.
523-
if pb.PrimitiveComposer.GetCommentDirectives().IsSet("AWAIT") {
524-
pb.PrimitiveComposer.SetAwait(true)
521+
commentDirectives := sqlparser.ExtractCommentDirectives(comments)
522+
pb.PrimitiveComposer.SetCommentDirectives(commentDirectives)
523+
}
524+
if pb.hasAwaitComment(comments) {
525+
pb.PrimitiveComposer.SetAwait(true)
526+
}
527+
}
528+
529+
func (pb *standardPrimitiveGenerator) hasAwaitComment(comments sqlparser.Comments) bool {
530+
if comments != nil {
531+
commentDirectives := sqlparser.ExtractCommentDirectives(comments)
532+
if commentDirectives.IsSet("AWAIT") {
533+
return true
525534
}
526535
}
536+
return false
527537
}
528538

529539
func (pb *standardPrimitiveGenerator) persistHerarchyToBuilder(
@@ -542,7 +552,8 @@ func (pb *standardPrimitiveGenerator) AnalyzeUnaryExec(
542552
selectNode *sqlparser.Select,
543553
cols []parserutil.ColumnHandle,
544554
) (tablemetadata.ExtendedTableMetadata, error) {
545-
err := pb.inferHeirarchyAndPersist(handlerCtx, node, nil, false)
555+
isAwait := pb.hasAwaitComment(node.Comments)
556+
err := pb.inferHeirarchyAndPersist(handlerCtx, node, nil, false, isAwait)
546557
if err != nil {
547558
return nil, err
548559
}
@@ -1025,7 +1036,8 @@ func (pb *standardPrimitiveGenerator) AnalyzeInsert(pbi planbuilderinput.PlanBui
10251036
if !ok {
10261037
return fmt.Errorf("could not cast node of type '%T' to required Insert", pbi.GetStatement())
10271038
}
1028-
err := pb.inferHeirarchyAndPersist(handlerCtx, node, pbi.GetPlaceholderParams(), false)
1039+
isAwait := pb.hasAwaitComment(node.Comments)
1040+
err := pb.inferHeirarchyAndPersist(handlerCtx, node, pbi.GetPlaceholderParams(), false, isAwait)
10291041
if err != nil {
10301042
return err
10311043
}
@@ -1181,7 +1193,8 @@ func (pb *standardPrimitiveGenerator) AnalyzeUpdate(pbi planbuilderinput.PlanBui
11811193
if !ok {
11821194
return fmt.Errorf("could not cast node of type '%T' to required Update", pbi.GetStatement())
11831195
}
1184-
err := pb.inferHeirarchyAndPersist(handlerCtx, node, pbi.GetPlaceholderParams(), false)
1196+
isAwait := pb.hasAwaitComment(node.Comments)
1197+
err := pb.inferHeirarchyAndPersist(handlerCtx, node, pbi.GetPlaceholderParams(), false, isAwait)
11851198
if err != nil {
11861199
return err
11871200
}
@@ -1292,8 +1305,10 @@ func (pb *standardPrimitiveGenerator) inferHeirarchyAndPersist(
12921305
handlerCtx handler.HandlerContext,
12931306
node sqlparser.SQLNode,
12941307
parameters parserutil.ColumnKeyedDatastore,
1295-
viewPermissive bool) error {
1296-
heirarchy, err := taxonomy.GetHeirarchyFromStatement(handlerCtx, node, parameters, viewPermissive)
1308+
viewPermissive bool,
1309+
isAwait bool,
1310+
) error {
1311+
heirarchy, err := taxonomy.GetHeirarchyFromStatement(handlerCtx, node, parameters, viewPermissive, isAwait)
12971312
if err != nil {
12981313
return err
12991314
}
@@ -1312,7 +1327,8 @@ func (pb *standardPrimitiveGenerator) analyzeDelete(
13121327
}
13131328
pb.parseComments(node.Comments)
13141329
paramMap, paramMapExists := pbi.GetAnnotatedAST().GetWhereParamMapsEntry(node.Where)
1315-
err := pb.inferHeirarchyAndPersist(handlerCtx, node, paramMap, false)
1330+
isAwait := pb.hasAwaitComment(node.Comments)
1331+
err := pb.inferHeirarchyAndPersist(handlerCtx, node, paramMap, false, isAwait)
13161332
if err != nil {
13171333
return err
13181334
}
@@ -1404,7 +1420,8 @@ func (pb *standardPrimitiveGenerator) analyzeDescribe(pbi planbuilderinput.PlanB
14041420
return fmt.Errorf("could not cast node of type '%T' to required Describe", pbi.GetStatement())
14051421
}
14061422
var err error
1407-
err = pb.inferHeirarchyAndPersist(handlerCtx, node, nil, true)
1423+
// DESCRIBE has no await or comments at this time
1424+
err = pb.inferHeirarchyAndPersist(handlerCtx, node, nil, true, false)
14081425
if err != nil {
14091426
return err
14101427
}
@@ -1515,12 +1532,14 @@ func (pb *standardPrimitiveGenerator) analyzeShow(
15151532
case "AUTH":
15161533
// TODO
15171534
case "INSERT":
1518-
err = pb.inferHeirarchyAndPersist(handlerCtx, node, nil, false)
1535+
// SHOW has no comments or concept of await
1536+
err = pb.inferHeirarchyAndPersist(handlerCtx, node, nil, false, false)
15191537
if err != nil {
15201538
return err
15211539
}
15221540
case "METHODS":
1523-
err = pb.inferHeirarchyAndPersist(handlerCtx, node, nil, false)
1541+
// SHOW has no comments or concept of await
1542+
err = pb.inferHeirarchyAndPersist(handlerCtx, node, nil, false, false)
15241543
if err != nil {
15251544
return err
15261545
}

internal/stackql/router/parameter_router.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ type ParameterRouter interface {
4949
// - Hierarchy.
5050
// - Columnar objects definitely assigned as HTTP method parameters.
5151
// - Error if applicable.
52-
Route(tb sqlparser.TableExpr, handler handler.HandlerContext) (taxonomy.AnnotationCtx, error)
52+
Route(tb sqlparser.TableExpr, handler handler.HandlerContext, isAwait bool) (taxonomy.AnnotationCtx, error)
5353

5454
// Detects:
5555
// - Dependency cycle.
@@ -511,14 +511,16 @@ func (pr *standardParameterRouter) isInvalidated(key string) bool {
511511
func (pr *standardParameterRouter) Route(
512512
tb sqlparser.TableExpr,
513513
handlerCtx handler.HandlerContext,
514+
isAwait bool,
514515
) (taxonomy.AnnotationCtx, error) {
515-
return pr.route(tb, handlerCtx)
516+
return pr.route(tb, handlerCtx, isAwait)
516517
}
517518

518519
//nolint:funlen,gocognit,govet,gocyclo,cyclop // inherently complex functionality
519520
func (pr *standardParameterRouter) route(
520521
tb sqlparser.TableExpr,
521522
handlerCtx handler.HandlerContext,
523+
isAwait bool,
522524
) (taxonomy.AnnotationCtx, error) {
523525
// TODO: Get rid of the dead set mess that is where paramters in preference.
524526
for k, v := range pr.whereParamMap.GetMap() {
@@ -589,9 +591,9 @@ func (pr *standardParameterRouter) route(
589591
priorParameters := runParamters.Clone()
590592
// notOnStringParams := notOnParams.GetStringified()
591593
// TODO: add parent params into the mix here.
592-
hr, err := taxonomy.GetHeirarchyFromStatement(handlerCtx, tb, notOnParams, false)
594+
hr, err := taxonomy.GetHeirarchyFromStatement(handlerCtx, tb, notOnParams, false, isAwait)
593595
if err != nil {
594-
hr, err = taxonomy.GetHeirarchyFromStatement(handlerCtx, tb, runParamters, false)
596+
hr, err = taxonomy.GetHeirarchyFromStatement(handlerCtx, tb, runParamters, false, isAwait)
595597
} else {
596598
// If the where parameters are sufficient, then need to switch
597599
// the Table - Paramater coupling object

internal/stackql/router/table_routing.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,16 @@ type standardTableRouteAstVisitor struct {
3030
router ParameterRouter
3131
tables taxonomy.TblMap
3232
annotations taxonomy.AnnotationCtxMap
33+
isAwait bool
3334
}
3435

35-
func NewTableRouteAstVisitor(handlerCtx handler.HandlerContext, router ParameterRouter) TableRouteAstVisitor {
36+
func NewTableRouteAstVisitor(handlerCtx handler.HandlerContext, router ParameterRouter, isAwait bool) TableRouteAstVisitor {
3637
return &standardTableRouteAstVisitor{
3738
handlerCtx: handlerCtx,
3839
router: router,
3940
tables: make(taxonomy.TblMap),
4041
annotations: make(taxonomy.AnnotationCtxMap),
42+
isAwait: isAwait,
4143
}
4244
}
4345

@@ -55,13 +57,13 @@ func (v *standardTableRouteAstVisitor) analyzeAliasedTable(
5557
) (taxonomy.AnnotationCtx, error) {
5658
switch ex := tb.Expr.(type) {
5759
case *sqlparser.Subquery:
58-
return v.router.Route(tb, v.handlerCtx)
60+
return v.router.Route(tb, v.handlerCtx, v.isAwait)
5961
case sqlparser.TableName:
6062
_, err := taxonomy.GetHeirarchyIDsFromParserNode(v.handlerCtx, ex)
6163
if err != nil {
6264
return nil, err
6365
}
64-
return v.router.Route(tb, v.handlerCtx)
66+
return v.router.Route(tb, v.handlerCtx, v.isAwait)
6567
default:
6668
return nil, fmt.Errorf("table of type '%T' not currently supported", ex)
6769
}
@@ -75,7 +77,7 @@ func (v *standardTableRouteAstVisitor) analyzeExecTableName(
7577
tb *sqlparser.ExecSubquery,
7678
) (taxonomy.AnnotationCtx, error) {
7779
// cannot be recursive
78-
return v.router.Route(tb, v.handlerCtx)
80+
return v.router.Route(tb, v.handlerCtx, v.isAwait)
7981
}
8082

8183
func (v *standardTableRouteAstVisitor) GetTableMap() taxonomy.TblMap {

internal/stackql/tablemetadata/hierarchy_objects.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,14 @@ type HeirarchyObjects interface {
4141
IsPGInternalObject() bool
4242
SetIndirect(internaldto.RelationDTO)
4343
GetIndirect() (internaldto.RelationDTO, bool)
44+
IsAwait() bool
4445
}
4546

46-
func NewHeirarchyObjects(hIDs internaldto.HeirarchyIdentifiers) HeirarchyObjects {
47+
func NewHeirarchyObjects(hIDs internaldto.HeirarchyIdentifiers, isAwait bool) HeirarchyObjects {
4748
return &standardHeirarchyObjects{
4849
heirarchyIDs: hIDs,
4950
hr: internaldto.NewHeirarchy(hIDs),
51+
isAwait: isAwait,
5052
}
5153
}
5254

@@ -56,6 +58,11 @@ type standardHeirarchyObjects struct {
5658
prov provider.IProvider
5759
sqlDataSource sql_datasource.SQLDataSource
5860
indirect internaldto.RelationDTO
61+
isAwait bool
62+
}
63+
64+
func (ho *standardHeirarchyObjects) IsAwait() bool {
65+
return ho.isAwait
5966
}
6067

6168
func (ho *standardHeirarchyObjects) GetIndirect() (internaldto.RelationDTO, bool) {

internal/stackql/taxonomy/hierarchy.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ func GetHeirarchyFromStatement(
184184
node sqlparser.SQLNode,
185185
parameters parserutil.ColumnKeyedDatastore,
186186
viewPermissive bool,
187+
isAwait bool,
187188
) (tablemetadata.HeirarchyObjects, error) {
188189
var hIDs internaldto.HeirarchyIdentifiers
189190
getFirstAvailableMethod := false
@@ -205,10 +206,10 @@ func GetHeirarchyFromStatement(
205206
case *sqlparser.AliasedTableExpr:
206207
switch n.Expr.(type) { //nolint:gocritic // this is expressive enough
207208
case *sqlparser.Subquery:
208-
retVal := tablemetadata.NewHeirarchyObjects(hIDs)
209+
retVal := tablemetadata.NewHeirarchyObjects(hIDs, isAwait)
209210
return retVal, nil
210211
}
211-
return GetHeirarchyFromStatement(handlerCtx, n.Expr, parameters, viewPermissive)
212+
return GetHeirarchyFromStatement(handlerCtx, n.Expr, parameters, viewPermissive, isAwait)
212213
case *sqlparser.Show:
213214
switch strings.ToUpper(n.Type) {
214215
case "INSERT":
@@ -228,7 +229,7 @@ func GetHeirarchyFromStatement(
228229
default:
229230
return nil, fmt.Errorf("cannot resolve taxonomy")
230231
}
231-
retVal := tablemetadata.NewHeirarchyObjects(hIDs)
232+
retVal := tablemetadata.NewHeirarchyObjects(hIDs, isAwait)
232233
sqlDataSource, isSQLDataSource := handlerCtx.GetSQLDataSource(hIDs.GetProviderStr())
233234
if isSQLDataSource {
234235
retVal.SetSQLDataSource(sqlDataSource)

0 commit comments

Comments
 (0)