Skip to content

Commit

Permalink
[columnar] Vectorized clauses should work also as CONST = VAR
Browse files Browse the repository at this point in the history
* Vectorized clauses should work in both directions. When comparing
  VAR = CONST and CONST = VAR. Introduced vectorized arg type which will
  wrap either vector column or constant.
  • Loading branch information
mkaruza authored and wuputah committed Nov 30, 2022
1 parent 3370bf9 commit a470460
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,9 @@ ConstructVectorizedQualList(TupleTableSlot *slot, List *vectorizedQual)

newVectorQual->u.expr.fmgrInfo = palloc0(sizeof(FmgrInfo));
newVectorQual->u.expr.fcInfo = palloc0(SizeForFunctionCallInfo(nargs));
newVectorQual->u.expr.vectorFnArguments =
(VectorFnArgument *) palloc0(sizeof(VectorFnArgument) * nargs);


fmgr_info(opExprNode->opfuncid, newVectorQual->u.expr.fmgrInfo);
fmgr_info_set_expr((Node *) node, newVectorQual->u.expr.fmgrInfo);
Expand All @@ -283,18 +286,28 @@ ConstructVectorizedQualList(TupleTableSlot *slot, List *vectorizedQual)
{
Expr *arg = (Expr *) lfirst(lcOpExprArgs);

VectorFnArgument *vectorFnArgument =
newVectorQual->u.expr.vectorFnArguments + argno;

if (IsA(arg, Const))
{
Const *con = (Const *) arg;

newVectorQual->u.expr.fcInfo->args[argno].value = con->constvalue;
vectorFnArgument->type = VECTOR_FN_ARG_CONSTANT;
vectorFnArgument->arg = con->constvalue;

newVectorQual->u.expr.fcInfo->args[argno].value = (Datum) vectorFnArgument;
newVectorQual->u.expr.fcInfo->args[argno].isnull = con->constisnull;
}
else if (IsA(arg, Var))
{
Var *variable = (Var *) arg;
int columnIdx = variable->varattno - 1;
newVectorQual->u.expr.fcInfo->args[argno].value = vectorSlot->tts.tts_values[columnIdx];

vectorFnArgument->type = VECTOR_FN_ARG_VAR;
vectorFnArgument->arg = vectorSlot->tts.tts_values[columnIdx];

newVectorQual->u.expr.fcInfo->args[argno].value = (Datum) vectorFnArgument;
newVectorQual->u.expr.fcInfo->args[argno].isnull = false;
}

Expand Down
4 changes: 2 additions & 2 deletions columnar/src/backend/columnar/vectorization/types/date.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "columnar/vectorization/types/types.h"

// date (int32)
BUILD_CMP_OPERATOR_INT(date_, DateADT, DateADT, PG_GETARG_POINTER, PG_GETARG_DATEADT)
BUILD_CMP_OPERATOR_INT(date_, DateADT, DateADT)

// time (int64)
BUILD_CMP_OPERATOR_INT(time_, TimeADT, TimeADT, PG_GETARG_POINTER, PG_GETARG_TIMEADT)
BUILD_CMP_OPERATOR_INT(time_, TimeADT, TimeADT)
20 changes: 10 additions & 10 deletions columnar/src/backend/columnar/vectorization/types/int.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@
#include "columnar/vectorization/types/types.h"

// char
BUILD_CMP_OPERATOR_INT( char, char, char, PG_GETARG_POINTER, PG_GETARG_CHAR)
BUILD_CMP_OPERATOR_INT( char, char, char)

// int2
BUILD_CMP_OPERATOR_INT( int2, int16, int16, PG_GETARG_POINTER, PG_GETARG_INT16)
BUILD_CMP_OPERATOR_INT(int24, int16, int32, PG_GETARG_POINTER, PG_GETARG_INT32)
BUILD_CMP_OPERATOR_INT(int28, int16, int64, PG_GETARG_POINTER, PG_GETARG_INT64)
BUILD_CMP_OPERATOR_INT( int2, int16, int16)
BUILD_CMP_OPERATOR_INT(int24, int16, int32)
BUILD_CMP_OPERATOR_INT(int28, int16, int64)

// int4
BUILD_CMP_OPERATOR_INT( int4, int32, int32, PG_GETARG_POINTER, PG_GETARG_INT32)
BUILD_CMP_OPERATOR_INT(int42, int32, int16, PG_GETARG_POINTER, PG_GETARG_INT16)
BUILD_CMP_OPERATOR_INT(int48, int32, int64, PG_GETARG_POINTER, PG_GETARG_INT64)
BUILD_CMP_OPERATOR_INT( int4, int32, int32)
BUILD_CMP_OPERATOR_INT(int42, int32, int16)
BUILD_CMP_OPERATOR_INT(int48, int32, int64)

// int8
BUILD_CMP_OPERATOR_INT( int8, int64, int64, PG_GETARG_POINTER, PG_GETARG_INT64)
BUILD_CMP_OPERATOR_INT(int82, int64, int16, PG_GETARG_POINTER, PG_GETARG_INT16)
BUILD_CMP_OPERATOR_INT(int84, int64, int32, PG_GETARG_POINTER, PG_GETARG_INT32)
BUILD_CMP_OPERATOR_INT( int8, int64, int64)
BUILD_CMP_OPERATOR_INT(int82, int64, int16)
BUILD_CMP_OPERATOR_INT(int84, int64, int32)
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,21 @@ typedef enum VectorQualType
VECTOR_QUAL_EXPR
} VectorQualTypeEnum;


