From f1f67e62cebb4144f0599196263cd93b41fa972e Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Mon, 6 Feb 2017 20:58:37 -0500 Subject: Made pronunciation::rhymes join dynamic This involved adding a new type of filter; one that compares (currently only equality and inequality) a field with another field located in an enclosing join context. In the process, it was discovered that simplifying the lemma::forms join field earlier actually made some queries return inaccurate results because the inflection of the form was being ignored and anything in the lemma would be used because of the inner join. Because the existing condition join did not allow for the condition field to be on the from side of the join, two things were done: a condition version of joinThrough was made, and lemma was finally eliminated as a top-level object, replaced instead with a condition join between word and form through lemmas_forms. Queries are also now grouped by the first select field (assumed to be the primary ID) of the top table, in order to eliminate duplicates created by inner joins, so that there is a uniform distribution between results for random queries. Created a database index on pronunciations(rhyme) which decreases query time for rhyming filters. The new database version is backwards-compatible because no data or structure changed. --- lib/filter.cpp | 201 +++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 166 insertions(+), 35 deletions(-) (limited to 'lib/filter.cpp') diff --git a/lib/filter.cpp b/lib/filter.cpp index ecff8ac..c201618 100644 --- a/lib/filter.cpp +++ b/lib/filter.cpp @@ -5,7 +5,6 @@ #include "word.h" #include "frame.h" #include "part.h" -#include "lemma.h" #include "form.h" #include "pronunciation.h" @@ -73,6 +72,14 @@ namespace verbly { break; } + + case comparison::field_equals: + case comparison::field_does_not_equal: + { + new(&singleton_.compareField) field(other.singleton_.compareField); + + break; + } } break; @@ -112,6 +119,7 @@ namespace verbly { std::string tempStringValue; int tempIntValue; bool tempBoolValue; + field tempCompareField; std::list tempChildren; bool tempOrlogic; @@ -173,6 +181,14 @@ namespace verbly { break; } + + case comparison::field_equals: + case comparison::field_does_not_equal: + { + tempCompareField = std::move(first.singleton_.compareField); + + break; + } } break; @@ -249,6 +265,14 @@ namespace verbly { break; } + + case comparison::field_equals: + case comparison::field_does_not_equal: + { + new(&first.singleton_.compareField) field(std::move(second.singleton_.compareField)); + + break; + } } break; @@ -325,6 +349,14 @@ namespace verbly { break; } + + case comparison::field_equals: + case comparison::field_does_not_equal: + { + new(&second.singleton_.compareField) field(std::move(tempCompareField)); + + break; + } } break; @@ -391,6 +423,14 @@ namespace verbly { break; } + + case comparison::field_equals: + case comparison::field_does_not_equal: + { + singleton_.compareField.~field(); + + break; + } } break; @@ -446,6 +486,8 @@ namespace verbly { case comparison::does_not_match: case comparison::hierarchally_matches: case comparison::does_not_hierarchally_match: + case comparison::field_equals: + case comparison::field_does_not_equal: { throw std::invalid_argument("Invalid comparison for integer field"); } @@ -490,6 +532,8 @@ namespace verbly { case comparison::does_not_match: case comparison::hierarchally_matches: case comparison::does_not_hierarchally_match: + case comparison::field_equals: + case comparison::field_does_not_equal: { throw std::invalid_argument("Invalid comparison for string field"); } @@ -534,6 +578,8 @@ namespace verbly { case comparison::does_not_match: case comparison::hierarchally_matches: case comparison::does_not_hierarchally_match: + case comparison::field_equals: + case comparison::field_does_not_equal: { throw std::invalid_argument("Invalid comparison for boolean field"); } @@ -576,6 +622,8 @@ namespace verbly { case comparison::does_not_match: case comparison::hierarchally_matches: case comparison::does_not_hierarchally_match: + case comparison::field_equals: + case comparison::field_does_not_equal: { throw std::invalid_argument("Incorrect constructor for given comparison"); } @@ -596,6 +644,7 @@ namespace verbly { case field::type::join: case field::type::join_where: case field::type::join_through: + case field::type::join_through_where: { switch (filterType) { @@ -624,6 +673,8 @@ namespace verbly { case comparison::is_not_null: case comparison::hierarchally_matches: case comparison::does_not_hierarchally_match: + case comparison::field_equals: + case comparison::field_does_not_equal: { throw std::invalid_argument("Incorrect constructor for given comparison"); } @@ -661,6 +712,8 @@ namespace verbly { case comparison::is_not_null: case comparison::matches: case comparison::does_not_match: + case comparison::field_equals: + case comparison::field_does_not_equal: { throw std::invalid_argument("Incorrect constructor for given comparison"); } @@ -679,6 +732,57 @@ namespace verbly { } } + filter::filter( + field filterField, + comparison filterType, + field compareField) : + type_(type::singleton) + { + switch (filterType) + { + case comparison::field_equals: + case comparison::field_does_not_equal: + { + if (filterField.getType() != compareField.getType()) + { + throw std::invalid_argument("Cannot compare two fields of different types"); + } + + if (filterField.isJoin()) + { + throw std::domain_error("Cannot compare join fields"); + } + + new(&singleton_.filterField) field(std::move(filterField)); + singleton_.filterType = filterType; + new(&singleton_.compareField) field(std::move(compareField)); + + break; + } + + case comparison::int_equals: + case comparison::int_does_not_equal: + case comparison::int_is_at_least: + case comparison::int_is_greater_than: + case comparison::int_is_at_most: + case comparison::int_is_less_than: + case comparison::boolean_equals: + case comparison::string_equals: + case comparison::string_does_not_equal: + case comparison::string_is_like: + case comparison::string_is_not_like: + case comparison::is_null: + case comparison::is_not_null: + case comparison::matches: + case comparison::does_not_match: + case comparison::hierarchally_matches: + case comparison::does_not_hierarchally_match: + { + throw std::domain_error("Incorrect constructor for given comparison"); + } + } + } + field filter::getField() const { if (type_ == type::singleton) @@ -726,6 +830,8 @@ namespace verbly { case comparison::boolean_equals: case comparison::is_null: case comparison::is_not_null: + case comparison::field_equals: + case comparison::field_does_not_equal: { throw std::domain_error("This filter does not have a join condition"); } @@ -762,6 +868,8 @@ namespace verbly { case comparison::does_not_match: case comparison::hierarchally_matches: case comparison::does_not_hierarchally_match: + case comparison::field_equals: + case comparison::field_does_not_equal: { throw std::domain_error("This filter does not have a string argument"); } @@ -798,6 +906,8 @@ namespace verbly { case comparison::does_not_match: case comparison::hierarchally_matches: case comparison::does_not_hierarchally_match: + case comparison::field_equals: + case comparison::field_does_not_equal: { throw std::domain_error("This filter does not have an integer argument"); } @@ -817,6 +927,47 @@ namespace verbly { } } + field filter::getCompareField() const + { + if (type_ != type::singleton) + { + throw std::domain_error("This filter does not have a compare field"); + } + + switch (singleton_.filterType) + { + case comparison::field_equals: + case comparison::field_does_not_equal: + { + return singleton_.compareField; + + break; + } + + case comparison::int_equals: + case comparison::int_does_not_equal: + case comparison::int_is_at_least: + case comparison::int_is_greater_than: + case comparison::int_is_at_most: + case comparison::int_is_less_than: + case comparison::boolean_equals: + case comparison::string_equals: + case comparison::string_does_not_equal: + case comparison::string_is_like: + case comparison::string_is_not_like: + case comparison::is_null: + case comparison::is_not_null: + case comparison::matches: + case comparison::does_not_match: + case comparison::hierarchally_matches: + case comparison::does_not_hierarchally_match: + { + throw std::domain_error("This filter doesn't have a compare field"); + } + } + + } + filter::filter(bool orlogic) : type_(type::group) { new(&group_.children) std::list(); @@ -970,6 +1121,16 @@ namespace verbly { { return filter(singleton_.filterField, comparison::hierarchally_matches, *singleton_.join); } + + case comparison::field_equals: + { + return filter(singleton_.filterField, comparison::field_does_not_equal, singleton_.compareField); + } + + case comparison::field_does_not_equal: + { + return filter(singleton_.filterField, comparison::field_equals, singleton_.compareField); + } } } @@ -1111,7 +1272,6 @@ namespace verbly { case object::word: case object::frame: case object::part: - case object::lemma: case object::form: case object::pronunciation: { @@ -1141,11 +1301,10 @@ namespace verbly { return (verbly::word::frames %= *this); } - case object::lemma: case object::form: case object::pronunciation: { - return (verbly::word::lemmas %= *this); + return (verbly::word::forms(inflection::base) %= *this); } } @@ -1161,7 +1320,6 @@ namespace verbly { case object::notion: case object::word: - case object::lemma: case object::form: case object::pronunciation: { @@ -1188,7 +1346,6 @@ namespace verbly { case object::notion: case object::word: case object::frame: - case object::lemma: case object::form: case object::pronunciation: { @@ -1197,32 +1354,6 @@ namespace verbly { } } - case object::lemma: - { - switch (singleton_.filterField.getObject()) - { - case object::notion: - case object::word: - case object::frame: - case object::part: - { - return verbly::lemma::words %= *this; - } - - case object::undefined: - case object::lemma: - { - return *this; - } - - case object::form: - case object::pronunciation: - { - return (verbly::lemma::forms(inflection::base) %= *this); - } - } - } - case object::form: { switch (singleton_.filterField.getObject()) @@ -1231,9 +1362,8 @@ namespace verbly { case object::word: case object::frame: case object::part: - case object::lemma: { - return verbly::form::lemmas %= *this; + return verbly::form::words(inflection::base) %= *this; } case object::undefined: @@ -1257,7 +1387,6 @@ namespace verbly { case object::word: case object::frame: case object::part: - case object::lemma: case object::form: { return verbly::pronunciation::forms %= *this; @@ -1354,6 +1483,8 @@ namespace verbly { case comparison::string_is_not_like: case comparison::is_null: case comparison::is_not_null: + case comparison::field_equals: + case comparison::field_does_not_equal: { result += std::move(normalized); -- cgit 1.4.1