<commit_before>/*-----------------------------------------------------------------------------
 *
 * sql_serializer_dl.c
 *			  
 *		
 *		AUTHOR: lord_pretzel
 *
 *		
 *
 *-----------------------------------------------------------------------------
 */

#include "common.h"
#include "instrumentation/timing_instrumentation.h"
#include "mem_manager/mem_mgr.h"
#include "log/logger.h"

#include "sql_serializer/sql_serializer_oracle.h"
#include "model/node/nodetype.h"
#include "model/query_operator/query_operator.h"
#include "model/query_operator/operator_property.h"
#include "model/datalog/datalog_model.h"
#include "model/list/list.h"
#include "model/set/set.h"
#include "utility/string_utils.h"

static void datalogToStr(StringInfo str, Node *n, int indent);
static char *constToLB(Constant *c);


char *
serializeOperatorModelLB(Node *q)
{
    StringInfo str = makeStringInfo();

    //TODO change operators and functions in expressions to LogiQL equivalent
    // add a rule for query output in lb
    appendStringInfo(str,"%s","_(X,Y) <- _move(X,Y)");
	appendStringInfoString(str, ".\n");

    datalogToStr(str, q, 0);

    return str->data;
}

//TODO 1) sanitize constants (remove '' from strings), they have to be lower-case), sanitize rel names (lowercase), translate expressions (e.g., string concat to skolems)

static void
datalogToStr(StringInfo str, Node *n, int indent)
{
    if (n == NULL)
        return;

    switch(n->type)
    {
        case T_DLAtom:
        {
            DLAtom *a = (DLAtom *) n;
            int i = 1;
            int len = LIST_LENGTH(a->args);

            if (a->negated)
                appendStringInfoString(str, "! ");

            // make IDB predicate local
            if (DL_HAS_PROP(a,DL_IS_IDB_REL) || !DL_HAS_PROP(a,DL_IS_EDB_REL))
            	appendStringInfo(str, "%s(", CONCAT_STRINGS("_", a->rel));
            else
            	appendStringInfo(str, "%s(", a->rel);

            FOREACH(Node,arg,a->args)
            {
                datalogToStr(str, arg, indent);
                if (i++ < len)
                    appendStringInfoString(str, ",");
            }
            appendStringInfoString(str, ")");
        }
        break;
        case T_DLRule:
        {
            DLRule *r = (DLRule *) n;
            int i = 1;
            int len = LIST_LENGTH(r->body);

            indentString(str,indent);

            datalogToStr(str, (Node *) r->head, indent);
            appendStringInfoString(str, " <- ");
            FOREACH(Node,a,r->body)
            {
                datalogToStr(str, a, indent);
                if (i++ < len)
                    appendStringInfoString(str, ",");
            }

            appendStringInfoString(str, ".\n");
        }
        break;
        case T_DLComparison:
        {
            DLComparison *c = (DLComparison *) n;

            datalogToStr(str,getNthOfListP(c->opExpr->args, 0), indent);
            appendStringInfo(str, " %s ", c->opExpr->name);
            datalogToStr(str,getNthOfListP(c->opExpr->args, 1), indent);
//            appendStringInfo(str, "%s", exprToSQL((Node *) c->opExpr));
        }
        break;
        case T_DLVar:
        {
            DLVar *v = (DLVar *) n;

            appendStringInfo(str, "%s", v->name);
        }
        break;
        case T_DLProgram:
        {
            DLProgram *p = (DLProgram *) n;

            FOREACH(Node,f,p->facts)
            {
                datalogToStr(str,(Node *) f, 0);
            }
            FOREACH(Node,r,p->rules)
            {
                datalogToStr(str,(Node *) r, 0);
            }
        }
        break;
        case T_Constant:
            appendStringInfo(str, "%s",
                    constToLB((Constant *) n));
        break;
        // provenance
        case T_List:
        {
            List *l = (List *) n;
            FOREACH(Node,el,l)
            datalogToStr(str,el, indent + 4);
        }
        break;
        default:
        {
            if (IS_EXPR(n))
            {
            	char *result = NULL;
            	result = replaceSubstr(exprToSQL(n), " || ", " + ");
            	result = replaceSubstr(result, "'", "\"");
            	appendStringInfo(str, "%s", result);
            }
            else
                FATAL_LOG("should have never come here, datalog program should"
                        " not have nodes like this: %s",
                        beatify(nodeToString(n)));
        }
        break;
    }
}

static char *
constToLB(Constant *c)
{
    if (CONST_IS_NULL(c))
        return strdup("null");
    if (c->constType == DT_STRING)
    {
        return STRING_VALUE(c);
    }
    else
        return CONST_TO_STRING(c);
}

char *
serializeQueryLB(QueryOperator *q)
{
    FATAL_LOG("should never have ended up here");
    return NULL;
}


char *
quoteIdentifierLB (char *ident)
{
    return ident; //TODO
}
<commit_msg>fix for the rule added to output query result for lb<commit_after>/*-----------------------------------------------------------------------------
 *
 * sql_serializer_dl.c
 *			  
 *		
 *		AUTHOR: lord_pretzel
 *
 *		
 *
 *-----------------------------------------------------------------------------
 */

#include "common.h"
#include "instrumentation/timing_instrumentation.h"
#include "mem_manager/mem_mgr.h"
#include "log/logger.h"

#include "sql_serializer/sql_serializer_oracle.h"
#include "model/node/nodetype.h"
#include "model/query_operator/query_operator.h"
#include "model/query_operator/operator_property.h"
#include "model/datalog/datalog_model.h"
#include "model/list/list.h"
#include "model/set/set.h"
#include "utility/string_utils.h"

static void datalogToStr(StringInfo str, Node *n, int indent);
static char *constToLB(Constant *c);


char *
serializeOperatorModelLB(Node *q)
{
    StringInfo str = makeStringInfo();

    //TODO change operators and functions in expressions to LogiQL equivalent

    // add a rule to output query result in lb
    DLProgram *p = (DLProgram *) q;
	char *headPred = NULL;
	int i = 0;

	FOREACH(DLRule,a,p->rules)
	{
		if (i == 0)
			headPred = a->head->rel;

		i++;
	}

	appendStringInfo(str,"%s",CONCAT_STRINGS("_(X,Y) <- _", headPred, "(X,Y).\n"));
    datalogToStr(str, q, 0);

    return str->data;
}

//TODO 1) sanitize constants (remove '' from strings), they have to be lower-case), sanitize rel names (lowercase), translate expressions (e.g., string concat to skolems)

static void
datalogToStr(StringInfo str, Node *n, int indent)
{
    if (n == NULL)
        return;

    switch(n->type)
    {
        case T_DLAtom:
        {
            DLAtom *a = (DLAtom *) n;
            int i = 1;
            int len = LIST_LENGTH(a->args);

            if (a->negated)
                appendStringInfoString(str, "! ");

            // make IDB predicate local
            if (DL_HAS_PROP(a,DL_IS_IDB_REL) || !DL_HAS_PROP(a,DL_IS_EDB_REL))
            	appendStringInfo(str, "%s(", CONCAT_STRINGS("_", a->rel));
            else
            	appendStringInfo(str, "%s(", a->rel);

            FOREACH(Node,arg,a->args)
            {
                datalogToStr(str, arg, indent);
                if (i++ < len)
                    appendStringInfoString(str, ",");
            }
            appendStringInfoString(str, ")");
        }
        break;
        case T_DLRule:
        {
            DLRule *r = (DLRule *) n;
            int i = 1;
            int len = LIST_LENGTH(r->body);

            indentString(str,indent);

            datalogToStr(str, (Node *) r->head, indent);
            appendStringInfoString(str, " <- ");
            FOREACH(Node,a,r->body)
            {
                datalogToStr(str, a, indent);
                if (i++ < len)
                    appendStringInfoString(str, ",");
            }

            appendStringInfoString(str, ".\n");
        }
        break;
        case T_DLComparison:
        {
            DLComparison *c = (DLComparison *) n;

            datalogToStr(str,getNthOfListP(c->opExpr->args, 0), indent);
            appendStringInfo(str, " %s ", c->opExpr->name);
            datalogToStr(str,getNthOfListP(c->opExpr->args, 1), indent);
//            appendStringInfo(str, "%s", exprToSQL((Node *) c->opExpr));
        }
        break;
        case T_DLVar:
        {
            DLVar *v = (DLVar *) n;

            appendStringInfo(str, "%s", v->name);
        }
        break;
        case T_DLProgram:
        {
            DLProgram *p = (DLProgram *) n;

            FOREACH(Node,f,p->facts)
            {
                datalogToStr(str,(Node *) f, 0);
            }
            FOREACH(Node,r,p->rules)
            {
                datalogToStr(str,(Node *) r, 0);
            }
        }
        break;
        case T_Constant:
            appendStringInfo(str, "%s",
                    constToLB((Constant *) n));
        break;
        // provenance
        case T_List:
        {
            List *l = (List *) n;
            FOREACH(Node,el,l)
            datalogToStr(str,el, indent + 4);
        }
        break;
        default:
        {
            if (IS_EXPR(n))
            {
            	// replace string concat and single quote to plus and double for lb
            	char *result = NULL;
            	result = replaceSubstr(exprToSQL(n), " || ", " + ");
            	result = replaceSubstr(result, "'", "\"");
            	appendStringInfo(str, "%s", result);
            }
            else
                FATAL_LOG("should have never come here, datalog program should"
                        " not have nodes like this: %s",
                        beatify(nodeToString(n)));
        }
        break;
    }
}

static char *
constToLB(Constant *c)
{
    if (CONST_IS_NULL(c))
        return strdup("null");
    if (c->constType == DT_STRING)
    {
        return STRING_VALUE(c);
    }
    else
        return CONST_TO_STRING(c);
}

char *
serializeQueryLB(QueryOperator *q)
{
    FATAL_LOG("should never have ended up here");
    return NULL;
}


char *
quoteIdentifierLB (char *ident)
{
    return ident; //TODO
}

{'fraction_non_alphanumeric': 0.15921982286794706, 'fraction_numerical': 0.0019902477858493384, 'mean_word_length': 1.6274509803921569, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 48, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4214167', 'n_tokens_mistral': 3232, 'n_tokens_neox': 3115, 'n_words': 651}