void CreateModel(CreateModelStmt *stmt,const char *queryString, DestReceiver *dest, char *completionTag) { List *query_list; List *planned_list; const char *commandTag; Portal portal; DestReceiver *tupledest; // create command Tag commandTag = CreateCommandTag(stmt->algorithmclause->trainingdata); // Rewrite the already analyzed Select query for the training data query_list = pg_rewrite_query((Query *)stmt->algorithmclause->trainingdata); // plan the query planned_list = pg_plan_queries(query_list,0,NULL); // results should be send to the ModelReceiver tupledest = CreateModelDestReceiver(stmt->modelname, (TargetEntry *)stmt->outputcolumn, stmt->timecolumns, ((Query *)stmt->algorithmclause->trainingdata)->jointree->quals, queryString,stmt->algorithmclause->algorithmname, ((AlgorithmClause *)copyObject(stmt->algorithmclause))->algorithmparameter, 0); // Create a new portal to run the query in portal = CreateNewPortal(); //Don't display the portal in pg_cursors, it is for internal use only portal->visible = false; PortalDefineQuery(portal, NULL, queryString, commandTag, planned_list, NULL); // Start the portal. No parameters here. PortalStart(portal, NULL, InvalidSnapshot); (void) PortalRun(portal, FETCH_ALL, false, tupledest, tupledest, completionTag); // Drop portal and receiver (*tupledest->rDestroy) (tupledest); PortalDrop(portal, false); }
/* * Set up the per-query execution_state records for a SQL function. * * The input is a List of Lists of parsed and rewritten, but not planned, * querytrees. The sublist structure denotes the original query boundaries. */ static List * init_execution_state(List *queryTree_list, SQLFunctionCachePtr fcache, bool lazyEvalOK) { List *eslist = NIL; execution_state *lasttages = NULL; ListCell *lc1; foreach(lc1, queryTree_list) { List *qtlist = (List *) lfirst(lc1); execution_state *firstes = NULL; execution_state *preves = NULL; ListCell *lc2; foreach(lc2, qtlist) { Query *queryTree = (Query *) lfirst(lc2); Node *stmt; execution_state *newes; Assert(IsA(queryTree, Query)); /* Plan the query if needed */ if (queryTree->commandType == CMD_UTILITY) stmt = queryTree->utilityStmt; else stmt = (Node *) pg_plan_query(queryTree, 0, NULL); /* Precheck all commands for validity in a function */ if (IsA(stmt, TransactionStmt)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), /* translator: %s is a SQL statement name */ errmsg("%s is not allowed in a SQL function", CreateCommandTag(stmt)))); if (fcache->readonly_func && !CommandIsReadOnly(stmt)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), /* translator: %s is a SQL statement name */ errmsg("%s is not allowed in a non-volatile function", CreateCommandTag(stmt)))); /* OK, build the execution_state for this query */ newes = (execution_state *) palloc(sizeof(execution_state)); if (preves) preves->next = newes; else firstes = newes; newes->next = NULL; newes->status = F_EXEC_START; newes->setsResult = false; /* might change below */ newes->lazyEval = false; /* might change below */ newes->stmt = stmt; newes->qd = NULL; if (queryTree->canSetTag) lasttages = newes; preves = newes; }