Polyglot SQL API Documentation - v0.2.0
    Preparing search index...

    Function annotateTypes

    • Parse SQL and annotate the AST with inferred type information.

      Parses the given SQL and runs bottom-up type inference, populating the inferred_type field on value-producing AST nodes (columns, operators, functions, casts, etc.). Use ast.getInferredType(expr) to read the inferred type from any expression node.

      Parameters

      • sql: string

        The SQL string to parse and annotate

      • dialect: Dialect = Dialect.Generic

        The dialect to use for parsing (default: Generic)

      • Optionalschema: Schema

        Optional schema for column type resolution

      Returns AnnotateTypesResult

      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);
      }
      });
      }