1

Hey I'm having an issue with altering table partitions using JDBI. Here is an example of the query I'm trying to run:

ALTER TABLE table1 DROP PARTITION P_1

This runs fine in MySQL when dropping the partition "P_1" from the table "table1."

I've implemented it in my java code as the following:

@SqlUpdate("ALTER TABLE table1 DROP PARTITION :partition;")
public void deletePartition(@Bind("partition") String partition);

And call this function as such

deletePartition("P_1")

However, this results in the following error:

Causing: org.skife.jdbi.v2.exceptions.UnableToExecuteStatementException: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''P_1'' at line 1 [statement:"ALTER TABLE table1 DROP PARTITION :partition;", located:"ALTER TABLE table1 DROP PARTITION :partition;", rewritten:"/* HiveDropBoxDBI.deletePartition */ ALTER TABLE table1 DROP PARTITION ?;", arguments:{ positional:{}, named:{partition:'P_1'}, finder:[]}]

Is this functionality not supported? Or am I missing something with my syntax?

Thanks

1
  • Thanks for editing out the spoiler! Didn't realize that's what the "+" symbol did, as it was part of the stack trace.
    – Andrew
    Aug 19, 2015 at 18:45

2 Answers 2

1

We need to use @Define for changing the query. @Bind is only for used for binding parameters (like some_field = :value).

  @SqlUpdate("ALTER TABLE table1 DROP PARTITION :partition;")
  public void deletePartition(@Bind("partition") String partition);
1

You can't use bind parameters for DDL such as CREATE, ALTER and DROP.

In order to drop your partition, you must concatenate the partition name onto the query instead.

However, taking a partition name and appending it straight into a SQL string which gets executed is a recipe for a security vulnerability. Consider some of the following:

  • 'escaping' the partition name with backticks,
  • checking that the partition name contains only certain whitelisted characters (e.g. alphanumerics and underscores),
  • querying the INFORMATION_SCHEMA.PARTITIONS table to see if the partition you're trying to drop exists. (This might not be a good idea if there is a chance that the partition may be created or dropped between checking for its existence and dropping it. I don't know your application well enough to say whether this would be a problem.)

As JDBI relies on constant strings for @SqlUpdate annotations, you will not be able to use JDBI in this way to drop partitions, unless you only ever want to drop the same partition.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.