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

AST to string causes double aliases #1426

Closed
peterbe opened this issue Feb 15, 2022 · 2 comments · Fixed by #1459
Closed

AST to string causes double aliases #1426

peterbe opened this issue Feb 15, 2022 · 2 comments · Fixed by #1459

Comments

@peterbe
Copy link

peterbe commented Feb 15, 2022

Steps to reproduce:

import alasql from "alasql";

const sql = `
SELECT title, LENGTH(title) AS length FROM ?
`.trim()
const ast = alasql.parse(sql);
console.log(ast.toString())
// Will output:
// SELECT title, LENGTH(title) AS length AS length FROM $0 AS default

Note the double AS length

Demo here:
https://codesandbox.io/s/sleepy-poincare-5t0jd?file=/src/App.tsx

@peterbe
Copy link
Author

peterbe commented Feb 15, 2022

For my own rushing needs, here's how I temporarily hacked around it :)

function fixDoubleAliases(sql: string): string {
  // Necessary because of https://github.com/agershun/alasql/issues/1426
  const dupes = new Set<string>();
  for (const match of sql.match(/\sAS \w+/g) || []) {
    if (dupes.has(match)) {
      sql = sql.replace(match, "");
    }
    dupes.add(match);
  }
  return sql;
}

@mathiasrw
Copy link
Member

Released as part of https://github.com/AlaSQL/alasql/releases/tag/v2.0.1

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

Successfully merging a pull request may close this issue.

2 participants