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

ORC-1813: [C++] Fix has_null forward compatibility #2082

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 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
20 changes: 10 additions & 10 deletions c++/src/Statistics.cc
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,13 @@ namespace orc {

ColumnStatisticsImpl::ColumnStatisticsImpl(const proto::ColumnStatistics& pb) {
stats_.setNumberOfValues(pb.number_of_values());
stats_.setHasNull(pb.has_null());
stats_.setHasNull(pb.has_has_null() ? pb.has_null() : true);
}

BinaryColumnStatisticsImpl::BinaryColumnStatisticsImpl(const proto::ColumnStatistics& pb,
const StatContext& statContext) {
stats_.setNumberOfValues(pb.number_of_values());
stats_.setHasNull(pb.has_null());
stats_.setHasNull(pb.has_has_null() ? pb.has_null() : true);
if (pb.has_binary_statistics() && statContext.correctStats) {
stats_.setHasTotalLength(pb.binary_statistics().has_sum());
stats_.setTotalLength(static_cast<uint64_t>(pb.binary_statistics().sum()));
Expand All @@ -197,7 +197,7 @@ namespace orc {
BooleanColumnStatisticsImpl::BooleanColumnStatisticsImpl(const proto::ColumnStatistics& pb,
const StatContext& statContext) {
stats_.setNumberOfValues(pb.number_of_values());
stats_.setHasNull(pb.has_null());
stats_.setHasNull(pb.has_has_null() ? pb.has_null() : true);
if (pb.has_bucket_statistics() && statContext.correctStats) {
hasCount_ = true;
trueCount_ = pb.bucket_statistics().count(0);
Expand All @@ -210,7 +210,7 @@ namespace orc {
DateColumnStatisticsImpl::DateColumnStatisticsImpl(const proto::ColumnStatistics& pb,
const StatContext& statContext) {
stats_.setNumberOfValues(pb.number_of_values());
stats_.setHasNull(pb.has_null());
stats_.setHasNull(pb.has_has_null() ? pb.has_null() : true);
if (!pb.has_date_statistics() || !statContext.correctStats) {
// hasMinimum_ is false by default;
// hasMaximum_ is false by default;
Expand All @@ -227,7 +227,7 @@ namespace orc {
DecimalColumnStatisticsImpl::DecimalColumnStatisticsImpl(const proto::ColumnStatistics& pb,
const StatContext& statContext) {
stats_.setNumberOfValues(pb.number_of_values());
stats_.setHasNull(pb.has_null());
stats_.setHasNull(pb.has_has_null() ? pb.has_null() : true);
if (pb.has_decimal_statistics() && statContext.correctStats) {
const proto::DecimalStatistics& stats = pb.decimal_statistics();
stats_.setHasMinimum(stats.has_minimum());
Expand All @@ -242,7 +242,7 @@ namespace orc {

DoubleColumnStatisticsImpl::DoubleColumnStatisticsImpl(const proto::ColumnStatistics& pb) {
stats_.setNumberOfValues(pb.number_of_values());
stats_.setHasNull(pb.has_null());
stats_.setHasNull(pb.has_has_null() ? pb.has_null() : true);
if (!pb.has_double_statistics()) {
stats_.setMinimum(0);
stats_.setMaximum(0);
Expand All @@ -261,7 +261,7 @@ namespace orc {

IntegerColumnStatisticsImpl::IntegerColumnStatisticsImpl(const proto::ColumnStatistics& pb) {
stats_.setNumberOfValues(pb.number_of_values());
stats_.setHasNull(pb.has_null());
stats_.setHasNull(pb.has_has_null() ? pb.has_null() : true);
if (!pb.has_int_statistics()) {
stats_.setMinimum(0);
stats_.setMaximum(0);
Expand All @@ -281,7 +281,7 @@ namespace orc {
StringColumnStatisticsImpl::StringColumnStatisticsImpl(const proto::ColumnStatistics& pb,
const StatContext& statContext) {
stats_.setNumberOfValues(pb.number_of_values());
stats_.setHasNull(pb.has_null());
stats_.setHasNull(pb.has_has_null() ? pb.has_null() : true);
if (!pb.has_string_statistics() || !statContext.correctStats) {
stats_.setTotalLength(0);
} else {
Expand All @@ -299,7 +299,7 @@ namespace orc {
TimestampColumnStatisticsImpl::TimestampColumnStatisticsImpl(const proto::ColumnStatistics& pb,
const StatContext& statContext) {
stats_.setNumberOfValues(pb.number_of_values());
stats_.setHasNull(pb.has_null());
stats_.setHasNull(pb.has_has_null() ? pb.has_null() : true);
if (!pb.has_timestamp_statistics() || !statContext.correctStats) {
stats_.setMinimum(0);
stats_.setMaximum(0);
Expand Down Expand Up @@ -365,7 +365,7 @@ namespace orc {
CollectionColumnStatisticsImpl::CollectionColumnStatisticsImpl(
const proto::ColumnStatistics& pb) {
stats_.setNumberOfValues(pb.number_of_values());
stats_.setHasNull(pb.has_null());
stats_.setHasNull(pb.has_has_null() ? pb.has_null() : true);
if (!pb.has_collection_statistics()) {
stats_.setMinimum(0);
stats_.setMaximum(0);
Expand Down
36 changes: 20 additions & 16 deletions c++/src/sargs/PredicateLeaf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -390,10 +390,15 @@ namespace orc {

DIAGNOSTIC_POP

static bool colHasNullForwardCompatible(const proto::ColumnStatistics& stats) {
suxiaogang223 marked this conversation as resolved.
Show resolved Hide resolved
suxiaogang223 marked this conversation as resolved.
Show resolved Hide resolved
// for foward compatibility, if has_null is not set, assume that the column has nulls
return stats.has_has_null() ? stats.has_null() : true;
}

static TruthValue evaluateBoolPredicate(const PredicateLeaf::Operator op,
const std::vector<Literal>& literals,
const proto::ColumnStatistics& stats) {
bool hasNull = stats.has_null();
bool hasNull = colHasNullForwardCompatible(stats);
if (!stats.has_bucket_statistics() || stats.bucket_statistics().count_size() == 0) {
// does not have bool stats
return hasNull ? TruthValue::YES_NO_NULL : TruthValue::YES_NO;
Expand Down Expand Up @@ -513,7 +518,7 @@ namespace orc {
colStats.int_statistics().has_maximum()) {
const auto& stats = colStats.int_statistics();
result = evaluatePredicateRange(operator_, literal2Long(literals_), stats.minimum(),
stats.maximum(), colStats.has_null());
stats.maximum(), colHasNullForwardCompatible(colStats));
}
break;
}
Expand All @@ -522,10 +527,11 @@ namespace orc {
colStats.double_statistics().has_maximum()) {
const auto& stats = colStats.double_statistics();
if (!std::isfinite(stats.sum())) {
result = colStats.has_null() ? TruthValue::YES_NO_NULL : TruthValue::YES_NO;
result = colHasNullForwardCompatible(colStats) ? TruthValue::YES_NO_NULL
: TruthValue::YES_NO;
} else {
result = evaluatePredicateRange(operator_, literal2Double(literals_), stats.minimum(),
stats.maximum(), colStats.has_null());
stats.maximum(), colHasNullForwardCompatible(colStats));
}
}
break;
Expand All @@ -536,7 +542,7 @@ namespace orc {
colStats.string_statistics().has_maximum()) {
const auto& stats = colStats.string_statistics();
result = evaluatePredicateRange(operator_, literal2String(literals_), stats.minimum(),
stats.maximum(), colStats.has_null());
stats.maximum(), colHasNullForwardCompatible(colStats));
}
break;
}
Expand All @@ -545,7 +551,7 @@ namespace orc {
colStats.date_statistics().has_maximum()) {
const auto& stats = colStats.date_statistics();
result = evaluatePredicateRange(operator_, literal2Date(literals_), stats.minimum(),
stats.maximum(), colStats.has_null());
stats.maximum(), colHasNullForwardCompatible(colStats));
}
break;
}
Expand All @@ -567,7 +573,7 @@ namespace orc {
stats.maximum_utc() / 1000,
static_cast<int32_t>((stats.maximum_utc() % 1000) * 1000000) + maxNano);
result = evaluatePredicateRange(operator_, literal2Timestamp(literals_), minTimestamp,
maxTimestamp, colStats.has_null());
maxTimestamp, colHasNullForwardCompatible(colStats));
}
break;
}
Expand All @@ -577,7 +583,7 @@ namespace orc {
const auto& stats = colStats.decimal_statistics();
result = evaluatePredicateRange(operator_, literal2Decimal(literals_),
Decimal(stats.minimum()), Decimal(stats.maximum()),
colStats.has_null());
colHasNullForwardCompatible(colStats));
}
break;
}
Expand All @@ -592,7 +598,7 @@ namespace orc {
}

// make sure null literal is respected for IN operator
if (operator_ == Operator::IN && colStats.has_null()) {
if (operator_ == Operator::IN && colHasNullForwardCompatible(colStats)) {
for (const auto& literal : literals_) {
if (literal.isNull()) {
result = TruthValue::YES_NO_NULL;
Expand Down Expand Up @@ -701,24 +707,22 @@ namespace orc {
}
}

// files written by trino may lack of hasnull field.
if (!colStats.has_has_null()) return TruthValue::YES_NO_NULL;
suxiaogang223 marked this conversation as resolved.
Show resolved Hide resolved

bool allNull = colStats.has_null() && colStats.number_of_values() == 0;
bool allNull = colHasNullForwardCompatible(colStats) && colStats.number_of_values() == 0;
if (operator_ == Operator::IS_NULL ||
((operator_ == Operator::EQUALS || operator_ == Operator::NULL_SAFE_EQUALS) &&
literals_.at(0).isNull())) {
// IS_NULL operator does not need to check min/max stats and bloom filter
return allNull ? TruthValue::YES
: (colStats.has_null() ? TruthValue::YES_NO : TruthValue::NO);
return allNull
? TruthValue::YES
: (colHasNullForwardCompatible(colStats) ? TruthValue::YES_NO : TruthValue::NO);
} else if (allNull) {
// if we don't have any value, everything must have been null
return TruthValue::IS_NULL;
}

TruthValue result = evaluatePredicateMinMax(colStats);
if (shouldEvaluateBloomFilter(operator_, result, bloomFilter)) {
return evaluatePredicateBloomFiter(bloomFilter, colStats.has_null());
return evaluatePredicateBloomFiter(bloomFilter, colHasNullForwardCompatible(colStats));
} else {
return result;
}
Expand Down
2 changes: 1 addition & 1 deletion c++/test/TestPredicateLeaf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ namespace orc {
TEST(TestPredicateLeaf, testLackOfSataistics) {
PredicateLeaf pred(PredicateLeaf::Operator::IS_NULL, PredicateDataType::STRING, 1, {});
EXPECT_EQ(TruthValue::YES_NO, evaluate(pred, createStringStats("c", "d", true)));
EXPECT_EQ(TruthValue::YES_NO_NULL, evaluate(pred, createIncompleteNullStats()));
EXPECT_EQ(TruthValue::YES, evaluate(pred, createIncompleteNullStats()));
suxiaogang223 marked this conversation as resolved.
Show resolved Hide resolved
}

} // namespace orc
13 changes: 7 additions & 6 deletions c++/test/TestStripeIndexStatistics.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,39 +46,40 @@ namespace orc {
intColStats = reinterpret_cast<const orc::IntegerColumnStatistics*>(
stripeStats->getRowIndexStatistics(1, 0));
EXPECT_EQ(
"Data type: Integer\nValues: 2000\nHas null: no\nMinimum: 1\nMaximum: 2000\nSum: 2001000\n",
"Data type: Integer\nValues: 2000\nHas null: yes\nMinimum: 1\nMaximum: 2000\nSum: "
"2001000\n",
intColStats->toString());
intColStats = reinterpret_cast<const orc::IntegerColumnStatistics*>(
stripeStats->getRowIndexStatistics(1, 1));
EXPECT_EQ(
"Data type: Integer\nValues: 2000\nHas null: no\nMinimum: 2001\nMaximum: 4000\nSum: "
"Data type: Integer\nValues: 2000\nHas null: yes\nMinimum: 2001\nMaximum: 4000\nSum: "
"6001000\n",
intColStats->toString());
intColStats = reinterpret_cast<const orc::IntegerColumnStatistics*>(
stripeStats->getRowIndexStatistics(1, 2));
EXPECT_EQ(
"Data type: Integer\nValues: 2000\nHas null: no\nMinimum: 4001\nMaximum: 6000\nSum: "
"Data type: Integer\nValues: 2000\nHas null: yes\nMinimum: 4001\nMaximum: 6000\nSum: "
"10001000\n",
intColStats->toString());

const orc::StringColumnStatistics* stringColStats;
stringColStats = reinterpret_cast<const orc::StringColumnStatistics*>(
stripeStats->getRowIndexStatistics(2, 0));
EXPECT_EQ(
"Data type: String\nValues: 2000\nHas null: no\nMinimum: 1000\nMaximum: 9a\nTotal length: "
"Data type: String\nValues: 2000\nHas null: yes\nMinimum: 1000\nMaximum: 9a\nTotal length: "
"7892\n",
stringColStats->toString());
stringColStats = reinterpret_cast<const orc::StringColumnStatistics*>(
stripeStats->getRowIndexStatistics(2, 1));
EXPECT_EQ(
"Data type: String\nValues: 2000\nHas null: no\nMinimum: 2001\nMaximum: 4000\nTotal "
"Data type: String\nValues: 2000\nHas null: yes\nMinimum: 2001\nMaximum: 4000\nTotal "
"length: "
"8000\n",
stringColStats->toString());
stringColStats = reinterpret_cast<const orc::StringColumnStatistics*>(
stripeStats->getRowIndexStatistics(2, 2));
EXPECT_EQ(
"Data type: String\nValues: 2000\nHas null: no\nMinimum: 4001\nMaximum: 6000\nTotal "
"Data type: String\nValues: 2000\nHas null: yes\nMinimum: 4001\nMaximum: 6000\nTotal "
"length: "
"8000\n",
stringColStats->toString());
Expand Down
Loading
Loading