Skip to content
Open
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
6 changes: 6 additions & 0 deletions sql/plan/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ var _ sql.CollationCoercible = (*Project)(nil)

// NewProject creates a new projection.
func NewProject(expressions []sql.Expression, child sql.Node) *Project {
if len(expressions) > 0 && expressions[0] == nil {
print()
}
return &Project{
UnaryNode: UnaryNode{child},
Projections: expressions,
Expand Down Expand Up @@ -210,6 +213,9 @@ func (p *Project) WithExpressions(exprs ...sql.Expression) (sql.Node, error) {
if len(exprs) != len(p.Projections) {
return nil, sql.ErrInvalidChildrenNumber.New(p, len(exprs), len(p.Projections))
}
if len(exprs) > 0 && exprs[0] == nil {
print()
}
np := *p
np.Projections = exprs
np.sch = nil
Expand Down
55 changes: 30 additions & 25 deletions sql/planbuilder/aggregates.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,25 @@ var _ ast.Expr = (*aggregateInfo)(nil)

type groupBy struct {
outScope *scope
aggs map[string]scopeColumn
aggs map[string]*scopeColumn
grouping map[string]bool
inCols []scopeColumn
inCols []*scopeColumn
}

func (g *groupBy) addInCol(c scopeColumn) {
func (g *groupBy) addInCol(c *scopeColumn) {
g.inCols = append(g.inCols, c)
}

func (g *groupBy) addOutCol(c scopeColumn) columnId {
func (g *groupBy) addOutCol(c *scopeColumn) columnId {
return g.outScope.newColumn(c)
}

func (g *groupBy) hasAggs() bool {
return len(g.aggs) > 0
}

func (g *groupBy) aggregations() []scopeColumn {
aggregations := make([]scopeColumn, 0, len(g.aggs))
func (g *groupBy) aggregations() []*scopeColumn {
aggregations := make([]*scopeColumn, 0, len(g.aggs))
for _, agg := range g.aggs {
aggregations = append(aggregations, agg)
}
Expand All @@ -61,9 +61,9 @@ func (g *groupBy) aggregations() []scopeColumn {
return aggregations
}

func (g *groupBy) addAggStr(c scopeColumn) {
func (g *groupBy) addAggStr(c *scopeColumn) {
if g.aggs == nil {
g.aggs = make(map[string]scopeColumn)
g.aggs = make(map[string]*scopeColumn)
}
g.aggs[strings.ToLower(c.scalar.String())] = c
}
Expand All @@ -72,8 +72,8 @@ func (g *groupBy) getAggRef(name string) sql.Expression {
if g.aggs == nil {
return nil
}
ret, _ := g.aggs[name]
if ret.empty() {
ret, ok := g.aggs[name]
if !ok || ret.empty() {
return nil
}
return ret.scalarGf()
Expand All @@ -99,7 +99,7 @@ func (b *Builder) buildGroupingCols(fromScope, projScope *scope, groupby ast.Gro

g := fromScope.groupBy
for _, e := range groupby {
var col scopeColumn
var col *scopeColumn
switch e := e.(type) {
case *ast.ColName:
var ok bool
Expand All @@ -111,7 +111,6 @@ func (b *Builder) buildGroupingCols(fromScope, projScope *scope, groupby ast.Gro
if !ok {
col, ok = projScope.resolveColumn(dbName, tblName, colName, true, true)
}

if !ok {
b.handleErr(sql.ErrColumnNotFound.New(e.Name.String()))
}
Expand Down Expand Up @@ -139,10 +138,10 @@ func (b *Builder) buildGroupingCols(fromScope, projScope *scope, groupby ast.Gro
col = projScope.cols[intIdx-1]
default:
expr := b.buildScalar(fromScope, e)
col = scopeColumn{
col: expr.String(),
col = &scopeColumn{
typ: nil,
scalar: expr,
col: expr.String(),
nullable: expr.IsNullable(),
}
}
Expand Down Expand Up @@ -333,7 +332,12 @@ func (b *Builder) buildAggregateFunc(inScope *scope, name string, e *ast.FuncExp
return gf
}

col := scopeColumn{col: aggName, scalar: agg, typ: aggType, nullable: agg.IsNullable()}
col := &scopeColumn{
col: aggName,
scalar: agg,
typ: aggType,
nullable: agg.IsNullable(),
}
id := gb.outScope.newColumn(col)

agg = agg.WithId(sql.ColumnId(id)).(sql.Aggregation)
Expand Down Expand Up @@ -398,18 +402,18 @@ func (b *Builder) buildAggFunctionArgs(inScope *scope, e *ast.FuncExpr, gb *grou
b.handleErr(fmt.Errorf("failed to resolve aggregate column argument: %s", e))
}
args = append(args, e)
col := scopeColumn{tableId: e.TableID(), db: e.Database(), table: e.Table(), col: e.Name(), scalar: e, typ: e.Type(), nullable: e.IsNullable()}
col := &scopeColumn{tableId: e.TableID(), db: e.Database(), table: e.Table(), col: e.Name(), scalar: e, typ: e.Type(), nullable: e.IsNullable()}
gb.addInCol(col)
case *expression.Star:
err := sql.ErrStarUnsupported.New()
b.handleErr(err)
case *plan.Subquery:
args = append(args, e)
col := scopeColumn{col: e.QueryString, scalar: e, typ: e.Type()}
col := &scopeColumn{col: e.QueryString, scalar: e, typ: e.Type()}
gb.addInCol(col)
default:
args = append(args, e)
col := scopeColumn{col: e.String(), scalar: e, typ: e.Type()}
col := &scopeColumn{col: e.String(), scalar: e, typ: e.Type()}
gb.addInCol(col)
}
}
Expand All @@ -432,7 +436,7 @@ func (b *Builder) buildJsonArrayStarAggregate(gb *groupBy) sql.Expression {
return gf
}

col := scopeColumn{col: strings.ToLower(agg.String()), scalar: agg, typ: agg.Type(), nullable: agg.IsNullable()}
col := &scopeColumn{col: strings.ToLower(agg.String()), scalar: agg, typ: agg.Type(), nullable: agg.IsNullable()}
id := gb.outScope.newColumn(col)

agg = agg.WithId(sql.ColumnId(id)).(*aggregation.JsonArray)
Expand Down Expand Up @@ -460,7 +464,7 @@ func (b *Builder) buildCountStarAggregate(e *ast.FuncExpr, gb *groupBy) sql.Expr
return gf
}

col := scopeColumn{col: strings.ToLower(agg.String()), scalar: agg, typ: agg.Type(), nullable: agg.IsNullable()}
col := &scopeColumn{col: strings.ToLower(agg.String()), scalar: agg, typ: agg.Type(), nullable: agg.IsNullable()}
id := gb.outScope.newColumn(col)
col.id = id

Expand Down Expand Up @@ -515,7 +519,7 @@ func (b *Builder) buildGroupConcat(inScope *scope, e *ast.GroupConcatExpr) sql.E
// todo store ref to aggregate
agg := aggregation.NewGroupConcat(e.Distinct, sortFields, separatorS, args, int(groupConcatMaxLen))
aggName := strings.ToLower(plan.AliasSubqueryString(agg))
col := scopeColumn{col: aggName, scalar: agg, typ: agg.Type(), nullable: agg.IsNullable()}
col := &scopeColumn{col: aggName, scalar: agg, typ: agg.Type(), nullable: agg.IsNullable()}

id := gb.outScope.newColumn(col)

Expand Down Expand Up @@ -573,7 +577,7 @@ func (b *Builder) buildOrderedInjectedExpr(inScope *scope, e *ast.OrderedInjecte
}

aggName := strings.ToLower(plan.AliasSubqueryString(agg))
col := scopeColumn{col: aggName, scalar: agg, typ: agg.Type(), nullable: agg.IsNullable()}
col := &scopeColumn{col: aggName, scalar: agg, typ: agg.Type(), nullable: agg.IsNullable()}
id := gb.outScope.newColumn(col)

agg = agg.WithId(sql.ColumnId(id)).(sql.Aggregation)
Expand Down Expand Up @@ -647,7 +651,7 @@ func (b *Builder) buildWindowFunc(inScope *scope, name string, e *ast.FuncExpr,
win = w.WithWindow(def)
}

col := scopeColumn{col: strings.ToLower(win.String()), scalar: win, typ: win.Type(), nullable: win.IsNullable()}
col := &scopeColumn{col: strings.ToLower(win.String()), scalar: win, typ: win.Type(), nullable: win.IsNullable()}
id := inScope.newColumn(col)
col.id = id
win = win.WithId(sql.ColumnId(id)).(sql.WindowAdaptableExpression)
Expand Down Expand Up @@ -947,13 +951,14 @@ func (b *Builder) buildInnerProj(fromScope, projScope *scope) *scope {
}

// getMatchingCol returns the column in cols that matches the name, if it exists
func getMatchingCol(cols []scopeColumn, name string) (scopeColumn, bool) {
func getMatchingCol(cols []*scopeColumn, name string) (*scopeColumn, bool) {
for _, c := range cols {
if strings.EqualFold(c.col, name) {
return c, true
}
}
return scopeColumn{}, false
// TODO: drop the bool
return nil, false
}

func (b *Builder) buildHaving(fromScope, projScope, outScope *scope, having *ast.Where) {
Expand Down
5 changes: 4 additions & 1 deletion sql/planbuilder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,9 @@ func (b *Builder) buildSubquery(inScope *scope, stmt ast.Statement, subQuery str
default:
b.handleErr(sql.ErrUnsupportedSyntax.New(ast.String(n)))
case ast.SelectStatement:
if subQuery == "SELECT i AS x FROM mytable ORDER BY x DESC" {
print()
}
outScope = b.buildSelectStmt(inScope, n)
if into := n.GetInto(); into != nil {
b.buildInto(outScope, into)
Expand Down Expand Up @@ -422,7 +425,7 @@ func (b *Builder) buildVirtualTableScan(db string, tab sql.Table) *plan.VirtualC
tableScope := b.newScope()
schema := tab.Schema()
for _, c := range schema {
tableScope.newColumn(scopeColumn{
tableScope.newColumn(&scopeColumn{
table: strings.ToLower(tab.Name()),
db: strings.ToLower(db),
col: strings.ToLower(c.Name),
Expand Down
8 changes: 6 additions & 2 deletions sql/planbuilder/create_ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,11 @@ func (b *Builder) validateStatement(inScope *scope, stmt ast.Statement) {
varName := strings.ToLower(v.String())
param := expression.NewProcedureParam(varName, typ)
inScope.proc.AddVar(param)
inScope.newColumn(scopeColumn{col: varName, typ: typ, scalar: param})
inScope.newColumn(&scopeColumn{
col: varName,
typ: typ,
scalar: param,
})
}
} else if s.Cursor != nil {
inScope.proc.AddCursor(s.Cursor.Name)
Expand Down Expand Up @@ -695,7 +699,7 @@ func (b *Builder) buildCreateView(inScope *scope, subQuery string, fullQuery str
var cols sql.ColSet

for i, col := range queryScope.cols {
id := outScope.newColumn(scopeColumn{
id := outScope.newColumn(&scopeColumn{
db: dbName,
table: aliasName,
col: strings.ToLower(queryAlias.ColumnNames[i]),
Expand Down
6 changes: 3 additions & 3 deletions sql/planbuilder/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -1417,7 +1417,7 @@ func (b *Builder) tableSpecToSchema(inScope, outScope *scope, db sql.Database, t
}

schema = append(schema, column)
outScope.newColumn(scopeColumn{
outScope.newColumn(&scopeColumn{
tableId: tabId,
table: tableName,
db: db.Name(),
Expand Down Expand Up @@ -1618,7 +1618,7 @@ func (b *Builder) modifySchemaTarget(inScope *scope, n sql.SchemaTarget, sch sql
func (b *Builder) ResolveSchemaDefaults(db string, tableName string, schema sql.Schema) sql.Schema {
tableScope := b.newScope()
for _, c := range schema {
tableScope.newColumn(scopeColumn{
tableScope.newColumn(&scopeColumn{
table: strings.ToLower(tableName),
db: strings.ToLower(db),
col: strings.ToLower(c.Name),
Expand Down Expand Up @@ -1655,7 +1655,7 @@ func (b *Builder) resolveSchemaDefaults(inScope *scope, schema sql.Schema) sql.S
// backfill rest of columns
resolveScope := inScope.replace()
for _, col := range schema {
resolveScope.newColumn(scopeColumn{
resolveScope.newColumn(&scopeColumn{
db: col.DatabaseSource,
table: strings.ToLower(col.Source),
col: strings.ToLower(col.Name),
Expand Down
2 changes: 1 addition & 1 deletion sql/planbuilder/dml.go
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ func (b *Builder) buildUpdate(inScope *scope, u *ast.Update) (outScope *scope) {
for _, rt := range tablesToUpdate {
tableScope := inScope.push()
for _, c := range rt.Schema() {
tableScope.addColumn(scopeColumn{
tableScope.addColumn(&scopeColumn{
db: rt.SqlDatabase.Name(),
table: strings.ToLower(rt.Name()),
tableId: tableScope.tables[strings.ToLower(rt.Name())],
Expand Down
26 changes: 18 additions & 8 deletions sql/planbuilder/from.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ func (b *Builder) buildFrom(inScope *scope, te ast.TableExprs) (outScope *scope)
outScope.ast = te
outScope.node = plan.NewResolvedDualTable()
// new unreferenceable column to mirror empty table schema
outScope.addColumn(scopeColumn{table: "dual"})
outScope.addColumn(&scopeColumn{
table: "dual",
})
return
}

Expand Down Expand Up @@ -228,7 +230,9 @@ func (b *Builder) buildUsingJoin(inScope, leftScope, rightScope *scope, te *ast.
filter = expression.NewAnd(filter, f)
}
usingCols[colName] = struct{}{}
outScope.redirect(scopeColumn{col: rCol.col}, lCol)
outScope.redirect(&scopeColumn{
col: rCol.col,
}, lCol)
}

// Add common columns first, then left, then right.
Expand Down Expand Up @@ -346,7 +350,7 @@ func (b *Builder) buildDataSource(inScope *scope, te ast.TableExpr) (outScope *s
if len(renameCols) > 0 {
col = renameCols[i]
}
toId := outScope.newColumn(scopeColumn{
toId := outScope.newColumn(&scopeColumn{
tableId: tabId,
db: c.db,
table: alias,
Expand Down Expand Up @@ -385,7 +389,13 @@ func (b *Builder) buildDataSource(inScope *scope, te ast.TableExpr) (outScope *s
tabId := outScope.addTable(tableName)
var cols sql.ColSet
for _, c := range vdt.Schema() {
id := outScope.newColumn(scopeColumn{col: c.Name, db: c.DatabaseSource, table: tableName, typ: c.Type, nullable: c.Nullable})
id := outScope.newColumn(&scopeColumn{
col: c.Name,
db: c.DatabaseSource,
table: tableName,
typ: c.Type,
nullable: c.Nullable,
})
cols.Add(sql.ColumnId(id))
}
var renameCols []string
Expand Down Expand Up @@ -532,7 +542,7 @@ func (b *Builder) buildTableFunc(inScope *scope, t *ast.TableFuncExpr) (outScope
tabId := outScope.addTable(name)
var colset sql.ColSet
for _, c := range newAlias.Schema() {
id := outScope.newColumn(scopeColumn{
id := outScope.newColumn(&scopeColumn{
db: database.Name(),
table: name,
col: c.Name,
Expand Down Expand Up @@ -613,7 +623,7 @@ func (b *Builder) buildJSONTable(inScope *scope, t *ast.JSONTableExpr) (outScope
recFlatten(col)
}
if col.Opts != nil {
id := outScope.newColumn(scopeColumn{
id := outScope.newColumn(&scopeColumn{
table: alias,
col: col.Opts.Name,
typ: col.Opts.Type,
Expand Down Expand Up @@ -743,7 +753,7 @@ func (b *Builder) buildResolvedTable(inScope *scope, db, schema, name string, as
var cols sql.ColSet

for _, c := range tab.Schema() {
id := outScope.newColumn(scopeColumn{
id := outScope.newColumn(&scopeColumn{
db: db,
table: strings.ToLower(tab.Name()),
col: strings.ToLower(c.Name),
Expand All @@ -769,7 +779,7 @@ func (b *Builder) buildResolvedTable(inScope *scope, db, schema, name string, as
tmpScope := inScope.push()
for i, c := range sch {
// bucket schema fragments into colsets for resolving defaults
newCol := scopeColumn{
newCol := &scopeColumn{
db: c.DatabaseSource,
table: c.Source,
col: strings.ToLower(c.Name),
Expand Down
Loading
Loading