Skip to content

Commit

Permalink
Merge pull request #1 from nebula-hackathon-2021/v2.6.1_udf-parser
Browse files Browse the repository at this point in the history
feat(parser): create/drop function
  • Loading branch information
arcosx authored Dec 15, 2021
2 parents fec5258 + 9e28dfa commit 1b9e6c8
Show file tree
Hide file tree
Showing 12 changed files with 210 additions and 0 deletions.
1 change: 1 addition & 0 deletions .linters/cpp/checkKeyword.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
'KW_TIME',
'KW_DATETIME',
'KW_VID_SIZE',
'KW_FUNCTION',
'KW_TAG',
'KW_TAGS',
'KW_UNION',
Expand Down
4 changes: 4 additions & 0 deletions src/graph/service/PermissionCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace graph {
* kInsertVertex, kUpdateVertex, kInsertEdge,
* kUpdateEdge, kDeleteVertex, kDeleteEdges
* Special operation : kShow, kChangePassword
* UDF : kCreateFunction, kAlterFunction, kDropFunction
*/

// static
Expand Down Expand Up @@ -83,6 +84,9 @@ Status PermissionCheck::permissionCheck(ClientSession *session,
case Sentence::Kind::kAlterEdge:
case Sentence::Kind::kDropTag:
case Sentence::Kind::kDropEdge:
case Sentence::Kind::kCreateFunction:
// case Sentence::Kind::kAlterFunction:
case Sentence::Kind::kDropFunction:
case Sentence::Kind::kCreateTagIndex:
case Sentence::Kind::kCreateEdgeIndex:
case Sentence::Kind::kCreateFTIndex:
Expand Down
25 changes: 25 additions & 0 deletions src/graph/validator/MaintainValidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,20 @@ static Status checkColName(const std::vector<ColumnSpecification *> specs) {
return Status::OK();
}

Status CreateFunctionValidator::validateImpl() {
// TODO(TripleZ): add create function logic
auto sentence = static_cast<CreateFunctionSentence *>(sentence_);
auto name = *sentence->name();
auto functionSource = *sentence->getFunctionSource();
auto funcType = functionSource.getType();
auto funcSource = functionSource.getSource();

std::cout << " => create function: name(" + name + "), type(" +
funcType + "), source(" + funcSource + ")" << std::endl;

return Status::OK();
}

Status CreateTagValidator::validateImpl() {
createCtx_ = getContext<CreateSchemaContext>();
auto sentence = static_cast<CreateTagSentence *>(sentence_);
Expand Down Expand Up @@ -269,6 +283,17 @@ Status ShowCreateEdgeValidator::toPlan() {
return Status::OK();
}

Status DropFunctionValidator::validateImpl() { return Status::OK(); }

Status DropFunctionValidator::toPlan() {
// TODO(TripleZ): add drop function logic
auto sentence = static_cast<DropFunctionSentence *>(sentence_);
auto name = *sentence->name();
std::cout << " => drop function: name(" + name + ")" << std::endl;

return Status::OK();
}

Status DropTagValidator::validateImpl() { return Status::OK(); }

Status DropTagValidator::toPlan() {
Expand Down
19 changes: 19 additions & 0 deletions src/graph/validator/MaintainValidator.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@

namespace nebula {
namespace graph {
class CreateFunctionValidator final : public Validator {
public:
CreateFunctionValidator(Sentence* sentence, QueryContext* context)
: Validator(sentence, context) {}

private:
Status validateImpl() override;
};

class CreateTagValidator final : public Validator {
public:
CreateTagValidator(Sentence* sentence, QueryContext* context) : Validator(sentence, context) {}
Expand Down Expand Up @@ -124,6 +133,16 @@ class ShowEdgesValidator final : public Validator {
Status toPlan() override;
};

class DropFunctionValidator final : public Validator {
public:
DropFunctionValidator(Sentence *sentence, QueryContext* context) : Validator(sentence, context) {}

private:
Status validateImpl() override;

Status toPlan() override;
};

class DropTagValidator final : public Validator {
public:
DropTagValidator(Sentence* sentence, QueryContext* context) : Validator(sentence, context) {}
Expand Down
4 changes: 4 additions & 0 deletions src/graph/validator/Validator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ std::unique_ptr<Validator> Validator::makeValidator(Sentence* sentence, QueryCon
return std::make_unique<CreateSpaceValidator>(sentence, context);
case Sentence::Kind::kCreateSpaceAs:
return std::make_unique<CreateSpaceAsValidator>(sentence, context);
case Sentence::Kind::kCreateFunction:
return std::make_unique<CreateFunctionValidator>(sentence, context);
case Sentence::Kind::kCreateTag:
return std::make_unique<CreateTagValidator>(sentence, context);
case Sentence::Kind::kCreateEdge:
Expand All @@ -103,6 +105,8 @@ std::unique_ptr<Validator> Validator::makeValidator(Sentence* sentence, QueryCon
return std::make_unique<ShowEdgesValidator>(sentence, context);
case Sentence::Kind::kDropSpace:
return std::make_unique<DropSpaceValidator>(sentence, context);
case Sentence::Kind::kDropFunction:
return std::make_unique<DropFunctionValidator>(sentence, context);
case Sentence::Kind::kDropTag:
return std::make_unique<DropTagValidator>(sentence, context);
case Sentence::Kind::kDropEdge:
Expand Down
19 changes: 19 additions & 0 deletions src/parser/MaintainSentences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,21 @@ std::string ColumnSpecificationList::toString() const {
return buf;
}

std::string CreateFunctionSentence::toString() const {
std::string buf;
buf.reserve(256);
buf += "CREATE FUNCTION ";
if (isIfNotExist()) {
buf += "IF NOT EXISTS ";
}
buf += "`";
buf += *name_;
buf += "` ";
buf += "FROM ";
buf += funcSource_->toString();
return buf;
}

std::string CreateTagSentence::toString() const {
std::string buf;
buf.reserve(256);
Expand Down Expand Up @@ -224,6 +239,10 @@ std::string DescribeEdgeSentence::toString() const {
return folly::stringPrintf("DESCRIBE EDGE %s", name_.get()->c_str());
}

std::string DropFunctionSentence::toString() const {
return folly::stringPrintf("DROP FUNCTION %s", name_.get()->c_str());
}

std::string DropTagSentence::toString() const {
return folly::stringPrintf("DROP TAG %s", name_.get()->c_str());
}
Expand Down
59 changes: 59 additions & 0 deletions src/parser/MaintainSentences.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,28 @@ class ColumnSpecificationList final {
std::vector<std::unique_ptr<ColumnSpecification>> columns_;
};

class FunctionSource final {
public:
FunctionSource() = default;
FunctionSource(std::string type, std::string source): type_(type), source_(source) {}

std::string toString() const {
return source_;
}

std::string getType() const {
return type_;
}

std::string getSource() const {
return source_;
}

private:
std::string type_;
std::string source_;
};

class ColumnNameList final {
public:
ColumnNameList() = default;
Expand Down Expand Up @@ -288,6 +310,28 @@ class CreateTagSentence final : public CreateSentence {
std::unique_ptr<SchemaPropList> schemaProps_;
};

class CreateFunctionSentence final : public CreateSentence {
public:
CreateFunctionSentence(std::string *name,
FunctionSource *funcSource,
bool ifNotExists)
: CreateSentence(ifNotExists) {
name_.reset(name);
funcSource_.reset(funcSource);
kind_ = Kind::kCreateFunction;
}

std::string toString() const override;

const std::string *name() const { return name_.get(); }

FunctionSource* getFunctionSource() const { return funcSource_.get(); }

private:
std::unique_ptr<std::string> name_;
std::unique_ptr<FunctionSource> funcSource_;
};

class CreateEdgeSentence final : public CreateSentence {
public:
CreateEdgeSentence(std::string *name,
Expand Down Expand Up @@ -440,6 +484,21 @@ class DescribeEdgeSentence final : public Sentence {
std::unique_ptr<std::string> name_;
};

class DropFunctionSentence final : public DropSentence {
public:
explicit DropFunctionSentence(std::string *name, bool ifExists) : DropSentence(ifExists) {
name_.reset(name);
kind_ = Kind::kDropFunction;
}

std::string toString() const override;

const std::string *name() const { return name_.get(); }

private:
std::unique_ptr<std::string> name_;
};

class DropTagSentence final : public DropSentence {
public:
explicit DropTagSentence(std::string *name, bool ifExists) : DropSentence(ifExists) {
Expand Down
3 changes: 3 additions & 0 deletions src/parser/Sentence.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ class Sentence {
kAssignment,
kCreateTag,
kAlterTag,
kCreateFunction,
// kAlterFunction,
kDropFunction,
kCreateEdge,
kAlterEdge,
kDescribeTag,
Expand Down
30 changes: 30 additions & 0 deletions src/parser/parser.yy
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ static constexpr size_t kCommentLengthLimit = 256;
nebula::AlterSchemaOptItem *alter_schema_opt_item;
nebula::RoleTypeClause *role_type_clause;
nebula::AclItemClause *acl_item_clause;
nebula::FunctionSource *function_source;
nebula::SchemaPropList *create_schema_prop_list;
nebula::SchemaPropItem *create_schema_prop_item;
nebula::SchemaPropList *alter_schema_prop_list;
Expand Down Expand Up @@ -201,6 +202,7 @@ static constexpr size_t kCommentLengthLimit = 256;
%token KW_SESSIONS KW_SESSION
%token KW_KILL KW_QUERY KW_QUERIES KW_TOP
%token KW_GEOGRAPHY KW_POINT KW_LINESTRING KW_POLYGON
%token KW_FUNCTION

/* symbols */
%token L_PAREN R_PAREN L_BRACKET R_BRACKET L_BRACE R_BRACE COMMA
Expand All @@ -213,6 +215,7 @@ static constexpr size_t kCommentLengthLimit = 256;
%token <intval> INTEGER
%token <doubleval> DOUBLE
%token <strval> STRING VARIABLE LABEL IPV4
%token <strval> HTTP_URL WASM_BASE64

%type <strval> name_label unreserved_keyword predicate_name
%type <expr> expression
Expand Down Expand Up @@ -359,6 +362,10 @@ static constexpr size_t kCommentLengthLimit = 256;
%type <sentence> create_snapshot_sentence drop_snapshot_sentence
%type <sentence> add_listener_sentence remove_listener_sentence list_listener_sentence

%type <sentence> create_function_sentence
%type <function_source> create_function_from_vendor
%type <sentence> drop_function_sentence

%type <sentence> admin_job_sentence
%type <sentence> create_user_sentence alter_user_sentence drop_user_sentence change_password_sentence
%type <sentence> show_queries_sentence kill_query_sentence
Expand Down Expand Up @@ -2195,6 +2202,27 @@ create_schema_prop_item
}
;

create_function_from_vendor
: HTTP_URL {
$$ = new FunctionSource(std::string("HTTP"), *$1);
}
| WASM_BASE64 {
$$ = new FunctionSource(std::string("WASM"), *$1);
}
;

create_function_sentence
: KW_CREATE KW_FUNCTION opt_if_not_exists name_label KW_FROM create_function_from_vendor {
$$ = new CreateFunctionSentence($4, $6, $3);
}
;

drop_function_sentence
: KW_DROP KW_FUNCTION opt_if_exists name_label {
$$ = new DropFunctionSentence($4, $3);
}
;

create_tag_sentence
: KW_CREATE KW_TAG opt_if_not_exists name_label L_PAREN R_PAREN opt_create_schema_prop_list {
if ($7 == nullptr) {
Expand Down Expand Up @@ -3508,10 +3536,12 @@ maintain_sentence
| drop_space_sentence { $$ = $1; }
| create_tag_sentence { $$ = $1; }
| create_edge_sentence { $$ = $1; }
| create_function_sentence { $$ = $1; }
| alter_tag_sentence { $$ = $1; }
| alter_edge_sentence { $$ = $1; }
| describe_tag_sentence { $$ = $1; }
| describe_edge_sentence { $$ = $1; }
| drop_function_sentence { $$ = $1; }
| drop_tag_sentence { $$ = $1; }
| drop_edge_sentence { $$ = $1; }
| create_tag_index_sentence { $$ = $1; }
Expand Down
13 changes: 13 additions & 0 deletions src/parser/scanner.lex
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ IP_OCTET ([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])



HTTP_URL (http:\/\/[a-zA-Z0-9\(\)@:%._\-\+~#=?&\/]+)
WASM_BASE64 (wasm:\/\/[A-Za-z0-9+\/=]+)
/* WASM_BASE64 (wasm:\/\/(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)) */

%%

/* Reserved keyword */
Expand Down Expand Up @@ -97,6 +101,7 @@ IP_OCTET ([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])
"DATE" { return TokenType::KW_DATE; }
"TIME" { return TokenType::KW_TIME; }
"DATETIME" { return TokenType::KW_DATETIME; }
"FUNCTION" { return TokenType::KW_FUNCTION; }
"TAG" { return TokenType::KW_TAG; }
"TAGS" { return TokenType::KW_TAGS; }
"UNION" { return TokenType::KW_UNION; }
Expand Down Expand Up @@ -331,6 +336,14 @@ IP_OCTET ([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])
}
return TokenType::LABEL;
}
{HTTP_URL} {
yylval->strval = new std::string(yytext, yyleng);
return TokenType::HTTP_URL;
}
{WASM_BASE64} {
yylval->strval = new std::string(yytext, yyleng);
return TokenType::WASM_BASE64;
}
{IP_OCTET}(\.{IP_OCTET}){3} {
yylval->strval = new std::string(yytext, yyleng);
return TokenType::IPV4;
Expand Down
28 changes: 28 additions & 0 deletions src/parser/test/ParserTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,34 @@ TEST_F(ParserTest, TagOperation) {
}
}

TEST_F(ParserTest, FunctionOperation) {
{
std::string query = "CREATE FUNCTION f1 FROM wasm://d2FzbQo=";
auto result = parse(query);
ASSERT_TRUE(result.ok()) << result.status();
}
{
std::string query = "CREATE FUNCTION f1 FROM http://nebula-graph.io/remote/f1";
auto result = parse(query);
ASSERT_TRUE(result.ok()) << result.status();
}
{
std::string query = "CREATE FUNCTION IF NOT EXISTS f1 FROM wasm://d2FzbQo=";
auto result = parse(query);
ASSERT_TRUE(result.ok()) << result.status();
}
{
std::string query = "CREATE FUNCTION IF NOT EXISTS f1 FROM http://nebula-graph.io/remote/f1";
auto result = parse(query);
ASSERT_TRUE(result.ok()) << result.status();
}
{
std::string query = "DROP FUNCTION f1";
auto result = parse(query);
ASSERT_TRUE(result.ok()) << result.status();
}
}

TEST_F(ParserTest, EdgeOperation) {
{
std::string query =
Expand Down
Loading

0 comments on commit 1b9e6c8

Please sign in to comment.