Index

sciSQL provides science-specific tools and extensions for SQL. Currently, the project contains user defined functions (UDFs) and stored procedures for MySQL or MariaDB in the areas of spherical geometry, statistics, and photometry. The project was motivated by the needs of the Rubin Observatory Legacy Survey of Space and Time (LSST) and has been sponsored by LSST and SLAC / DOE. sciSQL is distributed under the terms of the Apache License version 2.0.

sciSQL is also distributed with a pair of Javascript libraries. These are:

Read on for instructions on how to configure, build, test, install and uninstall the sciSQL software.

Prerequisites

Python 2.5.x or later
Python future 0.16 or later
MySQL server 8.x -or- MariaDB server 10.x
mysqlclient 2.1.x or later
This is a Python DB API 2.0 implementation for MySQL/MariaDB, and is required when running the unit tests and uninstalling sciSQL.
Mako 0.4 or later
This Python templating library is required when rebuilding release documentation.

In order to install the UDFs, you will need write permission to the MySQL/MariaDB server plug-in directory, as well as a MySQL/MariaDB account with admin priviledges.

Databases reserved for sciSQL use

The following database names are reserved for use by sciSQL:

scisql
Contains sciSQL stored procedures.
scisql_test
Used by sciSQL unit tests.
scisql_demo
Contains sample data that can be used to exercise the sciSQL UDFs.

The scisql_demo database is dropped and re-created during installation. If you are using it for other things, you must migrate its contents to a different database prior to installing sciSQL. If you do not, YOU WILL LOSE DATA.

Even though the scisql and scisql_test databases are never automatically dropped, their use is STRONGLY DISCOURAGED.

Build configuration

Run ./configure from the top-level sciSQL directory. Passing --help will yield a list of configuration options. Here are the ones most likely to require tweaking if ./configure doesn't work straight out of the box on your system:

--mysql-dir
Set this to the top-level of the MySQL/MariaDB server install tree
--mysql-config
Point to mysql_config or mariadb_config configuration tool
--mysql-includes
Point to MySQL/MariaDB headers (mysql.h and dependents)
--scisql-prefix
This string will be used as a prefix for all sciSQL UDF and stored procedure names. You can specify an empty string, which will result in unprefixed names. The default is "scisql_".

If you wish to build/install only the sciSQL client utilities and documentation, run configure with the --client-only option. In this case, a MySQL/MariaDB server or client install is not required, the only executable generated is scisql_index (a utility which generates HTM indexes for tables of circles or polygons stored in tab-separated-value format).

Build

sciSQL is built and staged with the usual make and make install commands.

You may wish to regenerate the HTML documentation if you've chosen a non-default value for --scisql-prefix, as the HTML distributed with release tar-balls is built under the assumption that --scisql-prefix="scisql_". To do this, run make html_docs.

Deploying sciSQL in a MySQL/MariaDB instance

Assuming you've installed scisql in $PREFIX, update your PATH and PYTHONPATH as described below:

export PATH="$PREFIX/bin:$PATH"
export PYTHONPATH="$PREFIX/python:$PATH"
Check that you have access to a local server instance and run scisql-deploy.py. Passing --help will yield a list of configuration options. Here are the ones most likely to require tweaking:

--mysql-user
Set this to the name of a MySQL/MariaDB admin user; defaults to root
--mysql-bin
Point to the mysql or mariadb command-line client
--mysql-socket
Point to the MySQL/MariaDB server UNIX socket file
--verbose
Verbosity level; possible value are FATAL, ERROR, WARN, INFO, DEBUG

You will be prompted for the MySQL/MariaDB admin user password during configuration. If you run scisql-deploy.py in a script, you can use standard input, for example via a pipe, for providing this password. Connection details, including this password, are stored in a temporary directory in a file named my-client.cnf using the MySQL options file format. This temporary file is removed at the end of the process, unless you set the verbose level to DEBUG.

The scisql-deploy.py command will CREATE the sciSQL UDFs, stored procedures, and databases (including the scisql_demo database). It will also automatically run the sciSQL integration tests, which check that sciSQL is correctly deployed. You can re-run the tests anytime by invoking scisql-deploy.py with the --test option.

Finally, note that after installation each UDF will be available under two names: one including a version number and one without. For example, assuming that --scisql-prefix="foo_" and that the sciSQL version number is 1.2.3, a hypothetical UDF named bar would be available as:

  • foo_bar
  • foo_bar_1_2_3

Invoking scisql-deploy.py with the --undeploy option will drop the versioned sciSQL UDFs and stored procedures. It will also drop the unversioned UDFs/procedures, but only if the unversioned UDF/procedure was created by the version of sciSQL being uninstalled. As a consequence, it is possible to have multiple versions of sciSQL installed at the same time, and the behavior of two versions can be compared from within a single MySQL/MariaDB instance. An unversioned name will resolve to the most recently installed versioned name.

An updeploy will also drop the scisql_demo database.

Rebuilding sciSQL

If you've already installed sciSQL, say using a UDF/procedure name prefix of "foo_", and then decide you'd like to change the prefix to "bar_", do the following from the top-level sciSQL directory:

scisql-deploy.py ... --undeploy
Uninstalls the UDFs and stored procedures named foo_*.
make distclean
Removes all build products and configuration files.
configure --scisql-prefix=bar_ ...
Reconfigures sciSQL (sets new name prefix).
make
Rebuilds sciSQL.
make install
Restages sciSQL.
scisql-deploy.py ...
Redeploys sciSQL

No MySQL/MariaDB restart is required. Note that multiple installations of the same version of sciSQL with different UDF/procedure name prefixes can coexist on a single MySQL/MariaDB instance.

MySQL/MariaDB server restarts

Installing different versions of sciSQL, or multiple copies of the same version with different UDF/procedure name prefixes, does not require a server restart.

Only sciSQL developers should need to perform restarts. They are required when changing the name of a UDF without changing the name of the shared library installed into the server plugin directory. In this case, an attempt to install the updated shared library will sometimes result in MySQL/MariaDB reporting that it cannot find symbol names that are actually present. This is presumably due to server and/or OS level caching effects, and restarting the server resolves the problem.

Reporting bugs and getting help

If you encounter test-case failures, or think you've identified a bug in the sciSQL code, or would just like to ask a question, please submit an issue.

