moved join constraints out to wheres
authorEric Prud'hommeaux <bertails@w3.org>
Wed, 16 Dec 2009 17:08:03 -0500
changeset 54 ff781439b4e0
parent 53 8ea6f8c6fcaf
child 55 f4ed11084996
moved join constraints out to wheres
src/main/scala/RDB2RDFMain.scala
src/main/scala/SQL.scala
src/test/scala/RDB2RDFTest.scala
src/test/scala/SQLTest.scala
--- a/src/main/scala/RDB2RDFMain.scala	Wed Dec 16 16:29:59 2009 -0500
+++ b/src/main/scala/RDB2RDFMain.scala	Wed Dec 16 17:08:03 2009 -0500
@@ -127,8 +127,8 @@
 	val objattr = RelAliasAttribute(relalias, attr)
 
 	// println(rel.n.s + " AS " + relalias.n.s)
-	val sconstraint:List[PrimaryExpression] = s match {
-	  case SUri(u) => List(uriConstraint(subjattr, u))
+	s match {
+	  case SUri(u) => exprs = exprs ::: List(uriConstraint(subjattr, u))
 	  case SVar(v) => {
 	    val binding:SQL2RDFValueMapper = varConstraint(subjattr, v, db, rel)
 	    varmap += v -> binding
@@ -174,16 +174,20 @@
 	      case false => {
 
 		sjoin match { // complex dance to keep joins ordered -- ouch!
-		  case Some(x) => joins = joins ::: List(Join(x, Expression(sconstraint)))
+		  case Some(x) => joins = joins ::: List(Join(x))
 		  case None => 
 		}
 
-		joins = joins ::: List(Join(RelAsRelAlias(fkrel,oRelAlias), Expression(conjuncts)))
+		exprs = exprs ::: conjuncts
+		joins = joins ::: List(Join(RelAsRelAlias(fkrel,oRelAlias)))
 		joined = joined + oRelAlias
 	      }
 	      case true => {
 		sjoin match {
-		  case Some(x) => joins = joins ::: List(Join(x, Expression(conjuncts)))
+		  case Some(x) => {
+		    exprs = exprs ::: conjuncts
+		    joins = joins ::: List(Join(x))
+		  }
 		  case None => 
 		}
 	      }
@@ -204,7 +208,7 @@
 	      }
 	    }
 	    sjoin match {
-	      case Some(x) => joins = joins ::: List(Join(x, Expression(sconstraint)))
+	      case Some(x) => joins = joins ::: List(Join(x))
 	      case None => 
 	    }
 
--- a/src/main/scala/SQL.scala	Wed Dec 16 16:29:59 2009 -0500
+++ b/src/main/scala/SQL.scala	Wed Dec 16 17:08:03 2009 -0500
@@ -13,7 +13,7 @@
 case class Relation(n:Name)
 case class RelAlias(n:Name)
 case class TableList(joins:List[Join])
-case class Join(relasalias:RelAsRelAlias, expression:Expression)
+case class Join(relasalias:RelAsRelAlias)
 case class RelAsRelAlias(rel:Relation, as:RelAlias)
 case class Expression(conjuncts:List[PrimaryExpression])
 sealed abstract class PrimaryExpression
@@ -88,19 +88,7 @@
     repsep(join, "INNER" ~ "JOIN") ^^ { TableList(_) }
 
   def join:Parser[Join] =
-    relasalias ~ opt(optexpr) ^^
-    {
-      case relasalias ~ optexpr => {
-	val expression:Expression = optexpr match {
-	  case None => Expression(List())
-	  case Some(f) => f
-	}
-	Join(relasalias, expression)
-      }
-    }
-
-  def optexpr:Parser[Expression] =
-    "ON" ~ expression ^^ { case "ON"~expression => expression }
+    relasalias ^^ { x => Join(x) }
 
   def relasalias:Parser[RelAsRelAlias] =
     relation ~ "AS" ~ relalias ^^
--- a/src/test/scala/RDB2RDFTest.scala	Wed Dec 16 16:29:59 2009 -0500
+++ b/src/test/scala/RDB2RDFTest.scala	Wed Dec 16 17:08:03 2009 -0500
@@ -42,7 +42,8 @@
     val sqlSelect = sqlParser.parseAll(sqlParser.select, """
 SELECT R_emp.id AS A_emp
        FROM Employee AS R_emp
-            INNER JOIN Employee AS R_id18 ON R_id18.id=R_emp.manager AND R_id18.id=18
+            INNER JOIN Employee AS R_id18
+ WHERE R_id18.id=R_emp.manager AND R_id18.id=18
 """).get
     assert(RDB2RDF(db, sparqlSelect, StemURI("http://hr.example/DB/"), PrimaryKey(Attribute(Name("id")))) === sqlSelect)
     true
@@ -61,7 +62,8 @@
     val sqlSelect = sqlParser.parseAll(sqlParser.select, """
 SELECT R_emp.id AS A_emp
        FROM Employee AS R_emp
-            INNER JOIN Employee AS R_18 ON R_18.id=R_emp.manager AND R_18.id=18
+            INNER JOIN Employee AS R_18
+ WHERE R_18.id=R_emp.manager AND R_18.id=18
 """).get
     assert(RDB2RDF(db, sparqlSelect, StemURI("http://hr.example/DB/"), PrimaryKey(Attribute(Name("id")))) === sqlSelect)
     true
@@ -80,8 +82,8 @@
     val sqlSelect = sqlParser.parseAll(sqlParser.select, """
 SELECT R_emp.lastName AS A_empName, R_manager.lastName AS A_manageName
        FROM Employee AS R_emp
-            INNER JOIN Employee AS R_manager ON R_manager.id=R_emp.manager
- WHERE R_emp.lastName IS NOT NULL AND R_manager.lastName IS NOT NULL
+            INNER JOIN Employee AS R_manager
+ WHERE R_manager.id=R_emp.manager AND R_emp.lastName IS NOT NULL AND R_manager.lastName IS NOT NULL
 """).get
     assert(RDB2RDF(db, sparqlSelect, StemURI("http://hr.example/DB/"), PrimaryKey(Attribute(Name("id")))) === sqlSelect)
   }
