import { annotateTypes, ast, Dialect } from '@polyglot-sql/sdk';
const result = annotateTypes(
"SELECT a + b FROM t",
Dialect.PostgreSQL,
{ tables: { t: { a: "INT", b: "DOUBLE" } } }
);
if (result.success) {
// Walk the AST and inspect inferred types
ast.walk(result.ast![0], (node) => {
const dt = ast.getInferredType(node);
if (dt) {
console.log(ast.getExprType(node), "=>", dt);
}
});
}
Parse SQL and annotate the AST with inferred type information.
Parses the given SQL and runs bottom-up type inference, populating the
inferred_typefield on value-producing AST nodes (columns, operators, functions, casts, etc.). Useast.getInferredType(expr)to read the inferred type from any expression node.