Skip to content

Commit

Permalink
cherry pick from nebula : full text bugfix (vesoft-inc#2829)
Browse files Browse the repository at this point in the history
* show analyzer when show ft index (vesoft-inc#5587)

* show analyzer when show ft index

* fix tck

* fix ft (vesoft-inc#5594)

* fix conflict

* Fix expression issues when using ES_QUERY in Lookup clause (vesoft-inc#5597)

* Add cherry pick missed codes

* Restore permission check logics

---------

Co-authored-by: hs.zhang <[email protected]>
Co-authored-by: Yee <[email protected]>
  • Loading branch information
3 people authored Jun 16, 2023
1 parent 00cb1b6 commit 6739028
Show file tree
Hide file tree
Showing 30 changed files with 553 additions and 155 deletions.
3 changes: 1 addition & 2 deletions src/common/datatypes/Edge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,8 @@ const Value& Edge::value(const std::string& key) const {
auto find = props.find(key);
if (find != props.end()) {
return find->second;
} else {
return Value::kNullValue;
}
return Value::kNullValue;
}

bool Edge::operator<(const Edge& rhs) const {
Expand Down
12 changes: 12 additions & 0 deletions src/common/datatypes/Vertex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,18 @@ const Value& Vertex::value(const std::string& key) const {
return Value::kNullValue;
}

const Value& Vertex::getTagProp(const std::string& tagName, const std::string& prop) const {
for (const auto& tag : tags) {
if (tag.name == tagName) {
auto find = tag.props.find(prop);
if (find != tag.props.end()) {
return find->second;
}
}
}
return Value::kNullValue;
}

std::string Vertex::toString() const {
std::stringstream os;
os << "(" << vid << ")";
Expand Down
1 change: 1 addition & 0 deletions src/common/datatypes/Vertex.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ struct Vertex {
bool contains(const Value& key) const;

const Value& value(const std::string& key) const;
const Value& getTagProp(const std::string& tagName, const std::string& prop) const;
};

inline void swap(Vertex& a, Vertex& b) {
Expand Down
6 changes: 4 additions & 2 deletions src/common/plugin/fulltext/elasticsearch/ESAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void ESBulk::put(const std::string& indexName,
const std::string& src,
const std::string& dst,
int64_t rank,
const std::string& text) {
std::map<std::string, std::string> data) {
folly::dynamic action = folly::dynamic::object();
folly::dynamic metadata = folly::dynamic::object();
folly::dynamic body = folly::dynamic::object();
Expand All @@ -35,7 +35,9 @@ void ESBulk::put(const std::string& indexName,
body["src"] = src;
body["dst"] = dst;
body["rank"] = rank;
body["text"] = text;
for (auto& [key, value] : data) {
body[key] = std::move(value);
}
documents_[indexName].emplace_back(std::move(action));
documents_[indexName].emplace_back(std::move(body));
}
Expand Down
2 changes: 1 addition & 1 deletion src/common/plugin/fulltext/elasticsearch/ESAdapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class ESBulk {
const std::string& src,
const std::string& dst,
int64_t rank,
const std::string& text);
std::map<std::string, std::string> data);
void delete_(const std::string& indexName,
const std::string& vid,
const std::string& src,
Expand Down
3 changes: 2 additions & 1 deletion src/common/plugin/fulltext/test/ElasticsearchTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,8 @@ TEST_F(ESTest, bulk) {
plugin::ESClient client(mockHttpClient, "http", "127.0.0.1:9200", "", "");
plugin::ESAdapter adapter(std::vector<plugin::ESClient>({client}));
plugin::ESBulk bulk;
bulk.put("nebula_index_1", "1", "", "", 0, "vertex text");
std::map<std::string, std::string> data{{"text", "vretex text"}};
bulk.put("nebula_index_1", "1", "", "", 0, data);
bulk.delete_("nebula_index_2", "", "a", "b", 10);
{
auto result = adapter.bulk(bulk, true);
Expand Down
19 changes: 19 additions & 0 deletions src/graph/context/iterator/SequentialIter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,25 @@ StatusOr<std::size_t> SequentialIter::getColumnIndex(const std::string& col) con
return index->second;
}

const Value& SequentialIter::getTagProp(const std::string& tag, const std::string& prop) const {
const auto& val = this->getColumn("VERTEX");
if (val.isVertex()) {
const auto& vertex = val.getVertex();
return vertex.getTagProp(tag, prop);
}
return Value::kNullValue;
}

const Value& SequentialIter::getEdgeProp(const std::string& edge, const std::string& prop) const {
DCHECK(!edge.empty());
const auto& val = this->getColumn("EDGE");
if (val.isEdge()) {
const auto& e = val.getEdge();
return e.value(prop);
}
return Value::kNullValue;
}

Value SequentialIter::getVertex(const std::string& name) {
return getColumn(name);
}
Expand Down
4 changes: 4 additions & 0 deletions src/graph/context/iterator/SequentialIter.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ class SequentialIter : public Iterator {

StatusOr<std::size_t> getColumnIndex(const std::string& col) const override;

const Value& getTagProp(const std::string& tag, const std::string& prop) const override;

const Value& getEdgeProp(const std::string& edge, const std::string& prop) const override;

Value getVertex(const std::string& name = "") override;

Value getEdge() const override;
Expand Down
7 changes: 6 additions & 1 deletion src/graph/executor/maintain/FTIndexExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ folly::Future<Status> ShowFTIndexesExecutor::execute() {

auto indexes = std::move(resp).value();
DataSet dataSet;
dataSet.colNames = {"Name", "Schema Type", "Schema Name", "Fields"};
dataSet.colNames = {"Name", "Schema Type", "Schema Name", "Fields", "Analyzer"};
for (auto &index : indexes) {
if (index.second.get_space_id() != spaceId) {
continue;
Expand All @@ -92,6 +92,11 @@ folly::Future<Status> ShowFTIndexesExecutor::execute() {
row.values.emplace_back(isEdge ? "Edge" : "Tag");
row.values.emplace_back(std::move(shmNameRet).value());
row.values.emplace_back(std::move(fields));
std::string analyzer = index.second.get_analyzer();
if (analyzer.empty()) {
analyzer = "default";
}
row.values.emplace_back(analyzer);
dataSet.rows.emplace_back(std::move(row));
}
return finish(ResultBuilder()
Expand Down
28 changes: 17 additions & 11 deletions src/graph/executor/query/FulltextIndexScanExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "common/datatypes/DataSet.h"
#include "common/datatypes/Edge.h"
#include "graph/planner/plan/Query.h"
#include "graph/util/Constants.h"
#include "graph/util/FTIndexUtils.h"

using nebula::storage::StorageClient;
Expand All @@ -31,7 +32,7 @@ folly::Future<Status> FulltextIndexScanExecutor::execute() {
const auto& space = qctx()->rctx()->session()->space();
if (!isIntVidType(space)) {
if (ftIndexScan->isEdge()) {
DataSet edges({"edge"});
DataSet edges({"id", kScore});
for (auto& item : esResultValue.items) {
Edge edge;
edge.src = item.src;
Expand All @@ -42,25 +43,26 @@ folly::Future<Status> FulltextIndexScanExecutor::execute() {
}
finish(ResultBuilder().value(Value(std::move(edges))).iter(Iterator::Kind::kProp).build());
} else {
DataSet vertices({kVid});
DataSet vertices({"id", kScore});
for (auto& item : esResultValue.items) {
vertices.emplace_back(Row({item.vid, item.score}));
}
finish(ResultBuilder().value(Value(std::move(vertices))).iter(Iterator::Kind::kProp).build());
}
} else {
if (ftIndexScan->isEdge()) {
DataSet edges({kSrc, kRank, kDst});
DataSet edges({"id", kScore});
for (auto& item : esResultValue.items) {
std::string srcStr = item.src;
std::string dstStr = item.dst;
int64_t src = *reinterpret_cast<int64_t*>(srcStr.data());
int64_t dst = *reinterpret_cast<int64_t*>(dstStr.data());
edges.emplace_back(Row({src, item.rank, dst}));
Edge edge;
edge.src = item.src;
edge.dst = item.dst;
edge.ranking = item.rank;
edge.type = ftIndexScan->schemaId();
edges.emplace_back(Row({std::move(edge), item.score}));
}
finish(ResultBuilder().value(Value(std::move(edges))).iter(Iterator::Kind::kProp).build());
} else {
DataSet vertices({kVid});
DataSet vertices({"id", kScore});
for (auto& item : esResultValue.items) {
std::string vidStr = item.vid;
int64_t vid = *reinterpret_cast<int64_t*>(vidStr.data());
Expand All @@ -82,8 +84,12 @@ StatusOr<plugin::ESQueryResult> FulltextIndexScanExecutor::accessFulltextIndex(
auto arg = tsExpr->arg();
auto index = arg->index();
auto query = arg->query();
int64_t offset = ftIndexScan->offset();
int64_t count = ftIndexScan->limit();
int64_t offset = ftIndexScan->getValidOffset();
auto limit = ftIndexScan->limit();
if (limit > std::numeric_limits<int32_t>::max()) {
limit = std::numeric_limits<int32_t>::max();
}
int64_t count = limit - offset;
execFunc = [=, &esAdapter]() { return esAdapter.queryString(index, query, offset, count); };
break;
}
Expand Down
2 changes: 1 addition & 1 deletion src/graph/executor/query/GetEdgesExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ folly::Future<Status> GetEdgesExecutor::getEdges() {
ge->exprs(),
ge->dedup(),
ge->orderBy(),
ge->limit(qctx()),
ge->getValidLimit(),
ge->filter())
.via(runner())
.ensure([this, getPropsTime]() {
Expand Down
2 changes: 1 addition & 1 deletion src/graph/executor/query/GetVerticesExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ folly::Future<Status> GetVerticesExecutor::getVertices() {
gv->exprs(),
gv->dedup(),
gv->orderBy(),
gv->limit(qctx()),
gv->getValidLimit(),
gv->filter())
.via(runner())
.ensure([this, getPropsTime]() {
Expand Down
16 changes: 12 additions & 4 deletions src/graph/planner/ngql/LookupPlanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,30 @@ StatusOr<SubPlan> LookupPlanner::transform(AstContext* astCtx) {
auto rank = FunctionCallExpression::make(pool, "rank", {ColumnExpression::make(pool, 0)});
auto dst = FunctionCallExpression::make(pool, "dst", {ColumnExpression::make(pool, 0)});

plan.root =
auto ge =
GetEdges::make(qctx, plan.root, spaceId, src, type, rank, dst, std::move(edgeProps));

auto yieldColumns = pool->makeAndAdd<YieldColumns>();
yieldColumns->addColumn(new YieldColumn(EdgeExpression::make(pool)));
plan.root = Project::make(qctx, ge, yieldColumns);

hashKeys = {ColumnExpression::make(pool, 0)};
probeKeys = {ColumnExpression::make(pool, 0)};
probeKeys = {EdgeExpression::make(pool)};
} else {
storage::cpp2::VertexProp vertexProp;
vertexProp.tag_ref() = lookupCtx->schemaId;
vertexProp.props_ref() = lookupCtx->idxReturnCols;
auto vertexProps = std::make_unique<std::vector<storage::cpp2::VertexProp>>();
vertexProps->emplace_back(std::move(vertexProp));
plan.root = GetVertices::make(
auto gv = GetVertices::make(
qctx, plan.root, spaceId, ColumnExpression::make(pool, 0), std::move(vertexProps));

auto yieldColumns = pool->makeAndAdd<YieldColumns>();
yieldColumns->addColumn(new YieldColumn(VertexExpression::make(pool)));
plan.root = Project::make(qctx, gv, yieldColumns);

hashKeys = {ColumnExpression::make(pool, 0)};
probeKeys = {FunctionCallExpression::make(pool, "id", {ColumnExpression::make(pool, 0)})};
probeKeys = {FunctionCallExpression::make(pool, "id", {VertexExpression::make(pool)})};
}

if (lookupCtx->hasScore) {
Expand Down
10 changes: 9 additions & 1 deletion src/graph/planner/plan/Query.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,18 @@ using folly::stringPrintf;
namespace nebula {
namespace graph {

int64_t Explore::getValidLimit() const {
auto limit = this->limit();
if (limit < 0) {
limit = std::numeric_limits<int64_t>::max();
}
return limit;
}

int64_t Explore::limit(QueryContext* qctx) const {
DCHECK(ExpressionUtils::isEvaluableExpr(limit_, qctx));
QueryExpressionContext ctx(qctx ? qctx->ectx() : nullptr);
return DCHECK_NOTNULL(limit_)->eval(ctx).getInt();
return limit(ctx);
}

std::unique_ptr<PlanNodeDescription> Explore::explain() const {
Expand Down
11 changes: 9 additions & 2 deletions src/graph/planner/plan/Query.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace graph {
// GetVertices,
// GetEdges,
// IndexScan
// FulltextIndexScan
class Explore : public SingleInputNode {
public:
GraphSpaceID space() const {
Expand All @@ -36,6 +37,8 @@ class Explore : public SingleInputNode {
return dedup_;
}

int64_t getValidLimit() const;

// Get the constant limit value
int64_t limit(QueryContext* qctx = nullptr) const;

Expand Down Expand Up @@ -441,7 +444,7 @@ class GetVertices : public Explore {
std::unique_ptr<std::vector<Expr>>&& exprs = nullptr,
bool dedup = false,
std::vector<storage::cpp2::OrderBy> orderBy = {},
int64_t limit = std::numeric_limits<int64_t>::max(),
int64_t limit = -1,
Expression* filter = nullptr) {
return qctx->objPool()->makeAndAdd<GetVertices>(qctx,
Kind::kGetVertices,
Expand Down Expand Up @@ -547,7 +550,7 @@ class GetEdges final : public Explore {
std::unique_ptr<std::vector<EdgeProp>>&& props = nullptr,
std::unique_ptr<std::vector<Expr>>&& exprs = nullptr,
bool dedup = false,
int64_t limit = std::numeric_limits<int64_t>::max(),
int64_t limit = -1,
std::vector<storage::cpp2::OrderBy> orderBy = {},
Expression* filter = nullptr) {
return qctx->objPool()->makeAndAdd<GetEdges>(qctx,
Expand Down Expand Up @@ -767,6 +770,10 @@ class FulltextIndexScan : public Explore {
return isEdge_;
}

int64_t getValidOffset() const {
return offset_ >= 0 ? offset_ : 0;
}

int64_t offset() const {
return offset_;
}
Expand Down
7 changes: 7 additions & 0 deletions src/graph/util/ExpressionUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,13 @@ Expression *ExpressionUtils::rewriteEdgePropFunc2LabelAttribute(
return RewriteVisitor::transform(expr, std::move(matcher), std::move(rewriter));
}

Expression *ExpressionUtils::rewriteLabelAttr2PropExpr(const Expression *expr, bool isEdge) {
if (isEdge) {
return rewriteLabelAttr2EdgeProp(expr);
}
return rewriteLabelAttr2TagProp(expr);
}

Expression *ExpressionUtils::rewriteLabelAttr2TagProp(const Expression *expr) {
ObjectPool *pool = expr->getObjPool();
auto matcher = [](const Expression *e) -> bool {
Expand Down
3 changes: 3 additions & 0 deletions src/graph/util/ExpressionUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ class ExpressionUtils {
static Expression* rewriteEdgePropFunc2LabelAttribute(
const Expression* expr, const std::unordered_map<std::string, AliasType>& aliasTypeMap);

// rewrite LabelAttr expr to property expr
static Expression* rewriteLabelAttr2PropExpr(const Expression* expr, bool isEdge);

// rewrite LabelAttr to tagProp
static Expression* rewriteLabelAttr2TagProp(const Expression* expr);

Expand Down
Loading

0 comments on commit 6739028

Please sign in to comment.