github.com/cockroachdb/cockroachdb-parser@v0.23.3-0.20240213214944-911057d40c9a/pkg/sql/sem/tree/union.go (about) 1 // Copyright 2012, Google Inc. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in licenses/BSD-vitess.txt. 4 5 // Portions of this file are additionally subject to the following 6 // license and copyright. 7 // 8 // Copyright 2015 The Cockroach Authors. 9 // 10 // Use of this software is governed by the Business Source License 11 // included in the file licenses/BSL.txt. 12 // 13 // As of the Change Date specified in that file, in accordance with 14 // the Business Source License, use of this software will be governed 15 // by the Apache License, Version 2.0, included in the file 16 // licenses/APL.txt. 17 18 // This code was derived from https://github.com/youtube/vitess. 19 20 package tree 21 22 import "fmt" 23 24 // UnionClause represents a UNION statement. 25 type UnionClause struct { 26 Type UnionType 27 Left, Right *Select 28 All bool 29 } 30 31 // UnionType represents one of the three set operations in sql. 32 type UnionType int 33 34 // Union.Type 35 const ( 36 UnionOp UnionType = iota 37 IntersectOp 38 ExceptOp 39 ) 40 41 var unionTypeName = [...]string{ 42 UnionOp: "UNION", 43 IntersectOp: "INTERSECT", 44 ExceptOp: "EXCEPT", 45 } 46 47 func (i UnionType) String() string { 48 if i < 0 || i > UnionType(len(unionTypeName)-1) { 49 return fmt.Sprintf("UnionType(%d)", i) 50 } 51 return unionTypeName[i] 52 } 53 54 // Format implements the NodeFormatter interface. 55 func (node *UnionClause) Format(ctx *FmtCtx) { 56 ctx.FormatNode(node.Left) 57 ctx.WriteByte(' ') 58 ctx.WriteString(node.Type.String()) 59 if node.All { 60 ctx.WriteString(" ALL") 61 } 62 ctx.WriteByte(' ') 63 ctx.FormatNode(node.Right) 64 }