Alan, thanks for your response, I'm still new at this.  Running it,
the part with refer had an issue, but I tinkered and got past that on
line #3.
I also added a print statement to see what the output looks like, but
there appears to be a hidden unmatched delimiter somewhere (??). I'm
digging around for it… i suppose it is in the regex?

(use 'clojure.java.io)
(require 'clojure.string)
;;(refer 'clojure.string :only [split])
(refer 'clojure.string :only '(split) )

(let [pattern #"case when (\\S+) in \\(([^)]+)\\) then (\\S+) as (\\S
+)"]
 (with-open [sql-in (reader "/Users/avram/Downloads/pg.sql")]
 (doseq [line (line-seq reader)]
 (let [[_ in-str then-str as-str]
   (re-groups (doto (re-matcher pattern line) .find)))
   in-list (split #"\\s+" in-str)]
;;now do whatever you want with in-list, then-str, as-str
  (print in-str)
))))

java.lang.Exception: Unmatched delimiter: )
  [Thrown class clojure.lang.LispReader$ReaderException]

Restarts:
 0: [QUIT] Quit to the SLIME top level
 1: [CAUSE1] Invoke debugger on cause  Unmatched delimiter: ) [Thrown
class java.lang.Exception]

Backtrace:
  0: clojure.lang.LispReader.read(LispReader.java:180)
  1: clojure.core$read.invoke(core.clj:2868)
  2: clojure.core$read.invoke(core.clj:2866)


I'll study this and tinker on..

Many thanks,
~Avram

On Oct 5, 2:10 pm, Alan <a...@malloys.org> wrote:
> (use 'clojure.java.io)
> (require 'clojure.string)
> (refer 'clojure.string :only [split])
>
> (let [pattern #"case when (\\S+) in \\(([^)]+)\\) then (\\S+) as (\\S
> +)"]
>   (with-open [sql-in (reader "/path/to/file")]
>     (doseq [line (line-seq reader)]
>       (let [[_ in-str then-str as-str]
>               (re-groups (doto (re-matcher pattern line) .find)))
>             in-list (split #"\\s+" in-str)]
>         ;;now do whatever you want with in-list, then-str, as-str
>         ))))
>
> Not tested at all, but this is how I'd approach it - read a bunch of
> lines, split them into useable chunks with a regex, and then process
> each line. The regex will probably need tuning for your data set if
> not every line looks exactly like your example, but hopefully you get
> the idea.
>
> On Oct 5, 11:45 am, Avram <aav...@me.com> wrote:
>
>
>
> > Hello Clojurians,
>
> > I'm a beginner with clojure, but feel I have a situation that macros
> > might be well-suited.
>
> > The problem description is that I have a few sql files with a couple
> > hundred lines of ANSI SQL.  These files contain many SQL statements
> > like the following:
>
> >    case when a in ('Q1', 'Q3', 'Q8', 'Q9') then textvar end as qv100,
>
> > The task here, is that I need to convert these ANSI SQL files to
> > something that can be run in Hive SQL.  My current belief is that Hive
> > SQL does not support "in" clauses (http://wiki.apache.org/hadoop/Hive/
> > LanguageManual/UDF), so I need to convert the above snippet to
> > explicit assignments like this:
>
> >    case when a='Q1' then textvar when a='Q3' then textvar when a='Q8'
> > then textvar when a='Q9' then textvar end as qv100,
>
> > Thus, for each value in the "in" clause there will be a "when a=value"
> > item.
>
> > So, I think I need a function or macro to read an sql file, identify
> > all "case when" statements in the file where there is also an "in"
> > clause, and translate the "in" clause to the explicit assignments, and
> > output a corrected Hive-ready sql file.  A complicating factor is that
> > since there are single-quotes embedded in each line of the "in" clause
> > statements, they likely need to be escaped a priori somehow.
>
> > Any thoughts, comments, solutions, or advice on how to tackle this
> > with clojure much appreciated.
> > Thanks in advance!
>
> > Regards,
> > ~Avram

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to