Git Product home page Git Product logo

sql's Introduction

sql

Build Status

Go Report Card

The interpreter can be run interactively.

$ go run cmd/sql/main.go
>> create table programming_languages (name text, first_appeared integer);
OK
>> insert into programming_languages values ('C', 1972), ('Python', 1990), ('Lisp', 1958), ('Go', 2009)
OK
>> select name, first_appeared from programming_languages where first_appeared < 2000 order by first_appeared
name     first_appeared
'Lisp'   1958
'C'      1972
'Python' 1990
>> select 1, 3.14 as pi, '✅' as emoji, 'Vegard' as name
1 pi       emoji name
1 3.140000 '✅'   'Vegard'
>> create table squares (number int, square int)
OK
>> insert into squares values (1, 1), (2, 2^2), (3, 3^2)
OK
>> create table cubes (number int, cube int)
OK
>> insert into cubes values (1, 1), (2, 2^3), (3, 3^3)
OK
>> select s.number, square, cube from squares s join cubes c on s.number = c.number
number square cube
1      1      1
2      4      8
3      9      27

The interpreter also supports running against standard input.

$ cat example.sql | go run cmd/sql/main.go
name     first_appeared
'C'      1972
'Python' 1990
'Lisp'   1958
'Go'     2009

Based on Thorsten Ball's excellent Writing an Interpreter in Go.

sql's People

Contributors

vegarsti avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

laplacekorea

sql's Issues

Add query planner

Currently the flow of a query is

Query -> Lexer turns string into tokens -> Parser parses tokens into abstract syntax tree structs, or basically commands -> Evaluator evaluates command

Typically, database systems have at least one component between the parser and the evaluator: a query planner. It generates a query plan, which tells the evaluator/runtime how to actually run the command in the AST on the specified set of data in the query.

Distingush between tuple and row

Distinguish between a row (a tuple, really) as stored on the backend and a row as it is being built up for returning. Currently the object.Row struct has a SortByValues slice which is used to determine where this row should end after sorting. But the stored tuple shouldn't need to know about this.

sql/object/object.go

Lines 34 to 39 in 9543f2b

type Row struct {
Aliases []string
Values []Object
SortByValues []SortBy
TableName []string
}

Possible solution: Define object.Tuple struct? Store row on backend as a []object.Object instead? (or could alias Tuple to []object.Object.

This came up (again) when I started working on persistent backend (issue #9)

Add OUTER JOIN

Could call them left join and right join, in Postgres the outer keyword is optional

Bug on self-join

>> create table nums (n int)
OK
>> insert into nums values (1)
OK
>> insert into nums values (2)
OK
>> select nums.n, n.n from nums, nums n
n n
1 1
1 1
2 2
2 2

but if we create another table that's equal, we get

>> create table mums (m int)
OK
>> insert into mums values (1)
OK
>> insert into mums values (2)
OK
>> select n, m from nums, mums
n m
1 1
1 2
2 1
2 2

which is also what Postgres returns on a self-join

# create table nums (n int);
CREATE TABLE
# insert into nums values (1), (2);
INSERT 0 2
# select * from nums, nums n;
 n | n
---+---
 1 | 1
 1 | 2
 2 | 1
 2 | 2

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.