Skip to content

Commit 05bbcc1

Browse files
authored
Merge pull request #2926 from dolthub/zachmu/django-bug-fixes
Provide a SchemaFormatter interface to complement the SqlParser interface
2 parents a69741d + 12f6f1a commit 05bbcc1

File tree

11 files changed

+277
-202
lines changed

11 files changed

+277
-202
lines changed

memory/table.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func stripTblNames(e sql.Expression) (sql.Expression, transform.TreeIdentity, er
134134
case *expression.GetField:
135135
// strip table names
136136
ne := expression.NewGetField(e.Index(), e.Type(), e.Name(), e.IsNullable())
137-
ne = ne.WithQuotedNames(sql.GlobalParser, e.IsQuotedIdentifier())
137+
ne = ne.WithQuotedNames(sql.GlobalSchemaFormatter, e.IsQuotedIdentifier())
138138
return ne, transform.NewTree, nil
139139
default:
140140
}

sql/analyzer/analyzer.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -264,14 +264,15 @@ func (ab *Builder) Build() *Analyzer {
264264
}
265265

266266
return &Analyzer{
267-
Debug: debug || ab.debug,
268-
Verbose: verbose,
269-
contextStack: make([]string, 0),
270-
Batches: batches,
271-
Catalog: NewCatalog(ab.provider),
272-
Coster: memo.NewDefaultCoster(),
273-
ExecBuilder: rowexec.DefaultBuilder,
274-
Parser: sql.GlobalParser,
267+
Debug: debug || ab.debug,
268+
Verbose: verbose,
269+
contextStack: make([]string, 0),
270+
Batches: batches,
271+
Catalog: NewCatalog(ab.provider),
272+
Coster: memo.NewDefaultCoster(),
273+
ExecBuilder: rowexec.DefaultBuilder,
274+
Parser: sql.GlobalParser,
275+
SchemaFormatter: sql.GlobalSchemaFormatter,
275276
}
276277
}
277278

@@ -296,6 +297,8 @@ type Analyzer struct {
296297
Runner StatementRunner
297298
// Parser is the parser used to parse SQL statements.
298299
Parser sql.Parser
300+
// SchemaFormatter is used to format the schema of a node to a string.
301+
SchemaFormatter sql.SchemaFormatter
299302
}
300303

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

sql/analyzer/resolve_column_defaults.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ func quoteDefaultColumnValueNames(ctx *sql.Context, a *Analyzer, n sql.Node, _ *
315315
switch node := n.(type) {
316316
case *plan.AlterDefaultSet:
317317
eWrapper := expression.WrapExpression(node.Default)
318-
newExpr, same, err := quoteIdentifiers(a.Parser, eWrapper)
318+
newExpr, same, err := quoteIdentifiers(a.SchemaFormatter, eWrapper)
319319
if err != nil {
320320
return node, transform.SameTree, err
321321
}
@@ -335,7 +335,7 @@ func quoteDefaultColumnValueNames(ctx *sql.Context, a *Analyzer, n sql.Node, _ *
335335
return e, transform.SameTree, nil
336336
}
337337

338-
return quoteIdentifiers(a.Parser, eWrapper)
338+
return quoteIdentifiers(a.SchemaFormatter, eWrapper)
339339
})
340340
case *plan.ResolvedTable:
341341
ct, ok := node.Table.(*information_schema.ColumnsTable)
@@ -354,7 +354,7 @@ func quoteDefaultColumnValueNames(ctx *sql.Context, a *Analyzer, n sql.Node, _ *
354354
return e, transform.SameTree, nil
355355
}
356356

357-
return quoteIdentifiers(a.Parser, eWrapper)
357+
return quoteIdentifiers(a.SchemaFormatter, eWrapper)
358358
})
359359

360360
if err != nil {
@@ -376,7 +376,7 @@ func quoteDefaultColumnValueNames(ctx *sql.Context, a *Analyzer, n sql.Node, _ *
376376
})
377377
}
378378

