Java Code Examples for org.apache.arrow.vector.TinyIntVector#setSafe()

The following examples show how to use org.apache.arrow.vector.TinyIntVector#setSafe() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestArrowByteConnector.java    From yosegi with Apache License 2.0 5 votes vote down vote up
@Test
public void T_convert_1() throws IOException{
  BufferAllocator allocator = new RootAllocator( 1024 * 1024 * 10 );
  TinyIntVector vector = new TinyIntVector( "test" , allocator );
  vector.allocateNew();
  vector.setSafe( 0 , (byte)0 );
  vector.setSafe( 1 , (byte)1 );
  vector.setSafe( 2 , (byte)0 );
  vector.setNull( 3 );
  vector.setSafe( 4 , (byte)1 );
  vector.setSafe( 5 , (byte)1 );
  vector.setSafe( 6 , (byte)1 );
  vector.setNull( 7 );
  vector.setValueCount( 8 );

  IColumn column = ArrowColumnFactory.convert( "test" , vector );
  assertEquals( column.getColumnName() , "test" );
  assertEquals( column.size() , 8 );
  assertTrue( ( column.getColumnType() == ColumnType.BYTE ) );
  assertEquals( ( (PrimitiveObject)( column.get(0).getRow() ) ).getByte() , (byte)0  );
  assertEquals( ( (PrimitiveObject)( column.get(1).getRow() ) ).getByte() , (byte)1  );
  assertEquals( ( (PrimitiveObject)( column.get(2).getRow() ) ).getByte() , (byte)0  );
  assertEquals( column.get(3).getRow() , null  );
  assertEquals( ( (PrimitiveObject)( column.get(4).getRow() ) ).getByte() , (byte)1 );
  assertEquals( ( (PrimitiveObject)( column.get(5).getRow() ) ).getByte() , (byte)1 );
  assertEquals( ( (PrimitiveObject)( column.get(6).getRow() ) ).getByte() , (byte)1 );
  assertEquals( column.get(7).getRow() , null  );
}
 
Example 2
Source File: TestArrowByteConnector.java    From multiple-dimension-spread with Apache License 2.0 5 votes vote down vote up
@Test
public void T_convert_1() throws IOException{
  BufferAllocator allocator = new RootAllocator( 1024 * 1024 * 10 );
  TinyIntVector vector = new TinyIntVector( "test" , allocator );
  vector.allocateNew();
  vector.setSafe( 0 , (byte)0 );  
  vector.setSafe( 1 , (byte)1 );  
  vector.setSafe( 2 , (byte)0 );  
  vector.setNull( 3 );  
  vector.setSafe( 4 , (byte)1 );  
  vector.setSafe( 5 , (byte)1 );  
  vector.setSafe( 6 , (byte)1 );  
  vector.setNull( 7 );  
  vector.setValueCount( 8 );

  IColumn column = ArrowColumnFactory.convert( "test" , vector );
  assertEquals( column.getColumnName() , "test" );
  assertEquals( column.size() , 8 );
  assertTrue( ( column.getColumnType() == ColumnType.BYTE ) );
  assertEquals( ( (PrimitiveObject)( column.get(0).getRow() ) ).getByte() , (byte)0  );
  assertEquals( ( (PrimitiveObject)( column.get(1).getRow() ) ).getByte() , (byte)1  );
  assertEquals( ( (PrimitiveObject)( column.get(2).getRow() ) ).getByte() , (byte)0  );
  assertEquals( column.get(3).getRow() , null  );
  assertEquals( ( (PrimitiveObject)( column.get(4).getRow() ) ).getByte() , (byte)1 );
  assertEquals( ( (PrimitiveObject)( column.get(5).getRow() ) ).getByte() , (byte)1 );
  assertEquals( ( (PrimitiveObject)( column.get(6).getRow() ) ).getByte() , (byte)1 );
  assertEquals( column.get(7).getRow() , null  );
}
 
