Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

View with column definition including "<expr> AS <alias>" (in SELECT) not parsed properly by converter #147

Open
JohnL4 opened this issue Nov 18, 2024 · 6 comments

Comments

@JohnL4
Copy link

JohnL4 commented Nov 18, 2024

Describe the bug
We have a view whose definition looks something like the following:

CREATE VIEW <schema>.<view> AS
SELECT DISTINCT
  MAX(RPT_DTTM) AS FILTER_RPT_DTTM
  ,'yes' as CRRNT_RPT_DTTM
FROM <tablename>
WHERE <expr>
UNION
<more horribleness>

I believe the regular expression parsing this view is greedy and treats the second AS keyword as the beginning of the view definition, thus eliding SELECT and everything that comes immediately after.

I suspect the problem is in the greediness of this regexp: https://github.com/littleK0i/SnowDDL/blame/c7ca3c48a533340bc88ac690b6664840005093ec/snowddl/converter/view.py#L11

I'm still fiddling around, but my suspicions are pretty strong at this point.

Expected behavior
Full text of the view definition appears in the yaml generated by snowddl-convert.

Attach log
(Later, if necessary.)

Attach YAML config (if applicable)
(No minimal reproduction at this time. Maybe later.)

@JohnL4 JohnL4 changed the title View with definition including View with column definition including "<expr> AS <alias" (in SELECT) not parsed properly by converter Nov 18, 2024
@littleK0i
Copy link
Owner

Wish we could find a way to extract VIEW text without relying on parsing. Maybe new REST API endpoints can do that, I'll take a look at some point.

Otherwise the only way to do it reliably is to introduce actual SQL parsing.

@JohnL4
Copy link
Author

JohnL4 commented Nov 19, 2024

Yeah, I hear you. Was thinking of that StackOverflow post on parsing HTML with regexps. (And, yes, parsing SQL is Hard.)

@JohnL4
Copy link
Author

JohnL4 commented Nov 19, 2024

Ok, so, for what it's worth, making the following change to the regexp solved my one particular issue for this one particular view:

view_text_re = compile(r"^.*?\sas(\n|\s)+(.*)$", DOTALL)

(Changed * operator to *?.)

No telling what other damage I've caused. :)

@JohnL4 JohnL4 changed the title View with column definition including "<expr> AS <alias" (in SELECT) not parsed properly by converter View with column definition including "<expr> AS <alias>" (in SELECT) not parsed properly by converter Nov 19, 2024
@littleK0i
Copy link
Owner

Upd in 0.36.0: applied your change, regexp looking for as keyword is now non-greedy.

Naturally, it may still fail if as is being used somewhere in the comments for individual columns or view itself.

I'll experiment with pyparsing soon and see if we can introduce more permanent solution.

@JohnL4
Copy link
Author

JohnL4 commented Nov 24, 2024

Fwiw, pyparsing docs:

ignore(expr) - function to specify parse expression to be ignored while matching defined patterns; can be called repeatedly to specify multiple expressions; useful to specify patterns of comment syntax, for example

@JohnL4
Copy link
Author

JohnL4 commented Dec 12, 2024

Per note on #149 , this can be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants