class SelectPartitionClauseSegment(BaseSegment): """This is the body of a partition clause.""" type = "partition_clause" match_grammar = Sequence( "PARTITION", Bracketed(Delimited(Ref("ObjectReferenceSegment"))), )
class CreateTableStatementSegment( ansi_dialect.get_segment("CreateTableStatementSegment") # type: ignore ): """Create table segment. https://dev.mysql.com/doc/refman/8.0/en/create-table.html """ match_grammar = ansi_dialect.get_segment( "CreateTableStatementSegment").match_grammar.copy(insert=[ AnyNumberOf( Sequence( Ref.keyword("DEFAULT", optional=True), Ref("ParameterNameSegment"), Ref("EqualsSegment"), OneOf(Ref("LiteralGrammar"), Ref("ParameterNameSegment")), ), ), ], )
class FromUnpivotExpressionSegment(BaseSegment): """An UNPIVOT expression.""" type = "from_unpivot_expression" match_grammar = Sequence("UNPIVOT", Bracketed(Anything())) parse_grammar = Sequence( "UNPIVOT", Bracketed( Ref("SingleIdentifierGrammar"), "FOR", Ref("SingleIdentifierGrammar"), "IN", Bracketed( Delimited(Ref("SingleIdentifierGrammar"), delimiter=Ref("CommaSegment")) ), ), )
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 PrimitiveTypeSegment(BaseSegment): """Primitive data types. Since DDL is based on Hive and DML based on Prestodb this class has primitives that may not work on specific situations - Hive: https://cwiki.apache.org/confluence/display/hive/languagemanual+types - PrestoDb: https://prestodb.io/docs/0.217/language/types.html """ type = "primitive_type" match_grammar = OneOf( "BOOLEAN", "TINYINT", "SMALLINT", "INTEGER", "INT", "BIGINT", "REAL", "FLOAT", Sequence("DOUBLE", Ref.keyword("PRECISION", optional=True)), Sequence( "DECIMAL", Bracketed( Ref("NumericLiteralSegment"), Ref("CommaSegment"), Ref("NumericLiteralSegment"), optional=True, ), ), "NUMERIC", "STRING", "VARCHAR", "CHAR", "VARBINARY", "JSON", "DATE", "TIMESTAMP", "INTERVAL", "TIME", "IPADDRESS", "HyperLogLog", "P4HyperLogLog", "QDigest", )
class PartitionedBySegment(BaseSegment): """Partitioned By Segment. As specified in https://docs.aws.amazon.com/redshift/latest/dg/r_CREATE_EXTERNAL_TABLE.html """ type = "partitioned_by_segment" match_grammar = Sequence( Ref.keyword("PARTITIONED"), "BY", Bracketed( Delimited( Sequence( Ref("ColumnReferenceSegment"), Ref("DatatypeSegment"), ), ), ), )
class IndexHintClauseSegment(BaseSegment): """This is the body of an index hint clause.""" type = "index_hint_clause" match_grammar = Sequence( OneOf("USE", "IGNORE", "FORCE"), OneOf("INDEX", "KEY"), Sequence( "FOR", OneOf("JOIN", Sequence("ORDER", "BY"), Sequence("GROUP", "BY"), optional=True), optional=True, ), Bracketed(Ref("ObjectReferenceSegment")), Ref("JoinOnConditionSegment", optional=True), )
class ReplaceClauseSegment(BaseSegment): """SELECT REPLACE clause.""" type = "select_replace_clause" match_grammar = Sequence( "REPLACE", OneOf( # Multiple replace in brackets Bracketed( Delimited( # Not *really* a select target element. It behaves exactly # the same way however. Ref("SelectClauseElementSegment"), delimiter=Ref("CommaSegment"), )), # Single replace not in brackets. Ref("SelectClauseElementSegment"), ), )
class CreateProcedureStatementSegment(BaseSegment): """A `CREATE PROCEDURE` statement. https://www.postgresql.org/docs/14/sql-createprocedure.html TODO: Just a basic statement for now, without full syntax. based on CreateFunctionStatementSegment without a return type. """ type = "create_procedure_statement" match_grammar = Sequence( "CREATE", Sequence("OR", "REPLACE", optional=True), "PROCEDURE", Ref("FunctionNameSegment"), Ref("ProcedureParameterListSegment"), Ref("FunctionDefinitionGrammar"), )
class CreateGroupStatementSegment(BaseSegment): """`CREATE GROUP` statement. https://docs.aws.amazon.com/redshift/latest/dg/r_CREATE_GROUP.html """ type = "create_group" match_grammar = Sequence( "CREATE", "GROUP", Ref("ObjectReferenceSegment"), Sequence( Ref.keyword("WITH", optional=True), "USER", Delimited(Ref("ObjectReferenceSegment"), ), optional=True, ), )
class SamplingExpressionSegment(BaseSegment): """A sampling expression. https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax#tablesample_operator """ type = "sample_expression" match_grammar = Sequence( "TABLESAMPLE", "SYSTEM", Bracketed(Ref("NumericLiteralSegment"), "PERCENT") )
class SelectStatementSegment(BaseSegment): """A `SELECT` statement. https://dev.mysql.com/doc/refman/5.7/en/select.html """ type = "select_statement" match_grammar = ansi_dialect.get_segment( "SelectStatementSegment" ).match_grammar.copy() # Inherit most of the parse grammar from the original. parse_grammar = UnorderedSelectStatementSegment.parse_grammar.copy( insert=[ Ref("OrderByClauseSegment", optional=True), Ref("LimitClauseSegment", optional=True), Ref("NamedWindowSegment", optional=True), ] )
class SelectClauseSegment(BaseSegment): """A group of elements in a select target statement.""" type = "select_clause" match_grammar = ansi_dialect.get_segment( "SelectClauseSegment").match_grammar.copy() match_grammar.terminator = match_grammar.terminator.copy( insert=[Ref("IntoKeywordSegment")]) parse_grammar = ansi_dialect.get_segment( "SelectClauseSegment").parse_grammar.copy()
class StatementSegment(ansi_dialect.get_segment("StatementSegment")): # type: ignore """A generic segment, to any of its child subsegments.""" parse_grammar = ansi_dialect.get_segment("StatementSegment").parse_grammar.copy( insert=[ Ref("UseStatementSegment"), Ref("CreateStatementSegment"), Ref("CreateCloneStatementSegment"), Ref("ShowStatementSegment"), Ref("AlterUserSegment"), ], remove=[ Ref("CreateTypeStatementSegment"), Ref("CreateExtensionStatementSegment"), Ref("CreateIndexStatementSegment"), Ref("DropIndexStatementSegment"), Ref("CreateFunctionStatementSegment"), ], )
class NextValueSequenceSegment(BaseSegment): """Segment to get next value from a sequence.""" type = "sequence_next_value" match_grammar = Sequence( "NEXT", "VALUE", "FOR", Ref("ObjectReferenceSegment"), )
class CreateSchemaStatementSegment(BaseSegment): """A `CREATE SCHEMA` statement. https://docs.aws.amazon.com/redshift/latest/dg/r_CREATE_SCHEMA.html TODO: support optional SCHEMA_ELEMENT """ type = "create_schema_statement" match_grammar = Sequence( "CREATE", "SCHEMA", OneOf( Sequence( Ref("IfNotExistsGrammar", optional=True), Ref("SchemaReferenceSegment"), Sequence( "AUTHORIZATION", Ref("ObjectReferenceSegment"), optional=True, ), ), Sequence( "AUTHORIZATION", Ref("ObjectReferenceSegment"), ), ), Sequence( "QUOTA", OneOf( Sequence( Ref("NumericLiteralSegment"), OneOf( "MB", "GB", "TB", ), ), "UNLIMITED", ), optional=True, ), )
class AlterDatabaseStatementSegment(BaseSegment): """An `ALTER DATABASE/SCHEMA` statement.""" type = "alter_database_statement" match_grammar = Sequence( "ALTER", OneOf("DATABASE", "SCHEMA"), Ref("DatabaseReferenceSegment"), "SET", OneOf( Sequence("DBPROPERTIES", Ref("BracketedPropertyListGrammar")), Sequence( "OWNER", OneOf("USER", "ROLE"), Ref("QuotedLiteralSegment"), ), Ref("LocationGrammar"), Sequence("MANAGEDLOCATION", Ref("QuotedLiteralSegment")), ), )
class DatabaseStatementSegment(BaseSegment): """A `DATABASE` statement. https://docs.teradata.com/r/Teradata-Database-SQL-Data-Definition-Language-Syntax-and-Examples/December-2015/Database-Statements/DATABASE """ type = "database_statement" match_grammar: Matchable = Sequence( "DATABASE", Ref("DatabaseReferenceSegment"), )
class SelectClauseSegment(ansi.SelectClauseSegment): """A group of elements in a select target statement. Remove OVERLAPS as a terminator as this can be part of SelectClauseModifierSegment """ match_grammar = StartsWith( Sequence( OneOf("SELECT", "SEL"), Ref("WildcardExpressionSegment", optional=True) ), terminator=OneOf( "FROM", "WHERE", Sequence("ORDER", "BY"), "LIMIT", Ref("SetOperatorSegment"), ), enforce_whitespace_preceding_terminator=True, ) parse_grammar = ansi.SelectClauseSegment.parse_grammar
class ExecuteFileSegment(BaseSegment): """A reference to an indextype.""" type = "execute_file_statement" match_grammar = Sequence( OneOf( Sequence( Ref("AtSignSegment"), Ref("AtSignSegment", optional=True), ), "START", ), # Probably should have a better file definition but this will do for now AnyNumberOf( Ref("SingleIdentifierGrammar"), Ref("DotSegment"), Ref("DivideSegment"), ), )
class TableDistributionClause(BaseSegment): """`CREATE TABLE` distribution clause. This is specific to Azure Synapse Analytics. """ type = "table_distribution_clause" match_grammar = Sequence( "DISTRIBUTION", Ref("EqualsSegment"), OneOf( "REPLICATE", "ROUND_ROBIN", Sequence( "HASH", Bracketed(Ref("ColumnReferenceSegment")), ), ), )
class DeclareStatementSegment(BaseSegment): """Declaration of a variable. https://cloud.google.com/bigquery/docs/reference/standard-sql/scripting#declare """ type = "declare_segment" match_grammar = StartsWith("DECLARE") parse_grammar = Sequence( "DECLARE", Delimited(Ref("NakedIdentifierSegment")), OneOf( Ref("DatatypeSegment"), Ref("DefaultDeclareOptionsGrammar"), Sequence( Ref("DatatypeSegment"), Ref("DefaultDeclareOptionsGrammar"), ), ), )
class TableExpressionSegment(BaseSegment): """Main table expression e.g. within a FROM clause, with hyphen support.""" type = "table_expression" match_grammar = ansi_dialect.get_segment( "TableExpressionSegment" ).match_grammar.copy( insert=[ Ref("HyphenatedObjectReferenceSegment"), ] )
class FromUnpivotExpressionSegment(BaseSegment): """An UNPIVOT expression. https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax#unpivot_operator """ type = "from_unpivot_expression" match_grammar = Sequence( "UNPIVOT", Sequence( OneOf("INCLUDE", "EXCLUDE"), "NULLS", optional=True, ), OneOf( Bracketed( Ref("SingleIdentifierGrammar"), "FOR", Ref("SingleIdentifierGrammar"), "IN", Bracketed( Delimited(Ref("SingleIdentifierGrammar")), Ref("UnpivotAliasExpressionSegment", optional=True), ), ), Bracketed( Bracketed( Delimited( Ref("SingleIdentifierGrammar"), min_delimiters=1, ), ), "FOR", Ref("SingleIdentifierGrammar"), "IN", Bracketed( Delimited( Sequence( Bracketed( Delimited( Ref("SingleIdentifierGrammar"), min_delimiters=1, ), ), Ref("UnpivotAliasExpressionSegment", optional=True), ), ), ), ), ), )
class AlterSessionStatementSegment(BaseSegment): """Snowflake's ALTER SESSION statement. ``` ALTER SESSION SET <param_name> = <param_value>; ALTER SESSION UNSET <param_name>, [ , <param_name> , ... ]; ``` https://docs.snowflake.com/en/sql-reference/sql/alter-session.html """ type = "alter_session_statement" match_grammar = Sequence( "ALTER", "SESSION", OneOf( Ref("AlterSessionSetClauseSegment"), Ref("AlterSessionUnsetClauseSegment"), ), )
class CloseStatementSegment(BaseSegment): """A `CLOSE` statement. As specified in https://docs.aws.amazon.com/redshift/latest/dg/close.html """ type = "close_statement" match_grammar = Sequence( "CLOSE", Ref("ObjectReferenceSegment"), )
class DropDatashareStatementSegment(BaseSegment): """A `DROP DATASHARE` statement. https://docs.aws.amazon.com/redshift/latest/dg/r_DROP_DATASHARE.html """ type = "drop_datashare_statement" match_grammar = Sequence( "DROP", "DATASHARE", Ref("ObjectReferenceSegment"), )
class TypelessStructSegment(BaseSegment): """Expression to construct a STRUCT with implicit types. https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#typeless_struct_syntax """ type = "typeless_struct" match_grammar = Sequence( "STRUCT", Bracketed( Delimited( AnyNumberOf( Sequence( Ref("BaseExpressionElementGrammar"), Ref("AliasExpressionSegment", optional=True), ), ), delimiter=Ref("CommaSegment"), ), optional=True, ), )
class IterateStatementSegment(BaseSegment): """A `ITERATE` statement. https://dev.mysql.com/doc/refman/8.0/en/iterate.html """ type = "iterate_statement" match_grammar = Sequence( "ITERATE", Ref("SingleIdentifierGrammar"), )
class DatatypeSegment(ansi.DatatypeSegment): """A data type segment. DATE FORMAT 'YYYY-MM-DD' """ match_grammar = Sequence( Ref("DatatypeIdentifierSegment"), Bracketed( OneOf( Delimited(Ref("ExpressionSegment")), # The brackets might be empty for some cases... optional=True, ), # There may be no brackets for some data types optional=True, ), Sequence( # FORMAT 'YYYY-MM-DD', "FORMAT", Ref("QuotedLiteralSegment"), optional=True ), )