diff --git a/slides/cse4562sp2019/2019-02-04-RA-Basics.html b/slides/cse4562sp2019/2019-02-04-RA-Basics.html
index 4a75ba7a..0941d4f0 100644
--- a/slides/cse4562sp2019/2019-02-04-RA-Basics.html
+++ b/slides/cse4562sp2019/2019-02-04-RA-Basics.html
@@ -219,7 +219,7 @@ For each week:
Relational Algebra
-
+
$$\pi_{R.A}(\sigma_{S.C=10}(\bowtie_{R.B = S.B}(\textbf{R}, \textbf{S})))$$
Delete rows that fail the condition $c$.
TREE_ID | SPC_COMMON | BORONAME | ... |
---|---|---|---|
204026 | 'honeylocust' | 'Brooklyn' | ... |
... and 177287 more |
+ SELECT * FROM Trees [[ WHERE BORONAME = 'Brooklyn' ]]
+
+ Delete attributes not in the projection list $A$.
BORONAME | |||||||
---|---|---|---|---|---|---|---|
Queens |
Operator | Symbol | Duplicates? | |||||
---|---|---|---|---|---|---|---|
Operator | Symbol | Creates Duplicates? | |||||
Selection | $\sigma$ | No | |||||
Projection | $\pi$ | Yes | |||||
Cross-product | $\times$ | No |
BORO | SPC_COMMON |
---|---|
Brooklyn | honeylocust |
Brooklyn | American linden |
Brooklyn | London planetree |
Manhattan | honeylocust |
Manhattan | American linden |
Manhattan | pin oak |
Queens | honeylocust |
Queens | American linden |
Bronx | honeylocust |
+ /
+
| |||||||
+ /
+
| |||||||
+ /
+
|
If time permits: Implement division using other operators.
+BORO | SPC_COMMON |
---|---|
Brooklyn | honeylocust |
Brooklyn | American linden |
Brooklyn | London planetree |
Manhattan | honeylocust |
Manhattan | American linden |
Manhattan | pin oak |
Queens | honeylocust |
Queens | American linden |
Bronx | honeylocust |
/ { honeylocust } | = Brooklyn, Manhattan, Queens, Bronx |
/ { honeylocust, American linden } | = Brooklyn, Manhattan, Queens |
/ { honeylocust, American linden, pin oak } | = Manhattan |
If time permits: Implement division using other operators.
-@@ -586,6 +620,7 @@ For each week:
- // StringReaders create a reader from a string
- Reader input = new StringReader("SELECT * FROM R")
+
+
CREATE TABLE PLAYERS(
+ ID string,
+ FIRSTNAME string,
+ LASTNAME string,
+ FIRSTSEASON int,
+ LASTSEASON int,
+ WEIGHT int,
+ BIRTHDATE date
+);
- // CCJSqlParser takes a Reader or InputStream
- CCJSqlParser parser = new CCJSqlParser(input)
+SELECT FIRSTNAME, LASTNAME,
+ WEIGHT, BIRTHDATE
+FROM PLAYERS
+WHERE WEIGHT>200;
+ CREATE TABLE PLAYERS(
+ ID string,
+ FIRSTNAME string,
+ LASTNAME string,
+ FIRSTSEASON int,
+ LASTSEASON int,
+ WEIGHT int,
+ BIRTHDATE date
+);
+ ↓
+ There is a table named "PLAYERS"... +
+ABDELAL01|Alaa|Abdelnaby|1990|1994|240|1968-06-24 +ABDULKA01|Kareem|Abdul-jabbar|1969|1988|225|1947-04-16 +ABDULMA01|Mahmo|Abdul-rauf|1990|2000|162|1969-03-09 +ABDULTA01|Tariq|Abdul-wahad|1997|2002|223|1974-11-03 +ABDURSH01|Shareef|Abdur-rahim|1996|2007|225|1976-12-11 +ABERNTO01|Tom|Abernethy|1976|1980|220|1954-05-06 +ABRAMJO01|John|Abramovic|1946|1947|195|1919-02-09 +ACKERAL01|Alex|Acker|2005|2008|185|1983-01-21 +ACKERDO01|Donald|Ackerman|1953|1953|183|1930-09-04 +ACRESMA01|Mark|Acres|1987|1992|220|1962-11-15 +ACTONCH01|Charles|Acton|1967|1967|210|1942-01-11 +...+
if(stmt instanceof Select){
+ SelectBody bodyBase = ((Select)stmt).getSelectBody();
+ if(bodyBase instanceof PlainSelect){
+ PlainSelect body = (PlainSelect)bodyBase;
+ ...
+ body.getFromItem()
+ body.getWhere()
+ body.getSelectItems()
+ ...
+ }
+ }
+
- // System.in is an InputStream
- CCJSqlParser parser = new CCJSqlParser(System.in)
-
- Statement statement = parser.Statement();
- // loop until you hit the last statement
- while(statement != null){
-
- //
- // Do something with statement
- //
-
- // ... then read the next statement
- statement = parser.Statement();
- }
-
-
- while(statement != null){
- if(statement instanceof Select){
- Select select = (Select)statement;
- // Do something with `select`
-
- } else if(statement instanceof CreateTable){
- CreateTable create = (CreateTable)statement;
- // Do something with `create`
-
- } else {
- throw new SqlException("Can't handle: "+statement);
+
+
+ Iterators
+ There are a number of data transformations that appear in more than one pattern. For example:
+
+ - Loading the CSV file in as data
+ - Filtering rows out of data
+ - Transforming (mapping) data into a new structure
+ - Summarizing (aggregating) data
+ - Printing output data
+
+
+ Suggestion: Abstract these steps out
+
+
+ Idea: Functions
+
+ - loadCSV(...) returns a table
+ - filter(condition, table) returns a table
+ - map(expressions, table) returns a table
+ - aggregate(aggregates, table) returns a row
+ - print(table)
+
+
+ Problem: A "table" can get very very big.
+
+
+ Better Idea: Iterators
+
+ - hasNext()
+ - Returns true if there are more rows to return
+ - next()
+ - Returns the next row
+
+ All "functions" can be implemented as iterators that use constant space
+
+
+ Example: SelectOperator
+
+class SelectOperator implements RAOperator
+{
+ RAOperator child;
+ Expression condition;
+ public ProjectOperator(RAOperator child, Expression condition) {
+ this.child = child; this.condition = condition;
}
- statement = parser.Statement()
- }
-
-
+
+ public boolean hasNext(){ /* ...? */ }
+ public /* ...? */ next(){ /* ...? */ }
+}
+
+ java.util.regexp.Pattern.compile()
SQL Type | PrimitiveValue |
---|---|
string | StringValue |
varchar | StringValue |
char | StringValue |
int | LongValue |
decimal | DoubleValue |
date | DateValue |
- Select select = (Select)statement;
- SelectBody body = select.getSelectBody();
- if(body instanceof /* ... */){
- // ...
+
+
+ EvalLib
+ How do you evaluate the A > 5
in
SELECT B FROM R WHERE A > 5
?
+ More generally, how do you evaluate a JSqlParser Expression
?
+
+
+
+ Eval eval = new Eval(){ /* we'll get what goes here shortly */ }
+
+
+
+ // Evaluate "1 + 2.0"
+ PrimitiveValue result;
+ result =
+ eval.eval(
+ new Addition(
+ new LongPrimitive(1),
+ new DoublePrimitive(2.0)
+ )
+ );
+ System.out.println("Result: "+result); // "Result: 3.0"
+
+
+
+
+
+ // Evaluate "R.A >= 5"
+ result =
+ eval.eval(
+ new GreaterThanEquals(
+ new Column(new Table(null, "R"), "A"), // `R.A`
+ new LongPrimitive(5)
+ )
+ );
+
+ Problem: What value should EvalLib give to R.A
?
+
+
+
+ Your code needs to tell it...
+
+ Eval eval = new Eval(){
+ public PrimitiveValue eval(Column c){
+ String colName = c.getColumnName();
+ String tableName = c.getTable().getName();
+ PrimitiveValue ret = /* look up colName.tableName */
+ return ret;
+ }
}
-
-
- - PlainSelect: (SELECT * FROM ...)
- - Union: (SELECT * FROM ...) UNION ALL (SELECT ...)
-
-
+
+
- Select select = (Select)statement;
- SelectBody body = select.getSelectBody();
- if(body instanceof PlainSelect){
- PlainSelect plain = (PlainSelect)body;
- // Do something with `plain`
- }
-
-
- SELECT [distinct] [selectItems]
- FROM [fromItem], [joins, ...]
- WHERE [where]
- GROUP BY [groupByColumnReferences]
- HAVING [having]
- ORDER BY [orderByElements]
- LIMIT [limit]
-
- Everything in [brackets] has a method in PlainSelect
-