Example 3
Source File: TinyIntToFixedConverterTest.java    From snowflake-jdbc with Apache License 2.0 5 votes vote down vote up
@Test
public void testInvalidConversion()
{
  // try convert to int/long/byte/short with scale > 0
  Map<String, String> customFieldMeta = new HashMap<>();
  customFieldMeta.put("logicalType", "FIXED");
  customFieldMeta.put("precision", "10");
  customFieldMeta.put("scale", "1");

  FieldType fieldType = new FieldType(true,
                                      Types.MinorType.TINYINT.getType(),
                                      null, customFieldMeta);

  TinyIntVector vector = new TinyIntVector("col_one", fieldType, allocator);
  vector.setSafe(0, 200);

  final ArrowVectorConverter converter = new TinyIntToScaledFixedConverter(vector, 0, this, 1);
  final int invalidConversionErrorCode =
      ErrorCode.INVALID_VALUE_CONVERT.getMessageCode();

  TestUtil.assertSFException(invalidConversionErrorCode,
                             () -> converter.toBoolean(0));
  TestUtil.assertSFException(invalidConversionErrorCode,
                             () -> converter.toLong(0));
  TestUtil.assertSFException(invalidConversionErrorCode,
                             () -> converter.toInt(0));
  TestUtil.assertSFException(invalidConversionErrorCode,
                             () -> converter.toShort(0));
  TestUtil.assertSFException(invalidConversionErrorCode,
                             () -> converter.toDate(0));
  TestUtil.assertSFException(invalidConversionErrorCode,
                             () -> converter.toTime(0));
  TestUtil.assertSFException(invalidConversionErrorCode,
                             () -> converter.toTimestamp(0, TimeZone.getDefault()));
  vector.clear();
}
 
Example 4
Source File: TinyIntToFixedConverterTest.java    From snowflake-jdbc with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetSmallerIntegralType() throws SFException
{
  // try convert to int/long/byte/short with scale > 0
  Map<String, String> customFieldMeta = new HashMap<>();
  customFieldMeta.put("logicalType", "FIXED");
  customFieldMeta.put("precision", "10");
  customFieldMeta.put("scale", "0");

  FieldType fieldType = new FieldType(true,
                                      Types.MinorType.TINYINT.getType(),
                                      null, customFieldMeta);

  // test value which is in range of byte, all get method should return
  TinyIntVector vectorBar = new TinyIntVector("col_one", fieldType,
                                              allocator);
  // set value which is out of range of int, but falls in long
  vectorBar.setSafe(0, 10);
  vectorBar.setSafe(1, -10);

  final ArrowVectorConverter converterBar =
      new TinyIntToFixedConverter(vectorBar, 0, this);

  assertThat(converterBar.toShort(0), is((short) 10));
  assertThat(converterBar.toShort(1), is((short) -10));
  assertThat(converterBar.toInt(0), is(10));
  assertThat(converterBar.toInt(1), is(-10));
  assertThat(converterBar.toLong(0), is(10L));
  assertThat(converterBar.toLong(1), is(-10L));
  vectorBar.clear();
}
 
Example 5
Source File: TinyIntToFixedConverterTest.java    From snowflake-jdbc with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetBooleanNoScale() throws SFException
{
  Map<String, String> customFieldMeta = new HashMap<>();
  customFieldMeta.put("logicalType", "FIXED");
  customFieldMeta.put("precision", "10");
  customFieldMeta.put("scale", "0");

  FieldType fieldType = new FieldType(true,
                                      Types.MinorType.TINYINT.getType(),
                                      null, customFieldMeta);

  TinyIntVector vector = new TinyIntVector("col_one", fieldType, allocator);
  vector.setSafe(0, 0);
  vector.setSafe(1, 1);
  vector.setNull(2);
  vector.setSafe(3, 5);

  ArrowVectorConverter converter = new TinyIntToFixedConverter(vector, 0, this);

  assertThat(false, is(converter.toBoolean(0)));
  assertThat(true, is(converter.toBoolean(1)));
  assertThat(false, is(converter.toBoolean(2)));
  TestUtil.assertSFException(invalidConversionErrorCode,
                             () -> converter.toBoolean(3));

  vector.close();
}
 
Example 6
Source File: TinyIntToFixedConverterTest.java    From snowflake-jdbc with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetBooleanWithScale() throws SFException
{
  Map<String, String> customFieldMeta = new HashMap<>();
  customFieldMeta.put("logicalType", "FIXED");
  customFieldMeta.put("precision", "10");
  customFieldMeta.put("scale", "3");

  FieldType fieldType = new FieldType(true,
                                      Types.MinorType.TINYINT.getType(),
                                      null, customFieldMeta);

  TinyIntVector vector = new TinyIntVector("col_one", fieldType, allocator);
  vector.setSafe(0, 0);
  vector.setSafe(1, 1);
  vector.setNull(2);
  vector.setSafe(3, 5);

  final ArrowVectorConverter converter = new TinyIntToScaledFixedConverter(vector, 0, this, 3);

  assertThat(false, is(converter.toBoolean(0)));
  TestUtil.assertSFException(invalidConversionErrorCode,
                             () -> converter.toBoolean(3));
  assertThat(false, is(converter.toBoolean(2)));
  TestUtil.assertSFException(invalidConversionErrorCode,
                             () -> converter.toBoolean(3));

  vector.close();
}
 
