class TdCollectStatisticsStatementSegment(BaseSegment): """A `COLLECT STATISTICS (Optimizer Form)` statement. # TODO: Make complete COLLECT [SUMMARY] (STATISTICS|STAT) [[COLUMN| [UNIQUE] INDEX] (expression (, expression ...)] ON TABLENAME """ type = "collect_statistics_statement" match_grammar = Sequence( "COLLECT", Ref.keyword("SUMMARY", optional=True), OneOf("STATISTICS", "STAT"), OneOf( Sequence( OneOf( "COLUMN", Sequence( Ref.keyword("UNIQUE", optional=True), "INDEX", ), ), OneOf( Bracketed( Delimited(Ref("ObjectReferenceSegment"), delimiter=Ref("CommaSegment"))), Ref("ObjectReferenceSegment"), ), ), optional=True, ), "ON", Ref("ObjectReferenceSegment"), )
class FromExpressionElementSegment(ansi.FromExpressionElementSegment): """A table expression. Overridden from ANSI to allow FINAL modifier. https://clickhouse.com/docs/en/sql-reference/statements/select/from#final-modifier """ type = "from_expression_element" match_grammar: Matchable = Sequence( Ref("PreTableFunctionKeywordsGrammar", optional=True), OptionallyBracketed(Ref("TableExpressionSegment")), Ref( "AliasExpressionSegment", exclude=OneOf( Ref("SamplingExpressionSegment"), Ref("JoinLikeClauseGrammar"), Ref.keyword("Final"), ), optional=True, ), Ref.keyword("FINAL", optional=True), # https://cloud.google.com/bigquery/docs/reference/standard-sql/arrays#flattening_arrays Sequence("WITH", "OFFSET", Ref("AliasExpressionSegment"), optional=True), Ref("SamplingExpressionSegment", optional=True), Ref("PostTableExpressionGrammar", optional=True), )
class DistributeByClauseSegment(ansi.OrderByClauseSegment): """A `DISTRIBUTE BY` clause like in `SELECT`.""" type = "distributeby_clause" match_grammar: Matchable = StartsWith( Sequence("DISTRIBUTE", "BY"), terminator=OneOf( "SORT", "LIMIT", "HAVING", "QUALIFY", # For window functions "WINDOW", Ref("FrameClauseUnitGrammar"), "SEPARATOR", ), ) parse_grammar: Optional[Matchable] = Sequence( "DISTRIBUTE", "BY", Indent, Delimited( Sequence( OneOf( Ref("ColumnReferenceSegment"), Ref("NumericLiteralSegment"), Ref("ExpressionSegment"), ), ), terminator=OneOf( Ref.keyword("LIMIT"), Ref("FrameClauseUnitGrammar"), Ref.keyword("SORT") ), ), Dedent, )
class TdCreateTableOptions(BaseSegment): """CreateTableOptions. , NO FALLBACK, NO BEFORE JOURNAL, NO AFTER JOURNAL, CHECKSUM = DEFAULT , DEFAULT MERGEBLOCKRATIO """ type = "create_table_options_statement" match_grammar = AnyNumberOf( Sequence( Ref("CommaSegment"), OneOf( # [ NO ] FALLBACK [ PROTECTION ] Sequence( Ref.keyword("NO", optional=True), "FALLBACK", Ref.keyword("PROTECTION", optional=True), ), # [NO | DUAL | LOCAL |NOT LOCAL] [AFTER | BEFORE] JOURNAL Sequence( OneOf("NO", "DUAL", "LOCAL", Sequence("NOT", "LOCAL"), optional=True), OneOf("BEFORE", "AFTER", optional=True), "JOURNAL", ), # CHECKSUM = (ON|OFF|DEFAULT) Sequence( "CHECKSUM", Ref("EqualsSegment"), OneOf( "ON", "OFF", "DEFAULT", ), ), # (NO|Default) MergeBlockRatio Sequence( OneOf( "DEFAULT", "NO", ), "MERGEBLOCKRATIO", ), # MergeBlockRatio = integer [PERCENT] Sequence( "MERGEBLOCKRATIO", Ref("EqualsSegment"), Ref("NumericLiteralSegment"), Ref.keyword("PERCENT", optional=True), ), ), ), )
class TdTableConstraints(BaseSegment): """Teradata specific table attributes. e.g. UNIQUE PRIMARY INDEX Column_name | ( Column_name, ... ) NO PRIMARY INDEX ... """ type = "td_table_constraint" match_grammar = AnyNumberOf( # PRIMARY Index OneOf( Sequence( # UNIQUE PRIMARY INDEX Column_name | ( Column_name, ... ) Ref.keyword("UNIQUE", optional=True), "PRIMARY", "INDEX", Ref("ObjectReferenceSegment", optional=True), # primary index name OneOf( Bracketed( Delimited( Ref("SingleIdentifierGrammar"), ) ), Ref("SingleIdentifierGrammar"), ), ), Sequence("NO", "PRIMARY", "INDEX"), # NO PRIMARY INDEX ), # PARTITION BY ... Sequence( # INDEX HOPR_TRN_TRAV_SIN_MP_I ( IND_TIPO_TARJETA ); "PARTITION", "BY", Ref("TdTablePartitioningLevel"), ), # Index Sequence( # INDEX HOPR_TRN_TRAV_SIN_MP_I ( IND_TIPO_TARJETA ); Ref.keyword("UNIQUE", optional=True), "INDEX", Ref("ObjectReferenceSegment"), # Index name Ref.keyword("ALL", optional=True), Bracketed( # Columns making up constraint Delimited(Ref("ColumnReferenceSegment")), ), ), # WITH DATA Sequence("WITH", Sequence("NO", optional=True), "DATA"), # ON COMMIT PRESERVE ROWS Sequence("ON", "COMMIT", OneOf("PRESERVE", "DELETE"), "ROWS"), )
class DropIndexStatementSegment(BaseSegment): """A `DROP INDEX` statement.""" type = "drop_statement" # DROP INDEX <Index name> [CONCURRENTLY] [IF EXISTS] {RESTRICT | CASCADE} match_grammar = Sequence( "DROP", "INDEX", Ref.keyword("CONCURRENTLY", optional=True), Ref("IfExistsGrammar", optional=True), Ref("IndexExpressionSegment"), OneOf("RESTRICT", Ref.keyword("CASCADE", optional=True), optional=True), )
class DataFormatSegment(BaseSegment): """DataFormat segment. Indicates data format available for COPY commands. https://docs.aws.amazon.com/redshift/latest/dg/c_Compression_encodings.html """ type = "data_format_segment" match_grammar = Sequence( Sequence( "FORMAT", Ref.keyword("AS", optional=True), optional=True, ), OneOf( Sequence( "CSV", Sequence( "QUOTE", Ref.keyword("AS", optional=True), Ref("QuotedLiteralSegment"), optional=True, ), ), Sequence( "SHAPEFILE", Sequence( "SIMPLIFY", Ref.keyword("AUTO", optional=True), Ref("NumericLiteralSegment", optional=True), optional=True, ), ), Sequence( OneOf("AVRO", "JSON"), Sequence( Ref.keyword("AS", optional=True), Ref("QuotedLiteralSegment"), optional=True, ), ), "PARQUET", "ORC", "RCFILE", "SEQUENCEFILE", ), )
class AuthorizationSegment(BaseSegment): """Authorization segment. Specifies authorization to access data in another AWS resource. https://docs.aws.amazon.com/redshift/latest/dg/copy-parameters-authorization.html """ type = "authorization_segment" match_grammar = AnySetOf( OneOf( Sequence( "IAM_ROLE", OneOf( "DEFAULT", Ref("QuotedLiteralSegment"), ), ), Sequence( Ref.keyword("WITH", optional=True), "CREDENTIALS", Ref.keyword("AS", optional=True), Ref("QuotedLiteralSegment"), ), Sequence( "ACCESS_KEY_ID", Ref("QuotedLiteralSegment"), "SECRET_ACCESS_KEY", Ref("QuotedLiteralSegment"), Sequence( "SESSION_TOKEN", Ref("QuotedLiteralSegment"), optional=True, ), ), optional=False, ), Sequence( "KMS_KEY_ID", Ref("QuotedLiteralSegment"), optional=True, ), Sequence( "MASTER_SYMMETRIC_KEY", Ref("QuotedLiteralSegment"), optional=True, ), )
class SortByClauseSegment(ansi.OrderByClauseSegment): """A `SORT BY` clause like in `SELECT`.""" type = "sortby_clause" match_grammar: Matchable = StartsWith( Sequence("SORT", "BY"), terminator=ansi.OrderByClauseSegment.match_grammar.terminator, # type: ignore ) parse_grammar: Optional[Matchable] = Sequence( "SORT", "BY", Indent, Delimited( Sequence( OneOf( Ref("ColumnReferenceSegment"), Ref("NumericLiteralSegment"), Ref("ExpressionSegment"), ), OneOf("ASC", "DESC", optional=True), Sequence("NULLS", OneOf("FIRST", "LAST"), optional=True), ), terminator=OneOf(Ref.keyword("LIMIT"), Ref("FrameClauseUnitGrammar")), ), Dedent, )
class PrimitiveTypeSegment(BaseSegment): """Primitive data types.""" type = "primitive_type" match_grammar = OneOf( "TINYINT", "SMALLINT", "INT", "INTEGER", "BIGINT", "BOOLEAN", "FLOAT", Sequence("DOUBLE", Ref.keyword("PRECISION", optional=True)), "STRING", "BINARY", "TIMESTAMP", Sequence( OneOf("DECIMAL", "DEC", "NUMERIC"), Bracketed( Ref("NumericLiteralSegment"), Ref("CommaSegment"), Ref("NumericLiteralSegment"), optional=True, ), ), "DATE", "VARCHAR", "CHAR", )
class VacuumStatementSegment(BaseSegment): """A `VACUUM` statement. https://docs.aws.amazon.com/redshift/latest/dg/r_VACUUM_command.html """ type = "vacuum_statement" match_grammar = Sequence( "VACUUM", OneOf( "FULL", "REINDEX", "RECLUSTER", Sequence( OneOf( "SORT", "DELETE", ), "ONLY", ), optional=True, ), Ref("TableReferenceSegment", optional=True), Sequence( "TO", Ref("NumericLiteralSegment"), "PERCENT", optional=True, ), Ref.keyword("BOOST", optional=True), )
class CreateTableAsStatementSegment(BaseSegment): """A `CREATE TABLE AS` statement. As specified in https://docs.aws.amazon.com/redshift/latest/dg/r_CREATE_TABLE_AS.html """ type = "create_table_as_statement" match_grammar = Sequence( "CREATE", Sequence( Ref.keyword("LOCAL", optional=True), OneOf("TEMPORARY", "TEMP"), optional=True, ), "TABLE", Ref("ObjectReferenceSegment"), Bracketed( Delimited(Ref("ColumnReferenceSegment"), ), optional=True, ), Sequence("BACKUP", OneOf("YES", "NO"), optional=True), Ref("TableAttributeSegment", optional=True), "AS", OptionallyBracketed(Ref("SelectableGrammar")), )
class CreateLibraryStatementSegment(BaseSegment): """A `CREATE LIBRARY` statement. https://docs.aws.amazon.com/redshift/latest/dg/r_CREATE_LIBRARY.html """ type = "create_library_statement" match_grammar = Sequence( "CREATE", Sequence( "OR", "REPLACE", optional=True, ), "LIBRARY", Ref("ObjectReferenceSegment"), "LANGUAGE", "PLPYTHONU", "FROM", Ref("QuotedLiteralSegment"), AnySetOf( Ref("AuthorizationSegment", optional=False), Sequence( "REGION", Ref.keyword("AS", optional=True), Ref("QuotedLiteralSegment"), optional=True, ), ), )
class TdColumnOptionSegment(BaseSegment): """Teradata specific column attributes. e.g. CHARACTER SET LATIN or [NOT] CASESPECIFIC """ type = "td_column_attribute_constraint" match_grammar = Sequence( OneOf( Sequence( # CHARACTER SET LATIN "CHARACTER", "SET", Ref("SingleIdentifierGrammar")), Sequence( # [NOT] CASESPECIFIC Ref.keyword("NOT", optional=True), "CASESPECIFIC", ), Sequence( # COMPRESS [(1.,3.) | 3. | NULL], "COMPRESS", OneOf( Bracketed( Delimited(Ref("LiteralGrammar"), delimiter=Ref("CommaSegment"))), Ref("LiteralGrammar"), "NULL", optional=True, ), ), ), )
class AlterTableConstraintClauses(BaseSegment): """ALTER TABLE `constraint_clauses` per defined in Oracle's grammar. https://docs.oracle.com/en/database/oracle/oracle-database/21/sqlrf/ALTER-TABLE.html If possible, please match the order of this sequence with what's defined in Oracle's constraint_clauses grammar. """ type = "alter_table_constraint_clauses" match_grammar = OneOf( Sequence( "ADD", Ref("TableConstraintSegment"), ), # @TODO MODIFY # @TODO RENAME # @TODO DROP # drop_constraint_clause Sequence( "DROP", OneOf( Sequence( "PRIMARY", "KEY", ), Sequence( "UNIQUE", Bracketed(Ref("ColumnReferenceSegment")), ), Sequence("CONSTRAINT", Ref("ObjectReferenceSegment")), ), Ref.keyword("CASCADE", optional=True), Sequence( OneOf( "KEEP", "DROP", ), "INDEX", optional=True, ), Ref.keyword("ONLINE", optional=True), ), )
class SamplingExpressionSegment(BaseSegment): """A sampling expression.""" type = "snowflake_sample_expression" match_grammar = Sequence( OneOf("SAMPLE", "TABLESAMPLE"), OneOf("BERNOULLI", "ROW", "SYSTEM", "BLOCK", optional=True), Bracketed(Ref("NumericLiteralSegment"), Ref.keyword("ROWS", optional=True)), Sequence(OneOf("REPEATABLE", "SEED"), Bracketed(Ref("NumericLiteralSegment"))), )
class TransactionStatementSegment(BaseSegment): """A `COMMIT`, `ROLLBACK` or `TRANSACTION` statement. mysql: https://dev.mysql.com/doc/refman/8.0/en/commit.html mysql: https://dev.mysql.com/doc/refman/8.0/en/begin-end.html """ type = "transaction_statement" match_grammar = OneOf( Sequence("START", "TRANSACTION"), Sequence( Sequence(Ref("SingleIdentifierGrammar"), Ref("ColonSegment"), optional=True), Sequence( "BEGIN", Ref.keyword("WORK", optional=True), Ref("StatementSegment"), ), ), Sequence( "LEAVE", Ref("SingleIdentifierGrammar", optional=True), ), Sequence( "COMMIT", Ref.keyword("WORK", optional=True), Sequence("AND", Ref.keyword("NO", optional=True), "CHAIN", optional=True), ), Sequence( "ROLLBACK", Ref.keyword("WORK", optional=True), ), Sequence( "END", Ref("SingleIdentifierGrammar", optional=True), ), )
class TruncateStatementSegment(BaseSegment): """`TRUNCATE TABLE` statement.""" type = "truncate_table" match_grammar = Sequence( "TRUNCATE", Ref.keyword("TABLE", optional=True), Ref("TableReferenceSegment"), Ref("PartitionSpecGrammar", optional=True), )
class UnpivotAliasExpressionSegment(BaseSegment): """In BigQuery UNPIVOT alias's can be single or double quoted.""" type = "alias_expression" match_grammar = Sequence( Ref.keyword("AS", optional=True), OneOf( Ref("SingleQuotedLiteralSegment"), Ref("DoubleQuotedLiteralSegment"), ), )
class DropTableStatementSegment(BaseSegment): """A `DROP TABLE` statement.""" type = "drop_table_statement" match_grammar = Sequence( "DROP", "TABLE", Ref("IfExistsGrammar", optional=True), Ref("TableReferenceSegment"), Ref.keyword("PURGE", optional=True), )
class FromUpdateClauseSegment(BaseSegment): """A `FROM` clause like in `SELECT` but terminated by SET.""" type = "from_in_update_clause" match_grammar = StartsWith("FROM", terminator=Ref.keyword("SET")) parse_grammar = Sequence( "FROM", Delimited( # Optional old school delimited joins Ref("FromExpressionElementSegment"), ), )
class CreateScriptingLuaScriptStatementSegment(BaseSegment): """`CREATE SCRIPT` statement to create a Lua scripting script. https://docs.exasol.com/sql/create_script.htm """ type = "create_scripting_lua_script" is_ddl = True is_dml = False is_dql = False is_dcl = False match_grammar = StartsWith( Sequence( "CREATE", Ref("OrReplaceGrammar", optional=True), Ref.keyword("LUA", optional=True), "SCRIPT", )) parse_grammar = Sequence( "CREATE", Ref("OrReplaceGrammar", optional=True), Ref.keyword("LUA", optional=True), "SCRIPT", Ref("ScriptReferenceSegment"), Bracketed( Delimited( Sequence(Ref.keyword("ARRAY", optional=True), Ref("SingleIdentifierGrammar")), optional=True, ), optional=True, ), Sequence(Ref.keyword("RETURNS"), OneOf("TABLE", "ROWCOUNT"), optional=True), "AS", Ref("ScriptContentSegment"), )
class CreateFunctionStatementSegment(BaseSegment): """A `CREATE FUNCTION` statement.""" type = "create_function_statement" is_ddl = True is_dml = False is_dql = False is_dcl = False match_grammar = StartsWith( Sequence( "CREATE", Ref("OrReplaceGrammar", optional=True), "FUNCTION", ) ) parse_grammar = Sequence( "CREATE", Ref("OrReplaceGrammar", optional=True), "FUNCTION", Ref("FunctionReferenceSegment"), Bracketed( Delimited( Sequence( Ref("SingleIdentifierGrammar"), # Column name Ref.keyword("IN", optional=True), Ref("DatatypeSegment"), # Column type ), optional=True, ), ), "RETURN", Ref("DatatypeSegment"), OneOf("IS", "AS", optional=True), AnyNumberOf( Sequence( Ref("VariableNameSegment"), Ref("DatatypeSegment"), Ref("SemicolonSegment"), ), optional=True, ), "BEGIN", AnyNumberOf(Ref("FunctionBodySegment")), "RETURN", Ref("FunctionContentsExpressionGrammar"), Ref("SemicolonSegment"), "END", Ref("FunctionReferenceSegment", optional=True), Ref("SemicolonSegment", optional=True), )
class CreateStatementCommentSegment(BaseSegment): """A comment in a create statement. e.g. comment = 'a new view' """ type = "snowflake_comment" match_grammar = Sequence( Ref.keyword("COMMENT"), Ref("EqualsSegment"), Ref("LiteralGrammar"), )
class SelectClauseSegment(ansi.SelectClauseSegment): """Overriding SelectClauseSegment to allow for additional segment parsing.""" match_grammar = ansi.SelectClauseSegment.match_grammar.copy() match_grammar.terminator = match_grammar.terminator.copy( # type: ignore insert=[ Sequence("CLUSTER", "BY"), Sequence("DISTRIBUTE", "BY"), Sequence("SORT", "BY"), ], before=Ref.keyword("LIMIT"), ) parse_grammar = ansi.SelectClauseSegment.parse_grammar.copy()
class SelectClauseModifierSegment(BaseSegment): """Things that come after SELECT but before the columns.""" type = "select_clause_modifier" match_grammar = Sequence( OneOf("DISTINCT", "ALL", "DISTINCTROW", optional=True), Ref.keyword("HIGH_PRIORITY", optional=True), Ref.keyword("STRAIGHT_JOIN", optional=True), Ref.keyword("SQL_SMALL_RESULT", optional=True), Ref.keyword("SQL_BIG_RESULT", optional=True), Ref.keyword("SQL_BUFFER_RESULT", optional=True), Ref.keyword("SQL_CACHE", optional=True), Ref.keyword("SQL_NO_CACHE", optional=True), Ref.keyword("SQL_CALC_FOUND_ROWS", optional=True), optional=True, )
class CreateStatementCommentSegment(BaseSegment): """A comment in a create view/table statement. e.g. comment = 'a new view/table' Please note that, for column comment, the syntax in Snowflake is `COMMENT 'text'` (Without the `=`). """ type = "snowflake_comment" match_grammar = Sequence( Ref.keyword("COMMENT"), Ref("EqualsSegment"), Ref("LiteralGrammar"), )
class ResignalSegment(BaseSegment): """This is the body of a `RESIGNAL` statement. https://dev.mysql.com/doc/refman/8.0/en/resignal.html """ type = "resignal_segment" match_grammar = Sequence( OneOf("SIGNAL", "RESIGNAL"), OneOf( Sequence( "SQLSTATE", Ref.keyword("VALUE", optional=True), Ref("QuotedLiteralSegment"), ), Ref("NakedIdentifierSegment"), optional=True, ), Sequence( "SET", Delimited( Sequence( OneOf( "CLASS_ORIGIN", "SUBCLASS_ORIGIN", "RETURNED_SQLSTATE", "MESSAGE_TEXT", "MYSQL_ERRNO", "CONSTRAINT_CATALOG", "CONSTRAINT_SCHEMA", "CONSTRAINT_NAME", "CATALOG_NAME", "SCHEMA_NAME", "TABLE_NAME", "COLUMN_NAME", "CURSOR_NAME", ), Ref("EqualsSegment"), OneOf( Ref("SessionVariableNameSegment"), Ref("LocalVariableNameSegment"), Ref("QuotedLiteralSegment"), ), ), ), optional=True, ), )
class CreateUserStatementSegment(BaseSegment): """`CREATE USER` statement. https://docs.aws.amazon.com/redshift/latest/dg/r_CREATE_USER.html """ type = "create_user" match_grammar = Sequence( "CREATE", "USER", Ref("ObjectReferenceSegment"), Ref.keyword("WITH", optional=True), "PASSWORD", OneOf(Ref("QuotedLiteralSegment"), "DISABLE"), AnySetOf( OneOf( "CREATEDB", "NOCREATEDB", ), OneOf( "CREATEUSER", "NOCREATEUSER", ), Sequence( "SYSLOG", "ACCESS", OneOf( "RESTRICTED", "UNRESTRICTED", ), ), Sequence("IN", "GROUP", Delimited(Ref("ObjectReferenceSegment"))), Sequence("VALID", "UNTIL", Ref("QuotedLiteralSegment")), Sequence( "CONNECTION", "LIMIT", OneOf( Ref("NumericLiteralSegment"), "UNLIMITED", ), ), Sequence( "SESSION", "TIMEOUT", Ref("NumericLiteralSegment"), ), ), )
class ExplainStatementSegment( ansi_dialect.get_segment("ExplainStatementSegment")): # type: ignore """An `Explain` statement. EXPLAIN [ ( option [, ...] ) ] statement EXPLAIN [ ANALYZE ] [ VERBOSE ] statement https://www.postgresql.org/docs/9.1/sql-explain.html """ parse_grammar = Sequence( "EXPLAIN", OneOf( Sequence( Ref.keyword("ANALYZE", optional=True), Ref.keyword("VERBOSE", optional=True), ), Bracketed( Delimited(Ref("ExplainOptionSegment"), delimiter=Ref("CommaSegment"))), optional=True, ), ansi_dialect.get_segment("ExplainStatementSegment").explainable_stmt, )