[Swift-commit] r5274 - provenancedb

lgadelha at ci.uchicago.edu lgadelha at ci.uchicago.edu
Tue Nov 8 18:10:54 CST 2011


Author: lgadelha
Date: 2011-11-08 18:10:53 -0600 (Tue, 08 Nov 2011)
New Revision: 5274

Modified:
   provenancedb/ProvSQL.g
Log:
Inclusion of built-in function compare_run, that abstract provenance correlation queries.


Modified: provenancedb/ProvSQL.g
===================================================================
--- provenancedb/ProvSQL.g	2011-11-07 19:46:44 UTC (rev 5273)
+++ provenancedb/ProvSQL.g	2011-11-09 00:10:53 UTC (rev 5274)
@@ -16,14 +16,15 @@
 	String whereClause = new String(); 
 	boolean hasWhereJoinExpression;
 	boolean hasWhereExpression = false;
+	static boolean hasCompareRunCall = false;
 	HashSet<String> relations = new HashSet<String>();
 	UndirectedGraph<String,DefaultEdge> schemaGraph;
 	HashSet<DefaultEdge> joinEdges;
+	static HashSet<String> compareRunParams = new HashSet<String>();;
 	
 	// Ideally it could receive a DB schema in SQL and build the graph automatically
 	public static UndirectedGraph<String,DefaultEdge> buildGraph() {
 		UndirectedGraph<String,DefaultEdge> schemaGraph = new Multigraph<String,DefaultEdge>(DefaultEdge.class);
-
 		schemaGraph.addVertex("a_run_t");
 		schemaGraph.addVertex("a_run_n");
 		schemaGraph.addVertex("run");
@@ -60,7 +61,6 @@
 		schemaGraph.addEdge("ds", "in_mem");
 		schemaGraph.addEdge("ds", "ds_cont");
 		schemaGraph.addEdge("ds", "ds_cont");
-		schemaGraph.addEdge("compare_run", "run");
 
 		return schemaGraph;
 	}
@@ -78,13 +78,10 @@
 			Iterator<DefaultEdge> j = (sP.getPathEdgeList()).iterator();
 			while(j.hasNext())
 				jEdges.add(j.next());
-		}
-
-		
+		}	
 		return jEdges;
 	}
 
-	
 	public static String computeFrom(UndirectedGraph<String,DefaultEdge> schemaGraph, HashSet<DefaultEdge> joinEdges, HashSet<String> qrels) {
 		HashSet<String> fromRels = new HashSet<String>();
 		String fromq = " FROM ";
@@ -116,15 +113,18 @@
 						fromRels.add(schemaGraph.getEdgeSource(aux));
 						fromRels.add(schemaGraph.getEdgeTarget(aux));				
 					}
-
 				}
 			}
 		Iterator<String> j = fromRels.iterator();
 		if(j.hasNext())
 			fromq += j.next();
 		while(j.hasNext())
-			fromq+=","+j.next();
-
+			fromq += "," + j.next();
+		if(hasCompareRunCall) {
+			if(fromRels.size() > 0)
+				fromq += ",";
+			fromq += "(" + computeCompareRunQuery(compareRunParams) + ") AS compare_run";
+		}
 		return fromq;
 	}
 
@@ -152,7 +152,6 @@
 		joinExpressions.put(schemaGraph.getEdge("ds", "in_mem"), "ds.id=in_mem.id");
 		joinExpressions.put(schemaGraph.getEdge("ds", "ds_cont"), "ds.id=ds_cont.in_id");
 		joinExpressions.put(schemaGraph.getEdge("ds", "ds_cont"), "ds.id=ds_cont.out_id");
-		joinExpressions.put(schemaGraph.getEdge("compare_run", "ds_cont"), "ds.id=ds_cont.out_id");
 
 		Iterator<DefaultEdge> i = jEdges.iterator();
 		if(i.hasNext()) {
@@ -189,9 +188,34 @@
 
 			}    		
 		}
-
 		return joinExpressionsString;
 	}
+
+	public static String computeCompareRunQuery(HashSet<String> atoms) {
+		String compareRunSelectClause = "SELECT run_id";
+		String compareRunFromClause = "FROM";
+		Iterator<String> i = atoms.iterator();
+		int nId = 0;
+		for(String arg: atoms) {
+			String[] argTokens = arg.split("=");
+			if(argTokens[0].equals("key_numeric") ||
+			   argTokens[0].equals("key_text")    ||
+			   argTokens[0].equals("parameter"))
+			{
+				String key = argTokens[1].split("'")[1];
+				nId++;
+				String sId = "j" + nId;
+				compareRunSelectClause+=", " + sId + ".value as " + key;
+				if(nId>1)
+					compareRunFromClause += " INNER JOIN";
+				compareRunFromClause += " compare_run_by_" + argTokens[0] + "(\'" + key + "\') as " + sId;
+				if(nId>1)
+					compareRunFromClause += " USING (run_id)";
+			}
+		}
+		String compareRunQuery = compareRunSelectClause + " " + compareRunFromClause;
+		return compareRunQuery;
+	}
 	
 }
 
