[Checkins] SVN: Sandbox/adamg/ocql/branches/qo-compiler/src/ocql/ add tests and some missing functions to queryboject and algebra

Charith Paranaliyanage paranaliyanage at gmail.com
Fri Aug 1 13:54:25 EDT 2008


Log message for revision 89167:
  add tests and some missing functions to queryboject and algebra

Changed:
  U   Sandbox/adamg/ocql/branches/qo-compiler/src/ocql/compiler/compiler.py
  U   Sandbox/adamg/ocql/branches/qo-compiler/src/ocql/compiler/compiler.txt
  U   Sandbox/adamg/ocql/branches/qo-compiler/src/ocql/rewriter/algebra.py
  U   Sandbox/adamg/ocql/branches/qo-compiler/src/ocql/rewriter/interfaces.py

-=-
Modified: Sandbox/adamg/ocql/branches/qo-compiler/src/ocql/compiler/compiler.py
===================================================================
--- Sandbox/adamg/ocql/branches/qo-compiler/src/ocql/compiler/compiler.py	2008-08-01 17:21:08 UTC (rev 89166)
+++ Sandbox/adamg/ocql/branches/qo-compiler/src/ocql/compiler/compiler.py	2008-08-01 17:54:23 UTC (rev 89167)
@@ -77,6 +77,21 @@
                 IAlgebraCompiler(self.context.coll1)(),
                 IAlgebraCompiler(self.context.coll2)())
 
+class DifferCompiler(BaseCompiler):
+    implements(IAlgebraCompiler)
+    adapts(IDiffer)
+
+    def __call__(self):
+        if self.context.klass == set:
+            return 'set.differ(%s, %s)' % (
+                IAlgebraCompiler(self.context.start)(),
+                IAlgebraCompiler(self.context.end)())
+
+        elif self.context.klass == list:
+            return '(%s)-(%s)' % (
+                IAlgebraCompiler(self.context.start)(),
+                IAlgebraCompiler(self.context.end)())
+
 class IterCompiler(BaseCompiler):
     implements(IAlgebraCompiler)
     adapts(IIter)
@@ -180,7 +195,7 @@
     adapts(ILambda)
 
     def __call__(self):
