Ejemplo n.º 1
0
def pythonmain():
  testexpr1 = Join([("object","subject")], Scan("R"), Scan("R"))

  R = Scan(Rr)
  #sR = Select("lambda t: t.predicate == 'knows'", R)
  #sS = Select("lambda t: t.predicate == 'holdsAccount'", R)
  #sT = Select("lambda t: t.predicate == 'accountServiceHomepage'", R)
  sR = Select(EQ(NamedAttributeRef("predicate"), StringLiteral("knows")), R)
  sS = Select(EQ(NamedAttributeRef("predicate"), StringLiteral("holdsAccount")), R)
  sT = Select(EQ(NamedAttributeRef("predicate"), StringLiteral("accountServiceHomepage")), R)

  sRsS = Join([("object","subject")], sR, sS)

  sRsSsT = Join([("object1", "subject")], sRsS, sT)

  ssR = Select(EQ(NamedAttributeRef("predicate"), StringLiteral("holdsAccount")), sR)

  test = Select(OR(EQ(NamedAttributeRef("predicate"), StringLiteral("knows")),EQ(NamedAttributeRef("predicate"), StringLiteral("holdsAccount"))), R)

  #print ssR
  ossR = optimize(test, target=PythonAlgebra, source=LogicalAlgebra)
  #ossR = optimize(ssR, target=PseudoCodeAlgebra, source=LogicalAlgebra)
  #print ossR
  #print compile(ossR)
  #print compile(sRsSsT)
  #print [gensym() for i in range(5)]

  #sR = Select(EQ(NamedAttributeRef("predicate"), NumericLiteral(330337405)), R)
  osR = optimize(sRsSsT, target=PythonAlgebra, source=LogicalAlgebra)
  print compile(osR)
Ejemplo n.º 2
0
 def to_json(self):
     """ Convert this query into an optimized JSON plan """
     # TODO deep copy, since optimize mutates
     sequence = Sequence([self.query])
     optimized = compile.optimize(sequence, OptLogicalAlgebra())
     myria = compile.optimize(optimized, MyriaLeftDeepTreeAlgebra())
     return compile_to_json(str(self.query), optimized, myria)
Ejemplo n.º 3
0
 def to_json(self):
     """ Convert this query into an optimized JSON plan """
     # TODO deep copy, since optimize mutates
     sequence = Sequence([self.query])
     optimized = compile.optimize(sequence, OptLogicalAlgebra())
     myria = compile.optimize(optimized, MyriaLeftDeepTreeAlgebra())
     return compile_to_json(str(self.query), optimized, myria)
Ejemplo n.º 4
0
    def get_plan(self, statements, logical=False):
        """Get the query plan"""
        self.logical = compile.optimize(statements, OptLogicalAlgebra())
        self.physical = compile.optimize(self.logical,
                                         MyriaLeftDeepTreeAlgebra())
        plan = self.logical if logical else self.physical

        # Verify that we can stringify
        assert str(plan)
        # Verify that we can convert to a dot
        raco.viz.get_dot(plan)
        # verify repr
        return replace_with_repr(plan)
Ejemplo n.º 5
0
    def get_plan(self, statements, logical=False):
        """Get the query plan"""
        self.logical = compile.optimize(statements, OptLogicalAlgebra())
        self.physical = compile.optimize(self.logical,
                                         MyriaLeftDeepTreeAlgebra())
        plan = self.logical if logical else self.physical

        # Verify that we can stringify
        assert str(plan)
        # Verify that we can convert to a dot
        raco.viz.get_dot(plan)
        # verify repr
        return replace_with_repr(plan)
Ejemplo n.º 6
0
def ccmain():
 # c implementation doesn't support string literals
 R = Scan(btc_schema["trial"])
 sR = Select(EQ(NamedAttributeRef("predicate"), NumericLiteral(1133564893)), R)
 sS = Select(EQ(NamedAttributeRef("predicate"), NumericLiteral(77645021)), R)
 sT = Select(EQ(NamedAttributeRef("predicate"), NumericLiteral(77645021)), R)


 sRsS = Join([("object","subject")], sR, sS)
 sRsSsT = Join([("object","subject")], sRsS, sT)
 """

 """
 result = optimize([('ans', sRsSsT)], target=CCAlgebra, source=LogicalAlgebra)
 #result = optimize(sR, target=CCAlgebra, source=LogicalAlgebra)
 return compile(result)
 def f(op):
   yield op #.__class__.__name__

 def g(op, vars):
   if op in vars: del vars[op]
   yield (op,vars.copy())

 def show(op):
   yield op.test

 #vars = dict([(x,1) for x in sR.postorder(f)])
 #vars.pop(sR, None)

 parents = {sRsSsT:[None]}
 sRsSsT.collectParents(parents)
Ejemplo n.º 7
0
 def get_json(self):
     lp = self.get_logical_plan()
     pps = optimize([(None, lp)], target=MyriaAlgebra,
                    source=LogicalAlgebra)
     # TODO This is not correct. The first argument is the raw query string,
     # not the string representation of the logical plan
     return compile_to_json(str(lp), pps[0][1], pps[0][1])
Ejemplo n.º 8
0
 def optimize(self, target=MyriaAlgebra,
              eliminate_common_subexpressions=False):
     """Convert logical plan to physical plan"""
     self.target = target
     self.physicalplan = optimize(
         self.logicalplan,
         target=self.target,
         source=LogicalAlgebra,
         eliminate_common_subexpressions=eliminate_common_subexpressions
     )
