From 419b318ff64a7d9048e9692dba7470eab9532d1f Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Fri, 24 Oct 2014 17:51:03 +0200 Subject: [PATCH] data: Added sdb_data_expr_type(). This function returns the expected type of the result of an arithmetic expression based on the operator and input types. --- src/core/data.c | 97 +++++++++++++++++++++++++++++++++++++++++ src/include/core/data.h | 17 ++++++++ t/unit/core/data_test.c | 18 ++++++++ 3 files changed, 132 insertions(+) diff --git a/src/core/data.c b/src/core/data.c index 2994b5f..8754f88 100644 --- a/src/core/data.c +++ b/src/core/data.c @@ -46,6 +46,77 @@ #include +/* + * Operator support maxtrix. + * -> op_matrix[][][] + */ + +/* add, sub, mul, div, mod, concat */ + +/* integer, decimal, string, datetime, binary, regex */ + +static int op_matrix[6][6][6] = { + /* SDB_DATA_ADD */ + { + { SDB_TYPE_INTEGER, -1, -1, -1, -1, -1 }, + { -1, SDB_TYPE_DECIMAL, -1, -1, -1, -1 }, + { -1, -1, -1, -1, -1, -1 }, + { -1, -1, -1, SDB_TYPE_DATETIME, -1, -1 }, + { -1, -1, -1, -1, -1, -1 }, + { -1, -1, -1, -1, -1, -1 }, + }, + + /* SDB_DATA_SUB */ + { + { SDB_TYPE_INTEGER, -1, -1, -1, -1, -1 }, + { -1, SDB_TYPE_DECIMAL, -1, -1, -1, -1 }, + { -1, -1, -1, -1, -1, -1 }, + { -1, -1, -1, SDB_TYPE_DATETIME, -1, -1 }, + { -1, -1, -1, -1, -1, -1 }, + { -1, -1, -1, -1, -1, -1 }, + }, + + /* SDB_DATA_MUL */ + { + { SDB_TYPE_INTEGER, -1, -1, SDB_TYPE_DATETIME, -1, -1 }, + { -1, SDB_TYPE_DECIMAL, -1, SDB_TYPE_DATETIME, -1, -1 }, + { -1, -1, -1, -1, -1, -1 }, + { SDB_TYPE_DATETIME, SDB_TYPE_DATETIME, -1, SDB_TYPE_DATETIME, -1, -1 }, + { -1, -1, -1, -1, -1, -1 }, + { -1, -1, -1, -1, -1, -1 }, + }, + + /* SDB_DATA_DIV */ + { + { SDB_TYPE_INTEGER, -1, -1, -1, -1, -1 }, + { -1, SDB_TYPE_DECIMAL, -1, -1, -1, -1 }, + { -1, -1, -1, -1, -1, -1 }, + { SDB_TYPE_DATETIME, SDB_TYPE_DATETIME, -1, SDB_TYPE_DATETIME, -1, -1 }, + { -1, -1, -1, -1, -1, -1 }, + { -1, -1, -1, -1, -1, -1 }, + }, + + /* SDB_DATA_MOD */ + { + { SDB_TYPE_INTEGER, -1, -1, -1, -1, -1 }, + { -1, SDB_TYPE_DECIMAL, -1, -1, -1, -1 }, + { -1, -1, -1, -1, -1, -1 }, + { SDB_TYPE_DATETIME, SDB_TYPE_DATETIME, -1, SDB_TYPE_DATETIME, -1, -1 }, + { -1, -1, -1, -1, -1, -1 }, + { -1, -1, -1, -1, -1, -1 }, + }, + + /* SDB_DATA_CONCAT */ + { + { -1, -1, -1, -1, -1, -1 }, + { -1, -1, -1, -1, -1, -1 }, + { -1, -1, SDB_TYPE_STRING, -1, -1, -1 }, + { -1, -1, -1, -1, -1, -1 }, + { -1, -1, -1, -1, SDB_TYPE_BINARY, -1 }, + { -1, -1, -1, -1, -1, -1 }, + }, +}; + /* * private helper functions */ @@ -624,6 +695,32 @@ sdb_data_expr_eval(int op, const sdb_data_t *d1, const sdb_data_t *d2, return -1; } /* sdb_data_expr_eval */ +int +sdb_data_expr_type(int op, int type1, int type2) +{ + int types_num = (int)SDB_STATIC_ARRAY_LEN(op_matrix[0]); + + assert(SDB_STATIC_ARRAY_LEN(op_matrix[0]) + == SDB_STATIC_ARRAY_LEN(op_matrix[0][0])); + + if ((op <= 0) || (SDB_STATIC_ARRAY_LEN(op_matrix) < (size_t)op)) + return -1; + + /* arrays only support concat; type has to match */ + if ((type1 & SDB_TYPE_ARRAY) || (type2 & SDB_TYPE_ARRAY)) { + if ((type1 != type2) || (op != SDB_DATA_CONCAT)) + return -1; + return type1; + } + if ((type1 < 0) || (types_num < type1) + || (type2 < 0) || (types_num < type2)) + return -1; + + if ((type1 == SDB_TYPE_NULL) || (type2 == SDB_TYPE_NULL)) + return SDB_TYPE_NULL; + return op_matrix[op - 1][type1 - 1][type2 - 1]; +} /* sdb_data_expr_type */ + size_t sdb_data_strlen(const sdb_data_t *datum) { diff --git a/src/include/core/data.h b/src/include/core/data.h index 7ff111c..9ab5cda 100644 --- a/src/include/core/data.h +++ b/src/include/core/data.h @@ -220,6 +220,23 @@ int sdb_data_expr_eval(int op, const sdb_data_t *d1, const sdb_data_t *d2, sdb_data_t *res); +/* + * sdb_data_expr_type: + * Determine the type of the expression when applying the specified operator + * to the specified types. Note that if an actual value is a typed NULL value + * (e.g. a NULL string value), the return value of this function does not + * match the return type of sdb_data_expr_eval. + * + * See the documentation of sdb_data_expr_eval() for a description of which + * operations are supported. + * + * Returns: + * - the type id on success + * - a negative value else + */ +int +sdb_data_expr_type(int op, int type1, int type2); + /* * sdb_data_strlen: * Returns a (worst-case) estimate for the number of bytes required to format diff --git a/t/unit/core/data_test.c b/t/unit/core/data_test.c index e8567b1..1129936 100644 --- a/t/unit/core/data_test.c +++ b/t/unit/core/data_test.c @@ -1220,6 +1220,7 @@ START_TEST(test_expr_eval) for (j = 0; j < SDB_STATIC_ARRAY_LEN(tests); ++j) { sdb_data_t res; int check; + int type1, type2, type; char d1_str[64] = "", d2_str[64] = ""; sdb_data_format(&golden_data[i].d1, d1_str, sizeof(d1_str), @@ -1227,12 +1228,29 @@ START_TEST(test_expr_eval) sdb_data_format(&golden_data[i].d2, d2_str, sizeof(d2_str), SDB_DOUBLE_QUOTED); + type1 = golden_data[i].d1.type; + type2 = golden_data[i].d2.type; + if (sdb_data_isnull(&golden_data[i].d1)) + type1 = SDB_TYPE_NULL; + if (sdb_data_isnull(&golden_data[i].d2)) + type2 = SDB_TYPE_NULL; + type = sdb_data_expr_type(tests[j].op, type1, type2); + check = sdb_data_expr_eval(tests[j].op, &golden_data[i].d1, &golden_data[i].d2, &res); fail_unless((check == 0) == (tests[j].expected.type != -1), "sdb_data_expr_eval(%s, %s, %s) = %d; expected: %d", SDB_DATA_OP_TO_STRING(tests[j].op), d1_str, d2_str, check, tests[j].expected.type == -1 ? -1 : 0); + + fail_unless(tests[j].expected.type == type, + "sdb_data_expr_eval(%s, %s, %s) expected to evaluate " + "to type %d while sdb_data_expr_type(%d, %d, %d) " + "predicted type %d", SDB_DATA_OP_TO_STRING(tests[j].op), + d1_str, d2_str, tests[j].expected.type, + tests[j].op, golden_data[i].d1.type, + golden_data[i].d2.type, type); + if (tests[j].expected.type == -1) continue; -- 2.30.2