diff --git a/CHANGELOG.md b/CHANGELOG.md index ec690625..79745316 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,14 @@ +x.x.x Release notes (yyyy-MM-dd) +============================================================= +### Breaking changes +* None + +### Enhancements +* Support for queries comparing optional properties to `null` + +### Bugfixes +* None + 0.11.1 Release notes (2016-3-29) ============================================================= ### Bugfixes diff --git a/src/object-store/src/parser/parser.cpp b/src/object-store/src/parser/parser.cpp index 6819a954..6b981b3e 100644 --- a/src/object-store/src/parser/parser.cpp +++ b/src/object-store/src/parser/parser.cpp @@ -56,6 +56,7 @@ struct number : seq< minus, sor< float_num, hex_num, int_num > > {}; struct true_value : pegtl_istring_t("true") {}; struct false_value : pegtl_istring_t("false") {}; +struct null_value : pegtl_istring_t("null") {}; // key paths struct key_path : list< seq< sor< alpha, one< '_' > >, star< sor< alnum, one< '_', '-' > > > >, one< '.' > > {}; @@ -65,7 +66,7 @@ struct argument_index : plus< digit > {}; struct argument : seq< one< '$' >, must< argument_index > > {}; // expressions and operators -struct expr : sor< dq_string, sq_string, number, argument, true_value, false_value, key_path > {}; +struct expr : sor< dq_string, sq_string, number, argument, true_value, false_value, null_value, key_path > {}; struct case_insensitive : pegtl_istring_t("[c]"){}; struct eq : seq< sor< two< '=' >, one< '=' > >, opt< case_insensitive > >{}; @@ -236,6 +237,7 @@ EXPRESSION_ACTION(key_path, Expression::Type::KeyPath) EXPRESSION_ACTION(number, Expression::Type::Number) EXPRESSION_ACTION(true_value, Expression::Type::True) EXPRESSION_ACTION(false_value, Expression::Type::False) +EXPRESSION_ACTION(null_value, Expression::Type::Null) EXPRESSION_ACTION(argument_index, Expression::Type::Argument) template<> struct action< true_pred > diff --git a/src/object-store/src/parser/parser.hpp b/src/object-store/src/parser/parser.hpp index 2f2ebed2..1e8afef8 100644 --- a/src/object-store/src/parser/parser.hpp +++ b/src/object-store/src/parser/parser.hpp @@ -28,7 +28,7 @@ class Schema; namespace parser { struct Expression { - enum class Type { None, Number, String, KeyPath, Argument, True, False } type; + enum class Type { None, Number, String, KeyPath, Argument, True, False, Null } type; std::string s; Expression(Type t = Type::None, std::string s = "") : type(t), s(s) {} }; diff --git a/src/object-store/src/parser/query_builder.cpp b/src/object-store/src/parser/query_builder.cpp index 69eb8e54..6984af85 100644 --- a/src/object-store/src/parser/query_builder.cpp +++ b/src/object-store/src/parser/query_builder.cpp @@ -265,22 +265,6 @@ void add_link_constraint_to_query(realm::Query &query, } } -void add_link_constraint_to_query(realm::Query &query, - Predicate::Operator op, - PropertyExpression &prop_expr, - realm::null) { - precondition(prop_expr.indexes.empty(), "KeyPath queries not supported for object comparisons."); - switch (op) { - case Predicate::Operator::NotEqual: - query.Not(); - case Predicate::Operator::Equal: - query.and_query(query.get_table()->column(prop_expr.prop->table_column).is_null()); - break; - default: - throw std::runtime_error("Only 'equal' and 'not equal' operators supported for object comparison."); - } -} - auto link_argument(const PropertyExpression &propExpr, const parser::Expression &argExpr, Arguments &args) { return args.object_index_for_argument(stot(argExpr.s)); @@ -438,6 +422,103 @@ void do_add_comparison_to_query(Query &query, const Schema &schema, const Object } } } + +template +void do_add_null_comparison_to_query(Query &query, Predicate::Operator op, const PropertyExpression &expr, Arguments &args) +{ + Columns column = expr.table_getter()->template column(expr.prop->table_column); + switch (op) { + case Predicate::Operator::NotEqual: + query.and_query(column != realm::null()); + break; + case Predicate::Operator::Equal: + query.and_query(column == realm::null()); + break; + default: + throw std::runtime_error("Only 'equal' and 'not equal' operators supported when comparing against 'null'."); + } +} + +template<> +void do_add_null_comparison_to_query(Query &query, Predicate::Operator op, const PropertyExpression &expr, Arguments &args) +{ + precondition(expr.indexes.empty(), "KeyPath queries not supported for data comparisons."); + Columns column = expr.table_getter()->template column(expr.prop->table_column); + switch (op) { + case Predicate::Operator::NotEqual: + query.not_equal(expr.prop->table_column, realm::null()); + break; + case Predicate::Operator::Equal: + query.equal(expr.prop->table_column, realm::null()); + break; + default: + throw std::runtime_error("Only 'equal' and 'not equal' operators supported when comparing against 'null'."); + } +} + +template<> +void do_add_null_comparison_to_query(Query &query, Predicate::Operator op, const PropertyExpression &expr, Arguments &args) +{ + precondition(expr.indexes.empty(), "KeyPath queries not supported for object comparisons."); + switch (op) { + case Predicate::Operator::NotEqual: + // for not equal we negate the query and then fallthrough + query.Not(); + case Predicate::Operator::Equal: + query.and_query(query.get_table()->column(expr.prop->table_column).is_null()); + break; + default: + throw std::runtime_error("Only 'equal' and 'not equal' operators supported for object comparison."); + } +} + +void do_add_null_comparison_to_query(Query &query, const Schema &schema, const ObjectSchema &object_schema, Predicate::Comparison cmp, + const PropertyExpression &expr, Arguments &args) +{ + auto type = expr.prop->type; + switch (type) { + case PropertyTypeBool: + do_add_null_comparison_to_query(query, cmp.op, expr, args); + break; + case PropertyTypeDate: + do_add_null_comparison_to_query(query, cmp.op, expr, args); + break; + case PropertyTypeDouble: + do_add_null_comparison_to_query(query, cmp.op, expr, args); + break; + case PropertyTypeFloat: + do_add_null_comparison_to_query(query, cmp.op, expr, args); + break; + case PropertyTypeInt: + do_add_null_comparison_to_query(query, cmp.op, expr, args); + break; + case PropertyTypeString: + do_add_null_comparison_to_query(query, cmp.op, expr, args); + break; + case PropertyTypeData: + do_add_null_comparison_to_query(query, cmp.op, expr, args); + break; + case PropertyTypeObject: + do_add_null_comparison_to_query(query, cmp.op, expr, args); + break; + case PropertyTypeArray: + throw std::runtime_error((std::string)"Comparing Lists to 'null' is not supported"); + break; + default: { + throw std::runtime_error((std::string)"Object type " + string_for_property_type(type) + " not supported"); + } + } +} + +bool expression_is_null(const parser::Expression &expr, Arguments &args) { + if (expr.type == parser::Expression::Type::Null) { + return true; + } + else if (expr.type == parser::Expression::Type::Argument) { + return args.is_argument_null(stot(expr.s)); + } + return false; +} void add_comparison_to_query(Query &query, const Predicate &pred, Arguments &args, const Schema &schema, const std::string &type) { @@ -446,11 +527,21 @@ void add_comparison_to_query(Query &query, const Predicate &pred, Arguments &arg auto object_schema = schema.find(type); if (t0 == parser::Expression::Type::KeyPath && t1 != parser::Expression::Type::KeyPath) { PropertyExpression expr(query, schema, object_schema, cmpr.expr[0].s); - do_add_comparison_to_query(query, schema, *object_schema, cmpr, expr, expr, cmpr.expr[1], args); + if (expression_is_null(cmpr.expr[1], args)) { + do_add_null_comparison_to_query(query, schema, *object_schema, cmpr, expr, args); + } + else { + do_add_comparison_to_query(query, schema, *object_schema, cmpr, expr, expr, cmpr.expr[1], args); + } } else if (t0 != parser::Expression::Type::KeyPath && t1 == parser::Expression::Type::KeyPath) { PropertyExpression expr(query, schema, object_schema, cmpr.expr[1].s); - do_add_comparison_to_query(query, schema, *object_schema, cmpr, expr, cmpr.expr[0], expr, args); + if (expression_is_null(cmpr.expr[0], args)) { + do_add_null_comparison_to_query(query, schema, *object_schema, cmpr, expr, args); + } + else { + do_add_comparison_to_query(query, schema, *object_schema, cmpr, expr, cmpr.expr[0], expr, args); + } } else { throw std::runtime_error("Predicate expressions must compare a keypath and another keypath or a constant value"); diff --git a/src/object-store/tests/query.json b/src/object-store/tests/query.json index e4edd4de..6cbbee55 100644 --- a/src/object-store/tests/query.json +++ b/src/object-store/tests/query.json @@ -245,6 +245,9 @@ ["QueryCount", 1, "LinkObject", "linkCol == $0", [0, "linkCol"]], ["QueryCount", 1, "LinkObject", "$0 == linkCol", [1, "linkCol"]], ["QueryCount", 2, "LinkObject", "linkCol != $0", [0, "linkCol"]], + ["QueryCount", 1, "LinkObject", "linkCol = null"], + ["QueryCount", 2, "LinkObject", "linkCol != NULL"], + ["QueryCount", 1, "LinkObject", "linkCol = $0", null], ["QueryThrows", "LinkObject", "linkCol > $0", [0, "linkCol"]], ["QueryThrows", "LinkObject", "intCol = $0", [0, "linkCol"]] @@ -317,6 +320,62 @@ ["ObjectSet", [1], "LinkTypesObject", "linkLink.basicLink.floatCol == 0.1"], ["ObjectSet", [1, 3], "LinkTypesObject", "linkLink.basicLink.floatCol > 0"] ] +}, + +"optionalTests" : { + "schema" : [ + { + "name": "OptionalTypesObject", + "primaryKey": "primaryKey", + "properties": [ + { "name": "primaryKey", "type": "int" }, + { "name": "intCol", "type": "int", "optional": true }, + { "name": "floatCol", "type": "float", "optional": true }, + { "name": "doubleCol", "type": "double", "optional": true }, + { "name": "stringCol", "type": "string", "optional": true }, + { "name": "dateCol", "type": "date", "optional": true }, + { "name": "dataCol", "type": "data", "optional": true } + ] + }, + { + "name": "LinkTypesObject", + "primaryKey": "primaryKey", + "properties": [ + { "name": "primaryKey", "type": "int" }, + { "name": "basicLink", "type": "object", "objectType": "OptionalTypesObject" } + ] + }], + "objects": [ + { "type": "LinkTypesObject", "value": [0, [0, 1, 0.1, 0.001, "1", 1, [1, 10, 100]]] }, + { "type": "LinkTypesObject", "value": [1, [1, null, null, null, null, null, null]] } + ], + "tests": [ + ["ObjectSet", [1], "OptionalTypesObject", "intCol == null"], + ["ObjectSet", [1], "OptionalTypesObject", "null == intCol"], + ["ObjectSet", [0], "OptionalTypesObject", "intCol != null"], + ["ObjectSet", [1], "OptionalTypesObject", "floatCol == null"], + ["ObjectSet", [0], "OptionalTypesObject", "floatCol != null"], + ["ObjectSet", [1], "OptionalTypesObject", "doubleCol == null"], + ["ObjectSet", [0], "OptionalTypesObject", "doubleCol != null"], + ["ObjectSet", [1], "OptionalTypesObject", "stringCol == null"], + ["ObjectSet", [0], "OptionalTypesObject", "stringCol != null"], + ["ObjectSet", [1], "OptionalTypesObject", "dateCol == null"], + ["ObjectSet", [0], "OptionalTypesObject", "dateCol != null"], + ["ObjectSet", [1], "OptionalTypesObject", "dataCol == null"], + ["ObjectSet", [0], "OptionalTypesObject", "dataCol != null"], + + ["ObjectSet", [1], "LinkTypesObject", "basicLink.intCol == null"], + ["ObjectSet", [0], "LinkTypesObject", "basicLink.intCol != null"], + ["ObjectSet", [1], "LinkTypesObject", "basicLink.floatCol == null"], + ["ObjectSet", [0], "LinkTypesObject", "basicLink.floatCol != null"], + ["ObjectSet", [1], "LinkTypesObject", "basicLink.doubleCol == null"], + ["ObjectSet", [0], "LinkTypesObject", "basicLink.doubleCol != null"], + ["ObjectSet", [1], "LinkTypesObject", "basicLink.stringCol == null"], + ["ObjectSet", [0], "LinkTypesObject", "basicLink.stringCol != null"], + ["ObjectSet", [1], "LinkTypesObject", "basicLink.dateCol == null"], + ["ObjectSet", [0], "LinkTypesObject", "basicLink.dateCol != null"], + ["QueryThrows", "LinkTypesObject", "basicLink.dataCol == null"] + ] } } diff --git a/tests/js/query-tests.js b/tests/js/query-tests.js index c4cc4d57..3f5f2ace 100644 --- a/tests/js/query-tests.js +++ b/tests/js/query-tests.js @@ -146,6 +146,9 @@ module.exports = BaseTest.extend({ }, testKeyPathQueries: function() { runQuerySuite(testCases.keyPathTests); + }, + testOptionalQueries: function() { + runQuerySuite(testCases.optionalTests); } });