Skip to content

Commit

Permalink
Added the concept of an interpreter expression
Browse files Browse the repository at this point in the history
  • Loading branch information
Hydrocharged committed Jan 30, 2025
1 parent 97b350e commit be458c2
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 16 deletions.
3 changes: 3 additions & 0 deletions engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ type Engine struct {
Parser sql.Parser
}

var _ analyzer.StatementRunner = (*Engine)(nil)

type ColumnWithRawDefault struct {
SqlColumn *sql.Column
Default string
Expand Down Expand Up @@ -195,6 +197,7 @@ func New(a *analyzer.Analyzer, cfg *Config) *Engine {
Parser: sql.GlobalParser,
}
ret.ReadOnly.Store(cfg.IsReadOnly)
a.Runner = ret
return ret
}

Expand Down
1 change: 1 addition & 0 deletions enginetest/initialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ func NewEngineWithProvider(_ *testing.T, harness Harness, provider sql.DatabaseP
if idh, ok := harness.(IndexDriverHarness); ok {
idh.InitializeIndexDriver(engine.Analyzer.Catalog.AllDatabases(NewContext(harness)))
}
analyzer.Runner = engine

return engine
}
Expand Down
2 changes: 2 additions & 0 deletions sql/analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,8 @@ type Analyzer struct {
Coster memo.Coster
// ExecBuilder converts a sql.Node tree into an executable iterator.
ExecBuilder sql.NodeExecBuilder
// Runner represents the engine, which is represented as a separate interface to work around circular dependencies
Runner StatementRunner
}

// NewDefault creates a default Analyzer instance with all default Rules and configuration.
Expand Down
47 changes: 47 additions & 0 deletions sql/analyzer/interpreter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2022 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package analyzer

import (
"github.com/dolthub/vitess/go/vt/sqlparser"

"github.com/dolthub/go-mysql-server/sql/transform"

"github.com/dolthub/go-mysql-server/sql"
"github.com/dolthub/go-mysql-server/sql/plan"
)

// Interpreter is an interface that implements an interpreter. These are typically used for functions (which may be
// implemented as a set of operations that are interpreted during runtime).
type Interpreter interface {
SetStatementRunner(ctx *sql.Context, runner StatementRunner) sql.Expression
}

// StatementRunner is essentially an interface that the engine will implement. We cannot directly reference the engine
// here as it will cause an import cycle, so this may be updated to suit any function changes that the engine
// experiences.
type StatementRunner interface {
QueryWithBindings(ctx *sql.Context, query string, parsed sqlparser.Statement, bindings map[string]sqlparser.Expr, qFlags *sql.QueryFlags) (sql.Schema, sql.RowIter, *sql.QueryFlags, error)
}

// interpreter hands the engine to any interpreter expressions.
func interpreter(ctx *sql.Context, a *Analyzer, n sql.Node, scope *plan.Scope, sel RuleSelector, qFlags *sql.QueryFlags) (sql.Node, transform.TreeIdentity, error) {
return transform.NodeExprs(n, func(expr sql.Expression) (sql.Expression, transform.TreeIdentity, error) {
if interp, ok := expr.(Interpreter); ok {
return interp.SetStatementRunner(ctx, a.Runner), transform.NewTree, nil
}
return expr, transform.SameTree, nil
})
}
1 change: 1 addition & 0 deletions sql/analyzer/rule_ids.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ const (
assignRoutinesId // assignRoutines
modifyUpdateExprsForJoinId // modifyUpdateExprsForJoin
applyForeignKeysId // applyForeignKeys
interpreterId // interpreter

// validate
validateResolvedId // validateResolved
Expand Down
33 changes: 17 additions & 16 deletions sql/analyzer/ruleid_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions sql/analyzer/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ var OnceAfterDefault = []Rule{
{assignRoutinesId, assignRoutines},
{modifyUpdateExprsForJoinId, modifyUpdateExprsForJoin},
{applyForeignKeysId, applyForeignKeys},
{interpreterId, interpreter},
}

// DefaultValidationRules to apply while analyzing nodes.
Expand Down

0 comments on commit be458c2

Please sign in to comment.