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

feat(shannon): parallel query processing #194

Merged
merged 1 commit into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions include/mem_root_deque.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* Copyright (c) 2019, 2023, Oracle and/or its affiliates.
Copyright (c) 2021, Huawei Technologies Co., Ltd.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
Expand All @@ -18,7 +19,9 @@

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

Copyright (c) 2023, Shannon Data AI and/or its affiliates. */

#ifndef MEM_ROOT_DEQUE_H
#define MEM_ROOT_DEQUE_H
Expand Down Expand Up @@ -166,7 +169,7 @@ class mem_root_deque {
~mem_root_deque() { clear(); }

Element_type &operator[](size_t idx) const { return get(idx + m_begin_idx); }

Element_type &at(size_t idx) const { return get(idx + m_begin_idx); }
/**
Adds the given element to the end of the deque.
The element is a copy of the given one.
Expand Down
16 changes: 15 additions & 1 deletion include/my_alloc.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* Copyright (c) 2000, 2023, Oracle and/or its affiliates.
Copyright (c) 2021, Huawei Technologies Co., Ltd.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
Expand All @@ -18,7 +19,9 @@

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

Copyright (c) 2023, Shannon Data AI and/or its affiliates. */

/**
* @file include/my_alloc.h
Expand All @@ -36,6 +39,7 @@
#include <new>
#include <type_traits>
#include <utility>
#include <functional>

#include "memory_debugging.h"
#include "my_compiler.h"
Expand All @@ -48,6 +52,10 @@
extern "C" void sql_alloc_error_handler();
#endif


using CallBackFunc = std::function<void (PSI_memory_key, size_t, uint)>;
constexpr uint PQ_MEMORY_USED_BUCKET = 16;

/**
* The MEM_ROOT is a simple arena, where allocations are carved out of
* larger blocks. Using an arena over plain malloc gives you two main
Expand Down Expand Up @@ -359,6 +367,8 @@ struct MEM_ROOT {
return false;
}

void set_alloc_func(CallBackFunc func) { allocCBFunc = func; }
void set_free_func(CallBackFunc func) { freeCBFunc = func; }
/// @}

private:
Expand Down Expand Up @@ -416,6 +426,10 @@ struct MEM_ROOT {
void (*m_error_handler)(void) = nullptr;

PSI_memory_key m_psi_key = 0;

CallBackFunc allocCBFunc = nullptr;

CallBackFunc freeCBFunc = nullptr;
};

/**
Expand Down
8 changes: 7 additions & 1 deletion include/my_dbug.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* Copyright (c) 2000, 2023, Oracle and/or its affiliates.
Copyright (c) 2021, Huawei Technologies Co., Ltd.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
Expand All @@ -18,7 +19,9 @@

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

Copyright (c) 2023, Shannon Data AI and/or its affiliates. */

#ifndef MY_DBUG_INCLUDED
#define MY_DBUG_INCLUDED
Expand Down Expand Up @@ -91,6 +94,9 @@ extern void _db_unlock_file_(void);
extern FILE *_db_fp_(void);
extern void _db_flush_();

extern void db_stack_copy(CODE_STATE *leader_cs);
extern void db_stack_reset();

#ifdef __cplusplus

#if defined(__GNUC__)
Expand Down
6 changes: 6 additions & 0 deletions include/sql_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,12 @@ class String {
@return allocated string or NULL
*/
char *dup(MEM_ROOT *root) const;

void ltrim();

void rtrim();

void rtrim_zero();
};

/**
Expand Down
13 changes: 13 additions & 0 deletions mysys/dbug.cc
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,19 @@ void _db_pop_() {
}
}

void db_stack_copy(CODE_STATE *leader_cs) {
CODE_STATE *cs;
get_code_state_or_return;
assert(cs->stack == &init_settings);
cs->stack = leader_cs->stack;
}

void db_stack_reset() {
CODE_STATE *cs;
get_code_state_or_return;
cs->stack = &init_settings;
}

/*
* FUNCTION
*
Expand Down
63 changes: 63 additions & 0 deletions sql-common/sql_string.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,69 @@ char *String::dup(MEM_ROOT *root) const {
return strmake_root(root, m_ptr, m_length);
}

void String::ltrim() {
if (m_ptr) {
auto pos = m_ptr;
while (m_length > 0 && *pos && my_isspace(m_charset, *pos)) {
pos++;
m_length--;
}

if (pos != m_ptr) {
memmove(m_ptr, pos, m_length);
if (m_length < m_alloced_length)
m_ptr[m_length] = 0;
else
mem_realloc_exp(m_length);
}
}
}

void String::rtrim() {
if (m_ptr) {
while ((m_length > 0) && my_isspace(m_charset, *(m_ptr + m_length - 1))) {
m_length--;
}
if (m_length < m_alloced_length)
m_ptr[m_length] = 0;
else
mem_realloc_exp(m_length);
}
}

void String::rtrim_zero() {
bool proint_flag = false;
size_t length = m_length;
if (m_ptr) {
while (length > 0) {
if (my_toupper(m_charset, *(m_ptr + length - 1)) == '.') {
proint_flag = true;
break;
}
length--;
}
// trim right last 0, when after '.'
while ((m_length > 0) &&
(my_toupper(m_charset, *(m_ptr + m_length - 1)) == '0') &&
proint_flag) {
m_length--;
}
if (my_toupper(m_charset, *(m_ptr + m_length - 1)) == '.') {
m_length--;
// convert string '.' to '0'
if (m_length == 0) {
m_length = 1;
*m_ptr = '0';
}
}

if (m_length < m_alloced_length)
m_ptr[m_length] = 0;
else
mem_realloc_exp(m_length);
}
}

/**
Convert string to printable ASCII string

Expand Down
6 changes: 6 additions & 0 deletions sql/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ SET(SQL_SHARED_SOURCES
debug_sync.cc
default_values.cc
derror.cc
exchange.cc
error_handler.cc
field.cc
field_conv.cc
Expand Down Expand Up @@ -569,6 +570,8 @@ SET(SQL_SHARED_SOURCES
sql_error.cc
sql_exception_handler.cc
sql_executor.cc
sql_exchange_nosort.cc
sql_exchange_sort.cc
sql_get_diagnostics.cc
sql_gipk.cc
sql_handler.cc
Expand All @@ -582,7 +585,9 @@ SET(SQL_SHARED_SOURCES
sql_load.cc
sql_locale.cc
sql_manager.cc
sql_message_queue.cc
sql_optimizer.cc
sql_parallel.cc
sql_parse.cc
sql_partition.cc
sql_partition_admin.cc
Expand All @@ -592,6 +597,7 @@ SET(SQL_SHARED_SOURCES
sql_prepare.cc
sql_profile.cc
sql_pq_condition.cc
sql_pq_clone.cc
sql_query_rewrite.cc
sql_reload.cc
sql_rename.cc
Expand Down
31 changes: 24 additions & 7 deletions sql/cmp_varlen_keys.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* Copyright (c) 2016, 2023, Oracle and/or its affiliates.
Copyright (c) 2021, Huawei Technologies Co., Ltd.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
Expand All @@ -18,7 +19,9 @@

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

Copyright (c) 2023, Shannon Data AI and/or its affiliates. */

#ifndef CMP_VARLEN_KEYS_INCLUDED
#define CMP_VARLEN_KEYS_INCLUDED
Expand All @@ -44,7 +47,7 @@
*/
inline bool cmp_varlen_keys(
Bounds_checked_array<st_sort_field> sort_field_array, bool use_hash,
const uchar *s1, const uchar *s2) {
const uchar *s1, const uchar *s2, int *pq_cmp_value = nullptr) {
const uchar *kp1 = s1 + Sort_param::size_of_varlength_field;
const uchar *kp2 = s2 + Sort_param::size_of_varlength_field;
int res;
Expand All @@ -53,7 +56,11 @@ inline bool cmp_varlen_keys(
if (sort_field.maybe_null) {
const int k1_nullbyte = *kp1++;
const int k2_nullbyte = *kp2++;
if (k1_nullbyte != k2_nullbyte) return k1_nullbyte < k2_nullbyte;

if (k1_nullbyte != k2_nullbyte) {
if (pq_cmp_value) *pq_cmp_value = k1_nullbyte - k2_nullbyte;
return k1_nullbyte < k2_nullbyte;
}
if (k1_nullbyte == 0 || k1_nullbyte == 0xff) {
if (!sort_field.is_varlen) {
kp1 += sort_field.length;
Expand All @@ -79,12 +86,19 @@ inline bool cmp_varlen_keys(

res = memcmp(kp1, kp2, kp_len);

if (res) return res < 0;
if (res) {
if (pq_cmp_value) *pq_cmp_value = res;
return res < 0;
}

if (kp1_len != kp2_len) {
if (sort_field.reverse)
if (sort_field.reverse) {
if (pq_cmp_value) *pq_cmp_value = kp2_len - kp1_len;
return kp2_len < kp1_len;
else
} else {
if (pq_cmp_value) *pq_cmp_value = kp1_len - kp2_len;
return kp1_len < kp2_len;
}
}

kp1 += kp1_len;
Expand All @@ -93,8 +107,11 @@ inline bool cmp_varlen_keys(

if (use_hash) {
// Compare hashes at the end of sort keys
return memcmp(kp1, kp2, 8) < 0;
int cmp_value = memcmp(kp1, kp2, 8);
if (pq_cmp_value) *pq_cmp_value = cmp_value;
return cmp_value < 0;
} else {
if (pq_cmp_value) *pq_cmp_value = 1;
return false;
}
}
Expand Down
Loading
Loading