379-
func quoteIdentifiers(parser sql.Parser, wrap *expression.Wrapper) (sql.Expression, transform.TreeIdentity, error) {
379+
func quoteIdentifiers(schemaFormatter sql.SchemaFormatter, wrap *expression.Wrapper) (sql.Expression, transform.TreeIdentity, error) {
380380
newDefault, ok := wrap.Unwrap().(*sql.ColumnDefaultValue)
381381
if !ok {
382382
return wrap, transform.SameTree, nil
@@ -388,7 +388,7 @@ func quoteIdentifiers(parser sql.Parser, wrap *expression.Wrapper) (sql.Expressi
388388

389389
newExpr, same, err := transform.Expr(newDefault.Expr, func(expr sql.Expression) (sql.Expression, transform.TreeIdentity, error) {
390390
if e, isGf := expr.(*expression.GetField); isGf {
391-
return e.WithQuotedNames(parser, true), transform.NewTree, nil
391+
return e.WithQuotedNames(schemaFormatter, true), transform.NewTree, nil
392392
}
393393
return expr, transform.SameTree, nil
394394
})

sql/auth.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,5 @@ func GetAuthorizationHandlerFactory() AuthorizationHandlerFactory {
8484
if globalAuthorizationHandlerFactory != nil {
8585
return globalAuthorizationHandlerFactory
8686
}
87-
return emptyAuthorizationHandlerFactory{}
87+
return NoopAuthorizationHandlerFactory{}
8888
}

sql/auth_empty.go

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

sql/auth_noop.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright 2024 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 sql
16+
17+
import (
18+
ast "github.com/dolthub/vitess/go/vt/sqlparser"
19+
)
20+
21+
// NoopAuthorizationHandlerFactory is the AuthorizationHandlerFactory for emptyAuthorizationHandler.
22+
type NoopAuthorizationHandlerFactory struct{}
23+
24+
var _ AuthorizationHandlerFactory = NoopAuthorizationHandlerFactory{}
25+
26+
// CreateHandler implements the AuthorizationHandlerFactory interface.
27+
func (NoopAuthorizationHandlerFactory) CreateHandler(cat Catalog) AuthorizationHandler {
28+
return NoopAuthorizationHandler{}
29+
}
30+
31+
// NoopAuthorizationHandler will always return a "true" result.
32+
type NoopAuthorizationHandler struct{}
33+
34+
var _ AuthorizationHandler = NoopAuthorizationHandler{}
35+
36+
// NewQueryState implements the AuthorizationHandler interface.
37+
func (NoopAuthorizationHandler) NewQueryState(ctx *Context) AuthorizationQueryState {
38+
return nil
39+
}
40+
41+
// HandleAuth implements the AuthorizationHandler interface.
42+
func (NoopAuthorizationHandler) HandleAuth(ctx *Context, aqs AuthorizationQueryState, auth ast.AuthInformation) error {
43+
return nil
44+
}
45+
46+
// HandleAuthNode implements the AuthorizationHandler interface.
47+
func (NoopAuthorizationHandler) HandleAuthNode(ctx *Context, state AuthorizationQueryState, node AuthorizationCheckerNode) error {
48+
return nil
49+
}
50+
51+
// CheckDatabase implements the AuthorizationHandler interface.
52+
func (NoopAuthorizationHandler) CheckDatabase(ctx *Context, aqs AuthorizationQueryState, dbName string) error {
53+
return nil
54+
}
55+
56+
// CheckSchema implements the AuthorizationHandler interface.
57+
func (NoopAuthorizationHandler) CheckSchema(ctx *Context, aqs AuthorizationQueryState, dbName string, schemaName string) error {
58+
return nil
59+
}
60+
61+
// CheckTable implements the AuthorizationHandler interface.
62+
func (NoopAuthorizationHandler) CheckTable(ctx *Context, aqs AuthorizationQueryState, dbName string, schemaName string, tableName string) error {
63+
return nil
64+
}

sql/expression/get_field.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ type GetField struct {
3838
fieldType2 sql.Type2
3939
nullable bool
4040

41-
// parser is the parser used to parse the expression and print it
42-
parser sql.Parser
41+
// schemaFormatter is the schemaFormatter used to quote field names
42+
schemaFormatter sql.SchemaFormatter
4343

4444
// quoteName indicates whether the field name should be quoted when printed with String()
4545
quoteName bool
@@ -170,7 +170,7 @@ func (p *GetField) String() string {
170170
// stripped away. The output of this method is load-bearing in many places of analysis and execution.
171171
if p.table == "" {
172172
if p.quoteName {
173-
return p.parser.QuoteIdentifier(p.name)
173+
return p.schemaFormatter.QuoteIdentifier(p.name)
174174
}
175175
return p.name
176176
}
@@ -197,10 +197,10 @@ func (p *GetField) WithIndex(n int) sql.Expression {
197197
}
198198

199199
// WithQuotedNames returns a copy of this expression with the backtick names flag set to the given value.
200-
func (p *GetField) WithQuotedNames(parser sql.Parser, quoteNames bool) *GetField {
200+
func (p *GetField) WithQuotedNames(formatter sql.SchemaFormatter, quoteNames bool) *GetField {
201201
p2 := *p
202202
p2.quoteName = quoteNames
203-
p2.parser = parser
203+
p2.schemaFormatter = formatter
204204
return &p2
205205
}
206206

0 commit comments

Comments
 (0)