Skip to content
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

Added InjectedStatement as an AST node #2504

Merged
merged 3 commits into from
May 21, 2024
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/dolthub/go-icu-regex v0.0.0-20230524105445-af7e7991c97e
github.com/dolthub/jsonpath v0.0.2-0.20240227200619-19675ab05c71
github.com/dolthub/sqllogictest/go v0.0.0-20201107003712-816f3ae12d81
github.com/dolthub/vitess v0.0.0-20240520192903-ff7bd6353f3e
github.com/dolthub/vitess v0.0.0-20240521104846-e778edc6deb3
github.com/go-kit/kit v0.10.0
github.com/go-sql-driver/mysql v1.7.2-0.20231213112541-0004702b931d
github.com/gocraft/dbr/v2 v2.7.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ github.com/dolthub/vitess v0.0.0-20240515231645-966f8cb81cfe h1:40//2vXX3xDDud5b
github.com/dolthub/vitess v0.0.0-20240515231645-966f8cb81cfe/go.mod h1:uBvlRluuL+SbEWTCZ68o0xvsdYZER3CEG/35INdzfJM=
github.com/dolthub/vitess v0.0.0-20240520192903-ff7bd6353f3e h1:idIiEhWOfi6vuBD7wZxn75TGPMcnZ6r9Zr/DHGAOCrQ=
github.com/dolthub/vitess v0.0.0-20240520192903-ff7bd6353f3e/go.mod h1:uBvlRluuL+SbEWTCZ68o0xvsdYZER3CEG/35INdzfJM=
github.com/dolthub/vitess v0.0.0-20240521104846-e778edc6deb3 h1:2Q6j9yI1XjDH5UOY0EAhaXoGIdN11jItAzpRGLl6O8o=
github.com/dolthub/vitess v0.0.0-20240521104846-e778edc6deb3/go.mod h1:uBvlRluuL+SbEWTCZ68o0xvsdYZER3CEG/35INdzfJM=
github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/eapache/go-resiliency v1.1.0/go.mod h1:kFI+JgMyC7bLPUVY133qvEBtVayf5mFgVsvEsIPBvNs=
github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21/go.mod h1:+020luEh2TKB4/GOp8oxxtq0Daoen/Cii55CzbTV6DU=
Expand Down
23 changes: 23 additions & 0 deletions sql/planbuilder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package planbuilder

import (
"fmt"
"strings"
"sync"

Expand Down Expand Up @@ -361,6 +362,8 @@ func (b *Builder) build(inScope *scope, stmt ast.Statement, query string) (outSc
return b.buildExecute(inScope, n)
case *ast.Deallocate:
return b.buildDeallocate(inScope, n)
case ast.InjectedStatement:
return b.buildInjectedStatement(inScope, n)
}
return
}
Expand Down Expand Up @@ -400,6 +403,26 @@ func (b *Builder) buildVirtualTableScan(db string, tab sql.Table) *plan.VirtualC
return plan.NewVirtualColumnTable(tab, projections)
}

// buildInjectedStatement returns the sql.Node encapsulated by the injected statement.
func (b *Builder) buildInjectedStatement(inScope *scope, n ast.InjectedStatement) (outScope *scope) {
resolvedChildren := make([]any, len(n.Children))
for i, child := range n.Children {
resolvedChildren[i] = b.buildScalar(inScope, child)
}
stmt, err := n.Statement.WithResolvedChildren(resolvedChildren)
if err != nil {
b.handleErr(err)
return nil
}
if sqlNode, ok := stmt.(sql.ExecSourceRel); ok {
outScope = inScope.push()
outScope.node = sqlNode
return outScope
}
b.handleErr(fmt.Errorf("Injected statement does not resolve to a valid node"))
return nil
}

// assignColumnIndexes fixes the column indexes in the expression to match the schema given
func assignColumnIndexes(e sql.Expression, schema sql.Schema) sql.Expression {
e, _, _ = transform.Expr(e, func(e sql.Expression) (sql.Expression, transform.TreeIdentity, error) {
Expand Down
1 change: 1 addition & 0 deletions sql/planbuilder/scalar.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ func (b *Builder) buildScalar(inScope *scope, e ast.Expr) (ex sql.Expression) {
expr, err := v.Expression.WithResolvedChildren(resolvedChildren)
if err != nil {
b.handleErr(err)
return nil
}
if sqlExpr, ok := expr.(sql.Expression); ok {
return sqlExpr
Expand Down
Loading