Edit on GitHub

# sqlglot.dialects.starrocks

View Source

     1from __future__ import annotations
     2
     3from sqlglot import exp
     4from sqlglot.dialects.dialect import (
     5    approx_count_distinct_sql,
     6    arrow_json_extract_sql,
     7    rename_func,
     8)
     9from sqlglot.dialects.mysql import MySQL
    10from sqlglot.helper import seq_get
    11
    12
    13class StarRocks(MySQL):
    14    class Parser(MySQL.Parser):  # type: ignore
    15        FUNCTIONS = {
    16            **MySQL.Parser.FUNCTIONS,
    17            "APPROX_COUNT_DISTINCT": exp.ApproxDistinct.from_arg_list,
    18            "DATE_TRUNC": lambda args: exp.TimestampTrunc(
    19                this=seq_get(args, 1), unit=seq_get(args, 0)
    20            ),
    21        }
    22
    23    class Generator(MySQL.Generator):  # type: ignore
    24        TYPE_MAPPING = {
    25            **MySQL.Generator.TYPE_MAPPING,  # type: ignore
    26            exp.DataType.Type.TEXT: "STRING",
    27            exp.DataType.Type.TIMESTAMP: "DATETIME",
    28            exp.DataType.Type.TIMESTAMPTZ: "DATETIME",
    29        }
    30
    31        TRANSFORMS = {
    32            **MySQL.Generator.TRANSFORMS,  # type: ignore
    33            exp.ApproxDistinct: approx_count_distinct_sql,
    34            exp.JSONExtractScalar: arrow_json_extract_sql,
    35            exp.JSONExtract: arrow_json_extract_sql,
    36            exp.DateDiff: rename_func("DATEDIFF"),
    37            exp.RegexpLike: rename_func("REGEXP"),
    38            exp.StrToUnix: lambda self, e: f"UNIX_TIMESTAMP({self.sql(e, 'this')}, {self.format_time(e)})",
    39            exp.TimestampTrunc: lambda self, e: self.func(
    40                "DATE_TRUNC", exp.Literal.string(e.text("unit")), e.this
    41            ),
    42            exp.TimeStrToDate: rename_func("TO_DATE"),
    43            exp.UnixToStr: lambda self, e: f"FROM_UNIXTIME({self.sql(e, 'this')}, {self.format_time(e)})",
    44            exp.UnixToTime: rename_func("FROM_UNIXTIME"),
    45        }
    46        TRANSFORMS.pop(exp.DateTrunc)

class StarRocks(sqlglot.dialects.mysql.MySQL): View Source

    14class StarRocks(MySQL):
    15    class Parser(MySQL.Parser):  # type: ignore
    16        FUNCTIONS = {
    17            **MySQL.Parser.FUNCTIONS,
    18            "APPROX_COUNT_DISTINCT": exp.ApproxDistinct.from_arg_list,
    19            "DATE_TRUNC": lambda args: exp.TimestampTrunc(
    20                this=seq_get(args, 1), unit=seq_get(args, 0)
    21            ),
    22        }
    23
    24    class Generator(MySQL.Generator):  # type: ignore
    25        TYPE_MAPPING = {
    26            **MySQL.Generator.TYPE_MAPPING,  # type: ignore
    27            exp.DataType.Type.TEXT: "STRING",
    28            exp.DataType.Type.TIMESTAMP: "DATETIME",
    29            exp.DataType.Type.TIMESTAMPTZ: "DATETIME",
    30        }
    31
    32        TRANSFORMS = {
    33            **MySQL.Generator.TRANSFORMS,  # type: ignore
    34            exp.ApproxDistinct: approx_count_distinct_sql,
    35            exp.JSONExtractScalar: arrow_json_extract_sql,
    36            exp.JSONExtract: arrow_json_extract_sql,
    37            exp.DateDiff: rename_func("DATEDIFF"),
    38            exp.RegexpLike: rename_func("REGEXP"),
    39            exp.StrToUnix: lambda self, e: f"UNIX_TIMESTAMP({self.sql(e, 'this')}, {self.format_time(e)})",
    40            exp.TimestampTrunc: lambda self, e: self.func(
    41                "DATE_TRUNC", exp.Literal.string(e.text("unit")), e.this
    42            ),
    43            exp.TimeStrToDate: rename_func("TO_DATE"),
    44            exp.UnixToStr: lambda self, e: f"FROM_UNIXTIME({self.sql(e, 'this')}, {self.format_time(e)})",
    45            exp.UnixToTime: rename_func("FROM_UNIXTIME"),
    46        }
    47        TRANSFORMS.pop(exp.DateTrunc)

##### Inherited Members

sqlglot.dialects.mysql.MySQL
  Tokenizer
sqlglot.dialects.dialect.Dialect
  get_or_raise
  format_time
  parse
  parse_into
  generate
  transpile
  tokenize
  parser
  generator

