Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
MCOL-573 Allow restricted words usage with quotation(w/o ANSI_QUOTE s…
…ql mode yet).
  • Loading branch information
drrtuy committed Jan 17, 2018
1 parent d7c0075 commit 3d483e8
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 12 deletions.
15 changes: 8 additions & 7 deletions dbcon/ddlpackage/ddl.l
Expand Up @@ -33,7 +33,6 @@
using namespace ddlpackage;
int lineno = 1;
void ddlerror(struct pass_to_bison* x, char const *s);

static char* scanner_copy(char *str, yyscan_t yyscanner);

%}
Expand All @@ -58,19 +57,20 @@ comment ("--"{non_newline}*)
self [,()\[\].;\:\+\-\*\/\%\^\<\>\=]
whitespace ({space}+|{comment})

quote '
grave_accent `
digit [0-9]
ident_start [A-Za-z\200-\377_]
ident_cont [A-Za-z\200-\377_0-9\$]
identifier {ident_start}{ident_cont}*
identifer_quoted {grave_accent}{identifier}{grave_accent}

integer [-+]?{digit}+
decimal ([-+]?({digit}*\.{digit}+)|({digit}+\.{digit}*))
real ({integer}|{decimal})[Ee][-+]?{digit}+
realfail1 ({integer}|{decimal})[Ee]
realfail2 ({integer}|{decimal})[Ee][-+]

quote '
grave_accent `

%%

Expand Down Expand Up @@ -172,15 +172,12 @@ LONGTEXT {return LONGTEXT;}
}

{identifier} {ddlget_lval(yyscanner)->str = scanner_copy(ddlget_text(yyscanner), yyscanner); return IDENT;}
{identifer_quoted} {ddlget_lval(yyscanner)->str = scanner_copy(ddlget_text(yyscanner), yyscanner); return IDENT_QUOTED;}

{self} {
return ddlget_text(yyscanner)[0];
}

{grave_accent} {
/* ignore */
}

%%

void ddlerror(struct pass_to_bison* x, char const *s)
Expand All @@ -198,6 +195,10 @@ using namespace ddlpackage;
*/
void scanner_init(const char* str, yyscan_t yyscanner)
{
#ifdef YYDEBUG
extern int ddldebug;
ddldebug = 1;
#endif
size_t slen = strlen(str);
scan_data* pScanData = (scan_data*)ddlget_extra(yyscanner);

Expand Down
37 changes: 36 additions & 1 deletion dbcon/ddlpackage/ddl.y
Expand Up @@ -64,6 +64,7 @@ using namespace ddlpackage;
int ddllex(YYSTYPE* ddllval, void* yyscanner);
void ddlerror(struct pass_to_bison* x, char const *s);
char* copy_string(const char *str);
const char* removeQuotes(const char *ident);
%}

%expect 17
Expand Down Expand Up @@ -121,7 +122,7 @@ REFERENCES RENAME RESTRICT SET SMALLINT TABLE TEXT TIME TINYBLOB TINYTEXT
TINYINT TO UNIQUE UNSIGNED UPDATE USER SESSION_USER SYSTEM_USER VARCHAR VARBINARY
VARYING WITH ZONE DOUBLE IDB_FLOAT REAL CHARSET IDB_IF EXISTS CHANGE TRUNCATE

%token <str> IDENT FCONST SCONST CP_SEARCH_CONDITION_TEXT ICONST DATE
%token <str> IDENT_QUOTED IDENT FCONST SCONST CP_SEARCH_CONDITION_TEXT ICONST DATE

/* Notes:
* 1. "ata" stands for alter_table_action
Expand Down Expand Up @@ -612,11 +613,22 @@ table_name:

qualified_name:
IDENT '.' IDENT {$$ = new QualifiedName($1, $3);}
| IDENT '.' IDENT_QUOTED {$$ = new QualifiedName($1, $3); $$->removeQuotes(); }
| IDENT_QUOTED '.' IDENT {$$ = new QualifiedName($1, $3); $$->removeQuotes(); }
| IDENT_QUOTED '.' IDENT_QUOTED {$$ = new QualifiedName($1, $3); $$->removeQuotes(); }
| IDENT {
if (x->fDBSchema.size())
$$ = new QualifiedName((char*)x->fDBSchema.c_str(), $1);
else
$$ = new QualifiedName($1);
$$->removeQuotes();
}
| IDENT_QUOTED {
if (x->fDBSchema.size())
$$ = new QualifiedName((char*)x->fDBSchema.c_str(), $1);
else
$$ = new QualifiedName($1);
$$->removeQuotes();
}
;

Expand All @@ -633,6 +645,7 @@ ata_add_column:
column_name:
DATE
|IDENT
|IDENT_QUOTED { $$ = removeQuotes($1); }
;

constraint_name:
Expand Down Expand Up @@ -1145,4 +1158,26 @@ opt_column:

%%

const char* removeQuotes(const char *ident)
{
const char quote = '`';
size_t len = strlen(ident), i = 0;
char* newIdent = new char[len];
memset(newIdent, 0, len);

const char* oldIter = ident;
char* newIter = newIdent;

for(;i < len; i++)
{
if(oldIter[i] == quote)
continue;

*newIter = oldIter[i];
++newIter;
}

// what to do with the memory consumed by ident?
return newIdent;
}

34 changes: 33 additions & 1 deletion dbcon/ddlpackage/ddlpkg.cpp
Expand Up @@ -22,6 +22,7 @@
***********************************************************************/

