#include "binder/expression/expression_util.h" #include "binder/expression/node_expression.h" #include "binder/expression/rel_expression.h" #include "binder/expression/scalar_function_expression.h" #include "binder/expression_binder.h" #include "catalog/catalog_entry/rel_group_catalog_entry.h" #include "catalog/catalog_entry/table_catalog_entry.h" #include "function/binary_function_executor.h" #include "function/list/functions/list_extract_function.h" #include "function/list/vector_list_functions.h" #include "function/rewrite_function.h" #include "function/scalar_function.h" #include "function/schema/vector_node_rel_functions.h" #include "function/struct/vector_struct_functions.h" using namespace lbug::common; using namespace lbug::binder; using namespace lbug::catalog; namespace lbug { namespace function { struct Label { static void operation(internalID_t& left, list_entry_t& right, string_t& result, ValueVector& leftVector, ValueVector& rightVector, ValueVector& resultVector, uint64_t resPos) { DASSERT(left.tableID < right.size); ListExtract::operation(right, left.tableID + 1 /* listExtract requires 1-based index */, result, rightVector, leftVector, resultVector, resPos); } }; static void execFunction(const std::vector>& params, const std::vector& paramSelVectors, ValueVector& result, SelectionVector* resultSelVector, void* dataPtr = nullptr) { DASSERT(params.size() == 2); BinaryFunctionExecutor::executeSwitch(*params[0], paramSelVectors[0], *params[1], paramSelVectors[1], result, resultSelVector, dataPtr); } static std::shared_ptr getLabelsAsLiteral( std::unordered_map map, ExpressionBinder* expressionBinder) { table_id_t maxTableID = 0; for (auto [id, name] : map) { if (id > maxTableID) { maxTableID = id; } } std::vector> labels; labels.resize(maxTableID + 1); for (auto i = 0u; i < labels.size(); ++i) { if (map.contains(i)) { labels[i] = std::make_unique(LogicalType::STRING(), map.at(i)); } else { labels[i] = std::make_unique(LogicalType::STRING(), std::string("")); } } auto labelsValue = Value(LogicalType::LIST(LogicalType::STRING()), std::move(labels)); return expressionBinder->createLiteralExpression(labelsValue); } static std::unordered_map getNodeTableIDToLabel( std::vector entries) { std::unordered_map map; for (auto& entry : entries) { map.insert({entry->getTableID(), entry->getName()}); } return map; } static std::unordered_map getRelTableIDToLabel( std::vector entries) { std::unordered_map map; for (auto& entry : entries) { auto& relGroupEntry = entry->constCast(); for (auto& relEntryInfo : relGroupEntry.getRelEntryInfos()) { map.insert({relEntryInfo.oid, entry->getName()}); } } return map; } std::shared_ptr LabelFunction::rewriteFunc(const RewriteFunctionBindInput& input) { DASSERT(input.arguments.size() == 1); auto argument = input.arguments[0].get(); auto expressionBinder = input.expressionBinder; if (ExpressionUtil::isNullLiteral(*argument)) { return expressionBinder->createNullLiteralExpression(); } expression_vector children; // For non-pattern expressions (VARIABLE, FUNCTION, etc.), extract the _LABEL field if (argument->expressionType != ExpressionType::PATTERN) { children.push_back(input.arguments[0]); children.push_back(expressionBinder->createLiteralExpression(InternalKeyword::LABEL)); return expressionBinder->bindScalarFunctionExpression(children, StructExtractFunctions::name); } auto disableLiteralRewrite = expressionBinder->getConfig().disableLabelFunctionLiteralRewrite; if (ExpressionUtil::isNodePattern(*argument)) { auto& node = argument->constCast(); if (!disableLiteralRewrite) { if (node.isEmpty()) { return expressionBinder->createLiteralExpression(""); } // ANY graph: the node's single catalog entry is the internal `_nodes` table, whose // name is not the user's label. Read the per-row `label` STRING[] column instead // (the same column `n.*` exposes). functionName is empty on the precompute path, // which preserves legacy behavior; only query-level label()/labels() get here. if (!input.functionName.empty() && isAnyGraphNodeOrRel(node, input.context) && node.hasPropertyExpression("label")) { auto labelProp = node.getPropertyExpression("label"); if (input.functionName == LabelsFunction::name) { return labelProp->copy(); // labels(n) -> STRING[] } // label(n) -> labels(n)[0] (list_extract is 1-based) return expressionBinder->bindScalarFunctionExpression( expression_vector{labelProp, expressionBinder->createLiteralExpression(Value((int64_t)1))}, ListExtractFunction::name); } if (!node.isMultiLabeled()) { auto label = node.getEntry(0)->getName(); return expressionBinder->createLiteralExpression(label); } } children.push_back(node.getInternalID()); auto map = getNodeTableIDToLabel(node.getEntries()); children.push_back(getLabelsAsLiteral(map, expressionBinder)); } else if (ExpressionUtil::isRelPattern(*argument)) { auto& rel = argument->constCast(); if (!disableLiteralRewrite) { if (rel.isEmpty()) { return expressionBinder->createLiteralExpression(""); } // ANY graph: the rel is backed by the internal `_edges` table, whose `label` is a // scalar STRING (a rel has one type). label(e) returns it directly; labels(e) wraps // it into a 1-element list for parity with node labels(). if (!input.functionName.empty() && isAnyGraphNodeOrRel(rel, input.context) && rel.hasPropertyExpression("label")) { auto labelProp = rel.getPropertyExpression("label"); if (input.functionName == LabelsFunction::name) { return expressionBinder->bindScalarFunctionExpression( expression_vector{labelProp}, ListCreationFunction::name); } return labelProp->copy(); // label(e) -> scalar STRING } if (!rel.isMultiLabeled()) { auto label = rel.getEntry(0)->getName(); return expressionBinder->createLiteralExpression(label); } } children.push_back(rel.getInternalID()); auto map = getRelTableIDToLabel(rel.getEntries()); children.push_back(getLabelsAsLiteral(map, expressionBinder)); } DASSERT(children.size() == 2); auto function = std::make_unique(LabelFunction::name, std::vector{LogicalTypeID::STRING, LogicalTypeID::INT64}, LogicalTypeID::STRING, execFunction); auto bindData = std::make_unique(LogicalType::STRING()); auto uniqueName = ScalarFunctionExpression::getUniqueName(LabelFunction::name, children); return std::make_shared(ExpressionType::FUNCTION, std::move(function), std::move(bindData), std::move(children), uniqueName); } function_set LabelFunction::getFunctionSet() { function_set set; auto inputTypes = std::vector{LogicalTypeID::NODE, LogicalTypeID::REL, LogicalTypeID::STRUCT}; for (auto& inputType : inputTypes) { auto function = std::make_unique(name, std::vector{inputType}, rewriteFunc); set.push_back(std::move(function)); } return set; } } // namespace function } // namespace lbug