BigQuery Java API to read an Array of Record : "Retrieving field value by name is not supported" exception - google-bigquery

My current table in BigQuery has a column that uses complex types. The "family" column is actually a list ("repeated" feature) of records (with 2 fields: id & name).
When I try to get the 1st "id" value of 1 row with the following syntax:
FieldValueList c = qr.getValues().iterator().next();
c.get("family").getRepeatedValue().get(0).getRecordValue().get("id");
I get the exception:
Method threw 'java.lang.UnsupportedOperationException' exception.
Retrieving field value by name is not supported when there is no fields schema provided
This is a bit annoying because my table has a clearly defined schema. And when I do the "read" query with the same Java call, I can also see that this schema is correctly found:
qr.getSchema().getFields().get("family").getSubFields().toString();
-->
[Field{name=id, type=INTEGER, mode=NULLABLE, description=null}, Field{name=name, type=STRING, mode=NULLABLE, description=null}]
Due to this exception, the workaround that I have found is to pass the "index" of the record field instead of giving it its name
c.get("family").getRepeatedValue().get(0).getRecordValue().get(0).getLongValue();
However, this seeks awkward to pass an index instead of a name.
Is there a better way to get the value of a field in a record inside an array (if my column is only a record, without array, then I don't get the exception) ?
Is this exception normal?

You can wrap the unnamed FieldValueList with a named one using the "of" static method:
FieldList subSchema = qr.getSchema().getFields().get("family").getSubFields();
FieldValueList c = qr.getValues().iterator().next();
FieldValueList.of(
c.get("family").getRepeatedValue().get(0).getRecordValue(),
subSchema).get("id");
The "of" method takes a FieldValueList (returned by getRecordValue() in this case) and a FieldList (subSchema here), and returns the same FieldValueList but with named access.

Related

Google Cloud Datalow:Getting a below error at runtime

I am writing data into nested array BQ table(array name inside the table is -merchant_array)using my dataflow template.
Sometime its running fine and loading the data but sometime its giving me that error at run time.
java.lang.RuntimeException: org.apache.beam.sdk.util.UserCodeException: com.fasterxml.jackson.databind.JsonMappingException: Null key for a Map not allowed in JSON (use a converting NullKeySerializer?) (through reference chain: com.google.api.services.bigquery.model.TableRow["null"])
"message" : "Error while reading data, error message: JSON parsing error in row starting at position 223615: Only optional fields can be set to NULL. Field: merchant_array; Value: NULL",
Anyone has any idea why I am getting this error.
Thanks in advance.
here I got the issue that was causing error so I am posting my own question's answer,it might be helpful for anyone.
So the error was like-
Only optional fields can be set to NULL. Field: merchant_array; Value: NULL",
And here merchant_array is defined as an array that contains record (repetitive) data.
As per google doc the the array can not be-
ARRAYs cannot be NULL.
NULL ARRAY elements cannot persist to a table.
At the same time I was using arraylist in my code, that allows null values. So before making a record type data in code or setting the data in arraylist, just remove the NULL tablerows if exist.
hope this will helpful.

how to access lookup object data using mule 4 salesforce query all connector

I want get the lookup object data for that I have one object called Agent and another object called pincode. added lookup data field in pincode.
In Query all connector i am applying this query: select firstName__c, lastname__c, experience__c, primarycontactnumber__c, role__c, active__c, pincodeId__c, CreatedBy.FirstName, pincode.Name FROM Agent__c where active__c= true
But here i am getting error SALESFORCE:INVALID_REQUEST_DATA for pincode.Name this field.
What is the correct query to get the pincode lookup data in the query?
Got the solution. By using select pincode__r I can get the lookup object data.
firstName__c, lastname__c, experience__c, primarycontactnumber__c, role__c, active__c, pincodeId__c, CreatedBy.FirstName, pincode__r.Name FROM Agent__c where active__c= true

AssertJ: `ignoringFields` is not ignoring all the provided fields?

I want to compare two objects using org.assertj.core.api.Assertions.assertThat from version 3.14.0:
assertThat(getActual())
.usingRecursiveComparison()
.ignoringFields("customer", "orders", "\$hashCode", "fragments.parentFields.__typename", "fragments.parentFields.image")
.isEqualTo(orderForm)
When I do so I get the following error:
when recursively comparing field by field, but found the following difference:
field/property 'fragments' differ:
- actual value : Fragments{parentFields=Optional[ParentFields{__typename=OrderForm, id=1, image=Image{__typename=Image, id=71, accuracy=100.0, type=ORDER_FORM, state=AUTOMATIC, value=order form, base64=}}]}
- expected value : Fragments{parentFields=Optional[ParentFields{__typename=ParentFields, id=1, image=Image{__typename=Image, id=123, accuracy=99.99, type=NONE, state=NONE, value=value, base64=base64}}]}
The recursive comparison was performed with this configuration:
- the following fields were ignored in the comparison: customer, orders, $hashCode, fragments.parentFields.__typename, fragments.parentFields.image
- overridden equals methods were used in the comparison
- these types were compared with the following comparators:
- java.lang.Double -> DoubleComparator[precision=1.0E-15]
- java.lang.Float -> FloatComparator[precision=1.0E-6]
- actual and expected objects and their fields were compared field by field recursively even if they were not of the same type, this allows for example to compare a Person to a PersonDto (call strictTypeChecking(true) to change that behavior).
From my understanding, ignoring all the fileds I added to the ignoreingFields function the comparison should be:
- actual value : Fragments{parentFields=Optional[ParentFields{id=1}]}
- expected value : Fragments{parentFields=Optional[ParentFields{id=1}]}
And thus it should not fail, it should succeed.
The other ignored fileds (customer, orders, \$hashCode) are getting ignored correctly. So I wonder if there is something wrong with fields of nested objects. But in the javadoc I found:
* // assertion succeeds as name and home.address.street fields are ignored in the comparison
* assertThat(sherlock).usingRecursiveComparison()
* .ignoringFields("name", "home.address.street")
* .isEqualTo(noName);
So I think im using it as intended by the API.
Ofcourse I could get around all this by simply doing:
assertThat(actual
?.fragments()
?.parentFields()
?.get()
?.id()
).isEqualTo(orderForm
.fragments()
.parentFields()
.get()
.id())
But I have many objects which I have to compare like that and which do not have the same super class.
So im going to create something like:
fun assertCommons(actual: Any, expected: Any) {
assertThat(actual)
.usingRecursiveComparison()
....
}
So back to the question: What goes wrong with ignoring these fields?
EDIT
Since parentFields is an optional:
...
.ignoringFields("customer", "orders", "\$hashCode", "fragments.parentFields.get().__typename", "fragments.parentFields.get().image")
...
field/property 'fragments' differ:
- actual value : Fragments{parentFields=Optional[ParentFields{__typename=OrderForm, id=1, image=Image{__typename=Image, id=71, accuracy=100.0, type=ORDER_FORM, state=AUTOMATIC, value=order form, base64=}}]}
- expected value : Fragments{parentFields=Optional[ParentFields{__typename=ParentFields, id=1, image=Image{__typename=Image, id=123, accuracy=99.99, type=NONE, state=NONE, value=value, base64=base64}}]}
The recursive comparison was performed with this configuration:
- the following fields were ignored in the comparison: customer, orders, $hashCode, fragments.parentFields.get().__typename, fragments.parentFields.get().image
- overridden equals methods were used in the comparison
- these types were compared with the following comparators:
- java.lang.Double -> DoubleComparator[precision=1.0E-15]
- java.lang.Float -> FloatComparator[precision=1.0E-6]
- actual and expected objects and their fields were compared field by field recursively even if they were not of the same type, this allows for example to compare a Person to a PersonDto (call strictTypeChecking(true) to change that behavior).
With value instead of get()
...
.ignoringFields("customer", "orders", "\$hashCode", "fragments.parentFields.value.__typename", "fragments.parentFields.value.image")
...
when recursively comparing field by field, but found the following difference:
field/property 'fragments' differ:
- actual value : Fragments{parentFields=Optional[ParentFields{__typename=OrderForm, id=1, image=Image{__typename=Image, id=71, accuracy=100.0, type=ORDER_FORM, state=AUTOMATIC, value=order form, base64=Optional[]}}]}
- expected value : Fragments{parentFields=Optional[ParentFields{__typename=ParentFields, id=1, image=Image{__typename=Image, id=123, accuracy=99.99, type=NONE, state=NONE, value=value, base64=Optional[base64]}}]}
The recursive comparison was performed with this configuration:
- the following fields were ignored in the comparison: customer, orders, $hashCode, fragments.parentFields.value
- overridden equals methods were used in the comparison
- these types were compared with the following comparators:
- java.lang.Double -> DoubleComparator[precision=1.0E-15]
- java.lang.Float -> FloatComparator[precision=1.0E-6]
- actual and expected objects and their fields were compared field by field recursively even if they were not of the same type, this allows for example to compare a Person to a PersonDto (call strictTypeChecking(true) to change that behavior).
"fragments.parentFields.get().__typename" is not a valid declaration for what you intend to do. when specifying "a.b.c" what AssertJ it going to do is try finding getA().getB().getC() or a.b.c or nay combination of field/getter (like getA().b.getC().
Specifying fragments.parentFields.get() is not going to work as there is no field named get(). Try using value instead as this is the field returned by get() (AssertJ can read private fields).

OpenERP - how to get form field value in python code

I have a field in the XML file, categ_id. I need to access the value of that field in my Python code, in product_template class. I tried vals as a paremeter but it did not work.
If you can give me an example object.field_name as it relates to the case I have described.
Nebojsa - your question is not understandable at all, but I'll try to answer it. You can get the value of categ_id in two or even three ways:
vals.get('categ_id') - this is the way to go when you are creating a new record or updating existing one with change in categ_id field - otherwise you'll get an error or NoneType defined.
template = self.pool.get('product.template).browse(cr, uid, ids) and then template.categ_id.id - to get the value when you do have an id of the record, so you can ask database of value stored or in transaction, if there were any changes.
third opition is the dirtiest one, because it is just cr.execute("SELECT categ_id FROM product_template WHERE id = %s", (ids[0],)) and then category_id = cr.fetchall() - it is not always good option to use that, as it asks for records already existing in database (not counting these in transaction)

How does NHibernate Projections.Max work with an empty table?

I'm trying to get the maximum value of an integer field in a table. Specifically, I'm trying to automatically increment the "InvoiceNumber" field when adding a new invoice. I don't want this to be an autoincrement field in the database, however, since it's controlled by the user -- I'm just trying to take care of the default case. Right now, I'm using
session.CreateCriteria<Invoice>()
.SetProjection(Projections.Max("InvoiceNumber"))
.FutureValue<int>();
to get the biggest invoice number already in the database. This works great, except when there are no invoices already in the database. Then I get a System.ArgumentException: The value "" is not of type "System.Int32" and cannot be used in this generic collection. Changing to FutureValue<int?>() didn't solve the problem. Is there a way to tell NHibernate to map the empty string to null? Or is there a better way to accomplish my goal altogether?
The stack trace of the exception (at least the relevant part) is
NHibernate.HibernateException: Error executing multi criteria : [SELECT max(this_.[InvoiceNumber]) as y0_ FROM dbo.[tblInvoice] this_;
SELECT this_.ID as ID647_0_, this_.[NHVersion] as column2_647_0_, this_.[Description] as column3_647_0_, this_.[DiscountPercent] as column4_647_0_, this_.[DiscountDateDays] as column5_647_0_, this_.[PaymentDueDateDays] as column6_647_0_, this_.[Notes] as column7_647_0_, this_.[DiscountDateMonths] as column8_647_0_, this_.[PaymentDueDateMonths] as column9_647_0_, this_.[DiscountDatePeriod] as column10_647_0_, this_.[DiscountDateMonthlyDay] as column11_647_0_, this_.[DiscountDateMonthlyDayDay] as column12_647_0_, this_.[DiscountDateMonthlyDayMonth] as column13_647_0_, this_.[DiscountDateMonthlyThe] as column14_647_0_, this_.[DiscountDateMonthlyTheDOW] as column15_647_0_, this_.[DiscountDateMonthlyTheMonth] as column16_647_0_, this_.[DiscountDateMonthlyTheWeek] as column17_647_0_, this_.[PaymentDueDatePeriod] as column18_647_0_, this_.[PaymentDueDateMonthlyDay] as column19_647_0_, this_.[PaymentDueDateMonthlyDayDay] as column20_647_0_, this_.[PaymentDueDateMonthlyDayMonth] as column21_647_0_, this_.[PaymentDueDateMonthlyThe] as column22_647_0_, this_.[PaymentDueDateMonthlyTheDOW] as column23_647_0_, this_.[PaymentDueDateMonthlyTheMonth] as column24_647_0_, this_.[PaymentDueDateMonthlyTheWeek] as column25_647_0_ FROM dbo.[tblTermsCode] this_;
] ---> System.ArgumentException: The value "" is not of type "System.Int32" and cannot be used in this generic collection.
Parameter name: value
at System.ThrowHelper.ThrowWrongValueTypeArgumentException(Object value, Type targetType)
at System.Collections.Generic.List`1.VerifyValueType(Object value)
at System.Collections.Generic.List`1.System.Collections.IList.Add(Object item)
at NHibernate.Impl.MultiCriteriaImpl.GetResultsFromDatabase(IList results)
use....UniqueValue<int?>();
NH uses a non-generic IList in their MultiCriteria implementation. Which is used for FutureValue batching. see here for why List<int?> fails to add null through it's IList implementation. I'm surprised I've never run into this before. Avoid using nullable value types with Future or MultiCriteria.
With the QueryOver API:
Session.QueryOver<T>()
.Select(Projections.Max<Statistic>(s => s.PeriodStart))
.SingleOrDefault<object>();
if nothing is returned its null, otherwise cast the result as numeric