From 6d79549d75fddac15175b4702a6f36ee4eab4dd8 Mon Sep 17 00:00:00 2001 From: Jeff Garzik Date: Tue, 13 Feb 2024 16:30:52 -0500 Subject: [PATCH 1/2] add extra tests to test suite --- Makefile.am | 12 +++++++++--- test-tst.sh | 20 ++++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) create mode 100755 test-tst.sh diff --git a/Makefile.am b/Makefile.am index c8cdcfa..3c8f4d2 100644 --- a/Makefile.am +++ b/Makefile.am @@ -12,7 +12,7 @@ pkgconfig_DATA = libsqlpars.pc # noinst_PROGRAMS = $(TESTS) EXTRA_DIST = sql.y sql.l \ pscan.js \ - test-ok.sh test-fail.sh \ + test-ok.sh test-tst.sh test-fail.sh \ fail1.sql \ fail3.sql \ fail4.sql \ @@ -27,7 +27,13 @@ EXTRA_DIST = sql.y sql.l \ ok7.sql \ ok8.sql \ ok9.sql \ - ok10.sql + ok10.sql \ + tst-create-db.sql \ + tst-create-table.sql \ + tst-delete.sql \ + tst-drop-db.sql \ + tst-drop-table.sql \ + tst-insert.sql sql_SOURCES = exec.c lib.c main.c sql-parser.h yyl.h sql_LDADD = @JANSSON_LIBS@ @@ -42,5 +48,5 @@ sql.tab.c sql.tab.h: sql.y Makefile sql.c sql.lex.h: sql.l Makefile ${LEX} -o $@ $< -TESTS = test-ok.sh test-fail.sh +TESTS = test-ok.sh test-fail.sh test-tst.sh diff --git a/test-tst.sh b/test-tst.sh new file mode 100755 index 0000000..8bdd85c --- /dev/null +++ b/test-tst.sh @@ -0,0 +1,20 @@ +#!/bin/sh + +TESTS=\ + $srcdir/tst-create-db.sql \ + $srcdir/tst-create-table.sql \ + $srcdir/tst-delete.sql \ + $srcdir/tst-drop-db.sql \ + $srcdir/tst-drop-table.sql \ + $srcdir/tst-insert.sql + +for testfn in $TESTS +do + cat $testfn | ./sql + if [ $? -ne 0 ] + then + echo "Failed on $testfn" + exit 1 + fi +done + From 247fd3deb382c9d6b238ff3717ecac2c4c6e8989 Mon Sep 17 00:00:00 2001 From: Jeff Garzik Date: Tue, 13 Feb 2024 16:50:26 -0500 Subject: [PATCH 2/2] Fix select-null edge cases. Contributed by @mewalig in #4. Fixes #4 --- Makefile.am | 5 ++++- exec.c | 10 ++++++++++ sql-parser.h | 2 ++ sql.y | 4 ++++ test-tst.sh | 10 +--------- tst-select-null.sql | 1 + tst-select-null2.sql | 1 + tst-select-null3.sql | 1 + 8 files changed, 24 insertions(+), 10 deletions(-) create mode 100644 tst-select-null.sql create mode 100644 tst-select-null2.sql create mode 100644 tst-select-null3.sql diff --git a/Makefile.am b/Makefile.am index 3c8f4d2..2efe8e6 100644 --- a/Makefile.am +++ b/Makefile.am @@ -33,7 +33,10 @@ EXTRA_DIST = sql.y sql.l \ tst-delete.sql \ tst-drop-db.sql \ tst-drop-table.sql \ - tst-insert.sql + tst-insert.sql \ + tst-select-null.sql \ + tst-select-null2.sql \ + tst-select-null3.sql sql_SOURCES = exec.c lib.c main.c sql-parser.h yyl.h sql_LDADD = @JANSSON_LIBS@ diff --git a/exec.c b/exec.c index d1d11d0..f610180 100644 --- a/exec.c +++ b/exec.c @@ -174,6 +174,11 @@ void sqlp_bool(struct psql_state *pstate, int val) boolout("BOOL", val); } +void sqlp_null(struct psql_state *pstate) +{ + strout("CONST", "NULL"); /* probably this should be a different func */ +} + void sqlp_call(struct psql_state *pstate, int n_args, const char *name) { json_t *obj = json_object(); @@ -397,6 +402,11 @@ void sqlp_expr_is_in(struct psql_state *pstate, int val) boolout("EXPR-IS-IN", val); } +void sqlp_expr_paren(struct psql_state *pstate) +{ + opout("PARENS"); /* probably this should be a different function */ +} + void sqlp_expr_op(struct psql_state *pstate, enum sqlp_expr_ops op) { strout("EXPR-OP", op_names[op]); diff --git a/sql-parser.h b/sql-parser.h index c78c34e..6e1d358 100644 --- a/sql-parser.h +++ b/sql-parser.h @@ -67,6 +67,7 @@ extern void sqlp_alias(struct psql_state *pstate, const char *alias); extern void sqlp_assign(struct psql_state *pstate, const char *db_name, const char *name); extern void sqlp_assign_at(struct psql_state *pstate, const char *name); extern void sqlp_bool(struct psql_state *pstate, int val); +extern void sqlp_null(struct psql_state *pstate); extern void sqlp_call(struct psql_state *pstate, int n_args, const char *name); extern void sqlp_call_date(struct psql_state *pstate, int n_args, enum sqlp_expr_ops op); extern void sqlp_call_trim_opts(struct psql_state *pstate, int trim_opts); @@ -99,6 +100,7 @@ extern void sqlp_delete_multi(struct psql_state *pstate, int opts, int n_del, in extern void sqlp_drop_db(struct psql_state *pstate, int if_exists, const char *name); extern void sqlp_drop_table(struct psql_state *pstate, int temp, int if_exists, int n_tables); extern void sqlp_enum_val(struct psql_state *pstate, const char *val); +extern void sqlp_expr_paren(struct psql_state *pstate); extern void sqlp_expr_op(struct psql_state *pstate, enum sqlp_expr_ops op); extern void sqlp_expr_cmp(struct psql_state *pstate, int comp); extern void sqlp_expr_cmp_sel(struct psql_state *pstate, int sel_type, int comp); diff --git a/sql.y b/sql.y index 350b695..43049db 100644 --- a/sql.y +++ b/sql.y @@ -850,6 +850,7 @@ expr: NAME { sqlp_name(pstate, $1); free($1); } | INTNUM { sqlp_number(pstate, $1); } | APPROXNUM { sqlp_float(pstate, $1); } | BOOL { sqlp_bool(pstate, $1); } + | NULLX { sqlp_null(pstate); } ; expr: expr '+' expr { sqlp_expr_op(pstate, SEO_ADD); } @@ -961,6 +962,9 @@ expr: CURRENT_TIMESTAMP { sqlp_now(pstate); }; expr: BINARY expr %prec UMINUS { sqlp_expr_op(pstate, SEO_STRTOBIN); } ; +expr: '(' expr ')' { sqlp_expr_paren(pstate); } + ; + %% void diff --git a/test-tst.sh b/test-tst.sh index 8bdd85c..ada404a 100755 --- a/test-tst.sh +++ b/test-tst.sh @@ -1,14 +1,6 @@ #!/bin/sh -TESTS=\ - $srcdir/tst-create-db.sql \ - $srcdir/tst-create-table.sql \ - $srcdir/tst-delete.sql \ - $srcdir/tst-drop-db.sql \ - $srcdir/tst-drop-table.sql \ - $srcdir/tst-insert.sql - -for testfn in $TESTS +for testfn in $srcdir/tst-*.sql do cat $testfn | ./sql if [ $? -ne 0 ] diff --git a/tst-select-null.sql b/tst-select-null.sql new file mode 100644 index 0000000..845c6fd --- /dev/null +++ b/tst-select-null.sql @@ -0,0 +1 @@ +select null from t; diff --git a/tst-select-null2.sql b/tst-select-null2.sql new file mode 100644 index 0000000..bac8db9 --- /dev/null +++ b/tst-select-null2.sql @@ -0,0 +1 @@ +select (1) from t; diff --git a/tst-select-null3.sql b/tst-select-null3.sql new file mode 100644 index 0000000..fdd4142 --- /dev/null +++ b/tst-select-null3.sql @@ -0,0 +1 @@ +select ((1+2) + (2+3)) from t;