TableList is a simple list of joins
authorEric Prud'hommeaux <bertails@w3.org>
Fri, 11 Dec 2009 14:28:11 -0500
changeset 17 f08e5d0a1540
parent 16 658eef0e7116
child 18 43d31777516b
TableList is a simple list of joins
src/main/scala/RDB2RDFMain.scala
src/main/scala/SQL.scala
src/test/scala/SQLTest.scala
--- a/src/main/scala/RDB2RDFMain.scala	Fri Dec 11 13:41:19 2009 -0500
+++ b/src/main/scala/RDB2RDFMain.scala	Fri Dec 11 14:28:11 2009 -0500
@@ -72,6 +72,7 @@
 	NamedAttribute(FQAttribute(Relation(Name("manager")),Attribute(Name("lastName"))),Attribute(Name("managName"))))), 
       // List[Join](), 
       List(
+	Join(TableAlias(Relation(Name("Employee")),Relation(Name("emp"))),None),
 	Join(TableAlias(Relation(Name("Employee")),Relation(Name("manager"))),
 	     Some(Expression(List(
 	       PrimaryExpressionEq(FQAttribute(Relation(Name("manager")),Attribute(Name("id"))),
@@ -86,30 +87,10 @@
       Map[Var, FQAttribute]()
     )
     triples.triplepatterns.foreach(s => r2rState = acc(r2rState, s))
-    val ret = Select(
+    Select(
       r2rState.project,
-      TableList(
-	TableAlias(Relation(Name("Employee")),Relation(Name("emp"))),
-	r2rState.joins
-      ),
+      TableList(r2rState.joins),
       Some(r2rState.exprs)
     )
-    ret
-//     val ret = Select(
-//       AttributeList(List(
-// 	NamedAttribute(FQAttribute(Relation(Name("emp")),Attribute(Name("lastName"))),Attribute(Name("empName"))), 
-// 	NamedAttribute(FQAttribute(Relation(Name("manager")),Attribute(Name("lastName"))),Attribute(Name("managName"))))),
-//       TableList((Relation(Name("Employee")),Relation(Name("emp"))),
-// //		r2rState.attrs
-// 		TableList(
-// 		  TableAlias(Relation(Name("Employee")),Relation(Name("emp"))),
-// 		  List(
-// 		    Join(TableAlias(Relation(Name("Employee")),Relation(Name("manager"))),
-// 			 Expression(
-// 			   List(PrimaryExpressionEq(FQAttribute(Relation(Name("manager")),Attribute(Name("id"))),
-// 						    RValueAttr(FQAttribute(Relation(Name("emp")),Attribute(Name("manager"))))))))))
-// 	      ),Some(Expression(List(PrimaryExpressionNotNull(FQAttribute(Relation(Name("emp")),Attribute(Name("lastName")))), PrimaryExpressionNotNull(FQAttribute(Relation(Name("manager")),Attribute(Name("lastName"))))))))
-//     ret
-
   }
 }
--- a/src/main/scala/SQL.scala	Fri Dec 11 13:41:19 2009 -0500
+++ b/src/main/scala/SQL.scala	Fri Dec 11 14:28:11 2009 -0500
@@ -11,7 +11,7 @@
 case class FQAttribute(relation:Relation, attribute:Attribute)
 case class Attribute(n:Name)
 case class Relation(n:Name)
-case class TableList(tablealias:TableAlias, joins:List[Join])
+case class TableList(joins:List[Join])
 case class Join(tablealias:TableAlias, expression:Option[Expression])
 case class TableAlias(rel:Relation, as:Relation)
 case class Expression(conjuncts:List[PrimaryExpression])
@@ -36,7 +36,7 @@
     "WHERE" ~ expression ^^ { case "WHERE" ~ expression => expression }
 
   def attributelist:Parser[AttributeList] =
-    repsep(namedattribute, ",") ^^ { AttributeList(_)}
+    repsep(namedattribute, ",") ^^ { AttributeList(_) }
 
   def namedattribute:Parser[NamedAttribute] =
     fqattribute ~ "AS" ~ attribute ^^
@@ -54,11 +54,11 @@
     """[a-zA-Z_]\w*""".r ^^ { x => Relation(Name(x)) }
 
   def tablelist:Parser[TableList] =
-    tablealias ~ rep("INNER" ~ "JOIN" ~ tablealias ~ opt(optexpr)) ^^
-    { case tablealias ~ repjoins =>
-        val joins:List[Join] = repjoins map { case "INNER"~"JOIN"~tablealias~optexpr => Join(tablealias, optexpr) }
-        TableList(tablealias, joins)
-    }
+    repsep(join, "INNER" ~ "JOIN") ^^ { TableList(_) }
+
+  def join:Parser[Join] =
+    tablealias ~ opt(optexpr) ^^
+    { case tablealias ~ optexpr => Join(tablealias, optexpr) }
 
   def optexpr:Parser[Expression] =
     "ON" ~ expression ^^ { case "ON"~expression => expression }
--- a/src/test/scala/SQLTest.scala	Fri Dec 11 13:41:19 2009 -0500
+++ b/src/test/scala/SQLTest.scala	Fri Dec 11 14:28:11 2009 -0500
@@ -13,7 +13,7 @@
             INNER JOIN Employee AS manager ON manager.id=emp.manager
  WHERE emp.lastName IS NOT NULL AND manager.lastName IS NOT NULL
 """
-    val expected = Select(AttributeList(List(NamedAttribute(FQAttribute(Relation(Name("emp")),Attribute(Name("lastName"))),Attribute(Name("empName"))), NamedAttribute(FQAttribute(Relation(Name("manager")),Attribute(Name("lastName"))),Attribute(Name("managName"))))),TableList(TableAlias(Relation(Name("Employee")),Relation(Name("emp"))),List(Join(TableAlias(Relation(Name("Employee")),Relation(Name("manager"))),Some(Expression(List(PrimaryExpressionEq(FQAttribute(Relation(Name("manager")),Attribute(Name("id"))),RValueAttr(FQAttribute(Relation(Name("emp")),Attribute(Name("manager"))))))))))),Some(Expression(List(PrimaryExpressionNotNull(FQAttribute(Relation(Name("emp")),Attribute(Name("lastName")))), PrimaryExpressionNotNull(FQAttribute(Relation(Name("manager")),Attribute(Name("lastName"))))))))
+    val expected = Select(AttributeList(List(NamedAttribute(FQAttribute(Relation(Name("emp")),Attribute(Name("lastName"))),Attribute(Name("empName"))), NamedAttribute(FQAttribute(Relation(Name("manager")),Attribute(Name("lastName"))),Attribute(Name("managName"))))),TableList(List(Join(TableAlias(Relation(Name("Employee")),Relation(Name("emp"))),None),Join(TableAlias(Relation(Name("Employee")),Relation(Name("manager"))),Some(Expression(List(PrimaryExpressionEq(FQAttribute(Relation(Name("manager")),Attribute(Name("id"))),RValueAttr(FQAttribute(Relation(Name("emp")),Attribute(Name("manager"))))))))))),Some(Expression(List(PrimaryExpressionNotNull(FQAttribute(Relation(Name("emp")),Attribute(Name("lastName")))), PrimaryExpressionNotNull(FQAttribute(Relation(Name("manager")),Attribute(Name("lastName"))))))))
     assert(expected === (a.parseAll(a.select, e).get))
   }
 
@@ -24,7 +24,7 @@
   FROM Employee AS emp
  WHERE emp.manager=18 AND emp.lastName IS NOT NULL
 """
-    val expected = Select(AttributeList(List(NamedAttribute(FQAttribute(Relation(Name("emp")),Attribute(Name("lastName"))),Attribute(Name("empName"))))),TableList(TableAlias(Relation(Name("Employee")),Relation(Name("emp"))),List()),Some(Expression(List(PrimaryExpressionEq(FQAttribute(Relation(Name("emp")),Attribute(Name("manager"))),RValueInt(Name("18"))), PrimaryExpressionNotNull(FQAttribute(Relation(Name("emp")),Attribute(Name("lastName"))))))))
+    val expected = Select(AttributeList(List(NamedAttribute(FQAttribute(Relation(Name("emp")),Attribute(Name("lastName"))),Attribute(Name("empName"))))),TableList(List(Join(TableAlias(Relation(Name("Employee")),Relation(Name("emp"))),None))),Some(Expression(List(PrimaryExpressionEq(FQAttribute(Relation(Name("emp")),Attribute(Name("manager"))),RValueInt(Name("18"))), PrimaryExpressionNotNull(FQAttribute(Relation(Name("emp")),Attribute(Name("lastName"))))))))
     assert(expected === (a.parseAll(a.select, e).get))
   }
 
@@ -37,7 +37,7 @@
                                        AND manager.lastName="Johnson"
 WHERE emp.lastName IS NOT NULL
 """
-    val expected = Select(AttributeList(List(NamedAttribute(FQAttribute(Relation(Name("emp")),Attribute(Name("lastName"))),Attribute(Name("empName"))))),TableList(TableAlias(Relation(Name("Employee")),Relation(Name("emp"))),List(Join(TableAlias(Relation(Name("Employee")),Relation(Name("manager"))),Some(Expression(List(PrimaryExpressionEq(FQAttribute(Relation(Name("emp")),Attribute(Name("manager"))),RValueAttr(FQAttribute(Relation(Name("manager")),Attribute(Name("id"))))), PrimaryExpressionEq(FQAttribute(Relation(Name("manager")),Attribute(Name("lastName"))),RValueString(Name("Johnson"))))))))),Some(Expression(List(PrimaryExpressionNotNull(FQAttribute(Relation(Name("emp")),Attribute(Name("lastName"))))))))
+    val expected = Select(AttributeList(List(NamedAttribute(FQAttribute(Relation(Name("emp")),Attribute(Name("lastName"))),Attribute(Name("empName"))))),TableList(List(Join(TableAlias(Relation(Name("Employee")),Relation(Name("emp"))),None),Join(TableAlias(Relation(Name("Employee")),Relation(Name("manager"))),Some(Expression(List(PrimaryExpressionEq(FQAttribute(Relation(Name("emp")),Attribute(Name("manager"))),RValueAttr(FQAttribute(Relation(Name("manager")),Attribute(Name("id"))))), PrimaryExpressionEq(FQAttribute(Relation(Name("manager")),Attribute(Name("lastName"))),RValueString(Name("Johnson"))))))))),Some(Expression(List(PrimaryExpressionNotNull(FQAttribute(Relation(Name("emp")),Attribute(Name("lastName"))))))))
     assert(expected === (a.parseAll(a.select, e).get))
   }
 
@@ -54,7 +54,7 @@
                                          AND grandManager.birthday < manager.birthday
  WHERE emp.lastName IS NOT NULL AND grandManager.lastName IS NOT NULL
 """
-    val expected = Select(AttributeList(List(NamedAttribute(FQAttribute(Relation(Name("emp")),Attribute(Name("lastName"))),Attribute(Name("empName"))), NamedAttribute(FQAttribute(Relation(Name("grandManager")),Attribute(Name("lastName"))),Attribute(Name("grandManagName"))))),TableList(TableAlias(Relation(Name("Employee")),Relation(Name("emp"))),List(Join(TableAlias(Relation(Name("Manage")),Relation(Name("lower"))),Some(Expression(List(PrimaryExpressionEq(FQAttribute(Relation(Name("lower")),Attribute(Name("manages"))),RValueAttr(FQAttribute(Relation(Name("emp")),Attribute(Name("id"))))))))), Join(TableAlias(Relation(Name("Employee")),Relation(Name("manager"))),Some(Expression(List(PrimaryExpressionEq(FQAttribute(Relation(Name("manager")),Attribute(Name("id"))),RValueAttr(FQAttribute(Relation(Name("lower")),Attribute(Name("manager"))))), PrimaryExpressionLt(FQAttribute(Relation(Name("manager")),Attribute(Name("birthday"))),RValueAttr(FQAttribute(Relation(Name("emp")),Attribute(Name("birthday"))))))))), Join(TableAlias(Relation(Name("Manage")),Relation(Name("upper"))),Some(Expression(List(PrimaryExpressionEq(FQAttribute(Relation(Name("upper")),Attribute(Name("manages"))),RValueAttr(FQAttribute(Relation(Name("manager")),Attribute(Name("id"))))))))), Join(TableAlias(Relation(Name("Employee")),Relation(Name("grandManager"))),Some(Expression(List(PrimaryExpressionEq(FQAttribute(Relation(Name("grandManager")),Attribute(Name("id"))),RValueAttr(FQAttribute(Relation(Name("upper")),Attribute(Name("manager"))))), PrimaryExpressionLt(FQAttribute(Relation(Name("grandManager")),Attribute(Name("birthday"))),RValueAttr(FQAttribute(Relation(Name("manager")),Attribute(Name("birthday"))))))))))),Some(Expression(List(PrimaryExpressionNotNull(FQAttribute(Relation(Name("emp")),Attribute(Name("lastName")))), PrimaryExpressionNotNull(FQAttribute(Relation(Name("grandManager")),Attribute(Name("lastName"))))))))
+    val expected = Select(AttributeList(List(NamedAttribute(FQAttribute(Relation(Name("emp")),Attribute(Name("lastName"))),Attribute(Name("empName"))), NamedAttribute(FQAttribute(Relation(Name("grandManager")),Attribute(Name("lastName"))),Attribute(Name("grandManagName"))))),TableList(List(Join(TableAlias(Relation(Name("Employee")),Relation(Name("emp"))),None),Join(TableAlias(Relation(Name("Manage")),Relation(Name("lower"))),Some(Expression(List(PrimaryExpressionEq(FQAttribute(Relation(Name("lower")),Attribute(Name("manages"))),RValueAttr(FQAttribute(Relation(Name("emp")),Attribute(Name("id"))))))))), Join(TableAlias(Relation(Name("Employee")),Relation(Name("manager"))),Some(Expression(List(PrimaryExpressionEq(FQAttribute(Relation(Name("manager")),Attribute(Name("id"))),RValueAttr(FQAttribute(Relation(Name("lower")),Attribute(Name("manager"))))), PrimaryExpressionLt(FQAttribute(Relation(Name("manager")),Attribute(Name("birthday"))),RValueAttr(FQAttribute(Relation(Name("emp")),Attribute(Name("birthday"))))))))), Join(TableAlias(Relation(Name("Manage")),Relation(Name("upper"))),Some(Expression(List(PrimaryExpressionEq(FQAttribute(Relation(Name("upper")),Attribute(Name("manages"))),RValueAttr(FQAttribute(Relation(Name("manager")),Attribute(Name("id"))))))))), Join(TableAlias(Relation(Name("Employee")),Relation(Name("grandManager"))),Some(Expression(List(PrimaryExpressionEq(FQAttribute(Relation(Name("grandManager")),Attribute(Name("id"))),RValueAttr(FQAttribute(Relation(Name("upper")),Attribute(Name("manager"))))), PrimaryExpressionLt(FQAttribute(Relation(Name("grandManager")),Attribute(Name("birthday"))),RValueAttr(FQAttribute(Relation(Name("manager")),Attribute(Name("birthday"))))))))))),Some(Expression(List(PrimaryExpressionNotNull(FQAttribute(Relation(Name("emp")),Attribute(Name("lastName")))), PrimaryExpressionNotNull(FQAttribute(Relation(Name("grandManager")),Attribute(Name("lastName"))))))))
     assert(expected === (a.parseAll(a.select, e).get))
   }