Skip to content

Commit 1a4cc3d

Browse files
committed
Added the concept of an interpreter expression
1 parent 3f5bb8c commit 1a4cc3d

File tree

7 files changed

+72
-16
lines changed

7 files changed

+72
-16
lines changed

engine.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ type Engine struct {
150150
Parser sql.Parser
151151
}
152152

153+
var _ analyzer.StatementRunner = (*Engine)(nil)
154+
153155
type ColumnWithRawDefault struct {
154156
SqlColumn *sql.Column
155157
Default string
@@ -195,6 +197,7 @@ func New(a *analyzer.Analyzer, cfg *Config) *Engine {
195197
Parser: sql.GlobalParser,
196198
}
197199
ret.ReadOnly.Store(cfg.IsReadOnly)
200+
a.Runner = ret
198201
return ret
199202
}
200203

enginetest/initialization.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ func NewEngineWithProvider(_ *testing.T, harness Harness, provider sql.DatabaseP
9191
if idh, ok := harness.(IndexDriverHarness); ok {
9292
idh.InitializeIndexDriver(engine.Analyzer.Catalog.AllDatabases(NewContext(harness)))
9393
}
94+
analyzer.Runner = engine
9495

9596
return engine
9697
}

sql/analyzer/analyzer.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,8 @@ type Analyzer struct {
286286
Coster memo.Coster
287287
// ExecBuilder converts a sql.Node tree into an executable iterator.
288288
ExecBuilder sql.NodeExecBuilder
289+
// Runner represents the engine, which is represented as a separate interface to work around circular dependencies
290+
Runner StatementRunner
289291
}
290292

291293
// NewDefault creates a default Analyzer instance with all default Rules and configuration.

sql/analyzer/interpreter.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright 2022 Dolthub, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package analyzer
16+
17+
import (
18+
"github.com/dolthub/vitess/go/vt/sqlparser"
19+
20+
"github.com/dolthub/go-mysql-server/sql/transform"
21+
22+
"github.com/dolthub/go-mysql-server/sql"
23+
"github.com/dolthub/go-mysql-server/sql/plan"
24+
)
25+
26+
// Interpreter is an interface that implements an interpreter. These are typically used for functions (which may be
27+
// implemented as a set of operations that are interpreted during runtime).
28+
type Interpreter interface {
29+
SetStatementRunner(ctx *sql.Context, runner StatementRunner) sql.Expression
30+
}
31+
32+
// StatementRunner is essentially an interface that the engine will implement. We cannot directly reference the engine
33+
// here as it will cause an import cycle, so this may be updated to suit any function changes that the engine
34+
// experiences.
35+
type StatementRunner interface {
36+
QueryWithBindings(ctx *sql.Context, query string, parsed sqlparser.Statement, bindings map[string]sqlparser.Expr, qFlags *sql.QueryFlags) (sql.Schema, sql.RowIter, *sql.QueryFlags, error)
37+
}
38+
39+
// interpreter hands the engine to any interpreter expressions.
40+
func interpreter(ctx *sql.Context, a *Analyzer, n sql.Node, scope *plan.Scope, sel RuleSelector, qFlags *sql.QueryFlags) (sql.Node, transform.TreeIdentity, error) {
41+
return transform.NodeExprs(n, func(expr sql.Expression) (sql.Expression, transform.TreeIdentity, error) {
42+
if interp, ok := expr.(Interpreter); ok {
43+
return interp.SetStatementRunner(ctx, a.Runner), transform.NewTree, nil
44+
}
45+
return expr, transform.SameTree, nil
46+
})
47+
}

sql/analyzer/rule_ids.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ const (
6565
assignRoutinesId // assignRoutines
6666
modifyUpdateExprsForJoinId // modifyUpdateExprsForJoin
6767
applyForeignKeysId // applyForeignKeys
68+
interpreterId // interpreter
6869

6970
// validate
7071
validateResolvedId // validateResolved

sql/analyzer/ruleid_string.go

Lines changed: 17 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sql/analyzer/rules.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ var OnceAfterDefault = []Rule{
9292
{assignRoutinesId, assignRoutines},
9393
{modifyUpdateExprsForJoinId, modifyUpdateExprsForJoin},
9494
{applyForeignKeysId, applyForeignKeys},
95+
{interpreterId, interpreter},
9596
}
9697

9798
// DefaultValidationRules to apply while analyzing nodes.

0 commit comments

Comments
 (0)