From 12e788094bbafd08151ea23f726329a88a83ec1f Mon Sep 17 00:00:00 2001 From: Dimitri Fontaine Date: Tue, 14 May 2019 15:49:24 +0200 Subject: [PATCH] Improve sexp parser and standard symbols support. Also add split-sequence to the list of special cases that we can use and is not found in the pgloader.transforms package. Fixes #965. --- src/parsers/command-sexp.lisp | 24 +++++++++++++++----- test/Makefile | 1 + test/csv-using-sexp.load | 29 ++++++++++++++++++++++++ test/regress/expected/csv-using-sexp.out | 3 +++ 4 files changed, 51 insertions(+), 6 deletions(-) create mode 100644 test/csv-using-sexp.load create mode 100644 test/regress/expected/csv-using-sexp.out diff --git a/src/parsers/command-sexp.lisp b/src/parsers/command-sexp.lisp index 7115ef3..8fe7158 100644 --- a/src/parsers/command-sexp.lisp +++ b/src/parsers/command-sexp.lisp @@ -17,11 +17,23 @@ (defrule sexp-symbol (and (symbol-first-character-p character) (* (symbol-character-p character))) (:lambda (schars) - (pgloader.transforms:intern-symbol - (text schars) - '(("nil" . cl:nil) - ("precision" . pgloader.transforms::precision) - ("scale" . pgloader.transforms::scale))))) + (if (char= #\: (car schars)) + (read-from-string (text schars)) + (pgloader.transforms:intern-symbol + (text schars) + '(("nil" . cl:nil) + ("cl:t" . cl:t) + ("precision" . pgloader.transforms::precision) + ("scale" . pgloader.transforms::scale) + ("split-sequence" . split-sequence:split-sequence)))))) + +(defrule sexp-char (and #\# #\\ + (alpha-char-p character) + (+ (or (alpha-char-p character) + (digit-char-p character) + #\_))) + (:lambda (char-name) + (read-from-string (text char-name)))) (defrule sexp-string-char (or (not-doublequote character) (and #\\ #\"))) @@ -40,7 +52,7 @@ (cons car cdr))) (defrule sexp-atom (and ignore-whitespace - (or sexp-string sexp-integer sexp-symbol)) + (or sexp-char sexp-string sexp-integer sexp-symbol)) (:lambda (atom) (bind (((_ a) atom)) a))) diff --git a/test/Makefile b/test/Makefile index 6e8ebc2..1b7ae6d 100644 --- a/test/Makefile +++ b/test/Makefile @@ -20,6 +20,7 @@ REGRESS= allcols.load \ csv-nulls.load \ csv-temp.load \ csv-trim-extra-blanks.load \ + csv-using-sexp.load \ csv.load \ copy.load \ copy-hex.load \ diff --git a/test/csv-using-sexp.load b/test/csv-using-sexp.load new file mode 100644 index 0000000..4ad0295 --- /dev/null +++ b/test/csv-using-sexp.load @@ -0,0 +1,29 @@ +/* + * See https://github.com/dimitri/pgloader/issues/965 + */ +LOAD CSV + FROM INLINE(id,f1) + INTO postgresql:///pgloader + TARGET TABLE sexp + ( + id, + f1 text using (format nil "~{~a~^ ~}" + (split-sequence #\Space f1 :remove-empty-subseqs cl:t)) + ) + + WITH truncate, + fields terminated by ',' + + BEFORE LOAD DO + $$ drop table if exists sexp; $$, + $$ CREATE TABLE sexp + ( + id int, + f1 text + ) + $$; + + +1,Hello World +2,Hello World +2, foobar foobaz diff --git a/test/regress/expected/csv-using-sexp.out b/test/regress/expected/csv-using-sexp.out new file mode 100644 index 0000000..c643e81 --- /dev/null +++ b/test/regress/expected/csv-using-sexp.out @@ -0,0 +1,3 @@ +1 Hello World +2 Hello World +2 foobar foobaz