How do I insert data into a table containg struct data type in BQ - google-bigquery

While trying to insert data into the following table, I get the following error message.
--create table mydataset.struct_1(x struct<course string,id int64>)
insert into `mydataset.struct_1` (course,id) values("B.A",12)
Error: Column course is not present in table mydataset.struct_1 at [2:35]

-- CREATE TABLE mydataset.struct_1(x STRUCT<course STRING,id INT64>)
INSERT INTO `mydataset.struct_1` (x) VALUES(STRUCT("B.A",12))

If you want to create STRUCT with a nested STRUCT named x with two fields y and z you should do this:
STRUCT<x STRUCT<y INT64, z INT64>>
So in your example:
create table mydataset.struct_1(STRUCT<x STRUCT<course string,id int64>>)

CREATE TABLE STRUCT_1 (x STRUCT<course: STRING,id: int>)
comment 'demonstrating how to work-around to insert complex
datatype unnested structs into a complex table '
Stored as parquet
location '/user/me/mestruct'
tblproperties ('created date: '=' 2019/01','done by: '='me');
Now lets do the insert.
insert into table STRUCT_1 select named_struct("course","B.A","id",12) from (select 't') s;

Related

Copying the structure of temp hive table to new table and adding additional table properties

I want to copy the structure of full load temp table and add the addition table properties like partitioned by (partition_col), Format='ORC'
Temp table :
Create table if not exists tmp.temp_table( id int,
name string,
datestr string )
temp table got created.
Final table :
CREATE TABLE IF NOT EXISTS tmp.{final_table_name} (
LIKE tmp.temp_table
)
WITH (
FORMAT = 'ORC'
partitioned by('datestr')
)
But I am getting the error as "Error: Error while compiling statement: FAILED: ParseException line 1:63 missing EOF at 'WITH' near 'temp_table' (state=42000,code=40000)"
Any solution to achieve this functionality.
You should not use like and instead use create table as (CTAS) select * from mytab where 1=2.
CREATE TABLE IF NOT EXISTS tmp.{final_table_name}
As select * from tmp.temp_table where 1=2
WITH (
FORMAT = 'ORC'
partitioned by('datestr')
)
Like will create an empty table with exact same definition. CTAS will use same column sequence, data type/length, the sql, and your definition to create new empty table because we are using 1=2.

Getting Error 10293 while inserting a row to a hive table having array as one of the fileds

I have a hive table created using the following query:
create table arraytbl (id string, model string, cost int, colors array <string>,size array <float>)
row format delimited fields terminated by ',' collection items terminated by '#';
Now , while trying to insert a row:
insert into mobilephones values
("AA","AAA",5600,colors("red","blue","green"),size(5.6,4.3));
I get the following error:
FAILED: SemanticException [Error 10293]: Unable to create temp file for insert values Expression of type TOK_FUNCTION not supported in insert/values
How can I resolve this issue?
The syantax to enter values in complex datatype if kinda bit weird, however this is my personal opinion.
You need a dummy table to insert values into hive table with complex datatype.
insert into arraytbl select "AA","AAA",5600, array("red","blue","green"), array(CAST(5.6 AS FLOAT),CAST(4.3 AS FLOAT)) from (select 'a') x;
And this is how it looks after insert.
hive> select * from arraytbl;
OK
AA AAA 5600 ["red","blue","green"] [5.6,4.3]

Hive - How to insert in a hive table an array of struct

So I learnt from here how to insert values into an array column:
INSERT INTO table
SELECT ARRAY("line1", "line2", "line3") as myArray
FROM source1;
And from here how to insert values into an struct column:
INSERT INTO table
SELECT NAMED_STRUCT('houseno','123','streetname','GoldStreet', 'town','London', 'postcode','W1a9JF') AS address
FROM source2;
Now I was trying to insert in the same way values in an array of structs. Which has got the following schema:
additionalattribute:array<struct<attribute_value:string,key:string,value:string>
I tried to extrapolate like this:
INSERT INTO table
ARRAY(NAMED_STRUCT('attribute_value','null','key','null','value','null')) as additionalattribute
FROM source2;
But it is not working. Does anyone know how to approach this issue?
you are missing the select statement after the table name. Demo
create table temp4
(
additionalattribute array<struct<attribute_value:string,key:string,value:string>>
);
INSERT INTO temp4 select
ARRAY(NAMED_STRUCT('attribute_value','null','key','null','value','null')) as additionalattribute
FROM (select '1' ) t;

create a temp table with the output of a stored procedure in postgres

I want a query to create a table with the output of a stored procedure function in Postgres.
SQL:
CREATE TEMP TABLE new_project AS select project_insert('1','test2343','tew3234','ccc',1);
Error:
ERROR: 42P16: column "projects_insert" has pseudo-type record
LOCATION: CheckAttributeType, heap.c:513
Note: project_insert is a function which insert the values and returns inserted values
The problem is that project_insert has been declared with RETURNS record, and that type is illegal for a column definition.
You have to specify name and type of the result columns in the query, like this:
CREATE TEMP TABLE new_project AS
SELECT x, y, z
FROM project_insert('1','test2343','tew3234','ccc',1)
AS p(x integer, y text, z bytea);
Replace the names and types with the appropriate names and types.
See the documentation for details.

Queries using temp tables

SELECT 1
FROM geo_locationInfoMajor_tbl
WHERE geo_locationInfoM_taluka IN(SELECT * from #temp
I have created a temp table which gets its values from the front end.. using a function I insert values into the temp table...
Now the data in the temp table is mixed... it can be integer or varchar..
when I pass only int or varchar into the temp table it is fine.
but if the output is mixed the query throws an error.. how to deal with this?
Conversion failed when converting the varchar value 'English' to data type int.
this is fine-->
#temp
1
this is not-->
1
English
How many values do you have in you temp table?
if you want to use the IN clause you should only use one column that is identical with your geo_locationInfoMajor_tbl.
try this:
SELECT * FROM geo_locationInfoMajor_tbl
WHERE geo_locationInfoM_taluka IN (SELECT geo_locationInfoM_taluka from #temp)