typedef enum VectorArgType
{
VECTOR_FN_ARG_CONSTANT,
VECTOR_FN_ARG_VAR
} VectorFnArgTypeEnum;


typedef struct VectorFnArgument
{
VectorFnArgTypeEnum type;
Datum arg;
} VectorFnArgument;


typedef struct VectorQual
{
VectorQualTypeEnum vectorQualType;
Expand All @@ -66,6 +81,7 @@ typedef struct VectorQual
{
FmgrInfo *fmgrInfo;
FunctionCallInfo fcInfo;
VectorFnArgument *vectorFnArguments;
} expr;
struct
{
Expand Down
72 changes: 54 additions & 18 deletions columnar/src/include/columnar/vectorization/types/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,70 @@

#include "columnar/vectorization/columnar_vector_types.h"

#define _BUILD_CMP_OP_INT(FNAME, LTYPE, RTYPE, LCAST, RCAST, OPSYM, OPSTR) \
#define _BUILD_CMP_OP_INT(FNAME, LTYPE, RTYPE, OPSYM, OPSTR) \
PG_FUNCTION_INFO_V1(v##FNAME##OPSTR); \
Datum v##FNAME##OPSTR(PG_FUNCTION_ARGS) \
{ \
VectorColumn *arg1 = (VectorColumn*) LCAST(0); \
RTYPE arg2 = RCAST(1); \
VectorColumn *res = BuildVectorColumn(arg1->dimension, 1, true); \
VectorFnArgument *left = (VectorFnArgument *) PG_GETARG_POINTER(0); \
VectorFnArgument *right = (VectorFnArgument *) PG_GETARG_POINTER(1); \
\
VectorColumn *vectorColumn = NULL; \
VectorColumn *res = NULL; \
int i = 0; \
LTYPE *arg1Idx = (LTYPE *) arg1->value; \
bool *resIdx = (bool *) res->value; \
\
for (i = 0; i < arg1->dimension; i++) \
if (left->type == VECTOR_FN_ARG_VAR && \
right->type == VECTOR_FN_ARG_CONSTANT) \
{ \
res->isnull[i] = arg1->isnull[i]; \
resIdx[i] = arg1Idx[i] OPSYM arg2; \
vectorColumn = (VectorColumn*) left->arg; \
RTYPE constValue = right->arg; \
\
res = BuildVectorColumn(vectorColumn->dimension, 1, true); \
\
LTYPE *vectorValue = (LTYPE *) vectorColumn->value; \
bool *resIdx = (bool *) res->value; \
\
for (i = 0; i < vectorColumn->dimension; i++) \
{ \
res->isnull[i] = vectorColumn->isnull[i]; \
resIdx[i] = vectorValue[i] OPSYM constValue; \
} \
\
res->dimension = vectorColumn->dimension; \
} \
else if (left->type == VECTOR_FN_ARG_CONSTANT && \
right->type == VECTOR_FN_ARG_VAR) \
{ \
vectorColumn = (VectorColumn*) right->arg; \
LTYPE constValue = left->arg; \
\
res = BuildVectorColumn(vectorColumn->dimension, 1, true); \
\
RTYPE *vectorValue = (RTYPE *) vectorColumn->value; \
bool *resIdx = (bool *) res->value; \
\
for (i = 0; i < vectorColumn->dimension; i++) \
{ \
res->isnull[i] = vectorColumn->isnull[i]; \
resIdx[i] = vectorValue[i] OPSYM constValue; \
} \
\
res->dimension = vectorColumn->dimension; \
} \
else \
{ \
\
} \
\
res->dimension = arg1->dimension; \
PG_RETURN_POINTER(res); \
} \

#define BUILD_CMP_OPERATOR_INT(FNAME, LTYPE, RTYPE, LCAST, RCAST) \
_BUILD_CMP_OP_INT(FNAME, LTYPE, RTYPE, LCAST, RCAST, ==, eq) \
_BUILD_CMP_OP_INT(FNAME, LTYPE, RTYPE, LCAST, RCAST, !=, ne) \
_BUILD_CMP_OP_INT(FNAME, LTYPE, RTYPE, LCAST, RCAST, >, gt) \
_BUILD_CMP_OP_INT(FNAME, LTYPE, RTYPE, LCAST, RCAST, <, lt) \
_BUILD_CMP_OP_INT(FNAME, LTYPE, RTYPE, LCAST, RCAST, <=, le) \
_BUILD_CMP_OP_INT(FNAME, LTYPE, RTYPE, LCAST, RCAST, >=, ge) \
#define BUILD_CMP_OPERATOR_INT(FNAME, LTYPE, RTYPE) \
_BUILD_CMP_OP_INT(FNAME, LTYPE, RTYPE, ==, eq) \
_BUILD_CMP_OP_INT(FNAME, LTYPE, RTYPE, !=, ne) \
_BUILD_CMP_OP_INT(FNAME, LTYPE, RTYPE, >, gt) \
_BUILD_CMP_OP_INT(FNAME, LTYPE, RTYPE, <, lt) \
_BUILD_CMP_OP_INT(FNAME, LTYPE, RTYPE, <=, le) \
_BUILD_CMP_OP_INT(FNAME, LTYPE, RTYPE, >=, ge) \

#endif

#endif

0 comments on commit a470460

Please sign in to comment.