當前位置: 首頁>>代碼示例>>Golang>>正文


Golang UnionStmt.GetResultFields方法代碼示例

本文整理匯總了Golang中github.com/pingcap/tidb/ast.UnionStmt.GetResultFields方法的典型用法代碼示例。如果您正苦於以下問題:Golang UnionStmt.GetResultFields方法的具體用法?Golang UnionStmt.GetResultFields怎麽用?Golang UnionStmt.GetResultFields使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/pingcap/tidb/ast.UnionStmt的用法示例。


在下文中一共展示了UnionStmt.GetResultFields方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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
}
開發者ID:XuHuaiyu,項目名稱:tidb,代碼行數:54,代碼來源:planbuilder.go


注:本文中的github.com/pingcap/tidb/ast.UnionStmt.GetResultFields方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。