The aim of the spherical geometry UDFs and stored procedures is to allow quick answers to the following sorts of questions:

  1. Which points in a table lie inside a region on the sphere? For example, an astronomer might wish to know which stars and galaxies lie inside the region of the sky observed by a single camera CCD.
  2. Which spherical regions in a table contain a particular point? For example, an astronomer might with to know which telescope images overlap the position of interesting object X.

HTM indexing

To accelerate these types of queries, sciSQL maps points/regions to the integer ID(s) of their containing/overlapping triangles in a Hierarchical Triangular Mesh (HTM). This is a decomposition of the unit sphere defined by A. Szalay, T. Budavari, G. Fekete at the Johns Hopkins University, and Jim Gray, Microsoft Research. See the following links for more information:

To accelerate spatial queries, standard B-tree indexes are created on the point/region HTM IDs and spatial constraints are expressed in terms of those IDs. This allows the database optimizer to restrict the rows that must be considered by a spatial query.

Read on to learn how to create and take advantage of HTM indexes on tables containing spatial data. The examples below can be run in the scisql_demo database, which contains all of the referenced tables and a tiny amount of sample data.

Supported region types

sciSQL supports 4 kinds of regions: longitude/latitude angle boxes, spherical circles (defined by a center and opening angle), spherical ellipses (the orthographic projection of a standard 2-d ellipse onto the sphere, where the 2-d ellipse is on a plane tangent to the unit sphere at the ellipse center), and spherical convex polygons (where polygon edges are great circles). Note also that spherical convex polygons have a binary representation, produced by scisql_s2CPolyToBin(), allowing them to be stored as values in a BINARY table column.

Points-in-region queries

sciSQL contains several UDFs for checking whether a point lies inside a region. These are: scisql_s2PtInBox(), scisql_s2PtInCircle(), scisql_s2PtInCPoly() and scisql_s2PtInEllipse(). They return 1 if the input point is inside the input region and 0 otherwise.

Given these UDFs, a simple way to answer question 1 is illustrated by the following example:

SELECT objectId
    FROM Object
    WHERE scisql_s2PtInCircle(ra_PS, decl_PS, 0, 0, 0.01) = 1;

This query returns all the objects within 0.01 degrees of (RA, Dec) = (0, 0). It is inefficient for small search regions because the scisql_s2PtInCircle() UDF must be called for every row in the Object table.

Lets assume that Object contains an indexed BIGINT column named htmId20. If it does not, the column and index can be added with ALTER TABLE. htmId20 can be populated with the subdivision-level 20 HTM IDs of object positions as follows:

ALTER TABLE Object DISABLE KEYS;
UPDATE Object
    SET htmId20 = scisql_s2HtmId(ra_PS, decl_PS, 20);
ALTER TABLE Object ENABLE KEYS;

The HTM subdivision level must be between 0 and 24. At subdivision level N, there are 8*4N triangles in the mesh, so the higher subdivision levels correspond to finer tesselations of the unit sphere.

Now that HTM IDs for object positions are available and indexed, the query above can be made more efficient:

CALL scisql.scisql_s2CircleRegion(0, 0, 0.01, 20);

SELECT o.objectId
    FROM Object AS o INNER JOIN scisql.Region AS r
         ON (o.htmId20 BETWEEN r.htmMin AND r.htmMax)
    WHERE scisql_s2PtInCircle(o.ra_PS, o.decl_PS, 0, 0, 0.01) = 1;

What's going on here? The first line in the example calls the scisql_s2CircleRegion() stored procedure. This procedure creates a temporary table called scisql.Region with two BIGINT NOT NULL columns named htmMin and htmMax. It then stores the HTM IDs overlapping the search region in scisql.Region (as ranges).

Next, the original query is augmented with a join against scisql.Region. This limits the objects considered by scisql_s2PtInCircle() to those within the HTM triangles overlapping the search region; the index on htmId20 allows MySQL to retrieve these objects very quickly when the search region is small. Note that if the search region is large (meaning that a large fraction of the table being searched is inside the search region), then the original query may actually be faster.

Here is another example, this time with a search region taken from a table called Science_Ccd_Exposure. This table includes a a column named poly that contains polygonal approximations to the regions of the sphere observed by CCD exposures.

SELECT poly FROM Science_Ccd_Exposure
    WHERE scienceCcdExposureId = 43856062009
    INTO @poly;

CALL scisql.scisql_s2CPolyRegion(@poly, 20);

SELECT o.objectId
    FROM Object AS o INNER JOIN scisql.Region AS r
         ON (o.htmId20 BETWEEN r.htmMin AND r.htmMax)
    WHERE scisql_s2PtInCPoly(o.ra_PS, o.decl_PS, @poly) = 1;

The first statement stores the polygonal boundary of a particular CCD exposure into the user variable @poly, the second computes overlapping HTM IDs, and the third performs the points-in-region query as before.

Regions-containing-point queries

An example for this type of query is:

SELECT scienceCcdExposureId FROM Science_Ccd_Exposure
    WHERE scisql_s2PtInCPoly(0, 0, poly) = 1;

This query returns all the CCD exposures containing the point (RA, Dec) = (0, 0). To accelerate it using HTM indexing, an auxiliary table is introduced:

CREATE TABLE Science_Ccd_Exposure_HtmId10 (
    scienceCcdExposureId BIGINT  NOT NULL,
    htmId10              INTEGER NOT NULL,
    PRIMARY KEY (htmId10, scienceCcdExposureId),
    KEY (scienceCcdExposureId)
);

Science_Ccd_Exposure_HtmId10 will store the level 10 HTM ID of every triangle overlapping a CCD exposure. To populate it, start by dumping the primary key and polygon vertex colunms from Science_Ccd_Exposure:

rm -f /tmp/scisql_demo_ccds.tsv
SELECT scienceCcdExposureId,
       llcRa, llcDecl,
       ulcRa, ulcDecl,
       urcRa, urcDecl,
       lrcRa, lrcDecl
    FROM Science_Ccd_Exposure
    INTO OUTFILE '/tmp/scisql_demo_ccds.tsv';

Then, run the sciSQL region indexing utility:

sudo chmod a+r /tmp/scisql_demo_ccds.tsv
scisql_index -l 10 /tmp/scisql_demo_htmid10.tsv /tmp/scisql_demo_ccds.tsv

and load the results:

