COMP9315 23T1 Assignment 1
Adding an GeoCoord Data Type to PostgreSQL
DBMS Implementation
Last updated: Wednesday 22nd March 6:06am
Most recent changes are shown in red ... older changes are shown in brown.

Aims

This assignment aims to give you

The goal is to implement a new data type for PostgreSQL, complete with input/output functions, comparison operators and the ability to build indexes on values of the type.

Summary

Deadline
Friday 17 March, 9pm

Pre-requisites:
before starting this assignment, it would be useful to complete Prac Work P04

Late Penalty:
5% of the max assessment mark per-day reduction, for up to 5 days

Marks:
This assignment contributes 15 marks toward your total mark for this course.

Submission:
Moodle > Assignment > Assignment 1

Make sure that you read this assignment specification carefully and completely before starting work on the assignment.
Questions which indicate that you haven't done this will simply get the response "Please read the spec".

We use the following names in the discussion below

Introduction

PostgreSQL has an extensibility model which, among other things, provides a well-defined process for adding new data types into a PostgreSQL server. This capability has led to the development by PostgreSQL users of a number of types (such as polygons) which have become part of the standard distribution. It also means that PostgreSQL is the database of choice in research projects which aim to push the boundaries of what kind of data a DBMS can manage.

In this assignment, we will be adding a new data type for dealing with geographical coordinates. You may implement the functions for the data type in any way you like provided that they satisfy the semantics given below (in the The Geographical Coordinate Data Type section).

The process for adding new base data types in PostgreSQL is described in the following sections of the PostgreSQL documentation:

Section 38.13 uses an example of a complex number type, which you can use as a starting point for defining your GeoCoord data type (see below). There are other examples of new data types under the directories:

These may or may not give you some useful ideas on how to implement the GeoCoord address data type.

Setting Up

You ought to start this assignment with a fresh copy of PostgreSQL, without any changes that you might have made for the Prac exercises (unless these changes are trivial). Note that you only need to configure, compile and install your PostgreSQL server once for this assignment. All subsequent compilation takes place in the src/tutorial directory, and only requires modification of the files there.

Once you have re-installed your PostgreSQL server, you should run the following commands:

$ cd PG_CODE/src/tutorial
$ cp complex.c gcoord.c
$ cp complex.source gcoord.source

Once you've made the gcoord.* files, you should also edit the Makefile in this directory and add the green text to the following lines:

MODULES = complex funcs gcoord
DATA_built = advanced.sql basics.sql complex.sql funcs.sql syscat.sql gcoord.sql

The rest of the work for this assignment involves editing only the gcoord.c and gcoord.source files. In order for the Makefile to work properly, you must use the identifier _OBJWD_ in the gcoord.source file to refer to the directory holding the compiled library. You should never modify directly the gcoord.sql file produced by the Makefile. Place all of you C code in the gcoord.c file; do not create any other *.c files.

Note that your submitted versions of gcoord.c and gcoord.source should not contain any references to the complex type. Make sure that the documentation (comments in program) describes the code that you wrote.

The Geographical Coordinate Data Type

We wish to define a new base type GeoCoord to represent location based on geographical coordinate system. We also aim to define a useful set of operations on values of type GeoCoord and wish to be able to create indexes on GeoCoord attributes. How you represent GeoCoord values internally, and how you implement the functions to manipulate them internally, is up to you. However, they must satisfy the requirements below.

Once implemented correctly, you should be able to use your PostgreSQL server to build the following kind of SQL applications:

create table StoreInfo (
   id  integer primary key,
   location GeoCoord,
   -- etc. etc.
);

insert into StoreInfo(id, location) values
(1,'Sydney,33.86°S,151.21°E'),
(2,'Melbourne,37.84°S,144.95°E');

Having defined a hash-based file structure, we would expect that the queries would make use of it. You can check this by adding the keyword EXPLAIN before the query, e.g.

db=# create index on StoreInfo using hash (location);
db=# explain analyze select * from StoreInfo where location='Melbourne,37.84°S,144.95°E';

which should, once you have correctly implemented the data type and loaded sufficient data, show that an index-based scan of the data is being used.

