Github user interma commented on a diff in the pull request:

    https://github.com/apache/incubator-hawq/pull/1345#discussion_r174721584
  
    --- Diff: contrib/vexecutor/vadt.c ---
    @@ -0,0 +1,388 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *   http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing,
    + * software distributed under the License is distributed on an
    + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    + * KIND, either express or implied.  See the License for the
    + * specific language governing permissions and limitations
    + * under the License.
    + */
    +
    +#include "postgres.h"
    +#include "catalog/pg_type.h"
    +#include "utils/builtins.h"
    +#include "vadt.h"
    +
    +/* Get the size of vectorized data */
    +#define FUNCTION_VTYPESIZE(type) \
    +size_t v##type##Size(vheader *vh) \
    +{\
    +   size_t len = offsetof(vheader,isnull);\
    +   return  len + vh->dim * sizeof(bool) + vh->dim * sizeof(type);\
    +}
    +
    +/* Build the vectorized data */
    +#define FUNCTION_BUILD(type, typeoid) \
    +vheader* buildv##type(int n) \
    +{ \
    +   vheader* result; \
    +   result = (vheader*) palloc0(offsetof(v##type,values) + n * 
sizeof(type)); \
    +   result->dim = n; \
    +   result->elemtype = typeoid; \
    +   result->isnull = palloc(sizeof(bool) * n); \
    +   SET_VARSIZE(result,v##type##Size(result)); \
    +   return result; \
    +}
    +
    +/* Destroy the vectorized data */
    +#define FUNCTION_DESTORY(type, typeoid) \
    +void destoryv##type(v##header **header) \
    +{ \
    +   v##type** ptr = (v##type**) header; \
    +   free((*header)->isnull); \
    +   free((*ptr)->values); \
    +   free(*ptr); \
    +   *ptr = NULL; \
    +}
    +
    +/*
    + * Serialize functions and deserialize functions for the abstract data 
types
    + */
    +#define FUNCTION_SERIALIZATION(type,typeoid) \
    +size_t v##type##serialization(vheader* vh,unsigned char *buf) \
    +{ \
    +   size_t len = 0; \
    +   v##type *vt = (v##type *)vh; \
    + \
    +   memcpy(buf + len,vt,offsetof(vheader,isnull)); \
    +   len += offsetof(vheader,isnull); \
    + \
    +   memcpy(buf + len,vh->isnull,sizeof(bool) * vh->dim); \
    +   len += sizeof(bool) * vh->dim; \
    + \
    +   memcpy(buf + len,vt->values,sizeof(type) * vh->dim); \
    +   len += sizeof(type) * vh->dim; \
    + \
    +   return len; \
    +} \
    +\
    +\
    +Datum v##type##deserialization(unsigned char* buf,size_t* len) \
    +{\
    +   vheader* vh = (vheader *) buf;\
    +   *len = 0;\
    +   \
    +   v##type* vt = palloc(sizeof(v##type) + sizeof(type) * vh->dim);\
    +   memcpy(vt,buf,offsetof(vheader,isnull));\
    +   *len += offsetof(vheader,isnull);\
    +   \
    +   vt->header.isnull = palloc(vh->dim * sizeof(bool));\
    +   memcpy(vt->header.isnull, buf + *len,vh->dim * sizeof(bool));\
    +   *len += vh->dim * sizeof(bool);\
    +   \
    +   memcpy(vt->values,buf + *len,vh->dim * sizeof(type)); \
    +   *len += vh->dim * sizeof(type); \
    +   \
    +   return PointerGetDatum(vt); \
    +}
    +
    +/*
    + * IN function for the abstract data types
    + * e.g. Datum vint2in(PG_FUNCTION_ARGS)
    + */
    +#define FUNCTION_IN(type, typeoid, MACRO_DATUM) \
    +PG_FUNCTION_INFO_V1(v##type##in); \
    +Datum \
    +v##type##in(PG_FUNCTION_ARGS) \
    +{ \
    +    char *intString = PG_GETARG_CSTRING(0); \
    +    v##type *res = NULL; \
    +    char tempstr[MAX_VECTOR_SIZE] = {0}; \
    +    int n = 0; \
    +    res = palloc0(offsetof(v##type, values) + (MAX_VECTOR_SIZE) * 
sizeof(type)); \
    +    for (n = 0; *intString && n < MAX_VECTOR_SIZE; n++) \
    +    { \
    +               char *start = NULL;\
    +        while (*intString && isspace((unsigned char) *intString)) \
    +            intString++; \
    +        if (*intString == '\0') \
    +            break; \
    +        start = intString; \
    +        while ((*intString && !isspace((unsigned char) *intString)) && 
*intString != '\0') \
    +            intString++; \
    +        Assert(intString - start < MAX_VECTOR_SIZE); \
    +        strncpy(tempstr, start, intString - start); \
    +        tempstr[intString - start] = 0; \
    +        res->values[n] =  MACRO_DATUM(DirectFunctionCall1(type##in, 
CStringGetDatum(tempstr))); \
    +        while (*intString && !isspace((unsigned char) *intString)) \
    +            intString++; \
    +    } \
    +    while (*intString && isspace((unsigned char) *intString)) \
    +        intString++; \
    +    if (*intString) \
    +        ereport(ERROR, \
    +        (errcode(ERRCODE_INVALID_PARAMETER_VALUE), \
    +                errmsg("int2vector has too many elements"))); \
    +    SET_VARSIZE(res, (offsetof(v##type, values) + n * sizeof(type))); \
    +    res->header.elemtype = typeoid; \
    +    res->header.dim = n; \
    +    PG_RETURN_POINTER(res); \
    +}
    +
    +/*
    + * OUT function for the abstract data types
    + * e.g. Datum vint2out(PG_FUNCTION_ARGS)
    + */
    +#define FUNCTION_OUT(type, typeoid, MACRO_DATUM) \
    +PG_FUNCTION_INFO_V1(v##type##out); \
    +Datum \
    +v##type##out(PG_FUNCTION_ARGS) \
    +{ \
    +   v##type * arg1 = (v##type *) PG_GETARG_POINTER(0); \
    +   int len = arg1->header.dim; \
    +    int i = 0; \
    +   char *rp; \
    +   char *result; \
    +   rp = result = (char *) palloc0(len * 16 + 1); \
    +   for (i = 0; i < len; i++) \
    +   { \
    +           if (i != 0) \
    +                   *rp++ = ' '; \
    +           strcat(rp, DatumGetCString(DirectFunctionCall1(type##out, 
MACRO_DATUM(arg1->values[i]))));\
    --- End diff --
    
    strncat() is better.



---

Reply via email to