COptTasks.h 7.4 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
//---------------------------------------------------------------------------
//	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
{
J
Jesse Zhang 已提交
28 29 30
class CMemoryPool;
class CBitSet;
}  // namespace gpos
31 32 33

namespace gpdxl
{
J
Jesse Zhang 已提交
34
class CDXLNode;
35 36 37 38
}

namespace gpopt
{
J
Jesse Zhang 已提交
39 40 41 42 43 44
class CExpression;
class CMDAccessor;
class CQueryContext;
class COptimizerConfig;
class ICostModel;
}  // namespace gpopt
45 46 47 48

struct PlannedStmt;
struct Query;
struct List;
H
Heikki Linnakangas 已提交
49
struct MemoryContextData;
50 51 52 53 54

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

55 56
// context of optimizer input and output objects
struct SOptContext
57
{
58 59 60 61
	// mark which pointer member should NOT be released
	// when calling Free() function
	enum EPin
	{
J
Jesse Zhang 已提交
62 63 64 65 66
		epinQueryDXL,  // keep m_query_dxl
		epinQuery,	   // keep m_query
		epinPlanDXL,   // keep m_plan_dxl
		epinPlStmt,	   // keep m_plan_stmt
		epinErrorMsg   // keep m_error_msg
67
	};
68

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

72
	// query object
73
	Query *m_query;
74

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

78
	// plan object
79
	PlannedStmt *m_plan_stmt;
80

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

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

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

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

93 94
	// ctor
	SOptContext();
95

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

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

103
	// Clone the error message in given context.
J
Jesse Zhang 已提交
104
	CHAR *CloneErrorMsg(struct MemoryContextData *context);
105

106
	// casting function
J
Jesse Zhang 已提交
107
	static SOptContext *Cast(void *ptr);
108

J
Jesse Zhang 已提交
109
};	// struct SOptContext
110

111 112
class COptTasks
{
J
Jesse Zhang 已提交
113 114 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 171 172 173 174 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 217 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 266 267 268 269 270 271
private:
	// context of relcache input and output objects
	struct SContextRelcacheToDXL
	{
		// list of object oids to lookup
		List *m_oid_list;

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

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

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

		// ctor
		SContextRelcacheToDXL(List *oid_list, ULONG cmp_type,
							  const char *filename);

		// casting function
		static SContextRelcacheToDXL *RelcacheConvert(void *ptr);
	};

	// 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_dxl;

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

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

	// 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_dxl_result;

		// casting function
		static SOptimizeMinidumpContext *Cast(void *ptr);
	};

	// execute a task given the argument
	static void Execute(void *(*func)(void *), void *func_arg);

	// map GPOS log severity level to GPDB, print error and delete the given error buffer
	static void LogExceptionMessageAndDelete(
		CHAR *err_buf, ULONG severity_level = CException::ExsevInvalid);

	// task that does the translation from xml to dxl to planned_stmt
	static void *ConvertToPlanStmtFromDXLTask(void *ptr);

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

	// dump relcache info for an object into DXL
	static void *ConvertToDXLFromMDObjsTask(void *ptr);

	// dump metadata about cast objects from relcache to a string in DXL format
	static void *ConvertToDXLFromMDCast(void *ptr);

	// dump metadata about scalar comparison objects from relcache to a string in DXL format
	static void *ConvertToDXLFromMDScalarCmp(void *ptr);

	// dump relstats info for an object into DXL
	static void *ConvertToDXLFromRelStatsTask(void *ptr);

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

	// create optimizer configuration object
	static COptimizerConfig *CreateOptimizerConfig(CMemoryPool *mp,
												   ICostModel *cost_model);

	// optimize a query to a physical DXL
	static void *OptimizeTask(void *ptr);

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

	// translate a DXL tree into a planned statement
	static PlannedStmt *ConvertToPlanStmtFromDXL(CMemoryPool *mp,
												 CMDAccessor *md_accessor,
												 const CDXLNode *dxlnode,
												 bool can_set_tag);

	// load search strategy from given path
	static CSearchStageArray *LoadSearchStrategy(CMemoryPool *mp, char *path);

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

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

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

	// generate an instance of optimizer cost model
	static ICostModel *GetCostModel(CMemoryPool *mp, ULONG num_segments);

	// print warning messages for columns with missing statistics
	static void PrintMissingStatsWarning(CMemoryPool *mp,
										 CMDAccessor *md_accessor,
										 IMdIdArray *col_stats,
										 MdidHashSet *phsmdidRel);

public:
	// convert Query->DXL->LExpr->Optimize->PExpr->DXL
	static char *Optimize(Query *query);

	// optimize Query->DXL->LExpr->Optimize->PExpr->DXL->PlannedStmt
	static PlannedStmt *GPOPTOptimizedPlan(
		Query *query, SOptContext *gpopt_context,
		BOOL *
			had_unexpected_failure	// output : set to true if optimizer unexpectedly failed to produce plan
	);

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

	// convert xml string to DXL and to PS
	static PlannedStmt *ConvertToiPlanStmtFromXML(char *xml_string);

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

	// 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 *DumpMDCast(List *oids);

	// dump scalar comparison from relcache to a string in DXL format
	static char *DumpMDScalarCmp(List *oids, char *cmp_type);

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

	// enable/disable a given xforms
	static bool SetXform(char *xform_str, bool should_disable);

	// return comparison type code
	static ULONG GetComparisonType(char *cmp_type);

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

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

J
Jesse Zhang 已提交
274
#endif	// COptTasks_H
275 276

// EOF