TRUNCATE TABLE Science_Ccd_Exposure_HtmId10;
LOAD DATA LOCAL INFILE '/tmp/scisql_demo_htmid10.tsv' INTO TABLE Science_Ccd_Exposure_HtmId10;

The example regions-containing-point query can now be expressed more efficiently as:

SELECT sce.scienceCcdExposureId
    FROM Science_Ccd_Exposure AS sce, (
             SELECT scienceCcdExposureId
             FROM Science_Ccd_Exposure_HtmId10
             WHERE htmId10 = scisql_s2HtmId(0, 0, 10)
         ) AS h
    WHERE sce.scienceCcdExposureId = h.scienceCcdExposureId AND
          scisql_s2PtInCPoly(0, 0, sce.poly) = 1;

User Defined Functions

scisql_angSep

FUNCTION scisql_angSep (
  lon1 DOUBLE PRECISION, deg Longitude angle of first position.
  lat1 DOUBLE PRECISION, deg Latitude angle of first position.
  lon2 DOUBLE PRECISION, deg Longitude angle of second position.
  lat2 DOUBLE PRECISION deg Latitude angle of second position.
) RETURNS DOUBLE PRECISION
FUNCTION scisql_angSep (
  x1 DOUBLE PRECISION, X coordinate of first position.
  y1 DOUBLE PRECISION, Y coordinate of first position.
  z1 DOUBLE PRECISION, Z coordinate of first position.
  x2 DOUBLE PRECISION, X coordinate of second position.
  y2 DOUBLE PRECISION, Y coordinate of second position.
  z2 DOUBLE PRECISION Z coordinate of second position.
) RETURNS DOUBLE PRECISION
Returns the angular separation in degrees between two positions on the unit sphere. Positions may be specified either as spherical coordinate pairs (lon1, lat1) and (lon2, lat2), or as 3-vectors (x1, y1, z1) and (x2, y2, z2) with arbitrary norm. If spherical coordinates are used, all arguments are assumed to be in units of degrees.
Notes
  • All arguments must be convertible to type DOUBLE PRECISION.
  • If any argument is NULL, NaN, or +/-Inf, NULL is returned. MySQL does not currently support storage of IEEE special values. However, their presence is checked for to ensure reasonable behavior if a future MySQL release does end up supporting them.
  • If spherical coordinates are passed in and either latitude angle is not in the [-90, 90] degree range, NULL is returned.
Examples
SELECT scisql_angSep(0, 0, 0, 90);
SELECT scisql_angSep(1, 0, 0, 0, 0, 1);
SELECT scisql_angSep(ra_PS, decl_PS, ra_SG, decl_SG) FROM Object LIMIT 10;

[internal] scisql_s2CPolyHtmRanges

FUNCTION scisql_s2CPolyHtmRanges (
  poly BINARY, Binary string representation of a polygon.
  level INTEGER, HTM subdivision level, must be in range [0, 24].
  maxranges INTEGER Maximum number of ranges to report.
) RETURNS MEDIUMBLOB
Returns a binary-string representation of HTM ID ranges overlapping a spherical convex polygon. The polygon must be specified in binary-string form (as produced by scisql_s2CPolyToBin()).
Notes
  • If any parameter is NULL, this is an error and NULL is returned.
  • If poly does not correspond to a valid binary serialization of a spherical convex polygon, this is an error and NULL is returned.
  • If level does not lie in the range [0, 24], this is an error and NULL is returned.
  • maxranges can be set to any value. In practice, its value is clamped such that the binary return string has size at most 16MB (fits in a MEDIUMBLOB). Negative values are interpreted to mean: "return as many ranges as possible subject to the 16MB output size limit".

scisql_s2CPolyToBin

FUNCTION scisql_s2CPolyToBin (
  v1Lon DOUBLE PRECISION, deg Longitude angle of first polygon vertex.
  v1Lat DOUBLE PRECISION, deg Latitude angle of first polygon vertex.
  v2Lon DOUBLE PRECISION, deg Longitude angle of second polygon vertex.
  v2Lat DOUBLE PRECISION, deg Latitude angle of second polygon vertex.
 ...
) RETURNS BINARY
Returns a binary-string representation of a spherical convex polygon. The polygon must be specified as a sequence of at least 3 and at most 20 vertices. An N vertex input will result in a binary string of length exactly 24*(N + 1).
Notes
  • If any parameter is NULL, NaN or +/-Inf, this is an error and NULL is returned.
  • If any latitude angle lies outside of [-90, 90] degrees, this is an error and NULL is returned.
  • Polygon vertices can be specified in either clockwise or counter-clockwise order. However, the vertices are assumed to be hemispherical, to define edges that do not intersect except at vertices, and to define edges that form a convex polygon.
  • Input coordinate must be convertible to type DOUBLE PRECISION. If their actual type is BIGINT or DECIMAL, then the conversion can result in loss of precision and hence an inaccurate result. Loss of precision will not occur so long as the inputs are values of type DOUBLE PRECISION, FLOAT, REAL, INTEGER, SMALLINT, or TINYINT.
Examples
CREATE TEMPORARY TABLE Poly (
    ra1  DOUBLE PRECISION NOT NULL,
    dec1 DOUBLE PRECISION NOT NULL,
    ra2  DOUBLE PRECISION NOT NULL,
    dec2 DOUBLE PRECISION NOT NULL,
    ra3  DOUBLE PRECISION NOT NULL,
    dec3 DOUBLE PRECISION NOT NULL,
    poly BINARY(96) DEFAULT NULL
);

INSERT INTO Poly VALUES (-10,  0,
                          10,  0,
                           0, 10,
                         NULL);

UPDATE Poly
    SET poly = scisql_s2CPolyToBin(
        ra1, dec1,
        ra2, dec2,
        ra3, dec3);

[internal] scisql_s2CircleHtmRanges

