Skip to content
This repository was archived by the owner on Oct 10, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions scripts/antlr4/Cypher.g4
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,7 @@ kU_RecursiveType

oC_RangeLiteral
: oC_LowerBound? SP? DOTDOT SP? oC_UpperBound?
| oC_IntegerLiteral ;
| oC_Expression ;

kU_RecursiveComprehension
: '(' SP? oC_Variable SP? ',' SP? oC_Variable ( SP? '|' SP? oC_Where SP? )? ( SP? '|' SP? kU_RecursiveProjectionItems SP? ',' SP? kU_RecursiveProjectionItems SP? )? ')' ;
Expand All @@ -693,10 +693,10 @@ kU_RecursiveProjectionItems
: '{' SP? oC_ProjectionItems? SP? '}' ;

oC_LowerBound
: DecimalInteger ;
: oC_Expression ;

oC_UpperBound
: DecimalInteger ;
: oC_Expression ;

oC_LabelName
: oC_SchemaName ;
Expand Down
2 changes: 1 addition & 1 deletion scripts/antlr4/hash.md5
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8334a684be17e562250acf07ae2bbca0
c39017591a40317e1e5aabb586c04e45
6 changes: 3 additions & 3 deletions src/antlr4/Cypher.g4
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ kU_RecursiveType

oC_RangeLiteral
: oC_LowerBound? SP? DOTDOT SP? oC_UpperBound?
| oC_IntegerLiteral ;
| oC_Expression ;

kU_RecursiveComprehension
: '(' SP? oC_Variable SP? ',' SP? oC_Variable ( SP? '|' SP? oC_Where SP? )? ( SP? '|' SP? kU_RecursiveProjectionItems SP? ',' SP? kU_RecursiveProjectionItems SP? )? ')' ;
Expand All @@ -446,10 +446,10 @@ kU_RecursiveProjectionItems
: '{' SP? oC_ProjectionItems? SP? '}' ;

oC_LowerBound
: DecimalInteger ;
: oC_Expression ;

oC_UpperBound
: DecimalInteger ;
: oC_Expression ;

oC_LabelName
: oC_SchemaName ;
Expand Down
21 changes: 13 additions & 8 deletions src/binder/bind/bind_graph_pattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include "common/exception/binder.h"
#include "common/string_format.h"
#include "common/utils.h"
#include "function/cast/functions/cast_from_string_functions.h"
#include "function/gds/rec_joins.h"
#include "function/rewrite_function.h"
#include "function/schema/vector_node_rel_functions.h"
Expand Down Expand Up @@ -526,15 +525,21 @@ expression_vector Binder::bindRecursivePatternRelProjectionList(const RecursiveR
std::pair<uint64_t, uint64_t> Binder::bindVariableLengthRelBound(const RelPattern& relPattern) {
auto recursiveInfo = relPattern.getRecursiveInfo();
uint32_t lowerBound = 0;
function::CastString::operation(
ku_string_t{recursiveInfo->lowerBound.c_str(), recursiveInfo->lowerBound.length()},
lowerBound);
auto boundLowerExpression = expressionBinder.bindExpression(*recursiveInfo->lowerBound);
if (boundLowerExpression->expressionType != ExpressionType::LITERAL &&
boundLowerExpression->expressionType != ExpressionType::PARAMETER) {
throw BinderException("Rel range lower bound must be a parameter/literal expression.");
}
lowerBound = ExpressionUtil::evaluateAsVariableLengthRelBound(*boundLowerExpression);
auto maxDepth = clientContext->getClientConfig()->varLengthMaxDepth;
auto upperBound = maxDepth;
if (!recursiveInfo->upperBound.empty()) {
function::CastString::operation(
ku_string_t{recursiveInfo->upperBound.c_str(), recursiveInfo->upperBound.length()},
upperBound);
if (recursiveInfo->upperBound.get()) {
auto boundUpperExpression = expressionBinder.bindExpression(*recursiveInfo->upperBound);
if (boundUpperExpression->expressionType != ExpressionType::LITERAL &&
boundUpperExpression->expressionType != ExpressionType::PARAMETER) {
throw BinderException("Rel range upper bound must be a parameter/literal expression.");
}
upperBound = ExpressionUtil::evaluateAsVariableLengthRelBound(*boundUpperExpression);
}
if (lowerBound > upperBound) {
throw BinderException(stringFormat("Lower bound of rel {} is greater than upperBound.",
Expand Down
16 changes: 16 additions & 0 deletions src/binder/expression/expression_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,22 @@ uint64_t ExpressionUtil::evaluateAsSkipLimit(const Expression& expr) {
return number;
}

uint64_t ExpressionUtil::evaluateAsVariableLengthRelBound(const Expression& expr) {
auto value = evaluateAsLiteralValue(expr);
auto errorMsg = "Rel range upper/lower bound must be a non-negative integer.";
uint64_t number = INVALID_RELBOUND;
TypeUtils::visit(
value.getDataType(),
[&]<IntegerTypes T>(T) {
if (value.getValue<T>() < 0) {
throw BinderException{errorMsg};
}
number = (uint64_t)value.getValue<T>();
},
[&](auto) { throw BinderException{errorMsg}; });
return number;
}

template<typename T>
T ExpressionUtil::getExpressionVal(const Expression& expr, const Value& value,
const LogicalType& targetType, validate_param_func<T> validateParamFunc) {
Expand Down
1 change: 1 addition & 0 deletions src/include/binder/expression/expression_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ struct KUZU_API ExpressionUtil {
static bool canEvaluateAsLiteral(const Expression& expr);
static common::Value evaluateAsLiteralValue(const Expression& expr);
static uint64_t evaluateAsSkipLimit(const Expression& expr);
static uint64_t evaluateAsVariableLengthRelBound(const Expression& expr);

template<typename T>
using validate_param_func = void (*)(T);
Expand Down
1 change: 1 addition & 0 deletions src/include/common/types/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ using relID_t = internalID_t;

using cardinality_t = uint64_t;
constexpr offset_t INVALID_LIMIT = UINT64_MAX;
constexpr offset_t INVALID_RELBOUND = UINT32_MAX;
using offset_vec_t = std::vector<offset_t>;
// System representation for internalID.
struct KUZU_API internalID_t {
Expand Down
4 changes: 2 additions & 2 deletions src/include/parser/query/graph_pattern/rel_pattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ namespace parser {
enum class ArrowDirection : uint8_t { LEFT = 0, RIGHT = 1, BOTH = 2 };

struct RecursiveRelPatternInfo {
std::string lowerBound;
std::string upperBound;
std::unique_ptr<ParsedExpression> lowerBound = nullptr;
std::unique_ptr<ParsedExpression> upperBound = nullptr;
std::string weightPropertyName;
std::string relName;
std::string nodeName;
Expand Down
20 changes: 11 additions & 9 deletions src/parser/transform/transform_graph_pattern.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "common/assert.h"
#include "parser/expression/parsed_literal_expression.h"
#include "parser/query/graph_pattern/pattern_element.h"
#include "parser/transformer.h"

Expand Down Expand Up @@ -126,23 +127,24 @@ RelPattern Transformer::transformRelationshipPattern(
relType = QueryRelType::VARIABLE_LENGTH_WALK;
}
// Parse lower, upper bound
auto lowerBound = std::string("1");
auto upperBound = std::string("");
std::unique_ptr<ParsedExpression> lowerBound =
std::make_unique<ParsedLiteralExpression>(Value(1), "1");
std::unique_ptr<ParsedExpression> upperBound = nullptr;
auto range = recursiveDetail->oC_RangeLiteral();
if (range) {
if (range->oC_IntegerLiteral()) {
lowerBound = range->oC_IntegerLiteral()->getText();
upperBound = lowerBound;
if (range->oC_Expression()) {
lowerBound = transformExpression(*range->oC_Expression());
upperBound = lowerBound->copy();
}
if (range->oC_LowerBound()) {
lowerBound = range->oC_LowerBound()->getText();
lowerBound = transformExpression(*range->oC_LowerBound()->oC_Expression());
}
if (range->oC_UpperBound()) {
upperBound = range->oC_UpperBound()->getText();
upperBound = transformExpression(*range->oC_UpperBound()->oC_Expression());
}
}
recursiveInfo.lowerBound = lowerBound;
recursiveInfo.upperBound = upperBound;
recursiveInfo.lowerBound = std::move(lowerBound);
recursiveInfo.upperBound = std::move(upperBound);
// Parse recursive comprehension
auto comprehension = recursiveDetail->kU_RecursiveComprehension();
if (comprehension) {
Expand Down
5 changes: 2 additions & 3 deletions test/test_files/tck/match/match4.test
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,5 @@ Parser exception: Invalid input <MATCH (a:A) MATCH (a)-[:LIKES..>: ex
MATCH (a)-[:LIKES*-2]->(c)
RETURN c.name;
---- error
Parser exception: Invalid input <MATCH (a:A) MATCH (a)-[:LIKES*->: expected rule oC_SingleQuery (line: 1, offset: 40)
"MATCH (a:A) MATCH (a)-[:LIKES*-2]->(c) RETURN c.name;"
^
Binder exception: Rel range upper/lower bound must be a non-negative integer.

Loading
Loading