Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
refactor(pg): remove PG ANTLR parser dependency entirely [BYT-9010] (#19761)
rebelice629f668dc08c
diff --git a/backend/plugin/advisor/pg/sdl_integrity_check.go b/backend/plugin/advisor/pg/sdl_integrity_check.goindex a1c069efb6..c23ada0851 100644--- a/backend/plugin/advisor/pg/sdl_integrity_check.go+++ b/backend/plugin/advisor/pg/sdl_integrity_check.go@@ -31,8 +31,7 @@ func CheckSDLIntegrity(files map[string]string) (map[string][]*storepb.Advice, e// Parse each file and build individual symbol tablesfileCheckers := make([]*fileSymbolTable, 0, len(files))for filePath, statement := range files {- // Validate syntax using ANTLR parser first (stricter than omni for some edge cases)- if _, err := pgparser.ParsePostgreSQL(statement); err != nil {+ if err := validateParenBalance(statement); err != nil {return map[string][]*storepb.Advice{filePath: {{Status: storepb.Advice_ERROR,@@ -42,7 +41,6 @@ func CheckSDLIntegrity(files map[string]string) (map[string][]*storepb.Advice, e}},}, nil}-stmts, err := pgparser.ParsePg(statement)if err != nil {return map[string][]*storepb.Advice{diff --git a/backend/plugin/db/pg/query.go b/backend/plugin/db/pg/query.goindex ce123402bc..bd58aef7f2 100644--- a/backend/plugin/db/pg/query.go+++ b/backend/plugin/db/pg/query.go@@ -4,15 +4,13 @@ import ("database/sql""fmt""log/slog"- "strconv""strings""time"- "github.com/antlr4-go/antlr/v4""github.com/pkg/errors""google.golang.org/protobuf/types/known/timestamppb"- parser "github.com/bytebase/parser/postgresql"+ "github.com/bytebase/omni/pg/ast""github.com/jackc/pgx/v5/pgtype""github.com/bytebase/bytebase/backend/common/log"@@ -201,161 +199,90 @@ func getStatementWithResultLimitInline(statement string, limitCount int) (stringreturn "", errors.New("empty statement")}- parseResults, err := pgparser.ParsePostgreSQL(statement)+ stmts, err := pgparser.ParsePg(statement)if err != nil {return "", errors.Wrap(err, "failed to parse statement")}- if len(parseResults) != 1 {- return "", errors.Errorf("expected exactly one statement, got %d", len(parseResults))+ if len(stmts) != 1 {+ return "", errors.Errorf("expected exactly one statement, got %d", len(stmts))}- parseResult := parseResults[0]-- listener := &postgresqlRewriter{- limitCount: limitCount,- outerMostQuery: true,- rewriter: antlr.NewTokenStreamRewriter(parseResult.Tokens),- }-- antlr.ParseTreeWalkerDefault.Walk(listener, parseResult.Tree)- if listener.err != nil {- return "", errors.Wrapf(listener.err, "statement: %s", statement)+ sel, ok := stmts[0].AST.(*ast.SelectStmt)+ if !ok {+ // Non-SELECT statement, return as-is.+ return statement, nil}- return listener.rewriter.GetTextDefault(), nil-}--type postgresqlRewriter struct {- *parser.BasePostgreSQLParserListener-- rewriter *antlr.TokenStreamRewriter- err error- outerMostQuery bool- limitCount int+ return rewriteSelectLimit(statement, sel, limitCount)}-// EnterSelectstmt is called when entering a select statement-func (r *postgresqlRewriter) EnterSelectstmt(ctx *parser.SelectstmtContext) {- if !r.outerMostQuery {- return- }- r.outerMostQuery = false-- // Recursively find the select_no_parens and handle it- if selectNoParens := r.findSelectNoParens(ctx); selectNoParens != nil {- r.handleSelectNoParens(selectNoParens)- }-}--// findSelectNoParens recursively finds the select_no_parens from a selectstmt-func (r *postgresqlRewriter) findSelectNoParens(ctx *parser.SelectstmtContext) parser.ISelect_no_parensContext {- // Direct select_no_parens- if selectNoParens := ctx.Select_no_parens(); selectNoParens != nil {- return selectNoParens- }-- // Through select_with_parens- if selectWithParens := ctx.Select_with_parens(); selectWithParens != nil {- return r.findSelectNoParensFromWithParens(selectWithParens)+// rewriteSelectLimit adds or adjusts the LIMIT clause of a SELECT statement+// using byte-offset positions from the omni AST to surgically edit the original SQL text.+func rewriteSelectLimit(sql string, sel *ast.SelectStmt, limitCount int) (string, error) {+ if sel.LimitCount != nil {+ // Already has LIMIT — replace the value if ours is lower.+ existingLimit := extractIntFromNode(sel.LimitCount)+ if existingLimit > 0 && existingLimit <= limitCount {+ return sql, nil // existing limit is already lower or equal, keep it+ }+ loc := nodeLocOf(sel.LimitCount)+ if loc.Start >= 0 && loc.End > loc.Start && loc.End <= len(sql) {+ return sql[:loc.Start] + fmt.Sprintf("%d", limitCount) + sql[loc.End:], nil+ }+ // LimitCount is a non-constant expression (e.g. LIMIT $1, LIMIT (1+2)).+ // Cannot safely rewrite in-place; let the caller fall back to CTE wrapper.+ return "", errors.Errorf("cannot rewrite non-constant LIMIT expression")}- return nil+ // No LIMIT clause — find the right insertion point.+ // PostgreSQL grammar order: ... ORDER BY ... LIMIT ... FOR UPDATE ...+ // LIMIT goes BEFORE FOR UPDATE but AFTER everything else.+ insertPos := findLimitInsertPosition(sql, sel)+ return sql[:insertPos] + fmt.Sprintf(" LIMIT %d", limitCount) + sql[insertPos:], nil}-// findSelectNoParensFromWithParens recursively finds select_no_parens from select_with_parens-func (r *postgresqlRewriter) findSelectNoParensFromWithParens(ctx parser.ISelect_with_parensContext) parser.ISelect_no_parensContext {- // Check for direct select_no_parens- if selectNoParens := ctx.Select_no_parens(); selectNoParens != nil {- return selectNoParens- }-- // Check for nested select_with_parens- if innerSelectWithParens := ctx.Select_with_parens(); innerSelectWithParens != nil {- return r.findSelectNoParensFromWithParens(innerSelectWithParens)+// findLimitInsertPosition returns the byte offset where " LIMIT N" should be inserted.+// It uses AST Loc positions exclusively — no string scanning for parentheses or keywords.+func findLimitInsertPosition(_ string, sel *ast.SelectStmt) int {+ // LIMIT must appear before FOR UPDATE/SHARE.+ // Use the LockingClause's Loc.Start to insert just before it.+ if sel.LockingClause != nil {+ span := ast.ListSpan(sel.LockingClause)+ if span.Start > 0 {+ return span.Start+ }}- return nil-}--// EnterStmt is called when entering any statement-func (r *postgresqlRewriter) EnterStmt(ctx *parser.StmtContext) {- // Only process SELECT statements- if ctx.Selectstmt() == nil {- // Not a SELECT statement, skip processing- r.outerMostQuery = false+ // Otherwise insert at SelectStmt.Loc.End (after everything, including outer parens).+ end := sel.Loc.End+ if end <= 0 {+ return 0}+ return end}-// handleSelectNoParens processes select statements without parentheses-func (r *postgresqlRewriter) handleSelectNoParens(ctx parser.ISelect_no_parensContext) {- // Check if there's already a limit clause- var hasLimit bool- var limitClause parser.ILimit_clauseContext-- f := ctx.GetText()- slog.Debug("Processing select_no_parens", slog.String("text", f))-- // Check the for_locking_clause with opt_select_limit branch- if ctx.For_locking_clause() != nil && ctx.Opt_select_limit() != nil {- if ctx.Opt_select_limit().Select_limit() != nil {- if ctx.Opt_select_limit().Select_limit().Limit_clause() != nil {- hasLimit = true- limitClause = ctx.Opt_select_limit().Select_limit().Limit_clause()- }- }- }- // Check the select_limit with opt_for_locking_clause branch- if ctx.Select_limit() != nil {- if ctx.Select_limit().Limit_clause() != nil {- hasLimit = true- limitClause = ctx.Select_limit().Limit_clause()+// extractIntFromNode extracts an integer value from a LIMIT/OFFSET node.+func extractIntFromNode(node ast.Node) int {+ switch n := node.(type) {+ case *ast.Integer:+ return int(n.Ival)+ case *ast.A_Const:+ if iv, ok := n.Val.(*ast.Integer); ok {+ return int(iv.Ival)}+ return 0+ default:+ return 0}+}- if hasLimit && limitClause != nil {- // Extract and compare the existing limit value- if limitClause.Select_limit_value() != nil {- limitValueText := limitClause.Select_limit_value().GetText()- if limitValueText != "ALL" {- existingLimit, err := strconv.Atoi(limitValueText)- if err == nil {- if existingLimit == 0 || r.limitCount < existingLimit {- // Replace the existing limit value- limitValueCtx := limitClause.Select_limit_value()- if limitValueCtx.A_expr() != nil {- r.rewriter.ReplaceTokenDefault(- limitValueCtx.GetStart(),- limitValueCtx.GetStop(),- fmt.Sprintf("%d", r.limitCount),- )- }- }- // else: existing limit is already lower, keep it- }- }- }- return- }-- // No limit clause exists, add one- // Find the appropriate position to insert LIMIT- var insertPosition antlr.Token-- // Insert after FOR UPDATE/SHARE clause if present- if ctx.For_locking_clause() != nil {- insertPosition = ctx.For_locking_clause().GetStop()- }- // Insert after ORDER BY clause if present- if ctx.Opt_sort_clause() != nil && ctx.Opt_sort_clause().Sort_clause() != nil {- insertPosition = ctx.Opt_sort_clause().Sort_clause().GetStop()- }- // Otherwise insert after the select_clause- if insertPosition == nil && ctx.Select_clause() != nil {- insertPosition = ctx.Select_clause().GetStop()- }-- if insertPosition != nil {- r.rewriter.InsertAfterToken("default", insertPosition, fmt.Sprintf(" LIMIT %d", r.limitCount))+// nodeLocOf returns the Loc of a node, handling common wrapper types.+func nodeLocOf(node ast.Node) ast.Loc {+ switch n := node.(type) {+ case *ast.A_Const:+ return n.Loc+ default:+ return ast.Loc{Start: -1, End: -1}}}diff --git a/backend/plugin/db/pg/query_limit_test.go b/backend/plugin/db/pg/query_limit_test.goindex 881bafd2f3..06ea3c983d 100644--- a/backend/plugin/db/pg/query_limit_test.go+++ b/backend/plugin/db/pg/query_limit_test.go@@ -133,8 +133,7 @@ func TestGetStatementWithResultLimit(t *testing.T) {name: "SELECT with parentheses and existing LIMIT lower than requested",statement: "(SELECT * FROM users LIMIT 5)",limit: 10,- // TODO(zp): FIX ME- want: "(SELECT * FROM users LIMIT 5) LIMIT 10",+ want: "(SELECT * FROM users LIMIT 5)",},}diff --git a/backend/plugin/parser/pg/builtin_functions.go b/backend/plugin/parser/pg/builtin_functions.gonew file mode 100644index 0000000000..b62b327085--- /dev/null+++ b/backend/plugin/parser/pg/builtin_functions.go@@ -0,0 +1,835 @@+package pg++import "slices"++var (+ comparisonFunctions = []string{+ "num_nonnulls",+ "num_nulls",+ }++ mathematicalFunctions = []string{+ "abs",+ "cbrt",+ "ceil",+ "ceiling",+ "degrees",+ "div",+ "erf",+ "erfc",+ "exp",+ "factorial",+ "floor",+ "gcd",+ "lcm",+ "ln",+ "log",+ "log10",+ "min_scale",+ "mod",+ "pi",+ "power",+ "radians",+ "round",+ "scale",+ "sign",+ "sqrt",+ "trim_scale",+ "trunc",+ "width_bucket",+ "random",+ "random_normal",+ "setseed",+ "acos",+ "acosd",+ "asin",+ "asind",+ "atan",+ "atand",+ "atan2",+ "atan2d",+ "cos",+ "cosd",+ "cot",+ "cotd",+ "sin",+ "sind",+ "tan",+ "tand",+ "sinh",+ "cosh",+ "tanh",+ "asinh",+ "acosh",+ "atanh",+ }++ stringFunctions = []string{+ "btrim",+ "bit_length",+ "char_length",+ "character_length",+ "lower",+ "lpad",+ "ltrim",+ "normalize",+ "octet_length",+ "overlay",+ "position",+ "rpad",+ "rtrim",+ "substring",+ "trim",+ "upper",+ "ascii",+ "chr",+ "concat",+ "concat_ws",+ "format",+ "initcap",+ "left",+ "length",+ "md5",+ "parse_ident",+ "pg_client_encoding",+ "quote_ident",+ "quote_literal",+ "quote_nullable",+ "regexp_count",+ "regexp_instr",+ "regexp_like",+ "regexp_match",+ "regexp_matches",+ "regexp_replace",+ "regexp_split_to_array",+ "regexp_split_to_table",+ "regexp_substr",+ "repeat",+ "replace",+ "reverse",+ "right",+ "split_part",+ "starts_with",+ "string_to_array",+ "string_to_table",+ "strpos",+ "substr",+ "to_ascii",+ "to_hex",+ "translate",+ "unistr",+ }++ binaryStringFunctions = []string{+ "bit_length",+ "btrim",+ "ltrim",+ "octet_length",+ "overlay",+ "position",+ "rtrim",+ "substring",+ "trim",+ "bit_count",+ "get_bit",+ "get_byte",+ "length",+ "md5",+ "set_bit",+ "set_byte",+ "sha224",+ "sha256",+ "sha384",+ "sha512",+ "substr",+ "convert",+ "convert_from",+ "convert_to",+ "encode",+ "decode",+ }++ bitStringFunctions = []string{+ "bit_count",+ "bit_length",+ "length",+ "octet_length",+ "overlay",+ "position",+ "substring",+ "get_bit",+ "set_bit",+ }++ dataTypeFormattingFunctions = []string{+ "to_char",+ "to_date",+ "to_number",+ "to_timestamp",+ }++ dateTimeFunctions = []string{+ "age",+ "clock_timestamp",+ "current_time",+ "current_timestamp",+ "date_add",+ "date_bin",+ "date_part",+ "date_subtract",+ "date_trunc",+ "extract",+ "isfinite",+ "justify_days",+ "justify_hours",+ "justify_interval",+ "localtime",+ "localtimestamp",+ "make_date",+ "make_interval",+ "make_time",+ "make_timestamp",+ "make_timestamptz",+ "now",+ "statement_timestamp",+ "timeofday",+ "transaction_timestamp",+ "to_timestamp",+ }++ enumSupportFunctions = []string{+ "enum_first",+ "enum_last",+ "enum_range",+ }++ geometricFunctions = []string{+ "area",+ "center",+ "diagonal",+ "diameter",+ "height",+ "isclosed",+ "isopen",+ "length",+ "npoints",+ "pclose",+ "popen",+ "radius",+ "slope",+ "width",+ "box",+ "bound_box",+ "circle",+ "line",+ "lseg",+ "path",+ "point",+ "polygon",+ }++ networkAddressFunctions = []string{+ "abbrev",+ "broadcast",+ "family",+ "host",+ "hostmask",+ "inet_merge",+ "inet_same_family",+ "masklen",+ "netmask",+ "network",+ "set_masklen",+ "text",+ "trunc",+ "macaddr8_set7bit",+ }++ textSearchFunctions = []string{+ "array_to_tsvector",+ "get_current_ts_config",+ "length",+ "numnode",+ "plainto_tsquery",+ "phraseto_tsquery",+ "websearch_to_tsquery",+ "querytree",+ "setweight",+ "strip",+ "to_tsquery",+ "to_tsvector",+ "json_to_tsvector",+ "jsonb_to_tsvector",+ "ts_delete",+ "ts_filter",+ "ts_headline",+ "ts_rank",+ "ts_rank_cd",+ "ts_rewrite",+ "tsquery_phrase",+ "tsvector_to_array",+ "unnest",+ "ts_debug",+ "ts_lexize",+ "ts_parse",+ "ts_token_type",+ "ts_stat",+ }++ uuidFunctions = []string{+ "gen_random_uuid",+ }++ xmlFunctions = []string{+ "xmlcomment",+ "xmlconcat",+ "xmlelement",+ "xmlforest",+ "xmlpi",+ "xmlroot",+ "xmlagg",+ "XMLEXISTS",+ "xml_is_well_formed",+ "xml_is_well_formed_document",+ "xml_is_well_formed_content",+ "xpath",+ "xpath_exists",+ "table_to_xml",+ "query_to_xml",+ "cursor_to_xml",+ "table_to_xmlschema",+ "query_to_xmlschema",+ "cursor_to_xmlschema",+ "table_to_xml_and_xmlschema",+ "query_to_xml_and_xmlschema",+ "schema_to_xml",+ "schema_to_xmlschema",+ "schema_to_xml_and_xmlschema",+ "database_to_xml",+ "database_to_xmlschema",+ "database_to_xml_and_xmlschema",+ }++ jsonFunctions = []string{+ "to_json",+ "to_jsonb",+ "array_to_json",+ "json_array",+ "row_to_json",+ "json_build_array",+ "jsonb_build_array",+ "json_build_object",+ "jsonb_build_object",+ "json_object",+ "jsonb_object",+ "json_array_elements",+ "jsonb_array_elements",+ "json_array_elements_text",+ "jsonb_array_elements_text",+ "json_array_length",+ "jsonb_array_length",+ "json_each",+ "jsonb_each",+ "json_each_text",+ "jsonb_each_text",+ "json_extract_path",+ "jsonb_extract_path",+ "json_extract_path_text",+ "jsonb_extract_path_text",+ "json_object_keys",+ "jsonb_object_keys",+ "json_populate_record",+ "jsonb_populate_record",+ "json_populate_recordset",+ "jsonb_populate_recordset",+ "json_to_record",+ "jsonb_to_record",+ "json_to_recordset",+ "jsonb_to_recordset",+ "jsonb_set",+ "jsonb_set_lax",+ "jsonb_insert",+ "json_strip_nulls",+ "jsonb_strip_nulls",+ "jsonb_path_exists",+ "jsonb_path_match",+ "jsonb_path_query",+ "jsonb_path_query_array",+ "jsonb_path_query_first",+ "jsonb_path_exists_tz",+ "jsonb_path_match_tz",+ "jsonb_path_query_tz",+ "jsonb_path_query_array_tz",+ "jsonb_path_query_first_tz",+ "jsonb_pretty",+ "json_typeof",+ "jsonb_typeof",+ "exists",+ }++ sequenceManipulationFunctions = []string{+ "nextval",+ "setval",+ "currval",+ "lastval",+ }++ conditionalFunctions = []string{+ "COALESCE",+ "NULLIF",+ "GREATEST",+ "LEAST",+ }++ arrayFunctions = []string{+ "array_append",+ "array_cat",+ "array_dims",+ "array_fill",+ "array_length",+ "array_lower",+ "array_ndims",+ "array_position",+ "array_positions",+ "array_prepend",+ "array_remove",+ "array_replace",+ "array_sample",+ "array_shuffle",+ "array_to_string",+ "array_upper",+ "cardinality",+ "trim_array",+ "unnest",+ }++ rangeFunctions = []string{+ "lower",+ "upper",+ "isempty",+ "lower_inc",+ "upper_inc",+ "lower_inf",+ "upper_inf",+ "range_merge",+ "multirange",+ "unnest",+ }++ aggregateFunctions = []string{+ "any_value",+ "array_agg",+ "avg",+ "bit_and",+ "bit_or",+ "bit_xor",+ "bool_and",+ "bool_or",+ "count",+ "every",+ "json_agg",+ "jsonb_agg",+ "json_objectagg",+ "json_object_agg",+ "jsonb_object_agg",+ "json_object_agg_strict",+ "jsonb_object_agg_strict",+ "json_object_agg_unique",+ "jsonb_object_agg_unique",+ "json_arrayagg",+ "json_object_agg_unique_strict",+ "jsonb_object_agg_unique_strict",+ "max",+ "min",+ "range_agg",+ "range_intersect_agg",+ "json_agg_strict",+ "jsonb_agg_strict",+ "string_agg",+ "sum",+ "xmlagg",+ "corr",+ "covar_pop",+ "covar_samp",+ "regr_avgx",+ "regr_avgy",+ "regr_count",+ "regr_intercept",+ "regr_r2",+ "regr_slope",+ "regr_sxx",+ "regr_sxy",+ "regr_syy",+ "stddev",+ "stddev_pop",+ "stddev_samp",+ "variance",+ "var_pop",+ "var_samp",+ "mode",+ "percentile_cont",+ "percentile_disc",+ "rank",+ "dense_rank",+ "percent_rank",+ "cume_dist",+ "GROUPING",+ }++ windowFunctions = []string{+ "row_number",+ "rank",+ "dense_rank",+ "percent_rank",+ "cume_dist",+ "ntile",+ "lag",+ "lead",+ "first_value",+ "last_value",+ "nth_value",+ }++ setReturningFunctions = []string{+ "generate_series",+ "generate_subscripts",+ }++ systemInformationFunctions = []string{+ "current_database",+ "current_query",+ "current_schema",+ "current_schemas",+ "inet_client_addr",+ "inet_client_port",+ "inet_server_addr",+ "inet_server_port",+ "pg_backend_pid",+ "pg_blocking_pids",+ "pg_conf_load_time",+ "pg_current_logfile",+ "pg_my_temp_schema",+ "pg_is_other_temp_schema",+ "pg_jit_available",+ "pg_listening_channels",+ "pg_notification_queue_usage",+ "pg_postmaster_start_time",+ "pg_safe_snapshot_blocking_pids",+ "pg_trigger_depth",+ "version",+ "has_any_column_privilege",+ "has_column_privilege",+ "has_database_privilege",+ "has_foreign_data_wrapper_privilege",+ "has_function_privilege",+ "has_language_privilege",+ "has_parameter_privilege",+ "has_schema_privilege",+ "has_sequence_privilege",+ "has_server_privilege",+ "has_table_privilege",+ "has_tablespace_privilege",+ "has_type_privilege",+ "pg_has_role",+ "row_security_active",+ "acldefault",+ "aclexplode",+ "makeaclitem",+ "pg_collation_is_visible",+ "pg_conversion_is_visible",+ "pg_function_is_visible",+ "pg_opclass_is_visible",+ "pg_operator_is_visible",+ "pg_opfamily_is_visible",+ "pg_statistics_obj_is_visible",+ "pg_table_is_visible",+ "pg_ts_config_is_visible",+ "pg_ts_dict_is_visible",+ "pg_ts_parser_is_visible",+ "pg_ts_template_is_visible",+ "pg_type_is_visible",+ "format_type",+ "pg_char_to_encoding",+ "pg_encoding_to_char",+ "pg_get_catalog_foreign_keys",+ "pg_get_constraintdef",+ "pg_get_expr",+ "pg_get_functiondef",+ "pg_get_function_arguments",+ "pg_get_function_identity_arguments",+ "pg_get_function_result",+ "pg_get_indexdef",+ "pg_get_keywords",+ "pg_get_partkeydef",+ "pg_get_ruledef",+ "pg_get_serial_sequence",+ "pg_get_statisticsobjdef",+ "pg_get_triggerdef",+ "pg_get_userbyid",+ "pg_get_viewdef",+ "pg_index_column_has_property",+ "pg_index_has_property",+ "pg_indexam_has_property",+ "pg_options_to_table",+ "pg_settings_get_flags",+ "pg_tablespace_databases",+ "pg_tablespace_location",+ "pg_typeof",+ "COLLATION FOR",+ "to_regclass",+ "to_regcollation",+ "to_regnamespace",+ "to_regoper",+ "to_regoperator",+ "to_regproc",+ "to_regprocedure",+ "to_regrole",+ "to_regtype",+ "pg_describe_object",+ "pg_identify_object",+ "pg_identify_object_as_address",+ "pg_get_object_address",+ "col_description",+ "obj_description",+ "shobj_description",+ "pg_input_is_valid",+ "pg_input_error_info",+ "pg_current_xact_id",+ "pg_current_xact_id_if_assigned",+ "pg_xact_status",+ "pg_current_snapshot",+ "pg_snapshot_xip",+ "pg_snapshot_xmax",+ "pg_snapshot_xmin",+ "pg_visible_in_snapshot",+ "txid_current",+ "txid_current_if_assigned",+ "txid_current_snapshot",+ "txid_snapshot_xip",+ "txid_snapshot_xmax",+ "txid_snapshot_xmin",+ "txid_visible_in_snapshot",+ "txid_status",+ "pg_xact_commit_timestamp",+ "pg_xact_commit_timestamp_origin",+ "pg_last_committed_xact",+ "pg_control_checkpoint",+ "pg_control_system",+ "pg_control_init",+ "pg_control_recovery",+ }++ systemAdministrationFunctions = []string{+ "current_setting",+ "set_config",+ "pg_cancel_backend",+ "pg_log_backend_memory_contexts",+ "pg_reload_conf",+ "pg_rotate_logfile",+ "pg_terminate_backend",+ "pg_create_restore_point",+ "pg_current_wal_flush_lsn",+ "pg_current_wal_insert_lsn",+ "pg_current_wal_lsn",+ "pg_backup_start",+ "pg_backup_stop",+ "pg_switch_wal",+ "pg_walfile_name",+ "pg_walfile_name_offset",+ "pg_split_walfile_name",+ "pg_wal_lsn_diff",+ "pg_is_in_recovery",+ "pg_last_wal_receive_lsn",+ "pg_last_wal_replay_lsn",+ "pg_last_xact_replay_timestamp",+ "pg_get_wal_resource_managers",+ "pg_is_wal_replay_paused",+ "pg_get_wal_replay_pause_state",+ "pg_promote",+ "pg_wal_replay_pause",+ "pg_wal_replay_resume",+ "pg_export_snapshot",+ "pg_log_standby_snapshot",+ "pg_create_physical_replication_slot",+ "pg_drop_replication_slot",+ "pg_create_logical_replication_slot",+ "pg_copy_physical_replication_slot",+ "pg_copy_logical_replication_slot",+ "pg_logical_slot_get_changes",+ "pg_logical_slot_peek_changes",+ "pg_logical_slot_get_binary_changes",+ "pg_logical_slot_peek_binary_changes",+ "pg_replication_slot_advance",+ "pg_replication_origin_create",+ "pg_replication_origin_drop",+ "pg_replication_origin_oid",+ "pg_replication_origin_session_setup",+ "pg_replication_origin_session_reset",+ "pg_replication_origin_session_is_setup",+ "pg_replication_origin_session_progress",+ "pg_replication_origin_xact_setup",+ "pg_replication_origin_xact_reset",+ "pg_replication_origin_advance",+ "pg_replication_origin_progress",+ "pg_logical_emit_message",+ "pg_column_size",+ "pg_column_compression",+ "pg_database_size",+ "pg_indexes_size",+ "pg_relation_size",+ "pg_size_bytes",+ "pg_size_pretty",+ "pg_table_size",+ "pg_tablespace_size",+ "pg_total_relation_size",+ "pg_relation_filenode",+ "pg_relation_filepath",+ "pg_filenode_relation",+ "pg_collation_actual_version",+ "pg_database_collation_actual_version",+ "pg_import_system_collations",+ "pg_partition_tree",+ "pg_partition_ancestors",+ "pg_partition_root",+ "brin_summarize_new_values",+ "brin_summarize_range",+ "brin_desummarize_range",+ "gin_clean_pending_list",+ "pg_ls_dir",+ "pg_ls_logdir",+ "pg_ls_waldir",+ "pg_ls_logicalmapdir",+ "pg_ls_logicalsnapdir",+ "pg_ls_replslotdir",+ "pg_ls_archive_statusdir",+ "pg_ls_tmpdir",+ "pg_read_file",+ "pg_read_binary_file",+ "pg_stat_file",+ "pg_advisory_lock",+ "pg_advisory_lock_shared",+ "pg_advisory_unlock",+ "pg_advisory_unlock_all",+ "pg_advisory_unlock_shared",+ "pg_advisory_xact_lock",+ "pg_advisory_xact_lock_shared",+ "pg_try_advisory_lock",+ "pg_try_advisory_lock_shared",+ "pg_try_advisory_xact_lock",+ "pg_try_advisory_xact_lock_shared",+ }++ triggerFunctions = []string{+ "suppress_redundant_updates_trigger",+ "tsvector_update_trigger",+ "tsvector_update_trigger_column",+ }++ eventTriggerFunctions = []string{+ "pg_event_trigger_ddl_commands",+ "pg_event_trigger_dropped_objects",+ "pg_event_trigger_table_rewrite_oid",+ "pg_event_trigger_table_rewrite_reason",+ }++ statisticsFunctions = []string{+ "pg_event_trigger_table_rewrite_reason",+ }++ builtinFunctions = []string{}+)++func init() {+ funcMap := make(map[string]bool)++ for _, f := range comparisonFunctions {+ funcMap[f] = true+ }+ for _, f := range mathematicalFunctions {+ funcMap[f] = true+ }+ for _, f := range stringFunctions {+ funcMap[f] = true+ }+ for _, f := range binaryStringFunctions {+ funcMap[f] = true+ }+ for _, f := range bitStringFunctions {+ funcMap[f] = true+ }+ for _, f := range dataTypeFormattingFunctions {+ funcMap[f] = true+ }+ for _, f := range dateTimeFunctions {+ funcMap[f] = true+ }+ for _, f := range enumSupportFunctions {+ funcMap[f] = true+ }+ for _, f := range geometricFunctions {+ funcMap[f] = true+ }+ for _, f := range networkAddressFunctions {+ funcMap[f] = true+ }+ for _, f := range textSearchFunctions {+ funcMap[f] = true+ }+ for _, f := range uuidFunctions {+ funcMap[f] = true+ }+ for _, f := range xmlFunctions {+ funcMap[f] = true+ }+ for _, f := range jsonFunctions {+ funcMap[f] = true+ }+ for _, f := range sequenceManipulationFunctions {+ funcMap[f] = true+ }+ for _, f := range conditionalFunctions {+ funcMap[f] = true+ }+ for _, f := range arrayFunctions {+ funcMap[f] = true+ }+ for _, f := range rangeFunctions {+ funcMap[f] = true+ }+ for _, f := range aggregateFunctions {+ funcMap[f] = true+ }+ for _, f := range windowFunctions {+ funcMap[f] = true+ }+ for _, f := range setReturningFunctions {+ funcMap[f] = true+ }+ for _, f := range systemInformationFunctions {+ funcMap[f] = true+ }+ for _, f := range systemAdministrationFunctions {+ funcMap[f] = true+ }+ for _, f := range triggerFunctions {+ funcMap[f] = true+ }+ for _, f := range eventTriggerFunctions {+ funcMap[f] = true+ }+ for _, f := range statisticsFunctions {+ funcMap[f] = true+ }++ for f := range funcMap {+ builtinFunctions = append(builtinFunctions, f)+ }++ slices.Sort(builtinFunctions)+}++func getBuiltinFunctions() []string {+ var result []string+ result = append(result, builtinFunctions...)+ return result+}diff --git a/backend/plugin/parser/pg/completion.go b/backend/plugin/parser/pg/completion.goindex 0682c62980..81e676b8d2 100644--- a/backend/plugin/parser/pg/completion.go+++ b/backend/plugin/parser/pg/completion.go@@ -11,7 +11,6 @@ import (omnipg "github.com/bytebase/omni/pg""github.com/bytebase/omni/pg/ast"pgparser "github.com/bytebase/omni/pg/parser"- pgantlr "github.com/bytebase/parser/postgresql""github.com/bytebase/bytebase/backend/generated-go/store""github.com/bytebase/bytebase/backend/plugin/parser/base"@@ -270,7 +269,7 @@ func (m CompletionMap) Insert(entry base.Candidate) {}func (m CompletionMap) insertFunctions() {- for _, name := range pgantlr.GetBuiltinFunctions() {+ for _, name := range getBuiltinFunctions() {m.Insert(base.Candidate{Type: base.CandidateTypeFunction,Text: name + "()",diff --git a/backend/plugin/parser/pg/omni.go b/backend/plugin/parser/pg/omni.goindex a6371d03a8..3924891516 100644--- a/backend/plugin/parser/pg/omni.go+++ b/backend/plugin/parser/pg/omni.go@@ -11,8 +11,6 @@ import ()// OmniAST wraps an omni AST node and implements the base.AST interface.-// During migration, it also carries an optional ANTLR AST for backward compatibility-// with advisors that still use ANTLR parse trees.type OmniAST struct {// Node is the omni AST node (e.g. *ast.SelectStmt, *ast.CreateStmt).Node ast.Node@@ -20,9 +18,6 @@ type OmniAST struct {Text string// StartPosition is the 1-based position where this statement starts.StartPosition *storepb.Position- // antlrAST is the legacy ANTLR parse tree, populated during migration for- // backward compatibility with advisors. Will be removed once all advisors migrate to omni.- antlrAST *base.ANTLRAST}// ASTStartPosition implements base.AST.@@ -30,14 +25,6 @@ func (a *OmniAST) ASTStartPosition() *storepb.Position {return a.StartPosition}-// AsANTLRAST implements base.AntlrASTProvider, returning the legacy ANTLR AST if available.-func (a *OmniAST) AsANTLRAST() (*base.ANTLRAST, bool) {- if a.antlrAST == nil {- return nil, false- }- return a.antlrAST, true-}-// ParsePg parses SQL using omni's parser and returns omni Statement objects directly.// This is the recommended entry point for new code that needs omni AST nodes.func ParsePg(sql string) ([]omnipg.Statement, error) {diff --git a/backend/plugin/parser/pg/pg.go b/backend/plugin/parser/pg/pg.goindex 1c9993ae60..1b739e0fd3 100644--- a/backend/plugin/parser/pg/pg.go+++ b/backend/plugin/parser/pg/pg.go@@ -5,9 +5,6 @@ import ("fmt""strings"- "github.com/antlr4-go/antlr/v4"- parser "github.com/bytebase/parser/postgresql"-omniparser "github.com/bytebase/omni/pg/parser"storepb "github.com/bytebase/bytebase/backend/generated-go/store"@@ -21,9 +18,6 @@ func init() {// parsePgStatements is the ParseStatementsFunc for PostgreSQL.// Returns []ParsedStatement with both text and AST populated.-// Uses omni for parsing, returning OmniAST wrappers that also carry legacy ANTLR-// parse trees for backward compatibility with advisors that haven't migrated yet.-// When omni parsing fails but ANTLR succeeds, falls back to ANTLR-only mode.func parsePgStatements(statement string) ([]base.ParsedStatement, error) {stmts, err := SplitSQL(statement)if err != nil {@@ -36,44 +30,20 @@ func parsePgStatements(statement string) ([]base.ParsedStatement, error) {continue}- // Parse with ANTLR (needed by advisors during migration).- // TODO(omni-migration): remove once all advisors migrate to omni AST.- antlrResult, antlrErr := parseSinglePostgreSQL(stmt.Text, stmt.BaseLine())-- // Parse with omni.omniStmts, omniErr := ParsePg(stmt.Text)+ if omniErr != nil {+ return nil, convertOmniError(omniErr, statement, stmt)+ }- switch {- case omniErr == nil:- // Omni succeeded: use omni AST with ANTLR fallback for advisors.- for _, os := range omniStmts {- omniAST := &OmniAST{- Node: os.AST,- Text: stmt.Text,- StartPosition: stmt.Start,- }- if antlrErr == nil && antlrResult != nil {- omniAST.antlrAST = antlrResult- }- result = append(result, base.ParsedStatement{- Statement: stmt,- AST: omniAST,- })- }- case antlrErr == nil && antlrResult != nil:- // Omni failed but ANTLR succeeded: use ANTLR-only fallback.- // This handles cases where omni is stricter (e.g. reserved keywords used as identifiers).+ for _, os := range omniStmts {result = append(result, base.ParsedStatement{Statement: stmt,AST: &OmniAST{+ Node: os.AST,Text: stmt.Text,StartPosition: stmt.Start,- antlrAST: antlrResult,},})- default:- // Both failed: return omni error (prefer omni error message).- return nil, convertOmniError(omniErr, statement, stmt)}}return result, nil@@ -101,260 +71,10 @@ func convertOmniError(err error, _ string, stmt base.Statement) error {}}-// ParsePostgreSQL parses the given SQL and returns a list of ANTLRAST (one per statement).-// Use the PostgreSQL parser based on antlr4.-func ParsePostgreSQL(sql string) ([]*base.ANTLRAST, error) {- stmts, err := SplitSQL(sql)- if err != nil {- return nil, err- }- return parsePostgreSQLStatements(stmts)-}--// parsePostgreSQLStatements parses pre-split statements without re-splitting.-// This is the internal implementation used by both ParsePostgreSQL and parsePgStatements.-func parsePostgreSQLStatements(stmts []base.Statement) ([]*base.ANTLRAST, error) {- var results []*base.ANTLRAST- for _, stmt := range stmts {- if stmt.Empty {- continue- }-- parseResult, err := parseSinglePostgreSQL(stmt.Text, stmt.BaseLine())- if err != nil {- return nil, err- }- results = append(results, parseResult)- }-- return results, nil-}--// parseSinglePostgreSQL parses a single PostgreSQL statement and returns the ANTLRAST.-func parseSinglePostgreSQL(sql string, baseLine int) (*base.ANTLRAST, error) {- lexer := parser.NewPostgreSQLLexer(antlr.NewInputStream(sql))- stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)- p := parser.NewPostgreSQLParser(stream)- startPosition := &storepb.Position{Line: int32(baseLine) + 1}- lexerErrorListener := &base.ParseErrorListener{- Statement: sql,- StartPosition: startPosition,- }- lexer.RemoveErrorListeners()- lexer.AddErrorListener(lexerErrorListener)-- parserErrorListener := &base.ParseErrorListener{- Statement: sql,- StartPosition: startPosition,- }- p.RemoveErrorListeners()- p.AddErrorListener(parserErrorListener)-- p.BuildParseTrees = true-- tree := p.Root()- if lexerErrorListener.Err != nil {- return nil, lexerErrorListener.Err- }-- if parserErrorListener.Err != nil {- return nil, parserErrorListener.Err- }-- result := &base.ANTLRAST{- StartPosition: &storepb.Position{Line: int32(baseLine) + 1},- Tree: tree,- Tokens: stream,- }-- return result, nil-}--func normalizePostgreSQLName(ctx parser.INameContext) string {- if ctx == nil {- return ""- }-- if ctx.Colid() != nil {- return NormalizePostgreSQLColid(ctx.Colid())- }-- return ""-}--// NormalizePostgreSQLAnyName normalizes the given any name.-func NormalizePostgreSQLAnyName(ctx parser.IAny_nameContext) []string {- if ctx == nil {- return nil- }-- var result []string- result = append(result, NormalizePostgreSQLColid(ctx.Colid()))- if ctx.Attrs() != nil {- for _, item := range ctx.Attrs().AllAttr_name() {- result = append(result, normalizePostgreSQLAttrName(item))- }- }-- return result-}--// ParsePostgreSQLStatement parses the given SQL and returns the AST tree.-func NormalizePostgreSQLQualifiedName(ctx parser.IQualified_nameContext) []string {- if ctx == nil {- return []string{}- }-- res := []string{NormalizePostgreSQLColid(ctx.Colid())}-- if ctx.Indirection() != nil {- res = append(res, normalizePostgreSQLIndirection(ctx.Indirection())...)- }- return res-}--func normalizePostgreSQLIndirection(ctx parser.IIndirectionContext) []string {- if ctx == nil {- return []string{}- }-- var res []string- for _, child := range ctx.AllIndirection_el() {- if part := normalizePostgreSQLIndirectionEl(child); part != "" {- res = append(res, part)- }- }- return res-}--func normalizePostgreSQLIndirectionEl(ctx parser.IIndirection_elContext) string {- if ctx == nil {- return ""- }-- if ctx.DOT() != nil {- if ctx.STAR() != nil {- return "*"- }- return normalizePostgreSQLAttrName(ctx.Attr_name())- }- // Array subscripts like col[expr] or col[start:end] are not part of the- // qualified column name — skip them so the base column name is preserved.- return ""-}--func normalizePostgreSQLAttrName(ctx parser.IAttr_nameContext) string {- return normalizePostgreSQLCollabel(ctx.Collabel())-}--func normalizePostgreSQLCollabel(ctx parser.ICollabelContext) string {- if ctx == nil {- return ""- }- if ctx.Identifier() != nil {- return normalizePostgreSQLIdentifier(ctx.Identifier())- }- return strings.ToLower(ctx.GetText())-}--// NormalizePostgreSQLColid normalizes the given colid.-func NormalizePostgreSQLColid(ctx parser.IColidContext) string {- if ctx == nil {- return ""- }-- if ctx.Identifier() != nil {- return normalizePostgreSQLIdentifier(ctx.Identifier())- }-- // For non-quote identifier, we just return the lower string for PostgreSQL.- return strings.ToLower(ctx.GetText())-}--func normalizePostgreSQLIdentifier(ctx parser.IIdentifierContext) string {- if ctx == nil {- return ""- }-- // TODO: handle USECAPE- switch {- case ctx.QuotedIdentifier() != nil:- return normalizePostgreSQLQuotedIdentifier(ctx.QuotedIdentifier().GetText())- case ctx.UnicodeQuotedIdentifier() != nil:- return normalizePostgreSQLUnicodeQuotedIdentifier(ctx.UnicodeQuotedIdentifier().GetText())- default:- return strings.ToLower(ctx.GetText())- }-}-+// normalizePostgreSQLQuotedIdentifier removes surrounding double quotes and unescapes internal ones.func normalizePostgreSQLQuotedIdentifier(s string) string {if len(s) < 2 {return s}-- // Remove the quote and unescape the quote.return strings.ReplaceAll(s[1:len(s)-1], `""`, `"`)}--func normalizePostgreSQLUnicodeQuotedIdentifier(s string) string {- // Do nothing for now.- return s-}--// NormalizePostgreSQLFuncName normalizes the given function name.-func NormalizePostgreSQLFuncName(ctx parser.IFunc_nameContext) []string {- if ctx == nil {- return []string{}- }-- var result []string-- // Handle type_function_name (simple identifiers)- if ctx.Type_function_name() != nil {- result = append(result, normalizePostgreSQLTypeFunctionName(ctx.Type_function_name()))- }-- // Handle qualified function name (colid + indirection)- if ctx.Colid() != nil {- result = append(result, NormalizePostgreSQLColid(ctx.Colid()))-- // Handle indirection for qualified names- if ctx.Indirection() != nil {- parts := normalizePostgreSQLIndirection(ctx.Indirection())- result = append(result, parts...)- }- }-- // Handle special keywords LEFT/RIGHT- if len(result) == 0 && ctx.GetText() != "" {- // Fallback for special cases like LEFT, RIGHT keywords- result = append(result, strings.ToLower(ctx.GetText()))- }-- return result-}--// normalizePostgreSQLTypeFunctionName normalizes a type_function_name context.-func normalizePostgreSQLTypeFunctionName(ctx parser.IType_function_nameContext) string {- if ctx == nil {- return ""- }-- // type_function_name can be identifier, unreserved_keyword, plsql_unreserved_keyword, or type_func_name_keyword- text := ctx.GetText()-- // Remove quotes if present and convert to lowercase for normalization- if len(text) >= 2 && text[0] == '"' && text[len(text)-1] == '"' {- // Quoted identifier - preserve case but remove quotes- return text[1 : len(text)-1]- }-- // Unquoted identifier - convert to lowercase- return strings.ToLower(text)-}--// NormalizePostgreSQLName normalizes the given name.-// nolint:revive-func NormalizePostgreSQLName(ctx parser.INameContext) string {- return normalizePostgreSQLName(ctx)-}diff --git a/backend/plugin/parser/pg/test-data/query_span.yaml b/backend/plugin/parser/pg/test-data/query_span.yamlindex 2ee137f959..4da0e1d298 100644--- a/backend/plugin/parser/pg/test-data/query_span.yaml+++ b/backend/plugin/parser/pg/test-data/query_span.yaml@@ -3143,4 +3143,9 @@schema: publictable: t1column: ""- predicatecolumns: []+ predicatecolumns:+ - server: ""+ database: db+ schema: public+ table: t1+ column: finished_atdiff --git a/backend/plugin/schema/expression_comparer.go b/backend/plugin/schema/expression_comparer.goindex abeb2a2a9e..8d2d9e3013 100644--- a/backend/plugin/schema/expression_comparer.go+++ b/backend/plugin/schema/expression_comparer.go@@ -4,19 +4,11 @@ import ("strings"storepb "github.com/bytebase/bytebase/backend/generated-go/store"- pgast "github.com/bytebase/bytebase/backend/plugin/schema/pg/ast")// CompareExpressionsSemantically compares two expressions semantically based on the engine type.-func CompareExpressionsSemantically(engine storepb.Engine, expr1, expr2 string) bool {- switch engine {- case storepb.Engine_POSTGRES:- return pgast.CompareExpressionsSemantically(expr1, expr2)- default:- // For unsupported engines, fall back to generic string-based comparison- // This is safer than using engine-specific logic for unknown engines- return compareExpressionsGeneric(expr1, expr2)- }+func CompareExpressionsSemantically(_ storepb.Engine, expr1, expr2 string) bool {+ return compareExpressionsGeneric(expr1, expr2)}// compareExpressionsGeneric provides a generic string-based comparison for unsupported enginesdiff --git a/backend/plugin/schema/pg/ast/expression_ast.go b/backend/plugin/schema/pg/ast/expression_ast.godeleted file mode 100644index e5df1c7f9e..0000000000--- a/backend/plugin/schema/pg/ast/expression_ast.go+++ /dev/null@@ -1,390 +0,0 @@-package ast //nolint:revive // intentional package name--import (- "fmt"- "strings"-)--// ExpressionType expression type enumeration-type ExpressionType int--const (- ExprTypeIdentifier ExpressionType = iota // identifier (column names, table names, etc.)- ExprTypeLiteral // literal values (numbers, strings, etc.)- ExprTypeBinaryOp // binary operators (+, -, =, <>, etc.)- ExprTypeUnaryOp // unary operators (NOT, -, etc.)- ExprTypeFunction // function calls- ExprTypeColumn // column references (may include table prefix)- ExprTypeParameter // parameter placeholders- ExprTypeList // expression lists (IN clauses, etc.)- ExprTypeParentheses // parentheses expressions-)--// ExpressionVisitor visitor pattern interface for expression AST-type ExpressionVisitor interface {- VisitIdentifier(expr *IdentifierExpr) any- VisitLiteral(expr *LiteralExpr) any- VisitBinaryOp(expr *BinaryOpExpr) any- VisitUnaryOp(expr *UnaryOpExpr) any- VisitFunction(expr *FunctionExpr) any- VisitList(expr *ListExpr) any- VisitParentheses(expr *ParenthesesExpr) any-}--// ExpressionAST abstract syntax tree node interface for expressions-type ExpressionAST interface {- Type() ExpressionType- String() string // normalized string representation- Equals(other ExpressionAST) bool // semantic equivalence comparison- Children() []ExpressionAST // child nodes- Accept(visitor ExpressionVisitor) any // visitor pattern support-}--// IdentifierExpr identifier expression-type IdentifierExpr struct {- Schema string // optional schema name- Table string // optional table name- Name string // identifier name-}--func (*IdentifierExpr) Type() ExpressionType {- return ExprTypeIdentifier-}--func (e *IdentifierExpr) String() string {- var parts []string- if e.Schema != "" {- parts = append(parts, e.Schema)- }- if e.Table != "" {- parts = append(parts, e.Table)- }- parts = append(parts, e.Name)- return strings.Join(parts, ".")-}--func (e *IdentifierExpr) Equals(other ExpressionAST) bool {- if other.Type() != ExprTypeIdentifier {- return false- }- o, ok := other.(*IdentifierExpr)- if !ok {- return false- }-- // Compare using normalized string representation to respect normalization settings- return e.String() == o.String()-}--func (*IdentifierExpr) Children() []ExpressionAST {- return nil-}--func (e *IdentifierExpr) Accept(visitor ExpressionVisitor) any {- return visitor.VisitIdentifier(e)-}--// LiteralExpr literal expression-type LiteralExpr struct {- Value string // literal value- ValueType string // type (string, number, boolean, null)-}--func (*LiteralExpr) Type() ExpressionType {- return ExprTypeLiteral-}--func (e *LiteralExpr) String() string {- return e.Value-}--func (e *LiteralExpr) Equals(other ExpressionAST) bool {- if other.Type() != ExprTypeLiteral {- return false- }- o, ok := other.(*LiteralExpr)- if !ok {- return false- }- return e.Value == o.Value && e.ValueType == o.ValueType-}--func (*LiteralExpr) Children() []ExpressionAST {- return nil-}--func (e *LiteralExpr) Accept(visitor ExpressionVisitor) any {- return visitor.VisitLiteral(e)-}--// BinaryOpExpr binary operation expression-type BinaryOpExpr struct {- Left ExpressionAST- Operator string // normalized operator (=, <>, +, -, AND, OR, etc.)- Right ExpressionAST-}--func (*BinaryOpExpr) Type() ExpressionType {- return ExprTypeBinaryOp-}--func (e *BinaryOpExpr) String() string {- return fmt.Sprintf("%s %s %s", e.Left.String(), e.Operator, e.Right.String())-}--func (e *BinaryOpExpr) Equals(other ExpressionAST) bool {- if other.Type() != ExprTypeBinaryOp {- return false- }- o, ok := other.(*BinaryOpExpr)- if !ok {- return false- }-- // normalize operators- op1 := normalizeOperator(e.Operator)- op2 := normalizeOperator(o.Operator)-- if op1 != op2 {- return false- }-- // for commutative operators, check both orders- if isCommutativeOperator(op1) {- return (e.Left.Equals(o.Left) && e.Right.Equals(o.Right)) ||- (e.Left.Equals(o.Right) && e.Right.Equals(o.Left))- }-- return e.Left.Equals(o.Left) && e.Right.Equals(o.Right)-}--func (e *BinaryOpExpr) Children() []ExpressionAST {- return []ExpressionAST{e.Left, e.Right}-}--func (e *BinaryOpExpr) Accept(visitor ExpressionVisitor) any {- return visitor.VisitBinaryOp(e)-}--// UnaryOpExpr unary operation expression-type UnaryOpExpr struct {- Operator string // operator (NOT, -, +, etc.)- Operand ExpressionAST // operand-}--func (*UnaryOpExpr) Type() ExpressionType {- return ExprTypeUnaryOp-}--func (e *UnaryOpExpr) String() string {- return fmt.Sprintf("%s %s", e.Operator, e.Operand.String())-}--func (e *UnaryOpExpr) Equals(other ExpressionAST) bool {- if other.Type() != ExprTypeUnaryOp {- return false- }- o, ok := other.(*UnaryOpExpr)- if !ok {- return false- }- return normalizeOperator(e.Operator) == normalizeOperator(o.Operator) &&- e.Operand.Equals(o.Operand)-}--func (e *UnaryOpExpr) Children() []ExpressionAST {- return []ExpressionAST{e.Operand}-}--func (e *UnaryOpExpr) Accept(visitor ExpressionVisitor) any {- return visitor.VisitUnaryOp(e)-}--// FunctionExpr function call expression-type FunctionExpr struct {- Schema string // optional schema name- Name string // function name- Args []ExpressionAST // argument list-}--func (*FunctionExpr) Type() ExpressionType {- return ExprTypeFunction-}--func (e *FunctionExpr) String() string {- var name string- if e.Schema != "" {- name = fmt.Sprintf("%s.%s", e.Schema, e.Name)- } else {- name = e.Name- }-- var args []string- for _, arg := range e.Args {- args = append(args, arg.String())- }-- return fmt.Sprintf("%s(%s)", name, strings.Join(args, ", "))-}--func (e *FunctionExpr) Equals(other ExpressionAST) bool {- if other.Type() != ExprTypeFunction {- return false- }- o, ok := other.(*FunctionExpr)- if !ok {- return false- }-- // Compare using string representation which handles normalization- return e.String() == o.String()-}--func (e *FunctionExpr) Children() []ExpressionAST {- return e.Args-}--func (e *FunctionExpr) Accept(visitor ExpressionVisitor) any {- return visitor.VisitFunction(e)-}--// ListExpr expression list (for IN clauses, etc.)-type ListExpr struct {- Elements []ExpressionAST-}--func (*ListExpr) Type() ExpressionType {- return ExprTypeList-}--func (e *ListExpr) String() string {- var elements []string- for _, elem := range e.Elements {- elements = append(elements, elem.String())- }- return fmt.Sprintf("(%s)", strings.Join(elements, ", "))-}--func (e *ListExpr) Equals(other ExpressionAST) bool {- if other.Type() != ExprTypeList {- return false- }- o, ok := other.(*ListExpr)- if !ok {- return false- }-- if len(e.Elements) != len(o.Elements) {- return false- }-- // for lists, order usually matters, but could consider unordered comparison- for i, elem := range e.Elements {- if !elem.Equals(o.Elements[i]) {- return false- }- }-- return true-}--func (e *ListExpr) Children() []ExpressionAST {- return e.Elements-}--func (e *ListExpr) Accept(visitor ExpressionVisitor) any {- return visitor.VisitList(e)-}--// ParenthesesExpr parentheses expression-type ParenthesesExpr struct {- Inner ExpressionAST-}--func (*ParenthesesExpr) Type() ExpressionType {- return ExprTypeParentheses-}--func (e *ParenthesesExpr) String() string {- if e.Inner == nil {- return "()"- }- return fmt.Sprintf("(%s)", e.Inner.String())-}--func (e *ParenthesesExpr) Equals(other ExpressionAST) bool {- // Handle nil inner expressions- if e.Inner == nil {- if other.Type() == ExprTypeParentheses {- if otherParen, ok := other.(*ParenthesesExpr); ok {- return otherParen.Inner == nil- }- }- return false- }-- // If the other expression is also parentheses, compare inner expressions- if other.Type() == ExprTypeParentheses {- if otherParen, ok := other.(*ParenthesesExpr); ok {- if otherParen.Inner == nil {- return false- }- return e.Inner.Equals(otherParen.Inner)- }- }-- // Otherwise, parentheses are usually ignored in semantic comparison- // Compare inner expression directly with the other expression- return e.Inner.Equals(other)-}--func (e *ParenthesesExpr) Children() []ExpressionAST {- if e.Inner == nil {- return nil- }- return []ExpressionAST{e.Inner}-}--func (e *ParenthesesExpr) Accept(visitor ExpressionVisitor) any {- return visitor.VisitParentheses(e)-}--// Helper functions--// normalizeOperator normalizes operators-func normalizeOperator(op string) string {- switch strings.ToUpper(op) {- case "!=":- return "<>"- case "&&":- return "AND"- case "||":- // In PostgreSQL, || is primarily string concatenation, but in boolean contexts- // it can be used as OR. For expression comparison, we treat || as OR since- // this is the most common semantic usage in filter expressions.- return "OR"- case "IS NULL", "ISNULL":- return "IS NULL"- case "IS NOT NULL", "ISNOTNULL":- return "IS NOT NULL"- case "::":- // PostgreSQL type cast operator- return "::"- default:- return strings.ToUpper(op)- }-}--// isCommutativeOperator checks if operator satisfies commutative property-func isCommutativeOperator(op string) bool {- commutativeOps := map[string]bool{- "=": true,- "<>": true,- "!=": true,- "+": true,- "*": true,- "AND": true,- "OR": true,- }- return commutativeOps[strings.ToUpper(op)]-}diff --git a/backend/plugin/schema/pg/ast/expression_comparer.go b/backend/plugin/schema/pg/ast/expression_comparer.godeleted file mode 100644index 1cc3b1fd6d..0000000000--- a/backend/plugin/schema/pg/ast/expression_comparer.go+++ /dev/null@@ -1,658 +0,0 @@-package ast //nolint:revive // intentional package name--import (- "strings"-- "github.com/pkg/errors"-)--// ExpressionComparer provides unified interface for expression semantic comparison-type ExpressionComparer interface {- // CompareExpressions compares two expressions for semantic equivalence- CompareExpressions(expr1, expr2 string) (bool, error)-- // ParseExpressionAST parses expression into AST- ParseExpressionAST(expr string) (ExpressionAST, error)-}--// PostgreSQLExpressionComparer PostgreSQL-specific expression comparer-type PostgreSQLExpressionComparer struct {- // Configuration options- IgnoreSchemaPrefix bool // ignore schema prefix (public.table -> table)- IgnoreParentheses bool // ignore semantically irrelevant parentheses- IgnoreWhitespace bool // ignore whitespace differences- CaseSensitive bool // case sensitive comparison-- // Internal components- parser *ExpressionParser- normalizer *ExpressionNormalizer-}--// NewPostgreSQLExpressionComparer creates a new PostgreSQL expression comparer-func NewPostgreSQLExpressionComparer() *PostgreSQLExpressionComparer {- return &PostgreSQLExpressionComparer{- IgnoreSchemaPrefix: true, // default to ignore public schema- IgnoreParentheses: true, // default to ignore irrelevant parentheses- IgnoreWhitespace: true, // default to ignore whitespace- CaseSensitive: false, // default case insensitive- parser: NewExpressionParser(),- normalizer: NewExpressionNormalizer(),- }-}--// WithConfig configures the comparer with specific options-func (c *PostgreSQLExpressionComparer) WithConfig(ignoreSchema, ignoreParens, ignoreWhitespace, caseSensitive bool) *PostgreSQLExpressionComparer {- c.IgnoreSchemaPrefix = ignoreSchema- c.IgnoreParentheses = ignoreParens- c.IgnoreWhitespace = ignoreWhitespace- c.CaseSensitive = caseSensitive-- // Create new normalizer with updated configuration- c.normalizer = &ExpressionNormalizer{- IgnoreSchemaPrefix: ignoreSchema,- IgnoreParentheses: ignoreParens,- IgnoreWhitespace: ignoreWhitespace,- CaseSensitive: caseSensitive,- }-- // Create new parser with updated configuration- c.parser = &ExpressionParser{- IgnoreSchemaPrefix: ignoreSchema,- CaseSensitive: caseSensitive,- }-- return c-}--// CompareExpressions compares two expressions for semantic equivalence-func (c *PostgreSQLExpressionComparer) CompareExpressions(expr1, expr2 string) (bool, error) {- // Quick check for identical strings- if expr1 == expr2 {- return true, nil- }-- // Handle empty expressions- if strings.TrimSpace(expr1) == "" && strings.TrimSpace(expr2) == "" {- return true, nil- }- if strings.TrimSpace(expr1) == "" || strings.TrimSpace(expr2) == "" {- return false, nil- }-- // Parse both expressions- ast1, err1 := c.parser.ParseExpression(expr1)- if err1 != nil {- // Fallback to string comparison if parsing fails- return c.compareExpressionsAsStrings(expr1, expr2), nil // nolint:nilerr- }-- ast2, err2 := c.parser.ParseExpression(expr2)- if err2 != nil {- // Fallback to string comparison if parsing fails- return c.compareExpressionsAsStrings(expr1, expr2), nil // nolint:nilerr- }-- if ast1 == nil && ast2 == nil {- return true, nil- }- if ast1 == nil || ast2 == nil {- return false, nil- }-- // Normalize both ASTs- normalized1 := c.normalizer.NormalizeExpression(ast1)- normalized2 := c.normalizer.NormalizeExpression(ast2)-- // Compare normalized ASTs- result := normalized1.Equals(normalized2)-- // If AST comparison fails, try fallback string comparison for special cases- if !result {- fallbackResult := c.compareExpressionsAsStrings(expr1, expr2)- return fallbackResult, nil- }-- return result, nil-}--// NormalizeExpression normalizes expression to canonical form-func (c *PostgreSQLExpressionComparer) NormalizeExpression(expr string) (string, error) {- if strings.TrimSpace(expr) == "" {- return "", nil- }-- // Parse expression- ast, err := c.parser.ParseExpression(expr)- if err != nil {- return "", err- }-- if ast == nil {- return "", nil- }-- // Normalize AST- normalized := c.normalizer.NormalizeExpression(ast)-- // Return string representation- return normalized.String(), nil-}--// ParseExpressionAST parses expression into AST-func (c *PostgreSQLExpressionComparer) ParseExpressionAST(expr string) (ExpressionAST, error) {- return c.parser.ParseExpression(expr)-}--// compareExpressionsAsStrings fallback string-based comparison-func (c *PostgreSQLExpressionComparer) compareExpressionsAsStrings(expr1, expr2 string) bool {- // Apply basic normalization- norm1 := c.normalizeStringExpression(expr1)- norm2 := c.normalizeStringExpression(expr2)-- if norm1 == norm2 {- return true- }-- // Handle PostgreSQL implicit type casts- return c.handleImplicitTypeCasts(norm1, norm2)-}--// normalizeStringExpression applies basic string normalization-func (c *PostgreSQLExpressionComparer) normalizeStringExpression(expr string) string {- // Remove extra whitespace- expr = strings.Join(strings.Fields(expr), " ")-- // Remove schema prefix if configured- if c.IgnoreSchemaPrefix {- expr = strings.ReplaceAll(expr, "public.", "")- }-- // Always normalize built-in types regardless of IgnoreSchemaPrefix setting- // This is necessary because built-in types should never be schema-qualified- expr = c.normalizeBuiltinTypes(expr)-- // Apply case normalization - but preserve string literal case- if !c.CaseSensitive {- expr = c.normalizeCasePreservingStringLiterals(expr)- }-- return strings.TrimSpace(expr)-}--// handleImplicitTypeCasts handles PostgreSQL implicit type casting scenarios-func (c *PostgreSQLExpressionComparer) handleImplicitTypeCasts(expr1, expr2 string) bool {- // Handle JSONB implicit casts- // '{}' should be equivalent to '{}'::jsonb- if c.isJSONBImplicitCast(expr1, expr2) {- return true- }-- // Handle BIT literal format differences- // B'1010' should be equivalent to '1010'::"bit"- if c.isBitLiteralEquivalent(expr1, expr2) {- return true- }-- // Handle array type implicit casts (already covered by AST comparison)- // 'ARRAY[]::TEXT[]' vs 'ARRAY[]::text[]'- if c.isArrayTypeImplicitCast(expr1, expr2) {- return true- }-- return false-}--// isJSONBImplicitCast checks if two expressions represent the same JSONB value with/without explicit cast-func (*PostgreSQLExpressionComparer) isJSONBImplicitCast(expr1, expr2 string) bool {- // Check if one has ::jsonb suffix and the other doesn't, but otherwise identical- if strings.HasSuffix(expr1, "::jsonb") && !strings.HasSuffix(expr2, "::jsonb") {- baseExpr1 := strings.TrimSuffix(expr1, "::jsonb")- baseExpr1 = strings.TrimSpace(baseExpr1)- return baseExpr1 == expr2- }- if strings.HasSuffix(expr2, "::jsonb") && !strings.HasSuffix(expr1, "::jsonb") {- baseExpr2 := strings.TrimSuffix(expr2, "::jsonb")- baseExpr2 = strings.TrimSpace(baseExpr2)- return baseExpr2 == expr1- }-- // Also handle JSON type casts (json vs jsonb)- if strings.HasSuffix(expr1, "::json") && !strings.HasSuffix(expr2, "::json") && !strings.HasSuffix(expr2, "::jsonb") {- baseExpr1 := strings.TrimSuffix(expr1, "::json")- baseExpr1 = strings.TrimSpace(baseExpr1)- return baseExpr1 == expr2- }- if strings.HasSuffix(expr2, "::json") && !strings.HasSuffix(expr1, "::json") && !strings.HasSuffix(expr1, "::jsonb") {- baseExpr2 := strings.TrimSuffix(expr2, "::json")- baseExpr2 = strings.TrimSpace(baseExpr2)- return baseExpr2 == expr1- }-- return false-}--// isBitLiteralEquivalent checks if two expressions represent the same BIT value in different formats-func (*PostgreSQLExpressionComparer) isBitLiteralEquivalent(expr1, expr2 string) bool {- // Check for b'...' vs '...'::"bit" patterns (lowercase b due to AST normalization)- if strings.HasPrefix(expr1, "b'") && strings.HasSuffix(expr1, "'") &&- strings.Contains(expr2, ":") && strings.Contains(expr2, "bit") {- // Extract bit value from b'...' format- bitValue1 := expr1[2 : len(expr1)-1] // Remove b'...'-- // Extract bit value from '...'::"bit" format- var bitValue2 string- if strings.HasPrefix(expr2, "'") && strings.Contains(expr2, "':") {- // Find the closing quote before the type cast- endQuotePos := strings.Index(expr2[1:], "'")- if endQuotePos > 0 {- bitValue2 = expr2[1 : endQuotePos+1] // Extract between first ' and second '- }- }-- return bitValue1 == bitValue2 && bitValue2 != ""- }-- // Check the reverse case: '...'::"bit" vs b'...'- if strings.HasPrefix(expr2, "b'") && strings.HasSuffix(expr2, "'") &&- strings.Contains(expr1, ":") && strings.Contains(expr1, "bit") {- // Extract bit value from b'...' format- bitValue2 := expr2[2 : len(expr2)-1] // Remove b'...'-- // Extract bit value from '...'::"bit" format- var bitValue1 string- if strings.HasPrefix(expr1, "'") && strings.Contains(expr1, "':") {- // Find the closing quote before the type cast- endQuotePos := strings.Index(expr1[1:], "'")- if endQuotePos > 0 {- bitValue1 = expr1[1 : endQuotePos+1] // Extract between first ' and second '- }- }-- return bitValue1 == bitValue2 && bitValue1 != ""- }-- return false-}--// isArrayTypeImplicitCast checks if two expressions represent array types with different case-func (c *PostgreSQLExpressionComparer) isArrayTypeImplicitCast(expr1, expr2 string) bool {- // This is primarily handled by the AST parser, but we can add string-level fallback- if c.CaseSensitive {- return false- }-- // Check for array type patterns like ARRAY[]::TYPE[] vs ARRAY[]::type[]- if strings.Contains(expr1, "ARRAY[]::") && strings.Contains(expr2, "ARRAY[]::") {- return strings.EqualFold(expr1, expr2)- }-- return false-}--// normalizeBuiltinTypes removes public schema qualification from built-in PostgreSQL types-func (*PostgreSQLExpressionComparer) normalizeBuiltinTypes(expr string) string {- // List of PostgreSQL built-in types that should not be schema-qualified- builtinTypes := []string{- "varbit", "bit", "bit varying",- "varchar", "character varying", "char", "character",- "text", "numeric", "decimal", "int", "integer", "bigint", "smallint",- "float", "real", "double precision", "boolean", "bool",- "date", "time", "timestamp", "timestamptz", "interval",- "uuid", "json", "jsonb", "bytea", "inet", "cidr",- "point", "line", "lseg", "box", "path", "polygon", "circle",- "int4range", "int8range", "numrange", "tsrange", "tstzrange", "daterange",- }-- for _, builtinType := range builtinTypes {- // Replace public.typename with typename- publicQualified := "public." + builtinType- if strings.Contains(expr, publicQualified) {- expr = strings.ReplaceAll(expr, publicQualified, builtinType)- }-- // Also handle quoted versions- quotedPublicQualified := "\"public\"." + builtinType- if strings.Contains(expr, quotedPublicQualified) {- expr = strings.ReplaceAll(expr, quotedPublicQualified, builtinType)- }- }-- return expr-}--// CompareExpressionLists compares two lists of expressions-func (c *PostgreSQLExpressionComparer) CompareExpressionLists(exprs1, exprs2 []string) (bool, error) {- if len(exprs1) != len(exprs2) {- return false, nil- }-- for i, expr1 := range exprs1 {- equal, err := c.CompareExpressions(expr1, exprs2[i])- if err != nil {- return false, err- }- if !equal {- return false, nil- }- }-- return true, nil-}--// CompareExpressionListsUnordered compares two lists of expressions ignoring order-func (c *PostgreSQLExpressionComparer) CompareExpressionListsUnordered(exprs1, exprs2 []string) (bool, error) {- if len(exprs1) != len(exprs2) {- return false, nil- }-- // For each expression in first list, find matching expression in second list- matched := make([]bool, len(exprs2))-- for _, expr1 := range exprs1 {- found := false- for j, expr2 := range exprs2 {- if matched[j] {- continue // already matched- }-- equal, err := c.CompareExpressions(expr1, expr2)- if err != nil {- return false, err- }-- if equal {- matched[j] = true- found = true- break- }- }-- if !found {- return false, nil- }- }-- return true, nil-}--// ValidateExpression validates an expression for correctness-func (c *PostgreSQLExpressionComparer) ValidateExpression(expr string) error {- if strings.TrimSpace(expr) == "" {- return errors.New("expression cannot be empty")- }-- // Parse expression to validate syntax- ast, err := c.parser.ParseExpression(expr)- if err != nil {- return errors.Wrap(err, "failed to parse expression")- }-- if ast == nil {- return errors.New("expression parsed to null AST")- }-- // Use validator visitor to check for semantic errors- validator := NewExpressionValidatorVisitor()- validationErrors := validator.Validate(ast)-- if len(validationErrors) > 0 {- return errors.Errorf("expression validation errors: %v", validationErrors)- }-- return nil-}--// GetExpressionComplexity returns a complexity score for an expression-func (c *PostgreSQLExpressionComparer) GetExpressionComplexity(expr string) (int, error) {- ast, err := c.parser.ParseExpression(expr)- if err != nil {- return 0, err- }-- if ast == nil {- return 0, nil- }-- return c.calculateComplexity(ast), nil-}--// calculateComplexity calculates complexity score recursively-func (c *PostgreSQLExpressionComparer) calculateComplexity(expr ExpressionAST) int {- if expr == nil {- return 0- }-- complexity := 1 // base complexity for any expression-- // Add complexity based on expression type- switch e := expr.(type) {- case *BinaryOpExpr:- complexity += 2 // binary operations are more complex- complexity += c.calculateComplexity(e.Left)- complexity += c.calculateComplexity(e.Right)- case *UnaryOpExpr:- complexity++- complexity += c.calculateComplexity(e.Operand)- case *FunctionExpr:- complexity += 3 // functions are complex- for _, arg := range e.Args {- complexity += c.calculateComplexity(arg)- }- case *ListExpr:- complexity += len(e.Elements)- for _, elem := range e.Elements {- complexity += c.calculateComplexity(elem)- }- case *ParenthesesExpr:- complexity += c.calculateComplexity(e.Inner)- case *IdentifierExpr:- // Simple identifier, base complexity already added- if e.Schema != "" {- complexity++- }- if e.Table != "" {- complexity++- }- case *LiteralExpr:- // Literal values are simple, base complexity already added- default:- }-- return complexity-}--// ExpressionStatistics provides statistics about expressions-type ExpressionStatistics struct {- TotalExpressions int- IdentifierCount int- LiteralCount int- BinaryOpCount int- UnaryOpCount int- FunctionCount int- ListCount int- ParenthesesCount int- AverageComplexity float64- MaxComplexity int- MinComplexity int-}--// AnalyzeExpressions analyzes a list of expressions and returns statistics-func (c *PostgreSQLExpressionComparer) AnalyzeExpressions(exprs []string) (*ExpressionStatistics, error) {- stats := &ExpressionStatistics{- TotalExpressions: len(exprs),- MinComplexity: int(^uint(0) >> 1), // max int- }-- totalComplexity := 0-- for _, expr := range exprs {- ast, err := c.parser.ParseExpression(expr)- if err != nil {- continue // skip invalid expressions- }-- if ast == nil {- continue- }-- // Count expression types- c.countExpressionTypes(ast, stats)-- // Calculate complexity- complexity := c.calculateComplexity(ast)- totalComplexity += complexity-- if complexity > stats.MaxComplexity {- stats.MaxComplexity = complexity- }- if complexity < stats.MinComplexity {- stats.MinComplexity = complexity- }- }-- if stats.TotalExpressions > 0 {- stats.AverageComplexity = float64(totalComplexity) / float64(stats.TotalExpressions)- }-- if stats.MinComplexity == int(^uint(0)>>1) {- stats.MinComplexity = 0- }-- return stats, nil-}--// countExpressionTypes recursively counts expression types-func (c *PostgreSQLExpressionComparer) countExpressionTypes(expr ExpressionAST, stats *ExpressionStatistics) {- if expr == nil {- return- }-- switch expr.(type) {- case *IdentifierExpr:- stats.IdentifierCount++- case *LiteralExpr:- stats.LiteralCount++- case *BinaryOpExpr:- stats.BinaryOpCount++- case *UnaryOpExpr:- stats.UnaryOpCount++- case *FunctionExpr:- stats.FunctionCount++- case *ListExpr:- stats.ListCount++- case *ParenthesesExpr:- stats.ParenthesesCount++- default:- }-- // Recursively count children- for _, child := range expr.Children() {- c.countExpressionTypes(child, stats)- }-}--// Global convenience functions--// CompareExpressionsSemantically compares two expressions semantically using default configuration-func CompareExpressionsSemantically(expr1, expr2 string) bool {- comparer := NewPostgreSQLExpressionComparer()- result, err := comparer.CompareExpressions(expr1, expr2)- if err != nil {- return false- }- return result-}--// normalizeCasePreservingStringLiterals normalizes case while preserving string literal content-func (*PostgreSQLExpressionComparer) normalizeCasePreservingStringLiterals(expr string) string {- // This is a production-grade implementation that preserves:- // 1. Single-quoted string literal case ('...')- // 2. Double-quoted identifier case ("...")- // while normalizing unquoted identifiers, keywords, and function names-- result := make([]rune, 0, len(expr))- runes := []rune(expr)- i := 0-- for i < len(runes) {- char := runes[i]-- // Handle single-quoted string literals- if char == '\'' {- // Preserve the entire string literal including quotes- result = append(result, char) // opening quote- i++-- // Copy content until closing quote, handling escaped quotes- for i < len(runes) {- char = runes[i]- result = append(result, char)-- if char == '\'' {- // Check if this is an escaped quote ''- if i+1 < len(runes) && runes[i+1] == '\'' {- // Escaped quote, copy both quotes- i++- result = append(result, runes[i])- } else {- // End of string literal- break- }- }- i++- }- } else if char == '"' {- // Handle double-quoted identifiers (case-sensitive)- // Preserve the entire quoted identifier including quotes- result = append(result, char) // opening quote- i++-- // Copy content until closing quote, handling escaped quotes- for i < len(runes) {- char = runes[i]- result = append(result, char)-- if char == '"' {- // Check if this is an escaped quote ""- if i+1 < len(runes) && runes[i+1] == '"' {- // Escaped quote, copy both quotes- i++- result = append(result, runes[i])- } else {- // End of quoted identifier- break- }- }- i++- }- } else {- // For non-quoted parts, apply case normalization- result = append(result, char)- }- i++- }-- // Apply case normalization to the parts outside quoted literals/identifiers- finalResult := string(result)-- // Process both single and double quotes to preserve their content- // First handle single quotes (string literals)- singleQuoteParts := strings.Split(finalResult, "'")- for i := 0; i < len(singleQuoteParts); i++ {- if i%2 == 0 {- // Outside single quotes - now check for double quotes- doubleQuoteParts := strings.Split(singleQuoteParts[i], `"`)- for j := 0; j < len(doubleQuoteParts); j++ {- if j%2 == 0 {- // Outside both single and double quotes - apply case normalization- doubleQuoteParts[j] = strings.ToLower(doubleQuoteParts[j])- }- // Inside double quotes - preserve case- }- singleQuoteParts[i] = strings.Join(doubleQuoteParts, `"`)- }- // Inside single quotes - preserve case- }-- return strings.Join(singleQuoteParts, "'")-}diff --git a/backend/plugin/schema/pg/ast/expression_comparer_production_test.go b/backend/plugin/schema/pg/ast/expression_comparer_production_test.godeleted file mode 100644index edf20dc7db..0000000000--- a/backend/plugin/schema/pg/ast/expression_comparer_production_test.go+++ /dev/null@@ -1,889 +0,0 @@-package ast //nolint:revive // intentional package name--import (- "fmt"- "strings"- "testing"-- "github.com/stretchr/testify/require"-)--// ProductionExpressionComparisonTest represents a comprehensive test case-// for production-grade expression comparison-type ProductionExpressionComparisonTest struct {- Name string // test case name- Description string // detailed description of what this tests- Expr1 string // first expression- Expr2 string // second expression- Expected bool // expected comparison result- Category string // test category (whitespace, case, operators, etc.)- Variants []string // additional variants that should also be equivalent-}--// TestPostgreSQLExpressionComparer_ProductionCases tests the expression comparer-// with comprehensive real-world scenarios focusing on different formats but identical semantics-func TestPostgreSQLExpressionComparer_ProductionCases(t *testing.T) {- comparer := NewPostgreSQLExpressionComparer()-- // Comprehensive test cases covering various semantic equivalence scenarios- testCases := []ProductionExpressionComparisonTest{- // === WHITESPACE NORMALIZATION ===- {- Name: "whitespace_around_operators",- Description: "Different whitespace around operators should be equivalent",- Expr1: "a = b",- Expr2: "a=b",- Expected: true,- Category: "whitespace",- Variants: []string{"a = b", "a\t=\tb", "a\n=\nb", " a = b ", " a = b "},- },- {- Name: "multiline_expressions",- Description: "Multiline expressions should be equivalent to single line",- Expr1: "column1 = 'value' AND column2 > 10",- Expr2: "column1 = 'value'\nAND column2 > 10",- Expected: true,- Category: "whitespace",- Variants: []string{- "column1 = 'value'\n AND column2 > 10",- "column1 = 'value' \n AND \n column2 > 10",- "column1='value'AND column2>10",- },- },- {- Name: "complex_whitespace_with_functions",- Description: "Function calls with varying whitespace",- Expr1: "UPPER(column_name)",- Expr2: "UPPER( column_name )",- Expected: true,- Category: "whitespace",- Variants: []string{"UPPER( column_name )", "UPPER(\tcolumn_name\t)", "UPPER(\ncolumn_name\n)"},- },-- // === CASE SENSITIVITY ===- {- Name: "function_name_case",- Description: "Function names should be case insensitive",- Expr1: "upper(column)",- Expr2: "UPPER(column)",- Expected: true,- Category: "case",- Variants: []string{"Upper(column)", "uPpEr(column)", "uPPER(column)"},- },- {- Name: "keyword_case",- Description: "SQL keywords should be case insensitive",- Expr1: "column1 AND column2",- Expr2: "column1 and column2",- Expected: true,- Category: "case",- Variants: []string{"column1 And column2", "column1 aNd column2", "column1 AnD column2"},- },- {- Name: "identifier_case",- Description: "Identifier names should be case insensitive by default",- Expr1: "Column_Name",- Expr2: "column_name",- Expected: true,- Category: "case",- Variants: []string{"COLUMN_NAME", "Column_name", "cOlUmN_nAmE"},- },- {- Name: "boolean_literal_case",- Description: "Boolean literals should be case insensitive",- Expr1: "TRUE",- Expr2: "true",- Expected: true,- Category: "case",- // Only include compatible variants for current implementation- Variants: []string{"True", "tRuE"},- },-- // === OPERATOR NORMALIZATION ===- {- Name: "inequality_operators",- Description: "Different inequality operators should be equivalent",- Expr1: "a != b",- Expr2: "a <> b",- Expected: true,- Category: "operators",- Variants: []string{"a != b", "a<>b", "a <> b"},- },- {- Name: "logical_and_operators",- Description: "Different AND operators should be equivalent",- Expr1: "a && b",- Expr2: "a AND b",- Expected: true,- Category: "operators",- Variants: []string{"a && b", "a and b", "a And b", "a AND b"},- },- {- Name: "logical_or_operators",- Description: "Different OR operators should be equivalent",- Expr1: "a OR b",- Expr2: "a or b",- Expected: true,- Category: "operators",- Variants: []string{"a Or b", "a OR b"},- },-- // === COMMUTATIVE OPERATIONS ===- {- Name: "equality_commutative",- Description: "Equality should be commutative",- Expr1: "a = b",- Expr2: "b = a",- Expected: true,- Category: "commutative",- },- {- Name: "addition_commutative",- Description: "Addition should be commutative",- Expr1: "a + b",- Expr2: "b + a",- Expected: true,- Category: "commutative",- },- {- Name: "multiplication_commutative",- Description: "Multiplication should be commutative",- Expr1: "a * b",- Expr2: "b * a",- Expected: true,- Category: "commutative",- },- {- Name: "logical_and_commutative",- Description: "Logical AND should be commutative",- Expr1: "condition1 AND condition2",- Expr2: "condition2 AND condition1",- Expected: true,- Category: "commutative",- },- {- Name: "logical_or_commutative",- Description: "Logical OR should be commutative",- Expr1: "condition1 OR condition2",- Expr2: "condition2 OR condition1",- Expected: true,- Category: "commutative",- },-- // === NON-COMMUTATIVE OPERATIONS ===- {- Name: "subtraction_not_commutative",- Description: "Subtraction should not be commutative",- Expr1: "a - b",- Expr2: "b - a",- Expected: false,- Category: "non_commutative",- },- {- Name: "division_not_commutative",- Description: "Division should not be commutative",- Expr1: "a / b",- Expr2: "b / a",- Expected: false,- Category: "non_commutative",- },- {- Name: "greater_than_not_commutative",- Description: "Greater than comparison should not be commutative",- Expr1: "a > b",- Expr2: "b > a",- Expected: false,- Category: "non_commutative",- },- {- Name: "less_than_not_commutative",- Description: "Less than comparison should not be commutative",- Expr1: "a < b",- Expr2: "b < a",- Expected: false,- Category: "non_commutative",- },-- // === PARENTHESES HANDLING ===- {- Name: "unnecessary_parentheses_simple",- Description: "Unnecessary parentheses around simple expressions should be ignored",- Expr1: "(column_name)",- Expr2: "column_name",- Expected: true,- Category: "parentheses",- // Test current behavior - only single level works- Variants: []string{},- },- {- Name: "unnecessary_parentheses_complex",- Description: "Unnecessary parentheses around complex expressions",- Expr1: "(a = b AND c = d)",- Expr2: "a = b AND c = d",- Expected: true,- Category: "parentheses",- // Remove variants that don't work with current implementation- Variants: []string{},- },- {- Name: "necessary_parentheses_precedence",- Description: "Necessary parentheses that affect precedence should be preserved",- Expr1: "(a + b) * c",- Expr2: "a + b * c",- Expected: false,- Category: "parentheses",- },-- // === SCHEMA PREFIX HANDLING ===- {- Name: "public_schema_prefix_ignored",- Description: "Public schema prefix should be ignored by default",- Expr1: "public.table.column",- Expr2: "table.column",- Expected: true,- Category: "schema",- Variants: []string{"public.table.column", "PUBLIC.table.column", "Public.table.column"},- },- {- Name: "both_public_schema_prefixes",- Description: "Both expressions with public schema should be equivalent",- Expr1: "public.table1.col1 = public.table2.col2",- Expr2: "table1.col1 = table2.col2",- Expected: true,- Category: "schema",- },- {- Name: "non_public_schema_preserved",- Description: "Non-public schema prefixes should be preserved and compared",- Expr1: "schema1.table.column",- Expr2: "schema2.table.column",- Expected: false,- Category: "schema",- },- {- Name: "function_schema_prefix",- Description: "Schema prefixes on functions should be handled",- Expr1: "public.upper(column)",- Expr2: "upper(column)",- Expected: true,- Category: "schema",- },-- // === FUNCTION CALLS ===- {- Name: "function_name_normalization",- Description: "Function names should be normalized consistently",- Expr1: "UPPER(column_name)",- Expr2: "upper(column_name)",- Expected: true,- Category: "functions",- },- {- Name: "function_argument_order",- Description: "Function argument order should matter",- Expr1: "SUBSTRING(text, 1, 5)",- Expr2: "SUBSTRING(text, 5, 1)",- Expected: false,- Category: "functions",- },- {- Name: "function_with_multiple_args",- Description: "Functions with multiple arguments",- Expr1: "COALESCE(col1, col2, 'default')",- Expr2: "coalesce(col1, col2, 'default')",- Expected: true,- Category: "functions",- },- {- Name: "nested_function_calls",- Description: "Nested function calls should be handled",- Expr1: "UPPER(TRIM(column_name))",- Expr2: "upper(trim(column_name))",- Expected: true,- Category: "functions",- },-- // === STRING LITERALS ===- {- Name: "single_quoted_strings",- Description: "Single quoted strings should be compared exactly",- Expr1: "'hello world'",- Expr2: "'hello world'",- Expected: true,- Category: "literals",- },- {- Name: "different_string_literals",- Description: "Different string literals should not be equal",- Expr1: "'hello'",- Expr2: "'world'",- Expected: false,- Category: "literals",- },- {- Name: "string_case_preservation",- Description: "String literal case should be preserved",- Expr1: "'Hello World'",- Expr2: "'hello world'",- Expected: false,- Category: "literals",- },- {- Name: "escaped_quotes_in_strings",- Description: "Escaped quotes in strings should be handled",- Expr1: "'don''t'",- Expr2: "'don''t'",- Expected: true,- Category: "literals",- },-- // === NUMERIC LITERALS ===- {- Name: "integer_literals",- Description: "Integer literals should be compared",- Expr1: "123",- Expr2: "123",- Expected: true,- Category: "literals",- },- {- Name: "decimal_literals",- Description: "Decimal literals should be compared",- Expr1: "123.456",- Expr2: "123.456",- Expected: true,- Category: "literals",- },- {- Name: "different_numeric_literals",- Description: "Different numeric literals should not be equal",- Expr1: "123",- Expr2: "456",- Expected: false,- Category: "literals",- },- {- Name: "scientific_notation",- Description: "Scientific notation literals",- Expr1: "1.23E+2",- Expr2: "1.23E+2",- Expected: true,- Category: "literals",- },-- // === COMPLEX REAL-WORLD EXPRESSIONS ===- {- Name: "check_constraint_equivalent",- Description: "Real-world check constraint expressions with formatting differences",- Expr1: "price > 0 AND quantity >= 0",- Expr2: "price>0 AND quantity>=0",- Expected: true,- Category: "real_world",- Variants: []string{- "price > 0 and quantity >= 0",- "PRICE > 0 AND QUANTITY >= 0",- " price > 0 AND quantity >= 0 ",- },- },- {- Name: "where_clause_equivalent",- Description: "WHERE clause expressions with different formatting",- Expr1: "status = 'active' AND created_at > '2023-01-01'",- Expr2: "STATUS='active'AND created_at>'2023-01-01'",- Expected: true,- Category: "real_world",- },- {- Name: "join_condition_equivalent",- Description: "JOIN condition expressions",- Expr1: "table1.id = table2.user_id",- Expr2: "table2.user_id = table1.id",- Expected: true,- Category: "real_world",- },- {- Name: "computed_column_expression",- Description: "Computed column expressions with functions",- Expr1: "COALESCE(first_name, '') || ' ' || COALESCE(last_name, '')",- Expr2: "coalesce(first_name, '') || ' ' || coalesce(last_name, '')",- Expected: true,- Category: "real_world",- },- {- Name: "simple_function_case",- Description: "Simple function call case differences",- Expr1: "UPPER(column_name)",- Expr2: "upper(column_name)",- Expected: true,- Category: "real_world",- },- {- Name: "basic_arithmetic",- Description: "Basic arithmetic expressions",- Expr1: "price * quantity",- Expr2: "PRICE * QUANTITY",- Expected: true,- Category: "real_world",- },- {- Name: "simple_comparison",- Description: "Simple comparison expressions",- Expr1: "age >= 18",- Expr2: "AGE >= 18",- Expected: true,- Category: "real_world",- },- {- Name: "like_pattern_equivalent",- Description: "LIKE pattern expressions",- Expr1: "name LIKE 'John%'",- Expr2: "name like 'John%'",- Expected: true,- Category: "real_world",- },- {- Name: "basic_null_check",- Description: "Basic NULL check expressions",- Expr1: "column_name",- Expr2: "COLUMN_NAME",- Expected: true,- Category: "real_world",- },-- // === INDEX EXPRESSIONS ===- {- Name: "btree_index_expression",- Description: "B-tree index expressions",- Expr1: "LOWER(email)",- Expr2: "lower(email)",- Expected: true,- Category: "index",- },- {- Name: "simple_column_index",- Description: "Simple column index expressions",- Expr1: "created_at",- Expr2: "CREATED_AT",- Expected: true,- Category: "index",- },- {- Name: "composite_index_simple",- Description: "Simple composite index expressions",- Expr1: "user_id",- Expr2: "USER_ID",- Expected: true,- Category: "index",- },-- // === FOREIGN KEY EXPRESSIONS ===- {- Name: "foreign_key_reference",- Description: "Foreign key reference expressions",- Expr1: "user_id",- Expr2: "user_id",- Expected: true,- Category: "foreign_key",- },- {- Name: "composite_foreign_key",- Description: "Composite foreign key expressions",- Expr1: "(tenant_id, user_id)",- Expr2: "(tenant_id, user_id)",- Expected: true,- Category: "foreign_key",- },-- // === VIEW DEFINITION EXPRESSIONS ===- {- Name: "view_column_list",- Description: "View column list expressions with formatting differences",- Expr1: "id",- Expr2: "ID",- Expected: true,- Category: "view",- },- {- Name: "view_where_condition",- Description: "View WHERE condition expressions",- Expr1: "active = true",- Expr2: "ACTIVE = TRUE",- Expected: true,- Category: "view",- },-- // === EDGE CASES ===- {- Name: "empty_expressions",- Description: "Both expressions are empty",- Expr1: "",- Expr2: "",- Expected: true,- Category: "edge_cases",- },- {- Name: "whitespace_only_expressions",- Description: "Both expressions contain only whitespace",- Expr1: " ",- Expr2: "\t\n ",- Expected: true,- Category: "edge_cases",- },- {- Name: "empty_vs_non_empty",- Description: "Empty expression compared to non-empty",- Expr1: "",- Expr2: "column",- Expected: false,- Category: "edge_cases",- },- }-- // Run main test cases- for _, tc := range testCases {- t.Run(tc.Name, func(t *testing.T) {- result, err := comparer.CompareExpressions(tc.Expr1, tc.Expr2)- require.NoError(t, err, "Unexpected error in test case: %s", tc.Name)- require.Equal(t, tc.Expected, result,- "Test case '%s' failed.\nDescription: %s\nExpr1: '%s'\nExpr2: '%s'\nExpected: %v, Got: %v",- tc.Name, tc.Description, tc.Expr1, tc.Expr2, tc.Expected, result)-- // Test variants if they exist- for i, variant := range tc.Variants {- variantResult, variantErr := comparer.CompareExpressions(tc.Expr1, variant)- require.NoError(t, variantErr, "Unexpected error in variant %d of test case: %s", i, tc.Name)- require.Equal(t, tc.Expected, variantResult,- "Test case '%s' variant %d failed.\nDescription: %s\nExpr1: '%s'\nVariant: '%s'\nExpected: %v, Got: %v",- tc.Name, i, tc.Description, tc.Expr1, variant, tc.Expected, variantResult)- }- })- }-}--// TestPostgreSQLExpressionComparer_ConfigurationVariations tests different configuration settings-func TestPostgreSQLExpressionComparer_ConfigurationVariations(t *testing.T) {- testCases := []struct {- name string- ignoreSchema bool- ignoreParens bool- ignoreWhitespace bool- caseSensitive bool- expr1 string- expr2 string- expectedResult bool- testDescription string- }{- {- name: "case_sensitive_enabled",- ignoreSchema: true,- ignoreParens: true,- ignoreWhitespace: true,- caseSensitive: true,- expr1: "Column_Name",- expr2: "column_name",- expectedResult: false,- testDescription: "Case sensitive mode should distinguish between different cases",- },- {- name: "case_sensitive_disabled",- ignoreSchema: true,- ignoreParens: true,- ignoreWhitespace: true,- caseSensitive: false,- expr1: "Column_Name",- expr2: "column_name",- expectedResult: true,- testDescription: "Case insensitive mode should ignore case differences",- },- {- name: "schema_prefix_preserved",- ignoreSchema: false,- ignoreParens: true,- ignoreWhitespace: true,- caseSensitive: false,- expr1: "public.table.column",- expr2: "table.column",- expectedResult: false,- testDescription: "With schema preservation, public schema should not be ignored",- },- {- name: "schema_prefix_ignored",- ignoreSchema: true,- ignoreParens: true,- ignoreWhitespace: true,- caseSensitive: false,- expr1: "public.table.column",- expr2: "table.column",- expectedResult: true,- testDescription: "With schema ignoring, public schema should be ignored",- },- {- name: "parentheses_significant",- ignoreSchema: true,- ignoreParens: false,- ignoreWhitespace: true,- caseSensitive: false,- expr1: "(a + b) * c",- expr2: "a + b * c",- expectedResult: false,- testDescription: "With parentheses preservation, semantically different parentheses should be significant",- },- {- name: "parentheses_ignored",- ignoreSchema: true,- ignoreParens: true,- ignoreWhitespace: true,- caseSensitive: false,- expr1: "(column_name)",- expr2: "column_name",- expectedResult: true,- testDescription: "With parentheses ignoring, unnecessary parentheses should be ignored",- },- {- name: "whitespace_significant",- ignoreSchema: true,- ignoreParens: true,- ignoreWhitespace: false,- caseSensitive: false,- expr1: "column1",- expr2: "column2",- expectedResult: false,- testDescription: "Different columns should be different regardless of whitespace settings",- },- {- name: "whitespace_ignored",- ignoreSchema: true,- ignoreParens: true,- ignoreWhitespace: true,- caseSensitive: false,- expr1: "a = b",- expr2: "a=b",- expectedResult: true,- testDescription: "With whitespace ignoring, whitespace differences should be ignored",- },- }-- for _, tc := range testCases {- t.Run(tc.name, func(t *testing.T) {- comparer := NewPostgreSQLExpressionComparer().WithConfig(- tc.ignoreSchema, tc.ignoreParens, tc.ignoreWhitespace, tc.caseSensitive)-- result, err := comparer.CompareExpressions(tc.expr1, tc.expr2)- require.NoError(t, err, "Unexpected error in configuration test: %s", tc.name)- require.Equal(t, tc.expectedResult, result,- "Configuration test '%s' failed.\nDescription: %s\nExpr1: '%s'\nExpr2: '%s'\nExpected: %v, Got: %v",- tc.name, tc.testDescription, tc.expr1, tc.expr2, tc.expectedResult, result)- })- }-}--// TestPostgreSQLExpressionComparer_BatchProcessing tests batch processing capabilities-func TestPostgreSQLExpressionComparer_BatchProcessing(t *testing.T) {- comparer := NewPostgreSQLExpressionComparer()-- // Test expression list comparison- t.Run("expression_lists_ordered", func(t *testing.T) {- list1 := []string{"col1", "col2", "col3"}- list2 := []string{"COL1", "Col2", "col3"}-- result, err := comparer.CompareExpressionLists(list1, list2)- require.NoError(t, err)- require.True(t, result, "Case differences in expression lists should be ignored")- })-- t.Run("expression_lists_unordered", func(t *testing.T) {- list1 := []string{"col1", "col2", "col3"}- list2 := []string{"col3", "col1", "col2"}-- result, err := comparer.CompareExpressionListsUnordered(list1, list2)- require.NoError(t, err)- require.True(t, result, "Order differences in expression lists should be ignored in unordered comparison")- })-- // Test normalization of expression lists- t.Run("normalize_expression_list", func(t *testing.T) {- expressions := []string{- "public.table.column",- "UPPER(name)",- "status != 'inactive'",- "(price > 0)",- }-- // Each expression should parse consistently- for _, expr := range expressions {- ast, err := comparer.ParseExpressionAST(expr)- require.NoError(t, err)- require.NotNil(t, ast, "AST should not be nil: %s", expr)-- // Test semantic comparison consistency- equal, err := comparer.CompareExpressions(expr, expr)- require.NoError(t, err)- require.True(t, equal, "Expression should be equal to itself: %s", expr)- }- })-}--// TestPostgreSQLExpressionComparer_PerformanceAndStatistics tests performance and statistics features-func TestPostgreSQLExpressionComparer_PerformanceAndStatistics(t *testing.T) {- comparer := NewPostgreSQLExpressionComparer()-- testExpressions := []string{- "simple_column",- "table.column",- "schema.table.column",- "UPPER(column)",- "a + b * c",- "(x = y AND z = w)",- "COALESCE(col1, col2, 'default')",- "status IN ('active', 'pending')",- "CASE WHEN x > 0 THEN 'positive' ELSE 'non-positive' END",- "EXTRACT(YEAR FROM created_at) = 2023",- }-- t.Run("expression_complexity", func(t *testing.T) {- for _, expr := range testExpressions {- complexity, err := comparer.GetExpressionComplexity(expr)- require.NoError(t, err, "Failed to get complexity for: %s", expr)- require.Greater(t, complexity, 0, "Complexity should be positive for: %s", expr)- }- })-- t.Run("expression_statistics", func(t *testing.T) {- stats, err := comparer.AnalyzeExpressions(testExpressions)- require.NoError(t, err)- require.Equal(t, len(testExpressions), stats.TotalExpressions)- require.Greater(t, stats.AverageComplexity, 0.0)- require.GreaterOrEqual(t, stats.MaxComplexity, stats.MinComplexity)- })-- t.Run("expression_validation", func(t *testing.T) {- validExpressions := []string{- "column_name",- "table.column",- "UPPER(column)",- "a = b",- }-- invalidExpressions := []string{- "",- " ",- }-- for _, expr := range validExpressions {- err := comparer.ValidateExpression(expr)- require.NoError(t, err, "Valid expression should not produce error: %s", expr)- }-- for _, expr := range invalidExpressions {- err := comparer.ValidateExpression(expr)- require.Error(t, err, "Invalid expression should produce error: %s", expr)- }- })-}--// BenchmarkPostgreSQLExpressionComparer_ProductionScenarios benchmarks realistic production scenarios-func BenchmarkPostgreSQLExpressionComparer_ProductionScenarios(b *testing.B) {- comparer := NewPostgreSQLExpressionComparer()-- scenarios := []struct {- name string- expr1 string- expr2 string- }{- {- name: "simple_column_comparison",- expr1: "column_name",- expr2: "COLUMN_NAME",- },- {- name: "complex_where_clause",- expr1: "public.users.status = 'active' AND users.created_at > '2023-01-01' AND users.email IS NOT NULL",- expr2: "users.status='active'AND users.created_at>'2023-01-01'AND users.email is not null",- },- {- name: "function_heavy_expression",- expr1: "UPPER(TRIM(COALESCE(first_name, ''))) || ' ' || UPPER(TRIM(COALESCE(last_name, '')))",- expr2: "upper(trim(coalesce(first_name, ''))) || ' ' || upper(trim(coalesce(last_name, '')))",- },- {- name: "mathematical_expression",- expr1: "(price * quantity * (1.0 + tax_rate)) - discount",- expr2: "(PRICE*QUANTITY*(1.0+TAX_RATE))-DISCOUNT",- },- }-- for _, scenario := range scenarios {- b.Run(scenario.name, func(b *testing.B) {- b.ResetTimer()- for i := 0; i < b.N; i++ {- _, _ = comparer.CompareExpressions(scenario.expr1, scenario.expr2)- }- })-- b.Run(scenario.name+"_parsing", func(b *testing.B) {- b.ResetTimer()- for i := 0; i < b.N; i++ {- _, _ = comparer.ParseExpressionAST(scenario.expr1)- }- })- }-}--// TestPostgreSQLExpressionComparer_RegressionTests tests for known edge cases and regressions-func TestPostgreSQLExpressionComparer_RegressionTests(t *testing.T) {- comparer := NewPostgreSQLExpressionComparer()-- regressionTests := []struct {- name string- description string- expr1 string- expr2 string- expected bool- }{- {- name: "quoted_identifiers_preserved",- description: "Quoted identifiers should preserve case and be different from unquoted",- expr1: `"Column_Name"`,- expr2: `column_name`,- expected: false,- },- {- name: "operator_precedence_respected",- description: "Operator precedence should be respected in comparisons",- expr1: `a + b * c`,- expr2: `(a + b) * c`,- expected: false,- },- {- name: "unicode_identifiers",- description: "Unicode identifiers should be handled correctly",- expr1: `test_column_name`,- expr2: `test_column_name`,- expected: true,- },- {- name: "very_long_expression",- description: "Very long expressions should be handled efficiently",- expr1: fmt.Sprintf("col%s", strings.Repeat("_part", 100)),- expr2: fmt.Sprintf("COL%s", strings.Repeat("_PART", 100)),- expected: true,- },- {- name: "nested_parentheses_deep",- description: "Deeply nested parentheses should be handled",- expr1: strings.Repeat("(", 20) + "column" + strings.Repeat(")", 20),- expr2: "column",- expected: true,- },- }-- for _, test := range regressionTests {- t.Run(test.name, func(t *testing.T) {- result, err := comparer.CompareExpressions(test.expr1, test.expr2)- require.NoError(t, err, "Regression test failed with error: %s", test.name)- require.Equal(t, test.expected, result,- "Regression test '%s' failed.\nDescription: %s\nExpr1: '%s'\nExpr2: '%s'\nExpected: %v, Got: %v",- test.name, test.description, test.expr1, test.expr2, test.expected, result)- })- }-}diff --git a/backend/plugin/schema/pg/ast/expression_comparer_strict_test.go b/backend/plugin/schema/pg/ast/expression_comparer_strict_test.godeleted file mode 100644index 79b68cdfb2..0000000000--- a/backend/plugin/schema/pg/ast/expression_comparer_strict_test.go+++ /dev/null@@ -1,541 +0,0 @@-package ast //nolint:revive // intentional package name--import (- "fmt"- "strings"- "testing"-- "github.com/stretchr/testify/require"-)--// TestPostgreSQLExpressionComparer_StrictProductionTests contains strict tests-// for production use. These tests SHOULD FAIL if the implementation is incomplete.-// When tests fail, it indicates implementation bugs that need to be fixed.-func TestPostgreSQLExpressionComparer_StrictProductionTests(t *testing.T) {- comparer := NewPostgreSQLExpressionComparer()-- // === CRITICAL PRODUCTION REQUIREMENTS ===- // These tests represent real-world scenarios that MUST work in production-- t.Run("OR_operator_equivalence", func(t *testing.T) {- // CRITICAL: PostgreSQL supports both || and OR operators- result, err := comparer.CompareExpressions("a || b", "a OR b")- require.NoError(t, err, "Should not error on valid PostgreSQL syntax")- require.True(t, result, "CRITICAL BUG: || and OR operators should be semantically equivalent in PostgreSQL")- })-- t.Run("IS_NULL_case_insensitive", func(t *testing.T) {- // CRITICAL: SQL keywords should be case insensitive- result, err := comparer.CompareExpressions("column IS NULL", "column is null")- require.NoError(t, err, "Should not error on valid SQL syntax")- require.True(t, result, "CRITICAL BUG: SQL keywords should be case insensitive")- })-- t.Run("BETWEEN_clause_equivalence", func(t *testing.T) {- // CRITICAL: BETWEEN clauses are common in production SQL- result, err := comparer.CompareExpressions("age BETWEEN 18 AND 65", "age between 18 and 65")- require.NoError(t, err, "Should not error on valid BETWEEN syntax")- require.True(t, result, "CRITICAL BUG: BETWEEN clauses should be case insensitive")- })-- t.Run("EXTRACT_function_equivalence", func(t *testing.T) {- // CRITICAL: EXTRACT is commonly used in PostgreSQL for date operations- result, err := comparer.CompareExpressions("EXTRACT(YEAR FROM created_at)", "extract(year from created_at)")- require.NoError(t, err, "Should not error on valid EXTRACT syntax")- require.True(t, result, "CRITICAL BUG: EXTRACT function should be case insensitive")- })-- t.Run("CASE_expression_equivalence", func(t *testing.T) {- // CRITICAL: CASE expressions are essential in production queries- expr1 := "CASE WHEN status = 'active' THEN 1 ELSE 0 END"- expr2 := "case when status = 'active' then 1 else 0 end"- result, err := comparer.CompareExpressions(expr1, expr2)- require.NoError(t, err, "Should not error on valid CASE syntax")- require.True(t, result, "CRITICAL BUG: CASE expressions should be case insensitive")- })-- t.Run("IN_clause_equivalence", func(t *testing.T) {- // CRITICAL: IN clauses are extremely common in production- expr1 := "status IN ('active', 'pending', 'approved')"- expr2 := "status in ('active', 'pending', 'approved')"- result, err := comparer.CompareExpressions(expr1, expr2)- require.NoError(t, err, "Should not error on valid IN syntax")- require.True(t, result, "CRITICAL BUG: IN clauses should be case insensitive")- })-- t.Run("complex_nested_parentheses", func(t *testing.T) {- // CRITICAL: Complex parentheses nesting should be handled- expr1 := "((column_name))"- expr2 := "column_name"- result, err := comparer.CompareExpressions(expr1, expr2)- require.NoError(t, err, "Should not error on nested parentheses")- require.True(t, result, "CRITICAL BUG: Nested unnecessary parentheses should be ignored")- })-- t.Run("parentheses_distribution", func(t *testing.T) {- // CRITICAL: Different parentheses grouping of same logical expression- expr1 := "(a = b) AND (c = d)"- expr2 := "a = b AND c = d"- result, err := comparer.CompareExpressions(expr1, expr2)- require.NoError(t, err, "Should not error on parentheses distribution")- require.True(t, result, "CRITICAL BUG: Unnecessary parentheses around AND clauses should be equivalent")- })-- t.Run("boolean_literal_variants", func(t *testing.T) {- // CRITICAL: All boolean literal formats should be equivalent- testCases := []struct {- expr1 string- expr2 string- expected bool- }{- {"TRUE", "true", true},- {"FALSE", "false", true},- {"True", "TRUE", true},- {"False", "FALSE", true},- {"TRUE", "FALSE", false}, // Different values should be different- }-- for i, tc := range testCases {- t.Run(fmt.Sprintf("boolean_case_%d", i), func(t *testing.T) {- result, err := comparer.CompareExpressions(tc.expr1, tc.expr2)- require.NoError(t, err, "Should not error on boolean literals")- require.Equal(t, tc.expected, result,- "CRITICAL BUG: Boolean literal comparison failed for %s vs %s", tc.expr1, tc.expr2)- })- }- })-- t.Run("complex_expression_list_parsing", func(t *testing.T) {- // CRITICAL: Complex expression lists in SELECT, GROUP BY, etc.- expr1 := "col1, col2, col3"- expr2 := "COL1, COL2, COL3"- result, err := comparer.CompareExpressions(expr1, expr2)- require.NoError(t, err, "Should not error on expression lists")- require.True(t, result, "CRITICAL BUG: Expression lists should be case insensitive")- })-- t.Run("schema_qualified_expressions", func(t *testing.T) {- // CRITICAL: Schema qualification is common in production databases- testCases := []struct {- name string- expr1 string- expr2 string- expected bool- }{- {- name: "public_schema_ignored",- expr1: "public.users.id",- expr2: "users.id",- expected: true,- },- {- name: "different_schemas_preserved",- expr1: "schema1.users.id",- expr2: "schema2.users.id",- expected: false,- },- {- name: "function_schema_qualification",- expr1: "public.upper(column)",- expr2: "upper(column)",- expected: true,- },- }-- for _, tc := range testCases {- t.Run(tc.name, func(t *testing.T) {- result, err := comparer.CompareExpressions(tc.expr1, tc.expr2)- require.NoError(t, err, "Should not error on schema-qualified expressions")- require.Equal(t, tc.expected, result,- "CRITICAL BUG: Schema qualification handling failed for %s vs %s", tc.expr1, tc.expr2)- })- }- })-}--// TestPostgreSQLExpressionComparer_EdgeCasesStrictMode tests edge cases that must work-func TestPostgreSQLExpressionComparer_EdgeCasesStrictMode(t *testing.T) {- comparer := NewPostgreSQLExpressionComparer()-- t.Run("quoted_identifiers_case_sensitivity", func(t *testing.T) {- testCases := []struct {- name string- expr1 string- expr2 string- shouldEqual bool- description string- }{- {- name: "quoted_vs_unquoted_different",- expr1: `"Column_Name"`,- expr2: `column_name`,- shouldEqual: false,- description: "Quoted identifiers should preserve case and differ from unquoted",- },- {- name: "quoted_vs_unquoted_same_case",- expr1: `"column_name"`,- expr2: `column_name`,- shouldEqual: true,- description: "Quoted lowercase should equal unquoted lowercase",- },- {- name: "quoted_identifiers_exact_match",- expr1: `"Column_Name"`,- expr2: `"Column_Name"`,- shouldEqual: true,- description: "Identical quoted identifiers should be equal",- },- {- name: "quoted_identifiers_case_different",- expr1: `"Column_Name"`,- expr2: `"column_name"`,- shouldEqual: false,- description: "Quoted identifiers with different cases should not be equal",- },- {- name: "quoted_identifiers_with_spaces",- expr1: `"Column Name"`,- expr2: `"Column Name"`,- shouldEqual: true,- description: "Quoted identifiers with spaces should work",- },- {- name: "quoted_identifiers_mixed_case_complex",- expr1: `"MyTable"."MyColumn"`,- expr2: `"mytable"."mycolumn"`,- shouldEqual: false,- description: "Complex quoted identifiers should preserve case sensitivity",- },- {- name: "quoted_table_unquoted_column",- expr1: `"MyTable".column_name`,- expr2: `"MyTable".COLUMN_NAME`,- shouldEqual: true,- description: "Unquoted parts should be case insensitive even with quoted parts",- },- {- name: "quoted_in_expressions",- expr1: `"User_ID" = 123`,- expr2: `"user_id" = 123`,- shouldEqual: false,- description: "Quoted identifiers in expressions should preserve case",- },- {- name: "quoted_function_names",- expr1: `"UPPER"("Column_Name")`,- expr2: `"upper"("Column_Name")`,- shouldEqual: false,- description: "Quoted function names should be case sensitive",- },- }-- for _, tc := range testCases {- t.Run(tc.name, func(t *testing.T) {- result, err := comparer.CompareExpressions(tc.expr1, tc.expr2)- require.NoError(t, err, "Should handle quoted identifiers: %s", tc.description)-- if tc.shouldEqual {- require.True(t, result, "CRITICAL BUG: %s - '%s' vs '%s'", tc.description, tc.expr1, tc.expr2)- } else {- require.False(t, result, "CRITICAL BUG: %s - '%s' vs '%s'", tc.description, tc.expr1, tc.expr2)- }- })- }- })-- t.Run("unicode_identifier_support", func(t *testing.T) {- // CRITICAL: Unicode identifiers should be supported in modern PostgreSQL- result, err := comparer.CompareExpressions("test_column_name", "test_column_name")- require.NoError(t, err, "Should support Unicode identifiers")- require.True(t, result, "CRITICAL BUG: Unicode identifiers should be supported")- })-- t.Run("very_long_expressions", func(t *testing.T) {- // CRITICAL: Should handle production-scale long expressions- longExpr1 := fmt.Sprintf("column_%s", strings.Repeat("very_long_name", 50))- longExpr2 := fmt.Sprintf("COLUMN_%s", strings.Repeat("VERY_LONG_NAME", 50))-- result, err := comparer.CompareExpressions(longExpr1, longExpr2)- require.NoError(t, err, "Should handle very long expressions without error")- require.True(t, result, "CRITICAL BUG: Very long expressions should work correctly")- })-- t.Run("deeply_nested_expressions", func(t *testing.T) {- // CRITICAL: Deep nesting should be handled (real queries can be very deep)- nested1 := strings.Repeat("(", 50) + "column" + strings.Repeat(")", 50)- nested2 := "column"-- result, err := comparer.CompareExpressions(nested1, nested2)- require.NoError(t, err, "Should handle deeply nested parentheses")- require.True(t, result, "CRITICAL BUG: Deeply nested unnecessary parentheses should be equivalent")- })-- t.Run("mixed_quote_types", func(t *testing.T) {- // CRITICAL: PostgreSQL supports different quote types- result, err := comparer.CompareExpressions("'hello'", "'hello'")- require.NoError(t, err, "Should handle string literals")- require.True(t, result, "String literals should be equal")-- // Different content should be different- result2, err2 := comparer.CompareExpressions("'hello'", "'world'")- require.NoError(t, err2, "Should handle different string literals")- require.False(t, result2, "Different string literals should not be equal")- })-}--// TestPostgreSQLExpressionComparer_RealWorldProductionQueries tests based on actual production patterns-func TestPostgreSQLExpressionComparer_RealWorldProductionQueries(t *testing.T) {- comparer := NewPostgreSQLExpressionComparer()-- t.Run("audit_log_expressions", func(t *testing.T) {- // Real audit log query patterns- expr1 := "created_at >= '2023-01-01' AND action IN ('INSERT', 'UPDATE', 'DELETE')"- expr2 := "CREATED_AT >= '2023-01-01' AND ACTION IN ('INSERT', 'UPDATE', 'DELETE')"-- result, err := comparer.CompareExpressions(expr1, expr2)- require.NoError(t, err, "Should handle audit log expressions")- require.True(t, result, "CRITICAL BUG: Audit log expressions should be case insensitive")- })-- t.Run("financial_calculations", func(t *testing.T) {- // Real financial calculation patterns- expr1 := "(price * quantity) * (1.0 + tax_rate) - discount"- expr2 := "(PRICE * QUANTITY) * (1.0 + TAX_RATE) - DISCOUNT"-- result, err := comparer.CompareExpressions(expr1, expr2)- require.NoError(t, err, "Should handle financial calculations")- require.True(t, result, "CRITICAL BUG: Financial calculations should be case insensitive")- })-- t.Run("user_permissions_check", func(t *testing.T) {- // Real permission checking patterns- expr1 := "user_role IN ('admin', 'moderator') OR user_id = owner_id"- expr2 := "USER_ROLE IN ('admin', 'moderator') OR USER_ID = OWNER_ID"-- result, err := comparer.CompareExpressions(expr1, expr2)- require.NoError(t, err, "Should handle permission checks")- require.True(t, result, "CRITICAL BUG: Permission check expressions should be case insensitive")- })-- t.Run("data_quality_validation", func(t *testing.T) {- // Real data validation patterns- expr1 := "email IS NOT NULL AND email LIKE '%@%.%' AND length(email) > 5"- expr2 := "EMAIL IS NOT NULL AND EMAIL LIKE '%@%.%' AND LENGTH(EMAIL) > 5"-- result, err := comparer.CompareExpressions(expr1, expr2)- require.NoError(t, err, "Should handle data validation")- require.True(t, result, "CRITICAL BUG: Data validation expressions should be case insensitive")- })-- t.Run("date_range_queries", func(t *testing.T) {- // Real date range patterns- expr1 := "created_at BETWEEN '2023-01-01' AND '2023-12-31'"- expr2 := "CREATED_AT BETWEEN '2023-01-01' AND '2023-12-31'"-- result, err := comparer.CompareExpressions(expr1, expr2)- require.NoError(t, err, "Should handle date ranges")- require.True(t, result, "CRITICAL BUG: Date range expressions should be case insensitive")- })-- t.Run("aggregation_with_conditions", func(t *testing.T) {- // Real aggregation patterns- expr1 := "SUM(CASE WHEN status = 'completed' THEN amount ELSE 0 END)"- expr2 := "sum(case when status = 'completed' then amount else 0 end)"-- result, err := comparer.CompareExpressions(expr1, expr2)- require.NoError(t, err, "Should handle conditional aggregations")- require.True(t, result, "CRITICAL BUG: Conditional aggregations should be case insensitive")- })-}--// TestPostgreSQLExpressionComparer_ConfigurationStrict tests configuration behavior strictly-func TestPostgreSQLExpressionComparer_ConfigurationStrict(t *testing.T) {- t.Run("case_sensitivity_must_work", func(t *testing.T) {- // CRITICAL: Case sensitivity configuration must work correctly- caseSensitive := NewPostgreSQLExpressionComparer().WithConfig(true, true, true, true)- caseInsensitive := NewPostgreSQLExpressionComparer().WithConfig(true, true, true, false)-- // Case sensitive should differentiate- result1, err1 := caseSensitive.CompareExpressions("Column_Name", "column_name")- require.NoError(t, err1, "Case sensitive comparer should not error")- require.False(t, result1, "CRITICAL BUG: Case sensitive mode should distinguish different cases")-- // Case insensitive should match- result2, err2 := caseInsensitive.CompareExpressions("Column_Name", "column_name")- require.NoError(t, err2, "Case insensitive comparer should not error")- require.True(t, result2, "CRITICAL BUG: Case insensitive mode should match different cases")- })-- t.Run("schema_handling_must_work", func(t *testing.T) {- // CRITICAL: Schema handling configuration must work- schemaIgnored := NewPostgreSQLExpressionComparer().WithConfig(true, true, true, false)- schemaPreserved := NewPostgreSQLExpressionComparer().WithConfig(false, true, true, false)-- // Schema ignored should match- result1, err1 := schemaIgnored.CompareExpressions("public.table.column", "table.column")- require.NoError(t, err1, "Schema ignored comparer should not error")- require.True(t, result1, "CRITICAL BUG: Schema ignored mode should match public schema")-- // Schema preserved should differentiate- result2, err2 := schemaPreserved.CompareExpressions("public.table.column", "table.column")- require.NoError(t, err2, "Schema preserved comparer should not error")- require.False(t, result2, "CRITICAL BUG: Schema preserved mode should distinguish schemas")- })-- t.Run("parentheses_handling_must_work", func(t *testing.T) {- // CRITICAL: Parentheses handling must be configurable- parensIgnored := NewPostgreSQLExpressionComparer().WithConfig(true, true, true, false)- parensPreserved := NewPostgreSQLExpressionComparer().WithConfig(true, false, true, false)-- // Parentheses ignored should match simple cases- result1, err1 := parensIgnored.CompareExpressions("(column_name)", "column_name")- require.NoError(t, err1, "Parentheses ignored comparer should not error")- require.True(t, result1, "CRITICAL BUG: Parentheses ignored mode should match simple parentheses")-- // Test meaningful parentheses that should never be ignored- result2, err2 := parensPreserved.CompareExpressions("(a + b) * c", "a + b * c")- require.NoError(t, err2, "Parentheses preserved comparer should not error")- require.False(t, result2, "CRITICAL BUG: Meaningful parentheses should always be preserved")- })-}--// TestPostgreSQLExpressionComparer_BatchOperationsStrict tests batch operations with strict requirements-func TestPostgreSQLExpressionComparer_BatchOperationsStrict(t *testing.T) {- comparer := NewPostgreSQLExpressionComparer()-- t.Run("large_expression_lists", func(t *testing.T) {- // CRITICAL: Should handle large lists efficiently- largeList1 := make([]string, 1000)- largeList2 := make([]string, 1000)-- for i := 0; i < 1000; i++ {- largeList1[i] = fmt.Sprintf("column_%d", i)- largeList2[i] = fmt.Sprintf("COLUMN_%d", i)- }-- result, err := comparer.CompareExpressionLists(largeList1, largeList2)- require.NoError(t, err, "Should handle large expression lists")- require.True(t, result, "CRITICAL BUG: Large expression lists should be comparable")- })-- t.Run("unordered_comparison_correctness", func(t *testing.T) {- // CRITICAL: Unordered comparison must be mathematically correct- list1 := []string{"a", "b", "c", "a"} // Contains duplicate- list2 := []string{"c", "a", "b", "a"} // Same elements, different order-- result, err := comparer.CompareExpressionListsUnordered(list1, list2)- require.NoError(t, err, "Should handle unordered comparison with duplicates")- require.True(t, result, "CRITICAL BUG: Unordered comparison should handle duplicates correctly")-- // Different lists should be different- list3 := []string{"a", "b", "c"}- list4 := []string{"a", "b", "d"} // One different element-- result2, err2 := comparer.CompareExpressionListsUnordered(list3, list4)- require.NoError(t, err2, "Should handle different unordered lists")- require.False(t, result2, "Different expression lists should not be equal")- })-}--// TestPostgreSQLExpressionComparer_ValidationStrict tests validation with strict requirements-func TestPostgreSQLExpressionComparer_ValidationStrict(t *testing.T) {- comparer := NewPostgreSQLExpressionComparer()-- t.Run("comprehensive_validation", func(t *testing.T) {- // CRITICAL: Should validate all types of valid expressions- validExpressions := []string{- "simple_column",- "schema.table.column",- "UPPER(column)",- "a = b AND c = d",- "CASE WHEN x > 0 THEN 'positive' ELSE 'negative' END",- "column IN (1, 2, 3)",- "column BETWEEN 1 AND 10",- "column IS NOT NULL",- "SUM(amount) OVER (PARTITION BY category)",- "EXTRACT(YEAR FROM date_column)",- }-- for _, expr := range validExpressions {- err := comparer.ValidateExpression(expr)- require.NoError(t, err, "CRITICAL BUG: Valid expression should not produce validation error: %s", expr)- }-- // Invalid expressions should be caught- invalidExpressions := []string{- "",- " ",- "\t\n",- }-- for _, expr := range invalidExpressions {- err := comparer.ValidateExpression(expr)- require.Error(t, err, "Invalid expression should produce validation error: %q", expr)- }- })-- t.Run("complexity_analysis_accuracy", func(t *testing.T) {- // CRITICAL: Complexity analysis should be meaningful and consistent- testCases := []struct {- expr string- minComplexity int- description string- }{- {"column", 1, "simple column"},- {"table.column", 2, "qualified column should be more complex"},- {"a = b", 3, "binary operation should be complex"},- {"a = b AND c = d", 6, "multiple operations should be very complex"},- {"UPPER(LOWER(column))", 4, "nested functions should be complex"},- }-- for _, tc := range testCases {- complexity, err := comparer.GetExpressionComplexity(tc.expr)- require.NoError(t, err, "Should calculate complexity for: %s", tc.expr)- require.GreaterOrEqual(t, complexity, tc.minComplexity,- "CRITICAL BUG: Complexity for %s (%s) should be at least %d, got %d",- tc.expr, tc.description, tc.minComplexity, complexity)- }- })-}--// TestPostgreSQLExpressionComparer_MemoryAndPerformance tests performance requirements-func TestPostgreSQLExpressionComparer_MemoryAndPerformance(t *testing.T) {- comparer := NewPostgreSQLExpressionComparer()-- t.Run("memory_efficiency_large_expressions", func(t *testing.T) {- // CRITICAL: Should handle large expressions without excessive memory use- largeExpr := strings.Repeat("column_name_", 1000) + "final"-- // Should not crash or consume excessive memory- result, err := comparer.CompareExpressions(largeExpr, strings.ToUpper(largeExpr))- require.NoError(t, err, "Should handle large expressions efficiently")- require.True(t, result, "Large expressions should be comparable")-- // Parsing should also work- ast, err := comparer.ParseExpressionAST(largeExpr)- require.NoError(t, err, "Should parse large expressions efficiently")- require.NotNil(t, ast, "Parsed AST should not be nil")- })-- t.Run("performance_consistency", func(t *testing.T) {- // CRITICAL: Performance should be consistent across different expression types- expressions := []string{- "simple",- "schema.table.column",- "UPPER(column)",- "a = b AND c = d OR e = f",- "(a + b) * (c - d) / (e + f)",- }-- for _, expr := range expressions {- // Each expression should complete quickly- start := testing.Short()- result, err := comparer.CompareExpressions(expr, strings.ToUpper(expr))- _ = start // Timing check in real implementation would go here-- require.NoError(t, err, "Expression should not cause performance issues: %s", expr)- require.True(t, result, "Expression should be comparable: %s", expr)- }- })-}diff --git a/backend/plugin/schema/pg/ast/expression_comparer_test.go b/backend/plugin/schema/pg/ast/expression_comparer_test.godeleted file mode 100644index 42b27169b8..0000000000--- a/backend/plugin/schema/pg/ast/expression_comparer_test.go+++ /dev/null@@ -1,635 +0,0 @@-package ast //nolint:revive // intentional package name--import (- "testing"-)--func TestPostgreSQLExpressionComparer_CompareExpressions(t *testing.T) {- comparer := NewPostgreSQLExpressionComparer()-- tests := []struct {- name string- expr1 string- expr2 string- expected bool- error bool- }{- // Basic equality tests- {- name: "identical expressions",- expr1: "column_name",- expr2: "column_name",- expected: true,- },- {- name: "case insensitive identifiers",- expr1: "Column_Name",- expr2: "column_name",- expected: true,- },- {- name: "different identifiers",- expr1: "column1",- expr2: "column2",- expected: false,- },-- // Schema prefix tests- {- name: "public schema ignored",- expr1: "public.table.column",- expr2: "table.column",- expected: true,- },- {- name: "both have public schema",- expr1: "public.table.column",- expr2: "public.table.column",- expected: true,- },- {- name: "non-public schema preserved",- expr1: "schema1.table.column",- expr2: "schema2.table.column",- expected: false,- },-- // Operator normalization tests- {- name: "inequality operators",- expr1: "a != b",- expr2: "a <> b",- expected: true,- },- {- name: "logical operators",- expr1: "a && b",- expr2: "a AND b",- expected: true,- },-- // Commutative operator tests- {- name: "commutative equality",- expr1: "a = b",- expr2: "b = a",- expected: true,- },- {- name: "commutative addition",- expr1: "a + b",- expr2: "b + a",- expected: true,- },- {- name: "non-commutative subtraction",- expr1: "a - b",- expr2: "b - a",- expected: false,- },-- // Parentheses tests- {- name: "irrelevant parentheses ignored",- expr1: "(column_name)",- expr2: "column_name",- expected: true,- },- {- name: "nested parentheses",- expr1: "((column_name))",- expr2: "column_name",- expected: true,- },- {- name: "where condition with parentheses",- expr1: "text_col IS NOT NULL",- expr2: "(text_col IS NOT NULL)",- expected: true,- },- {- name: "complex where condition with parentheses",- expr1: "column > 0 AND status = 'active'",- expr2: "(column > 0 AND status = 'active')",- expected: true,- },-- // Function call tests - basic cases- {- name: "function calls case insensitive",- expr1: "UPPER(column)",- expr2: "upper(column)",- expected: true,- },- {- name: "function with schema",- expr1: "public.UPPER(column)",- expr2: "UPPER(column)",- expected: true,- },- {- name: "function with different arguments",- expr1: "UPPER(col1)",- expr2: "UPPER(col2)",- expected: false,- },-- // Quoted vs unquoted function names - PostgreSQL identifier normalization- {- name: "unquoted LEFT vs quoted lowercase left - EQUAL",- expr1: "LEFT(text_col, 50)",- expr2: "\"left\"(text_col, 50)",- expected: true,- },- {- name: "unquoted left vs quoted lowercase left - EQUAL",- expr1: "left(text_col, 50)",- expr2: "\"left\"(text_col, 50)",- expected: true,- },- {- name: "unquoted LEFT vs quoted uppercase LEFT - NOT EQUAL",- expr1: "LEFT(text_col, 50)",- expr2: "\"LEFT\"(text_col, 50)",- expected: false,- },- {- name: "unquoted left vs quoted uppercase LEFT - NOT EQUAL",- expr1: "left(text_col, 50)",- expr2: "\"LEFT\"(text_col, 50)",- expected: false,- },- {- name: "unquoted LEFT vs quoted mixed case Left - NOT EQUAL",- expr1: "LEFT(text_col, 50)",- expr2: "\"Left\"(text_col, 50)",- expected: false,- },- {- name: "quoted function with complex expression",- expr1: "SUBSTRING(column FROM 1 FOR 10)",- expr2: "\"substring\"(column FROM 1 FOR 10)",- expected: true,- },-- // Additional identifier normalization tests- {- name: "unquoted Column_Name vs quoted lowercase column_name - EQUAL",- expr1: "Column_Name",- expr2: "\"column_name\"",- expected: true,- },- {- name: "unquoted Column_Name vs quoted original Column_Name - NOT EQUAL",- expr1: "Column_Name",- expr2: "\"Column_Name\"",- expected: false,- },-- // Complex expression tests- {- name: "complex equality expression",- expr1: "table1.col1 = table2.col2 AND status = 'active'",- expr2: "table1.col1 = table2.col2 AND status = 'active'",- expected: true,- },- {- name: "complex expression with whitespace differences",- expr1: "table1.col1=table2.col2 AND status='active'",- expr2: "table1.col1 = table2.col2 AND status = 'active'",- expected: true,- },-- // PostgreSQL type cast tests- {- name: "JSONB implicit cast",- expr1: "'{}'",- expr2: "'{}'::jsonb",- expected: true,- },- {- name: "JSONB explicit cast both ways",- expr1: "'{\"key\": \"value\"}'::jsonb",- expr2: "'{\"key\": \"value\"}'::jsonb",- expected: true,- },- {- name: "Array type cast case insensitive",- expr1: "ARRAY[]::TEXT[]",- expr2: "ARRAY[]::text[]",- expected: true,- },- {- name: "BIT literal formats",- expr1: "B'1010'",- expr2: "'1010'::\"bit\"",- expected: true,- },- {- name: "BIT literal formats reverse",- expr1: "'0000'::\"bit\"",- expr2: "B'0000'",- expected: true,- },- {- name: "VARBIT schema qualification",- expr1: "varbit(16)",- expr2: "public.varbit(16)",- expected: true,- },- {- name: "BIT schema qualification",- expr1: "bit(32)",- expr2: "public.bit(32)",- expected: true,- },-- // Literal tests- {- name: "string literals",- expr1: "'hello'",- expr2: "'hello'",- expected: true,- },- {- name: "different string literals",- expr1: "'hello'",- expr2: "'world'",- expected: false,- },- {- name: "numeric literals",- expr1: "123",- expr2: "123",- expected: true,- },- {- name: "boolean literals",- expr1: "TRUE",- expr2: "true",- expected: true,- },-- // Empty and null tests- {- name: "empty expressions",- expr1: "",- expr2: "",- expected: true,- },- {- name: "empty vs non-empty",- expr1: "",- expr2: "column",- expected: false,- },- {- name: "whitespace only expressions",- expr1: " ",- expr2: " ",- expected: true,- },- }-- for _, test := range tests {- t.Run(test.name, func(t *testing.T) {- result, err := comparer.CompareExpressions(test.expr1, test.expr2)-- if test.error {- if err == nil {- t.Error("Expected error but got none")- }- return- }-- if err != nil {- t.Errorf("Unexpected error: %v", err)- return- }-- if result != test.expected {- t.Errorf("Expected %v, got %v for comparing '%s' with '%s'",- test.expected, result, test.expr1, test.expr2)- }- })- }-}--func TestPostgreSQLExpressionComparer_CompareExpressionLists(t *testing.T) {- comparer := NewPostgreSQLExpressionComparer()-- tests := []struct {- name string- exprs1 []string- exprs2 []string- expected bool- }{- {- name: "identical lists",- exprs1: []string{"col1", "col2", "col3"},- exprs2: []string{"col1", "col2", "col3"},- expected: true,- },- {- name: "case differences",- exprs1: []string{"COL1", "Col2", "col3"},- exprs2: []string{"col1", "col2", "col3"},- expected: true,- },- {- name: "schema prefix differences",- exprs1: []string{"public.col1", "col2"},- exprs2: []string{"col1", "col2"},- expected: true,- },- {- name: "different lengths",- exprs1: []string{"col1", "col2"},- exprs2: []string{"col1", "col2", "col3"},- expected: false,- },- {- name: "different expressions",- exprs1: []string{"col1", "col2"},- exprs2: []string{"col1", "col3"},- expected: false,- },- {- name: "empty lists",- exprs1: []string{},- exprs2: []string{},- expected: true,- },- }-- for _, test := range tests {- t.Run(test.name, func(t *testing.T) {- result, err := comparer.CompareExpressionLists(test.exprs1, test.exprs2)-- if err != nil {- t.Errorf("Unexpected error: %v", err)- return- }-- if result != test.expected {- t.Errorf("Expected %v, got %v for comparing %v with %v",- test.expected, result, test.exprs1, test.exprs2)- }- })- }-}--func TestPostgreSQLExpressionComparer_CompareExpressionListsUnordered(t *testing.T) {- comparer := NewPostgreSQLExpressionComparer()-- tests := []struct {- name string- exprs1 []string- exprs2 []string- expected bool- }{- {- name: "same order",- exprs1: []string{"col1", "col2", "col3"},- exprs2: []string{"col1", "col2", "col3"},- expected: true,- },- {- name: "different order",- exprs1: []string{"col1", "col2", "col3"},- exprs2: []string{"col3", "col1", "col2"},- expected: true,- },- {- name: "case and order differences",- exprs1: []string{"COL1", "Col2", "col3"},- exprs2: []string{"col3", "col1", "col2"},- expected: true,- },- {- name: "different expressions",- exprs1: []string{"col1", "col2", "col3"},- exprs2: []string{"col1", "col2", "col4"},- expected: false,- },- {- name: "duplicate handling",- exprs1: []string{"col1", "col1", "col2"},- exprs2: []string{"col2", "col1", "col1"},- expected: true,- },- }-- for _, test := range tests {- t.Run(test.name, func(t *testing.T) {- result, err := comparer.CompareExpressionListsUnordered(test.exprs1, test.exprs2)-- if err != nil {- t.Errorf("Unexpected error: %v", err)- return- }-- if result != test.expected {- t.Errorf("Expected %v, got %v for comparing %v with %v (unordered)",- test.expected, result, test.exprs1, test.exprs2)- }- })- }-}--func TestPostgreSQLExpressionComparer_ValidateExpression(t *testing.T) {- comparer := NewPostgreSQLExpressionComparer()-- tests := []struct {- name string- expr string- expectError bool- }{- {- name: "valid simple identifier",- expr: "column_name",- expectError: false,- },- {- name: "valid qualified identifier",- expr: "table.column",- expectError: false,- },- {- name: "valid function call",- expr: "UPPER(column)",- expectError: false,- },- {- name: "valid binary operation",- expr: "a = b",- expectError: false,- },- {- name: "valid complex expression",- expr: "table1.col1 = table2.col2 AND status = 'active'",- expectError: false,- },- {- name: "empty expression",- expr: "",- expectError: true,- },- {- name: "whitespace only",- expr: " ",- expectError: true,- },- }-- for _, test := range tests {- t.Run(test.name, func(t *testing.T) {- err := comparer.ValidateExpression(test.expr)-- if test.expectError {- if err == nil {- t.Errorf("Expected error but got none for expression: %s", test.expr)- }- } else {- if err != nil {- t.Errorf("Unexpected error for expression '%s': %v", test.expr, err)- }- }- })- }-}--func TestPostgreSQLExpressionComparer_GetExpressionComplexity(t *testing.T) {- comparer := NewPostgreSQLExpressionComparer()-- tests := []struct {- name string- expr string- minScore int // minimum expected complexity score- }{- {- name: "simple identifier",- expr: "column",- minScore: 1,- },- {- name: "qualified identifier",- expr: "table.column",- minScore: 2,- },- {- name: "binary operation",- expr: "a = b",- minScore: 4, // binary op + 2 identifiers- },- {- name: "function call",- expr: "UPPER(column)",- minScore: 5, // function + identifier- },- {- name: "complex expression",- expr: "table1.col1 = table2.col2 AND status = 'active'",- minScore: 10, // multiple operations and identifiers- },- }-- for _, test := range tests {- t.Run(test.name, func(t *testing.T) {- score, err := comparer.GetExpressionComplexity(test.expr)-- if err != nil {- t.Errorf("Unexpected error: %v", err)- return- }-- if score < test.minScore {- t.Errorf("Expected complexity >= %d, got %d for expression: %s",- test.minScore, score, test.expr)- }- })- }-}--func TestCompareExpressionsSemantically(t *testing.T) {- tests := []struct {- name string- expr1 string- expr2 string- expected bool- }{- {- name: "basic comparison",- expr1: "column_name",- expr2: "column_name",- expected: true,- },- {- name: "case insensitive",- expr1: "Column_Name",- expr2: "column_name",- expected: true,- },- {- name: "schema prefix ignored",- expr1: "public.table.column",- expr2: "table.column",- expected: true,- },- {- name: "different expressions",- expr1: "col1",- expr2: "col2",- expected: false,- },- }-- for _, test := range tests {- t.Run(test.name, func(t *testing.T) {- result := CompareExpressionsSemantically(test.expr1, test.expr2)-- if result != test.expected {- t.Errorf("Expected %v, got %v for comparing '%s' with '%s'",- test.expected, result, test.expr1, test.expr2)- }- })- }-}--func TestPostgreSQLExpressionComparer_WithConfig(t *testing.T) {- // Test case-sensitive configuration- comparer := NewPostgreSQLExpressionComparer().WithConfig(true, true, true, true)-- result, err := comparer.CompareExpressions("Column_Name", "column_name")- if err != nil {- t.Errorf("Unexpected error: %v", err)- }-- // With case sensitivity enabled, these should be different- if result {- t.Error("Expected case-sensitive comparison to return false")- }-- // Test schema prefix preservation- comparer = NewPostgreSQLExpressionComparer().WithConfig(false, true, true, false)-- result, err = comparer.CompareExpressions("public.table.column", "table.column")- if err != nil {- t.Errorf("Unexpected error: %v", err)- }-- // With schema prefix preservation, these should be different- if result {- t.Error("Expected schema-sensitive comparison to return false")- }-}--func BenchmarkCompareExpressionsSemantically(b *testing.B) {- expr1 := "public.table1.column1 = table2.column2 AND status = 'active'"- expr2 := "table1.column1 = table2.column2 AND status = 'active'"-- b.ResetTimer()- for i := 0; i < b.N; i++ {- CompareExpressionsSemantically(expr1, expr2)- }-}diff --git a/backend/plugin/schema/pg/ast/expression_normalizer.go b/backend/plugin/schema/pg/ast/expression_normalizer.godeleted file mode 100644index a51c50aec6..0000000000--- a/backend/plugin/schema/pg/ast/expression_normalizer.go+++ /dev/null@@ -1,398 +0,0 @@-package ast //nolint:revive // intentional package name--import (- "strings"-)--// ExpressionNormalizer normalizes expressions for semantic comparison-type ExpressionNormalizer struct {- // Configuration options- IgnoreSchemaPrefix bool // ignore public schema prefix- IgnoreParentheses bool // ignore semantically irrelevant parentheses- IgnoreWhitespace bool // ignore whitespace differences- CaseSensitive bool // case sensitive comparison-}--// NewExpressionNormalizer creates a new expression normalizer-func NewExpressionNormalizer() *ExpressionNormalizer {- return &ExpressionNormalizer{- IgnoreSchemaPrefix: true, // default to ignore public schema- IgnoreParentheses: true, // default to ignore irrelevant parentheses- IgnoreWhitespace: true, // default to ignore whitespace- CaseSensitive: false, // default case insensitive- }-}--// NormalizeExpression normalizes an expression AST for comparison-func (n *ExpressionNormalizer) NormalizeExpression(expr ExpressionAST) ExpressionAST {- if expr == nil {- return nil- }-- visitor := &normalizationVisitor{normalizer: n}- result := expr.Accept(visitor)- if normalized, ok := result.(ExpressionAST); ok {- return normalized- }- return expr-}--// NormalizeExpressionString normalizes an expression string for comparison-func (n *ExpressionNormalizer) NormalizeExpressionString(exprStr string) (string, error) {- parser := NewExpressionParser()- ast, err := parser.ParseExpression(exprStr)- if err != nil {- return "", err- }-- if ast == nil {- return "", nil- }-- normalized := n.NormalizeExpression(ast)- return normalized.String(), nil-}--// normalizationVisitor implements the visitor pattern for normalization-type normalizationVisitor struct {- normalizer *ExpressionNormalizer-}--// VisitIdentifier normalizes identifier expressions-func (v *normalizationVisitor) VisitIdentifier(expr *IdentifierExpr) any {- schema := v.normalizeSchema(expr.Schema)- table := v.normalizeIdentifier(expr.Table)- name := v.normalizeIdentifier(expr.Name)-- // Special handling for two-part identifiers where first part might be public schema- if schema == "" && table != "" && strings.ToLower(table) == "public" && v.normalizer.IgnoreSchemaPrefix {- // This is likely public.column, treat as just column- table = ""- }-- normalized := &IdentifierExpr{- Schema: schema,- Table: table,- Name: name,- }- return normalized-}--// VisitLiteral normalizes literal expressions-func (v *normalizationVisitor) VisitLiteral(expr *LiteralExpr) any {- normalized := &LiteralExpr{- Value: v.normalizeLiteral(expr.Value, expr.ValueType),- ValueType: expr.ValueType,- }- return normalized-}--// VisitBinaryOp normalizes binary operation expressions-func (v *normalizationVisitor) VisitBinaryOp(expr *BinaryOpExpr) any {- left := v.normalizeChild(expr.Left)- operator := v.normalizeOperator(expr.Operator)-- // Special handling for type cast operator- var right ExpressionAST- if operator == "::" {- // For type cast, the right operand should be normalized as a type name- right = v.normalizeChildAsType(expr.Right)-- // Check if this is a redundant type cast that can be removed- if v.isRedundantTypeCast(left, right) {- return left // Return the left operand without the cast- }- } else {- right = v.normalizeChild(expr.Right)- }-- // For commutative operators, ensure consistent ordering- if isCommutativeOperator(operator) && v.shouldSwapOperands(left, right) {- left, right = right, left- }-- return &BinaryOpExpr{- Left: left,- Operator: operator,- Right: right,- }-}--// VisitUnaryOp normalizes unary operation expressions-func (v *normalizationVisitor) VisitUnaryOp(expr *UnaryOpExpr) any {- return &UnaryOpExpr{- Operator: v.normalizeOperator(expr.Operator),- Operand: v.normalizeChild(expr.Operand),- }-}--// VisitFunction normalizes function expressions-func (v *normalizationVisitor) VisitFunction(expr *FunctionExpr) any {- var normalizedArgs []ExpressionAST- for _, arg := range expr.Args {- normalizedArgs = append(normalizedArgs, v.normalizeChild(arg))- }-- return &FunctionExpr{- Schema: v.normalizeSchema(expr.Schema),- Name: v.normalizeFunctionName(expr.Name),- Args: normalizedArgs,- }-}--// VisitList normalizes list expressions-func (v *normalizationVisitor) VisitList(expr *ListExpr) any {- var normalizedElements []ExpressionAST- for _, elem := range expr.Elements {- normalizedElements = append(normalizedElements, v.normalizeChild(elem))- }-- // For some contexts, list order might not matter, but default to preserving order- return &ListExpr{- Elements: normalizedElements,- }-}--// VisitParentheses normalizes parentheses expressions-func (v *normalizationVisitor) VisitParentheses(expr *ParenthesesExpr) any {- inner := v.normalizeChild(expr.Inner)-- // If configured to ignore irrelevant parentheses, return inner expression- if v.normalizer.IgnoreParentheses && !v.isSignificantParentheses(expr) {- return inner- }-- return &ParenthesesExpr{Inner: inner}-}--// Helper methods for normalization--// normalizeChild normalizes a child expression-func (v *normalizationVisitor) normalizeChild(expr ExpressionAST) ExpressionAST {- if expr == nil {- return nil- }- result := expr.Accept(v)- if normalized, ok := result.(ExpressionAST); ok {- return normalized- }- return expr-}--// normalizeChildAsType normalizes a child expression as a type name-func (v *normalizationVisitor) normalizeChildAsType(expr ExpressionAST) ExpressionAST {- if expr == nil {- return nil- }-- // If it's a literal, treat it as a type name- if literal, ok := expr.(*LiteralExpr); ok {- normalizedValue := v.normalizeTypeName(literal.Value)- return &LiteralExpr{- Value: normalizedValue,- ValueType: literal.ValueType,- }- }-- // Otherwise, normalize normally- return v.normalizeChild(expr)-}--// normalizeSchema normalizes schema names-func (v *normalizationVisitor) normalizeSchema(schema string) string {- if schema == "" {- return ""- }-- // Remove public schema if configured to ignore it- if v.normalizer.IgnoreSchemaPrefix && strings.ToLower(schema) == "public" {- return ""- }-- return v.normalizeIdentifier(schema)-}--// normalizeIdentifier normalizes identifier names according to PostgreSQL rules:-// - Unquoted identifiers are case-insensitive and folded to lowercase-// - Quoted identifiers preserve their original case but without quotes in the IR-func (v *normalizationVisitor) normalizeIdentifier(identifier string) string {- if identifier == "" {- return ""- }-- // Handle quoted identifiers- if v.isQuotedIdentifier(identifier) {- // Remove quotes and preserve the original case content- unquoted := identifier[1 : len(identifier)-1]- // Handle escaped quotes within the identifier- unquoted = strings.ReplaceAll(unquoted, `""`, `"`)- return unquoted- }-- // For unquoted identifiers, apply PostgreSQL case folding (to lowercase)- return v.normalizeUnquotedIdentifier(identifier)-}--// normalizeUnquotedIdentifier normalizes unquoted identifiers-func (v *normalizationVisitor) normalizeUnquotedIdentifier(identifier string) string {- // For unquoted identifiers, apply case normalization- if v.normalizer.CaseSensitive {- return identifier- }-- return strings.ToLower(identifier)-}--// normalizeFunctionName normalizes function names using PostgreSQL standard rules-// Same logic as normalizeIdentifier - function names are identifiers-func (v *normalizationVisitor) normalizeFunctionName(name string) string {- // Function names follow the same identifier normalization rules- return v.normalizeIdentifier(name)-}--// normalizeLiteral normalizes literal values-func (v *normalizationVisitor) normalizeLiteral(value, valueType string) string {- switch strings.ToLower(valueType) {- case "boolean":- return strings.ToUpper(value) // TRUE/FALSE- case "null":- return "NULL"- case "string":- // Normalize string literals - preserve quotes and content- return value- case "number":- // Normalize numeric literals - could handle leading zeros, etc.- return value- case "type":- // Type names in cast expressions should be case-insensitive- return v.normalizeTypeName(value)- default:- return value- }-}--// normalizeTypeName normalizes type names for case-insensitive comparison-func (v *normalizationVisitor) normalizeTypeName(typeName string) string {- if v.normalizer.CaseSensitive {- return typeName- }- return strings.ToLower(typeName)-}--// normalizeOperator normalizes operators-func (*normalizationVisitor) normalizeOperator(operator string) string {- return normalizeOperator(operator) // Use the helper from expression_ast.go-}--// shouldSwapOperands determines if operands should be swapped for consistent ordering-func (*normalizationVisitor) shouldSwapOperands(left, right ExpressionAST) bool {- // Simple ordering based on string representation- // This ensures consistent ordering for commutative operators- leftStr := left.String()- rightStr := right.String()- return leftStr > rightStr-}--// isSignificantParentheses determines if parentheses are semantically significant-func (*normalizationVisitor) isSignificantParentheses(expr *ParenthesesExpr) bool {- // Parentheses are significant if they change operator precedence or grouping-- inner := expr.Inner- if inner == nil {- return false- }-- // Parentheses around simple identifiers or literals are never significant- // This is the key insight: (name) and name are always semantically equivalent- switch inner.(type) {- case *IdentifierExpr, *LiteralExpr:- return false- }-- // Parentheses around binary operations might be significant for precedence- if binaryOp, isBinaryOp := inner.(*BinaryOpExpr); isBinaryOp {- // For most practical cases in filter expressions, parentheses around- // high-precedence operators (=, <>, <, >, etc.) are not significant- // when used with lower-precedence operators (AND, OR)- op := strings.ToUpper(binaryOp.Operator)-- // High precedence comparison operators usually don't need parentheses- highPrecedenceOps := map[string]bool{- "=": true,- "<>": true,- "!=": true,- "<": true,- ">": true,- "<=": true,- ">=": true,- "LIKE": true,- "ILIKE": true,- "IS NULL": true,- "IS NOT NULL": true,- "::": true, // Type cast operator- }-- if highPrecedenceOps[op] {- return false // These operators have high precedence, parentheses usually not needed- }-- // For AND/OR operators, parentheses might be significant for grouping- // But in simple cases like (A AND B) vs A AND B, they're not significant- if op == "AND" || op == "OR" {- return false // Allow AND/OR to be ungrouped for simple cases- }-- // Be conservative for other operators- return true- }-- // Be conservative for other expression types (functions, etc.)- return true-}--// isQuotedIdentifier checks if an identifier is quoted-func (*normalizationVisitor) isQuotedIdentifier(identifier string) bool {- return len(identifier) >= 2 &&- ((identifier[0] == '"' && identifier[len(identifier)-1] == '"') ||- (identifier[0] == '`' && identifier[len(identifier)-1] == '`'))-}--// isRedundantTypeCast checks if a type cast can be safely removed-// This handles cases where PostgreSQL implicit casting makes explicit casts unnecessary-func (v *normalizationVisitor) isRedundantTypeCast(leftExpr, typeExpr ExpressionAST) bool {- // Check if left is a string literal and right is a JSON/JSONB type- if literalExpr, ok := leftExpr.(*LiteralExpr); ok {- if typeIdent, ok := typeExpr.(*IdentifierExpr); ok {- typeName := strings.ToLower(typeIdent.Name)-- // For JSON-like literals cast to jsonb/json, the cast is redundant- // because PostgreSQL will implicitly cast string literals to JSON types- if (typeName == "jsonb" || typeName == "json") &&- literalExpr.ValueType == "string" &&- v.isJSONLiteral(literalExpr.Value) {- return true- }- }- }-- return false-}--// isJSONLiteral checks if a string value looks like JSON-func (*normalizationVisitor) isJSONLiteral(value string) bool {- // Remove quotes if present- if len(value) >= 2 && value[0] == '\'' && value[len(value)-1] == '\'' {- value = value[1 : len(value)-1]- }-- // Check for simple JSON patterns- trimmed := strings.TrimSpace(value)- return (strings.HasPrefix(trimmed, "{") && strings.HasSuffix(trimmed, "}")) ||- (strings.HasPrefix(trimmed, "[") && strings.HasSuffix(trimmed, "]")) ||- trimmed == "null" ||- trimmed == "true" ||- trimmed == "false" ||- (len(trimmed) > 0 && (trimmed[0] == '"' ||- (trimmed[0] >= '0' && trimmed[0] <= '9') ||- trimmed[0] == '-'))-}diff --git a/backend/plugin/schema/pg/ast/expression_parser.go b/backend/plugin/schema/pg/ast/expression_parser.godeleted file mode 100644index 6f99be56b0..0000000000--- a/backend/plugin/schema/pg/ast/expression_parser.go+++ /dev/null@@ -1,859 +0,0 @@-package ast //nolint:revive // intentional package name--import (- "strings"-- "github.com/antlr4-go/antlr/v4"- pgparser "github.com/bytebase/parser/postgresql"-)--// ExpressionParser parses PostgreSQL expressions into AST-type ExpressionParser struct {- // Configuration options- IgnoreSchemaPrefix bool // ignore public schema prefix- CaseSensitive bool // case sensitive comparison-}--// NewExpressionParser creates a new expression parser-func NewExpressionParser() *ExpressionParser {- return &ExpressionParser{- IgnoreSchemaPrefix: true, // default to ignore public schema- CaseSensitive: false, // default case insensitive- }-}--// ParseExpression parses a PostgreSQL expression into AST-func (p *ExpressionParser) ParseExpression(expr string) (ExpressionAST, error) {- if strings.TrimSpace(expr) == "" {- return nil, nil- }-- // Use ANTLR to parse the expression- inputStream := antlr.NewInputStream(expr)- lexer := pgparser.NewPostgreSQLLexer(inputStream)- stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)- stream.Fill()- tokens := stream.GetAllTokens()-- // Build AST from tokens using a simple token-based approach- // This is more practical than full grammar parsing for expressions- return p.buildASTFromTokens(tokens)-}--// buildASTFromTokens builds AST from ANTLR tokens-func (p *ExpressionParser) buildASTFromTokens(tokens []antlr.Token) (ExpressionAST, error) {- if len(tokens) == 0 {- return nil, nil- }-- // Filter out whitespace and EOF tokens- var filteredTokens []antlr.Token- for _, token := range tokens {- if !p.isWhitespaceToken(token.GetTokenType()) && token.GetText() != "<EOF>" {- filteredTokens = append(filteredTokens, token)- }- }-- if len(filteredTokens) == 0 {- return nil, nil- }-- // Parse expression using recursive descent approach- return p.parseExpressionTokens(filteredTokens, 0, len(filteredTokens))-}--// parseExpressionTokens parses expression tokens recursively-func (p *ExpressionParser) parseExpressionTokens(tokens []antlr.Token, start, end int) (ExpressionAST, error) {- if start >= end {- return nil, nil- }-- // Handle parentheses - only if they truly wrap the entire expression- if tokens[start].GetText() == "(" && tokens[end-1].GetText() == ")" {- // Check if the opening parenthesis has a matching closing parenthesis at the end- // AND there are no other top-level operators outside these parentheses- if p.isBalancedParentheses(tokens, start, end) && p.parenthesesWrapEntireExpression(tokens, start, end) {- inner, err := p.parseExpressionTokens(tokens, start+1, end-1)- if err != nil {- return nil, err- }- return &ParenthesesExpr{Inner: inner}, nil- }- }-- // Check for expression lists (comma-separated expressions)- // Only parse as list if there are actually commas at the top level- if listExpr := p.parseExpressionList(tokens, start, end); listExpr != nil {- return listExpr, nil- }-- // Find binary operators (precedence handling)- if binaryExpr := p.parseBinaryOperation(tokens, start, end); binaryExpr != nil {- return binaryExpr, nil- }-- // Find unary operators- if unaryExpr := p.parseUnaryOperation(tokens, start, end); unaryExpr != nil {- return unaryExpr, nil- }-- // Parse function calls- if funcExpr := p.parseFunctionCall(tokens, start, end); funcExpr != nil {- return funcExpr, nil- }-- // Parse single token (identifier or literal)- if end-start == 1 {- return p.parseSingleToken(tokens[start])- }-- // Parse qualified identifier (schema.table.column)- if qualifiedExpr := p.parseQualifiedIdentifier(tokens, start, end); qualifiedExpr != nil {- return qualifiedExpr, nil- }-- // Fallback: create a literal expression with the concatenated text- var parts []string- for i := start; i < end; i++ {- parts = append(parts, tokens[i].GetText())- }- return &LiteralExpr{- Value: strings.Join(parts, " "),- ValueType: "unknown",- }, nil-}--// parseMultiKeywordOperators parses multi-keyword operators like IS NULL, BETWEEN...AND, etc.-func (p *ExpressionParser) parseMultiKeywordOperators(tokens []antlr.Token, start, end int) ExpressionAST {- // Look for CASE expressions first (they are most complex)- if caseExpr := p.parseCaseExpression(tokens, start, end); caseExpr != nil {- return caseExpr- }-- // Look for IS NULL / IS NOT NULL patterns- if nullExpr := p.parseIsNullOperator(tokens, start, end); nullExpr != nil {- return nullExpr- }-- // Look for BETWEEN...AND patterns- return p.parseBetweenOperator(tokens, start, end)-}--// parseCaseExpression parses CASE WHEN...THEN...ELSE...END expressions-func (p *ExpressionParser) parseCaseExpression(tokens []antlr.Token, start, end int) ExpressionAST {- // Look for CASE...END pattern- if start < end && strings.ToUpper(tokens[start].GetText()) == "CASE" &&- strings.ToUpper(tokens[end-1].GetText()) == "END" {- // Parse the CASE expression content between CASE and END- var caseArgs []ExpressionAST-- // Add a marker to identify this as a CASE expression- caseArgs = append(caseArgs, &LiteralExpr{Value: "CASE", ValueType: "keyword"})-- // Parse WHEN...THEN pairs and optional ELSE- i := start + 1- for i < end-1 {- tokenText := strings.ToUpper(tokens[i].GetText())-- if tokenText == "WHEN" {- // Find the corresponding THEN- thenPos := -1- parenLevel := 0- for j := i + 1; j < end-1; j++ {- if tokens[j].GetText() == "(" {- parenLevel++- } else if tokens[j].GetText() == ")" {- parenLevel--- } else if parenLevel == 0 && strings.ToUpper(tokens[j].GetText()) == "THEN" {- thenPos = j- break- }- }-- if thenPos != -1 {- // Parse condition between WHEN and THEN- condition, _ := p.parseExpressionTokens(tokens, i+1, thenPos)- if condition != nil {- caseArgs = append(caseArgs, condition)- }-- // Find the next WHEN, ELSE, or END to determine the THEN value range- nextPos := end - 1 // default to END- parenLevel = 0- for j := thenPos + 1; j < end-1; j++ {- if tokens[j].GetText() == "(" {- parenLevel++- } else if tokens[j].GetText() == ")" {- parenLevel--- } else if parenLevel == 0 {- nextToken := strings.ToUpper(tokens[j].GetText())- if nextToken == "WHEN" || nextToken == "ELSE" {- nextPos = j- break- }- }- }-- // Parse value between THEN and next keyword- value, _ := p.parseExpressionTokens(tokens, thenPos+1, nextPos)- if value != nil {- caseArgs = append(caseArgs, value)- }-- i = nextPos- } else {- i++- }- } else if tokenText == "ELSE" {- // Parse ELSE value (everything from ELSE to END)- elseValue, _ := p.parseExpressionTokens(tokens, i+1, end-1)- if elseValue != nil {- caseArgs = append(caseArgs, elseValue)- }- break- } else {- i++- }- }-- return &FunctionExpr{- Name: "CASE",- Args: caseArgs,- }- }-- return nil-}--// parseIsNullOperator parses IS NULL and IS NOT NULL operators-func (p *ExpressionParser) parseIsNullOperator(tokens []antlr.Token, start, end int) ExpressionAST {- // Look for IS NULL pattern (minimum 3 tokens: operand IS NULL)- for i := start + 1; i < end-1; i++ {- if p.getParenthesesLevel(tokens, start, i) == 0 {- isToken := strings.ToUpper(tokens[i].GetText())-- if isToken == "IS" {- // Check for IS NOT NULL (4 tokens after IS)- if i+2 < end {- notToken := strings.ToUpper(tokens[i+1].GetText())- nullToken := strings.ToUpper(tokens[i+2].GetText())-- if notToken == "NOT" && nullToken == "NULL" {- // Parse: operand IS NOT NULL- left, _ := p.parseExpressionTokens(tokens, start, i)- if left != nil {- return &BinaryOpExpr{- Left: left,- Operator: "IS NOT NULL",- Right: &LiteralExpr{Value: "NULL", ValueType: "null"},- }- }- }- }-- // Check for IS NULL (2 tokens after IS)- if i+1 < end {- nullToken := strings.ToUpper(tokens[i+1].GetText())-- if nullToken == "NULL" {- // Parse: operand IS NULL- left, _ := p.parseExpressionTokens(tokens, start, i)- if left != nil {- return &BinaryOpExpr{- Left: left,- Operator: "IS NULL",- Right: &LiteralExpr{Value: "NULL", ValueType: "null"},- }- }- }- }- }- }- }-- return nil-}--// parseBetweenOperator parses BETWEEN...AND operators-func (p *ExpressionParser) parseBetweenOperator(tokens []antlr.Token, start, end int) ExpressionAST {- // Look for BETWEEN pattern (minimum 5 tokens: operand BETWEEN value1 AND value2)- for i := start + 1; i < end-3; i++ {- if p.getParenthesesLevel(tokens, start, i) == 0 {- betweenToken := strings.ToUpper(tokens[i].GetText())-- if betweenToken == "BETWEEN" {- // Find the AND keyword- for j := i + 2; j < end-1; j++ {- if p.getParenthesesLevel(tokens, i+1, j) == 0 {- andToken := strings.ToUpper(tokens[j].GetText())-- if andToken == "AND" {- // Parse: operand BETWEEN value1 AND value2- left, _ := p.parseExpressionTokens(tokens, start, i)- value1, _ := p.parseExpressionTokens(tokens, i+1, j)- value2, _ := p.parseExpressionTokens(tokens, j+1, end)-- if left != nil && value1 != nil && value2 != nil {- // Create a special function-like expression for BETWEEN- return &FunctionExpr{- Name: "BETWEEN",- Args: []ExpressionAST{left, value1, value2},- }- }- }- }- }- }- }- }-- return nil-}--// parseExpressionList parses comma-separated expression lists-func (p *ExpressionParser) parseExpressionList(tokens []antlr.Token, start, end int) ExpressionAST {- // Look for comma separators at the top level (not inside parentheses)- var commaPositions []int- parenLevel := 0-- for i := start; i < end; i++ {- tokenText := tokens[i].GetText()- if tokenText == "(" {- parenLevel++- } else if tokenText == ")" {- parenLevel--- } else if tokenText == "," && parenLevel == 0 {- commaPositions = append(commaPositions, i)- }- }-- // If we found commas at the top level, parse as expression list- if len(commaPositions) > 0 {- var elements []ExpressionAST-- // Parse elements between commas, but use a non-recursive approach for list elements- prevPos := start- for _, commaPos := range commaPositions {- if commaPos > prevPos {- element := p.parseNonListExpression(tokens, prevPos, commaPos)- if element != nil {- elements = append(elements, element)- }- }- prevPos = commaPos + 1- }-- // Parse last element- if prevPos < end {- element := p.parseNonListExpression(tokens, prevPos, end)- if element != nil {- elements = append(elements, element)- }- }-- if len(elements) > 1 {- return &ListExpr{Elements: elements}- }- }-- return nil-}--// parseNonListExpression parses expressions but excludes list parsing to avoid infinite recursion-func (p *ExpressionParser) parseNonListExpression(tokens []antlr.Token, start, end int) ExpressionAST {- if start >= end {- return nil- }-- // Handle parentheses- if tokens[start].GetText() == "(" && tokens[end-1].GetText() == ")" {- // Check if parentheses are balanced and wrap the entire expression- if p.isBalancedParentheses(tokens, start, end) {- inner := p.parseNonListExpression(tokens, start+1, end-1)- if inner != nil {- return &ParenthesesExpr{Inner: inner}- }- }- }-- // Find binary operators (precedence handling)- if binaryExpr := p.parseBinaryOperation(tokens, start, end); binaryExpr != nil {- return binaryExpr- }-- // Find unary operators- if unaryExpr := p.parseUnaryOperation(tokens, start, end); unaryExpr != nil {- return unaryExpr- }-- // Parse function calls- if funcExpr := p.parseFunctionCall(tokens, start, end); funcExpr != nil {- return funcExpr- }-- // Parse single token (identifier or literal)- if end-start == 1 {- result, _ := p.parseSingleToken(tokens[start])- return result- }-- // Parse qualified identifier (schema.table.column)- if qualifiedExpr := p.parseQualifiedIdentifier(tokens, start, end); qualifiedExpr != nil {- return qualifiedExpr- }-- // Fallback: create a literal expression with the concatenated text- var parts []string- for i := start; i < end; i++ {- parts = append(parts, tokens[i].GetText())- }- return &LiteralExpr{- Value: strings.Join(parts, " "),- ValueType: "unknown",- }-}--// parseBinaryOperation parses binary operations with precedence-func (p *ExpressionParser) parseBinaryOperation(tokens []antlr.Token, start, end int) ExpressionAST {- // Check for multi-keyword operators first- if multiKeywordExpr := p.parseMultiKeywordOperators(tokens, start, end); multiKeywordExpr != nil {- return multiKeywordExpr- }-- // Precedence levels (lowest to highest)- precedenceLevels := [][]string{- {"OR", "||"}, // lowest precedence- {"AND", "&&"},- {"=", "<>", "!=", "<", ">", "<=", ">=", "LIKE", "ILIKE"},- {"+", "-"},- {"*", "/", "%"},- {"::"}, // type cast has high precedence- }-- // Find operators from lowest to highest precedence- for _, operators := range precedenceLevels {- for i := end - 2; i > start; i-- { // scan right to left for left associativity- if p.containsOperator(operators, tokens[i].GetText()) {- // Make sure this operator is not inside parentheses- if p.getParenthesesLevel(tokens, start, i) == 0 {- left, _ := p.parseExpressionTokens(tokens, start, i)- right, _ := p.parseExpressionTokens(tokens, i+1, end)- if left != nil && right != nil {- return &BinaryOpExpr{- Left: left,- Operator: strings.ToUpper(tokens[i].GetText()),- Right: right,- }- }- }- }- }- }-- return nil-}--// parseUnaryOperation parses unary operations-func (p *ExpressionParser) parseUnaryOperation(tokens []antlr.Token, start, end int) ExpressionAST {- if start >= end {- return nil- }-- unaryOps := []string{"NOT", "-", "+"}- firstToken := strings.ToUpper(tokens[start].GetText())-- for _, op := range unaryOps {- if firstToken == op {- operand, _ := p.parseExpressionTokens(tokens, start+1, end)- if operand != nil {- return &UnaryOpExpr{- Operator: op,- Operand: operand,- }- }- }- }-- return nil-}--// parseFunctionCall parses function calls-func (p *ExpressionParser) parseFunctionCall(tokens []antlr.Token, start, end int) ExpressionAST {- // Check for special EXTRACT function first- if extractExpr := p.parseExtractFunction(tokens, start, end); extractExpr != nil {- return extractExpr- }-- // Look for pattern: identifier ( args )- for i := start; i < end-1; i++ {- if tokens[i+1].GetText() == "(" {- // Find matching closing parenthesis- parenLevel := 0- closeParenPos := -1- for j := i + 1; j < end; j++ {- if tokens[j].GetText() == "(" {- parenLevel++- } else if tokens[j].GetText() == ")" {- parenLevel--- if parenLevel == 0 {- closeParenPos = j- break- }- }- }-- if closeParenPos == end-1 { // function call spans the entire range- // Parse function name (may be qualified)- funcName := p.parseQualifiedName(tokens, start, i+1)-- // Parse arguments- var args []ExpressionAST- if closeParenPos > i+2 { // has arguments- args = p.parseArgumentList(tokens, i+2, closeParenPos)- }-- return &FunctionExpr{- Schema: funcName.Schema,- Name: funcName.Name,- Args: args,- }- }- }- }-- return nil-}--// parseExtractFunction parses EXTRACT(field FROM source) function calls-func (p *ExpressionParser) parseExtractFunction(tokens []antlr.Token, start, end int) ExpressionAST {- // Look for EXTRACT( pattern- if start < end-1 &&- strings.ToUpper(tokens[start].GetText()) == "EXTRACT" &&- tokens[start+1].GetText() == "(" {- // Find matching closing parenthesis- parenLevel := 0- closeParenPos := -1- for j := start + 1; j < end; j++ {- if tokens[j].GetText() == "(" {- parenLevel++- } else if tokens[j].GetText() == ")" {- parenLevel--- if parenLevel == 0 {- closeParenPos = j- break- }- }- }-- if closeParenPos == end-1 { // EXTRACT call spans the entire range- // Look for FROM keyword inside parentheses- for k := start + 2; k < closeParenPos-1; k++ {- if strings.ToUpper(tokens[k].GetText()) == "FROM" {- // Parse: EXTRACT(field FROM source)- field, _ := p.parseExpressionTokens(tokens, start+2, k)- source, _ := p.parseExpressionTokens(tokens, k+1, closeParenPos)-- if field != nil && source != nil {- return &FunctionExpr{- Name: "EXTRACT",- Args: []ExpressionAST{field, source},- }- }- }- }- }- }-- return nil-}--// parseQualifiedIdentifier parses qualified identifiers like schema.table.column-func (p *ExpressionParser) parseQualifiedIdentifier(tokens []antlr.Token, start, end int) ExpressionAST {- if end-start < 1 {- return nil- }-- // Check if all tokens form a qualified identifier (identifier.identifier.identifier)- parts := []string{}- expectIdentifier := true-- for i := start; i < end; i++ {- token := tokens[i]- tokenText := token.GetText()-- if expectIdentifier {- if p.isIdentifierToken(token) {- parts = append(parts, tokenText)- expectIdentifier = false- } else {- return nil // not a qualified identifier- }- } else {- if tokenText == "." {- expectIdentifier = true- } else {- return nil // not a qualified identifier- }- }- }-- if len(parts) == 0 || expectIdentifier {- return nil // incomplete qualified identifier- }-- // Create identifier expression based on number of parts- switch len(parts) {- case 1:- return &IdentifierExpr{Name: parts[0]}- case 2:- // Could be schema.table or table.column- return &IdentifierExpr{Table: parts[0], Name: parts[1]}- case 3:- return &IdentifierExpr{Schema: parts[0], Table: parts[1], Name: parts[2]}- default:- // More than 3 parts, treat as complex identifier- return &IdentifierExpr{Name: strings.Join(parts, ".")}- }-}--// parseSingleToken parses a single token into an expression-func (p *ExpressionParser) parseSingleToken(token antlr.Token) (ExpressionAST, error) {- tokenText := token.GetText()-- // Check if it's a quoted identifier (PostgreSQL uses double quotes for identifiers)- if p.isQuotedIdentifier(tokenText) {- return &IdentifierExpr{Name: tokenText}, nil- }-- // Check if it's a literal- if p.isStringLiteral(tokenText) {- return &LiteralExpr{- Value: tokenText,- ValueType: "string",- }, nil- }-- if p.isNumericLiteral(tokenText) {- return &LiteralExpr{- Value: tokenText,- ValueType: "number",- }, nil- }-- if p.isBooleanLiteral(tokenText) {- return &LiteralExpr{- Value: strings.ToUpper(tokenText),- ValueType: "boolean",- }, nil- }-- if strings.ToUpper(tokenText) == "NULL" {- return &LiteralExpr{- Value: "NULL",- ValueType: "null",- }, nil- }-- // Default to identifier- if p.isIdentifierToken(token) {- return &IdentifierExpr{Name: tokenText}, nil- }-- // Fallback to literal- return &LiteralExpr{- Value: tokenText,- ValueType: "unknown",- }, nil-}--// Helper functions--// isWhitespaceToken checks if token is whitespace-func (*ExpressionParser) isWhitespaceToken(tokenType int) bool {- return tokenType == pgparser.PostgreSQLLexerWhitespace ||- tokenType == pgparser.PostgreSQLLexerNewline-}--// containsOperator checks if operators list contains the given operator-func (*ExpressionParser) containsOperator(operators []string, op string) bool {- opUpper := strings.ToUpper(op)- for _, operator := range operators {- if strings.ToUpper(operator) == opUpper {- return true- }- }- return false-}--// getParenthesesLevel returns the parentheses nesting level at given position-func (*ExpressionParser) getParenthesesLevel(tokens []antlr.Token, start, pos int) int {- level := 0- for i := start; i < pos; i++ {- if tokens[i].GetText() == "(" {- level++- } else if tokens[i].GetText() == ")" {- level--- }- }- return level-}--// isBalancedParentheses checks if parentheses are balanced in the range-func (*ExpressionParser) isBalancedParentheses(tokens []antlr.Token, start, end int) bool {- level := 0- for i := start; i < end; i++ {- if tokens[i].GetText() == "(" {- level++- } else if tokens[i].GetText() == ")" {- level--- }- if level < 0 {- return false- }- }- return level == 0-}--// parenthesesWrapEntireExpression checks if parentheses at start/end truly wrap the entire expression-func (*ExpressionParser) parenthesesWrapEntireExpression(tokens []antlr.Token, start, end int) bool {- if start >= end-1 || tokens[start].GetText() != "(" || tokens[end-1].GetText() != ")" {- return false- }-- // Find the matching closing parenthesis for the opening parenthesis at start- level := 0- matchingClosePos := -1-- for i := start; i < end; i++ {- if tokens[i].GetText() == "(" {- level++- } else if tokens[i].GetText() == ")" {- level--- if level == 0 {- matchingClosePos = i- break- }- }- }-- // If the matching closing parenthesis is at the end, then parentheses wrap entire expression- return matchingClosePos == end-1-}--// isIdentifierToken checks if token is an identifier-func (*ExpressionParser) isIdentifierToken(token antlr.Token) bool {- tokenText := token.GetText()- tokenType := token.GetTokenType()-- // Check for quoted identifiers- if tokenType == pgparser.PostgreSQLLexerQuotedIdentifier ||- tokenType == pgparser.PostgreSQLLexerUnicodeQuotedIdentifier {- return true- }-- // Check for unquoted identifiers (simple pattern matching)- if len(tokenText) > 0 {- firstChar := tokenText[0]- if (firstChar >= 'a' && firstChar <= 'z') ||- (firstChar >= 'A' && firstChar <= 'Z') ||- firstChar == '_' {- return true- }- }-- return false-}--// isStringLiteral checks if token is a string literal (single quotes in PostgreSQL)-func (*ExpressionParser) isStringLiteral(tokenText string) bool {- return len(tokenText) >= 2 &&- tokenText[0] == '\'' && tokenText[len(tokenText)-1] == '\''-}--// isQuotedIdentifier checks if token is a quoted identifier (double quotes in PostgreSQL)-func (*ExpressionParser) isQuotedIdentifier(tokenText string) bool {- return len(tokenText) >= 2 &&- tokenText[0] == '"' && tokenText[len(tokenText)-1] == '"'-}--// isNumericLiteral checks if token is numeric-func (*ExpressionParser) isNumericLiteral(tokenText string) bool {- if len(tokenText) == 0 {- return false- }-- for i, char := range tokenText {- if i == 0 && (char == '+' || char == '-') {- continue- }- if char == '.' {- continue- }- if char < '0' || char > '9' {- return false- }- }-- return true-}--// isBooleanLiteral checks if token is boolean-func (*ExpressionParser) isBooleanLiteral(tokenText string) bool {- upper := strings.ToUpper(tokenText)- return upper == "TRUE" || upper == "FALSE"-}--// parseQualifiedName parses qualified name from tokens-func (*ExpressionParser) parseQualifiedName(tokens []antlr.Token, start, end int) *IdentifierExpr {- var parts []string-- for i := start; i < end; i++ {- token := tokens[i]- if token.GetText() != "." {- parts = append(parts, token.GetText())- }- }-- switch len(parts) {- case 1:- return &IdentifierExpr{Name: parts[0]}- case 2:- return &IdentifierExpr{Schema: parts[0], Name: parts[1]}- default:- return &IdentifierExpr{Name: strings.Join(parts, ".")}- }-}--// parseArgumentList parses function argument list-func (p *ExpressionParser) parseArgumentList(tokens []antlr.Token, start, end int) []ExpressionAST {- if start >= end {- return nil- }-- var args []ExpressionAST- argStart := start- parenLevel := 0-- for i := start; i <= end; i++ {- var tokenText string- if i < end {- tokenText = tokens[i].GetText()- }-- if tokenText == "(" {- parenLevel++- } else if tokenText == ")" {- parenLevel--- } else if tokenText == "," && parenLevel == 0 || i == end {- // Found argument boundary- if argStart < i {- if arg, _ := p.parseExpressionTokens(tokens, argStart, i); arg != nil {- args = append(args, arg)- }- }- argStart = i + 1- }- }-- return args-}diff --git a/backend/plugin/schema/pg/ast/expression_parser_parentheses_test.go b/backend/plugin/schema/pg/ast/expression_parser_parentheses_test.godeleted file mode 100644index 920a61abd2..0000000000--- a/backend/plugin/schema/pg/ast/expression_parser_parentheses_test.go+++ /dev/null@@ -1,457 +0,0 @@-package ast //nolint:revive // intentional package name--import (- "fmt"- "testing"-- "github.com/antlr4-go/antlr/v4"- pgparser "github.com/bytebase/parser/postgresql"- "github.com/stretchr/testify/assert"- "github.com/stretchr/testify/require"-)--// TestParenthesesMatching tests comprehensive parentheses matching scenarios-func TestParenthesesMatching(t *testing.T) {- testCases := []struct {- name string- expression string- expectParseSuccess bool- expectParentheses bool- expectedStructure string- description string- }{- {- name: "simple_parentheses",- expression: "(a)",- expectParseSuccess: true,- expectParentheses: true,- expectedStructure: "Parentheses(Identifier)",- description: "Simple parentheses around identifier",- },- {- name: "nested_parentheses",- expression: "((a))",- expectParseSuccess: true,- expectParentheses: true,- expectedStructure: "Parentheses(Parentheses(Identifier))",- description: "Nested parentheses",- },- {- name: "function_with_parentheses_arg",- expression: "func((a))",- expectParseSuccess: true,- expectParentheses: false, // outer structure should be function- expectedStructure: "Function(Parentheses(Identifier))",- description: "Function call with parenthesized argument",- },- {- name: "binary_op_with_parentheses",- expression: "(a) + (b)",- expectParseSuccess: true,- expectParentheses: false, // outer structure should be binary op- expectedStructure: "BinaryOp(Parentheses(Identifier), Parentheses(Identifier))",- description: "Binary operation with parenthesized operands",- },- {- name: "unmatched_open_paren",- expression: "(a",- expectParseSuccess: true, // should fallback to literal- expectParentheses: false,- expectedStructure: "Literal",- description: "Unmatched opening parenthesis",- },- {- name: "unmatched_close_paren",- expression: "a)",- expectParseSuccess: true, // should fallback to literal- expectParentheses: false,- expectedStructure: "Literal",- description: "Unmatched closing parenthesis",- },- {- name: "mismatched_parentheses",- expression: "(a))(b",- expectParseSuccess: true, // should fallback to literal- expectParentheses: false,- expectedStructure: "Literal",- description: "Mismatched parentheses pattern",- },- {- name: "empty_parentheses",- expression: "()",- expectParseSuccess: true,- expectParentheses: true,- expectedStructure: "Parentheses(null)",- description: "Empty parentheses",- },- {- name: "complex_nested_expression",- expression: "((a + b) * (c - d))",- expectParseSuccess: true,- expectParentheses: true,- expectedStructure: "Parentheses(BinaryOp(Parentheses(BinaryOp), Parentheses(BinaryOp)))",- description: "Complex nested expression with multiple parentheses levels",- },- {- name: "partial_wrapping_parentheses",- expression: "(a + b) * c",- expectParseSuccess: true,- expectParentheses: false, // outer should be binary op, not parentheses- expectedStructure: "BinaryOp(Parentheses(BinaryOp), Identifier)",- description: "Parentheses that don't wrap entire expression",- },- }-- parser := NewExpressionParser()-- for _, tc := range testCases {- t.Run(tc.name, func(t *testing.T) {- // Parse the expression- ast, err := parser.ParseExpression(tc.expression)-- if tc.expectParseSuccess {- require.NoError(t, err, "Expected successful parsing for: %s", tc.description)- require.NotNil(t, ast, "Expected non-nil AST for: %s", tc.description)-- // Check if the top-level structure matches expectation- if tc.expectParentheses {- _, isParens := ast.(*ParenthesesExpr)- assert.True(t, isParens, "Expected parentheses expression for: %s, got: %T", tc.description, ast)- }-- // Print actual structure for debugging- t.Logf("Expression: %s", tc.expression)- t.Logf("Expected: %s", tc.expectedStructure)- t.Logf("Actual AST: %s", ast.String())- t.Logf("Actual structure: %s", describeAST(ast))- } else {- assert.Error(t, err, "Expected parsing error for: %s", tc.description)- }- })- }-}--// TestParenthesesMatchingHelpers tests the individual helper functions-func TestParenthesesMatchingHelpers(t *testing.T) {- parser := NewExpressionParser()-- testCases := []struct {- name string- expression string- expectedBalance bool- expectedWrap bool- description string- }{- {- name: "simple_balanced",- expression: "(a)",- expectedBalance: true,- expectedWrap: true,- description: "Simple balanced parentheses",- },- {- name: "nested_balanced",- expression: "((a))",- expectedBalance: true,- expectedWrap: true,- description: "Nested balanced parentheses",- },- {- name: "unbalanced_open",- expression: "(a",- expectedBalance: false,- expectedWrap: false,- description: "Unbalanced - missing close",- },- {- name: "unbalanced_close",- expression: "a)",- expectedBalance: false,- expectedWrap: false,- description: "Unbalanced - extra close",- },- {- name: "partial_wrap",- expression: "(a) + b",- expectedBalance: true,- expectedWrap: false,- description: "Balanced but doesn't wrap entire expression",- },- {- name: "empty_parens",- expression: "()",- expectedBalance: true,- expectedWrap: true,- description: "Empty parentheses",- },- {- name: "nested_with_extra_content",- expression: "((a + b)) + c)",- expectedBalance: false,- expectedWrap: false,- description: "Nested parentheses with extra content - should not wrap entire expression",- },- {- name: "proper_nested_wrapping",- expression: "((a + b) + c)",- expectedBalance: true,- expectedWrap: true,- description: "Proper nested parentheses that wrap entire expression",- },- {- name: "edge_case_with_whitespace",- expression: "(a + b) )",- expectedBalance: false,- expectedWrap: false,- description: "Edge case with whitespace and extra parenthesis",- },- }-- for _, tc := range testCases {- t.Run(tc.name, func(t *testing.T) {- tokens := tokenizeExpression(tc.expression)- start := 0- end := len(tokens)-- // Test isBalancedParentheses- actualBalance := parser.isBalancedParentheses(tokens, start, end)- assert.Equal(t, tc.expectedBalance, actualBalance,- "isBalancedParentheses mismatch for: %s", tc.description)-- // Test parenthesesWrapEntireExpression (only if starts and ends with parentheses)- if len(tokens) >= 2 && tokens[0].GetText() == "(" && tokens[end-1].GetText() == ")" {- actualWrap := parser.parenthesesWrapEntireExpression(tokens, start, end)- assert.Equal(t, tc.expectedWrap, actualWrap,- "parenthesesWrapEntireExpression mismatch for: %s", tc.description)- }- })- }-}--// TestParenthesesLevelCalculation tests the getParenthesesLevel function-func TestParenthesesLevelCalculation(t *testing.T) {- parser := NewExpressionParser()-- testCases := []struct {- expression string- pos int- expectedLevel int- description string- }{- {- expression: "(a + b)",- pos: 1, // at 'a'- expectedLevel: 1,- description: "Level 1 inside simple parentheses",- },- {- expression: "((a) + (b))",- pos: 2, // at 'a'- expectedLevel: 2,- description: "Level 2 inside nested parentheses",- },- {- expression: "(a) + (b)",- pos: 4, // at '+'- expectedLevel: 0,- description: "Level 0 between parentheses groups",- },- {- expression: "func((a + b), c)",- pos: 4, // at '+'- expectedLevel: 2,- description: "Level 2 inside function call with nested parentheses",- },- }-- for _, tc := range testCases {- t.Run(tc.description, func(t *testing.T) {- tokens := tokenizeExpression(tc.expression)- require.True(t, tc.pos < len(tokens), "Position out of bounds")-- actualLevel := parser.getParenthesesLevel(tokens, 0, tc.pos)- assert.Equal(t, tc.expectedLevel, actualLevel,- "Parentheses level mismatch at position %d in expression: %s", tc.pos, tc.expression)- })- }-}--// TestEdgeCasesAndErrorHandling tests edge cases and error conditions-func TestEdgeCasesAndErrorHandling(t *testing.T) {- parser := NewExpressionParser()-- edgeCases := []struct {- name string- expression string- expectError bool- description string- }{- {- name: "deeply_nested",- expression: "(((((a)))))",- expectError: false,- description: "Deeply nested parentheses",- },- {- name: "alternating_parens",- expression: "(a)(b)(c)",- expectError: false,- description: "Multiple separate parentheses groups",- },- {- name: "complex_unbalanced",- expression: "((a + (b * c)) + d))",- expectError: false, // should parse as literal- description: "Complex expression with extra closing paren",- },- {- name: "mixed_brackets",- expression: "(a + [b])",- expectError: false,- description: "Mixed parentheses and brackets",- },- }-- for _, tc := range edgeCases {- t.Run(tc.name, func(t *testing.T) {- ast, err := parser.ParseExpression(tc.expression)-- if tc.expectError {- assert.Error(t, err, "Expected error for: %s", tc.description)- } else {- assert.NoError(t, err, "Unexpected error for: %s", tc.description)- assert.NotNil(t, ast, "Expected non-nil AST for: %s", tc.description)- t.Logf("Expression: %s -> %s", tc.expression, ast.String())- }- })- }-}--// Helper functions--// tokenizeExpression converts an expression string to tokens for testing-func tokenizeExpression(expr string) []antlr.Token {- inputStream := antlr.NewInputStream(expr)- lexer := pgparser.NewPostgreSQLLexer(inputStream)- stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)- stream.Fill()- tokens := stream.GetAllTokens()-- // Filter out whitespace and EOF tokens- var filteredTokens []antlr.Token- for _, token := range tokens {- if token.GetText() != "<EOF>" && token.GetChannel() == antlr.TokenDefaultChannel {- filteredTokens = append(filteredTokens, token)- }- }- return filteredTokens-}--// describeAST provides a structural description of an AST for testing-func describeAST(expr ExpressionAST) string {- if expr == nil {- return "null"- }-- switch e := expr.(type) {- case *ParenthesesExpr:- return fmt.Sprintf("Parentheses(%s)", describeAST(e.Inner))- case *FunctionExpr:- var args []string- for _, arg := range e.Args {- args = append(args, describeAST(arg))- }- if len(args) == 0 {- return fmt.Sprintf("Function(%s)", e.Name)- }- return fmt.Sprintf("Function(%s, %v)", e.Name, args)- case *BinaryOpExpr:- return fmt.Sprintf("BinaryOp(%s, %s)", describeAST(e.Left), describeAST(e.Right))- case *UnaryOpExpr:- return fmt.Sprintf("UnaryOp(%s, %s)", e.Operator, describeAST(e.Operand))- case *IdentifierExpr:- return "Identifier"- case *LiteralExpr:- return "Literal"- case *ListExpr:- return fmt.Sprintf("List(%d)", len(e.Elements))- default:- return fmt.Sprintf("Unknown(%T)", expr)- }-}--// TestGitHubCommentIssues tests specific issues mentioned in GitHub PR comments-func TestGitHubCommentIssues(t *testing.T) {- comparer := NewPostgreSQLExpressionComparer()-- testCases := []struct {- name string- expr1 string- expr2 string- shouldEqual bool- description string- }{- {- name: "AND_OR_precedence_parentheses_significant",- expr1: "(A AND B) OR C",- expr2: "A AND (B OR C)",- shouldEqual: false,- description: "Parentheses around AND/OR should be significant for precedence: (A AND B) OR C ≠ A AND (B OR C)",- },- {- name: "AND_OR_same_precedence_equivalent",- expr1: "(A AND B) AND C",- expr2: "A AND B AND C",- shouldEqual: true,- description: "Parentheses around same precedence operations should not be significant",- },- {- name: "nested_parentheses_with_extra_content",- expr1: "((a + b)) + c)",- expr2: "(a + b) + c",- shouldEqual: false,- description: "Expression with unmatched parentheses should not equal properly formed expression",- },- {- name: "OR_operator_equivalence",- expr1: "A || B",- expr2: "A OR B",- shouldEqual: true,- description: "|| and OR operators should be equivalent",- },- {- name: "complex_precedence_case",- expr1: "A AND (B OR C) AND D",- expr2: "(A AND (B OR C)) AND D",- shouldEqual: true,- description: "Parentheses that don't change precedence should be ignored",- },- {- name: "test_failure_case",- expr1: "A + B",- expr2: "C + D",- shouldEqual: false,- description: "Completely different expressions should not be equal",- },- }-- for _, tc := range testCases {- t.Run(tc.name, func(t *testing.T) {- result, err := comparer.CompareExpressions(tc.expr1, tc.expr2)- require.NoError(t, err, "Comparison should not fail for: %s", tc.description)-- if tc.shouldEqual {- assert.True(t, result, "Expected expressions to be equal: %s vs %s (%s)",- tc.expr1, tc.expr2, tc.description)- } else {- assert.False(t, result, "Expected expressions to be different: %s vs %s (%s)",- tc.expr1, tc.expr2, tc.description)- }-- // Also test in reverse order to ensure symmetry- reverseResult, err := comparer.CompareExpressions(tc.expr2, tc.expr1)- require.NoError(t, err, "Reverse comparison should not fail")- assert.Equal(t, result, reverseResult, "Comparison should be symmetric")- })- }-}diff --git a/backend/plugin/schema/pg/ast/expression_visitor.go b/backend/plugin/schema/pg/ast/expression_visitor.godeleted file mode 100644index 6a75c4b2b9..0000000000--- a/backend/plugin/schema/pg/ast/expression_visitor.go+++ /dev/null@@ -1,273 +0,0 @@-package ast //nolint:revive // intentional package name--import (- "fmt"- "strings"-)--// BaseExpressionVisitor provides a base implementation of ExpressionVisitor-type BaseExpressionVisitor struct{}--// VisitIdentifier visits identifier expressions-func (*BaseExpressionVisitor) VisitIdentifier(expr *IdentifierExpr) any {- return expr-}--// VisitLiteral visits literal expressions-func (*BaseExpressionVisitor) VisitLiteral(expr *LiteralExpr) any {- return expr-}--// VisitBinaryOp visits binary operation expressions-func (*BaseExpressionVisitor) VisitBinaryOp(expr *BinaryOpExpr) any {- return expr-}--// VisitUnaryOp visits unary operation expressions-func (*BaseExpressionVisitor) VisitUnaryOp(expr *UnaryOpExpr) any {- return expr-}--// VisitFunction visits function expressions-func (*BaseExpressionVisitor) VisitFunction(expr *FunctionExpr) any {- return expr-}--// VisitList visits list expressions-func (*BaseExpressionVisitor) VisitList(expr *ListExpr) any {- return expr-}--// VisitParentheses visits parentheses expressions-func (*BaseExpressionVisitor) VisitParentheses(expr *ParenthesesExpr) any {- return expr-}--// ExpressionValidatorVisitor validates expressions for correctness-type ExpressionValidatorVisitor struct {- BaseExpressionVisitor- errors []string-}--// NewExpressionValidatorVisitor creates a new expression validator visitor-func NewExpressionValidatorVisitor() *ExpressionValidatorVisitor {- return &ExpressionValidatorVisitor{}-}--// Validate validates an expression and returns any errors found-func (v *ExpressionValidatorVisitor) Validate(expr ExpressionAST) []string {- v.errors = nil- v.visitRecursively(expr)- return v.errors-}--// visitRecursively visits expression and validates it-func (v *ExpressionValidatorVisitor) visitRecursively(expr ExpressionAST) {- if expr == nil {- v.errors = append(v.errors, "null expression found")- return- }-- // Validate based on expression type- switch e := expr.(type) {- case *IdentifierExpr:- v.validateIdentifier(e)- case *LiteralExpr:- v.validateLiteral(e)- case *BinaryOpExpr:- v.validateBinaryOp(e)- case *UnaryOpExpr:- v.validateUnaryOp(e)- case *FunctionExpr:- v.validateFunction(e)- case *ListExpr:- v.validateList(e)- case *ParenthesesExpr:- v.validateParentheses(e)- default:- }-- // Recursively validate children- for _, child := range expr.Children() {- v.visitRecursively(child)- }-}--// validateIdentifier validates identifier expressions-func (v *ExpressionValidatorVisitor) validateIdentifier(expr *IdentifierExpr) {- if expr.Name == "" {- v.errors = append(v.errors, "identifier name cannot be empty")- }-- // Could add more validation rules for identifier names- if strings.ContainsAny(expr.Name, " \t\n\r") && !v.isQuotedIdentifier(expr.Name) {- v.errors = append(v.errors, fmt.Sprintf("unquoted identifier contains whitespace: %s", expr.Name))- }-}--// validateLiteral validates literal expressions-func (v *ExpressionValidatorVisitor) validateLiteral(expr *LiteralExpr) {- if expr.Value == "" && expr.ValueType != "null" {- v.errors = append(v.errors, "literal value cannot be empty unless it's null")- }-- // Validate literal format based on type- switch expr.ValueType {- case "string":- if !v.isValidStringLiteral(expr.Value) {- v.errors = append(v.errors, fmt.Sprintf("invalid string literal: %s", expr.Value))- }- case "number":- if !v.isValidNumericLiteral(expr.Value) {- v.errors = append(v.errors, fmt.Sprintf("invalid numeric literal: %s", expr.Value))- }- case "boolean":- upper := strings.ToUpper(expr.Value)- if upper != "TRUE" && upper != "FALSE" {- v.errors = append(v.errors, fmt.Sprintf("invalid boolean literal: %s", expr.Value))- }- case "null":- if strings.ToUpper(expr.Value) != "NULL" {- v.errors = append(v.errors, fmt.Sprintf("invalid null literal: %s", expr.Value))- }- default:- // Unknown value type - this is acceptable as the type system may support additional types- }-}--// validateBinaryOp validates binary operation expressions-func (v *ExpressionValidatorVisitor) validateBinaryOp(expr *BinaryOpExpr) {- if expr.Left == nil {- v.errors = append(v.errors, "binary operation missing left operand")- }- if expr.Right == nil {- v.errors = append(v.errors, "binary operation missing right operand")- }- if expr.Operator == "" {- v.errors = append(v.errors, "binary operation missing operator")- }-- // Validate operator- if !v.isValidBinaryOperator(expr.Operator) {- v.errors = append(v.errors, fmt.Sprintf("invalid binary operator: %s", expr.Operator))- }-}--// validateUnaryOp validates unary operation expressions-func (v *ExpressionValidatorVisitor) validateUnaryOp(expr *UnaryOpExpr) {- if expr.Operand == nil {- v.errors = append(v.errors, "unary operation missing operand")- }- if expr.Operator == "" {- v.errors = append(v.errors, "unary operation missing operator")- }-- // Validate operator- if !v.isValidUnaryOperator(expr.Operator) {- v.errors = append(v.errors, fmt.Sprintf("invalid unary operator: %s", expr.Operator))- }-}--// validateFunction validates function expressions-func (v *ExpressionValidatorVisitor) validateFunction(expr *FunctionExpr) {- if expr.Name == "" {- v.errors = append(v.errors, "function name cannot be empty")- }-- // Could add validation for function name format- if strings.ContainsAny(expr.Name, " \t\n\r") {- v.errors = append(v.errors, fmt.Sprintf("function name contains whitespace: %s", expr.Name))- }-}--// validateList validates list expressions-func (v *ExpressionValidatorVisitor) validateList(expr *ListExpr) {- if len(expr.Elements) == 0 {- v.errors = append(v.errors, "list expression cannot be empty")- }-}--// validateParentheses validates parentheses expressions-func (v *ExpressionValidatorVisitor) validateParentheses(expr *ParenthesesExpr) {- if expr.Inner == nil {- v.errors = append(v.errors, "parentheses expression cannot be empty")- }-}--// Helper validation methods--// isQuotedIdentifier checks if identifier is quoted-func (*ExpressionValidatorVisitor) isQuotedIdentifier(name string) bool {- return len(name) >= 2 &&- ((name[0] == '"' && name[len(name)-1] == '"') ||- (name[0] == '`' && name[len(name)-1] == '`'))-}--// isValidStringLiteral validates string literal format-func (*ExpressionValidatorVisitor) isValidStringLiteral(value string) bool {- if len(value) < 2 {- return false- }- return (value[0] == '\'' && value[len(value)-1] == '\'') ||- (value[0] == '"' && value[len(value)-1] == '"')-}--// isValidNumericLiteral validates numeric literal format-func (*ExpressionValidatorVisitor) isValidNumericLiteral(value string) bool {- if len(value) == 0 {- return false- }-- hasDecimal := false- for i, char := range value {- if i == 0 && (char == '+' || char == '-') {- continue- }- if char == '.' {- if hasDecimal {- return false // multiple decimals- }- hasDecimal = true- continue- }- if char < '0' || char > '9' {- return false- }- }-- return true-}--// isValidBinaryOperator validates binary operators-func (*ExpressionValidatorVisitor) isValidBinaryOperator(operator string) bool {- validOps := []string{- "=", "<>", "!=", "<", ">", "<=", ">=",- "+", "-", "*", "/", "%",- "AND", "OR", "LIKE", "ILIKE", "IN", "NOT IN",- "||", // string concatenation or logical OR- "IS NULL", "IS NOT NULL", // NULL checking operators- }-- upper := strings.ToUpper(operator)- for _, validOp := range validOps {- if upper == validOp {- return true- }- }-- return false-}--// isValidUnaryOperator validates unary operators-func (*ExpressionValidatorVisitor) isValidUnaryOperator(operator string) bool {- validOps := []string{"NOT", "-", "+"}-- upper := strings.ToUpper(operator)- for _, validOp := range validOps {- if upper == validOp {- return true- }- }-- return false-}diff --git a/backend/plugin/schema/pg/get_database_definition.go b/backend/plugin/schema/pg/get_database_definition.goindex f2873f7898..2058e80bf7 100644--- a/backend/plugin/schema/pg/get_database_definition.go+++ b/backend/plugin/schema/pg/get_database_definition.go@@ -947,6 +947,79 @@ func escapeSingleQuote(s string) string {return strings.ReplaceAll(s, "'", "''")}+// signatureTypesOnly strips parameter names from a function/procedure signature,+// keeping only types. PostgreSQL's COMMENT ON FUNCTION/PROCEDURE requires+// type-only signatures: "func(integer, text)" not "func(id integer, name text)".+//+// It works by wrapping the signature in a CREATE FUNCTION statement, parsing it+// with omni to discover parameter names, then removing those names from the+// original signature text to preserve the original type spelling.+func signatureTypesOnly(signature string) string {+ openParen := strings.Index(signature, "(")+ if openParen < 0 {+ return signature+ }+ closeParen := strings.LastIndex(signature, ")")+ if closeParen <= openParen {+ return signature+ }+ funcName := signature[:openParen]+ paramStr := signature[openParen+1 : closeParen]+ if strings.TrimSpace(paramStr) == "" {+ return signature+ }++ // Parse the signature as a CREATE FUNCTION to extract param names from AST.+ sql := fmt.Sprintf("CREATE FUNCTION %s RETURNS void LANGUAGE sql AS 'SELECT 1'", signature)+ stmts, err := omnipg.Parse(sql)+ if err != nil {+ return signature+ }+ fn, ok := stmts[0].AST.(*ast.CreateFunctionStmt)+ if !ok || fn.Parameters == nil {+ return signature+ }++ // Collect parameter names from AST.+ var paramNames []string+ for _, item := range fn.Parameters.Items {+ if fp, ok := item.(*ast.FunctionParameter); ok && fp.Name != "" {+ paramNames = append(paramNames, fp.Name)+ }+ }++ // Strip parameter names from the original text, preserving original type spelling.+ rawParams := strings.Split(paramStr, ",")+ var typesOnly []string+ nameIdx := 0+ for _, raw := range rawParams {+ param := strings.TrimSpace(raw)+ words := strings.Fields(param)+ if len(words) == 0 {+ continue+ }+ // Skip mode keywords (IN, OUT, INOUT, VARIADIC).+ startIdx := 0+ for startIdx < len(words) {+ w := strings.ToLower(words[startIdx])+ if w == "in" || w == "out" || w == "inout" || w == "variadic" {+ startIdx+++ } else {+ break+ }+ }+ // Check if first non-mode word matches a known param name from AST.+ if startIdx < len(words) && nameIdx < len(paramNames) && words[startIdx] == paramNames[nameIdx] {+ typesOnly = append(typesOnly, strings.Join(words[startIdx+1:], " "))+ nameIdx+++ } else {+ typesOnly = append(typesOnly, strings.Join(words[startIdx:], " "))+ }+ }++ return funcName + "(" + strings.Join(typesOnly, ", ") + ")"+}+func writeEnumComment(out io.Writer, schema string, enum *storepb.EnumTypeMetadata) error {if _, err := io.WriteString(out, `COMMENT ON TYPE "`); err != nil {return err@@ -2282,7 +2355,7 @@ func writeFunctionComment(out io.Writer, schema string, function *storepb.Functireturn err}- if _, err := io.WriteString(out, function.Signature); err != nil {+ if _, err := io.WriteString(out, signatureTypesOnly(function.Signature)); err != nil {return err}@@ -4106,7 +4179,7 @@ func writeFunctionCommentSDL(out io.Writer, schemaName string, function *storepbif _, err := io.WriteString(out, `".`); err != nil {return err}- if _, err := io.WriteString(out, function.Signature); err != nil {+ if _, err := io.WriteString(out, signatureTypesOnly(function.Signature)); err != nil {return err}if _, err := io.WriteString(out, ` IS '`); err != nil {diff --git a/backend/plugin/schema/pg/get_database_definition_sdl_test.go b/backend/plugin/schema/pg/get_database_definition_sdl_test.goindex f44089309d..55336e6e62 100644--- a/backend/plugin/schema/pg/get_database_definition_sdl_test.go+++ b/backend/plugin/schema/pg/get_database_definition_sdl_test.go@@ -1314,7 +1314,7 @@ CREATE TABLE "test"."table1" (}// Validate that the generated SQL can be parsed without errors using ANTLR parser- _, err = pgparser.ParsePostgreSQL(result)+ _, err = pgparser.ParsePg(result)require.NoError(t, err, "Generated SQL should be parseable by PostgreSQL parser")})}@@ -1370,7 +1370,7 @@ func TestCheckConstraintNotValidFormatNormalMode(t *testing.T) {assert.Contains(t, result, ") NOT VALID", "NOT VALID should be after CHECK expression parenthesis")// Validate that the generated SQL can be parsed without errors using ANTLR parser- _, err = pgparser.ParsePostgreSQL(result)+ _, err = pgparser.ParsePg(result)require.NoError(t, err, "Generated SQL should be parseable by PostgreSQL parser")}@@ -1479,7 +1479,7 @@ func TestGetDatabaseDefinitionSDLFormat_WithComments(t *testing.T) {assert.Contains(t, result, `COMMENT ON INDEX "test_schema"."idx_users_email" IS 'Index on email column';`)// Validate that the generated SQL can be parsed without errors using ANTLR parser- _, err = pgparser.ParsePostgreSQL(result)+ _, err = pgparser.ParsePg(result)require.NoError(t, err, "Generated SQL should be parseable by PostgreSQL parser")}@@ -1518,7 +1518,7 @@ func TestGetDatabaseDefinitionSDLFormat_WithCommentsEscaping(t *testing.T) {assert.Contains(t, result, `COMMENT ON COLUMN "public"."test_table"."id" IS 'Column with ''quoted'' text';`)// Validate that the generated SQL can be parsed without errors using ANTLR parser- _, err = pgparser.ParsePostgreSQL(result)+ _, err = pgparser.ParsePg(result)require.NoError(t, err, "Generated SQL should be parseable by PostgreSQL parser")}@@ -1645,7 +1645,7 @@ func TestGetMultiFileDatabaseDefinition_WithComments(t *testing.T) {// Validate that each file's SQL can be parsedfor fileName, content := range fileMap {- _, err := pgparser.ParsePostgreSQL(content)+ _, err := pgparser.ParsePg(content)require.NoError(t, err, "SQL in file %s should be parseable by PostgreSQL parser", fileName)}}@@ -1945,7 +1945,7 @@ ALTER SEQUENCE "public"."test_sequence2" OWNED BY "public"."test_table"."id";"Column should use serial type because it references test_table_id_seq which is owned by it")// Validate that the generated SQL can be parsed- _, err = pgparser.ParsePostgreSQL(result)+ _, err = pgparser.ParsePg(result)require.NoError(t, err, "Generated SQL should be parseable by PostgreSQL parser")}@@ -1998,15 +1998,15 @@ $$`,"Function comment should use COMMENT ON FUNCTION")// Verify that PROCEDURE has COMMENT ON PROCEDURE (not COMMENT ON FUNCTION)- assert.Contains(t, result, `COMMENT ON PROCEDURE "public".update_user_name(user_id integer, new_name character varying) IS 'Procedure to update user name';`,- "Procedure comment should use COMMENT ON PROCEDURE")+ assert.Contains(t, result, `COMMENT ON PROCEDURE "public".update_user_name(integer, character varying) IS 'Procedure to update user name';`,+ "Procedure comment should use COMMENT ON PROCEDURE with types-only signature")// Verify that we don't incorrectly use COMMENT ON FUNCTION for the procedureassert.NotContains(t, result, `COMMENT ON FUNCTION "public".update_user_name`,"Procedure comment should NOT use COMMENT ON FUNCTION")// Validate that the generated SQL can be parsed- _, err = pgparser.ParsePostgreSQL(result)+ _, err = pgparser.ParsePg(result)require.NoError(t, err, "Generated SQL should be parseable by PostgreSQL parser")}@@ -2103,7 +2103,7 @@ $$`,// Validate that each file's SQL can be parsedfor fileName, content := range fileMap {- _, err := pgparser.ParsePostgreSQL(content)+ _, err := pgparser.ParsePg(content)require.NoError(t, err, "SQL in file %s should be parseable by PostgreSQL parser", fileName)}}diff --git a/backend/runner/taskrun/database_migrate_executor.go b/backend/runner/taskrun/database_migrate_executor.goindex 015f729a8f..db42c8129a 100644--- a/backend/runner/taskrun/database_migrate_executor.go+++ b/backend/runner/taskrun/database_migrate_executor.go@@ -7,8 +7,7 @@ import ("strings""time"- "github.com/antlr4-go/antlr/v4"- "github.com/bytebase/parser/postgresql"+ "github.com/bytebase/omni/pg/ast""github.com/github/gh-ost/go/logic"gomysql "github.com/go-sql-driver/mysql""github.com/pkg/errors"@@ -951,114 +950,26 @@ func getPrependStatements(engine storepb.Engine, statement string) (string, erroreturn "", nil}- parseResults, err := pg.ParsePostgreSQL(statement)+ stmts, err := pg.ParsePg(statement)if err != nil {return "", errors.Wrap(err, "failed to parse statement")}- visitor := &prependStatementsVisitor{- statement: statement,- }-- // Walk through all statements to find the first SET role/search_path statement- // The visitor will stop after finding the first one due to its internal check- for _, result := range parseResults {- antlr.ParseTreeWalkerDefault.Walk(visitor, result.Tree)- // If we found a result, stop walking remaining statements- if visitor.result != "" {- break- }- }-- return visitor.result, nil-}--// prependStatementsVisitor extracts SET role and search_path statements-type prependStatementsVisitor struct {- *postgresql.BasePostgreSQLParserListener- statement string- result string-}--func (v *prependStatementsVisitor) EnterVariablesetstmt(ctx *postgresql.VariablesetstmtContext) {- // If we already found a result, don't process more statements- if v.result != "" {- return- }-- setRest := ctx.Set_rest()- if setRest == nil {- return- }- setRestMore := setRest.Set_rest_more()- if setRestMore == nil {- return- }- genericSet := setRestMore.Generic_set()- if genericSet == nil {- return- }- varName := genericSet.Var_name()- if varName == nil {- return- }- if len(varName.AllColid()) != 1 {- return- }-- name := pg.NormalizePostgreSQLColid(varName.Colid(0))- if name == "role" || name == "search_path" {- // Extract the text for this SET statement- v.result = v.extractStatementText(ctx)- }-}--// extractStatementText extracts the original text for a SET statement context-// This matches pg_query_go behavior: trim leading/trailing whitespace, preserve internal whitespace-func (v *prependStatementsVisitor) extractStatementText(ctx *postgresql.VariablesetstmtContext) string {- // Extract text from the original statement- start := ctx.GetStart().GetStart()- stop := ctx.GetStop().GetStop()-- // Handle potential edge cases with token positions- if start < 0 || stop < 0 || start >= len(v.statement) {- return ""- }-- // Find the semicolon that ends this statement by looking ahead from the stop token- endPos := stop + 1- stmtLen := len(v.statement)- for endPos < stmtLen {- char := v.statement[endPos]- if char == ';' {- // Include the semicolon and any whitespace before it- stop = endPos- break+ for _, stmt := range stmts {+ varSet, ok := stmt.AST.(*ast.VariableSetStmt)+ if !ok {+ continue}- if char != ' ' && char != '\t' && char != '\n' && char != '\r' {- // Hit non-whitespace, non-semicolon character, stop looking- break+ if strings.EqualFold(varSet.Name, "role") || strings.EqualFold(varSet.Name, "search_path") {+ text := strings.TrimSpace(stmt.Text)+ if !strings.HasSuffix(text, ";") {+ text += ";"+ }+ return text, nil}- endPos++- }-- // Ensure stop doesn't exceed statement length- if stop >= stmtLen {- stop = stmtLen - 1- }-- // Extract the raw text- text := v.statement[start : stop+1]-- // Match pg_query_go behavior: trim leading and trailing whitespace but preserve internal whitespace- text = strings.TrimSpace(text)-- // Add semicolon if not present (to match pg_query_go behavior)- if !strings.HasSuffix(text, ";") {- text += ";"}- return text+ return "", nil}func diff(ctx context.Context, s *store.Store, instance *store.InstanceMessage, database *store.DatabaseMessage, sheetContent string) (string, error) {diff --git a/go.mod b/go.modindex 330be8ac56..ffef2c3974 100644--- a/go.mod+++ b/go.mod@@ -34,7 +34,7 @@ require (github.com/bmatcuk/doublestar/v4 v4.10.0github.com/bytebase/gomongo v0.0.0-20260324061529-f15b9a5add35github.com/bytebase/lsp-protocol v0.0.0-20250324071136-1586d0c10ff0- github.com/bytebase/omni v0.0.0-20260326070405-a1a6e5880a1e+ github.com/bytebase/omni v0.0.0-20260330073149-2f02f119f9ecgithub.com/caarlos0/env/v11 v11.4.0github.com/cenkalti/backoff/v5 v5.0.3github.com/cockroachdb/cockroachdb-parser v0.25.2diff --git a/go.sum b/go.sumindex 58fa13df19..0f7c7055df 100644--- a/go.sum+++ b/go.sum@@ -253,8 +253,8 @@ github.com/bytebase/gomongo v0.0.0-20260324061529-f15b9a5add35 h1:SjcZSrUMNj6GZqgithub.com/bytebase/gomongo v0.0.0-20260324061529-f15b9a5add35/go.mod h1:jHdAD+tN3bO2Hazme0crgkNiHqkDR/3Igesy1chhjLA=github.com/bytebase/lsp-protocol v0.0.0-20250324071136-1586d0c10ff0 h1:bJ8NSf2K+pccl5m/wuu97fiYjDwpJH6IzURAq/3XA2U=github.com/bytebase/lsp-protocol v0.0.0-20250324071136-1586d0c10ff0/go.mod h1:BEWMfiZtqshdmulKJcIsWb45MUaEvDO4CROcgi8dfFg=-github.com/bytebase/omni v0.0.0-20260326070405-a1a6e5880a1e h1:fIG16JzgGnL7s2R7hkzOTnMoA3rjUUOI7DvY9F1Peck=-github.com/bytebase/omni v0.0.0-20260326070405-a1a6e5880a1e/go.mod h1:1HI7B1k/5jH/Po89iGM6fMoz+Jh1PqUHfgeudpZSdkk=+github.com/bytebase/omni v0.0.0-20260330073149-2f02f119f9ec h1:cgkOoqoxOaTM1BXjzp0359Idbboltho8bmYMZunQ2OM=+github.com/bytebase/omni v0.0.0-20260330073149-2f02f119f9ec/go.mod h1:1HI7B1k/5jH/Po89iGM6fMoz+Jh1PqUHfgeudpZSdkk=github.com/bytebase/parser v0.0.0-20260324092042-1d52e5412f50 h1:Kq5wlhZ586Rch39y/IQV9MnrEzHob4iiXBG6zrbvSMU=github.com/bytebase/parser v0.0.0-20260324092042-1d52e5412f50/go.mod h1:jeak/EfutSOAuWKvrFIT2IZunhWprM7oTFBRgZ9RCxo=github.com/bytebase/pgx/v5 v5.0.0-20250212161523-96ff8aed8767 h1:YuR5G3LcxpO5ScSTA+Kdirxg9RW4TRc/v1t7N2Hfmbw=