FUNCTION scisql_s2CircleHtmRanges (
  centerLon DOUBLE PRECISION, deg Longitude angle of circle center.
  centerLat DOUBLE PRECISION, deg Latitude angle of circle center.
  radius DOUBLE PRECISION, deg Circle radius.
  level INTEGER, HTM subdivision level, must be in range [0, 24].
  maxranges INTEGER Maximum number of ranges to report.
) RETURNS MEDIUMBLOB
Returns a binary-string representation of HTM ID ranges overlapping a circle on the unit sphere. This string will be at most 16MB long, i.e. it will fit in a MEDIUMBLOB.
Notes
  • The centerLon, centerLat, and radius arguments must be convertible to type DOUBLE PRECISION. If they are of type BIGINT or DECIMAL, then the conversion can result in loss of precision and hence an inaccurate result. Loss of precision will not occur so long as the inputs are values of type DOUBLE PRECISION, FLOAT, REAL, INTEGER, SMALLINT, or TINYINT.
  • The level and maxranges arguments must be integers.
  • If any parameter is NULL, NaN or +/-Inf, this is an error and NULL is returned.
  • If centerLat is not in the [-90, 90] degree range, this is an error and NULL is returned.
  • If radius is negative or greater than 180, this is an error and NULL is returned.
  • If level does not lie in the range [0, 24], this is an error and NULL is returned.
  • maxranges can be set to any value. In practice, its value is clamped such that the binary return string has size at most 16MB (fits in a MEDIUMBLOB). Negative values are interpreted to mean: "return as many ranges as possible subject to the 16MB output size limit".

scisql_s2HtmId

FUNCTION scisql_s2HtmId (
  lon DOUBLE PRECISION, deg Longitude angle of the point to index.
  lat DOUBLE PRECISION, deg Latitude angle of the point to index.
  level INTEGER HTM subdivision level, required to lie in the range [0, 24].
) RETURNS BIGINT
Returns the HTM ID of a point at the given subdivision level.
Notes
  • If any parameter is NULL, NULL is returned.
  • If lon or lat is NaN or +/-Inf, this is an error and NULL is returned (IEEE specials are not currently supported by MySQL).
  • If lat lies outside of [-90, 90] degrees, this is an error and NULL is returned.
  • If level is not in the range [0, 24], this is an error and NULL is returned.
  • The lon and lat arguments must be convertible to type DOUBLE PRECISION. If their actual type is BIGINT or DECIMAL, then the conversion can result in loss of precision and hence an inaccurate result. Loss of precision will not occur so long as the inputs are values of type DOUBLE PRECISION, FLOAT, REAL, INTEGER, SMALLINT or TINYINT.
Examples
SELECT objectId, ra_PS, decl_PS, scisql_s2HtmId(ra_PS, decl_PS, 20)
    FROM Object LIMIT 10;

scisql_s2HtmLevel

FUNCTION scisql_s2HtmLevel (
  id BIGINT HTM ID.
) RETURNS INTEGER
Returns the subdivision level of the given HTM ID.
Notes
  • If id is NULL or an invalid HTM ID, NULL is returned.
Examples
SELECT scisql_s2HtmLevel(32);

scisql_s2PtInBox

FUNCTION scisql_s2PtInBox (
  lon DOUBLE PRECISION, deg Longitude angle of point to test.
  lat DOUBLE PRECISION, deg Latitude angle of point to test.
  lonMin DOUBLE PRECISION, deg Minimum longitude angle of points in box.
  latMin DOUBLE PRECISION, deg Minimum latitude angle points in box.
  lonMax DOUBLE PRECISION, deg Maximum longitude angle of points in box.
  latMax DOUBLE PRECISION deg Maximum latitude angle of points in box.
) RETURNS INTEGER
Returns 1 if the point (lon, lat) lies inside the given longitude/latitude angle box, and 0 otherwise. The UDF handles range reduction of longitudes so that one can easily test whether a point is inside a box spanning the 0/360 degree longitude angle discontinuity. However, performing this test with a UDF call will inhibit the optimizer from using indexes, so in some cases it may be preferrable to express the test in SQL.
Notes
  • If any parameter is NULL, 0 is returned.
  • If any parameter is NaN or +/-Inf, this is an error and NULL is returned (IEEE specials are not currently supported by MySQL).
  • If lat, latMin or latMax lie outside of [-90, 90] degrees, this is an error and NULL is returned.
  • If both lonMin and lonMax lie in the range [0, 360], then lonMax may be less than lonMin. For example, a box with lonMin = 350 and lonMax = 10 includes points with longitudes in the ranges [350, 360) and [0, 10].
  • If either lonMin or lonMax lies outside of [0, 360], then lonMin must be less than or equal to lonMax. Otherwise, NULL is returned. However, the two values can be arbitrarily large. If they are separated by 360 degrees or more, then the box spans [0, 360) in longitude. Otherwise, lonMin and lonMax are range reduced. So for example, a spherical box with lonMin = 350 and lonMax = 370 includes longitudes in the ranges [350, 360) and [0, 10].
  • Input values must be convertible to type DOUBLE PRECISION. If their actual types are BIGINT or DECIMAL, then the conversion can result in loss of precision and hence an inaccurate result. Loss of precision will not occur so long as the inputs are values of type DOUBLE PRECISION, FLOAT, REAL, INTEGER, SMALLINT or TINYINT.
Examples
SELECT objectId, ra_PS, decl_PS
    FROM Object
    WHERE scisql_s2PtInBox(ra_PS, decl_PS, -10, 10, 10, 20) = 1;

scisql_s2PtInCPoly

FUNCTION scisql_s2PtInCPoly (
  lon DOUBLE PRECISION, deg Longitude angle of point to test.
  lat DOUBLE PRECISION, deg Latitude angle of point to test.
  poly VARBINARY Binary-string representation of polygon.
) RETURNS INTEGER
FUNCTION scisql_s2PtInCPoly (
  lon DOUBLE PRECISION, deg Longitude angle of point to test.
  lat DOUBLE PRECISION, deg Latitude angle of point to test.
  v1Lon DOUBLE PRECISION, deg Longitude angle of first polygon vertex.
  v1Lat DOUBLE PRECISION, deg Latitude angle of first polygon vertex.
  v2Lon DOUBLE PRECISION, deg Longitude angle of second polygon vertex.
  v2Lat DOUBLE PRECISION, deg Latitude angle of second polygon vertex.
 ...
) RETURNS INTEGER
Returns 1 if the point (lon, lat) lies inside the given spherical convex polygon and 0 otherwise. The polygon may be specified either as a VARBINARY byte string (as produced by scisql_s2CPolyToBin()), or as a sequence of at least 3 and at most 20 vertex pairs.
Notes
  • If any parameter is NULL, 0 is returned.
  • If any coordinate is NaN or +/-Inf, this is an error and NULL is returned (IEEE specials are not currently supported by MySQL).
  • If any latitude angle lies outside of [-90, 90] degrees, this is an error and NULL is returned.
  • Polygon vertices can be specified in either clockwise or counter-clockwise order. However, vertices are assumed to be hemispherical, to define edges that do not intersect except at vertices, and to define edges that form a convex polygon.
  • Coordinate values must be convertible to type DOUBLE PRECISION. If their actual types are BIGINT or DECIMAL, then the conversion can result in loss of precision and hence an inaccurate result. Loss of precision will not occur so long as the inputs are values of type DOUBLE PRECISION, FLOAT, REAL, INTEGER, SMALLINT or TINYINT.
