query in drop_partitions
maks
Community Edition User ✭
Hello. I have a table divided into partitions by date. I want to delete the oldest partition (I calculate the value of this partition so: SELECT MAX (CAST(time AS DATE)) FROM mytable;
Now I want to delete this partition, I try like this: SELECT DROP_PARTITIONS ('mytable','SELECT MAX (CAST(time AS DATE)) FROM mytable','SELECT MAX (CAST(time AS DATE)) FROM mytable');
But I get an error:
Failed to convert to internal form: Invalid input syntax for date
ERROR 3721: Invalid partition key
How can I use query in DROP_PARTITIONS?
Tagged:
0
Best Answer
-
marcothesane - Select Field - Administrator
With this exact script:
-- find the oldest partition key in the table, and embed that into a working SQL statement \t -- tuples only in output \a -- do not align the output SELECT 'SELECT DROP_PARTITIONS('||CHR(10)|| ' ''flex.drp_p'''||CHR(10)|| ', '||QUOTE_LITERAL(MIN(prk)::VARCHAR(10))||CHR(10)|| ', '||QUOTE_LITERAL(MIN(prk)::VARCHAR(10))||CHR(10)|| ');' FROM flex.drp_p;
I can do it without creating an intermediate file, by piping the first output of vsql into a second vsql session:
$ vsql -qf d.sql | vsql SET DROP_PARTITIONS ------------------- Partition dropped (1 row)
5
Answers
@maks, are you selecting the partition key column for the drop? (from the error)
What output you get for your select SQL?
Try this:
SELECT MAX(DATE(time)) FROM mytable;
This will simplify the CAST/CONVERT requirement. Partition keys are stored as VARCHAR so another cast step may be needed.
I get date value, for example, 2020-05-23
I try but I get the same error Failed to convert to internal form: Invalid input syntax for date
Ensure that you are using the right partition key format:
Try:
You'll see the format of the keys.
But:
So, SQL generating SQL:
Result:
Once generated that statement (to file), you just run that file.
marcothesane, I'm very sorry but I don't understand how to do from your query real query that delete partitions
Here's my full example:
The table:
Here is a query to find out all partition keys of the table
drp_p
:The table is partitioned by
prk
, which is a DATE. The oldest partition key is 2019-01-01. This would need to be dropped.You cannot use subselects in a call to
DROP_PARTITIONS()
:, as I said in my previous post.The solution is to generate a new SQL script, while querying for the oldest partition key.
If the script above is in a file called
generate.sql
, you can generate it like so:$ vsql -Xqf generate.sql > doit.sql
Find the output of the query above here below.
Then, go:
$ vsql -f doit.sql
, to actually drop the partition.marcothesane, thanks a lot. In file doit.sql I have additional strings: first string
?column?
and last string(1 row)
When I try to run doit.sql:
vsql -f doit.sql
I get an error:
ERROR 4856: Syntax error at or near "?" at character 1 LINE 1: ?column?
ERROR 4856: Syntax error at or near "1" at character 2 LINE 1: (1 row)
If I delete this additional string manually the script run correct
Are these additional strings OK? (I must delete them, for example, with bash/python or can they be deleted somehow in the script generate.sql?
You did not add the lines
\t
and\a
to your generator script. They would have removed the?column?
line and the(1 row)
line from the report.marcothesane, thanks a lot!!!