[Checkins] SVN: Sandbox/adamg/ocql/trunk/src/ocql/ itentified other optimization point down in the algebra tree
Charith Paranaliyanage
paranaliyanage at gmail.com
Sun Aug 17 10:53:49 EDT 2008
Log message for revision 89925:
itentified other optimization point down in the algebra tree
Changed:
U Sandbox/adamg/ocql/trunk/src/ocql/aoptimizer/aoptimizer.py
U Sandbox/adamg/ocql/trunk/src/ocql/rewriter/algebra.py
U Sandbox/adamg/ocql/trunk/src/ocql/rewriter/rewriter.txt
U Sandbox/adamg/ocql/trunk/src/ocql/testing/sample/interfaces.py
U Sandbox/adamg/ocql/trunk/src/ocql/testing/sample/student.py
U Sandbox/adamg/ocql/trunk/src/ocql/testing/utils.py
U Sandbox/adamg/ocql/trunk/src/ocql/tests/test_zope.py
-=-
Modified: Sandbox/adamg/ocql/trunk/src/ocql/aoptimizer/aoptimizer.py
===================================================================
--- Sandbox/adamg/ocql/trunk/src/ocql/aoptimizer/aoptimizer.py 2008-08-17 14:42:20 UTC (rev 89924)
+++ Sandbox/adamg/ocql/trunk/src/ocql/aoptimizer/aoptimizer.py 2008-08-17 14:53:48 UTC (rev 89925)
@@ -36,12 +36,16 @@
def findItrTreePattern(tree):
"""Checks whole Iter tree pattern exists stating from the Iter algebra object"""
iter_obj = bfsFind(tree)
- if iter_obj is not None:
+ while iter_obj:
#need to check If and Make objects present
if (isinstance(iter_obj.func, Lambda) and isinstance(iter_obj.coll, Make)):
if isinstance(iter_obj.func.expr, If):
if isinstance(iter_obj.func.expr.cond , Binary):
return iter_obj
+ else:
+ iter_obj = bfsFind(iter_obj.func)
+ else:
+ return None
return None
@@ -57,19 +61,14 @@
value = tree.func.expr.cond.right.value
elif isinstance(tree.func.expr.cond.right, Identifier):
value = tree.func.expr.cond.right.name
- else:
- return tree.__parent__
if not metadata.hasPropertyIndex(interface, cond.split(".")[1]):
return tree.__parent__
#new algebra objects
- if operator:
- makeFromIndex = MakeFromIndex(coll , coll, interface,
- cond.split(".")[1],
- operator, value=value)
- else:
- return tree.__parent__
+ makeFromIndex = MakeFromIndex(coll , coll, interface,
+ cond.split(".")[1],
+ operator, value=value)
newlambda = Lambda(var, single)
newTree = Iter(coll, newlambda, makeFromIndex)
@@ -77,12 +76,8 @@
if isinstance(parent, Head):
return Head(newTree)
else:
- for c in parent.children:
- if isinstance(c, Iter):
- del c
- parent.children.append(newTree)
- locate(newTree, parent, 'iter')
- return newTree
+ #possibly another optimization point, down in the algebra tree
+ return None
def addMarkerIF(obj, marker):
@@ -103,6 +98,8 @@
if results is not None:
alg = iterPatternMatcher(metadata, results)
+ if alg is None:
+ alg = self.context
addMarkerIF(alg, IOptimizedAlgebraObject)
return alg
Modified: Sandbox/adamg/ocql/trunk/src/ocql/rewriter/algebra.py
===================================================================
--- Sandbox/adamg/ocql/trunk/src/ocql/rewriter/algebra.py 2008-08-17 14:42:20 UTC (rev 89924)
+++ Sandbox/adamg/ocql/trunk/src/ocql/rewriter/algebra.py 2008-08-17 14:53:48 UTC (rev 89925)
@@ -27,7 +27,7 @@
locate(tree, self, 'tree')
def walk(self):
- yield self.tree
+ return self.tree.walk()
def __repr__(self):
return ('%s') % (self.tree)
@@ -249,7 +249,7 @@
self.setProp('right', right)
def __repr__(self):
- return "%s%s%s" % (self.left, self.op.op, self.right)
+ return "%s%s%s" % (self.left, self.op, self.right)
class Operator(BaseAlgebra):
implements(IOperator)
Modified: Sandbox/adamg/ocql/trunk/src/ocql/rewriter/rewriter.txt
===================================================================
--- Sandbox/adamg/ocql/trunk/src/ocql/rewriter/rewriter.txt 2008-08-17 14:42:20 UTC (rev 89924)
+++ Sandbox/adamg/ocql/trunk/src/ocql/rewriter/rewriter.txt 2008-08-17 14:53:48 UTC (rev 89925)
@@ -31,6 +31,14 @@
>>> print str(alg)
Union(<type 'set'>, Single(<type 'set'>, `1`), Single(<type 'set'>, `2`))
+ >>> for i in alg.walk():
+ ... print i
+ Union(<type 'set'>, Single(<type 'set'>, `1`), Single(<type 'set'>, `2`))
+ Single(<type 'set'>, `1`)
+ `1`
+ Single(<type 'set'>, `2`)
+ `2`
+
>>> qo = QueryParser("list [ | 1 ] union list [|2]")(TestMetadata())
>>> opt = QueryOptimizer(qo)()
>>> alg = Rewriter(opt)()
Modified: Sandbox/adamg/ocql/trunk/src/ocql/testing/sample/interfaces.py
===================================================================
--- Sandbox/adamg/ocql/trunk/src/ocql/testing/sample/interfaces.py 2008-08-17 14:42:20 UTC (rev 89924)
+++ Sandbox/adamg/ocql/trunk/src/ocql/testing/sample/interfaces.py 2008-08-17 14:53:48 UTC (rev 89925)
@@ -37,6 +37,7 @@
name = TextLine(title=u"Student Name")
country = Attribute('student country')
+ mentor = Field(title=u'assigned mentor')
class IMentor(Interface):
"""A Mentor object"""
Modified: Sandbox/adamg/ocql/trunk/src/ocql/testing/sample/student.py
===================================================================
--- Sandbox/adamg/ocql/trunk/src/ocql/testing/sample/student.py 2008-08-17 14:42:20 UTC (rev 89924)
+++ Sandbox/adamg/ocql/trunk/src/ocql/testing/sample/student.py 2008-08-17 14:53:48 UTC (rev 89925)
@@ -28,9 +28,10 @@
name = u''
country = None
- def __init__(self, name=u'', country=None):
+ def __init__(self, name=u'', country=None, mentor=None):
self.name = name
self.country = country
+ self.mentor = mentor
def __repr__(self):
return "%s <%s>" % (self.__class__.__name__, self.name)
Modified: Sandbox/adamg/ocql/trunk/src/ocql/testing/utils.py
===================================================================
--- Sandbox/adamg/ocql/trunk/src/ocql/testing/utils.py 2008-08-17 14:42:20 UTC (rev 89924)
+++ Sandbox/adamg/ocql/trunk/src/ocql/testing/utils.py 2008-08-17 14:53:48 UTC (rev 89925)
@@ -126,6 +126,10 @@
id = intids.register(s3)
cat.index_doc(id, s3)
+ s4 = Student(u"Stewart", u"Hungary", m1)
+ id = intids.register(s4)
+ cat.index_doc(id, s4)
+
o1 = Organization(u"Zope.org")
id = intids.register(o1)
cat.index_doc(id, o1)
Modified: Sandbox/adamg/ocql/trunk/src/ocql/tests/test_zope.py
===================================================================
--- Sandbox/adamg/ocql/trunk/src/ocql/tests/test_zope.py 2008-08-17 14:42:20 UTC (rev 89924)
+++ Sandbox/adamg/ocql/trunk/src/ocql/tests/test_zope.py 2008-08-17 14:53:48 UTC (rev 89925)
@@ -60,109 +60,229 @@
def test_gsoc(self):
metadata = IDB(None)
+# symbols = SymbolContainer()
+#
+# #
+# # Simple empty query
+# #
+# # set [ ]
+# #
+# query = "set [ ]"
+# qo=Head(Query(metadata, symbols,
+# set,
+# [] ,
+# Identifier(metadata, symbols,
+# '') ))
+#
+# self.doit(query, qo, set([]))
+#
+#
+# symbols = SymbolContainer()
+# #
+# # Simple empty query
+# #
+# # list [ ]
+# #
+# query = "list [ ]"
+# qo=Head(Query(metadata, symbols,
+# list,
+# [] ,
+# Identifier(metadata, symbols,
+# '') ))
+#
+# self.doit(query, qo, [])
+#
+#
+# symbols = SymbolContainer()
+# #
+# # Simple SELECT ALL
+# #
+# # set [ c in IStudent | c ]
+# #
+# query = "[c in IStudent | c]"
+# qo = Head(Query(
+# metadata, symbols,
+# set,
+# [
+# In(
+# metadata, symbols,
+# Identifier(metadata,symbols,'c'),
+# Identifier(metadata,symbols,'IStudent'))
+# ], Identifier(metadata,symbols,'c')))
+#
+# self.doit(query, qo, set(metadata.getAll('IStudent')))
+#
+#
+# symbols = SymbolContainer()
+# #
+# # Selecting a property
+# #
+# # set [ c in IStudent | c.name ]
+# #
+# query = "[c in IStudent | c.name]"
+# qo = Head(Query(
+# metadata, symbols,
+# set,
+# [
+# In(
+# metadata, symbols,
+# Identifier(metadata, symbols,'c'),
+# Identifier(metadata, symbols, 'IStudent'))
+# ],Identifier(metadata, symbols, 'c.name')))
+# self.doit(query, qo, set(["Charith", "Jane", "Ann", "Stewart"]))
+#
+#
+# symbols = SymbolContainer()
+# #
+# # Filtering --one result
+# #
+# # set [ c in IProject , c.description="test" | c.name]
+# #
+# query = "[c in IProject , c.description=test | c.name]"
+# qo = Head(Query(
+# metadata, symbols,
+# set,
+# [
+# In(
+# metadata, symbols,
+# Identifier(metadata,symbols,'c'),
+# Identifier(metadata,symbols, 'IProject')),
+# Eq(
+# metadata,symbols,
+# Identifier(metadata, symbols, 'c.description'),
+# Identifier(metadata, symbols, '"test"'))
+# ], Identifier(metadata, symbols, 'c.name')))
+#
+# self.doit(query, qo, set(["Save the world"]))
+#
+#
+# symbols = SymbolContainer()
+# #
+# # Filtering --one result using optimization
+# #
+# # set [ c in IStudent , c.country="USA" | c.name]
+# #
+# query = "[c in IStudent , c.country=USA | c.name]"
+# qo = Head(Query(
+# metadata, symbols,
+# set,
+# [
+# In(
+# metadata, symbols,
+# Identifier(metadata,symbols,'c'),
+# Identifier(metadata,symbols, 'IStudent')),
+# Eq(
+# metadata,symbols,
+# Identifier(metadata, symbols, 'c.country'),
+# Identifier(metadata, symbols, '"USA"'))
+# ], Identifier(metadata, symbols, 'c.name')))
+#
+# self.doit(query, qo, set([metadata.getFromIndex('IStudent', 'country','==', 'USA')[0].name]))
+#
+#
+# symbols = SymbolContainer()
+# #
+# # Filtering --one result using optimization
+# #
+# # set [ c in IStudent , c.country!="USA" | c.name]
+# #
+# query = "[c in IStudent , c.country != USA | c.name]"
+# qo = Head(Query(
+# metadata, symbols,
+# set,
+# [
+# In(
+# metadata, symbols,
+# Identifier(metadata,symbols,'c'),
+# Identifier(metadata,symbols, 'IStudent')),
+# Ne(
+# metadata,symbols,
+# Identifier(metadata, symbols, 'c.country'),
+# Identifier(metadata, symbols, '"USA"'))
+# ], Identifier(metadata, symbols, 'c.name')))
+#
+# self.doit(query, qo, set([i.name for i in metadata.getFromIndex('IStudent', 'country','!=', 'USA')]))
+#
+#
+# symbols = SymbolContainer()
+# #
+# # Filtering --one result using optimization
+# #
+# # set [ c in IStudent , c.country <= "Sri Lanka" | c.name]
+# #
+# query = "[c in IStudent , c.country <= 'Sri Lanka' | c.name]"
+# qo = Head(Query(
+# metadata, symbols,
+# set,
+# [
+# In(
+# metadata, symbols,
+# Identifier(metadata,symbols,'c'),
+# Identifier(metadata,symbols, 'IStudent')),
+# Le(
+# metadata,symbols,
+# Identifier(metadata, symbols, 'c.country'),
+# Identifier(metadata, symbols, '"Sri Lanka"'))
+# ], Identifier(metadata, symbols, 'c.name')))
+#
+# self.doit(query, qo, set([i.name for i in metadata.getFromIndex('IStudent', 'country','<=', 'Sri Lanka')]))
+#
+#
+# symbols = SymbolContainer()
+# #
+# # Filtering --one result using optimization
+# #
+# # set [ c in IStudent , c.country >= "Sri Lanka" | c.name]
+# #
+# query = "[c in IStudent , c.country >= 'Sri Lanka' | c.name]"
+# qo = Head(Query(
+# metadata, symbols,
+# set,
+# [
+# In(
+# metadata, symbols,
+# Identifier(metadata,symbols,'c'),
+# Identifier(metadata,symbols, 'IStudent')),
+# Ge(
+# metadata,symbols,
+# Identifier(metadata, symbols, 'c.country'),
+# Identifier(metadata, symbols, '"Sri Lanka"'))
+# ], Identifier(metadata, symbols, 'c.name')))
+#
+# self.doit(query, qo, set([i.name for i in metadata.getFromIndex('IStudent', 'country','>=', 'Sri Lanka')]))
+#
+#
+# symbols = SymbolContainer()
+# #
+# # Filtering --one result using optimization
+# #
+# # set [ c in IStudent , c.country < "Sri Lanka" | c.name]
+# #
+# query = "[c in IStudent , c.country < 'Sri Lanka' | c.name]"
+# qo = Head(Query(
+# metadata, symbols,
+# set,
+# [
+# In(
+# metadata, symbols,
+# Identifier(metadata,symbols,'c'),
+# Identifier(metadata,symbols, 'IStudent')),
+# Lt(
+# metadata,symbols,
+# Identifier(metadata, symbols, 'c.country'),
+# Identifier(metadata, symbols, '"Sri Lanka"'))
+# ], Identifier(metadata, symbols, 'c.name')))
+#
+# self.doit(query, qo, set([i.name for i in metadata.getFromIndex('IStudent', 'country','<', 'Sri Lanka')]))
+#
+#
symbols = SymbolContainer()
-
#
- # Simple empty query
- #
- # set [ ]
- #
- query = "set [ ]"
- qo=Head(Query(metadata, symbols,
- set,
- [] ,
- Identifier(metadata, symbols,
- '') ))
-
- self.doit(query, qo, set([]))
-
-
- symbols = SymbolContainer()
- #
- # Simple empty query
- #
- # list [ ]
- #
- query = "list [ ]"
- qo=Head(Query(metadata, symbols,
- list,
- [] ,
- Identifier(metadata, symbols,
- '') ))
-
- self.doit(query, qo, [])
-
-
- symbols = SymbolContainer()
- #
- # Simple SELECT ALL
- #
- # set [ c in IStudent | c ]
- #
- query = "[c in IStudent | c]"
- qo = Head(Query(
- metadata, symbols,
- set,
- [
- In(
- metadata, symbols,
- Identifier(metadata,symbols,'c'),
- Identifier(metadata,symbols,'IStudent'))
- ], Identifier(metadata,symbols,'c')))
-
- self.doit(query, qo, set(metadata.getAll('IStudent')))
-
-
- symbols = SymbolContainer()
- #
- # Selecting a property
- #
- # set [ c in IStudent | c.name ]
- #
- query = "[c in IStudent | c.name]"
- qo = Head(Query(
- metadata, symbols,
- set,
- [
- In(
- metadata, symbols,
- Identifier(metadata, symbols,'c'),
- Identifier(metadata, symbols, 'IStudent'))
- ],Identifier(metadata, symbols, 'c.name')))
- self.doit(query, qo, set(["Charith", "Jane", "Ann"]))
-
-
- symbols = SymbolContainer()
- #
- # Filtering --one result
- #
- # set [ c in IProject , c.description="test" | c.name]
- #
- query = "[c in IProject , c.description=test | c.name]"
- qo = Head(Query(
- metadata, symbols,
- set,
- [
- In(
- metadata, symbols,
- Identifier(metadata,symbols,'c'),
- Identifier(metadata,symbols, 'IProject')),
- Eq(
- metadata,symbols,
- Identifier(metadata, symbols, 'c.description'),
- Identifier(metadata, symbols, '"test"'))
- ], Identifier(metadata, symbols, 'c.name')))
-
- self.doit(query, qo, set(["Save the world"]))
-
-
- symbols = SymbolContainer()
- #
# Filtering --one result using optimization
#
- # set [ c in IStudent , c.country="USA" | c.name]
+ # set [ c in IStudent , c.country > "Sri Lanka" | c.name]
#
- query = "[c in IStudent , c.country=USA | c.name]"
+ query = "[c in IStudent , c.country > 'Sri Lanka' | c.name]"
qo = Head(Query(
metadata, symbols,
set,
@@ -171,135 +291,68 @@
metadata, symbols,
Identifier(metadata,symbols,'c'),
Identifier(metadata,symbols, 'IStudent')),
- Eq(
+ Gt(
metadata,symbols,
Identifier(metadata, symbols, 'c.country'),
- Identifier(metadata, symbols, '"USA"'))
- ], Identifier(metadata, symbols, 'c.name')))
-
- self.doit(query, qo, set([metadata.getFromIndex('IStudent', 'country','==', 'USA')[0].name]))
-
-
- symbols = SymbolContainer()
- #
- # Filtering --one result using optimization
- #
- # set [ c in IStudent , c.country!="USA" | c.name]
- #
- query = "[c in IStudent , c.country != USA | c.name]"
- qo = Head(Query(
- metadata, symbols,
- set,
- [
- In(
- metadata, symbols,
- Identifier(metadata,symbols,'c'),
- Identifier(metadata,symbols, 'IStudent')),
- Ne(
- metadata,symbols,
- Identifier(metadata, symbols, 'c.country'),
- Identifier(metadata, symbols, '"USA"'))
- ], Identifier(metadata, symbols, 'c.name')))
-
- self.doit(query, qo, set([i.name for i in metadata.getFromIndex('IStudent', 'country','!=', 'USA')]))
-
-
- symbols = SymbolContainer()
- #
- # Filtering --one result using optimization
- #
- # set [ c in IStudent , c.country <= "Sri Lanka" | c.name]
- #
- query = "[c in IStudent , c.country <= 'Sri Lanka' | c.name]"
- qo = Head(Query(
- metadata, symbols,
- set,
- [
- In(
- metadata, symbols,
- Identifier(metadata,symbols,'c'),
- Identifier(metadata,symbols, 'IStudent')),
- Le(
- metadata,symbols,
- Identifier(metadata, symbols, 'c.country'),
Identifier(metadata, symbols, '"Sri Lanka"'))
], Identifier(metadata, symbols, 'c.name')))
- self.doit(query, qo, set([i.name for i in metadata.getFromIndex('IStudent', 'country','<=', 'Sri Lanka')]))
+ self.doit(query, qo, set([i.name for i in metadata.getFromIndex('IStudent', 'country','>', 'Sri Lanka')]))
symbols = SymbolContainer()
#
- # Filtering --one result using optimization
#
- # set [ c in IStudent , c.country >= "Sri Lanka" | c.name]
+ # join -- Mentor who is mentoring Hungary student
#
- query = "[c in IStudent , c.country >= 'Sri Lanka' | c.name]"
- qo = Head(Query(
- metadata, symbols,
- set,
- [
- In(
- metadata, symbols,
- Identifier(metadata,symbols,'c'),
- Identifier(metadata,symbols, 'IStudent')),
- Ge(
- metadata,symbols,
- Identifier(metadata, symbols, 'c.country'),
- Identifier(metadata, symbols, '"Sri Lanka"'))
- ], Identifier(metadata, symbols, 'c.name')))
-
- self.doit(query, qo, set([i.name for i in metadata.getFromIndex('IStudent', 'country','>=', 'Sri Lanka')]))
-
-
- symbols = SymbolContainer()
+ # set [ m in IMentor, every set [ s in IStudent, some s.mentor = m | s.country ] == Hungary | m.name ]
#
- # Filtering --one result using optimization
- #
- # set [ c in IStudent , c.country < "Sri Lanka" | c.name]
- #
- query = "[c in IStudent , c.country < 'Sri Lanka' | c.name]"
- qo = Head(Query(
- metadata, symbols,
- set,
- [
- In(
- metadata, symbols,
- Identifier(metadata,symbols,'c'),
- Identifier(metadata,symbols, 'IStudent')),
- Lt(
- metadata,symbols,
- Identifier(metadata, symbols, 'c.country'),
- Identifier(metadata, symbols, '"Sri Lanka"'))
- ], Identifier(metadata, symbols, 'c.name')))
+ query = """set [ m in IMentor,
+ every
+ set [ s in IStudent, some s.mentor = m; s.country=Hungary | s.name] == Stewart
+ | m.name ]"""
+ qo=Head(Query(
+ metadata, symbols,
+ set,
+ [
+ In(
+ metadata, symbols,
+ Identifier(metadata, symbols,'m'),
+ Identifier(metadata, symbols,'IMentor')),
+ Eq(
+ metadata, symbols,
+ Quanted(
+ metadata, symbols,
+ Every(metadata, symbols, ''),
+ Query(
+ metadata, symbols,
+ set,
+ [
+ In(
+ metadata, symbols,
+ Identifier(metadata, symbols,'s'),
+ Identifier(metadata, symbols,'IStudent')),
+ Eq(
+ metadata,symbols,
+ Identifier(metadata, symbols, 's.country'),
+ Identifier(metadata, symbols, '"Hungary"')),
+ Eq(
+ metadata, symbols,
+ Identifier(metadata, symbols,'m'),
+ Quanted(
+ metadata, symbols,
+ Some(metadata, symbols, ''),
+ Property(metadata, symbols,
+ Identifier(metadata, symbols, 's'),
+ Identifier(metadata, symbols, 'mentor'))
+ ))
+ ], Identifier(metadata, symbols, 's.name'))
+ ),Constant(metadata, symbols,'Stewart')),
+ ] ,Identifier(metadata, symbols,'m.name')))
- self.doit(query, qo, set([i.name for i in metadata.getFromIndex('IStudent', 'country','<', 'Sri Lanka')]))
+ self.doit(query, qo, set(['John Doe']))
- symbols = SymbolContainer()
- #
- # Filtering --one result using optimization
- #
- # set [ c in IStudent , c.country > "Sri Lanka" | c.name]
- #
- query = "[c in IStudent , c.country > 'Sri Lanka' | c.name]"
- qo = Head(Query(
- metadata, symbols,
- set,
- [
- In(
- metadata, symbols,
- Identifier(metadata,symbols,'c'),
- Identifier(metadata,symbols, 'IStudent')),
- Gt(
- metadata,symbols,
- Identifier(metadata, symbols, 'c.country'),
- Identifier(metadata, symbols, '"Sri Lanka"'))
- ], Identifier(metadata, symbols, 'c.name')))
-
- self.doit(query, qo, set([i.name for i in metadata.getFromIndex('IStudent', 'country','>', 'Sri Lanka')]))
-
-
def test_suite():
flags = doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS
return unittest.TestSuite((
More information about the Checkins
mailing list