diff --git a/.travis.yml b/.travis.yml index 1ccd703..e622e12 100644 --- a/.travis.yml +++ b/.travis.yml @@ -29,5 +29,11 @@ compiler: script: - make cleanall - - make + - make -j4 + - make test + - make test_format + - make test_example + + # Test if benchmark can be built. + # - make build_benchmark diff --git a/Makefile b/Makefile index 4bf9c0e..cb55818 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,9 @@ -# directories +# Directories. BIN = bin SRC = src SRCPARSER = src/parser -# files +# Files. PARSERCPP = $(SRCPARSER)/bison_parser.cpp $(SRCPARSER)/flex_lexer.cpp LIBCPP = $(shell find $(SRC) -name '*.cpp' -not -path "$(SRCPARSER)/*") $(PARSERCPP) LIBOBJ = $(LIBCPP:%.cpp=%.o) @@ -11,44 +11,54 @@ TESTCPP = $(shell find test/ -name '*.cpp') ALLLIB = $(shell find $(SRC) -name '*.cpp' -not -path "$(SRCPARSER)/*") $(shell find $(SRC) -name '*.h' -not -path "$(SRCPARSER)/*") ALLTEST = $(shell find test/ -name '*.cpp') $(shell find test/ -name '*.h') +EXAMPLESRC = $(shell find example/ -name '*.cpp') $(shell find example/ -name '*.h') -# compile & link flages -CFLAGS = -std=c++11 -Wall -fPIC -g +# Compiler & linker flags. +CFLAGS = -std=c++11 -Wall -fPIC LIBFLAGS = -shared TARGET = libsqlparser.so INSTALL = /usr/local -CTESTFLAGS = -Wall -Isrc/ -Itest/ -L./ -std=c++11 -lstdc++ -g +CTESTFLAGS = -Wall -Isrc/ -Itest/ -L./ -std=c++11 -lstdc++ -O3 + +# Set compile mode to -g or -O3. +mode ?= release +ifeq ($(mode), debug) + CFLAGS += -g +else + CFLAGS += -O3 +endif + +GMAKE = make mode=$(mode) all: library library: $(TARGET) $(TARGET): $(LIBOBJ) + echo $(mode) $(CXX) $(LIBFLAGS) -o $(TARGET) $(LIBOBJ) -$(SRCPARSER)/flex_lexer.o: $(SRCPARSER)/flex_lexer.cpp +$(SRCPARSER)/flex_lexer.o: $(SRCPARSER)/flex_lexer.cpp $(SRCPARSER)/bison_parser.cpp $(CXX) $(CFLAGS) -c -o $@ $< -Wno-sign-compare -Wno-unneeded-internal-declaration -Wno-deprecated-register %.o: %.cpp $(PARSERCPP) $(CXX) $(CFLAGS) -c -o $@ $< $(SRCPARSER)/bison_parser.cpp: $(SRCPARSER)/bison_parser.y - make -C $(SRCPARSER)/ bison_parser.cpp + $(GMAKE) -C $(SRCPARSER)/ bison_parser.cpp $(SRCPARSER)/flex_lexer.cpp: $(SRCPARSER)/flex_lexer.l - make -C $(SRCPARSER)/ flex_lexer.cpp - -parser: - make -C $(SRCPARSER) all + $(GMAKE) -C $(SRCPARSER)/ flex_lexer.cpp clean: rm -f $(TARGET) rm -rf $(BIN) find $(SRC) -type f -name '*.o' -delete + $(GMAKE) -C benchmark/ clean cleanparser: - make -C $(SRCPARSER)/ clean + $(GMAKE) -C $(SRCPARSER)/ clean cleanall: clean cleanparser @@ -58,12 +68,15 @@ install: cp -r src $(INSTALL)/include/hsql find $(INSTALL)/include/hsql -not -name '*.h' -type f | xargs rm -format: - astyle --options=astyle.options $(ALLLIB) - astyle --options=astyle.options $(ALLTEST) +################# +### Benchmark ### +################# -run_benchmark: - make -C benchmark/ clean run +benchmark: library + $(GMAKE) -C benchmark/ clean run + +build_benchmark: library + $(GMAKE) -C benchmark/ parser_benchmark ############ ### Test ### @@ -72,11 +85,26 @@ run_benchmark: test: $(BIN)/sql_tests bash test/test.sh -# test whete -test_install: - make -C example/ - ./example/example "SELECT * FROM students WHERE name = 'Max Mustermann';" +test_example: + $(GMAKE) -C example/ + LD_LIBRARY_PATH=./ \ + ./example/example "SELECT * FROM students WHERE name = 'Max Mustermann';" + +test_format: + @! astyle --options=astyle.options $(ALLLIB) | grep -q "Formatted" + @! astyle --options=astyle.options $(ALLTEST) | grep -q "Formatted" $(BIN)/sql_tests: library @mkdir -p $(BIN)/ $(CXX) $(CTESTFLAGS) $(TESTCPP) -o $(BIN)/sql_tests -lsqlparser + + +############ +### Misc ### +############ + +format: + astyle --options=astyle.options $(ALLLIB) + astyle --options=astyle.options $(ALLTEST) + astyle --options=astyle.options $(EXAMPLESRC) + diff --git a/benchmark/Makefile b/benchmark/Makefile index a33b9d4..c5564a3 100644 --- a/benchmark/Makefile +++ b/benchmark/Makefile @@ -2,12 +2,13 @@ SRC = ./ CPP = $(shell find $(SRC) -name '*.cpp') -CFLAGS = -std=c++11 -lstdc++ -Wall -I../src/ -L../ +CFLAGS = -std=c++11 -lstdc++ -Wall -I../src/ -L../ -O3 all: parser_benchmark run: parser_benchmark - @export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:../ && ./parser_benchmark + @export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:../ &&\ + ./parser_benchmark parser_benchmark: $(CPP) $(CXX) $(CFLAGS) $(CPP) -o parser_benchmark -lbenchmark -lpthread -lsqlparser diff --git a/benchmark/parser_benchmark.cpp b/benchmark/parser_benchmark.cpp index cea4709..e42edda 100644 --- a/benchmark/parser_benchmark.cpp +++ b/benchmark/parser_benchmark.cpp @@ -10,12 +10,39 @@ #include "benchmark_utils.h" -PARSE_QUERY_BENCHMARK(BM_SimpleSelect, +PARSE_QUERY_BENCHMARK(BM_Q1SimpleSelect, "SELECT * FROM test;"); -PARSE_QUERY_BENCHMARK(BM_SimpleSubSelect, - "SELECT age, street AS address FROM (SELECT * FROM data);"); +PARSE_QUERY_BENCHMARK(BM_Q2SimpleSubSelect, + "SELECT a, b AS address FROM (SELECT * FROM test WHERE c < 100 AND b > 3) t1 WHERE a < 10 AND b < 100;"); +PARSE_QUERY_BENCHMARK(BM_Q3SingleJoin, + "SELECT \"left\".a, \"left\".b, \"right\".a, \"right\".b FROM table_a AS \"left\" JOIN table_b AS \"right\" ON \"left\".a = \"right\".a;"); + +PARSE_QUERY_BENCHMARK(BM_Q4TPCHQuery, +"SELECT" +" l_orderkey," +" SUM(l_extendedprice * (1 - l_discount)) AS revenue," +" o_orderdate," +" o_shippriority" +" FROM" +" customer," +" orders," +" lineitem" +" WHERE" +" c_mktsegment = '%s'" +" and c_custkey = o_custkey" +" and l_orderkey = o_orderkey" +" and o_orderdate < '%s'" +" and l_shipdate > '%s'" +" GROUP BY" +" l_orderkey," +" o_orderdate," +" o_shippriority" +" ORDER BY" +" revenue DESC," +" o_orderdate;" + ); PARSE_QUERY_BENCHMARK(BM_TwoSelects, "SELECT * FROM test; SELECT age, street AS address FROM data;"); diff --git a/example/example.cpp b/example/example.cpp index 9961d3d..7cf0817 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -8,34 +8,34 @@ // contains printing utilities #include "util/sqlhelper.h" -int main(int argc, char *argv[]) { - if (argc <= 1) { - fprintf(stderr, "Usage: ./example \"SELECT * FROM test;\"\n"); - return -1; - } - std::string query = argv[1]; - - // parse a given query - hsql::SQLParserResult result; - hsql::SQLParser::parseSQLString(query, &result); - - // check whether the parsing was successful - - if (result.isValid()) { - printf("Parsed successfully!\n"); - printf("Number of statements: %lu\n", result.size()); - - for (uint i = 0; i < result.size(); ++i) { - // Print a statement summary. - hsql::printStatementInfo(result.getStatement(i)); - } - return 0; - } else { - fprintf(stderr, "Given string is not a valid SQL query.\n"); - fprintf(stderr, "%s (L%d:%d)\n", - result.errorMsg(), - result.errorLine(), - result.errorColumn()); - return -1; +int main(int argc, char* argv[]) { + if (argc <= 1) { + fprintf(stderr, "Usage: ./example \"SELECT * FROM test;\"\n"); + return -1; + } + std::string query = argv[1]; + + // parse a given query + hsql::SQLParserResult result; + hsql::SQLParser::parseSQLString(query, &result); + + // check whether the parsing was successful + + if (result.isValid()) { + printf("Parsed successfully!\n"); + printf("Number of statements: %lu\n", result.size()); + + for (uint i = 0; i < result.size(); ++i) { + // Print a statement summary. + hsql::printStatementInfo(result.getStatement(i)); } + return 0; + } else { + fprintf(stderr, "Given string is not a valid SQL query.\n"); + fprintf(stderr, "%s (L%d:%d)\n", + result.errorMsg(), + result.errorLine(), + result.errorColumn()); + return -1; + } }