#include <iostream>
#include <algorithm>

#define DDLPKG_DLLEXPORT
#include "ddlpkg.h"
Expand Down Expand Up @@ -49,6 +50,38 @@ QualifiedName::QualifiedName(const char* catalog, const char* schema, const char
{
}

void QualifiedName::removeQuotes()
{
//THD* thd = current_thd;
const char quote = '`';
string::iterator fNameBegin = fName.begin();
string::iterator fNameEnd = fName.end();

//if(thd->variables.sql_mode & MODE_ANSI_QUOTES)
// quote = '"';

if (count(fNameBegin, fNameEnd, quote) == 2 && *fNameBegin == quote && *(--fNameEnd) == quote)
{
string* fNameUpdated = new string(++fNameBegin, fNameEnd);
fName.swap(*fNameUpdated);
delete fNameUpdated;
}

if (fSchema.size())
{
string::iterator fSchemaBegin = fSchema.begin();
string::iterator fSchemaEnd = fSchema.end();

if (count(fSchemaBegin, fSchemaEnd, quote) == 2 && *fSchemaBegin == quote && *(--fSchemaEnd) == quote)
{
string* fSchemaUpdated = new string(++fSchemaBegin, fSchemaEnd);
fSchema.swap(*fSchemaUpdated);
delete fSchemaUpdated;
}
}

}

ostream& operator<<(ostream& os, const QualifiedName& qname)
{
if (!qname.fCatalog.empty())
Expand All @@ -61,7 +94,6 @@ ostream& operator<<(ostream& os, const QualifiedName& qname)
return os;
}


/** @brief Map a DECIMAL precision to data width in bytes */
unsigned int precision_width(unsigned p)
{
Expand Down
2 changes: 2 additions & 0 deletions dbcon/ddlpackage/ddlpkg.h
Expand Up @@ -511,6 +511,8 @@ struct QualifiedName
virtual ~QualifiedName()
{}

void removeQuotes();

std::string fCatalog;
std::string fName;
std::string fSchema;
Expand Down
1 change: 0 additions & 1 deletion dbcon/ddlpackage/sqlparser.h
Expand Up @@ -37,7 +37,6 @@

namespace ddlpackage
{

typedef SqlStatementList ParseTree;

/** @brief SqlParser is a class interface around the Bison parser
Expand Down
4 changes: 2 additions & 2 deletions dbcon/mysql/ha_calpont_ddl.cpp
Expand Up @@ -692,13 +692,13 @@ int ProcessDDLStatement(string& ddlStatement, string& schema, const string& tabl
{
CreateTableStatement* createTable = dynamic_cast <CreateTableStatement*> ( &stmt );

//@Bug 5767. To handle key words inside `` for a tablename.
// Restricted chars section. Should be useless since the backtick quotation commit.
if (!(boost::iequals(schema, createTable->fTableDef->fQualifiedName->fSchema)) || !(boost::iequals(table, createTable->fTableDef->fQualifiedName->fName)))
{
rc = 1;
thd->get_stmt_da()->set_overwrite_status(true);

thd->raise_error_printf(ER_CHECK_NOT_IMPLEMENTED, (IDBErrorInfo::instance()->errorMsg(ERR_CREATE_DATATYPE_NOT_SUPPORT)).c_str());
thd->raise_error_printf(ER_CHECK_NOT_IMPLEMENTED, (IDBErrorInfo::instance()->errorMsg(ERR_DML_INCORECT_IDENT)).c_str());
ci->alterTableState = cal_connection_info::NOT_ALTER;
ci->isAlter = false;
return rc;
Expand Down
1 change: 1 addition & 0 deletions utils/loggingcpp/ErrorMessage.txt
Expand Up @@ -33,6 +33,7 @@
1016 ERR_PARTITION_BY_RANGE The column type %1% is currently not supported in %2% function.
1017 ERR_SP_FUNCTION_NOT_SUPPORT Stored function is currently not supported in Columnstore.
1018 ERR_DBJ_ANTI_NULL Cannot currently process a disk-based antijoin with a function filter and a NULL join column in the large-side table.
1019 ERR_DML_INCORECT_IDENT Incorrect table or column identifier.

# Other errors ...
2001 ERR_JOIN_TOO_BIG Join or subselect exceeds memory limit.
Expand Down

0 comments on commit 3d483e8

Please sign in to comment.