zed
zed copied to clipboard
casting complex type is order dependent when docs say different
The cast docs mention:
If val is a record (or if any of its nested value is a record): ...
- fields are matched by name and are order independent and the input order is retained.
❯ zq -version
Version: v1.16.0
❯ zq 'type foo={a:string,b:int64} yield {} | fill(this,<foo>) | cast(this,<foo>)'
{a:null(string),b:null(int64)}(=foo)
❯ zq 'type foo={a:string,b:int64} yield {b:1} | fill(this,<foo>) | cast(this,<foo>)'
{b:1,a:null(string)}
❯ zq 'type foo={a:string,b:int64} yield {a:"x"} | fill(this,<foo>) | cast(this,<foo>)'
{a:"x",b:null(int64)}(=foo)
The middle example shows that the casting fails (returns the original input) because the order of fields isn't correct, but the docs say that "fields are matched by name and are order independent".
As a result of looking for similar issues, I discovered the shape
function, which is what I can use going forward and not be impacted by this.