Skip to content

Commit

Permalink
addressing PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jortiz16 committed Jul 16, 2016
1 parent 795fbd8 commit 2839454
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
*
*/
public class CreateFunctionEncoding extends MyriaApiEncoding {
@Required public String functionName;
@Required public String functionDefinition;
@Required public Schema functionOutputSchema;
@Required public String name;
@Required public String definition;
@Required public Schema outputSchema;
public Set<Integer> workers;
}
46 changes: 11 additions & 35 deletions src/edu/washington/escience/myria/coordinator/MasterCatalog.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public final class MasterCatalog {
+ " col_name TEXT,\n"
+ " col_type TEXT NOT NULL,\n"
+ " is_indexed INTEGER NOT NULL, \n"
+ " ascending_order INTEGER, \n"
+ " is_ascending_index INTEGER, \n"
+ " FOREIGN KEY (user_name,program_name,relation_name) REFERENCES relations ON DELETE CASCADE);";
/** Create an index on the relation_schema table. */
private static final String CREATE_RELATION_SCHEMA_INDEX =
Expand Down Expand Up @@ -1713,13 +1713,13 @@ protected Void job(final SQLiteConnection sqliteConnection)
*/

public void registerFunction(
@Nonnull final String functionName,
@Nonnull final String functionDefinition,
@Nonnull final String functionOutputSchema)
@Nonnull final String name,
@Nonnull final String definition,
@Nonnull final String outputSchema)
throws CatalogException {
Objects.requireNonNull(functionName, "function name");
Objects.requireNonNull(functionDefinition, "function definition");
Objects.requireNonNull(functionOutputSchema, "function output schema");
Objects.requireNonNull(name, "function name");
Objects.requireNonNull(definition, "function definition");
Objects.requireNonNull(outputSchema, "function output schema");
if (isClosed) {
throw new CatalogException("Catalog is closed.");
}
Expand All @@ -1733,14 +1733,12 @@ public void registerFunction(
protected Void job(final SQLiteConnection sqliteConnection)
throws CatalogException, SQLiteException {
try {
deleteFunctionIfExists(sqliteConnection, functionName);

SQLiteStatement statement =
sqliteConnection.prepare(
"INSERT INTO registered_functions (function_name, function_definition, function_outputSchema) VALUES (?,?,?);");
statement.bind(1, functionName);
statement.bind(2, functionDefinition);
statement.bind(3, functionOutputSchema);
"INSERT OR REPLACE INTO registered_functions (function_name, function_definition, function_outputSchema) VALUES (?,?,?);");
statement.bind(1, name);
statement.bind(2, definition);
statement.bind(3, outputSchema);
statement.stepThrough();
statement.dispose();
statement = null;
Expand All @@ -1756,28 +1754,6 @@ protected Void job(final SQLiteConnection sqliteConnection)
}
}

/**
* Delete the specified function from the catalog, if it exists.
*
* @param sqliteConnection the connection to the SQLite database
* @param functionName the function to be deleted.
* @throws CatalogException if there is an error
*/
private void deleteFunctionIfExists(
@Nonnull final SQLiteConnection sqliteConnection, @Nonnull final String functionName)
throws CatalogException {
try {
String sql = String.format("DELETE FROM registered_functions WHERE function_name=?;");
SQLiteStatement statement = sqliteConnection.prepare(sql);
statement.bind(1, functionName);
statement.stepThrough();
statement.dispose();
statement = null;
} catch (final SQLiteException e) {
throw new CatalogException(e);
}
}

/**
* Add the metadata for one or more relations to the catalog.
*
Expand Down
19 changes: 12 additions & 7 deletions src/edu/washington/escience/myria/parallel/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -1100,11 +1100,16 @@ public long createView(

/**
* Create a function and register it in the catalog
*
* @param name the name of the function
* @param definition the function definition (must be postgres specific)
* @param outputSchema the output schema of the function
* @return the status of the function
*/
public String createFunction(
final String functionName,
final String functionDefinition,
final String functionOutputSchema,
final String name,
final String definition,
final String outputSchema,
final Set<Integer> workers)
throws DbException, InterruptedException {
String response = "Created Function";
Expand All @@ -1113,14 +1118,14 @@ public String createFunction(
actualWorkers = getWorkers().keySet();
}

/* Validate the command */
/* Postgres specific syntax - Validate the command */
Pattern pattern = Pattern.compile("(CREATE FUNCTION)([\\s\\S]*)(LANGUAGE SQL;)");
Matcher matcher = pattern.matcher(functionDefinition);
Matcher matcher = pattern.matcher(definition);

if (matcher.matches()) {
/* Add a replace statement */
String modifiedReplaceFunction =
functionDefinition.replace("CREATE FUNCTION", "CREATE OR REPLACE FUNCTION");
definition.replace("CREATE FUNCTION", "CREATE OR REPLACE FUNCTION");

/* Create the function */
try {
Expand Down Expand Up @@ -1150,7 +1155,7 @@ public String createFunction(

/* Register the function to the catalog */
try {
catalog.registerFunction(functionName, functionDefinition, functionOutputSchema);
catalog.registerFunction(name, definition, outputSchema);
} catch (CatalogException e) {
throw new DbException(e);
}
Expand Down

0 comments on commit 2839454

Please sign in to comment.