nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: AfterShip/clickhouse-sql-parser
Pull request #22 (2023-11-02 14:03:02)

Added support 'IF NOT EXISTS' for functions


Patch:
diff --git a/parser/ast.go b/parser/ast.go
index 6069680..e519c0b 100644
--- a/parser/ast.go
+++ b/parser/ast.go
@@ -863,6 +863,7 @@ func (c *CreateView) String(level int) string {
 
 type CreateFunction struct {
 	CreatePos    Pos
+	IfNotExists  bool
 	FunctionName *Ident
 	OnCluster    *OnClusterExpr
 	Params       *ParamExprList
@@ -884,6 +885,9 @@ func (c *CreateFunction) End() Pos {
 func (c *CreateFunction) String(level int) string {
 	var builder strings.Builder
 	builder.WriteString("CREATE FUNCTION ")
+	if c.IfNotExists {
+		builder.WriteString("IF NOT EXISTS ")
+	}
 	builder.WriteString(c.FunctionName.String(level))
 	if c.OnCluster != nil {
 		builder.WriteString(NewLine(level))
diff --git a/parser/parser_table.go b/parser/parser_table.go
index 2a51892..4626ded 100644
--- a/parser/parser_table.go
+++ b/parser/parser_table.go
@@ -1263,6 +1263,10 @@ func (p *Parser) parseCreateFunction(pos Pos) (*CreateFunction, error) {
 	if err := p.consumeKeyword(KeywordFunction); err != nil {
 		return nil, err
 	}
+	ifNotExists, err := p.tryParseIfNotExists()
+	if err != nil {
+		return nil, err
+	}
 	functionName, err := p.parseIdent()
 	if err != nil {
 		return nil, err
@@ -1287,6 +1291,7 @@ func (p *Parser) parseCreateFunction(pos Pos) (*CreateFunction, error) {
 	}
 	return &CreateFunction{
 		CreatePos:    pos,
+		IfNotExists:  ifNotExists,
 		FunctionName: functionName,
 		OnCluster:    onCluster,
 		Params:       params,


Test patch:
diff --git a/parser/testdata/ddl/output/create_function_simple.sql.golden.json b/parser/testdata/ddl/output/create_function_simple.sql.golden.json
index 05f9827..19665ec 100644
--- a/parser/testdata/ddl/output/create_function_simple.sql.golden.json
+++ b/parser/testdata/ddl/output/create_function_simple.sql.golden.json
@@ -1,6 +1,7 @@
 [
   {
     "CreatePos": 0,
+    "IfNotExists": false,
     "FunctionName": {
       "Name": "linear_equation",
       "Unquoted": false,