Skip to content

Commit 2766041

Browse files
authored
move applyEventScheduler logic and eventscheduler to builder (#2729)
1 parent f3be1d2 commit 2766041

27 files changed

+133
-170
lines changed

engine.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ func (e *Engine) AnalyzeQuery(
209209
ctx *sql.Context,
210210
query string,
211211
) (sql.Node, error) {
212-
binder := planbuilder.New(ctx, e.Analyzer.Catalog, e.Parser)
212+
binder := planbuilder.New(ctx, e.Analyzer.Catalog, e.EventScheduler, e.Parser)
213213
parsed, _, _, qFlags, err := binder.Parse(query, nil, false)
214214
if err != nil {
215215
return nil, err
@@ -237,7 +237,7 @@ func (e *Engine) PrepareParsedQuery(
237237
statementKey, query string,
238238
stmt sqlparser.Statement,
239239
) (sql.Node, error) {
240-
binder := planbuilder.New(ctx, e.Analyzer.Catalog, e.Parser)
240+
binder := planbuilder.New(ctx, e.Analyzer.Catalog, e.EventScheduler, e.Parser)
241241
node, _, err := binder.BindOnly(stmt, query, nil)
242242

243243
if err != nil {
@@ -517,7 +517,7 @@ func (e *Engine) BoundQueryPlan(ctx *sql.Context, query string, parsed sqlparser
517517

518518
query = sql.RemoveSpaceAndDelimiter(query, ';')
519519

520-
binder := planbuilder.New(ctx, e.Analyzer.Catalog, e.Parser)
520+
binder := planbuilder.New(ctx, e.Analyzer.Catalog, e.EventScheduler, e.Parser)
521521
binder.SetBindings(bindings)
522522

523523
// Begin a transaction if necessary (no-op if one is in flight)
@@ -571,7 +571,7 @@ func (e *Engine) preparedStatement(ctx *sql.Context, query string, parsed sqlpar
571571
preparedAst, preparedDataFound = e.PreparedDataCache.GetCachedStmt(ctx.Session.ID(), query)
572572
}
573573

574-
binder := planbuilder.New(ctx, e.Analyzer.Catalog, e.Parser)
574+
binder := planbuilder.New(ctx, e.Analyzer.Catalog, e.EventScheduler, e.Parser)
575575
if preparedDataFound {
576576
parsed = preparedAst
577577
binder.SetBindings(bindings)
@@ -804,6 +804,10 @@ func (e *Engine) EngineAnalyzer() *analyzer.Analyzer {
804804
return e.Analyzer
805805
}
806806

807+
func (e *Engine) EngineEventScheduler() sql.EventScheduler {
808+
return e.EventScheduler
809+
}
810+
807811
// InitializeEventScheduler initializes the EventScheduler for the engine with the given sql.Context
808812
// getter function, |ctxGetterFunc, the EventScheduler |status|, and the |period| for the event scheduler
809813
// to check for events to execute. If |period| is less than 1, then it is ignored and the default period
@@ -814,8 +818,6 @@ func (e *Engine) InitializeEventScheduler(ctxGetterFunc func() (*sql.Context, fu
814818
if err != nil {
815819
return err
816820
}
817-
818-
e.Analyzer.EventScheduler = e.EventScheduler
819821
return nil
820822
}
821823

enginetest/engine_only_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ func TestAnalyzer_Exp(t *testing.T) {
442442
require.NoError(t, err)
443443

444444
ctx := enginetest.NewContext(harness)
445-
b := planbuilder.New(ctx, e.EngineAnalyzer().Catalog, sql.NewMysqlParser())
445+
b := planbuilder.New(ctx, e.EngineAnalyzer().Catalog, e.EngineEventScheduler(), nil)
446446
parsed, _, _, _, err := b.Parse(tt.query, nil, false)
447447
require.NoError(t, err)
448448

enginetest/enginetests.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,6 @@ func TestQueryPlan(t *testing.T, harness Harness, e QueryEngine, tt queries.Quer
583583
func TestQueryPlanWithName(t *testing.T, name string, harness Harness, e QueryEngine, query, expectedPlan string, options sql.DescribeOptions) {
584584
t.Run(name, func(t *testing.T) {
585585
ctx := NewContext(harness)
586-
587586
parsed, qFlags, err := planbuilder.Parse(ctx, e.EngineAnalyzer().Catalog, query)
588587
require.NoError(t, err)
589588

enginetest/evaluation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ func injectBindVarsAndPrepare(
526526
}
527527
}
528528

529-
b := planbuilder.New(ctx, e.EngineAnalyzer().Catalog, sql.NewMysqlParser())
529+
b := planbuilder.New(ctx, e.EngineAnalyzer().Catalog, e.EngineEventScheduler(), nil)
530530
b.SetParserOptions(sql.LoadSqlMode(ctx).ParserOptions())
531531
resPlan, _, err := b.BindOnly(parsed, q, nil)
532532
if err != nil {

enginetest/plangen/cmd/plangen/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ func generatePlansForSuite(spec PlanSpec, w *bytes.Buffer) error {
165165

166166
if !tt.Skip {
167167
ctx := enginetest.NewContext(harness)
168-
binder := planbuilder.New(ctx, engine.EngineAnalyzer().Catalog, sql.NewMysqlParser())
168+
binder := planbuilder.New(ctx, engine.EngineAnalyzer().Catalog, engine.EngineEventScheduler(), nil)
169169
parsed, _, _, qFlags, err := binder.Parse(tt.Query, nil, false)
170170
if err != nil {
171171
exit(fmt.Errorf("%w\nfailed to parse query: %s", err, tt.Query))

enginetest/query_engine.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type QueryEngine interface {
2828
Query(ctx *sql.Context, query string) (sql.Schema, sql.RowIter, *sql.QueryFlags, error)
2929
// TODO: get rid of this, should not be exposed to engine tests
3030
EngineAnalyzer() *analyzer.Analyzer
31+
EngineEventScheduler() sql.EventScheduler
3132
// TODO: get rid of this, should not be exposed to engine tests
3233
EnginePreparedDataCache() *sqle.PreparedDataCache
3334
QueryWithBindings(ctx *sql.Context, query string, parsed sqlparser.Statement, bindings map[string]sqlparser.Expr, qFlags *sql.QueryFlags) (sql.Schema, sql.RowIter, *sql.QueryFlags, error)

enginetest/server_engine.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ func (s *ServerQueryEngine) EngineAnalyzer() *analyzer.Analyzer {
150150
return s.engine.Analyzer
151151
}
152152

153+
func (s *ServerQueryEngine) EngineEventScheduler() sql.EventScheduler {
154+
return s.engine.EventScheduler
155+
}
156+
153157
func (s *ServerQueryEngine) EnginePreparedDataCache() *sqle.PreparedDataCache {
154158
return s.engine.PreparedDataCache
155159
}
@@ -632,7 +636,7 @@ func convertGoSqlType(columnType *gosql.ColumnType) (sql.Type, error) {
632636
// It cannot sort user-defined binding variables (e.g. :var, :foo)
633637
func prepareBindingArgs(ctx *sql.Context, bindings map[string]sqlparser.Expr) ([]any, error) {
634638
// NOTE: using binder with nil catalog and parser since we're only using it to convert SQLVal.
635-
binder := planbuilder.New(ctx, nil, nil)
639+
binder := planbuilder.New(ctx, nil, nil, nil)
636640
numBindVars := len(bindings)
637641
args := make([]any, numBindVars)
638642
for i := 0; i < numBindVars; i++ {

eventscheduler/event_scheduler.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ func InitEventScheduler(
9191

9292
// Close closes the EventScheduler.
9393
func (es *EventScheduler) Close() {
94+
if es == nil {
95+
return
96+
}
9497
es.status = SchedulerOff
9598
es.executor.shutdown()
9699
}
@@ -99,6 +102,9 @@ func (es *EventScheduler) Close() {
99102
// This function requires valid analyzer and sql context to evaluate all events in all databases
100103
// to load enabled events to the EventScheduler.
101104
func (es *EventScheduler) TurnOnEventScheduler(a *analyzer.Analyzer) error {
105+
if es == nil {
106+
return nil
107+
}
102108
if es.status == SchedulerDisabled {
103109
return ErrEventSchedulerDisabled
104110
} else if es.status == SchedulerOn {
@@ -120,6 +126,9 @@ func (es *EventScheduler) TurnOnEventScheduler(a *analyzer.Analyzer) error {
120126

121127
// TurnOffEventScheduler is called when user sets --event-scheduler system variable to OFF or 0.
122128
func (es *EventScheduler) TurnOffEventScheduler() error {
129+
if es == nil {
130+
return nil
131+
}
123132
if es.status == SchedulerDisabled {
124133
return ErrEventSchedulerDisabled
125134
} else if es.status == SchedulerOff {
@@ -135,6 +144,9 @@ func (es *EventScheduler) TurnOffEventScheduler() error {
135144
// loadEventsAndStartEventExecutor evaluates all events in all databases and evaluates the enabled events
136145
// with valid schedule to load into the eventExecutor. Then, it starts the eventExecutor.
137146
func (es *EventScheduler) loadEventsAndStartEventExecutor(ctx *sql.Context, a *analyzer.Analyzer) error {
147+
if es == nil {
148+
return nil
149+
}
138150
es.executor.catalog = a.Catalog
139151
es.executor.loadAllEvents(ctx)
140152
go es.executor.start()
@@ -144,6 +156,9 @@ func (es *EventScheduler) loadEventsAndStartEventExecutor(ctx *sql.Context, a *a
144156
// AddEvent implements sql.EventScheduler interface.
145157
// This function is called when there is an event created at runtime.
146158
func (es *EventScheduler) AddEvent(ctx *sql.Context, edb sql.EventDatabase, details sql.EventDefinition) {
159+
if es == nil {
160+
return
161+
}
147162
if es.status == SchedulerDisabled || es.status == SchedulerOff {
148163
return
149164
}
@@ -153,6 +168,9 @@ func (es *EventScheduler) AddEvent(ctx *sql.Context, edb sql.EventDatabase, deta
153168
// UpdateEvent implements sql.EventScheduler interface.
154169
// This function is called when there is an event altered at runtime.
155170
func (es *EventScheduler) UpdateEvent(ctx *sql.Context, edb sql.EventDatabase, orgEventName string, details sql.EventDefinition) {
171+
if es == nil {
172+
return
173+
}
156174
if es.status == SchedulerDisabled || es.status == SchedulerOff {
157175
return
158176
}
@@ -163,6 +181,9 @@ func (es *EventScheduler) UpdateEvent(ctx *sql.Context, edb sql.EventDatabase, o
163181
// This function is called when there is an event dropped at runtime. This function
164182
// removes the given event if it exists in the enabled events list of the EventScheduler.
165183
func (es *EventScheduler) RemoveEvent(dbName, eventName string) {
184+
if es == nil {
185+
return
186+
}
166187
if es.status == SchedulerDisabled || es.status == SchedulerOff {
167188
return
168189
}
@@ -173,6 +194,9 @@ func (es *EventScheduler) RemoveEvent(dbName, eventName string) {
173194
// This function is called when there is a database dropped at runtime. This function
174195
// removes all events of given database that exist in the enabled events list of the EventScheduler.
175196
func (es *EventScheduler) RemoveSchemaEvents(dbName string) {
197+
if es == nil {
198+
return
199+
}
176200
if es.status == SchedulerDisabled || es.status == SchedulerOff {
177201
return
178202
}

sql/analyzer/analyzer.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,9 +286,6 @@ type Analyzer struct {
286286
Coster memo.Coster
287287
// ExecBuilder converts a sql.Node tree into an executable iterator.
288288
ExecBuilder sql.NodeExecBuilder
289-
// EventScheduler is used to communiate with the event scheduler
290-
// for any EVENT related statements. It can be nil if EventScheduler is not defined.
291-
EventScheduler sql.EventScheduler
292289
}
293290

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

sql/analyzer/apply_event_scheduler_notifier.go

Lines changed: 0 additions & 34 deletions
This file was deleted.

0 commit comments

Comments
 (0)