(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 <[email protected]> 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 [email protected]
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to