Example 7
Source File: TinyIntToFixedConverterTest.java    From snowflake-jdbc with Apache License 2.0 4 votes vote down vote up
@Test
public void testFixedNoScale() throws SFException
{
  final int rowCount = 1000;
  List<Byte> expectedValues = new ArrayList<>();
  Set<Integer> nullValIndex = new HashSet<>();
  for (int i = 0; i < rowCount; i++)
  {
    expectedValues.add((byte) random.nextInt(1 << 8));
  }

  Map<String, String> customFieldMeta = new HashMap<>();
  customFieldMeta.put("logicalType", "FIXED");
  customFieldMeta.put("precision", "10");
  customFieldMeta.put("scale", "0");

  FieldType fieldType = new FieldType(true,
                                      Types.MinorType.TINYINT.getType(),
                                      null, customFieldMeta);

  TinyIntVector vector = new TinyIntVector("col_one", fieldType, allocator);
  for (int i = 0; i < rowCount; i++)
  {
    boolean isNull = random.nextBoolean();
    if (isNull)
    {
      vector.setNull(i);
      nullValIndex.add(i);
    }
    else
    {
      vector.setSafe(i, expectedValues.get(i));
    }
  }

  ArrowVectorConverter converter = new TinyIntToFixedConverter(vector, 0, this);

  for (int i = 0; i < rowCount; i++)
  {
    byte byteVal = converter.toByte(i);
    Object longObject = converter.toObject(i); // the logical type is long
    String byteString = converter.toString(i);

    if (nullValIndex.contains(i))
    {
      assertThat(byteVal, is((byte) 0));
      assertThat(longObject, is(nullValue()));
      assertThat(byteString, is(nullValue()));
    }
    else
    {
      assertThat(byteVal, is(expectedValues.get(i)));
      assertEquals(longObject, (long) expectedValues.get(i));
      assertThat(byteString, is(expectedValues.get(i).toString()));
    }
  }
  vector.clear();
}
 
Example 8
Source File: TinyIntToFixedConverterTest.java    From snowflake-jdbc with Apache License 2.0 4 votes vote down vote up
@Test
public void testFixedWithScale() throws SFException
{
  final int rowCount = 1000;
  List<Byte> expectedValues = new ArrayList<>();
  Set<Integer> nullValIndex = new HashSet<>();
  for (int i = 0; i < rowCount; i++)
  {
    expectedValues.add((byte) random.nextInt(1 << 8));
  }

  Map<String, String> customFieldMeta = new HashMap<>();
  customFieldMeta.put("logicalType", "FIXED");
  customFieldMeta.put("precision", "10");
  customFieldMeta.put("scale", "1");

  FieldType fieldType = new FieldType(true,
                                      Types.MinorType.TINYINT.getType(),
                                      null, customFieldMeta);

  TinyIntVector vector = new TinyIntVector("col_one", fieldType, allocator);
  for (int i = 0; i < rowCount; i++)
  {
    boolean isNull = random.nextBoolean();
    if (isNull)
    {
      vector.setNull(i);
      nullValIndex.add(i);
    }
    else
    {
      vector.setSafe(i, expectedValues.get(i));
    }
  }

  ArrowVectorConverter converter = new TinyIntToScaledFixedConverter(vector, 0, this, 1);

  for (int i = 0; i < rowCount; i++)
  {
    BigDecimal bigDecimalVal = converter.toBigDecimal(i);
    Object objectVal = converter.toObject(i);
    String stringVal = converter.toString(i);

    if (nullValIndex.contains(i))
    {
      assertThat(bigDecimalVal, nullValue());
      assertThat(objectVal, nullValue());
      assertThat(stringVal, nullValue());
    }
    else
    {
      BigDecimal expectedVal = BigDecimal.valueOf(expectedValues.get(i), 1);
      assertThat(bigDecimalVal, is(expectedVal));
      assertThat(objectVal, is(expectedVal));
      assertThat(stringVal, is(expectedVal.toString()));
    }
  }

  vector.clear();
}