DBAL Querybuilder copy and invert column - doctrine

With Doctrine DBAL Querybuilder, i can easily copy a column into another one:
$qb->update('table')->set('col_1', 'col_2');
But as both columns are boolean(resp. tinyint), how can i just invert col_2 while copying it?

Related

How to create Fact table in Hive and replace original values in table with key (id) values

What I want to create I explanied below. Is that possible to do in Hive?
I could do that in Python using Pandas and replace over columns, but I was wondering can that be done with query in Hive?
I have uploaded Source table in Hive and created Dimensional tables like below also (in Cloudera HUE), so is it possible to somehow create that Fact table by using Dimensional tables id values and replace values in Source table?
I have my Source table:
I create Dimensional tables from Source table:
And I want to create Fact table like this:
Join by values with source table and select IDs:
insert overwrite table fact
select pr.id as property, t.id as type, pl.id as place, s.price
from source_table s
left join property_dim pr on s.property=pr.property
left join type_dim t on s.type=t.type
left join place_dim pl on s.place=pl.place

What is the best way to set all cells in a table to the same value?

What is the best way to pass all cells in a table the same value?
For instance, if I start with this table:
How might I most easily change it to this:
Or to this:
I would like the solution to work for any size table, without having to hard-code the columns' names. So I'm pretty sure the solution will include Table.ColumnNames.
My suggestion would be to create a new table with the dimensions and table type of the original table. The table type includes column names and column data types (and any keys if these would be present).
Note: this won't work if the original table has a primary key, e.g. after removing duplicates.
let
NewValue = "a",
Source = Table1,
NewTable = Table.FromColumns(List.Repeat({List.Repeat({NewValue},Table.RowCount(Source))},Table.ColumnCount(Source)), Value.Type(Source))
in
NewTable

How to rename a table along with projections in Vertica?

I have to alter many columns in a Vertica table, and decided to drop the table altogether and create a new one. I also need 'undo' scripts ready to revert back the changes if needed (using mybatis migrations).
This is my plan:
rename mytable to mytable_backup
create mytable
create projection mytable_super (as select from mytable)
--undo
drop mytable if exists
rename mytable_backup to mytable
The original mytable was also created with a projection. The above script gives an error saying projection already exists.
DBCException: SQL Error [4482] [42710]: [Vertica][VJDBC](4482) ROLLBACK: Projection with base name "mytable_super" already exists
I believe when I rename the original table, the underlying projection is not being renamed.
What is the best way to rename a table with projections in vertica? Or what is the best way to back up a table and revert back?
You'll need to rename the projections as well.
alter projection mytable_super rename to mytable_super_backup;
For renaming a table, the above answer is the one.
One way to rename a projection, would be to get the projection inside a SQL file. For example:
select CONCAT(CONCAT(projection_schema,'.'),projection_name) from projections where projection_schema like '%one_table%'
Then modify it into the following SQL and execute it (and don't forget to run refresh):
https://www.vertica.com/docs/9.2.x/HTML/Content/Authoring/AdministratorsGuide/Projections/UpdatingProjectionsUsingRefresh.htm
Once you have the SQL, you can do \i /path/of/sql (inside vertica shell) or you can /opt/vertica/bin/vsql -f /path/to/sql/that.sql -U vertica_user -w vertica_passwd

HIVE: How create a table with all columns in another table EXCEPT one of them?

When I need to change a column into a partition (convert normal column as partition column in hive), I want to create a new table to copy all columns except one. I currently have >50 columns in the original table. Is there any clean way of doing that?
Something like:
CREATE student_copy LIKE student EXCEPT age and hair_color;
Thanks!
You can use a regex:
CTAS using REGEX column spec. :
set hive.support.quoted.identifiers=none;
CREATE TABLE student_copy AS SELECT `(age|hair_color)?+.+` FROM student;
set hive.support.quoted.identifiers=column;
BUT (as mentioned by Kishore Kumar Suthar :
this will not create a partitioned table, as that is not supported with CTAS (Create Table As Select).
Only way I see for you to get your partitioned table is by getting the complete create statement of the table (as mentioned by Abraham):
SHOW CREATE TABLE student;
Altering it to create a partition on the column you want. And after that you can use the select with regex when inserting into the new table.
If your partition column is already part of this select, then you need to make sure it is the last column you insert. If it is not you can exclude that column in the regex and including it as last. Also if you expect several partitions to be created based on your insert statement you need to enable 'dynamic partitioning':
set hive.support.quoted.identifiers=none;
set hive.exec.dynamic.partition=true;
set hive.exec.dynamic.partition.mode=nonstrict;
INSERT INTO TABLE student_copy PARTITION(partcol1) SELECT `(age|hair_color|partcol1)?+.+`, partcol1 FROM student;
set hive.support.quoted.identifiers=column;
the 'hive.support.quoted.identifiers=none' is required to use the backticks '`' in the regex part of the query. I set this parameter to it's original value after my statement: 'hive.support.quoted.identifiers=column'
CREATE TABLE student_copy LIKE student;
It just copies the source table definition.
CREATE TABLE student_copy AS select name, age, class from student;
Target cannot be partitioned table.
Target cannot be external table.
It copies the structure as well as the data
I use below command to get the create statement of existing table.
SHOW CREATE TABLE student;
Copy the result and modify that based on your requirement for new table and run the modified command to get the new table.

Oracle Backup and restore constraints to allow changing a column type

I need to change a column type in more than 200 tables I am following the next recipe:
Disable all foreign constraints if the column is referenced by any FK
Store columns in varray and Drop primary key if the column is part of a PK
Create a temporal new column in the table with the same type
Update the temporal new column with original values
Delete values from original column
Change column type of original column
Update original column with temporal column values
Restore primary key if applied
Enable FK if applied
I am having some issues with the following cases
. When a primary key is compound (Multiple columns)
. I need to store the original FK and PK signature to allow me to restore them after the change
------- My ideas --------
Backup all_constraints and all_cons_columns records in a temporary table and after changing the column type resstoring the constraints info.
Keep with the same idea of storing the FK and PK signature to restore them after changing the column type
¿ Any suggestions ? would be appreaciate it, thx!!
You could try the old CTAS then rename method:
basically:
create new table as select c1,c1,c3 ... from old table (include data
type transformation here;
establish any indexes, constraints ...
drop old table (may need to disable donstraints first)
rename new table to old table

Resources