← Back to team overview

maria-developers team mailing list archive

GSoC2015: MDEV-8356 patch for review

 

Hello all,

I am attaching a patch for MDEV-8356
<https://mariadb.atlassian.net/browse/MDEV-8356> as part of my work under
GSoC2015 implementing table valued functions.

Please feel free to point out mistakes, if any.

regards
Diwas Joshi
diff --git a/sql/sp.cc b/sql/sp.cc
index 9fd17b0..3413ba1 100644
--- a/sql/sp.cc
+++ b/sql/sp.cc
@@ -925,21 +925,66 @@ class Bad_db_error_handler : public Internal_error_handler
   bzero((char*) &share, sizeof(share));
   table.in_use= thd;
   table.s = &share;
-  field= sp->create_result_field(0, 0, &table);
-  field->sql_type(result);
-
-  if (field->has_charset())
+  if (sp->m_type == TYPE_ENUM_FUNCTION)
   {
-    result.append(STRING_WITH_LEN(" CHARSET "));
-    result.append(field->charset()->csname);
-    if (!(field->charset()->state & MY_CS_PRIMARY))
+    field= sp->create_result_field(0, 0, &table);
+    field->sql_type(result);
+
+    if (field->has_charset())
     {
-      result.append(STRING_WITH_LEN(" COLLATE "));
-      result.append(field->charset()->name);
+      result.append(STRING_WITH_LEN(" CHARSET "));
+      result.append(field->charset()->csname);
+      if (!(field->charset()->state & MY_CS_PRIMARY))
+      {
+        result.append(STRING_WITH_LEN(" COLLATE "));
+        result.append(field->charset()->name);
+      }
     }
+    delete field;
+  }
+  else if (sp->m_type == TYPE_ENUM_TABLE)
+  {
+    List_iterator_fast<Create_field> it(sp->m_cols_list);
+    Create_field *cols_field;
+    cols_field= it++;
+    result.append(STRING_WITH_LEN("TABLE "));
+    result.append(sp->m_table_alias.str);
+    result.append(STRING_WITH_LEN("("));
+    while (cols_field)
+    {
+      uint unused1= 0;
+      sp_prepare_create_field(thd, cols_field);
+      prepare_create_field(cols_field, &unused1, HA_CAN_GEOMETRY);
+      uint field_length;
+      Field *field;
+      String tmp_result(64);
+      field_length= cols_field->length;
+      cols_field->flags= 0;
+      field= make_field(table.s,                     /* TABLE_SHARE ptr */
+                          (uchar*) 0,                   /* field ptr */
+                          field_length,                 /* field [max] length */
+                          (uchar*) "",                  /* null ptr */
+                          0,                            /* null bit */
+                          cols_field->pack_flag,
+                          cols_field->sql_type,
+                          cols_field->charset,
+                          cols_field->geom_type, cols_field->srid,
+                          Field::NONE,                  /* unreg check */
+                          cols_field->interval,
+                          cols_field->field_name);
+      field->vcol_info= cols_field->vcol_info;
+      field->stored_in_db= cols_field->stored_in_db;
+      if (field)
+        field->init(&table);
+      field->sql_type(tmp_result);
+      result.append(tmp_result);
+      cols_field= it++;
+      if(cols_field)
+        result.append(STRING_WITH_LEN(", "));
+      delete field;
+    }
+    result.append(STRING_WITH_LEN(")"));
   }
-
-  delete field;
 }
 
 
@@ -1020,7 +1065,8 @@ class Bad_db_error_handler : public Internal_error_handler
   char definer_buf[USER_HOST_BUFF_SIZE];
   LEX_STRING definer;
   ulonglong saved_mode= thd->variables.sql_mode;
-  MDL_key::enum_mdl_namespace mdl_type= type == TYPE_ENUM_FUNCTION ?
+  MDL_key::enum_mdl_namespace mdl_type= type == (type == TYPE_ENUM_FUNCTION ||
+                                                 type == TYPE_ENUM_TABLE)  ?
                                         MDL_key::FUNCTION : MDL_key::PROCEDURE;
 
   CHARSET_INFO *db_cs= get_default_db_collation(thd, sp->m_db.str);
@@ -1036,7 +1082,8 @@ class Bad_db_error_handler : public Internal_error_handler
   retstr.set_charset(system_charset_info);
 
   DBUG_ASSERT(type == TYPE_ENUM_PROCEDURE ||
-              type == TYPE_ENUM_FUNCTION);
+              type == TYPE_ENUM_FUNCTION ||
+              type == TYPE_ENUM_TABLE);
 
   /* Grab an exclusive MDL lock. */
   if (lock_object_name(thd, mdl_type, sp->m_db.str, sp->m_name.str))
@@ -1117,7 +1164,8 @@ class Bad_db_error_handler : public Internal_error_handler
 
     store_failed= store_failed ||
       table->field[MYSQL_PROC_MYSQL_TYPE]->
-        store((longlong)type, TRUE);
+        store((longlong)(type == TYPE_ENUM_TABLE ?
+                         TYPE_ENUM_FUNCTION : type), TRUE);
 
     store_failed= store_failed ||
       table->field[MYSQL_PROC_FIELD_SPECIFIC_NAME]->
@@ -1145,7 +1193,7 @@ class Bad_db_error_handler : public Internal_error_handler
       table->field[MYSQL_PROC_FIELD_PARAM_LIST]->
         store(sp->m_params.str, sp->m_params.length, system_charset_info);
 
-    if (sp->m_type == TYPE_ENUM_FUNCTION)
+    if (sp->m_type == TYPE_ENUM_FUNCTION || sp->m_type == TYPE_ENUM_TABLE)
     {
       sp_returns_type(thd, retstr, sp);
 
@@ -1153,7 +1201,7 @@ class Bad_db_error_handler : public Internal_error_handler
         table->field[MYSQL_PROC_FIELD_RETURNS]->
           store(retstr.ptr(), retstr.length(), system_charset_info);
     }
-
+    String body_with_alias(64);
     store_failed= store_failed ||
       table->field[MYSQL_PROC_FIELD_BODY]->
         store(sp->m_body.str, sp->m_body.length, system_charset_info);
diff --git a/sql/sql_yacc.yy b/sql/sql_yacc.yy
index b53d939..d7d7d7b 100644
--- a/sql/sql_yacc.yy
+++ b/sql/sql_yacc.yy
@@ -16392,12 +16392,6 @@ sf_tail:
             Lex_input_stream *lip= YYLIP;
 
             lex->sphead->set_body_start(thd, lip->get_cpp_tok_start());
-            
-            if (Lex->sphead->m_type== TYPE_ENUM_TABLE) //to be removed later.
-            {
-              my_error(ER_NOT_SUPPORTED_YET, MYF(0), "Table functions");
-              MYSQL_YYABORT;
-            }
           }
           sp_proc_stmt /* $15 */
           {