@@ -173,11 +173,23 @@ func (p *Parser) parseSelectWithUnion() *ast.SelectWithUnionQuery {
173173 p .nextToken ()
174174 }
175175 query .UnionModes = append (query .UnionModes , mode )
176- sel := p .parseSelect ()
177- if sel == nil {
178- break
176+
177+ // Handle parenthesized subqueries: UNION ALL (SELECT ... UNION ALL SELECT ...)
178+ if p .currentIs (token .LPAREN ) {
179+ p .nextToken () // skip (
180+ nested := p .parseSelectWithUnion ()
181+ p .expect (token .RPAREN )
182+ // Flatten nested union selects into current query
183+ for _ , s := range nested .Selects {
184+ query .Selects = append (query .Selects , s )
185+ }
186+ } else {
187+ sel := p .parseSelect ()
188+ if sel == nil {
189+ break
190+ }
191+ query .Selects = append (query .Selects , sel )
179192 }
180- query .Selects = append (query .Selects , sel )
181193 }
182194
183195 return query
@@ -917,6 +929,17 @@ func (p *Parser) parseInsert() *ast.InsertQuery {
917929 }
918930 } else if p .currentIs (token .SELECT ) || p .currentIs (token .WITH ) {
919931 ins .Select = p .parseSelectWithUnion ()
932+ // If the SELECT has settings, mark the INSERT as having settings too
933+ if ins .Select != nil {
934+ if sel , ok := ins .Select .(* ast.SelectWithUnionQuery ); ok && sel != nil && len (sel .Selects ) > 0 {
935+ lastSel := sel .Selects [len (sel .Selects )- 1 ]
936+ if lastSel != nil {
937+ if selQuery , ok := lastSel .(* ast.SelectQuery ); ok && selQuery != nil && len (selQuery .Settings ) > 0 {
938+ ins .HasSettings = true
939+ }
940+ }
941+ }
942+ }
920943 }
921944
922945 // Parse FORMAT (format names can be keywords like Null, JSON, etc.)
@@ -1314,21 +1337,27 @@ func (p *Parser) parseDataType() *ast.DataType {
13141337
13151338 // Parse type parameters
13161339 if p .currentIs (token .LPAREN ) {
1340+ dt .HasParentheses = true
13171341 p .nextToken ()
13181342
13191343 // Special handling for Nested type - it contains column declarations, not just types
13201344 if strings .ToUpper (dt .Name ) == "NESTED" {
13211345 for ! p .currentIs (token .RPAREN ) && ! p .currentIs (token .EOF ) {
13221346 // Parse as column name + type
13231347 if p .currentIs (token .IDENT ) || p .current .Token .IsKeyword () {
1348+ pos := p .current .Pos
13241349 colName := p .current .Value
13251350 p .nextToken ()
13261351 // Parse the type for this column
13271352 colType := p .parseDataType ()
13281353 if colType != nil {
1329- // Wrap in a special format or just store as data type
1330- colType .Name = colName + " " + colType .Name
1331- dt .Parameters = append (dt .Parameters , colType )
1354+ // Use NameTypePair for Nested column declarations
1355+ ntp := & ast.NameTypePair {
1356+ Position : pos ,
1357+ Name : colName ,
1358+ Type : colType ,
1359+ }
1360+ dt .Parameters = append (dt .Parameters , ntp )
13321361 }
13331362 }
13341363 if p .currentIs (token .COMMA ) {
0 commit comments