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

Added Support for Fetch Modes #4

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
22 changes: 22 additions & 0 deletions SeasClick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,10 +354,32 @@ PHP_METHOD(SEASCLICK_RES_NAME, select)
zval *return_tmp;
for (size_t row = 0; row < block.GetRowCount(); ++row)
{
if (fetch_mode & SC_FETCH_KEY_PAIR) {
if (block.GetColumnCount() < 2) {
throw std::runtime_error("Key pair mode requires at least 2 columns to be present");
}
zval *col1, *col2;
SC_MAKE_STD_ZVAL(col1);
SC_MAKE_STD_ZVAL(col2);

convertToZval(col1, block[0], row, "", 0, fetch_mode|SC_FETCH_ONE);
convertToZval(col2, block[1], row, "", 0, fetch_mode|SC_FETCH_ONE);

if (Z_TYPE_P(col1) == IS_LONG) {
zend_hash_index_update(Z_ARRVAL_P(return_value), Z_LVAL_P(col1), col2);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compatibility

#define zend_hash_index_update(ht, h, pData, nDataSize, pDest) _zend_hash_index_update_or_next_insert(ht, h, pData, nDataSize, pDest, HASH_UPDATE ZEND_FILE_LINE_CC)

This is the definition of PHP 5 version

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

} else {
convert_to_string(col1);
zend_symtable_update(Z_ARRVAL_P(return_value), Z_STR_P(col1), col2);
}
zval_ptr_dtor(col1);
continue;
}

SC_MAKE_STD_ZVAL(return_tmp);
if (!(fetch_mode & SC_FETCH_COLUMN)) {
array_init(return_tmp);
}

for (size_t column = 0; column < block.GetColumnCount(); ++column)
{
string column_name = block.GetColumnName(column);
Expand Down
329 changes: 329 additions & 0 deletions tests/013.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,329 @@
--TEST--
SeasClick Single Column Fetch
--SKIPIF--
<?php if (!extension_loaded("SeasClick")) print "skip"; ?>
--FILE--
<?php
$config = [
"host" => "clickhouse",
"port" => "9000",
"compression" => true,
];

$deleteTable = true;
$client = new SeasClick($config);
$client->execute('CREATE DATABASE IF NOT EXISTS test');

$client->execute("CREATE TABLE IF NOT EXISTS test.single_val_fetch (
tuple_c Tuple(id UInt64, name String),
int64_c UInt64,
string_c String,
array_c Array(Int8),
arraynull_c Array(Nullable(String)),
enum8_c Enum8('One8' = 1, 'Two8' = 2),
enum16_c Enum16('One16' = 1, 'Two16' = 2),
fixedstring_c FixedString(50),
int8null_c Nullable(Int8),
stringnull_c Nullable(String),
enumnull_c Nullable(Enum8('One8' = 1, 'Two8' = 2)),
float32null_c Nullable(Float32),
uuidnull_c Nullable(UUID),
int8_c Int8,
int16_c Int16,
uint8_c UInt8,
uint16_c UInt16,
float32_c Float32,
float64_c Float64,
uuid_c UUID,
uuid2_c UUID,
date_c Date,
datetime_c DateTime
) ENGINE = Memory");

$data = [
[
'tuple_c' => [1, 'one'],
'int64_c' => 1,
'string_c' => 'string_one',
'array_c' => [1, 2, 3],
'arraynull_c' => ['str_array'],
'enum8_c' => 1,
'enum16_c' => 'Two16',
'fixedstring_c' => 'fixedstring_c1',
'int8null_c' => 8,
'stringnull_c' => 'string',
'enumnull_c' => 'One8',
'float32null_c' => 7.77,
'uuidnull_c' => '31249a1b7b0542709f37c609b48a9bb2',
'int8_c' => 8,
'int16_c' => 16,
'uint8_c' => 18,
'uint16_c' => 20,
'float32_c' => 32.32,
'float64_c' => 64.64,
'uuid_c' => '31249a1b-7b05-4270-9f37-c609b48a9bb2',
'uuid2_c' => '31249a1b7b0542709f37c609b48a9bb2',
'date_c' => 1548633600,
'datetime_c' => 1548687925,
],
[
'tuple_c' => [2, 'two'],
'int64_c' => 2,
'string_c' => 'string_two',
'array_c' => [2, 3, 4],
'arraynull_c' => [null],
'enum8_c' => 'Two8',
'enum16_c' => 2,
'fixedstring_c' => 'fixedstring_c2',
'int8null_c' => null,
'stringnull_c' => null,
'enumnull_c' => null,
'float32null_c' => null,
'uuidnull_c' => null,
'int8_c' => 28,
'int16_c' => 216,
'uint8_c' => 218,
'uint16_c' => 220,
'float32_c' => 232.32,
'float64_c' => 264.64,
'uuid_c' => '31249a1b-7b05-4270-9f37-c609b48a9bb2',
'uuid2_c' => null,
'date_c' => 1548547200,
'datetime_c' => 1548513600,
],
];

$fields = array_keys(current($data));

$expected = $data;
$expected[0]['uuid_c'] = '31249a1b7b0542709f37c609b48a9bb2';
$expected[0]['enum8_c'] = 'One8';
$expected[1]['enum16_c'] = 'Two16';
$expected[1]['uuid_c'] = '31249a1b7b0542709f37c609b48a9bb2';
$expected[1]['arraynull_c'] = [null];
$expected[1]['uuid2_c'] = '00000000000000000000000000000000';

$client->insert('test.single_val_fetch', $fields, [array_values($data[0]), array_values($data[1])]);

foreach ($fields as $field) {
$result = $client->select("SELECT {$field} FROM test.single_val_fetch ORDER BY int64_c ASC", [], SeasClick::FETCH_COLUMN);

$res = var_export($result, true);
$exp = var_export([$expected[0][$field], $expected[1][$field]], true);
$match = $res === $exp ? 'OK' : 'FAIL';

echo $field, ': ', $res , ' - ', $exp , ' - ', $match, "\n";
}

$client->execute('DROP TABLE test.single_val_fetch');
?>
--EXPECT--
tuple_c: array (
0 =>
array (
0 => 1,
1 => 'one',
),
1 =>
array (
0 => 2,
1 => 'two',
),
) - array (
0 =>
array (
0 => 1,
1 => 'one',
),
1 =>
array (
0 => 2,
1 => 'two',
),
) - OK
int64_c: array (
0 => 1,
1 => 2,
) - array (
0 => 1,
1 => 2,
) - OK
string_c: array (
0 => 'string_one',
1 => 'string_two',
) - array (
0 => 'string_one',
1 => 'string_two',
) - OK
array_c: array (
0 =>
array (
0 => 1,
1 => 2,
2 => 3,
),
1 =>
array (
0 => 2,
1 => 3,
2 => 4,
),
) - array (
0 =>
array (
0 => 1,
1 => 2,
2 => 3,
),
1 =>
array (
0 => 2,
1 => 3,
2 => 4,
),
) - OK
arraynull_c: array (
0 =>
array (
0 => 'str_array',
),
1 =>
array (
0 => NULL,
),
) - array (
0 =>
array (
0 => 'str_array',
),
1 =>
array (
0 => NULL,
),
) - OK
enum8_c: array (
0 => 'One8',
1 => 'Two8',
) - array (
0 => 'One8',
1 => 'Two8',
) - OK
enum16_c: array (
0 => 'Two16',
1 => 'Two16',
) - array (
0 => 'Two16',
1 => 'Two16',
) - OK
fixedstring_c: array (
0 => 'fixedstring_c1',
1 => 'fixedstring_c2',
) - array (
0 => 'fixedstring_c1',
1 => 'fixedstring_c2',
) - OK
int8null_c: array (
0 => 8,
1 => NULL,
) - array (
0 => 8,
1 => NULL,
) - OK
stringnull_c: array (
0 => 'string',
1 => NULL,
) - array (
0 => 'string',
1 => NULL,
) - OK
enumnull_c: array (
0 => 'One8',
1 => NULL,
) - array (
0 => 'One8',
1 => NULL,
) - OK
float32null_c: array (
0 => 7.77,
1 => NULL,
) - array (
0 => 7.77,
1 => NULL,
) - OK
uuidnull_c: array (
0 => '31249a1b7b0542709f37c609b48a9bb2',
1 => NULL,
) - array (
0 => '31249a1b7b0542709f37c609b48a9bb2',
1 => NULL,
) - OK
int8_c: array (
0 => 8,
1 => 28,
) - array (
0 => 8,
1 => 28,
) - OK
int16_c: array (
0 => 16,
1 => 216,
) - array (
0 => 16,
1 => 216,
) - OK
uint8_c: array (
0 => 18,
1 => 218,
) - array (
0 => 18,
1 => 218,
) - OK
uint16_c: array (
0 => 20,
1 => 220,
) - array (
0 => 20,
1 => 220,
) - OK
float32_c: array (
0 => 32.32,
1 => 232.32,
) - array (
0 => 32.32,
1 => 232.32,
) - OK
float64_c: array (
0 => 64.64,
1 => 264.64,
) - array (
0 => 64.64,
1 => 264.64,
) - OK
uuid_c: array (
0 => '31249a1b7b0542709f37c609b48a9bb2',
1 => '31249a1b7b0542709f37c609b48a9bb2',
) - array (
0 => '31249a1b7b0542709f37c609b48a9bb2',
1 => '31249a1b7b0542709f37c609b48a9bb2',
) - OK
uuid2_c: array (
0 => '31249a1b7b0542709f37c609b48a9bb2',
1 => '00000000000000000000000000000000',
) - array (
0 => '31249a1b7b0542709f37c609b48a9bb2',
1 => '00000000000000000000000000000000',
) - OK
date_c: array (
0 => 1548633600,
1 => 1548547200,
) - array (
0 => 1548633600,
1 => 1548547200,
) - OK
datetime_c: array (
0 => 1548687925,
1 => 1548513600,
) - array (
0 => 1548687925,
1 => 1548513600,
) - OK
Loading