@@ -98,8 +100,8 @@
     val sqlSelect = sqlParser.parseAll(sqlParser.select, """
 SELECT R_emp.lastName AS A_empName
        FROM Employee AS R_emp
-            INNER JOIN Employee AS R_id18 ON R_id18.id=R_emp.manager AND R_id18.id=18
- WHERE R_emp.lastName IS NOT NULL
+            INNER JOIN Employee AS R_id18
+ WHERE R_id18.id=R_emp.manager AND R_id18.id=18 AND R_emp.lastName IS NOT NULL
 """).get
     assert(RDB2RDF(db, sparqlSelect, StemURI("http://hr.example/DB/"), PrimaryKey(Attribute(Name("id")))) === sqlSelect)
   }
@@ -118,8 +120,8 @@
     val sqlSelect = sqlParser.parseAll(sqlParser.select, """
 SELECT R_emp.lastName AS A_empName
   FROM Employee AS R_emp
-       INNER JOIN Employee AS R_manager ON R_manager.id=R_emp.manager
-WHERE R_manager.lastName="Johnson" AND R_emp.lastName IS NOT NULL
+       INNER JOIN Employee AS R_manager
+WHERE R_manager.id=R_emp.manager AND R_manager.lastName="Johnson" AND R_emp.lastName IS NOT NULL
 """).get
     assert(RDB2RDF(db, sparqlSelect, StemURI("http://hr.example/DB/"), PrimaryKey(Attribute(Name("id")))) === sqlSelect)
   }
@@ -144,11 +146,11 @@
     val sqlSelect = sqlParser.parseAll(sqlParser.select, """
 SELECT R_emp.lastName AS A_empName, R_grandManager.lastName AS A_grandManagName
   FROM Employee AS R_emp
-       INNER JOIN Manage AS R_lower ON R_emp.id=R_lower.manages
-       INNER JOIN Employee AS R_manager ON R_manager.id=R_lower.manager
-       INNER JOIN Manage AS R_upper ON R_manager.id=R_upper.manages
-       INNER JOIN Employee AS R_grandManager ON R_grandManager.id=R_upper.manager
- WHERE R_manager.birthday < R_emp.birthday AND R_grandManager.birthday < R_manager.birthday AND R_emp.lastName IS NOT NULL AND R_grandManager.lastName IS NOT NULL
+       INNER JOIN Manage AS R_lower
+       INNER JOIN Employee AS R_manager
+       INNER JOIN Manage AS R_upper
+       INNER JOIN Employee AS R_grandManager
+ WHERE R_emp.id=R_lower.manages AND R_manager.id=R_lower.manager AND R_manager.id=R_upper.manages AND R_grandManager.id=R_upper.manager AND R_manager.birthday < R_emp.birthday AND R_grandManager.birthday < R_manager.birthday AND R_emp.lastName IS NOT NULL AND R_grandManager.lastName IS NOT NULL
 """).get
     assert(RDB2RDF(db2, sparqlSelect, StemURI("http://hr.example/DB/"), PrimaryKey(Attribute(Name("id")))) === sqlSelect)
   }
