Updating table schema

Updating table schema in RT mode

  1. ALTER TABLE table ADD COLUMN column_name [{INTEGER|INT|BIGINT|FLOAT|BOOL|MULTI|MULTI64|JSON|STRING|TIMESTAMP|TEXT [INDEXED [ATTRIBUTE]]}] [engine='columnar']
  2. ALTER TABLE table DROP COLUMN column_name

It supports adding one field at a time for RT tables. Supported data types are:

  • int - integer attribute
  • timestamp - timestamp attribute
  • bigint - big integer attribute
  • float - float attribute
  • bool - boolean attribute
  • multi - multi-valued integer attribute
  • multi64 - multi-valued bigint attribute
  • json - json attribute
  • string / text attribute / string attribute - string attribute
  • text / text indexed stored / string indexed stored - full-text indexed field with original value stored in docstore
  • text indexed / string indexed - full-text indexed field, indexed only (the original value is not stored in docstore)
  • text indexed attribute / string indexed attribute - full text indexed field + string attribute (not storing the original value in docstore)
  • text stored / string stored - the value will be only stored in docstore, not full-text indexed, not a string attribute
  • adding engine='columnar' to any attribute (except for json) will make it stored in the columnar storage

Important notes:

  • ❗It’s recommended to backup table files before ALTERing it to avoid data corruption in case of a sudden power interruption or other similar issues.
  • Querying a table is impossible while a column is being added.
  • Newly created attribute’s values are set to 0.
  • ALTER will not work for distributed tables and tables without any attributes.
  • DROP COLUMN will fail if a table has only one field.
  • When dropping a field which is both a full-text field and a string attribute the first ALTER DROP drops the attribute, the second one drops the full-text field.
  • Adding/dropping full-text field is only supported in the RT mode.
  • Example

Example

  1. mysql> desc rt;
  2. +------------+-----------+
  3. | Field | Type |
  4. +------------+-----------+
  5. | id | bigint |
  6. | text | field |
  7. | group_id | uint |
  8. | date_added | timestamp |
  9. +------------+-----------+
  10. mysql> alter table rt add column test integer;
  11. mysql> desc rt;
  12. +------------+-----------+
  13. | Field | Type |
  14. +------------+-----------+
  15. | id | bigint |
  16. | text | field |
  17. | group_id | uint |
  18. | date_added | timestamp |
  19. | test | uint |
  20. +------------+-----------+
  21. mysql> alter table rt drop column group_id;
  22. mysql> desc rt;
  23. +------------+-----------+
  24. | Field | Type |
  25. +------------+-----------+
  26. | id | bigint |
  27. | text | field |
  28. | date_added | timestamp |
  29. | test | uint |
  30. +------------+-----------+
  31. mysql> alter table rt add column title text indexed;
  32. mysql> desc rt;
  33. +------------+-----------+------------+
  34. | Field | Type | Properties |
  35. +------------+-----------+------------+
  36. | id | bigint | |
  37. | text | text | indexed |
  38. | title | text | indexed |
  39. | date_added | timestamp | |
  40. | test | uint | |
  41. +------------+-----------+------------+
  42. mysql> alter table rt add column title text attribute;
  43. mysql> desc rt;
  44. +------------+-----------+------------+
  45. | Field | Type | Properties |
  46. +------------+-----------+------------+
  47. | id | bigint | |
  48. | text | text | indexed |
  49. | title | text | indexed |
  50. | date_added | timestamp | |
  51. | test | uint | |
  52. | title | string | |
  53. +------------+-----------+------------+
  54. mysql> alter table rt drop column title;
  55. mysql> desc rt;
  56. +------------+-----------+------------+
  57. | Field | Type | Properties |
  58. +------------+-----------+------------+
  59. | id | bigint | |
  60. | text | text | indexed |
  61. | title | text | indexed |
  62. | date_added | timestamp | |
  63. | test | uint | |
  64. +------------+-----------+------------+
  65. mysql> alter table rt drop column title;
  66. mysql> desc rt;
  67. +------------+-----------+------------+
  68. | Field | Type | Properties |
  69. +------------+-----------+------------+
  70. | id | bigint | |
  71. | text | text | indexed |
  72. | date_added | timestamp | |
  73. | test | uint | |
  74. +------------+-----------+------------+

Updating table FT settings in RT mode

  1. ALTER TABLE table ft_setting='value'[, ft_setting2='value']

You can also use ALTER to modify full-text settings of your table in the RT mode. Just remember that it doesn’t affect existing documents, it only affects new ones. Take a look at the example where we:

  • create a table with a full-text field and charset_table that allows only 3 searchable characters: a, b and c.
  • then we insert document ‘abcd’ and find it by query abcd, the d just gets ignored since it’s not in the charset_table array
  • then we understand, that we want d to be searchable too, so we add it with help of ALTER
  • but the same query where match('abcd') still says it searched by abc, because the existing document remembers previous contents of charset_table
  • then we add another document abcd and search by abcd again
  • now it finds the both documents and show meta says it used two keywords: abc (to find the old document) and abcd (for the new one).
  • Example

