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

Does duckdb-async support Async User-defined function ? #32

Open
unknowntpo opened this issue Apr 19, 2024 · 4 comments
Open

Does duckdb-async support Async User-defined function ? #32

unknowntpo opened this issue Apr 19, 2024 · 4 comments

Comments

@unknowntpo
Copy link

Hi, thanks for this amazing package,
when I'm playing around with user-defined function, I came across this issue.
The code snippet below only give me Promise, not the actual string.

async function fetchFromHttp() {
  const db = await Database.create(":memory:");

  db.register_udf('css_selector', 'string', (url) => {
    console.log(`fetching from url: ${url}`);
    const data = fetch(url)
      .then(res => res.text())
      .then(data => {        
        return data;
      });
    return data;
  });

  const rows = await db.all(`select css_selector('https://example.com/') as html`);
  await db.wait();

  console.log(rows);
}

await fetchFromHttp();
$ yarn start
yarn run v1.22.21
$ node src/index.js
fetching from url: https://example.com/
[ { html: '[object Promise]' } ]
@vinimdocarmo
Copy link

vinimdocarmo commented Jun 3, 2024

@unknowntpo your defined function is returning a promise instance instead of returning the promise resolved value.

Try marking your defined function as async and return await data; instead.

Just another 10 cents: you don't need the last .then in your chain. It's redundant.

It should look like this:

async function fetchFromHttp() {
  const db = await Database.create(":memory:");

  db.register_udf('css_selector', 'string', async (url) => {
    console.log(`fetching from url: ${url}`);
    const data = fetch(url)
      .then(res => res.text());
    return await data;
  });

  const rows = await db.all(`select css_selector('https://example.com/') as html`);
  await db.wait();

  console.log(rows);
}

await fetchFromHttp();

@unknowntpo
Copy link
Author

unknowntpo commented Jun 5, 2024

@vinimdocarmo

Just another 10 cents: you don't need the last .then in your chain. It's redundant.

This is correct, thank you !

But actually this code does not show me the actual html content, it just show me [object Promise], I try to use JSON.stringify but it gets the same result.

$ yarn start                                                                                                                                                                      (base) 08:48:25
yarn run v1.22.19
$ node src/index.js
fetching from url: https://example.com/
[
        {
                "html": "[object Promise]"
        }
]
✨  Done in 1.16s.

This is the code snippet.

https://github.com/unknowntpo/playground-2022/blob/50613cdcff617e6c265fd7c05e63d2986618dcfc/js/duckdb-example/src/index.js

@vinimdocarmo
Copy link

@unknowntpo I was checking DuckDB Node.js API and apparently it doesn't expect your udf callback to return a promise 😕 Which could explain why it doesn't resolve. See this code:

https://github.com/duckdb/duckdb-node/blob/main/lib/duckdb.js#L336

It doesn't await on your supplied callback function. I guess in this case you should open an Issue there for discussion. Maybe there's a good reason why they don't do that.

@unknowntpo
Copy link
Author

@vinimdocarmo Sorry for my late reply and thanks for your answer! That makes sense, and I'll open an new issue at duckdb-node.

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