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

Add Round trip tests for Array <--> ScalarValue #13777

Merged
merged 5 commits into from
Dec 16, 2024

Conversation

alamb
Copy link
Contributor

@alamb alamb commented Dec 14, 2024

Which issue does this PR close?

Rationale for this change

When trying to write a unit test for #13762 I found there was no good place to test the round tripping of Array --> ScalarValue --> Array (which is what is done for constant values)

So I wrote some

What changes are included in this PR?

Add tests for this path, with ones that fail due to bugs commented out

Are these changes tested?

Yes, by CI

Are there any user-facing changes?

No - this are just tests

@github-actions github-actions bot added the common Related to common crate label Dec 14, 2024
// null array
Arc::new(NullArray::new(3)),
// dense union
/* Dense union fails due to https://github.com/apache/datafusion/issues/13762
Copy link
Contributor Author

@alamb alamb Dec 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the test for #13762

It fails because the type of the produced array is UnionArray::Sparse rather than dense

Arc::new(StringArray::from(vec![Some("foo"), None, Some("bar")])),
Arc::new(LargeStringArray::from(vec![Some("foo"), None, Some("bar")])),
Arc::new(StringViewArray::from(vec![Some("foo"), None, Some("bar")])),
// string dictionary fails due to XXX
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is XXX?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry -- that was a left over from when I though dictionary failed, and left an XXX in there as a placeholder when I filed a ticket

// [ ] (empty list)
builder.append(true);
// Null
builder.values().append_value("?"); // irrelevant
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it can be omitted afaict, see apache/arrow-rs#6775

let values_builder = Int32Builder::new();
let mut builder = FixedSizeListBuilder::new(values_builder, 3);

// [[0, 1, 2], null, [3, null, 5], [6, 7, null]]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

6,7,null are not in the data. update comment

}
}

/// for each index in array, convert to to a scalar and back to an array and compare for equality
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to to

let array = scalar.to_array_of_size(1).unwrap();
assert_eq!(array.len(), 1);
assert_eq!(array.data_type(), arr.data_type());
assert_eq!(array.as_ref(), arr.slice(i, 1).as_ref());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this compare internal representation or logical value?
for example a null list can have non-zero offset or zero offset (see comment above & apache/arrow-rs#6775)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It compares logical values

Specifically Array::slice returns a new Array (that is underneath just a different view of the same underlying data / buffers)

@@ -5554,6 +5555,194 @@ mod tests {
assert_eq!(&array, &expected);
}

#[test]
fn round_trip() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the code in this test is more or less copy/pasted from the arrow documentation

Copy link
Contributor Author

@alamb alamb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @findepi

let array = scalar.to_array_of_size(1).unwrap();
assert_eq!(array.len(), 1);
assert_eq!(array.data_type(), arr.data_type());
assert_eq!(array.as_ref(), arr.slice(i, 1).as_ref());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It compares logical values

Specifically Array::slice returns a new Array (that is underneath just a different view of the same underlying data / buffers)

@alamb alamb merged commit 59410ea into apache:main Dec 16, 2024
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
common Related to common crate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants