Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Selecting elements of nested array  #43

@kruglov-dmitry

Description

@kruglov-dmitry

Given table:

CREATE TABLE IF NOT EXISTS maps.multiarray
(
    `arr` Array(Array(UInt64))
)
ENGINE = Memory;

with single row:

INSERT INTO maps.multiarray VALUES ([[0,1,2,3,4,5], [100, 200], [10,20, 50, 70]]);

I am trying to run select, using code similar to what is written at test:

string SELECT_QUERY = "SELECT arr FROM maps.multiarray";
client.Select(SELECT_QUERY, [](const Block& block)
{
    if (block.GetRowCount() == 0) {
         return;
    }

    for (size_t c = 0; c < block.GetRowCount(); ++c) {
        auto col = block[0]->As<ColumnArray>()->GetAsColumn(c);
	cout << col->Size() << endl;
	cout << "[";
	for (size_t i = 0; i < col->Size(); ++i) {
	      auto col2 = col->As<ColumnArray>()->GetAsColumn(i);
              for (size_t j = 0; j < col2->Size(); ++j) {
		     cout << (int)(*col2->As<ColumnUInt64>())[j];
                     if (j + 1 != col2->Size()) {
			cout << " ";
                     }
               }
       }
       std::cout << "]" << std::endl;
    }
});

And as a result got only this kind of output:

1
[0]

While I was expecting to have something like that:

5
[0 1 2 3 4 5]
2
[100 200]
4
[10 20 50 70]

I've tried to access exact same table using client in other languages and it seems to work as expected:

from clickhouse_driver import connect
conn = connect('clickhouse://localhost')
cursor = conn.cursor()
cursor.execute('SELECT * FROM maps.multiarray')
cursor.fetchall()
[([[0, 1, 2, 3, 4, 5], [100, 200], [10, 20, 50, 70]],)]

I've also saw similar issue within alternative(?) implementation - here.

I can sort of work-around it by issuing first SELECT length(arr) FROM maps.multiarray with subsequent series of calls select arrayElement(arr, idx) from maps.multiarray;

But it is not clear is it limitation of existing client or I just miss something important in my implementation?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions