Closed lochana-chathura closed 4 months ago
The representation of multiple keys as tuples is defined by this sentence:
If the key sequence consists of multiple field names f1,f2,...,fn where n is ≥ 2, then the key value for a member r is a tuple with members v1,v2,...,vn where vi is the value of field fi of r.
Also, note that in the current implementation, if one wants to have a single key which itself is a tuple, they have to use that tuple inside another tuple, as the first tuple represents the number of keys.
I don't think that's right. The key constraint is constraining the key value of each table row. In the case, where the key constraint has a single field name, then the key value consists of that key. The spec says:
if the key sequence consists of a single field name f, then the key value of a member is that value of field f of that member
Consider the below sample,
type Employee record {|
readonly [string, string] name;
readonly string department;
readonly string city;
int salary;
|};
public function foo() {
// case I
table<Employee> key<[string, string]> t1 = table key(name) [ ];
// case II
table<Employee> key<[string, string]> t2 = table key(department, city) [ ];
}
According to the aforementioned spec rules, both single-key and multiple-key table constructor scenarios in this sample are assignable to the table<Employee> key<[string, string]>
type right?
This means if we consider, table<C> key<K>
where K = [T1, T2, ... , Tn]
,
n = 1
, only single-field-key with type [T1]
is assignable. n >=2
, both single-field-key with type [T1, ..., Tn]
and n-field-key with types T1, ..., Tn
are assignable?i.e. For table<C> key<K>
where K = [T1, T2, ... , Tn]
and n>=2
, we cannot guarantee the number of fields in the key. It could be both 1 and n.
Right on both points.
The way I think of it is that every row in the table has exactly one key which is constructed from one or more fields. When the type of the key is a tuple with length > 1, you cannot tell just from the type of the key whether it is constructed from one field (of tuple type) or multiple fields. All you care about is the type of the key.
Thanks, @jclark for the clarification.
Description: Consider the below sample extracted from ballerina by example: https://ballerina.io/learn/by-example/multiple-key-fields
The above sample is valid in the current jBallerina implementation.
In this sample, we use
[string, string]
tuple to represent the multiple-keys in the LHS table type descriptor. (Each element in the tuple represents a key in multiple-keys) Is that correct? (I couldn't find such in the spec) If not, how do we represent multiple-keys using tablekey-constraint
?Also, note that in the current implementation, if one wants to have a single key which itself is a tuple, they have to use that tuple inside another tuple, as the first tuple represents the number of keys. e.g.
Useful or not, member access for the multiple-key tables anyway has been modeled using the list-constructor-expr. Quoting from the spec.
CC: @pcnfernando @gimantha @MaryamZi