summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 914a0b5)
raw | patch | inline | side by side (parent: 914a0b5)
author | Sebastian Harl <sh@tokkee.org> | |
Fri, 24 Oct 2014 15:51:03 +0000 (17:51 +0200) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Fri, 24 Oct 2014 15:51:03 +0000 (17:51 +0200) |
This function returns the expected type of the result of an arithmetic
expression based on the operator and input types.
expression based on the operator and input types.
src/core/data.c | patch | blob | history | |
src/include/core/data.h | patch | blob | history | |
t/unit/core/data_test.c | patch | blob | history |
diff --git a/src/core/data.c b/src/core/data.c
index 2994b5f50d3e18348dd183a553c719322e48002c..8754f8868a11471fe1e359cee75ac0d52b87721d 100644 (file)
--- a/src/core/data.c
+++ b/src/core/data.c
#include <math.h>
+/*
+ * Operator support maxtrix.
+ * <type1> <op> <type2> -> op_matrix[<op>][<type1>][<type2>]
+ */
+
+/* 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
*/
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)
{
index 7ff111c1f17395218874a5d954b554cc39d02628..9ab5cdabf50936d33f5a494a64456bad3502c6c9 100644 (file)
--- a/src/include/core/data.h
+++ b/src/include/core/data.h
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
index e8567b1ad61c1f2cb2c1a3cf0f72874a93cdfcc9..1129936f1184cea190907b4851be0ede710b60ad 100644 (file)
--- a/t/unit/core/data_test.c
+++ b/t/unit/core/data_test.c
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),
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;