Skip to content

Commit

Permalink
Merge pull request #174 from jburke-cadc/CADC-13452
Browse files Browse the repository at this point in the history
CADC-13452 ingest an existing table into the tap_schema
  • Loading branch information
pdowler authored Sep 17, 2024
2 parents 524d1a3 + 5538de4 commit 96d4187
Show file tree
Hide file tree
Showing 18 changed files with 1,738 additions and 221 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ public String toString() {
*/
protected final Map<TapDataType, TypePair> dataTypes = new HashMap<>();

/**
* Mapping of database data types to VOTable data types.
*/
protected final Map<String, TapDataType> dbDataTypes = new HashMap<>();

public BasicDataTypeMapper() {
// votable type -> db type
dataTypes.put(TapDataType.BOOLEAN, new TypePair("BOOLEAN", Types.BOOLEAN));
Expand All @@ -131,6 +136,25 @@ public BasicDataTypeMapper() {
dataTypes.put(TapDataType.STRING, new TypePair("CHAR", Types.CHAR));
dataTypes.put(TapDataType.TIMESTAMP, new TypePair("TIMESTAMP", Types.TIMESTAMP));
dataTypes.put(TapDataType.URI, new TypePair("CHAR", Types.CHAR));

// DatabaseMetadata -> TAP_DATA_TYPE
// TYPE_NAME DATA_TYPE TAP_DATA_TYPE
// bool -7 BOOLEAN
// bpchar 1 CHAR
// varchar 4096 12 STRING or URI
// int2 5 SHORT
// int4 4 INTEGER
// int8 -5 LONG
// float4 7 FLOAT
// float8 8 DOUBLE
// timestamp 93 TIMESTAMP
dbDataTypes.put("varchar", TapDataType.STRING);
dbDataTypes.put("int2", TapDataType.SHORT);
dbDataTypes.put("int4", TapDataType.INTEGER);
dbDataTypes.put("int8", TapDataType.LONG);
dbDataTypes.put("float4", TapDataType.FLOAT);
dbDataTypes.put("float8", TapDataType.DOUBLE);
dbDataTypes.put("timestamp", TapDataType.TIMESTAMP);
}

/**
Expand Down Expand Up @@ -189,8 +213,21 @@ public String getIndexUsingQualifier(ColumnDesc columnDesc, boolean unique) {
public String getIndexColumnOperator(ColumnDesc columnDesc) {
return null;
}



/**
* Maps standard database datatypes to a TapDatatype. Database specific datatypes can by mapped in a sub class database specific mapper.
*
* @param datatype database datatype
* @return TapDatatype
*/
public TapDataType getTapDataType(String datatype) {
TapDataType tapDataType = dbDataTypes.get(datatype);
if (tapDataType != null) {
return tapDataType;
}
throw new UnsupportedOperationException("Unknown database datatype: " + datatype);
}

/**
* Find or create a TypePair for the specified data type. The current implementation
* looks for exact matches in the dataTypes map and, if not found, it rechecks with
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
import ca.nrc.cadc.dali.Point;
import ca.nrc.cadc.dali.Polygon;
import ca.nrc.cadc.tap.schema.ColumnDesc;
import ca.nrc.cadc.tap.schema.TapDataType;

/**
* Interface to convert ADQL data types to a database
Expand Down Expand Up @@ -182,4 +183,12 @@ public interface DatabaseDataType
Object getArrayObject(float[] val);

Object getArrayObject(double[] val);

/**
* Convert a database data type to a a TAP data type.
* @param datatype the database data type
* @return a TapDataType
*/
TapDataType getTapDataType(String datatype);

}
224 changes: 224 additions & 0 deletions cadc-tap-schema/src/main/java/ca/nrc/cadc/tap/db/TableIngester.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
/*
************************************************************************
******************* CANADIAN ASTRONOMY DATA CENTRE *******************
************** CENTRE CANADIEN DE DONNÉES ASTRONOMIQUES **************
*
* (c) 2024. (c) 2024.
* Government of Canada Gouvernement du Canada
* National Research Council Conseil national de recherches
* Ottawa, Canada, K1A 0R6 Ottawa, Canada, K1A 0R6
* All rights reserved Tous droits réservés
*
* NRC disclaims any warranties, Le CNRC dénie toute garantie
* expressed, implied, or énoncée, implicite ou légale,
* statutory, of any kind with de quelque nature que ce
* respect to the software, soit, concernant le logiciel,
* including without limitation y compris sans restriction
* any warranty of merchantability toute garantie de valeur
* or fitness for a particular marchande ou de pertinence
* purpose. NRC shall not be pour un usage particulier.
* liable in any event for any Le CNRC ne pourra en aucun cas
* damages, whether direct or être tenu responsable de tout
* indirect, special or general, dommage, direct ou indirect,
* consequential or incidental, particulier ou général,
* arising from the use of the accessoire ou fortuit, résultant
* software. Neither the name de l'utilisation du logiciel. Ni
* of the National Research le nom du Conseil National de
* Council of Canada nor the Recherches du Canada ni les noms
* names of its contributors may de ses participants ne peuvent
* be used to endorse or promote être utilisés pour approuver ou
* products derived from this promouvoir les produits dérivés
* software without specific prior de ce logiciel sans autorisation
* written permission. préalable et particulière
* par écrit.
*
* This file is part of the Ce fichier fait partie du projet
* OpenCADC project. OpenCADC.
*
* OpenCADC is free software: OpenCADC est un logiciel libre ;
* you can redistribute it and/or vous pouvez le redistribuer ou le
* modify it under the terms of modifier suivant les termes de
* the GNU Affero General Public la “GNU Affero General Public
* License as published by the License” telle que publiée
* Free Software Foundation, par la Free Software Foundation
* either version 3 of the : soit la version 3 de cette
* License, or (at your option) licence, soit (à votre gré)
* any later version. toute version ultérieure.
*
* OpenCADC is distributed in the OpenCADC est distribué
* hope that it will be useful, dans l’espoir qu’il vous
* but WITHOUT ANY WARRANTY; sera utile, mais SANS AUCUNE
* without even the implied GARANTIE : sans même la garantie
* warranty of MERCHANTABILITY implicite de COMMERCIALISABILITÉ
* or FITNESS FOR A PARTICULAR ni d’ADÉQUATION À UN OBJECTIF
* PURPOSE. See the GNU Affero PARTICULIER. Consultez la Licence
* General Public License for Générale Publique GNU Affero
* more details. pour plus de détails.
*
* You should have received Vous devriez avoir reçu une
* a copy of the GNU Affero copie de la Licence Générale
* General Public License along Publique GNU Affero avec
* with OpenCADC. If not, see OpenCADC ; si ce n’est
* <http://www.gnu.org/licenses/>. pas le cas, consultez :
* <http://www.gnu.org/licenses/>.
*
* : 5 $
*
************************************************************************
*/

package ca.nrc.cadc.tap.db;

import ca.nrc.cadc.auth.AuthenticationUtil;
import ca.nrc.cadc.db.DatabaseTransactionManager;
import ca.nrc.cadc.tap.PluginFactory;
import ca.nrc.cadc.tap.schema.ADQLIdentifierException;
import ca.nrc.cadc.tap.schema.ColumnDesc;
import ca.nrc.cadc.tap.schema.SchemaDesc;
import ca.nrc.cadc.tap.schema.TableDesc;
import ca.nrc.cadc.tap.schema.TapDataType;
import ca.nrc.cadc.tap.schema.TapPermissions;
import ca.nrc.cadc.tap.schema.TapSchemaDAO;
import ca.nrc.cadc.tap.schema.TapSchemaUtil;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.security.auth.Subject;
import javax.sql.DataSource;
import org.apache.log4j.Logger;

public class TableIngester {
private static final Logger log = Logger.getLogger(TableIngester.class);

private final DataSource dataSource;
private final DatabaseDataType databaseDataType;
private final TapSchemaDAO tapSchemaDAO;

public TableIngester(DataSource dataSource) {
this.dataSource = dataSource;
PluginFactory pluginFactory = new PluginFactory();
this.tapSchemaDAO = pluginFactory.getTapSchemaDAO();
this.tapSchemaDAO.setDataSource(dataSource);
this.databaseDataType = pluginFactory.getDatabaseDataType();
log.debug("loaded: " + databaseDataType.getClass().getName());
}

public void ingest(String schemaName, String tableName) {
// create the table description
TableDesc ingestTable;
try {
ingestTable = createTableDesc(schemaName, tableName);
} catch (SQLException e) {
throw new IllegalArgumentException(String.format("error getting database metadata for %s because: %s",
tableName, e.getMessage()));
}

// check the table is valid ADQL
try {
TapSchemaUtil.checkValidTableName(ingestTable.getTableName());
} catch (ADQLIdentifierException ex) {
throw new IllegalArgumentException("invalid table name: " + ingestTable.getTableName(), ex);
}
try {
for (ColumnDesc cd : ingestTable.getColumnDescs()) {
TapSchemaUtil.checkValidIdentifier(cd.getColumnName());
}
} catch (ADQLIdentifierException ex) {
throw new IllegalArgumentException(ex.getMessage());
}

// make caller the table owner
Subject caller = AuthenticationUtil.getCurrentSubject();
TapPermissions tapPermissions = new TapPermissions();
tapPermissions.owner = caller;
ingestTable.tapPermissions = tapPermissions;

DatabaseTransactionManager tm = new DatabaseTransactionManager(dataSource);
try {
tm.startTransaction();

// TODO: change getSchema() above to lockSchema() once implemented to prevent duplicate put
// add the schema to the tap_schema if it doesn't exist
SchemaDesc schemaDesc = tapSchemaDAO.getSchema(schemaName, true);
if (schemaDesc != null) {
log.debug(String.format("existing schema '%s' in tap_schema", schemaDesc.getSchemaName()));
}

// add the table to the tap_schema
TableDesc tableDesc = tapSchemaDAO.getTable(tableName, true);
if (tableDesc != null) {
throw new IllegalStateException(String.format("table already exists in tap_schema: %s", tableName));
}
tapSchemaDAO.put(ingestTable);
log.debug(String.format("added table '%s' to tap_schema", tableName));

tm.commitTransaction();
} catch (Exception ex) {
try {
log.error("update tap_schema failed - rollback", ex);
tm.rollbackTransaction();
log.error("update tap_schema failed - rollback: OK");
} catch (Exception oops) {
log.error("update tap_schema failed - rollback : FAIL", oops);
}
throw new RuntimeException(String.format("failed to update tap_schema with %s", tableName), ex);
} finally {
if (tm.isOpen()) {
log.error("BUG: open transaction in finally - trying to rollback");
try {
tm.rollbackTransaction();
log.error("BUG: rollback in finally: OK");
} catch (Exception oops) {
log.error("BUG: rollback in finally: FAIL", oops);
}
throw new RuntimeException("BUG: open transaction in finally");
}
}
}

protected TableDesc createTableDesc(String schemaName, String tableName)
throws SQLException {
log.debug(String.format("creating TableDesc for %s %s", schemaName, tableName));
// get the table metadata
String unqualifiedTableName = getUnqualifiedTableNameFromTable(tableName);
DatabaseMetaData databaseMetaData = dataSource.getConnection().getMetaData();
log.debug(String.format("querying DatabaseMetadata for schema=%s table=%s", schemaName, unqualifiedTableName));
//TODO too pg specific? table names are stored lower case in the system tables queried for the metadata
ResultSet columnInfo = databaseMetaData.getColumns(null, schemaName, unqualifiedTableName.toLowerCase(), null);
ResultSet indexInfo = databaseMetaData.getIndexInfo(null, schemaName, unqualifiedTableName.toLowerCase(), false, false);
// get column names for indexed columns
List<String> indexedColumns = new ArrayList<String>();
while (indexInfo.next()) {
String indexedColumn = indexInfo.getString("COLUMN_NAME");
indexedColumns.add(indexedColumn);
log.debug("indexed column: " + indexedColumn);
}

// build TableDesc
TableDesc tableDesc = new TableDesc(schemaName, tableName);
tableDesc.tableType = TableDesc.TableType.TABLE;
tableDesc.apiCreated = true;
log.debug(String.format("creating TableDesc %s %s", schemaName, tableName));
while (columnInfo.next()) {
String columnName = columnInfo.getString("COLUMN_NAME");
String columnType = columnInfo.getString("TYPE_NAME");
TapDataType tapDataType = this.databaseDataType.getTapDataType(columnType);
log.debug(String.format("creating ColumnDesc %s %s %s", tableName, columnName, tapDataType));
ColumnDesc columnDesc = new ColumnDesc(tableName, columnName, tapDataType);
columnDesc.indexed = indexedColumns.contains(columnName);
tableDesc.getColumnDescs().add(columnDesc);
}
return tableDesc;
}

String getUnqualifiedTableNameFromTable(String tableName) {
String[] st = tableName.split("[.]");
if (st.length == 2) {
return st[1];
}
throw new IllegalArgumentException("invalid table name: " + tableName + " (expected: <schema>.<table>)");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public void doAction() throws Exception {
ts.setDataSource(ds);

tm = new DatabaseTransactionManager(ds);
checkDropTablePermission(ts, tableName);
TablesAction.checkDropTablePermission(ts, tableName, logInfo);

try {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public void doAction() throws Exception {

TapSchemaDAO dao = getTapSchemaDAO();
if (tableName != null) {
checkTableReadPermissions(dao, tableName);
checkTableReadPermissions(dao, tableName, logInfo);
TableDesc td = dao.getTable(tableName);
if (td == null) {
// currently, permission check already threw this
Expand All @@ -134,7 +134,7 @@ public void doAction() throws Exception {
syncOutput.setHeader("Content-Type", "text/xml");
tw.write(td, new OutputStreamWriter(syncOutput.getOutputStream()));
} else if (schemaName != null) {
checkViewSchemaPermissions(dao, schemaName);
checkViewSchemaPermissions(dao, schemaName, logInfo);
// TODO: TapSchemaDAO only supports schema only, ok for detail=min
// should at least list tables for default detail
// should provide columns at detail=max
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ public void doAction() throws Exception {
TapSchemaDAO dao = getTapSchemaDAO();
TapPermissions permissions = null;
if (Util.isSchemaName(name)) {
permissions = checkViewSchemaPermissions(dao, name);
permissions = checkViewSchemaPermissions(dao, name, logInfo);
} else if (Util.isTableName(name)) {
permissions = checkViewTablePermissions(dao, name);
permissions = checkViewTablePermissions(dao, name, logInfo);
} else {
throw new IllegalArgumentException("No such object: " + name);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,13 @@ public void doAction() throws Exception {
// get the permissions first to ensure table exists and to ensure user
// is allowed to view/modifiy permissions.
if (Util.isSchemaName(name)) {
checkModifySchemaPermissions(dao, name);
TablesAction.checkModifySchemaPermissions(dao, name, logInfo);
TapPermissions perms = dao.getSchemaPermissions(name);
modifyPermissions(perms);
log.debug("Setting schema permissions to: \n" + perms);
dao.setSchemaPermissions(name, perms);
} else if (Util.isTableName(name)) {
checkModifyTablePermissionsPermissions(dao, name);
TablesAction.checkModifyTablePermissions(dao, name, logInfo);
TapPermissions perms = dao.getTablePermissions(name);
modifyPermissions(perms);
log.debug("Setting table permissions to: \n" + perms);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public void doAction() throws Exception {
checkIsAdmin();
} else {
// create table
checkSchemaWritePermissions(ts, schemaName);
TablesAction.checkSchemaWritePermissions(ts, schemaName, logInfo);
}

if (tableName != null) {
Expand Down
Loading

0 comments on commit 96d4187

Please sign in to comment.