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

TPC-DS queries and BETWEEN comparison #106

Merged
merged 14 commits into from
Sep 18, 2024
2 changes: 1 addition & 1 deletion duckdb
Submodule duckdb updated 127 files
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 @@ -414,6 +414,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 @@ -538,6 +561,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 @@ -558,7 +584,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 @@ -743,7 +769,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 @@ -947,7 +973,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 @@ -985,15 +1011,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 @@ -1423,7 +1449,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 );
530 changes: 530 additions & 0 deletions test/sql/test_substrait_tpcds.test

Large diffs are not rendered by default.

Loading