Skip to content

Commit

Permalink
Merge pull request #1441 from kuzudb/copy-npy-grammar-change
Browse files Browse the repository at this point in the history
Change npy copy grammar
  • Loading branch information
mewim authored Apr 5, 2023
2 parents 0fe05bd + 7587212 commit 2f5b1b4
Show file tree
Hide file tree
Showing 12 changed files with 3,294 additions and 3,276 deletions.
2 changes: 1 addition & 1 deletion dataset/npy-1d/copy.cypher
Original file line number Diff line number Diff line change
@@ -1 +1 @@
npy copy npytable from ("dataset/npy-1d/one_dim_int64.npy", "dataset/npy-1d/one_dim_int32.npy", "dataset/npy-1d/one_dim_int16.npy", "dataset/npy-1d/one_dim_double.npy", "dataset/npy-1d/one_dim_float.npy");
copy npytable from ("dataset/npy-1d/one_dim_int64.npy", "dataset/npy-1d/one_dim_int32.npy", "dataset/npy-1d/one_dim_int16.npy", "dataset/npy-1d/one_dim_double.npy", "dataset/npy-1d/one_dim_float.npy") by column;
2 changes: 1 addition & 1 deletion dataset/npy-20k/copy.cypher
Original file line number Diff line number Diff line change
@@ -1 +1 @@
npy copy npytable from ("dataset/npy-20k/id_int64.npy", "dataset/npy-20k/two_dim_float.npy");
copy npytable from ("dataset/npy-20k/id_int64.npy", "dataset/npy-20k/two_dim_float.npy") by column;
2 changes: 1 addition & 1 deletion dataset/npy-2d/copy.cypher
Original file line number Diff line number Diff line change
@@ -1 +1 @@
npy copy npytable from ("dataset/npy-2d/id_int64.npy", "dataset/npy-2d/two_dim_int64.npy", "dataset/npy-2d/two_dim_int32.npy", "dataset/npy-2d/two_dim_int16.npy", "dataset/npy-2d/two_dim_double.npy", "dataset/npy-2d/two_dim_float.npy");
copy npytable from ("dataset/npy-2d/id_int64.npy", "dataset/npy-2d/two_dim_int64.npy", "dataset/npy-2d/two_dim_int32.npy", "dataset/npy-2d/two_dim_int16.npy", "dataset/npy-2d/two_dim_double.npy", "dataset/npy-2d/two_dim_float.npy") by column;
2 changes: 1 addition & 1 deletion dataset/npy-3d/copy.cypher
Original file line number Diff line number Diff line change
@@ -1 +1 @@
npy copy npytable FROM ("dataset/npy-3d/id_int64.npy", "dataset/npy-3d/three_dim_int64.npy");
copy npytable FROM ("dataset/npy-3d/id_int64.npy", "dataset/npy-3d/three_dim_int64.npy") by column;
4 changes: 3 additions & 1 deletion src/antlr4/Cypher.g4
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ kU_CopyCSV
: COPY SP oC_SchemaName SP FROM SP kU_FilePaths ( SP? '(' SP? kU_ParsingOptions SP? ')' )? ;

kU_CopyNPY
: NPY SP COPY SP oC_SchemaName SP FROM ( SP '(' SP? StringLiteral ( SP? ',' SP? StringLiteral )* ')' ) ;
: COPY SP oC_SchemaName SP FROM SP '(' SP? StringLiteral ( SP? ',' SP? StringLiteral )* ')' SP BY SP COLUMN ;

kU_FilePaths
: '[' SP? StringLiteral ( SP? ',' SP? StringLiteral )* ']'
Expand All @@ -41,6 +41,8 @@ FROM : ( 'F' | 'f' ) ( 'R' | 'r' ) ( 'O' | 'o' ) ( 'M' | 'm' );

NPY : ( 'N' | 'n' ) ( 'P' | 'p' ) ( 'Y' | 'y' ) ;

COLUMN : ( 'C' | 'c' ) ( 'O' | 'o' ) ( 'L' | 'l' ) ( 'U' | 'u' ) ( 'M' | 'm' ) ( 'N' | 'n' ) ;

kU_DDL
: kU_CreateNode
| kU_CreateRel
Expand Down
2 changes: 1 addition & 1 deletion src/binder/bind/bind_copy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ std::unique_ptr<BoundStatement> Binder::bindCopy(const Statement& statement) {
std::unordered_map<common::property_id_t, std::string> propertyToNpyMap;
if (expectedFileType == common::CopyDescription::FileType::UNKNOWN &&
actualFileType == common::CopyDescription::FileType::NPY) {
throw BinderException("Please use NPY COPY statement for copying npy files.");
throw BinderException("Please use COPY FROM BY COLUMN statement for copying npy files.");
}
if (expectedFileType == common::CopyDescription::FileType::NPY &&
actualFileType != expectedFileType) {
Expand Down
30 changes: 15 additions & 15 deletions test/copy/copy_npy_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,66 +246,66 @@ TEST_F(CopyLargeNpyTest, CopyLargeNpyTest) {

TEST_F(CopyNpyFaultTest, CopyNpyInsufficientNumberOfProperties) {
conn->query("create node table npytable (id INT64,i64 INT64[12],PRIMARY KEY(id));");
validateCopyException("npy copy npytable from (\"" +
validateCopyException("copy npytable from (\"" +
TestHelper::appendKuzuRootPath("dataset/npy-3d/three_dim_int64.npy") +
"\");",
"\") by column;",
"Binder exception: Number of npy files is not equal to number of properties in table "
"npytable.");
}

TEST_F(CopyNpyFaultTest, CopyNpyRedundantProperties) {
conn->query("create node table npytable (id INT64,i32 INT32, PRIMARY KEY(id));");
auto f32Path = TestHelper::appendKuzuRootPath("dataset/npy-20k/two_dim_float.npy");
validateCopyException("npy copy npytable from (\"" +
validateCopyException("copy npytable from (\"" +
TestHelper::appendKuzuRootPath("dataset/npy-20k/id_int64.npy") +
"\", \"" + f32Path + "\");",
"\", \"" + f32Path + "\") BY COLUMN;",
"The type of npy file " + f32Path + " does not match the type defined in table npytable.");
}

TEST_F(CopyNpyFaultTest, CopyNpyVectorIntoScaler) {
conn->query("create node table npytable (id INT64,f32 FLOAT, PRIMARY KEY(id));");
auto f32Path = TestHelper::appendKuzuRootPath("dataset/npy-20k/two_dim_float.npy");
validateCopyException("npy copy npytable from (\"" +
validateCopyException("copy npytable from (\"" +
TestHelper::appendKuzuRootPath("dataset/npy-20k/id_int64.npy") +
"\", \"" + f32Path + "\");",
"\", \"" + f32Path + "\") by column;",
"Cannot copy a vector property in npy file " + f32Path +
" to a scalar property in table npytable.");
}

TEST_F(CopyNpyFaultTest, CopyNpyWithMismatchedTypeOneDimensionTest) {
conn->query("create node table npytable (id INT64,i32 INT32, PRIMARY KEY(id));");
auto f32Path = TestHelper::appendKuzuRootPath("dataset/npy-1d/one_dim_float.npy");
validateCopyException("npy copy npytable from ( \"" +
validateCopyException("copy npytable from ( \"" +
TestHelper::appendKuzuRootPath("dataset/npy-1d/one_dim_int64.npy") +
"\", \"" + f32Path + "\");",
"\", \"" + f32Path + "\") by column;",
"The type of npy file " + f32Path + " does not match the type defined in table npytable.");
}

TEST_F(CopyNpyFaultTest, CopyNpyWithMismatchedTypeTwoDimensionTest) {
conn->query("create node table npytable (id INT64,i32 INT32[10], PRIMARY KEY(id));");
auto f32Path = TestHelper::appendKuzuRootPath("dataset/npy-20k/two_dim_float.npy");
validateCopyException("npy copy npytable from (\"" +
validateCopyException("copy npytable from (\"" +
TestHelper::appendKuzuRootPath("dataset/npy-20k/id_int64.npy") +
"\", \"" + f32Path + "\");",
"\", \"" + f32Path + "\") by column;",
"The type of npy file " + f32Path + " does not match the type defined in table npytable.");
}

TEST_F(CopyNpyFaultTest, CopyNpyWithMismatchedDimensionTest) {
conn->query("create node table npytable (id INT64,f32 FLOAT[12],PRIMARY KEY(id));");
auto twoDimFloatNpyPath = TestHelper::appendKuzuRootPath("dataset/npy-20k/two_dim_float.npy");
validateCopyException("npy copy npytable from (\"" +
validateCopyException("copy npytable from (\"" +
TestHelper::appendKuzuRootPath("dataset/npy-20k/id_int64.npy") +
"\", \"" + twoDimFloatNpyPath + "\");",
"\", \"" + twoDimFloatNpyPath + "\") by column;",
"The shape of " + twoDimFloatNpyPath +
" does not match the length of the fixed list property in table npytable.");
}

TEST_F(CopyNpyFaultTest, CopyNpyWithMismatchedLengthTest) {
conn->query("create node table npytable (id INT64,i64 INT64[12],PRIMARY KEY(id));");
validateCopyException(
"npy copy npytable from (\"" +
TestHelper::appendKuzuRootPath("dataset/npy-20k/id_int64.npy") + "\", \"" +
TestHelper::appendKuzuRootPath("dataset/npy-3d/three_dim_int64.npy") + "\");",
"copy npytable from (\"" + TestHelper::appendKuzuRootPath("dataset/npy-20k/id_int64.npy") +
"\", \"" + TestHelper::appendKuzuRootPath("dataset/npy-3d/three_dim_int64.npy") +
"\") by column;",
"Number of rows in npy files is not equal to each other.");
}
} // namespace testing
Expand Down
Loading

0 comments on commit 2f5b1b4

Please sign in to comment.