BigQuery Legacy SQL - How to insert into tables that has nested fields? - google-bigquery

I'm trying to insert records into tables that has Nested and Repeated fields. I know that STRUCT and ARRAY keyword can be used respectively in Standard SQL.
What is equivalent of STRUCT and ARRAY keyword in Legacy SQL to insert records into Nested and Repeated fields?

I am reusing example you provided in bq command line tool - How to insert into Big query tables that has nested fields?
Try below, it is for Legacy SQL and using in-line version of Javascript UDF for Legacy SQL
Note: by default BigQuery Legacy SQL flattens any result, so make sure you set destination table and set Allow Large Results to true (or check it in Web UI) and Flatten Results to false (or uncheck it in Web UI)
SELECT Employee_id, Name, Age, Department.*, Location.* FROM JS((
SELECT Employee_id, Name, Age, Department_id, Department_Name, Department_Code, e.Location_id AS Location_id, Country, State, City
FROM (SELECT e.Employee_id AS Employee_id, e.Name AS Name, e.Age AS Age,
e.Department_id AS Department_id, d.Department_Name AS Department_Name, d.Department_Code AS Department_Code, e.Location_id AS Location_id
FROM Employee e JOIN Department d ON e.Department_id = d.Department_id ) AS e
JOIN Location l ON e.Location_id = l.Location_id
),
// input columns
Employee_id, Name, Age, Department_id, Department_Name, Department_Code, Location_id, Country, State, City,
// output schema
"[
{'name': 'Employee_id', 'type': 'INTEGER', 'mode': 'NULLABLE'},
{'name': 'Name', 'type': 'STRING', 'mode': 'NULLABLE'},
{'name': 'Age', 'type': 'INTEGER', 'mode': 'NULLABLE'},
{'name': 'Department', 'type': 'RECORD', 'mode': 'NULLABLE', 'fields': [
{'name': 'Department_id', 'type': 'STRING', 'mode': 'NULLABLE'},
{'name': 'Department_Name', 'type': 'STRING', 'mode': 'NULLABLE'},
{'name': 'Department_Code', 'type': 'STRING', 'mode': 'NULLABLE'}
]},
{'name': 'Location', 'type': 'RECORD', 'mode': 'NULLABLE', 'fields': [
{'name': 'Location_id', 'type': 'STRING', 'mode': 'NULLABLE'},
{'name': 'Country', 'type': 'STRING', 'mode': 'NULLABLE'},
{'name': 'State', 'type': 'STRING', 'mode': 'NULLABLE'},
{'name': 'City', 'type': 'STRING', 'mode': 'NULLABLE'}
]}
]",
// function
"function(r, emit){
emit({
Employee_id: r.Employee_id, Name: r.Name, Age: r.Age,
Department: {Department_id:r.Department_id, Department_Name:r.Department_Name, Department_Code:r.Department_Code},
Location: {Location_id:r.Location_id, Country:r.Country, State:r.State, City:r.City}
});
}"
)
Please note: i am using in-line version of UDF here for the purpose of easy showing and testing. In-line version is not recommended and not officially supported. But you can easily convert it to supported version - see more for User-Defined Functions in Legacy SQL
P.S. even though above works and helped a lot before the Standard SQL was an option - now what is the big reason for you to use Legacy SQL where Standard SQL is more elegant and gives you much more flexibility especially when it is about dealing with nested and repeated fields

Related

How to use Format for Julia DashTables

