本文整理汇总了Golang中github.com/pingcap/tidb/ast.UnionStmt类的典型用法代码示例。如果您正苦于以下问题:Golang UnionStmt类的具体用法?Golang UnionStmt怎么用?Golang UnionStmt使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了UnionStmt类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: buildUnion
func (b *planBuilder) buildUnion(union *ast.UnionStmt) Plan {
sels := make([]Plan, len(union.SelectList.Selects))
for i, sel := range union.SelectList.Selects {
sels[i] = b.buildSelect(sel)
}
var p Plan
p = &Union{
Selects: sels,
}
unionFields := union.GetResultFields()
for _, sel := range sels {
for i, f := range sel.Fields() {
if i == len(unionFields) {
b.err = errors.New("The used SELECT statements have a different number of columns")
return nil
}
uField := unionFields[i]
/*
* The lengths of the columns in the UNION result take into account the values retrieved by all of the SELECT statements
* SELECT REPEAT('a',1) UNION SELECT REPEAT('b',10);
* +---------------+
* | REPEAT('a',1) |
* +---------------+
* | a |
* | bbbbbbbbbb |
* +---------------+
*/
if f.Column.Flen > uField.Column.Flen {
uField.Column.Flen = f.Column.Flen
}
// For select nul union select "abc", we should not convert "abc" to nil.
// And the result field type should be VARCHAR.
if uField.Column.Tp == 0 || uField.Column.Tp == mysql.TypeNull {
uField.Column.Tp = f.Column.Tp
}
}
addChild(p, sel)
}
for _, v := range unionFields {
v.Expr.SetType(&v.Column.FieldType)
}
p.SetFields(unionFields)
if union.Distinct {
p = b.buildDistinct(p)
}
if union.OrderBy != nil {
p = b.buildSort(p, union.OrderBy.Items)
}
if union.Limit != nil {
p = b.buildLimit(p, union.Limit)
}
return p
}
示例2: convertUnion
func convertUnion(converter *expressionConverter, u *ast.UnionStmt) (*stmts.UnionStmt, error) {
oldUnion := &stmts.UnionStmt{
Text: u.Text(),
}
oldUnion.Selects = make([]*stmts.SelectStmt, len(u.Selects))
oldUnion.Distincts = make([]bool, len(u.Selects)-1)
if u.Distinct {
for i := range oldUnion.Distincts {
oldUnion.Distincts[i] = true
}
}
for i, val := range u.Selects {
oldSelect, err := convertSelect(converter, val)
if err != nil {
return nil, errors.Trace(err)
}
oldUnion.Selects[i] = oldSelect
}
if u.OrderBy != nil {
oldOrderBy, err := convertOrderBy(converter, u.OrderBy)
if err != nil {
return nil, errors.Trace(err)
}
oldUnion.OrderBy = oldOrderBy
}
if u.Limit != nil {
if u.Limit.Offset > 0 {
oldUnion.Offset = &rsets.OffsetRset{Count: u.Limit.Offset}
}
if u.Limit.Count > 0 {
oldUnion.Limit = &rsets.LimitRset{Count: u.Limit.Count}
}
}
// Union order by can not converted to old because it is pushed to select statements.
return oldUnion, nil
}