class StarRocks.Parser(sqlglot.dialects.mysql.MySQL.Parser): View Source

    15    class Parser(MySQL.Parser):  # type: ignore
    16        FUNCTIONS = {
    17            **MySQL.Parser.FUNCTIONS,
    18            "APPROX_COUNT_DISTINCT": exp.ApproxDistinct.from_arg_list,
    19            "DATE_TRUNC": lambda args: exp.TimestampTrunc(
    20                this=seq_get(args, 1), unit=seq_get(args, 0)
    21            ),
    22        }

Parser consumes a list of tokens produced by the `sqlglot.tokens.Tokenizer` and produces a parsed syntax tree.

###### Arguments:

- error_level: the desired error level. Default: ErrorLevel.RAISE
- error_message_context: determines the amount of context to capture from a query string when displaying the error message (in number of characters). Default: 50.
- index_offset: Index offset for arrays eg ARRAY[0] vs ARRAY[1] as the head of a list. Default: 0
- alias_post_tablesample: If the table alias comes after tablesample. Default: False
- max_errors: Maximum number of error messages to include in a raised ParseError. This is only relevant if error_level is ErrorLevel.RAISE. Default: 3
- null_ordering: Indicates the default null ordering method to use if not explicitly set. Options are "nulls_are_small", "nulls_are_large", "nulls_are_last". Default: "nulls_are_small"

##### Inherited Members

sqlglot.parser.Parser
  Parser
  reset
  parse
  parse_into
  check_errors
  raise_error
  expression
  validate_expression

class StarRocks.Generator(sqlglot.dialects.mysql.MySQL.Generator): View Source

    24    class Generator(MySQL.Generator):  # type: ignore
    25        TYPE_MAPPING = {
    26            **MySQL.Generator.TYPE_MAPPING,  # type: ignore
    27            exp.DataType.Type.TEXT: "STRING",
    28            exp.DataType.Type.TIMESTAMP: "DATETIME",
    29            exp.DataType.Type.TIMESTAMPTZ: "DATETIME",
    30        }
    31
    32        TRANSFORMS = {
    33            **MySQL.Generator.TRANSFORMS,  # type: ignore
    34            exp.ApproxDistinct: approx_count_distinct_sql,
    35            exp.JSONExtractScalar: arrow_json_extract_sql,
    36            exp.JSONExtract: arrow_json_extract_sql,
    37            exp.DateDiff: rename_func("DATEDIFF"),
    38            exp.RegexpLike: rename_func("REGEXP"),
    39            exp.StrToUnix: lambda self, e: f"UNIX_TIMESTAMP({self.sql(e, 'this')}, {self.format_time(e)})",
    40            exp.TimestampTrunc: lambda self, e: self.func(
    41                "DATE_TRUNC", exp.Literal.string(e.text("unit")), e.this
    42            ),
    43            exp.TimeStrToDate: rename_func("TO_DATE"),
    44            exp.UnixToStr: lambda self, e: f"FROM_UNIXTIME({self.sql(e, 'this')}, {self.format_time(e)})",
    45            exp.UnixToTime: rename_func("FROM_UNIXTIME"),
    46        }
    47        TRANSFORMS.pop(exp.DateTrunc)

Generator interprets the given syntax tree and produces a SQL string as an output.

###### Arguments:

- time_mapping (dict): the dictionary of custom time mappings in which the key represents a python time format and the output the target time format
- time_trie (trie): a trie of the time_mapping keys
- pretty (bool): if set to True the returned string will be formatted. Default: False.
- quote_start (str): specifies which starting character to use to delimit quotes. Default: '.
- quote_end (str): specifies which ending character to use to delimit quotes. Default: '.
- identifier_start (str): specifies which starting character to use to delimit identifiers. Default: ".
- identifier_end (str): specifies which ending character to use to delimit identifiers. Default: ".
- identify (bool | str): 'always': always quote, 'safe': quote identifiers if they don't contain an upcase, True defaults to always.
- normalize (bool): if set to True all identifiers will lower cased
- string_escape (str): specifies a string escape character. Default: '.
- identifier_escape (str): specifies an identifier escape character. Default: ".
- pad (int): determines padding in a formatted string. Default: 2.
- indent (int): determines the size of indentation in a formatted string. Default: 4.
- unnest_column_only (bool): if true unnest table aliases are considered only as column aliases
- normalize_functions (str): normalize function names, "upper", "lower", or None Default: "upper"
- alias_post_tablesample (bool): if the table alias comes after tablesample Default: False
- unsupported_level (ErrorLevel): determines the generator's behavior when it encounters unsupported expressions. Default ErrorLevel.WARN.
- null_ordering (str): Indicates the default null ordering method to use if not explicitly set. Options are "nulls_are_small", "nulls_are_large", "nulls_are_last". Default: "nulls_are_small"
- max_unsupported (int): Maximum number of unsupported messages to include in a raised UnsupportedError. This is only relevant if unsupported_level is ErrorLevel.RAISE. Default: 3
- leading_comma (bool): if the the comma is leading or trailing in select statements Default: False
- max_text_width: The max number of characters in a segment before creating new lines in pretty mode. The default is on the smaller end because the length only represents a segment and not the true line length. Default: 80
- comments: Whether or not to preserve comments in the output SQL code. Default: True

