select *: address review comments

Signed-off-by: Sugu Sougoumarane <ssougou@gmail.com>
This commit is contained in:
Sugu Sougoumarane 2018-11-18 08:56:56 -08:00
Родитель cf87a287cd
Коммит b30a5c6a1a
5 изменённых файлов: 47 добавлений и 30 удалений

Просмотреть файл

@ -178,8 +178,8 @@
"Name": "user",
"Sharded": true
},
"Query": "select authoritative.user_id as user_id, authoritative.col1 as col1, authoritative.col2 as col2 from authoritative order by user_id asc",
"FieldQuery": "select authoritative.user_id as user_id, authoritative.col1 as col1, authoritative.col2 as col2 from authoritative where 1 != 1",
"Query": "select user_id, col1, col2 from authoritative order by user_id asc",
"FieldQuery": "select user_id, col1, col2 from authoritative where 1 != 1",
"OrderBy": [
{
"Col": 0,

Просмотреть файл

@ -207,8 +207,8 @@
"Name": "user",
"Sharded": true
},
"Query": "select authoritative.user_id as user_id, authoritative.col1 as col1, authoritative.col2 as col2 from authoritative",
"FieldQuery": "select authoritative.user_id as user_id, authoritative.col1 as col1, authoritative.col2 as col2 from authoritative where 1 != 1"
"Query": "select user_id, col1, col2 from authoritative",
"FieldQuery": "select user_id, col1, col2 from authoritative where 1 != 1"
}
}

Просмотреть файл

@ -257,18 +257,33 @@ func (pb *primitiveBuilder) expandStar(inrcs []*resultColumn, expr *sqlparser.St
return inrcs, false, nil
}
}
singleTable := false
if len(tables) == 1 {
singleTable = true
}
for _, t := range tables {
for _, col := range t.orderdColumns {
// If a and b have id as their column, then
// select * from a join b should result in
// select a.id as id, b.id as id from a join b.
expr := &sqlparser.AliasedExpr{
Expr: &sqlparser.ColName{
Metadata: t.columns[col.Lowered()],
Name: col,
Qualifier: t.alias,
},
As: col,
for _, col := range t.columnNames {
var expr *sqlparser.AliasedExpr
if singleTable {
// If there's only one table, we use unqualifed column names.
expr = &sqlparser.AliasedExpr{
Expr: &sqlparser.ColName{
Metadata: t.columns[col.Lowered()],
Name: col,
},
}
} else {
// If a and b have id as their column, then
// select * from a join b should result in
// select a.id as id, b.id as id from a join b.
expr = &sqlparser.AliasedExpr{
Expr: &sqlparser.ColName{
Metadata: t.columns[col.Lowered()],
Name: col,
Qualifier: t.alias,
},
As: col,
}
}
rc, _, err := pb.bldr.PushSelect(expr, t.origin)
if err != nil {
@ -289,7 +304,7 @@ func (pb *primitiveBuilder) expandStar(inrcs []*resultColumn, expr *sqlparser.St
if !t.isAuthoritative {
return inrcs, false, nil
}
for _, col := range t.orderdColumns {
for _, col := range t.columnNames {
expr := &sqlparser.AliasedExpr{
Expr: &sqlparser.ColName{
Metadata: t.columns[col.Lowered()],

Просмотреть файл

@ -50,8 +50,8 @@ var errNoTable = errors.New("no table info")
// which is later used to determine if the subquery can be
// merged with an outer route.
type symtab struct {
tables map[sqlparser.TableName]*table
orderedTables []sqlparser.TableName
tables map[sqlparser.TableName]*table
tableNames []sqlparser.TableName
// uniqueColumns has the column name as key
// and points at the columns that tables contains.
@ -147,10 +147,9 @@ func (st *symtab) AddVindexTable(alias sqlparser.TableName, vindexTable *vindexe
// At this point, only tables and uniqueColumns are set.
// All other fields are ignored.
func (st *symtab) Merge(newsyms *symtab) error {
if st.orderedTables == nil || newsyms.orderedTables == nil {
// This code is unreachable because there are higher level
// protections that disallow intermixing of symtabs with
// no tables with symtabs that have tables.
if st.tableNames == nil || newsyms.tableNames == nil {
// If any side of symtab has anonymous tables,
// we treat the merged symtab as having anonymous tables.
return nil
}
for _, t := range newsyms.tables {
@ -170,7 +169,7 @@ func (st *symtab) AddTable(t *table) error {
return fmt.Errorf("duplicate symbol: %s", sqlparser.String(t.alias))
}
st.tables[t.alias] = t
st.orderedTables = append(st.orderedTables, t.alias)
st.tableNames = append(st.tableNames, t.alias)
// update the uniqueColumns list, and eliminate
// duplicate symbols if found.
@ -190,8 +189,11 @@ func (st *symtab) AddTable(t *table) error {
// AllTables returns an ordered list of all current tables.
func (st *symtab) AllTables() []*table {
var tables []*table
for _, tname := range st.orderedTables {
if len(st.tableNames) == 0 {
return nil
}
tables := make([]*table, 0, len(st.tableNames))
for _, tname := range st.tableNames {
tables = append(tables, st.tables[tname])
}
return tables
@ -204,7 +206,7 @@ func (st *symtab) AllTables() []*table {
// This may be a deviation from the formal definition of SQL, but there
// are currently no use cases that require the full support.
func (st *symtab) FindTable(tname sqlparser.TableName) (*table, error) {
if st.orderedTables == nil {
if st.tableNames == nil {
// Unreachable because current code path checks for this condition
// before invoking this function.
return nil, errNoTable
@ -440,7 +442,7 @@ func (st *symtab) ResolveSymbols(node sqlparser.SQLNode) error {
type table struct {
alias sqlparser.TableName
columns map[string]*column
orderdColumns []sqlparser.ColIdent
columnNames []sqlparser.ColIdent
isAuthoritative bool
origin builder
vindexTable *vindexes.Table
@ -453,10 +455,10 @@ func (t *table) addColumn(alias sqlparser.ColIdent, c *column) {
lowered := alias.Lowered()
// Dups are allowed, but first one wins if referenced.
if _, ok := t.columns[lowered]; !ok {
c.colnum = len(t.orderdColumns)
c.colnum = len(t.columnNames)
t.columns[lowered] = c
}
t.orderdColumns = append(t.orderdColumns, alias)
t.columnNames = append(t.columnNames, alias)
}
// column represents a unique symbol in the query that other

Просмотреть файл

@ -65,7 +65,7 @@ message Table {
// The keyspace id is represened in hex form
// like in keyranges.
string pinned = 5;
// is_authoritative is set ot true if columns is
// is_authoritative is set to true if columns is
// an authoritative list for the table. This allows
// us to expand 'select *' expressions.
bool is_authoritative = 6;