I am trying to format numbers in a Julia Dash DataTable so that they are comma separated. However, I cannot get it to work.
(name = id_name, id = id_name, format = (locale = (grouping = [3])))
I have an example below in python where they use this. Could someone show me a working example?
Source: https://dash.plotly.com/datatable/typing
app.layout = html.Div([
dash_table.DataTable(
id='typing_formatting',
data=df_typing_formatting.to_dict('records'),
columns=[{
'id': 'city',
'name': 'City',
'type': 'text'
}, {
'id': 'max',
'name': u'Max Temperature (˚F)',
'type': 'numeric',
'format': Format(
precision=0,
scheme=Scheme.fixed,
symbol=Symbol.yes,
symbol_suffix=u'˚F'
),
# equivalent manual configuration
# 'format': {
# 'locale': {
# 'symbol': ['', '˚F']
# },
# 'specifier': '$.0f'
# }
}, {
'id': 'max_date',
'name': 'Max Temperature (Date)',
'type': 'datetime'
}, {
'id': 'min',
'name': u'Min Temperature (˚F)',
'type': 'numeric',
'format': Format(
nully='N/A',
precision=0,
scheme=Scheme.fixed,
sign=Sign.parantheses,
symbol=Symbol.yes,
symbol_suffix=u'˚F'
),
# equivalent manual configuration
# 'format': {
# 'locale': {
# 'symbol': ['', '˚F']
# },
# 'nully': 'N/A'
# 'specifier': '($.0f'
# }
'on_change': {
'action': 'coerce',
'failure': 'default'
},
'validation': {
'default': None
}
}, {
'id': 'min_date',
'name': 'Min Temperature (Date)',
'type': 'datetime',
'on_change': {
'action': 'none'
}
}]

Athena query error : extraneous input 'select' expecting

Got an error while running query :
extraneous input 'select' expecting {'(', 'add', 'all', 'some', 'any', 'at', 'no', 'substring', 'position', 'tinyint', 'smallint', 'integer', 'date', 'time', 'timestamp', 'interval', 'year', 'month', 'day', 'hour', 'minute', 'second', 'zone', 'filter', 'over', 'partition', 'range', 'rows', 'preceding', 'following', 'current', 'row', 'schema', 'comment', 'view', 'replace', 'grant', 'revoke', 'privileges', 'public', 'option', 'explain', 'analyze', 'format', 'type', 'text', 'graphviz', 'logical', 'distributed', 'validate', 'show', 'tables', 'views', 'schemas', 'catalogs', 'columns', 'column', 'use', 'partitions', 'functions', 'to', 'system', 'bernoulli', 'poissonized', 'tablesample', 'unnest', 'array', 'map', 'set', 'reset', 'session', 'data', 'start', 'transaction', 'commit', 'rollback', 'work', 'isolation', 'level', 'serializable', 'repeatable', 'committed', 'uncommitted', 'read', 'write', 'only', 'call', 'input', 'output', 'cascade', 'restrict', 'including', 'excluding', 'properties', 'function', 'lambda_invoke', 'returns', 'sagemaker_invoke_endpoint', 'nfd', 'nfc', 'nfkd', 'nfkc', 'if', 'nullif', 'coalesce', identifier, digit_identifier, quoted_identifier, backquoted_identifier}
query is right join table ss with dd ,like:
select * from
(
select platform, id, nextMonth
FROM "logs"."user_records" as ss
right join
select id as idRight, platform, month
FROM "logs"."user_records" as dd
on ss.platform = dd.platform and ss.userid = dd.useridRight and ss.nextMonth=dd.month )
You probably need to surround the subquery after your right join in parentheses. Untested, but I'd guess:
select * from
(
select platform, id, nextMonth
FROM "logs"."user_records" as ss
right join
( select id as idRight, platform, month
FROM "logs"."user_records" ) as dd
on ss.platform = dd.platform and ss.userid = dd.useridRight and ss.nextMonth=dd.month )

fetch the data from array of objects sql BigQuery

I need to fetch key value pairs from the second object in array. Also, need to create new columns with the fetched data. I am only interested in the second object, some arrays have 3 objects, some have 4 etc. The data looks like this:
[{'adUnitCode': ca-pub, 'id': 35, 'name': ca-pub}, {'adUnitCode': hmies, 'id': 49, 'name': HMIES}, {'adUnitCode': moda, 'id': 50, 'name': moda}, {'adUnitCode': nova, 'id': 55, 'name': nova}, {'adUnitCode': listicle, 'id': 11, 'name': listicle}]
[{'adUnitCode': ca-pub, 'id': 35, 'name': ca-pub-73}, {'adUnitCode': hmiuk-jam, 'id': 23, 'name': HM}, {'adUnitCode': recipes, 'id': 26, 'name': recipes}]
[{'adUnitCode': ca-pub, 'id': 35, 'name': ca-pub-733450927}, {'adUnitCode': digital, 'id': 48, 'name': Digital}, {'adUnitCode': movies, 'id': 50, 'name': movies}, {'adUnitCode': cannes-film-festival, 'id': 57, 'name': cannes-film-festival}, {'adUnitCode': article, 'id': 57, 'name': article}]
The desired output:
adUnitCode id name
hmies 49 HMIES
hmiuk-jam 23 HM
digital 48 Digital
Below is for BigQuery Standard SQL
#standardSQL
select
json_extract_scalar(second_object, "$.adUnitCode") as adUnitCode,
json_extract_scalar(second_object, "$.id") as id,
json_extract_scalar(second_object, "$.name") as name
from `project.dataset.table`, unnest(
[json_extract_array(regexp_replace(mapping, r"(: )([\w-]+)(,|})", "\\1'\\2'\\3"))[safe_offset(1)]]
) as second_object
if applied to sample data from your question - output is
as you can see, the "trick" here is to use proper regexp in regexp_replace function. I've included now any alphabetical chars and - . you can include more as you see needed
As an alternative yo can try regexp_replace(mapping, r"(: )([^,}]+)", "\\1'\\2'") as in below example - so you will cover potentially more cases without changes in code
#standardSQL
select
json_extract_scalar(second_object, "$.adUnitCode") as adUnitCode,
json_extract_scalar(second_object, "$.id") as id,
json_extract_scalar(second_object, "$.name") as name
from `project.dataset.table`, unnest(
[json_extract_array(regexp_replace(mapping, r"(: )([^,}]+)", "\\1'\\2'"))[safe_offset(1)]]
) as second_object

How to generate an invoice from a custom module in Odoo13?

I am developing a custom module.
I tried to add it through an object button with the following code but doesn't seem to work
def create_invoice(self):
rslt = self.env['account.invoice'].create({
'partner_id': self.instructor.id,
'name': 'customer invoice',
'type': 'out_invoice',
'date_invoice': 'create_date'
})
return rslt
How can I add a button that generates an invoice?
desu
From Odoo13 there is a change in invoice object, It is now account.move instead of account.invoice.You can take this reference demo example.
invoice = self.env['account.move'].create({
'type': 'out_invoice',
'journal_id': journal.id,
'partner_id': product_id.id,
'invoice_date': date_invoice,
'date': date_invoice,
'invoice_line_ids': [(0, 0, {
'product_id': product_id.id,
'quantity': 40.0,
'name': 'product test 1',
'discount': 10.00,
'price_unit': 2.27,
})]
})

why the following Bigquery insertion is failing?

Hello I am trying to insert one row into a table, I succesfully created the table as follows:
schema = [{'name': 'foo', 'type': 'STRING', 'mode': 'nullable'},{'name': 'bar', 'type': 'FLOAT', 'mode': 'nullable'}]
created = client.create_table(dataset='api_data_set_course_33', table='insert_test_333', schema=schema)
print('Creation Result ',created)
However when I push the row I got False,
rows = [{'id': 'NzAzYmRiY', 'one': 'uno', 'two': 'dos'}]
inserted = client.push_rows('api_data_set_course_33','insert_test_333', rows, 'id')
print('Insertion Result ',inserted)
So I don't have idea what is wrong, I really would like to appreciate support to overcome this task
This is the API that I am testing:
https://github.com/tylertreat/BigQuery-Python
This is my complete code:
schema = [{'name': 'foo', 'type': 'STRING', 'mode': 'nullable'},{'name': 'bar', 'type': 'FLOAT', 'mode': 'nullable'}]
created = client.create_table(dataset='api_data_set_course_33', table='insert_test_333', schema=schema)
print('Creation Result ',created)
rows = [{'id': 'NzAzYmRiY', 'one': 'uno', 'two': 'dos'}]
inserted = client.push_rows('api_data_set_course_33','insert_test_333', rows, 'id')
print('Insertion Result ',inserted)
Output:
Creation Result True
Insertion Result False
After feedback I tried:
>>> client = get_client(project_id, service_account=service_account,private_key_file=key, readonly=False)
>>> schema = [{'name': 'foo', 'type': 'STRING', 'mode': 'nullable'},{'name': 'bar', 'type': 'FLOAT', 'mode': 'nullable'}]
>>> rows = [{'id': 'NzAzYmRiY', 'foo': 'uno', 'bar': 'dos'}]
>>> inserted = client.push_rows('api_data_set_course_33','insert_test_333', rows, 'id')
>>> print(inserted)
False
and also:
>>> rows = [{'id': 'NzAzYmRiY', 'foo': 'uno', 'bar': 45}]
>>> inserted = client.push_rows('api_data_set_course_33','insert_test_333', rows, 'id')
>>> print(inserted)
False
However I only got false
Your row field names don't match your schema field names. Try this instead:
rows = [{'id': 'NzAzYmRiY', 'foo': 'uno', 'bar': 'dos'}]