COptTasks.h 7.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
//---------------------------------------------------------------------------
//	Greenplum Database
//	Copyright (C) 2011 EMC Greenplum, Inc.
//
//	@filename:
//		COptTasks.h
//
//	@doc:
//		Tasks that will perform optimization and related tasks
//
//	@test:
//
//
//---------------------------------------------------------------------------
#ifndef COptTasks_H
#define COptTasks_H

#include "gpos/error/CException.h"

#include "gpopt/base/CColRef.h"
#include "gpopt/search/CSearchStage.h"



// fwd decl
namespace gpos
{
	class IMemoryPool;
	class CBitSet;
}

namespace gpdxl
{
	class CDXLNode;
}

namespace gpopt
{
	class CExpression;
	class CMDAccessor;
	class CQueryContext;
	class COptimizerConfig;
	class ICostModel;
}

struct PlannedStmt;
struct Query;
struct List;

using namespace gpos;
using namespace gpdxl;
using namespace gpopt;

54 55
// context of optimizer input and output objects
struct SOptContext
56 57
{

58 59 60 61 62 63 64 65 66 67
	// mark which pointer member should NOT be released
	// when calling Free() function
	enum EPin
	{
		epinQueryDXL, // keep m_szQueryDXL
		epinQuery, 	 // keep m_pquery
		epinPlanDXL, // keep m_szPlanDXL
		epinPlStmt, // keep m_pplstmt
		epinErrorMsg // keep m_szErrorMsg
	};
68

69 70
	// query object serialized to DXL
	CHAR *m_szQueryDXL;
71

72 73
	// query object
	Query *m_pquery;
74

75 76
	// plan object serialized to DXL
	CHAR *m_szPlanDXL;
77

78 79
	// plan object
	PlannedStmt *m_pplstmt;
80

81 82
	// is generating a plan object required ?
	BOOL m_fGeneratePlStmt;
83

84 85
	// is serializing a plan to DXL required ?
	BOOL m_fSerializePlanDXL;
86

87 88
	// did the optimizer fail unexpectedly?
	BOOL m_fUnexpectedFailure;
89

90 91
	// buffer for optimizer error messages
	CHAR *m_szErrorMsg;
92

93 94
	// ctor
	SOptContext();
95

96 97 98
	// If there is an error print as warning and throw exception to abort
	// plan generation
	void HandleError(BOOL *pfUnexpectedFailure);
99

100 101
	// free all members except input and output pointers
	void Free(EPin epinInput, EPin epinOutput);
102

103 104
	// Clone the error message in given context.
	CHAR* CloneErrorMsg(MemoryContext context);
105

106 107 108
	// casting function
	static
	SOptContext *PoptctxtConvert(void *pv);
109

110
}; // struct SOptContext
111

112 113 114
class COptTasks
{
	private:
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170

		// context of relcache input and output objects
		struct SContextRelcacheToDXL
		{
			// list of object oids to lookup
			List *m_plistOids;

			// comparison type for tasks retrieving scalar comparisons
			ULONG m_ulCmpt;

			// if filename is not null, then output will be written to file
			const char *m_szFilename;

			// if filename is null, then output will be stored here
			char *m_szDXL;

			// ctor
			SContextRelcacheToDXL(List *plistOids, ULONG ulCmpt, const char *szFilename);

			// casting function
			static
			SContextRelcacheToDXL *PctxrelcacheConvert(void *pv);
		};

		// Structure containing the input and output string for a task that evaluates expressions.
		struct SEvalExprContext
		{
			// Serialized DXL of the expression to be evaluated
			char *m_szDXL;

			// The result of evaluating the expression
			char *m_szDXLResult;

			// casting function
			static
			SEvalExprContext *PevalctxtConvert(void *pv);
		};

		// context of minidump load and execution
		struct SOptimizeMinidumpContext
		{
			// the name of the file containing the minidump
			char *m_szFileName;

			// the result of optimizing the minidump
			char *m_szDXLResult;

			// casting function
			static
			SOptimizeMinidumpContext *PoptmdpConvert(void *pv);
		};

		// execute a task given the argument
		static
		void Execute ( void *(*pfunc) (void *), void *pfuncArg);

171 172 173 174
		// print error and delete the given error buffer
		static
		void LogErrorAndDelete(CHAR* err_buf);

175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
		// task that does the translation from xml to dxl to pplstmt
		static
		void* PvPlstmtFromDXLTask(void *pv);