-        return 'lambda %s: %s'%(
+        return 'lambda %s: %s' % (
             self.context.var,
             IAlgebraCompiler(self.context.expr)())
 
@@ -235,6 +250,7 @@
     provideAdapter(EmptyCompiler)
     provideAdapter(SingleCompiler)
     provideAdapter(UnionCompiler)
+    provideAdapter(DifferCompiler)
     provideAdapter(IterCompiler)
     provideAdapter(SelectCompiler)
     provideAdapter(ReduceCompiler)

Modified: Sandbox/adamg/ocql/branches/qo-compiler/src/ocql/compiler/compiler.txt
===================================================================
--- Sandbox/adamg/ocql/branches/qo-compiler/src/ocql/compiler/compiler.txt	2008-08-01 17:21:08 UTC (rev 89166)
+++ Sandbox/adamg/ocql/branches/qo-compiler/src/ocql/compiler/compiler.txt	2008-08-01 17:54:23 UTC (rev 89167)
@@ -23,6 +23,14 @@
     >>> print str(run)
     RunnableQuery: set([1])
 
+    >>> metadata = TestMetadata()
+    >>> qo = QueryParser("list [ | 1 ]")(metadata)
+    >>> opt = QueryOptimizer(qo)()
+    >>> alg = Rewriter(opt)()
+    >>> aopt = AlgebraOptimizer(alg)()
+    >>> run = AlgebraCompiler(aopt)(metadata, alg)
+    >>> print str(run)
+    RunnableQuery: [1]
 
     >>> metadata = TestMetadata()
     >>> qo = QueryParser("set [ | 1 ] union set [|2]")(TestMetadata())
@@ -33,16 +41,31 @@
     >>> print str(run)
     RunnableQuery: set.union(set([1]), set([2]))
 
+    >>> metadata = TestMetadata()
+    >>> qo = QueryParser("list [ | 1 ] union list [|2]")(TestMetadata())
+    >>> opt = QueryOptimizer(qo)()
+    >>> alg = Rewriter(opt)()
+    >>> aopt = AlgebraOptimizer(alg)()
+    >>> run = AlgebraCompiler(aopt)(metadata, alg)
+    >>> print str(run)
+    RunnableQuery: ([1])+([2])
 
-    ##Differ not implemented
-    ##>>> qo = QueryParser("set [ | 1 ] differ set [|2]")(TestMetadata())
-    ##>>> opt = QueryOptimizer(qo)()
-    ##>>> alg = Rewriter(opt)()
-    ##>>> print str(alg)
-    ##Union(<type 'set'>,Single(<type 'set'>,`1`),Single(<type 'set'>,`2`))
+    >>> qo = QueryParser("set [ | 1 ] differ set [|2]")(TestMetadata())
+    >>> opt = QueryOptimizer(qo)()
+    >>> alg = Rewriter(opt)()
+    >>> aopt = AlgebraOptimizer(alg)()
+    >>> run = AlgebraCompiler(aopt)(metadata, alg)
+    >>> print str(run)
+    RunnableQuery: set.differ(set([1]), set([2]))
 
+    >>> qo = QueryParser("list [ | 1 ] differ list [|2]")(TestMetadata())
+    >>> opt = QueryOptimizer(qo)()
+    >>> alg = Rewriter(opt)()
+    >>> aopt = AlgebraOptimizer(alg)()
+    >>> run = AlgebraCompiler(aopt)(metadata, alg)
+    >>> print str(run)
+    RunnableQuery: ([1])-([2])
 
-
     >>> metadata = TestMetadata()
     >>> qo = QueryParser("set [ i in ICourse | i ]")(TestMetadata())
     >>> opt = QueryOptimizer(qo)()
@@ -52,6 +75,14 @@
     >>> print str(run)
     RunnableQuery: reduce(set.union, map(lambda i: set([i]),set(metadata.getAll("ICourse"))), set())
 
+    >>> metadata = TestMetadata()
+    >>> qo = QueryParser("list [ i in ICourse | i ]")(TestMetadata())
+    >>> opt = QueryOptimizer(qo)()
+    >>> alg = Rewriter(opt)()
+    >>> aopt = AlgebraOptimizer(alg)()
+    >>> run = AlgebraCompiler(aopt)(metadata, alg)
+    >>> print str(run)
+    RunnableQuery: reduce(operator.add, map(lambda i: [i], list(metadata.getAll("ICourse"))), [])
 
     ##bag not implemented
     ##>>> qo = QueryParser("size set [ i in ICourse | i ]")(TestMetadata())
@@ -67,4 +98,13 @@
     >>> aopt = AlgebraOptimizer(alg)()
     >>> run = AlgebraCompiler(aopt)(metadata, alg)
     >>> print str(run)
-    RunnableQuery: reduce(set.union, map(lambda c: set([c.code]),set(metadata.getAll("ICourse"))), set())
\ No newline at end of file
+    RunnableQuery: reduce(set.union, map(lambda c: set([c.code]),set(metadata.getAll("ICourse"))), set())
+
+    >>> metadata = TestMetadata()
+    >>> qo = QueryParser("list [ c in ICourse | c.code ]")(TestMetadata())
+    >>> opt = QueryOptimizer(qo)()
+    >>> alg = Rewriter(opt)()
+    >>> aopt = AlgebraOptimizer(alg)()
+    >>> run = AlgebraCompiler(aopt)(metadata, alg)
+    >>> print str(run)
+    RunnableQuery: reduce(operator.add, map(lambda c: [c.code], list(metadata.getAll("ICourse"))), [])

Modified: Sandbox/adamg/ocql/branches/qo-compiler/src/ocql/rewriter/algebra.py
===================================================================
--- Sandbox/adamg/ocql/branches/qo-compiler/src/ocql/rewriter/algebra.py	2008-08-01 17:21:08 UTC (rev 89166)
+++ Sandbox/adamg/ocql/branches/qo-compiler/src/ocql/rewriter/algebra.py	2008-08-01 17:54:23 UTC (rev 89167)
@@ -31,7 +31,6 @@
     def __repr__(self):
         return ('%s') % (self.tree)
 
-
 class BaseAlgebra(Location):
     implements(IAlgebraObject)
     children = []
@@ -43,7 +42,6 @@
                 yield t
 
 class Empty(BaseAlgebra):
-
     implements(IEmpty)
 
     def __init__(self, klass, expr):
@@ -52,9 +50,7 @@
     def __repr__(self):
         return 'Empty(%s)'%(self.klass)
 
-
 class Single(BaseAlgebra):
-
     implements(ISingle)
 
     def __init__(self, klass, expr):
@@ -67,7 +63,6 @@
         return 'Single(%s,%s)'%(self.klass, self.expr)
 
 class Union(BaseAlgebra):
-
     implements(IUnion)
 
     def __init__(self, klass, coll1, coll2):
@@ -81,22 +76,18 @@
     def __repr__(self):
         return 'Union(%s,%s,%s)'%(self.klass, self.coll1, self.coll2)
 
+class Differ:
+    implements(IDiffer)
 
-#class Differ:
-#    def __init__(self, klass, start, end):
-#        self.klass = klass
-#        self.start = start
-#        self.end = end
-#
-#    def compile(self):
-#        if self.klass == set:
-#            return 'set(range(%s,%s))' % (self.start.compile(),self.end.compile())
-#        if self.klass == list:
-#            return 'range(%s,%s)' % (self.start.compile(),self.end.compile())
+    def __init__(self, klass, start, end):
+        self.klass = klass
+        self.start = start
+        self.end = end
 
+    def __repr__(self):
+        return 'Differ(%s,%s,%s)'%(self.klass, self.start, self.end)
 
 class Iter(BaseAlgebra):
-
     implements(IIter)
 
     def __init__(self, klass, func, coll):
@@ -110,9 +101,7 @@
     def __repr__(self):
         return "Iter(%s,%s,%s)"%(self.klass, self.func, self.coll)
 
-
 class Select(BaseAlgebra):
-
     implements(ISelect)
 
     def __init__(self, klass, func, coll):
@@ -126,9 +115,7 @@
     def __repr__(self):
         return "Select(%s,%s,%s)"%(self.klass, self.func, self.coll)
 
-
 class Reduce(BaseAlgebra):
-
     implements(IReduce)
 
     def __init__(self, klass, expr, func, aggreg, coll):
@@ -146,7 +133,6 @@
     def __repr__(self):
         return "Reduce(%s,%s,%s,%s,%s)"%(self.klass, self.expr, self.func, self.aggreg, self.coll)
 
-
 #class Equal:
 #    def __init__(self, klass, coll1, coll2):
 #        self.klass = klass
@@ -160,7 +146,6 @@
 #            return 'filter(%s,%s)' % (self.coll1.compile(),self.coll2.compile())
 #
 class Range(BaseAlgebra):
-
     implements(IRange)
 
     def __init__(self, klass, start, end):
@@ -171,11 +156,9 @@
         locate(end, self, 'end')
         self.children.extend([start, end])
 
-
 #class Index
 
 class Make(BaseAlgebra):
-
     implements(IMake)
 
     def __init__(self, coll1, coll2, expr):
@@ -195,7 +178,6 @@
 #class Being:
 
 class If(BaseAlgebra):
-
     implements(IIf)
 
     def __init__(self, cond, expr1, expr2):
@@ -210,12 +192,10 @@
     def __repr__(self):
         return "If(%s,%s,%s)" % (self.cond, self.expr1, self.expr2)
 
-
 #
 #
 #
 class Lambda(BaseAlgebra):
-
     implements(ILambda)
 
     def __init__(self, var, expr):
@@ -227,9 +207,7 @@
     def __repr__(self):
         return "Lambda %s: %s" %(self.var, self.expr)
 
-
 class Constant(BaseAlgebra):
-
     implements(IConstant)
 
     def __init__(self, value):
@@ -238,9 +216,7 @@
     def __repr__(self):
         return "`%s`" %(self.value)
 
-
 class Identifier(BaseAlgebra):
-
     implements(IIdentifier)
 
     def __init__(self, name):
@@ -250,7 +226,6 @@
         return "%s" % self.name
 
 class Binary(BaseAlgebra):
-
     implements(IBinary)
 
     def __init__(self, left, op, right):
@@ -264,9 +239,7 @@
     def __repr__(self):
         return "%s%s%s" % (self.left, self.op.op, self.right)
 
-
 class Operator(BaseAlgebra):
-
     implements(IOperator)
 
     def __init__(self, op):

Modified: Sandbox/adamg/ocql/branches/qo-compiler/src/ocql/rewriter/interfaces.py
===================================================================
--- Sandbox/adamg/ocql/branches/qo-compiler/src/ocql/rewriter/interfaces.py	2008-08-01 17:21:08 UTC (rev 89166)
+++ Sandbox/adamg/ocql/branches/qo-compiler/src/ocql/rewriter/interfaces.py	2008-08-01 17:54:23 UTC (rev 89167)
@@ -32,6 +32,13 @@
     coll1 = Attribute('first collection')
     coll2 = Attribute('second collection')
 
+class IDiffer(IAlgebraObject):
+    """Objects providing this interface represent the
+    Union Algebra object
+    """
+    klass = Attribute('collection type name')
+    start = Attribute('first collection')
+    end = Attribute('second collection')
 
 class IIter(IAlgebraObject):
     """Objects providing this interface represent the



More information about the Checkins mailing list