|
3 | 3 | #include "binder/expression/literal_expression.h" |
4 | 4 | #include "binder/expression/path_expression.h" |
5 | 5 | #include "binder/expression/property_expression.h" |
| 6 | +#include "binder/expression/scalar_function_expression.h" |
6 | 7 | #include "binder/expression_visitor.h" |
7 | 8 | #include "catalog/catalog.h" |
8 | 9 | #include "catalog/catalog_entry/node_table_catalog_entry.h" |
|
11 | 12 | #include "common/exception/binder.h" |
12 | 13 | #include "common/string_format.h" |
13 | 14 | #include "common/utils.h" |
| 15 | +#include "function/built_in_function_utils.h" |
14 | 16 | #include "function/cast/functions/cast_from_string_functions.h" |
15 | 17 | #include "function/rewrite_function.h" |
| 18 | +#include "function/scalar_function.h" |
16 | 19 | #include "function/schema/vector_node_rel_functions.h" |
17 | 20 | #include "main/client_context.h" |
18 | 21 |
|
@@ -56,6 +59,106 @@ QueryGraph Binder::bindPatternElement(const PatternElement& patternElement) { |
56 | 59 | nodeAndRels.push_back(rightNode); |
57 | 60 | leftNode = rightNode; |
58 | 61 | } |
| 62 | + if (clientContext->getClientConfig()->recursivePatternSemantic != common::PathSemantic::WALK) { |
| 63 | + if (queryGraph.hasRecursiveRel()) { |
| 64 | + // The only one recursive rel doesn't need to be handled because RecursiveJoin |
| 65 | + // already implements path semantics. Like (a)-[b*2]-(c) |
| 66 | + // What needs to be handled is the recursive |
| 67 | + // rels with other nodes/rels. Like (a)-[b*2]-(c)-[d*2]-(e) or (a)-[b*2]-(c)-[d]-(e) |
| 68 | + auto rels = queryGraph.getQueryRels(); |
| 69 | + if (rels.size() > 1) { |
| 70 | + std::vector<LogicalType> childrenTypes; |
| 71 | + binder::expression_vector childrenExpressions; |
| 72 | + std::string funcName; |
| 73 | + if (clientContext->getClientConfig()->recursivePatternSemantic == |
| 74 | + common::PathSemantic::ACYCLIC) { |
| 75 | + auto nodes = queryGraph.getQueryNodes(); |
| 76 | + for (uint32_t j = 0; j < nodes.size(); ++j) { |
| 77 | + childrenTypes.push_back(nodes[j]->getInternalID()->dataType.copy()); |
| 78 | + childrenExpressions.push_back(nodes[j]->getInternalID()); |
| 79 | + } |
| 80 | + for (uint32_t j = 0; j < rels.size(); ++j) { |
| 81 | + if (rels[j]->isRecursive()) { |
| 82 | + childrenTypes.push_back(rels[j]->dataType.copy()); |
| 83 | + childrenExpressions.push_back(rels[j]); |
| 84 | + } |
| 85 | + } |
| 86 | + funcName = function::IsNodeDistinctFunction::name; |
| 87 | + } else { |
| 88 | + for (uint32_t j = 0; j < rels.size(); ++j) { |
| 89 | + if (rels[j]->isRecursive()) { |
| 90 | + childrenTypes.push_back(rels[j]->dataType.copy()); |
| 91 | + childrenExpressions.push_back(rels[j]); |
| 92 | + } else { |
| 93 | + childrenTypes.push_back( |
| 94 | + rels[j]->getInternalIDProperty()->dataType.copy()); |
| 95 | + childrenExpressions.push_back(rels[j]->getInternalIDProperty()); |
| 96 | + } |
| 97 | + } |
| 98 | + funcName = function::IsRelDistinctFunction::name; |
| 99 | + } |
| 100 | + auto catalog = clientContext->getCatalog(); |
| 101 | + auto transaction = clientContext->getTransaction(); |
| 102 | + auto functionEntry = catalog->getFunctionEntry(transaction, funcName); |
| 103 | + auto function = function::BuiltInFunctionsUtils::matchFunction(funcName, |
| 104 | + childrenTypes, functionEntry->ptrCast<FunctionCatalogEntry>()) |
| 105 | + ->ptrCast<function::ScalarFunction>() |
| 106 | + ->copy(); |
| 107 | + std::unique_ptr<function::FunctionBindData> bindData = |
| 108 | + std::make_unique<function::FunctionBindData>( |
| 109 | + LogicalType(function->returnTypeID)); |
| 110 | + auto uniqueExpressionName = binder::ScalarFunctionExpression::getUniqueName( |
| 111 | + function->name, childrenExpressions); |
| 112 | + auto functionExpression = std::make_shared<binder::ScalarFunctionExpression>( |
| 113 | + ExpressionType::FUNCTION, std::move(function), std::move(bindData), |
| 114 | + childrenExpressions, uniqueExpressionName); |
| 115 | + queryGraph.addSemanticExpression(functionExpression); |
| 116 | + } |
| 117 | + } else { |
| 118 | + // not have recursive rel. Like: (a)-[b]-(c)-[d]-(e) |
| 119 | + std::vector<LogicalType> childrenTypes; |
| 120 | + binder::expression_vector childrenExpressions; |
| 121 | + if (clientContext->getClientConfig()->recursivePatternSemantic == |
| 122 | + common::PathSemantic::ACYCLIC) { |
| 123 | + auto nodes = queryGraph.getQueryNodes(); |
| 124 | + for (uint32_t j = 0; j < nodes.size(); ++j) { |
| 125 | + childrenTypes.push_back(nodes[j]->getInternalID()->dataType.copy()); |
| 126 | + childrenExpressions.push_back(nodes[j]->getInternalID()); |
| 127 | + } |
| 128 | + } else { |
| 129 | + auto rels = queryGraph.getQueryRels(); |
| 130 | + for (uint32_t j = 0; j < rels.size(); ++j) { |
| 131 | + childrenTypes.push_back(rels[j]->getInternalIDProperty()->dataType.copy()); |
| 132 | + childrenExpressions.push_back(rels[j]->getInternalIDProperty()); |
| 133 | + } |
| 134 | + } |
| 135 | + if (childrenExpressions.size() > 2) { |
| 136 | + auto catalog = clientContext->getCatalog(); |
| 137 | + auto transaction = clientContext->getTransaction(); |
| 138 | + auto functionEntry = |
| 139 | + catalog->getFunctionEntry(transaction, function::IsIDDistinctFunction::name); |
| 140 | + auto function = function::BuiltInFunctionsUtils::matchFunction( |
| 141 | + function::IsIDDistinctFunction::name, childrenTypes, |
| 142 | + functionEntry->ptrCast<FunctionCatalogEntry>()) |
| 143 | + ->ptrCast<function::ScalarFunction>() |
| 144 | + ->copy(); |
| 145 | + std::unique_ptr<function::FunctionBindData> bindData = |
| 146 | + std::make_unique<function::FunctionBindData>( |
| 147 | + LogicalType(function->returnTypeID)); |
| 148 | + auto uniqueExpressionName = binder::ScalarFunctionExpression::getUniqueName( |
| 149 | + function->name, childrenExpressions); |
| 150 | + auto functionExpression = std::make_shared<binder::ScalarFunctionExpression>( |
| 151 | + ExpressionType::FUNCTION, std::move(function), std::move(bindData), |
| 152 | + childrenExpressions, uniqueExpressionName); |
| 153 | + queryGraph.addSemanticExpression(functionExpression); |
| 154 | + } else if (childrenExpressions.size() == 2) { |
| 155 | + // when only two children use no_equal |
| 156 | + auto noEquals = expressionBinder.bindComparisonExpression( |
| 157 | + kuzu::common::ExpressionType::NOT_EQUALS, childrenExpressions); |
| 158 | + queryGraph.addSemanticExpression(noEquals); |
| 159 | + } |
| 160 | + } |
| 161 | + } |
59 | 162 | if (patternElement.hasPathName()) { |
60 | 163 | auto pathName = patternElement.getPathName(); |
61 | 164 | auto pathExpression = createPath(pathName, nodeAndRels); |
|
0 commit comments