|
36 | 36 | import java.util.List;
|
37 | 37 | import java.util.Objects;
|
38 | 38 | import java.util.Random;
|
| 39 | +import java.util.function.Function; |
| 40 | +import java.util.stream.Stream; |
39 | 41 |
|
40 | 42 | import org.apache.arrow.memory.ArrowBuf;
|
41 | 43 | import org.apache.arrow.memory.BufferAllocator;
|
|
52 | 54 | import org.junit.jupiter.api.AfterEach;
|
53 | 55 | import org.junit.jupiter.api.BeforeEach;
|
54 | 56 | import org.junit.jupiter.api.Test;
|
| 57 | +import org.junit.jupiter.params.ParameterizedTest; |
| 58 | +import org.junit.jupiter.params.provider.Arguments; |
| 59 | +import org.junit.jupiter.params.provider.MethodSource; |
55 | 60 |
|
56 | 61 |
|
57 | 62 | public class TestVarCharViewVector {
|
@@ -1517,6 +1522,198 @@ public void testVectorLoadUnload() {
|
1517 | 1522 | }
|
1518 | 1523 | }
|
1519 | 1524 |
|
| 1525 | + static Stream<Arguments> vectorCreatorProvider() { |
| 1526 | + return Stream.of( |
| 1527 | + Arguments.of((Function<BufferAllocator, BaseVariableWidthViewVector>) |
| 1528 | + (allocator -> newVector(ViewVarBinaryVector.class, EMPTY_SCHEMA_PATH, |
| 1529 | + Types.MinorType.VIEWVARBINARY, allocator))), |
| 1530 | + Arguments.of((Function<BufferAllocator, BaseVariableWidthViewVector>) |
| 1531 | + (allocator -> newVector(ViewVarCharVector.class, EMPTY_SCHEMA_PATH, |
| 1532 | + Types.MinorType.VIEWVARCHAR, allocator))) |
| 1533 | + ); |
| 1534 | + } |
| 1535 | + |
| 1536 | + @ParameterizedTest |
| 1537 | + @MethodSource({"vectorCreatorProvider"}) |
| 1538 | + public void testCopyFromWithNulls(Function<BufferAllocator, BaseVariableWidthViewVector> vectorCreator) { |
| 1539 | + try (final BaseVariableWidthViewVector vector = vectorCreator.apply(allocator); |
| 1540 | + final BaseVariableWidthViewVector vector2 = vectorCreator.apply(allocator)) { |
| 1541 | + final int initialCapacity = 1024; |
| 1542 | + vector.setInitialCapacity(initialCapacity); |
| 1543 | + vector.allocateNew(); |
| 1544 | + int capacity = vector.getValueCapacity(); |
| 1545 | + assertTrue(capacity >= initialCapacity); |
| 1546 | + |
| 1547 | + // setting number of values such that we have enough space in the initial allocation |
| 1548 | + // to avoid re-allocation. This is to test copyFrom() without re-allocation. |
| 1549 | + final int numberOfValues = initialCapacity / 2 / ViewVarCharVector.ELEMENT_SIZE; |
| 1550 | + |
| 1551 | + final String prefixString = generateRandomString(12); |
| 1552 | + |
| 1553 | + for (int i = 0; i < numberOfValues; i++) { |
| 1554 | + if (i % 3 == 0) { |
| 1555 | + // null values |
| 1556 | + vector.setNull(i); |
| 1557 | + } else if (i % 3 == 1) { |
| 1558 | + // short strings |
| 1559 | + byte[] b = Integer.toString(i).getBytes(StandardCharsets.UTF_8); |
| 1560 | + vector.set(i, b, 0, b.length); |
| 1561 | + } else { |
| 1562 | + // long strings |
| 1563 | + byte[] b = (i + prefixString).getBytes(StandardCharsets.UTF_8); |
| 1564 | + vector.set(i, b, 0, b.length); |
| 1565 | + } |
| 1566 | + } |
| 1567 | + |
| 1568 | + assertEquals(capacity, vector.getValueCapacity()); |
| 1569 | + |
| 1570 | + vector.setValueCount(numberOfValues); |
| 1571 | + |
| 1572 | + for (int i = 0; i < numberOfValues; i++) { |
| 1573 | + if (i % 3 == 0) { |
| 1574 | + assertNull(vector.getObject(i)); |
| 1575 | + } else if (i % 3 == 1) { |
| 1576 | + assertArrayEquals(Integer.toString(i).getBytes(StandardCharsets.UTF_8), |
| 1577 | + vector.get(i), |
| 1578 | + "unexpected value at index: " + i); |
| 1579 | + } else { |
| 1580 | + assertArrayEquals((i + prefixString).getBytes(StandardCharsets.UTF_8), |
| 1581 | + vector.get(i), |
| 1582 | + "unexpected value at index: " + i); |
| 1583 | + } |
| 1584 | + } |
| 1585 | + |
| 1586 | + vector2.setInitialCapacity(initialCapacity); |
| 1587 | + vector2.allocateNew(); |
| 1588 | + int capacity2 = vector2.getValueCapacity(); |
| 1589 | + assertEquals(capacity2, capacity); |
| 1590 | + |
| 1591 | + for (int i = 0; i < numberOfValues; i++) { |
| 1592 | + vector2.copyFrom(i, i, vector); |
| 1593 | + if (i % 3 == 0) { |
| 1594 | + assertNull(vector2.getObject(i)); |
| 1595 | + } else if (i % 3 == 1) { |
| 1596 | + assertArrayEquals(Integer.toString(i).getBytes(StandardCharsets.UTF_8), |
| 1597 | + vector.get(i), |
| 1598 | + "unexpected value at index: " + i); |
| 1599 | + } else { |
| 1600 | + assertArrayEquals((i + prefixString).getBytes(StandardCharsets.UTF_8), |
| 1601 | + vector.get(i), |
| 1602 | + "unexpected value at index: " + i); |
| 1603 | + } |
| 1604 | + } |
| 1605 | + |
| 1606 | + assertEquals(capacity, vector2.getValueCapacity()); |
| 1607 | + |
| 1608 | + vector2.setValueCount(numberOfValues); |
| 1609 | + |
| 1610 | + for (int i = 0; i < numberOfValues; i++) { |
| 1611 | + if (i % 3 == 0) { |
| 1612 | + assertNull(vector2.getObject(i)); |
| 1613 | + } else if (i % 3 == 1) { |
| 1614 | + assertArrayEquals(Integer.toString(i).getBytes(StandardCharsets.UTF_8), |
| 1615 | + vector.get(i), |
| 1616 | + "unexpected value at index: " + i); |
| 1617 | + } else { |
| 1618 | + assertArrayEquals((i + prefixString).getBytes(StandardCharsets.UTF_8), |
| 1619 | + vector.get(i), |
| 1620 | + "unexpected value at index: " + i); |
| 1621 | + } |
| 1622 | + } |
| 1623 | + } |
| 1624 | + } |
| 1625 | + |
| 1626 | + @ParameterizedTest |
| 1627 | + @MethodSource("vectorCreatorProvider") |
| 1628 | + public void testCopyFromSafeWithNulls(Function<BufferAllocator, BaseVariableWidthViewVector> vectorCreator) { |
| 1629 | + try (final BaseVariableWidthViewVector vector = vectorCreator.apply(allocator); |
| 1630 | + final BaseVariableWidthViewVector vector2 = vectorCreator.apply(allocator)) { |
| 1631 | + |
| 1632 | + final int initialCapacity = 4096; |
| 1633 | + vector.setInitialCapacity(initialCapacity); |
| 1634 | + vector.allocateNew(); |
| 1635 | + int capacity = vector.getValueCapacity(); |
| 1636 | + assertTrue(capacity >= initialCapacity); |
| 1637 | + |
| 1638 | + final int numberOfValues = initialCapacity / ViewVarCharVector.ELEMENT_SIZE; |
| 1639 | + |
| 1640 | + final String prefixString = generateRandomString(12); |
| 1641 | + |
| 1642 | + for (int i = 0; i < numberOfValues; i++) { |
| 1643 | + if (i % 3 == 0) { |
| 1644 | + // null values |
| 1645 | + vector.setNull(i); |
| 1646 | + } else if (i % 3 == 1) { |
| 1647 | + // short strings |
| 1648 | + byte[] b = Integer.toString(i).getBytes(StandardCharsets.UTF_8); |
| 1649 | + vector.setSafe(i, b, 0, b.length); |
| 1650 | + } else { |
| 1651 | + // long strings |
| 1652 | + byte[] b = (i + prefixString).getBytes(StandardCharsets.UTF_8); |
| 1653 | + vector.setSafe(i, b, 0, b.length); |
| 1654 | + } |
| 1655 | + } |
| 1656 | + |
| 1657 | + /* NO reAlloc() should have happened in setSafe() */ |
| 1658 | + assertEquals(capacity, vector.getValueCapacity()); |
| 1659 | + |
| 1660 | + vector.setValueCount(numberOfValues); |
| 1661 | + |
| 1662 | + for (int i = 0; i < numberOfValues; i++) { |
| 1663 | + if (i % 3 == 0) { |
| 1664 | + assertNull(vector.getObject(i)); |
| 1665 | + } else if (i % 3 == 1) { |
| 1666 | + assertArrayEquals(Integer.toString(i).getBytes(StandardCharsets.UTF_8), |
| 1667 | + vector.get(i), |
| 1668 | + "unexpected value at index: " + i); |
| 1669 | + } else { |
| 1670 | + assertArrayEquals((i + prefixString).getBytes(StandardCharsets.UTF_8), |
| 1671 | + vector.get(i), |
| 1672 | + "unexpected value at index: " + i); |
| 1673 | + } |
| 1674 | + } |
| 1675 | + |
| 1676 | + vector2.setInitialCapacity(initialCapacity); |
| 1677 | + vector2.allocateNew(); |
| 1678 | + int capacity2 = vector2.getValueCapacity(); |
| 1679 | + assertEquals(capacity2, capacity); |
| 1680 | + |
| 1681 | + for (int i = 0; i < numberOfValues; i++) { |
| 1682 | + vector2.copyFromSafe(i, i, vector); |
| 1683 | + if (i % 3 == 0) { |
| 1684 | + assertNull(vector2.getObject(i)); |
| 1685 | + } else if (i % 3 == 1) { |
| 1686 | + assertArrayEquals(Integer.toString(i).getBytes(StandardCharsets.UTF_8), |
| 1687 | + vector.get(i), |
| 1688 | + "unexpected value at index: " + i); |
| 1689 | + } else { |
| 1690 | + assertArrayEquals((i + prefixString).getBytes(StandardCharsets.UTF_8), |
| 1691 | + vector.get(i), |
| 1692 | + "unexpected value at index: " + i); |
| 1693 | + } |
| 1694 | + } |
| 1695 | + |
| 1696 | + /* NO reAlloc() should have happened in setSafe() */ |
| 1697 | + assertEquals(capacity, vector2.getValueCapacity()); |
| 1698 | + |
| 1699 | + vector2.setValueCount(numberOfValues); |
| 1700 | + |
| 1701 | + for (int i = 0; i < numberOfValues; i++) { |
| 1702 | + if (i % 3 == 0) { |
| 1703 | + assertNull(vector2.getObject(i)); |
| 1704 | + } else if (i % 3 == 1) { |
| 1705 | + assertArrayEquals(Integer.toString(i).getBytes(StandardCharsets.UTF_8), |
| 1706 | + vector.get(i), |
| 1707 | + "unexpected value at index: " + i); |
| 1708 | + } else { |
| 1709 | + assertArrayEquals((i + prefixString).getBytes(StandardCharsets.UTF_8), |
| 1710 | + vector.get(i), |
| 1711 | + "unexpected value at index: " + i); |
| 1712 | + } |
| 1713 | + } |
| 1714 | + } |
| 1715 | + } |
| 1716 | + |
1520 | 1717 | private String generateRandomString(int length) {
|
1521 | 1718 | Random random = new Random();
|
1522 | 1719 | StringBuilder sb = new StringBuilder(length);
|
|
0 commit comments