stoneatom / stonedb

StoneDB is an Open-Source MySQL HTAP and MySQL-Native DataBase for OLTP, Real-Time Analytics, a counterpart of MySQLHeatWave. (https://stonedb.io)
https://stonedb.io/
GNU General Public License v2.0
866 stars 141 forks source link

bug: query result not correct #1942

Closed zhenglin-charlie-li closed 1 year ago

zhenglin-charlie-li commented 1 year ago

Have you read the Contributing Guidelines on issues?

Please confirm if bug report does NOT exists already ?

Describe the problem

SQLancer found that the query below result is not correct on docker v1.0.4, though correct result on docker v1.0.3

image

Expected behavior

both two select return NULL

How To Reproduce

CREATE TABLE t0(c0 INT);
SELECT * FROM t0 WHERE (t0.c0 IS NULL); 
SELECT SUM(count) FROM (SELECT (t0.c0 IS NULL) IS TRUE  as count FROM t0) as res; -- Expted NULL but got 1
SELECT SUM(count) FROM (SELECT CAST((t0.c0 IS NULL) AS UNSIGNED)  as count FROM t0) as res;  -- Expted NULL but got 1

Environment

docker v1.0.4

Are you interested in submitting a PR to solve the problem?

Double0101 commented 1 year ago

In the function AggregationAlgorithm::Aggregate in aggregation_algorithm.cpp, it doesn't check validity of mit before PutAggregatedValue.

void AggregationAlgorithm::Aggregate(bool just_distinct, int64_t &limit, int64_t &offset, ResultSender *sender) {
  MEASURE_FET("TempTable::Aggregate(...)");
  thd_proc_info(m_conn->Thd(), "aggregation");
......
  {
      for (int gr_a = gbw.NumOfGroupingAttrs(); gr_a < gbw.NumOfAttrs(); gr_a++) {
        TempTable::Attr &cur_a = *(t->GetAttrP(gr_a));

        if (cur_a.term.vc && dynamic_cast<Tianmu::vcolumn::ExpressionColumn *>(cur_a.term.vc)) {
          bool value_successfully_aggregated = gbw.PutAggregatedValue(gr_a, 0, mit, mit.Factor());
          if (!value_successfully_aggregated) {
            gbw.DistinctlyOmitted(gr_a, 0);
          }
        }
      }
    }
  } 
......
}