From b83d53814cff479aa5c7540cd94fc873ef2d46d4 Mon Sep 17 00:00:00 2001 From: Rui Mo Date: Tue, 23 Aug 2022 15:39:26 +0800 Subject: [PATCH] Support selecting subfield from struct (#38) Fix bigint range (#40) --- velox/substrait/SubstraitToVeloxExpr.cpp | 33 +++++++++++++++++++++--- velox/type/Filter.cpp | 1 - velox/type/Filter.h | 4 +-- 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/velox/substrait/SubstraitToVeloxExpr.cpp b/velox/substrait/SubstraitToVeloxExpr.cpp index b0b4ca0ed380..fab8f19fd4f0 100644 --- a/velox/substrait/SubstraitToVeloxExpr.cpp +++ b/velox/substrait/SubstraitToVeloxExpr.cpp @@ -29,19 +29,43 @@ SubstraitVeloxExprConverter::toVeloxExpr( case ::substrait::Expression::FieldReference::ReferenceTypeCase:: kDirectReference: { const auto& dRef = substraitField.direct_reference(); + VELOX_CHECK(dRef.has_struct_field(), "Struct field expected."); int32_t colIdx = subParser_->parseReferenceSegment(dRef); + std::optional childIdx; + if (dRef.struct_field().has_child()) { + childIdx = + subParser_->parseReferenceSegment(dRef.struct_field().child()); + } + + const auto& inputTypes = inputType->children(); const auto& inputNames = inputType->names(); const int64_t inputSize = inputNames.size(); - if (colIdx <= inputSize) { - const auto& inputTypes = inputType->children(); - // Convert type to row. + + if (colIdx >= inputSize) { + VELOX_FAIL("Missing the column with id '{}' .", colIdx); + } + + if (!childIdx.has_value()) { return std::make_shared( inputTypes[colIdx], std::make_shared(inputTypes[colIdx]), inputNames[colIdx]); } else { - VELOX_FAIL("Missing the column with id '{}' .", colIdx); + // Select a subfield in a struct by name. + if (auto inputColumnType = asRowType(inputTypes[colIdx])) { + if (childIdx.value() >= inputColumnType->size()) { + VELOX_FAIL("Missing the subfield with id '{}' .", childIdx.value()); + } + return std::make_shared( + inputColumnType->childAt(childIdx.value()), + std::make_shared( + inputTypes[colIdx], inputNames[colIdx]), + inputColumnType->nameOf(childIdx.value())); + } else { + VELOX_FAIL("RowType expected."); + } } + break; } default: VELOX_NYI( @@ -120,6 +144,7 @@ SubstraitVeloxExprConverter::toVeloxExpr( if (veloxFunction == "alias") { return toAliasExpr(std::move(params)); } + if (veloxFunction == "is_not_null") { return toIsNotNullExpr(std::move(params), toVeloxType(typeName)); } diff --git a/velox/type/Filter.cpp b/velox/type/Filter.cpp index 8612b4616af2..a489b2b8559a 100644 --- a/velox/type/Filter.cpp +++ b/velox/type/Filter.cpp @@ -574,7 +574,6 @@ std::unique_ptr createBigintValuesFilter( min, max, values, nullAllowed); } - std::unique_ptr createBigintValues( const std::vector& values, bool nullAllowed) { diff --git a/velox/type/Filter.h b/velox/type/Filter.h index 308d6373a231..d927148eb79b 100644 --- a/velox/type/Filter.h +++ b/velox/type/Filter.h @@ -555,8 +555,8 @@ class BigintRange final : public Filter { bool upperExclusive, bool nullAllowed) : Filter(true, nullAllowed, FilterKind::kBigintRange), - lower_(lowerExclusive ? lower - 1 : lower), - upper_(upperExclusive ? upper + 1 : upper), + lower_(lowerExclusive ? lower + 1 : lower), + upper_(upperExclusive ? upper - 1 : upper), lower32_(std::max(lower, std::numeric_limits::min())), upper32_(std::min(upper, std::numeric_limits::max())), lower16_(std::max(lower, std::numeric_limits::min())),