@@ -36,14 +36,14 @@ func (s tableNameSet) add(tbl filter.Table) {
3636 if s == nil {
3737 return
3838 }
39- s [tbl ] = struct {}{}
39+ s [normalizeTableName ( tbl . Schema , tbl . Name ) ] = struct {}{}
4040}
4141
4242func (s tableNameSet ) has (tbl filter.Table ) bool {
4343 if s == nil {
4444 return false
4545 }
46- _ , ok := s [tbl ]
46+ _ , ok := s [normalizeTableName ( tbl . Schema , tbl . Name ) ]
4747 return ok
4848}
4949
@@ -85,24 +85,75 @@ type SchemaImportPlan struct {
8585type viewDependencyCollector struct {
8686 currentSchema string
8787 deps tableNameSet
88+ cteNameScopes []map [string ]struct {}
89+ }
90+
91+ func (c * viewDependencyCollector ) pushCTEScope () {
92+ c .cteNameScopes = append (c .cteNameScopes , make (map [string ]struct {}))
93+ }
94+
95+ func (c * viewDependencyCollector ) popCTEScope () {
96+ if len (c .cteNameScopes ) == 0 {
97+ return
98+ }
99+ c .cteNameScopes = c .cteNameScopes [:len (c .cteNameScopes )- 1 ]
100+ }
101+
102+ func (c * viewDependencyCollector ) recordCTEName (name string ) {
103+ if len (c .cteNameScopes ) == 0 {
104+ return
105+ }
106+ c .cteNameScopes [len (c .cteNameScopes )- 1 ][strings .ToLower (name )] = struct {}{}
107+ }
108+
109+ func (c * viewDependencyCollector ) isCTEName (name string ) bool {
110+ normalized := strings .ToLower (name )
111+ for i := len (c .cteNameScopes ) - 1 ; i >= 0 ; i -- {
112+ if _ , ok := c.cteNameScopes [i ][normalized ]; ok {
113+ return true
114+ }
115+ }
116+ return false
88117}
89118
90119func (c * viewDependencyCollector ) Enter (n ast.Node ) (ast.Node , bool ) {
91- tbl , ok := n .(* ast.TableName )
92- if ! ok {
120+ switch node := n .(type ) {
121+ case * ast.SelectStmt :
122+ if node .With != nil {
123+ c .pushCTEScope ()
124+ }
93125 return n , false
94- }
126+ case * ast.CommonTableExpression :
127+ if node .IsRecursive {
128+ c .recordCTEName (node .Name .O )
129+ }
130+ return n , false
131+ case * ast.TableName :
132+ if node .Schema .O == "" && c .isCTEName (node .Name .O ) {
133+ return n , true
134+ }
95135
96- schema := tbl .Schema .O
97- if schema == "" {
98- // Dumpling may omit the schema for same-database references.
99- schema = c .currentSchema
136+ schema := node .Schema .O
137+ if schema == "" {
138+ // Dumpling may omit the schema for same-database references.
139+ schema = c .currentSchema
140+ }
141+ c .deps .add (filterTableName (schema , node .Name .O ))
142+ return n , true
143+ default :
144+ return n , false
100145 }
101- c .deps .add (filter.Table {Schema : schema , Name : tbl .Name .O })
102- return n , true
103146}
104147
105- func (* viewDependencyCollector ) Leave (n ast.Node ) (ast.Node , bool ) {
148+ func (c * viewDependencyCollector ) Leave (n ast.Node ) (ast.Node , bool ) {
149+ switch node := n .(type ) {
150+ case * ast.CommonTableExpression :
151+ c .recordCTEName (node .Name .O )
152+ case * ast.SelectStmt :
153+ if node .With != nil {
154+ c .popCTEScope ()
155+ }
156+ }
106157 return n , true
107158}
108159
@@ -235,30 +286,33 @@ func buildViewRestorePlan(parsedViews []*parsedViewSchema, dumpTables tableNameS
235286 }
236287
237288 for _ , parsed := range parsedViews {
238- if _ , exists := plan .nodes [parsed .key ]; exists {
289+ normalizedKey := normalizeTableName (parsed .key .Schema , parsed .key .Name )
290+ if _ , exists := plan .nodes [normalizedKey ]; exists {
239291 return nil , errors .Errorf ("duplicate view definition for %s" , parsed .key .String ())
240292 }
241- plan .nodes [parsed . key ] = & viewNode {
293+ plan .nodes [normalizedKey ] = & viewNode {
242294 key : parsed .key ,
243295 deps : append ([]filter.Table (nil ), parsed .deps ... ),
244296 createSQL : parsed .createSQL ,
245297 }
246298 }
247299
248300 for _ , node := range plan .nodes {
301+ nodeKey := normalizeTableName (node .key .Schema , node .key .Name )
249302 for _ , dep := range node .deps {
250- if dep == node .key {
303+ normalizedDep := normalizeTableName (dep .Schema , dep .Name )
304+ if normalizedDep == nodeKey {
251305 return nil , errors .Errorf ("cyclic view dependency detected for %s" , node .key .String ())
252306 }
253- if depNode , ok := plan .nodes [dep ]; ok {
307+ if depNode , ok := plan .nodes [normalizedDep ]; ok {
254308 node .indegree ++
255- depNode .dependents = append (depNode .dependents , node . key )
309+ depNode .dependents = append (depNode .dependents , nodeKey )
256310 continue
257311 }
258- if dumpTables .has (dep ) {
312+ if dumpTables .has (normalizedDep ) {
259313 continue
260314 }
261- node .externalDeps = append (node .externalDeps , dep )
315+ node .externalDeps = append (node .externalDeps , normalizedDep )
262316 }
263317 sort .Slice (node .dependents , func (i , j int ) bool {
264318 if node .dependents [i ].Schema != node .dependents [j ].Schema {
0 commit comments