Skip to content

Commit

Permalink
now with sql
Browse files Browse the repository at this point in the history
  • Loading branch information
carlguo866 committed Nov 14, 2023
1 parent 0a26185 commit 0b85724
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 41 deletions.
4 changes: 3 additions & 1 deletion include/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
add_subdirectory(polygeist)
add_subdirectory(sql)
if (ENABLE_SQL)
add_subdirectory(sql)
endif()
2 changes: 0 additions & 2 deletions include/sql/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
//===----------------------------------------------------------------------===//




#ifndef SQLPARSER_H
#define SQLPARSER_H

Expand Down
23 changes: 23 additions & 0 deletions include/sql/SQLOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ def WhereOp: SQL_Op<"where", [Pure]> {
let hasCanonicalizer = 0;
}

def BoolArithOp: SQL_Op<"bool_arith", [Pure]> {
let summary = "bool_arith op";

let arguments = (ins SQLBoolType:$left, SQLBoolType:$right, StrAttr:$op);
let results = (outs SQLBoolType:$result);

let hasFolder = 0;
let hasCanonicalizer = 0;
}

def CalcBoolOp: SQL_Op<"calc_bool", [Pure]> {
let summary = "calc_bool op";

Expand All @@ -59,6 +69,19 @@ def CalcBoolOp: SQL_Op<"calc_bool", [Pure]> {
let hasCanonicalizer = 0;
}




def ArithOp: SQL_Op<"arith", [Pure]> {
let summary = "arith op";

let arguments = (ins SQLExprType:$left, SQLExprType:$right, StrAttr:$op);
let results = (outs SQLExprType:$result);

let hasFolder = 0;
let hasCanonicalizer = 0;
}

def AndOp: SQL_Op<"and", [Pure]> {
let summary = "and op";

Expand Down
4 changes: 3 additions & 1 deletion lib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
add_subdirectory(polygeist)
add_subdirectory(sql)
if (ENABLE_SQL)
add_subdirectory(sql)
endif()
3 changes: 2 additions & 1 deletion lib/sql/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Types.cpp
Dialect.cpp
Ops.cpp
Parser.cpp
NewParser.cpp


ADDITIONAL_HEADER_DIRS
Expand All @@ -13,7 +14,7 @@ MLIRSQLOpsIncGen
# MLIRSQLTypesIncGen

LINK_LIBS PUBLIC
MLIRIR
MLIRIR sqlparse_lib
)

add_subdirectory(Passes)
71 changes: 38 additions & 33 deletions test_with_pragma.c
Original file line number Diff line number Diff line change
@@ -1,52 +1,57 @@
#include <libpq-fe.h>
#include <stdio.h>
#include <stdlib.h>
#include <libpq-fe.h>

// PGresult *PQexec(PGconn*, const char* command);
// PQgetvalue
// %7 = call @PQexec(%2, %6) : (memref<?x1xi8>, memref<?xi8>) -> memref<?x1xi8>
#pragma lower_to(num_rows_fn, "sql.num_results")
int num_rows_fn(size_t);// char*
// %7 = call @PQexec(%2, %6) : (memref<?x1xi8>, memref<?xi8>) ->
// memref<?x1xi8>
// #pragma lower_to(num_rows_fn, "sql.num_results")
// int num_rows_fn(size_t);// char*

#pragma lower_to(get_value_fn_int, "sql.get_value")
int get_value_fn_int(size_t, int, int);
// #pragma lower_to(get_value_fn_int, "sql.get_value")
// int get_value_fn_int(size_t, int, int);

#pragma lower_to(get_value_fn_double, "sql.get_value")
double get_value_fn_double(size_t, int, int);
// #pragma lower_to(get_value_fn_double, "sql.get_value")
// double get_value_fn_double(size_t, int, int);

// #pragma lower_to(execute, "sql.execute")
// PGresult* execute(size_t, char*);

void do_exit(PGconn *conn) {
PQfinish(conn);
exit(1);
PQfinish(conn);
exit(1);
}

int main() {

PGconn *conn = PQconnectdb("user=janbodnar dbname=testdb");

if (PQstatus(conn) == CONNECTION_BAD) {

fprintf(stderr, "Connection to database failed: %s\n",
PQerrorMessage(conn));
do_exit(conn);
}

PGresult *res = PQexec(conn, "SELECT VERSION()");

if (PQresultStatus(res) != PGRES_TUPLES_OK) {
PGconn *conn = PQconnectdb("user=carl dbname=testdb");

printf("No data retrieved\n");
PQclear(res);
do_exit(conn);
}

printf("%s\n", PQgetvalue(res, 0, 0));
printf("%d\n", get_value_fn_int((size_t)res, 0, 0));
printf("%d\n", num_rows_fn((size_t)res));
// res, 0, 0));
if (PQstatus(conn) == CONNECTION_BAD) {

fprintf(stderr, "Connection to database failed: %s\n",
PQerrorMessage(conn));
do_exit(conn);
}

// PGresult *res = PQexec(conn, "SELECT 17");
PGresult *res = PQexec(conn, "SELECT a FROM table1");
PGresult *res1 = PQexec(conn, "SELECT * FROM table1 WHERE b > 10 OR c < 10 AND a <= 20");
PGresult *res2 = PQexec(conn, "SELECT * FROM table1 WHERE b > 10 AND c < 10");
PGresult *res3 = PQexec(conn, "SELECT b, c FROM table1 WHERE a <= 10 LIMIT 10");
// PGresult *res3 = PQexec(conn, "SELECT b, c FROM table1 LIMIT ALL");
if (PQresultStatus(res) != PGRES_TUPLES_OK) {

printf("No data retrieved\n");
PQclear(res);
PQfinish(conn);
do_exit(conn);
}

PQclear(res);
PQclear(res1);
PQclear(res2);
PQclear(res3);
PQfinish(conn);

return 0;
return 0;
}
8 changes: 5 additions & 3 deletions tools/cgeist/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ target_compile_definitions(cgeist PUBLIC -DLLVM_OBJ_ROOT="${LLVM_BINARY_DIR}")
target_link_libraries(cgeist PRIVATE
MLIRSCFTransforms
MLIRPolygeist
MLIRSQL

MLIRSupport
MLIRIR
MLIRAnalysis
Expand All @@ -77,7 +75,6 @@ target_link_libraries(cgeist PRIVATE
MLIRMathToLLVM
MLIRTargetLLVMIRImport
MLIRPolygeistTransforms
MLIRSQLTransforms
MLIRLLVMToLLVMIRTranslation
MLIRSCFToOpenMP
MLIROpenMPToLLVM
Expand Down Expand Up @@ -110,6 +107,11 @@ target_link_libraries(cgeist PRIVATE
clangLex
clangSerialization
)

if (ENABLE_SQL)
target_link_libraries(cgeist PRIVATE MLIRSQLTransforms MLIRSQL)
endif()

add_dependencies(cgeist MLIRPolygeistOpsIncGen MLIRPolygeistPassIncGen)
add_dependencies(cgeist MLIRSQLOpsIncGen MLIRSQLPassIncGen)
add_subdirectory(Test)
4 changes: 4 additions & 0 deletions tools/polygeist-opt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ set(LIBS
MLIRPolygeist
MLIRPolygeistTransforms
)
if (ENABLE_SQL)
set(LIBS ${LIBS} MLIRSQLTransforms MLIRSQL)
endif()

add_llvm_executable(polygeist-opt polygeist-opt.cpp)

install(TARGETS polygeist-opt
Expand Down

0 comments on commit 0b85724

Please sign in to comment.