Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: make the table reference syntax carry its alias, if any #17031

Merged
merged 1 commit into from
Jul 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 55 additions & 48 deletions pkg/sql/data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,22 +387,17 @@ func (p *planner) getDataSource(
case *parser.ParenTableExpr:
return p.getDataSource(ctx, t.Expr, hints, scanVisibility)

case *parser.TableRef:
return p.getTableScanByRef(ctx, t, hints, scanVisibility)

case *parser.AliasedTableExpr:
// Alias clause: source AS alias(cols...)

var src planDataSource
var err error

if t.Hints != nil {
hints = t.Hints
}

if tref, ok := t.Expr.(*parser.TableRef); ok {
// Special case: operand is a numeric table reference.
src, err = p.getTableScanByRef(ctx, tref, hints, scanVisibility)
} else {
src, err = p.getDataSource(ctx, t.Expr, hints, scanVisibility)
}
src, err := p.getDataSource(ctx, t.Expr, hints, scanVisibility)
if err != nil {
return src, err
}
Expand All @@ -415,43 +410,7 @@ func (p *planner) getDataSource(
src = p.wrapOrdinality(src)
}

var tableAlias parser.TableName
if t.As.Alias != "" {
// If an alias was specified, use that.
tableAlias.TableName = t.As.Alias
src.info.sourceAliases = sourceAliases{{
name: tableAlias,
columnRange: fillColumnRange(0, len(src.info.sourceColumns)-1),
}}
}
colAlias := t.As.Cols

if len(colAlias) > 0 {
// Make a copy of the slice since we are about to modify the contents.
src.info.sourceColumns = append(sqlbase.ResultColumns(nil), src.info.sourceColumns...)

// The column aliases can only refer to explicit columns.
for colIdx, aliasIdx := 0, 0; aliasIdx < len(colAlias); colIdx++ {
if colIdx >= len(src.info.sourceColumns) {
var srcName string
if tableAlias.DatabaseName != "" {
srcName = tableAlias.String()
} else {
srcName = tableAlias.TableName.String()
}

return planDataSource{}, errors.Errorf(
"source %q has %d columns available but %d columns specified",
srcName, aliasIdx, len(colAlias))
}
if src.info.sourceColumns[colIdx].Hidden {
continue
}
src.info.sourceColumns[colIdx].Name = string(colAlias[aliasIdx])
aliasIdx++
}
}
return src, nil
return renameSource(src, t.As, false)

default:
return planDataSource{}, errors.Errorf("unsupported FROM type %T", src)
Expand Down Expand Up @@ -486,7 +445,7 @@ func (p *planner) getTableScanByRef(
}
desc, err := descFunc(ctx, p.txn, tableID)
if err != nil {
return planDataSource{}, err
return planDataSource{}, errors.Errorf("%s: %v", parser.ErrString(tref), err)
}

tn := parser.TableName{
Expand All @@ -502,7 +461,55 @@ func (p *planner) getTableScanByRef(
DBNameOriginallyOmitted: true,
}

return p.getPlanForDesc(ctx, desc, &tn, hints, scanVisibility, tref.Columns)
src, err := p.getPlanForDesc(ctx, desc, &tn, hints, scanVisibility, tref.Columns)
if err != nil {
return src, err
}

return renameSource(src, tref.As, true)
}

// renameSource applies an AS clause to a data source.
func renameSource(
src planDataSource, as parser.AliasClause, includeHidden bool,
) (planDataSource, error) {
var tableAlias parser.TableName
if as.Alias != "" {
// If an alias was specified, use that.
tableAlias.TableName = as.Alias
src.info.sourceAliases = sourceAliases{{
name: tableAlias,
columnRange: fillColumnRange(0, len(src.info.sourceColumns)-1),
}}
}
colAlias := as.Cols

if len(colAlias) > 0 {
// Make a copy of the slice since we are about to modify the contents.
src.info.sourceColumns = append(sqlbase.ResultColumns(nil), src.info.sourceColumns...)

// The column aliases can only refer to explicit columns.
for colIdx, aliasIdx := 0, 0; aliasIdx < len(colAlias); colIdx++ {
if colIdx >= len(src.info.sourceColumns) {
var srcName string
if tableAlias.DatabaseName != "" {
srcName = parser.ErrString(&tableAlias)
} else {
srcName = parser.ErrString(tableAlias.TableName)
}

return planDataSource{}, errors.Errorf(
"source %q has %d columns available but %d columns specified",
srcName, aliasIdx, len(colAlias))
}
if !includeHidden && src.info.sourceColumns[colIdx].Hidden {
continue
}
src.info.sourceColumns[colIdx].Name = string(colAlias[aliasIdx])
aliasIdx++
}
}
return src, nil
}

// getTableScanOrViewPlan builds a planDataSource from a single data source
Expand Down
16 changes: 8 additions & 8 deletions pkg/sql/parser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -591,16 +591,16 @@ func TestParse(t *testing.T) {
{`SELECT * FROM (VALUES (1, 2)) AS foo`},
{`SELECT * FROM (VALUES (1, 2)) AS foo (a, b)`},

{`SELECT * FROM [123] AS t`},
{`SELECT * FROM [123(1, 2, 3)] AS t`},
{`SELECT * FROM [123()] AS t`},
{`SELECT * FROM [123 AS t]`},
{`SELECT * FROM [123(1, 2, 3) AS t]`},
{`SELECT * FROM [123() AS t]`},
{`SELECT * FROM t@[123]`},
{`SELECT * FROM t@{FORCE_INDEX=[123],NO_INDEX_JOIN}`},
{`SELECT * FROM [123]@[456] AS t`},
{`SELECT * FROM [123]@{FORCE_INDEX=[456],NO_INDEX_JOIN} AS t`},
{`SELECT * FROM [123 AS t]@[456]`},
{`SELECT * FROM [123 AS t]@{FORCE_INDEX=[456],NO_INDEX_JOIN}`},

// TODO(pmattis): Is this a postgres extension?
{`TABLE a`}, // Shorthand for: SELECT * FROM a
{`TABLE a`}, // Shorthand for: SELECT * FROM a; used e.g. in CREATE VIEW v AS TABLE t
{`TABLE [123 AS a]`},

{`TRUNCATE TABLE a`},
{`TRUNCATE TABLE a, b.c`},
Expand Down Expand Up @@ -748,7 +748,7 @@ func TestParse2(t *testing.T) {
`SELECT 'a' FROM t@{FORCE_INDEX=bar,NO_INDEX_JOIN}`},

{`SELECT 'a' FROM t@{FORCE_INDEX=[123]}`, `SELECT 'a' FROM t@[123]`},
{`SELECT 'a' FROM [123]@{FORCE_INDEX=[456]} AS t`, `SELECT 'a' FROM [123]@[456] AS t`},
{`SELECT 'a' FROM [123 AS t]@{FORCE_INDEX=[456]}`, `SELECT 'a' FROM [123 AS t]@[456]`},

{`SELECT a FROM t WHERE a IS UNKNOWN`, `SELECT a FROM t WHERE a IS NULL`},
{`SELECT a FROM t WHERE a IS NOT UNKNOWN`, `SELECT a FROM t WHERE a IS NOT NULL`},
Expand Down
Loading