@@ -322,7 +346,7 @@
 			selectClause+=$b.text;
 		} 
 		'(' 	{ selectClause+="("; }
-		c=entityAttribute  
+		c=entityAndAttribute  
 		{ 
 			selectClause += $c.text; 
 			relations.add($c.text.split("\\.")[0]);
@@ -331,7 +355,7 @@
 		}
 		')' 	{ selectClause+=")"; }
 		|
-		builtInAttribute
+		builtInProcedureAttribute 
 	;
 
 selectExpression
@@ -361,11 +385,16 @@
 	;
 
 whereAtom 
-	:	a=entityAndAttribute 
-		{ 
+	:	(a=entityAndAttribute 
+		{
 			relations.add($a.text.split("\\.")[0]);
 			whereClause += $a.text;
 		}
+		|
+		j=COMPARERUN { whereClause+="comapare_run"; }
+		DOT 
+		k=ID { whereClause+="."+$k.text; } 
+		)
 			(
 		NOT
 		{
@@ -560,15 +589,61 @@
 
 entityAttribute	:	ID (DOT ID)?;
 
-builtInAttribute
-	:	COMPARERUN '(' builtInAtom (COLON builtInAtom)* ')' (DOT ID)?;
-
-builtInAtom
-	:	('parameter' | 'key' ) '=' STRING ;
-	
 entityAndAttribute
 	:	ID DOT ID;
 
+
+builtInProcedureAttribute
+	:	COMPARERUN {
+			boolean hasAttribute = false;
+		}
+		{
+			hasCompareRunCall=true;
+		}
+		'(' 
+		a=builtInAtom
+		{ 
+			compareRunParams.add($a.text);
+			if(relations.size() > 0)
+				relations.add("run");
+		}
+	 	(COLON 
+	 	b=builtInAtom
+	 	{ 
+			compareRunParams.add($b.text);
+			if(relations.size() > 0)
+				relations.add("run");
+		} 		
+	 	)* ')' ( 
+	 	DOT
+	 	{
+	 		hasAttribute = true;
+	 	}
+	 	 (
+	 	c=ID
+	 	{
+	 		selectClause += "compare_run." + $c.text;
+	 	}
+	 		 | '{' 
+	 	d=ID
+	 	{
+	 		selectClause += "compare_run." + $d.text;
+	 	}
+	 	(COLON 
+	 	e=ID 
+	 	{ 
+	 		selectClause += ", compare_run." + $e.text; 
+	 	}
+	 	)* '}'))?
+	 	{
+	 		if(!hasAttribute)
+	 			selectClause += "compare_run.*";
+	 	}
+;
+
+builtInAtom 
+	:	('parameter' | 'key_numeric' | 'key_text') OP STRING;
+
 OP	:	'=' | '>' | '>=' | '<' | '<=';
 
 GROUP	:	'group';
@@ -590,39 +665,40 @@
 
 ASC	:	'asc';
 
+
 DISTINCT 
-	:	'd' 'i' 's' 't' 'i' 'n' 'c' 't';
+	:	'distinct';
 	
-WHERE	:	'w' 'h' 'e' 'r' 'e';
+WHERE	:	'where';
 
-AND	:	'a' 'n' 'd';
+AND	:	'and';
 
-OR	:	'o' 'r';
+OR	:	'or';
 
-NOT	:	'n' 'o' 't';
+NOT	:	'not';
 
-IN	:	'i' 'n';
+IN	:	'in';
 
-ANY	:	'a' 'n' 'y';
+ANY	:	'any';
 
-UNION	:	'u' 'n' 'i' 'o' 'n';
+UNION	:	'union';
 
 INTERSECT 
-	:	'i' 'n' 't' 'e' 'r' 's' 'e' 'c' 't';
+	:	'intersect';
 
-EXCEPT	:	'e' 'x' 'c' 'e' 'p' 't';
+EXCEPT	:	'except';
 
-ALL	:	'a' 'l' 'l';
+ALL	:	'all';
 
 DOT	:	'.';
 
 COLON	:	',';
 
-BETWEEN	:	'b' 'e' 't' 'w' 'e' 'e' 'n';
+BETWEEN	:	'between';
 
 HAVING	:	'having';
 
-LIKE 	:	'l' 'i' 'k' 'e';
+LIKE 	:	'like';
 
 SEMICOLON	:	';';
 




More information about the Swift-commit mailing list