[Checkins]
SVN: Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/
merge down of changes done to qo-compiler
Adam Groszer
agroszer at gmail.com
Thu Aug 7 10:25:11 EDT 2008
Log message for revision 89497:
merge down of changes done to qo-compiler
Changed:
U Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/compiler/compiler.py
U Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/compiler/compiler.txt
U Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/interfaces.py
U Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/parser/parser.txt
D Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/parser/queryparser.py
A Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/parser/queryparser.py
U Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/qoptimizer/qoptimizer.py
A Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/queryobject/interfaces.py
U Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/queryobject/queryobject.py
A Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/queryobject/queryobject.txt
A Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/queryobject/tests.py
U Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/rewriter/algebra.py
A Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/rewriter/algebra.txt
U Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/rewriter/interfaces.py
U Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/rewriter/rewriter.py
U Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/rewriter/rewriter.txt
U Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/rewriter/tests.py
U Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/tests/run.txt
U Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/tests/test_old.py
U Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/tests/test_zope.py
-=-
Modified: Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/compiler/compiler.py
===================================================================
--- Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/compiler/compiler.py 2008-08-07 12:29:55 UTC (rev 89496)
+++ Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/compiler/compiler.py 2008-08-07 14:25:09 UTC (rev 89497)
@@ -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.coll1)(),
+ IAlgebraCompiler(self.context.coll2)())
+
+ elif self.context.klass == list:
+ return '(%s)-(%s)' % (
+ IAlgebraCompiler(self.context.coll1)(),
+ IAlgebraCompiler(self.context.coll2)())
+
class IterCompiler(BaseCompiler):
implements(IAlgebraCompiler)
adapts(IIter)
@@ -193,7 +208,7 @@
adapts(ILambda)
def __call__(self):
- return 'lambda %s: %s'%(
+ return 'lambda %s: %s' % (
self.context.var,
IAlgebraCompiler(self.context.expr)())
@@ -248,6 +263,7 @@
provideAdapter(EmptyCompiler)
provideAdapter(SingleCompiler)
provideAdapter(UnionCompiler)
+ provideAdapter(DifferCompiler)
provideAdapter(IterCompiler)
provideAdapter(SelectCompiler)
provideAdapter(ReduceCompiler)
Modified: Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/compiler/compiler.txt
===================================================================
--- Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/compiler/compiler.txt 2008-08-07 12:29:55 UTC (rev 89496)
+++ Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/compiler/compiler.txt 2008-08-07 14:25:09 UTC (rev 89497)
@@ -11,9 +11,12 @@
>>> from ocql.testing.database import TestMetadata
- >>> from ocql.compiler.compiler import registerAdapters
- >>> registerAdapters()
+ >>> import ocql.compiler.compiler
+ >>> ocql.compiler.compiler.registerAdapters()
+ >>> import ocql.rewriter.rewriter
+ >>> ocql.rewriter.rewriter.registerAdapters()
+
>>> metadata = TestMetadata()
>>> qo = QueryParser("set [ | 1 ]")(metadata)
>>> opt = QueryOptimizer(qo)()
@@ -23,6 +26,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 +44,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 +78,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 +101,13 @@
>>> aopt = AlgebraOptimizer(alg)(metadata)
>>> 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/optimize-with-index/src/ocql/interfaces.py
===================================================================
--- Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/interfaces.py 2008-08-07 12:29:55 UTC (rev 89496)
+++ Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/interfaces.py 2008-08-07 14:25:09 UTC (rev 89497)
@@ -59,10 +59,20 @@
# Objects passed around
################
+class IObjectQueryHead(Interface):
+ """Represents head of the query object tree
+ """
+ tree = Attribute('Holds the root of the query object tree')
+
+# def rewrite(self):
+# """Rewrites query object in to algebra object"""
+
class IObjectQuery(Interface):
"""Objects providing this interface represent the OCQL query
as python objects
"""
+ metadata = Attribute('metadata')
+ symbols = Attribute('symbols')
class IOptimizedObjectQuery(Interface):
"""Objects providing this interface represent the OCQL query
@@ -74,14 +84,16 @@
"""
tree = Attribute('holds the root of the algebra object tree')
- def walk(self):
+ def walk():
"""Iterate the Algebra object tree"""
class IAlgebraObject(Interface):
"""Objects providing this interface represent the
rewritten ObjectQuery to Algebra objects
"""
- def walk(self):
+ children = Attribute('Children collection')
+
+ def walk():
"""Iterate the Algebra object tree"""
class IOptimizedAlgebraObject(Interface):
Modified: Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/parser/parser.txt
===================================================================
--- Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/parser/parser.txt 2008-08-07 12:29:55 UTC (rev 89496)
+++ Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/parser/parser.txt 2008-08-07 14:25:09 UTC (rev 89497)
@@ -5,8 +5,8 @@
#FAILS, why?
- >>> QueryParser("set [ ]")(None)
- Query(<type 'set'>, , None)
+ #>>> QueryParser("set [ ]")(None)
+ #Query(<type 'set'>, , None)
>>> QueryParser("set [ | 1 ]")(None)
Query(<type 'set'>, , Constant(1))
@@ -24,7 +24,7 @@
>>> QueryParser("set [ i in ICourse | i ]")(None)
Query(<type 'set'>, In(Identifier(i), Identifier(ICourse)), Identifier(i))
- >>> QueryParser("size set [ i in ICourse | i ]")(None)
+ >>> QueryParser("len ( set [ i in ICourse | i ] )")(None)
Count(Query(<type 'set'>, In(Identifier(i), Identifier(ICourse)), Identifier(i)))
#FAILS, see raise "Help"
Deleted: Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/parser/queryparser.py
===================================================================
--- Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/parser/queryparser.py 2008-08-07 12:29:55 UTC (rev 89496)
+++ Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/parser/queryparser.py 2008-08-07 14:25:09 UTC (rev 89497)
@@ -1,554 +0,0 @@
-# -*- coding: UTF-8 -*-
-
-"""Parse a string to Query Object
-
-$Id$
-"""
-
-#TODOs:
-#add metadata into the picture!!!
-#remove shift/reduce conflicts, when possible
-#look after raise "Help"
-#revise according to new grammar
-
-from ply import lex, yacc
-from collections import deque
-from threading import local
-
-from zope.component import adapts
-from zope.component import provideAdapter
-from zope.interface import implements
-
-from ocql.queryobject.queryobject import *
-from ocql.interfaces import IQueryParser
-
-DEBUG = 0
-
-class SymbolContainer:
- def __init__(self):
- self.stack = deque()
- self.stack.append(dict())
-
- def addlevel(self):
- top = self.current()
- new = dict(top)
- self.stack.append(new)
-
- def dellevel(self):
- self.stack.pop()
-
- def current(self):
- return self.stack[-1]
-
-#further expand COND_OP, MODIFIER, QUANTOR
-tokens = ('TYPE', 'UNION', 'DIFFER', 'AND', 'OR', 'NOT', 'ELLIPSIS', 'IN',
- 'AS', 'SIZE', 'EQ','NE','LT','GT','LE','GE', 'ATLEAST','ATMOST','JUST','EVERY','SOME', 'CONSTANT',
- 'IDENTIFIER', 'PLUS', 'MINUS', 'MUL', 'DIV', 'BRACEL', 'BRACER',
- 'SQUAREL', 'SQUARER', 'CURLYL', 'CURLYR',
- 'PIPE', 'DOT', 'SEMICOL', 'COMMA'
- )
-
-precedence = (
- ('left', 'UNION'),
- ('left', 'DIFFER'),
-# ('token', 'SQUAREL'),
-# ('token', 'PIPE'),
-# ('token', 'SQUARER'),
- ('left', 'AND'),
- ('left', 'OR'),
- ('right', 'NOT'),
- ('left', 'EQ'),
- ('left', 'NE'),
- ('left', 'LT'),
- ('left', 'GT'),
- ('left', 'LE'),
- ('left', 'GE'),
- ('left', 'PLUS', 'MINUS'),
- ('left', 'MUL', 'DIV'),
-# ('token', 'IDENTIFIER'),
-# ('token', 'BRACEL'),
-# ('token', 'BRACER'),
-# ('token', 'CONSTANT'),
-# ('token', 'TYPE'),
-# ('token', 'CURLYL'),
-# ('token', 'CURLYR'),
-# ('token', 'ELLIPSIS'),
- ('left', 'DOT'),
-# ('token', 'COMMA'),
- ('left', 'SEMICOL'),
-
- ('left', 'IN'),
- ('left', 'AS'),
-
-# ('token', 'MODIFIER'),
-# ('token', 'QUANTOR'),
-
- ('left', 'SIZE'),
-)
-
-class Lexer(object):
- tokens = tokens
- t_ignore = ' \t\n\r'
-
- def t_error(self, t):
- print "Illegal character '%s'" % t.value[0]
- t.lexer.skip(1)
-
- # Tokens
- def t_TYPE(self, t):
- r'(set|bag|map|list)'
- return t
-
- def t_UNION(self, t):
- r'union'
- return t
-
- def t_DIFFER(self, t):
- r'differ'
- return t
-
- def t_AND(self, t):
- r'and'
- return t
-
- def t_OR(self, t):
- r'or'
- return t
-
- def t_NOT(self, t):
- r'not'
- return t
-
- def t_ELLIPSIS(self, t):
- r'\.\.\.'
- return t
-
- def t_IN(self, t):
- r'in'
- return t
-
- def t_AS(self, t):
- r'as'
- return t
-
- def t_SIZE(self, t):
- r'size'
- return t
-
- #def t_COND_OP(self, t):
- # r'(<|>|<=|>=|==|!=)'
- # return t
-
- def t_LT(self, t):
- r'<'
- return t
-
- def t_GT(self, t):
- r'>'
- return t
-
- def t_LE(self, t):
- r'<='
- return t
-
- def t_GE(self, t):
- r'>='
- return t
-
- def t_EQ(self, t):
- r'=='
- return t
-
- def t_NE(self, t):
- r'!='
- return t
-
- #def t_MODIFIER(self, t):
- # r'(atleast|atmost|just)'
- # return t
-
- def t_ATLEAST(self, t):
- r'atleast'
- return t
-
- def t_ATMOST(self, t):
- r'atmost'
- return t
-
- def t_JUST(self, t):
- r'just'
- return t
-
- #def t_QUANTOR(self, t):
- # r'(every|some)'
- # return t
-
- def t_EVERY(self, t):
- r'every'
- return t
-
- def t_SOME(self, t):
- r'some'
- return t
-
- def t_CONSTANT(self, t):
- r'(\'(\\.|[^\'])*\'|"(\\.|[^"])*"|[0-9]+)'
- return t
-
- def t_IDENTIFIER(self, t):
- r'[a-zA-Z][0-9a-zA-Z_]*'
- if DEBUG: print "IDEN", t
- return t
-
- def t_PLUS(self, t):
- r'\+'
- return t
-
- def t_MINUS(self, t):
- r'-'
- return t
-
- def t_MUL(self, t):
- r'\*'
- return t
-
- def t_DIV(self, t):
- r'/'
- return t
-
- def t_BRACEL(self, t):
- r'\('
- return t
-
- def t_BRACER(self, t):
- r'\)'
- return t
-
- def t_SQUAREL(self, t):
- r'\['
- return t
-
- def t_SQUARER(self, t):
- r'\]'
- return t
-
- def t_CURLYL(self, t):
- r'{'
- return t
-
- def t_CURLYR(self, t):
- r'}'
- return t
-
- def t_PIPE(self, t):
- r'\|'
- return t
-
- def t_DOT(self, t):
- r'\.'
- return t
-
- def t_SEMICOL(self, t):
- r';'
- return t
-
- def t_COMMA(self, t):
- r','
- return t
-
-class Parser(object):
- tokens = tokens
- precedence = precedence
- metadata = None
- symbols = None
- types = { 'set' : set, 'list': list }
- start = 'expr'
-
- def __init__(self, metadata):
- self.metadata = metadata
- self.symbols = SymbolContainer()
-
- def p_error(self, t):
- print "Syntax error at '%s' (%s)" % (t.value, t.lexpos)
-
- #expr COND_OP modifier nexpr seems to be wrong logically
- #def p_expr_cond(self, t):
- # r'''expr : modifier nexpr COND_OP modifier nexpr
- # | modifier expr COND_OP modifier nexpr'''
- # raise "Help"
-
- def p_expr_atleast(self, t):
- r'''expr : ATLEAST expr
- '''
- t[0] = Atleast(self.metadata, self.symbols, t[2])
-
- def p_expr_atmost(self, t):
- r'''expr : ATMOST expr
- '''
- t[0] = Atmost(self.metadata, self.symbols, t[2])
-
- def p_expr_just(self, t):
- r'''expr : JUST expr
- '''
- t[0] = Just(self.metadata, self.symbols, t[2])
-
- def p_expr_every(self, t):
- r'''expr : EVERY expr
- '''
- t[0] = Every(self.metadata, self.symbols, t[2])
-
- def p_expr_some(self, t):
- r'''quented : expr
- | SOME expr
- '''
- if len(t)==2:
- t[0] = t[1]
- else:
- t[0] = Some(self.metadata, self.symbols, t[2])
-
- def p_expr_eq(self, t):
- r'''expr : quented EQ expr
- '''
- t[0] = Eq(self.metadata, self.symbols, t[1], t[3])
-
- def p_expr_ne(self, t):
- r'''expr : expr NE expr
- '''
- t[0] = Ne(self.metadata, self.symbols, t[1], t[3])
-
- def p_expr_lt(self, t):
- r'''expr : expr LT expr
- '''
- t[0] = Lt(self.metadata, self.symbols, t[1], t[3])
-
- def p_expr_gt(self, t):
- r'''expr : expr GT expr
- '''
- t[0] = Gt(self.metadata, self.symbols, t[1], t[3])
-
- def p_expr_le(self, t):
- r'''expr : expr LE expr
- '''
- t[0] = Le(self.metadata, self.symbols, t[1], t[3])
-
- def p_expr_ge(self, t):
- r'''expr : expr GE expr
- '''
- t[0] = Ge(self.metadata, self.symbols, t[1], t[3])
-
- def p_expr_union(self, t):
- r'''expr : expr UNION expr
- '''
- t[0] = Union(self.metadata, self.symbols, t[1], t[3])
- if DEBUG: print t[0]
-
- def p_expr_differ(self, t):
- r'''expr : expr DIFFER expr
- '''
- t[0] = Differ(self.metadata, self.symbols, t[1], t[3])
- if DEBUG: print t[0]
-
- def p_expr_equery(self, t):
- r'''expr : TYPE SQUAREL xprs SQUARER
- '''
- t[0] = Query(self.metadata, self.symbols, self.types[t[1]],t[3],None)
-
- def p_expr_query(self, t):
- r'''expr : TYPE SQUAREL xprs PIPE expr SQUARER
- '''
- t[0] = Query(self.metadata, self.symbols, self.types[t[1]], t[3], t[5])
- if DEBUG: print "p_expr_query", t[0]
-
- def p_expr_and(self, t):
- r'''expr : expr AND expr
- '''
- t[0] = And(self.metadata, self.symbols, t[1], t[3])
- if DEBUG: print t[0]
-
- def p_expr_or(self, t):
- r'''expr : expr OR expr
- '''
- t[0] = Or(self.metadata, self.symbols, t[1], t[3])
- if DEBUG: print t[0]
-
- def p_expr_plus(self, t):
- r'''expr : expr PLUS expr
- '''
- t[0] = Add(self.metadata, self.symbols, t[1], t[3])
- if DEBUG: print t[0]
-
- def p_expr_minus(self, t):
- r'''expr : expr MINUS expr
- '''
- t[0] = Sub(self.metadata, self.symbols, t[1], t[3])
- if DEBUG: print t[0]
-
- def p_expr_mul(self, t):
- r'''expr : expr MUL expr
- '''
- t[0] = Mul(self.metadata, self.symbols, t[1], t[3])
- if DEBUG: print t[0]
-
- def p_expr_div(self, t):
- r'''expr : expr DIV expr
- '''
- t[0] = Div(self.metadata, self.symbols, t[1], t[3])
- if DEBUG: print t[0]
-
- def p_expr_not(self, t):
- r'''expr : NOT expr
- '''
- t[0] = Not(self.metadata, self.symbols, t[2])
- if DEBUG: print t[0]
-
- def p_expr_dot(self, t):
- r'''expr : expr DOT expr
- '''
- t[0] = Property(self.metadata, self.symbols, t[1], t[3])
- if DEBUG: print t[0]
-
- def p_expr_id(self, t):
- r'''expr : IDENTIFIER
- '''
- t[0] = Identifier(self.metadata, self.symbols, t[1])
- if DEBUG: print "p_expr_id", t[0]
-
-
- def p_expr_call(self, t):
- r'''expr : IDENTIFIER BRACEL exprs BRACER
- '''
- raise NotImplementedError("Function call")
-
- def p_expr_const(self, t):
- r'''expr : CONSTANT
- '''
- t[0] = Constant(self.metadata, self.symbols, t[1])
- if DEBUG: print t[0]
-
- def p_expr_array(self, t):
- r'''expr : TYPE '{' exprs '}'
- '''
- raise NotImplementedError("array")
-
- def p_expr_range(self, t):
- r'''expr : TYPE CURLYL expr ELLIPSIS expr CURLYR
- '''
- raise NotImplementedError("range")
-
-
- def p_expr_index(self, t):
- r'''expr : expr DOT SQUAREL expr SQUARER
- '''
- t[0] = Index(self.metadata, self.symbols, t[1], t[4])
- if DEBUG: print t[0]
-
- def p_expr_size(self, t):
- r'''expr : SIZE expr
- '''
- t[0] = Count(self.metadata, self.symbols, t[2])
- if DEBUG: print t[0]
-
- def p_expr_bracket(self, t):
- r'''expr : BRACEL expr BRACER
- '''
- t[0] = t[2]
- if DEBUG: print t[0]
-
- #def p_omodifier(self, t):
- # r'''omodifier : QUANTOR
- # | MODIFIER
- # '''
- # if DEBUG: print t[0]
-
- #def p_modifier(self, t):
- # r'''modifier : QUANTOR
- # | MODIFIER
- # '''
- # if DEBUG: print t[0]
-
- def p_exprs(self, t):
- r'''exprs : expr
- | expr COMMA exprs
- '''
- if DEBUG: print t[0]
-
- def p_in_expr(self, t):
- r'''in_expr : IDENTIFIER IN expr'''
- t[0] = In(self.metadata,
- self.symbols,
- Identifier(self.metadata,
- self.symbols,
- t[1]),
- t[3])
- if DEBUG: print "p_in_expr", t[0]
- if DEBUG: print t[1], t[3]
-
- def p_as_expr(self, t):
- r'''as_expr : IDENTIFIER AS expr'''
- if DEBUG: print "p_as_expr", t[0]
-
- def p_xprs(self, t):
- r'''xprs :
- | xpr SEMICOL xprs
- | xpr
- '''
- if len(t)==1:
- t[0] = []
- elif len(t)==2:
- t[0] = [ t[1] ]
- else:
- t[3].insert(0,t[1])
- t[0] = t[3]
- if DEBUG: print "p_xprs", t[0]
-
- def p_xpr(self, t):
- r'''xpr : as_expr
- | in_expr
- | expr
- '''
- t[0] = t[1]
- if DEBUG: print "p_xpr", t[0]
-
-#these are here, to keep lexer and parser instantiation to a minimum possible
-#level because they are quite expensive operations
-#parsers must be thread safe on the other hand!
-LEXER = lex.lex(object=Lexer(), debug=0)
-#PARSERS = local()
-
-def parse(str, metadata):
- lexer = LEXER.clone()
-
- #global PARSERS
- #try:
- # parser = PARSERS.parser
- #
- # try:
- # parser.restart()
- # except AttributeError:
- # pass
- #
- #except AttributeError:
- # parser = yacc.yacc(module = Parser(metadata))
- # PARSERS.parser = parser
-
- try:
- parser = yacc.yacc(module = Parser(metadata))
-
- retval = parser.parse(str, lexer = lexer)
- except Exception, e:
- if DEBUG: print e
- raise
- return retval
-
-class QueryParser(object):
- implements(IQueryParser)
- adapts(basestring)
-
- def __init__(self, context):
- self.context = context
- #self.db = db
-
- def __call__(self, metadata):
- strg = self.context
- return parse(strg, metadata)
- #return parse(strg, None)
Copied: Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/parser/queryparser.py (from rev 89449, Sandbox/adamg/ocql/trunk/src/ocql/parser/queryparser.py)
===================================================================
--- Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/parser/queryparser.py (rev 0)
+++ Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/parser/queryparser.py 2008-08-07 14:25:09 UTC (rev 89497)
@@ -0,0 +1,651 @@
+# -*- coding: UTF-8 -*-
+
+"""Parse a string to Query Object
+
+"""
+
+#TODOs:
+#add metadata into the picture!!!
+#remove shift/reduce conflicts, when possible
+#look after raise "Help"
+#revise according to new grammar
+
+from ply import lex, yacc
+from collections import deque
+from threading import local
+
+from zope.component import adapts
+from zope.component import provideAdapter
+from zope.interface import implements
+
+from ocql.queryobject.queryobject import *
+from ocql.interfaces import IQueryParser
+
+DEBUG = 0
+
+class SymbolContainer:
+ def __init__(self):
+ self.stack = deque()
+ self.stack.append(dict())
+
+ def addlevel(self):
+ top = self.current()
+ new = dict(top)
+ self.stack.append(new)
+
+ def dellevel(self):
+ self.stack.pop()
+
+ def current(self):
+ return self.stack[-1]
+
+tokens = ('SET', 'LIST', 'COMMA', 'NOT_EQUAL', 'UNION', 'AS', 'EVERY', 'ATMOST', 'LT', 'GT', 'ELLIPSIS', 'BRACKET_R', 'OR', 'PIPE', 'DOT', 'IN', 'LTE', 'SOME', 'AND', 'CBRACKET_L', 'CONSTANT', 'EQUAL', 'GTE', 'ISINSTANCE', 'SEMI_COLON', 'BRACKET_L', 'ASSIGN', 'NOT_ASSIGN', 'FOR', 'CBRACKET_R', 'JUST', 'IDENTIFIER', 'DIFFER', 'LEN', 'BAG', 'SBRACKET_L', 'NOT', 'ATLEAST', 'SBRACKET_R')
+
+precedence = (
+ ('left', 'UNION'),
+ ('left', 'DIFFER'),
+# ('token', 'SQUAREL'),
+# ('token', 'PIPE'),
+# ('token', 'SQUARER'),
+ ('left', 'AND'),
+ ('left', 'OR'),
+ ('right', 'NOT'),
+ #('left', 'COND_OP'),
+# ('left', 'PLUS', 'MINUS'),
+# ('left', 'MUL', 'DIV'),
+# ('token', 'IDENTIFIER'),
+# ('token', 'BRACEL'),
+# ('token', 'BRACER'),
+# ('token', 'CONSTANT'),
+# ('token', 'TYPE'),
+# ('token', 'CURLYL'),
+# ('token', 'CURLYR'),
+# ('token', 'ELLIPSIS'),
+ ('left', 'DOT'),
+# ('token', 'COMMA'),
+ ('left', 'SEMI_COLON'),
+
+ ('left', 'IN'),
+ ('left', 'AS'),
+
+# ('token', 'MODIFIER'),
+# ('token', 'QUANTOR'),
+
+# ('left', 'SIZE'),
+)
+
+class Lexer(object):
+ tokens = tokens
+ t_ignore = ' \t\n\r'
+
+ def t_error(self, t):
+ print "Illegal character '%s'" % t.value[0]
+ t.lexer.skip(1)
+
+ def t_UNION(self, t):
+ r'union'
+ return t
+
+ def t_DIFFER(self, t):
+ r'differ'
+ return t
+
+ def t_SET(self, t):
+ r'set'
+ return t
+
+ def t_LIST(self, t):
+ r'list'
+ return t
+
+ def t_BAG(self, t):
+ r'bag'
+ return t
+
+ def t_FOR(self, t):
+ r'for'
+ return t
+
+ def t_LEN(self, t):
+ r'len'
+ return t
+
+ def t_AS(self, t):
+ r'as'
+ return t
+
+ def t_IN(self, t):
+ r'in'
+ return t
+
+ def t_OR(self, t):
+ r'or'
+ return t
+
+ def t_AND(self, t):
+ r'and'
+ return t
+
+ def t_NOT(self, t):
+ r'not'
+ return t
+
+ def t_ISINSTANCE(self, t):
+ r'isinstance'
+ return t
+
+ def t_EVERY(self, t):
+ r'every'
+ return t
+
+ def t_ATMOST(self, t):
+ r'atmost'
+ return t
+
+ def t_ATLEAST(self, t):
+ r'atleast'
+ return t
+
+ def t_SOME(self, t):
+ r'some'
+ return t
+
+ def t_JUST(self, t):
+ r'just'
+ return t
+
+ def t_CONSTANT(self, t):
+ r'(\'(\\.|[^\'])*\'|"(\\.|[^"])*"|[0-9]+)'
+ return t
+
+ def t_IDENTIFIER(self, t):
+ r'[a-zA-Z][0-9a-zA-Z_]*'
+ return t
+
+ def t_COMMA(self, t):
+ r','
+ return t
+
+#this may be != sign
+ def t_NOT_EQUAL(self, t):
+ r'~=='
+ return t
+
+ def t_LT(self, t):
+ r'<'
+ return t
+
+ def t_GT(self, t):
+ r'>'
+ return t
+
+ def t_ELLIPSIS(self, t):
+ r'\.\.\.'
+ return t
+
+ def t_PIPE(self, t):
+ r'\|'
+ return t
+
+ def t_DOT(self, t):
+ r'\.'
+ return t
+
+ def t_MUL(self, t):
+ r'\*'
+ return t
+
+ def t_CBRACKET_L(self, t):
+ r'{'
+ return t
+
+ def t_CBRACKET_R(self, t):
+ r'}'
+ return t
+
+ def t_EQUAL(self, t):
+ r'=='
+ return t
+
+ def t_GTE(self, t):
+ r'>='
+ return t
+
+ def t_LTE(self, t):
+ r'<='
+ return t
+
+ def t_SEMI_COLON(self, t):
+ r';'
+ return t
+
+ def t_BRACKET_L(self, t):
+ r'\('
+ return t
+
+ def t_BRACKET_R(self, t):
+ r'\)'
+ return t
+
+ def t_ASSIGN(self, t):
+ r'='
+ return t
+
+ def t_NOT_ASSIGN(self, t):
+ r'~='
+ return t
+
+# def t_DIV(self, t):
+# r'/'
+# return t
+
+# def t_PLUS(self, t):
+# r'\+'
+# return t
+
+# def t_MINUS(self, t):
+# r'-'
+# return t
+
+ def t_SBRACKET_L(self, t):
+ r'\['
+ return t
+
+ def t_SBRACKET_R(self, t):
+ r'\]'
+ return t
+
+
+
+class Parser(object):
+ tokens = tokens
+ precedence = precedence
+ metadata = None
+ symbols = None
+ types = { 'set' : set, 'list': list }
+ start = 'expression'
+
+ def __init__(self, metadata):
+ self.metadata = metadata
+ self.symbols = SymbolContainer()
+
+ def p_error(self, t):
+ print "Syntax error at '%s' (%s)" % (t.value, t.lexpos)
+
+ def p_expr_union(self, t):
+ r'''expression : expression UNION expression
+ '''
+ t[0] = Union(self.metadata, self.symbols, t[1], t[3])
+ if DEBUG: print 'reducing "expression UNION expression" to "expression"', t[0]
+
+ def p_expr_differ(self, t):
+ r'''expression : expression DIFFER expression
+ '''
+ t[0] = Differ(self.metadata, self.symbols, t[1], t[3])
+ if DEBUG: print 'reducing "expression DIFFER expression" to "expression"', t[0]
+
+# def p_expr_3(self, t):
+# r'''expression : collection SBRACKET_L expression SBRACKET_R
+# '''
+# t[0] = Query(self.metadata, self.symbols, t[1], [], t[3])
+# if DEBUG: print 'reducing "collection SBRACKET_L qualifier PIPE expression SBRACKET_R" to "expression"'
+
+ def p_expr_query(self, t):
+ r'''expression : collection SBRACKET_L qualifier PIPE expression SBRACKET_R
+ '''
+ t[0] = Query(self.metadata, self.symbols, t[1], t[3], t[5])
+ if DEBUG: print 'reducing "collection SBRACKET_L qualifier PIPE expression SBRACKET_R" to "expression"', t[0]
+
+#TODO add a test
+ def p_expr_for_query(self, t):
+ r'''expression : collection SBRACKET_L qualifier FOR expression SBRACKET_R
+ '''
+ t[0] = Query(self.metadata, self.symbols, t[1], t[3], t[5])
+ if DEBUG: print 'reducing "collection SBRACKET_L qualifier FOR expression SBRACKET_R" to "expression"', t[0]
+
+ def p_expr_literal(self, t):
+ r'''expression : literal
+ '''
+ t[0] = t[1]
+ if DEBUG: print 'reducing "literal" to "expression"', t[0]
+
+ def p_expr_path(self, t):
+ r'''expression : path
+ '''
+ t[0] = t[1]
+ if DEBUG: print 'reducing "path" to "expression"', t[0]
+
+ def p_expr_call(self, t):
+ r'''expression : call
+ '''
+ t[0] = t[1]
+ if DEBUG: print 'reducing "path" to "expression"', t[0]
+
+ def p_expr_len(self, t):
+ r'''expression : LEN BRACKET_L expression BRACKET_R
+ '''
+ t[0] = Count(self.metadata, self.symbols, t[3])
+ if DEBUG: print 'reducing "LEN BRACKET_L expression BRACKET_R" to "expression"', t[0]
+
+ def p_collection_set(self, t):
+ r'''collection : SET
+ '''
+ t[0] = self.types['set']
+ if DEBUG: print 'reducing "set" to "collection"', t[0]
+
+ def p_collection_list(self, t):
+ r'''collection : LIST
+ '''
+ t[0] = self.types['list']
+ if DEBUG: print 'reducing "list" to "collection"', t[0]
+
+ def p_collection_bag(self, t):
+ r'''collection : BAG
+ '''
+ raise NotImplementedError('bag')
+ if DEBUG: print 'reducing "bag" to "collection"', t[0]
+
+ def p_qualifier_null(self, t):
+ r'''qualifier :
+ '''
+ t[0] = []
+ if DEBUG: print 'reducing "" to "qualifier"', t[0]
+
+ def p_qualifier_generator(self, t):
+ r'''qualifier : generator
+ '''
+ t[0] = [t[1]]
+ if DEBUG: print 'reducing "generator" to "qualifier"', t[0]
+
+ def p_qualifier_definition(self, t):
+ r'''qualifier : definition
+ '''
+ t[0] = [t[1]]
+ if DEBUG: print 'reducing "definition" to "qualifier"', t[0]
+
+ def p_qualifier_filter(self, t):
+ r'''qualifier : filter
+ '''
+ t[0] = [t[1]]
+ if DEBUG: print 'reducing "filter" to "qualifier"', t[0]
+
+ def p_qualifier_qualifier(self, t):
+ r'''qualifier : qualifier SEMI_COLON qualifier
+ '''
+ t[0] = t[0].extend(t[1])
+ t[0] = t[0].extend(t[3])
+ if DEBUG: print 'reducing "qualifier SEMI_COLON qualifier" to "qualifier"', t[0]
+
+# def p_qualifier_6(self, t):
+# r'''qualifier : expression
+# '''
+# t[0] = t[1]
+# if DEBUG: print 'reducing "expression" to "qualifier"'
+
+ def p_generator_in(self, t):
+ r'''generator : IDENTIFIER IN expression
+ '''
+ t[0] = In(self.metadata,
+ self.symbols,
+ Identifier(self.metadata,
+ self.symbols,
+ t[1]),
+ t[3])
+ if DEBUG: print 'reducing "IDENTIFIER IN expression" to "generator"', t[0]
+
+ def p_filter_and(self, t):
+ r'''filter : filter AND filter
+ '''
+ t[0] = And(self.metadata, self.symbols, t[1], t[3])
+ if DEBUG: print 'reducing "filter AND filter" to "filter"', t[0]
+
+ def p_filter_or(self, t):
+ r'''filter : filter OR filter
+ '''
+ t[0] = Or(self.metadata, self.symbols, t[1], t[3])
+ if DEBUG: print 'reducing "filter OR filter" to "filter"', t[0]
+
+ def p_filter_not(self, t):
+ r'''filter : NOT condition
+ '''
+ t[0] = Not(self.metadata, self.symbols, t[1], t[3])
+ if DEBUG: print 'reducing "NOT condition" to "filter"', t[0]
+
+ def p_filter_condition(self, t):
+ r'''filter : condition
+ '''
+ t[0] = t[1]
+ if DEBUG: print 'reducing "condition" to "filter"', t[0]
+
+ def p_condition_filter(self, t):
+ r'''condition : BRACKET_L filter BRACKET_R
+ '''
+ t[0] = t[2]
+ if DEBUG: print 'reducing "BRACKET_L filter BRACKET_R" to "condition"', t[0]
+
+ def p_condition_assign(self, t):
+ r'''condition : quantified ASSIGN quantified
+ '''
+ raise NotImplementedError('assign')
+ if DEBUG: print 'reducing "quantified operator quantified" to "condition"', t[0]
+
+ def p_condition_not_assign(self, t):
+ r'''condition : quantified NOT_ASSIGN quantified
+ '''
+ raise NotImplementedError('not assign')
+ if DEBUG: print 'reducing "quantified operator quantified" to "condition"', t[0]
+
+ def p_condition_lt(self, t):
+ r'''condition : quantified LT quantified
+ '''
+ t[0] = Lt(self.metadata, self.symbols, t[1], t[3])
+ if DEBUG: print 'reducing "quantified operator quantified" to "condition"', t[0]
+
+ def p_condition_lte(self, t):
+ r'''condition : quantified LTE quantified
+ '''
+ t[0] = Le(self.metadata, self.symbols, t[1], t[3])
+ if DEBUG: print 'reducing "quantified operator quantified" to "condition"', t[0]
+
+ def p_condition_gt(self, t):
+ r'''condition : quantified GT quantified
+ '''
+ t[0] = Gt(self.metadata, self.symbols, t[1], t[3])
+ if DEBUG: print 'reducing "quantified operator quantified" to "condition"', t[0]
+
+ def p_condition_gte(self, t):
+ r'''condition : quantified GTE quantified
+ '''
+ t[0] = Ge(self.metadata, self.symbols, t[1], t[3])
+ if DEBUG: print 'reducing "quantified operator quantified" to "condition"', t[0]
+
+ def p_condition_equal(self, t):
+ r'''condition : quantified EQUAL quantified
+ '''
+ t[0] = Eq(self.metadata, self.symbols, t[1], t[3])
+ if DEBUG: print 'reducing "quantified operator quantified" to "condition"', t[0]
+
+ def p_condition_not_equal(self, t):
+ r'''condition : quantified NOT_EQUAL quantified
+ '''
+ t[0] = Ne(self.metadata, self.symbols, t[1], t[3])
+ if DEBUG: print 'reducing "quantified operator quantified" to "condition"', t[0]
+
+ #need to extend this for collection of types
+ def p_condition_isinstance(self, t):
+ r'''condition : ISINSTANCE BRACKET_L expression COMMA IDENTIFIER BRACKET_R
+ '''
+ raise NotImplementedError('isinstance')
+ if DEBUG: print 'reducing "ISINSTANCE BRACKET_L expression COMMA IDENTIFIER BRACKET_R" to "condition"', t[0]
+
+ def p_quantified_expression(self, t):
+ r'''quantified : expression
+ '''
+ t[0] = t[1]
+ if DEBUG: print 'reducing "expression" to "quantified"', t[0]
+
+ def p_quantified_some(self, t):
+ r'''quantified : SOME expression
+ '''
+ t[0] = Some(self.metadata, self.symbols, t[2])
+ if DEBUG: print 'reducing "quantification expression" to "quantified"', t[0]
+
+ def p_quantified_just(self, t):
+ r'''quantified : JUST expression
+ '''
+ t[0] = Just(self.metadata, self.symbols, t[2])
+ if DEBUG: print 'reducing "quantification expression" to "quantified"', t[0]
+
+ def p_quantified_every(self, t):
+ r'''quantified : EVERY expression
+ '''
+ t[0] = Every(self.metadata, self.symbols, t[2])
+ if DEBUG: print 'reducing "quantification expression" to "quantified"', t[0]
+
+ def p_quantified_atleast(self, t):
+ r'''quantified : ATLEAST expression
+ '''
+ t[0] = Atleast(self.metadata, self.symbols, t[2])
+ if DEBUG: print 'reducing "quantification expression" to "quantified"', t[0]
+
+ def p_quantified_almost(self, t):
+ r'''quantified : ATMOST expression
+ '''
+ t[0] = Atmost(self.metadata, self.symbols, t[2])
+ if DEBUG: print 'reducing "quantification expression" to "quantified"', t[0]
+
+ def p_definition_as(self, t):
+ r'''definition : IDENTIFIER AS expression
+ '''
+ #t[0]=''
+ if DEBUG: print 'reducing "IDENTIFIER AS expression" to "definition"', t[0]
+
+ def p_literal_constant(self, t):
+ r'''literal : CONSTANT
+ '''
+ t[0] = Constant(self.metadata, self.symbols, t[1])
+ if DEBUG: print 'reducing "CONSTANT" to "literal"', t[0]
+
+ def p_literal_element(self, t):
+ r'''literal : collection CBRACKET_L element CBRACKET_R
+ '''
+ raise NotImplementedError('collection set')
+ if DEBUG: print 'reducing "collection CBRACKET_L element CBRACKET_R" to "literal"', t[0]
+
+ def p_element_null(self, t):
+ r'''element :
+ '''
+ t[0] = None
+ if DEBUG: print 'reducing "" to "element"', t[0]
+
+ def p_element_expression(self, t):
+ r'''element : expression
+ '''
+ t[0] = t[1]
+ if DEBUG: print 'reducing "expression" to "element"', t[0]
+
+# Why this raise a shift/reduce conflict
+# def p_element_comma(self, t):
+# r'''element : element COMMA element
+# '''
+# raise NotImplementedError('element list')
+# if DEBUG: print 'reducing "element COMMA element" to "element"', t[0]
+
+ def p_element_ellipsis(self, t):
+ r'''element : expression ELLIPSIS expression
+ '''
+ raise NotImplementedError('range')
+ if DEBUG: print 'reducing "expression ELLIPSIS expression" to "element"', t[0]
+
+ def p_path_identifier(self, t):
+ r'''path : IDENTIFIER
+ '''
+ t[0] = Identifier(self.metadata, self.symbols, t[1])
+ if DEBUG: print 'reducing "IDENTIFIER" to "path"', t[0]
+
+ def p_path_method(self, t):
+ r'''path : IDENTIFIER DOT method
+ '''
+ t[0] = Property(self.metadata, self.symbols, Identifier(self.metadata, self.symbols, t[1]), t[3])
+ if DEBUG: print 'reducing "IDENTIFIER DOT method" to "path"', t[0]
+
+ def p_method_identifier(self, t):
+ r'''method : IDENTIFIER
+ '''
+ t[0] = Identifier(self.metadata, self.symbols, t[1])
+ if DEBUG: print 'reducing "IDENTIFIER" to "method"', t[0]
+
+ def p_method_arguments(self, t):
+ r'''method : IDENTIFIER BRACKET_L argument_list BRACKET_R
+ '''
+ raise NotImplementedError('function call')
+ if DEBUG: print 'reducing "IDENTIFIER BRACKET_L argument_list BRACKET_R" to "method"', t[0]
+
+ def p_argument_list_null(self, t):
+ r'''argument_list :
+ '''
+ t[0] = None
+ if DEBUG: print 'reducing "" to "argument_list"', t[0]
+
+ def p_argument_list_expression(self, t):
+ r'''argument_list : expression
+ '''
+ t[0] = t[1]
+ if DEBUG: print 'reducing "expression" to "argument_list"', t[0]
+
+ def p_argument_list_set(self, t):
+ r'''argument_list : expression COMMA argument_list
+ '''
+ t[0]=''
+ if DEBUG: print 'reducing "expression COMMA argument_list" to "argument_list"', t[0]
+
+ def p_call(self, t):
+ r'''call : IDENTIFIER BRACKET_L argument_list BRACKET_R
+ '''
+ raise NotImplementedError('function call')
+ if DEBUG: print 'reducing "IDENTIFIER BRACKET_L argument_list BRACKET_R" to "call"', t[0]
+
+#these are here, to keep lexer and parser instantiation to a minimum possible
+#level because they are quite expensive operations
+#parsers must be thread safe on the other hand!
+LEXER = lex.lex(object=Lexer(), debug=0)
+#PARSERS = local()
+
+def parse(str, metadata):
+ lexer = LEXER.clone()
+
+ #global PARSERS
+ #try:
+ # parser = PARSERS.parser
+ #
+ # try:
+ # parser.restart()
+ # except AttributeError:
+ # pass
+ #
+ #except AttributeError:
+ # parser = yacc.yacc(module = Parser(metadata))
+ # PARSERS.parser = parser
+
+ try:
+ parser = yacc.yacc(module = Parser(metadata))
+
+ retval = parser.parse(str, lexer = lexer)
+ except Exception, e:
+ if DEBUG: print e
+ raise
+ return retval
+
+class QueryParser(object):
+ implements(IQueryParser)
+ adapts(basestring)
+
+ def __init__(self, context):
+ self.context = context
+ #self.db = db
+
+ def __call__(self, metadata):
+ strg = self.context
+ tree = parse(strg, metadata)
+ return Head(tree)
+ #return parse(strg, None)
\ No newline at end of file
Modified: Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/qoptimizer/qoptimizer.py
===================================================================
--- Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/qoptimizer/qoptimizer.py 2008-08-07 12:29:55 UTC (rev 89496)
+++ Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/qoptimizer/qoptimizer.py 2008-08-07 14:25:09 UTC (rev 89497)
@@ -14,7 +14,7 @@
from ocql.interfaces import IQueryOptimizer
-from ocql.interfaces import IObjectQuery
+from ocql.interfaces import IObjectQueryHead
from ocql.interfaces import IOptimizedObjectQuery
def addMarkerIF(obj, marker):
@@ -24,7 +24,7 @@
class QueryOptimizer(object):
implements(IQueryOptimizer)
- adapts(IObjectQuery)
+ adapts(IObjectQueryHead)
def __init__(self, context):
self.context = context
Copied: Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/queryobject/interfaces.py (from rev 89449, Sandbox/adamg/ocql/trunk/src/ocql/queryobject/interfaces.py)
===================================================================
--- Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/queryobject/interfaces.py (rev 0)
+++ Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/queryobject/interfaces.py 2008-08-07 14:25:09 UTC (rev 89497)
@@ -0,0 +1,243 @@
+# -*- coding: UTF-8 -*-
+
+from zope.interface import Attribute, Interface
+from zope.schema import TextLine
+
+from ocql.interfaces import IObjectQuery
+
+class IObjectQueryChild(Interface):
+ """Objects providing this interface represents a child in the query object tree
+ """
+ children = Attribute('Children collection')
+
+class ITerm(IObjectQueryChild):
+ """Objects providing this interface represent the
+ Term Query object
+ """
+ identifier = Attribute('identifier')
+ expression = Attribute('attribute')
+
+class IExpression(ITerm, IObjectQuery):
+ """Objects providing this interface represent the
+ Expression Query object
+ """
+
+class IhasClassWith(IExpression):
+ """Objects providing this interface represent the
+ hasClassWith Query object
+ """
+ expression = Attribute('expression')
+ klass = Attribute('collection type')
+ conditional = Attribute('conditional')
+
+class IIdentifier(IExpression):
+ """Objects providing this interface represent the
+ Identifier Query object
+ """
+ name = TextLine()
+
+class IConstant(IExpression):
+ """Objects providing this interface represent the
+ Constant Query object
+ """
+ value = Attribute('constant value')
+
+class IStringConstant(IConstant):
+ """Objects providing this interface represent the
+ StringConstant Query object
+ """
+
+class INumericConstant(IConstant):
+ """Objects providing this interface represent the
+ NumericConstant Query object
+ """
+
+class IBooleanConstant(IConstant):
+ """Objects providing this interface represent the
+ BooleanConstant Query object
+ """
+
+class ICollectionConstant(IConstant):
+ """Objects providing this interface represent the
+ CollectionConstant Query object
+ """
+
+class IQuery(IExpression):
+ """Objects providing this interface represent the
+ Query object
+ """
+ collection_type = Attribute('collection type')
+ terms = Attribute('terms')
+ target = Attribute('target')
+
+class IIn(ITerm):
+ """Objects providing this interface represent the
+ In Query object
+ """
+
+class IAlias(ITerm):
+ """Objects providing this interface represent the
+ Alias Query object
+ """
+
+class IBinary(IExpression):
+ """Objects providing this interface represent the
+ Binary Query object
+ """
+ left = Attribute('left side of the binary expression')
+ right = Attribute('right side of the binary expression')
+
+class IUnion(IBinary):
+ """Objects providing this interface represent the
+ Union Query object
+ """
+
+class IDiffer(IBinary):
+ """Objects providing this interface represent the
+ Differ Query object
+ """
+
+class IAnd(IBinary):
+ """Objects providing this interface represent the
+ And Query object
+ """
+
+class IOr(IBinary):
+ """Objects providing this interface represent the
+ Or Query object
+ """
+
+class IProperty(IBinary):
+ """Objects providing this interface represent the
+ Property Query object
+ """
+
+class IIndex(IBinary):
+ """Objects providing this interface represent the
+ Index Query object
+ """
+
+class IArithmetic(IBinary):
+ """Objects providing this interface represent the
+ Arithmetic Query object
+ """
+
+class IAdd(IArithmetic):
+ """Objects providing this interface represent the
+ Add Query object
+ """
+class IMul(IArithmetic):
+ """Objects providing this interface represent the
+ Multiplication Query object
+ """
+
+class ISub(IArithmetic):
+ """Objects providing this interface represent the
+ Subtract Query object
+ """
+
+class IDiv(IArithmetic):
+ """Objects providing this interface represent the
+ Division Query object
+ """
+
+class IUnary(IExpression):
+ """Objects providing this interface represent the
+ Unary Query object
+ """
+ expression = Attribute('expression')
+
+class INot(IUnary):
+ """Objects providing this interface represent the
+ Not Query object
+ """
+
+class IAggregate(IUnary):
+ """Objects providing this interface represent the
+ Aggregate Query object
+ """
+
+class ICount(IAggregate):
+ """Objects providing this interface represent the
+ Count Query object
+ """
+
+class ISum(IAggregate):
+ """Objects providing this interface represent the
+ Sum Query object
+ """
+
+class IQuantor(IObjectQuery):
+ """Objects providing this interface represent the
+ Quantor Query object
+ """
+ expr = Attribute('expression')
+
+class IQuanted(IObjectQueryChild):
+ """Objects providing this interface represent the
+ Quanted Query object
+ """
+ quantor = Attribute('quantor')
+ expression = Attribute('expression')
+
+class IEvery(IQuantor):
+ """Objects providing this interface represent the
+ Every Query object
+ """
+
+class ISome(IQuantor):
+ """Objects providing this interface represent the
+ Some Query object
+ """
+
+class IAtmost(IQuantor):
+ """Objects providing this interface represent the
+ Atmost Query object
+ """
+
+class IAtleast(IQuantor):
+ """Objects providing this interface represent the
+ Atleast Query object
+ """
+
+class IJust(IQuantor):
+ """Objects providing this interface represent the
+ Just Query object
+ """
+
+class ICondition(IExpression):
+ """Objects providing this interface represent the
+ Condition Query object
+ """
+ left = Attribute('left side of the condition')
+ right = Attribute('right side of the condition')
+
+class IEq(ICondition):
+ """Objects providing this interface represent the
+ Equal Query object
+ """
+
+class INe(ICondition):
+ """Objects providing this interface represent the
+ Negation Query object
+ """
+
+class ILt(ICondition):
+ """Objects providing this interface represent the
+ Less than Query object
+ """
+
+class IGt(ICondition):
+ """Objects providing this interface represent the
+ Greater than Query object
+ """
+
+class ILe(ICondition):
+ """Objects providing this interface represent the
+ Less than or Equal Query object
+ """
+
+class IGe(ICondition):
+ """Objects providing this interface represent the
+ Greater than or Equal Query object
+ """
Modified: Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/queryobject/queryobject.py
===================================================================
--- Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/queryobject/queryobject.py 2008-08-07 12:29:55 UTC (rev 89496)
+++ Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/queryobject/queryobject.py 2008-08-07 14:25:09 UTC (rev 89497)
@@ -6,8 +6,6 @@
"""
#TODOS:
-#write Interfaces for all classes
-
#move self.rewrite to ocql.rewriter into adapters
#add self.__repr__ to ALL, see Query class
@@ -15,10 +13,43 @@
#implement a traversable tree of queryobjects (parent, child, sibling, ....)
from zope.interface import implements
+from zope.location import locate, Location
+from zope.location.interfaces import ILocation
from ocql.interfaces import IObjectQuery
+from ocql.interfaces import IObjectQueryHead
+from ocql.queryobject.interfaces import *
-class QueryObject:
+class Head(Location):
+ implements(IObjectQueryHead)
+ def __init__(self, tree):
+ self.name = 'head'
+ self.tree = tree
+ locate(tree, self, 'tree')
+
+ def __repr__(self):
+ return ('%s') % (self.tree)
+
+class Child(Location):
+ implements(IObjectQueryChild)
+
+ def __init__(self):
+ self.children = []
+
+ def setProp(self, name, value):
+ setattr(self, name, value)
+ if ILocation.providedBy(value):
+ locate(value, self, name)
+ self.children.append(value)
+
+ def setProperties(self, name, value):
+ setattr(self, name, value)
+ for term in value:
+ if ILocation.providedBy(term):
+ locate(term, self, 'term')
+ self.children.extend(value)
+
+class QueryObject(Child):
#TODO: this is dirty here, at the end we'll need to have a tree of
#QueryObject's whose topmost element will only get this IF
implements(IObjectQuery)
@@ -42,7 +73,6 @@
except AttributeError:
from pub.dbgpclient import brk; brk()
-
def get_collection_type(self, klass=None):
if klass is None:
klass = self.get_class()
@@ -54,20 +84,28 @@
#print self.name,rv
return rv
- def rewrite(self, algebra):
- raise NotImplementedError(self)
+class Term(Child):
+ implements(ITerm)
+ identifier = None
+ expression = None
-class Term:
- pass
+ def __init__(self, metadata, symbols, identifier, expression):
+ self.metadata = metadata
+ self.symbols = symbols
+ Child.__init__(self)
+ self.setProp('identifier', identifier)
+ self.setProp('expression', expression)
class Expression(Term, QueryObject):
- pass
+ implements(IExpression)
#
# General
#
class hasClassWith(Expression):
#NotImplementedError
+ implements(IhasClassWith)
+
expression = None
klass = None
conditional = None
@@ -75,17 +113,20 @@
def __init__(self, metadata, symbols, expr, klass, conditional):
self.metadata = metadata
self.symbols = symbols
- self.expression = expr
- self.klass = klass
- self.conditional = conditional
+ Child.__init__(self)
+ self.setProp('expr', expr)
+ self.setProp('klass', klass)
+ self.setProp('conditional', conditional)
class Identifier(Expression):
+ implements(IIdentifier)
name = None
def __init__(self, metadata, symbols, name):
self.metadata = metadata
self.symbols = symbols
- self.name = name
+ Child.__init__(self)
+ self.setProp('name', name)
def __repr__(self):
return "%s(%s)" % (
@@ -93,25 +134,21 @@
str(self.name),
)
- def rewrite(self, algebra):
- return algebra.Identifier(self.name)
-
class Constant(Expression):
+ implements(IConstant)
#this shall be abstract?
value = None
def __init__(self, metadata, symbols, value):
self.metadata = metadata
self.symbols = symbols
- self.value=value
+ Child.__init__(self)
+ self.setProp('value', value)
def __repr__(self):
return "%s(%s)" % (
self.__class__.__name__, self.value)
- def rewrite(self, algebra):
- return algebra.Constant(self.value)
-
class StringConstant(Constant):
pass
@@ -127,6 +164,7 @@
pass
class Query(Expression):
+ implements(IQuery)
collection_type = None
terms = None
target = None
@@ -134,9 +172,10 @@
def __init__(self, metadata, symbols, collection_type, terms, target):
self.metadata = metadata
self.symbols = symbols
- self.collection_type = collection_type
- self.terms = terms
- self.target = target
+ Child.__init__(self)
+ self.setProp('collection_type', collection_type)
+ self.setProperties('terms', terms)
+ self.setProp('target', target)
def __repr__(self):
return "%s(%s, %s, %s)" % (
@@ -149,78 +188,8 @@
def get_collection_type(self, klass=None):
return self.collection_type
- def rewrite(self, algebra):
- self.symbols.addlevel()
- rv=None
-
- if len(self.terms):
- for t in self.terms:
- t.addSymbol()
-
- firstTerm = self.terms[0]
- if isinstance(firstTerm, In):
-
- ctype = firstTerm.get_collection_type()
-
- rv = algebra.Iter(
- self.collection_type,
- algebra.Lambda(
- firstTerm.identifier.name,
- Query(
- self.metadata,
- self.symbols,
- self.collection_type,
- self.terms[1:],
- self.target
- ).rewrite(algebra)
- ), algebra.Make(
- self.collection_type,
- ctype,
- firstTerm.expression.rewrite(algebra),
- self.target
- ) # FIXME: ?set? must be determined by type(firstTerm.expression)
- )
- elif isinstance(firstTerm, Alias):
- rv = Query(
- self.metadata,
- self.symbols,
- self.collection_type,
- [In(
- self.metadata,
- self.symbols,
- firstTerm.identifier,
- firstTerm.expression
- )]+self.terms[1:],
- self.target).rewrite(algebra)
- else:
- rv = algebra.If(
- firstTerm.rewrite(algebra),
- Query(
- self.metadata,
- self.symbols,
- self.collection_type,
- self.terms[1:],
- self.target).rewrite(algebra),
- algebra.Empty(self.collection_type, None)
- )
- else:
- rv = algebra.Single(
- self.collection_type,
- self.target.rewrite(algebra))
-
- self.symbols.dellevel()
- return rv
-
class In(Term):
- identifier = None
- expression = None
-
- def __init__(self, metadata, symbols, identifier, expression):
- self.metadata = metadata
- self.symbols = symbols
- self.identifier = identifier
- self.expression = expression
-
+ implements(IIn)
def __repr__(self):
return "%s(%s, %s)" % (
self.__class__.__name__,
@@ -239,15 +208,7 @@
return rv
class Alias(Term):
- identifier = None
- expression = None
-
- def __init__(self, metadata, symbols, identifier, expression):
- self.metadata = metadata
- self.symbols = symbols
- self.identifier = identifier
- self.expression = expression
-
+ implements(IAlias)
def __repr__(self):
return "%s(%s, %s)" % (
self.__class__.__name__,
@@ -271,14 +232,16 @@
# Binary operators
#
class Binary(Expression):
+ implements(IBinary)
left = None
right = None
def __init__(self, metadata, symbols, left, right):
self.metadata = metadata
self.symbols = symbols
- self.left = left
- self.right = right
+ Child.__init__(self)
+ self.setProp('left', left)
+ self.setProp('right', right)
def __repr__(self):
return "%s(%s, %s)" % (
@@ -286,39 +249,21 @@
str(self.left), str(self.right)
)
- def rewrite(self, algebra):
- return algebra.Binary(
- self.left.rewrite(algebra),
- self.get_operator(algebra),
- self.right.rewrite(algebra))
-
# Sets and properties
class Union(Binary):
- def rewrite(self, algebra):
- return algebra.Union(
- self.left.get_collection_type(),
- self.left.rewrite(algebra),
- self.right.rewrite(algebra))
+ implements(IUnion)
class Differ(Binary):
- def rewrite(self, algebra):
- return algebra.Differ(
- self.left.get_collection_type(),
- self.left.rewrite(algebra),
- self.right.rewrite(algebra))
+ implements(IDiffer)
class And(Binary):
- def get_operator(self, algebra):
- return algebra.Operator('and')
+ implements(IAnd)
class Or(Binary):
- def get_operator(self, algebra):
- return algebra.Operator('or')
+ implements(IOr)
class Property(Binary):
- def rewrite(self, algebra): # FIXME: Ezt gondold at...
- return algebra.Identifier(
- '.'.join([self.left.name, self.right.name]))
+ implements(IProperty)
def get_class(self):
t = self.left.get_class()
@@ -338,39 +283,36 @@
class Index(Binary):
#NotImplementedError
- pass
+ implements(IIndex)
-
# Arithmetic operators
class Arithmetic(Binary):
- pass
+ implements(IArithmetic)
class Add(Arithmetic):
- def get_operator(self, algebra):
- return algebra.Operator('+')
+ implements(IAdd)
class Mul(Arithmetic):
- def get_operator(self, algebra):
- return algebra.Operator('*')
+ implements(IMul)
class Sub(Arithmetic):
- def get_operator(self, algebra):
- return algebra.Operator('-')
+ implements(ISub)
class Div(Arithmetic):
- def get_operator(self, algebra):
- return algebra.Operator('/')
+ implements(IDiv)
#
# Unary operators
#
class Unary(Expression):
+ implements(IUnary)
expression = None
def __init__(self, metadata, symbols, expression):
self.metadata = metadata
self.symbols = symbols
- self.expression = expression
+ Child.__init__(self)
+ self.setProp('expression', expression)
def __repr__(self):
return "%s(%s)" % (
@@ -379,176 +321,94 @@
)
class Not(Unary):
- def rewrite(self, algebra):
- return algebra.Not(self.expression.rewrite(algebra))
+ implements(INot)
class Aggregate(Unary):
- pass
+ implements(IAggregate)
class Count(Aggregate):
- def rewrite(self, algebra):
- return algebra.Reduce(
- bag, # FIXME milyen bag
- 0,
- algebra.Lambda('i', algebra.Constant(1)),
- algebra.Operator('+'),
- make(bag, set, self.expression.rewrite(algebra))
- # FIXME ?set? must be determined by type(self.expression)
- )
+ implements(ICount)
class Sum(Aggregate):
#NotImplementedError
- pass
+ implements(ISum)
#
# Conditional
#
class Quantor(QueryObject):
+ implements(IQuantor)
def __init__(self, metadata, symbols, expr):
self.metadata = metadata
self.symbols = symbols
- self.expr = expr
+ Child.__init__(self)
+ self.setProp('expr', expr)
def __repr__(self):
return "(%s)" % (
self.__class__.__name__
)
- def rewrite(self, algebra, expression, quanter, operator):
- raise NotImplementedError()
-
-class Quanted:
+class Quanted(Child):
+ implements(IQuanted)
quantor = None
expression = None
def __init__(self, metadata, symbols, quantor, expression):
self.metadata = metadata
self.symbols = symbols
- self.quantor = quantor
- self.expression = expression
+ Child.__init__(self)
+ self.setProp('quantor', quantor)
+ self.setProp('expression', expression)
- def rewrite(self, algebra, expression, operator):
- return self.quantor.rewrite(algebra, expression, self.expression, operator)
-
# Quantors
class Every(Quantor):
- def rewrite(self, algebra, expression, quanted, operator):
- ctype = quanted.get_collection_type()
+ implements(IEvery)
- return algebra.Reduce(
- ctype, # FIXME ?set? but which type() to take? quanted.expression?
- algebra.Identifier('True'),
- algebra.Lambda('i',
- operator.__class__(
- self.metadata,
- self.symbols,
- Identifier(
- self.metadata,
- self.symbols,
- 'i'),
- expression
- ).rewrite(algebra)
- ),
- algebra.Operator('and'),
- quanted.rewrite(algebra)
- )
-
class Some(Quantor):
- def rewrite(self, algebra, expression, quanted, operator):
- ctype = quanted.get_collection_type()
- return algebra.Reduce(
- ctype, # FIXME ?set? but which type() to take? quanted.expression?
- algebra.Identifier('False'),
- algebra.Lambda('i',
- operator.__class__(
- self.metadata,
- self.symbols,
- Identifier(self.metadata, self.symbols,'i'),
- expression
- ).rewrite(algebra)
- ),
- algebra.Operator('or'),
- quanted.rewrite(algebra)
- )
+ implements(ISome)
class Atmost(Quantor):
- pass
- #expr = None
+ implements(IAtmost)
- #def __init__(self, metadata, symbols, expr):
- # raise NotImplementedError(self)
- # self.metadata = metadata
- # self.symbols = symbols
- # self.expr = expr
-
class Atleast(Quantor):
- pass
+ implements(IAtleast)
#expr = None
- #def __init__(self, metadata, symbols, expr):
- # raise NotImplementedError(self)
- # self.metadata = metadata
- # self.symbols = symbols
- # self.expr = expr
-
class Just(Quantor):
- pass
+ implements(IJust)
#expr = None
- #def __init__(self, metadata, symbols, expr):
- # raise NotImplementedError(self)
- # self.metadata = metadata
- # self.symbols = symbols
- # self.expr = expr
-
# Logical operators
class Condition(Expression):
+ implements(ICondition)
left = None
right = None
def __init__(self, metadata, symbols, left, right):
self.metadata = metadata
self.symbols = symbols
- self.left = left
- self.right = right
+ Child.__init__(self)
+ self.setProp('left', left)
+ self.setProp('right', right)
- def rewrite(self, algebra):
- if isinstance(self.left, Quanted):
- return self.left.rewrite(
- algebra,
- self.right,
- self)
- if isinstance(self.right, Quanted):
- return self.right.rewrite(
- algebra,
- self.left,
- self)
- else:
- return algebra.Binary(self.left.rewrite(algebra), self.get_operator(algebra), self.right.rewrite(algebra))
-
class Eq(Condition):
- def get_operator(self, algebra):
- return algebra.Operator('==')
+ implements(IEq)
class Ne(Condition):
- def get_operator(self, algebra):
- return algebra.Operator('!=')
+ implements(INe)
class Lt(Condition):
- def get_operator(self, algebra):
- return algebra.Operator('<')
+ implements(ILt)
class Gt(Condition):
- def get_operator(self, algebra):
- return algebra.Operator('>')
+ implements(IGt)
class Le(Condition):
- def get_operator(self, algebra):
- return algebra.Operator('<=')
+ implements(ILe)
class Ge(Condition):
- def get_operator(self, algebra):
- return algebra.Operator('>=')
+ implements(IGe)
# TODO: missing:
#xi{Es}
Copied: Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/queryobject/queryobject.txt (from rev 89449, Sandbox/adamg/ocql/trunk/src/ocql/queryobject/queryobject.txt)
===================================================================
--- Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/queryobject/queryobject.txt (rev 0)
+++ Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/queryobject/queryobject.txt 2008-08-07 14:25:09 UTC (rev 89497)
@@ -0,0 +1,228 @@
+
+Checks here make sure that Interfaces are properly implemented
+
+#TODO: extend for all Interfaces and classes
+
+ >>> from ocql.interfaces import IObjectQuery
+ >>> from ocql.interfaces import IObjectQueryHead
+ >>> from ocql.queryobject.interfaces import *
+
+ >>> from ocql.queryobject.queryobject import *
+ >>> from ocql.testing.database import TestMetadata
+ >>> from ocql.parser.queryparser import SymbolContainer
+
+ >>> from zope.interface.verify import verifyClass, verifyObject
+
+ >>> child = Child()
+ >>> metadata = TestMetadata()
+ >>> symbols = SymbolContainer()
+
+ >>> verifyClass(IObjectQueryHead, Head)
+ True
+ >>> verifyObject(IObjectQueryHead, Head(child))
+ True
+
+ >>> verifyClass(IObjectQueryChild, Child)
+ True
+ >>> verifyObject(IObjectQueryChild, child)
+ True
+
+ >>> verifyClass(IObjectQuery, QueryObject)
+ True
+ >>> verifyObject(IObjectQuery, QueryObject(metadata, symbols))
+ True
+
+ >>> verifyClass(ITerm, Term)
+ True
+ >>> verifyObject(ITerm, Term(metadata, symbols, child, child))
+ True
+
+ >>> verifyClass(IExpression, Expression)
+ True
+ >>> verifyObject(IExpression, Expression(metadata, symbols, child, child))
+ True
+
+ >>> verifyClass(IhasClassWith, hasClassWith)
+ True
+ >>> verifyObject(IhasClassWith, hasClassWith(metadata, symbols, child, set, child))
+ True
+
+ >>> verifyClass(IIdentifier, Identifier)
+ True
+ >>> verifyObject(IIdentifier, Identifier(metadata, symbols, child))
+ True
+
+ >>> verifyClass(IConstant, Constant)
+ True
+ >>> verifyObject(IConstant, Constant(metadata, symbols, child))
+ True
+
+ >>> verifyClass(IQuery, Query)
+ True
+ >>> verifyObject(IQuery, Query(metadata, symbols, set, [child], child))
+ True
+
+ >>> verifyClass(IIn, In)
+ True
+ >>> verifyObject(IIn, In(metadata, symbols, child, child))
+ True
+
+ >>> verifyClass(IAlias, Alias)
+ True
+ >>> verifyObject(IAlias, Alias(metadata, symbols, child, child))
+ True
+
+ >>> verifyClass(IBinary, Binary)
+ True
+ >>> verifyObject(IBinary, Binary(metadata, symbols, child, child))
+ True
+
+ >>> verifyClass(IUnion, Union)
+ True
+ >>> verifyObject(IUnion, Union(metadata, symbols, child, child))
+ True
+
+ >>> verifyClass(IDiffer, Differ)
+ True
+ >>> verifyObject(IDiffer, Differ(metadata, symbols, child, child))
+ True
+
+ >>> verifyClass(IAnd, And)
+ True
+ >>> verifyObject(IAnd, And(metadata, symbols, child, child))
+ True
+
+ >>> verifyClass(IOr, Or)
+ True
+ >>> verifyObject(IOr, Or(metadata, symbols, child, child))
+ True
+
+ >>> verifyClass(IProperty, Property)
+ True
+ >>> verifyObject(IProperty, Property(metadata, symbols, child, child))
+ True
+
+ >>> verifyClass(IIndex, Index)
+ True
+ >>> verifyObject(IIndex, Index(metadata, symbols, child, child))
+ True
+
+ >>> verifyClass(IArithmetic, Arithmetic)
+ True
+ >>> verifyObject(IArithmetic, Arithmetic(metadata, symbols, child, child))
+ True
+
+ >>> verifyClass(IAdd, Add)
+ True
+ >>> verifyObject(IAdd, Add(metadata, symbols, child, child))
+ True
+
+ >>> verifyClass(IMul, Mul)
+ True
+ >>> verifyObject(IMul, Mul(metadata, symbols, child, child))
+ True
+
+ >>> verifyClass(ISub, Sub)
+ True
+ >>> verifyObject(ISub, Sub(metadata, symbols, child, child))
+ True
+
+ >>> verifyClass(IDiv, Div)
+ True
+ >>> verifyObject(IDiv, Div(metadata, symbols, child, child))
+ True
+
+ >>> verifyClass(IUnary, Unary)
+ True
+ >>> verifyObject(IUnary, Unary(metadata, symbols, child))
+ True
+
+ >>> verifyClass(INot, Not)
+ True
+ >>> verifyObject(INot , Not(metadata, symbols, child))
+ True
+
+ >>> verifyClass(IAggregate, Aggregate)
+ True
+ >>> verifyObject(IAggregate, Aggregate(metadata, symbols, child))
+ True
+
+ >>> verifyClass(ICount, Count)
+ True
+ >>> verifyObject(ICount, Count(metadata, symbols, child))
+ True
+
+ >>> verifyClass(ISum, Sum)
+ True
+ >>> verifyObject(ISum, Sum(metadata, symbols, child))
+ True
+
+ >>> verifyClass(IQuantor, Quantor)
+ True
+ >>> verifyObject(IQuantor, Quantor(metadata, symbols, child))
+ True
+
+ >>> verifyClass(IQuanted, Quanted)
+ True
+ >>> verifyObject(IQuanted, Quanted(metadata, symbols, child, child))
+ True
+
+ >>> verifyClass(IEvery, Every)
+ True
+ >>> verifyObject(IEvery, Every(metadata, symbols, child))
+ True
+
+ >>> verifyClass(ISome, Some)
+ True
+ >>> verifyObject(ISome, Some(metadata, symbols, child))
+ True
+
+ >>> verifyClass(IAtmost, Atmost)
+ True
+ >>> verifyObject(IAtmost, Atmost(metadata, symbols, child))
+ True
+
+ >>> verifyClass(IAtleast, Atleast)
+ True
+ >>> verifyObject(IAtleast, Atleast(metadata, symbols, child))
+ True
+
+ >>> verifyClass(IJust, Just)
+ True
+ >>> verifyObject(IJust, Just(metadata, symbols, child))
+ True
+
+ >>> verifyClass(ICondition, Condition)
+ True
+ >>> verifyObject(ICondition, Condition(metadata, symbols, child, child))
+ True
+
+ >>> verifyClass(IEq, Eq)
+ True
+ >>> verifyObject(IEq, Eq(metadata, symbols, child, child))
+ True
+
+ >>> verifyClass(INe, Ne)
+ True
+ >>> verifyObject(INe, Ne(metadata, symbols, child, child))
+ True
+
+ >>> verifyClass(ILt, Lt)
+ True
+ >>> verifyObject(ILt, Lt(metadata, symbols, child, child))
+ True
+
+ >>> verifyClass(IGt, Gt)
+ True
+ >>> verifyObject(IGt, Gt(metadata, symbols, child, child))
+ True
+
+ >>> verifyClass(ILe, Le)
+ True
+ >>> verifyObject(ILe, Le(metadata, symbols, child, child))
+ True
+
+ >>> verifyClass(IGe, Ge)
+ True
+ >>> verifyObject(IGe, Ge(metadata, symbols, child, child))
+ True
Copied: Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/queryobject/tests.py (from rev 89449, Sandbox/adamg/ocql/trunk/src/ocql/queryobject/tests.py)
===================================================================
--- Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/queryobject/tests.py (rev 0)
+++ Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/queryobject/tests.py 2008-08-07 14:25:09 UTC (rev 89497)
@@ -0,0 +1,14 @@
+import unittest
+import doctest
+from zope.testing.doctestunit import DocTestSuite,DocFileSuite
+
+def test_suite():
+ flags = doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS
+ return unittest.TestSuite((
+ DocFileSuite('queryobject.txt',
+ optionflags=flags),
+ ))
+
+
+if __name__ == '__main__':
+ unittest.main(defaultTest='test_suite')
Modified: Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/rewriter/algebra.py
===================================================================
--- Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/rewriter/algebra.py 2008-08-07 12:29:55 UTC (rev 89496)
+++ Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/rewriter/algebra.py 2008-08-07 14:25:09 UTC (rev 89497)
@@ -26,10 +26,12 @@
self.tree = tree
locate(tree, self, 'tree')
+ def walk(self):
+ yield self.tree
+
def __repr__(self):
return ('%s') % (self.tree)
-
class BaseAlgebra(Location):
implements(IAlgebraObject)
@@ -49,19 +51,16 @@
yield t
class Empty(BaseAlgebra):
-
implements(IEmpty)
- def __init__(self, klass, expr):
+ def __init__(self, klass):
BaseAlgebra.__init__(self)
self.setProp('klass', klass)
def __repr__(self):
return 'Empty(%s)'%(self.klass)
-
class Single(BaseAlgebra):
-
implements(ISingle)
def __init__(self, klass, expr):
@@ -73,7 +72,6 @@
return 'Single(%s, %s)'%(self.klass, self.expr)
class Union(BaseAlgebra):
-
implements(IUnion)
def __init__(self, klass, coll1, coll2):
@@ -85,22 +83,21 @@
def __repr__(self):
return 'Union(%s, %s, %s)'%(self.klass, self.coll1, self.coll2)
+class Differ(BaseAlgebra):
+ implements(IDiffer)
-#class Differ:
-# def __init__(self, klass, start, end):
-# self.setProp('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, coll1, coll2):
+ self.klass = klass
+ self.coll1 = coll1
+ self.coll2 = coll2
+ locate(coll1, self, 'coll1')
+ locate(coll2, self, 'coll2')
+ self.children.extend([coll1, coll2])
+ def __repr__(self):
+ return 'Differ(%s,%s,%s)'%(self.klass, self.coll1, self.coll2)
class Iter(BaseAlgebra):
-
implements(IIter)
def __init__(self, klass, func, coll):
@@ -112,9 +109,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 +121,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):
@@ -142,7 +135,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.setProp('klass', klass)
@@ -156,7 +148,6 @@
# return 'filter(%s, %s)' % (self.coll1.compile(),self.coll2.compile())
#
class Range(BaseAlgebra):
-
implements(IRange)
def __init__(self, klass, start, end):
@@ -165,11 +156,9 @@
self.setProp('start', start)
self.setProp('end', end)
-
#class Index
class Make(BaseAlgebra):
-
implements(IMake)
def __init__(self, coll1, coll2, expr1, expr2):
@@ -205,7 +194,6 @@
#class Being:
class If(BaseAlgebra):
-
implements(IIf)
def __init__(self, cond, expr1, expr2):
@@ -217,12 +205,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):
@@ -233,9 +219,7 @@
def __repr__(self):
return "Lambda %s: %s" %(self.var, self.expr)
-
class Constant(BaseAlgebra):
-
implements(IConstant)
def __init__(self, value):
@@ -245,9 +229,7 @@
def __repr__(self):
return "`%s`" %(self.value)
-
class Identifier(BaseAlgebra):
-
implements(IIdentifier)
def __init__(self, name):
@@ -258,7 +240,6 @@
return "%s" % self.name
class Binary(BaseAlgebra):
-
implements(IBinary)
def __init__(self, left, op, right):
@@ -270,9 +251,7 @@
def __repr__(self):
return "%s%s%s" % (self.left, self.op.op, self.right)
-
class Operator(BaseAlgebra):
-
implements(IOperator)
def __init__(self, op):
Copied: Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/rewriter/algebra.txt (from rev 89449, Sandbox/adamg/ocql/trunk/src/ocql/rewriter/algebra.txt)
===================================================================
--- Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/rewriter/algebra.txt (rev 0)
+++ Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/rewriter/algebra.txt 2008-08-07 14:25:09 UTC (rev 89497)
@@ -0,0 +1,104 @@
+
+Checks here make sure that Interfaces are properly implemented
+
+#TODO: extend for all Interfaces and classes
+
+ >>> from ocql.interfaces import IAlgebraObject
+ >>> from ocql.interfaces import IAlgebraObjectHead
+ >>> from ocql.rewriter.interfaces import *
+
+ >>> from ocql.rewriter.algebra import *
+
+ >>> from zope.interface.verify import verifyClass, verifyObject
+
+ >>> verifyClass(IAlgebraObjectHead, Head)
+ True
+ >>> baseAlgebra = BaseAlgebra()
+ >>> verifyObject(IAlgebraObjectHead, Head(baseAlgebra))
+ True
+
+ >>> verifyClass(IAlgebraObject, BaseAlgebra)
+ True
+ >>> verifyObject(IAlgebraObject, baseAlgebra)
+ True
+
+ >>> verifyClass(IEmpty, Empty)
+ True
+ >>> verifyObject(IEmpty, Empty(set))
+ True
+
+ >>> verifyClass(ISingle, Single)
+ True
+ >>> verifyObject(ISingle, Single(set, baseAlgebra))
+ True
+
+ >>> verifyClass(IUnion, Union)
+ True
+ >>> verifyObject(IUnion, Union(set, baseAlgebra, baseAlgebra))
+ True
+
+ >>> verifyClass(IDiffer, Differ)
+ True
+ >>> verifyObject(IDiffer, Differ(set, baseAlgebra, baseAlgebra))
+ True
+
+ >>> verifyClass(IIter, Iter)
+ True
+ >>> verifyObject(IIter, Iter(set, baseAlgebra, baseAlgebra))
+ True
+
+ >>> verifyClass(ISelect, Select)
+ True
+ >>> verifyObject(ISelect, Select(set, baseAlgebra, baseAlgebra))
+ True
+
+ >>> verifyClass(IReduce, Reduce)
+ True
+ >>> verifyObject(IReduce, Reduce(set, baseAlgebra, baseAlgebra, baseAlgebra, baseAlgebra))
+ True
+
+ >>> verifyClass(IRange, Range)
+ True
+ >>> verifyObject(IRange, Range(set, baseAlgebra, baseAlgebra))
+ True
+
+ >>> verifyClass(IMake, Make)
+ True
+ >>> verifyObject(IMake, Make(baseAlgebra, baseAlgebra, baseAlgebra))
+ True
+
+ #Uncomment this after merge other branch modifications
+ #>>> verifyClass(IMakeFromIndex, MakeFromIndex)
+ #True
+ #>>> verifyObject(IMakeFromIndex, MakeFromIndex(baseAlgebra, baseAlgebra, baseAlgebra, baseAlgebra))
+ #True
+
+ >>> verifyClass(IIf, If)
+ True
+ >>> verifyObject(IIf, If(baseAlgebra, baseAlgebra, baseAlgebra))
+ True
+
+ >>> verifyClass(ILambda, Lambda)
+ True
+ >>> verifyObject(ILambda, Lambda(baseAlgebra, baseAlgebra))
+ True
+
+ >>> verifyClass(IConstant, Constant)
+ True
+ >>> verifyObject(IConstant, Constant(baseAlgebra))
+ True
+
+ >>> verifyClass(IIdentifier, Identifier)
+ True
+ >>> verifyObject(IIdentifier, Identifier(baseAlgebra))
+ True
+
+ >>> verifyClass(IBinary, Binary)
+ True
+ >>> verifyObject(IBinary, Binary(baseAlgebra, baseAlgebra, baseAlgebra))
+ True
+
+ >>> verifyClass(IOperator, Operator)
+ True
+ >>> verifyObject(IOperator, Operator(baseAlgebra))
+ True
Modified: Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/rewriter/interfaces.py
===================================================================
--- Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/rewriter/interfaces.py 2008-08-07 12:29:55 UTC (rev 89496)
+++ Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/rewriter/interfaces.py 2008-08-07 14:25:09 UTC (rev 89497)
@@ -13,9 +13,7 @@
Empty Algebra object
"""
klass = Attribute('collection type name')
- expr = Attribute('expression')
-
class ISingle(IAlgebraObject):
"""Objects providing this interface represent the
Single Algebra object
@@ -23,7 +21,6 @@
klass = Attribute('collection type name')
expr = Attribute('expression')
-
class IUnion(IAlgebraObject):
"""Objects providing this interface represent the
Union Algebra object
@@ -32,36 +29,40 @@
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')
+ coll1 = Attribute('first collection')
+ coll2 = Attribute('second collection')
class IIter(IAlgebraObject):
"""Objects providing this interface represent the
Iter Algebra object
"""
klass = Attribute('collection type name')
- fun = Attribute('function')
+ func = Attribute('function')
coll = Attribute('collection')
-
class ISelect(IAlgebraObject):
"""Objects providing this interface represent the
Select Algebra object
"""
klass = Attribute('collection type name')
- fun = Attribute('function')
+ func = Attribute('function')
coll = Attribute('collection')
-
class IReduce(IAlgebraObject):
"""Objects providing this interface represent the
Reduce Algebra object
"""
klass = Attribute('collection type name')
expr = Attribute('expression')
- fun = Attribute('function')
+ func = Attribute('function')
aggreg = Attribute('aggregation')
coll = Attribute('collection')
-
class IRange(IAlgebraObject):
"""Objects providing this interface represent the
Range Algebra object
@@ -70,7 +71,6 @@
start = Attribute('range start point')
end = Attribute('range end point')
-
class IMake(IAlgebraObject):
"""Objects providing this interface represent the
Make Algebra object
@@ -80,7 +80,6 @@
coll1 = Attribute('first collection')
coll2 = Attribute('second collection')
-
class IMakeFromIndex(IAlgebraObject):
"""Objects providing this interface represent the
MakeFromIndex Algebra object
@@ -101,7 +100,6 @@
expr1 = Attribute('first expression')
expr2 =Attribute('second expression')
-
class ILambda(IAlgebraObject):
"""Objects providing this interface represent the
Lambda Algebra object
@@ -109,21 +107,18 @@
var = Attribute('variable')
expr = Attribute('expression')
-
class IConstant(IAlgebraObject):
"""Objects providing this interface represent the
Constant Algebra object
"""
value = Attribute('constant value')
-
class IIdentifier(IAlgebraObject):
"""Objects providing this interface represent the
Identifier Algebra object
"""
name = Text()
-
class IBinary(IAlgebraObject):
"""Objects providing this interface represent the
Binery Algebra object
@@ -132,10 +127,9 @@
op = Attribute('operator')
right = Attribute('right expression')
-
class IOperator(IAlgebraObject):
"""Objects providing this interface represent the
Operator Algebra object
"""
- ops = Dict(Text(), Text())
+# ops = Dict(Text(), Text())
op = Text()
Modified: Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/rewriter/rewriter.py
===================================================================
--- Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/rewriter/rewriter.py 2008-08-07 12:29:55 UTC (rev 89496)
+++ Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/rewriter/rewriter.py 2008-08-07 14:25:09 UTC (rev 89497)
@@ -12,12 +12,14 @@
from zope.component import adapts
from zope.interface import implements
from zope.location import locate
+from zope.component import provideAdapter
from ocql.interfaces import IRewriter
from ocql.interfaces import IOptimizedObjectQuery
-from ocql.rewriter.algebra import Head
-
+from ocql.rewriter.algebra import *
+from ocql.queryobject.interfaces import *
from ocql.rewriter import algebra as target_algebra
+import ocql.queryobject.queryobject
class Rewriter(object):
implements(IRewriter)
@@ -27,7 +29,337 @@
self.context = context
def __call__(self):
- query = self.context
- alg = query.rewrite(target_algebra)
- head = Head(alg)
- return head
\ No newline at end of file
+ query = self.context.tree
+ adapter = IRewriter(query)
+ alg = adapter()
+ return Head(alg)
+
+class ChildRewriter(object):
+ def __init__(self, context):
+ self.context = context
+
+class IdentifierRewriter(ChildRewriter):
+ implements(IRewriter)
+ adapts(IIdentifier)
+
+ def __call__(self):
+ return Identifier(self.context.name)
+
+class ConstantRewriter(ChildRewriter):
+ implements(IRewriter)
+ adapts(IConstant)
+
+ def __call__(self):
+ return Constant(self.context.value)
+
+class QueryRewriter(ChildRewriter):
+ implements(IRewriter)
+ adapts(IQuery)
+
+ def __call__(self):
+ self.context.symbols.addlevel()
+ rv=None
+
+ if len(self.context.terms):
+ for t in self.context.terms:
+ t.addSymbol()
+
+ firstTerm = self.context.terms[0]
+ if isinstance(firstTerm, ocql.queryobject.queryobject.In):
+
+ ctype = firstTerm.get_collection_type()
+
+ rv = Iter(
+ self.context.collection_type,
+ Lambda(
+ firstTerm.identifier.name,
+ IRewriter(ocql.queryobject.queryobject.Query(
+ self.context.metadata,
+ self.context.symbols,
+ self.context.collection_type,
+ self.context.terms[1:],
+ self.context.target
+ ))()
+ ), Make(
+ self.context.collection_type,
+ ctype,
+ IRewriter(firstTerm.expression)()
+ ) # FIXME: ?set? must be determined by type(firstTerm.expression)
+ )
+ elif isinstance(firstTerm, ocql.queryobject.queryobject.Alias):
+ rv = IRewriter(ocql.queryobject.queryobject.Query(
+ self.context.metadata,
+ self.context.symbols,
+ self.context.collection_type,
+ [ocql.queryobject.queryobject.In(
+ self.context.metadata,
+ self.context.symbols,
+ firstTerm.identifier,
+ firstTerm.expression
+ )]+self.context.terms[1:],
+ self.context.target))()
+ else:
+ rv = If(
+ IRewriter(firstTerm)(),
+ IRewriter(ocql.queryobject.queryobject.Query(
+ self.context.metadata,
+ self.context.symbols,
+ self.context.collection_type,
+ self.context.terms[1:],
+ self.context.target))(),
+ Empty(self.context.collection_type)
+ )
+ else:
+ rv = Single(
+ self.context.collection_type,
+ IRewriter(self.context.target)())
+
+ self.context.symbols.dellevel()
+ return rv
+
+class BinaryRewriter(ChildRewriter):
+ implements(IRewriter)
+ adapts(IBinary)
+
+ def __call__(self):
+ return Binary(IRewriter(self.context.left)(),
+ IRewriter(self.context).get_operator(),
+ IRewriter(self.context.right)())
+
+class UnionRewriter(BinaryRewriter):
+ implements(IRewriter)
+ adapts(IUnion)
+
+ def __call__(self):
+ return Union(
+ self.context.left.get_collection_type(),
+ IRewriter(self.context.left)(),
+ IRewriter(self.context.right)())
+
+class DifferRewriter(BinaryRewriter):
+ implements(IRewriter)
+ adapts(IDiffer)
+
+ def __call__(self):
+ return Differ(
+ self.context.left.get_collection_type(),
+ IRewriter(self.context.left)(),
+ IRewriter(self.context.right)())
+
+class AndRewriter(BinaryRewriter):
+ implements(IRewriter)
+ adapts(IAnd)
+
+ def get_operator(self):
+ return Operator('and')
+
+class OrRewriter(BinaryRewriter):
+ implements(IRewriter)
+ adapts(IOr)
+
+ def get_operator(self):
+ return Operator('or')
+
+class PropertyRewriter(BinaryRewriter):
+ implements(IRewriter)
+ adapts(IProperty)
+
+ def __call__(self):
+ return Identifier(
+ '.'.join([self.context.left.name, self.context.right.name]))
+
+class AddRewriter(BinaryRewriter):
+ implements(IRewriter)
+ adapts(IAdd)
+
+ def get_operator(self):
+ return Operator('+')
+
+class MulRewriter(BinaryRewriter):
+ implements(IRewriter)
+ adapts(IMul)
+
+ def get_operator(self):
+ return Operator('*')
+
+class SubRewriter(BinaryRewriter):
+ implements(IRewriter)
+ adapts(ISub)
+
+ def get_operator(self):
+ return Operator('-')
+
+class DivRewriter(BinaryRewriter):
+ implements(IRewriter)
+ adapts(IDiv)
+
+ def get_operator(self):
+ return Operator('/')
+
+class NotRewriter(BinaryRewriter):
+ implements(IRewriter)
+ adapts(INot)
+
+ def __call__(self):
+ return Not(IRewriter(self.context.expression)())
+
+class CountRewriter(ChildRewriter):
+ implements(IRewriter)
+ adapts(ICount)
+
+ def __call__(self):
+ return Reduce(
+ bag, # FIXME milyen bag
+ 0,
+ Lambda('i', Constant(1)),
+ Operator('+'),
+ make(bag, set, IRewriter(self.context.expression)())
+ # FIXME ?set? must be determined by type(self.expression)
+ )
+
+class QuantorRewriter(ChildRewriter):
+ implements(IRewriter)
+ adapts(IQuantor)
+
+ def __call__(self, expression, quanter, operator):
+ return NotImplementedError()
+
+class QuentedRewriter(ChildRewriter):
+ implements(IRewriter)
+ adapts(IQuanted)
+
+ def __call__(self, expression, operator):
+ return IRewriter(self.context.quantor)(expression, self.context.expression, operator)
+
+class EveryRewriter(ChildRewriter):
+ implements(IRewriter)
+ adapts(IEvery)
+
+ def __call__(self, expression, quanted, operator):
+ ctype = quanted.get_collection_type()
+
+ return Reduce(
+ ctype, # FIXME ?set? but which type() to take? quanted.expression?
+ Identifier('True'),
+ Lambda('i',
+ IRewriter(operator.__class__(
+ self.context.metadata,
+ self.context.symbols,
+ ocql.queryobject.queryobject.Identifier(
+ self.context.metadata,
+ self.context.symbols,
+ 'i'),
+ expression
+ ))()
+ ),
+ Operator('and'),
+ IRewriter(quanted)()
+ )
+
+class SomeRewriter(ChildRewriter):
+ implements(IRewriter)
+ adapts(ISome)
+
+ def __call__(self, expression, quanted, operator):
+ ctype = quanted.get_collection_type()
+ return Reduce(
+ ctype, # FIXME ?set? but which type() to take? quanted.expression?
+ Identifier('False'),
+ Lambda('i',
+ IRewriter(operator.__class__(
+ self.context.metadata,
+ self.context.symbols,
+ ocql.queryobject.queryobject.Identifier(
+ self.context.metadata,
+ self.context.symbols,'i'),
+ expression
+ ))()
+ ),
+ Operator('or'),
+ IRewriter(quanted)()
+ )
+
+class ConditionRewriter(ChildRewriter):
+ implements(IRewriter)
+ adapts(ICondition)
+
+ def __call__(self):
+ if isinstance(self.context.left, ocql.queryobject.queryobject.Quanted):
+ return IRewriter(self.context.left)(self.context.right, self.context)
+ if isinstance(self.context.right, ocql.queryobject.queryobject.Quanted):
+ return IRewriter(self.context.right)(self.context.left, self.context)
+ else:
+ return Binary(
+ IRewriter(self.context.left)(),
+ IRewriter(self.context).get_operator(),
+ IRewriter(self.context.right)())
+
+class EqRewriter(ConditionRewriter):
+ implements(IRewriter)
+ adapts(IEq)
+
+ def get_operator(self):
+ return Operator('==')
+
+class NeRewriter(ConditionRewriter):
+ implements(IRewriter)
+ adapts(INe)
+
+ def get_operator(self):
+ return Operator('!=')
+
+class LtRewriter(ConditionRewriter):
+ implements(IRewriter)
+ adapts(ILt)
+
+ def get_operator(self):
+ return Operator('<')
+
+class GtRewriter(ConditionRewriter):
+ implements(IRewriter)
+ adapts(IGt)
+
+ def get_operator(self):
+ return Operator('>')
+
+class LeRewriter(ConditionRewriter):
+ implements(IRewriter)
+ adapts(ILe)
+
+ def get_operator(self):
+ return Operator('<=')
+
+class GeRewriter(ConditionRewriter):
+ implements(IRewriter)
+ adapts(IGe)
+
+ def get_operator(self):
+ return Operator('>=')
+
+
+def registerAdapters():
+ provideAdapter(IdentifierRewriter)
+ provideAdapter(ConstantRewriter)
+ provideAdapter(QueryRewriter)
+ provideAdapter(BinaryRewriter)
+ provideAdapter(UnionRewriter)
+ provideAdapter(DifferRewriter)
+ provideAdapter(AndRewriter)
+ provideAdapter(OrRewriter)
+ provideAdapter(PropertyRewriter)
+ provideAdapter(AddRewriter)
+ provideAdapter(MulRewriter)
+ provideAdapter(SubRewriter)
+ provideAdapter(DivRewriter)
+ provideAdapter(NotRewriter)
+ provideAdapter(CountRewriter)
+ provideAdapter(QuentedRewriter)
+ provideAdapter(EveryRewriter)
+ provideAdapter(SomeRewriter)
+ provideAdapter(ConditionRewriter)
+ provideAdapter(EqRewriter)
+ provideAdapter(NeRewriter)
+ provideAdapter(LtRewriter)
+ provideAdapter(GtRewriter)
+ provideAdapter(LeRewriter)
+ provideAdapter(GeRewriter)
Modified: Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/rewriter/rewriter.txt
===================================================================
--- Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/rewriter/rewriter.txt 2008-08-07 12:29:55 UTC (rev 89496)
+++ Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/rewriter/rewriter.txt 2008-08-07 14:25:09 UTC (rev 89497)
@@ -12,13 +12,21 @@
>>> from ocql.testing.database import TestMetadata
+ >>> import ocql.rewriter.rewriter
+ >>> ocql.rewriter.rewriter.registerAdapters()
+
>>> qo = QueryParser("set [ | 1 ]")(TestMetadata())
>>> opt = QueryOptimizer(qo)()
>>> alg = Rewriter(opt)()
>>> print str(alg)
Single(<type 'set'>, `1`)
+ >>> qo = QueryParser("list [ | 1 ]")(TestMetadata())
+ >>> opt = QueryOptimizer(qo)()
+ >>> alg = Rewriter(opt)()
+ >>> print str(alg)
+ Single(<type 'list'>,`1`)
>>> qo = QueryParser("set [ | 1 ] union set [|2]")(TestMetadata())
>>> opt = QueryOptimizer(qo)()
@@ -26,14 +34,23 @@
>>> print str(alg)
Union(<type 'set'>, Single(<type 'set'>, `1`), Single(<type 'set'>, `2`))
+ >>> qo = QueryParser("list [ | 1 ] union list [|2]")(TestMetadata())
+ >>> opt = QueryOptimizer(qo)()
+ >>> alg = Rewriter(opt)()
+ >>> print str(alg)
+ Union(<type 'list'>,Single(<type 'list'>,`1`),Single(<type 'list'>,`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)()
+ >>> print str(alg)
+ Differ(<type 'set'>,Single(<type 'set'>,`1`),Single(<type 'set'>,`2`))
+ >>> qo = QueryParser("list [ | 1 ] differ list [|2]")(TestMetadata())
+ >>> opt = QueryOptimizer(qo)()
+ >>> alg = Rewriter(opt)()
+ >>> print str(alg)
+ Differ(<type 'list'>,Single(<type 'list'>,`1`),Single(<type 'list'>,`2`))
>>> qo = QueryParser("set [ i in ICourse | i ]")(TestMetadata())
>>> opt = QueryOptimizer(qo)()
@@ -41,6 +58,11 @@
>>> print str(alg)
Iter(<type 'set'>, Lambda i: Single(<type 'set'>, i), Make(<type 'set'>, <type 'set'>, ICourse))
+# >>> qo = QueryParser("list [ i in ICourse | i ]")(TestMetadata())
+# >>> opt = QueryOptimizer(qo)()
+# >>> alg = Rewriter(opt)()
+# >>> print str(alg)
+# Iter(<type 'list'>,Lambda i: Single(<type 'list'>,i),Make(<type 'list'>,<type 'list'>,ICourse))
>>> qo = QueryParser("set [ c in ICourse; c.credits>3 | c.code ]")(TestMetadata())
>>> opt = QueryOptimizer(qo)()
@@ -54,4 +76,4 @@
#>>> opt = QueryOptimizer(qo)()
#>>> alg = Rewriter(opt)()
#>>> print str(alg)
- #Iter(<type 'set'>, Lambda i: Single(<type 'set'>, i), Make(<type 'set'>, <type 'set'>, ICourse))
\ No newline at end of file
+ #Iter(<type 'set'>, Lambda i: Single(<type 'set'>, i), Make(<type 'set'>, <type 'set'>, ICourse))
Modified: Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/rewriter/tests.py
===================================================================
--- Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/rewriter/tests.py 2008-08-07 12:29:55 UTC (rev 89496)
+++ Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/rewriter/tests.py 2008-08-07 14:25:09 UTC (rev 89497)
@@ -7,6 +7,8 @@
return unittest.TestSuite((
DocFileSuite('rewriter.txt',
optionflags=flags),
+ DocFileSuite('algebra.txt',
+ optionflags=flags),
DocTestSuite('ocql.rewriter.algebra')
))
Modified: Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/tests/run.txt
===================================================================
--- Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/tests/run.txt 2008-08-07 12:29:55 UTC (rev 89496)
+++ Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/tests/run.txt 2008-08-07 14:25:09 UTC (rev 89497)
@@ -19,10 +19,13 @@
>>> from ocql.engine import OCQLEngine
- >>> from ocql.compiler.compiler import registerAdapters
- >>> registerAdapters()
+ >>> import ocql.compiler.compiler
+ >>> ocql.compiler.compiler.registerAdapters()
+ >>> import ocql.rewriter.rewriter
+ >>> ocql.rewriter.rewriter.registerAdapters()
+
>>> engine = OCQLEngine()
>>> run = engine.compile("set [ | 1 ]")
>>> run
Modified: Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/tests/test_old.py
===================================================================
--- Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/tests/test_old.py 2008-08-07 12:29:55 UTC (rev 89496)
+++ Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/tests/test_old.py 2008-08-07 14:25:09 UTC (rev 89497)
@@ -33,12 +33,12 @@
from ocql.interfaces import IDB
from ocql.interfaces import IQueryParser
-from ocql.interfaces import IObjectQuery
+from ocql.interfaces import IObjectQueryHead
class QueryNullParser(object):
implements(IQueryParser)
- adapts(IObjectQuery)
+ adapts(IObjectQueryHead)
def __init__(self, context):
self.context = context
@@ -95,11 +95,11 @@
# set [ ]
#
query = "set [ ]"
- qo=Query(metadata, symbols,
+ qo=Head(Query(metadata, symbols,
set,
[] ,
Identifier(metadata, symbols,
- '') )
+ '') ))
self.doit(query, qo, set([]))
@@ -111,7 +111,7 @@
# set [ c in ICourse | c ]
#
query = "[ c in ICourse | c ]"
- qo=Query(
+ qo=Head(Query(
metadata, symbols,
set,
[
@@ -119,7 +119,7 @@
metadata, symbols,
Identifier(metadata, symbols,'c'),
Identifier(metadata, symbols,'ICourse')),
- ] ,Identifier(metadata, symbols,'c') )
+ ] ,Identifier(metadata, symbols,'c') ))
#caution, these here are object references
self.doit(query, qo, set([ C1 , C2, C3 ]))
@@ -132,7 +132,7 @@
# set [ c in ICourse | c.code ]
#
query = "[ c in ICourse | c.code ]"
- qo=Query(
+ qo=Head(Query(
metadata, symbols,
set,
[
@@ -140,7 +140,7 @@
metadata, symbols,
Identifier(metadata, symbols,'c'),
Identifier(metadata, symbols,'ICourse')),
- ] ,Identifier(metadata, symbols,'c.code') )
+ ] ,Identifier(metadata, symbols,'c.code') ))
self.doit(query, qo, set([ "C1" , "C2", "C3" ]))
@@ -152,7 +152,7 @@
# set [ c in ICourse , c.credits>3 | c.code ]
#
query = "[ c in ICourse, c.credits>3 | c.code ]"
- qo=Query(
+ qo=Head(Query(
metadata, symbols,
set,
[
@@ -164,8 +164,11 @@
metadata, symbols,
Identifier(metadata, symbols,'c.credits'),
Constant(metadata, symbols,'3')),
- ] ,Identifier(metadata, symbols, 'c.code') )
+ ] ,Identifier(metadata, symbols, 'c.code') ))
+ from pub.dbgpclient import brk; brk('172.16.144.39')
+
+
self.doit(query, qo, set([]))
@@ -176,7 +179,7 @@
# set [ c in ICourse , c.credits<=3 | c.code ]
#
query = "[ c in ICourse, c.credits<=3 | c.code ]"
- qo=Query(
+ qo=Head(Query(
metadata, symbols,
set, [
In(
@@ -186,7 +189,7 @@
Le(metadata, symbols,
Identifier(metadata, symbols,'c.credits'),
Constant(metadata, symbols,'3')),
- ] ,Identifier(metadata, symbols,'c.code'))
+ ] ,Identifier(metadata, symbols,'c.code')))
self.doit(query, qo, set([ "C1" , "C2", "C3" ]))
@@ -198,7 +201,7 @@
# set [ c in ICourse , c.credits=3 | c.code ]
#
query = "[ c in ICourse, c.credits=3 | c.code ]"
- qo=Query(
+ qo=Head(Query(
metadata, symbols,
set,
[
@@ -210,7 +213,7 @@
metadata, symbols,
Identifier(metadata, symbols,'c.credits'),
Constant(metadata, symbols,'3')),
- ] ,Identifier(metadata, symbols,'c.code'))
+ ] ,Identifier(metadata, symbols,'c.code')))
self.doit(query, qo, set([ "C2", "C3" ]))
@@ -222,7 +225,7 @@
# set [ c in ICourse , c.credits<5, c.credits >=1 | c.code ]
#
query = "[ c in ICourse, c.credits<3, c.credits>=1 | c.code ]"
- qo=Query(
+ qo=Head(Query(
metadata, symbols,
set,
[
@@ -238,7 +241,7 @@
metadata, symbols,
Identifier(metadata, symbols,'c.credits'),
Constant(metadata, symbols,'1')),
- ] ,Identifier(metadata, symbols, 'c.code'))
+ ] ,Identifier(metadata, symbols, 'c.code')))
self.doit(query, qo, set([ "C1", "C2", "C3" ]))
@@ -250,7 +253,7 @@
# set [ c in ICourse , c.credits<=2, 2<=c.credits | c.code ]
#
query = "[ c in ICourse, c.credits<=2, 2<=c.credits | c.code ]"
- qo=Query(
+ qo=Head(Query(
metadata, symbols,
set, [
In(
@@ -265,7 +268,7 @@
metadata, symbols,
Constant(metadata, symbols,'2'),
Identifier(metadata, symbols,'c.credits')),
- ] ,Identifier(metadata, symbols,'c.code'))
+ ] ,Identifier(metadata, symbols,'c.code')))
self.doit(query, qo, set([ "C1" ]))
@@ -277,7 +280,7 @@
# set [ c in ICourse , c.credits>=2, 2>=c.credits | c.code ]
#
query = "[ c in ICourse, c.credits>=2, 2>=c.credits | c.code ]"
- qo=Query(
+ qo=Head(Query(
metadata, symbols,
set, [
In(
@@ -292,7 +295,7 @@
metadata, symbols,
Constant(metadata, symbols,'2'),
Identifier(metadata, symbols,'c.credits')),
- ] ,Identifier(metadata, symbols,'c.code'))
+ ] ,Identifier(metadata, symbols,'c.code')))
self.doit(query, qo, set([ "C1" ]))
@@ -304,7 +307,7 @@
# set [ c in ICourse , c.credits=3, c.credits!=3 | c.code ]
#
query = "[ c in ICourse, c.credits=3, c.credits!=3 | c.code ]"
- qo=Query(
+ qo=Head(Query(
metadata, symbols,
set, [
In(
@@ -319,7 +322,7 @@
metadata, symbols,
Identifier(metadata, symbols,'c.credits'),
Constant(metadata, symbols,'3')),
- ] ,Identifier(metadata, symbols,'c.code'))
+ ] ,Identifier(metadata, symbols,'c.code')))
self.doit(query, qo, set([]))
@@ -332,7 +335,7 @@
# some c.runBy = d | d.name ]
#
query = "[ c in ICourse, d in IDepartments, d = some c.runBy | d.name ]"
- qo=Query(
+ qo=Head(Query(
metadata, symbols,
set, [
In(
@@ -352,7 +355,7 @@
Identifier(metadata, symbols, 'c'),
Identifier(metadata, symbols, 'runBy'))
)),
- ] ,Identifier(metadata, symbols,'d.name'))
+ ] ,Identifier(metadata, symbols,'d.name')))
self.doit(query, qo, set(['Other department', 'Computing Science']))
@@ -364,7 +367,7 @@
# set [ d in ICourse, c in ICourse, c.credits=3, some c.runBy = d | d.name ]
#
query = "[ c in ICourse, d in IDepartments, c.credits=3, d = some c.runBy | d.name ]"
- qo=Query(
+ qo=Head(Query(
metadata, symbols,
set,
[
@@ -390,7 +393,7 @@
Identifier(metadata, symbols, 'c'),
Identifier(metadata, symbols, 'runBy'))
)),
- ] ,Identifier(metadata, symbols, 'd.name'))
+ ] ,Identifier(metadata, symbols, 'd.name')))
self.doit(query, qo, set(['Computing Science']))
@@ -403,7 +406,7 @@
query = """[ d in IDepartments,
c in ICourse,
some c.runBy = d, c.credits != 3| d.name ]"""
- qo=Query(
+ qo=Head(Query(
metadata, symbols,
set,
[
@@ -429,7 +432,7 @@
metadata, symbols,
Constant(metadata, symbols,'3'),
Identifier(metadata, symbols,'c.credits')),
- ] ,Identifier(metadata, symbols,'d.name'))
+ ] ,Identifier(metadata, symbols,'d.name')))
self.doit(query, qo, set(['Other department','Computing Science']))
@@ -445,7 +448,7 @@
every
set [ c in ICourse, some c.runBy = d | c.credits ] = 2
| d.name ]"""
- qo=Query(
+ qo=Head(Query(
metadata, symbols,
set,
[
@@ -479,7 +482,7 @@
], Identifier(metadata, symbols, 'c.credits')
)
),Constant(metadata, symbols,'2')),
- ] ,Identifier(metadata, symbols,'d.name'))
+ ] ,Identifier(metadata, symbols,'d.name')))
self.doit(query, qo, set(['Other department']))
Modified: Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/tests/test_zope.py
===================================================================
--- Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/tests/test_zope.py 2008-08-07 12:29:55 UTC (rev 89496)
+++ Sandbox/adamg/ocql/branches/optimize-with-index/src/ocql/tests/test_zope.py 2008-08-07 14:25:09 UTC (rev 89497)
@@ -75,11 +75,11 @@
# set [ ]
#
query = "set [ ]"
- qo=Query(metadata, symbols,
+ qo=Head(Query(metadata, symbols,
set,
[] ,
Identifier(metadata, symbols,
- '') )
+ '') ))
self.doit(query, qo, set([]))
@@ -91,7 +91,7 @@
# set [ c in IStudent | c ]
#
query = "[c in IStudent | c]"
- qo = Query(
+ qo = Head(Query(
metadata, symbols,
set,
[
@@ -99,7 +99,7 @@
metadata, symbols,
Identifier(metadata,symbols,'c'),
Identifier(metadata,symbols,'IStudent'))
- ], Identifier(metadata,symbols,'c'))
+ ], Identifier(metadata,symbols,'c')))
self.doit(query, qo, set(metadata.getAll('IStudent')))
@@ -111,7 +111,7 @@
# set [ c in IStudent | c.name ]
#
query = "[c in IStudent | c.name]"
- qo = Query(
+ qo = Head(Query(
metadata, symbols,
set,
[
@@ -119,7 +119,7 @@
metadata, symbols,
Identifier(metadata, symbols,'c'),
Identifier(metadata, symbols, 'IStudent'))
- ],Identifier(metadata, symbols, 'c.name'))
+ ],Identifier(metadata, symbols, 'c.name')))
self.doit(query, qo, set(["Charith", "Jane", "Ann"]))
@@ -130,7 +130,7 @@
# set [ c in IProject , c.description="test" | c.name]
#
query = "[c in IProject , c.description=test | c.name]"
- qo = Query(
+ qo = Head(Query(
metadata, symbols,
set,
[
@@ -142,7 +142,7 @@
metadata,symbols,
Identifier(metadata, symbols, 'c.description'),
Identifier(metadata, symbols, '"test"'))
- ], Identifier(metadata, symbols, 'c.name'))
+ ], Identifier(metadata, symbols, 'c.name')))
self.doit(query, qo, set(["Save the world"]))
More information about the Checkins
mailing list