Fix segfault on printing cast/extract exprs

- Additionally, fix printing logic
This commit is contained in:
Ankush Rayabhari 2020-08-31 18:21:32 -07:00
parent 159c786cdb
commit b6dfe73051
3 changed files with 40 additions and 3 deletions

View File

@ -218,7 +218,7 @@ Expr* Expr::makeInOperator(Expr* expr, SelectStatement* select) {
} }
Expr* Expr::makeExtract(DatetimeField datetimeField, Expr* expr) { Expr* Expr::makeExtract(DatetimeField datetimeField, Expr* expr) {
Expr* e = new Expr(kExprFunctionRef); Expr* e = new Expr(kExprExtract);
e->name = strdup("EXTRACT"); e->name = strdup("EXTRACT");
e->datetimeField = datetimeField; e->datetimeField = datetimeField;
e->expr = expr; e->expr = expr;
@ -226,7 +226,7 @@ Expr* Expr::makeExtract(DatetimeField datetimeField, Expr* expr) {
} }
Expr* Expr::makeCast(Expr* expr, ColumnType columnType) { Expr* Expr::makeCast(Expr* expr, ColumnType columnType) {
Expr* e = new Expr(kExprFunctionRef); Expr* e = new Expr(kExprCast);
e->name = strdup("CAST"); e->name = strdup("CAST");
e->columnType = columnType; e->columnType = columnType;
e->expr = expr; e->expr = expr;

View File

@ -27,7 +27,8 @@ enum ExprType {
kExprHint, kExprHint,
kExprArray, kExprArray,
kExprArrayIndex, kExprArrayIndex,
kExprDatetimeField kExprExtract,
kExprCast
}; };
// Operator types. These are important for expressions of type kExprOperator. // Operator types. These are important for expressions of type kExprOperator.

View File

@ -10,6 +10,7 @@ namespace hsql {
void printAlias(Alias* alias, uintmax_t numIndent); void printAlias(Alias* alias, uintmax_t numIndent);
std::ostream& operator<<(std::ostream& os, const OperatorType& op); std::ostream& operator<<(std::ostream& os, const OperatorType& op);
std::ostream& operator<<(std::ostream& os, const DatetimeField& op);
std::string indent(uintmax_t numIndent) { std::string indent(uintmax_t numIndent) {
return std::string(numIndent, '\t'); return std::string(numIndent, '\t');
@ -32,6 +33,12 @@ namespace hsql {
void inprint(const OperatorType& op, uintmax_t numIndent) { void inprint(const OperatorType& op, uintmax_t numIndent) {
std::cout << indent(numIndent) << op << std::endl; std::cout << indent(numIndent) << op << std::endl;
} }
void inprint(const ColumnType& colType, uintmax_t numIndent) {
std::cout << indent(numIndent) << colType << std::endl;
}
void inprint(const DatetimeField& colType, uintmax_t numIndent) {
std::cout << indent(numIndent) << colType << std::endl;
}
void printTableRefInfo(TableRef* table, uintmax_t numIndent) { void printTableRefInfo(TableRef* table, uintmax_t numIndent) {
switch (table->type) { switch (table->type) {
@ -118,6 +125,16 @@ namespace hsql {
inprint(expr->name, numIndent); inprint(expr->name, numIndent);
for (Expr* e : *expr->exprList) printExpression(e, numIndent + 1); for (Expr* e : *expr->exprList) printExpression(e, numIndent + 1);
break; break;
case kExprExtract:
inprint(expr->name, numIndent);
inprint(expr->datetimeField, numIndent + 1);
printExpression(expr->expr, numIndent + 1);
break;
case kExprCast:
inprint(expr->name, numIndent);
inprint(expr->columnType, numIndent + 1);
printExpression(expr->expr, numIndent + 1);
break;
case kExprOperator: case kExprOperator:
printOperatorExpression(expr, numIndent); printOperatorExpression(expr, numIndent);
break; break;
@ -372,4 +389,23 @@ namespace hsql {
} }
} }
std::ostream& operator<<(std::ostream& os, const DatetimeField& datetime) {
static const std::map<const DatetimeField, const std::string> operatorToToken = {
{kDatetimeNone, "None"},
{kDatetimeSecond, "SECOND"},
{kDatetimeMinute, "MINUTE"},
{kDatetimeHour, "HOUR"},
{kDatetimeDay, "DAY"},
{kDatetimeMonth, "MONTH"},
{kDatetimeYear, "YEAR"}
};
const auto found = operatorToToken.find(datetime);
if (found == operatorToToken.cend()) {
return os << static_cast<uint64_t>(datetime);
} else {
return os << (*found).second;
}
}
} // namespace hsql } // namespace hsql