Skip to content

Commit 0a21b92

Browse files
committed
Merge branch 'view' into structs
2 parents d488993 + bac2ff3 commit 0a21b92

File tree

11 files changed

+610
-21
lines changed

11 files changed

+610
-21
lines changed

.github/workflows/main_distribution.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ jobs:
2222
uses: duckdb/extension-ci-tools/.github/workflows/_extension_distribution.yml@main
2323
with:
2424
duckdb_version: main
25+
ci_tools_version: main
2526
exclude_archs: "wasm_mvp;wasm_eh;wasm_threads;windows_amd64;windows_amd64_rtools"
2627
extension_name: substrait
2728

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ all: release
66
EXT_NAME=substrait
77
EXT_CONFIG=${PROJ_DIR}extension_config.cmake
88

9-
CORE_EXTENSIONS='tpch;json'
9+
CORE_EXTENSIONS='tpch;tpcds;json'
1010

1111
# Set this flag during building to enable the benchmark runner
1212
ifeq (${BUILD_BENCHMARK}, 1)

duckdb

Submodule duckdb updated 1863 files

duckdb-r

Submodule duckdb-r updated 640 files

src/from_substrait.cpp

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,6 @@ unique_ptr<ParsedExpression> SubstraitToDuckDB::TransformScalarFunctionExpr(cons
264264
return make_uniq<ComparisonExpression>(ExpressionType::COMPARE_NOT_DISTINCT_FROM, std::move(children[0]),
265265
std::move(children[1]));
266266
} else if (function_name == "between") {
267-
// FIXME: ADD between to substrait extension
268267
D_ASSERT(children.size() == 3);
269268
return make_uniq<BetweenExpression>(std::move(children[0]), std::move(children[1]), std::move(children[2]));
270269
} else if (function_name == "extract") {
@@ -541,17 +540,14 @@ shared_ptr<Relation> SubstraitToDuckDB::TransformAggregateOp(const substrait::Re
541540
std::move(groups));
542541
}
543542
unique_ptr<TableDescription> TableInfo(ClientContext &context, const string &schema_name, const string &table_name) {
544-
unique_ptr<TableDescription> result;
545543
// obtain the table info
546544
auto table = Catalog::GetEntry<TableCatalogEntry>(context, INVALID_CATALOG, schema_name, table_name,
547545
OnEntryNotFound::RETURN_NULL);
548546
if (!table) {
549547
return {};
550548
}
551549
// write the table info to the result
552-
result = make_uniq<TableDescription>();
553-
result->schema = schema_name;
554-
result->table = table_name;
550+
auto result = make_uniq<TableDescription>(INVALID_CATALOG, schema_name, table_name);
555551
for (auto &column : table->GetColumns().Logical()) {
556552
result->columns.emplace_back(column.Copy());
557553
}
@@ -561,6 +557,7 @@ unique_ptr<TableDescription> TableInfo(ClientContext &context, const string &sch
561557
shared_ptr<Relation> SubstraitToDuckDB::TransformReadOp(const substrait::Rel &sop) {
562558
auto &sget = sop.read();
563559
shared_ptr<Relation> scan;
560+
auto context_wrapper = make_shared_ptr<RelationContextWrapper>(context);
564561
if (sget.has_named_table()) {
565562
auto table_name = sget.named_table().names(0);
566563
// If we can't find a table with that name, let's try a view.
@@ -569,9 +566,19 @@ shared_ptr<Relation> SubstraitToDuckDB::TransformReadOp(const substrait::Rel &so
569566
if (!table_info) {
570567
throw CatalogException("Table '%s' does not exist!", table_name);
571568
}
572-
scan = make_shared_ptr<TableRelation>(context, std::move(table_info), acquire_lock);
569+
if (acquire_lock) {
570+
scan = make_shared_ptr<TableRelation>(context, std::move(table_info));
571+
572+
} else {
573+
scan = make_shared_ptr<TableRelation>(context_wrapper, std::move(table_info));
574+
}
573575
} catch (...) {
574-
scan = make_shared_ptr<ViewRelation>(context, DEFAULT_SCHEMA, table_name, acquire_lock);
576+
if (acquire_lock) {
577+
scan = make_shared_ptr<ViewRelation>(context, DEFAULT_SCHEMA, table_name);
578+
579+
} else {
580+
scan = make_shared_ptr<ViewRelation>(context_wrapper, DEFAULT_SCHEMA, table_name);
581+
}
575582
}
576583
} else if (sget.has_local_files()) {
577584
vector<Value> parquet_files;
@@ -593,8 +600,15 @@ shared_ptr<Relation> SubstraitToDuckDB::TransformReadOp(const substrait::Rel &so
593600
string name = "parquet_" + StringUtil::GenerateRandomName();
594601
named_parameter_map_t named_parameters({{"binary_as_string", Value::BOOLEAN(false)}});
595602
vector<Value> parameters {Value::LIST(parquet_files)};
596-
auto scan_rel = make_shared_ptr<TableFunctionRelation>(
597-
context, "parquet_scan", parameters, std::move(named_parameters), nullptr, true, acquire_lock);
603+
shared_ptr<TableFunctionRelation> scan_rel;
604+
if (acquire_lock) {
605+
scan_rel = make_shared_ptr<TableFunctionRelation>(context, "parquet_scan", parameters,
606+
std::move(named_parameters));
607+
} else {
608+
scan_rel = make_shared_ptr<TableFunctionRelation>(context_wrapper, "parquet_scan", parameters,
609+
std::move(named_parameters));
610+
}
611+
598612
auto rel = static_cast<Relation *>(scan_rel.get());
599613
scan = rel->Alias(name);
600614
} else if (sget.has_virtual_table()) {
@@ -610,7 +624,12 @@ shared_ptr<Relation> SubstraitToDuckDB::TransformReadOp(const substrait::Rel &so
610624
expression_rows.emplace_back(expression_row);
611625
}
612626
vector<string> column_names;
613-
scan = make_shared_ptr<ValueRelation>(context, expression_rows, column_names, "values", acquire_lock);
627+
if (acquire_lock) {
628+
scan = make_shared_ptr<ValueRelation>(context, expression_rows, column_names);
629+
630+
} else {
631+
scan = make_shared_ptr<ValueRelation>(context_wrapper, expression_rows, column_names);
632+
}
614633
} else {
615634
throw NotImplementedException("Unsupported type of read operator for substrait");
616635
}

src/include/to_substrait.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ class DuckDBToSubstrait {
9696
void TransformFunctionExpression(Expression &dexpr, substrait::Expression &sexpr, uint64_t col_offset);
9797
static void TransformConstantExpression(Expression &dexpr, substrait::Expression &sexpr);
9898
void TransformComparisonExpression(Expression &dexpr, substrait::Expression &sexpr);
99+
void TransformBetweenExpression(Expression &dexpr, substrait::Expression &sexpr);
99100
void TransformConjunctionExpression(Expression &dexpr, substrait::Expression &sexpr, uint64_t col_offset);
100101
void TransformNotNullExpression(Expression &dexpr, substrait::Expression &sexpr, uint64_t col_offset);
101102
void TransformIsNullExpression(Expression &dexpr, substrait::Expression &sexpr, uint64_t col_offset);

src/to_substrait.cpp

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,29 @@ void DuckDBToSubstrait::TransformComparisonExpression(Expression &dexpr, substra
413413
*scalar_fun->mutable_output_type() = DuckToSubstraitType(dcomp.return_type);
414414
}
415415

416+
void DuckDBToSubstrait::TransformBetweenExpression(Expression &dexpr, substrait::Expression &sexpr) {
417+
auto &dcomp = dexpr.Cast<BoundBetweenExpression>();
418+
419+
if (dexpr.type != ExpressionType::COMPARE_BETWEEN) {
420+
throw InternalException("Not a between comparison expression");
421+
}
422+
423+
auto scalar_fun = sexpr.mutable_scalar_function();
424+
vector<::substrait::Type> args_types;
425+
args_types.emplace_back(DuckToSubstraitType(dcomp.input->return_type));
426+
args_types.emplace_back(DuckToSubstraitType(dcomp.lower->return_type));
427+
args_types.emplace_back(DuckToSubstraitType(dcomp.upper->return_type));
428+
scalar_fun->set_function_reference(RegisterFunction("between", args_types));
429+
430+
auto sarg = scalar_fun->add_arguments();
431+
TransformExpr(*dcomp.input, *sarg->mutable_value(), 0);
432+
sarg = scalar_fun->add_arguments();
433+
TransformExpr(*dcomp.lower, *sarg->mutable_value(), 0);
434+
sarg = scalar_fun->add_arguments();
435+
TransformExpr(*dcomp.upper, *sarg->mutable_value(), 0);
436+
*scalar_fun->mutable_output_type() = DuckToSubstraitType(dcomp.return_type);
437+
}
438+
416439
void DuckDBToSubstrait::TransformConjunctionExpression(Expression &dexpr, substrait::Expression &sexpr,
417440
uint64_t col_offset) {
418441
auto &dconj = dexpr.Cast<BoundConjunctionExpression>();
@@ -537,6 +560,9 @@ void DuckDBToSubstrait::TransformExpr(Expression &dexpr, substrait::Expression &
537560
case ExpressionType::COMPARE_NOT_DISTINCT_FROM:
538561
TransformComparisonExpression(dexpr, sexpr);
539562
break;
563+
case ExpressionType::COMPARE_BETWEEN:
564+
TransformBetweenExpression(dexpr, sexpr);
565+
break;
540566
case ExpressionType::CONJUNCTION_AND:
541567
case ExpressionType::CONJUNCTION_OR:
542568
TransformConjunctionExpression(dexpr, sexpr, col_offset);
@@ -557,7 +583,7 @@ void DuckDBToSubstrait::TransformExpr(Expression &dexpr, substrait::Expression &
557583
TransformNotExpression(dexpr, sexpr, col_offset);
558584
break;
559585
default:
560-
throw InternalException(ExpressionTypeToString(dexpr.type));
586+
throw NotImplementedException(ExpressionTypeToString(dexpr.type));
561587
}
562588
}
563589

@@ -742,7 +768,7 @@ substrait::Expression *DuckDBToSubstrait::TransformJoinCond(const JoinCondition
742768
join_comparision = "lt";
743769
break;
744770
default:
745-
throw InternalException("Unsupported join comparison: " + ExpressionTypeToOperator(dcond.comparison));
771+
throw NotImplementedException("Unsupported join comparison: " + ExpressionTypeToOperator(dcond.comparison));
746772
}
747773
vector<::substrait::Type> args_types;
748774
auto scalar_fun = expr->mutable_scalar_function();
@@ -946,7 +972,7 @@ substrait::Rel *DuckDBToSubstrait::TransformComparisonJoin(LogicalOperator &dop)
946972
sjoin->set_type(substrait::JoinRel::JoinType::JoinRel_JoinType_JOIN_TYPE_OUTER);
947973
break;
948974
default:
949-
throw InternalException("Unsupported join type " + JoinTypeToString(djoin.join_type));
975+
throw NotImplementedException("Unsupported join type " + JoinTypeToString(djoin.join_type));
950976
}
951977
// somewhat odd semantics on our side
952978
if (djoin.left_projection_map.empty()) {
@@ -984,15 +1010,15 @@ substrait::Rel *DuckDBToSubstrait::TransformAggregateGroup(LogicalOperator &dop)
9841010
for (auto &dgrp : daggr.groups) {
9851011
if (dgrp->type != ExpressionType::BOUND_REF) {
9861012
// TODO push projection or push substrait to allow expressions here
987-
throw InternalException("No expressions in groupings yet");
1013+
throw NotImplementedException("No expressions in groupings yet");
9881014
}
9891015
TransformExpr(*dgrp, *sgrp->add_grouping_expressions());
9901016
}
9911017
for (auto &dmeas : daggr.expressions) {
9921018
auto smeas = saggr->add_measures()->mutable_measure();
9931019
if (dmeas->type != ExpressionType::BOUND_AGGREGATE) {
9941020
// TODO push projection or push substrait, too
995-
throw InternalException("No non-aggregate expressions in measures yet");
1021+
throw NotImplementedException("No non-aggregate expressions in measures yet");
9961022
}
9971023
auto &daexpr = dmeas->Cast<BoundAggregateExpression>();
9981024

@@ -1422,7 +1448,7 @@ substrait::Rel *DuckDBToSubstrait::TransformOp(LogicalOperator &dop) {
14221448
case LogicalOperatorType::LOGICAL_DUMMY_SCAN:
14231449
return TransformDummyScan();
14241450
default:
1425-
throw InternalException(LogicalOperatorToString(dop.type));
1451+
throw NotImplementedException(LogicalOperatorToString(dop.type));
14261452
}
14271453
}
14281454

test/sql/test_between.test

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# name: test/sql/test_between.test
2+
# description: Test BETWEEN comparison
3+
# group: [sql]
4+
5+
require substrait
6+
7+
statement ok
8+
PRAGMA enable_verification
9+
10+
statement ok
11+
create table t as select * from range(100) as t(x)
12+
13+
statement ok
14+
CALL get_substrait('select * from t where x BETWEEN 4 AND 6;', enable_optimizer = false );

0 commit comments

Comments
 (0)