Commit a3694b42 authored by Tom Lane's avatar Tom Lane

Gene Selkov's SEG datatype (GiST example code)

parent 9892ddf5
#
# $Header: /cvsroot/pgsql/contrib/seg/Makefile,v 1.1 2000/12/11 20:40:33 tgl Exp $
#
subdir = contrib/seg
top_builddir = ../..
include $(top_builddir)/src/Makefile.global
# override libdir to install shlib in contrib not main directory
libdir := $(libdir)/contrib
# shared library parameters
NAME= seg
SO_MAJOR_VERSION= 1
SO_MINOR_VERSION= 0
override CPPFLAGS += -I$(srcdir)
OBJS= seg.o segparse.o segscan.o buffer.o
all: all-lib $(NAME).sql
# Shared library stuff
include $(top_srcdir)/src/Makefile.shlib
segparse.c segparse.h: segparse.y
$(YACC) -d $(YFLAGS) -p seg_yy $<
mv -f y.tab.c segparse.c
mv -f y.tab.h segparse.h
segscan.c: segscan.l
ifdef FLEX
$(FLEX) $(FLEXFLAGS) -Pseg_yy -o'$@' $<
else
@$(missing) flex $< $@
endif
$(NAME).sql: $(NAME).sql.in
sed -e 's:MODULE_PATHNAME:$(libdir)/$(shlib):g' < $< > $@
.PHONY: submake
submake:
$(MAKE) -C $(top_builddir)/src/test/regress pg_regress
# against installed postmaster
installcheck: submake
$(top_builddir)/src/test/regress/pg_regress seg
# in-tree test doesn't work yet (no way to install my shared library)
#check: all submake
# $(top_builddir)/src/test/regress/pg_regress --temp-install \
# --top-builddir=$(top_builddir) seg
check:
@echo "'make check' is not supported."
@echo "Do 'make install', then 'make installcheck' instead."
install: all installdirs install-lib
$(INSTALL_DATA) $(srcdir)/README.$(NAME) $(docdir)/contrib
$(INSTALL_DATA) $(NAME).sql $(datadir)/contrib
installdirs:
$(mkinstalldirs) $(docdir)/contrib $(datadir)/contrib $(libdir)
uninstall: uninstall-lib
rm -f $(docdir)/contrib/README.$(NAME) $(datadir)/contrib/$(NAME).sql
clean distclean maintainer-clean: clean-lib
rm -f segparse.c segparse.h segscan.c
rm -f y.tab.c y.tab.h $(OBJS) $(NAME).sql
# things created by various check targets
rm -rf results tmp_check log
rm -f regression.diffs regression.out regress.out run_check.out
ifeq ($(PORTNAME), win)
rm -f regress.def
endif
depend dep:
$(CC) -MM $(CFLAGS) *.c >depend
ifeq (depend,$(wildcard depend))
include depend
endif
This diff is collapsed.
/* This module defines the parse buffer and routines for setting/reading it */
#include "postgres.h"
#include "utils/elog.h"
static char * PARSE_BUFFER;
static char * PARSE_BUFFER_PTR;
static unsigned int PARSE_BUFFER_SIZE;
static unsigned int SCANNER_POS;
void set_parse_buffer( char* s );
void reset_parse_buffer( void );
int read_parse_buffer( void );
char * parse_buffer( void );
char * parse_buffer_ptr( void );
unsigned int parse_buffer_curr_char( void );
unsigned int parse_buffer_size( void );
unsigned int parse_buffer_pos( void );
extern void seg_flush_scanner_buffer(void); /* defined in segscan.l */
void set_parse_buffer( char* s )
{
PARSE_BUFFER = s;
PARSE_BUFFER_SIZE = strlen(s);
if ( PARSE_BUFFER_SIZE == 0 ) {
elog(ERROR, "seg_in: can't parse an empty string");
}
PARSE_BUFFER_PTR = PARSE_BUFFER;
SCANNER_POS = 0;
}
void reset_parse_buffer( void )
{
PARSE_BUFFER_PTR = PARSE_BUFFER;
SCANNER_POS = 0;
seg_flush_scanner_buffer();
}
int read_parse_buffer( void )
{
int c;
/*
c = *PARSE_BUFFER_PTR++;
SCANNER_POS++;
*/
c = PARSE_BUFFER[SCANNER_POS];
if(SCANNER_POS < PARSE_BUFFER_SIZE)
SCANNER_POS++;
return c;
}
char * parse_buffer( void )
{
return PARSE_BUFFER;
}
unsigned int parse_buffer_curr_char( void )
{
return PARSE_BUFFER[SCANNER_POS];
}
char * parse_buffer_ptr( void )
{
return PARSE_BUFFER_PTR;
}
unsigned int parse_buffer_pos( void )
{
return SCANNER_POS;
}
unsigned int parse_buffer_size( void )
{
return PARSE_BUFFER_SIZE;
}
extern void set_parse_buffer( char* s );
extern void reset_parse_buffer( void );
extern int read_parse_buffer( void );
extern char * parse_buffer( void );
extern char * parse_buffer_ptr( void );
extern unsigned int parse_buffer_curr_char( void );
extern unsigned int parse_buffer_pos( void );
extern unsigned int parse_buffer_size( void );
This diff is collapsed.
This diff is collapsed.
#!/usr/bin/perl
$integer = '[+-]?[0-9]+';
$real = '[+-]?[0-9]+\.[0-9]+';
$RANGE = '(\.\.)(\.)?';
$PLUMIN = q(\'\+\-\');
$FLOAT = "(($integer)|($real))([eE]($integer))?";
$EXTENSION = '<|>|~';
$boundary = "($EXTENSION)?$FLOAT";
$deviation = $FLOAT;
$rule_1 = $boundary . $PLUMIN . $deviation;
$rule_2 = $boundary . $RANGE . $boundary;
$rule_3 = $boundary . $RANGE;
$rule_4 = $RANGE . $boundary;
$rule_5 = $boundary;
print "$rule_5\n";
while (<>) {
# s/ +//g;
if ( /^($rule_1)$/ ) {
print;
}
elsif ( /^($rule_2)$/ ) {
print;
}
elsif ( /^($rule_3)$/ ) {
print;
}
elsif ( /^($rule_4)$/ ) {
print;
}
elsif ( /^($rule_5)$/ ) {
print;
}
else {
print STDERR "error in $_\n";
}
}
This diff is collapsed.
This diff is collapsed.
typedef struct SEG {
float lower;
float upper;
char l_sigd;
char u_sigd;
char l_ext;
char u_ext;
} SEG;
%{
#define YYERROR_VERBOSE
#define YYPARSE_PARAM result /* need this to pass a pointer (void *) to yyparse */
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include "segdata.h"
#include "buffer.h"
#include "postgres.h"
#include "utils/elog.h"
#undef yylex /* falure to redefine yylex will result in calling the */
#define yylex seg_yylex /* wrong scanner when running inside postgres backend */
extern int errno;
extern int yylex(); /* defined as seg_yylex in segscan.c */
extern int significant_digits( char *str ); /* defined in seg.c */
int seg_yyerror( char *msg );
int seg_yyparse( void *result );
float seg_atof( char *value );
#define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
#define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
#define ABS(X) ((X) < 0 ? (-X) : (X))
long threshold;
char strbuf[25] = {
'0', '0', '0', '0', '0',
'0', '0', '0', '0', '0',
'0', '0', '0', '0', '0',
'0', '0', '0', '0', '0',
'0', '0', '0', '0', '\0'
};
%}
/* BISON Declarations */
%union {
struct BND {
float val;
char ext;
char sigd;
} bnd;
char * text;
}
%token <text> FLOAT
%token <text> RANGE
%token <text> PLUMIN
%token <text> EXTENSION
%type <bnd> boundary
%type <bnd> deviation
%start range
/* Grammar follows */
%%
range:
boundary PLUMIN deviation {
((SEG *)result)->lower = $1.val - $3.val;
((SEG *)result)->upper = $1.val + $3.val;
sprintf(strbuf, "%g", ((SEG *)result)->lower);
((SEG *)result)->l_sigd = MAX(MIN(6, significant_digits(strbuf)), MAX($1.sigd, $3.sigd));
sprintf(strbuf, "%g", ((SEG *)result)->upper);
((SEG *)result)->u_sigd = MAX(MIN(6, significant_digits(strbuf)), MAX($1.sigd, $3.sigd));
((SEG *)result)->l_ext = '\0';
((SEG *)result)->u_ext = '\0';
}
|
boundary RANGE boundary {
((SEG *)result)->lower = $1.val;
((SEG *)result)->upper = $3.val;
if ( ((SEG *)result)->lower > ((SEG *)result)->upper ) {
reset_parse_buffer();
elog(ERROR, "swapped boundaries: %g is greater than %g", ((SEG *)result)->lower, ((SEG *)result)->upper );
YYERROR;
}
((SEG *)result)->l_sigd = $1.sigd;
((SEG *)result)->u_sigd = $3.sigd;
((SEG *)result)->l_ext = ( $1.ext ? $1.ext : '\0' );
((SEG *)result)->u_ext = ( $3.ext ? $3.ext : '\0' );
}
|
boundary RANGE {
((SEG *)result)->lower = $1.val;
((SEG *)result)->upper = HUGE;
((SEG *)result)->l_sigd = $1.sigd;
((SEG *)result)->u_sigd = 0;
((SEG *)result)->l_ext = ( $1.ext ? $1.ext : '\0' );
((SEG *)result)->u_ext = '-';
}
;
|
RANGE boundary {
((SEG *)result)->lower = -HUGE;
((SEG *)result)->upper = $2.val;
((SEG *)result)->l_sigd = 0;
((SEG *)result)->u_sigd = $2.sigd;
((SEG *)result)->l_ext = '-';
((SEG *)result)->u_ext = ( $2.ext ? $2.ext : '\0' );
}
|
boundary {
((SEG *)result)->lower = ((SEG *)result)->upper = $1.val;
((SEG *)result)->l_sigd = ((SEG *)result)->u_sigd = $1.sigd;
((SEG *)result)->l_ext = ((SEG *)result)->u_ext = ( $1.ext ? $1.ext : '\0' );
}
;
boundary:
FLOAT {
$$.ext = '\0';
$$.sigd = significant_digits($1);
$$.val = seg_atof($1);
}
|
EXTENSION FLOAT {
$$.ext = $1[0];
$$.sigd = significant_digits($2);
$$.val = seg_atof($2);
}
;
deviation:
FLOAT {
$$.ext = '\0';
$$.sigd = significant_digits($1);
$$.val = seg_atof($1);
}
;
%%
float seg_atof ( char *value ) {
float result;
char *buf = (char *) palloc(256);
errno = 0;
sscanf(value, "%f", &result);
if ( errno ) {
sprintf(buf, "numeric value %s unrepresentable", value);
reset_parse_buffer();
elog(ERROR, buf);
}
return result;
}
int seg_yyerror ( char *msg ) {
char *buf = (char *) palloc(256);
int position;
yyclearin;
if ( !strcmp(msg, "parse error, expecting `$'") ) {
msg = "expecting end of input";
}
position = parse_buffer_pos() > parse_buffer_size() ? parse_buffer_pos() - 1 : parse_buffer_pos();
sprintf(
buf,
"%s at or near position %d, character ('%c', \\%03o), input: '%s'\n",
msg,
position,
parse_buffer()[position - 1],
parse_buffer()[position - 1],
parse_buffer()
);
reset_parse_buffer();
elog(ERROR, buf);
return 0;
}
%{
/*
** A scanner for EMP-style numeric ranges
*/
#include <string.h>
#include <stdio.h>
#include "segparse.h"
#include "buffer.h"
#define YY_NO_UNPUT 1
#undef yywrap
/* flex screws a couple symbols when used with the -P otion; fix those */
#define YY_DECL int seg_yylex YY_PROTO(( void )); \
int seg_yylex YY_PROTO(( void ))
#define yylval seg_yylval
/* redefined YY_INPUT reads byte-wise from the memory area defined in buffer.c */
#undef YY_INPUT
#define YY_INPUT(buf,result,max_size) \
{ \
int c = read_parse_buffer(); \
result = (c == '\0') ? YY_NULL : (buf[0] = c, 1); \
}
void seg_flush_scanner_buffer(void);
%}
range (\.\.)(\.)?
plumin (\'\+\-\')|(\(\+\-)\)
integer [+-]?[0-9]+
real [+-]?[0-9]+\.[0-9]+
float ({integer}|{real})([eE]{integer})?
%%
{range} yylval.text = yytext; return RANGE;
{plumin} yylval.text = yytext; return PLUMIN;
{float} yylval.text = yytext; return FLOAT;
\< yylval.text = "<"; return EXTENSION;
\> yylval.text = ">"; return EXTENSION;
\~ yylval.text = "~"; return EXTENSION;
[ ]+ /* discard spaces */
. return yytext[0]; /* alert parser of the garbage */
%%
int seg_yylex();
void seg_flush_scanner_buffer(void) {
YY_FLUSH_BUFFER;
}
#!/usr/bin/perl
# this script will sort any table with the segment data type in its last column
while (<>) {
chomp;
push @rows, $_;
}
foreach ( sort {
@ar = split("\t", $a);
$valA = pop @ar;
$valA =~ s/[~<> ]+//g;
@ar = split("\t", $b);
$valB = pop @ar;
$valB =~ s/[~<> ]+//g;
$valA <=> $valB
} @rows ) {
print "$_\n";;
}
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment