Ejemplo n.º 1
0
// handleTableSources checks name duplication
// and puts the table source in current resolverContext.
func (nr *nameResolver) handleTableSource(ts *ast.TableSource) {
	for _, v := range ts.GetResultFields() {
		v.TableAsName = ts.AsName
	}
	var name string
	if ts.AsName.L != "" {
		name = ts.AsName.L
	} else {
		tableName := ts.Source.(*ast.TableName)
		name = nr.tableUniqueName(tableName.Schema, tableName.Name)
	}
	ctx := nr.currentContext()
	if _, ok := ctx.tableMap[name]; ok {
		nr.Err = errors.Errorf("duplicated table/alias name %s", name)
		return
	}
	ctx.tableMap[name] = len(ctx.tables)
	ctx.tables = append(ctx.tables, ts)
	return
}
Ejemplo n.º 2
0
// handleTableSources checks name duplication
// and puts the table source in current resolverContext.
// Note:
// "select * from t as a join (select 1) as a;" is not duplicate.
// "select * from t as a join t as a;" is duplicate.
// "select * from (select 1) as a join (select 1) as a;" is duplicate.
func (nr *nameResolver) handleTableSource(ts *ast.TableSource) {
	for _, v := range ts.GetResultFields() {
		v.TableAsName = ts.AsName
	}
	ctx := nr.currentContext()
	switch ts.Source.(type) {
	case *ast.TableName:
		var name string
		if ts.AsName.L != "" {
			name = ts.AsName.L
		} else {
			tableName := ts.Source.(*ast.TableName)
			name = nr.tableUniqueName(tableName.Schema, tableName.Name)
		}
		if _, ok := ctx.tableMap[name]; ok {
			nr.Err = errors.Errorf("duplicated table/alias name %s", name)
			return
		}
		ctx.tableMap[name] = len(ctx.tables)
	case *ast.SelectStmt:
		name := ts.AsName.L
		if _, ok := ctx.derivedTableMap[name]; ok {
			nr.Err = errors.Errorf("duplicated table/alias name %s", name)
			return
		}
		ctx.derivedTableMap[name] = len(ctx.tables)
	}
	dupNames := make(map[string]struct{}, len(ts.GetResultFields()))
	for _, f := range ts.GetResultFields() {
		// duplicate column name in one table is not allowed.
		// "select * from (select 1, 1) as a;" is duplicate.
		name := f.ColumnAsName.L
		if name == "" {
			name = f.Column.Name.L
		}
		if _, ok := dupNames[name]; ok {
			nr.Err = errors.Errorf("Duplicate column name '%s'", name)
			return
		}
		dupNames[name] = struct{}{}
	}
	ctx.tables = append(ctx.tables, ts)
	return
}