Join and Select always have (non-Option) Expression
authorEric Prud'hommeaux <bertails@w3.org>
Wed, 16 Dec 2009 16:29:59 -0500
changeset 53 8ea6f8c6fcaf
parent 52 8955fa73705e
child 54 ff781439b4e0
Join and Select always have (non-Option) Expression
src/main/scala/RDB2RDFMain.scala
src/main/scala/SQL.scala
src/test/scala/SQLTest.scala
--- a/src/main/scala/RDB2RDFMain.scala	Wed Dec 16 15:15:32 2009 -0500
+++ b/src/main/scala/RDB2RDFMain.scala	Wed Dec 16 16:29:59 2009 -0500
@@ -49,14 +49,14 @@
     RelAlias(Name("R_" + v))
   }
 
-  def uriConstraint(constrainMe:RelAliasAttribute, u:ObjUri):Expression = {
+  def uriConstraint(constrainMe:RelAliasAttribute, u:ObjUri):PrimaryExpression = {
     // println("equiv+= " + toString(constrainMe) + "=" + value)
-    Expression(List(PrimaryExpressionEq(constrainMe,RValueTyped(SQLDatatype.INTEGER,Name(u.v.s)))))
+    PrimaryExpressionEq(constrainMe,RValueTyped(SQLDatatype.INTEGER,Name(u.v.s)))
   }
 
-  def literalConstraint(constrainMe:RelAliasAttribute, lit:SparqlLiteral, dt:SQLDatatype):Expression = {
+  def literalConstraint(constrainMe:RelAliasAttribute, lit:SparqlLiteral, dt:SQLDatatype):PrimaryExpression = {
     // println("equiv+= " + toString(attr) + "=" + lit)
-    Expression(List(PrimaryExpressionEq(constrainMe,RValueTyped(dt,Name(lit.lit.lexicalForm)))))
+    PrimaryExpressionEq(constrainMe,RValueTyped(dt,Name(lit.lit.lexicalForm)))
   }
 
   /** varConstraint
@@ -127,15 +127,12 @@
 	val objattr = RelAliasAttribute(relalias, attr)
 
 	// println(rel.n.s + " AS " + relalias.n.s)
-	val sconstraint:Option[Expression] = s match {
-	  case SUri(u) => {
-	    uriConstraint(subjattr, u)
-	    None
-	  }
+	val sconstraint:List[PrimaryExpression] = s match {
+	  case SUri(u) => List(uriConstraint(subjattr, u))
 	  case SVar(v) => {
 	    val binding:SQL2RDFValueMapper = varConstraint(subjattr, v, db, rel)
 	    varmap += v -> binding
-	    None
+	    List()
 	  }
 	}
 	val sjoin = joined contains(relalias) match {
@@ -162,9 +159,9 @@
 
 	      /* Literal foreign keys should probably throw an error,
 	       * instead does what user meant. */
-	      case OLit(l) => List(joinconstraint) ::: literalConstraint(fkaliasattr, l, dt).conjuncts
+	      case OLit(l) => List(joinconstraint) ::: List(literalConstraint(fkaliasattr, l, dt))
 
-	      case OUri(u) => List(joinconstraint) ::: uriConstraint(fkaliasattr, u).conjuncts
+	      case OUri(u) => List(joinconstraint) ::: List(uriConstraint(fkaliasattr, u))
 
 	      case OVar(v) => {
 		val binding = varConstraint(fkaliasattr, v, db, fkrel)
@@ -177,16 +174,16 @@
 	      case false => {
 
 		sjoin match { // complex dance to keep joins ordered -- ouch!
-		  case Some(x) => joins = joins ::: List(Join(x, sconstraint))
+		  case Some(x) => joins = joins ::: List(Join(x, Expression(sconstraint)))
 		  case None => 
 		}
 
-		joins = joins ::: List(Join(RelAsRelAlias(fkrel,oRelAlias), Some(Expression(conjuncts))))
+		joins = joins ::: List(Join(RelAsRelAlias(fkrel,oRelAlias), Expression(conjuncts)))
 		joined = joined + oRelAlias
 	      }
 	      case true => {
 		sjoin match {
-		  case Some(x) => joins = joins ::: List(Join(x, Some(Expression(conjuncts))))
+		  case Some(x) => joins = joins ::: List(Join(x, Expression(conjuncts)))
 		  case None => 
 		}
 	      }
@@ -195,8 +192,8 @@
 	  case Value(dt) => {
 	    o match {
 	      case OLit(l) => {
-		val c = literalConstraint(objattr, l, dt).conjuncts
-		exprs = exprs ::: c
+		val c = literalConstraint(objattr, l, dt)
+		exprs = exprs ::: List(c)
 	      }
 	      case OUri(u) => uriConstraint(objattr, u)
 	      case OVar(v) => {
@@ -207,7 +204,7 @@
 	      }
 	    }
 	    sjoin match {
-	      case Some(x) => joins = joins ::: List(Join(x, sconstraint))
+	      case Some(x) => joins = joins ::: List(Join(x, Expression(sconstraint)))
 	      case None => 
 	    }
 
@@ -313,16 +310,12 @@
     /* Add null guards for attributes associated with variables which
      * are not optional and have not been used in constraints. */
     r2rState.allVars.foreach(s => exprs = nullGuard(exprs, inConstraint, r2rState.varmap, s))
-    val where = exprs.size match {
-      case 0 => None
-      case _ => Some(Expression(exprs))
-    }
 
     /* Construct the generated query as an abstract syntax. */
     Select(
       AttributeList(attrlist),
       TableList(r2rState.joins),
-      where
+      Expression(exprs)
     )
   }
 }
--- a/src/main/scala/SQL.scala	Wed Dec 16 15:15:32 2009 -0500
+++ b/src/main/scala/SQL.scala	Wed Dec 16 16:29:59 2009 -0500
@@ -3,7 +3,7 @@
 import scala.util.parsing.combinator._
 import java.net.URI
 
-case class Select(attributelist:AttributeList, tablelist:TableList, expression:Option[Expression])
+case class Select(attributelist:AttributeList, tablelist:TableList, expression:Expression)
 case class AttributeList(attributes:List[NamedAttribute])
 case class NamedAttribute(fqattribute:RelAliasAttribute, attralias:AttrAlias)
 //case class RelAttribute(relation:Relation, attribute:Attribute) c.f. ForeignKey
@@ -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:Option[Expression])
+case class Join(relasalias:RelAsRelAlias, expression:Expression)
 case class RelAsRelAlias(rel:Relation, as:RelAlias)
 case class Expression(conjuncts:List[PrimaryExpression])
 sealed abstract class PrimaryExpression
@@ -47,8 +47,15 @@
 
   def select:Parser[Select] =
     "SELECT" ~ attributelist ~ "FROM" ~ tablelist ~ opt(where) ^^
-    { case "SELECT" ~ attributes ~ "FROM" ~ tables ~ whereexpr =>
-      Select(attributes, tables, whereexpr) }
+    {
+      case "SELECT" ~ attributes ~ "FROM" ~ tables ~ whereexpr => {
+	val expression:Expression = whereexpr match {
+	  case None => Expression(List())
+	  case Some(f) => f
+	}
+	Select(attributes, tables, expression)
+      }
+    }
 
   def where:Parser[Expression] =
     "WHERE" ~ expression ^^ { case "WHERE" ~ expression => expression }
@@ -82,7 +89,15 @@
 
   def join:Parser[Join] =
     relasalias ~ opt(optexpr) ^^
-    { case relasalias ~ optexpr => Join(relasalias, 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 }
--- a/src/test/scala/SQLTest.scala	Wed Dec 16 15:15:32 2009 -0500
+++ b/src/test/scala/SQLTest.scala	Wed Dec 16 16:29:59 2009 -0500
@@ -19,16 +19,16 @@
 					     NamedAttribute(RelAliasAttribute(RelAlias(Name("R_manager")),
 									      Attribute(Name("lastName"))),
 							    AttrAlias(Name("A_managName"))))),