Examples
SELECT scienceCcdExposureId
    FROM Science_Ccd_Exposure
    WHERE scisql_s2PtInCPoly(
        0.0, 0.0,
        llcRa, llcDecl,
        ulcRa, ulcDecl,
        urcRa, urcDecl,
        lrcRa, lrcDecl) = 1;

scisql_s2PtInCircle

FUNCTION scisql_s2PtInCircle (
  lon DOUBLE PRECISION, deg Longitude angle of point to test.
  lat DOUBLE PRECISION, deg Latitude angle of point to test.
  centerLon DOUBLE PRECISION, deg Circle center longitude angle.
  centerLat DOUBLE PRECISION, deg Circle center latitude angle.
  radius DOUBLE PRECISION deg Circle radius.
) RETURNS INTEGER
Returns 1 if the point (lon, lat) lies inside the given spherical circle and 0 otherwise.
Notes
  • If any parameter is NULL, 0 is returned.
  • If any parameter is NaN or +/-Inf, this is an error and NULL is returned (IEEE specials are not currently supported by MySQL).
  • If lat or centerLat lies outside of [-90, 90] degrees, this is an error and NULL is returned.
  • If radius is negative or greater than 180, this is an error and NULL is returned.
  • Input values must be convertible to type DOUBLE PRECISION. If their actual types are BIGINT or DECIMAL, then the conversion can result in loss of precision and hence an inaccurate result. Loss of precision will not occur so long as the inputs are values of type DOUBLE PRECISION, FLOAT, REAL, INTEGER, SMALLINT or TINYINT.
Examples
SELECT objectId, ra_PS, decl_PS
    FROM Object
    WHERE scisql_s2PtInCircle(ra_PS, decl_PS, 0.0, 0.0, 1.0) = 1;

scisql_s2PtInEllipse

FUNCTION scisql_s2PtInEllipse (
  lon DOUBLE PRECISION, deg Longitude angle of point to test.
  lat DOUBLE PRECISION, deg Latitude angle of point to test.
  centerLon DOUBLE PRECISION, deg Ellipse center longitude angle.
  centerLat DOUBLE PRECISION, deg Ellipse center latitude angle.
  semiMajorAxisAngle DOUBLE PRECISION, arcsec Semi-major axis length.
  semiMinorAxisAngle DOUBLE PRECISION, arcsec Semi-minor axis length.
  positionAngle DOUBLE PRECISION deg Ellipse position angle, east of north.
) RETURNS INTEGER
Returns 1 if the point (lon, lat) lies inside the given spherical ellipse and 0 otherwise.
Notes
  • If any parameter is NULL, 0 is returned.
  • If any parameter is NaN or +/-Inf, this is an error and NULL is returned (IEEE specials are not currently supported by MySQL).
  • If lat or centerLat lies outside of [-90, 90] degrees, this is an error and NULL is returned.
  • If semiMinorAxisAngle is negative or greater than semiMajorAxisAngle, this is an error and NULL is returned.
  • If semiMajorAxisAngle is greater than 36,000 arcsec (10 deg), this is an error and NULL is returned.
  • All inputs must be convertible to type DOUBLE PRECISION. If their actual types are BIGINT or DECIMAL, then the conversion can result in loss of precision and hence an inaccurate result. Loss of precision will not occur so long as the inputs are values of type DOUBLE PRECISION, FLOAT, REAL, INTEGER, SMALLINT or TINYINT.
Examples
SELECT objectId, ra_PS, decl_PS
    FROM Object
    WHERE scisql_s2PtInEllipse(ra_PS, decl_PS, 0, 0, 10, 5, 90) = 1;

Stored Procedures

scisql.scisql_s2CPolyRegion

PROCEDURE scisql.scisql_s2CPolyRegion (
IN poly VARBINARY(255), Binary-string representation of a polygon.
IN level INTEGER HTM subdivision level, must be in range [0, 24].
)
Creates a temporary table `scisql.Region` containing HTM ID ranges for the HTM triangles overlapping the given spherical convex polygon. A maximum of 256 ranges will be returned. If the number of ID ranges at the desired subdivision level exceeds this number, then the effective subdivision level is decreased. This strategy can reduce the number of ranges required to represent any input geometry to just 4, but makes the resulting range list a poorer (coarser, higher area) approximation to the input geometry.
Notes
  • The `scisql.Region` table is allowed to exist prior to calling scisql_s2CPolyRegion() - if it does, its contents are completely replaced.
  • Before using this stored procedure, an adminstrator must GRANT the required permissions (e.g. using scisql_grantPermissions()).
  • If any input is NULL, the procedure will fail.
  • If poly is not a valid binary-string representation of a polygon (e.g. as produced by scisql_s2CPolyToBin()), the procedure will fail.
  • If level does not lie in the range [0, 24], the procedure will fail.
Examples
CALL scisql.scisql_s2CPolyRegion(scisql_s2CPolyToBin(0,0, 1,0, 0,1), 20);
SELECT * FROM scisql.Region;

scisql.scisql_s2CircleRegion

PROCEDURE scisql.scisql_s2CircleRegion (
IN centerLon DOUBLE PRECISION, deg Circle center longitude angle.
IN centerLat DOUBLE PRECISION, deg Circle center latitude angle.
IN radius DOUBLE PRECISION, deg Circle radius.
IN level INTEGER HTM subdivision level, must be in range [0, 24].
)
Creates a temporary table `scisql.Region` containing HTM ID ranges for the HTM triangles overlapping the given circle. A maximum of 256 ranges will be returned. If the number of ID ranges at the desired subdivision level exceeds this number, then the effective subdivision level is decreased. This strategy can reduce the number of ranges required to represent any input geometry to just 4, but makes the resulting range list a poorer (coarser, higher area) approximation to the input geometry.
Notes
  • The `scisql.Region` table is allowed to exist prior to calling scisql_s2CircleRegion() - if it does, its contents are completely replaced.
  • Before using this stored procedure, an adminstrator must GRANT the required permissions (e.g. using scisql_grantPermissions()).
  • If any input is NULL, NaN or +/-Inf, the procedure will fail.
  • If centerLat does not lie in the [-90, 90] degree range, the procedure will fail.
  • If level does not lie in the range [0, 24], the procedure will fail.
Examples
CALL scisql.scisql_s2CircleRegion(0, 0, 0.5, 20);
SELECT * FROM scisql.Region;

These UDFs provide conversions between raw fluxes, calibrated (AB) fluxes and AB magnitudes.

User Defined Functions

scisql_abMagToDn

FUNCTION scisql_abMagToDn (
  mag DOUBLE PRECISION, mag AB magnitude to convert to a raw flux.
  fluxMag0 DOUBLE PRECISION DN Raw flux of a zero-magnitude object.
) RETURNS DOUBLE PRECISION
Converts an AB magnitude to a raw flux in DN.
Notes
  • All arguments must be convertible to type DOUBLE PRECISION.
  • If any argument is NULL, NaN, or +/-Inf, NULL is returned.
  • If fluxMag0 is negative or zero, NULL is returned.
Examples
SELECT scisql_abMagToDn(20.5, 3.0e+12);

scisql_abMagToDnSigma

FUNCTION scisql_abMagToDnSigma (
  mag DOUBLE PRECISION, mag AB magnitude.
  magSigma DOUBLE PRECISION, mag Standard deviation of mag.
  fluxMag0 DOUBLE PRECISION, DN Raw flux of a zero-magnitude object.
  fluxMag0Sigma DOUBLE PRECISION DN Standard deviation of fluxMag0.
) RETURNS DOUBLE PRECISION
Converts an AB magnitude error to a raw flux error in DN.
Notes
  • All arguments must be convertible to type DOUBLE PRECISION.
  • If any argument is NULL, NaN, or +/-Inf, NULL is returned.
  • If either magSigma or fluxMag0Sigma is negative, NULL is returned.
  • If fluxMag0 is negative or zero, NULL is returned.
Examples
SELECT scisql_abMagToDnSigma(20.5, 0.01, 3.0e+12, 0.0);

scisql_abMagToFlux

FUNCTION scisql_abMagToFlux (
  mag DOUBLE PRECISION mag AB magnitude to convert to a calibrated flux.
) RETURNS DOUBLE PRECISION
Converts an AB magnitude to a calibrated flux. The return value is in erg/cm2/sec/Hz.
Notes
  • The mag argument must be convertible to type DOUBLE PRECISION.
  • If the mag argument is NULL, NaN, or +/-Inf, NULL is returned.
Examples
SELECT scisql_abMagToFlux(20.5);

scisql_abMagToFluxSigma

FUNCTION scisql_abMagToFluxSigma (
  mag DOUBLE PRECISION, mag AB magnitude.
  magSigma DOUBLE PRECISION mag Standard deviation of mag.
) RETURNS DOUBLE PRECISION
Converts an AB magnitude error to a calibrated flux error. The return value is in erg/cm2/sec/Hz.
Notes
  • All arguments must be convertible to type DOUBLE PRECISION.
  • If any argument is NULL, NaN, or +/-Inf, NULL is returned.
  • If magSigma is negative, NULL is returned.
Examples
SELECT scisql_abMagToFluxSigma(20.5, 0.01);

scisql_abMagToNanojansky

FUNCTION scisql_abMagToNanojansky (
  mag DOUBLE PRECISION mag AB magnitude to convert to a calibrated flux.
) RETURNS DOUBLE PRECISION
Converts an AB magnitude to a calibrated flux. The return value is in nanojanskys.
Notes
  • The mag argument must be convertible to type DOUBLE PRECISION.
  • If the mag argument is NULL, NaN, or +/-Inf, NULL is returned.
Examples
SELECT scisql_abMagToNanojansky(20.5);

scisql_abMagToNanojanskySigma

FUNCTION scisql_abMagToNanojanskySigma (
  mag DOUBLE PRECISION, mag AB magnitude.
  magSigma DOUBLE PRECISION mag Standard deviation of mag.
) RETURNS DOUBLE PRECISION
Converts an AB magnitude error to a calibrated flux error. The return value is in nanojanskys.
Notes
  • All arguments must be convertible to type DOUBLE PRECISION.
  • If any argument is NULL, NaN, or +/-Inf, NULL is returned.
  • If magSigma is negative, NULL is returned.
Examples
SELECT scisql_abMagToNanojanskySigma(20.5, 0.01);

scisql_dnToAbMag

FUNCTION scisql_dnToAbMag (
  dn DOUBLE PRECISION, DN Raw flux to convert to an AB magnitude.
  fluxMag0 DOUBLE PRECISION DN Raw flux of a zero-magnitude object.
) RETURNS DOUBLE PRECISION
Converts a raw flux in DN to an AB magnitude.
Notes
  • All arguments must be convertible to type DOUBLE PRECISION.
  • If any argument is NULL, NaN, or +/-Inf, NULL is returned.
  • If either fluxMag0 or dn is negative or zero, NULL is returned.
Examples
SELECT scisql_dnToAbMag(src.psfFlux, ccd.fluxMag0)
    FROM Source AS src, Science_Ccd_Exposure ccd
    WHERE src.scienceCcdExposureId = ccd.scienceCcdExposureId
    LIMIT 10;

scisql_dnToAbMagSigma

FUNCTION scisql_dnToAbMagSigma (
  dn DOUBLE PRECISION, DN Raw flux.
  dnSigma DOUBLE PRECISION, DN Standard deviation of dn.
  fluxMag0 DOUBLE PRECISION, DN Raw flux of a zero-magnitude object.
  fluxMag0Sigma DOUBLE PRECISION DN Standard deviation of fluxMag0.
) RETURNS DOUBLE PRECISION
Converts a raw flux error to an AB magnitude error.
Notes
  • All arguments must be convertible to type DOUBLE PRECISION.
  • If any argument is NULL, NaN, or +/-Inf, NULL is returned.
  • If either dnSigma or fluxMag0Sigma is negative, NULL is returned.
  • If either dn or fluxMag0 is negative or zero, NULL is returned.
