Skip to content

Commit

Permalink
Merge pull request #2570 from cockroachdb/pmattis/sql-unique-id
Browse files Browse the repository at this point in the history
Add unique_id() builtin.
  • Loading branch information
petermattis committed Sep 21, 2015
2 parents 88ef0ca + a4f31c2 commit e5b07f1
Show file tree
Hide file tree
Showing 19 changed files with 268 additions and 121 deletions.
2 changes: 2 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ func (s *Server) Start(selfBootstrap bool) error {
// Begin recording status summaries.
s.startWriteSummaries()

s.sqlServer.SetNodeID(s.node.Descriptor.NodeID)

log.Infof("starting %s server at %s", s.ctx.HTTPRequestScheme(), s.rpc.Addr())
s.initHTTP()
s.rpc.Serve(s)
Expand Down
6 changes: 3 additions & 3 deletions sql/analyze_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func parseAndNormalizeExpr(t *testing.T, sql string) (parser.Expr, qvalMap) {
t.Fatalf("%s: %v", sql, err)
}
expr := q[0].(*parser.Select).Exprs[0].Expr
expr, err = parser.NormalizeExpr(expr)
expr, err = (parser.EvalContext{}).NormalizeExpr(expr)
if err != nil {
t.Fatalf("%s: %v", sql, err)
}
Expand Down Expand Up @@ -87,11 +87,11 @@ func checkEquivExpr(a, b parser.Expr, qvals qvalMap) error {
for _, q := range qvals {
q.datum = v
}
da, err := parser.EvalExpr(a)
da, err := (parser.EvalContext{}).EvalExpr(a)
if err != nil {
return fmt.Errorf("%s: %v", a, err)
}
db, err := parser.EvalExpr(b)
db, err := (parser.EvalContext{}).EvalExpr(b)
if err != nil {
return fmt.Errorf("%s: %v", b, err)
}
Expand Down
17 changes: 14 additions & 3 deletions sql/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,30 @@ var errTransactionInProgress = errors.New("there is already a transaction in pro

// An Executor executes SQL statements.
type Executor struct {
db client.DB
db client.DB
nodeID uint32
}

// NewExecutor creates an Executor.
func NewExecutor(db client.DB) Executor {
return Executor{db}
return Executor{db: db}
}

// SetNodeID sets the node ID for the SQL server.
func (e *Executor) SetNodeID(nodeID proto.NodeID) {
e.nodeID = uint32(nodeID)
}

// Execute the statement(s) in the given request and return a response.
// On error, the returned integer is an HTTP error code.
func (e Executor) Execute(args driver.Request) (driver.Response, int, error) {
planMaker := planner{
user: args.GetUser(),
evalCtx: parser.EvalContext{
NodeID: e.nodeID,
},
}
// Pick up current session state.
planMaker := planner{user: args.GetUser()}
if err := gogoproto.Unmarshal(args.Session, &planMaker.session); err != nil {
return args.CreateReply(), http.StatusBadRequest, err
}
Expand Down
4 changes: 3 additions & 1 deletion sql/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func (p *planner) groupBy(n *parser.Select, s *scanNode) (*groupNode, error) {
}

group := &groupNode{
planner: p,
columns: s.columns,
render: s.render,
funcs: funcs,
Expand All @@ -93,6 +94,7 @@ func (p *planner) groupBy(n *parser.Select, s *scanNode) (*groupNode, error) {
}

type groupNode struct {
planner *planner
plan planNode
columns []string
row parser.DTuple
Expand Down Expand Up @@ -146,7 +148,7 @@ func (n *groupNode) Next() bool {
// Render the results.
n.row = make([]parser.Datum, len(n.render))
for i, r := range n.render {
n.row[i], n.err = parser.EvalExpr(r)
n.row[i], n.err = n.planner.evalCtx.EvalExpr(r)
if n.err != nil {
return false
}
Expand Down
8 changes: 4 additions & 4 deletions sql/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (p *planner) Insert(n *parser.Insert) (planNode, error) {

// Construct the default expressions. The returned slice will be nil if no
// column in the table has a default expression.
defaultExprs, err := makeDefaultExprs(cols)
defaultExprs, err := p.makeDefaultExprs(cols)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -108,7 +108,7 @@ func (p *planner) Insert(n *parser.Insert) (planNode, error) {
rowVals = append(rowVals, parser.DNull)
continue
}
d, err := parser.EvalExpr(defaultExprs[i])
d, err := p.evalCtx.EvalExpr(defaultExprs[i])
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -256,7 +256,7 @@ func (p *planner) fillDefaults(defaultExprs []parser.Expr,
return rows, nil
}

func makeDefaultExprs(cols []ColumnDescriptor) ([]parser.Expr, error) {
func (p *planner) makeDefaultExprs(cols []ColumnDescriptor) ([]parser.Expr, error) {
// Check to see if any of the columns have DEFAULT expressions. If there are
// no DEFAULT expressions, we don't bother with constructing the defaults map
// as the defaults are all NULL.
Expand All @@ -282,7 +282,7 @@ func makeDefaultExprs(cols []ColumnDescriptor) ([]parser.Expr, error) {
if err != nil {
return nil, err
}
expr, err = parser.NormalizeExpr(expr)
expr, err = p.evalCtx.NormalizeExpr(expr)
if err != nil {
return nil, err
}
Expand Down
10 changes: 5 additions & 5 deletions sql/limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ import (
)

// limit constructs a limitNode based on the LIMIT and OFFSET clauses.
func (*planner) limit(n *parser.Select, p planNode) (planNode, error) {
func (p *planner) limit(n *parser.Select, plan planNode) (planNode, error) {
if n.Limit == nil {
return p, nil
return plan, nil
}

var count, offset int64
Expand All @@ -51,11 +51,11 @@ func (*planner) limit(n *parser.Select, p planNode) (planNode, error) {
return nil, fmt.Errorf("argument of %s must not contain variables", datum.name)
}

normalized, err := parser.NormalizeExpr(datum.src)
normalized, err := p.evalCtx.NormalizeExpr(datum.src)
if err != nil {
return nil, err
}
dstDatum, err := parser.EvalExpr(normalized)
dstDatum, err := p.evalCtx.EvalExpr(normalized)
if err != nil {
return nil, err
}
Expand All @@ -74,7 +74,7 @@ func (*planner) limit(n *parser.Select, p planNode) (planNode, error) {
}
}

return &limitNode{planNode: p, count: count, offset: offset}, nil
return &limitNode{planNode: plan, count: count, offset: offset}, nil
}

type limitNode struct {
Expand Down
Loading

0 comments on commit e5b07f1

Please sign in to comment.