Ejemplo n.º 9
0
    def get_physical_plan(self):
        """Return an operator representing the physical query plan."""

        # TODO: Get rid of the dummy label argument here.
        # Return first (only) plan; strip off dummy label.
        logical_plan = self.get_logical_plan()
        physical_plans = optimize([('root', logical_plan)],
                                  target=MyriaAlgebra,
                                  source=LogicalAlgebra)
        return physical_plans[0][1]
Ejemplo n.º 10
0
    def __get_physical_plan_for__(self, target_phys_algebra, **kwargs):
        logical_plan = self.get_logical_plan(**kwargs)

        kwargs['target'] = target_phys_algebra
        return optimize(logical_plan, **kwargs)
Ejemplo n.º 11
0
    def __get_physical_plan_for__(self, target_phys_algebra, **kwargs):
        logical_plan = self.get_logical_plan(**kwargs)

        kwargs['target'] = target_phys_algebra
        return optimize(logical_plan, **kwargs)
Ejemplo n.º 12
0
 def logical_to_physical(lp, **kwargs):
     if kwargs.get('hypercube', False):
         algebra = MyriaHyperCubeAlgebra(FakeCatalog(64))
     else:
         algebra = MyriaLeftDeepTreeAlgebra()
     return optimize(lp, algebra, **kwargs)
Ejemplo n.º 13
0
 def optimize(self, target, **kwargs):
     """Convert logical plan to physical plan"""
     self.physicalplan = optimize(self.logicalplan, target, **kwargs)
Ejemplo n.º 14
0
 def logical_to_physical(lp):
     physical_plans = optimize([('root', lp)],
                               target=MyriaAlgebra,
                               source=LogicalAlgebra)
     return physical_plans[0][1]
Ejemplo n.º 15
0
sch = raco.scheme.Scheme([("subject", int), ("predicate", int), ("object", int)])

# Create a relation object.  We can add formats here as needed.
trialdat = raco.catalog.ASCIIFile("trial.dat", sch)
print sch

# Now write the RA expression

# Scan just takes a pointer to a relation object
R = Scan(trialdat, sch)  #TODO: is this supposed to pass sch?
print R.scheme()


# Select
# EQ(x,y) means x=y, GT(x,y) means x>y, etc.
sR = Select(EQ(NamedAttributeRef("predicate"), NumericLiteral(1133564893)), R)
sS = Select(EQ(NamedAttributeRef("predicate"), NumericLiteral(77645021)), R)
#sT = Select(EQ(NamedAttributeRef("predicate"), NumericLiteral(77645021)), R)
sT = Select(EQ(NamedAttributeRef("object"), NumericLiteral(1018848684)), R)

# Join([(w,x),(y,z)], R, S) means "JOIN R, S ON (R.w = S.x AND R.y = S.z)"
sRsS = Join([("object","subject")], sR, sS)
sRsSsT = Join([("object","subject")], sRsS, sT)

# optimize applies a set of rules to translate a source
# expression to a target expression
result = optimize([("Ans", sT)], CCAlgebra)

# compile generates the linear code from the expression tree
print compile(result)
Ejemplo n.º 16
0
 def optimize(self, target, **kwargs):
     """Convert logical plan to physical plan"""
     self.physicalplan = optimize(self.logicalplan, target, **kwargs)
Ejemplo n.º 17
0
#ra = onlyrule.toRA(parsedprogram)

#print exprs
ra = exprs

print query
print ra

print "//-------"


# optimize applies a set of rules to translate a source
# expression to a target expression
#result = optimize(ra, target=PseudoCodeAlgebra, source=LogicalAlgebra)
#result = optimize(ra, target=ProtobufAlgebra, source=LogicalAlgebra)
result = optimize(ra, target=MyriaAlgebra, source=LogicalAlgebra)

print result

#comment(result)
physicalplan = result
#physicalplan = common_subexpression_elimination(result)
#comment(physicalplan)
#for x in showids(physicalplan):
#  print x

# generate code in the target language
#print compile(physicalplan)
#compile(physicalplan)
print compile(physicalplan)
Ejemplo n.º 18
0
      #query = 'symmetric(a,b) :- edges1(a,b),edges2(b,a)'
      query = 'TwoPath(a,b,c) :- R(a,b),R(b,c)'

  import re
  p = re.compile('[^(]*')
  headname = p.match(query).group(0)

  print "query:", query, "\n"


  parsedprogram = parse(query)
  print "parsed:", parsedprogram, "\n"


  ra = parsedprogram.toRA()
  generateDot(ra,headname+'.dot')
  print "ra:", ra, "\n"

  physicalplan = optimize(ra, target=CCAlgebra, source=LogicalAlgebra)

  LOG.info("physical: %s",physicalplan[0])
  LOG.info('args=%s',physicalplan[0][1].args)
  LOG.info('joinconditions=%s',physicalplan[0][1].joinconditions)
  LOG.info('leftconditions=%s',physicalplan[0][1].leftconditions)
  LOG.info('rightconditions=%s',physicalplan[0][1].rightconditions)
  LOG.info('final=%s',physicalplan[0][1].finalcondition)
  tmp = sc.cpp_code(physicalplan,headname,dis=set_sem)
  LOG.info('cpp_code obj:%s',tmp)
  tmp.gen_code()