--- a/src/test/scala/SQLTest.scala	Wed Dec 16 16:29:59 2009 -0500
+++ b/src/test/scala/SQLTest.scala	Wed Dec 16 17:08:03 2009 -0500
@@ -10,8 +10,8 @@
     val e = """
 SELECT R_emp.lastName AS A_empName, R_manager.lastName AS A_managName
        FROM Employee AS R_emp
-            INNER JOIN Employee AS R_manager ON R_manager.id=R_emp.manager
- WHERE R_emp.lastName IS NOT NULL AND R_manager.lastName IS NOT NULL
+            INNER JOIN Employee AS R_manager
+ WHERE R_manager.id=R_emp.manager AND R_emp.lastName IS NOT NULL AND R_manager.lastName IS NOT NULL
 """
     val expected = Select(AttributeList(List(NamedAttribute(RelAliasAttribute(RelAlias(Name("R_emp")),
 									      Attribute(Name("lastName"))),
@@ -19,16 +19,13 @@
 					     NamedAttribute(RelAliasAttribute(RelAlias(Name("R_manager")),
 									      Attribute(Name("lastName"))),
 							    AttrAlias(Name("A_managName"))))),
-			  TableList(List(Join(RelAsRelAlias(Relation(Name("Employee")),RelAlias(Name("R_emp"))),Expression(List())),
-					 Join(RelAsRelAlias(Relation(Name("Employee")),RelAlias(Name("R_manager"))),
-					      Expression(List(PrimaryExpressionEq(RelAliasAttribute(RelAlias(Name("R_manager")),
-												    Attribute(Name("id"))),
-										  RValueAttr(RelAliasAttribute(RelAlias(Name("R_emp")),
-													       Attribute(Name("manager")))))))))),
-			  Expression(List(PrimaryExpressionNotNull(RelAliasAttribute(RelAlias(Name("R_emp")),
-										     Attribute(Name("lastName")))),
-					  PrimaryExpressionNotNull(RelAliasAttribute(RelAlias(Name("R_manager")),
-										     Attribute(Name("lastName")))))))
+			  TableList(List(Join(RelAsRelAlias(Relation(Name("Employee")),RelAlias(Name("R_emp")))),
+					 Join(RelAsRelAlias(Relation(Name("Employee")),RelAlias(Name("R_manager")))))),
+			  Expression(List(
+			    PrimaryExpressionEq(RelAliasAttribute(RelAlias(Name("R_manager")),Attribute(Name("id"))),
+						RValueAttr(RelAliasAttribute(RelAlias(Name("R_emp")),Attribute(Name("manager"))))),
+			    PrimaryExpressionNotNull(RelAliasAttribute(RelAlias(Name("R_emp")),Attribute(Name("lastName")))),
+			    PrimaryExpressionNotNull(RelAliasAttribute(RelAlias(Name("R_manager")),Attribute(Name("lastName")))))))
     assert(expected === (a.parseAll(a.select, e).get))
   }
 
@@ -42,12 +39,10 @@
     val expected = Select(AttributeList(List(NamedAttribute(RelAliasAttribute(RelAlias(Name("R_emp")),
 									      Attribute(Name("lastName"))),
 							    AttrAlias(Name("A_empName"))))),
-			  TableList(List(Join(RelAsRelAlias(Relation(Name("Employee")),RelAlias(Name("R_emp"))),Expression(List())))),
-			  Expression(List(PrimaryExpressionEq(RelAliasAttribute(RelAlias(Name("R_emp")),
-										Attribute(Name("manager"))),
+			  TableList(List(Join(RelAsRelAlias(Relation(Name("Employee")),RelAlias(Name("R_emp")))))),
+			  Expression(List(PrimaryExpressionEq(RelAliasAttribute(RelAlias(Name("R_emp")),Attribute(Name("manager"))),
 							      RValueTyped(SQLDatatype.INTEGER,Name("18"))),
-					  PrimaryExpressionNotNull(RelAliasAttribute(RelAlias(Name("R_emp")),
-										     Attribute(Name("lastName")))))))
+					  PrimaryExpressionNotNull(RelAliasAttribute(RelAlias(Name("R_emp")),Attribute(Name("lastName")))))))
     assert(expected === (a.parseAll(a.select, e).get))
   }
 
