Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get/set inconsistency for fractional array indices #2815

Closed
ncfavier opened this issue Aug 2, 2023 · 9 comments
Closed

Get/set inconsistency for fractional array indices #2815

ncfavier opened this issue Aug 2, 2023 · 9 comments

Comments

@ncfavier
Copy link
Contributor

ncfavier commented Aug 2, 2023

$ jq -n '[1,2,3] | .[1.5]'
null
$ jq -n '[1,2,3] | .[1.5] = 42'
[1,42,3]

Environment (please complete the following information):

  • jq version: 1.7rc1
@wader
Copy link
Member

wader commented Aug 2, 2023

Had a vague memory of seeing this before, but can't find any jq issue about... but turns i reported it for gojq itchyny/gojq#194 some time ago :)

I guess jq should behave the same as gojq?

@ncfavier
Copy link
Contributor Author

ncfavier commented Aug 2, 2023

I don't know what it should do but I'd expect it to either fail or be a lawful lens.

@nicowilliams
Copy link
Contributor

nicowilliams commented Aug 2, 2023

014b45b added the check of whether the index is an integer, and if not return null. We should apply the same logic in jv_set(). This was #239. The intent seems to have been to require indices to be in the INT_MIN..INT_MAX range, and the integer check seems to have been glossed over.

@nicowilliams
Copy link
Contributor

  • This is fine: .[1.5] == null and .[1.5] = "x" -> error
  • But so is this: .[1.5] == .[1] and .[1.5] = "x" -> set .[1] = "x"
  • And so is this: .[1.5] -> error and .[1.5] = "x" -> error

What's not fine is the current inconsistency.

Which behavior is best?

@ncfavier
Copy link
Contributor Author

ncfavier commented Aug 3, 2023

I think it should behave the same as out-of-bounds negative indices (so the first option). This also reduces the extent of the breaking change.

@nicowilliams
Copy link
Contributor

We're also inconsistent w.r.t. the indices in slices:

: ; ./jq -cn '"foobar"|.[1.5:3.5]'
"oob"
: ; ./jq -cn '"foobar"|.[1.5:3.5] = "xyz"'
jq: error (at <unknown>): Cannot update field at object index of string
: ; ./jq -cn '[range(10)]|.[1.5:3.5] = "xyz"'
jq: error (at <unknown>): A slice of an array can only be assigned another array
: ; ./jq -cn '[range(10)]|.[1.5:3.5] = ["xyz"]'
[0,"xyz",4,5,6,7,8,9]

Fun!

@nicowilliams
Copy link
Contributor

Another inconsistency:

: ; jq -n '[1,2,3] | .[1.5]'
null
: ; jq -cn '"foobar"|.[1.5]'
jq: error (at <unknown>): Cannot index string with number

@wader
Copy link
Member

wader commented Aug 4, 2023

Got curious about .[{start:1,end:2}] slicing syntax. Seems to be a difference code path ending up here i think https://github.com/jqlang/jq/blob/master/src/jv_aux.c#L16?

@wader
Copy link
Member

wader commented Aug 4, 2023

Ah sorry for the noise, now i see how it works 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants