1
0
mirror of https://github.com/mariadb-corporation/mariadb-columnstore-engine.git synced 2025-07-30 19:23:07 +03:00

MCOL-597 Fix up Windows Functions

This commit is contained in:
David Hall
2017-03-21 16:27:09 -05:00
parent a3c4503bc9
commit d7da34c249
7 changed files with 576 additions and 396 deletions

View File

@ -230,35 +230,10 @@
<F N="simplecolumn.cpp"/> <F N="simplecolumn.cpp"/>
<F N="simplefilter.cpp"/> <F N="simplefilter.cpp"/>
<F N="simplescalarfilter.cpp"/> <F N="simplescalarfilter.cpp"/>
<F N="tdriver-oid.cpp"/>
<F N="tdriver-sm.cpp"/>
<F N="tdriver.cpp"/>
<F N="tdriver1.cpp"/>
<F N="tdriver10.cpp"/>
<F N="tdriver11.cpp"/>
<F N="tdriver12.cpp"/>
<F N="tdriver13.cpp"/>
<F N="tdriver14.cpp"/>
<F N="tdriver15.cpp"/>
<F N="tdriver16.cpp"/>
<F N="tdriver17.cpp"/>
<F N="tdriver18.cpp"/>
<F N="tdriver19.cpp"/>
<F N="tdriver2.cpp"/>
<F N="tdriver20.cpp"/>
<F N="tdriver21.cpp"/>
<F N="tdriver22.cpp"/>
<F N="tdriver23.cpp"/>
<F N="tdriver3.cpp"/>
<F N="tdriver4.cpp"/>
<F N="tdriver5.cpp"/>
<F N="tdriver6.cpp"/>
<F N="tdriver7.cpp"/>
<F N="tdriver8.cpp"/>
<F N="tdriver9.cpp"/>
<F N="treenode.cpp"/> <F N="treenode.cpp"/>
<F N="treenodeimpl.cpp"/> <F N="treenodeimpl.cpp"/>
<F N="vendorexecutionplan.cpp"/> <F N="vendorexecutionplan.cpp"/>
<F N="windowfunctioncolumn.cpp"/>
</Folder> </Folder>
<Folder <Folder
Name="Header Files" Name="Header Files"
@ -308,6 +283,7 @@
<F N="treenode.h"/> <F N="treenode.h"/>
<F N="treenodeimpl.h"/> <F N="treenodeimpl.h"/>
<F N="vendorexecutionplan.h"/> <F N="vendorexecutionplan.h"/>
<F N="windowfunctioncolumn.h"/>
</Folder> </Folder>
<Folder <Folder
Name="Resource Files" Name="Resource Files"

View File

