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

Can't cast from Nullable(UInt64) #368

Open
Sembl4 opened this issue Apr 18, 2024 · 2 comments
Open

Can't cast from Nullable(UInt64) #368

Sembl4 opened this issue Apr 18, 2024 · 2 comments
Labels
need-info Awaiting extra info from community/issue creator

Comments

@Sembl4
Copy link

Sembl4 commented Apr 18, 2024

Hello, everyone!
I try to get a value from Nullable(UInt64) column, and I want to get std::optional<uint64_t> as a result, with this code:

block[col]->AsStrict<AsStrict<ColumnNullableT<ColumnUInt64>>()->At(row);

But I have an exception:

terminate called after throwing an instance of 'clickhouse::ValidationError'
what():  Can't cast from Nullable(UInt64)

I can get a value only this way, and this code works good:

if(!block[b_p_b]->AsStrict<ColumnNullable>()->IsNull(row)){
   std::cout << block[b_p_b]->AsStrict<ColumnNullable>()->GetItem(row).get<uint64_t>() << std::endl;
}

But this way is not very good for me, it would be much better if I could get std::optional<uint64_t> as a result of this operation.
Please tell me how can I do this?
Thanks in advance!

@Enmk
Copy link
Collaborator

Enmk commented Apr 18, 2024

Hi @Sembl4, thank you for a report!

Could you please specify library version and minimal viable example that against which you are able to reproduce the issue?

@Enmk Enmk added the need-info Awaiting extra info from community/issue creator label Apr 18, 2024
@Seredenko-V
Copy link

@Enmk, hi!

I have the same problem when I try to use the result of executing SELECT. The table stores only one column with the Nullable(Int64) type. An example of the code is attached

#include <clickhouse/client.h>
#include <iostream>

using namespace clickhouse;
using namespace std;

int main()
{
   // ok
   {
      ColumnRef column = std::make_shared< ColumnNullableT< ColumnInt64 > >();
      Block block;
      block.AppendColumn( "nullable_test"s, column );

      auto column2 = block[ 0 ]->As< ::clickhouse::ColumnNullable >();
      block[ 0 ]->AsStrict< ::clickhouse::ColumnNullableT< ::clickhouse::ColumnInt64 > >()->Append( 8 );
      cout << block[ 0 ]->AsStrict< ::clickhouse::ColumnNullableT< ::clickhouse::ColumnInt64 > >()->At( 0 ).value()
           << endl;
   }

   // have proplem
   Client client( ClientOptions().SetHost( "localhost" ) );
   client.Execute( "DROP TABLE IF EXISTS test_data;"s );
   client.Execute( "CREATE TABLE IF NOT EXISTS test_data (nullable_test Nullable(Int64)) ENGINE = Memory;"s );

   Block block;
   ColumnRef column = std::make_shared< ColumnNullableT< ColumnInt64 > >();
   column->AsStrict< ColumnNullableT< ColumnInt64 > >()->Append( 5 );
   block.AppendColumn( "nullable_test"s, column );
   client.Insert( "test_data"s, block );

   clickhouse::Block answer;
   auto handler = [ &answer ]( const Block& block )
   {
      if( block.GetColumnCount() > 0 && block.GetRowCount() > 0 )
         answer = block;
   };

   Query query( "SELECT * FROM test_data;"s );
   query.OnData( handler );
   client.Execute( query );

   //   answer[ 0 ]->AsStrict< ColumnNullableT< ColumnInt64 > >()->Append( 10 );

   // exception "Can't cast from Nullable(Int64)"
   cout << answer[ 0 ]->AsStrict< ColumnNullableT< ColumnInt64 > >()->At( 0 ).has_value() << endl;

   client.Execute( "DROP TABLE IF EXISTS test_data;"s );
   return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
need-info Awaiting extra info from community/issue creator
Projects
None yet
Development

No branches or pull requests

3 participants