Skip to content

Commit 1a548db

Browse files
authored
Fix vector constant parsing. (#20)
1 parent 8fc4106 commit 1a548db

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

src/topk.cpp

+25-11
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,18 @@ void EndFaginsState(FaginsState *state){
552552
return;
553553
}
554554

555+
void parseVectorConstant(std::string &src) {
556+
std::size_t brace_search_pos = 0;
557+
while ((brace_search_pos = src.find("{", brace_search_pos)) != std::string::npos) {
558+
src.replace(brace_search_pos, 1, "\'{");
559+
brace_search_pos += 2;
560+
}
561+
brace_search_pos = 0;
562+
while ((brace_search_pos = src.find("}", brace_search_pos)) != std::string::npos) {
563+
src.replace(brace_search_pos, 1, "}\'");
564+
++brace_search_pos;
565+
}
566+
}
555567

556568
Datum topk(PG_FUNCTION_ARGS) {
557569

@@ -600,36 +612,38 @@ Datum topk(PG_FUNCTION_ARGS) {
600612

601613
std::string rank_exp(text_to_cstring(rank_expression_text));
602614
// DEBUG_PRINT("Setting Rank_exp %lu", rank_exp.size());
603-
if ( rank_exp.size() == 0 ){
615+
if (rank_exp.size() == 0) {
604616
int sz = orderby_expressions.size();
605-
for ( int i = 0; i < sz; i++ ) {
617+
for (int i = 0; i < sz; i++) {
606618
std::string orderby = orderby_expressions[i];
607-
rank_exp+=("("+orderby+")");
608-
if ( i < sz-1 ) rank_exp+=" + ";
619+
rank_exp += ("(" + orderby + ")");
620+
if (i < sz - 1) rank_exp += " + ";
609621
}
610622
}
611623

624+
parseVectorConstant(rank_exp);
625+
612626
DEBUG_PRINT("RANK Expresion = %s", rank_exp.c_str());
613627

614628
std::vector<IndexScanState*> scannodes;
615629
std::vector<QueryDesc*> qDescs;
616630
SPI_connect();
617631

618632
// prepare sql plans, each contain the corresponding index scan execution node for the orderby expression.
619-
for ( std::string exp : orderby_expressions) {
633+
for (std::string exp : orderby_expressions) {
634+
parseVectorConstant(exp);
620635
DEBUG_PRINT("Prepare IndexScan node for orderby_expresion: %s ", exp.c_str());
621636

622637
char sourceText[102400];
623638

624-
if (orderby_expressions.size() == 1)
625-
{
626-
if ( strlen(text_to_cstring(filter_exp_text)) == 0){
639+
if (orderby_expressions.size() == 1) {
640+
if (strlen(text_to_cstring(filter_exp_text)) == 0) {
627641
snprintf(sourceText, sizeof(sourceText), "select %s from %s order by %s", text_to_cstring(attr_exp_text), text_to_cstring(tablename), exp.c_str());
628642
} else {
629643
snprintf(sourceText, sizeof(sourceText), "select %s from %s where %s order by %s", text_to_cstring(attr_exp_text), text_to_cstring(tablename), text_to_cstring(filter_exp_text), exp.c_str());
630644
}
631-
} else{
632-
if ( strlen(text_to_cstring(filter_exp_text)) == 0){
645+
} else {
646+
if (strlen(text_to_cstring(filter_exp_text)) == 0) {
633647
snprintf(sourceText, sizeof(sourceText), "select %s, %s from %s order by %s", text_to_cstring(attr_exp_text), rank_exp.c_str(), text_to_cstring(tablename), exp.c_str());
634648
} else {
635649
snprintf(sourceText, sizeof(sourceText), "select %s, %s from %s where %s order by %s", text_to_cstring(attr_exp_text), rank_exp.c_str(), text_to_cstring(tablename), text_to_cstring(filter_exp_text), exp.c_str());
@@ -638,7 +652,7 @@ Datum topk(PG_FUNCTION_ARGS) {
638652

639653
PlannedStmt* plan = NULL;
640654

641-
if ( !(plan = extractIndexScanNode(sourceText)) ){
655+
if (!(plan = extractIndexScanNode(sourceText))){
642656
elog(ERROR, "cannot find index for order by expr = %s", sourceText);
643657
}
644658

0 commit comments

Comments
 (0)