Skip to content

Commit

Permalink
Merge branch 'stonedb-5.7-dev' of github.com:adofsauron/stonedb-dev-2…
Browse files Browse the repository at this point in the history
…0230323 into stonedb-5.7-dev
  • Loading branch information
adofsauron committed Mar 30, 2023
2 parents 3649acb + 05db04d commit 0fe2904
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
DROP DATABASE IF EXISTS issue1476_test;
CREATE DATABASE issue1476_test;
USE issue1476_test;
DROP DATABASE IF EXISTS issue1402_test;
CREATE DATABASE issue1402_test;
USE issue1402_test;
CREATE TABLE t1 (id INT, d DECIMAL(10,4)) ENGINE=tianmu;
INSERT INTO t1 VALUES (1, 1.2345), (2, 2.3456), (3, 3.4567);
SELECT COUNT(*), SUM(d), AVG(d), MIN(d), MAX(d) FROM t1;
Expand Down Expand Up @@ -50,4 +50,4 @@ SELECT t1.id, SUM(t1.d), SUM(t2.d) FROM t1 JOIN (SELECT id, d FROM t1 WHERE d >
id SUM(t1.d) SUM(t2.d)
SELECT t1.id, SUM(t1.d), SUM(t2.d) FROM t1 JOIN (SELECT id, d FROM t1 WHERE d > 2) t2 ON t1.id = t2.id WHERE t1.id > 1 GROUP BY t1.id HAVING SUM(t1.d) > 3 ORDER BY SUM(t1.d) DESC LIMIT 1 OFFSET 1;
id SUM(t1.d) SUM(t2.d)
drop database issue1476_test;
drop database issue1402_test;
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
--source include/have_tianmu.inc

--disable_warnings
DROP DATABASE IF EXISTS issue1476_test;
DROP DATABASE IF EXISTS issue1402_test;
--enable_warnings

CREATE DATABASE issue1476_test;
CREATE DATABASE issue1402_test;

USE issue1476_test;
USE issue1402_test;

CREATE TABLE t1 (id INT, d DECIMAL(10,4)) ENGINE=tianmu;

Expand Down Expand Up @@ -40,4 +40,4 @@ SELECT t1.id, SUM(t1.d), SUM(t2.d) FROM t1 JOIN (SELECT id, d FROM t1 WHERE d >

SELECT t1.id, SUM(t1.d), SUM(t2.d) FROM t1 JOIN (SELECT id, d FROM t1 WHERE d > 2) t2 ON t1.id = t2.id WHERE t1.id > 1 GROUP BY t1.id HAVING SUM(t1.d) > 3 ORDER BY SUM(t1.d) DESC LIMIT 1 OFFSET 1;

drop database issue1476_test;
drop database issue1402_test;
2 changes: 1 addition & 1 deletion sql/item.h
Original file line number Diff line number Diff line change
Expand Up @@ -3570,7 +3570,7 @@ class Item_decimal :public Item_num
enum_field_types field_type() const { return MYSQL_TYPE_NEWDECIMAL; }
longlong val_int();
double val_real();
String *val_str(String*);
virtual String *val_str(String*);
my_decimal *val_decimal(my_decimal *val) { return &decimal_value; }
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate)
{
Expand Down
46 changes: 26 additions & 20 deletions storage/tianmu/core/aggregation_algorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ void AggregationAlgorithm::Aggregate(bool just_distinct, int64_t &limit, int64_t

for (uint i = 0; i < t->NumOfAttrs(); i++) { // first pass: find all grouping attributes
TempTable::Attr &cur_a = *(t->GetAttrP(i));
if (cur_a.mode == common::ColOperation::DELAYED) // delayed column (e.g. complex exp. on
// aggregations)

// delayed column (e.g. complex exp. on aggregations)
if (cur_a.mode == common::ColOperation::DELAYED)
continue;

if ((just_distinct && cur_a.alias) || cur_a.mode == common::ColOperation::GROUP_BY) {
if (cur_a.mode == common::ColOperation::GROUP_BY)
group_by_found = true;
Expand All @@ -62,10 +64,9 @@ void AggregationAlgorithm::Aggregate(bool just_distinct, int64_t &limit, int64_t
break;
}
}
if (already_added == false) {
if (!already_added) {
int new_attr_number = gbw.NumOfGroupingAttrs();
gbw.AddGroupingColumn(new_attr_number, i,
*(t->GetAttrP(i))); // GetAttrP(i) is needed
gbw.AddGroupingColumn(new_attr_number, i, *(t->GetAttrP(i))); // GetAttrP(i) is needed

// approximate a number of groups
if (upper_approx_of_groups < mind->NumOfTuples()) {
Expand All @@ -80,8 +81,9 @@ void AggregationAlgorithm::Aggregate(bool just_distinct, int64_t &limit, int64_t

for (uint i = 0; i < t->NumOfAttrs(); i++) { // second pass: find all aggregated attributes
TempTable::Attr &cur_a = *(t->GetAttrP(i));
if (cur_a.mode == common::ColOperation::DELAYED) { // delayed column (e.g. complex exp.
// on aggregations)

// delayed column (e.g. complex exp.on aggregations)
if (cur_a.mode == common::ColOperation::DELAYED) {
MIDummyIterator m(1);
cur_a.term.vc->LockSourcePacks(m);
continue;
Expand Down Expand Up @@ -109,19 +111,22 @@ void AggregationAlgorithm::Aggregate(bool just_distinct, int64_t &limit, int64_t
max_size = cur_a.term.vc->MaxStringSize();
min_v = cur_a.term.vc->RoughMin();
max_v = cur_a.term.vc->RoughMax();
if (cur_a.distinct && cur_a.term.vc->IsDistinct() && cur_a.mode != common::ColOperation::LISTING)
if (cur_a.distinct && cur_a.term.vc->IsDistinct() && cur_a.mode != common::ColOperation::LISTING) {
cur_a.distinct = false; // "distinct" not needed, as values are distinct anyway
else if (cur_a.distinct) {
} else if (cur_a.distinct) {
max_no_of_distinct = cur_a.term.vc->GetApproxDistVals(false); // no nulls included
if (tianmu_control_.isOn())
tianmu_control_.lock(m_conn->GetThreadID())
<< "Adding dist. column, min = " << min_v << ", max = " << max_v << ", dist = " << max_no_of_distinct
<< system::unlock;
}
}

// special case: aggregations on empty result (should not
// be 0, because it triggers max. buffer settings)
if (max_no_of_distinct == 0)
max_no_of_distinct = 1; // special case: aggregations on empty result (should not
// be 0, because it triggers max. buffer settings)
max_no_of_distinct = 1;

gbw.AddAggregatedColumn(i, cur_a, max_no_of_distinct, min_v, max_v, max_size);
}
}
Expand Down Expand Up @@ -169,8 +174,7 @@ void AggregationAlgorithm::Aggregate(bool just_distinct, int64_t &limit, int64_t
gbw.PutAggregatedValueForCount(0, row, count_distinct);
all_done_in_one_row = true;
}
} // Special case 3: SELECT MIN(col) FROM ..... or SELECT MAX(col) FROM
// .....;
} // Special case 3: SELECT MIN(col) FROM ..... or SELECT MAX(col) FROM ...;
else if (t->GetWhereConds().Size() == 0 &&
((gbw.IsMinOnly() && t->NumOfAttrs() == 1 && min_v != common::MINUS_INF_64) ||
(gbw.IsMaxOnly() && t->NumOfAttrs() == 1 && max_v != common::PLUS_INF_64))) {
Expand All @@ -189,9 +193,9 @@ void AggregationAlgorithm::Aggregate(bool just_distinct, int64_t &limit, int64_t
t->GetAttrP(i)->page_size = 1;
t->GetAttrP(i)->CreateBuffer(1);
}
if (limit == -1 || (offset == 0 && limit >= 1)) { // limit is -1 (off), or a positive
// number, 0 means nothing should be
// displayed.

// limit is -1 (off), or a positive number, 0 means nothing should be displayed.
if (limit == -1 || (offset == 0 && limit >= 1)) {
--limit;
AggregateFillOutput(gbw, row, offset);
if (sender) {
Expand All @@ -206,11 +210,13 @@ void AggregationAlgorithm::Aggregate(bool just_distinct, int64_t &limit, int64_t
if (limit != -1)
limit = local_limit;
}
t->ClearMultiIndexP(); // cleanup (i.e. regarded as materialized,
// one-dimensional)

// cleanup (i.e. regarded as materialized, one-dimensional)
t->ClearMultiIndexP();

// to prevent another execution of HAVING on DISTINCT+GROUP BY
if (t->HasHavingConditions())
t->ClearHavingConditions(); // to prevent another execution of HAVING on
// DISTINCT+GROUP BY
t->ClearHavingConditions();
}

void AggregationAlgorithm::MultiDimensionalGroupByScan(GroupByWrapper &gbw, int64_t &limit, int64_t &offset,
Expand Down
3 changes: 3 additions & 0 deletions storage/tianmu/core/item_tianmu_field.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#pragma once

#include <map>
#include <mutex>
#include <set>

#include "common/common_definitions.h"
Expand Down Expand Up @@ -133,10 +134,12 @@ class Item_tianmudecimal : public Item_decimal {
Item_tianmudecimal(DataType t);

void Set(int64_t val);
String *val_str(String *) override;

private:
int scale;
int64_t scaleCoef;
std::mutex mtx;
};

//! Base class for TIANMU's Item classes to store date/time values of columns
Expand Down
1 change: 1 addition & 0 deletions storage/tianmu/core/pack_guardian.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ void VCPackGuardian::LockPackrowOnLockOneByThread(const MIIterator &mit) {
last_pack_thread_[thread_id] = std::move(pack_value);
}
} else {
std::scoped_lock lock(mx_thread_);
auto &lock_thread = last_pack_thread_[thread_id];
auto &lock_dim = lock_thread[cur_dim];
lock_dim[col_index] = cur_pack;
Expand Down

0 comments on commit 0fe2904

Please sign in to comment.