[ 
https://issues.apache.org/jira/browse/CASSANDRA-6526?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13856594#comment-13856594
 ] 

Yariv Amar commented on CASSANDRA-6526:
---------------------------------------

below is a test case:
{code:java}
@Test
public void testAddRow() throws Exception
{
        String KS = "cql_keyspace";
        String TABLE = "table1";
        
        File tempdir = Files.createTempDir();
        File dataDir = new File(tempdir.getAbsolutePath() + File.separator + KS 
+ File.separator + TABLE);
        assertTrue( dataDir.mkdirs());
        
        String schema = "CREATE TABLE cql_keyspace.table1 ("
                        + "  k int PRIMARY KEY,"
                        + "  v1 text,"
                        + "  v2 int"
                        + ")";
        String insert = "INSERT INTO cql_keyspace.table1 (k, v1, v2) VALUES (?, 
?, ?)";
        CQLSSTableWriter writer = CQLSSTableWriter.builder()
                        .inDirectory(dataDir)
                        .forTable(schema)
                        
.withPartitioner(StorageService.instance.getPartitioner())
                        .using(insert).build();
        
        Map<String, Object> values = new HashMap<>();
        values.put( "k", 0 );
        values.put( "v1", "test1" );
        values.put( "v2", 24 );
        writer.addRow( values );
        
        values.clear();  
        values.put( "k", 1 );
        values.put( "v1", "test2" );
//      values.put( "v2", null ); //commented intentionally so that v2 will get 
null by the writer.
        writer.addRow( values );
        
        values.clear();
        values.put( "k", 2 );
        values.put( "V1", "test3 - will not be found, since V1 is not 
lowercase" );
        values.put( "v2", 42 );
        values.put( "v3", "some ignored key" );
        writer.addRow( values );
        
        writer.close();
        
        SSTableLoader loader = new SSTableLoader(dataDir, new 
SSTableLoader.Client()
        {
                public void init(String keyspace)
                {
                        for (Range<Token> range : 
StorageService.instance.getLocalRanges("cql_keyspace"))
                                addRangeForEndpoint(range, 
FBUtilities.getBroadcastAddress());
                        setPartitioner(StorageService.getPartitioner());
                }
                
                public CFMetaData getCFMetaData(String keyspace, String cfName)
                {
                        return Schema.instance.getCFMetaData(keyspace, cfName);
                }
        }, new OutputHandler.SystemOutput(false, false));
        
        loader.stream().get();
        
        UntypedResultSet rs = QueryProcessor.processInternal("SELECT * FROM 
cql_keyspace.table1;");
        assertEquals(3, rs.size());
        
        Iterator<UntypedResultSet.Row> iter = rs.iterator();
        UntypedResultSet.Row row;
        
        row = iter.next();
        assertEquals(0, row.getInt("k"));
        assertEquals("test1", row.getString("v1"));
        assertEquals(24, row.getInt("v2"));
        
        row = iter.next();
        assertEquals(1, row.getInt("k"));
        assertEquals("test2", row.getString("v1"));
        assertFalse(row.has("v2"));
        
        row = iter.next();
        assertEquals(2, row.getInt("k"));
        assertFalse(row.has("v1"));
        assertEquals(42, row.getInt("v2"));
        assertFalse(row.has("v3"));

}

{code}

> CQLSSTableWriter addRow(Map<String, Object> values) does not work as 
> documented.
> --------------------------------------------------------------------------------
>
>                 Key: CASSANDRA-6526
>                 URL: https://issues.apache.org/jira/browse/CASSANDRA-6526
>             Project: Cassandra
>          Issue Type: Bug
>          Components: Core
>            Reporter: Yariv Amar
>             Fix For: 2.0.4
>
>   Original Estimate: 24h
>  Remaining Estimate: 24h
>
> There are 2 bugs in the method
> {code}
> addRow(Map<String, Object> values)
> {code}
> First issue is that the map <b>must</b> contain all the column names as keys 
> in the map otherwise the addRow fails (with InvalidRequestException "Invalid 
> number of arguments, expecting %d values but got %d").
> Second Issue is that the keys in the map must be in lower-case otherwise they 
> may not be found in the map, which will result in a NPE during decompose.
> h6. SUGGESTED SOLUTION:
> Fix the addRow method with:
> {code}
> public CQLSSTableWriter addRow(Map<String, Object> values)
>     throws InvalidRequestException, IOException
> {
>     int size = boundNames.size();
>     Map<String, ByteBuffer> rawValues = new HashMap<>(size);
>     for (int i = 0; i < size; i++) {
>         ColumnSpecification spec = boundNames.get(i);
>         String colName = spec.name.toString();
>         rawValues.put(colName, values.get(colName) == null ? null : 
> ((AbstractType)spec.type).decompose(values.get(colName)));
>     }
>     return rawAddRow(rawValues);
> }
> {code}
> When creating the new Map for the insert we need to go over all columns and 
> apply null to missing columns.
> Fix the method documentation add this line:
> {code}
>      * <p>
>      * Keys in the map <b>must</b> be in lower case, otherwise their value 
> will be null.
>      *
> {code}



--
This message was sent by Atlassian JIRA
(v6.1.5#6160)

Reply via email to