##### Inherited Members

sqlglot.generator.Generator
  Generator
  generate
  unsupported
  sep
  seg
  pad_comment
  maybe_comment
  wrap
  no_identify
  normalize_func
  indent
  sql
  uncache_sql
  cache_sql
  characterset_sql
  column_sql
  columnposition_sql
  columndef_sql
  columnconstraint_sql
  autoincrementcolumnconstraint_sql
  compresscolumnconstraint_sql
  generatedasidentitycolumnconstraint_sql
  notnullcolumnconstraint_sql
  primarykeycolumnconstraint_sql
  uniquecolumnconstraint_sql
  create_sql
  describe_sql
  prepend_ctes
  with_sql
  cte_sql
  tablealias_sql
  bitstring_sql
  hexstring_sql
  bytestring_sql
  datatype_sql
  directory_sql
  delete_sql
  drop_sql
  except_sql
  except_op
  fetch_sql
  filter_sql
  hint_sql
  index_sql
  identifier_sql
  inputoutputformat_sql
  national_sql
  partition_sql
  properties_sql
  root_properties
  properties
  with_properties
  locate_properties
  property_sql
  likeproperty_sql
  fallbackproperty_sql
  journalproperty_sql
  freespaceproperty_sql
  afterjournalproperty_sql
  checksumproperty_sql
  mergeblockratioproperty_sql
  datablocksizeproperty_sql
  blockcompressionproperty_sql
  isolatedloadingproperty_sql
  lockingproperty_sql
  withdataproperty_sql
  insert_sql
  intersect_sql
  intersect_op
  introducer_sql
  pseudotype_sql
  onconflict_sql
  returning_sql
  rowformatdelimitedproperty_sql
  table_sql
  tablesample_sql
  pivot_sql
  tuple_sql
  update_sql
  values_sql
  var_sql
  into_sql
  from_sql
  group_sql
  having_sql
  join_sql
  lambda_sql
  lateral_sql
  limit_sql
  offset_sql
  setitem_sql
  set_sql
  pragma_sql
  lock_sql
  literal_sql
  loaddata_sql
  null_sql
  boolean_sql
  order_sql
  cluster_sql
  distribute_sql
  sort_sql
  ordered_sql
  matchrecognize_sql
  query_modifiers
  select_sql
  schema_sql
  star_sql
  structkwarg_sql
  parameter_sql
  sessionparameter_sql
  placeholder_sql
  subquery_sql
  qualify_sql
  union_sql
  union_op
  unnest_sql
  where_sql
  window_sql
  partition_by_sql
  windowspec_sql
  withingroup_sql
  between_sql
  bracket_sql
  all_sql
  any_sql
  exists_sql
  case_sql
  constraint_sql
  nextvaluefor_sql
  extract_sql
  trim_sql
  concat_sql
  check_sql
  foreignkey_sql
  primarykey_sql
  unique_sql
  if_sql
  matchagainst_sql
  jsonkeyvalue_sql
  jsonobject_sql
  in_sql
  in_unnest_op
  interval_sql
  return_sql
  reference_sql
  anonymous_sql
  paren_sql
  neg_sql
  not_sql
  alias_sql
  aliases_sql
  attimezone_sql
  add_sql
  and_sql
  connector_sql
  bitwiseand_sql
  bitwiseleftshift_sql
  bitwisenot_sql
  bitwiseor_sql
  bitwiserightshift_sql
  bitwisexor_sql
  cast_sql
  currentdate_sql
  collate_sql
  command_sql
  comment_sql
  transaction_sql
  commit_sql
  rollback_sql
  altercolumn_sql
  renametable_sql
  altertable_sql
  droppartition_sql
  addconstraint_sql
  distinct_sql
  ignorenulls_sql
  respectnulls_sql
  intdiv_sql
  dpipe_sql
  div_sql
  overlaps_sql
  distance_sql
  dot_sql
  eq_sql
  escape_sql
  glob_sql
  gt_sql
  gte_sql
  ilike_sql
  ilikeany_sql
  is_sql
  like_sql
  likeany_sql
  similarto_sql
  lt_sql
  lte_sql
  mod_sql
  mul_sql
  neq_sql
  nullsafeeq_sql
  nullsafeneq_sql
  or_sql
  slice_sql
  sub_sql
  trycast_sql
  use_sql
  binary
  function_fallback_sql
  func
  format_args
  text_width
  format_time
  expressions
  op_expressions
  naked_property
  set_operation
  tag_sql
  token_sql
  userdefinedfunction_sql
  joinhint_sql
  kwarg_sql
  when_sql
  merge_sql
  tochar_sql
sqlglot.dialects.mysql.MySQL.Generator
  show_sql