diff options
author | Michael Widenius <monty@askmonty.org> | 2011-05-12 14:30:34 +0300 |
---|---|---|
committer | Michael Widenius <monty@askmonty.org> | 2011-05-12 14:30:34 +0300 |
commit | f09f1c7c7da344d0078b2c13edc9249c4c61c6b9 (patch) | |
tree | 1b707c9edf2d159102019147a5dbca390e1aac83 /sql/item_strfunc.cc | |
parent | f34be1893892745b5b1a7a099eab4ad8e9ac8641 (diff) | |
parent | 4c81cef75d7871e2c77d6723813ac328c34603b5 (diff) | |
download | mariadb-git-f09f1c7c7da344d0078b2c13edc9249c4c61c6b9.tar.gz |
Merge with dynamic column code
Diffstat (limited to 'sql/item_strfunc.cc')
-rw-r--r-- | sql/item_strfunc.cc | 775 |
1 files changed, 775 insertions, 0 deletions
diff --git a/sql/item_strfunc.cc b/sql/item_strfunc.cc index 148170110f5..e7e77fb58b6 100644 --- a/sql/item_strfunc.cc +++ b/sql/item_strfunc.cc @@ -3505,3 +3505,778 @@ String *Item_func_uuid::val_str(String *str) return str; } + + +Item_func_dyncol_create::Item_func_dyncol_create(List<Item> &args, + DYNCALL_CREATE_DEF *dfs) + : Item_str_func(args), defs(dfs), vals(0), nums(0) +{ + DBUG_ASSERT((args.elements & 0x1) == 0); // even number of arguments +} + + +bool Item_func_dyncol_create::fix_fields(THD *thd, Item **ref) +{ + bool res= Item_func::fix_fields(thd, ref); // no need Item_str_func here + vals= (DYNAMIC_COLUMN_VALUE *) alloc_root(thd->mem_root, + sizeof(DYNAMIC_COLUMN_VALUE) * + (arg_count / 2)); + nums= (uint *) alloc_root(thd->mem_root, + sizeof(uint) * (arg_count / 2)); + return res || vals == 0 || nums == 0; +} + + +void Item_func_dyncol_create::fix_length_and_dec() +{ + maybe_null= TRUE; + collation.set(&my_charset_bin); + decimals= 0; +} + +void Item_func_dyncol_create::prepare_arguments() +{ + char buff[STRING_BUFFER_USUAL_SIZE]; + String *res, tmp(buff, sizeof(buff), &my_charset_bin); + uint column_count= (arg_count / 2); + uint i; + my_decimal dtmp, *dres; + + /* get values */ + for (i= 0; i < column_count; i++) + { + uint valpos= i * 2 + 1; + DYNAMIC_COLUMN_TYPE type= defs[i].type; + if (type == DYN_COL_NULL) // auto detect + { + /* + We don't have a default here to ensure we get a warning if + one adds a new not handled MYSQL_TYPE_... + */ + switch (args[valpos]->field_type()) { + case MYSQL_TYPE_DECIMAL: + case MYSQL_TYPE_NEWDECIMAL: + type= DYN_COL_DECIMAL; + break; + case MYSQL_TYPE_TINY: + case MYSQL_TYPE_SHORT: + case MYSQL_TYPE_LONG: + case MYSQL_TYPE_LONGLONG: + case MYSQL_TYPE_INT24: + case MYSQL_TYPE_YEAR: + case MYSQL_TYPE_BIT: + type= args[valpos]->unsigned_flag ? DYN_COL_UINT : DYN_COL_INT; + break; + case MYSQL_TYPE_FLOAT: + case MYSQL_TYPE_DOUBLE: + type= DYN_COL_DOUBLE; + break; + case MYSQL_TYPE_NULL: + type= DYN_COL_NULL; + break; + case MYSQL_TYPE_TIMESTAMP: + case MYSQL_TYPE_DATETIME: + type= DYN_COL_DATETIME; + break; + case MYSQL_TYPE_DATE: + case MYSQL_TYPE_NEWDATE: + type= DYN_COL_DATE; + break; + case MYSQL_TYPE_TIME: + type= DYN_COL_TIME; + break; + case MYSQL_TYPE_VARCHAR: + case MYSQL_TYPE_ENUM: + case MYSQL_TYPE_SET: + case MYSQL_TYPE_TINY_BLOB: + case MYSQL_TYPE_MEDIUM_BLOB: + case MYSQL_TYPE_LONG_BLOB: + case MYSQL_TYPE_BLOB: + case MYSQL_TYPE_VAR_STRING: + case MYSQL_TYPE_STRING: + case MYSQL_TYPE_GEOMETRY: + type= DYN_COL_STRING; + break; + } + } + nums[i]= (uint) args[i * 2]->val_int(); + vals[i].type= type; + switch (type) { + case DYN_COL_NULL: + DBUG_ASSERT(args[valpos]->field_type() == MYSQL_TYPE_NULL); + break; + case DYN_COL_INT: + vals[i].long_value= args[valpos]->val_int(); + break; + case DYN_COL_UINT: + vals[i].ulong_value= args[valpos]->val_int(); + break; + case DYN_COL_DOUBLE: + vals[i].double_value= args[valpos]->val_real(); + break; + case DYN_COL_STRING: + res= args[valpos]->val_str(&tmp); + if (res && + (vals[i].string_value.str= my_strndup(res->ptr(), res->length(), + MYF(MY_WME)))) + { + vals[i].string_value.length= res->length(); + vals[i].charset= res->charset(); + } + else + { + args[valpos]->null_value= 1; // In case of out of memory + vals[i].string_value.str= NULL; + vals[i].string_value.length= 0; // just to be safe + } + break; + case DYN_COL_DECIMAL: + if ((dres= args[valpos]->val_decimal(&dtmp))) + { + dynamic_column_prepare_decimal(&vals[i]); + DBUG_ASSERT(vals[i].decimal_value.len == dres->len); + vals[i].decimal_value.intg= dres->intg; + vals[i].decimal_value.frac= dres->frac; + vals[i].decimal_value.sign= dres->sign(); + memcpy(vals[i].decimal_buffer, dres->buf, + sizeof(vals[i].decimal_buffer)); + } + else + { + dynamic_column_prepare_decimal(&vals[i]); // just to be safe + DBUG_ASSERT(args[valpos]->null_value); + } + break; + case DYN_COL_DATETIME: + args[valpos]->get_date(&vals[i].time_value, TIME_FUZZY_DATE); + break; + case DYN_COL_DATE: + args[valpos]->get_date(&vals[i].time_value, TIME_FUZZY_DATE); + break; + case DYN_COL_TIME: + args[valpos]->get_time(&vals[i].time_value); + break; + default: + DBUG_ASSERT(0); + vals[i].type= DYN_COL_NULL; + } + if (vals[i].type != DYN_COL_NULL && args[valpos]->null_value) + { + if (vals[i].type == DYN_COL_STRING) + my_free(vals[i].string_value.str, MYF(MY_ALLOW_ZERO_PTR)); + vals[i].type= DYN_COL_NULL; + } + } +} + +void Item_func_dyncol_create::cleanup_arguments() +{ + uint column_count= (arg_count / 2); + uint i; + + for (i= 0; i < column_count; i++) + { + if (vals[i].type == DYN_COL_STRING) + my_free(vals[i].string_value.str, MYF(MY_ALLOW_ZERO_PTR)); + } +} + +String *Item_func_dyncol_create::val_str(String *str) +{ + DYNAMIC_COLUMN col; + String *res; + uint column_count= (arg_count / 2); + enum enum_dyncol_func_result rc; + DBUG_ASSERT((arg_count & 0x1) == 0); // even number of arguments + + prepare_arguments(); + + if ((rc= dynamic_column_create_many(&col, column_count, nums, vals))) + { + dynamic_column_error_message(rc); + dynamic_column_column_free(&col); + res= NULL; + null_value= TRUE; + } + else + { + /* Move result from DYNAMIC_COLUMN to str_value */ + char *ptr; + size_t length, alloc_length; + dynamic_column_reassociate(&col, &ptr, &length, &alloc_length); + str_value.reassociate(ptr, (uint32) length, (uint32) alloc_length, + &my_charset_bin); + res= &str_value; + null_value= FALSE; + } + + /* cleanup */ + cleanup_arguments(); + + return res; +} + +void Item_func_dyncol_create::print_arguments(String *str, + enum_query_type query_type) +{ + uint i; + uint column_count= (arg_count / 2); + for (i= 0; i < column_count; i++) + { + args[i*2]->print(str, query_type); + str->append(','); + args[i*2 + 1]->print(str, query_type); + switch (defs[i].type) { + case DYN_COL_NULL: // automatic type => write nothing + break; + case DYN_COL_INT: + str->append(STRING_WITH_LEN(" AS int")); + break; + case DYN_COL_UINT: + str->append(STRING_WITH_LEN(" AS unsigned int")); + break; + case DYN_COL_DOUBLE: + str->append(STRING_WITH_LEN(" AS double")); + break; + case DYN_COL_STRING: + str->append(STRING_WITH_LEN(" AS char")); + if (defs[i].cs) + { + str->append(STRING_WITH_LEN(" charset ")); + str->append(defs[i].cs->csname); + str->append(' '); + } + break; + case DYN_COL_DECIMAL: + str->append(STRING_WITH_LEN(" AS decimal")); + break; + case DYN_COL_DATETIME: + str->append(STRING_WITH_LEN(" AS datetime")); + break; + case DYN_COL_DATE: + str->append(STRING_WITH_LEN(" AS date")); + break; + case DYN_COL_TIME: + str->append(STRING_WITH_LEN(" AS time")); + break; + } + if (i < column_count - 1) + str->append(','); + } +} + + +void Item_func_dyncol_create::print(String *str, + enum_query_type query_type) +{ + DBUG_ASSERT((arg_count & 0x1) == 0); // even number of arguments + str->append(STRING_WITH_LEN("column_create(")); + print_arguments(str, query_type); + str->append(')'); +} + + +String *Item_func_dyncol_add::val_str(String *str) +{ + DYNAMIC_COLUMN col; + String *res; + uint column_count= (arg_count / 2); + enum enum_dyncol_func_result rc; + DBUG_ASSERT((arg_count & 0x1) == 1); // odd number of arguments + + /* We store the packed data last */ + res= args[arg_count - 1]->val_str(str); + if (args[arg_count - 1]->null_value) + goto null; + init_dynamic_string(&col, NULL, res->length() + STRING_BUFFER_USUAL_SIZE, + STRING_BUFFER_USUAL_SIZE); + + col.length= res->length(); + memcpy(col.str, res->ptr(), col.length); + + prepare_arguments(); + + if ((rc= dynamic_column_update_many(&col, column_count, nums, vals))) + { + dynamic_column_error_message(rc); + dynamic_column_column_free(&col); + cleanup_arguments(); + goto null; + } + + { + /* Move result from DYNAMIC_COLUMN to str */ + char *ptr; + size_t length, alloc_length; + dynamic_column_reassociate(&col, &ptr, &length, &alloc_length); + str->reassociate(ptr, (uint32) length, (uint32) alloc_length, + &my_charset_bin); + null_value= FALSE; + } + + /* cleanup */ + dynamic_column_column_free(&col); + cleanup_arguments(); + + return str; + +null: + null_value= TRUE; + return NULL; +} + + +void Item_func_dyncol_add::print(String *str, + enum_query_type query_type) +{ + DBUG_ASSERT((arg_count & 0x1) == 1); // odd number of arguments + str->append(STRING_WITH_LEN("column_create(")); + args[arg_count - 1]->print(str, query_type); + str->append(','); + print_arguments(str, query_type); + str->append(')'); +} + + +/** + Get value for a column stored in a dynamic column + + @notes + This function ensures that null_value is set correctly +*/ + +bool Item_dyncol_get::get_dyn_value(DYNAMIC_COLUMN_VALUE *val, String *tmp) +{ + DYNAMIC_COLUMN dyn_str; + String *res; + longlong num; + enum enum_dyncol_func_result rc; + + num= args[1]->val_int(); + if (args[1]->null_value || num < 0 || num > INT_MAX) + { + null_value= 1; + return 1; + } + + res= args[0]->val_str(tmp); + if (args[0]->null_value) + { + null_value= 1; + return 1; + } + + dyn_str.str= (char*) res->ptr(); + dyn_str.length= res->length(); + if ((rc= dynamic_column_get(&dyn_str, (uint) num, val))) + { + dynamic_column_error_message(rc); + null_value= 1; + return 1; + } + + null_value= 0; + return 0; // ok +} + + +String *Item_dyncol_get::val_str(String *str_result) +{ + DYNAMIC_COLUMN_VALUE val; + char buff[STRING_BUFFER_USUAL_SIZE]; + String tmp(buff, sizeof(buff), &my_charset_bin); + + if (get_dyn_value(&val, &tmp)) + return NULL; + + switch (val.type) { + case DYN_COL_NULL: + goto null; + case DYN_COL_INT: + case DYN_COL_UINT: + str_result->set_int(val.long_value, test(val.type == DYN_COL_UINT), + &my_charset_latin1); + break; + case DYN_COL_DOUBLE: + str_result->set_real(val.double_value, NOT_FIXED_DEC, &my_charset_latin1); + break; + case DYN_COL_STRING: + if ((char*) tmp.ptr() <= val.string_value.str && + (char*) tmp.ptr() + tmp.length() >= val.string_value.str) + { + /* value is allocated in tmp buffer; We have to make a copy */ + str_result->copy(val.string_value.str, val.string_value.length, + val.charset); + } + else + { + /* + It's safe to use the current value because it's either pointing + into a field or in a buffer for another item and this buffer + is not going to be deleted during expression evaluation + */ + str_result->set(val.string_value.str, val.string_value.length, + val.charset); + } + break; + case DYN_COL_DECIMAL: + { + int res; + int length= + my_decimal_string_length((const my_decimal*)&val.decimal_value); + if (str_result->alloc(length)) + goto null; + if ((res= decimal2string(&val.decimal_value, (char*) str_result->ptr(), + &length, 0, 0, ' ')) != E_DEC_OK) + { + char buff[40]; + int len= sizeof(buff); + DBUG_ASSERT(length < (int)sizeof(buff)); + decimal2string(&val.decimal_value, buff, &len, 0, 0, ' '); + decimal_operation_results(res, buff, "CHAR"); + } + str_result->set_charset(&my_charset_latin1); + str_result->length(length); + break; + } + case DYN_COL_DATETIME: + case DYN_COL_DATE: + case DYN_COL_TIME: + { + int length; + if (str_result->alloc(MAX_DATE_STRING_REP_LENGTH) || + !(length= my_TIME_to_str(&val.time_value, (char*) str_result->ptr()))) + goto null; + str_result->set_charset(&my_charset_latin1); + str_result->length(length); + break; + } + } + return str_result; + +null: + null_value= TRUE; + return 0; +} + + +longlong Item_dyncol_get::val_int() +{ + DYNAMIC_COLUMN_VALUE val; + char buff[STRING_BUFFER_USUAL_SIZE]; + String tmp(buff, sizeof(buff), &my_charset_bin); + + if (get_dyn_value(&val, &tmp)) + return 0; + + switch (val.type) { + case DYN_COL_NULL: + goto null; + case DYN_COL_UINT: + unsigned_flag= 1; // Make it possible for caller to detect sign + return val.long_value; + case DYN_COL_INT: + unsigned_flag= 0; // Make it possible for caller to detect sign + return val.long_value; + case DYN_COL_DOUBLE: + { + bool error; + longlong num; + + num= double_to_longlong(val.double_value, unsigned_flag, &error); + if (error) + { + char buff[30]; + sprintf(buff, "%lg", val.double_value); + push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN, + ER_DATA_OVERFLOW, + ER(ER_DATA_OVERFLOW), + buff, + unsigned_flag ? "UNSIGNED INT" : "INT"); + } + return num; + } + case DYN_COL_STRING: + { + int error; + longlong num; + char *end= val.string_value.str + val.string_value.length, *org_end= end; + + num= my_strtoll10(val.string_value.str, &end, &error); + if (end != org_end || error > 0) + { + char buff[80]; + strmake(buff, val.string_value.str, min(sizeof(buff)-1, + val.string_value.length)); + push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN, + ER_BAD_DATA, + ER(ER_BAD_DATA), + buff, + unsigned_flag ? "UNSIGNED INT" : "INT"); + } + unsigned_flag= error >= 0; + return num; + } + case DYN_COL_DECIMAL: + { + longlong num; + my_decimal2int(E_DEC_FATAL_ERROR, &val.decimal_value, unsigned_flag, + &num); + return num; + } + case DYN_COL_DATETIME: + case DYN_COL_DATE: + case DYN_COL_TIME: + unsigned_flag= 1; + return TIME_to_ulonglong(&val.time_value); + } + +null: + null_value= TRUE; + return 0; +} + + +double Item_dyncol_get::val_real() +{ + DYNAMIC_COLUMN_VALUE val; + char buff[STRING_BUFFER_USUAL_SIZE]; + String tmp(buff, sizeof(buff), &my_charset_bin); + + if (get_dyn_value(&val, &tmp)) + return 0.0; + + switch (val.type) { + case DYN_COL_NULL: + goto null; + case DYN_COL_UINT: + return ulonglong2double(val.ulong_value); + case DYN_COL_INT: + return (double) val.long_value; + case DYN_COL_DOUBLE: + return (double) val.double_value; + case DYN_COL_STRING: + { + int error; + char *end; + double res= my_strntod(val.charset, (char*) val.string_value.str, + val.string_value.length, &end, &error); + + if (end != (char*) val.string_value.str + val.string_value.length || + error) + { + char buff[80]; + strmake(buff, val.string_value.str, min(sizeof(buff)-1, + val.string_value.length)); + push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN, + ER_BAD_DATA, + ER(ER_BAD_DATA), + buff, "DOUBLE"); + } + return res; + } + case DYN_COL_DECIMAL: + { + double res; + /* This will always succeed */ + decimal2double(&val.decimal_value, &res); + return res; + } + case DYN_COL_DATETIME: + case DYN_COL_DATE: + case DYN_COL_TIME: + return (ulonglong2double(TIME_to_ulonglong(&val.time_value)) + + val.time_value.second_part / (double) TIME_SUBSECOND_RANGE); + } + +null: + null_value= TRUE; + return 0.0; +} + + +my_decimal *Item_dyncol_get::val_decimal(my_decimal *decimal_value) +{ + DYNAMIC_COLUMN_VALUE val; + char buff[STRING_BUFFER_USUAL_SIZE]; + String tmp(buff, sizeof(buff), &my_charset_bin); + + if (get_dyn_value(&val, &tmp)) + return NULL; + + switch (val.type) { + case DYN_COL_NULL: + goto null; + case DYN_COL_UINT: + int2my_decimal(E_DEC_FATAL_ERROR, val.long_value, TRUE, decimal_value); + break; + case DYN_COL_INT: + int2my_decimal(E_DEC_FATAL_ERROR, val.long_value, FALSE, decimal_value); + break; + case DYN_COL_DOUBLE: + double2my_decimal(E_DEC_FATAL_ERROR, val.double_value, decimal_value); + break; + case DYN_COL_STRING: + { + int rc; + rc= str2my_decimal(0, val.string_value.str, val.string_value.length, + val.charset, decimal_value); + char buff[80]; + strmake(buff, val.string_value.str, min(sizeof(buff)-1, + val.string_value.length)); + if (rc != E_DEC_OK) + { + push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN, + ER_BAD_DATA, + ER(ER_BAD_DATA), + buff, "DECIMAL"); + } + break; + } + case DYN_COL_DECIMAL: + { + int length= STRING_BUFFER_USUAL_SIZE; + decimal2string(&val.decimal_value, buff, &length, 0,0, 0); + decimal2my_decimal(&val.decimal_value, decimal_value); + + break; + } + case DYN_COL_DATETIME: + case DYN_COL_DATE: + case DYN_COL_TIME: + { + double tmp= (ulonglong2double(TIME_to_ulonglong(&val.time_value)) + + val.time_value.second_part / (double) TIME_SUBSECOND_RANGE); + /* This can't overflow as time is always in the range of decimal */ + double2my_decimal(E_DEC_FATAL_ERROR, tmp, decimal_value); + break; + } + } + return decimal_value; + +null: + null_value= TRUE; + return 0; +} + + +bool Item_dyncol_get::get_date(MYSQL_TIME *ltime, uint fuzzy_date) +{ + DYNAMIC_COLUMN_VALUE val; + char buff[STRING_BUFFER_USUAL_SIZE]; + String tmp(buff, sizeof(buff), &my_charset_bin); + bool signed_value= 0; + + if (get_dyn_value(&val, &tmp)) + return 1; // Error + + switch (val.type) { + case DYN_COL_NULL: + goto null; + case DYN_COL_INT: + signed_value= 1; // For error message + /* fall_trough */ + case DYN_COL_UINT: + { + ulonglong num; + int error; + + num= val.ulong_value; + number_to_datetime(num, ltime, fuzzy_date, &error); + if (error) + { + char buff[65]; + int errnum= error == 2 ? ER_DATA_OVERFLOW : ER_BAD_DATA; + longlong2str(num, buff, signed_value ? -10 : 10, 1); + push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN, + errnum, + ER(errnum), + buff, "DATE or DATETIME"); + goto null; + } + return 0; + } + case DYN_COL_DOUBLE: + { + if (double_to_datetime_with_warn(val.double_value, ltime, fuzzy_date)) + goto null; + return 0; + } + case DYN_COL_DECIMAL: + if (decimal_to_datetime_with_warn(&val.decimal_value, ltime, fuzzy_date)) + goto null; + return 0; + case DYN_COL_STRING: + { + if (str_to_datetime_with_warn(val.string_value.str, + val.string_value.length, + ltime, fuzzy_date) <= MYSQL_TIMESTAMP_ERROR) + goto null; + return 0; + } + case DYN_COL_DATETIME: + case DYN_COL_DATE: + case DYN_COL_TIME: + *ltime= val.time_value; + return 0; + } + +null: + null_value= TRUE; + return 1; +} + + +void Item_dyncol_get::print(String *str, enum_query_type query_type) +{ + str->append(STRING_WITH_LEN("column_get(")); + args[0]->print(str, query_type); + str->append(','); + args[1]->print(str, query_type); + str->append(')'); +} + + +String *Item_func_dyncol_list::val_str(String *str) +{ + uint i; + enum enum_dyncol_func_result rc; + DYNAMIC_ARRAY arr; + DYNAMIC_COLUMN col; + String *res= args[0]->val_str(str); + + if (args[0]->null_value) + goto null; + col.length= res->length(); + /* We do not change the string, so could do this trick */ + col.str= (char *)res->ptr(); + if ((rc= dynamic_column_list(&col, &arr))) + { + dynamic_column_error_message(rc); + delete_dynamic(&arr); + goto null; + } + + /* + We support elements from 0 - 65536, so max size for one element is + 6 (including ,). + */ + if (str->alloc(arr.elements * 6)) + goto null; + + str->length(0); + for (i= 0; i < arr.elements; i++) + { + str->qs_append(*dynamic_element(&arr, i, uint*)); + if (i < arr.elements - 1) + str->qs_append(','); + } + + null_value= FALSE; + delete_dynamic(&arr); + return str; + +null: + null_value= TRUE; + return NULL; +} |