Skip to content

Commit a76df51

Browse files
committed
Added additional analyzer hooks for integrators
1 parent 6e2cb91 commit a76df51

File tree

7 files changed

+41
-11
lines changed

7 files changed

+41
-11
lines changed

sql/analyzer/analyzer.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,12 @@ type Builder struct {
7979
// NewBuilder creates a new Builder from a specific catalog.
8080
// This builder allow us add custom Rules and modify some internal properties.
8181
func NewBuilder(pro sql.DatabaseProvider) *Builder {
82+
allBeforeDefault := make([]Rule, len(OnceBeforeDefault)+len(AlwaysBeforeDefault))
83+
copy(allBeforeDefault, OnceBeforeDefault)
84+
copy(allBeforeDefault[len(OnceBeforeDefault):], AlwaysBeforeDefault)
8285
return &Builder{
8386
provider: pro,
84-
onceBeforeRules: OnceBeforeDefault,
87+
onceBeforeRules: allBeforeDefault,
8588
defaultRules: DefaultRules,
8689
onceAfterRules: OnceAfterDefault,
8790
validationRules: DefaultValidationRules,

sql/analyzer/node_batches.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ func getBatchesForNode(n sql.Node, orig []*Batch) ([]*Batch, bool) {
1616
case *plan.InsertInto:
1717
if n.LiteralValueSource {
1818
return []*Batch{
19+
{
20+
Desc: "alwaysBeforeDefault",
21+
Iterations: 1,
22+
Rules: AlwaysBeforeDefault,
23+
},
1924
{
2025
Desc: "simpleInsert",
2126
Iterations: 1,
@@ -48,6 +53,11 @@ func getBatchesForNode(n sql.Node, orig []*Batch) ([]*Batch, bool) {
4853
case *plan.Update:
4954
if n.HasSingleRel && !n.IsJoin {
5055
return []*Batch{
56+
{
57+
Desc: "alwaysBeforeDefault",
58+
Iterations: 1,
59+
Rules: AlwaysBeforeDefault,
60+
},
5161
{
5262
Desc: "simpleUpdate",
5363
Iterations: 1,
@@ -88,6 +98,11 @@ func getBatchesForNode(n sql.Node, orig []*Batch) ([]*Batch, bool) {
8898
case *plan.DeleteFrom:
8999
if !n.HasExplicitTargets() && n.RefsSingleRel {
90100
return []*Batch{
101+
{
102+
Desc: "alwaysBeforeDefault",
103+
Iterations: 1,
104+
Rules: AlwaysBeforeDefault,
105+
},
91106
{
92107
Desc: "simpleDelete",
93108
Iterations: 1,

sql/analyzer/rule_ids.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const (
4141
resolveTableFunctionsId // resolveTableFunctions
4242
resolveDeclarationsId // resolveDeclarations
4343
resolveColumnDefaultsId // resolveColumnDefaults
44-
validateColumnDefaultsId // validateColumnDefaults
44+
ValidateColumnDefaultsId // validateColumnDefaults
4545
validateCreateTriggerId // validateCreateTrigger
4646
validateCreateProcedureId // validateCreateProcedure
4747
resolveCreateProcedureId // resolveCreateProcedure
@@ -128,7 +128,7 @@ const (
128128
validateGroupById // validateGroupBy
129129
validateSchemaSourceId // validateSchemaSource
130130
validateIndexCreationId // validateIndexCreation
131-
validateOperandsId // validateOperands
131+
ValidateOperandsId // validateOperands
132132
validateCaseResultTypesId // validateCaseResultTypes
133133
validateIntervalUsageId // validateIntervalUsage
134134
validateExplodeUsageId // validateExplodeUsage

sql/analyzer/ruleid_string.go

Lines changed: 3 additions & 3 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: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ var OnceBeforeDefault = []Rule{
5454
{resolveUnionsId, resolveUnions},
5555
{resolveDescribeQueryId, resolveDescribeQuery}, //TODO
5656
{validateCreateTriggerId, validateCreateTrigger},
57-
{validateColumnDefaultsId, validateColumnDefaults},
57+
{ValidateColumnDefaultsId, validateColumnDefaults},
5858
{validateReadOnlyDatabaseId, validateReadOnlyDatabase},
5959
{validateReadOnlyTransactionId, validateReadOnlyTransaction},
6060
{validateDatabaseSetId, validateDatabaseSet},
@@ -65,6 +65,12 @@ var OnceBeforeDefault = []Rule{
6565
{hoistOutOfScopeFiltersId, hoistOutOfScopeFilters},
6666
}
6767

68+
// AlwaysBeforeDefault contains the rules to be applied just once before the
69+
// DefaultRules. These are an extension of the OnceBeforeDefault rules that
70+
// will always apply to nodes, unlike the OnceBeforeDefault rules that may
71+
// be excluded depending on the node. This is only used by integrators.
72+
var AlwaysBeforeDefault []Rule
73+
6874
// DefaultRules to apply when analyzing nodes.
6975
var DefaultRules = []Rule{
7076
{validateStarExpressionsId, validateStarExpressions}, //TODO
@@ -105,7 +111,7 @@ var DefaultValidationRules = []Rule{
105111
{validateGroupById, validateGroupBy},
106112
{validateSchemaSourceId, validateSchemaSource},
107113
{validateIndexCreationId, validateIndexCreation},
108-
{validateOperandsId, validateOperands},
114+
{ValidateOperandsId, validateOperands},
109115
{validateIntervalUsageId, validateIntervalUsage},
110116
{validateSubqueryColumnsId, validateSubqueryColumns},
111117
{validateUnionSchemasMatchId, validateUnionSchemasMatch},

sql/analyzer/validation_rules_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ func TestValidateOperands(t *testing.T) {
483483
},
484484
}
485485

486-
rule := getValidationRule(validateOperandsId)
486+
rule := getValidationRule(ValidateOperandsId)
487487
for _, tt := range testCases {
488488
t.Run(tt.name, func(t *testing.T) {
489489
require := require.New(t)

sql/types/typecheck.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,14 @@ func IsSigned(t sql.Type) bool {
110110

111111
// IsText checks if t is a CHAR, VARCHAR, TEXT, BINARY, VARBINARY, or BLOB (including TEXT and BLOB variants).
112112
func IsText(t sql.Type) bool {
113-
_, ok := t.(StringType)
114-
return ok
113+
if _, ok := t.(StringType); ok {
114+
return ok
115+
}
116+
if extendedType, ok := t.(ExtendedType); ok {
117+
_, isString := extendedType.Zero().(string)
118+
return isString
119+
}
120+
return false
115121
}
116122

117123
// IsTextBlob checks if t is one of the TEXTs or BLOBs.

0 commit comments

Comments
 (0)