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
32 changes: 29 additions & 3 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 @@ -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 @@ -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 );
496 changes: 496 additions & 0 deletions test/sql/test_substrait_tpcds.test

Large diffs are not rendered by default.

Loading