		// task that does the translation from query to XML
		static
		void* PvDXLFromQueryTask(void *pv);

		// dump relcache info for an object into DXL
		static
		void* PvDXLFromMDObjsTask(void *pv);

		// dump metadata about cast objects from relcache to a string in DXL format
		static
		void *PvMDCast(void *pv);
		
		// dump metadata about scalar comparison objects from relcache to a string in DXL format
		static
		void *PvMDScCmp(void *pv);
		
		// dump relstats info for an object into DXL
		static
		void* PvDXLFromRelStatsTask(void *pv);

		// evaluates an expression given as a serialized DXL string and returns the serialized DXL result
		static
		void* PvEvalExprFromDXLTask(void *pv);

		// create optimizer configuration object
		static
		COptimizerConfig *PoconfCreate(IMemoryPool *pmp, ICostModel *pcm);

		// optimize a query to a physical DXL
		static
		void* PvOptimizeTask(void *pv);

		// optimize the query in a minidump and return resulting plan in DXL format
		static
		void* PvOptimizeMinidumpTask(void *pv);

		// translate a DXL tree into a planned statement
		static
217
		PlannedStmt *Pplstmt(IMemoryPool *pmp, CMDAccessor *pmda, const CDXLNode *pdxln, bool canSetTag);
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265

		// load search strategy from given path
		static
		DrgPss *PdrgPssLoad(IMemoryPool *pmp, char *szPath);

		// allocate memory for string
		static
		CHAR *SzAllocate(IMemoryPool *pmp, ULONG ulSize);

		// helper for converting wide character string to regular string
		static
		CHAR *SzFromWsz(const WCHAR *wsz);

		// lookup given exception type in the given array
		static
		BOOL FExceptionFound(gpos::CException &exc, const ULONG *pulExceptions, ULONG ulSize);

		// check if given exception is an unexpected reason for failing to produce a plan
		static
		BOOL FUnexpectedFailure(gpos::CException &exc);

		// check if given exception should error out
		static
		BOOL FErrorOut(gpos::CException &exc);

		// set cost model parameters
		static
		void SetCostModelParams(ICostModel *pcm);

		// generate an instance of optimizer cost model
		static
		ICostModel *Pcm(IMemoryPool *pmp, ULONG ulSegments);

		// print warning messages for columns with missing statistics
		static
		void PrintMissingStatsWarning(IMemoryPool *pmp, CMDAccessor *pmda, DrgPmdid *pdrgmdidCol, HMMDIdMDId *phmmdidRel);

	public:

		// convert Query->DXL->LExpr->Optimize->PExpr->DXL
		static
		char *SzOptimize(Query *pquery);

		// optimize Query->DXL->LExpr->Optimize->PExpr->DXL->PlannedStmt
		static
		PlannedStmt *PplstmtOptimize
			(
			Query *pquery,
266
			SOptContext* octx,
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
			BOOL *pfUnexpectedFailure // output : set to true if optimizer unexpectedly failed to produce plan
			);

		// convert query to DXL to xml string.
		static
		char *SzDXL(Query *pquery);

		// convert xml string to DXL and to PS
		static
		PlannedStmt *PplstmtFromXML(char *szXmlString);

		// dump metadata objects from relcache to file in DXL format
		static
		void DumpMDObjs(List *oids, const char *szFilename);

		// dump metadata objects from relcache to a string in DXL format
		static
		char *SzMDObjs(List *oids);
		
		// dump cast function from relcache to a string in DXL format
		static
		char *SzMDCast(List *oids);
		
		// dump scalar comparison from relcache to a string in DXL format
		static
		char *SzMDScCmp(List *oids, char *szCmpType);

		// dump statistics from relcache to a string in DXL format
		static
		char *SzRelStats(List *oids);

		// enable/disable a given xforms
		static
		bool FSetXform(char *szXform, bool fDisable);
		
		// return comparison type code
		static
		ULONG UlCmpt(char *szCmpType);

		// converts XML string to DXL and evaluates the expression
		static
		char *SzEvalExprFromXML(char *szXmlString);

		// loads a minidump from the given file path, executes it and returns
		// the serialized representation of the result as DXL
		static
		char *SzOptimizeMinidumpFromFile(char *szFileName);
};

#endif // COptTasks_H

// EOF