Geographical Coordinate values

The precise format of geographical coordinates is defined as following:

A more precise definition can be given using a BNF grammar:

GeoCoord ::=  LocationName ',' Latitude ',' Longitude | LocationName ',' Latitude ' ' Longitude | LocationName ',' Longitude ',' Latitde | LocationName ',' Longitude ' ' Latitde

LocationName ::= WordList 

Latitude ::= CoordValue '°' LatDir

LatDir ::= 'N' | 'S'

Longitude  ::=  CoordValue '°' LongDir

LongDir ::= 'W' | 'E'

WordList ::= Word | Word' 'WordList 

Word ::= Letter | Letter Word

Letter ::= 'a' | 'b' | ... | 'z' | 'A' | 'B' | ... 'Z'

You may assume that the maximum length of the LocationName part is 256 chars.

Under this syntax, the following are valid geographical coordinate.

Melbourne,37.84°S,144.95°E
Melbourne,37.84°S 144.95°E
Melbourne,144.95°E,37.84°S
San Francisco,37.77°N,122.42°W
San Francisco,122.42°W,37.77°N
san francisco,122.42°W 37.77°N

The following geographical coordinate are not valid in our system.

Melbourne,37.84S,144.95E
Melbourne,37.84,144.95
Melbourne,37.84
Melbourne:37.84°S,144.95°E
Melbourne ,37.84°S,144.95°E
12Melbourne,37.84°S,144.95°E
Melbourne,-37.84°S,144.95°E
San francisco,122.42°W,37.77
 San Francisco\,37.77°N,122.4194°W
San francisco,165°N,22°W
37.84°S,144.95°E

Important: for this assignment, we define an ordering on geographical coordinate as follows:

There are examples of how this works in the section on Operations on Geographical Coordinates below.

Representing Geographical Coordinates

The first thing you need to do is to decide on an internal representation for your GeoCoord data type. You should do this, however, after you have looked at the description of the operators below, since what they require may affect how you decide to structure your internal GeoCoord values.

When you read strings representing GeoCoord values, they are converted into your internal form, stored in the database in this form, and operations on GeoCoord values are carried out using this data structure. It is useful to define a canonical form for geographical coordinates, which may be slightly different to the form in which they are read (e.g. "Melbourne,144.95°E 37.84°S" should be rendered as "melbourne,37.84°S,144.95°E"). When you display GeoCoord values, you should show them in canonical form, regardless of how they were entered or how they are stored.

The initial functions you need to write are ones to read and display values of type GeoCoord. You should write analogues of the functions complex_in(), complex_out that are defined in the file complex.c. Make sure that you use the V1 style function interface (as is done in complex.c).

Note that the two input/output functions should be complementary, meaning that any string displayed by the output function must be able to be read using the input function. There is no requirement for you to retain the precise string that was used for input (e.g. you could store the GeoCoord value internally in a different form such as splitting it into several parts).

One thing that gcoord_in() must do is determine whether the input string has the correct structure (according to the garmmer above). Your gcoord_out() should display each geographical coordinate in a format that can be read by gcoord_in().

You are not required (but you can) to define binary input/output functions, called receive_function and send_function in the PostgreSQL documentation, and called complex_send and complex_recv in the complex.c file.

As noted above, you cannot assume anything about the input length of the geographical coordinates. Using a fixed-size representation for GeoCoord limits your maximum possible mark to 10/15.

Operations on Geographical Coordinates

You must implement all of the following operations for the GeoCoord type:

Hint: test out as many of your C functions as you can outside PostgreSQL (e.g. write a simple test driver) before you try to install them in PostgreSQL. This will make debugging much easier.

Testing

You can testing your solution by writing some simple SQL codes. As a reference, we also have written some scripts for testing. The tutorial to use that can be found in the testing page. Note that more test cases will be used in marking.

Submission

You need to submit two files on Moodle: gcoord.c containing the C functions that implement the internals of the GeoCoord data type, and gcoord.source containing the template SQL commands to install the GeoCoord data type into a PostgreSQL server. Do not submit the gcoord.sql file, since it contains absolute file names which do not work in our test environment.

Have Fun~