diff options
author | Håvard Pettersen <havardpe@oath.com> | 2019-11-11 12:45:03 +0000 |
---|---|---|
committer | Håvard Pettersen <havardpe@oath.com> | 2019-11-13 11:27:41 +0000 |
commit | 67e381b74bdba089e618de1bada8eb328e8af3b1 (patch) | |
tree | b85c8173ef5988679f98c1e75d4119d19514d312 /eval | |
parent | 3d9c56b13ef888a668a6429ab0a65315582f9fee (diff) |
enable obtaining the unsorted dimensions list
... as a side-effect of parsing a type spec
Diffstat (limited to 'eval')
-rw-r--r-- | eval/src/tests/eval/value_type/value_type_test.cpp | 31 | ||||
-rw-r--r-- | eval/src/vespa/eval/eval/value_type.cpp | 6 | ||||
-rw-r--r-- | eval/src/vespa/eval/eval/value_type.h | 1 | ||||
-rw-r--r-- | eval/src/vespa/eval/eval/value_type_spec.cpp | 18 | ||||
-rw-r--r-- | eval/src/vespa/eval/eval/value_type_spec.h | 6 |
5 files changed, 59 insertions, 3 deletions
diff --git a/eval/src/tests/eval/value_type/value_type_test.cpp b/eval/src/tests/eval/value_type/value_type_test.cpp index 42b34c0d3ea..d0b48759578 100644 --- a/eval/src/tests/eval/value_type/value_type_test.cpp +++ b/eval/src/tests/eval/value_type/value_type_test.cpp @@ -162,6 +162,37 @@ TEST("require that value type spec can be parsed with extra whitespace") { EXPECT_EQUAL(ValueType::tensor_type({{"y", 10}}, CellType::FLOAT), ValueType::from_spec(" tensor < float > ( y [ 10 ] ) ")); } +TEST("require that the unsorted dimension list can be obtained when parsing type spec") { + std::vector<ValueType::Dimension> unsorted; + auto type = ValueType::from_spec("tensor(y[10],z[5],x{})", unsorted); + EXPECT_EQUAL(ValueType::tensor_type({{"x"}, {"y", 10}, {"z", 5}}), type); + ASSERT_EQUAL(unsorted.size(), 3u); + EXPECT_EQUAL(unsorted[0].name, "y"); + EXPECT_EQUAL(unsorted[0].size, 10u); + EXPECT_EQUAL(unsorted[1].name, "z"); + EXPECT_EQUAL(unsorted[1].size, 5u); + EXPECT_EQUAL(unsorted[2].name, "x"); + EXPECT_EQUAL(unsorted[2].size, npos); +} + +TEST("require that the unsorted dimension list can be obtained also when the type spec is invalid") { + std::vector<ValueType::Dimension> unsorted; + auto type = ValueType::from_spec("tensor(x[10],x[5])...", unsorted); + EXPECT_TRUE(type.is_error()); + ASSERT_EQUAL(unsorted.size(), 2u); + EXPECT_EQUAL(unsorted[0].name, "x"); + EXPECT_EQUAL(unsorted[0].size, 10u); + EXPECT_EQUAL(unsorted[1].name, "x"); + EXPECT_EQUAL(unsorted[1].size, 5u); +} + +TEST("require that the unsorted dimension list can not be obtained if the parse itself fails") { + std::vector<ValueType::Dimension> unsorted; + auto type = ValueType::from_spec("tensor(x[10],x[5]", unsorted); + EXPECT_TRUE(type.is_error()); + EXPECT_EQUAL(unsorted.size(), 0u); +} + TEST("require that malformed value type spec is parsed as error") { EXPECT_TRUE(ValueType::from_spec("").is_error()); EXPECT_TRUE(ValueType::from_spec(" ").is_error()); diff --git a/eval/src/vespa/eval/eval/value_type.cpp b/eval/src/vespa/eval/eval/value_type.cpp index 688112bbb8a..211a9c305a3 100644 --- a/eval/src/vespa/eval/eval/value_type.cpp +++ b/eval/src/vespa/eval/eval/value_type.cpp @@ -260,6 +260,12 @@ ValueType::from_spec(const vespalib::string &spec) return value_type::from_spec(spec); } +ValueType +ValueType::from_spec(const vespalib::string &spec, std::vector<ValueType::Dimension> &unsorted) +{ + return value_type::from_spec(spec, unsorted); +} + vespalib::string ValueType::to_spec() const { diff --git a/eval/src/vespa/eval/eval/value_type.h b/eval/src/vespa/eval/eval/value_type.h index df9c582aee8..b02053be3cb 100644 --- a/eval/src/vespa/eval/eval/value_type.h +++ b/eval/src/vespa/eval/eval/value_type.h @@ -77,6 +77,7 @@ public: static ValueType double_type() { return ValueType(Type::DOUBLE); } static ValueType tensor_type(std::vector<Dimension> dimensions_in, CellType cell_type = CellType::DOUBLE); static ValueType from_spec(const vespalib::string &spec); + static ValueType from_spec(const vespalib::string &spec, std::vector<ValueType::Dimension> &unsorted); vespalib::string to_spec() const; static ValueType join(const ValueType &lhs, const ValueType &rhs); static CellType unify_cell_types(const ValueType &a, const ValueType &b); diff --git a/eval/src/vespa/eval/eval/value_type_spec.cpp b/eval/src/vespa/eval/eval/value_type_spec.cpp index bbfa6f4fa28..0246800ca2a 100644 --- a/eval/src/vespa/eval/eval/value_type_spec.cpp +++ b/eval/src/vespa/eval/eval/value_type_spec.cpp @@ -176,7 +176,8 @@ CellType parse_cell_type(ParseContext &ctx) { } // namespace vespalib::eval::value_type::<anonymous> ValueType -parse_spec(const char *pos_in, const char *end_in, const char *&pos_out) +parse_spec(const char *pos_in, const char *end_in, const char *&pos_out, + std::vector<ValueType::Dimension> *unsorted) { ParseContext ctx(pos_in, end_in, pos_out); vespalib::string type_name = parse_ident(ctx); @@ -188,6 +189,9 @@ parse_spec(const char *pos_in, const char *end_in, const char *&pos_out) ValueType::CellType cell_type = parse_cell_type(ctx); std::vector<ValueType::Dimension> list = parse_dimension_list(ctx); if (!ctx.failed()) { + if (unsorted != nullptr) { + *unsorted = list; + } return ValueType::tensor_type(std::move(list), cell_type); } } else { @@ -208,6 +212,18 @@ from_spec(const vespalib::string &spec) return type; } +ValueType +from_spec(const vespalib::string &spec, std::vector<ValueType::Dimension> &unsorted) +{ + const char *after = nullptr; + const char *end = spec.data() + spec.size(); + ValueType type = parse_spec(spec.data(), end, after, &unsorted); + if (after != end) { + return ValueType::error_type(); + } + return type; +} + vespalib::string to_spec(const ValueType &type) { diff --git a/eval/src/vespa/eval/eval/value_type_spec.h b/eval/src/vespa/eval/eval/value_type_spec.h index f2609f59f32..ff5113c769a 100644 --- a/eval/src/vespa/eval/eval/value_type_spec.h +++ b/eval/src/vespa/eval/eval/value_type_spec.h @@ -6,9 +6,11 @@ namespace vespalib::eval::value_type { -ValueType parse_spec(const char *pos_in, const char *end_in, const char *&pos_out); +ValueType parse_spec(const char *pos_in, const char *end_in, const char *&pos_out, + std::vector<ValueType::Dimension> *unsorted = nullptr); -ValueType from_spec(const vespalib::string &str); +ValueType from_spec(const vespalib::string &spec); +ValueType from_spec(const vespalib::string &spec, std::vector<ValueType::Dimension> &unsorted); vespalib::string to_spec(const ValueType &type); } |