Add support for query params in load csv (#1653)

This commit is contained in:
DavIvek 2024-02-09 18:26:27 +01:00 committed by GitHub
parent 786cdea260
commit 0133673f1d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 25 additions and 6 deletions

View File

@ -467,8 +467,10 @@ antlrcpp::Any CypherMainVisitor::visitLoadCsv(MemgraphCypher::LoadCsvContext *ct
auto *load_csv = storage_->Create<LoadCsv>();
// handle file name
if (ctx->csvFile()->literal()->StringLiteral()) {
if (ctx->csvFile()->literal() && ctx->csvFile()->literal()->StringLiteral()) {
load_csv->file_ = std::any_cast<Expression *>(ctx->csvFile()->accept(this));
} else if (ctx->csvFile()->parameter()) {
load_csv->file_ = std::any_cast<ParameterLookup *>(ctx->csvFile()->accept(this));
} else {
throw SemanticException("CSV file path should be a string literal");
}

View File

@ -265,7 +265,7 @@ loadCsv : LOAD CSV FROM csvFile ( WITH | NO ) HEADER
( NULLIF nullif ) ?
AS rowVar ;
csvFile : literal ;
csvFile : literal | parameter ;
delimiter : literal ;

View File

@ -232,7 +232,6 @@ class RuleBasedPlanner {
} else if (auto *load_csv = utils::Downcast<query::LoadCsv>(clause)) {
const auto &row_sym = context.symbol_table->at(*load_csv->row_var_);
context.bound_symbols.insert(row_sym);
input_op = std::make_unique<plan::LoadCsv>(std::move(input_op), load_csv->file_, load_csv->with_header_,
load_csv->ignore_bad_, load_csv->delimiter_, load_csv->quote_,
load_csv->nullif_, row_sym);

View File

@ -16,6 +16,7 @@ from pathlib import Path
import pytest
from gqlalchemy import Memgraph
from mgclient import DatabaseError
from neo4j import GraphDatabase
SIMPLE_CSV_FILE = "simple.csv"
@ -52,5 +53,22 @@ def test_given_one_row_in_db_when_load_csv_after_match_then_pass():
assert len(list(results)) == 4
def test_load_csv_with_parameters():
memgraph = Memgraph("localhost", 7687)
URI = "bolt://localhost:7687"
AUTH = ("", "")
with GraphDatabase.driver(URI, auth=AUTH) as client:
with client.session(database="memgraph") as session:
results = session.run(
f"""MATCH (n {{prop: 1}}) LOAD CSV
FROM $file WITH HEADER AS row
CREATE (:Person {{name: row.name}})
RETURN n""",
file=get_file_path(SIMPLE_CSV_FILE),
)
assert len(list(results)) == 4
if __name__ == "__main__":
sys.exit(pytest.main([__file__, "-rA"]))

View File

@ -64,7 +64,7 @@ Feature: Functions
Given an empty graph
And having executed
"""
CREATE (:Node {prop: ToBoolean("t")});
CREATE (:Node {prop: TOBOOLEAN("t")});
"""
When executing query:
"""
@ -74,11 +74,11 @@ Feature: Functions
| n.prop |
| true |
Scenario: ToBoolean test 03:
Scenario: ToBoolean test 04:
Given an empty graph
And having executed
"""
CREATE (:Node {prop: ToBoolean("f")});
CREATE (:Node {prop: TOBOOLEAN("f")});
"""
When executing query:
"""