Attribute rename


#1

Since attribute_rename() operator was removed from version 15.7, and it suggests that the workaround is to use project(apply()) for each attribute you want to rename. After the project(apply()) operation, I got an array with the attribute name I wanted, and I only want one array in the database, should I store the generated result, and then remove the original array? I think the store and remove process may be cause lower performance. Why the attribute_rename operator is removed? I think it is a common used operator. So is there any advice for the attribute rename process?


#2

Hi, sorry about the trouble.

There are two kinds of “renaming”:

  1. rename an attribute mid-query - because you want to disambiguate or use it in an expression
  2. rename an attribute of a stored array, and persist the effect for all future uses of the array

So, (1) can be achieved with project/apply or cast or formerly attribute_rename. But we never had any way to do (2) other than creating a new array. 2 is actually a transactional and metadata operation. So having attribute_rename wouldn’t have helped your case. 2 is not too hard to do, just hasn’t popped up on our radar yet.

If you’re stuck with a large array and decide you need a different name, do you really need to make a copy of everything? That does seem excessive.

One option is an advanced maneuver in the postgres catalog. This is quick but should be done carefully:

$ iquery -aq "create array foo <a:double>[i=1:10]"
Query was executed successfully

$ iquery -aq "store(build(foo, i), foo)"
{i} a
{1} 1
{2} 2
{3} 3
{4} 4
{5} 5
{6} 6
{7} 7
{8} 8
{9} 9
{10} 10

#Go into Postgres, where the names are stored:
$ sudo -u postgres psql
could not change directory to "/home/scidb": Permission denied
psql (9.3.15)
Type "help" for help.

--connect to your catalog (same name as your scidb config)
postgres=# \c mydb

--find the ID of foo
mydb=# select * from "array" where name like 'foo';
  id  | name | flags | distribution_id 
------+------+-------+-----------------
 7933 | foo  |     0 |            7933
(1 row)

--find all versions of foo. 
--this will output many IDs if you have many versions of the array:
mydb=# select * from "array_version" where array_id=7933;
 array_id | version_id | version_array_id | time_stamp 
----------+------------+------------------+------------
     7933 |          1 |             7934 | 1490985571
(1 row)

--And here are the attributes. Note it's stored once for the first definition and also once per version:
mydb=# select * from array_attribute where array_id in (7933, 7934);
 array_id | id |   name   |   type    | flags | default_compression_method | reserve | default_missing_reason | default_value 
----------+----+----------+-----------+-------+----------------------------+---------+------------------------+---------------
     7933 |  0 | a        | double    |     1 |                          0 |       0 |                      0 | 
     7933 |  1 | EmptyTag | indicator |     2 |                          0 |       0 |                     -1 | 
     7934 |  0 | a        | double    |     1 |                          0 |       0 |                      0 | 
     7934 |  1 | EmptyTag | indicator |     2 |                          0 |       0 |                     -1 | 
(4 rows)

--So let's rename all instances of "a" to "b". Careful here to specify the IDs for just foo - not other arrays!
mydb=# update array_attribute set name = 'b' where name = 'a' and array_id in (7933,7934);
UPDATE 2

mydb=# \q

#Re-examine:
$ iquery -aq "show(foo)";
{i} schema
{0} 'foo<b:double> [i=1:10:0:1000000]'

$ iquery -aq "scan(foo)";
{i} b
{1} 1
{2} 2
{3} 3
{4} 4
{5} 5
{6} 6
{7} 7
{8} 8
{9} 9
{10} 10

So this may help if you get into a bind where you need to rename an attribute. But I’ll inform the dev team about this and we’ll see how we can make this easier in future releases.