Example

  1. mysql> create table rt(title text) charset_table='a,b,c';
  2. mysql> insert into rt(title) values('abcd');
  3. mysql> select * from rt where match('abcd');
  4. +---------------------+-------+
  5. | id | title |
  6. +---------------------+-------+
  7. | 1514630637682688054 | abcd |
  8. +---------------------+-------+
  9. mysql> show meta;
  10. +---------------+-------+
  11. | Variable_name | Value |
  12. +---------------+-------+
  13. | total | 1 |
  14. | total_found | 1 |
  15. | time | 0.000 |
  16. | keyword[0] | abc |
  17. | docs[0] | 1 |
  18. | hits[0] | 1 |
  19. +---------------+-------+
  20. mysql> alter table rt charset_table='a,b,c,d';
  21. mysql> select * from rt where match('abcd');
  22. +---------------------+-------+
  23. | id | title |
  24. +---------------------+-------+
  25. | 1514630637682688054 | abcd |
  26. +---------------------+-------+
  27. mysql> show meta
  28. +---------------+-------+
  29. | Variable_name | Value |
  30. +---------------+-------+
  31. | total | 1 |
  32. | total_found | 1 |
  33. | time | 0.000 |
  34. | keyword[0] | abc |
  35. | docs[0] | 1 |
  36. | hits[0] | 1 |
  37. +---------------+-------+
  38. mysql> insert into rt(title) values('abcd');
  39. mysql> select * from rt where match('abcd');
  40. +---------------------+-------+
  41. | id | title |
  42. +---------------------+-------+
  43. | 1514630637682688055 | abcd |
  44. | 1514630637682688054 | abcd |
  45. +---------------------+-------+
  46. mysql> show meta;
  47. +---------------+-------+
  48. | Variable_name | Value |
  49. +---------------+-------+
  50. | total | 2 |
  51. | total_found | 2 |
  52. | time | 0.000 |
  53. | keyword[0] | abc |
  54. | docs[0] | 1 |
  55. | hits[0] | 1 |
  56. | keyword[1] | abcd |
  57. | docs[1] | 1 |
  58. | hits[1] | 1 |
  59. +---------------+-------+

Updating table FT settings in plain mode

  1. ALTER TABLE table RECONFIGURE

ALTER can also reconfigure an RT table in the plain mode), so that new tokenization, morphology and other text processing settings from the configuration file take effect for new documents. Note, that the existing document will be left intact. Internally, it forcibly saves the current RAM chunk as a new disk chunk and adjusts the table header, so that new documents are tokenized using the updated full-text settings.

  • Example

Example

  1. mysql> show table rt settings;
  2. +---------------+-------+
  3. | Variable_name | Value |
  4. +---------------+-------+
  5. | settings | |
  6. +---------------+-------+
  7. 1 row in set (0.00 sec)
  8. mysql> alter table rt reconfigure;
  9. Query OK, 0 rows affected (0.00 sec)
  10. mysql> show table rt settings;
  11. +---------------+----------------------+
  12. | Variable_name | Value |
  13. +---------------+----------------------+
  14. | settings | morphology = stem_en |
  15. +---------------+----------------------+
  16. 1 row in set (0.00 sec)

Rebuild secondary index

  1. ALTER TABLE table REBUILD SECONDARY

ALTER can also be used to rebuild secondary indexes in a given table. Sometimes a secondary index can be disabled for the whole table or for one/multiple attributes in it:

  • On UPDATE of an attribute: in this case its secondary index gets disabled.
  • In case Manticore loads a table with old formatted secondary indexes: in this case secondary indexes will be disabled for the whole table.

ALTER TABLE table REBUILD SECONDARY rebuilds secondary indexes from attribute data and enables them again.

  • Example

Example

  1. ALTER TABLE rt REBUILD SECONDARY;

Response

  1. Query OK, 0 rows affected (0.00 sec)

Functions

Mathematical functions

ABS()

Returns the absolute value of the argument.

ATAN2()

Returns the arctangent function of two arguments, expressed in radians.

BITDOT()

BITDOT(mask, w0, w1, ...) returns the sum of products of an each bit of a mask multiplied with its weight. bit0*w0 + bit1*w1 + ...

CEIL()

Returns the smallest integer value greater or equal to the argument.

COS()

Returns the cosine of the argument.

CRC32()

Returns the CRC32 value of a string argument.

EXP()

Returns the exponent of the argument (e=2.718… to the power of the argument).

FIBONACCI()

Returns the N-th Fibonacci number, where N is the integer argument. That is, arguments of 0 and up will generate the values 0, 1, 1, 2, 3, 5, 8, 13 and so on. Note that the computations are done using 32-bit integer math and thus numbers 48th and up will be returned modulo 2\^32.

FLOOR()

Returns the largest integer value lesser or equal to the argument.

GREATEST()

GREATEST(attr_json.some_array) function takes JSON array as the argument, and returns the greatest value in that array. Also works for MVA.

IDIV()

Returns the result of an integer division of the first argument by the second argument. Both arguments must be of an integer type.

LEAST()

LEAST(attr_json.some_array) function takes JSON array as the argument, and returns the least value in that array. Also works for MVA.

LN()

Returns the natural logarithm of the argument (with the base of e=2.718…).

LOG10()

Returns the common logarithm of the argument (with the base of 10).

LOG2()

Returns the binary logarithm of the argument (with the base of 2).

MAX()

Returns the bigger of two arguments.

MIN()

Returns the smaller of two arguments.

POW()

Returns the first argument raised to the power of the second argument.

RAND()

RAND(seed) function returns a random float between 0..1. Optionally can accept seed which can be:

  • constant integer
  • or integer attribute’s name

If you use the seed take into account that it resets rand()‘s starting point separately for each plain table / RT disk / RAM chunk / pseudo shard, so queries to a distributed table in any form can return multiple identical random values.

SIN()

Returns the sine of the argument.

SQRT()

Returns the square root of the argument.