Skip to content

Commit

Permalink
sql: remove field sql_default_value
Browse files Browse the repository at this point in the history
This patch removes the sql_default_value field from the struct field_def
and the sql_default_value_expr field from the struct tuple_field as they
are no longer needed.

Follow-up #8793

NO_DOC=refactoring
NO_TEST=refactoring
NO_CHANGELOG=refactoring
  • Loading branch information
ImeevMA authored and igormunkin committed Nov 23, 2023
1 parent 2e31751 commit 1f128c5
Show file tree
Hide file tree
Showing 8 changed files with 9 additions and 138 deletions.
10 changes: 0 additions & 10 deletions src/box/field_def.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ static const struct opt_def field_def_reg[] = {
OPT_DEF_ENUM("nullable_action", on_conflict_action, struct field_def,
nullable_action, NULL),
OPT_DEF("collation", OPT_UINT32, struct field_def, coll_id),
OPT_DEF("sql_default", OPT_STRPTR, struct field_def, sql_default_value),
OPT_DEF_ENUM("compression", compression_type, struct field_def,
compression_type, NULL),
OPT_DEF_CUSTOM("default", field_def_parse_default_value),
Expand All @@ -222,7 +221,6 @@ const struct field_def field_def_default = {
.is_nullable = false,
.nullable_action = ON_CONFLICT_ACTION_DEFAULT,
.coll_id = COLL_NONE,
.sql_default_value = NULL,
.default_value = NULL,
.default_value_size = 0,
.default_func_id = 0,
Expand Down Expand Up @@ -443,10 +441,6 @@ field_def_array_dup(const struct field_def *fields, uint32_t field_count)
grp_alloc_reserve_data(&all, sizeof(*fields) * field_count);
for (uint32_t i = 0; i < field_count; i++) {
grp_alloc_reserve_str0(&all, fields[i].name);
if (fields[i].sql_default_value != NULL) {
grp_alloc_reserve_str0(&all,
fields[i].sql_default_value);
}
grp_alloc_reserve_data(&all, fields[i].default_value_size);
}
grp_alloc_use(&all, xmalloc(grp_alloc_size(&all)));
Expand All @@ -455,10 +449,6 @@ field_def_array_dup(const struct field_def *fields, uint32_t field_count)
for (uint32_t i = 0; i < field_count; ++i) {
copy[i] = fields[i];
copy[i].name = grp_alloc_create_str0(&all, fields[i].name);
if (fields[i].sql_default_value != NULL) {
copy[i].sql_default_value = grp_alloc_create_str0(
&all, fields[i].sql_default_value);
}
if (fields[i].default_value != NULL) {
size_t size = fields[i].default_value_size;
char *buf = grp_alloc_create_data(&all, size);
Expand Down
2 changes: 0 additions & 2 deletions src/box/field_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,6 @@ struct field_def {
enum on_conflict_action nullable_action;
/** Collation ID for string comparison. */
uint32_t coll_id;
/** 0-terminated SQL expression for DEFAULT value. */
char *sql_default_value;
/** MsgPack with the default value. */
char *default_value;
/** Size of the default value. */
Expand Down
23 changes: 0 additions & 23 deletions src/box/sql.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,6 @@ static const char nil_key[] = { 0x90 }; /* Empty MsgPack array. */
static bool sql_seq_scan_default = false;
TWEAK_BOOL(sql_seq_scan_default);

static Expr *
sql_expr_compile_cb(const char *expr, int expr_len)
{
return sql_expr_compile(expr, expr_len);
}

static void
sql_expr_delete_cb(struct Expr *expr)
{
sql_expr_delete(expr);
}

uint32_t
sql_default_session_flags(void)
{
Expand All @@ -89,9 +77,6 @@ sql_default_session_flags(void)
void
sql_init(void)
{
tuple_format_expr_compile = sql_expr_compile_cb;
tuple_format_expr_delete = sql_expr_delete_cb;

current_session()->sql_flags = sql_default_session_flags();

if (sql_init_db(&db) != 0)
Expand Down Expand Up @@ -365,7 +350,6 @@ sql_ephemeral_space_new(const struct sql_space_info *info)
names += strlen(fields[i].name) + 1;
fields[i].is_nullable = true;
fields[i].nullable_action = ON_CONFLICT_ACTION_NONE;
fields[i].sql_default_value = NULL;
fields[i].default_value = NULL;
fields[i].default_value_size = 0;
fields[i].default_func_id = 0;
Expand Down Expand Up @@ -1012,12 +996,9 @@ sql_encode_table(struct region *region, struct space_def *def, uint32_t *size)
for (uint32_t i = 0; i < field_count && !is_error; i++) {
uint32_t cid = def->fields[i].coll_id;
struct field_def *field = &def->fields[i];
const char *default_str = field->sql_default_value;
int base_len = 4;
if (cid != COLL_NONE)
base_len += 1;
if (default_str != NULL)
base_len += 1;
if (field->default_value != NULL)
base_len += 1;
if (field->default_func_id != 0)
Expand Down Expand Up @@ -1056,10 +1037,6 @@ sql_encode_table(struct region *region, struct space_def *def, uint32_t *size)
mpstream_encode_str(&stream, "collation");
mpstream_encode_uint(&stream, cid);
}
if (default_str != NULL) {
mpstream_encode_str(&stream, "sql_default");
mpstream_encode_str(&stream, default_str);
}
if (field->default_value != NULL) {
mpstream_encode_str(&stream, "default");
mpstream_memcpy(&stream, field->default_value,
Expand Down
43 changes: 6 additions & 37 deletions src/box/sql/insert.c
Original file line number Diff line number Diff line change
Expand Up @@ -560,19 +560,11 @@ sqlInsert(Parse * pParse, /* Parser context */
}
if ((!useTempTable && !pList)
|| (pColumn && j >= pColumn->nId)) {
if (i == (int) autoinc_fieldno) {
sqlVdbeAddOp2(v, OP_Integer, -1,
regCols + i + 1);
} else {
struct tuple_field *field =
tuple_format_field(
space->format, i);
struct Expr *dflt =
field->sql_default_value_expr;
sqlExprCode(pParse,
dflt,
regCols + i + 1);
}
int reg = regCols + i + 1;
if (i == (int)autoinc_fieldno)
sqlVdbeAddOp2(v, OP_Integer, -1, reg);
else
sqlVdbeAddOp2(v, OP_Null, 0, reg);
} else if (useTempTable) {
sqlVdbeAddOp3(v, OP_Column, srcTab, j,
regCols + i + 1);
Expand Down Expand Up @@ -624,17 +616,7 @@ sqlInsert(Parse * pParse, /* Parser context */
}
if (j < 0 || nColumn == 0
|| (pColumn && j >= pColumn->nId)) {
if (i == (int) autoinc_fieldno) {
sqlVdbeAddOp2(v, OP_Null, 0, iRegStore);
continue;
}
struct tuple_field *field =
tuple_format_field(space->format, i);
struct Expr *dflt =
field->sql_default_value_expr;
sqlExprCodeFactorable(pParse,
dflt,
iRegStore);
sqlVdbeAddOp2(v, OP_Null, 0, iRegStore);
} else if (useTempTable) {
if (i == (int) autoinc_fieldno) {
int regTmp = ++pParse->nMem;
Expand Down Expand Up @@ -1091,19 +1073,6 @@ xferOptimization(Parse * pParse, /* Parser context */
if (!dest->def->fields[i].is_nullable &&
src->def->fields[i].is_nullable)
return 0;
/* Default values for second and subsequent columns need to match. */
if (i > 0) {
char *src_expr_str =
src->def->fields[i].sql_default_value;
char *dest_expr_str =
dest->def->fields[i].sql_default_value;
if ((dest_expr_str == NULL) != (src_expr_str == NULL) ||
(dest_expr_str &&
strcmp(src_expr_str, dest_expr_str) != 0)
) {
return 0; /* Default values must be the same for all columns */
}
}
}

for (uint32_t i = 0; i < dest->index_count; ++i) {
Expand Down
6 changes: 2 additions & 4 deletions src/box/sql/pragma.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,8 @@ sql_pragma_table_info(struct Parse *parse, const struct space *space)
k = key_def_find_by_fieldno(kdef, i) - kdef->parts + 1;
}
sqlVdbeMultiLoad(v, 1, "issisi", i, field->name,
field_type_strs[field->type],
!field->is_nullable,
field->sql_default_value,
k);
field_type_strs[field->type],
!field->is_nullable, NULL, k);
sqlVdbeAddOp2(v, OP_ResultRow, 1, 6);
}
}
Expand Down
29 changes: 0 additions & 29 deletions src/box/tuple_format.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ static intptr_t recycled_format_ids = FORMAT_ID_NIL;
static uint32_t formats_size = 0, formats_capacity = 0;
static uint64_t formats_epoch = 0;

tuple_format_expr_compile_f tuple_format_expr_compile;
tuple_format_expr_delete_f tuple_format_expr_delete;

/**
* Find in format1::fields the field by format2_field's JSON path.
* Routine uses fiber region for temporal path allocation and
Expand Down Expand Up @@ -118,21 +115,6 @@ tuple_format_cmp(const struct tuple_format *format1,
if (field_a->is_key_part != field_b->is_key_part)
return (int)field_a->is_key_part -
(int)field_b->is_key_part;
if (field_a->sql_default_value_expr !=
field_b->sql_default_value_expr) {
/*
* We cannot compare SQL expression ASTs, so as a
* workaround we compare pointers (which are actually
* never equal): since we need to provide some total
* order, we compare pointer values.
*/
uintptr_t ptr_a =
(uintptr_t)field_a->sql_default_value_expr;
uintptr_t ptr_b =
(uintptr_t)field_b->sql_default_value_expr;
intptr_t cmp = (intptr_t)(ptr_a - ptr_b);
return cmp < 0 ? -1 : 1;
}
if (field_a->compression_type != field_b->compression_type)
return (int)field_a->compression_type -
(int)field_b->compression_type;
Expand Down Expand Up @@ -184,8 +166,6 @@ tuple_format_hash(struct tuple_format *format)
TUPLE_FIELD_MEMBER_HASH(f, coll_id, h, carry, size)
TUPLE_FIELD_MEMBER_HASH(f, nullable_action, h, carry, size)
TUPLE_FIELD_MEMBER_HASH(f, is_key_part, h, carry, size)
TUPLE_FIELD_MEMBER_HASH(f, sql_default_value_expr, h, carry,
size)
TUPLE_FIELD_MEMBER_HASH(f, compression_type, h, carry, size);
for (uint32_t i = 0; i < f->constraint_count; ++i)
size += tuple_constraint_hash_process(&f->constraint[i],
Expand Down Expand Up @@ -241,8 +221,6 @@ tuple_field_delete(struct tuple_field *field)
for (uint32_t i = 0; i < field->constraint_count; i++)
field->constraint[i].destroy(&field->constraint[i]);
free(field->constraint);
if (field->sql_default_value_expr != NULL)
tuple_format_expr_delete(field->sql_default_value_expr);
field_default_func_destroy(&field->default_value.func);
free(field->default_value.data);
free(field);
Expand Down Expand Up @@ -555,13 +533,6 @@ tuple_format_create(struct tuple_format *format, struct key_def *const *keys,
tuple_constraint_array_new(fields[i].constraint_def,
fields[i].constraint_count);
field->constraint_count = fields[i].constraint_count;
const char *expr = fields[i].sql_default_value;
if (expr != NULL) {
field->sql_default_value_expr =
tuple_format_expr_compile(expr, strlen(expr));
if (field->sql_default_value_expr == NULL)
return -1;
}
if (fields[i].default_func_id > 0) {
field->default_value.func.id =
fields[i].default_func_id;
Expand Down
21 changes: 0 additions & 21 deletions src/box/tuple_format.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,27 +69,8 @@ struct tuple;
struct tuple_info;
struct tuple_format;
struct coll;
struct Expr;
struct mpstream;

typedef struct Expr *
(*tuple_format_expr_compile_f)(const char *expr, int expr_len);

typedef void
(*tuple_format_expr_delete_f)(struct Expr *expr);

/**
* Callback that compiles an SQL expression.
* Needed for avoid SQL dependency.
*/
extern tuple_format_expr_compile_f tuple_format_expr_compile;

/**
* Callback that deletes an SQL expression.
* Needed for avoid SQL dependency.
*/
extern tuple_format_expr_delete_f tuple_format_expr_delete;

/** Engine-specific tuple format methods. */
struct tuple_format_vtab {
/**
Expand Down Expand Up @@ -183,8 +164,6 @@ struct tuple_field {
struct tuple_constraint *constraint;
/** Number of constraints. */
uint32_t constraint_count;
/** AST for parsed SQL default value. */
struct Expr *sql_default_value_expr;
/** Tuple field default value. */
struct field_default_value default_value;
};
Expand Down
13 changes: 1 addition & 12 deletions test/unit/tuple_format.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ mpstream_error(void *is_err)
static int
test_tuple_format_cmp(void)
{
plan(19);
plan(18);
header();

char buf[1024];
Expand Down Expand Up @@ -124,17 +124,6 @@ test_tuple_format_cmp(void)
coll_id_delete(coll_id2);
coll_id_delete(coll_id1);

size = mp_format(buf, lengthof(buf), "[{%s%s %s%s}]",
"name", "f", "sql_default", "1 + 1");
f1 = runtime_tuple_format_new(buf, size, false);
size = mp_format(buf, lengthof(buf), "[{%s%s %s%s}]",
"name", "f", "sql_default", "2");
f2 = runtime_tuple_format_new(buf, size, false);
ok(f1 != f2, "tuple formats with different expressions in "
"'sql_default' definitions are not equal");
tuple_format_delete(f1);
tuple_format_delete(f2);

size = mp_format(buf, lengthof(buf), "[{%s%s %s{%s%d %s%d}}]",
"name", "f", "constraint", "c1", 1,
"c2", 2);
Expand Down

0 comments on commit 1f128c5

Please sign in to comment.