Commit 274ef066 authored by cznic's avatar cznic

Adjust lexer and parser for the new index statements. Updates #14.

parent b39b6ba8
// Copyright 2013 The Go Authors. All rights reserved.
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
......
// Copyright 2013 The Go Authors. All rights reserved.
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
......
......@@ -138,12 +138,12 @@ The table name is stored in field #3 (name).
Indices
Consider an index named N, indexing column named C. C has index X in scols.
The encoding of this particular index is a string "<tag>N". <tag> is a string
"n" for non unique indices and "u" for unique indices. There is this index
information for all columns of scols. Where the column is not indexed, the
index info is an empty string. Infos for all indexes are joined with "|". For
example
Consider an index named N, indexing column named C. The encoding of this
particular index is a string "<tag>N". <tag> is a string "n" for non unique
indices and "u" for unique indices. There is this index information for the
index possibly indexing the record id() and for all other columns of scols.
Where the column is not indexed, the index info is an empty string. Infos for
all indexes are joined with "|". For example
BEGIN TRANSACTION;
CREATE TABLE t (Foo int, Bar bool, Baz string);
......@@ -154,18 +154,31 @@ example
The values of fields #1 and #4 for the above is
scols: "lFoo|bBar|sBaz"
indices: "uY||nX"
indices: "|uY||nX"
Aligning properly the "|" split parts
id col #0 col#1 col#2
+----------+----+--------+--------+--------+
| scols: | | "lFoo" | "bBar" | "sBaz" |
+----------+----+--------+--------+--------+
| indices: | "" | "uY" | "" | "nX" |
+----------+----+--------+--------+--------+
shows that the record id() is not indexed for this table while the columns Foo
and Baz are.
Note that there cannot be two differently named indexes for the same column and
it's intended. The indices are B+Trees[2]. The list of handles to their roots
is pointed to by xroots with zeros for non indexed columns. For the previous
example
tableMeta.xroots -> {y, 0, x}
tableMeta.xroots -> {0, y, 0, x}
where x is the root of the B+Tree for the X index and y is the root of the
B+Tree for the Y index. Similarly to hhead, xroots is never zero, even when
there are no indices for a table.
B+Tree for the Y index. If there would be an index for id(), its B+Tree root
will be present where the first zero is. Similarly to hhead, xroots is never
zero, even when there are no indices for a table.
Table record
......
// Copyright 2013 The Go Authors. All rights reserved.
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
......@@ -134,11 +134,11 @@
//
// The following keywords are reserved and may not be used as identifiers.
//
// ADD BETWEEN BY CREATE duration FROM int16 NOT TABLE uint16 VALUES
// ALTER bigint byte DELETE false GROUP int32 NULL time uint32 WHERE
// AND bigrat COLUMN DESC float IN int64 ORDER true uint64
// AS blob complex128 DISTINCT float32 INSERT int8 SELECT TRUNCATE uint8
// ASC bool complex64 DROP float64 int INTO string uint UPDATE
// ADD BETWEEN BY CREATE duration FROM int INTO SELECT TRUNCATE uint8
// ALTER bigint byte DELETE false GROUP int16 NOT string uint UNIQUE
// AND bigrat COLUMN DESC float IN int32 NULL TABLE uint16 UPDATE
// AS blob complex128 DISTINCT float32 INDEX int64 ON time uint32 VALUES
// ASC bool complex64 DROP float64 INSERT int8 ORDER true uint64 WHERE
//
// Keywords are not case sensitive.
//
......@@ -1095,8 +1095,9 @@
// Statements control execution.
//
// Statement = EmptyStmt | AlterTableStmt | BeginTransactionStmt | CommitStmt
// | CreateTableStmt | DeleteFromStmt | DropTableStmt | InsertIntoStmt
// | RollbackStmt | SelectStmt | TruncateTableStmt | UpdateStmt .
// | CreateIndexStmt | CreateTableStmt | DeleteFromStmt | DropIndexStmt
// | DropTableStmt | InsertIntoStmt | RollbackStmt | SelectStmt
// | TruncateTableStmt | UpdateStmt .
//
// StatementList = Statement { ";" Statement } .
//
......@@ -1157,6 +1158,32 @@
// INSERT INTO AccountB (Amount) VALUES (-$1);
// COMMIT;
//
// CREATE INDEX
//
// Create index statements create new indices. Index is a named projection of
// ordered values of a table column to the respective records. As a special
// case the id() of the record can be indexed.
//
// CreateIndexStmt = "CREATE" "INDEX" IndexName
// "ON" TableName "(" ( ColumnName | "id" Call ) ")" .
//
// For example
//
// BEGIN TRANSACTION;
// CREATE TABLE Orders (CustomerID int, Date time);
// CREATE INDEX OrdersID ON Orders (id());
// CREATE INDEX OrdersDate ON Orders (Date);
// CREATE TABLE Items (OrderID int, ProductID int, Qty int);
// CREATE INDEX ItemsOrderID ON Items (OrderID);
// COMMIT;
//
// Now certain SELECT statements may use the indices to speed up joins and/or
// to speed up record set filtering when the WHERE clause is used; or the
// indices might be used to improve the performance when the ORDER BY clause is
// present.
//
//TODO Describe when an index will be used with examples. Show also how to circumvent the limitations.
//
// CREATE TABLE
//
// Create table statements create new tables. A column definition declares the
......@@ -1198,9 +1225,22 @@
// WHERE DepartmentName == "Ponies";
// COMMIT;
//
// If the WhereClause is not present then all rows are removed and the
// If the WHERE clause is not present then all rows are removed and the
// statement is equivalent to the TRUNCATE TABLE statement.
//
// DROP INDEX
//
// Drop index statements remove indices from the DB. The index must exist.
//
// DropIndexStmt = "DROP" "INDEX" IndexName .
// IndexName = identifier .
//
// For example
//
// BEGIN TRANSACTION;
// DROP INDEX ItemsOrderID;
// COMMIT;
//
// DROP TABLE
//
// Drop table statements remove tables from the DB. The table must exist.
......
// Copyright 2013 The Go Authors. All rights reserved.
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found pIn the LICENSE file.
......@@ -19,7 +19,7 @@ var (
_ expression = (*conversion)(nil)
_ expression = (*ident)(nil)
_ expression = (*pIn)(nil)
_ expression = (*index)(nil)
_ expression = (*indexOp)(nil)
_ expression = (*isNull)(nil)
_ expression = (*parameter)(nil)
_ expression = (*pexpr)(nil)
......@@ -3151,13 +3151,13 @@ func (is *isNull) eval(ctx map[interface{}]interface{}, arg []interface{}) (v in
return val == nil != is.not, nil
}
type index struct {
type indexOp struct {
expr, x expression
}
func newIndex(sv, xv expression) (v expression, err error) {
s, fs, i := "", false, uint64(0)
x := index{sv, xv}
x := indexOp{sv, xv}
if x.expr.isStatic() {
v, err := x.expr.eval(nil, nil)
if err != nil {
......@@ -3199,13 +3199,13 @@ func newIndex(sv, xv expression) (v expression, err error) {
return &x, nil
}
func (x *index) isStatic() bool {
func (x *indexOp) isStatic() bool {
return x.expr.isStatic() && x.x.isStatic()
}
func (x *index) String() string { return fmt.Sprintf("%s[%s]", x.expr, x.x) }
func (x *indexOp) String() string { return fmt.Sprintf("%s[%s]", x.expr, x.x) }
func (x *index) eval(ctx map[interface{}]interface{}, arg []interface{}) (v interface{}, err error) {
func (x *indexOp) eval(ctx map[interface{}]interface{}, arg []interface{}) (v interface{}, err error) {
s0, err := x.expr.eval(ctx, arg)
if err != nil {
return nil, runErr(err)
......
// Copyright (c) 2013 Go Authors. All rights reserved.
// Copyright (c) 2014 Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
......
// Copyright (c) 2013 Go Authors. All rights reserved.
// Copyright (c) 2014 Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
......
This diff is collapsed.
%{
// Copyright (c) 2013 Go Authors. All rights reserved.
// Copyright (c) 2014 Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
......@@ -36,16 +36,16 @@ import (
eq
falseKwd floatType float32Type float64Type floatLit from
ge group
identifier imaginaryLit in insert intType int16Type int32Type int64Type int8Type is
into intLit
identifier imaginaryLit in index insert intType int16Type int32Type
int64Type int8Type is into intLit
le lsh
neq not null
order oror
neq not null
on order oror
qlParam
rollback rsh runeType
selectKwd stringType stringLit
tableKwd timeType transaction trueKwd truncate
uintType uint16Type uint32Type uint64Type uint8Type update
uintType uint16Type uint32Type uint64Type uint8Type unique update
values
where
......@@ -66,8 +66,8 @@ import (
%type <item> AlterTableStmt Assignment AssignmentList AssignmentList1
BeginTransactionStmt
Call Call1 ColumnDef ColumnName ColumnNameList ColumnNameList1
CommitStmt Conversion CreateTableStmt CreateTableStmt1
DeleteFromStmt DropTableStmt
CommitStmt Conversion CreateIndexStmt CreateTableStmt CreateTableStmt1
DeleteFromStmt DropIndexStmt DropTableStmt
EmptyStmt Expression ExpressionList ExpressionList1
Factor Factor1 Field Field1 FieldList
GroupByClause
......@@ -186,6 +186,19 @@ Conversion:
$$ = &conversion{typ: $1.(int), val: $3.(expression)}
}
CreateIndexStmt:
create index identifier on identifier '(' identifier ')'
{
$$ = &createIndexStmt{$3.(string), $5.(string), $7.(string)}
}
| create index identifier on identifier '(' identifier '(' ')' ')'
{
$$ = &createIndexStmt{$3.(string), $5.(string), $7.(string)}
if $7.(string) != "id" {
yylex.(*lexer).err("only the built-in id() can be indexed on")
}
}
CreateTableStmt:
create tableKwd TableName '(' ColumnDef CreateTableStmt1 CreateTableStmt2 ')'
{
......@@ -216,6 +229,12 @@ DeleteFromStmt:
$$ = &deleteStmt{tableName: $3.(string), where: $4.(*whereRset).expr}
}
DropIndexStmt:
drop index identifier
{
$$ = &dropIndexStmt{indexName: $3.(string)}
}
DropTableStmt:
drop tableKwd TableName
{
......@@ -767,8 +786,10 @@ Statement:
| AlterTableStmt
| BeginTransactionStmt
| CommitStmt
| CreateIndexStmt
| CreateTableStmt
| DeleteFromStmt
| DropIndexStmt
| DropTableStmt
| InsertIntoStmt
| RollbackStmt
......
......@@ -69,8 +69,13 @@ ColumnName = identifier .
ColumnNameList = ColumnName { "," ColumnName } [ "," ] .
CommitStmt = "COMMIT" .
Conversion = Type "(" Expression ")" .
CreateIndexStmt = "CREATE" "INDEX" IndexName "ON" TableName "(" (
ColumnName
| "id" Call
) ")" .
CreateTableStmt = "CREATE" "TABLE" TableName "(" ColumnDef { "," ColumnDef } [ "," ] ")" .
DeleteFromStmt = "DELETE" "FROM" TableName [ WhereClause ] .
DropIndexStmt = "DROP" "INDEX" IndexName .
DropTableStmt = "DROP" "TABLE" TableName .
EmptyStmt = .
Expression = Term { oror Term } .
......@@ -89,6 +94,7 @@ Field = Expression [ "AS" identifier ] .
FieldList = Field { "," Field } [ "," ] .
GroupByClause = "GROUP BY" ColumnNameList .
Index = "[" Expression "]" .
IndexName = identifier .
InsertIntoStmt = "INSERT" "INTO" TableName [
"(" ColumnNameList ")"
] ( Values | SelectStmt ) .
......@@ -148,8 +154,10 @@ Statement = EmptyStmt
| AlterTableStmt
| BeginTransactionStmt
| CommitStmt
| CreateIndexStmt
| CreateTableStmt
| DeleteFromStmt
| DropIndexStmt
| DropTableStmt
| InsertIntoStmt
| RollbackStmt
......
This diff is collapsed.
This diff is collapsed.
/*
Copyright (c) 2013 Go Authors. All rights reserved.
Copyright (c) 2014 Go Authors. All rights reserved.
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file.
......@@ -150,16 +150,19 @@ drop {d}{r}{o}{p}
from {f}{r}{o}{m}
group {g}{r}{o}{u}{p}
in {i}{n}
index {i}{n}{d}{e}{x}
insert {i}{n}{s}{e}{r}{t}
into {i}{n}{t}{o}
is {i}{s}
not {n}{o}{t}
on {o}{n}
order {o}{r}{d}{e}{r}
rollback {r}{o}{l}{l}{b}{a}{c}{k}
select {s}{e}{l}{e}{c}{t}
table {t}{a}{b}{l}{e}
transaction {t}{r}{a}{n}{s}{a}{c}{t}{i}{o}{n}
truncate {t}{r}{u}{n}{c}{a}{t}{e}
unique {u}{n}{i}{q}{u}{e}
update {u}{p}{d}{a}{t}{e}
values {v}{a}{l}{u}{e}{s}
where {w}{h}{e}{r}{e}
......@@ -247,11 +250,13 @@ ident {idchar0}{idchars}*
{drop} return drop
{from} return from
{group} return group
{index} return index
{insert} return insert
{into} return into
{in} return in
{is} return is
{not} return not
{on} return on
{order} return order
{rollback} return rollback
......@@ -262,6 +267,7 @@ ident {idchar0}{idchars}*
{transaction} return transaction
{truncate} return truncate
{update} return update
{unique} return unique
{values} return values
{where} return where
......
// Copyright (c) 2013 Go Authors. All rights reserved.
// Copyright (c) 2014 Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
......@@ -17,8 +17,10 @@ import (
var (
_ stmt = (*alterTableAddStmt)(nil)
_ stmt = (*alterTableDropColumnStmt)(nil)
_ stmt = (*createIndexStmt)(nil)
_ stmt = (*createTableStmt)(nil)
_ stmt = (*deleteStmt)(nil)
_ stmt = (*dropIndexStmt)(nil)
_ stmt = (*dropTableStmt)(nil)
_ stmt = (*insertIntoStmt)(nil)
_ stmt = (*selectStmt)(nil)
......@@ -250,6 +252,18 @@ func (s *truncateTableStmt) exec(ctx *execCtx) (Recordset, error) {
func (s *truncateTableStmt) isUpdating() bool { return true }
type dropIndexStmt struct {
indexName string
}
func (s *dropIndexStmt) String() string { return fmt.Sprintf("DROP INDEX %s;", s.indexName) }
func (s *dropIndexStmt) exec(ctx *execCtx) (Recordset, error) {
panic("TODO")
}
func (s *dropIndexStmt) isUpdating() bool { return true }
type dropTableStmt struct {
tableName string
}
......@@ -564,6 +578,22 @@ func (rollbackStmt) exec(*execCtx) (Recordset, error) {
}
func (rollbackStmt) isUpdating() bool { log.Panic("internal error"); panic("unreachable") }
type createIndexStmt struct {
indexName string
tableName string
colName string // alt. "id()" for index on id()
}
func (s *createIndexStmt) String() string {
return fmt.Sprintf("CREATE INDEX %s ON %s (%s);", s.indexName, s.tableName, s.colName)
}
func (s *createIndexStmt) exec(ctx *execCtx) (_ Recordset, err error) {
panic("TODO")
}
func (s *createIndexStmt) isUpdating() bool { return true }
type createTableStmt struct {
tableName string
cols []*col
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment