Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
fabuzaid21 committed Jan 12, 2018
1 parent 53eb13e commit 47208b0
Show file tree
Hide file tree
Showing 53 changed files with 111 additions and 4,122 deletions.
151 changes: 22 additions & 129 deletions sql/src/main/antlr4/edu/stanford/futuredata/macrobase/SqlBase.g4
Original file line number Diff line number Diff line change
Expand Up @@ -33,103 +33,9 @@ statement
| IMPORT FROM CSV FILE STRING INTO qualifiedName
('(' columnDefinition (',' columnDefinition)* ')')? #importCsv
;
// | USE schema=identifier #use
// | USE catalog=identifier '.' schema=identifier #use
// | CREATE SCHEMA (IF NOT EXISTS)? qualifiedName
// (WITH properties)? #createSchema
// | DROP SCHEMA (IF EXISTS)? qualifiedName (CASCADE | RESTRICT)? #dropSchema
// | ALTER SCHEMA qualifiedName RENAME TO identifier #renameSchema
// | CREATE TABLE (IF NOT EXISTS)? qualifiedName ('(' columnAliases ')')?
// (COMMENT string)?
// (WITH properties)? AS (query | '('query')')
// (WITH (NO)? DATA)? #createTableAsSelect
// | CREATE TABLE (IF NOT EXISTS)? qualifiedName
// '(' tableElement (',' tableElement)* ')'
// (COMMENT string)?
// (WITH properties)? #createTable
// | DROP TABLE (IF EXISTS)? qualifiedName #dropTable
// | INSERT INTO qualifiedName ('(' columnAliases ')')? query #insertInto
// | DELETE FROM qualifiedName (WHERE booleanExpression)? #delete
// | ALTER TABLE from=qualifiedName RENAME TO to=qualifiedName #renameTable
// | ALTER TABLE tableName=qualifiedName
// RENAME COLUMN from=identifier TO to=identifier #renameColumn
// | ALTER TABLE tableName=qualifiedName
// DROP COLUMN column=qualifiedName #dropColumn
// | ALTER TABLE tableName=qualifiedName
// ADD COLUMN column=columnDefinition #addColumn
// | CREATE (OR REPLACE)? VIEW qualifiedName AS query #createView
// | DROP VIEW (IF EXISTS)? qualifiedName #dropView
// | CALL qualifiedName '(' (callArgument (',' callArgument)*)? ')' #call
// | GRANT
// (privilege (',' privilege)* | ALL PRIVILEGES)
// ON TABLE? qualifiedName TO grantee=identifier
// (WITH GRANT OPTION)? #grant
// | REVOKE
// (GRANT OPTION FOR)?
// (privilege (',' privilege)* | ALL PRIVILEGES)
// ON TABLE? qualifiedName FROM grantee=identifier #revoke
// | SHOW GRANTS
// (ON TABLE? qualifiedName)? #showGrants
// | EXPLAIN ANALYZE? VERBOSE?
// ('(' explainOption (',' explainOption)* ')')? statement #explain
// | SHOW CREATE TABLE qualifiedName #showCreateTable
// | SHOW CREATE VIEW qualifiedName #showCreateView
// | SHOW TABLES ((FROM | IN) qualifiedName)? (LIKE pattern=string)? #showTables
// | SHOW SCHEMAS ((FROM | IN) identifier)? (LIKE pattern=string)? #showSchemas
// | SHOW CATALOGS (LIKE pattern=string)? #showCatalogs
// | SHOW COLUMNS (FROM | IN) qualifiedName #showColumns
// | SHOW STATS (FOR | ON) qualifiedName #showStats
// | SHOW STATS FOR '(' querySpecification ')' #showStatsForQuery
// | DESCRIBE qualifiedName #showColumns
// | DESC qualifiedName #showColumns
// | SHOW FUNCTIONS #showFunctions
// | SHOW SESSION #showSession
// | SET SESSION qualifiedName EQ expression #setSession
// | RESET SESSION qualifiedName #resetSession
// | START TRANSACTION (transactionMode (',' transactionMode)*)? #startTransaction
// | COMMIT WORK? #commit
// | ROLLBACK WORK? #rollback
// | SHOW PARTITIONS (FROM | IN) qualifiedName
// (WHERE booleanExpression)?
// (ORDER BY sortItem (',' sortItem)*)?
// (LIMIT limit=(INTEGER_VALUE | ALL))? #showPartitions
// | PREPARE identifier FROM statement #prepare
// | DEALLOCATE PREPARE identifier #deallocate
// | EXECUTE identifier (USING expression (',' expression)*)? #execute
// | DESCRIBE INPUT identifier #describeInput
// | DESCRIBE OUTPUT identifier #describeOutput

query
: with? queryNoWith
;

with
: WITH RECURSIVE? namedQuery (',' namedQuery)*
;

//tableElement
// : columnDefinition
// | likeClause
// ;

columnDefinition
: identifier type (COMMENT string)?
;

//likeClause
// : LIKE qualifiedName (optionType=(INCLUDING | EXCLUDING) PROPERTIES)?
// ;

//properties
// : '(' property (',' property)* ')'
// ;

//property
// : identifier EQ expression
// ;

queryNoWith:
queryTerm
: queryTerm
;

queryTerm
Expand All @@ -143,11 +49,7 @@ queryPrimary
| diffQuerySpecification #diffQuery
| TABLE qualifiedName #table
| VALUES expression (',' expression)* #inlineTable
| '(' queryNoWith ')' #subquery
;

sortItem
: expression ordering=(ASC | DESC)? (NULLS nullOrdering=(FIRST | LAST))?
| '(' query ')' #subquery
;

querySpecification
Expand All @@ -158,9 +60,9 @@ querySpecification
(HAVING having=booleanExpression)?
(ORDER BY sortItem (',' sortItem)*)?
(LIMIT limit=(INTEGER_VALUE | ALL))?
exportExpression?
exportClause?
| SELECT setQuantifier? selectItem (',' selectItem)*
exportExpression?
exportClause?
(FROM relation (',' relation)*)?
(WHERE where=booleanExpression)?
(GROUP BY groupBy)?
Expand All @@ -171,18 +73,18 @@ querySpecification

diffQuerySpecification
: SELECT setQuantifier? selectItem (',' selectItem)*
FROM DIFF queryNoWith qualifiedName? (',' queryNoWith qualifiedName?)?
FROM DIFF queryTerm qualifiedName? (',' queryTerm qualifiedName?)?
ON columnAliases
(WITH minRatioExpression? minSupportExpression?)?
(COMPARE BY ratioMetricExpression)?
(MAX COMBO maxCombo=INTEGER_VALUE)?
(WHERE where=booleanExpression)?
(ORDER BY sortItem (',' sortItem)*)?
(LIMIT limit=(INTEGER_VALUE | ALL))?
exportExpression?
exportClause?
| SELECT setQuantifier? selectItem (',' selectItem)*
exportExpression?
FROM DIFF queryNoWith qualifiedName? (',' queryNoWith qualifiedName?)?
exportClause?
FROM DIFF queryTerm qualifiedName? (',' queryTerm qualifiedName?)?
ON columnAliases
(WITH minRatioExpression? minSupportExpression?)
(COMPARE BY ratioMetricExpression)?
Expand All @@ -192,6 +94,14 @@ diffQuerySpecification
(LIMIT limit=(INTEGER_VALUE | ALL))?
;

columnDefinition
: identifier type (COMMENT string)?
;

sortItem
: expression ordering=(ASC | DESC)? (NULLS nullOrdering=(FIRST | LAST))?
;

minRatioExpression
: MIN RATIO minRatio=DECIMAL_VALUE
;
Expand Down Expand Up @@ -237,10 +147,6 @@ groupingSet
| qualifiedName
;

namedQuery
: name=identifier ('(' columnAliases ')')? AS '(' query ')'
;

setQuantifier
: DISTINCT
| ALL
Expand All @@ -252,27 +158,27 @@ selectItem
| ASTERISK #selectAll
;

exportExpression
exportClause
: (
INTO OUTFILE filename=STRING
(
fieldsFormat=(FIELDS | COLUMNS)
delimiterExpression /*escapeExpression?*/
delimiterClause /*escapeClause?*/
)?
(
LINES delimiterExpression
LINES delimiterClause
)?
)
;


delimiterExpression
delimiterClause
: TERMINATED BY delimiter=STRING
// | OPTIONALLY? ENCLOSED BY enclosion=STRING
;

// TODO: support
escapeExpression
// TODO: add support for escaping
escapeClause
: ESCAPED BY escaping=STRING
;

Expand Down Expand Up @@ -319,8 +225,6 @@ columnAliases
relationPrimary
: qualifiedName #tableName
| '(' query ')' #subqueryRelation
| UNNEST '(' expression (',' expression)* ')' (WITH ORDINALITY)? #unnest
| LATERAL '(' query ')' #lateral
| '(' relation ')' #parenthesizedRelation
;

Expand Down Expand Up @@ -390,12 +294,6 @@ primaryExpression
| value=primaryExpression '[' index=valueExpression ']' #subscript
| identifier #columnReference
| base=primaryExpression '.' fieldName=identifier #dereference
| name=CURRENT_DATE #specialDateTimeFunction
| name=CURRENT_TIME ('(' precision=INTEGER_VALUE ')')? #specialDateTimeFunction
| name=CURRENT_TIMESTAMP ('(' precision=INTEGER_VALUE ')')? #specialDateTimeFunction
| name=LOCALTIME ('(' precision=INTEGER_VALUE ')')? #specialDateTimeFunction
| name=LOCALTIMESTAMP ('(' precision=INTEGER_VALUE ')')? #specialDateTimeFunction
| SUBSTRING '(' valueExpression FROM valueExpression (FOR valueExpression)? ')' #substring
| NORMALIZE '(' valueExpression (',' normalForm)? ')' #normalize
| EXTRACT '(' identifier FROM valueExpression ')' #extract
| '(' expression ')' #parenthesizedExpression
Expand Down Expand Up @@ -562,9 +460,6 @@ CSV: 'CSV';
CROSS: 'CROSS';
CUBE: 'CUBE';
CURRENT: 'CURRENT';
CURRENT_DATE: 'CURRENT_DATE';
CURRENT_TIME: 'CURRENT_TIME';
CURRENT_TIMESTAMP: 'CURRENT_TIMESTAMP';
DATA: 'DATA';
DATE: 'DATE';
DAY: 'DAY';
Expand Down Expand Up @@ -626,8 +521,6 @@ LEVEL: 'LEVEL';
LIKE: 'LIKE';
LIMIT: 'LIMIT';
LINES: 'LINES';
LOCALTIME: 'LOCALTIME';
LOCALTIMESTAMP: 'LOCALTIMESTAMP';
LOGICAL: 'LOGICAL';
MAP: 'MAP';
MAX: 'MAX';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import edu.stanford.futuredata.macrobase.sql.tree.CoalesceExpression;
import edu.stanford.futuredata.macrobase.sql.tree.ComparisonExpression;
import edu.stanford.futuredata.macrobase.sql.tree.Cube;
import edu.stanford.futuredata.macrobase.sql.tree.CurrentTime;
import edu.stanford.futuredata.macrobase.sql.tree.DecimalLiteral;
import edu.stanford.futuredata.macrobase.sql.tree.DereferenceExpression;
import edu.stanford.futuredata.macrobase.sql.tree.DoubleLiteral;
Expand Down Expand Up @@ -140,21 +139,6 @@ protected String visitAtTimeZone(AtTimeZone node, Void context) {
.append(process(node.getTimeZone(), context)).toString();
}

@Override
protected String visitCurrentTime(CurrentTime node, Void context) {
StringBuilder builder = new StringBuilder();

builder.append(node.getType().getName());

if (node.getPrecision() != null) {
builder.append('(')
.append(node.getPrecision())
.append(')');
}

return builder.toString();
}

@Override
protected String visitExtract(Extract node, Void context) {
return "EXTRACT(" + node.getField() + " FROM " + process(node.getExpression(), context) + ")";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import edu.stanford.futuredata.macrobase.sql.tree.NotExpression;
import edu.stanford.futuredata.macrobase.sql.tree.NullLiteral;
import edu.stanford.futuredata.macrobase.sql.tree.OrderBy;
import edu.stanford.futuredata.macrobase.sql.tree.Query;
import edu.stanford.futuredata.macrobase.sql.tree.QueryBody;
import edu.stanford.futuredata.macrobase.sql.tree.QuerySpecification;
import edu.stanford.futuredata.macrobase.sql.tree.Select;
Expand Down Expand Up @@ -80,17 +79,17 @@ DataFrame executeQuery(QueryBody query) throws MacrobaseException {
log.debug(diffQuery.toString());
return executeDiffQuerySpec(diffQuery);
}
throw new MacrobaseSQLException("query of type " + query.getClass().getSimpleName() + " not yet"
+ "supported");
throw new MacrobaseSQLException(
"query of type " + query.getClass().getSimpleName() + " not yet supported");
}

private DataFrame executeDiffQuerySpec(final DiffQuerySpecification diffQuery)
throws MacrobaseException {
assert (diffQuery.getSecond().isPresent()); // TODO: support single DataFrame queries
// Extract parameters for Diff query
// TODO: too many get's; too many fields are Optional that shouldn't be
final Query first = diffQuery.getFirst().get();
final Query second = diffQuery.getSecond().get();
final TableSubquery first = diffQuery.getFirst().get();
final TableSubquery second = diffQuery.getSecond().get();
final List<String> explainCols = diffQuery.getAttributeCols().stream().map(Identifier::toString)
.collect(toImmutableList());
final double minRatioMetric = diffQuery.getMinRatioExpression().get().getMinRatio();
Expand All @@ -100,10 +99,8 @@ private DataFrame executeDiffQuerySpec(final DiffQuerySpecification diffQuery)
final long order = diffQuery.getMaxCombo().get().getValue();

// execute subqueries
final DataFrame firstDf = executeQuery(
((TableSubquery) first.getQueryBody()).getQuery().getQueryBody());
final DataFrame secondDf = executeQuery(
((TableSubquery) second.getQueryBody()).getQuery().getQueryBody());
final DataFrame firstDf = executeQuery(first.getQuery().getQueryBody());
final DataFrame secondDf = executeQuery(second.getQuery().getQueryBody());
if (!firstDf.getSchema().hasColumns(explainCols) || !secondDf.getSchema()
.hasColumns(explainCols)) {
throw new MacrobaseSQLException(
Expand Down
Loading

0 comments on commit 47208b0

Please sign in to comment.