@@ -56,23 +51,19 @@
     val e = """
 SELECT R_emp.lastName AS A_empName
   FROM Employee AS R_emp
-       INNER JOIN Employee AS R_manager ON R_emp.manager=R_manager.id
-                                       AND R_manager.lastName="Johnson"
-WHERE R_emp.lastName IS NOT NULL
+       INNER JOIN Employee AS R_manager
+WHERE R_emp.manager=R_manager.id AND R_manager.lastName="Johnson" AND R_emp.lastName IS NOT NULL
 """
     val expected = Select(AttributeList(List(NamedAttribute(RelAliasAttribute(RelAlias(Name("R_emp")),
 									      Attribute(Name("lastName"))),
 							    AttrAlias(Name("A_empName"))))),
-			  TableList(List(Join(RelAsRelAlias(Relation(Name("Employee")),RelAlias(Name("R_emp"))),Expression(List())),
-					 Join(RelAsRelAlias(Relation(Name("Employee")),RelAlias(Name("R_manager"))),
-					      Expression(List(PrimaryExpressionEq(RelAliasAttribute(RelAlias(Name("R_emp")),
-												    Attribute(Name("manager"))),
-										  RValueAttr(RelAliasAttribute(RelAlias(Name("R_manager")),
-													       Attribute(Name("id"))))),
-							      PrimaryExpressionEq(RelAliasAttribute(RelAlias(Name("R_manager")),
-												    Attribute(Name("lastName"))),
-										  RValueTyped(SQLDatatype.STRING,Name("Johnson")))))))),
-			  Expression(List(PrimaryExpressionNotNull(RelAliasAttribute(RelAlias(Name("R_emp")),Attribute(Name("lastName")))))))
+			  TableList(List(Join(RelAsRelAlias(Relation(Name("Employee")),RelAlias(Name("R_emp")))),
+					 Join(RelAsRelAlias(Relation(Name("Employee")),RelAlias(Name("R_manager")))))),
+			  Expression(List(PrimaryExpressionEq(RelAliasAttribute(RelAlias(Name("R_emp")),Attribute(Name("manager"))),
+							      RValueAttr(RelAliasAttribute(RelAlias(Name("R_manager")),Attribute(Name("id"))))),
+					  PrimaryExpressionEq(RelAliasAttribute(RelAlias(Name("R_manager")),Attribute(Name("lastName"))),
+							      RValueTyped(SQLDatatype.STRING,Name("Johnson"))),
+					  PrimaryExpressionNotNull(RelAliasAttribute(RelAlias(Name("R_emp")),Attribute(Name("lastName")))))))
     assert(expected === (a.parseAll(a.select, e).get))
   }
 
