Skip to content

Conversation

@lmangani
Copy link
Collaborator

Basic macro for url function reading all the supported formats.

Parameters

  • url
  • format (JSON, CSV, PARQUET, TEXT, BLOB)

Usage

SELECT * FROM url('https://urleng.com/test', 'JSON')

Credits

Thanks @carlopi for the TABLE MACRO solution!

@lmangani lmangani marked this pull request as ready for review September 19, 2024 21:19
@lmangani lmangani merged commit 8c03d92 into main Sep 19, 2024
@lmangani lmangani deleted the url-function branch September 19, 2024 21:19
@carlopi
Copy link

carlopi commented Sep 19, 2024

Cool!

I guess you can make it slightly more robust like:

CREATE OR REPLACE MACRO url(url, format) AS TABLE (
    WITH "internal_JSON" as (SELECT * FROM read_json_auto(url))
        , "internal_CSV" as (SELECT * FROM read_csv(url))
      --- ....
         convert_format as (CASE WHEN format ILIKE 'json' THEN 'internal_JSON'
                             WHEN formal ILIKE 'csv' THEN 'internal_CSV'
                             ------ ....
                             ELSE error('Unsupported parameter, try CSV or JSON') END )
    FROM query_table(convert_format)
);
FROM url('some.csv', 'asdfasdfasdf');
Invalid Input Error: Unsupported parameter, try CSV or JSON

(error is another obscure but quite cool feature, that I am not sure if here is working as intended)

@lmangani
Copy link
Collaborator Author

Thanks for sharing this pattern: I had no idea the error feature existed this is really really cool!
Since this is an experimental extension with no critical mass I guess we can take the risk and adopt obscure tricks ;)

@lmangani
Copy link
Collaborator Author

@carlopi I actually can't make this work with the above example no matter how simple I keep it. Could you share a minimalistic working version? Or does this require 1.1.1?

NOTE: this query drives the Motherduck AI autofix feature nuts :)

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

Successfully merging this pull request may close these issues.

3 participants