Exemple #1
0
//---------------------------------------------------------------------------
static void runQuery(Database& db,const string& query,bool explain)
   // Evaluate a query
{
   QueryGraph queryGraph;
   {
      // Parse the query
      SPARQLLexer lexer(query);
      SPARQLParser parser(lexer);
      try {
         parser.parse();
      } catch (const SPARQLParser::ParserException& e) {
         cerr << "parse error: " << e.message << endl;
         return;
      }

      // And perform the semantic anaylsis
      try {
         SemanticAnalysis semana(db);
         semana.transform(parser,queryGraph);
      } catch (const SemanticAnalysis::SemanticException& e) {
         cerr << "semantic error: " << e.message << endl;
         return;
      }
      if (queryGraph.knownEmpty()) {
         if (explain)
            cerr << "static analysis determined that the query result will be empty" << endl; else
            cout << "<empty result>" << endl;
         return;
      }
   }

   // Run the optimizer
   PlanGen plangen;
   Plan* plan=plangen.translate(db,queryGraph);
   //plan->print(6);
   if (!plan) {
      cerr << "internal error plan generation failed" << endl;
      return;
   }

   // Build a physical plan
   Runtime runtime(db);
   Operator* operatorTree=CodeGen().translate(runtime,queryGraph,plan,false);

   // Explain if requested
   if (explain) {
      DebugPlanPrinter out(runtime,false);
      operatorTree->print(out);
   } else {
      // Else execute it
      if (operatorTree->first()) {
         while (operatorTree->next()) ;
      }
   }

   delete operatorTree;
}
Exemple #2
0
//---------------------------------------------------------------------------
static void evalQuery(Database& db,const string& query,bool silent)
   // Evaluate a query
{
   QueryGraph queryGraph;
   {
      // Parse the query
      SPARQLLexer lexer(query);
      SPARQLParser parser(lexer);
      try {
         parser.parse();
      } catch (const SPARQLParser::ParserException& e) {
         cout << "parse error: " << e.message << endl;
         return;
      }

      // And perform the semantic anaylsis
      try {
         SemanticAnalysis semana(db);
         semana.transform(parser,queryGraph);
      } catch (const SemanticAnalysis::SemanticException& e) {
         cout << "semantic error: " << e.message << endl;
         return;
      }
      if (queryGraph.knownEmpty()) {
         cout << "<empty result>" << endl;
         return;
      }
   }

   // Run the optimizer
   PlanGen plangen;
   Plan* plan=plangen.translate(db,queryGraph);
   if (!plan) {
      cout << "plan generation failed" << endl;
      return;
   }
   if (getenv("SHOWCOSTS"))
      plan->print(0);
   if (getenv("DISABLESKIPPING"))
      Operator::disableSkipping=true;

   // Build a physical plan
   TemporaryDictionary tempDict(db.getDictionary());
   Runtime runtime(db,0,&tempDict);
   Operator* operatorTree=CodeGen().translate(runtime,queryGraph,plan,silent);

   if (getenv("SHOWPLAN")) {
      DebugPlanPrinter out(runtime,false);
      operatorTree->print(out);
   }

   // And execute it
   Scheduler scheduler;
   Timestamp start;
   scheduler.execute(operatorTree);
   Timestamp stop;
   cout << "Execution time: " << (stop-start) << " ms" << endl;

   if (getenv("SHOWCARD")) {
      DebugPlanPrinter out(runtime,true);
      operatorTree->print(out);
   }

   delete operatorTree;
}
//---------------------------------------------------------------------------
static bool evalQuery(Database& db,SPARQLLexer& lexer,ostream& planOut,ostream& statsOut,bool showJoins)
   // Evaluate a query
{
   QueryGraph queryGraph;
   std::string::const_iterator queryStart=lexer.getReader();
   {
      // Parse the query
      SPARQLParser parser(lexer);
      try {
         parser.parse(true);
      } catch (const SPARQLParser::ParserException& e) {
         cerr << "parse error: " << e.message << endl;
         return false;
      }

      // And perform the semantic anaylsis
      SemanticAnalysis semana(db);
      semana.transform(parser,queryGraph);
      if (queryGraph.knownEmpty()) {
         cerr << "known empty result ignored" << endl;
         return true;
      }
   }

   // Run the optimizer
   PlanGen plangen;
   Plan* plan=plangen.translate(db,queryGraph);
   if (!plan) {
      cerr << "plan generation failed" << endl;
      return true;
   }
   Operator::disableSkipping=true;

   // Build a physical plan
   Runtime runtime(db);
   Operator* operatorTree=CodeGen().translate(runtime,queryGraph,plan,true);
   Operator* root=dynamic_cast<ResultsPrinter*>(operatorTree)->getInput();

   // And execute it
   Scheduler scheduler;
   scheduler.execute(root);
   Timestamp stop;

   // Write the plan
   planOut << "# SPARQL Query:" << endl << "# ";
   std::string::const_iterator queryEnd=lexer.getReader();
   for (string::const_iterator iter=queryStart;iter!=queryEnd;++iter)
      if ((*iter)=='\n')
         planOut << endl << "# "; else
         planOut << *iter;
   planOut << endl << endl << "# Execution plan: <Operator expectedCardinality observedCardinalit [args] [input]>" << endl << endl;
   {
      DebugPlanPrinter out(planOut,runtime,true);
      root->print(out);
   }

   // Write the selectivities
   {
      if (showJoins)
         statsOut << "# relations joinVar1 joinVar2 bindings expectedSelectivity observedSelectivity" << endl; else
         statsOut << "# Stats: {relations} {new predicates(s)} {previous predicate(s)} expectedCardinality observedCardinality expectedSelectivity observedSelectivity" << endl;
      PredicateCollector out(statsOut,runtime,showJoins);
      root->print(out);
   }

   delete operatorTree;
   return true;
}