Bison crashes postgresql

Поиск
Список
Период
Сортировка
Искать

Bison crashes postgresql

От:
Werner Echezuria <wercool@gmail.com>
Дата:
Hi, I have a code in which I translate some code from sqlf to sql, but
when it comes to yy_parse the server crashes, I have no idea why,
because it works fine in other situations.

This is the code (the problem is in parse_sqlf, when I call  sqlf_yyparse):

#include "postgres.h"
#include "gram.h"
#include "utils/builtins.h"
#include "funcapi.h"
#include "executor/spi.h"
#include "access/heapam.h"
#include "fmgr.h"
#include "miscadmin.h"

extern Datum sqlf(PG_FUNCTION_ARGS);
char *parse_sqlf();

PG_MODULE_MAGIC;

PG_FUNCTION_INFO_V1(sqlf);

Datum
sqlf(PG_FUNCTION_ARGS)
{   char                *query = text_to_cstring(PG_GETARG_TEXT_PP(0));   char                *sql;   ReturnSetInfo       *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;   Tuplestorestate     *tupstore;   TupleDesc           tupdesc;   int                 call_cntr;   int                 max_calls;   AttInMetadata       *attinmeta;   SPITupleTable       *spi_tuptable;   TupleDesc           spi_tupdesc;   bool		firstpass;   char                *lastrowid;   int                 i;   int                 num_categories;   MemoryContext       per_query_ctx;   MemoryContext       oldcontext;   int                 ret;   int                 proc;
   sql=(char *)palloc(strlen(query)*sizeof(char *));
   sql=parse_sqlf(query);
   /* check to see if caller supports us returning a tuplestore */   if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))           ereport(ERROR,                   (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),                    errmsg("set-valued function called in context
that cannot accept a set")));   if (!(rsinfo->allowedModes & SFRM_Materialize))           ereport(ERROR,                   (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),                    errmsg("materialize mode required, but it is not " \                                   "allowed in this context")));
   per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
   /* Connect to SPI manager */   if ((ret = SPI_connect()) < 0)           /* internal error */           elog(ERROR, "SPI_connect returned %d", ret);

   /* Retrieve the desired rows */   ret = SPI_execute(sql, true, 0);   proc = SPI_processed;
   /* If no qualifying tuples, fall out early */   if (ret != SPI_OK_SELECT || proc <= 0)   {           SPI_finish();           rsinfo->isDone = ExprEndResult;           PG_RETURN_NULL();   }
   spi_tuptable = SPI_tuptable;   spi_tupdesc = spi_tuptable->tupdesc;
   /* get a tuple descriptor for our result type */   switch (get_call_result_type(fcinfo, NULL, &tupdesc))   {           case TYPEFUNC_COMPOSITE:                   /* success */                   break;           case TYPEFUNC_RECORD:                   /* failed to determine actual type of RECORD */                   ereport(ERROR,                           (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),                            errmsg("function returning record called
in context "                                           "that cannot accept type record")));                   break;           default:                   /* result type isn't composite */                   elog(ERROR, "return type must be a row type");                   break;   }

   /*    * switch to long-lived memory context    */   oldcontext = MemoryContextSwitchTo(per_query_ctx);
   /* make sure we have a persistent copy of the result tupdesc */   tupdesc = CreateTupleDescCopy(tupdesc);
   /* initialize our tuplestore in long-lived context */   tupstore =           tuplestore_begin_heap(rsinfo->allowedModes &
SFRM_Materialize_Random,                                                     false, work_mem);
   MemoryContextSwitchTo(oldcontext);

   /*    * Generate attribute metadata needed later to produce tuples from raw C    * strings    */   attinmeta = TupleDescGetAttInMetadata(tupdesc);
   /* total number of tuples to be examined */   max_calls = proc;
   /* the return tuple always must have 1 rowid + num_categories columns */   num_categories = tupdesc->natts;
   firstpass = true;   lastrowid = NULL;

   for (call_cntr = 0; call_cntr < max_calls; call_cntr++)   {       char            **values;       HeapTuple	spi_tuple;       HeapTuple	tuple;
       /* allocate and zero space */       values = (char **) palloc0((1 + num_categories) * sizeof(char *));
       /* get the next sql result tuple */       spi_tuple = spi_tuptable->vals[call_cntr];
       /*        * now loop through the sql results and assign each value in sequence        * to the next category        */       for (i = 0; i < num_categories; i++)       {               /* see if we've gone too far already */               if (call_cntr >= max_calls)                       break;
               values[i] = SPI_getvalue(spi_tuple, spi_tupdesc, i+1);       }
       /* build the tuple */       tuple = BuildTupleFromCStrings(attinmeta, values);
       /* switch to appropriate context while storing the tuple */       oldcontext = MemoryContextSwitchTo(per_query_ctx);       tuplestore_puttuple(tupstore, tuple);       MemoryContextSwitchTo(oldcontext);
       heap_freetuple(tuple);
       /* Clean up */       for (i = 0; i < num_categories + 1; i++)               if (values[i] != NULL)                       pfree(values[i]);       pfree(values);
   }
   /* let the caller know we're sending back a tuplestore */   rsinfo->returnMode = SFRM_Materialize;   rsinfo->setResult = tupstore;   rsinfo->setDesc = tupdesc;
   /* release SPI related resources (and return to caller's context) */   SPI_finish();
   pfree(sql);   return (Datum) 0;

}

char *parse_sqlf(const char *query){   void    *result;
   yy_scan_string(query);
   sqlf_yyparse(&result);
   return (char *)result;
}

Bison code:

%{   #include    #include    #include 
   #include "postgres.h"   #include "parsing.h"
   #define YYDEBUG 1   #define QUERY_LENGTH 4   #define YYPARSE_PARAM result  /* need this to pass a pointer (void
*) to yyparse */
   int sqlf_yyparse(void *result);
   int real_length;   char *field;   char *fuzzy_query[QUERY_LENGTH];

%}

%name-prefix="sqlf_yy"

%union {   int	integer;   char *text;
}

%token CREATE FUZZY PREDICATE ON AS COMMA DOTDOT LEFTP RIGHTP INFINIT       DROP EQUAL SELECT WHERE FROM AND OR ORDER BY ASC DESC WITH CALIBRATION
%token  PARAMETER
%type  Param Param_select Param_from List_where List_order SelectStmt

%%


query:  /* empty string */     |  query command
;

command: '\n'           | CreateFuzzyPredStmt           | DropFuzzyPredStmt           | SelectStmt           {             int i;
             *((void **)result) = fuzzy_query[real_length-1];
             for (i=0;i %f AND %s < %f"               str_result=(char *)palloc(sizeof(char)*(len*2));               $$=translate_fuzzy_preds(str_result,field,$3);               pfree(str_result);           }           | List_where AND Param {               strcat($$," AND ");               strcat($$,$3);               field=$3;           }           | List_where RIGHTP AND Param {               strcat($$,") AND ");               strcat($$,$4);               field=$4;           }           | List_where OR Param {               strcat($$," OR ");               strcat($$,$3);               field=$3;           }           | List_where RIGHTP OR Param {               strcat($$,") OR ");               strcat($$,$4);               field=$4;           }
;

List_order:           Param { $$=$1; }           | List_order COMMA Param {               strcat($$,", ");               strcat($$,$3);           }           | List_order ASC {               strcat($$," ASC");               strcat($$,$1);           }           | List_order DESC {               strcat($$," DESC");               strcat($$,$1);           }
;

%%
void yyerror (char *s) {elog (ERROR, "%s\n", s);}

#include "scan.c"

Re: Bison crashes postgresql

От:
Andrew Dunstan <andrew@dunslane.net>
Дата:


Werner Echezuria wrote:
> Hi, I have a code in which I translate some code from sqlf to sql, but
> when it comes to yy_parse the server crashes, I have no idea why,
> because it works fine in other situations.
>   

I don't understand why you're doing what you're doing this way. Wouldn't 
it be better to patch the main postgres parser and make your 
functionality first class rather than having it run via an SQL string 
and a function that calls a secondary parser?

cheers

andrew



Re: Bison crashes postgresql

От:
Hans-Juergen Schoenig -- PostgreSQL <postgres@cybertec.at>
Дата:
Andrew Dunstan wrote:
>
>
> Werner Echezuria wrote:
>> Hi, I have a code in which I translate some code from sqlf to sql, but
>> when it comes to yy_parse the server crashes, I have no idea why,
>> because it works fine in other situations.
>>   
>
> I don't understand why you're doing what you're doing this way. 
> Wouldn't it be better to patch the main postgres parser and make your 
> functionality first class rather than having it run via an SQL string 
> and a function that calls a secondary parser?
>
> cheers
>
> andrew
>

yes, this is the thing i had in mind as well.
what is your ultimate goal?
   many thanks,
      hans


-- 
Cybertec Schoenig & Schoenig GmbH
Reyergasse 9 / 2
A-2700 Wiener Neustadt
Web: www.postgresql-support.de


Re: Bison crashes postgresql

От:
Werner Echezuria <wercool@gmail.com>
Дата:
The thing is I was in a project to develop a Fuzzy Database Management
System. We have to bring fuzzy logic to postgresql, there was a team
developing a software in java and the other developing in the
postgresql core. But I always think these were wrong, so I'm trying to
develop a library to do that, but I guess I'm moving forward, the bug
was solved and I have a google code project:
http://code.google.com/p/fuzzyquery/. Now I hope to give more features
and have a fully functional library.

2009/9/1 Hans-Juergen Schoenig -- PostgreSQL :
> Andrew Dunstan wrote:
>>
>>
>> Werner Echezuria wrote:
>>>
>>> Hi, I have a code in which I translate some code from sqlf to sql, but
>>> when it comes to yy_parse the server crashes, I have no idea why,
>>> because it works fine in other situations.
>>>
>>
>> I don't understand why you're doing what you're doing this way. Wouldn't
>> it be better to patch the main postgres parser and make your functionality
>> first class rather than having it run via an SQL string and a function that
>> calls a secondary parser?
>>
>> cheers
>>
>> andrew
>>
>
> yes, this is the thing i had in mind as well.
> what is your ultimate goal?
>
>   many thanks,
>
>      hans
>
>
> --
> Cybertec Schoenig & Schoenig GmbH
> Reyergasse 9 / 2
> A-2700 Wiener Neustadt
> Web: www.postgresql-support.de
>
>

FAQ