def query(connection): # Get the physical plan for a test query catalog = MyriaCatalog(connection) servers = catalog.get_num_servers() parser = myrialparser.Parser() processor = interpreter.StatementProcessor(catalog, True) program = """ books = scan(Brandon:Demo:MoreBooks); longerBooks = [from books where pages > 300 emit name]; store(longerBooks, Brandon:Demo:LongerBooks); """ statement_list = parser.parse(program) processor.evaluate(statement_list) pd = processor.get_physical_plan(target_alg=MyriaLeftDeepTreeAlgebra()) json = processor.get_json_from_physical_plan(pd) return {'rawQuery': program, 'logicalRa': 'empty', 'fragments': json}
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)
def test_running_mean_stateful_apply(self): """Calculate the mean using stateful apply""" scan = Scan(TestQueryFunctions.emp_key, TestQueryFunctions.emp_schema) initex0 = NumericLiteral(0) updateex0 = PLUS(NamedStateAttributeRef("count"), NumericLiteral(1)) initex1 = NumericLiteral(0) updateex1 = PLUS(NamedStateAttributeRef("sum"), NamedAttributeRef("salary")) avgex = IDIVIDE(NamedStateAttributeRef("sum"), NamedStateAttributeRef("count")) sapply = StatefulApply([("avg", avgex)], [ StateVar("count", initex0, updateex0), StateVar("sum", initex1, updateex1) ], scan) store = Store(RelationKey("OUTPUT"), sapply) result = list(self.db.evaluate(sapply)) self.assertEqual(len(result), len(TestQueryFunctions.emp_table)) self.assertEqual([x[0] for x in result][-1], 37857) # test whether we can generate json without errors from raco.backends.myria import (compile_to_json, MyriaLeftDeepTreeAlgebra) from compile import optimize plan = compile_to_json("", None, optimize(store, MyriaLeftDeepTreeAlgebra())) # noqa for op in plan['plan']['fragments'][0]['operators']: if op['opType'] == 'StatefulApply': assert not any(exp is None for exp in op['emitExpressions'])
def execute_query(self, query, test_logical=False, skip_json=False, output="OUTPUT", algebra=MyriaLeftDeepTreeAlgebra): """Run a test query against the fake database""" dlog = RACompiler() dlog.fromDatalog(query) assert algebra in [MyriaLeftDeepTreeAlgebra, MyriaHyperCubeAlgebra] if test_logical: plan = dlog.logicalplan else: if algebra == MyriaLeftDeepTreeAlgebra: dlog.optimize(MyriaLeftDeepTreeAlgebra()) else: dlog.optimize(MyriaHyperCubeAlgebra(FakeCatalog(64))) plan = dlog.physicalplan if not skip_json: # test whether we can generate json without errors json_string = json.dumps( compile_to_json(query, dlog.logicalplan, dlog.physicalplan, "datalog")) assert json_string self.db.evaluate(plan) return self.db.get_table(output)
def get_physical_plan(self, **kwargs): """Return an operator representing the physical query plan.""" target_phys_algebra = kwargs.get('target_alg') if target_phys_algebra is None: if kwargs.get('multiway_join', False): target_phys_algebra = MyriaHyperCubeAlgebra(self.catalog) else: target_phys_algebra = MyriaLeftDeepTreeAlgebra() return self.__get_physical_plan_for__(target_phys_algebra, **kwargs)
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)
def _get_plan(self, query, language, plan_type, **kwargs): catalog = MyriaCatalog(self) algebra = MyriaHyperCubeAlgebra(catalog) \ if kwargs.get('multiway_jon', False) \ else MyriaLeftDeepTreeAlgebra() if language.lower() == "datalog": return self._get_datalog_plan(query, plan_type, algebra, **kwargs) elif language.lower() in ["myrial", "sql"]: return self._get_myrial_or_sql_plan(query, plan_type, catalog, algebra, **kwargs) else: raise NotImplementedError('Language %s not supported' % language)
def check(self, query, name, **kwargs): kwargs['target_alg'] = MyriaLeftDeepTreeAlgebra() plan = self.get_physical_plan(query, **kwargs) assert plan == plan_from_repr(repr(plan))