From 90712aff7d800cb23d88447abb40f824981e6705 Mon Sep 17 00:00:00 2001 From: Bill Sun Date: Tue, 11 Jan 2022 15:16:17 -0500 Subject: [PATCH] Added multi-query support Updated example (q.sql) --- README.md | 4 +- mo_sql_parsing/sql_parser.py | 17 ++++-- q.sql | 100 ++++++++++++++++++++++++++++++++++- q1.sql | 46 ---------------- 4 files changed, 113 insertions(+), 54 deletions(-) diff --git a/README.md b/README.md index a0687e0..c8a59ce 100644 --- a/README.md +++ b/README.md @@ -4,8 +4,8 @@ AQuery Compiler that compiles AQuery into [K9](https://shakti.com/). Frontend built on top of [mo-sql-parsing](https://github.com/klahnakoski/mo-sql-parsing). ## Roadmap -- [ ] SQL Parser -> AQuery Parser -- [ ] -> AQuery-K9 Compiler +- [x] SQL Parser -> AQuery Parser +- -> AQuery-K9 Compiler - [ ] -> Optimizing Compiler # Descriptions from mo-sql-parsing: diff --git a/mo_sql_parsing/sql_parser.py b/mo_sql_parsing/sql_parser.py index 535824d..b7e22e3 100644 --- a/mo_sql_parsing/sql_parser.py +++ b/mo_sql_parsing/sql_parser.py @@ -620,11 +620,18 @@ def parser(literal_string, ident, sqlserver=False): + Optional(assign("where", expr)) ) / to_json_call - return ( - inline_kblock - | udf - | query + sql_stmts = delimited_list( ( + query | (insert | update | delete) | (create_table | create_view | create_cache | create_index) | (drop_table | drop_view | drop_index) - ).finalize() + )("stmts"), ";") + + other_stmt = ( + inline_kblock + | udf + ) ("stmts") + + stmts = ZeroOrMore(sql_stmts|other_stmt) + + return stmts.finalize() diff --git a/q.sql b/q.sql index e456f51..4a6921e 100644 --- a/q.sql +++ b/q.sql @@ -11,4 +11,102 @@ ELSE px END ELSE 1 END ) -} \ No newline at end of file +} +FUNCTION covariance (x , y ) { +xmean := avg (x) ; +ymean := avg (y) ; +avg (( x - xmean ) * (y - ymean )) +} +FUNCTION sd ( x) { +sqrt ( covariance (x , x) ) +} +FUNCTION pairCorr (x , y ) { +covariance (x , y ) / ( sd (x) * sd (y )) +} + +` +p:5 +q:2 +phi:(p+1)?1. +theta:q?1. +"p q phi theta" +p +q +phi +theta +l:() +e:() + +` +L1:10?20 +Le1:10?2. +L2:3?20 +Le2:3?2. +"L1 Le1 L2 Le2" +L1 +Le1 +L2 +Le2 + +` +"Add L1, then predict" +l:l,L1 +e:e,Le1 +predict:(phi(0)) + (sum ({[x](phi(x+1)) * (l(((#l)-1)-x))}[!p])) - (sum ({[x](theta(x)) * (e(((#e)-1)-x))}[!q])) +predict + +` +"Add L2, then predict" +l:l,L2 +e:e,Le2 +predict:(phi(0)) + (sum ({[x](phi(x+1)) * (l(((#l)-1)-x))}[!p])) - (sum ({[x](theta(x)) * (e(((#e)-1)-x))}[!q])) +predict + + + +WITH +Target (Id , TradeDate , ClosePrice ) AS +( SELECT +Id , TradeDate , ClosePrice +FROM price +WHERE Id IN stock10 AND +TradeDate >= startYear10 AND +TradeDate <= startYear10 + 365 * 10), +weekly (Id , bucket , name , low , high , mean ) AS +( SELECT +Id , +timeBucket , +" weekly " , +min ( ClosePrice ) , +max ( ClosePrice ) , +avg ( ClosePrice ) +FROM Target +GROUP BY Id , getWeek ( TradeDate ) as +timeBucket ), +monthly ( Id , bucket , name , low , high , mean ) AS +( SELECT +Id , +timeBucket , +" monthly " , +min ( ClosePrice ) , +max ( ClosePrice ) , +avg ( ClosePrice ) +FROM Target +GROUP BY Id , getMonth ( TradeDate ) as +timeBucket ), +yearly (Id , bucket , name , low , high , mean ) AS +( SELECT +Id , +timeBucket , +" yearly " , +min ( ClosePrice ) , +max ( ClosePrice ) , +avg ( ClosePrice ) +FROM Target +GROUP BY Id , getYear ( TradeDate ) as +timeBucket ) +SELECT +Id , bucket , name , low , high , mean +FROM +CONCATENATE ( weekly , monthly , yearly ) +ASSUMING ASC Id , ASC name , ASC bucket diff --git a/q1.sql b/q1.sql index fb7fcaa..e69de29 100644 --- a/q1.sql +++ b/q1.sql @@ -1,46 +0,0 @@ -WITH -Target (Id , TradeDate , ClosePrice ) AS -( SELECT -Id , TradeDate , ClosePrice -FROM price -WHERE Id IN stock10 AND -TradeDate >= startYear10 AND -TradeDate <= startYear10 + 365 * 10), -weekly (Id , bucket , name , low , high , mean ) AS -( SELECT -Id , -timeBucket , -" weekly " , -min ( ClosePrice ) , -max ( ClosePrice ) , -avg ( ClosePrice ) -FROM Target -GROUP BY Id , getWeek ( TradeDate ) as -timeBucket ), -monthly ( Id , bucket , name , low , high , mean ) AS -( SELECT -Id , -timeBucket , -" monthly " , -min ( ClosePrice ) , -max ( ClosePrice ) , -avg ( ClosePrice ) -FROM Target -GROUP BY Id , getMonth ( TradeDate ) as -timeBucket ), -yearly (Id , bucket , name , low , high , mean ) AS -( SELECT -Id , -timeBucket , -" yearly " , -min ( ClosePrice ) , -max ( ClosePrice ) , -avg ( ClosePrice ) -FROM Target -GROUP BY Id , getYear ( TradeDate ) as -timeBucket ) -SELECT -Id , bucket , name , low , high , mean -FROM -CONCATENATE ( weekly , monthly , yearly ) -ASSUMING ASC Id , ASC name , ASC bucket