@@ -81,35 +72,39 @@
     val e = """
 SELECT R_emp.lastName AS A_empName, R_grandManager.lastName AS A_grandManagName
   FROM Employee AS R_emp
-       INNER JOIN Manage AS R_lower ON R_lower.manages=R_emp.id
-       INNER JOIN Employee AS R_manager ON R_manager.id=R_lower.manager
-                                         AND R_manager.birthday < R_emp.birthday
-       INNER JOIN Manage AS R_upper ON R_upper.manages=R_manager.id
-       INNER JOIN Employee AS R_grandManager ON R_grandManager.id=R_upper.manager
-                                         AND R_grandManager.birthday < R_manager.birthday
- WHERE R_emp.lastName IS NOT NULL AND R_grandManager.lastName IS NOT NULL
+       INNER JOIN Manage AS R_lower
+       INNER JOIN Employee AS R_manager
+       INNER JOIN Manage AS R_upper
+       INNER JOIN Employee AS R_grandManager
+ WHERE R_lower.manages=R_emp.id AND R_manager.id=R_lower.manager
+   AND R_manager.birthday < R_emp.birthday
+   AND R_upper.manages=R_manager.id AND R_grandManager.id=R_upper.manager
+   AND R_grandManager.birthday < R_manager.birthday
+   AND R_emp.lastName IS NOT NULL AND R_grandManager.lastName IS NOT NULL
 """
     val expected = Select(AttributeList(List(NamedAttribute(RelAliasAttribute(RelAlias(Name("R_emp")), Attribute(Name("lastName"))),
 							    AttrAlias(Name("A_empName"))),
 					     NamedAttribute(RelAliasAttribute(RelAlias(Name("R_grandManager")),Attribute(Name("lastName"))),
 							    AttrAlias(Name("A_grandManagName"))))),
-			  TableList(List(Join(RelAsRelAlias(Relation(Name("Employee")),RelAlias(Name("R_emp"))),Expression(List())),
-					 Join(RelAsRelAlias(Relation(Name("Manage")),RelAlias(Name("R_lower"))),Expression(List(PrimaryExpressionEq(RelAliasAttribute(RelAlias(Name("R_lower")),Attribute(Name("manages"))),RValueAttr(RelAliasAttribute(RelAlias(Name("R_emp")),Attribute(Name("id")))))))),
-					 Join(RelAsRelAlias(Relation(Name("Employee")),RelAlias(Name("R_manager"))),Expression(List(PrimaryExpressionEq(RelAliasAttribute(RelAlias(Name("R_manager")),Attribute(Name("id"))),RValueAttr(RelAliasAttribute(RelAlias(Name("R_lower")),Attribute(Name("manager"))))), PrimaryExpressionLt(RelAliasAttribute(RelAlias(Name("R_manager")),Attribute(Name("birthday"))),RValueAttr(RelAliasAttribute(RelAlias(Name("R_emp")),Attribute(Name("birthday")))))))),
-					 Join(RelAsRelAlias(Relation(Name("Manage")),RelAlias(Name("R_upper"))),Expression(List(PrimaryExpressionEq(RelAliasAttribute(RelAlias(Name("R_upper")),Attribute(Name("manages"))),RValueAttr(RelAliasAttribute(RelAlias(Name("R_manager")),Attribute(Name("id")))))))),
-					 Join(RelAsRelAlias(Relation(Name("Employee")),RelAlias(Name("R_grandManager"))),
-					      Expression(List(PrimaryExpressionEq(RelAliasAttribute(RelAlias(Name("R_grandManager")),
-												    Attribute(Name("id"))),
-										  RValueAttr(RelAliasAttribute(RelAlias(Name("R_upper")),
-													       Attribute(Name("manager"))))),
-							      PrimaryExpressionLt(RelAliasAttribute(RelAlias(Name("R_grandManager")),
-												    Attribute(Name("birthday"))),
-										  RValueAttr(RelAliasAttribute(RelAlias(Name("R_manager")),
-													       Attribute(Name("birthday")))))))))),
-			  Expression(List(PrimaryExpressionNotNull(RelAliasAttribute(RelAlias(Name("R_emp")),
-										     Attribute(Name("lastName")))),
-					  PrimaryExpressionNotNull(RelAliasAttribute(RelAlias(Name("R_grandManager")),
-										     Attribute(Name("lastName")))))))
+			  TableList(List(Join(RelAsRelAlias(Relation(Name("Employee")),RelAlias(Name("R_emp")))),
+					 Join(RelAsRelAlias(Relation(Name("Manage")),RelAlias(Name("R_lower")))),
+					 Join(RelAsRelAlias(Relation(Name("Employee")),RelAlias(Name("R_manager")))),
+					 Join(RelAsRelAlias(Relation(Name("Manage")),RelAlias(Name("R_upper")))),
+					 Join(RelAsRelAlias(Relation(Name("Employee")),RelAlias(Name("R_grandManager")))))),
+			  Expression(List(PrimaryExpressionEq(RelAliasAttribute(RelAlias(Name("R_lower")),Attribute(Name("manages"))),
+							      RValueAttr(RelAliasAttribute(RelAlias(Name("R_emp")),Attribute(Name("id"))))),
+					  PrimaryExpressionEq(RelAliasAttribute(RelAlias(Name("R_manager")),Attribute(Name("id"))),
+							      RValueAttr(RelAliasAttribute(RelAlias(Name("R_lower")),Attribute(Name("manager"))))),
+					  PrimaryExpressionLt(RelAliasAttribute(RelAlias(Name("R_manager")),Attribute(Name("birthday"))),
+							      RValueAttr(RelAliasAttribute(RelAlias(Name("R_emp")),Attribute(Name("birthday"))))),
+					  PrimaryExpressionEq(RelAliasAttribute(RelAlias(Name("R_upper")),Attribute(Name("manages"))),
+							      RValueAttr(RelAliasAttribute(RelAlias(Name("R_manager")),Attribute(Name("id"))))),
+					  PrimaryExpressionEq(RelAliasAttribute(RelAlias(Name("R_grandManager")),Attribute(Name("id"))),
+							      RValueAttr(RelAliasAttribute(RelAlias(Name("R_upper")),Attribute(Name("manager"))))),
+					  PrimaryExpressionLt(RelAliasAttribute(RelAlias(Name("R_grandManager")),Attribute(Name("birthday"))),
+							      RValueAttr(RelAliasAttribute(RelAlias(Name("R_manager")),Attribute(Name("birthday"))))),
+					  PrimaryExpressionNotNull(RelAliasAttribute(RelAlias(Name("R_emp")),Attribute(Name("lastName")))),
+					  PrimaryExpressionNotNull(RelAliasAttribute(RelAlias(Name("R_grandManager")),Attribute(Name("lastName")))))))
     assert(expected === (a.parseAll(a.select, e).get))
   }