Examples
SELECT scisql_dnToAbMagSigma(
        src.psfFlux, src.psfFluxSigma, ccd.fluxMag0, ccd.fluxMag0Sigma)
    FROM Source AS src, Science_Ccd_Exposure ccd
    WHERE src.scienceCcdExposureId = ccd.scienceCcdExposureId
    LIMIT 10;

scisql_dnToFlux

FUNCTION scisql_dnToFlux (
  dn DOUBLE PRECISION, DN Raw flux to convert to a calibrated (AB) flux.
  fluxMag0 DOUBLE PRECISION DN Raw flux of a zero-magnitude object.
) RETURNS DOUBLE PRECISION
Converts a raw flux in DN to a calibrated (AB) flux. The return value will be in units of erg/cm2/sec/Hz.
Notes
  • All arguments must be convertible to type DOUBLE PRECISION.
  • If any argument is NULL, NaN, or +/-Inf, NULL is returned.
  • If fluxMag0 is negative or zero, NULL is returned.
Examples
-- An example using the LSST schema:
SELECT scisql_dnToFlux(src.psfFlux, ccd.fluxMag0)
    FROM Source AS src, Science_Ccd_Exposure ccd
    WHERE src.scienceCcdExposureId = ccd.scienceCcdExposureId
    LIMIT 10;

scisql_dnToFluxSigma

FUNCTION scisql_dnToFluxSigma (
  dn DOUBLE PRECISION, DN Raw flux.
  dnSigma DOUBLE PRECISION, DN Standard deviation of dn.
  fluxMag0 DOUBLE PRECISION, DN Raw flux of a zero-magnitude object.
  fluxMag0Sigma DOUBLE PRECISION DN Standard deviation of fluxMag0.
) RETURNS DOUBLE PRECISION
Converts a raw flux error to a calibrated (AB) flux error. The return value will be in units of erg/cm2/sec/Hz.
Notes
  • All arguments must be convertible to type DOUBLE PRECISION.
  • If any argument is NULL, NaN, or +/-Inf, NULL is returned.
  • If either dnSigma or fluxMag0Sigma is negative, NULL is returned.
  • If fluxMag0 is negative or zero, NULL is returned.
Examples
SELECT scisql_dnToAbMagSigma(
        src.psfFlux, src.psfFluxSigma, ccd.fluxMag0, ccd.fluxMag0Sigma)
    FROM Source AS src, Science_Ccd_Exposure ccd
    WHERE src.scienceCcdExposureId = ccd.scienceCcdExposureId
    LIMIT 10;

scisql_fluxToAbMag

FUNCTION scisql_fluxToAbMag (
  flux DOUBLE PRECISION erg/cm2/sec/Hz Calibrated flux to convert to an AB magnitude.
) RETURNS DOUBLE PRECISION
Converts a calibrated (AB) flux to an AB magnitude.
Notes
  • The flux argument must be convertible to type DOUBLE PRECISION.
  • If the flux argument is negative, zero, NULL, NaN, or +/-Inf, NULL is returned.
Examples
SELECT scisql_fluxToAbMag(rFlux_PS)
    FROM Object
    WHERE rFlux_PS IS NOT NULL
    LIMIT 10;

scisql_fluxToAbMagSigma

FUNCTION scisql_fluxToAbMagSigma (
  flux DOUBLE PRECISION, erg/cm2/sec/Hz Calibrated (AB) flux.
  fluxSigma DOUBLE PRECISION erg/cm2/sec/Hz Standard deviation of flux.
) RETURNS DOUBLE PRECISION
Converts a calibrated (AB) flux error to an AB magnitude error.
Notes
  • All arguments must be convertible to type DOUBLE PRECISION.
  • If any argument is NULL, NaN, or +/-Inf, NULL is returned.
  • If the flux argument is negative or zero, NULL is returned.
  • If the fluxSigma argument is negative, NULL is returned.
Examples
SELECT scisql_fluxToAbMagSigma(rFlux_PS, rFlux_PS_Sigma)
    FROM Object
    WHERE rFlux_PS IS NOT NULL and rFlux_PS_Sigma IS NOT NULL
    LIMIT 10;

scisql_fluxToDn

FUNCTION scisql_fluxToDn (
  flux DOUBLE PRECISION, erg/cm2/sec/Hz Calibrated flux to convert to a raw DN value.
  fluxMag0 DOUBLE PRECISION DN Raw flux of a zero-magnitude object.
) RETURNS DOUBLE PRECISION
Converts a calibrated (AB) flux to a raw DN value.
Notes
  • Both arguments must be convertible to type DOUBLE PRECISION.
  • If either argument is NULL, NaN, or +/-Inf, NULL is returned.
  • If fluxMag0 is negative or zero, NULL is returned.
Examples
SELECT scisql_fluxToDn(rFlux_PS, 3.0e+12)
    FROM Object
    WHERE rFlux_PS IS NOT NULL
    LIMIT 10;

scisql_fluxToDnSigma

FUNCTION scisql_fluxToDnSigma (
  flux DOUBLE PRECISION, erg/cm2/sec/Hz Calibrated (AB) flux.
  fluxSigma DOUBLE PRECISION, erg/cm2/sec/Hz Standard deviation of flux.
  fluxMag0 DOUBLE PRECISION, DN Raw flux of a zero-magnitude object.
  fluxMag0Sigma DOUBLE PRECISION DN Standard deviation of fluxMag0.
) RETURNS DOUBLE PRECISION
Converts a calibrated (AB) flux error to raw flux error in DN.
Notes
  • All arguments must be convertible to type DOUBLE PRECISION.
  • If any argument is NULL, NaN, or +/-Inf, NULL is returned.
  • If fluxMag0 is negative or zero, NULL is returned.
  • If either fluxSigma or fluxMag0Sigma is negative, NULL is returned.
Examples
SELECT scisql_fluxToDnSigma(
        rFlux_PS, rFlux_PS_Sigma, 3.0e+12, 0.0)
    FROM Object
    WHERE rFlux_PS IS NOT NULL
    LIMIT 10;

scisql_nanojanskyToAbMag

FUNCTION scisql_nanojanskyToAbMag (
  flux DOUBLE PRECISION nanojansky Calibrated flux to convert to an AB magnitude.
) RETURNS DOUBLE PRECISION
Converts a calibrated (AB) flux to an AB magnitude.
Notes
  • The flux argument must be convertible to type DOUBLE PRECISION.
  • If the flux argument is negative, zero, NULL, NaN, or +/-Inf, NULL is returned.
Examples
SELECT scisql_nanojanskyToAbMag(rFlux_PS)
    FROM Object
    WHERE rFlux_PS IS NOT NULL
    LIMIT 10;

scisql_nanojanskyToAbMagSigma

FUNCTION scisql_nanojanskyToAbMagSigma (
  flux DOUBLE PRECISION, nanojansky Calibrated (AB) flux.
  fluxSigma DOUBLE PRECISION nanojansky Standard deviation of flux.
) RETURNS DOUBLE PRECISION
Converts a calibrated (AB) flux error to an AB magnitude error.
Notes
  • All arguments must be convertible to type DOUBLE PRECISION.
  • If any argument is NULL, NaN, or +/-Inf, NULL is returned.
  • If the flux argument is negative or zero, NULL is returned.
  • If the fluxSigma argument is negative, NULL is returned.
Examples
SELECT scisql_nanojanskyToAbMagSigma(rFlux_PS, rFlux_PS_Sigma)
    FROM Object
    WHERE rFlux_PS IS NOT NULL and rFlux_PS_Sigma IS NOT NULL
    LIMIT 10;

These UDFs provide the ability to compute medians and percentiles. Averages and standard deviations can already be computed with the AVG and STDDEV SQL constructs.

User Defined Functions

scisql_median

AGGREGATE FUNCTION scisql_median (
  value DOUBLE PRECISION Value, column name or expression yielding input values.
) RETURNS DOUBLE PRECISION
Returns the median of a GROUP of values.
Notes
  • NULL and NaN values are ignored. MySQL does not currently support storage of NaNs. However, their presence is checked for to ensure reasonable behaviour if a future MySQL release does end up supporting them.
  • If all input values for a GROUP are NULL/NaN, then NULL is returned.
  • If there are no inputs, NULL is returned.
  • If there are an even number of elements in the input GROUP, the return value is the mean of the two middle elements in a sorted copy of the GROUP.
  • As previously mentioned, input values are coerced to be of type DOUBLE PRECISION. If the inputs are of type BIGINT or DECIMAL, then the coercion can result in loss of precision and hence an inaccurate result. Loss of precision will not occur so long as median() is called on values of type DOUBLE PRECISION, FLOAT, INTEGER, SMALLINT, or TINYINT.
  • This UDF can handle a maximum of 227 (134,217,728) input values per GROUP.
Examples
SELECT objectId, scisql_median(psfFlux)
    FROM Source
    WHERE objectId IS NOT NULL
    GROUP BY objectId;

scisql_percentile

AGGREGATE FUNCTION scisql_percentile (
  value DOUBLE PRECISION, Value, column name, or expression yielding input values.
  percent DOUBLE PRECISION Desired percentile, must lie in the range [0, 100].
) RETURNS DOUBLE PRECISION
Returns the desired percentile of a GROUP of values. Given a GROUP of N DOUBLE PRECISION values, percentile returns the value V such that at most floor(N * percent/100.0) of the values are less than V and at most N - floor(N * percent/100.0) are greater. The percent argument must not vary across the elements of a GROUP for which a percentile is being computed, or the return value is undefined.
Notes
  • NULL and NaN values are ignored. MySQL does not currently support storage of NaNs. However, their presence is checked for to ensure reasonable behaviour if a future MySQL release does end up supporting them.
  • If all inputs are NULL/NaN, then NULL is returned.
  • If there are no input values, NULL is returned.
  • If the input GROUP contains exactly one value, that value is returned.
  • If the percent argument is NULL or does not lie in the range [0, 100], NULL is returned.
  • If (N - 1) * percent/100.0 = K is an integer, the value returned is the K-th smallest element in a sorted copy of the input GROUP A. Otherwise, the return value is A[k] + f*(A[k + 1] - A[k]), where k = floor(K) and f = K - k.
  • As previously mentioned, input values are coerced to be of type DOUBLE PRECISION. If the inputs are of type BIGINT or DECIMAL, then the coercion can result in loss of precision and hence an inaccurate result. Loss of precision will not occur so long as median() is called on values of type DOUBLE PRECISION, FLOAT, INTEGER, SMALLINT, or TINYINT.
  • This UDF can handle a maximum of 227 (134,217,728) input values per GROUP.
Examples
SELECT objectId,
       scisql_percentile(psfFlux, 25) AS firstQuartile,
       scisql_percentile(psfFlux, 75) AS thirdQuartile
    FROM Source
    WHERE objectId IS NOT NULL
    GROUP BY objectId
    LIMIT 10;

These UDFs and stored procedures are either administrative, internal, or informational - they are not directly useful for scientific computation / queries.

User Defined Functions

[internal] scisql_extractInt64

FUNCTION scisql_extractInt64 (
  data STRING, Byte string to extract a 64-bit integer from.
  idx INTEGER The index of the 64-bit integer to extract.
) RETURNS BIGINT
Extracts a 64-bit integer stored in host byte order from a binary string.
Notes
  • If any argument is NULL, NULL is returned.
  • If idx is negative or out of range, NULL is returned.

scisql_getVersion

FUNCTION scisql_getVersion ( ) RETURNS CHAR
Returns the version of the sciSQL library in use.
Examples
SELECT scisql_getVersion();

[internal] scisql_raiseError

FUNCTION scisql_raiseError ( ) RETURNS BIGINT
FUNCTION scisql_raiseError (
  message STRING Error message.
) RETURNS BIGINT
Fails with an optional error message.

This UDF exists solely because MySQL 5.1 does not support SIGNAL in stored procedures. The error messages it produces are slightly more readable than the results of hacks like SELECT * FROM `Lorem ipsum dolor`.

Examples
SELECT scisql_raiseError('Lorem ipsum dolor');

Stored Procedures

scisql.scisql_grantPermissions

PROCEDURE scisql.scisql_grantPermissions (
IN user VARCHAR(255), User name - may not contain wildcards.
IN host VARCHAR(255) Host name - wildcards ('%') are allowed.
)
Gives a user connecting from the specified host permission to call sciSQL stored procedures and to create/use temporary tables in the scisql database.
Notes
  • You must have MySQL admin priviledges (including GRANT OPTION) to call this stored procedure.
Examples
CALL scisql.scisql_grantPermissions('bob', 'localhost');