-			  TableList(List(Join(RelAsRelAlias(Relation(Name("Employee")),RelAlias(Name("R_emp"))),None),
+			  TableList(List(Join(RelAsRelAlias(Relation(Name("Employee")),RelAlias(Name("R_emp"))),Expression(List())),
 					 Join(RelAsRelAlias(Relation(Name("Employee")),RelAlias(Name("R_manager"))),
-					      Some(Expression(List(PrimaryExpressionEq(RelAliasAttribute(RelAlias(Name("R_manager")),
-													 Attribute(Name("id"))),
-										       RValueAttr(RelAliasAttribute(RelAlias(Name("R_emp")),
-														    Attribute(Name("manager"))))))))))),
-			  Some(Expression(List(PrimaryExpressionNotNull(RelAliasAttribute(RelAlias(Name("R_emp")),
-											  Attribute(Name("lastName")))),
-					       PrimaryExpressionNotNull(RelAliasAttribute(RelAlias(Name("R_manager")),
-											  Attribute(Name("lastName"))))))))
+					      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")))))))
     assert(expected === (a.parseAll(a.select, e).get))
   }
 
@@ -42,12 +42,12 @@
     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"))),None))),
-			  Some(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"))))))))
+			  TableList(List(Join(RelAsRelAlias(Relation(Name("Employee")),RelAlias(Name("R_emp"))),Expression(List())))),
+			  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")))))))
     assert(expected === (a.parseAll(a.select, e).get))
   }
 
@@ -63,16 +63,16 @@
     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"))),None),
+			  TableList(List(Join(RelAsRelAlias(Relation(Name("Employee")),RelAlias(Name("R_emp"))),Expression(List())),
 					 Join(RelAsRelAlias(Relation(Name("Employee")),RelAlias(Name("R_manager"))),
-					      Some(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"))))))))),
-			  Some(Expression(List(PrimaryExpressionNotNull(RelAliasAttribute(RelAlias(Name("R_emp")),Attribute(Name("lastName"))))))))
+					      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")))))))
     assert(expected === (a.parseAll(a.select, e).get))
   }
 
@@ -93,23 +93,23 @@
 							    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"))),None),
-					 Join(RelAsRelAlias(Relation(Name("Manage")),RelAlias(Name("R_lower"))),Some(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"))),Some(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"))),Some(Expression(List(PrimaryExpressionEq(RelAliasAttribute(RelAlias(Name("R_upper")),Attribute(Name("manages"))),RValueAttr(RelAliasAttribute(RelAlias(Name("R_manager")),Attribute(Name("id"))))))))),
+			  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"))),
-					      Some(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"))))))))))),
-			  Some(Expression(List(PrimaryExpressionNotNull(RelAliasAttribute(RelAlias(Name("R_emp")),
-											  Attribute(Name("lastName")))),
-					       PrimaryExpressionNotNull(RelAliasAttribute(RelAlias(Name("R_grandManager")),
-											  Attribute(Name("lastName"))))))))
+					      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")))))))
     assert(expected === (a.parseAll(a.select, e).get))
   }