@ -41,7 +41,7 @@ class ByteStream;
*/ */
namespace execplan { namespace execplan {
// This enum is made consistant with mysql item_func_window // This enum is made consistant with mysql Item_window_func
enum WF_FRAME enum WF_FRAME
{ {
WF_PRECEDING = 0, WF_PRECEDING = 0,

View File

@ -21,7 +21,7 @@
*/ */
/** @file */ /** @file */
//#define DEBUG_WALK_COND #define DEBUG_WALK_COND
#include <my_config.h> #include <my_config.h>
#include <string> #include <string>
#include <iostream> #include <iostream>
@ -38,7 +38,6 @@
#include <cerrno> #include <cerrno>
#include <cstring> #include <cstring>
#include <time.h> #include <time.h>
//#define NDEBUG
#include <cassert> #include <cassert>
#include <vector> #include <vector>
#include <map> #include <map>
@ -699,13 +698,11 @@ void debug_walk(const Item *item, void *arg)
cout << ": <NULL>" << endl; cout << ": <NULL>" << endl;
break; break;
} }
#if 0
case Item::WINDOW_FUNC_ITEM: case Item::WINDOW_FUNC_ITEM:
{ {
cout << "Window Function Item" << endl; cout << "Window Function Item" << endl;
break; break;
} }
#endif
default: default:
{ {
cout << "UNKNOWN_ITEM type " << item->type() << endl; cout << "UNKNOWN_ITEM type " << item->type() << endl;
@ -2464,11 +2461,11 @@ ReturnedColumn* buildReturnedColumn(Item* item, gp_walk_info& gwi, bool& nonSupp
rc = new ConstantColumn(valStr); rc = new ConstantColumn(valStr);
break; break;
} }
#if 0
case Item::WINDOW_FUNC_ITEM: case Item::WINDOW_FUNC_ITEM:
{ {
return buildWindowFunctionColumn(item, gwi, nonSupport); return buildWindowFunctionColumn(item, gwi, nonSupport);
} }
#if INTERVAL_ITEM
case Item::INTERVAL_ITEM: case Item::INTERVAL_ITEM:
{ {
Item_interval* interval = (Item_interval*)item; Item_interval* interval = (Item_interval*)item;
@ -4437,17 +4434,15 @@ void gp_walk(const Item *item, void *arg)
gwip->rcWorkStack.push(buildReturnedColumn(itp, *gwip, gwip->fatalParseError)); gwip->rcWorkStack.push(buildReturnedColumn(itp, *gwip, gwip->fatalParseError));
break; break;
} }
#if 0
case Item::WINDOW_FUNC_ITEM: case Item::WINDOW_FUNC_ITEM:
{ {
gwip->hasWindowFunc = true; gwip->hasWindowFunc = true;
Item_func_window* ifa = (Item_func_window*)item; Item_window_func* ifa = (Item_window_func*)item;
ReturnedColumn* af = buildWindowFunctionColumn(ifa, *gwip, gwip->fatalParseError); ReturnedColumn* af = buildWindowFunctionColumn(ifa, *gwip, gwip->fatalParseError);
if (af) if (af)
gwip->rcWorkStack.push(af); gwip->rcWorkStack.push(af);
break; break;
} }
#endif
case Item::COPY_STR_ITEM: case Item::COPY_STR_ITEM:
printf("********** received COPY_STR_ITEM *********\n"); printf("********** received COPY_STR_ITEM *********\n");
break; break;
@ -4631,11 +4626,9 @@ void parse_item (Item *item, vector<Item_field*>& field_vec, bool& hasNonSupport
setError(item->thd(), ER_CHECK_NOT_IMPLEMENTED, parseErrorText); setError(item->thd(), ER_CHECK_NOT_IMPLEMENTED, parseErrorText);
break; break;
} }
#if 0
case Item::WINDOW_FUNC_ITEM: case Item::WINDOW_FUNC_ITEM:
parseInfo |= AF_BIT; parseInfo |= AF_BIT;
break; break;
#endif
default: default:
break; break;
} }
@ -5525,7 +5518,6 @@ int getSelectPlan(gp_walk_info& gwi, SELECT_LEX& select_lex, SCSEP& csep, bool i
setError(gwi.thd, ER_CHECK_NOT_IMPLEMENTED, gwi.parseErrorText, gwi); setError(gwi.thd, ER_CHECK_NOT_IMPLEMENTED, gwi.parseErrorText, gwi);
return ER_CHECK_NOT_IMPLEMENTED; return ER_CHECK_NOT_IMPLEMENTED;
} }
#if 0
case Item::WINDOW_FUNC_ITEM: case Item::WINDOW_FUNC_ITEM:
{ {
SRCP srcp(buildWindowFunctionColumn(item, gwi, gwi.fatalParseError)); SRCP srcp(buildWindowFunctionColumn(item, gwi, gwi.fatalParseError));
@ -5539,7 +5531,6 @@ int getSelectPlan(gp_walk_info& gwi, SELECT_LEX& select_lex, SCSEP& csep, bool i
gwi.returnedCols.push_back(srcp); gwi.returnedCols.push_back(srcp);
break; break;
} }
#endif
default: default:
{ {
break; break;
@ -5692,7 +5683,6 @@ int getSelectPlan(gp_walk_info& gwi, SELECT_LEX& select_lex, SCSEP& csep, bool i
// check if window functions are in order by. InfiniDB process order by list if // check if window functions are in order by. InfiniDB process order by list if
// window functions are involved, either in order by or projection. // window functions are involved, either in order by or projection.
#if 0
bool hasWindowFunc = gwi.hasWindowFunc; bool hasWindowFunc = gwi.hasWindowFunc;
gwi.hasWindowFunc = false; gwi.hasWindowFunc = false;
for (; groupcol; groupcol= groupcol->next) for (; groupcol; groupcol= groupcol->next)
@ -5708,7 +5698,6 @@ int getSelectPlan(gp_walk_info& gwi, SELECT_LEX& select_lex, SCSEP& csep, bool i
return ER_CHECK_NOT_IMPLEMENTED; return ER_CHECK_NOT_IMPLEMENTED;
} }
gwi.hasWindowFunc = hasWindowFunc; gwi.hasWindowFunc = hasWindowFunc;
#endif
groupcol = reinterpret_cast<ORDER*>(select_lex.group_list.first); groupcol = reinterpret_cast<ORDER*>(select_lex.group_list.first);
for (; groupcol; groupcol= groupcol->next) for (; groupcol; groupcol= groupcol->next)
@ -5954,13 +5943,11 @@ int getSelectPlan(gp_walk_info& gwi, SELECT_LEX& select_lex, SCSEP& csep, bool i
// check if window functions are in order by. InfiniDB process order by list if // check if window functions are in order by. InfiniDB process order by list if
// window functions are involved, either in order by or projection. // window functions are involved, either in order by or projection.
#if 0
for (; ordercol; ordercol= ordercol->next) for (; ordercol; ordercol= ordercol->next)
{ {
if ((*(ordercol->item))->type() == Item::WINDOW_FUNC_ITEM) if ((*(ordercol->item))->type() == Item::WINDOW_FUNC_ITEM)
gwi.hasWindowFunc = true; gwi.hasWindowFunc = true;
} }
#endif
// re-visit the first of ordercol list // re-visit the first of ordercol list
ordercol = reinterpret_cast<ORDER*>(order_list.first); ordercol = reinterpret_cast<ORDER*>(order_list.first);

View File

@ -3017,7 +3017,7 @@ int ha_calpont_impl_rnd_next(uchar *buf, TABLE* table)
rc = fetchNextRow(buf, ti, ci); rc = fetchNextRow(buf, ti, ci);
} catch (std::exception& e) } catch (std::exception& e)
{ {
string emsg = string("Lost connection to ExeMgr while fetching: ") + e.what(); string emsg = string("Error while fetching from ExeMgr: ") + e.what();
setError(thd, ER_INTERNAL_ERROR, emsg); setError(thd, ER_INTERNAL_ERROR, emsg);
CalpontSystemCatalog::removeCalpontSystemCatalog(tid2sid(thd->thread_id)); CalpontSystemCatalog::removeCalpontSystemCatalog(tid2sid(thd->thread_id));
return ER_INTERNAL_ERROR; return ER_INTERNAL_ERROR;

View File

@ -63,26 +63,26 @@ ReturnedColumn* nullOnError(gp_walk_info& gwi)
return NULL; return NULL;
} }
#if 0 WF_FRAME frame(Window_frame_bound::Bound_precedence_type bound, Item* offset)
WF_FRAME frame(BOUND& bound)
{ {
switch (bound) switch (bound)
{ {
case PRECEDING: case Window_frame_bound::PRECEDING:
if (offset)
return WF_PRECEDING; return WF_PRECEDING;
case FOLLOWING: else
return WF_FOLLOWING;
case UNBOUNDED_PRECEDING:
return WF_UNBOUNDED_PRECEDING; return WF_UNBOUNDED_PRECEDING;
case UNBOUNDED_FOLLOWING: case Window_frame_bound::FOLLOWING:
if (offset)
return WF_FOLLOWING;
else
return WF_UNBOUNDED_FOLLOWING; return WF_UNBOUNDED_FOLLOWING;
case CURRENT_ROW: case Window_frame_bound::CURRENT: // Offset is meaningless
return WF_CURRENT_ROW; return WF_CURRENT_ROW;
default: default:
return WF_UNKNOWN; return WF_UNKNOWN;
} }
} }
#endif
ReturnedColumn* buildBoundExp(WF_Boundary& bound, SRCP& order, gp_walk_info& gwi) ReturnedColumn* buildBoundExp(WF_Boundary& bound, SRCP& order, gp_walk_info& gwi)
{ {
if (!(gwi.thd->infinidb_vtable.cal_conn_info)) if (!(gwi.thd->infinidb_vtable.cal_conn_info))
@ -178,6 +178,96 @@ ReturnedColumn* buildBoundExp(WF_Boundary& bound, SRCP& order, gp_walk_info& gwi
return rc; return rc;
} }
// Since columnstore implemented Windows Functions before MariaDB, we need
// map from the enum MariaDB uses to the string that columnstore uses to
// identify the function type.
string ConvertFuncName(Item_sum* item)
{
switch (item->sum_func())
{
case Item_sum::COUNT_FUNC:
if (!item->arguments()[0]->name)
return "COUNT(*)";
return "COUNT";
break;
case Item_sum::COUNT_DISTINCT_FUNC:
return "COUNT_DISTINCT";
break;
case Item_sum::SUM_FUNC:
return "SUM";
break;
case Item_sum::SUM_DISTINCT_FUNC:
return "SUM_DISTINCT";
break;
case Item_sum::AVG_FUNC:
return "AVG";
break;
case Item_sum::AVG_DISTINCT_FUNC:
return "AVG_DISTINCT";
break;
case Item_sum::MIN_FUNC:
return "MIN";
break;
case Item_sum::MAX_FUNC:
return "MAX";
break;
case Item_sum::STD_FUNC:
return "STDDEV_POP";
break;
case Item_sum::VARIANCE_FUNC:
return "VAR_POP";
break;
case Item_sum::SUM_BIT_FUNC:
if (strcmp(item->func_name(), "bit_or(") == 0)
return "BIT_OR";
if (strcmp(item->func_name(), "bit_and(") == 0)
return "BIT_AND";
if (strcmp(item->func_name(), "bit_xor(") == 0)
return "BIT_XOR";
break;
case Item_sum::UDF_SUM_FUNC:
return "UDF_SUM_FUNC"; // Not supported
break;
case Item_sum::GROUP_CONCAT_FUNC:
return "GROUP_CONCAT"; // Not supported
break;
case Item_sum::ROW_NUMBER_FUNC:
return "ROW_NUMBER";
break;
case Item_sum::RANK_FUNC:
return "RANK";
break;
case Item_sum::DENSE_RANK_FUNC:
return "DENSE_RANK";
break;
case Item_sum::PERCENT_RANK_FUNC:
return "PERCENT_RANK";
break;
case Item_sum::CUME_DIST_FUNC:
return "CUME_DIST";
break;
case Item_sum::NTILE_FUNC:
return "NTILE";
break;
case Item_sum::FIRST_VALUE_FUNC:
return "FIRST_VALUE";
break;
case Item_sum::LAST_VALUE_FUNC:
return "LAST_VALUE";
break;
case Item_sum::NTH_VALUE_FUNC:
return "NTH_VALUE";
break;
case Item_sum::LEAD_FUNC:
return "LEAD";
break;
case Item_sum::LAG_FUNC:
return "LAG";
break;
};
return "";
}
ReturnedColumn* buildWindowFunctionColumn(Item* item, gp_walk_info& gwi, bool& nonSupport) ReturnedColumn* buildWindowFunctionColumn(Item* item, gp_walk_info& gwi, bool& nonSupport)
{ {
//@todo fix print for create view //@todo fix print for create view
@ -186,37 +276,82 @@ ReturnedColumn* buildWindowFunctionColumn(Item* item, gp_walk_info& gwi, bool& n
//cout << str.c_ptr() << endl; //cout << str.c_ptr() << endl;
if (!(gwi.thd->infinidb_vtable.cal_conn_info)) if (!(gwi.thd->infinidb_vtable.cal_conn_info))
gwi.thd->infinidb_vtable.cal_conn_info = (void*)(new cal_connection_info()); gwi.thd->infinidb_vtable.cal_conn_info = (void*)(new cal_connection_info());
// cal_connection_info* ci = reinterpret_cast<cal_connection_info*>(gwi.thd->infinidb_vtable.cal_conn_info); cal_connection_info* ci = reinterpret_cast<cal_connection_info*>(gwi.thd->infinidb_vtable.cal_conn_info);
gwi.hasWindowFunc = true; gwi.hasWindowFunc = true;
// Item_func_window* wf = (Item_func_window*)item; Item_window_func* wf = (Item_window_func*)item;
string funcName /*= wf->func_name()*/; string funcName = ConvertFuncName(wf->window_func());
WindowFunctionColumn* ac = new WindowFunctionColumn(funcName); WindowFunctionColumn* ac = new WindowFunctionColumn(funcName);
// ac->distinct(wf->isDistinct()); ac->distinct(wf->window_func()->has_with_distinct());
// Window_context *wf_ctx = wf->window_ctx(); Window_spec *win_spec = wf->window_spec;
SRCP srcp; SRCP srcp;
#if 0
// arguments // arguments
vector<SRCP> funcParms; vector<SRCP> funcParms;
for (uint32_t i = 0; i < wf->argument_count(); i++) Item_sum* item_sum = (Item_sum*)wf->arguments()[0];
for (uint32_t i = 0; i < item_sum->argument_count(); i++)
{ {
srcp.reset(buildReturnedColumn(wf->arguments()[i], gwi, nonSupport)); srcp.reset(buildReturnedColumn((item_sum->arguments()[i]), gwi, nonSupport));
if (!srcp) if (!srcp)
return nullOnError(gwi); return nullOnError(gwi);
funcParms.push_back(srcp); funcParms.push_back(srcp);
if (gwi.clauseType == WHERE && !gwi.rcWorkStack.empty()) if (gwi.clauseType == WHERE && !gwi.rcWorkStack.empty())
gwi.rcWorkStack.pop(); gwi.rcWorkStack.pop();
} }
// Some functions, such as LEAD/LAG don't have all parameters implemented in the
// front end. Add dummies here to make the backend use defaults.
// Some of these will be temporary until they are implemented in the front end.
// Others need to stay because the back end expects them, but the front end
// no longer sends them.
// This case is kept in enum order in hopes the compiler can optimize
switch (wf->window_func()->sum_func())
{
case Item_sum::COUNT_FUNC:
case Item_sum::COUNT_DISTINCT_FUNC:
break;
case Item_sum::FIRST_VALUE_FUNC:
srcp.reset(new ConstantColumn("1", (uint64_t)1, ConstantColumn::NUM)); // OFFSET (always one)
funcParms.push_back(srcp);
srcp.reset(new ConstantColumn("1", (uint64_t)1, ConstantColumn::NUM)); // FROM_FIRST
funcParms.push_back(srcp);
srcp.reset(new ConstantColumn("1", (uint64_t)1, ConstantColumn::NUM)); // IGNORE/RESPECT NULLS. 1 => RESPECT
funcParms.push_back(srcp);
break;
case Item_sum::LAST_VALUE_FUNC:
srcp.reset(new ConstantColumn("1", (uint64_t)1, ConstantColumn::NUM)); // OFFSET (always one)
funcParms.push_back(srcp);
srcp.reset(new ConstantColumn("0", (uint64_t)0, ConstantColumn::NUM)); // FROM_LAST
funcParms.push_back(srcp);
srcp.reset(new ConstantColumn("1", (uint64_t)1, ConstantColumn::NUM)); // IGNORE/RESPECT NULLS. 1 => RESPECT
funcParms.push_back(srcp);
break;
case Item_sum::NTH_VALUE_FUNC:
// When the front end supports these paramters, this needs modification
srcp.reset(new ConstantColumn("1", (uint64_t)1, ConstantColumn::NUM)); // FROM FIRST/LAST 1 => FIRST
funcParms.push_back(srcp);
srcp.reset(new ConstantColumn("1", (uint64_t)1, ConstantColumn::NUM)); // IGNORE/RESPECT NULLS. 1 => RESPECT
funcParms.push_back(srcp);
break;
case Item_sum::LEAD_FUNC:
case Item_sum::LAG_FUNC:
// When the front end supports these paramters, this needs modification
srcp.reset(new ConstantColumn("", ConstantColumn::NULLDATA)); // Default to fill in for NULL values
funcParms.push_back(srcp);
srcp.reset(new ConstantColumn("1", (uint64_t)1, ConstantColumn::NUM)); // IGNORE/RESPECT NULLS. 1 => RESPECT
funcParms.push_back(srcp);
break;
default:
break;
};
ac->functionParms(funcParms); ac->functionParms(funcParms);
// Partition by // Partition by
if (wf_ctx) if (win_spec)
{ {
vector<SRCP> partitions; vector<SRCP> partitions;
for (uint32_t i = 0; i < wf_ctx->partition_count; i++) for (ORDER *ord= win_spec->partition_list->first; ord; ord=ord->next)
{ {
srcp.reset(buildReturnedColumn(wf_ctx->partitions[i], gwi, nonSupport)); srcp.reset(buildReturnedColumn(*ord->item, gwi, nonSupport));
if (!srcp) if (!srcp)
return nullOnError(gwi); return nullOnError(gwi);
partitions.push_back(srcp); partitions.push_back(srcp);
@ -224,22 +359,23 @@ ReturnedColumn* buildWindowFunctionColumn(Item* item, gp_walk_info& gwi, bool& n
ac->partitions(partitions); ac->partitions(partitions);
// Order by // Order by
if (wf_ctx->ordering) if (win_spec->order_list)
{ {
WF_OrderBy orderBy; WF_OrderBy orderBy;
// order columns // order columns
if (wf_ctx->ordering->orders) if (win_spec->order_list)
{ {
vector<SRCP> orders; vector<SRCP> orders;
ORDER* orderCol = reinterpret_cast<ORDER*>(wf_ctx->ordering->orders->first); ORDER* orderCol = reinterpret_cast<ORDER*>(win_spec->order_list->first);
for (; orderCol; orderCol= orderCol->next) for (; orderCol; orderCol= orderCol->next)
{ {
Item* orderItem = *(orderCol->item); Item* orderItem = *(orderCol->item);
srcp.reset(buildReturnedColumn(orderItem, gwi, nonSupport)); srcp.reset(buildReturnedColumn(orderItem, gwi, nonSupport));
if (!srcp) if (!srcp)
return nullOnError(gwi); return nullOnError(gwi);
srcp->asc(orderCol->asc); srcp->asc(orderCol->direction == ORDER::ORDER_ASC ? true : false);
srcp->nullsFirst(orderCol->nulls); // nulls 1-nulls first 0-nulls last // srcp->nullsFirst(orderCol->nulls); // nulls 2-default, 1-nulls first, 0-nulls last
srcp->nullsFirst(1); // WINDOWS TODO: implement NULLS FIRST/LAST in 10.2 front end
orders.push_back(srcp); orders.push_back(srcp);
} }
orderBy.fOrders = orders; orderBy.fOrders = orders;
@ -247,17 +383,18 @@ ReturnedColumn* buildWindowFunctionColumn(Item* item, gp_walk_info& gwi, bool& n
// window frame // window frame
WF_Frame frm; WF_Frame frm;
if (wf_ctx->ordering->frame) if (win_spec->window_frame)
{ {
frm.fIsRange = wf_ctx->ordering->frame->isRange; frm.fIsRange = win_spec->window_frame->units == Window_frame::UNITS_RANGE;
// start // start
if (wf_ctx->ordering->frame->start) if (win_spec->window_frame->top_bound)
{ {
frm.fStart.fFrame = frame(wf_ctx->ordering->frame->start->bound); frm.fStart.fFrame = frame(win_spec->window_frame->top_bound->precedence_type,
win_spec->window_frame->top_bound->offset); // offset NULL means UNBOUNDED
if (wf_ctx->ordering->frame->start->item) if (win_spec->window_frame->top_bound->offset)
{ {
frm.fStart.fVal.reset(buildReturnedColumn(wf_ctx->ordering->frame->start->item, gwi, nonSupport)); frm.fStart.fVal.reset(buildReturnedColumn(win_spec->window_frame->top_bound->offset, gwi, nonSupport));
if (!frm.fStart.fVal) if (!frm.fStart.fVal)
return nullOnError(gwi); return nullOnError(gwi);
@ -295,12 +432,13 @@ ReturnedColumn* buildWindowFunctionColumn(Item* item, gp_walk_info& gwi, bool& n
} }
// end // end
if (wf_ctx->ordering->frame->end) if (win_spec->window_frame->bottom_bound)
{ {
frm.fEnd.fFrame = frame(wf_ctx->ordering->frame->end->bound); frm.fEnd.fFrame = frame(win_spec->window_frame->bottom_bound->precedence_type,
if (wf_ctx->ordering->frame->end->item) win_spec->window_frame->bottom_bound->offset);
if (win_spec->window_frame->bottom_bound->offset)
{ {
frm.fEnd.fVal.reset(buildReturnedColumn(wf_ctx->ordering->frame->end->item, gwi, nonSupport)); frm.fEnd.fVal.reset(buildReturnedColumn(win_spec->window_frame->bottom_bound->offset, gwi, nonSupport));
if (!frm.fEnd.fVal) if (!frm.fEnd.fVal)
return nullOnError(gwi); return nullOnError(gwi);
@ -409,8 +547,57 @@ ReturnedColumn* buildWindowFunctionColumn(Item* item, gp_walk_info& gwi, bool& n
} }
else else
{ {
// Certain function types have different default boundaries
// This case is kept in enum order in hopes the compiler can optimize
switch (wf->window_func()->sum_func())
{
case Item_sum::COUNT_FUNC:
case Item_sum::COUNT_DISTINCT_FUNC:
case Item_sum::SUM_FUNC:
case Item_sum::SUM_DISTINCT_FUNC:
case Item_sum::AVG_FUNC:
case Item_sum::AVG_DISTINCT_FUNC:
case Item_sum::MIN_FUNC:
case Item_sum::MAX_FUNC:
case Item_sum::STD_FUNC:
case Item_sum::VARIANCE_FUNC:
case Item_sum::SUM_BIT_FUNC:
case Item_sum::UDF_SUM_FUNC:
case Item_sum::GROUP_CONCAT_FUNC:
frm.fStart.fFrame = WF_UNBOUNDED_PRECEDING; frm.fStart.fFrame = WF_UNBOUNDED_PRECEDING;
frm.fEnd.fFrame = WF_CURRENT_ROW; frm.fEnd.fFrame = WF_CURRENT_ROW;
break;
case Item_sum::ROW_NUMBER_FUNC:
case Item_sum::RANK_FUNC:
frm.fStart.fFrame = WF_UNBOUNDED_PRECEDING;
frm.fEnd.fFrame = WF_UNBOUNDED_FOLLOWING;
break;
case Item_sum::DENSE_RANK_FUNC:
case Item_sum::PERCENT_RANK_FUNC:
case Item_sum::CUME_DIST_FUNC:
frm.fStart.fFrame = WF_UNBOUNDED_PRECEDING;
frm.fEnd.fFrame = WF_CURRENT_ROW;
break;
case Item_sum::NTILE_FUNC:
frm.fStart.fFrame = WF_UNBOUNDED_PRECEDING;
frm.fEnd.fFrame = WF_UNBOUNDED_FOLLOWING;
break;
case Item_sum::FIRST_VALUE_FUNC:
case Item_sum::LAST_VALUE_FUNC:
case Item_sum::NTH_VALUE_FUNC:
frm.fStart.fFrame = WF_UNBOUNDED_PRECEDING;
frm.fEnd.fFrame = WF_CURRENT_ROW;
break;
case Item_sum::LEAD_FUNC:
case Item_sum::LAG_FUNC:
frm.fStart.fFrame = WF_UNBOUNDED_PRECEDING;
frm.fEnd.fFrame = WF_UNBOUNDED_FOLLOWING;
break;
default:
frm.fStart.fFrame = WF_UNBOUNDED_PRECEDING;
frm.fEnd.fFrame = WF_CURRENT_ROW;
break;
};
} }
orderBy.fFrame = frm; orderBy.fFrame = frm;
@ -426,8 +613,7 @@ ReturnedColumn* buildWindowFunctionColumn(Item* item, gp_walk_info& gwi, bool& n
return NULL; return NULL;
} }
ac->resultType(colType_MysqlToIDB(wf)); ac->resultType(colType_MysqlToIDB(wf->arguments()[0]));
// bug5736. Make the result type double for some window functions when // bug5736. Make the result type double for some window functions when
// infinidb_double_for_decimal_math is set. // infinidb_double_for_decimal_math is set.
ac->adjustResultType(); ac->adjustResultType();
@ -438,7 +624,6 @@ ReturnedColumn* buildWindowFunctionColumn(Item* item, gp_walk_info& gwi, bool& n
// put ac on windowFuncList // put ac on windowFuncList
gwi.windowFuncList.push_back(ac); gwi.windowFuncList.push_back(ac);
#endif
return ac; return ac;
} }

View File

@ -65,6 +65,7 @@ template <class T> bool isnan(T);
#include "sql_table.h" #include "sql_table.h"
#include "sql_select.h" #include "sql_select.h"
#include "item_windowfunc.h"
// Now clean up the pollution as best we can... // Now clean up the pollution as best we can...
#undef min #undef min

View File

@ -121,6 +121,11 @@ void WF_lead_lag<T>::parseParms(const std::vector<execplan::SRCP>& parms)
{ {
// lead | lag // lead | lag
fLead = 1; fLead = 1;
fRespectNulls = true;
fDefNull = false;
fDefault = (T)0;
fOffsetNull = false;
fOffset = 0;
if (fFunctionId == WF__LAG) if (fFunctionId == WF__LAG)
fLead = -1; fLead = -1;
@ -148,6 +153,32 @@ void WF_lead_lag<T>::parseParms(const std::vector<execplan::SRCP>& parms)
idbassert(cc != NULL); idbassert(cc != NULL);
bool isNull = false; // dummy, harded coded bool isNull = false; // dummy, harded coded
fRespectNulls = (cc->getIntVal(fRow, isNull) > 0); fRespectNulls = (cc->getIntVal(fRow, isNull) > 0);
#if 0
// parms[1]: offset
for (std::vector<execplan::SRCP>::size_type i = 1/*ignore 0th element*/; i < parms.size(); ++i)
{
ConstantColumn* cc = dynamic_cast<ConstantColumn*>(parms[i].get());
if (cc != NULL)
{
fOffset = cc->getIntVal(fRow, fOffsetNull) * fLead; // row not used, no need to setData.
continue;
}
cc = dynamic_cast<ConstantColumn*>(parms[i].get());
if (cc != NULL)
{
getConstValue(cc, fDefault, fDefNull);
continue;
}
// IGNORE/RESPECT nulls is currently broken in the front end
cc = dynamic_cast<ConstantColumn*>(parms[i].get());
if (cc != NULL)
{
bool isNull = false; // dummy, harded coded
fRespectNulls = (cc->getIntVal(fRow, isNull) > 0);
}
}
#endif
} }