| 1 |
.. -*- rst -*- |
| 2 |
|
| 3 |
.. groonga-command |
| 4 |
.. database: commands_copy |
| 5 |
|
| 6 |
``column_copy`` |
| 7 |
=============== |
| 8 |
|
| 9 |
Summary |
| 10 |
------- |
| 11 |
|
| 12 |
.. versionadded:: 5.0.7 |
| 13 |
|
| 14 |
``column_copy`` copies all column values to other column. |
| 15 |
|
| 16 |
You can implement the following features with this command: |
| 17 |
|
| 18 |
* Changing column configuration |
| 19 |
* Changing table configuration |
| 20 |
|
| 21 |
You can change column configuration by the following steps: |
| 22 |
|
| 23 |
1. Create a new column with new configuration |
| 24 |
2. Copy all values from the current column to the new column |
| 25 |
3. Remove the current column |
| 26 |
4. Rename the new column to the current column |
| 27 |
|
| 28 |
You can change table configuration by the following steps: |
| 29 |
|
| 30 |
1. Create a new table with new configuration |
| 31 |
2. Create all same columns to the new table |
| 32 |
3. Copy all column values from the current table to the new table |
| 33 |
4. Remove the current table |
| 34 |
5. Rename the new table to the current table |
| 35 |
|
| 36 |
Concrete examples are showed later. |
| 37 |
|
| 38 |
You can't copy column values from a ``TABLE_NO_KEY`` table to another |
| 39 |
table. And you can't copy column values to a ``TABLE_NO_KEY`` table |
| 40 |
from another table. Because Groonga can't map records without record |
| 41 |
key. |
| 42 |
|
| 43 |
You can copy column values from a ``TABLE_NO_KEY`` table to the same |
| 44 |
``TABLE_NO_KEY`` table. |
| 45 |
|
| 46 |
You can copy column values from a ``TABLE_HASH_KEY`` / |
| 47 |
``TABLE_PAT_KEY`` / ``TABLE_DAT_KEY`` table to the same or another |
| 48 |
``TABLE_HASH_KEY`` / ``TABLE_PAT_KEY`` / ``TABLE_DAT_KEY`` table. |
| 49 |
|
| 50 |
Syntax |
| 51 |
------ |
| 52 |
|
| 53 |
This command takes four parameters. |
| 54 |
|
| 55 |
All parameters are required:: |
| 56 |
|
| 57 |
column_copy from_table |
| 58 |
from_name |
| 59 |
to_table |
| 60 |
to_name |
| 61 |
|
| 62 |
Usage |
| 63 |
----- |
| 64 |
|
| 65 |
Here are use cases of this command: |
| 66 |
|
| 67 |
* Changing column configuration |
| 68 |
* Changing table configuration |
| 69 |
|
| 70 |
How to change column configuration |
| 71 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 72 |
|
| 73 |
You can change column value type. For example, you can change |
| 74 |
``UInt32`` column value to ``ShortText`` column value. |
| 75 |
|
| 76 |
You can change column type. For example, you can change |
| 77 |
``COLUMN_SCALAR`` column to ``COLUMN_VECTOR`` column. |
| 78 |
|
| 79 |
You can move a column to other table. For example, you can move |
| 80 |
``high_score`` column to ``Users`` table from ``Players`` table. |
| 81 |
|
| 82 |
Here are basic steps to change column configuration: |
| 83 |
|
| 84 |
1. Create a new column with new configuration |
| 85 |
2. Copy all values from the current column to the new column |
| 86 |
3. Remove the current column |
| 87 |
4. Rename the new column to the current column |
| 88 |
|
| 89 |
Here is an example to change column value type to ``Int32`` from |
| 90 |
``ShortText``. |
| 91 |
|
| 92 |
Here are schema and data: |
| 93 |
|
| 94 |
.. groonga-command |
| 95 |
.. include:: ../../example/reference/commands/column_copy/change_column_value_type_setup.log |
| 96 |
.. table_create Logs TABLE_HASH_KEY ShortText |
| 97 |
.. column_create Logs serial COLUMN_SCALAR Int32 |
| 98 |
.. load --table Logs |
| 99 |
.. [ |
| 100 |
.. {"_key": "log1", "serial": 1} |
| 101 |
.. ] |
| 102 |
|
| 103 |
The following commands change ``Logs.serial`` column value type to |
| 104 |
``ShortText`` from ``Int32``: |
| 105 |
|
| 106 |
.. groonga-command |
| 107 |
.. include:: ../../example/reference/commands/column_copy/change_column_value_type.log |
| 108 |
.. column_create Logs new_serial COLUMN_SCALAR ShortText |
| 109 |
.. column_copy Logs serial Logs new_serial |
| 110 |
.. column_remove Logs serial |
| 111 |
.. column_rename Logs new_serial serial |
| 112 |
.. select Logs |
| 113 |
|
| 114 |
You can find ``Logs.serial`` stores ``ShortText`` value from the |
| 115 |
response of ``select``. |
| 116 |
|
| 117 |
Here is an example to change column type to ``COLUMN_VECTOR`` from |
| 118 |
``COLUMN_SCALAR``. |
| 119 |
|
| 120 |
Here are schema and data: |
| 121 |
|
| 122 |
.. groonga-command |
| 123 |
.. include:: ../../example/reference/commands/column_copy/change_column_type_setup.log |
| 124 |
.. table_create Entries TABLE_HASH_KEY ShortText |
| 125 |
.. column_create Entries tag COLUMN_SCALAR ShortText |
| 126 |
.. load --table Entries |
| 127 |
.. [ |
| 128 |
.. {"_key": "entry1", "tag": "Groonga"} |
| 129 |
.. ] |
| 130 |
|
| 131 |
The following commands change ``Entries.tag`` column to |
| 132 |
``COLUMN_VECTOR`` from ``COLUMN_SCALAR``: |
| 133 |
|
| 134 |
.. groonga-command |
| 135 |
.. include:: ../../example/reference/commands/column_copy/change_column_type.log |
| 136 |
.. column_create Entries new_tag COLUMN_VECTOR ShortText |
| 137 |
.. column_copy Entries tag Entries new_tag |
| 138 |
.. column_remove Entries tag |
| 139 |
.. column_rename Entries new_tag tag |
| 140 |
.. select Entries |
| 141 |
|
| 142 |
You can find ``Entries.tag`` stores ``COLUMN_VECTOR`` value from the |
| 143 |
response of ``select``. |
| 144 |
|
| 145 |
Here is an example to move ``high_score`` column to ``Users`` table |
| 146 |
from ``Players`` table. |
| 147 |
|
| 148 |
Here are schema and data: |
| 149 |
|
| 150 |
.. groonga-command |
| 151 |
.. include:: ../../example/reference/commands/column_copy/move_column_setup.log |
| 152 |
.. table_create Players TABLE_HASH_KEY ShortText |
| 153 |
.. column_create Players high_score COLUMN_SCALAR Int32 |
| 154 |
.. load --table Players |
| 155 |
.. [ |
| 156 |
.. {"_key": "player1", "high_score": 100} |
| 157 |
.. ] |
| 158 |
|
| 159 |
The following commands move ``high_score`` column to ``Users`` table |
| 160 |
from ``Players`` table: |
| 161 |
|
| 162 |
.. groonga-command |
| 163 |
.. include:: ../../example/reference/commands/column_copy/move_column.log |
| 164 |
.. table_create Users TABLE_HASH_KEY ShortText |
| 165 |
.. column_create Users high_score COLUMN_SCALAR Int32 |
| 166 |
.. column_copy Players high_score Users high_score |
| 167 |
.. column_remove Players high_score |
| 168 |
.. select Users |
| 169 |
|
| 170 |
You can find ``Users.high_score`` is moved from ``Players.high_score`` |
| 171 |
from the response of ``select``. |
| 172 |
|
| 173 |
How to change table configuration |
| 174 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 175 |
|
| 176 |
You can change table key type. For example, you can change |
| 177 |
key type to ``ShortText`` from ``Int32``. |
| 178 |
|
| 179 |
You can change table type. For example, you can change |
| 180 |
``TABLE_HASH_KEY`` table to ``TABLE_PAT_KEY`` table. |
| 181 |
|
| 182 |
You can also change other options such as default tokenizer and |
| 183 |
normalizer. For example, you can change default tokenizer to |
| 184 |
``TokenBigramSplitSymbolAlphaDigit`` from ``TokenBigrm``. |
| 185 |
|
| 186 |
.. note:: |
| 187 |
|
| 188 |
You can't change ``TABLE_NO_KEY`` table. Because ``TABLE_NO_KEY`` |
| 189 |
doesn't have record key. Groonga can't identify copy destination |
| 190 |
record without record key. |
| 191 |
|
| 192 |
Here are basic steps to change table configuration: |
| 193 |
|
| 194 |
1. Create a new table with new configuration |
| 195 |
2. Create all same columns to the new table |
| 196 |
3. Copy all column values from the current table to the new table |
| 197 |
4. Remove the current table |
| 198 |
5. Rename the new table to the current table |
| 199 |
|
| 200 |
Here is an example to change table key type to ``ShortText`` from |
| 201 |
``Int32``. |
| 202 |
|
| 203 |
Here are schema and data: |
| 204 |
|
| 205 |
.. groonga-command |
| 206 |
.. include:: ../../example/reference/commands/column_copy/change_table_key_type_setup.log |
| 207 |
.. table_create IDs TABLE_HASH_KEY Int32 |
| 208 |
.. column_create IDs label COLUMN_SCALAR ShortText |
| 209 |
.. column_create IDs used COLUMN_SCALAR Bool |
| 210 |
.. load --table IDs |
| 211 |
.. [ |
| 212 |
.. {"_key": 100, "label": "ID 100", used: true} |
| 213 |
.. ] |
| 214 |
|
| 215 |
The following commands change ``IDs`` table key type to ``ShortText`` |
| 216 |
from ``Int32``: |
| 217 |
|
| 218 |
.. groonga-command |
| 219 |
.. include:: ../../example/reference/commands/column_copy/change_table_key_type.log |
| 220 |
.. table_create NewIDs TABLE_HASH_KEY Int32 |
| 221 |
.. column_create NewIDs label COLUMN_SCALAR ShortText |
| 222 |
.. column_create NewIDs used COLUMN_SCALAR Bool |
| 223 |
.. column_copy IDs label NewIDs label |
| 224 |
.. column_copy IDs used NewIDs used |
| 225 |
.. table_remove IDs |
| 226 |
.. table_rename NewIDs IDs |
| 227 |
.. select IDs |
| 228 |
|
| 229 |
You can find ``IDs`` stores ``ShortText`` key from the response of |
| 230 |
``select``. |
| 231 |
|
| 232 |
Here is an example to change table type to ``TABLE_PAT_KEY`` from |
| 233 |
``TABLE_HASH_KEY``. |
| 234 |
|
| 235 |
Here are schema and data: |
| 236 |
|
| 237 |
.. groonga-command |
| 238 |
.. include:: ../../example/reference/commands/column_copy/change_table_type_setup.log |
| 239 |
.. table_create Names TABLE_HASH_KEY ShortText |
| 240 |
.. column_create Names used COLUMN_SCALAR Bool |
| 241 |
.. load --table Names |
| 242 |
.. [ |
| 243 |
.. {"_key": "alice", "used": false} |
| 244 |
.. ] |
| 245 |
|
| 246 |
The following commands change ``Names`` table to ``TABLE_PAT_KEY`` |
| 247 |
from ``TABLE_HASH_KEY``: |
| 248 |
|
| 249 |
.. groonga-command |
| 250 |
.. include:: ../../example/reference/commands/column_copy/change_table_type.log |
| 251 |
.. table_create NewNames TABLE_PAT_KEY ShortText |
| 252 |
.. column_create NewNames used COLUMN_SCALAR Bool |
| 253 |
.. column_copy Names used NewNames used |
| 254 |
.. table_remove Names |
| 255 |
.. table_rename NewNames Names |
| 256 |
.. select Names --filter '_key @^ "ali"' |
| 257 |
|
| 258 |
You can find ``Names`` is a ``TABLE_PAT_KEY`` because ``select`` can |
| 259 |
use :ref:`script-syntax-prefix-search-operator`. You can't use |
| 260 |
:ref:`script-syntax-prefix-search-operator` with ``TABLE_HASH_KEY``. |
| 261 |
|
| 262 |
Parameters |
| 263 |
---------- |
| 264 |
|
| 265 |
This section describes parameters. |
| 266 |
|
| 267 |
Required parameters |
| 268 |
^^^^^^^^^^^^^^^^^^^ |
| 269 |
|
| 270 |
All parameters are required. |
| 271 |
|
| 272 |
.. _column-copy-from-table: |
| 273 |
|
| 274 |
``from_table`` |
| 275 |
"""""""""""""" |
| 276 |
|
| 277 |
Specifies the table name of source column. |
| 278 |
|
| 279 |
You can specify any table including ``TABLE_NO_KEY`` table. |
| 280 |
|
| 281 |
If you specify ``TABLE_NO_KEY`` table, :ref:`column-copy-to-table` |
| 282 |
must be the same table. |
| 283 |
|
| 284 |
Here is an example to use ``from_table``. |
| 285 |
|
| 286 |
Here are schema and data: |
| 287 |
|
| 288 |
.. groonga-command |
| 289 |
.. include:: ../../example/reference/commands/column_copy/from_table_setup.log |
| 290 |
.. table_create FromTable TABLE_HASH_KEY ShortText |
| 291 |
.. column_create FromTable from_column COLUMN_SCALAR ShortText |
| 292 |
.. column_create FromTable to_column COLUMN_SCALAR ShortText |
| 293 |
.. load --table FromTable |
| 294 |
.. [ |
| 295 |
.. {"_key": "key1", "from_column": "value1"} |
| 296 |
.. ] |
| 297 |
.. select FromTable --output_columns _key,from_column,to_column |
| 298 |
|
| 299 |
You can copy all values to ``to_column`` from ``from_column``: |
| 300 |
|
| 301 |
.. groonga-command |
| 302 |
.. include:: ../../example/reference/commands/column_copy/from_table.log |
| 303 |
.. column_copy FromTable from_column FromTable to_column |
| 304 |
.. select FromTable --output_columns _key,from_column,to_column |
| 305 |
|
| 306 |
.. _column-copy-from-name: |
| 307 |
|
| 308 |
``from_name`` |
| 309 |
""""""""""""" |
| 310 |
|
| 311 |
Specifies the column name to be copied values. |
| 312 |
|
| 313 |
See :ref:`column-copy-from-table` for example. |
| 314 |
|
| 315 |
.. _column-copy-to-table: |
| 316 |
|
| 317 |
``to_table`` |
| 318 |
"""""""""""" |
| 319 |
|
| 320 |
Specifies the table name of destination column. |
| 321 |
|
| 322 |
You can specify the same table name as :ref:`column-copy-from-table` |
| 323 |
when you want to copy column values in the same table. |
| 324 |
|
| 325 |
You can't specify ``TABLE_NO_KEY`` table to ``to_table`` because |
| 326 |
Groonga can't identify destination records without record key. |
| 327 |
|
| 328 |
There is one exception. If you specify the same name as ``from_table`` |
| 329 |
to ``to_table``, you can use ``TABLE_NO_KEY`` table as |
| 330 |
``to_table``. Because Groonga can identify destination records when |
| 331 |
source table and destination table is the same table. |
| 332 |
|
| 333 |
Here is an example to use ``to_table``. |
| 334 |
|
| 335 |
Here are schema and data: |
| 336 |
|
| 337 |
.. groonga-command |
| 338 |
.. include:: ../../example/reference/commands/column_copy/to_table_setup.log |
| 339 |
.. table_create Table TABLE_HASH_KEY ShortText |
| 340 |
.. column_create Table column COLUMN_SCALAR ShortText |
| 341 |
.. table_create ToTable TABLE_HASH_KEY ShortText |
| 342 |
.. column_create ToTable to_column COLUMN_SCALAR ShortText |
| 343 |
.. load --table Table |
| 344 |
.. [ |
| 345 |
.. {"_key": "key1", "column": "value1"} |
| 346 |
.. ] |
| 347 |
|
| 348 |
You can copy all values to ``ToTable.to_column`` from |
| 349 |
``Table.column``: |
| 350 |
|
| 351 |
.. groonga-command |
| 352 |
.. include:: ../../example/reference/commands/column_copy/to_table.log |
| 353 |
.. column_copy Table column ToTable to_column |
| 354 |
.. select ToTable --output_columns _key,to_column |
| 355 |
|
| 356 |
.. _column-copy-to-name: |
| 357 |
|
| 358 |
``to_name`` |
| 359 |
""""""""""" |
| 360 |
|
| 361 |
Specifies the destination column name. |
| 362 |
|
| 363 |
See :ref:`column-copy-to-table` for example. |
| 364 |
|
| 365 |
Optional parameters |
| 366 |
^^^^^^^^^^^^^^^^^^^ |
| 367 |
|
| 368 |
There is no optional parameter. |
| 369 |
|
| 370 |
Return value |
| 371 |
------------ |
| 372 |
|
| 373 |
The command returns ``true`` as body on success such as:: |
| 374 |
|
| 375 |
[HEADER, true] |
| 376 |
|
| 377 |
If the command fails, error details are in ``HEADER``. |
| 378 |
|
| 379 |
See :doc:`/reference/command/output_format` for ``HEADER``. |