# Conditionally treating array response as singular value

**URL:** <https://discuss.dgraph.io/t/conditionally-treating-array-response-as-singular-value/16001>\
**Category:** Dgraph\
**Created:** [November 9, 2021, 11:36am UTC](https://discuss.dgraph.io/t/conditionally-treating-array-response-as-singular-value/16001 "2021-11-09T11:36:32Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/daniel/32/8499_2.png) [@Daniel](https://discuss.dgraph.io/u/Daniel)\
**Post date:** [November 9, 2021, 11:36am UTC](https://discuss.dgraph.io/t/conditionally-treating-array-response-as-singular-value/16001/1 "2021-11-09T11:36:32Z")

</div>

## What I want to do

A knowledge base like Wikidata has properties that are **reusable independent of singular or plural usage**. I.e. they don’t distinguish between [uid] and uid at a global level but leaves that to context-specific constraints (for example in the context of a certain class, the property is restricted to a singular value).

Take for example the property `headOfGovernment` and it’s inverse `headOfGovernmentOf` documented here:

> **[head of government](https://www.wikidata.org/wiki/Property:P6)**
>
> head of the executive power of this town, city, municipality, state, country, or other governmental body

This property is for example used as {USA} headOfGovernment {JoeBiden}:

> **[United States](https://www.wikidata.org/wiki/Q30)**
>
> country located primarily in North America

 ![image](https://canada1.discourse-cdn.com/flex007/uploads/dgraph/original/2X/3/3b8223d46a317ff523efd9683c22bd9b7fc4e662.png)

As you can see, the property allows for multiple values and **a facet called “rank=preferred” ([preferred rank - Wikidata](https://www.wikidata.org/wiki/Q71533031)) is used to treat Joe Biden as the most actual value**. (That’s what the little up-arrow indicates.)

 ![image](https://canada1.discourse-cdn.com/flex007/uploads/dgraph/original/2X/1/1108d28cbfe86868dd77c0156652e54aac9339e0.png)

It would likely involve a lot of redundancy for Wikidata and similar knowledge graphs to define both singular and plural properties and their inverses – imagine having to define something like the below for the same use case:

headOfGovernment \<\> headOfGovernmentOf  
headsOfGovernment \<\> headsOfGovernmentOf  
or:  
headOfGovernment \<\> headOfGovernment  
historicalHeadsOfGovernment \<\> historicalHeadsOfGovernmentOf

This will quickly lead to a breakdown of semantics and redundancy where new properties are introduced, instead of constrained as needed.

## The query layer

So let’s say we treated all properties in Dgraph the way Wikidata does (not making an a priori assumption about how many values are given).

What problems would we then face at the query layer when it comes to sort, group, aggregate operations that I guess assume a singular value to operate on?

Take again the example this property configured as multi-value (in both directions):

headOfGovernment [uid]  
headOfGovernmentOf [uid]

How could one now query for something like “Sort all headOfGovernment (with rank=“preferred”) by name?”

The problem here, I assume is that the resulting set of property values is an array but we want to treat it as a singular value. In other words it seems we want a mechanism to determine (at query-time) whether we are certain that we only want to operate on a single value (the first or only returned) versus an array.

I’d be very happy for any thoughts about how such a strategy would play out with DQLs capabilities. And more specifically about how to treat or cast an array response as a single value.

I’m attaching some reference data below for any experimentation:

## Reference data

```auto
{

  "set": [

    {
      "dgraph.type" : "Country",
  		"name" : "USA",
  	  "uid": "_:USA",
  
      "headOfGovernment" : [
  			{
     "dgraph.type" : "Human",
  			"uid": "_:JoeBiden",
      	"name": "JoeBiden",
  			"headOfGovernment|rank" : "preferred"
},
{
       "dgraph.type" : "Human",
  			"uid": "_:DonaldTrump",
      	"name": "DonaldTrump",
  			"headOfGovernment|rank" : "default"
},
{
       "dgraph.type" : "Human",
  			"uid": "_:BarackObama",
      	"name": "BarackObama",
  			"headOfGovernment|rank" : "default"
				}
        
			]
    }
    
  ]
}

```
