{"record":{"id":"61616a0ecf1c1294","repo":"bytebase/bytebase","slug":"parse-61616a","errorCode":null,"errorMessage":"parse","messagePattern":"parse","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"backend/plugin/schema/pg/walk_through_loader.go","lineNumber":1200,"sourceCode":"\tsel, ok := node.(*ast.SelectStmt)\n\tif !ok {\n\t\treturn nil, errors.Errorf(\"default %q: expected SelectStmt\", expr)\n\t}\n\tif sel.TargetList == nil || len(sel.TargetList.Items) != 1 {\n\t\treturn nil, errors.Errorf(\"default %q: expected 1 target\", expr)\n\t}\n\trt, ok := sel.TargetList.Items[0].(*ast.ResTarget)\n\tif !ok {\n\t\treturn nil, errors.Errorf(\"default %q: expected ResTarget\", expr)\n\t}\n\treturn rt.Val, nil\n}\n\n// wtParseSelectBody parses a SQL string into *ast.SelectStmt.\nfunc wtParseSelectBody(sql string) (*ast.SelectStmt, error) {\n\tnodes, err := omniparser.Parse(sql)\n\tif err != nil {\n\t\treturn nil, errors.Wrap(err, \"parse\")\n\t}\n\tif nodes == nil || len(nodes.Items) != 1 {\n\t\treturn nil, errors.Errorf(\"expected 1 statement, got %d\", len(nodes.Items))\n\t}\n\tnode := nodes.Items[0]\n\tif raw, ok := node.(*ast.RawStmt); ok {\n\t\tnode = raw.Stmt\n\t}\n\tsel, ok := node.(*ast.SelectStmt)\n\tif !ok {\n\t\treturn nil, errors.Errorf(\"expected SelectStmt, got %T\", node)\n\t}\n\treturn sel, nil\n}\n\n// ---- type ref extraction (local, no import from parser/pg) ----\n\ntype wtUserTypeRef struct {","sourceCodeStart":1182,"sourceCodeEnd":1218,"githubUrl":"https://github.com/bytebase/bytebase/blob/1870550677fe08f0d2a78c07acd27541464eb945/backend/plugin/schema/pg/walk_through_loader.go#L1182-L1218","documentation":"wtParseSelectBody wraps the underlying omniparser.Parse failure in a Go error with the message 'parse'. It fires when the SQL text of a view or materialized-view definition cannot be tokenized/parsed at all (syntax error, truncated definition, or parser incompatibility).","triggerScenarios":"wtInstallView or wtInstallMatView passing a view's stored SQL body (from pg_get_viewdef or a dump) to omniparser.Parse and the parser returning a non-nil error — e.g. truncated or syntactically invalid view SQL.","commonSituations":"Round-tripping schemas across Postgres versions where view definitions contain version-specific syntax; corrupted metadata; custom functions/types unknown to the parser making the text unparseable.","solutions":["Print the wrapped cause (errors.Unwrap / %v of err) to see the exact syntax error and fix the view SQL accordingly","Re-dump the view definition from the source database (SELECT pg_get_viewdef) and verify it is complete","Check that the SQL does not rely on extensions or syntax unsupported by omniparser; simplify or register support","Upgrade the parser dependency if the view uses syntax newer than the parser supports"],"exampleFix":"-- before (truncated stored definition)\nSELECT * FROM orders WHERE\n-- after (complete definition)\nSELECT * FROM orders WHERE status = 'open';","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"sel, err := wtParseSelectBody(viewSQL)\nif err != nil {\n    var parseErr *base.ParseError\n    if errors.As(errors.Unwrap(err), &parseErr) {\n        return fmt.Errorf(\"view %q SQL invalid at line %d: %w\", name, parseErr.Line, err)\n    }\n    return err\n}","preventionTips":["Always store complete view definitions (verify with pg_get_viewdef)","Validate view SQL before saving it into metadata","Keep the omniparser dependency in sync with the target Postgres version","Log the full wrapped error chain, not just the 'parse' message"],"tags":["postgres","parser","sql-syntax","view"],"backgroundTag":"sql-query-failed","analyzedSha":"1870550677fe08f0d2a78c07acd27541464eb945","analyzedAt":"2026-09-06T21:16:13.665Z","contentChangedAt":"2026-09-06T21:16:13.665Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}