|
| 1 | +// Copyright 2026 PingCAP, 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 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +package event |
| 15 | + |
| 16 | +import ( |
| 17 | + "strings" |
| 18 | + |
| 19 | + "github.com/pingcap/ticdc/pkg/errors" |
| 20 | + "github.com/pingcap/tidb/pkg/parser" |
| 21 | + "github.com/pingcap/tidb/pkg/parser/ast" |
| 22 | +) |
| 23 | + |
| 24 | +// NormalizeCreateViewQueryWithStoredSelect replaces the SELECT body in a |
| 25 | +// CREATE VIEW query with TiDB's stored View.SelectStmt when the stored SELECT |
| 26 | +// carries information that the original query text does not carry. |
| 27 | +// |
| 28 | +// TiDB persists the normalized SELECT body of a view in TableInfo.View.SelectStmt |
| 29 | +// when executing CREATE VIEW, so this field can carry resolved source-table |
| 30 | +// references even if job.Query keeps the original session-level text. |
| 31 | +func NormalizeCreateViewQueryWithStoredSelect(query string, storedSelectStmt string, currentSchema string) (string, error) { |
| 32 | + if query == "" || storedSelectStmt == "" { |
| 33 | + return query, nil |
| 34 | + } |
| 35 | + |
| 36 | + stmt, err := parser.New().ParseOneStmt(query, "", "") |
| 37 | + if err != nil { |
| 38 | + return query, errors.WrapError(errors.ErrDDLEventError, err) |
| 39 | + } |
| 40 | + createViewStmt, ok := stmt.(*ast.CreateViewStmt) |
| 41 | + if !ok { |
| 42 | + return query, nil |
| 43 | + } |
| 44 | + |
| 45 | + selectStmt, err := parser.New().ParseOneStmt(storedSelectStmt, "", "") |
| 46 | + if err != nil { |
| 47 | + return query, errors.WrapError(errors.ErrDDLEventError, err) |
| 48 | + } |
| 49 | + if !normalizeCreateViewSelect(selectStmt, currentSchema) { |
| 50 | + return query, nil |
| 51 | + } |
| 52 | + |
| 53 | + createViewStmt.Select = selectStmt |
| 54 | + normalizedQuery, err := Restore(createViewStmt) |
| 55 | + if err != nil { |
| 56 | + return query, errors.WrapError(errors.ErrDDLEventError, err) |
| 57 | + } |
| 58 | + return normalizedQuery, nil |
| 59 | +} |
| 60 | + |
| 61 | +type createViewSelectNormalizer struct { |
| 62 | + currentSchema string |
| 63 | + currentSchemaOnly bool |
| 64 | + changed bool |
| 65 | + scopes []createViewSelectScope |
| 66 | +} |
| 67 | + |
| 68 | +type createViewSelectScope struct { |
| 69 | + aliases map[string]struct{} |
| 70 | + tableByName map[string]string |
| 71 | + ambiguousTables map[string]struct{} |
| 72 | +} |
| 73 | + |
| 74 | +// normalizeCreateViewSelect returns true when CREATE VIEW should use the stored |
| 75 | +// SELECT body. It also turns unaliased table-qualified column references into |
| 76 | +// schema-qualified references: `orders`.`id` with FROM `source_db`.`orders` |
| 77 | +// becomes `source_db`.`orders`.`id`. Explicit alias references are preserved. |
| 78 | +func normalizeCreateViewSelect(selectStmt ast.StmtNode, currentSchema string) bool { |
| 79 | + normalizer := &createViewSelectNormalizer{ |
| 80 | + currentSchema: currentSchema, |
| 81 | + currentSchemaOnly: true, |
| 82 | + scopes: make([]createViewSelectScope, 0), |
| 83 | + } |
| 84 | + selectStmt.Accept(normalizer) |
| 85 | + return !normalizer.currentSchemaOnly || normalizer.changed |
| 86 | +} |
| 87 | + |
| 88 | +func (n *createViewSelectNormalizer) Enter(in ast.Node) (ast.Node, bool) { |
| 89 | + switch v := in.(type) { |
| 90 | + case *ast.SelectStmt: |
| 91 | + n.scopes = append(n.scopes, buildCreateViewSelectScope(v)) |
| 92 | + case *ast.TableName: |
| 93 | + if v.Schema.O != "" && !strings.EqualFold(v.Schema.O, n.currentSchema) { |
| 94 | + n.currentSchemaOnly = false |
| 95 | + } |
| 96 | + case *ast.ColumnName: |
| 97 | + n.qualifyColumnName(v) |
| 98 | + } |
| 99 | + return in, false |
| 100 | +} |
| 101 | + |
| 102 | +func (n *createViewSelectNormalizer) Leave(in ast.Node) (ast.Node, bool) { |
| 103 | + if _, ok := in.(*ast.SelectStmt); ok { |
| 104 | + n.scopes = n.scopes[:len(n.scopes)-1] |
| 105 | + } |
| 106 | + return in, true |
| 107 | +} |
| 108 | + |
| 109 | +func (n *createViewSelectNormalizer) qualifyColumnName(c *ast.ColumnName) { |
| 110 | + if len(n.scopes) == 0 || c == nil || c.Schema.O != "" || c.Table.O == "" { |
| 111 | + return |
| 112 | + } |
| 113 | + |
| 114 | + scope := n.scopes[len(n.scopes)-1] |
| 115 | + tableKey := strings.ToLower(c.Table.O) |
| 116 | + if _, ok := scope.aliases[tableKey]; ok { |
| 117 | + return |
| 118 | + } |
| 119 | + if _, ok := scope.ambiguousTables[tableKey]; ok { |
| 120 | + return |
| 121 | + } |
| 122 | + schema, ok := scope.tableByName[tableKey] |
| 123 | + if !ok { |
| 124 | + return |
| 125 | + } |
| 126 | + c.Schema = ast.NewCIStr(schema) |
| 127 | + n.changed = true |
| 128 | +} |
| 129 | + |
| 130 | +func buildCreateViewSelectScope(selectStmt *ast.SelectStmt) createViewSelectScope { |
| 131 | + scope := createViewSelectScope{ |
| 132 | + aliases: make(map[string]struct{}), |
| 133 | + tableByName: make(map[string]string), |
| 134 | + ambiguousTables: make(map[string]struct{}), |
| 135 | + } |
| 136 | + if selectStmt == nil || selectStmt.From == nil || selectStmt.From.TableRefs == nil { |
| 137 | + return scope |
| 138 | + } |
| 139 | + collectCreateViewSelectTables(selectStmt.From.TableRefs, &scope) |
| 140 | + return scope |
| 141 | +} |
| 142 | + |
| 143 | +func collectCreateViewSelectTables(node ast.ResultSetNode, scope *createViewSelectScope) { |
| 144 | + switch v := node.(type) { |
| 145 | + case *ast.Join: |
| 146 | + if v.Left != nil { |
| 147 | + collectCreateViewSelectTables(v.Left, scope) |
| 148 | + } |
| 149 | + if v.Right != nil { |
| 150 | + collectCreateViewSelectTables(v.Right, scope) |
| 151 | + } |
| 152 | + case *ast.TableSource: |
| 153 | + if v.AsName.O != "" { |
| 154 | + scope.aliases[strings.ToLower(v.AsName.O)] = struct{}{} |
| 155 | + return |
| 156 | + } |
| 157 | + tableName, ok := v.Source.(*ast.TableName) |
| 158 | + if !ok || tableName.Schema.O == "" || tableName.Name.O == "" { |
| 159 | + return |
| 160 | + } |
| 161 | + tableKey := strings.ToLower(tableName.Name.O) |
| 162 | + if _, ambiguous := scope.ambiguousTables[tableKey]; ambiguous { |
| 163 | + return |
| 164 | + } |
| 165 | + if _, exists := scope.tableByName[tableKey]; exists { |
| 166 | + delete(scope.tableByName, tableKey) |
| 167 | + scope.ambiguousTables[tableKey] = struct{}{} |
| 168 | + return |
| 169 | + } |
| 170 | + scope.tableByName[tableKey] = tableName.Schema.O |
| 171 | + } |
| 172 | +} |
0 commit comments