How to return NULL/empty space when the expected property doesn't exist in some of verxtes /edges - properties

I am getting the following error, because some of the vertexes don't have the expected property.
The property does not exist as the key has no associated value for the provided element
Query:-
get_g().V().hasLabel(search_vertex).has(T.id, TextP.containing(search_text)).limit(limit).
as_('property_value').inE('owns').outV().as_('id', 'name')
.select('property_value', 'id', 'name').by(T.id).by(T.id).by('name').toList()
How to avoid exception and return NULL or empty space, if the expected property doesn't exist

In version of TinkerPop before 3.5.0 you can accomplish this using coalesce() (details here) and constant() (details here) steps to return a specific value in the case that a value does not exist as shown here:
gremlin> g.V().as('a').select('a').by(coalesce(values('age'), constant('foo')))
==>29
==>27
==>foo
==>32
==>foo
==>35
In versions after 3.5 this is no longer required as null will now be returned for these values, assuming the database supports this, as shown here:
gremlin> g.V().as('a').select('a').by(values('age'))
==>29
==>27
==>null
==>32
==>null
==>35
Additional details on this change can be found in this post on the Gremlin users group: https://groups.google.com/g/gremlin-users/c/aoaA25H1IE0/m/gTu1MqR1AQAJ

Related

Excluding undefined Field Values in MariaDB TypeORM Queries

I have a basic Nest project using TypeORM. A model, Plate, has been contained in its own module and is imported into the main module.
The service for the Plate model contains the following method:
public query(params?: QueryPlateParams, limit: number = 100): Promise<Plate[]> {
const findOp = PlatesService.FindOperatorMap[params.type];
const whereObj = {
// If a find operation is defined, apply it to the value. Otherwise, just use the
// value itself.
id: findOp?.(params.value) ?? params.value,
available: params.isAvailable,
state: params.state
};
console.log(whereObj);
return this._platesRepository.find({
where: whereObj,
take: limit
});
}
The idea of this method is to take the query object, which could have any combination of the value, isAvailable, and state properties defined, then to apply the defined values to the query's WHERE clause with some modification, of course not defining filters for fields which had no value in the source query object.
When a query is made, the following logs are observed.
{ id: 'ABSOLVE', available: undefined, state: undefined }
query: SELECT `Plate`.`id` AS `Plate_id`, `Plate`.`state` AS `Plate_state`, `Plate`.`available` AS `Plate_available`, `Plate`.`lastChecked` AS `Plate_lastChecked` FROM `plates` `Plate` WHERE (`Plate`.`id` = ? AND `Plate`.`available` = ? AND `Plate`.`state` = ?) LIMIT 100 -- PARAMETERS: ["ABSOLVE",null,null]
What's important here is the parameter interpolation at the end of the second line: PARAMETERS: ["ABSOLVE",null,null]. The values for isAvailable and state are undefined, but the SQL statement translated them to null, but undefined and null are distinct values in Javascript/Typescript. The intent, of course, is to ignore undefined values in the query, not interpret them as null.
I found an option for the MongoDB connector that will ignore these undefined values. I do not see the same for the MariaDB/MySQL connector, which is what is being used for this service.
Is there any option to ignore these undefined values instead of interpreting them as null? I'd like to use the IsNull() or null values to explicitly check for SQL NULL.
Sadly you need just to not provide the undefined fields (add filter before passing whereObj into the find function.
Or you can read this thread with some other options dealing with the issue https://github.com/typeorm/typeorm/issues/2934
Basically - the issue is because creator thought that setting property of object to undefined removes the property from the object own properties therefore he treated undefined and null the same.

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.

Apache Drill - Getting error on KVGEN method

I have a huge dataset where each record have json data similar to below -
{"project":{"id":"2625","createDate":1542597000000,"rank":0,"highlight":false,"isDisplay":true,"isNewProject":true,"propertyId":2231,"districts":{"id":41,"name":"abc","region":"123"}}}
When I am trying to genrate key value pairs using select kvgen(t.project) from dfs.filePath t in apache drill, I am getting below error -
DrillRuntimeException: Mappify/kvgen does not support heterogeneous value types. All values in the input map must be of the same type. The field [createDate] has a differing type [minor_type: BIGINT mode: OPTIONAL ]
It looks like drill expects all values to be of same type. But how to do that? Is there any function available in drill?
My drill version is 1.9.0
Try setting session option store.json.all_text_mode to true.
https://drill.apache.org/docs/json-data-model/
I figured it out. KVGEN method doesn't work if json is nested.
To make it work, there are two approaches which can be followed -
Take out the nested json outside
{"project":{"id":"2625","createDate":1542597000000,"rank":0,"highlight":false,"isDisplay":true,"isNewProject":true,"propertyId":2231},"districts":{"id":41,"name":"abc","region":"123"}}
and then apply KVGEN method as select kvgen(t.project) from dfs.filePath t
Apply kvgen method on inner json first and then use nested query as below
select tbl2.col1.id, tbl2.col2.value from (select tbl1.project as col1, flatten(kvgen(tbl1.project.districts)) col2 from dfs.filePath tbl1) tbl2
And as rightly mentioned by #arina-yelchiyeva, session option store.json.all_text_mode needs to be set to true.

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

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.

String was not recognized as a valid Boolean Error on varchar column

I am getting this error:
String was not recognized as a valid Boolean.Couldn't store <No> in meetsstd Column. Expected type is Boolean
When I am running this query:
SELECT * FROM work_nylustis_2013_q3.nylustis_details WHERE siteid = 'NYLUSTIS-155718' LIMIT 50
From this code:
Adapter.SelectCommand = New NpgsqlCommand(SQL, MLConnect)
Adapter.Fill(subDT) ' This line throws error
The meetsstd field is a varchar(3) and it does store either a 'Yes' or a 'No' value. How is this getting this confused with a boolean - a varchar should not care whether is holds 'Yes', or 'Si', or 'Oui'? And it only happens on 27 records out of the 28,000 in the table.
I usually blame npgsql for this kind of strangeness, but the last entry in the stack trace is: System.Data.DataColumn.set_Item(Int32 record, Object value)
Any clues?
Thanks!
Brad
To check if it is problem with database or with driver you can reduce problem to one row and column using your current environment:
SELECT meetsstd FROM work_nylustis_2013_q3.nylustis_details WHERE sitenum=1
(of course you must change sitenum into primary key)
Then try such query using psql, pgAdmin or some JDBC/ODBC based general editor.
If psql shows such record which raises error with your Npgsql based application then problem is with Npgsql driver or problem is with displaying query results.
If other tools shows such strange errors then problem is with your data.
Have you changed type of meetsstd field? Mayby you try to show it on some grid and this grid used Boolean field which was converted to Yes/No for displaying?