Skip to content

Commit

Permalink
Merge pull request #106 from nickgerrets/tpcds
Browse files Browse the repository at this point in the history
TPC-DS queries and BETWEEN comparison
  • Loading branch information
pdet authored Sep 18, 2024
2 parents d8ca65b + 5ef934a commit 78d265c
Show file tree
Hide file tree
Showing 7 changed files with 579 additions and 9 deletions.
2 changes: 1 addition & 1 deletion duckdb-r
Submodule duckdb-r updated 640 files
1 change: 0 additions & 1 deletion src/from_substrait.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,6 @@ unique_ptr<ParsedExpression> SubstraitToDuckDB::TransformScalarFunctionExpr(cons
return make_uniq<ComparisonExpression>(ExpressionType::COMPARE_NOT_DISTINCT_FROM, std::move(children[0]),
std::move(children[1]));
} else if (function_name == "between") {
// FIXME: ADD between to substrait extension
D_ASSERT(children.size() == 3);
return make_uniq<BetweenExpression>(std::move(children[0]), std::move(children[1]), std::move(children[2]));
} else if (function_name == "extract") {
Expand Down
1 change: 1 addition & 0 deletions src/include/to_substrait.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ class DuckDBToSubstrait {
void TransformFunctionExpression(Expression &dexpr, substrait::Expression &sexpr, uint64_t col_offset);
static void TransformConstantExpression(Expression &dexpr, substrait::Expression &sexpr);
void TransformComparisonExpression(Expression &dexpr, substrait::Expression &sexpr);
void TransformBetweenExpression(Expression &dexpr, substrait::Expression &sexpr);
void TransformConjunctionExpression(Expression &dexpr, substrait::Expression &sexpr, uint64_t col_offset);
void TransformNotNullExpression(Expression &dexpr, substrait::Expression &sexpr, uint64_t col_offset);
void TransformIsNullExpression(Expression &dexpr, substrait::Expression &sexpr, uint64_t col_offset);
Expand Down
38 changes: 32 additions & 6 deletions src/to_substrait.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,29 @@ void DuckDBToSubstrait::TransformComparisonExpression(Expression &dexpr, substra
*scalar_fun->mutable_output_type() = DuckToSubstraitType(dcomp.return_type);
}

void DuckDBToSubstrait::TransformBetweenExpression(Expression &dexpr, substrait::Expression &sexpr) {
auto &dcomp = dexpr.Cast<BoundBetweenExpression>();

if (dexpr.type != ExpressionType::COMPARE_BETWEEN) {
throw InternalException("Not a between comparison expression");
}

auto scalar_fun = sexpr.mutable_scalar_function();
vector<::substrait::Type> args_types;
args_types.emplace_back(DuckToSubstraitType(dcomp.input->return_type));
args_types.emplace_back(DuckToSubstraitType(dcomp.lower->return_type));
args_types.emplace_back(DuckToSubstraitType(dcomp.upper->return_type));
scalar_fun->set_function_reference(RegisterFunction("between", args_types));

auto sarg = scalar_fun->add_arguments();
TransformExpr(*dcomp.input, *sarg->mutable_value(), 0);
sarg = scalar_fun->add_arguments();
TransformExpr(*dcomp.lower, *sarg->mutable_value(), 0);
sarg = scalar_fun->add_arguments();
TransformExpr(*dcomp.upper, *sarg->mutable_value(), 0);
*scalar_fun->mutable_output_type() = DuckToSubstraitType(dcomp.return_type);
}

void DuckDBToSubstrait::TransformConjunctionExpression(Expression &dexpr, substrait::Expression &sexpr,
uint64_t col_offset) {
auto &dconj = dexpr.Cast<BoundConjunctionExpression>();
Expand Down Expand Up @@ -537,6 +560,9 @@ void DuckDBToSubstrait::TransformExpr(Expression &dexpr, substrait::Expression &
case ExpressionType::COMPARE_NOT_DISTINCT_FROM:
TransformComparisonExpression(dexpr, sexpr);
break;
case ExpressionType::COMPARE_BETWEEN:
TransformBetweenExpression(dexpr, sexpr);
break;
case ExpressionType::CONJUNCTION_AND:
case ExpressionType::CONJUNCTION_OR:
TransformConjunctionExpression(dexpr, sexpr, col_offset);
Expand All @@ -557,7 +583,7 @@ void DuckDBToSubstrait::TransformExpr(Expression &dexpr, substrait::Expression &
TransformNotExpression(dexpr, sexpr, col_offset);
break;
default:
throw InternalException(ExpressionTypeToString(dexpr.type));
throw NotImplementedException(ExpressionTypeToString(dexpr.type));
}
}

Expand Down Expand Up @@ -742,7 +768,7 @@ substrait::Expression *DuckDBToSubstrait::TransformJoinCond(const JoinCondition
join_comparision = "lt";
break;
default:
throw InternalException("Unsupported join comparison: " + ExpressionTypeToOperator(dcond.comparison));
throw NotImplementedException("Unsupported join comparison: " + ExpressionTypeToOperator(dcond.comparison));
}
vector<::substrait::Type> args_types;
auto scalar_fun = expr->mutable_scalar_function();
Expand Down Expand Up @@ -946,7 +972,7 @@ substrait::Rel *DuckDBToSubstrait::TransformComparisonJoin(LogicalOperator &dop)
sjoin->set_type(substrait::JoinRel::JoinType::JoinRel_JoinType_JOIN_TYPE_OUTER);
break;
default:
throw InternalException("Unsupported join type " + JoinTypeToString(djoin.join_type));
throw NotImplementedException("Unsupported join type " + JoinTypeToString(djoin.join_type));
}
// somewhat odd semantics on our side
if (djoin.left_projection_map.empty()) {
Expand Down Expand Up @@ -984,15 +1010,15 @@ substrait::Rel *DuckDBToSubstrait::TransformAggregateGroup(LogicalOperator &dop)
for (auto &dgrp : daggr.groups) {
if (dgrp->type != ExpressionType::BOUND_REF) {
// TODO push projection or push substrait to allow expressions here
throw InternalException("No expressions in groupings yet");
throw NotImplementedException("No expressions in groupings yet");
}
TransformExpr(*dgrp, *sgrp->add_grouping_expressions());
}
for (auto &dmeas : daggr.expressions) {
auto smeas = saggr->add_measures()->mutable_measure();
if (dmeas->type != ExpressionType::BOUND_AGGREGATE) {
// TODO push projection or push substrait, too
throw InternalException("No non-aggregate expressions in measures yet");
throw NotImplementedException("No non-aggregate expressions in measures yet");
}
auto &daexpr = dmeas->Cast<BoundAggregateExpression>();

Expand Down Expand Up @@ -1422,7 +1448,7 @@ substrait::Rel *DuckDBToSubstrait::TransformOp(LogicalOperator &dop) {
case LogicalOperatorType::LOGICAL_DUMMY_SCAN:
return TransformDummyScan();
default:
throw InternalException(LogicalOperatorToString(dop.type));
throw NotImplementedException(LogicalOperatorToString(dop.type));
}
}

Expand Down
14 changes: 14 additions & 0 deletions test/sql/test_between.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# name: test/sql/test_between.test
# description: Test BETWEEN comparison
# group: [sql]

require substrait

statement ok
PRAGMA enable_verification

statement ok
create table t as select * from range(100) as t(x)

statement ok
CALL get_substrait('select * from t where x BETWEEN 4 AND 6;', enable_optimizer = false );
Loading

0 comments on commit 78d265c

Please sign in to comment.