├── .github └── workflows │ └── go.yml ├── LICENSE ├── README.md ├── copy_files.sh ├── go.mod ├── go.sum ├── parser_test.go ├── patches ├── 0001-add-extra-pretty-option-for-no-new-lines.patch ├── 0002-fix-quote-names.patch ├── 0003-use-function-name.patch └── 0004-enc-always-quoted.patch ├── pkg ├── build │ ├── bazel │ │ ├── bazel.go │ │ ├── non_bazel.go │ │ └── util │ │ │ └── tinystringer │ │ │ └── main.go │ ├── cgo_compiler.go │ ├── cgo_compiler_nocgo.go │ ├── info.go │ ├── info.pb.go │ ├── info.proto │ └── version.txt ├── cli │ └── exit │ │ ├── codes.go │ │ ├── doc.go │ │ └── exit.go ├── col │ ├── coldata │ │ ├── batch.go │ │ ├── bytes.go │ │ ├── datum_vec.go │ │ ├── json.go │ │ ├── native_types.go │ │ ├── nulls.go │ │ ├── testutils.go │ │ ├── vec.eg.go │ │ ├── vec.go │ │ └── vec_tmpl.go │ └── typeconv │ │ └── typeconv.go ├── docs │ └── docs.go ├── geo │ ├── bbox.go │ ├── encode.go │ ├── errors.go │ ├── geo.go │ ├── geopb │ │ ├── config.go │ │ ├── config.pb.go │ │ ├── config.proto │ │ ├── geopb.go │ │ ├── geopb.pb.go │ │ ├── geopb.proto │ │ └── types.go │ ├── geoprojbase │ │ ├── data │ │ │ └── proj.json.gz │ │ ├── embeddedproj │ │ │ └── embedded_proj.go │ │ ├── geoprojbase.go │ │ └── projections.go │ ├── hilbert.go │ ├── iterator.go │ ├── latlng.go │ ├── parse.go │ ├── polyline.go │ └── summary.go ├── keysbase │ └── data.go ├── kv │ └── kvserver │ │ └── concurrency │ │ └── isolation │ │ ├── levels.go │ │ ├── levels.pb.go │ │ └── levels.proto ├── security │ └── username │ │ └── username.go ├── settings │ ├── bool.go │ ├── byte_size.go │ ├── common.go │ ├── doc.go │ ├── duration.go │ ├── encoding.go │ ├── encoding.pb.go │ ├── encoding.proto │ ├── enum.go │ ├── float.go │ ├── int.go │ ├── masked.go │ ├── options.go │ ├── protobuf.go │ ├── registry.go │ ├── setting.go │ ├── string.go │ ├── updater.go │ ├── values.go │ └── version.go ├── sql │ ├── colexec │ │ └── execgen │ │ │ ├── cmd │ │ │ └── execgen │ │ │ │ ├── agg_gen_util.go │ │ │ │ ├── and_or_projection_gen.go │ │ │ │ ├── any_not_null_agg_gen.go │ │ │ │ ├── avg_agg_gen.go │ │ │ │ ├── bool_and_or_agg_gen.go │ │ │ │ ├── cast_gen.go │ │ │ │ ├── cast_gen_util.go │ │ │ │ ├── concat_agg_gen.go │ │ │ │ ├── const_gen.go │ │ │ │ ├── count_agg_gen.go │ │ │ │ ├── crossjoiner_gen.go │ │ │ │ ├── data_manipulation_gen.go │ │ │ │ ├── datum_to_vec_gen.go │ │ │ │ ├── default_agg_gen.go │ │ │ │ ├── default_cmp_expr_gen.go │ │ │ │ ├── default_cmp_proj_ops_gen.go │ │ │ │ ├── default_cmp_sel_ops_gen.go │ │ │ │ ├── distinct_gen.go │ │ │ │ ├── first_last_nth_value_gen.go │ │ │ │ ├── hash_aggregator_gen.go │ │ │ │ ├── hash_utils_gen.go │ │ │ │ ├── hashjoiner_gen.go │ │ │ │ ├── hashtable_gen.go │ │ │ │ ├── is_null_ops_gen.go │ │ │ │ ├── lead_lag_gen.go │ │ │ │ ├── like_ops_gen.go │ │ │ │ ├── main.go │ │ │ │ ├── mergejoinbase_gen.go │ │ │ │ ├── mergejoiner_gen.go │ │ │ │ ├── min_max_agg_gen.go │ │ │ │ ├── min_max_removable_agg_gen.go │ │ │ │ ├── ntile_gen.go │ │ │ │ ├── ordered_synchronizer_gen.go │ │ │ │ ├── overloads_abbr.go │ │ │ │ ├── overloads_base.go │ │ │ │ ├── overloads_bin.go │ │ │ │ ├── overloads_cmp.go │ │ │ │ ├── overloads_gen_util.go │ │ │ │ ├── overloads_hash.go │ │ │ │ ├── projection_ops_gen.go │ │ │ │ ├── range_offset_handler_gen.go │ │ │ │ ├── rank_gen.go │ │ │ │ ├── relative_rank_gen.go │ │ │ │ ├── row_number_gen.go │ │ │ │ ├── rowtovec_gen.go │ │ │ │ ├── select_in_gen.go │ │ │ │ ├── selection_ops_gen.go │ │ │ │ ├── sort_gen.go │ │ │ │ ├── sorttopk_gen.go │ │ │ │ ├── span_encoder_gen.go │ │ │ │ ├── substring_gen.go │ │ │ │ ├── sum_agg_gen.go │ │ │ │ ├── values_differ_gen.go │ │ │ │ ├── vec_comparators_gen.go │ │ │ │ ├── vec_gen.go │ │ │ │ ├── vec_to_datum_gen.go │ │ │ │ ├── window_aggregator_gen.go │ │ │ │ ├── window_framer_gen.go │ │ │ │ └── window_peer_grouper_gen.go │ │ │ ├── execgen.go │ │ │ ├── inline.go │ │ │ ├── placeholders.go │ │ │ ├── supported_bin_cmp_ops.go │ │ │ ├── template.go │ │ │ └── util.go │ ├── colexecerror │ │ └── error.go │ ├── inverted │ │ ├── expression.go │ │ ├── span_expression.pb.go │ │ └── span_expression.proto │ ├── lex │ │ ├── encode.go │ │ ├── encode.pb.go │ │ └── encode.proto │ ├── lexbase │ │ ├── allkeywords │ │ │ └── main.go │ │ ├── encode.go │ │ ├── experimental_keywords.go │ │ ├── keywords.go │ │ ├── normalize.go │ │ ├── predicates.go │ │ ├── reserved_keywords.go │ │ ├── sql-gen.sh │ │ └── tokens.go │ ├── oidext │ │ └── oidext.go │ ├── parser │ │ ├── README.md │ │ ├── help.awk │ │ ├── help.go │ │ ├── help_gen_test.sh │ │ ├── help_messages.go │ │ ├── lexer.go │ │ ├── parse.go │ │ ├── replace_help_rules.awk │ │ ├── reserved_keywords.awk │ │ ├── scanner.go │ │ ├── show_syntax.go │ │ ├── sql.go │ │ ├── sql.y │ │ ├── statements │ │ │ └── statement.go │ │ └── unreserved_keywords.awk │ ├── pgrepl │ │ └── lsn │ │ │ └── lsn.go │ ├── pgwire │ │ ├── pgcode │ │ │ ├── codes.go │ │ │ ├── doc.go │ │ │ ├── errcodes.txt │ │ │ ├── generate.sh │ │ │ ├── generate_names.sh │ │ │ └── plpgsql_codenames.go │ │ └── pgerror │ │ │ ├── constraint_name.go │ │ │ ├── errors.go │ │ │ ├── errors.pb.go │ │ │ ├── errors.proto │ │ │ ├── flatten.go │ │ │ ├── internal_errors.go │ │ │ ├── pgcode.go │ │ │ ├── severity.go │ │ │ ├── with_candidate_code.go │ │ │ └── wrap.go │ ├── plpgsql │ │ └── parser │ │ │ ├── lexbase │ │ │ ├── keywords.go │ │ │ ├── tokens.go │ │ │ └── utils.go │ │ │ ├── lexer.go │ │ │ ├── parse.go │ │ │ ├── plpgsql.go │ │ │ └── plpgsql.y │ ├── privilege │ │ ├── kind.go │ │ ├── privilege.go │ │ └── target_object_type.go │ ├── scanner │ │ ├── .gitignore │ │ ├── jsonpath_scan.go │ │ ├── plpgsql_scan.go │ │ └── scan.go │ ├── sem │ │ ├── builtins │ │ │ └── builtinsregistry │ │ │ │ └── builtins_registry.go │ │ ├── cast │ │ │ ├── cast.go │ │ │ ├── cast_map.go │ │ │ ├── cast_map_gen.sh │ │ │ └── type_name.go │ │ ├── catconstants │ │ │ ├── constants.go │ │ │ ├── namespace.go │ │ │ └── schemas.go │ │ ├── catid │ │ │ ├── ids.go │ │ │ └── index_id_set.go │ │ ├── idxtype │ │ │ ├── idxtype.go │ │ │ ├── idxtype.pb.go │ │ │ └── idxtype.proto │ │ ├── plpgsqltree │ │ │ ├── constants.go │ │ │ ├── exception.go │ │ │ ├── statements.go │ │ │ ├── variable.go │ │ │ └── visitor.go │ │ ├── semenumpb │ │ │ ├── constraint.pb.go │ │ │ ├── constraint.proto │ │ │ ├── enum.go │ │ │ ├── trigger.pb.go │ │ │ └── trigger.proto │ │ ├── tree │ │ │ ├── adjust_constants.go │ │ │ ├── alter_backup.go │ │ │ ├── alter_backup_schedule.go │ │ │ ├── alter_changefeed.go │ │ │ ├── alter_database.go │ │ │ ├── alter_default_privileges.go │ │ │ ├── alter_index.go │ │ │ ├── alter_policy.go │ │ │ ├── alter_range.go │ │ │ ├── alter_role.go │ │ │ ├── alter_schema.go │ │ │ ├── alter_sequence.go │ │ │ ├── alter_table.go │ │ │ ├── alter_tenant.go │ │ │ ├── alter_type.go │ │ │ ├── analyze.go │ │ │ ├── annotation.go │ │ │ ├── backup.go │ │ │ ├── batch.go │ │ │ ├── call.go │ │ │ ├── changefeed.go │ │ │ ├── check.go │ │ │ ├── col_name.go │ │ │ ├── comment_on_column.go │ │ │ ├── comment_on_constraint.go │ │ │ ├── comment_on_database.go │ │ │ ├── comment_on_index.go │ │ │ ├── comment_on_schema.go │ │ │ ├── comment_on_table.go │ │ │ ├── comment_on_type.go │ │ │ ├── compare.go │ │ │ ├── constant.go │ │ │ ├── constant_eval.go │ │ │ ├── constants.go │ │ │ ├── constraint.go │ │ │ ├── copy.go │ │ │ ├── create.go │ │ │ ├── create_logical_replication.go │ │ │ ├── create_policy.go │ │ │ ├── create_routine.go │ │ │ ├── create_trigger.go │ │ │ ├── createtypevariety_string.go │ │ │ ├── cursor.go │ │ │ ├── data_placement.go │ │ │ ├── datum.go │ │ │ ├── datum_alloc.go │ │ │ ├── decimal.go │ │ │ ├── delete.go │ │ │ ├── discard.go │ │ │ ├── do.go │ │ │ ├── drop.go │ │ │ ├── drop_owned_by.go │ │ │ ├── drop_policy.go │ │ │ ├── eval.go │ │ │ ├── eval_binary_ops.go │ │ │ ├── eval_expr_generated.go │ │ │ ├── eval_op_generated.go │ │ │ ├── eval_unary_ops.go │ │ │ ├── evalgen │ │ │ │ ├── eval_gen.go │ │ │ │ ├── expr.go │ │ │ │ ├── op.go │ │ │ │ └── string_set.go │ │ │ ├── explain.go │ │ │ ├── export.go │ │ │ ├── expr.go │ │ │ ├── format.go │ │ │ ├── format_fingerprint.go │ │ │ ├── function_definition.go │ │ │ ├── function_name.go │ │ │ ├── fuzz.go │ │ │ ├── grant.go │ │ │ ├── import.go │ │ │ ├── indexed_vars.go │ │ │ ├── insert.go │ │ │ ├── name_part.go │ │ │ ├── name_resolution.go │ │ │ ├── object_name.go │ │ │ ├── overload.go │ │ │ ├── parse_array.go │ │ │ ├── parse_string.go │ │ │ ├── parse_tuple.go │ │ │ ├── persistence.go │ │ │ ├── pgwire_encode.go │ │ │ ├── placeholders.go │ │ │ ├── prepare.go │ │ │ ├── pretty.go │ │ │ ├── reassign_owned_by.go │ │ │ ├── regexp_cache.go │ │ │ ├── region.go │ │ │ ├── rename.go │ │ │ ├── returning.go │ │ │ ├── revoke.go │ │ │ ├── role_spec.go │ │ │ ├── routine.go │ │ │ ├── run_control.go │ │ │ ├── schedule.go │ │ │ ├── schema_feature_name.go │ │ │ ├── schema_helpers.go │ │ │ ├── scrub.go │ │ │ ├── select.go │ │ │ ├── set.go │ │ │ ├── show.go │ │ │ ├── split.go │ │ │ ├── statementreturntype_string.go │ │ │ ├── statementtype_string.go │ │ │ ├── stmt.go │ │ │ ├── survival_goal.go │ │ │ ├── table_name.go │ │ │ ├── table_pattern.go │ │ │ ├── table_ref.go │ │ │ ├── tenant.go │ │ │ ├── tenant_settings.go │ │ │ ├── testutils.go │ │ │ ├── time.go │ │ │ ├── treebin │ │ │ │ ├── binary_operator.go │ │ │ │ └── doc.go │ │ │ ├── treecmp │ │ │ │ ├── comparison_operator.go │ │ │ │ └── doc.go │ │ │ ├── treewindow │ │ │ │ ├── constants.go │ │ │ │ └── doc.go │ │ │ ├── truncate.go │ │ │ ├── txn.go │ │ │ ├── type_check.go │ │ │ ├── type_name.go │ │ │ ├── typing.go │ │ │ ├── union.go │ │ │ ├── unlisten.go │ │ │ ├── unsupported_error.go │ │ │ ├── update.go │ │ │ ├── values.go │ │ │ ├── var_expr.go │ │ │ ├── var_name.go │ │ │ ├── walk.go │ │ │ ├── with.go │ │ │ └── zone.go │ │ └── volatility │ │ │ └── volatility.go │ ├── sessiondatapb │ │ ├── local_only_session_data.go │ │ ├── local_only_session_data.pb.go │ │ ├── local_only_session_data.proto │ │ ├── sequence_cache.go │ │ ├── sequence_cache_node.go │ │ ├── session_data.go │ │ ├── session_data.pb.go │ │ ├── session_data.proto │ │ ├── session_migration.pb.go │ │ ├── session_migration.proto │ │ ├── session_revival_token.pb.go │ │ └── session_revival_token.proto │ └── types │ │ ├── alias.go │ │ ├── oid.go │ │ ├── testutils.go │ │ ├── types.go │ │ ├── types.pb.go │ │ ├── types.proto │ │ └── types_jsonpb.go └── util │ ├── admission │ └── admissionpb │ │ ├── admission_stats.pb.go │ │ ├── admission_stats.proto │ │ ├── admissionpb.go │ │ ├── doc.go │ │ ├── io_threshold.go │ │ ├── io_threshold.pb.go │ │ └── io_threshold.proto │ ├── allstacks │ └── allstacks.go │ ├── arith │ └── arith.go │ ├── base64 │ └── base64.go │ ├── bitarray │ └── bitarray.go │ ├── buildutil │ ├── crdb_test_off.go │ └── crdb_test_on.go │ ├── cache │ └── cache.go │ ├── collatedstring │ └── collatedstring.go │ ├── ctxutil │ ├── canceler_1_20.go │ ├── canceler_1_21.go │ ├── canceler_1_21_bazel.go │ ├── context.go │ ├── context_linkname.go │ ├── context_no_linkname.go │ ├── doc.go │ └── fast_value.go │ ├── debugutil │ └── debugutil.go │ ├── deduplicate │ └── deduplicate.go │ ├── duration │ ├── duration.go │ ├── duration.pb.go │ ├── duration.proto │ └── parse.go │ ├── encoding │ ├── complement_fast.go │ ├── complement_safe.go │ ├── decimal.go │ ├── encoding.go │ ├── encodingtype │ │ └── encoding_type.go │ ├── float.go │ └── type_string.go │ ├── envutil │ └── env.go │ ├── errorutil │ ├── catch.go │ ├── error.go │ ├── sentinel.go │ ├── tenant.go │ ├── tenant_deprecated_wrapper.go │ └── unimplemented │ │ └── unimplemented.go │ ├── every_n.go │ ├── fast_int_map.go │ ├── grunning │ ├── disabled.go │ ├── enabled.go │ └── grunning.go │ ├── hash.go │ ├── hlc │ ├── doc.go │ ├── hlc.go │ ├── legacy_timestamp.pb.go │ ├── legacy_timestamp.proto │ ├── timestamp.go │ ├── timestamp.pb.go │ └── timestamp.proto │ ├── humanizeutil │ ├── count.go │ ├── duration.go │ └── humanize.go │ ├── interval │ ├── btree_based_interval.go │ ├── bu23.go │ ├── interval.go │ ├── llrb_based_interval.go │ ├── range_group.go │ └── td234.go │ ├── intsets │ ├── bitmap.go │ ├── fast.go │ ├── fast_large.go │ ├── fast_small.go │ ├── fast_str.go │ ├── fast_testonly.go │ ├── oracle.go │ └── sparse.go │ ├── ipaddr │ ├── ip.go │ └── ipaddr.go │ ├── iterutil │ └── iterutil.go │ ├── json │ ├── config.go │ ├── contains.go │ ├── contains_testers.go │ ├── encode.go │ ├── encoded.go │ ├── encoded_format.go │ ├── fuzz.go │ ├── iterator.go │ ├── jentry.go │ ├── json.go │ ├── parser.go │ ├── random.go │ ├── tables.go │ └── tokenizer │ │ ├── README_crdb.md │ │ ├── buffer.go │ │ ├── decoder.go │ │ └── scanner.go │ ├── jsonbytes │ └── jsonbytes.go │ ├── jsonpath │ ├── jsonpath.go │ ├── method.go │ ├── operation.go │ ├── parser │ │ ├── jsonpath.go │ │ ├── jsonpath.y │ │ ├── lexbase │ │ │ ├── keywords.go │ │ │ ├── tokens.go │ │ │ └── utils.go │ │ ├── lexer.go │ │ └── parse.go │ ├── path.go │ └── scalar.go │ ├── log │ ├── eventpb │ │ └── eventpbgen │ │ │ ├── extract_log_channels.awk │ │ │ ├── gen.go │ │ │ └── log_channels_generated.go │ └── logpb │ │ ├── event.go │ │ ├── event.pb.go │ │ ├── event.proto │ │ ├── json_encode_generated.go │ │ ├── log.pb.go │ │ ├── log.proto │ │ ├── severity.go │ │ └── test_utils.go │ ├── metamorphic │ ├── constants.go │ ├── constants_metamorphic_disable.go │ ├── constants_metamorphic_enable.go │ └── metamorphicutil │ │ └── is_metamorphic.go │ ├── netutil │ └── addr │ │ └── addr.go │ ├── nocopy.go │ ├── num32 │ ├── doc.go │ ├── mat.go │ ├── scalar.go │ └── vec.go │ ├── pluralize.go │ ├── pretty │ ├── document.go │ ├── pretty.go │ └── util.go │ ├── protoutil │ ├── clone.go │ ├── clone.pb.go │ ├── clone.proto │ ├── jsonpb_marshal.go │ ├── marshal.go │ └── marshaler.go │ ├── race_off.go │ ├── race_on.go │ ├── randutil │ └── rand.go │ ├── reflect.go │ ├── ring │ └── ring_buffer.go │ ├── slices.go │ ├── smalltrace.go │ ├── stringencoding │ └── string_encoding.go │ ├── strings.go │ ├── strutil │ └── util.go │ ├── syncutil │ ├── atomic.go │ ├── map.go │ ├── mutex_deadlock.go │ ├── mutex_sync.go │ ├── mutex_sync_race.go │ ├── mutex_tracing.go │ └── set.go │ ├── system │ ├── cache_line.go │ └── num_cpu.go │ ├── testaddr_default.go │ ├── testaddr_random.go │ ├── timeofday │ └── time_of_day.go │ ├── timetz │ └── timetz.go │ ├── timeutil │ ├── cpustopwatch.go │ ├── gen │ │ └── main.go │ ├── lowercase_timezones_generated.go │ ├── manual_time.go │ ├── pgdate │ │ ├── field_extract.go │ │ ├── field_string.go │ │ ├── fields.go │ │ ├── math.go │ │ ├── parsing.go │ │ ├── pgdate.go │ │ ├── pgdate.pb.go │ │ ├── pgdate.proto │ │ ├── setters.go │ │ └── zone_cache.go │ ├── stopwatch.go │ ├── ticker.go │ ├── time.go │ ├── time_source.go │ ├── time_zone_util.go │ ├── timeout.go │ ├── timeout_error.go │ ├── timer.go │ ├── timeutil.go │ └── zoneinfo.go │ ├── topk.go │ ├── tracing │ ├── context.go │ ├── crdbspan.go │ ├── doc.go │ ├── span.go │ ├── span_finalizer_race_off.go │ ├── span_finalizer_race_on.go │ ├── span_inner.go │ ├── span_options.go │ ├── tags.go │ ├── test_utils.go │ ├── tracer.go │ ├── tracer_snapshots.go │ ├── tracing_aggregator.go │ ├── tracingpb │ │ ├── recorded_span.go │ │ ├── recorded_span.pb.go │ │ ├── recorded_span.proto │ │ ├── recording.go │ │ ├── tracing.go │ │ ├── tracing.pb.go │ │ └── tracing.proto │ └── utils.go │ ├── treeprinter │ └── tree_printer.go │ ├── tsearch │ ├── config.go │ ├── encoding.go │ ├── eval.go │ ├── lex.go │ ├── random.go │ ├── rank.go │ ├── snowball.go │ ├── stopwords.go │ ├── stopwords │ │ ├── chinese.stop │ │ ├── danish.stop │ │ ├── dutch.stop │ │ ├── english.stop │ │ ├── finnish.stop │ │ ├── french.stop │ │ ├── german.stop │ │ ├── hungarian.stop │ │ ├── italian.stop │ │ ├── nepali.stop │ │ ├── norwegian.stop │ │ ├── portuguese.stop │ │ ├── russian.stop │ │ ├── spanish.stop │ │ ├── swedish.stop │ │ └── turkish.stop │ ├── tsquery.go │ └── tsvector.go │ ├── uint128 │ └── uint128.go │ ├── unresolved_addr.go │ ├── unresolved_addr.pb.go │ ├── unresolved_addr.proto │ ├── uuid │ ├── codec.go │ ├── fuzz.go │ ├── generator.go │ ├── sql.go │ ├── uuid.go │ └── uuid_wrapper.go │ └── vector │ ├── vector.go │ ├── vector.pb.go │ ├── vector.proto │ └── vector_set.go ├── snapshot.sh └── version /.github/workflows/go.yml: -------------------------------------------------------------------------------- 1 | # This workflow will build a golang project 2 | # For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go 3 | 4 | name: Go 5 | 6 | on: 7 | push: 8 | branches: [ "main" ] 9 | pull_request: 10 | branches: [ "main" ] 11 | 12 | jobs: 13 | 14 | build: 15 | runs-on: ubuntu-latest 16 | steps: 17 | - uses: actions/checkout@v3 18 | 19 | - name: Set up Go 20 | uses: actions/setup-go@v3 21 | with: 22 | go-version: 1.23 23 | 24 | - name: Build 25 | run: go build -v ./... 26 | 27 | - name: Test 28 | run: go test -v ./... 29 | -------------------------------------------------------------------------------- /copy_files.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | DIR=$1 4 | FROM=$2 5 | TO=$3 6 | 7 | mkdir -p $TO/$DIR 8 | for file in $(find $FROM/$DIR -maxdepth 1 -type f); do 9 | cp $file $TO/$DIR/$(basename $file) 10 | done 11 | -------------------------------------------------------------------------------- /parser_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/cockroachdb/cockroachdb-parser/pkg/sql/parser" 7 | "github.com/cockroachdb/cockroachdb-parser/pkg/sql/sem/tree" 8 | "github.com/stretchr/testify/require" 9 | ) 10 | 11 | func TestParser(t *testing.T) { 12 | p, err := parser.ParseOne("CREATE TABLE t (a TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP)") 13 | require.NoError(t, err) 14 | f := tree.DefaultPrettyCfg() 15 | t.Log(f.Pretty(p.AST)) 16 | } 17 | -------------------------------------------------------------------------------- /patches/0002-fix-quote-names.patch: -------------------------------------------------------------------------------- 1 | diff --git a/pkg/sql/sem/tree/type_name.go b/pkg/sql/sem/tree/type_name.go 2 | index 10cd908..98daa0e 100644 3 | --- a/pkg/sql/sem/tree/type_name.go 4 | +++ b/pkg/sql/sem/tree/type_name.go 5 | @@ -42,8 +42,7 @@ func (t *TypeName) Format(ctx *FmtCtx) { 6 | 7 | // SQLString implements the ResolvableTypeReference interface. 8 | func (t *TypeName) SQLString() string { 9 | - // FmtBareIdentifiers prevents the TypeName string from being wrapped in quotations. 10 | - return AsStringWithFlags(t, FmtBareIdentifiers) 11 | + return AsStringWithFlags(t, FmtSimple) 12 | } 13 | 14 | func (t *TypeName) objectName() {} 15 | @@ -240,14 +239,12 @@ func (node *ArrayTypeReference) Format(ctx *FmtCtx) { 16 | 17 | // SQLString implements the ResolvableTypeReference interface. 18 | func (node *ArrayTypeReference) SQLString() string { 19 | - // FmtBareIdentifiers prevents the TypeName string from being wrapped in quotations. 20 | - return AsStringWithFlags(node, FmtBareIdentifiers) 21 | + return AsStringWithFlags(node, FmtSimple) 22 | } 23 | 24 | // SQLString implements the ResolvableTypeReference interface. 25 | func (name *UnresolvedObjectName) SQLString() string { 26 | - // FmtBareIdentifiers prevents the TypeName string from being wrapped in quotations. 27 | - return AsStringWithFlags(name, FmtBareIdentifiers) 28 | + return AsStringWithFlags(name, FmtSimple) 29 | } 30 | 31 | // IsReferenceSerialType returns whether the input reference is a known 32 | -------------------------------------------------------------------------------- /patches/0003-use-function-name.patch: -------------------------------------------------------------------------------- 1 | From 486ec4ba7b334a1e549fa90cb00efcacdb685983 Mon Sep 17 00:00:00 2001 2 | From: Oliver Tan 3 | Date: Mon, 15 May 2023 13:31:05 +1000 4 | Subject: [PATCH] use-function-name 5 | 6 | --- 7 | pkg/sql/sem/tree/function_name.go | 7 +++++-- 8 | 1 file changed, 5 insertions(+), 2 deletions(-) 9 | 10 | diff --git a/pkg/sql/sem/tree/function_name.go b/pkg/sql/sem/tree/function_name.go 11 | index 3b0ac48..5e29921 100644 12 | --- a/pkg/sql/sem/tree/function_name.go 13 | +++ b/pkg/sql/sem/tree/function_name.go 14 | @@ -19,7 +19,6 @@ import ( 15 | "github.com/cockroachdb/cockroachdb-parser/pkg/sql/pgwire/pgerror" 16 | "github.com/cockroachdb/cockroachdb-parser/pkg/sql/sem/catconstants" 17 | "github.com/cockroachdb/errors" 18 | - "github.com/cockroachdb/redact" 19 | "github.com/lib/pq/oid" 20 | ) 21 | 22 | @@ -146,7 +145,11 @@ func (ref *ResolvableFunctionReference) Resolve( 23 | func WrapFunction(n string) ResolvableFunctionReference { 24 | fd, ok := FunDefs[n] 25 | if !ok { 26 | - panic(errors.AssertionFailedf("function %s() not defined", redact.Safe(n))) 27 | + return ResolvableFunctionReference{ 28 | + FunctionReference: &FunctionDefinition{ 29 | + Name: n, 30 | + }, 31 | + } 32 | } 33 | return ResolvableFunctionReference{FunctionReference: fd} 34 | } 35 | -- 36 | 2.39.2 (Apple Git-143) 37 | 38 | -------------------------------------------------------------------------------- /pkg/build/bazel/non_bazel.go: -------------------------------------------------------------------------------- 1 | // Copyright 2015 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | //go:build !bazel 7 | 8 | package bazel 9 | 10 | // This file contains stub implementations for non-bazel builds. 11 | // See bazel.go for full documentation on the contracts of these functions. 12 | 13 | // BuiltWithBazel returns true iff this library was built with Bazel. 14 | func BuiltWithBazel() bool { 15 | return false 16 | } 17 | 18 | // InBazelTest returns true iff called from a test run by Bazel. 19 | func InBazelTest() bool { 20 | return false 21 | } 22 | 23 | // InTestWrapper returns true iff called from Bazel's generated test wrapper. 24 | func InTestWrapper() bool { 25 | return false 26 | } 27 | 28 | // FindBinary is not implemented. 29 | func FindBinary(pkg, name string) (string, bool) { 30 | panic("not build with Bazel") 31 | } 32 | 33 | // Runfile is not implemented. 34 | func Runfile(string) (string, error) { 35 | panic("not built with Bazel") 36 | } 37 | 38 | // RunfilesPath is not implemented. 39 | func RunfilesPath() (string, error) { 40 | panic("not built with Bazel") 41 | } 42 | 43 | // TestTmpDir is not implemented. 44 | func TestTmpDir() string { 45 | panic("not built with Bazel") 46 | } 47 | 48 | // NewTmpDir is not implemented. 49 | func NewTmpDir(prefix string) (string, error) { 50 | panic("not built with Bazel") 51 | } 52 | 53 | // RelativeTestTargetPath is not implemented. 54 | func RelativeTestTargetPath() string { 55 | panic("not built with Bazel") 56 | } 57 | 58 | // SetGoEnv is not implemented. 59 | func SetGoEnv() { 60 | panic("not built with Bazel") 61 | } 62 | -------------------------------------------------------------------------------- /pkg/build/cgo_compiler.go: -------------------------------------------------------------------------------- 1 | // Copyright 2015 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package build 7 | 8 | // const char* compilerVersionParser() { 9 | // #if defined(__clang__) 10 | // return __VERSION__; 11 | // #elif defined(__GNUC__) || defined(__GNUG__) 12 | // return "gcc " __VERSION__; 13 | // #else 14 | // return "non-gcc, non-clang (or an unrecognized version)"; 15 | // #endif 16 | // } 17 | import "C" 18 | 19 | func cgoVersion() string { 20 | return C.GoString(C.compilerVersionParser()) 21 | } 22 | -------------------------------------------------------------------------------- /pkg/build/cgo_compiler_nocgo.go: -------------------------------------------------------------------------------- 1 | // Copyright 2018 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | //go:build !cgo 7 | 8 | package build 9 | 10 | func cgoVersion() string { 11 | return "cgo-disabled" 12 | } 13 | -------------------------------------------------------------------------------- /pkg/build/version.txt: -------------------------------------------------------------------------------- 1 | v1.2.3 2 | -------------------------------------------------------------------------------- /pkg/cli/exit/exit.go: -------------------------------------------------------------------------------- 1 | // Copyright 2020 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package exit 7 | 8 | import ( 9 | "fmt" 10 | "os" 11 | 12 | "github.com/cockroachdb/redact" 13 | ) 14 | 15 | // Code represents an exit code. 16 | type Code struct { 17 | code int 18 | } 19 | 20 | // String implements the fmt.Stringer interface. 21 | func (c Code) String() string { return fmt.Sprint(c.code) } 22 | 23 | // Format implements the fmt.Formatter interface. 24 | func (c Code) Format(s fmt.State, verb rune) { 25 | _, f := redact.MakeFormat(s, verb) 26 | fmt.Fprintf(s, f, c.code) 27 | } 28 | 29 | // SafeValue implements the redact.SafeValue interface. 30 | func (c Code) SafeValue() {} 31 | 32 | var _ redact.SafeValue = Code{} 33 | 34 | // WithCode terminates the process and sets its exit status code to 35 | // the provided code. 36 | func WithCode(code Code) { 37 | os.Exit(code.code) 38 | } 39 | -------------------------------------------------------------------------------- /pkg/docs/docs.go: -------------------------------------------------------------------------------- 1 | // Copyright 2020 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package docs 7 | 8 | import ( 9 | "fmt" 10 | 11 | "github.com/cockroachdb/cockroachdb-parser/pkg/build" 12 | ) 13 | 14 | // URLBase is the root URL for the version of the docs associated with this 15 | // binary. 16 | var URLBase = "https://www.cockroachlabs.com/docs/" + build.VersionForURLs() 17 | 18 | // URLReleaseNotesBase is the root URL for the release notes for the .0 patch 19 | // release associated with this binary. 20 | var URLReleaseNotesBase = fmt.Sprintf("https://www.cockroachlabs.com/docs/releases/%s.0.html", 21 | build.VersionForURLs()) 22 | 23 | // URL generates the URL to pageName in the version of the docs associated 24 | // with this binary. 25 | func URL(pageName string) string { return URLBase + "/" + pageName } 26 | 27 | // ReleaseNotesURL generates the URL to pageName in the .0 patch release notes 28 | // docs associated with this binary. 29 | func ReleaseNotesURL(pageName string) string { return URLReleaseNotesBase + pageName } 30 | -------------------------------------------------------------------------------- /pkg/geo/geopb/config.go: -------------------------------------------------------------------------------- 1 | // Copyright 2022 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package geopb 7 | 8 | // IsEmpty returns whether the config contains a geospatial index 9 | // configuration. 10 | func (cfg Config) IsEmpty() bool { 11 | return cfg.S2Geography == nil && cfg.S2Geometry == nil 12 | } 13 | 14 | // IsGeography returns whether the config is a geography geospatial index 15 | // configuration. 16 | func (cfg Config) IsGeography() bool { 17 | return cfg.S2Geography != nil 18 | } 19 | 20 | // IsGeometry returns whether the config is a geometry geospatial index 21 | // configuration. 22 | func (cfg Config) IsGeometry() bool { 23 | return cfg.S2Geometry != nil 24 | } 25 | -------------------------------------------------------------------------------- /pkg/geo/geopb/geopb.go: -------------------------------------------------------------------------------- 1 | // Copyright 2020 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package geopb 7 | 8 | import ( 9 | "fmt" 10 | "unsafe" 11 | ) 12 | 13 | // EWKBHex returns the EWKB-hex version of this data type 14 | func (b *SpatialObject) EWKBHex() string { 15 | return fmt.Sprintf("%X", b.EWKB) 16 | } 17 | 18 | // MemSize returns the size of the spatial object in memory. If deterministic is 19 | // true, then only length of EWKB slice is included - this option should only be 20 | // used when determinism is favored over precision. 21 | func (b *SpatialObject) MemSize(deterministic bool) uintptr { 22 | var bboxSize uintptr 23 | if bbox := b.BoundingBox; bbox != nil { 24 | bboxSize = unsafe.Sizeof(*bbox) 25 | } 26 | ewkbSize := uintptr(cap(b.EWKB)) 27 | if deterministic { 28 | ewkbSize = uintptr(len(b.EWKB)) 29 | } 30 | return unsafe.Sizeof(*b) + bboxSize + ewkbSize 31 | } 32 | 33 | // MultiType returns the corresponding multi-type for a shape type, or unset 34 | // if there is no multi-type. 35 | func (s ShapeType) MultiType() ShapeType { 36 | switch s { 37 | case ShapeType_Unset: 38 | return ShapeType_Unset 39 | case ShapeType_Point, ShapeType_MultiPoint: 40 | return ShapeType_MultiPoint 41 | case ShapeType_LineString, ShapeType_MultiLineString: 42 | return ShapeType_MultiLineString 43 | case ShapeType_Polygon, ShapeType_MultiPolygon: 44 | return ShapeType_MultiPolygon 45 | case ShapeType_GeometryCollection: 46 | return ShapeType_GeometryCollection 47 | default: 48 | return ShapeType_Unset 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /pkg/geo/geoprojbase/data/proj.json.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cockroachdb/cockroachdb-parser/b03514afe32deab5e987a1a88d5946ed21eb92e5/pkg/geo/geoprojbase/data/proj.json.gz -------------------------------------------------------------------------------- /pkg/geo/hilbert.go: -------------------------------------------------------------------------------- 1 | // Copyright 2020 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package geo 7 | 8 | // hilbertInverse converts (x,y) to d on a Hilbert Curve. 9 | // Adapted from `xy2d` from https://en.wikipedia.org/wiki/Hilbert_curve#Applications_and_mapping_algorithms. 10 | func hilbertInverse(n, x, y uint64) uint64 { 11 | var d uint64 12 | for s := n / 2; s > 0; s /= 2 { 13 | var rx uint64 14 | if (x & s) > 0 { 15 | rx = 1 16 | } 17 | var ry uint64 18 | if (y & s) > 0 { 19 | ry = 1 20 | } 21 | d += s * s * ((3 * rx) ^ ry) 22 | x, y = hilbertRotate(n, x, y, rx, ry) 23 | } 24 | return d 25 | } 26 | 27 | // hilbertRotate rotates/flips a quadrant appropriately. 28 | // Adapted from `rot` in https://en.wikipedia.org/wiki/Hilbert_curve#Applications_and_mapping_algorithms. 29 | func hilbertRotate(n, x, y, rx, ry uint64) (uint64, uint64) { 30 | if ry == 0 { 31 | if rx == 1 { 32 | x = n - 1 - x 33 | y = n - 1 - y 34 | } 35 | 36 | x, y = y, x 37 | } 38 | return x, y 39 | } 40 | -------------------------------------------------------------------------------- /pkg/geo/latlng.go: -------------------------------------------------------------------------------- 1 | // Copyright 2020 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package geo 7 | 8 | import "math" 9 | 10 | // NormalizeLatitudeDegrees normalizes latitudes to the range [-90, 90]. 11 | func NormalizeLatitudeDegrees(lat float64) float64 { 12 | // math.Remainder(lat, 360) returns in the range [-180, 180]. 13 | lat = math.Remainder(lat, 360) 14 | // If we are above 90 degrees, we curve back to 0, e.g. 91 -> 89, 100 -> 80. 15 | if lat > 90 { 16 | return 180 - lat 17 | } 18 | // If we are below 90 degrees, we curve back towards 0, e.g. -91 -> -89, -100 -> -80. 19 | if lat < -90 { 20 | return -180 - lat 21 | } 22 | return lat 23 | } 24 | 25 | // NormalizeLongitudeDegrees normalizes longitude to the range [-180, 180]. 26 | func NormalizeLongitudeDegrees(lng float64) float64 { 27 | // math.Remainder(lng, 360) returns in the range [-180, 180]. 28 | return math.Remainder(lng, 360) 29 | } 30 | -------------------------------------------------------------------------------- /pkg/keysbase/data.go: -------------------------------------------------------------------------------- 1 | // Copyright 2022 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package keysbase 7 | 8 | // KeyMax is a maximum key value which sorts after all other keys. 9 | var KeyMax = []byte{0xff, 0xff} 10 | 11 | // PrefixEnd determines the end key given b as a prefix, that is the key that 12 | // sorts precisely behind all keys starting with prefix: "1" is added to the 13 | // final byte and the carry propagated. The special cases of nil and KeyMin 14 | // always returns KeyMax. 15 | func PrefixEnd(b []byte) []byte { 16 | if len(b) == 0 { 17 | return KeyMax 18 | } 19 | // Switched to "make and copy" pattern in #4963 for performance. 20 | end := make([]byte, len(b)) 21 | copy(end, b) 22 | for i := len(end) - 1; i >= 0; i-- { 23 | end[i] = end[i] + 1 24 | if end[i] != 0 { 25 | return end[:i+1] 26 | } 27 | } 28 | // This statement will only be reached if the key is already a maximal byte 29 | // string (i.e. already \xff...). 30 | return b 31 | } 32 | -------------------------------------------------------------------------------- /pkg/settings/encoding.go: -------------------------------------------------------------------------------- 1 | // Copyright 2022 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package settings 7 | 8 | import ( 9 | "github.com/cockroachdb/redact" 10 | "github.com/cockroachdb/redact/interfaces" 11 | ) 12 | 13 | // String is part of fmt.Stringer. 14 | func (v EncodedValue) String() string { 15 | return redact.Sprint(v).StripMarkers() 16 | } 17 | 18 | // SafeFormat is part of redact.SafeFormatter. 19 | func (v EncodedValue) SafeFormat(s interfaces.SafePrinter, verb rune) { 20 | s.Printf("%q (%s)", v.Value, redact.SafeString(v.Type)) 21 | } 22 | 23 | var _ redact.SafeFormatter = EncodedValue{} 24 | -------------------------------------------------------------------------------- /pkg/settings/encoding.proto: -------------------------------------------------------------------------------- 1 | // Copyright 2022 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | syntax = "proto3"; 7 | package cockroach.settings; 8 | option go_package = "github.com/cockroachdb/cockroach/pkg/settings"; 9 | 10 | import "gogoproto/gogo.proto"; 11 | 12 | // EncodedValue contains the value of a cluster setting serialized as an opaque 13 | // string, along with a type identifier. Used when storing setting values on 14 | // disk or passing them over the wire. 15 | message EncodedValue { 16 | option (gogoproto.equal) = true; 17 | option (gogoproto.goproto_stringer) = false; 18 | 19 | string value = 1; 20 | string type = 2; 21 | } 22 | -------------------------------------------------------------------------------- /pkg/sql/colexec/execgen/cmd/execgen/any_not_null_agg_gen.go: -------------------------------------------------------------------------------- 1 | // Copyright 2018 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package main 7 | 8 | import ( 9 | "io" 10 | "strings" 11 | "text/template" 12 | 13 | "github.com/cockroachdb/cockroachdb-parser/pkg/sql/sem/tree/treecmp" 14 | ) 15 | 16 | const anyNotNullAggTmpl = "pkg/sql/colexec/colexecagg/any_not_null_agg_tmpl.go" 17 | 18 | func genAnyNotNullAgg(inputFileContents string, wr io.Writer) error { 19 | r := strings.NewReplacer( 20 | "_CANONICAL_TYPE_FAMILY", "{{.CanonicalTypeFamilyStr}}", 21 | "_TYPE_WIDTH", typeWidthReplacement, 22 | "_GOTYPESLICE", "{{.GoTypeSliceName}}", 23 | "_GOTYPE", "{{.GoType}}", 24 | "_TYPE", "{{.VecMethod}}", 25 | "TemplateType", "{{.VecMethod}}", 26 | ) 27 | s := r.Replace(inputFileContents) 28 | 29 | findAnyNotNull := makeFunctionRegex("_FIND_ANY_NOT_NULL", 6) 30 | s = findAnyNotNull.ReplaceAllString(s, `{{template "findAnyNotNull" buildDict "Global" . "HasNulls" $5 "HasSel" $6}}`) 31 | 32 | s = replaceManipulationFuncs(s) 33 | 34 | tmpl, err := template.New("any_not_null_agg").Funcs(template.FuncMap{"buildDict": buildDict}).Parse(s) 35 | if err != nil { 36 | return err 37 | } 38 | 39 | return tmpl.Execute(wr, sameTypeComparisonOpToOverloads[treecmp.EQ]) 40 | } 41 | 42 | func init() { 43 | registerAggGenerator( 44 | genAnyNotNullAgg, "any_not_null_agg.eg.go", /* filenameSuffix */ 45 | anyNotNullAggTmpl, "anyNotNull" /* aggName */, false, /* genWindowVariant */ 46 | ) 47 | } 48 | -------------------------------------------------------------------------------- /pkg/sql/colexec/execgen/cmd/execgen/cast_gen.go: -------------------------------------------------------------------------------- 1 | // Copyright 2019 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package main 7 | 8 | import ( 9 | "io" 10 | "strings" 11 | "text/template" 12 | ) 13 | 14 | const castTmpl = "pkg/sql/colexec/colexecbase/cast_tmpl.go" 15 | 16 | const castOpInvocation = `{{template "castOp" buildDict "Global" . "FromInfo" $fromInfo "FromFamily" $fromFamily "ToFamily" $toFamily}}` 17 | 18 | func genCastOperators(inputFileContents string, wr io.Writer) error { 19 | r := strings.NewReplacer( 20 | "_TYPE_FAMILY", "{{.TypeFamily}}", 21 | "_TYPE_WIDTH", typeWidthReplacement, 22 | "_TO_GO_TYPE", "{{.GoType}}", 23 | "_FROM_TYPE", "{{$fromInfo.VecMethod}}", 24 | "_TO_TYPE", "{{.VecMethod}}", 25 | "_NAME", "{{$fromInfo.TypeName}}{{.TypeName}}", 26 | "_GENERATE_CAST_OP", castOpInvocation, 27 | ) 28 | s := r.Replace(inputFileContents) 29 | 30 | castRe := makeFunctionRegex("_CAST", 5) 31 | s = castRe.ReplaceAllString(s, makeTemplateFunctionCall("Cast", 5)) 32 | 33 | tmpl, err := template.New("cast").Funcs(template.FuncMap{"buildDict": buildDict}).Parse(s) 34 | if err != nil { 35 | return err 36 | } 37 | 38 | return tmpl.Execute(wr, getCastFromTmplInfos()) 39 | } 40 | 41 | func init() { 42 | registerGenerator(genCastOperators, "cast.eg.go", castTmpl) 43 | } 44 | -------------------------------------------------------------------------------- /pkg/sql/colexec/execgen/cmd/execgen/concat_agg_gen.go: -------------------------------------------------------------------------------- 1 | // Copyright 2020 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package main 7 | 8 | import ( 9 | "io" 10 | "text/template" 11 | 12 | "github.com/cockroachdb/cockroachdb-parser/pkg/sql/types" 13 | ) 14 | 15 | const concatAggTmpl = "pkg/sql/colexec/colexecagg/concat_agg_tmpl.go" 16 | 17 | func genConcatAgg(inputFileContents string, wr io.Writer) error { 18 | accumulateConcatRe := makeFunctionRegex("_ACCUMULATE_CONCAT", 5) 19 | s := accumulateConcatRe.ReplaceAllString(inputFileContents, `{{template "accumulateConcat" buildDict "HasNulls" $4 "HasSel" $5}}`) 20 | 21 | s = replaceManipulationFuncs(s) 22 | 23 | tmpl, err := template.New("concat_agg").Funcs(template.FuncMap{"buildDict": buildDict}).Parse(s) 24 | if err != nil { 25 | return err 26 | } 27 | return tmpl.Execute(wr, aggTmplInfoBase{canonicalTypeFamily: types.BytesFamily}) 28 | } 29 | 30 | func init() { 31 | registerAggGenerator( 32 | genConcatAgg, "concat_agg.eg.go", /* filenameSuffix */ 33 | concatAggTmpl, "concat" /* aggName */, true, /* genWindowVariant */ 34 | ) 35 | } 36 | -------------------------------------------------------------------------------- /pkg/sql/colexec/execgen/cmd/execgen/const_gen.go: -------------------------------------------------------------------------------- 1 | // Copyright 2019 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package main 7 | 8 | import ( 9 | "io" 10 | "strings" 11 | "text/template" 12 | 13 | "github.com/cockroachdb/cockroachdb-parser/pkg/sql/sem/tree/treecmp" 14 | ) 15 | 16 | const constTmpl = "pkg/sql/colexec/colexecbase/const_tmpl.go" 17 | 18 | func genConstOps(inputFileContents string, wr io.Writer) error { 19 | 20 | r := strings.NewReplacer( 21 | "_CANONICAL_TYPE_FAMILY", "{{.CanonicalTypeFamilyStr}}", 22 | "_TYPE_WIDTH", typeWidthReplacement, 23 | "_GOTYPE", "{{.GoType}}", 24 | "_TYPE", "{{.VecMethod}}", 25 | "TemplateType", "{{.VecMethod}}", 26 | ) 27 | s := r.Replace(inputFileContents) 28 | 29 | s = replaceManipulationFuncs(s) 30 | 31 | // Now, generate the op, from the template. 32 | tmpl, err := template.New("const_op").Parse(s) 33 | if err != nil { 34 | return err 35 | } 36 | 37 | return tmpl.Execute(wr, sameTypeComparisonOpToOverloads[treecmp.EQ]) 38 | } 39 | func init() { 40 | registerGenerator(genConstOps, "const.eg.go", constTmpl) 41 | } 42 | -------------------------------------------------------------------------------- /pkg/sql/colexec/execgen/cmd/execgen/crossjoiner_gen.go: -------------------------------------------------------------------------------- 1 | // Copyright 2020 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package main 7 | 8 | import ( 9 | "io" 10 | "strings" 11 | "text/template" 12 | 13 | "github.com/cockroachdb/cockroachdb-parser/pkg/sql/sem/tree/treecmp" 14 | ) 15 | 16 | const crossJoinerTmpl = "pkg/sql/colexec/colexecjoin/crossjoiner_tmpl.go" 17 | 18 | func genCrossJoiner(inputFileContents string, wr io.Writer) error { 19 | r := strings.NewReplacer( 20 | "_CANONICAL_TYPE_FAMILY", "{{.CanonicalTypeFamilyStr}}", 21 | "_TYPE_WIDTH", typeWidthReplacement, 22 | "TemplateType", "{{.VecMethod}}", 23 | ) 24 | s := r.Replace(inputFileContents) 25 | 26 | s = replaceManipulationFuncs(s) 27 | 28 | tmpl, err := template.New("crossjoiner").Parse(s) 29 | if err != nil { 30 | return err 31 | } 32 | 33 | return tmpl.Execute(wr, sameTypeComparisonOpToOverloads[treecmp.EQ]) 34 | } 35 | 36 | func init() { 37 | registerGenerator(genCrossJoiner, "crossjoiner.eg.go", crossJoinerTmpl) 38 | } 39 | -------------------------------------------------------------------------------- /pkg/sql/colexec/execgen/cmd/execgen/datum_to_vec_gen.go: -------------------------------------------------------------------------------- 1 | // Copyright 2020 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package main 7 | 8 | import ( 9 | "io" 10 | "strings" 11 | "text/template" 12 | ) 13 | 14 | const datumToVecTmpl = "pkg/sql/colconv/datum_to_vec_tmpl.go" 15 | 16 | func genDatumToVec(inputFileContents string, wr io.Writer) error { 17 | r := strings.NewReplacer( 18 | "_TYPE_FAMILY", "{{.TypeFamily}}", 19 | "_TYPE_WIDTH", typeWidthReplacement, 20 | ) 21 | s := r.Replace(inputFileContents) 22 | 23 | preludeRe := makeFunctionRegex("_PRELUDE", 1) 24 | s = preludeRe.ReplaceAllString(s, makeTemplateFunctionCall("Prelude", 1)) 25 | convertRe := makeFunctionRegex("_CONVERT", 1) 26 | s = convertRe.ReplaceAllString(s, makeTemplateFunctionCall("Convert", 1)) 27 | 28 | tmpl, err := template.New("utils").Parse(s) 29 | if err != nil { 30 | return err 31 | } 32 | 33 | return tmpl.Execute(wr, getRowToVecTmplInfos()) 34 | } 35 | 36 | func init() { 37 | registerGenerator(genDatumToVec, "datum_to_vec.eg.go", datumToVecTmpl) 38 | } 39 | -------------------------------------------------------------------------------- /pkg/sql/colexec/execgen/cmd/execgen/default_agg_gen.go: -------------------------------------------------------------------------------- 1 | // Copyright 2020 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package main 7 | 8 | import ( 9 | "io" 10 | "text/template" 11 | ) 12 | 13 | const defaultAggTmpl = "pkg/sql/colexec/colexecagg/default_agg_tmpl.go" 14 | 15 | func genDefaultAgg(inputFileContents string, wr io.Writer) error { 16 | addTuple := makeFunctionRegex("_ADD_TUPLE", 5) 17 | s := addTuple.ReplaceAllString(inputFileContents, `{{template "addTuple" buildDict "HasSel" $5}}`) 18 | 19 | setResult := makeFunctionRegex("_SET_RESULT", 2) 20 | s = setResult.ReplaceAllString(s, `{{template "setResult"}}`) 21 | 22 | tmpl, err := template.New("default_agg").Funcs(template.FuncMap{"buildDict": buildDict}).Parse(s) 23 | if err != nil { 24 | return err 25 | } 26 | 27 | return tmpl.Execute(wr, struct{}{}) 28 | 29 | } 30 | 31 | func init() { 32 | registerAggGenerator( 33 | genDefaultAgg, "default_agg.eg.go", /* filenameSuffix */ 34 | defaultAggTmpl, "defaultAgg" /* aggName */, false, /* genWindowVariant */ 35 | ) 36 | } 37 | -------------------------------------------------------------------------------- /pkg/sql/colexec/execgen/cmd/execgen/default_cmp_expr_gen.go: -------------------------------------------------------------------------------- 1 | // Copyright 2020 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package main 7 | 8 | import ( 9 | "io" 10 | "strings" 11 | "text/template" 12 | ) 13 | 14 | type defaultCmpExprTmplInfo struct { 15 | CalledOnNullInput bool 16 | FlippedArgs bool 17 | Negate bool 18 | } 19 | 20 | const defaultCmpExprTmpl = "pkg/sql/colexec/colexeccmp/default_cmp_expr_tmpl.go" 21 | 22 | func genDefaultCmpExpr(inputFileContents string, wr io.Writer) error { 23 | s := strings.ReplaceAll( 24 | inputFileContents, "_EXPR_NAME", "cmp{{if .CalledOnNullInput}}Nullable{{end}}"+ 25 | "{{if .FlippedArgs}}Flipped{{end}}{{if .Negate}}Negate{{end}}ExprAdapter", 26 | ) 27 | 28 | tmpl, err := template.New("default_cmp_expr").Parse(s) 29 | if err != nil { 30 | return err 31 | } 32 | var info []defaultCmpExprTmplInfo 33 | for _, nullable := range []bool{false, true} { 34 | for _, flipped := range []bool{false, true} { 35 | for _, negate := range []bool{false, true} { 36 | info = append(info, defaultCmpExprTmplInfo{ 37 | CalledOnNullInput: nullable, 38 | FlippedArgs: flipped, 39 | Negate: negate, 40 | }) 41 | } 42 | } 43 | } 44 | return tmpl.Execute(wr, info) 45 | } 46 | 47 | func init() { 48 | registerGenerator(genDefaultCmpExpr, "default_cmp_expr.eg.go", defaultCmpExprTmpl) 49 | } 50 | -------------------------------------------------------------------------------- /pkg/sql/colexec/execgen/cmd/execgen/default_cmp_sel_ops_gen.go: -------------------------------------------------------------------------------- 1 | // Copyright 2020 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package main 7 | 8 | import ( 9 | "io" 10 | "strings" 11 | "text/template" 12 | ) 13 | 14 | const defaultCmpSelOpsTmpl = "pkg/sql/colexec/colexecsel/default_cmp_sel_ops_tmpl.go" 15 | 16 | func genDefaultCmpSelOps(inputFileContents string, wr io.Writer) error { 17 | s := strings.ReplaceAll(inputFileContents, "_KIND", "{{.Kind}}") 18 | 19 | tmpl, err := template.New("default_cmp_sel_ops").Parse(s) 20 | if err != nil { 21 | return err 22 | } 23 | return tmpl.Execute(wr, []struct { 24 | HasConst bool 25 | Kind string 26 | }{ 27 | {HasConst: false, Kind: ""}, 28 | {HasConst: true, Kind: "Const"}, 29 | }) 30 | } 31 | 32 | func init() { 33 | registerGenerator(genDefaultCmpSelOps, "default_cmp_sel_ops.eg.go", defaultCmpSelOpsTmpl) 34 | } 35 | -------------------------------------------------------------------------------- /pkg/sql/colexec/execgen/cmd/execgen/hash_aggregator_gen.go: -------------------------------------------------------------------------------- 1 | // Copyright 2020 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package main 7 | 8 | import ( 9 | "fmt" 10 | "io" 11 | ) 12 | 13 | const hashAggTmpl = "pkg/sql/colexec/hash_aggregator_tmpl.go" 14 | 15 | func genHashAggregator(inputFileContents string, wr io.Writer) error { 16 | _, err := fmt.Fprint(wr, inputFileContents) 17 | return err 18 | } 19 | 20 | func init() { 21 | registerGenerator(genHashAggregator, "hash_aggregator.eg.go", hashAggTmpl) 22 | } 23 | -------------------------------------------------------------------------------- /pkg/sql/colexec/execgen/cmd/execgen/hash_utils_gen.go: -------------------------------------------------------------------------------- 1 | // Copyright 2020 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package main 7 | 8 | import ( 9 | "io" 10 | "strings" 11 | "text/template" 12 | ) 13 | 14 | const hashUtilsTmpl = "pkg/sql/colexec/colexechash/hash_utils_tmpl.go" 15 | 16 | func genHashUtils(inputFileContents string, wr io.Writer) error { 17 | 18 | r := strings.NewReplacer( 19 | "_CANONICAL_TYPE_FAMILY", "{{.CanonicalTypeFamilyStr}}", 20 | "_TYPE_WIDTH", typeWidthReplacement, 21 | "_TYPE", "{{.VecMethod}}", 22 | "TemplateType", "{{.VecMethod}}", 23 | // Currently, github.com/dave/dst library used by execgen doesn't 24 | // support the generics well, so we need to put the generic type clause 25 | // manually. 26 | "func rehash(", "func rehash[T uint32 | uint64](", 27 | ) 28 | s := r.Replace(inputFileContents) 29 | 30 | assignHash := makeFunctionRegex("_ASSIGN_HASH", 4) 31 | s = assignHash.ReplaceAllString(s, makeTemplateFunctionCall("Global.AssignHash", 4)) 32 | 33 | rehash := makeFunctionRegex("_REHASH_BODY", 7) 34 | s = rehash.ReplaceAllString(s, `{{template "rehashBody" buildDict "Global" . "HasSel" $6 "HasNulls" $7}}`) 35 | 36 | s = replaceManipulationFuncsAmbiguous(".Global", s) 37 | 38 | tmpl, err := template.New("hash_utils").Funcs(template.FuncMap{"buildDict": buildDict}).Parse(s) 39 | if err != nil { 40 | return err 41 | } 42 | 43 | return tmpl.Execute(wr, hashOverloads) 44 | } 45 | 46 | func init() { 47 | registerGenerator(genHashUtils, "hash_utils.eg.go", hashUtilsTmpl) 48 | } 49 | -------------------------------------------------------------------------------- /pkg/sql/colexec/execgen/cmd/execgen/hashjoiner_gen.go: -------------------------------------------------------------------------------- 1 | // Copyright 2018 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package main 7 | 8 | import ( 9 | "fmt" 10 | "io" 11 | ) 12 | 13 | const hashJoinerTmpl = "pkg/sql/colexec/colexecjoin/hashjoiner_tmpl.go" 14 | 15 | func genHashJoiner(inputFileContents string, wr io.Writer) error { 16 | _, err := fmt.Fprint(wr, inputFileContents) 17 | return err 18 | } 19 | 20 | func init() { 21 | registerGenerator(genHashJoiner, "hashjoiner.eg.go", hashJoinerTmpl) 22 | } 23 | -------------------------------------------------------------------------------- /pkg/sql/colexec/execgen/cmd/execgen/is_null_ops_gen.go: -------------------------------------------------------------------------------- 1 | // Copyright 2020 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package main 7 | 8 | import ( 9 | "io" 10 | "strings" 11 | "text/template" 12 | ) 13 | 14 | const isNullOpsTmpl = "pkg/sql/colexec/is_null_ops_tmpl.go" 15 | 16 | func genIsNullOps(inputFileContents string, wr io.Writer) error { 17 | r := strings.NewReplacer( 18 | "_IS_TUPLE", ".IsTuple", 19 | "_KIND", "{{.Kind}}", 20 | ) 21 | s := r.Replace(inputFileContents) 22 | 23 | computeIsNullRe := makeFunctionRegex("_COMPUTE_IS_NULL", 5) 24 | s = computeIsNullRe.ReplaceAllString(s, `{{template "computeIsNull" buildDict "HasNulls" $4 "IsTuple" $5}}`) 25 | maybeSelectRe := makeFunctionRegex("_MAYBE_SELECT", 6) 26 | s = maybeSelectRe.ReplaceAllString(s, `{{template "maybeSelect" buildDict "IsTuple" $6}}`) 27 | 28 | tmpl, err := template.New("is_null_ops").Funcs(template.FuncMap{"buildDict": buildDict}).Parse(s) 29 | if err != nil { 30 | return err 31 | } 32 | 33 | return tmpl.Execute(wr, []struct { 34 | IsTuple bool 35 | Kind string 36 | }{ 37 | {IsTuple: false, Kind: ""}, 38 | {IsTuple: true, Kind: "Tuple"}, 39 | }) 40 | } 41 | 42 | func init() { 43 | registerGenerator(genIsNullOps, "is_null_ops.eg.go", isNullOpsTmpl) 44 | } 45 | -------------------------------------------------------------------------------- /pkg/sql/colexec/execgen/cmd/execgen/mergejoinbase_gen.go: -------------------------------------------------------------------------------- 1 | // Copyright 2019 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package main 7 | 8 | import ( 9 | "io" 10 | "strings" 11 | "text/template" 12 | 13 | "github.com/cockroachdb/cockroachdb-parser/pkg/sql/sem/tree/treecmp" 14 | ) 15 | 16 | const mergeJoinBaseTmpl = "pkg/sql/colexec/colexecjoin/mergejoinbase_tmpl.go" 17 | 18 | func genMergeJoinBase(inputFileContents string, wr io.Writer) error { 19 | r := strings.NewReplacer( 20 | "_CANONICAL_TYPE_FAMILY", "{{.CanonicalTypeFamilyStr}}", 21 | "_TYPE_WIDTH", typeWidthReplacement, 22 | "_TYPE", "{{.VecMethod}}", 23 | "TemplateType", "{{.VecMethod}}", 24 | ) 25 | s := r.Replace(inputFileContents) 26 | 27 | assignEqRe := makeFunctionRegex("_ASSIGN_EQ", 6) 28 | s = assignEqRe.ReplaceAllString(s, makeTemplateFunctionCall("Assign", 6)) 29 | 30 | s = replaceManipulationFuncs(s) 31 | 32 | tmpl, err := template.New("mergejoinbase").Parse(s) 33 | if err != nil { 34 | return err 35 | } 36 | 37 | return tmpl.Execute(wr, sameTypeComparisonOpToOverloads[treecmp.EQ]) 38 | } 39 | 40 | func init() { 41 | registerGenerator(genMergeJoinBase, "mergejoinbase.eg.go", mergeJoinBaseTmpl) 42 | } 43 | -------------------------------------------------------------------------------- /pkg/sql/colexec/execgen/cmd/execgen/ntile_gen.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package main 7 | 8 | import ( 9 | "io" 10 | "strings" 11 | "text/template" 12 | ) 13 | 14 | type nTileTmplInfo struct { 15 | HasPartition bool 16 | String string 17 | } 18 | 19 | const nTileTmpl = "pkg/sql/colexec/colexecwindow/ntile_tmpl.go" 20 | 21 | func genNTileOp(inputFileContents string, wr io.Writer) error { 22 | s := strings.ReplaceAll(inputFileContents, "_NTILE_STRING", "{{.String}}") 23 | 24 | // Now, generate the op, from the template. 25 | tmpl, err := template.New("ntile_op").Funcs(template.FuncMap{"buildDict": buildDict}).Parse(s) 26 | if err != nil { 27 | return err 28 | } 29 | 30 | nTileTmplInfos := []nTileTmplInfo{ 31 | {HasPartition: false, String: "nTileNoPartition"}, 32 | {HasPartition: true, String: "nTileWithPartition"}, 33 | } 34 | return tmpl.Execute(wr, nTileTmplInfos) 35 | } 36 | 37 | func init() { 38 | registerGenerator(genNTileOp, "ntile.eg.go", nTileTmpl) 39 | } 40 | -------------------------------------------------------------------------------- /pkg/sql/colexec/execgen/cmd/execgen/ordered_synchronizer_gen.go: -------------------------------------------------------------------------------- 1 | // Copyright 2020 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package main 7 | 8 | import ( 9 | "io" 10 | "strings" 11 | "text/template" 12 | 13 | "github.com/cockroachdb/cockroachdb-parser/pkg/sql/sem/tree/treecmp" 14 | ) 15 | 16 | const ordSyncTmpl = "pkg/sql/colexec/ordered_synchronizer_tmpl.go" 17 | 18 | func genOrderedSynchronizer(inputFileContents string, wr io.Writer) error { 19 | r := strings.NewReplacer( 20 | "_CANONICAL_TYPE_FAMILY", "{{.CanonicalTypeFamilyStr}}", 21 | "_TYPE_WIDTH", typeWidthReplacement, 22 | "_TYPE", "{{.VecMethod}}", 23 | ) 24 | s := r.Replace(inputFileContents) 25 | 26 | s = replaceManipulationFuncs(s) 27 | 28 | tmpl, err := template.New("ordered_synchronizer").Parse(s) 29 | if err != nil { 30 | return err 31 | } 32 | 33 | // It doesn't matter that we're passing in all overloads of Equality 34 | // comparison operator - we simply need to iterate over all supported 35 | // types. 36 | return tmpl.Execute(wr, sameTypeComparisonOpToOverloads[treecmp.EQ]) 37 | } 38 | 39 | func init() { 40 | registerGenerator(genOrderedSynchronizer, "ordered_synchronizer.eg.go", ordSyncTmpl) 41 | } 42 | -------------------------------------------------------------------------------- /pkg/sql/colexec/execgen/cmd/execgen/row_number_gen.go: -------------------------------------------------------------------------------- 1 | // Copyright 2019 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package main 7 | 8 | import ( 9 | "io" 10 | "strings" 11 | "text/template" 12 | ) 13 | 14 | type rowNumberTmplInfo struct { 15 | HasPartition bool 16 | String string 17 | } 18 | 19 | const rowNumberTmpl = "pkg/sql/colexec/colexecwindow/row_number_tmpl.go" 20 | 21 | func genRowNumberOp(inputFileContents string, wr io.Writer) error { 22 | s := strings.ReplaceAll(inputFileContents, "_ROW_NUMBER_STRING", "{{.String}}") 23 | 24 | computeRowNumberRe := makeFunctionRegex("_COMPUTE_ROW_NUMBER", 1) 25 | s = computeRowNumberRe.ReplaceAllString(s, `{{template "computeRowNumber" buildDict "HasPartition" .HasPartition "HasSel" $1}}`) 26 | 27 | // Now, generate the op, from the template. 28 | tmpl, err := template.New("row_number_op").Funcs(template.FuncMap{"buildDict": buildDict}).Parse(s) 29 | if err != nil { 30 | return err 31 | } 32 | 33 | rowNumberTmplInfos := []rowNumberTmplInfo{ 34 | {HasPartition: false, String: "rowNumberNoPartition"}, 35 | {HasPartition: true, String: "rowNumberWithPartition"}, 36 | } 37 | return tmpl.Execute(wr, rowNumberTmplInfos) 38 | } 39 | 40 | func init() { 41 | registerGenerator(genRowNumberOp, "row_number.eg.go", rowNumberTmpl) 42 | } 43 | -------------------------------------------------------------------------------- /pkg/sql/colexec/execgen/cmd/execgen/select_in_gen.go: -------------------------------------------------------------------------------- 1 | // Copyright 2019 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package main 7 | 8 | import ( 9 | "io" 10 | "strings" 11 | "text/template" 12 | 13 | "github.com/cockroachdb/cockroachdb-parser/pkg/sql/sem/tree/treecmp" 14 | ) 15 | 16 | const selectInTmpl = "pkg/sql/colexec/select_in_tmpl.go" 17 | 18 | func genSelectIn(inputFileContents string, wr io.Writer) error { 19 | r := strings.NewReplacer( 20 | "_CANONICAL_TYPE_FAMILY", "{{.CanonicalTypeFamilyStr}}", 21 | "_TYPE_WIDTH", typeWidthReplacement, 22 | "_GOTYPESLICE", "{{.GoTypeSliceName}}", 23 | "_GOTYPE_UPCAST_INT", `{{if or (eq .VecMethod "Int16") (eq .VecMethod "Int32")}}int64{{else}}{{.GoType}}{{end}}`, 24 | "_GOTYPE", "{{.GoType}}", 25 | "_TYPE", "{{.VecMethod}}", 26 | "TemplateType", "{{.VecMethod}}", 27 | ) 28 | s := r.Replace(inputFileContents) 29 | 30 | compare := makeFunctionRegex("_COMPARE", 5) 31 | s = compare.ReplaceAllString(s, makeTemplateFunctionCall("Compare", 5)) 32 | 33 | s = replaceManipulationFuncs(s) 34 | 35 | tmpl, err := template.New("select_in").Parse(s) 36 | if err != nil { 37 | return err 38 | } 39 | 40 | return tmpl.Execute(wr, sameTypeComparisonOpToOverloads[treecmp.EQ]) 41 | } 42 | 43 | func init() { 44 | registerGenerator(genSelectIn, "select_in.eg.go", selectInTmpl) 45 | } 46 | -------------------------------------------------------------------------------- /pkg/sql/colexec/execgen/cmd/execgen/sorttopk_gen.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package main 7 | 8 | import ( 9 | "fmt" 10 | "io" 11 | ) 12 | 13 | const sortTopKTmpl = "pkg/sql/colexec/sorttopk_tmpl.go" 14 | 15 | func genSortTopK(inputFileContents string, wr io.Writer) error { 16 | _, err := fmt.Fprint(wr, inputFileContents) 17 | return err 18 | } 19 | 20 | func init() { 21 | registerGenerator(genSortTopK, "sorttopk.eg.go", sortTopKTmpl) 22 | } 23 | -------------------------------------------------------------------------------- /pkg/sql/colexec/execgen/cmd/execgen/substring_gen.go: -------------------------------------------------------------------------------- 1 | // Copyright 2020 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package main 7 | 8 | import ( 9 | "fmt" 10 | "io" 11 | "strings" 12 | "text/template" 13 | 14 | "github.com/cockroachdb/cockroachdb-parser/pkg/sql/types" 15 | ) 16 | 17 | const substringTmpl = "pkg/sql/colexec/substring_tmpl.go" 18 | 19 | func genSubstring(inputFileContents string, wr io.Writer) error { 20 | r := strings.NewReplacer( 21 | "_START_WIDTH", fmt.Sprintf("{{$startWidth}}{{if eq $startWidth %d}}: default{{end}}", anyWidth), 22 | "_LENGTH_WIDTH", fmt.Sprintf("{{$lengthWidth}}{{if eq $lengthWidth %d}}: default{{end}}", anyWidth), 23 | "_StartType", fmt.Sprintf("Int{{if eq $startWidth %d}}64{{else}}{{$startWidth}}{{end}}", anyWidth), 24 | "_LengthType", fmt.Sprintf("Int{{if eq $lengthWidth %d}}64{{else}}{{$lengthWidth}}{{end}}", anyWidth), 25 | ) 26 | s := r.Replace(inputFileContents) 27 | 28 | tmpl, err := template.New("substring").Parse(s) 29 | if err != nil { 30 | return err 31 | } 32 | 33 | supportedIntWidths := supportedWidthsByCanonicalTypeFamily[types.IntFamily] 34 | intWidthsToIntWidths := make(map[int32][]int32) 35 | for _, intWidth := range supportedIntWidths { 36 | intWidthsToIntWidths[intWidth] = supportedIntWidths 37 | } 38 | return tmpl.Execute(wr, intWidthsToIntWidths) 39 | } 40 | 41 | func init() { 42 | registerGenerator(genSubstring, "substring.eg.go", substringTmpl) 43 | } 44 | -------------------------------------------------------------------------------- /pkg/sql/colexec/execgen/cmd/execgen/values_differ_gen.go: -------------------------------------------------------------------------------- 1 | // Copyright 2019 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package main 7 | 8 | import ( 9 | "io" 10 | "strings" 11 | "text/template" 12 | 13 | "github.com/cockroachdb/cockroachdb-parser/pkg/sql/sem/tree/treecmp" 14 | ) 15 | 16 | const valuesDifferTmpl = "pkg/sql/colexec/values_differ_tmpl.go" 17 | 18 | func genValuesDiffer(inputFileContents string, wr io.Writer) error { 19 | r := strings.NewReplacer( 20 | "_CANONICAL_TYPE_FAMILY", "{{.CanonicalTypeFamilyStr}}", 21 | "_TYPE_WIDTH", typeWidthReplacement, 22 | "_GOTYPE", "{{.GoType}}", 23 | "_TYPE", "{{.VecMethod}}", 24 | "TemplateType", "{{.VecMethod}}", 25 | ) 26 | s := r.Replace(inputFileContents) 27 | 28 | assignNeRe := makeFunctionRegex("_ASSIGN_NE", 6) 29 | s = assignNeRe.ReplaceAllString(s, makeTemplateFunctionCall("Assign", 6)) 30 | 31 | s = replaceManipulationFuncs(s) 32 | 33 | // Now, generate the op, from the template. 34 | tmpl, err := template.New("values_differ").Parse(s) 35 | if err != nil { 36 | return err 37 | } 38 | 39 | return tmpl.Execute(wr, sameTypeComparisonOpToOverloads[treecmp.NE]) 40 | } 41 | func init() { 42 | registerGenerator(genValuesDiffer, "values_differ.eg.go", valuesDifferTmpl) 43 | } 44 | -------------------------------------------------------------------------------- /pkg/sql/colexec/execgen/cmd/execgen/vec_comparators_gen.go: -------------------------------------------------------------------------------- 1 | // Copyright 2019 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package main 7 | 8 | import ( 9 | "io" 10 | "strings" 11 | "text/template" 12 | 13 | "github.com/cockroachdb/cockroachdb-parser/pkg/sql/sem/tree/treecmp" 14 | ) 15 | 16 | const vecCmpTmpl = "pkg/sql/colexec/vec_comparators_tmpl.go" 17 | 18 | func genVecComparators(inputFileContents string, wr io.Writer) error { 19 | r := strings.NewReplacer( 20 | "_CANONICAL_TYPE_FAMILY", "{{.CanonicalTypeFamilyStr}}", 21 | "_TYPE_WIDTH", typeWidthReplacement, 22 | "_GOTYPESLICE", "{{.GoTypeSliceName}}", 23 | "_TYPE", "{{.VecMethod}}", 24 | ) 25 | s := r.Replace(inputFileContents) 26 | 27 | compareRe := makeFunctionRegex("_COMPARE", 5) 28 | s = compareRe.ReplaceAllString(s, makeTemplateFunctionCall("Compare", 5)) 29 | 30 | s = replaceManipulationFuncs(s) 31 | 32 | tmpl, err := template.New("vec_comparators").Parse(s) 33 | if err != nil { 34 | return err 35 | } 36 | 37 | return tmpl.Execute(wr, sameTypeComparisonOpToOverloads[treecmp.EQ]) 38 | } 39 | 40 | func init() { 41 | registerGenerator(genVecComparators, "vec_comparators.eg.go", vecCmpTmpl) 42 | } 43 | -------------------------------------------------------------------------------- /pkg/sql/colexec/execgen/cmd/execgen/vec_gen.go: -------------------------------------------------------------------------------- 1 | // Copyright 2018 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package main 7 | 8 | import ( 9 | "io" 10 | "strings" 11 | "text/template" 12 | 13 | "github.com/cockroachdb/cockroachdb-parser/pkg/sql/sem/tree/treecmp" 14 | ) 15 | 16 | const vecTmpl = "pkg/col/coldata/vec_tmpl.go" 17 | 18 | func genVec(inputFileContents string, wr io.Writer) error { 19 | r := strings.NewReplacer("_CANONICAL_TYPE_FAMILY", "{{.CanonicalTypeFamilyStr}}", 20 | "_TYPE_WIDTH", typeWidthReplacement, 21 | "_GOTYPESLICE", "{{.GoTypeSliceNameInColdata}}", 22 | "_GOTYPE", "{{.GoType}}", 23 | "_TYPE", "{{.VecMethod}}", 24 | "TemplateType", "{{.VecMethod}}", 25 | ) 26 | s := r.Replace(inputFileContents) 27 | 28 | copyWithReorderedSource := makeFunctionRegex("_COPY_WITH_REORDERED_SOURCE", 1) 29 | s = copyWithReorderedSource.ReplaceAllString(s, `{{template "copyWithReorderedSource" buildDict "Global" . "SrcHasNulls" $1}}`) 30 | 31 | s = replaceManipulationFuncs(s) 32 | 33 | // Now, generate the op, from the template. 34 | tmpl, err := template.New("vec_op").Funcs(template.FuncMap{"buildDict": buildDict}).Parse(s) 35 | if err != nil { 36 | return err 37 | } 38 | 39 | // It doesn't matter that we're passing in all overloads of Equality 40 | // comparison operator - we simply need to iterate over all supported 41 | // types. 42 | return tmpl.Execute(wr, sameTypeComparisonOpToOverloads[treecmp.EQ]) 43 | } 44 | func init() { 45 | registerGenerator(genVec, "vec.eg.go", vecTmpl) 46 | } 47 | -------------------------------------------------------------------------------- /pkg/sql/colexec/execgen/cmd/execgen/window_aggregator_gen.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package main 7 | 8 | import "io" 9 | 10 | const windowAggregatorTmpl = "pkg/sql/colexec/colexecwindow/window_aggregator_tmpl.go" 11 | 12 | func genWindowAggregator(inputFileContents string, outputFile io.Writer) error { 13 | _, err := outputFile.Write([]byte(inputFileContents)) 14 | return err 15 | } 16 | 17 | func init() { 18 | registerGenerator(genWindowAggregator, "window_aggregator.eg.go", windowAggregatorTmpl) 19 | } 20 | -------------------------------------------------------------------------------- /pkg/sql/colexec/execgen/cmd/execgen/window_peer_grouper_gen.go: -------------------------------------------------------------------------------- 1 | // Copyright 2019 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package main 7 | 8 | import ( 9 | "io" 10 | "strings" 11 | "text/template" 12 | ) 13 | 14 | type windowPeerGrouperTmplInfo struct { 15 | AllPeers bool 16 | HasPartition bool 17 | String string 18 | } 19 | 20 | const windowPeerGrouperOpsTmpl = "pkg/sql/colexec/colexecwindow/window_peer_grouper_tmpl.go" 21 | 22 | func genWindowPeerGrouperOps(inputFileContents string, wr io.Writer) error { 23 | s := strings.ReplaceAll(inputFileContents, "_PEER_GROUPER_STRING", "{{.String}}") 24 | 25 | // Now, generate the op, from the template. 26 | tmpl, err := template.New("peer_grouper_op").Parse(s) 27 | if err != nil { 28 | return err 29 | } 30 | 31 | windowPeerGrouperTmplInfos := []windowPeerGrouperTmplInfo{ 32 | {AllPeers: false, HasPartition: false, String: "windowPeerGrouperNoPartition"}, 33 | {AllPeers: false, HasPartition: true, String: "windowPeerGrouperWithPartition"}, 34 | {AllPeers: true, HasPartition: false, String: "windowPeerGrouperAllPeersNoPartition"}, 35 | {AllPeers: true, HasPartition: true, String: "windowPeerGrouperAllPeersWithPartition"}, 36 | } 37 | return tmpl.Execute(wr, windowPeerGrouperTmplInfos) 38 | } 39 | 40 | func init() { 41 | registerGenerator(genWindowPeerGrouperOps, "window_peer_grouper.eg.go", windowPeerGrouperOpsTmpl) 42 | } 43 | -------------------------------------------------------------------------------- /pkg/sql/colexec/execgen/execgen.go: -------------------------------------------------------------------------------- 1 | // Copyright 2020 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package execgen 7 | 8 | import ( 9 | "go/parser" 10 | "go/token" 11 | "strings" 12 | 13 | "github.com/dave/dst/decorator" 14 | ) 15 | 16 | // Generate transforms the string contents of an input execgen template by 17 | // processing all supported // execgen annotations. 18 | func Generate(inputFileContents string) (string, error) { 19 | f, err := decorator.ParseFile(token.NewFileSet(), "", inputFileContents, parser.ParseComments) 20 | if err != nil { 21 | return "", err 22 | } 23 | 24 | // Generate template variants: // execgen:template 25 | expandTemplates(f) 26 | 27 | // Inline functions: // execgen:inline 28 | inlineFuncs(f) 29 | 30 | // Produce output string. 31 | var sb strings.Builder 32 | if err := decorator.Fprint(&sb, f); err != nil { 33 | panic(err) 34 | } 35 | return sb.String(), nil 36 | } 37 | -------------------------------------------------------------------------------- /pkg/sql/colexec/execgen/placeholders.go: -------------------------------------------------------------------------------- 1 | // Copyright 2019 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package execgen 7 | 8 | import ( 9 | "github.com/cockroachdb/cockroachdb-parser/pkg/sql/colexecerror" 10 | "github.com/cockroachdb/errors" 11 | ) 12 | 13 | const nonTemplatePanic = "do not call from non-template code" 14 | 15 | // Remove unused warnings. 16 | var ( 17 | _ = COPYVAL 18 | _ = APPENDSLICE 19 | _ = APPENDVAL 20 | _ = SETVARIABLESIZE 21 | ) 22 | 23 | // COPYVAL is a template function that can be used to set a scalar to the value 24 | // of another scalar in such a way that the destination won't be modified if the 25 | // source is. 26 | func COPYVAL(dest, src interface{}) { 27 | colexecerror.InternalError(errors.AssertionFailedf(nonTemplatePanic)) 28 | } 29 | 30 | // APPENDSLICE is a template function. 31 | func APPENDSLICE(target, src, destIdx, srcStartIdx, srcEndIdx interface{}) { 32 | colexecerror.InternalError(errors.AssertionFailedf(nonTemplatePanic)) 33 | } 34 | 35 | // APPENDVAL is a template function. 36 | func APPENDVAL(target, v interface{}) { 37 | colexecerror.InternalError(errors.AssertionFailedf(nonTemplatePanic)) 38 | } 39 | 40 | // SETVARIABLESIZE is a template function. 41 | func SETVARIABLESIZE(target, value interface{}) interface{} { 42 | colexecerror.InternalError(errors.AssertionFailedf(nonTemplatePanic)) 43 | return nil 44 | } 45 | -------------------------------------------------------------------------------- /pkg/sql/colexec/execgen/supported_bin_cmp_ops.go: -------------------------------------------------------------------------------- 1 | // Copyright 2020 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package execgen 7 | 8 | import ( 9 | "github.com/cockroachdb/cockroachdb-parser/pkg/sql/sem/tree/treebin" 10 | "github.com/cockroachdb/cockroachdb-parser/pkg/sql/sem/tree/treecmp" 11 | ) 12 | 13 | // BinaryOpName is a mapping from all binary operators that are supported by 14 | // the vectorized engine to their names. 15 | var BinaryOpName = map[treebin.BinaryOperatorSymbol]string{ 16 | treebin.Bitand: "Bitand", 17 | treebin.Bitor: "Bitor", 18 | treebin.Bitxor: "Bitxor", 19 | treebin.Plus: "Plus", 20 | treebin.Minus: "Minus", 21 | treebin.Mult: "Mult", 22 | treebin.Div: "Div", 23 | treebin.FloorDiv: "FloorDiv", 24 | treebin.Mod: "Mod", 25 | treebin.Pow: "Pow", 26 | treebin.Concat: "Concat", 27 | treebin.LShift: "LShift", 28 | treebin.RShift: "RShift", 29 | treebin.JSONFetchVal: "JSONFetchVal", 30 | treebin.JSONFetchText: "JSONFetchText", 31 | treebin.JSONFetchValPath: "JSONFetchValPath", 32 | treebin.JSONFetchTextPath: "JSONFetchTextPath", 33 | } 34 | 35 | // ComparisonOpName is a mapping from all comparison operators that are 36 | // supported by the vectorized engine to their names. 37 | var ComparisonOpName = map[treecmp.ComparisonOperatorSymbol]string{ 38 | treecmp.EQ: "EQ", 39 | treecmp.NE: "NE", 40 | treecmp.LT: "LT", 41 | treecmp.LE: "LE", 42 | treecmp.GT: "GT", 43 | treecmp.GE: "GE", 44 | } 45 | -------------------------------------------------------------------------------- /pkg/sql/colexec/execgen/util.go: -------------------------------------------------------------------------------- 1 | // Copyright 2020 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package execgen 7 | 8 | import ( 9 | "fmt" 10 | "strings" 11 | 12 | "github.com/dave/dst" 13 | "github.com/dave/dst/decorator" 14 | ) 15 | 16 | func prettyPrintStmts(stmts ...dst.Stmt) string { 17 | if len(stmts) == 0 { 18 | return "" 19 | } 20 | f := &dst.File{ 21 | Name: dst.NewIdent("main"), 22 | Decls: []dst.Decl{ 23 | &dst.FuncDecl{ 24 | Name: dst.NewIdent("test"), 25 | Type: &dst.FuncType{}, 26 | Body: &dst.BlockStmt{ 27 | List: stmts, 28 | }, 29 | }, 30 | }, 31 | } 32 | var ret strings.Builder 33 | _ = decorator.Fprint(&ret, f) 34 | prelude := `package main 35 | 36 | func test() { 37 | ` 38 | postlude := `} 39 | ` 40 | s := ret.String() 41 | return strings.TrimSpace(s[len(prelude) : len(s)-len(postlude)]) 42 | } 43 | 44 | func prettyPrintExprs(exprs ...dst.Expr) string { 45 | stmts := make([]dst.Stmt, len(exprs)) 46 | for i := range exprs { 47 | stmts[i] = &dst.ExprStmt{X: exprs[i]} 48 | } 49 | return prettyPrintStmts(stmts...) 50 | } 51 | 52 | func parseStmt(stmt string) (dst.Stmt, error) { 53 | f, err := decorator.Parse(fmt.Sprintf( 54 | `package main 55 | func test() { 56 | %s 57 | }`, stmt)) 58 | if err != nil { 59 | return nil, err 60 | } 61 | return f.Decls[0].(*dst.FuncDecl).Body.List[0], nil 62 | } 63 | 64 | func mustParseStmt(stmt string) dst.Stmt { 65 | ret, err := parseStmt(stmt) 66 | if err != nil { 67 | panic(err) 68 | } 69 | return ret 70 | } 71 | -------------------------------------------------------------------------------- /pkg/sql/inverted/span_expression.proto: -------------------------------------------------------------------------------- 1 | // Copyright 2020 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | syntax = "proto3"; 7 | package cockroach.sql.inverted; 8 | option go_package = "github.com/cockroachdb/cockroach/pkg/sql/inverted"; 9 | 10 | import "gogoproto/gogo.proto"; 11 | 12 | // SetOperator is an operator on sets. 13 | enum SetOperator { 14 | option (gogoproto.goproto_enum_prefix) = false; 15 | 16 | // None is used in an expression node with no children. 17 | None = 0; 18 | 19 | // SetUnion unions the children. 20 | SetUnion = 1; 21 | 22 | // SetIntersection intersects the children. 23 | SetIntersection = 2; 24 | } 25 | 26 | // SpanExpressionProto is a proto representation of an inverted.Expression 27 | // tree consisting only of SpanExpressions. It is intended for use in 28 | // expression execution. 29 | message SpanExpressionProto { 30 | // Span is a span of the inverted index. Represents [start, end). 31 | message Span { 32 | bytes start = 1; 33 | bytes end = 2; 34 | } 35 | message Node { 36 | repeated Span factored_union_spans = 1 [(gogoproto.nullable) = false]; 37 | SetOperator operator = 2; 38 | Node left = 3; 39 | Node right = 4; 40 | } 41 | repeated Span spans_to_read = 1 [(gogoproto.nullable) = false]; 42 | Node node = 2 [(gogoproto.nullable) = false]; 43 | } 44 | -------------------------------------------------------------------------------- /pkg/sql/lex/encode.proto: -------------------------------------------------------------------------------- 1 | // Copyright 2022 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | syntax = "proto3"; 7 | package cockroach.sql.sessiondatapb; 8 | option go_package = "github.com/cockroachdb/cockroach/pkg/sql/lex"; 9 | 10 | import "gogoproto/gogo.proto"; 11 | 12 | // BytesEncodeFormat is the configuration for bytes to string conversions. 13 | enum BytesEncodeFormat { 14 | option (gogoproto.goproto_enum_prefix) = false; 15 | option (gogoproto.goproto_enum_stringer) = false; 16 | 17 | // BytesEncodeHex uses the hex format: e'abc\n'::BYTES::STRING -> '\x61626312'. 18 | // This is the default, for compatibility with PostgreSQL. 19 | BytesEncodeHex = 0; 20 | // BytesEncodeEscape uses the escaped format: e'abc\n'::BYTES::STRING -> 'abc\012'. 21 | BytesEncodeEscape = 1; 22 | // BytesEncodeBase64 uses base64 encoding. 23 | BytesEncodeBase64 = 2; 24 | } 25 | -------------------------------------------------------------------------------- /pkg/sql/lexbase/experimental_keywords.go: -------------------------------------------------------------------------------- 1 | // Copyright 2019 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package lexbase 7 | 8 | // AllowedExperimental contains keywords for which the EXPERIMENTAL_ 9 | // or TESTING_ prefixes are allowed to be parsed along with the 10 | // keyword to the same token. This ambiguity exists during the 11 | // deprecation period of an EXPERIMENTAL_ keyword as it is being 12 | // transitioned to the normal version. Once the transition is done, 13 | // the keyword should be removed from here as well. 14 | var AllowedExperimental = map[string]struct{}{ 15 | "ranges": {}, 16 | } 17 | -------------------------------------------------------------------------------- /pkg/sql/lexbase/sql-gen.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Copyright 2022 The Cockroach Authors. 4 | # 5 | # Use of this software is governed by the CockroachDB Software License 6 | # included in the /LICENSE file. 7 | 8 | 9 | # This is used through bazel when generating sql.go, plpgsql.go, and jsonpath.go. 10 | # Look at BUILD.bazel in pkg/sql/parser, pkg/sql/plpgsql/parser, or 11 | # pkg/util/jsonpath/parser for usage. 12 | 13 | set -euo pipefail 14 | 15 | LANG=$2 16 | SYMUNION="${LANG}"'SymUnion' 17 | GENYACC=$LANG-gen.y 18 | 19 | 20 | awk -v regex="$SYMUNION" '/func.*'"$SYMUNION"'/ {print $(NF - 1)}' $1 | \ 21 | sed -e 's/[]\/$*.^|[]/\\&/g' | \ 22 | sed -e "s/^/s_(type|token) <(/" | \ 23 | awk '{print $0")>_\\1 /* <\\2> */_"}' > types_regex.tmp 24 | 25 | sed -E -f types_regex.tmp < $1 | \ 26 | if [ $LANG != plpgsql ] && [ $LANG != pgrepl ] && [ $LANG != jsonpath ]; then \ 27 | awk -f $3 | \ 28 | sed -Ee 's,//.*$$,,g;s,/[*]([^*]|[*][^/])*[*]/, ,g;s/ +$$//g' > $GENYACC 29 | else 30 | sed -Ee 's,//.*$$,,g;s,/[*]([^*]|[*][^/])*[*]/, ,g;s/ +$$//g' > $GENYACC 31 | fi; 32 | 33 | rm types_regex.tmp 34 | 35 | ret=$($5 -p $LANG -o $4 $GENYACC); \ 36 | if expr "$ret" : ".*conflicts" >/dev/null; then \ 37 | echo "$ret"; exit 1; \ 38 | fi; 39 | rm $GENYACC 40 | -------------------------------------------------------------------------------- /pkg/sql/parser/help_gen_test.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Copyright 2017 The Cockroach Authors. 4 | # 5 | # Use of this software is governed by the CockroachDB Software License 6 | # included in the /LICENSE file. 7 | 8 | 9 | # Trigger this script by running `make generate PKG=./pkg/sql/parser` from the 10 | # repository root to ensure your PATH includes vendored binaries. 11 | 12 | set -euo pipefail 13 | 14 | # We need to set these environment variables to ensure the output of 15 | # `sort` is the same everywhere. 16 | export LC_ALL=C 17 | export LANG=C 18 | 19 | cat < 0) { 12 | # non-terminal is prefixed by |, e.g. because 13 | # there was a non-terminal before that. Extract 14 | # only its name: 15 | # | some_yacc_rule_name 16 | # ^^^^^^^^^^^^^^^^^^^ take this 17 | rulename = substr(rulename, index($0, "|")+1) 18 | } 19 | printf "%s %%prec VALUES | %s HELPTOKEN %%prec UMINUS { return helpWith(sqllex, \"%s\") }\n", prefix, rulename, helpkey 20 | next 21 | } 22 | /\/\/ SHOW HELP:/ { 23 | # some_yacc_rule // SHOW HELP: BLABLA 24 | # ^^^^^^^^^^^^^^ take this 25 | prefix = substr($0, 1, index($0, "//")-1) 26 | # some_yacc_rule // SHOW HELP: BLABLA 27 | # ^^^^^^^^ take this 28 | helpkey = substr($0, index($0, "HELP:")+6) 29 | printf "%s { return helpWith(sqllex, \"%s\") }\n", prefix, helpkey 30 | next 31 | } 32 | { print } 33 | -------------------------------------------------------------------------------- /pkg/sql/parser/reserved_keywords.awk: -------------------------------------------------------------------------------- 1 | /^col_name_keyword:/ { 2 | reserved_keyword = 1 3 | next 4 | } 5 | 6 | /^type_func_name_(no_crdb_extra|crdb_extra)?_keyword:/ { 7 | reserved_keyword = 1 8 | next 9 | } 10 | 11 | /^(cockroachdb_extra_)?reserved_keyword:/ { 12 | reserved_keyword = 1 13 | next 14 | } 15 | 16 | /^$/ { 17 | reserved_keyword = 0 18 | } 19 | 20 | BEGIN { 21 | print "// Code generated by reserved_keywords.awk. DO NOT EDIT." 22 | print "// GENERATED FILE DO NOT EDIT" 23 | print 24 | print "package lexbase" 25 | print 26 | print "var reservedKeywords = map[string]struct{}{" 27 | 28 | # This variable will be associated with a pipe for intermediate output. 29 | sort = "env LC_ALL=C sort" 30 | } 31 | 32 | { 33 | if (reserved_keyword && $NF != "") { 34 | printf("\"%s\": {},\n", tolower($NF)) | sort 35 | } 36 | } 37 | 38 | END { 39 | # Flush the intermediate output by closing the pipe. 40 | close(sort) 41 | print "}" 42 | } 43 | -------------------------------------------------------------------------------- /pkg/sql/parser/scanner.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package parser 7 | 8 | import ( 9 | "github.com/cockroachdb/cockroachdb-parser/pkg/sql/lexbase" 10 | "github.com/cockroachdb/cockroachdb-parser/pkg/sql/scanner" 11 | ) 12 | 13 | func makeSQLScanner(str string) scanner.SQLScanner { 14 | var s scanner.SQLScanner 15 | s.Init(str) 16 | return s 17 | } 18 | 19 | // SplitFirstStatement returns the length of the prefix of the string up to and 20 | // including the first semicolon that separates statements. If there is no 21 | // including the first semicolon that separates statements. If there is no 22 | // semicolon, returns ok=false. 23 | func SplitFirstStatement(sql string) (pos int, ok bool) { 24 | s := makeSQLScanner(sql) 25 | var lval = &sqlSymType{} 26 | for { 27 | s.Scan(lval) 28 | switch lval.ID() { 29 | case 0, lexbase.ERROR: 30 | return 0, false 31 | case ';': 32 | return s.Pos(), true 33 | } 34 | } 35 | } 36 | 37 | // Tokens decomposes the input into lexical tokens. 38 | func Tokens(sql string) (tokens []TokenString, ok bool) { 39 | s := makeSQLScanner(sql) 40 | for { 41 | var lval = &sqlSymType{} 42 | s.Scan(lval) 43 | if lval.ID() == lexbase.ERROR { 44 | return nil, false 45 | } 46 | if lval.ID() == 0 { 47 | break 48 | } 49 | tokens = append(tokens, TokenString{TokenID: lval.ID(), Str: lval.Str()}) 50 | } 51 | return tokens, true 52 | } 53 | 54 | // TokenString is the unit value returned by Tokens. 55 | type TokenString struct { 56 | TokenID int32 57 | Str string 58 | } 59 | -------------------------------------------------------------------------------- /pkg/sql/parser/unreserved_keywords.awk: -------------------------------------------------------------------------------- 1 | /^(cockroachdb_extra_)?reserved_keyword:/ { 2 | keyword = 0 3 | next 4 | } 5 | 6 | /^.*_keyword:/ { 7 | keyword = 1 8 | next 9 | } 10 | 11 | /^$/ { 12 | keyword = 0 13 | } 14 | 15 | { 16 | if (keyword && $NF != "") { 17 | print $NF 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /pkg/sql/pgrepl/lsn/lsn.go: -------------------------------------------------------------------------------- 1 | // Copyright 2023 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | // Package lsn contains logic for handling the pg_lsn type. 7 | package lsn 8 | 9 | import ( 10 | "fmt" 11 | 12 | "github.com/cockroachdb/apd/v3" 13 | ) 14 | 15 | type LSN uint64 16 | 17 | func (lsn LSN) String() string { 18 | return fmt.Sprintf("%X/%X", uint32(lsn>>32), uint32(lsn)) 19 | } 20 | 21 | func ParseLSN(str string) (LSN, error) { 22 | var lo, hi uint32 23 | if _, err := fmt.Sscanf(str, "%X/%X", &hi, &lo); err != nil { 24 | return 0, err 25 | } 26 | return (LSN(hi) << 32) | LSN(lo), nil 27 | } 28 | 29 | func (lsn LSN) Decimal() (*apd.Decimal, error) { 30 | ret, _, err := apd.NewFromString(fmt.Sprintf("%d", lsn)) 31 | return ret, err 32 | } 33 | 34 | func (lsn LSN) Compare(other LSN) int { 35 | if lsn > other { 36 | return 1 37 | } 38 | if lsn < other { 39 | return -1 40 | } 41 | return 0 42 | } 43 | 44 | func (lsn LSN) Add(val LSN) LSN { 45 | return lsn + val 46 | } 47 | 48 | func (lsn LSN) Sub(val LSN) LSN { 49 | return lsn - val 50 | } 51 | -------------------------------------------------------------------------------- /pkg/sql/pgwire/pgcode/doc.go: -------------------------------------------------------------------------------- 1 | // Copyright 2019 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | // Package pgcode defines the PostgreSQL 5-character support codes 7 | // used throughout the CockroachDB source tree. 8 | // 9 | // Note that CockroachDB may not use the same codes as PostgreSQL for 10 | // the same situations. 11 | package pgcode 12 | -------------------------------------------------------------------------------- /pkg/sql/pgwire/pgcode/generate.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # Copyright 2019 The Cockroach Authors. 4 | # 5 | # Use of this software is governed by the CockroachDB Software License 6 | # included in the /LICENSE file. 7 | 8 | 9 | set -eu 10 | 11 | # This script will generate a list of error codes. It will not perform 12 | # any cleanup, so some manual post-processing may be necessary for 13 | # duplicate 'Error' strings, capitalizing initialisms and acronyms, 14 | # and fixing Golint errors. 15 | sed '/^\s*$/d' errcodes.txt | 16 | sed '/^#.*$/d' | 17 | sed -E 's|^(Section.*)$|// \1|' | 18 | sed -E 's|^([A-Z0-9]{5}) . ERRCODE_([A-Z_]+).*$|\2 = MakeCode("\1")|' | 19 | # Postgres uses class 58 just for external errors, but we've extended it with some errors 20 | # internal to the cluster (inspired by DB2). 21 | sed -E 's|// Section: Class 58 - System Error \(errors external to PostgreSQL itself\)|// Section: Class 58 - System Error|' | 22 | awk '{$1=tolower($1); print $0}' | 23 | perl -pe 's/(^|_)./uc($&)/ge;s/_//g' > errcodes.generated 24 | -------------------------------------------------------------------------------- /pkg/sql/pgwire/pgcode/generate_names.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # Copyright 2023 The Cockroach Authors. 4 | # 5 | # Use of this software is governed by the CockroachDB Software License 6 | # included in the /LICENSE file. 7 | 8 | 9 | set -eu 10 | 11 | # This script will generate a mapping from condition names to error codes. 12 | # It will not perform any cleanup, so some manual post-processing may be 13 | # necessary for duplicate 'Error' strings, capitalizing initialisms and 14 | # acronyms, and fixing Golint errors. 15 | sed '/^\s*$/d' errcodes.txt | 16 | sed '/^#.*$/d' | 17 | sed -E 's|^(Section.*)$|// \1|' | 18 | sed -E 's|^([A-Z0-9]{5}) . ([A-Z_]+)[[:space:]]+([a-z_]+).*$|"\3": {"\1"},|' | 19 | # Postgres uses class 58 just for external errors, but we've extended it with some errors 20 | # internal to the cluster (inspired by DB2). 21 | sed -E 's|// Section: Class 58 - System Error \(errors external to PostgreSQL itself\)|// Section: Class 58 - System Error|' > errcodes.generated 22 | -------------------------------------------------------------------------------- /pkg/sql/pgwire/pgerror/errors.proto: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | syntax = "proto3"; 7 | package cockroach.pgerror; 8 | option go_package = "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"; 9 | 10 | // Error contains all Postgres wire protocol error fields. 11 | // See https://www.postgresql.org/docs/current/static/protocol-error-fields.html 12 | // for a list of all Postgres error fields, most of which are optional and can 13 | // be used to provide auxiliary error information. 14 | message Error { 15 | // standard pg error fields. This can be passed 16 | // over the pg wire protocol. 17 | string code = 1; 18 | string message = 2; 19 | string detail = 3; 20 | string hint = 4; 21 | string severity = 8; 22 | string constraint_name = 9; 23 | 24 | message Source { 25 | string file = 1; 26 | int32 line = 2; 27 | string function = 3; 28 | } 29 | Source source = 5; 30 | 31 | reserved 6,7; 32 | }; 33 | -------------------------------------------------------------------------------- /pkg/sql/pgwire/pgerror/internal_errors.go: -------------------------------------------------------------------------------- 1 | // Copyright 2019 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package pgerror 7 | 8 | import ( 9 | "fmt" 10 | 11 | "github.com/cockroachdb/cockroachdb-parser/pkg/build" 12 | "github.com/cockroachdb/errors" 13 | ) 14 | 15 | // This file provides facilities to track internal errors. 16 | 17 | // NewInternalTrackingError instantiates an error 18 | // meant for use with telemetry.ReportError directly. 19 | // 20 | // Do not use this! Convert uses to AssertionFailedf or similar 21 | // above. 22 | func NewInternalTrackingError(issue int, detail string) error { 23 | key := fmt.Sprintf("#%d.%s", issue, detail) 24 | err := errors.AssertionFailedWithDepthf(1, "%s", errors.Safe(key)) 25 | err = errors.WithTelemetry(err, key) 26 | err = errors.WithIssueLink(err, errors.IssueLink{IssueURL: build.MakeIssueURL(issue)}) 27 | return err 28 | } 29 | -------------------------------------------------------------------------------- /pkg/sql/pgwire/pgerror/wrap.go: -------------------------------------------------------------------------------- 1 | // Copyright 2019 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package pgerror 7 | 8 | import ( 9 | "github.com/cockroachdb/cockroachdb-parser/pkg/sql/pgwire/pgcode" 10 | "github.com/cockroachdb/errors" 11 | ) 12 | 13 | // Wrapf wraps an error and adds a pg error code. See 14 | // the doc on WrapWithDepthf for details. 15 | func Wrapf(err error, code pgcode.Code, format string, args ...interface{}) error { 16 | return WrapWithDepthf(1, err, code, format, args...) 17 | } 18 | 19 | // WrapWithDepthf wraps an error. It also annotates the provided 20 | // pg code as new candidate code, to be used if the underlying 21 | // error does not have one already. 22 | func WrapWithDepthf( 23 | depth int, err error, code pgcode.Code, format string, args ...interface{}, 24 | ) error { 25 | err = errors.WrapWithDepthf(1+depth, err, format, args...) 26 | err = WithCandidateCode(err, code) 27 | return err 28 | } 29 | 30 | // Wrap wraps an error and adds a pg error code. Only the code 31 | // is added if the message is empty. 32 | func Wrap(err error, code pgcode.Code, msg string) error { 33 | if msg == "" { 34 | return WithCandidateCode(err, code) 35 | } 36 | return WrapWithDepthf(1, err, code, "%s", msg) 37 | } 38 | -------------------------------------------------------------------------------- /pkg/sql/plpgsql/parser/lexbase/utils.go: -------------------------------------------------------------------------------- 1 | // Copyright 2023 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | // Package lexbase contains utilities for lexing plpgsql. 7 | package lexbase 8 | -------------------------------------------------------------------------------- /pkg/sql/scanner/.gitignore: -------------------------------------------------------------------------------- 1 | token_names_test.go 2 | -------------------------------------------------------------------------------- /pkg/sql/sem/cast/type_name.go: -------------------------------------------------------------------------------- 1 | // Copyright 2023 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package cast 7 | 8 | import ( 9 | "strings" 10 | 11 | "github.com/cockroachdb/cockroachdb-parser/pkg/sql/types" 12 | "github.com/lib/pq/oid" 13 | ) 14 | 15 | // CastTypeName returns the name of the type used for casting. 16 | func CastTypeName(t *types.T) string { 17 | // SQLString is wrong for these types. 18 | switch t.Oid() { 19 | case oid.T_numeric: 20 | // SQLString returns `decimal` 21 | return "numeric" 22 | case oid.T_char: 23 | // SQLString returns `"char"` 24 | return "char" 25 | case oid.T_bpchar: 26 | // SQLString returns `char`. 27 | return "bpchar" 28 | case oid.T_text: 29 | // SQLString returns `string` 30 | return "text" 31 | case oid.T_bit: 32 | // SQLString returns `decimal` 33 | return "bit" 34 | } 35 | return strings.ToLower(t.SQLString()) 36 | } 37 | -------------------------------------------------------------------------------- /pkg/sql/sem/catconstants/namespace.go: -------------------------------------------------------------------------------- 1 | // Copyright 2018 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package catconstants 7 | 8 | const ( 9 | // NamespaceTableFamilyID is the column family of the namespace table which is 10 | // actually written to. 11 | NamespaceTableFamilyID = 4 12 | 13 | // NamespaceTablePrimaryIndexID is the id of the primary index of the 14 | // namespace table. 15 | NamespaceTablePrimaryIndexID = 1 16 | 17 | // PreMigrationNamespaceTableName is the name that was used on the descriptor 18 | // of the current namespace table before the DeprecatedNamespaceTable was 19 | // migrated away. 20 | PreMigrationNamespaceTableName = "namespace2" 21 | ) 22 | -------------------------------------------------------------------------------- /pkg/sql/sem/idxtype/idxtype.proto: -------------------------------------------------------------------------------- 1 | // Copyright 2025 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | syntax = "proto3"; 7 | package cockroach.sql.sem.idxtype; 8 | option go_package = "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype"; 9 | 10 | import "gogoproto/gogo.proto"; 11 | 12 | // T represents different types of indexes that can be defined on a table. 13 | enum T { 14 | option (gogoproto.goproto_enum_prefix) = false; 15 | 16 | // FORWARD is a standard relational index, containing at most one entry for 17 | // each row in the table (if a partial index, a table row may not have a 18 | // corresponding entry in the index). 19 | FORWARD = 0; 20 | // INVERTED indexes can contain multiple entries for each row in the table, 21 | // which is useful for indexing collection or composite data types like JSONB, 22 | // ARRAY, and GEOGRAPHY. 23 | INVERTED = 1; 24 | // VECTOR indexes high-dimensional vectors to enable rapid similarity search 25 | // using an approximate nearest neighbor (ANN) algorithm. 26 | VECTOR = 2; 27 | }; 28 | -------------------------------------------------------------------------------- /pkg/sql/sem/plpgsqltree/variable.go: -------------------------------------------------------------------------------- 1 | // Copyright 2023 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package plpgsqltree 7 | 8 | import "github.com/cockroachdb/cockroachdb-parser/pkg/sql/sem/tree" 9 | 10 | type Variable = tree.Name 11 | -------------------------------------------------------------------------------- /pkg/sql/sem/semenumpb/constraint.proto: -------------------------------------------------------------------------------- 1 | // Copyright 2022 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | // This file should contain only ENUM definitions for concepts that 7 | // are visible in the SQL layer (i.e. concepts that can be configured 8 | // in a SQL query). 9 | // It uses proto3 so other packages can import those enum definitions 10 | // when needed. 11 | syntax = "proto3"; 12 | package cockroach.sql.sem.semenumpb; 13 | option go_package = "github.com/cockroachdb/cockroach/pkg/sql/sem/semenumpb"; 14 | 15 | // ForeignKeyAction describes the action which should be taken when a foreign 16 | // key constraint reference is acted upon. 17 | enum ForeignKeyAction { 18 | NO_ACTION = 0; 19 | RESTRICT = 1; 20 | SET_NULL = 2; 21 | SET_DEFAULT = 3; 22 | CASCADE = 4; 23 | } 24 | 25 | // Match is the algorithm used to compare composite keys. 26 | enum Match { 27 | SIMPLE = 0; 28 | FULL = 1; 29 | PARTIAL = 2; // Note: not actually supported, but we reserve the value for future use. 30 | } 31 | -------------------------------------------------------------------------------- /pkg/sql/sem/semenumpb/enum.go: -------------------------------------------------------------------------------- 1 | // Copyright 2022 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package semenumpb 7 | 8 | import "github.com/cockroachdb/redact" 9 | 10 | var ( 11 | _ redact.SafeValue = ForeignKeyAction(0) 12 | _ redact.SafeValue = TriggerActionTime(0) 13 | _ redact.SafeValue = TriggerEventType(0) 14 | ) 15 | 16 | // SafeValue implements redact.SafeValue. 17 | func (x ForeignKeyAction) SafeValue() {} 18 | 19 | // SafeValue implements redact.SafeValue 20 | func (TriggerActionTime) SafeValue() {} 21 | 22 | // SafeValue implements redact.SafeValue 23 | func (TriggerEventType) SafeValue() {} 24 | -------------------------------------------------------------------------------- /pkg/sql/sem/semenumpb/trigger.proto: -------------------------------------------------------------------------------- 1 | // Copyright 2024 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | // This file should contain only ENUM definitions for concepts that 7 | // are visible in the SQL layer (i.e. concepts that can be configured 8 | // in a SQL query). 9 | // It uses proto3 so other packages can import those enum definitions 10 | // when needed. 11 | syntax = "proto3"; 12 | package cockroach.sql.sem.semenumpb; 13 | option go_package = "github.com/cockroachdb/cockroach/pkg/sql/sem/semenumpb"; 14 | 15 | // TriggerActionTime describes the timing of a trigger: before, after, or 16 | // instead of the event. 17 | enum TriggerActionTime { 18 | ACTION_UNKNOWN = 0; 19 | BEFORE = 1; 20 | AFTER = 2; 21 | INSTEAD_OF = 3; 22 | } 23 | 24 | // TriggerEventType describes the event that will cause a trigger to fire: 25 | // insert, update, delete, truncate, or upsert. 26 | enum TriggerEventType { 27 | EVENT_UNKNOWN = 0; 28 | INSERT = 1; 29 | UPDATE = 2; 30 | DELETE = 3; 31 | TRUNCATE = 4; 32 | } 33 | -------------------------------------------------------------------------------- /pkg/sql/sem/tree/alter_policy.go: -------------------------------------------------------------------------------- 1 | // Copyright 2024 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package tree 7 | 8 | var _ Statement = &AlterPolicy{} 9 | 10 | // AlterPolicy is a tree struct for the ALTER POLICY DDL statement 11 | type AlterPolicy struct { 12 | PolicyName Name 13 | TableName *UnresolvedObjectName 14 | NewPolicyName Name 15 | Roles RoleSpecList 16 | Exprs PolicyExpressions 17 | } 18 | 19 | // Format implements the NodeFormatter interface. 20 | func (node *AlterPolicy) Format(ctx *FmtCtx) { 21 | ctx.WriteString("ALTER POLICY ") 22 | ctx.FormatNode(&node.PolicyName) 23 | ctx.WriteString(" ON ") 24 | ctx.FormatNode(node.TableName) 25 | 26 | if node.NewPolicyName != "" { 27 | ctx.WriteString(" RENAME TO ") 28 | ctx.FormatNode(&node.NewPolicyName) 29 | return 30 | } 31 | 32 | if len(node.Roles) > 0 { 33 | ctx.WriteString(" TO ") 34 | ctx.FormatNode(&node.Roles) 35 | } 36 | ctx.FormatNode(&node.Exprs) 37 | } 38 | -------------------------------------------------------------------------------- /pkg/sql/sem/tree/alter_schema.go: -------------------------------------------------------------------------------- 1 | // Copyright 2020 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package tree 7 | 8 | // AlterSchema represents an ALTER SCHEMA statement. 9 | type AlterSchema struct { 10 | Schema ObjectNamePrefix 11 | Cmd AlterSchemaCmd 12 | } 13 | 14 | var _ Statement = &AlterSchema{} 15 | 16 | // Format implements the NodeFormatter interface. 17 | func (node *AlterSchema) Format(ctx *FmtCtx) { 18 | ctx.WriteString("ALTER SCHEMA ") 19 | ctx.FormatNode(&node.Schema) 20 | ctx.FormatNode(node.Cmd) 21 | } 22 | 23 | // AlterSchemaCmd represents a schema modification operation. 24 | type AlterSchemaCmd interface { 25 | NodeFormatter 26 | alterSchemaCmd() 27 | } 28 | 29 | func (*AlterSchemaRename) alterSchemaCmd() {} 30 | 31 | // AlterSchemaRename represents an ALTER SCHEMA RENAME command. 32 | type AlterSchemaRename struct { 33 | NewName Name 34 | } 35 | 36 | // Format implements the NodeFormatter interface. 37 | func (node *AlterSchemaRename) Format(ctx *FmtCtx) { 38 | ctx.WriteString(" RENAME TO ") 39 | ctx.FormatNode(&node.NewName) 40 | } 41 | 42 | func (*AlterSchemaOwner) alterSchemaCmd() {} 43 | 44 | // AlterSchemaOwner represents an ALTER SCHEMA OWNER TO command. 45 | type AlterSchemaOwner struct { 46 | Owner RoleSpec 47 | } 48 | 49 | // Format implements the NodeFormatter interface. 50 | func (node *AlterSchemaOwner) Format(ctx *FmtCtx) { 51 | ctx.WriteString(" OWNER TO ") 52 | ctx.FormatNode(&node.Owner) 53 | } 54 | -------------------------------------------------------------------------------- /pkg/sql/sem/tree/alter_sequence.go: -------------------------------------------------------------------------------- 1 | // Copyright 2015 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package tree 7 | 8 | // AlterSequence represents an ALTER SEQUENCE statement, except in the case of 9 | // ALTER SEQUENCE RENAME TO , which is represented by a 10 | // RenameTable node. 11 | type AlterSequence struct { 12 | IfExists bool 13 | Name *UnresolvedObjectName 14 | Options SequenceOptions 15 | } 16 | 17 | // Format implements the NodeFormatter interface. 18 | func (node *AlterSequence) Format(ctx *FmtCtx) { 19 | ctx.WriteString("ALTER SEQUENCE ") 20 | if node.IfExists { 21 | ctx.WriteString("IF EXISTS ") 22 | } 23 | ctx.FormatNode(node.Name) 24 | ctx.FormatNode(&node.Options) 25 | } 26 | -------------------------------------------------------------------------------- /pkg/sql/sem/tree/analyze.go: -------------------------------------------------------------------------------- 1 | // Copyright 2020 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package tree 7 | 8 | // Analyze represents an ANALYZE statement. 9 | type Analyze struct { 10 | Table TableExpr 11 | } 12 | 13 | // Format implements the NodeFormatter interface. 14 | func (node *Analyze) Format(ctx *FmtCtx) { 15 | ctx.WriteString("ANALYZE ") 16 | ctx.FormatNode(node.Table) 17 | } 18 | -------------------------------------------------------------------------------- /pkg/sql/sem/tree/annotation.go: -------------------------------------------------------------------------------- 1 | // Copyright 2018 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package tree 7 | 8 | // AnnotationIdx is the 1-based index of an annotation. AST nodes that can 9 | // be annotated store such an index (unique within that AST). 10 | type AnnotationIdx int32 11 | 12 | // NoAnnotation is the uninitialized annotation index. 13 | const NoAnnotation AnnotationIdx = 0 14 | 15 | // AnnotatedNode is embedded in AST nodes that have an annotation. 16 | type AnnotatedNode struct { 17 | AnnIdx AnnotationIdx 18 | } 19 | 20 | // GetAnnotation retrieves the annotation associated with this node. 21 | func (n AnnotatedNode) GetAnnotation(ann *Annotations) interface{} { 22 | if n.AnnIdx == NoAnnotation { 23 | return nil 24 | } 25 | return ann.Get(n.AnnIdx) 26 | } 27 | 28 | // SetAnnotation sets the annotation associated with this node. 29 | func (n AnnotatedNode) SetAnnotation(ann *Annotations, annotation interface{}) { 30 | ann.Set(n.AnnIdx, annotation) 31 | } 32 | 33 | // Annotations is a container for AST annotations. 34 | type Annotations []interface{} 35 | 36 | // MakeAnnotations allocates an annotations container of the given size. 37 | func MakeAnnotations(numAnnotations AnnotationIdx) Annotations { 38 | return make(Annotations, numAnnotations) 39 | } 40 | 41 | // Set an annotation in the container. 42 | func (a *Annotations) Set(idx AnnotationIdx, annotation interface{}) { 43 | (*a)[idx-1] = annotation 44 | } 45 | 46 | // Get an annotation from the container. 47 | func (a *Annotations) Get(idx AnnotationIdx) interface{} { 48 | return (*a)[idx-1] 49 | } 50 | -------------------------------------------------------------------------------- /pkg/sql/sem/tree/batch.go: -------------------------------------------------------------------------------- 1 | // Copyright 2023 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package tree 7 | 8 | // Batch represents a BATCH clause. 9 | type Batch struct { 10 | Params []BatchParam 11 | } 12 | 13 | var _ NodeFormatter = &Batch{} 14 | 15 | type BatchParam interface { 16 | NodeFormatter 17 | } 18 | 19 | // SizeBatchParam represents a BATCH (SIZE size) parameter. 20 | type SizeBatchParam struct { 21 | // Size is the expression specified by SIZE . 22 | // It must be positive. 23 | Size Expr 24 | } 25 | 26 | // BatchParam represents a BATCH (param) parameter. 27 | var _ BatchParam = &SizeBatchParam{} 28 | 29 | // Format implements NodeFormatter. 30 | func (p *SizeBatchParam) Format(ctx *FmtCtx) { 31 | ctx.WriteString("SIZE ") 32 | p.Size.Format(ctx) 33 | } 34 | 35 | // Format implements the NodeFormatter interface. 36 | func (b *Batch) Format(ctx *FmtCtx) { 37 | if b == nil { 38 | return 39 | } 40 | ctx.WriteString("BATCH ") 41 | params := b.Params 42 | if len(params) > 0 { 43 | ctx.WriteString("(") 44 | for i, param := range params { 45 | if i > 0 { 46 | ctx.WriteString(",") 47 | } 48 | param.Format(ctx) 49 | } 50 | ctx.WriteString(") ") 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /pkg/sql/sem/tree/call.go: -------------------------------------------------------------------------------- 1 | // Copyright 2023 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package tree 7 | 8 | // Call represents a CALL statement to invoke a procedure. 9 | type Call struct { 10 | // Proc contains the procedure reference and the arguments of the procedure. 11 | Proc *FuncExpr 12 | } 13 | 14 | // Format implements the NodeFormatter interface. 15 | func (node *Call) Format(ctx *FmtCtx) { 16 | ctx.WriteString("CALL ") 17 | ctx.FormatNode(node.Proc) 18 | } 19 | 20 | var _ Statement = &Call{} 21 | -------------------------------------------------------------------------------- /pkg/sql/sem/tree/comment_on_column.go: -------------------------------------------------------------------------------- 1 | // Copyright 2018 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package tree 7 | 8 | import "github.com/cockroachdb/cockroachdb-parser/pkg/sql/lexbase" 9 | 10 | // CommentOnColumn represents an COMMENT ON COLUMN statement. 11 | type CommentOnColumn struct { 12 | *ColumnItem 13 | Comment *string 14 | } 15 | 16 | // Format implements the NodeFormatter interface. 17 | func (n *CommentOnColumn) Format(ctx *FmtCtx) { 18 | ctx.WriteString("COMMENT ON COLUMN ") 19 | ctx.FormatNode(n.ColumnItem) 20 | ctx.WriteString(" IS ") 21 | if n.Comment != nil { 22 | // TODO(knz): Replace all this with ctx.FormatNode 23 | // when COMMENT supports expressions. 24 | if ctx.flags.HasFlags(FmtHideConstants) { 25 | ctx.WriteString("'_'") 26 | } else { 27 | lexbase.EncodeSQLStringWithFlags(&ctx.Buffer, *n.Comment, ctx.flags.EncodeFlags()) 28 | } 29 | } else { 30 | ctx.WriteString("NULL") 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /pkg/sql/sem/tree/comment_on_constraint.go: -------------------------------------------------------------------------------- 1 | // Copyright 2018 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package tree 7 | 8 | import "github.com/cockroachdb/cockroachdb-parser/pkg/sql/lexbase" 9 | 10 | // CommentOnConstraint represents a COMMENT ON CONSTRAINT statement 11 | type CommentOnConstraint struct { 12 | Constraint Name 13 | Table *UnresolvedObjectName 14 | Comment *string 15 | } 16 | 17 | // Format implements the NodeFormatter interface. 18 | func (n *CommentOnConstraint) Format(ctx *FmtCtx) { 19 | ctx.WriteString("COMMENT ON CONSTRAINT ") 20 | ctx.FormatNode(&n.Constraint) 21 | ctx.WriteString(" ON ") 22 | ctx.FormatNode(n.Table) 23 | ctx.WriteString(" IS ") 24 | if n.Comment != nil { 25 | // TODO(knz): Replace all this with ctx.FormatNode 26 | // when COMMENT supports expressions. 27 | if ctx.flags.HasFlags(FmtHideConstants) { 28 | ctx.WriteByte('_') 29 | } else { 30 | lexbase.EncodeSQLStringWithFlags(&ctx.Buffer, *n.Comment, ctx.flags.EncodeFlags()) 31 | } 32 | } else { 33 | ctx.WriteString("NULL") 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /pkg/sql/sem/tree/comment_on_database.go: -------------------------------------------------------------------------------- 1 | // Copyright 2018 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package tree 7 | 8 | import "github.com/cockroachdb/cockroachdb-parser/pkg/sql/lexbase" 9 | 10 | // CommentOnDatabase represents an COMMENT ON DATABASE statement. 11 | type CommentOnDatabase struct { 12 | Name Name 13 | Comment *string 14 | } 15 | 16 | // Format implements the NodeFormatter interface. 17 | func (n *CommentOnDatabase) Format(ctx *FmtCtx) { 18 | ctx.WriteString("COMMENT ON DATABASE ") 19 | ctx.FormatNode(&n.Name) 20 | ctx.WriteString(" IS ") 21 | if n.Comment != nil { 22 | // TODO(knz): Replace all this with ctx.FormatNode 23 | // when COMMENT supports expressions. 24 | if ctx.flags.HasFlags(FmtHideConstants) { 25 | ctx.WriteString("'_'") 26 | } else { 27 | lexbase.EncodeSQLStringWithFlags(&ctx.Buffer, *n.Comment, ctx.flags.EncodeFlags()) 28 | } 29 | } else { 30 | ctx.WriteString("NULL") 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /pkg/sql/sem/tree/comment_on_index.go: -------------------------------------------------------------------------------- 1 | // Copyright 2019 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package tree 7 | 8 | import "github.com/cockroachdb/cockroachdb-parser/pkg/sql/lexbase" 9 | 10 | // CommentOnIndex represents a COMMENT ON INDEX statement. 11 | type CommentOnIndex struct { 12 | Index TableIndexName 13 | Comment *string 14 | } 15 | 16 | // Format implements the NodeFormatter interface. 17 | func (n *CommentOnIndex) Format(ctx *FmtCtx) { 18 | ctx.WriteString("COMMENT ON INDEX ") 19 | ctx.FormatNode(&n.Index) 20 | ctx.WriteString(" IS ") 21 | if n.Comment != nil { 22 | // TODO(knz): Replace all this with ctx.FormatNode 23 | // when COMMENT supports expressions. 24 | if ctx.flags.HasFlags(FmtHideConstants) { 25 | ctx.WriteString("'_'") 26 | } else { 27 | lexbase.EncodeSQLStringWithFlags(&ctx.Buffer, *n.Comment, ctx.flags.EncodeFlags()) 28 | } 29 | } else { 30 | ctx.WriteString("NULL") 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /pkg/sql/sem/tree/comment_on_schema.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package tree 7 | 8 | import "github.com/cockroachdb/cockroachdb-parser/pkg/sql/lexbase" 9 | 10 | // CommentOnSchema represents an COMMENT ON SCHEMA statement. 11 | type CommentOnSchema struct { 12 | Name ObjectNamePrefix 13 | Comment *string 14 | } 15 | 16 | // Format implements the NodeFormatter interface. 17 | func (n *CommentOnSchema) Format(ctx *FmtCtx) { 18 | ctx.WriteString("COMMENT ON SCHEMA ") 19 | ctx.FormatNode(&n.Name) 20 | ctx.WriteString(" IS ") 21 | if n.Comment != nil { 22 | // TODO(knz): Replace all this with ctx.FormatNode 23 | // when COMMENT supports expressions. 24 | if ctx.flags.HasFlags(FmtHideConstants) { 25 | ctx.WriteByte('_') 26 | } else { 27 | lexbase.EncodeSQLStringWithFlags(&ctx.Buffer, *n.Comment, ctx.flags.EncodeFlags()) 28 | } 29 | } else { 30 | ctx.WriteString("NULL") 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /pkg/sql/sem/tree/comment_on_table.go: -------------------------------------------------------------------------------- 1 | // Copyright 2018 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package tree 7 | 8 | import "github.com/cockroachdb/cockroachdb-parser/pkg/sql/lexbase" 9 | 10 | // CommentOnTable represents an COMMENT ON TABLE statement. 11 | type CommentOnTable struct { 12 | Table *UnresolvedObjectName 13 | Comment *string 14 | } 15 | 16 | // Format implements the NodeFormatter interface. 17 | func (n *CommentOnTable) Format(ctx *FmtCtx) { 18 | ctx.WriteString("COMMENT ON TABLE ") 19 | ctx.FormatNode(n.Table) 20 | ctx.WriteString(" IS ") 21 | if n.Comment != nil { 22 | // TODO(knz): Replace all this with ctx.FormatNode 23 | // when COMMENT supports expressions. 24 | if ctx.flags.HasFlags(FmtHideConstants) { 25 | ctx.WriteString("'_'") 26 | } else { 27 | lexbase.EncodeSQLStringWithFlags(&ctx.Buffer, *n.Comment, ctx.flags.EncodeFlags()) 28 | } 29 | } else { 30 | ctx.WriteString("NULL") 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /pkg/sql/sem/tree/comment_on_type.go: -------------------------------------------------------------------------------- 1 | // Copyright 2024 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package tree 7 | 8 | import "github.com/cockroachdb/cockroachdb-parser/pkg/sql/lexbase" 9 | 10 | // CommentOnType represents a COMMENT ON TYPE statement. 11 | type CommentOnType struct { 12 | Name *UnresolvedObjectName 13 | Comment *string 14 | } 15 | 16 | // Format implements the NodeFormatter interface. 17 | func (n *CommentOnType) Format(ctx *FmtCtx) { 18 | ctx.WriteString("COMMENT ON TYPE ") 19 | ctx.FormatNode(n.Name) 20 | ctx.WriteString(" IS ") 21 | if n.Comment != nil { 22 | // TODO(knz): Replace all this with ctx.FormatNode 23 | // when COMMENT supports expressions. 24 | if ctx.flags.HasFlags(FmtHideConstants) { 25 | ctx.WriteString("'_'") 26 | } else { 27 | lexbase.EncodeSQLStringWithFlags(&ctx.Buffer, *n.Comment, ctx.flags.EncodeFlags()) 28 | } 29 | } else { 30 | ctx.WriteString("NULL") 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /pkg/sql/sem/tree/constant_eval.go: -------------------------------------------------------------------------------- 1 | // Copyright 2018 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package tree 7 | 8 | import ( 9 | "github.com/cockroachdb/cockroachdb-parser/pkg/sql/sem/cast" 10 | "github.com/cockroachdb/cockroachdb-parser/pkg/sql/sem/volatility" 11 | ) 12 | 13 | // OperatorIsImmutable returns true if the given expression corresponds to a 14 | // constant operator. Note importantly that this will return true for all 15 | // expr types other than FuncExpr, CastExpr, UnaryExpr, BinaryExpr, and 16 | // ComparisonExpr. It does not do any recursive searching. 17 | func OperatorIsImmutable(expr Expr) bool { 18 | switch t := expr.(type) { 19 | case *FuncExpr: 20 | return t.ResolvedOverload().Class == NormalClass && t.fn.Volatility <= volatility.Immutable 21 | 22 | case *CastExpr: 23 | v, ok := cast.LookupCastVolatility(t.Expr.(TypedExpr).ResolvedType(), t.typ) 24 | return ok && v <= volatility.Immutable 25 | 26 | case *UnaryExpr: 27 | return t.op.Volatility <= volatility.Immutable 28 | 29 | case *BinaryExpr: 30 | return t.Op.Volatility <= volatility.Immutable 31 | 32 | case *ComparisonExpr: 33 | return t.Op.Volatility <= volatility.Immutable 34 | 35 | default: 36 | return true 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /pkg/sql/sem/tree/constants.go: -------------------------------------------------------------------------------- 1 | // Copyright 2020 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package tree 7 | 8 | // NoColumnIdx is a special value that can be used as a "column index" to 9 | // indicate that the column is not present. 10 | const NoColumnIdx = -1 11 | -------------------------------------------------------------------------------- /pkg/sql/sem/tree/createtypevariety_string.go: -------------------------------------------------------------------------------- 1 | // Copyright 2023 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | // Code generated by "stringer"; DO NOT EDIT. 7 | 8 | package tree 9 | 10 | import "strconv" 11 | 12 | func _() { 13 | // An "invalid array index" compiler error signifies that the constant values have changed. 14 | // Re-run the stringer command to generate them again. 15 | var x [1]struct{} 16 | _ = x[Enum-1] 17 | _ = x[Composite-2] 18 | _ = x[Range-3] 19 | _ = x[Base-4] 20 | _ = x[Shell-5] 21 | _ = x[Domain-6] 22 | } 23 | 24 | func (i CreateTypeVariety) String() string { 25 | switch i { 26 | case Enum: 27 | return "Enum" 28 | case Composite: 29 | return "Composite" 30 | case Range: 31 | return "Range" 32 | case Base: 33 | return "Base" 34 | case Shell: 35 | return "Shell" 36 | case Domain: 37 | return "Domain" 38 | default: 39 | return "CreateTypeVariety(" + strconv.FormatInt(int64(i), 10) + ")" 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /pkg/sql/sem/tree/delete.go: -------------------------------------------------------------------------------- 1 | // Copyright 2012, Google Inc. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in licenses/BSD-vitess.txt. 4 | 5 | // Portions of this file are additionally subject to the following 6 | // license and copyright. 7 | // 8 | // Copyright 2015 The Cockroach Authors. 9 | // 10 | // Use of this software is governed by the CockroachDB Software License 11 | // included in the /LICENSE file. 12 | 13 | // This code was derived from https://github.com/youtube/vitess. 14 | 15 | package tree 16 | 17 | // Delete represents a DELETE statement. 18 | type Delete struct { 19 | Batch *Batch 20 | With *With 21 | Table TableExpr 22 | Where *Where 23 | OrderBy OrderBy 24 | Using TableExprs 25 | Limit *Limit 26 | Returning ReturningClause 27 | } 28 | 29 | // Format implements the NodeFormatter interface. 30 | func (node *Delete) Format(ctx *FmtCtx) { 31 | ctx.FormatNode(node.With) 32 | ctx.WriteString("DELETE ") 33 | ctx.FormatNode(node.Batch) 34 | ctx.WriteString("FROM ") 35 | ctx.FormatNode(node.Table) 36 | if len(node.Using) > 0 { 37 | ctx.WriteString(" USING ") 38 | ctx.FormatNode(&node.Using) 39 | } 40 | if node.Where != nil { 41 | ctx.WriteByte(' ') 42 | ctx.FormatNode(node.Where) 43 | } 44 | if len(node.OrderBy) > 0 { 45 | ctx.WriteByte(' ') 46 | ctx.FormatNode(&node.OrderBy) 47 | } 48 | if node.Limit != nil { 49 | ctx.WriteByte(' ') 50 | ctx.FormatNode(node.Limit) 51 | } 52 | if HasReturningClause(node.Returning) { 53 | ctx.WriteByte(' ') 54 | ctx.FormatNode(node.Returning) 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /pkg/sql/sem/tree/discard.go: -------------------------------------------------------------------------------- 1 | // Copyright 2017 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package tree 7 | 8 | // Discard represents a DISCARD statement. 9 | type Discard struct { 10 | Mode DiscardMode 11 | } 12 | 13 | var _ Statement = &Discard{} 14 | 15 | // DiscardMode is an enum of the various discard modes. 16 | type DiscardMode int 17 | 18 | const ( 19 | // DiscardModeAll represents a DISCARD ALL statement. 20 | DiscardModeAll DiscardMode = iota 21 | 22 | // DiscardModeSequences represents a DISCARD SEQUENCES statement 23 | DiscardModeSequences 24 | 25 | // DiscardModeTemp represents a DISCARD TEMPORARY statement 26 | DiscardModeTemp 27 | ) 28 | 29 | // Format implements the NodeFormatter interface. 30 | func (node *Discard) Format(ctx *FmtCtx) { 31 | switch node.Mode { 32 | case DiscardModeAll: 33 | ctx.WriteString("DISCARD ALL") 34 | case DiscardModeSequences: 35 | ctx.WriteString("DISCARD SEQUENCES") 36 | case DiscardModeTemp: 37 | ctx.WriteString("DISCARD TEMPORARY") 38 | } 39 | } 40 | 41 | // String implements the Statement interface. 42 | func (node *Discard) String() string { 43 | return AsString(node) 44 | } 45 | -------------------------------------------------------------------------------- /pkg/sql/sem/tree/drop_owned_by.go: -------------------------------------------------------------------------------- 1 | // Copyright 2020 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package tree 7 | 8 | // DropOwnedBy represents a DROP OWNED BY command. 9 | type DropOwnedBy struct { 10 | Roles RoleSpecList 11 | DropBehavior DropBehavior 12 | } 13 | 14 | var _ Statement = &DropOwnedBy{} 15 | 16 | // Format implements the NodeFormatter interface. 17 | func (node *DropOwnedBy) Format(ctx *FmtCtx) { 18 | ctx.WriteString("DROP OWNED BY ") 19 | for i := range node.Roles { 20 | if i > 0 { 21 | ctx.WriteString(", ") 22 | } 23 | node.Roles[i].Format(ctx) 24 | } 25 | if node.DropBehavior != DropDefault { 26 | ctx.WriteString(" ") 27 | ctx.WriteString(node.DropBehavior.String()) 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /pkg/sql/sem/tree/drop_policy.go: -------------------------------------------------------------------------------- 1 | // Copyright 2024 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package tree 7 | 8 | var _ Statement = &DropPolicy{} 9 | 10 | // DropPolicy is a tree struct for the DROP POLICY DDL statement 11 | type DropPolicy struct { 12 | PolicyName Name 13 | TableName *UnresolvedObjectName 14 | DropBehavior DropBehavior 15 | IfExists bool 16 | } 17 | 18 | // Format implements the NodeFormatter interface. 19 | func (node *DropPolicy) Format(ctx *FmtCtx) { 20 | ctx.WriteString("DROP POLICY ") 21 | if node.IfExists { 22 | ctx.WriteString("IF EXISTS ") 23 | } 24 | ctx.FormatNode(&node.PolicyName) 25 | ctx.WriteString(" ON ") 26 | ctx.FormatNode(node.TableName) 27 | if node.DropBehavior != DropDefault { 28 | ctx.WriteString(" ") 29 | ctx.WriteString(node.DropBehavior.String()) 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /pkg/sql/sem/tree/eval_unary_ops.go: -------------------------------------------------------------------------------- 1 | // Copyright 2022 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package tree 7 | 8 | import "context" 9 | 10 | // UnaryNoop is a UnaryEvalOp. 11 | type UnaryNoop struct{} 12 | 13 | // Eval of UnaryNoop does nothing and returns the passed Datum. 14 | func (v *UnaryNoop) Eval(ctx context.Context, evaluator OpEvaluator, d Datum) (Datum, error) { 15 | return d, nil 16 | } 17 | 18 | type ( 19 | // UnaryMinusFloatOp is a UnaryEvalOp. 20 | UnaryMinusFloatOp struct{} 21 | // UnaryMinusIntervalOp is a UnaryEvalOp. 22 | UnaryMinusIntervalOp struct{} 23 | // UnaryMinusIntOp is a UnaryEvalOp. 24 | UnaryMinusIntOp struct{} 25 | // UnaryMinusDecimalOp is a UnaryEvalOp. 26 | UnaryMinusDecimalOp struct{} 27 | ) 28 | type ( 29 | // ComplementIntOp is a UnaryEvalOp. 30 | ComplementIntOp struct{} 31 | // ComplementVarBitOp is a UnaryEvalOp. 32 | ComplementVarBitOp struct{} 33 | // ComplementINetOp is a UnaryEvalOp. 34 | ComplementINetOp struct{} 35 | ) 36 | type ( 37 | // SqrtFloatOp is a UnaryEvalOp. 38 | SqrtFloatOp struct{} 39 | // SqrtDecimalOp is a UnaryEvalOp. 40 | SqrtDecimalOp struct{} 41 | // CbrtFloatOp is a UnaryEvalOp. 42 | CbrtFloatOp struct{} 43 | // CbrtDecimalOp is a UnaryEvalOp. 44 | CbrtDecimalOp struct{} 45 | ) 46 | -------------------------------------------------------------------------------- /pkg/sql/sem/tree/evalgen/string_set.go: -------------------------------------------------------------------------------- 1 | // Copyright 2022 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package main 7 | 8 | import "sort" 9 | 10 | type stringSet map[string]struct{} 11 | 12 | func (ss stringSet) add(s string) { ss[s] = struct{}{} } 13 | 14 | func (ss stringSet) removeAll(other stringSet) { 15 | for s := range other { 16 | delete(ss, s) 17 | } 18 | } 19 | 20 | func (ss stringSet) addAll(other stringSet) { 21 | for s := range other { 22 | ss.add(s) 23 | } 24 | } 25 | 26 | func (ss stringSet) ordered() []string { 27 | list := make([]string, 0, len(ss)) 28 | for s := range ss { 29 | list = append(list, s) 30 | } 31 | sort.Strings(list) 32 | return list 33 | } 34 | 35 | func (ss stringSet) contains(name string) bool { 36 | _, exists := ss[name] 37 | return exists 38 | } 39 | 40 | func (ss stringSet) intersection(other stringSet) stringSet { 41 | intersection := stringSet{} 42 | for s := range ss { 43 | if other.contains(s) { 44 | intersection.add(s) 45 | } 46 | } 47 | return intersection 48 | } 49 | -------------------------------------------------------------------------------- /pkg/sql/sem/tree/export.go: -------------------------------------------------------------------------------- 1 | // Copyright 2018 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package tree 7 | 8 | // Export represents a EXPORT statement. 9 | type Export struct { 10 | Query *Select 11 | FileFormat string 12 | File Expr 13 | Options KVOptions 14 | } 15 | 16 | var _ Statement = &Export{} 17 | 18 | // Format implements the NodeFormatter interface. 19 | func (node *Export) Format(ctx *FmtCtx) { 20 | ctx.WriteString("EXPORT INTO ") 21 | ctx.WriteString(node.FileFormat) 22 | ctx.WriteString(" ") 23 | ctx.FormatURI(node.File) 24 | if node.Options != nil { 25 | ctx.WriteString(" WITH OPTIONS(") 26 | ctx.FormatNode(&node.Options) 27 | ctx.WriteString(")") 28 | } 29 | ctx.WriteString(" FROM ") 30 | ctx.FormatNode(node.Query) 31 | } 32 | -------------------------------------------------------------------------------- /pkg/sql/sem/tree/fuzz.go: -------------------------------------------------------------------------------- 1 | // Copyright 2019 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | //go:build gofuzz 7 | 8 | package tree 9 | 10 | import "github.com/cockroachdb/cockroachdb-parser/pkg/util/timeutil" 11 | 12 | var ( 13 | timeCtx = NewParseContext(timeutil.Now()) 14 | ) 15 | 16 | func FuzzParseDDecimal(data []byte) int { 17 | _, err := ParseDDecimal(string(data)) 18 | if err != nil { 19 | return 0 20 | } 21 | return 1 22 | } 23 | 24 | func FuzzParseDDate(data []byte) int { 25 | _, _, err := ParseDDate(timeCtx, string(data)) 26 | if err != nil { 27 | return 0 28 | } 29 | return 1 30 | } 31 | -------------------------------------------------------------------------------- /pkg/sql/sem/tree/import.go: -------------------------------------------------------------------------------- 1 | // Copyright 2017 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package tree 7 | 8 | // Import represents a IMPORT statement. 9 | type Import struct { 10 | Table *TableName 11 | Into bool 12 | IntoCols NameList 13 | FileFormat string 14 | Files Exprs 15 | Bundle bool 16 | Options KVOptions 17 | } 18 | 19 | var _ Statement = &Import{} 20 | 21 | // Format implements the NodeFormatter interface. 22 | func (node *Import) Format(ctx *FmtCtx) { 23 | ctx.WriteString("IMPORT ") 24 | 25 | if node.Bundle { 26 | if node.Table != nil { 27 | ctx.WriteString("TABLE ") 28 | ctx.FormatNode(node.Table) 29 | ctx.WriteString(" FROM ") 30 | } 31 | ctx.WriteString(node.FileFormat) 32 | ctx.WriteByte(' ') 33 | ctx.FormatURIs(node.Files) 34 | } else { 35 | if node.Into { 36 | ctx.WriteString("INTO ") 37 | ctx.FormatNode(node.Table) 38 | if node.IntoCols != nil { 39 | ctx.WriteByte('(') 40 | ctx.FormatNode(&node.IntoCols) 41 | ctx.WriteString(") ") 42 | } else { 43 | ctx.WriteString(" ") 44 | } 45 | } else { 46 | ctx.WriteString("TABLE ") 47 | ctx.FormatNode(node.Table) 48 | } 49 | ctx.WriteString(node.FileFormat) 50 | ctx.WriteString(" DATA ") 51 | if len(node.Files) == 1 { 52 | ctx.WriteString("(") 53 | } 54 | ctx.FormatURIs(node.Files) 55 | if len(node.Files) == 1 { 56 | ctx.WriteString(")") 57 | } 58 | } 59 | 60 | if node.Options != nil { 61 | ctx.WriteString(" WITH OPTIONS (") 62 | ctx.FormatNode(&node.Options) 63 | ctx.WriteString(")") 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /pkg/sql/sem/tree/persistence.go: -------------------------------------------------------------------------------- 1 | // Copyright 2020 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package tree 7 | 8 | // Persistence defines the persistence strategy for a given table. 9 | type Persistence int 10 | 11 | const ( 12 | // PersistencePermanent indicates a permanent table. 13 | PersistencePermanent Persistence = iota 14 | // PersistenceTemporary indicates a temporary table. 15 | PersistenceTemporary 16 | // PersistenceUnlogged indicates an unlogged table. 17 | // Note this state is not persisted on disk and is used at parse time only. 18 | PersistenceUnlogged 19 | ) 20 | 21 | // IsTemporary returns whether the Persistence value is Temporary. 22 | func (p Persistence) IsTemporary() bool { 23 | return p == PersistenceTemporary 24 | } 25 | 26 | // IsUnlogged returns whether the Persistence value is Unlogged. 27 | func (p Persistence) IsUnlogged() bool { 28 | return p == PersistenceUnlogged 29 | } 30 | -------------------------------------------------------------------------------- /pkg/sql/sem/tree/reassign_owned_by.go: -------------------------------------------------------------------------------- 1 | // Copyright 2020 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package tree 7 | 8 | // ReassignOwnedBy represents a REASSIGN OWNED BY TO statement. 9 | type ReassignOwnedBy struct { 10 | OldRoles RoleSpecList 11 | NewRole RoleSpec 12 | } 13 | 14 | var _ Statement = &ReassignOwnedBy{} 15 | 16 | // Format implements the NodeFormatter interface. 17 | func (node *ReassignOwnedBy) Format(ctx *FmtCtx) { 18 | ctx.WriteString("REASSIGN OWNED BY ") 19 | for i := range node.OldRoles { 20 | if i > 0 { 21 | ctx.WriteString(", ") 22 | } 23 | node.OldRoles[i].Format(ctx) 24 | } 25 | ctx.WriteString(" TO ") 26 | ctx.FormatNode(&node.NewRole) 27 | } 28 | -------------------------------------------------------------------------------- /pkg/sql/sem/tree/schema_feature_name.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package tree 7 | 8 | import "strings" 9 | 10 | // SchemaFeatureName feature name for a given statement, which can be used 11 | // to detect via the feature check functions if the schema change is allowed. 12 | type SchemaFeatureName string 13 | 14 | // GetSchemaFeatureNameFromStmt takes a statement and converts it to a schema 15 | // feature name, which can be enabled or disabled via a feature flag. 16 | func GetSchemaFeatureNameFromStmt(stmt Statement) SchemaFeatureName { 17 | statementTag := stmt.StatementTag() 18 | statementInfo := strings.Split(statementTag, " ") 19 | 20 | switch stmt.(type) { 21 | case *CommentOnDatabase, *CommentOnSchema, *CommentOnTable, 22 | *CommentOnColumn, *CommentOnIndex, *CommentOnConstraint, *DropOwnedBy: 23 | return SchemaFeatureName(statementTag) 24 | } 25 | // Only grab the first two words (i.e. ALTER TABLE, etc..). 26 | if len(statementInfo) >= 2 { 27 | return SchemaFeatureName(statementInfo[0] + " " + statementInfo[1]) 28 | } 29 | return SchemaFeatureName(statementInfo[0]) 30 | } 31 | -------------------------------------------------------------------------------- /pkg/sql/sem/tree/statementreturntype_string.go: -------------------------------------------------------------------------------- 1 | // Copyright 2023 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | // Code generated by "stringer"; DO NOT EDIT. 7 | 8 | package tree 9 | 10 | import "strconv" 11 | 12 | func _() { 13 | // An "invalid array index" compiler error signifies that the constant values have changed. 14 | // Re-run the stringer command to generate them again. 15 | var x [1]struct{} 16 | _ = x[Ack-0] 17 | _ = x[DDL-1] 18 | _ = x[RowsAffected-2] 19 | _ = x[Rows-3] 20 | _ = x[CopyIn-4] 21 | _ = x[CopyOut-5] 22 | _ = x[Replication-6] 23 | _ = x[Unknown-7] 24 | } 25 | 26 | func (i StatementReturnType) String() string { 27 | switch i { 28 | case Ack: 29 | return "Ack" 30 | case DDL: 31 | return "DDL" 32 | case RowsAffected: 33 | return "RowsAffected" 34 | case Rows: 35 | return "Rows" 36 | case CopyIn: 37 | return "CopyIn" 38 | case CopyOut: 39 | return "CopyOut" 40 | case Replication: 41 | return "Replication" 42 | case Unknown: 43 | return "Unknown" 44 | default: 45 | return "StatementReturnType(" + strconv.FormatInt(int64(i), 10) + ")" 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /pkg/sql/sem/tree/statementtype_string.go: -------------------------------------------------------------------------------- 1 | // Copyright 2023 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | // Code generated by "stringer"; DO NOT EDIT. 7 | 8 | package tree 9 | 10 | import "strconv" 11 | 12 | func _() { 13 | // An "invalid array index" compiler error signifies that the constant values have changed. 14 | // Re-run the stringer command to generate them again. 15 | var x [1]struct{} 16 | _ = x[TypeDDL-0] 17 | _ = x[TypeDML-1] 18 | _ = x[TypeDCL-2] 19 | _ = x[TypeTCL-3] 20 | } 21 | 22 | func (i StatementType) String() string { 23 | switch i { 24 | case TypeDDL: 25 | return "TypeDDL" 26 | case TypeDML: 27 | return "TypeDML" 28 | case TypeDCL: 29 | return "TypeDCL" 30 | case TypeTCL: 31 | return "TypeTCL" 32 | default: 33 | return "StatementType(" + strconv.FormatInt(int64(i), 10) + ")" 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /pkg/sql/sem/tree/table_ref.go: -------------------------------------------------------------------------------- 1 | // Copyright 2017 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package tree 7 | 8 | import "github.com/cockroachdb/cockroachdb-parser/pkg/sql/sem/catid" 9 | 10 | // ID is a custom type for {Database,Table}Descriptor IDs. 11 | type ID = catid.ColumnID 12 | 13 | // ColumnID is a custom type for ColumnDescriptor IDs. 14 | type ColumnID = catid.ColumnID 15 | 16 | // TableRef represents a numeric table reference. 17 | // (Syntax !NNN in SQL.) 18 | type TableRef struct { 19 | // TableID is the descriptor ID of the requested table. 20 | TableID int64 21 | 22 | // ColumnIDs is the list of column IDs requested in the table. 23 | // Note that a nil array here means "unspecified" (all columns) 24 | // whereas an array of length 0 means "zero columns". 25 | // Lists of zero columns are not supported and will throw an error. 26 | Columns []ColumnID 27 | 28 | // As determines the names that can be used in the surrounding query 29 | // to refer to this source. 30 | As AliasClause 31 | } 32 | 33 | // Format implements the NodeFormatter interface. 34 | func (n *TableRef) Format(ctx *FmtCtx) { 35 | ctx.Printf("[%d", n.TableID) 36 | if n.Columns != nil { 37 | ctx.WriteByte('(') 38 | for i, c := range n.Columns { 39 | if i > 0 { 40 | ctx.WriteString(", ") 41 | } 42 | ctx.Printf("%d", c) 43 | } 44 | ctx.WriteByte(')') 45 | } 46 | if n.As.Alias != "" { 47 | ctx.WriteString(" AS ") 48 | ctx.FormatNode(&n.As) 49 | } 50 | ctx.WriteByte(']') 51 | } 52 | func (n *TableRef) String() string { return AsString(n) } 53 | 54 | // tableExpr implements the TableExpr interface. 55 | func (n *TableRef) tableExpr() {} 56 | -------------------------------------------------------------------------------- /pkg/sql/sem/tree/tenant.go: -------------------------------------------------------------------------------- 1 | // Copyright 2022 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package tree 7 | 8 | import "strconv" 9 | 10 | // TenantID represents a tenant ID that can be pretty-printed. 11 | type TenantID struct { 12 | ID uint64 13 | 14 | // Specified is set to true when the TENANT clause was specified in 15 | // the backup target input syntax. We need this, instead of relying 16 | // on ID != 0, because we need a special marker for 17 | // the case when the value was anonymized. In other places, the 18 | // value used for anonymized integer literals is 0, but we can't use 19 | // 0 for TenantID as this is refused during parsing, nor can we use 20 | // any other value since all non-zero integers are valid tenant IDs. 21 | Specified bool 22 | } 23 | 24 | var _ NodeFormatter = (*TenantID)(nil) 25 | 26 | // IsSet returns whether the TenantID is set. 27 | func (t *TenantID) IsSet() bool { 28 | return t.Specified && t.ID != 0 29 | } 30 | 31 | // Format implements the NodeFormatter interface. 32 | func (t *TenantID) Format(ctx *FmtCtx) { 33 | if ctx.flags.HasFlags(FmtHideConstants) || !t.IsSet() { 34 | // The second part of the condition above is conceptually 35 | // redundant, but is sadly needed here because we need to be able 36 | // to re-print a TENANT clause after it has been anonymized, 37 | // and we can't emit the value zero which is unparseable. 38 | ctx.WriteByte('_') 39 | } else { 40 | ctx.WriteString(strconv.FormatUint(t.ID, 10)) 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /pkg/sql/sem/tree/tenant_settings.go: -------------------------------------------------------------------------------- 1 | // Copyright 2022 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package tree 7 | 8 | // AlterTenantSetClusterSetting represents an ALTER VIRTUAL CLUSTER 9 | // SET CLUSTER SETTING statement. 10 | type AlterTenantSetClusterSetting struct { 11 | SetClusterSetting 12 | TenantSpec *TenantSpec 13 | } 14 | 15 | // Format implements the NodeFormatter interface. 16 | func (n *AlterTenantSetClusterSetting) Format(ctx *FmtCtx) { 17 | ctx.WriteString("ALTER VIRTUAL CLUSTER ") 18 | ctx.FormatNode(n.TenantSpec) 19 | ctx.WriteByte(' ') 20 | ctx.FormatNode(&n.SetClusterSetting) 21 | } 22 | 23 | // ShowTenantClusterSetting represents a SHOW CLUSTER SETTING ... FOR VIRTUAL CLUSTER statement. 24 | type ShowTenantClusterSetting struct { 25 | *ShowClusterSetting 26 | TenantSpec *TenantSpec 27 | } 28 | 29 | // Format implements the NodeFormatter interface. 30 | func (node *ShowTenantClusterSetting) Format(ctx *FmtCtx) { 31 | ctx.FormatNode(node.ShowClusterSetting) 32 | ctx.WriteString(" FOR VIRTUAL CLUSTER ") 33 | ctx.FormatNode(node.TenantSpec) 34 | } 35 | 36 | // ShowTenantClusterSettingList represents a SHOW CLUSTER SETTINGS FOR VIRTUAL CLUSTER statement. 37 | type ShowTenantClusterSettingList struct { 38 | *ShowClusterSettingList 39 | TenantSpec *TenantSpec 40 | } 41 | 42 | // Format implements the NodeFormatter interface. 43 | func (node *ShowTenantClusterSettingList) Format(ctx *FmtCtx) { 44 | ctx.FormatNode(node.ShowClusterSettingList) 45 | ctx.WriteString(" FOR VIRTUAL CLUSTER ") 46 | ctx.FormatNode(node.TenantSpec) 47 | } 48 | -------------------------------------------------------------------------------- /pkg/sql/sem/tree/time.go: -------------------------------------------------------------------------------- 1 | // Copyright 2019 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package tree 7 | 8 | import ( 9 | "time" 10 | 11 | "github.com/cockroachdb/errors" 12 | ) 13 | 14 | // TimeFamilyPrecisionToRoundDuration takes in a type's precision, and returns the 15 | // duration to use to pass into time.Truncate to truncate to that duration. 16 | // Panics if the precision is not supported. 17 | func TimeFamilyPrecisionToRoundDuration(precision int32) time.Duration { 18 | switch precision { 19 | case 0: 20 | return time.Second 21 | case 1: 22 | return time.Millisecond * 100 23 | case 2: 24 | return time.Millisecond * 10 25 | case 3: 26 | return time.Millisecond 27 | case 4: 28 | return time.Microsecond * 100 29 | case 5: 30 | return time.Microsecond * 10 31 | case 6: 32 | return time.Microsecond 33 | } 34 | panic(errors.Newf("unsupported precision: %d", precision)) 35 | } 36 | -------------------------------------------------------------------------------- /pkg/sql/sem/tree/treebin/doc.go: -------------------------------------------------------------------------------- 1 | // Copyright 2022 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | // Package treebin contains the implementation-agnostic information about all 7 | // binary operators that we support. 8 | package treebin 9 | -------------------------------------------------------------------------------- /pkg/sql/sem/tree/treecmp/doc.go: -------------------------------------------------------------------------------- 1 | // Copyright 2022 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | // Package treecmp contains the implementation-agnostic information about all 7 | // comparison operators that we support. 8 | package treecmp 9 | -------------------------------------------------------------------------------- /pkg/sql/sem/tree/treewindow/doc.go: -------------------------------------------------------------------------------- 1 | // Copyright 2022 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | // Package treewindow contains some constants describing window-function 7 | // specific options. 8 | package treewindow 9 | -------------------------------------------------------------------------------- /pkg/sql/sem/tree/truncate.go: -------------------------------------------------------------------------------- 1 | // Copyright 2012, Google Inc. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in licenses/BSD-vitess.txt. 4 | 5 | // Portions of this file are additionally subject to the following 6 | // license and copyright. 7 | // 8 | // Copyright 2015 The Cockroach Authors. 9 | // 10 | // Use of this software is governed by the CockroachDB Software License 11 | // included in the /LICENSE file. 12 | 13 | // This code was derived from https://github.com/youtube/vitess. 14 | 15 | package tree 16 | 17 | // Truncate represents a TRUNCATE statement. 18 | type Truncate struct { 19 | Tables TableNames 20 | DropBehavior DropBehavior 21 | } 22 | 23 | // Format implements the NodeFormatter interface. 24 | func (node *Truncate) Format(ctx *FmtCtx) { 25 | ctx.WriteString("TRUNCATE TABLE ") 26 | sep := "" 27 | for i := range node.Tables { 28 | ctx.WriteString(sep) 29 | ctx.FormatNode(&node.Tables[i]) 30 | sep = ", " 31 | } 32 | if node.DropBehavior != DropDefault { 33 | ctx.WriteByte(' ') 34 | ctx.WriteString(node.DropBehavior.String()) 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /pkg/sql/sem/tree/union.go: -------------------------------------------------------------------------------- 1 | // Copyright 2012, Google Inc. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in licenses/BSD-vitess.txt. 4 | 5 | // Portions of this file are additionally subject to the following 6 | // license and copyright. 7 | // 8 | // Copyright 2015 The Cockroach Authors. 9 | // 10 | // Use of this software is governed by the CockroachDB Software License 11 | // included in the /LICENSE file. 12 | 13 | // This code was derived from https://github.com/youtube/vitess. 14 | 15 | package tree 16 | 17 | import "fmt" 18 | 19 | // UnionClause represents a UNION statement. 20 | type UnionClause struct { 21 | Type UnionType 22 | Left, Right *Select 23 | All bool 24 | } 25 | 26 | // UnionType represents one of the three set operations in sql. 27 | type UnionType int 28 | 29 | // Union.Type 30 | const ( 31 | UnionOp UnionType = iota 32 | IntersectOp 33 | ExceptOp 34 | ) 35 | 36 | var unionTypeName = [...]string{ 37 | UnionOp: "UNION", 38 | IntersectOp: "INTERSECT", 39 | ExceptOp: "EXCEPT", 40 | } 41 | 42 | func (i UnionType) String() string { 43 | if i < 0 || i > UnionType(len(unionTypeName)-1) { 44 | return fmt.Sprintf("UnionType(%d)", i) 45 | } 46 | return unionTypeName[i] 47 | } 48 | 49 | // Format implements the NodeFormatter interface. 50 | func (node *UnionClause) Format(ctx *FmtCtx) { 51 | ctx.FormatNode(node.Left) 52 | ctx.WriteByte(' ') 53 | ctx.WriteString(node.Type.String()) 54 | if node.All { 55 | ctx.WriteString(" ALL") 56 | } 57 | ctx.WriteByte(' ') 58 | ctx.FormatNode(node.Right) 59 | } 60 | -------------------------------------------------------------------------------- /pkg/sql/sem/tree/unlisten.go: -------------------------------------------------------------------------------- 1 | // Copyright 2022 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package tree 7 | 8 | // Unlisten represents a UNLISTEN statement. 9 | type Unlisten struct { 10 | ChannelName *UnresolvedObjectName 11 | Star bool 12 | } 13 | 14 | var _ Statement = &Unlisten{} 15 | 16 | // Format implements the NodeFormatter interface. 17 | func (node *Unlisten) Format(ctx *FmtCtx) { 18 | ctx.WriteString("UNLISTEN ") 19 | if node.Star { 20 | ctx.WriteString("* ") 21 | } else if node.ChannelName != nil { 22 | ctx.FormatNode(node.ChannelName) 23 | } 24 | } 25 | 26 | // String implements the Statement interface. 27 | func (node *Unlisten) String() string { 28 | return AsString(node) 29 | } 30 | -------------------------------------------------------------------------------- /pkg/sql/sem/tree/unsupported_error.go: -------------------------------------------------------------------------------- 1 | // Copyright 2020 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package tree 7 | 8 | var _ error = &UnsupportedError{} 9 | 10 | // UnsupportedError is an error object which is returned by some unimplemented SQL 11 | // statements. It is currently only used to skip over PGDUMP statements during 12 | // an import. 13 | type UnsupportedError struct { 14 | Err error 15 | FeatureName string 16 | } 17 | 18 | func (u *UnsupportedError) Error() string { 19 | return u.Err.Error() 20 | } 21 | 22 | // Cause implements causer. 23 | func (u *UnsupportedError) Cause() error { return u.Err } 24 | 25 | // Unwrap implements wrapper. 26 | func (u *UnsupportedError) Unwrap() error { return u.Err } 27 | -------------------------------------------------------------------------------- /pkg/sql/sem/tree/with.go: -------------------------------------------------------------------------------- 1 | // Copyright 2017 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package tree 7 | 8 | // With represents a WITH statement. 9 | type With struct { 10 | Recursive bool 11 | CTEList []*CTE 12 | } 13 | 14 | // CTE represents a common table expression inside of a WITH clause. 15 | type CTE struct { 16 | Name AliasClause 17 | Mtr CTEMaterializeClause 18 | Stmt Statement 19 | } 20 | 21 | // CTEMaterializeClause represents either MATERIALIZED, NOT MATERIALIZED, or an 22 | // empty materialization clause. 23 | type CTEMaterializeClause int8 24 | 25 | const ( 26 | // CTEMaterializeDefault represents an empty materialization clause. 27 | CTEMaterializeDefault CTEMaterializeClause = iota 28 | // CTEMaterializeAlways represents MATERIALIZED. 29 | CTEMaterializeAlways 30 | // CTEMaterializeNever represents NOT MATERIALIZED. 31 | CTEMaterializeNever 32 | ) 33 | 34 | // Format implements the NodeFormatter interface. 35 | func (node *With) Format(ctx *FmtCtx) { 36 | if node == nil { 37 | return 38 | } 39 | ctx.WriteString("WITH ") 40 | if node.Recursive { 41 | ctx.WriteString("RECURSIVE ") 42 | } 43 | for i, cte := range node.CTEList { 44 | if i != 0 { 45 | ctx.WriteString(", ") 46 | } 47 | ctx.FormatNode(&cte.Name) 48 | ctx.WriteString(" AS ") 49 | switch cte.Mtr { 50 | case CTEMaterializeAlways: 51 | ctx.WriteString("MATERIALIZED ") 52 | case CTEMaterializeNever: 53 | ctx.WriteString("NOT MATERIALIZED ") 54 | } 55 | ctx.WriteString("(") 56 | ctx.FormatNode(cte.Stmt) 57 | ctx.WriteString(")") 58 | } 59 | ctx.WriteByte(' ') 60 | } 61 | -------------------------------------------------------------------------------- /pkg/sql/sessiondatapb/session_migration.proto: -------------------------------------------------------------------------------- 1 | // Copyright 2021 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | syntax = "proto3"; 7 | package cockroach.sql.sessiondatapb; 8 | option go_package = "github.com/cockroachdb/cockroach/pkg/sql/sessiondatapb"; 9 | 10 | import "gogoproto/gogo.proto"; 11 | import "sql/sessiondatapb/session_data.proto"; 12 | import "sql/sessiondatapb/local_only_session_data.proto"; 13 | 14 | // MigratableSession represents a serialization of a session that can be 15 | // migrated between SQL sessions. 16 | message MigratableSession { 17 | cockroach.sql.sessiondatapb.SessionData session_data = 1 [(gogoproto.nullable)=false]; 18 | cockroach.sql.sessiondatapb.LocalOnlySessionData local_only_session_data = 2 [(gogoproto.nullable)=false]; 19 | 20 | // PreparedStatement represents a prepared statement in a migratable session. 21 | message PreparedStatement { 22 | string name = 1; 23 | repeated uint32 placeholder_type_hints = 2 [ 24 | (gogoproto.customtype)="github.com/lib/pq/oid.Oid" 25 | ]; 26 | string sql = 3 [(gogoproto.customname)="SQL"]; 27 | } 28 | repeated PreparedStatement prepared_statements = 3 [(gogoproto.nullable)=false]; 29 | 30 | } 31 | -------------------------------------------------------------------------------- /pkg/sql/sessiondatapb/session_revival_token.proto: -------------------------------------------------------------------------------- 1 | // Copyright 2022 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | syntax = "proto3"; 7 | package cockroach.sql.sessiondatapb; 8 | option go_package = "github.com/cockroachdb/cockroach/pkg/sql/sessiondatapb"; 9 | 10 | import "google/protobuf/timestamp.proto"; 11 | 12 | // SessionRevivalToken is an opaque piece of data that can be used to 13 | // authenticate a new SQL session for the user who created this token. 14 | message SessionRevivalToken { 15 | // Payload is defined as a separate type so that it’s easier to sign. 16 | message Payload { 17 | // The SQL user who can use this token to authenticate. 18 | string user = 1; 19 | // The algorithm used to sign the payload. Can be either Ed25519 or RSA. 20 | string algorithm = 2; 21 | // The time that this token is no longer considered valid. 22 | google.protobuf.Timestamp expires_at = 3; 23 | // The time that this token was created. 24 | google.protobuf.Timestamp issued_at = 4; 25 | } 26 | 27 | // The payload to sign. This uses raw bytes so the data being signed is 28 | // deterministic and not dependent on the proto version. 29 | bytes payload = 1; 30 | // The signature of the payload, signed using the new signing key. 31 | bytes signature = 2; 32 | } 33 | -------------------------------------------------------------------------------- /pkg/sql/types/alias.go: -------------------------------------------------------------------------------- 1 | // Copyright 2020 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package types 7 | 8 | // PublicSchemaAliases contain a mapping from type name to builtin types 9 | // which are on the public schema on PostgreSQL as they are available 10 | // as an extension. 11 | var PublicSchemaAliases = map[string]*T{ 12 | "box2d": Box2D, 13 | "geometry": Geometry, 14 | "geography": Geography, 15 | } 16 | -------------------------------------------------------------------------------- /pkg/sql/types/testutils.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package types 7 | 8 | // The following variables are useful for testing. 9 | var ( 10 | // OneIntCol is a slice of one IntType. 11 | OneIntCol = []*T{Int} 12 | // TwoIntCols is a slice of two IntTypes. 13 | TwoIntCols = []*T{Int, Int} 14 | // ThreeIntCols is a slice of three IntTypes. 15 | ThreeIntCols = []*T{Int, Int, Int} 16 | // FourIntCols is a slice of four IntTypes. 17 | FourIntCols = []*T{Int, Int, Int, Int} 18 | ) 19 | 20 | // MakeIntCols makes a slice of numCols IntTypes. 21 | func MakeIntCols(numCols int) []*T { 22 | ret := make([]*T, numCols) 23 | for i := 0; i < numCols; i++ { 24 | ret[i] = Int 25 | } 26 | return ret 27 | } 28 | -------------------------------------------------------------------------------- /pkg/sql/types/types_jsonpb.go: -------------------------------------------------------------------------------- 1 | // Copyright 2015 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package types 7 | 8 | import ( 9 | "bytes" 10 | 11 | "github.com/gogo/protobuf/jsonpb" 12 | ) 13 | 14 | // This file contains logic to allow the *types.T to properly marshal to json. 15 | // It is a separate file to make it straightforward to defeat the linter that 16 | // refuses to allow one to call a method Marshal unless it's protoutil.Marshal. 17 | 18 | // MarshalJSONPB marshals the T to json. This is necessary as otherwise 19 | // this field will be lost to the crdb_internal.pb_to_json and the likes. 20 | func (t *T) MarshalJSONPB(marshaler *jsonpb.Marshaler) ([]byte, error) { 21 | temp := *t 22 | if err := temp.downgradeType(); err != nil { 23 | return nil, err 24 | } 25 | var buf bytes.Buffer 26 | if err := marshaler.Marshal(&buf, &temp.InternalType); err != nil { 27 | return nil, err 28 | } 29 | return buf.Bytes(), nil 30 | } 31 | 32 | // UnmarshalJSONPB unmarshals the T to json. This is necessary as otherwise 33 | // this field will be lost to the crdb_internal.json_to_pb and the likes. 34 | func (t *T) UnmarshalJSONPB(unmarshaler *jsonpb.Unmarshaler, data []byte) error { 35 | if err := unmarshaler.Unmarshal(bytes.NewReader(data), &t.InternalType); err != nil { 36 | return err 37 | } 38 | return t.upgradeType() 39 | } 40 | -------------------------------------------------------------------------------- /pkg/util/admission/admissionpb/admission_stats.proto: -------------------------------------------------------------------------------- 1 | // Copyright 2023 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | syntax = "proto3"; 7 | package cockroach.util.admission.admissionpb; 8 | option go_package = "github.com/cockroachdb/cockroach/pkg/util/admission/admissionpb"; 9 | 10 | import "gogoproto/gogo.proto"; 11 | 12 | // AdmissionWorkQueueStats is recorded for work items waiting in the admission 13 | // work queue. 14 | message AdmissionWorkQueueStats { 15 | option (gogoproto.goproto_stringer) = false; 16 | 17 | // WaitDurationNanos is the time spent waiting. 18 | int64 wait_duration_nanos = 1 [(gogoproto.casttype) = "time.Duration"]; 19 | 20 | // QueueKind is the string name of the admission queue kind. 21 | string queue_kind = 2; 22 | 23 | reserved 3; 24 | 25 | // DeadlineExceededCount counts the number of times -- usually zero or one 26 | // unless the stats are an aggregate -- that the deadline was exceeded while 27 | // waiting in the queue. 28 | int32 deadline_exceeded_count = 5; 29 | 30 | reserved 4; 31 | // WorkPriority is the integer representation of work priority, or if the 32 | // AdmissionWorkQueueStats represents an aggregation of stats, the minimum of 33 | // the priorities across the aggregated stats. 34 | int32 work_priority = 6; 35 | 36 | // TODO(dt): consider adding map agg = 7 to 37 | // aggregate events from separate queues separately when rolling up events. 38 | } 39 | -------------------------------------------------------------------------------- /pkg/util/admission/admissionpb/doc.go: -------------------------------------------------------------------------------- 1 | // Copyright 2022 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | // Package admissionpb contains the base types for the admission package. 7 | // This is to prevent excess dependencies to util/log on sessiondatapb. 8 | package admissionpb 9 | -------------------------------------------------------------------------------- /pkg/util/admission/admissionpb/io_threshold.proto: -------------------------------------------------------------------------------- 1 | // Copyright 2022 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | syntax = "proto3"; 7 | package cockroach.util.admission.admissionpb; 8 | option go_package = "github.com/cockroachdb/cockroach/pkg/util/admission/admissionpb"; 9 | 10 | import "gogoproto/gogo.proto"; 11 | 12 | // IOThreshold wraps the raw signals that IO admission control utilizes to determine 13 | // when to introduce queueing. 14 | message IOThreshold { 15 | option (gogoproto.goproto_stringer) = false; 16 | 17 | int64 l0_num_sub_levels = 1; 18 | int64 l0_num_sub_levels_threshold = 2; 19 | int64 l0_num_files = 3; 20 | int64 l0_num_files_threshold = 4; 21 | int64 l0_size = 5; 22 | int64 l0_minimum_size_per_sub_level = 6; 23 | } 24 | -------------------------------------------------------------------------------- /pkg/util/allstacks/allstacks.go: -------------------------------------------------------------------------------- 1 | // Copyright 2023 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package allstacks 7 | 8 | import ( 9 | "runtime" 10 | 11 | "github.com/cockroachdb/cockroachdb-parser/pkg/util/debugutil" 12 | ) 13 | 14 | // Get returns all stacks, except if that takes more than 512mb of memory, in 15 | // which case it returns only 512mb worth of stacks (clipping the last stack 16 | // if necessary). 17 | func Get() debugutil.SafeStack { 18 | return GetWithBuf(nil) 19 | } 20 | 21 | // GetWithBuf is like Get, but tries to use the provided slice first, allocating 22 | // a new, larger, slice only if necessary. 23 | func GetWithBuf(buf []byte) debugutil.SafeStack { 24 | buf = buf[:cap(buf)] 25 | // We don't know how big the traces are, so grow a few times if they don't 26 | // fit. Start large, though. 27 | for n := 1 << 20; /* 1mb */ n <= (1 << 29); /* 512mb */ n *= 2 { 28 | if len(buf) < n { 29 | buf = make([]byte, n) 30 | } 31 | nbytes := runtime.Stack(buf, true /* all */) 32 | if nbytes < len(buf) { 33 | return buf[:nbytes] 34 | } 35 | } 36 | return buf // returning full 512MB slice 37 | } 38 | -------------------------------------------------------------------------------- /pkg/util/buildutil/crdb_test_off.go: -------------------------------------------------------------------------------- 1 | // Copyright 2020 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | //go:build !crdb_test || crdb_test_off 7 | 8 | // Package buildutil provides a constant CrdbTestBuild. 9 | package buildutil 10 | 11 | // CrdbTestBuild is a flag that is set to true if the binary was compiled 12 | // with the 'crdb_test' build tag (which is the case for all test targets). This 13 | // flag can be used to enable expensive checks, test randomizations, or other 14 | // metamorphic-style perturbations that will not affect test results but will 15 | // exercise different parts of the code. 16 | const CrdbTestBuild = false 17 | -------------------------------------------------------------------------------- /pkg/util/buildutil/crdb_test_on.go: -------------------------------------------------------------------------------- 1 | // Copyright 2020 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | //go:build crdb_test && !crdb_test_off 7 | 8 | // Package buildutil provides a constant CrdbTestBuild. 9 | package buildutil 10 | 11 | // CrdbTestBuild is a flag that is set to true if the binary was compiled 12 | // with the 'crdb_test' build tag (which is the case for all test targets). This 13 | // flag can be used to enable expensive checks, test randomizations, or other 14 | // metamorphic-style perturbations that will not affect test results but will 15 | // exercise different parts of the code. 16 | const CrdbTestBuild = true 17 | -------------------------------------------------------------------------------- /pkg/util/collatedstring/collatedstring.go: -------------------------------------------------------------------------------- 1 | // Copyright 2023 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package collatedstring 7 | 8 | import "golang.org/x/text/collate" 9 | 10 | // DefaultCollationTag is the "default" collation for strings. 11 | const DefaultCollationTag = "default" 12 | 13 | // CCollationTag is the "C" collation for strings. At the time of writing 14 | // this, it behaves the same os "default" since LC_COLLATE cannot be modified. 15 | const CCollationTag = "C" 16 | 17 | // PosixCollationTag is the "POSIX" collation for strings. At the time of 18 | // writing this, it behaves the same os "default" since LC_COLLATE cannot be 19 | // modified. 20 | const PosixCollationTag = "POSIX" 21 | 22 | var supportedTagNames []string 23 | 24 | func IsDefaultEquivalentCollation(s string) bool { 25 | return s == DefaultCollationTag || s == CCollationTag || s == PosixCollationTag 26 | } 27 | 28 | // Supported returns a list of all the collation names that are supported. 29 | func Supported() []string { 30 | return supportedTagNames 31 | } 32 | 33 | func init() { 34 | if collate.CLDRVersion != "23" { 35 | panic("This binary was built with an incompatible version of golang.org/x/text. " + 36 | "See https://github.com/cockroachdb/cockroachdb-parser/issues/63738 for details") 37 | } 38 | 39 | supportedTagNames = []string{ 40 | DefaultCollationTag, 41 | CCollationTag, 42 | PosixCollationTag, 43 | } 44 | for _, t := range collate.Supported() { 45 | supportedTagNames = append(supportedTagNames, t.String()) 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /pkg/util/ctxutil/canceler_1_20.go: -------------------------------------------------------------------------------- 1 | // Copyright 2023 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | //go:build go1.20 && !go1.21 7 | 8 | package ctxutil 9 | 10 | import ( 11 | "context" 12 | _ "unsafe" 13 | ) 14 | 15 | // propagateCancel arranges for f to be invoked when parent completes. 16 | func propagateCancel(parent context.Context, f WhenDoneFunc) { 17 | child := whenDone{Context: parent, notify: f} 18 | context_propagateCancel(parent, &child) 19 | } 20 | 21 | // A canceler is a context type that can be canceled directly. The 22 | // implementations are *cancelCtx and *timerCtx. 23 | // This interface definition is applicable to go1.20 or higher 24 | type canceler interface { 25 | cancel(removeFromParent bool, err, cause error) 26 | Done() <-chan struct{} 27 | } 28 | 29 | // whenDone is an adopter that implements canceler interface. 30 | type whenDone struct { 31 | context.Context 32 | notify WhenDoneFunc 33 | } 34 | 35 | func (c *whenDone) cancel(removeFromParent bool, err, cause error) { 36 | if removeFromParent { 37 | context_removeChild(c.Context, c) 38 | } 39 | c.notify() 40 | } 41 | 42 | //go:linkname context_removeChild context.removeChild 43 | func context_removeChild(parent context.Context, child canceler) 44 | 45 | //go:linkname context_propagateCancel context.propagateCancel 46 | func context_propagateCancel(parent context.Context, child canceler) 47 | -------------------------------------------------------------------------------- /pkg/util/ctxutil/canceler_1_21.go: -------------------------------------------------------------------------------- 1 | // Copyright 2023 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | //go:build go1.21 && !bazel 7 | 8 | package ctxutil 9 | 10 | import "context" 11 | 12 | // Linkage definition for go1.21 or higher built outside ./dev toolchain -- 13 | // that is, a toolchain that did not apply cockroach runtime patches. 14 | 15 | // Since this code was built outside ./dev (i.e. "!bazel" tag defined), 16 | // we cannot use patched context implementation. 17 | // Instead, fallback to spinning up goroutine to detect parent cancellation. 18 | func propagateCancel(parent context.Context, notify WhenDoneFunc) { 19 | done := parent.Done() 20 | if done == nil { 21 | panic("unexpected non-cancelable context") 22 | } 23 | go func() { 24 | <-done 25 | notify() 26 | }() 27 | } 28 | -------------------------------------------------------------------------------- /pkg/util/ctxutil/canceler_1_21_bazel.go: -------------------------------------------------------------------------------- 1 | // Copyright 2023 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | //go:build go1.21 && bazel 7 | 8 | package ctxutil 9 | 10 | import ( 11 | "context" 12 | 13 | "github.com/cockroachdb/errors" 14 | ) 15 | 16 | // Linkage definition for go1.21 or higher built with ./dev toolchain -- 17 | // that is, a toolchain that applies cockroach runtime patches. 18 | 19 | // propagateCancel invokes notify when parent context completes. 20 | // Since this code was built with ./dev, use patched context.Context to access 21 | // needed functionality. 22 | func propagateCancel(parent context.Context, notify WhenDoneFunc) { 23 | if !context.PropagateCancel(parent, notify) { 24 | // This shouldn't happen since WhenDone checks to make sure parent is cancellable. 25 | panic(errors.Newf("parent context expected to be cancellable, found %T", parent)) 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /pkg/util/ctxutil/context.go: -------------------------------------------------------------------------------- 1 | // Copyright 2023 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package ctxutil 7 | 8 | // WhenDoneFunc is the callback invoked by context when it becomes done. 9 | type WhenDoneFunc func() 10 | -------------------------------------------------------------------------------- /pkg/util/ctxutil/context_no_linkname.go: -------------------------------------------------------------------------------- 1 | // Copyright 2025 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | // 6 | // This file includes an implementation of WhenDone for builds after Go 1.23 7 | // that do not use our Go fork. 8 | // 9 | //go:build !bazel && gc && go1.23 10 | 11 | package ctxutil 12 | 13 | import "context" 14 | 15 | // WhenDone arranges for the specified function to be invoked when 16 | // parent context becomes done and returns true. 17 | // See context_bazel.go for the full documentation on this function. 18 | // This version does the same but is missing an assertion that requires the 19 | // patched Go runtime to work properly. 20 | func WhenDone(parent context.Context, done WhenDoneFunc) bool { 21 | if parent.Done() == nil { 22 | return false 23 | } 24 | 25 | propagateCancel(parent, done) 26 | return true 27 | } 28 | -------------------------------------------------------------------------------- /pkg/util/ctxutil/doc.go: -------------------------------------------------------------------------------- 1 | // Copyright 2023 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package ctxutil 7 | 8 | /* 9 | 10 | This package provides context utilities that require some "black magic" to 11 | access internals of context.Context implementation. 12 | 13 | In particular, this package provides WhenDone function: 14 | 15 | // WhenDone arranges for the specified function to be invoked when 16 | // parent context becomes done and returns true. 17 | func WhenDone(parent context.Context, done WhenDoneFunc) bool {...} 18 | 19 | 20 | Obviously, one can detect when parent is done and simply invoke the function 21 | when that happens using simple goroutine: 22 | go func() { 23 | select { 24 | <-parent.Done() 25 | invokeCallback() 26 | }() 27 | 28 | However, doing so requires spinning up goroutines. Goroutines are very cheap, 29 | but not 0 cost. Certain performance critical code should avoid adding additional 30 | goroutines if it can be helped (every goroutine adds just a bit of load on go scheduler, 31 | and when the goroutine is created, it has to run; create enough of these goroutines, 32 | and scheduler will experience high latency). 33 | 34 | Hence, the "black magic" in this package. 35 | 36 | Currently, there are condition compilation available for go1.19, go1.20, and go1.21. 37 | Once switch to go1.21 completes, earlier version hacks may be removed. 38 | */ 39 | -------------------------------------------------------------------------------- /pkg/util/duration/duration.proto: -------------------------------------------------------------------------------- 1 | // Copyright 2021 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | syntax = "proto3"; 7 | package cockroach.util.duration; 8 | option go_package = "github.com/cockroachdb/cockroach/pkg/util/duration"; 9 | 10 | // IntervalStyle matches the PostgreSQL IntervalStyle session parameter. 11 | enum IntervalStyle { 12 | POSTGRES = 0; 13 | ISO_8601 = 1; 14 | SQL_STANDARD = 2; 15 | } 16 | -------------------------------------------------------------------------------- /pkg/util/encoding/complement_fast.go: -------------------------------------------------------------------------------- 1 | // Copyright 2015 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | //go:build 386 || amd64 7 | 8 | package encoding 9 | 10 | import "unsafe" 11 | 12 | // The idea for the fast ones complement is borrowed from fastXORBytes 13 | // in the crypto standard library. 14 | const wordSize = int(unsafe.Sizeof(uintptr(0))) 15 | 16 | func onesComplement(b []byte) { 17 | n := len(b) 18 | w := n / wordSize 19 | if w > 0 { 20 | bw := *(*[]uintptr)(unsafe.Pointer(&b)) 21 | for i := 0; i < w; i++ { 22 | bw[i] = ^bw[i] 23 | } 24 | } 25 | 26 | for i := w * wordSize; i < n; i++ { 27 | b[i] = ^b[i] 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /pkg/util/encoding/complement_safe.go: -------------------------------------------------------------------------------- 1 | // Copyright 2015 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | //go:build !386 && !amd64 7 | 8 | package encoding 9 | 10 | func onesComplement(b []byte) { 11 | for i := range b { 12 | b[i] = ^b[i] 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /pkg/util/encoding/encodingtype/encoding_type.go: -------------------------------------------------------------------------------- 1 | // Copyright 2020 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package encodingtype 7 | 8 | // T is declared in the encodingtype package so that it can be 9 | // registered as always safe to report, while avoiding an import 10 | // cycle. 11 | type T int 12 | -------------------------------------------------------------------------------- /pkg/util/errorutil/catch.go: -------------------------------------------------------------------------------- 1 | // Copyright 2019 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package errorutil 7 | 8 | import ( 9 | "runtime" 10 | 11 | "github.com/cockroachdb/errors" 12 | ) 13 | 14 | // ShouldCatch is used for catching errors thrown as panics. Its argument is the 15 | // object returned by recover(); it succeeds if the object is an error. If the 16 | // error is a runtime.Error, it is converted to an internal error (see 17 | // errors.AssertionFailedf). 18 | func ShouldCatch(obj interface{}) (ok bool, err error) { 19 | err, ok = obj.(error) 20 | if ok { 21 | if errors.HasInterface(err, (*runtime.Error)(nil)) { 22 | // Convert runtime errors to internal errors, which display the stack and 23 | // get reported to Sentry. 24 | err = errors.HandleAsAssertionFailure(err) 25 | } 26 | } 27 | return ok, err 28 | } 29 | -------------------------------------------------------------------------------- /pkg/util/errorutil/error.go: -------------------------------------------------------------------------------- 1 | // Copyright 2017 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package errorutil 7 | 8 | import ( 9 | "fmt" 10 | 11 | "github.com/cockroachdb/errors" 12 | ) 13 | 14 | // UnexpectedWithIssueErrorf indicates an error with an associated Github issue. 15 | // It's supposed to be used for conditions that would otherwise be checked by 16 | // assertions, except that they fail and we need the public's help for tracking 17 | // it down. 18 | // The error message will invite users to report repros. 19 | func UnexpectedWithIssueErrorf(issue int, format string, args ...interface{}) error { 20 | err := errors.Newf(format, args...) 21 | err = errors.Wrap(err, "unexpected error") 22 | err = errors.WithSafeDetails(err, "issue #%d", errors.Safe(issue)) 23 | err = errors.WithHint(err, 24 | fmt.Sprintf("We've been trying to track this particular issue down. "+ 25 | "Please report your reproduction at "+ 26 | "https://github.com/cockroachdb/cockroachdb-parser/issues/%d "+ 27 | "unless that issue seems to have been resolved "+ 28 | "(in which case you might want to update crdb to a newer version).", 29 | issue)) 30 | return err 31 | } 32 | -------------------------------------------------------------------------------- /pkg/util/errorutil/sentinel.go: -------------------------------------------------------------------------------- 1 | // Copyright 2023 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package errorutil 7 | 8 | // TempSentinel is a sentinel type that allows other packages to retrieve the 9 | // path to this package with reflect and PkgPath. 10 | type TempSentinel struct{} 11 | -------------------------------------------------------------------------------- /pkg/util/errorutil/tenant.go: -------------------------------------------------------------------------------- 1 | // Copyright 2020 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package errorutil 7 | 8 | import "github.com/cockroachdb/cockroachdb-parser/pkg/util/errorutil/unimplemented" 9 | 10 | // UnsupportedUnderClusterVirtualizationMessage is the message used by UnsupportedUnderClusterVirtualization error. 11 | const UnsupportedUnderClusterVirtualizationMessage = "operation is unsupported within a virtual cluster" 12 | 13 | // UnsupportedUnderClusterVirtualization returns an error suitable for 14 | // returning when an operation could not be carried out due to the SQL 15 | // server running inside a virtual cluster. In that mode, Gossip and 16 | // other components of the KV layer are not available. 17 | func UnsupportedUnderClusterVirtualization(issue int) error { 18 | return unimplemented.NewWithIssue(issue, UnsupportedUnderClusterVirtualizationMessage) 19 | } 20 | 21 | // FeatureNotAvailableToNonSystemTenantsIssue is to be used with the 22 | // Optional and related error interfaces when a feature is simply not 23 | // available to non-system tenants (i.e. we're not planning to change 24 | // this). 25 | // For all other multitenancy errors where there is a plan to 26 | // improve the situation, a specific issue should be created instead. 27 | const FeatureNotAvailableToNonSystemTenantsIssue = 54252 28 | -------------------------------------------------------------------------------- /pkg/util/every_n.go: -------------------------------------------------------------------------------- 1 | // Copyright 2017 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package util 7 | 8 | import ( 9 | "time" 10 | 11 | "github.com/cockroachdb/cockroachdb-parser/pkg/util/syncutil" 12 | ) 13 | 14 | // EveryN provides a way to rate limit spammy events. It tracks how recently a 15 | // given event has occurred so that it can determine whether it's worth 16 | // handling again. 17 | // 18 | // The zero value for EveryN is usable and is equivalent to Every(0), meaning 19 | // that all calls to ShouldProcess will return true. 20 | // 21 | // NOTE: If you specifically care about log messages, you should use the 22 | // version of this in the log package, as it integrates with the verbosity 23 | // flags. 24 | type EveryN struct { 25 | // N is the minimum duration of time between log messages. 26 | N time.Duration 27 | 28 | syncutil.Mutex 29 | lastProcessed time.Time 30 | } 31 | 32 | // Every is a convenience constructor for an EveryN object that allows a log 33 | // message every n duration. 34 | func Every(n time.Duration) EveryN { 35 | return EveryN{N: n} 36 | } 37 | 38 | // ShouldProcess returns whether it's been more than N time since the last event. 39 | func (e *EveryN) ShouldProcess(now time.Time) bool { 40 | var shouldProcess bool 41 | e.Lock() 42 | if now.Sub(e.lastProcessed) >= e.N { 43 | shouldProcess = true 44 | e.lastProcessed = now 45 | } 46 | e.Unlock() 47 | return shouldProcess 48 | } 49 | -------------------------------------------------------------------------------- /pkg/util/grunning/disabled.go: -------------------------------------------------------------------------------- 1 | // Copyright 2022 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | // See grunning.Supported for an explanation behind this build tag. 7 | // 8 | //go:build !bazel 9 | 10 | package grunning 11 | 12 | const supported = false 13 | 14 | func grunningnanos() int64 { return 0 } 15 | -------------------------------------------------------------------------------- /pkg/util/grunning/enabled.go: -------------------------------------------------------------------------------- 1 | // Copyright 2022 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | // See grunning.Supported for an explanation behind this build tag. 7 | // 8 | //go:build bazel 9 | 10 | package grunning 11 | 12 | import "runtime" 13 | 14 | const supported = true 15 | 16 | // grunningnanos returns the running time observed by the current goroutine. 17 | func grunningnanos() int64 { 18 | return runtime.Grunningnanos() 19 | } 20 | -------------------------------------------------------------------------------- /pkg/util/hash.go: -------------------------------------------------------------------------------- 1 | // Copyright 2017 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package util 7 | 8 | import ( 9 | "hash/crc32" 10 | 11 | "github.com/cockroachdb/errors" 12 | ) 13 | 14 | // CRC32 computes the Castagnoli CRC32 of the given data. 15 | func CRC32(data []byte) uint32 { 16 | hash := crc32.New(crc32.MakeTable(crc32.Castagnoli)) 17 | if _, err := hash.Write(data); err != nil { 18 | panic(errors.Wrap(err, `"It never returns an error." -- https://golang.org/pkg/hash`)) 19 | } 20 | return hash.Sum32() 21 | } 22 | 23 | // Magic FNV Base constant as suitable for a FNV-64 hash. 24 | const fnvBase = uint64(14695981039346656037) 25 | const fnvPrime = 1099511628211 26 | 27 | // FNV64 encapsulates the hash state. 28 | type FNV64 struct { 29 | sum uint64 30 | } 31 | 32 | // MakeFNV64 initializes a new FNV64 hash state. 33 | func MakeFNV64() FNV64 { 34 | return FNV64{sum: fnvBase} 35 | } 36 | 37 | // Init initializes FNV64 to starting value. 38 | func (f *FNV64) Init() { 39 | f.sum = fnvBase 40 | } 41 | 42 | // IsInitialized returns true if the hash struct was initialized, which happens 43 | // automatically when created through MakeFNV64 above. 44 | func (f *FNV64) IsInitialized() bool { 45 | return f.sum != 0 46 | } 47 | 48 | // Add modifies the underlying FNV64 state by accumulating the given integer 49 | // hash to the existing state. 50 | func (f *FNV64) Add(c uint64) { 51 | f.sum *= fnvPrime 52 | f.sum ^= c 53 | } 54 | 55 | // Sum returns the hash value accumulated till now. 56 | func (f *FNV64) Sum() uint64 { 57 | return f.sum 58 | } 59 | -------------------------------------------------------------------------------- /pkg/util/hlc/legacy_timestamp.proto: -------------------------------------------------------------------------------- 1 | // Copyright 2017 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | syntax = "proto2"; 7 | package cockroach.util.hlc; 8 | option go_package = "github.com/cockroachdb/cockroach/pkg/util/hlc"; 9 | 10 | import "gogoproto/gogo.proto"; 11 | 12 | // LegacyTimestamp is convertible to hlc.Timestamp, but uses the 13 | // legacy encoding as it is encoded "below raft". 14 | message LegacyTimestamp { 15 | option (gogoproto.equal) = true; 16 | 17 | option (gogoproto.goproto_stringer) = false; 18 | option (gogoproto.populate) = true; 19 | 20 | // Holds a wall time, typically a unix epoch time expressed in 21 | // nanoseconds. 22 | optional int64 wall_time = 1 [(gogoproto.nullable) = false]; 23 | // The logical component captures causality for events whose wall 24 | // times are equal. It is effectively bounded by (maximum clock 25 | // skew)/(minimal ns between events) and nearly impossible to 26 | // overflow. 27 | optional int32 logical = 2 [(gogoproto.nullable) = false]; 28 | reserved 3; 29 | } 30 | -------------------------------------------------------------------------------- /pkg/util/hlc/timestamp.proto: -------------------------------------------------------------------------------- 1 | // Copyright 2014 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | syntax = "proto3"; 7 | package cockroach.util.hlc; 8 | option go_package = "github.com/cockroachdb/cockroach/pkg/util/hlc"; 9 | 10 | import "gogoproto/gogo.proto"; 11 | 12 | // Timestamp represents a state of the hybrid logical clock. 13 | message Timestamp { 14 | option (gogoproto.equal) = true; 15 | 16 | option (gogoproto.goproto_stringer) = false; 17 | option (gogoproto.populate) = true; 18 | 19 | // Holds a wall time, typically a unix epoch time expressed in 20 | // nanoseconds. 21 | int64 wall_time = 1; 22 | // The logical component captures causality for events whose wall times 23 | // are equal. It is effectively bounded by (maximum clock skew)/(minimal 24 | // ns between events) and nearly impossible to overflow. 25 | int32 logical = 2; 26 | reserved 3; 27 | } 28 | -------------------------------------------------------------------------------- /pkg/util/humanizeutil/count.go: -------------------------------------------------------------------------------- 1 | // Copyright 2020 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package humanizeutil 7 | 8 | import ( 9 | "math" 10 | 11 | "github.com/cockroachdb/redact" 12 | "github.com/dustin/go-humanize" 13 | ) 14 | 15 | // Count formats a unitless integer value like a row count. It uses separating 16 | // commas for large values (e.g. "1,000,000"). 17 | func Count(val uint64) redact.SafeString { 18 | if val > math.MaxInt64 { 19 | val = math.MaxInt64 20 | } 21 | return redact.SafeString(humanize.Comma(int64(val))) 22 | } 23 | 24 | func Countf(val float64) redact.SafeString { 25 | if val > math.MaxInt64 { 26 | val = math.MaxInt64 27 | } 28 | return redact.SafeString(humanize.Commaf(val)) 29 | } 30 | -------------------------------------------------------------------------------- /pkg/util/interval/bu23.go: -------------------------------------------------------------------------------- 1 | // Copyright 2019 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | // Copyright ©2014 The bíogo Authors. All rights reserved. 7 | // Use of this source code is governed by a BSD-style 8 | // license that can be found in licenses/BSD3-biogo.store.llrb.txt. 9 | 10 | // This code originated in the github.com/biogo/store/interval package. 11 | 12 | //go:build !td234 13 | 14 | package interval 15 | 16 | // LLRBMode . 17 | const LLRBMode = BU23 18 | -------------------------------------------------------------------------------- /pkg/util/interval/td234.go: -------------------------------------------------------------------------------- 1 | // Copyright 2019 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | // Copyright ©2014 The bíogo Authors. All rights reserved. 7 | // Use of this source code is governed by a BSD-style 8 | // license that can be found in licenses/BSD3-biogo.store.llrb.txt. 9 | 10 | // This code originated in the github.com/biogo/store/interval package. 11 | 12 | //go:build td234 13 | 14 | package interval 15 | 16 | const LLRBMode = TD234 17 | -------------------------------------------------------------------------------- /pkg/util/intsets/fast_large.go: -------------------------------------------------------------------------------- 1 | // Copyright 2020 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | //go:build fast_int_set_large 7 | 8 | package intsets 9 | 10 | var fastIntSetAlwaysSmall = false 11 | -------------------------------------------------------------------------------- /pkg/util/intsets/fast_small.go: -------------------------------------------------------------------------------- 1 | // Copyright 2020 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | //go:build fast_int_set_small 7 | 8 | package intsets 9 | 10 | var fastIntSetAlwaysSmall = true 11 | -------------------------------------------------------------------------------- /pkg/util/intsets/fast_str.go: -------------------------------------------------------------------------------- 1 | // Copyright 2020 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package intsets 7 | 8 | import ( 9 | "bytes" 10 | "fmt" 11 | ) 12 | 13 | // String returns a list representation of elements. Sequential runs of positive 14 | // numbers are shown as ranges. For example, for the set {0, 1, 2, 5, 6, 10}, 15 | // the output is "(0-2,5,6,10)". 16 | func (s Fast) String() string { 17 | var buf bytes.Buffer 18 | buf.WriteByte('(') 19 | appendRange := func(start, end int) { 20 | if buf.Len() > 1 { 21 | buf.WriteByte(',') 22 | } 23 | if start == end { 24 | fmt.Fprintf(&buf, "%d", start) 25 | } else if start+1 == end { 26 | fmt.Fprintf(&buf, "%d,%d", start, end) 27 | } else { 28 | fmt.Fprintf(&buf, "%d-%d", start, end) 29 | } 30 | } 31 | rangeStart, rangeEnd := -1, -1 32 | s.ForEach(func(i int) { 33 | if i < 0 { 34 | appendRange(i, i) 35 | return 36 | } 37 | if rangeStart != -1 && rangeEnd == i-1 { 38 | rangeEnd = i 39 | } else { 40 | if rangeStart != -1 { 41 | appendRange(rangeStart, rangeEnd) 42 | } 43 | rangeStart, rangeEnd = i, i 44 | } 45 | }) 46 | if rangeStart != -1 { 47 | appendRange(rangeStart, rangeEnd) 48 | } 49 | buf.WriteByte(')') 50 | return buf.String() 51 | } 52 | -------------------------------------------------------------------------------- /pkg/util/json/fuzz.go: -------------------------------------------------------------------------------- 1 | // Copyright 2019 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | //go:build gofuzz 7 | 8 | package json 9 | 10 | func FuzzParseJSON(data []byte) int { 11 | _, err := ParseJSON(string(data)) 12 | if err != nil { 13 | return 0 14 | } 15 | return 1 16 | } 17 | -------------------------------------------------------------------------------- /pkg/util/json/iterator.go: -------------------------------------------------------------------------------- 1 | // Copyright 2017 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package json 7 | 8 | // ObjectIterator is an iterator to access the key value pair of an object in 9 | // sorted order based on key. 10 | type ObjectIterator struct { 11 | src jsonObject 12 | idx int 13 | } 14 | 15 | func newObjectIterator(src jsonObject) *ObjectIterator { 16 | return &ObjectIterator{ 17 | src: src, 18 | idx: -1, 19 | } 20 | } 21 | 22 | // Next updates the cursor and returns whether the next pair exists. 23 | func (it *ObjectIterator) Next() bool { 24 | if it.idx >= len(it.src)-1 { 25 | return false 26 | } 27 | it.idx++ 28 | return true 29 | } 30 | 31 | // Key returns key of the current pair. 32 | func (it *ObjectIterator) Key() string { 33 | return string(it.src[it.idx].k) 34 | } 35 | 36 | // Value returns value of the current pair 37 | func (it *ObjectIterator) Value() JSON { 38 | return it.src[it.idx].v 39 | } 40 | -------------------------------------------------------------------------------- /pkg/util/json/tokenizer/README_crdb.md: -------------------------------------------------------------------------------- 1 | # CRDB fork of pkg/json [![GoDoc](https://godoc.org/github.com/pkg/json?status.svg)](https://godoc.org/github.com/pkg/json) 2 | 3 | These files are a fork of `pkg/json` package. 4 | - `scanner.go`, `decoder.go`, and `reader.go` were copied from pkg/json 5 | - Additional utility methods added (e.g. to reset decoder) 6 | - Ability to query reader position, etc. 7 | - Package name renamed from `json` to `tokenizer` 8 | - Removal of unused functions 9 | - Probably, the largest change from the original is in the `scanner.go`. 10 | Changes were made to support un-escaping escaped characters, including 11 | unicode characters. 12 | -------------------------------------------------------------------------------- /pkg/util/jsonpath/jsonpath.go: -------------------------------------------------------------------------------- 1 | // Copyright 2025 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package jsonpath 7 | 8 | import "strings" 9 | 10 | type Jsonpath struct { 11 | Strict bool 12 | Path Path 13 | } 14 | 15 | func (j Jsonpath) String() string { 16 | var sb strings.Builder 17 | if j.Strict { 18 | sb.WriteString("strict ") 19 | } 20 | j.Path.ToString(&sb, false /* inKey */, true /* printBrackets */) 21 | return sb.String() 22 | } 23 | 24 | // Validate walks the Jsonpath AST. It returns an error if the AST is invalid 25 | // (last not in array subscripts, @ in root expressions). 26 | func (j Jsonpath) Validate() error { 27 | return j.Path.Validate(0 /* nestingLevel */, false /* insideArraySubscript */) 28 | } 29 | -------------------------------------------------------------------------------- /pkg/util/jsonpath/method.go: -------------------------------------------------------------------------------- 1 | // Copyright 2025 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package jsonpath 7 | 8 | import ( 9 | "fmt" 10 | "strings" 11 | 12 | "github.com/cockroachdb/errors" 13 | ) 14 | 15 | type MethodType int 16 | 17 | const ( 18 | InvalidMethod MethodType = iota 19 | SizeMethod 20 | TypeMethod 21 | AbsMethod 22 | FloorMethod 23 | CeilingMethod 24 | ) 25 | 26 | var MethodTypeStrings = [...]string{ 27 | SizeMethod: "size", 28 | TypeMethod: "type", 29 | AbsMethod: "abs", 30 | FloorMethod: "floor", 31 | CeilingMethod: "ceiling", 32 | } 33 | 34 | type Method struct { 35 | Type MethodType 36 | } 37 | 38 | var _ Path = Method{} 39 | 40 | func (m Method) ToString(sb *strings.Builder, _, _ bool) { 41 | switch m.Type { 42 | case SizeMethod, TypeMethod, AbsMethod, FloorMethod, CeilingMethod: 43 | sb.WriteString(fmt.Sprintf(".%s()", MethodTypeStrings[m.Type])) 44 | default: 45 | panic(errors.AssertionFailedf("unhandled method type: %d", m.Type)) 46 | } 47 | } 48 | 49 | func (m Method) Validate(nestingLevel int, insideArraySubscript bool) error { 50 | return nil 51 | } 52 | -------------------------------------------------------------------------------- /pkg/util/jsonpath/parser/lexbase/utils.go: -------------------------------------------------------------------------------- 1 | // Copyright 2025 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package lexbase 7 | -------------------------------------------------------------------------------- /pkg/util/jsonpath/scalar.go: -------------------------------------------------------------------------------- 1 | // Copyright 2025 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package jsonpath 7 | 8 | import ( 9 | "fmt" 10 | "strings" 11 | 12 | "github.com/cockroachdb/cockroachdb-parser/pkg/util/json" 13 | "github.com/cockroachdb/errors" 14 | ) 15 | 16 | type ScalarType int 17 | 18 | const ( 19 | ScalarInt ScalarType = iota 20 | ScalarFloat 21 | ScalarString 22 | ScalarBool 23 | ScalarNull 24 | ScalarVariable 25 | ) 26 | 27 | type Scalar struct { 28 | Type ScalarType 29 | Value json.JSON 30 | Variable string 31 | } 32 | 33 | var _ Path = Scalar{} 34 | 35 | func (s Scalar) ToString(sb *strings.Builder, _, _ bool) { 36 | switch s.Type { 37 | case ScalarInt, ScalarFloat, ScalarString, ScalarBool, ScalarNull: 38 | sb.WriteString(s.Value.String()) 39 | return 40 | case ScalarVariable: 41 | sb.WriteString(fmt.Sprintf("$%q", s.Variable)) 42 | return 43 | default: 44 | panic(errors.AssertionFailedf("unhandled scalar type: %d", s.Type)) 45 | } 46 | } 47 | 48 | func (s Scalar) Validate(nestingLevel int, insideArraySubscript bool) error { 49 | return nil 50 | } 51 | -------------------------------------------------------------------------------- /pkg/util/log/eventpb/eventpbgen/extract_log_channels.awk: -------------------------------------------------------------------------------- 1 | # extract_log_channels.awk builds a go file in package main with a 2 | # map[string]struct{} containing entries corresponding to the members of the 3 | # protobuf enum Channel. 4 | 5 | BEGIN { 6 | inside_enum = 0 7 | print ("// Code generated by gen.go. DO NOT EDIT.\n") 8 | print ("package main\n") 9 | print ("var channels = map[string]struct{}{") 10 | } 11 | 12 | $0 ~ /^enum Channel \{/ { 13 | inside_enum = 1 14 | } 15 | 16 | inside_enum && $1 ~ /[A-Z]+/ { 17 | printf "\t\"%s\": {},\n", $1 18 | } 19 | 20 | inside_enum && $0 == "}" { 21 | print("}") 22 | inside_enum = 0 23 | } 24 | 25 | -------------------------------------------------------------------------------- /pkg/util/log/eventpb/eventpbgen/log_channels_generated.go: -------------------------------------------------------------------------------- 1 | // Code generated by gen.go. DO NOT EDIT. 2 | 3 | package main 4 | 5 | var channels = map[string]struct{}{ 6 | "DEV": {}, 7 | "OPS": {}, 8 | "HEALTH": {}, 9 | "STORAGE": {}, 10 | "SESSIONS": {}, 11 | "SQL_SCHEMA": {}, 12 | "USER_ADMIN": {}, 13 | "PRIVILEGES": {}, 14 | "SENSITIVE_ACCESS": {}, 15 | "SQL_EXEC": {}, 16 | "SQL_PERF": {}, 17 | "SQL_INTERNAL_PERF": {}, 18 | "TELEMETRY": {}, 19 | "KV_DISTRIBUTION": {}, 20 | "CHANNEL_MAX": {}, 21 | } 22 | -------------------------------------------------------------------------------- /pkg/util/log/logpb/event.proto: -------------------------------------------------------------------------------- 1 | // Copyright 2022 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | syntax = "proto3"; 7 | package cockroach.util.log; 8 | option go_package = "github.com/cockroachdb/cockroach/pkg/util/log/logpb"; 9 | 10 | import "gogoproto/gogo.proto"; 11 | 12 | 13 | // CommonEventDetails contains the fields common to all structed events. 14 | message CommonEventDetails { 15 | // The timestamp of the event. Expressed as nanoseconds since 16 | // the Unix epoch. 17 | int64 timestamp = 1 [(gogoproto.jsontag) = ",omitempty"]; 18 | // The type of the event. 19 | string event_type = 2 [(gogoproto.jsontag) = ",omitempty", (gogoproto.moretags) = "redact:\"nonsensitive\""]; 20 | } 21 | -------------------------------------------------------------------------------- /pkg/util/log/logpb/json_encode_generated.go: -------------------------------------------------------------------------------- 1 | // Code generated by gen.go. DO NOT EDIT. 2 | 3 | package logpb 4 | 5 | import ( 6 | "strconv" 7 | 8 | "github.com/cockroachdb/cockroachdb-parser/pkg/util/jsonbytes" 9 | "github.com/cockroachdb/redact" 10 | "github.com/gogo/protobuf/jsonpb" 11 | ) 12 | 13 | var _ = jsonpb.Marshaler{} 14 | 15 | // AppendJSONFields implements the EventPayload interface. 16 | func (m *CommonEventDetails) AppendJSONFields(printComma bool, b redact.RedactableBytes) (bool, redact.RedactableBytes) { 17 | 18 | if m.Timestamp != 0 { 19 | if printComma { 20 | b = append(b, ',') 21 | } 22 | printComma = true 23 | b = append(b, "\"Timestamp\":"...) 24 | b = strconv.AppendInt(b, int64(m.Timestamp), 10) 25 | } 26 | 27 | if m.EventType != "" { 28 | if printComma { 29 | b = append(b, ',') 30 | } 31 | printComma = true 32 | b = append(b, "\"EventType\":\""...) 33 | b = redact.RedactableBytes(jsonbytes.EncodeString([]byte(b), string(m.EventType))) 34 | b = append(b, '"') 35 | } 36 | 37 | return printComma, b 38 | } 39 | -------------------------------------------------------------------------------- /pkg/util/log/logpb/test_utils.go: -------------------------------------------------------------------------------- 1 | // Copyright 2022 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package logpb 7 | 8 | import ( 9 | "github.com/cockroachdb/cockroachdb-parser/pkg/util/jsonbytes" 10 | "github.com/cockroachdb/redact" 11 | ) 12 | 13 | // TestingStructuredLogEvent is an implementation of EventPayload for use in 14 | // tests, in order to avoid importing the eventpb package. 15 | type TestingStructuredLogEvent struct { 16 | CommonEventDetails 17 | Channel 18 | Event string 19 | } 20 | 21 | var _ EventPayload = (*TestingStructuredLogEvent)(nil) 22 | 23 | // CommonDetails is part of the EventPayload interface. 24 | func (f TestingStructuredLogEvent) CommonDetails() *CommonEventDetails { 25 | return &f.CommonEventDetails 26 | } 27 | 28 | // LoggingChannel is part of the EventPayload interface. 29 | func (f TestingStructuredLogEvent) LoggingChannel() Channel { 30 | return f.Channel 31 | } 32 | 33 | // AppendJSONFields is part of the EventPayload interface. 34 | func (f TestingStructuredLogEvent) AppendJSONFields( 35 | printComma bool, b redact.RedactableBytes, 36 | ) (bool, redact.RedactableBytes) { 37 | printComma, b = f.CommonEventDetails.AppendJSONFields(printComma, b) 38 | if f.Event != "" { 39 | if printComma { 40 | b = append(b, ',') 41 | } 42 | printComma = true 43 | b = append(b, "\"Event\":\""...) 44 | b = append(b, redact.StartMarker()...) 45 | b = redact.RedactableBytes(jsonbytes.EncodeString([]byte(b), string(redact.EscapeMarkers([]byte(f.Event))))) 46 | b = append(b, redact.EndMarker()...) 47 | b = append(b, '"') 48 | } 49 | return printComma, b 50 | } 51 | -------------------------------------------------------------------------------- /pkg/util/metamorphic/constants_metamorphic_disable.go: -------------------------------------------------------------------------------- 1 | // Copyright 2022 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | //go:build metamorphic_disable 7 | 8 | package metamorphic 9 | 10 | // DisableMetamorphicTesting can be used to disable metamorphic tests. If it 11 | // is set to true then metamorphic testing will not be enabled. 12 | var disableMetamorphicTesting = true 13 | -------------------------------------------------------------------------------- /pkg/util/metamorphic/constants_metamorphic_enable.go: -------------------------------------------------------------------------------- 1 | // Copyright 2022 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | //go:build !metamorphic_disable 7 | 8 | package metamorphic 9 | 10 | import "github.com/cockroachdb/cockroachdb-parser/pkg/util/envutil" 11 | 12 | // disableMetamorphicTesting can be used to disable metamorphic tests. If it 13 | // is set to true then metamorphic testing will not be enabled. 14 | var disableMetamorphicTesting = envutil.EnvOrDefaultBool(DisableMetamorphicEnvVar, false) 15 | -------------------------------------------------------------------------------- /pkg/util/metamorphic/metamorphicutil/is_metamorphic.go: -------------------------------------------------------------------------------- 1 | // Copyright 2024 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package metamorphicutil 7 | 8 | // NB: init() in pkg/util/metamorphic/constants.go may set this value to true. 9 | // We don't put this variable in that package as we would like to have a way to 10 | // reliably determine which packages use metamorphic constants, and 11 | // `bazel query somepath(_, //pkg/util/metamorphic)` should be a good way to do 12 | // that. However, some packages (like pkg/testutils/skip) need to check whether 13 | // we're running a metamorphic build, without necessarily depending on the 14 | // metamorphic package. 15 | // 16 | // Generally, you should use metamorphic.IsMetaMorphicBuild() instead of checking 17 | // this value. 18 | var IsMetamorphicBuild bool 19 | -------------------------------------------------------------------------------- /pkg/util/nocopy.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package util 7 | 8 | // NoCopy may be embedded into structs which must not be copied 9 | // after the first use. 10 | // 11 | // See https://github.com/golang/go/issues/8005#issuecomment-190753527 12 | // for details. 13 | type NoCopy struct{} 14 | 15 | // Silence unused warnings. 16 | var _ = NoCopy{} 17 | 18 | // Lock is a no-op used by -copylocks checker from `go vet`. 19 | func (*NoCopy) Lock() {} 20 | 21 | // Unlock is a no-op used by -copylocks checker from `go vet`. 22 | func (*NoCopy) Unlock() {} 23 | -------------------------------------------------------------------------------- /pkg/util/num32/doc.go: -------------------------------------------------------------------------------- 1 | // Copyright 2024 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | /* 7 | Package num32 contains basic numeric functions that operate on scalar, vector, 8 | and matrix float32 values. Inputs and outputs deliberately use simple float 9 | types so that they can be used in multiple contexts. It uses the gonum library 10 | when possible, since it offers assembly language implementations of various 11 | useful primitives. 12 | 13 | Using the same convention as gonum, when a slice is being modified in place, it 14 | has the name dst and the function does not return a value. In addition, many of 15 | the functions have the same name and semantics as those in gonum. 16 | 17 | Where possible, functions in this package are written with the assumption that 18 | the caller prevents bad input. They will panic with assertion errors if this is 19 | not the case, rather than returning error values. Callers should generally have 20 | panic recovery logic further up the stack to gracefully handle these assertions, 21 | as they indicate buggy code. 22 | */ 23 | package num32 24 | -------------------------------------------------------------------------------- /pkg/util/num32/scalar.go: -------------------------------------------------------------------------------- 1 | // Copyright 2024 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package num32 7 | 8 | import "math" 9 | 10 | // Abs returns the absolute value of x. 11 | // 12 | // Special cases are: 13 | // 14 | // Abs(±Inf) = +Inf 15 | // Abs(NaN) = NaN 16 | func Abs(x float32) float32 { 17 | return math.Float32frombits(math.Float32bits(x) &^ (1 << 31)) 18 | } 19 | 20 | // Sqrt returns the square root of x. 21 | // 22 | // Special cases are: 23 | // 24 | // Sqrt(+Inf) = +Inf 25 | // Sqrt(±0) = ±0 26 | // Sqrt(x < 0) = NaN 27 | // Sqrt(NaN) = NaN 28 | func Sqrt(x float32) float32 { 29 | // For now, use the float64 Sqrt operation. 30 | // TODO(andyk): Consider using the SQRTSS instruction like gonum does 31 | // internally. 32 | return float32(math.Sqrt(float64(x))) 33 | } 34 | 35 | // IsNaN reports whether f is an IEEE 754 “not-a-number” value. 36 | func IsNaN(v float32) bool { 37 | // IEEE 754 says that only NaNs satisfy f != f. 38 | return v != v 39 | } 40 | -------------------------------------------------------------------------------- /pkg/util/pluralize.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package util 7 | 8 | import "github.com/cockroachdb/redact" 9 | 10 | // Pluralize returns a single character 's' unless n == 1. 11 | func Pluralize(n int64) redact.SafeString { 12 | if n == 1 { 13 | return "" 14 | } 15 | return "s" 16 | } 17 | -------------------------------------------------------------------------------- /pkg/util/protoutil/clone.proto: -------------------------------------------------------------------------------- 1 | // Copyright 2017 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | syntax = "proto3"; 7 | package cockroach.util.protoutil; 8 | option go_package = "github.com/cockroachdb/cockroach/pkg/util/protoutil"; 9 | 10 | import "gogoproto/gogo.proto"; 11 | 12 | message RecursiveAndUncloneable { 13 | RecursiveAndUncloneable r = 1; 14 | bytes uuid = 2 [(gogoproto.nullable) = false, 15 | (gogoproto.customtype) = "github.com/cockroachdb/cockroach/pkg/util/uuid.UUID"]; 16 | } 17 | -------------------------------------------------------------------------------- /pkg/util/race_off.go: -------------------------------------------------------------------------------- 1 | // Copyright 2017 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | //go:build !race 7 | 8 | package util 9 | 10 | // RaceEnabled is true if CockroachDB was built with the race build tag. 11 | const RaceEnabled = false 12 | 13 | // EnableRacePreemptionPoints enables goroutine preemption points declared with 14 | // RacePreempt for builds using the race build tag. 15 | func EnableRacePreemptionPoints() func() { return func() {} } 16 | 17 | // RacePreempt adds a goroutine preemption point if CockroachDB was built with 18 | // the race build tag and preemption points have been enabled. The function is a 19 | // no-op (and should be optimized out through dead code elimination) if the race 20 | // build tag was not used. 21 | func RacePreempt() {} 22 | -------------------------------------------------------------------------------- /pkg/util/race_on.go: -------------------------------------------------------------------------------- 1 | // Copyright 2017 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | //go:build race 7 | 8 | package util 9 | 10 | import "runtime" 11 | 12 | // RaceEnabled is true if CockroachDB was built with the race build tag. 13 | const RaceEnabled = true 14 | 15 | // racePreemptionPoints is set in EnableRacePreemptionPoints. 16 | var racePreemptionPoints = false 17 | 18 | // EnableRacePreemptionPoints enables goroutine preemption points declared with 19 | // RacePreempt for builds using the race build tag. 20 | func EnableRacePreemptionPoints() func() { 21 | racePreemptionPoints = true 22 | return func() { 23 | racePreemptionPoints = false 24 | } 25 | } 26 | 27 | // RacePreempt adds a goroutine preemption point if CockroachDB was built with 28 | // the race build tag and preemption points have been enabled. The function is a 29 | // no-op (and should be optimized out through dead code elimination) if the race 30 | // build tag was not used. 31 | func RacePreempt() { 32 | if racePreemptionPoints { 33 | runtime.Gosched() 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /pkg/util/reflect.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package util 7 | 8 | import "reflect" 9 | 10 | // EqualPtrFields uses reflection to check two "mirror" structures for matching pointer fields that 11 | // point to the same object. Used to verify cloning/deep copy functions. 12 | // 13 | // Returns the names of equal pointer fields. 14 | func EqualPtrFields(src, dst reflect.Value, prefix string) []string { 15 | t := dst.Type() 16 | if t.Kind() != reflect.Struct { 17 | return nil 18 | } 19 | if srcType := src.Type(); srcType != t { 20 | return nil 21 | } 22 | var res []string 23 | for i := 0; i < t.NumField(); i++ { 24 | srcF, dstF := src.Field(i), dst.Field(i) 25 | switch f := t.Field(i); f.Type.Kind() { 26 | case reflect.Ptr: 27 | if srcF.Interface() == dstF.Interface() { 28 | res = append(res, prefix+f.Name) 29 | } 30 | case reflect.Slice: 31 | if srcF.Pointer() == dstF.Pointer() { 32 | res = append(res, prefix+f.Name) 33 | } 34 | l := dstF.Len() 35 | if srcLen := srcF.Len(); srcLen < l { 36 | l = srcLen 37 | } 38 | for i := 0; i < l; i++ { 39 | res = append(res, EqualPtrFields(srcF.Index(i), dstF.Index(i), f.Name+".")...) 40 | } 41 | case reflect.Struct: 42 | res = append(res, EqualPtrFields(srcF, dstF, f.Name+".")...) 43 | } 44 | } 45 | return res 46 | } 47 | -------------------------------------------------------------------------------- /pkg/util/smalltrace.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package util 7 | 8 | import ( 9 | "runtime" 10 | "strings" 11 | 12 | "github.com/cockroachdb/redact" 13 | ) 14 | 15 | var prefix = func() string { 16 | result := "github.com/cockroachdb/cockroachdb-parser/pkg/" 17 | if runtime.Compiler == "gccgo" { 18 | result = strings.Replace(result, ".", "_", -1) 19 | result = strings.Replace(result, "/", "_", -1) 20 | } 21 | return result 22 | }() 23 | 24 | // GetSmallTrace returns a comma-separated string containing the top 25 | // 5 callers from a given skip level. 26 | func GetSmallTrace(skip int) redact.RedactableString { 27 | var pcs [5]uintptr 28 | runtime.Callers(skip, pcs[:]) 29 | frames := runtime.CallersFrames(pcs[:]) 30 | var callers redact.StringBuilder 31 | 32 | var callerPrefix redact.RedactableString 33 | for { 34 | f, more := frames.Next() 35 | function := strings.TrimPrefix(f.Function, prefix) 36 | file := f.File 37 | if index := strings.LastIndexByte(file, '/'); index >= 0 { 38 | file = file[index+1:] 39 | } 40 | callers.Printf("%s%s:%d:%s", callerPrefix, redact.SafeString(file), f.Line, redact.SafeString(function)) 41 | callerPrefix = "," 42 | if !more { 43 | break 44 | } 45 | } 46 | 47 | return callers.RedactableString() 48 | } 49 | -------------------------------------------------------------------------------- /pkg/util/strutil/util.go: -------------------------------------------------------------------------------- 1 | // Copyright 2022 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package strutil 7 | 8 | import ( 9 | "fmt" 10 | "strconv" 11 | "strings" 12 | ) 13 | 14 | // AppendInt appends the decimal form of x to b and returns the result. 15 | // If the decimal form is shorter than width, the result is padded with leading 0's. 16 | // If the decimal is longer than width, returns formatted decimal without 17 | // any truncation. 18 | func AppendInt(b []byte, x int, width int) []byte { 19 | if x < 0 { 20 | width-- 21 | x = -x 22 | b = append(b, '-') 23 | } 24 | 25 | var scratch [16]byte 26 | xb := strconv.AppendInt(scratch[:0], int64(x), 10) 27 | 28 | // Add 0-padding. 29 | for w := len(xb); w < width; w++ { 30 | b = append(b, '0') 31 | } 32 | return append(b, xb...) 33 | } 34 | 35 | // JoinIDs joins a slice of any ids into a comma separated string. Each ID could 36 | // be prefixed with a string (e.g. n1, n2, n3 to represent nodes). 37 | func JoinIDs[T ~int | ~int32 | ~int64](prefix string, ids []T) string { 38 | idNames := make([]string, 0, len(ids)) 39 | for _, id := range ids { 40 | idNames = append(idNames, fmt.Sprintf("%s%d", prefix, id)) 41 | } 42 | return strings.Join(idNames, ", ") 43 | } 44 | -------------------------------------------------------------------------------- /pkg/util/syncutil/mutex_deadlock.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | //go:build deadlock 7 | 8 | package syncutil 9 | 10 | import ( 11 | "time" 12 | 13 | deadlock "github.com/sasha-s/go-deadlock" 14 | ) 15 | 16 | // DeadlockEnabled is true if the deadlock detector is enabled. 17 | const DeadlockEnabled = true 18 | 19 | func init() { 20 | deadlock.Opts.DeadlockTimeout = 5 * time.Minute 21 | } 22 | 23 | // A Mutex is a mutual exclusion lock. 24 | type Mutex struct { 25 | deadlock.Mutex 26 | } 27 | 28 | // AssertHeld is a no-op for deadlock mutexes. 29 | func (m *Mutex) AssertHeld() { 30 | } 31 | 32 | // TryLock is a no-op for deadlock mutexes. 33 | func (rw *Mutex) TryLock() bool { 34 | return false 35 | } 36 | 37 | // An RWMutex is a reader/writer mutual exclusion lock. 38 | type RWMutex struct { 39 | deadlock.RWMutex 40 | } 41 | 42 | // AssertHeld is a no-op for deadlock mutexes. 43 | func (rw *RWMutex) AssertHeld() { 44 | } 45 | 46 | // AssertRHeld is a no-op for deadlock mutexes. 47 | func (rw *RWMutex) AssertRHeld() { 48 | } 49 | 50 | // TryLock is a no-op for deadlock mutexes. 51 | func (rw *RWMutex) TryLock() bool { 52 | return false 53 | } 54 | 55 | // TryRLock is a no-op for deadlock mutexes. 56 | func (rw *RWMutex) TryRLock() bool { 57 | return false 58 | } 59 | -------------------------------------------------------------------------------- /pkg/util/system/cache_line.go: -------------------------------------------------------------------------------- 1 | // Copyright 2024 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package system 7 | 8 | import ( 9 | "unsafe" 10 | 11 | "golang.org/x/sys/cpu" 12 | ) 13 | 14 | // CacheLineSize is the size of a CPU cache line in bytes, or a non-zero 15 | // estimate if the size is unknown. 16 | const CacheLineSize = max( 17 | int(unsafe.Sizeof(cpu.CacheLinePad{})), 18 | // The size of the CacheLinePad struct is 0 on some software-abstracted 19 | // platforms, like Wasm, where the cache line size of the underlying CPU is 20 | // not known. In such cases, use a reasonable default value of 64 bytes. 21 | // 22 | // NOTE: we use max instead of an init-time comparison so that CacheLineSize 23 | // can be used in const expressions. The subtraction here drives this term 24 | // below 0 to be ignored by max in all cases except when the size of the 25 | // CacheLinePad struct is 0. 26 | 64-65*int(unsafe.Sizeof(cpu.CacheLinePad{})), 27 | ) 28 | -------------------------------------------------------------------------------- /pkg/util/system/num_cpu.go: -------------------------------------------------------------------------------- 1 | // Copyright 2020 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package system 7 | 8 | import "runtime" 9 | 10 | // NumCPU returns the number of logical CPUs usable by the current process. 11 | // 12 | // !!Note!! If you are considering using this to scale parallelism with the 13 | // machine size, use runtime.GOMAXPROCS(0) instead. The latter is better because 14 | // GOMAXPROCS is reduced with certain test runs (with the race detector); it can 15 | // also be reduced in containerized environments. 16 | // 17 | // The set of available CPUs is checked by querying the operating system 18 | // at process startup. Changes to operating system CPU allocation after 19 | // process startup are not reflected. 20 | func NumCPU() int { 21 | return runtime.NumCPU() 22 | } 23 | -------------------------------------------------------------------------------- /pkg/util/testaddr_default.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | //go:build !linux 7 | 8 | package util 9 | 10 | func init() { 11 | IsolatedTestAddr = TestAddr 12 | } 13 | -------------------------------------------------------------------------------- /pkg/util/testaddr_random.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | //go:build linux 7 | 8 | package util 9 | 10 | import ( 11 | "fmt" 12 | 13 | "github.com/cockroachdb/cockroachdb-parser/pkg/util/randutil" 14 | ) 15 | 16 | func init() { 17 | r, _ := randutil.NewTestRand() 18 | // 127.255.255.255 is special (broadcast), so choose values less 19 | // than 255. 20 | a := r.Intn(255) 21 | b := r.Intn(255) 22 | c := r.Intn(255) 23 | IsolatedTestAddr = NewUnresolvedAddr("tcp", fmt.Sprintf("127.%d.%d.%d:0", a, b, c)) 24 | } 25 | -------------------------------------------------------------------------------- /pkg/util/timeutil/pgdate/field_string.go: -------------------------------------------------------------------------------- 1 | // Copyright 2023 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | // Code generated by "stringer"; DO NOT EDIT. 7 | 8 | package pgdate 9 | 10 | import "strconv" 11 | 12 | func _() { 13 | // An "invalid array index" compiler error signifies that the constant values have changed. 14 | // Re-run the stringer command to generate them again. 15 | var x [1]struct{} 16 | _ = x[fieldYear-0] 17 | _ = x[fieldMonth-1] 18 | _ = x[fieldDay-2] 19 | _ = x[fieldEra-3] 20 | _ = x[fieldHour-4] 21 | _ = x[fieldMinute-5] 22 | _ = x[fieldSecond-6] 23 | _ = x[fieldNanos-7] 24 | _ = x[fieldMeridian-8] 25 | _ = x[fieldTZHour-9] 26 | _ = x[fieldTZMinute-10] 27 | _ = x[fieldTZSecond-11] 28 | _ = x[fieldMinimum-0] 29 | _ = x[fieldMaximum-11] 30 | } 31 | 32 | func (i field) String() string { 33 | switch i { 34 | case fieldYear: 35 | return "fieldYear" 36 | case fieldMonth: 37 | return "fieldMonth" 38 | case fieldDay: 39 | return "fieldDay" 40 | case fieldEra: 41 | return "fieldEra" 42 | case fieldHour: 43 | return "fieldHour" 44 | case fieldMinute: 45 | return "fieldMinute" 46 | case fieldSecond: 47 | return "fieldSecond" 48 | case fieldNanos: 49 | return "fieldNanos" 50 | case fieldMeridian: 51 | return "fieldMeridian" 52 | case fieldTZHour: 53 | return "fieldTZHour" 54 | case fieldTZMinute: 55 | return "fieldTZMinute" 56 | case fieldTZSecond: 57 | return "fieldTZSecond" 58 | default: 59 | return "field(" + strconv.FormatInt(int64(i), 10) + ")" 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /pkg/util/timeutil/pgdate/pgdate.proto: -------------------------------------------------------------------------------- 1 | // Copyright 2021 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | syntax = "proto3"; 7 | package cockroach.util.timeutil.pgdate; 8 | option go_package = "github.com/cockroachdb/cockroach/pkg/util/timeutil/pgdate"; 9 | 10 | // DateStyle refers to the PostgreSQL DateStyle allowed variables. 11 | message DateStyle { 12 | // Style refers to the style to print output dates. 13 | Style style = 1; 14 | // Order refers to the order of day, month and year components. 15 | Order order = 2; 16 | } 17 | 18 | // Order refers to the Order component of a DateStyle. 19 | enum Order { 20 | MDY = 0; 21 | DMY = 1; 22 | YMD = 2; 23 | } 24 | 25 | // Style refers to the Style component of a DateStyle. 26 | enum Style { 27 | ISO = 0; 28 | SQL = 1; 29 | POSTGRES = 2; 30 | GERMAN = 3; 31 | } 32 | -------------------------------------------------------------------------------- /pkg/util/timeutil/timeout.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package timeutil 7 | 8 | import ( 9 | "context" 10 | "time" 11 | 12 | "github.com/cockroachdb/errors" 13 | "github.com/cockroachdb/redact" 14 | ) 15 | 16 | // RunWithTimeout runs a function with a timeout, the same way you'd do with 17 | // context.WithTimeout. It improves the opaque error messages returned by 18 | // WithTimeout by augmenting them with the op string that is passed in. 19 | func RunWithTimeout( 20 | ctx context.Context, 21 | op redact.RedactableString, 22 | timeout time.Duration, 23 | fn func(ctx context.Context) error, 24 | ) error { 25 | ctx, cancel := context.WithTimeout(ctx, timeout) // nolint:context 26 | defer cancel() 27 | start := Now() 28 | err := fn(ctx) 29 | if err != nil && errors.Is(ctx.Err(), context.DeadlineExceeded) { 30 | err = &TimeoutError{ 31 | operation: op, 32 | timeout: timeout, 33 | took: Since(start), 34 | cause: err, 35 | } 36 | } 37 | return err 38 | } 39 | -------------------------------------------------------------------------------- /pkg/util/timeutil/timeutil.go: -------------------------------------------------------------------------------- 1 | // Copyright 2020 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package timeutil 7 | 8 | // FullTimeFormat is the time format used to display any unknown timestamp 9 | // type, and always shows the full time zone offset. 10 | const FullTimeFormat = "2006-01-02 15:04:05.999999-07:00:00" 11 | 12 | // TimestampWithTZFormat is the time format used to display 13 | // timestamps with a time zone offset. The minutes and seconds 14 | // offsets are only added if they are non-zero. 15 | const TimestampWithTZFormat = "2006-01-02 15:04:05.999999-07" 16 | 17 | // TimestampWithoutTZFormat is the time format used to display 18 | // timestamps without a time zone offset. The minutes and seconds 19 | // offsets are only added if they are non-zero. 20 | const TimestampWithoutTZFormat = "2006-01-02 15:04:05.999999" 21 | 22 | // TimeWithTZFormat is the time format used to display a time 23 | // with a time zone offset. 24 | const TimeWithTZFormat = "15:04:05.999999-07" 25 | 26 | // TimeWithoutTZFormat is the time format used to display a time 27 | // without a time zone offset. 28 | const TimeWithoutTZFormat = "15:04:05.999999" 29 | 30 | // DateFormat is the time format used to display a date. 31 | const DateFormat = "2006-01-02" 32 | -------------------------------------------------------------------------------- /pkg/util/tracing/span_finalizer_race_off.go: -------------------------------------------------------------------------------- 1 | // Copyright 2022 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | //go:build !race 7 | 8 | package tracing 9 | 10 | import "runtime" 11 | 12 | // setFinalizer registers a finalizer to run when the Span is garbage collected. 13 | // Passing nil clears any previously registered finalizer. 14 | func (sp *Span) setFinalizer(fn func(sp *Span)) { 15 | if fn == nil { 16 | // Avoid typed nil. 17 | runtime.SetFinalizer(sp, nil) 18 | return 19 | } 20 | runtime.SetFinalizer(sp, fn) 21 | } 22 | -------------------------------------------------------------------------------- /pkg/util/tracing/span_finalizer_race_on.go: -------------------------------------------------------------------------------- 1 | // Copyright 2022 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | //go:build race 7 | 8 | package tracing 9 | 10 | // finalizers are expensive under race, so this is a no-op. 11 | func (sp *Span) setFinalizer(fn func(sp *Span)) {} 12 | -------------------------------------------------------------------------------- /pkg/util/tracing/tracingpb/tracing.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package tracingpb 7 | 8 | // Empty returns true if t does not have any tracing info in it. 9 | func (t *TraceInfo) Empty() bool { 10 | return t == nil || t.TraceID == 0 11 | } 12 | -------------------------------------------------------------------------------- /pkg/util/tracing/tracingpb/tracing.proto: -------------------------------------------------------------------------------- 1 | // Copyright 2021 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | syntax = "proto3"; 7 | package cockroach.util.tracing.tracingpb; 8 | option go_package = "github.com/cockroachdb/cockroach/pkg/util/tracing/tracingpb"; 9 | 10 | import "gogoproto/gogo.proto"; 11 | 12 | 13 | enum RecordingMode { 14 | OFF = 0; 15 | VERBOSE = 1; 16 | STRUCTURED = 2; 17 | } 18 | 19 | // TraceInfo represents the tracing context of an operation. It is the proto 20 | // representation of tracing.SpanMeta. TraceInfos are passed around in RPC 21 | // payloads so that the server can create spans that are linked to the remote 22 | // parent. 23 | message TraceInfo { 24 | uint64 trace_id = 1 [(gogoproto.nullable) = false, (gogoproto.customname) = "TraceID", (gogoproto.customtype) = "TraceID"]; 25 | // ID of the parent span. 26 | uint64 parent_span_id = 2 [(gogoproto.nullable) = false, (gogoproto.customname) = "ParentSpanID", (gogoproto.customtype) = "SpanID"]; 27 | 28 | // The type of recording, if any, that the parent span is doing. See 29 | // tracing.RecordingType. 30 | RecordingMode recording_mode = 3; 31 | 32 | // OtelInfo contains the OpenTelemetry tracing context, if any. 33 | message OtelInfo { 34 | // trace_id will have exactly 16 bytes. 35 | bytes trace_id = 1 [(gogoproto.customname) = "TraceID"]; 36 | // span_id will have exactly 8 bytes. 37 | bytes span_id = 2 [(gogoproto.customname) = "SpanID"]; 38 | } 39 | 40 | OtelInfo otel = 4; 41 | } 42 | 43 | -------------------------------------------------------------------------------- /pkg/util/tsearch/config.go: -------------------------------------------------------------------------------- 1 | // Copyright 2022 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package tsearch 7 | 8 | import "strings" 9 | 10 | // ValidConfig returns an error if the input string is not a supported and valid 11 | // text search config. 12 | func ValidConfig(input string) error { 13 | input = GetConfigKey(input) 14 | _, err := getStemmer(input) 15 | return err 16 | } 17 | 18 | // GetConfigKey returns a config that can be used as a key to look up stemmers 19 | // and stopwords from an input config value. This is simulating the more 20 | // advanced customizable dictionaries and configs that Postgres has, which 21 | // allows user-defined text search configurations: because of this, configs can 22 | // have schema prefixes. Because we don't (yet?) allow this, we just have to 23 | // trim off any `pg_catalog.` prefix if it exists. 24 | func GetConfigKey(config string) string { 25 | return strings.TrimPrefix(config, "pg_catalog.") 26 | } 27 | -------------------------------------------------------------------------------- /pkg/util/tsearch/stopwords.go: -------------------------------------------------------------------------------- 1 | // Copyright 2022 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | package tsearch 7 | 8 | import ( 9 | "bytes" 10 | "embed" 11 | "path" 12 | "strings" 13 | ) 14 | 15 | //go:embed stopwords/* 16 | var stopwordFS embed.FS 17 | 18 | var stopwordsMap map[string]map[string]struct{} 19 | 20 | func init() { 21 | stopwordsMap = make(map[string]map[string]struct{}) 22 | dir, err := stopwordFS.ReadDir("stopwords") 23 | if err != nil { 24 | panic("error loading stopwords: " + err.Error()) 25 | } 26 | for _, f := range dir { 27 | filename := f.Name() 28 | name := strings.TrimSuffix(filename, ".stop") 29 | // N.B. we use path.Join here instead of filepath.Join because go:embed 30 | // always uses forward slashes. https://github.com/golang/go/issues/45230 31 | contents, err := stopwordFS.ReadFile(path.Join("stopwords", filename)) 32 | if err != nil { 33 | panic("error loading stopwords: " + err.Error()) 34 | } 35 | wordList := bytes.Fields(contents) 36 | stopwordsMap[name] = make(map[string]struct{}, len(wordList)) 37 | for _, word := range wordList { 38 | stopwordsMap[name][string(word)] = struct{}{} 39 | } 40 | } 41 | // The simple text search config has no stopwords. 42 | stopwordsMap["simple"] = nil 43 | } 44 | -------------------------------------------------------------------------------- /pkg/util/tsearch/stopwords/danish.stop: -------------------------------------------------------------------------------- 1 | og 2 | i 3 | jeg 4 | det 5 | at 6 | en 7 | den 8 | til 9 | er 10 | som 11 | på 12 | de 13 | med 14 | han 15 | af 16 | for 17 | ikke 18 | der 19 | var 20 | mig 21 | sig 22 | men 23 | et 24 | har 25 | om 26 | vi 27 | min 28 | havde 29 | ham 30 | hun 31 | nu 32 | over 33 | da 34 | fra 35 | du 36 | ud 37 | sin 38 | dem 39 | os 40 | op 41 | man 42 | hans 43 | hvor 44 | eller 45 | hvad 46 | skal 47 | selv 48 | her 49 | alle 50 | vil 51 | blev 52 | kunne 53 | ind 54 | når 55 | være 56 | dog 57 | noget 58 | ville 59 | jo 60 | deres 61 | efter 62 | ned 63 | skulle 64 | denne 65 | end 66 | dette 67 | mit 68 | også 69 | under 70 | have 71 | dig 72 | anden 73 | hende 74 | mine 75 | alt 76 | meget 77 | sit 78 | sine 79 | vor 80 | mod 81 | disse 82 | hvis 83 | din 84 | nogle 85 | hos 86 | blive 87 | mange 88 | ad 89 | bliver 90 | hendes 91 | været 92 | thi 93 | jer 94 | sådan 95 | -------------------------------------------------------------------------------- /pkg/util/tsearch/stopwords/dutch.stop: -------------------------------------------------------------------------------- 1 | de 2 | en 3 | van 4 | ik 5 | te 6 | dat 7 | die 8 | in 9 | een 10 | hij 11 | het 12 | niet 13 | zijn 14 | is 15 | was 16 | op 17 | aan 18 | met 19 | als 20 | voor 21 | had 22 | er 23 | maar 24 | om 25 | hem 26 | dan 27 | zou 28 | of 29 | wat 30 | mijn 31 | men 32 | dit 33 | zo 34 | door 35 | over 36 | ze 37 | zich 38 | bij 39 | ook 40 | tot 41 | je 42 | mij 43 | uit 44 | der 45 | daar 46 | haar 47 | naar 48 | heb 49 | hoe 50 | heeft 51 | hebben 52 | deze 53 | u 54 | want 55 | nog 56 | zal 57 | me 58 | zij 59 | nu 60 | ge 61 | geen 62 | omdat 63 | iets 64 | worden 65 | toch 66 | al 67 | waren 68 | veel 69 | meer 70 | doen 71 | toen 72 | moet 73 | ben 74 | zonder 75 | kan 76 | hun 77 | dus 78 | alles 79 | onder 80 | ja 81 | eens 82 | hier 83 | wie 84 | werd 85 | altijd 86 | doch 87 | wordt 88 | wezen 89 | kunnen 90 | ons 91 | zelf 92 | tegen 93 | na 94 | reeds 95 | wil 96 | kon 97 | niets 98 | uw 99 | iemand 100 | geweest 101 | andere 102 | -------------------------------------------------------------------------------- /pkg/util/tsearch/stopwords/english.stop: -------------------------------------------------------------------------------- 1 | i 2 | me 3 | my 4 | myself 5 | we 6 | our 7 | ours 8 | ourselves 9 | you 10 | your 11 | yours 12 | yourself 13 | yourselves 14 | he 15 | him 16 | his 17 | himself 18 | she 19 | her 20 | hers 21 | herself 22 | it 23 | its 24 | itself 25 | they 26 | them 27 | their 28 | theirs 29 | themselves 30 | what 31 | which 32 | who 33 | whom 34 | this 35 | that 36 | these 37 | those 38 | am 39 | is 40 | are 41 | was 42 | were 43 | be 44 | been 45 | being 46 | have 47 | has 48 | had 49 | having 50 | do 51 | does 52 | did 53 | doing 54 | a 55 | an 56 | the 57 | and 58 | but 59 | if 60 | or 61 | because 62 | as 63 | until 64 | while 65 | of 66 | at 67 | by 68 | for 69 | with 70 | about 71 | against 72 | between 73 | into 74 | through 75 | during 76 | before 77 | after 78 | above 79 | below 80 | to 81 | from 82 | up 83 | down 84 | in 85 | out 86 | on 87 | off 88 | over 89 | under 90 | again 91 | further 92 | then 93 | once 94 | here 95 | there 96 | when 97 | where 98 | why 99 | how 100 | all 101 | any 102 | both 103 | each 104 | few 105 | more 106 | most 107 | other 108 | some 109 | such 110 | no 111 | nor 112 | not 113 | only 114 | own 115 | same 116 | so 117 | than 118 | too 119 | very 120 | s 121 | t 122 | can 123 | will 124 | just 125 | don 126 | should 127 | now 128 | -------------------------------------------------------------------------------- /pkg/util/tsearch/stopwords/french.stop: -------------------------------------------------------------------------------- 1 | au 2 | aux 3 | avec 4 | ce 5 | ces 6 | dans 7 | de 8 | des 9 | du 10 | elle 11 | en 12 | et 13 | eux 14 | il 15 | je 16 | la 17 | le 18 | leur 19 | lui 20 | ma 21 | mais 22 | me 23 | même 24 | mes 25 | moi 26 | mon 27 | ne 28 | nos 29 | notre 30 | nous 31 | on 32 | ou 33 | par 34 | pas 35 | pour 36 | qu 37 | que 38 | qui 39 | sa 40 | se 41 | ses 42 | son 43 | sur 44 | ta 45 | te 46 | tes 47 | toi 48 | ton 49 | tu 50 | un 51 | une 52 | vos 53 | votre 54 | vous 55 | c 56 | d 57 | j 58 | l 59 | à 60 | m 61 | n 62 | s 63 | t 64 | y 65 | été 66 | étée 67 | étées 68 | étés 69 | étant 70 | étante 71 | étants 72 | étantes 73 | suis 74 | es 75 | est 76 | sommes 77 | êtes 78 | sont 79 | serai 80 | seras 81 | sera 82 | serons 83 | serez 84 | seront 85 | serais 86 | serait 87 | serions 88 | seriez 89 | seraient 90 | étais 91 | était 92 | étions 93 | étiez 94 | étaient 95 | fus 96 | fut 97 | fûmes 98 | fûtes 99 | furent 100 | sois 101 | soit 102 | soyons 103 | soyez 104 | soient 105 | fusse 106 | fusses 107 | fût 108 | fussions 109 | fussiez 110 | fussent 111 | ayant 112 | ayante 113 | ayantes 114 | ayants 115 | eu 116 | eue 117 | eues 118 | eus 119 | ai 120 | as 121 | avons 122 | avez 123 | ont 124 | aurai 125 | auras 126 | aura 127 | aurons 128 | aurez 129 | auront 130 | aurais 131 | aurait 132 | aurions 133 | auriez 134 | auraient 135 | avais 136 | avait 137 | avions 138 | aviez 139 | avaient 140 | eut 141 | eûmes 142 | eûtes 143 | eurent 144 | aie 145 | aies 146 | ait 147 | ayons 148 | ayez 149 | aient 150 | eusse 151 | eusses 152 | eût 153 | eussions 154 | eussiez 155 | eussent 156 | -------------------------------------------------------------------------------- /pkg/util/tsearch/stopwords/russian.stop: -------------------------------------------------------------------------------- 1 | и 2 | в 3 | во 4 | не 5 | что 6 | он 7 | на 8 | я 9 | с 10 | со 11 | как 12 | а 13 | то 14 | все 15 | она 16 | так 17 | его 18 | но 19 | да 20 | ты 21 | к 22 | у 23 | же 24 | вы 25 | за 26 | бы 27 | по 28 | только 29 | ее 30 | мне 31 | было 32 | вот 33 | от 34 | меня 35 | еще 36 | нет 37 | о 38 | из 39 | ему 40 | теперь 41 | когда 42 | даже 43 | ну 44 | вдруг 45 | ли 46 | если 47 | уже 48 | или 49 | ни 50 | быть 51 | был 52 | него 53 | до 54 | вас 55 | нибудь 56 | опять 57 | уж 58 | вам 59 | ведь 60 | там 61 | потом 62 | себя 63 | ничего 64 | ей 65 | может 66 | они 67 | тут 68 | где 69 | есть 70 | надо 71 | ней 72 | для 73 | мы 74 | тебя 75 | их 76 | чем 77 | была 78 | сам 79 | чтоб 80 | без 81 | будто 82 | чего 83 | раз 84 | тоже 85 | себе 86 | под 87 | будет 88 | ж 89 | тогда 90 | кто 91 | этот 92 | того 93 | потому 94 | этого 95 | какой 96 | совсем 97 | ним 98 | здесь 99 | этом 100 | один 101 | почти 102 | мой 103 | тем 104 | чтобы 105 | нее 106 | сейчас 107 | были 108 | куда 109 | зачем 110 | всех 111 | никогда 112 | можно 113 | при 114 | наконец 115 | два 116 | об 117 | другой 118 | хоть 119 | после 120 | над 121 | больше 122 | тот 123 | через 124 | эти 125 | нас 126 | про 127 | всего 128 | них 129 | какая 130 | много 131 | разве 132 | три 133 | эту 134 | моя 135 | впрочем 136 | хорошо 137 | свою 138 | этой 139 | перед 140 | иногда 141 | лучше 142 | чуть 143 | том 144 | нельзя 145 | такой 146 | им 147 | более 148 | всегда 149 | конечно 150 | всю 151 | между 152 | -------------------------------------------------------------------------------- /pkg/util/tsearch/stopwords/swedish.stop: -------------------------------------------------------------------------------- 1 | och 2 | det 3 | att 4 | i 5 | en 6 | jag 7 | hon 8 | som 9 | han 10 | på 11 | den 12 | med 13 | var 14 | sig 15 | för 16 | så 17 | till 18 | är 19 | men 20 | ett 21 | om 22 | hade 23 | de 24 | av 25 | icke 26 | mig 27 | du 28 | henne 29 | då 30 | sin 31 | nu 32 | har 33 | inte 34 | hans 35 | honom 36 | skulle 37 | hennes 38 | där 39 | min 40 | man 41 | ej 42 | vid 43 | kunde 44 | något 45 | från 46 | ut 47 | när 48 | efter 49 | upp 50 | vi 51 | dem 52 | vara 53 | vad 54 | över 55 | än 56 | dig 57 | kan 58 | sina 59 | här 60 | ha 61 | mot 62 | alla 63 | under 64 | någon 65 | eller 66 | allt 67 | mycket 68 | sedan 69 | ju 70 | denna 71 | själv 72 | detta 73 | åt 74 | utan 75 | varit 76 | hur 77 | ingen 78 | mitt 79 | ni 80 | bli 81 | blev 82 | oss 83 | din 84 | dessa 85 | några 86 | deras 87 | blir 88 | mina 89 | samma 90 | vilken 91 | er 92 | sådan 93 | vår 94 | blivit 95 | dess 96 | inom 97 | mellan 98 | sådant 99 | varför 100 | varje 101 | vilka 102 | ditt 103 | vem 104 | vilket 105 | sitta 106 | sådana 107 | vart 108 | dina 109 | vars 110 | vårt 111 | våra 112 | ert 113 | era 114 | vilkas 115 | -------------------------------------------------------------------------------- /pkg/util/tsearch/stopwords/turkish.stop: -------------------------------------------------------------------------------- 1 | acaba 2 | ama 3 | aslında 4 | az 5 | bazı 6 | belki 7 | biri 8 | birkaç 9 | birşey 10 | biz 11 | bu 12 | çok 13 | çünkü 14 | da 15 | daha 16 | de 17 | defa 18 | diye 19 | eğer 20 | en 21 | gibi 22 | hem 23 | hep 24 | hepsi 25 | her 26 | hiç 27 | için 28 | ile 29 | ise 30 | kez 31 | ki 32 | kim 33 | mı 34 | mu 35 | mü 36 | nasıl 37 | ne 38 | neden 39 | nerde 40 | nerede 41 | nereye 42 | niçin 43 | niye 44 | o 45 | sanki 46 | şey 47 | siz 48 | şu 49 | tüm 50 | ve 51 | veya 52 | ya 53 | yani 54 | -------------------------------------------------------------------------------- /pkg/util/unresolved_addr.proto: -------------------------------------------------------------------------------- 1 | // Copyright 2015 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | syntax = "proto2"; 7 | package cockroach.util; 8 | option go_package = "github.com/cockroachdb/cockroach/pkg/util"; 9 | 10 | import "gogoproto/gogo.proto"; 11 | 12 | // UnresolvedAddr is an unresolved version of net.Addr. 13 | message UnresolvedAddr { 14 | option (gogoproto.goproto_stringer) = false; 15 | option (gogoproto.equal) = true; 16 | 17 | optional string network_field = 1 [(gogoproto.nullable) = false]; 18 | optional string address_field = 2 [(gogoproto.nullable) = false]; 19 | } 20 | -------------------------------------------------------------------------------- /pkg/util/uuid/fuzz.go: -------------------------------------------------------------------------------- 1 | // Copyright 2019 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | // Copyright (c) 2018 Andrei Tudor Călin 7 | // Use of this source code is governed by a MIT-style 8 | // license that can be found in licenses/MIT-gofrs.txt. 9 | 10 | // This code originated in github.com/gofrs/uuid. 11 | 12 | //go:build gofuzz 13 | 14 | package uuid 15 | 16 | // Fuzz implements a simple fuzz test for FromString / UnmarshalText. 17 | // 18 | // To run: 19 | // 20 | // $ go get github.com/dvyukov/go-fuzz/... 21 | // $ cd $GOPATH/src/github.com/gofrs/uuid 22 | // $ go-fuzz-build github.com/gofrs/uuid 23 | // $ go-fuzz -bin=uuid-fuzz.zip -workdir=./testdata 24 | // 25 | // If you make significant changes to FromString / UnmarshalText and add 26 | // new cases to fromStringTests (in codec_test.go), please run 27 | // 28 | // $ go test -seed_fuzz_corpus 29 | // 30 | // to seed the corpus with the new interesting inputs, then run the fuzzer. 31 | func Fuzz(data []byte) int { 32 | _, err := FromString(string(data)) 33 | if err != nil { 34 | return 0 35 | } 36 | return 1 37 | } 38 | -------------------------------------------------------------------------------- /pkg/util/vector/vector.proto: -------------------------------------------------------------------------------- 1 | // Copyright 2024 The Cockroach Authors. 2 | // 3 | // Use of this software is governed by the CockroachDB Software License 4 | // included in the /LICENSE file. 5 | 6 | syntax = "proto3"; 7 | package cockroach.util.vector; 8 | option go_package = "github.com/cockroachdb/cockroach/pkg/util/vector"; 9 | 10 | import "gogoproto/gogo.proto"; 11 | 12 | option (gogoproto.goproto_getters_all) = false; 13 | 14 | // Set is a set of float32 vectors of equal dimension. Vectors in the set are 15 | // stored contiguously in a slice, in row-wise order. They are assumed to be 16 | // unordered; some methods do not preserve ordering. 17 | message Set { 18 | // Dims is the number of dimensions of each vector in the set. 19 | int64 dims = 1 [(gogoproto.casttype) = "int"]; 20 | // Count is the number of vectors in the set. 21 | int64 count = 2 [(gogoproto.casttype) = "int"]; 22 | // Data is a float32 slice that contains all vectors, laid out contiguously in 23 | // row-wise order in memory. 24 | // NB: Avoid using this field directly, instead preferring to use the At 25 | // function to access individual vectors. 26 | repeated float data = 3; 27 | } 28 | -------------------------------------------------------------------------------- /version: -------------------------------------------------------------------------------- 1 | 259c8a573eb2ba28643ffd55ee3bf6a438198c04 2 | --------------------------------------------------------------------------------