Skip to content

Commit

Permalink
Merge pull request #1913 from ellieapi/new-types-for-snowflake
Browse files Browse the repository at this point in the history
Added support for additional snowflake data types
  • Loading branch information
taozhi8833998 authored May 22, 2024
2 parents a6b7aa4 + 263be9a commit 33a0dc6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
21 changes: 16 additions & 5 deletions pegjs/snowflake.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -4327,22 +4327,28 @@ KW_END = "END"i !ident_start

KW_CAST = "CAST"i !ident_start { return 'CAST' }

KW_BINARY = "BINARY"i !ident_start { return 'BINARY'; }
KW_VARBINARY = "VARBINARY"i !ident_start { return 'VARBINARY'; }

KW_BOOL = "BOOL"i !ident_start { return 'BOOL'; }
KW_BOOLEAN = "BOOLEAN"i !ident_start { return 'BOOLEAN'; }
KW_CHAR = "CHAR"i !ident_start { return 'CHAR'; }
KW_CHARACTER = "CHARACTER"i !ident_start { return 'CHARACTER'; }
KW_VARCHAR = "VARCHAR"i !ident_start { return 'VARCHAR';}
KW_NUMBER = "NUMBER"i !ident_start { return 'NUMBER'; }
KW_NUMERIC = "NUMERIC"i !ident_start { return 'NUMERIC'; }
KW_DECIMAL = "DECIMAL"i !ident_start { return 'DECIMAL'; }
KW_STRING = "STRING"i !ident_start { return 'STRING'; }
KW_SIGNED = "SIGNED"i !ident_start { return 'SIGNED'; }
KW_UNSIGNED = "UNSIGNED"i !ident_start { return 'UNSIGNED'; }
KW_INT = "INT"i !ident_start { return 'INT'; }
KW_BYTEINT = "BYTEINT"i !ident_start { return 'BYTEINT'; }
KW_ZEROFILL = "ZEROFILL"i !ident_start { return 'ZEROFILL'; }
KW_INTEGER = "INTEGER"i !ident_start { return 'INTEGER'; }
KW_JSON = "JSON"i !ident_start { return 'JSON'; }
KW_JSONB = "JSONB"i !ident_start { return 'JSONB'; }
KW_GEOMETRY = "GEOMETRY"i !ident_start { return 'GEOMETRY'; }
KW_GEOGRAPHY = "GEOGRAPHY"i !ident_start { return 'GEOGRAPHY'; }
KW_SMALLINT = "SMALLINT"i !ident_start { return 'SMALLINT'; }
KW_SERIAL = "SERIAL"i !ident_start { return 'SERIAL'; }
KW_TINYINT = "TINYINT"i !ident_start { return 'TINYINT'; }
Expand All @@ -4361,6 +4367,7 @@ KW_DATETIME = "DATETIME"i !ident_start { return 'DATETIME'; }
KW_ROWS = "ROWS"i !ident_start { return 'ROWS'; }
KW_TIME = "TIME"i !ident_start { return 'TIME'; }
KW_TIMESTAMP= "TIMESTAMP"i!ident_start { return 'TIMESTAMP'; }
KW_TIMESTAMP_TZ = "TIMESTAMP_TZ"i !ident_start { return 'TIMESTAMP_TZ'; }
KW_TRUNCATE = "TRUNCATE"i !ident_start { return 'TRUNCATE'; }
KW_USER = "USER"i !ident_start { return 'USER'; }
KW_UUID = "UUID"i !ident_start { return 'UUID'; }
Expand Down Expand Up @@ -4716,6 +4723,7 @@ data_type
/ enum_type
/ serial_interval_type
/ binary_type
/ geography_type
/ oid_type


Expand All @@ -4733,7 +4741,7 @@ boolean_type
= t:(KW_BOOL / KW_BOOLEAN) { /* => data_type */ return { dataType: t }}

binary_type
= 'bytea'i { /* => data_type */ return { dataType: 'BYTEA' }; }
= t:(KW_BINARY / KW_VARBINARY) { /* => data_type */ return { dataType: t }; }

character_string_type
= t:(KW_CHAR / KW_VARCHAR) __ LPAREN __ l:[0-9]+ __ RPAREN {
Expand All @@ -4751,9 +4759,9 @@ numeric_type_suffix
return result
}
numeric_type
= t:(KW_NUMBER / KW_DECIMAL / KW_INT / KW_INTEGER / KW_SMALLINT / KW_TINYINT / KW_BIGINT / KW_FLOAT / KW_DOUBLE / KW_SERIAL / KW_BIGSERIAL / KW_REAL) __ LPAREN __ l:[0-9]+ __ r:(COMMA __ [0-9]+)? __ RPAREN __ s:numeric_type_suffix? { /* => data_type */ return { dataType: t, length: parseInt(l.join(''), 10), scale: r && parseInt(r[2].join(''), 10), parentheses: true, suffix: s }; }
/ t:(KW_NUMBER / KW_DECIMAL / KW_INT / KW_INTEGER / KW_SMALLINT / KW_TINYINT / KW_BIGINT / KW_FLOAT / KW_DOUBLE / KW_SERIAL / KW_BIGSERIAL / KW_REAL)l:[0-9]+ __ s:numeric_type_suffix? { /* => data_type */ return { dataType: t, length: parseInt(l.join(''), 10), suffix: s }; }
/ t:(KW_NUMBER / KW_DECIMAL / KW_INT / KW_INTEGER / KW_SMALLINT / KW_TINYINT / KW_BIGINT / KW_FLOAT / KW_DOUBLE / KW_SERIAL / KW_BIGSERIAL / KW_REAL) __ s:numeric_type_suffix? __{ /* => data_type */ return { dataType: t, suffix: s }; }
= t:(KW_NUMBER / KW_DECIMAL / KW_INT / KW_BYTEINT / KW_INTEGER / KW_NUMERIC / KW_SMALLINT / KW_TINYINT / KW_BIGINT / KW_FLOAT / KW_DOUBLE / KW_SERIAL / KW_BIGSERIAL / KW_REAL) __ LPAREN __ l:[0-9]+ __ r:(COMMA __ [0-9]+)? __ RPAREN __ s:numeric_type_suffix? { /* => data_type */ return { dataType: t, length: parseInt(l.join(''), 10), scale: r && parseInt(r[2].join(''), 10), parentheses: true, suffix: s }; }
/ t:(KW_NUMBER / KW_DECIMAL / KW_INT / KW_BYTEINT / KW_INTEGER / KW_NUMERIC / KW_SMALLINT / KW_TINYINT / KW_BIGINT / KW_FLOAT / KW_DOUBLE / KW_SERIAL / KW_BIGSERIAL / KW_REAL)l:[0-9]+ __ s:numeric_type_suffix? { /* => data_type */ return { dataType: t, length: parseInt(l.join(''), 10), suffix: s }; }
/ t:(KW_NUMBER / KW_DECIMAL / KW_INT / KW_BYTEINT / KW_INTEGER / KW_NUMERIC / KW_SMALLINT / KW_TINYINT / KW_BIGINT / KW_FLOAT / KW_DOUBLE / KW_SERIAL / KW_BIGSERIAL / KW_REAL) __ s:numeric_type_suffix? __{ /* => data_type */ return { dataType: t, suffix: s }; }

oid_type
= t:(KW_OID / KW_REGCLASS / KW_REGCOLLATION / KW_REGCONFIG / KW_REGDICTIONARY / KW_REGNAMESPACE / KW_REGOPER / KW_REGOPERATOR / KW_REGPROC / KW_REGPROCEDURE / KW_REGROLE / KW_REGTYPE) { /* => data_type */ return { dataType: t }}
Expand All @@ -4770,7 +4778,7 @@ time_type

datetime_type
= t:(KW_DATE / KW_DATETIME) __ LPAREN __ l:[0-9]+ __ RPAREN { /* => data_type */ return { dataType: t, length: parseInt(l.join(''), 10), parentheses: true }; }
/ t:(KW_DATE / KW_DATETIME) { /* => data_type */ return { dataType: t }; }
/ t:(KW_DATE / KW_DATETIME / KW_TIMESTAMP_TZ) { /* => data_type */ return { dataType: t }; }
/ time_type

enum_type
Expand All @@ -4789,6 +4797,9 @@ json_type
geometry_type
= t:KW_GEOMETRY {/* => data_type */ return { dataType: t }; }

geography_type
= t:KW_GEOGRAPHY {/* => data_type */ return { dataType: t }; }

serial_interval_type
= t:(KW_SERIAL / KW_INTERVAL) { /* => data_type */ return { dataType: t }; }

Expand Down
17 changes: 17 additions & 0 deletions test/snowflake.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,23 @@ describe('snowflake', () => {
`SELECT '1' || '_' || "tj"."ID" AS "JOB_KEY", "tj"."ID" AS "PAY_JOB_ID", "tj"."ID", CASE WHEN "tj"."transcription_job_split_id" IS NOT NULL THEN 1 ELSE 0 END AS "TRANSCRIPTION_IS_SPLIT", CASE WHEN "tjs"."transcription_job_id" IS NOT NULL THEN "tjs"."transcription_job_id" ELSE "tj"."id" END AS "PARENT_TRANSCRIPTION_JOB_ID", CASE WHEN "tjss"."transcription_job_id" IS NOT NULL THEN "tjss"."transcription_job_id" WHEN "tjss"."transcription_job_id" IS NULL AND "tjs"."transcription_job_id" IS NOT NULL THEN "tjs"."transcription_job_id" ELSE "tj"."id" END AS "TRANSCRIPTION_ORIGINAL_JOB_ID", CASE WHEN "tjss"."transcription_job_id" IS NOT NULL THEN "tjss"."transcription_job_id" WHEN "tjss"."transcription_job_id" IS NULL AND "tjs"."transcription_job_id" IS NOT NULL THEN "tjs"."transcription_job_id" ELSE "tj"."id" END AS "REV_JOB_ID", "tj"."catalog_item_id", PARSE_JSON("tj"."METADATA") AS "METADATA", PARSE_JSON("tj"."METADATA") :entry_id::STRING AS "ENTRY_ID", PARSE_JSON("tj"."METADATA") :partner_id::NUMBER AS "PARTNER_ID", PARSE_JSON("tj"."METADATA") :original_profile_id::NUMBER AS "ORIGINAL_PROFILE_ID", PARSE_JSON("tj"."config") :external_provider_captioning_provider::STRING AS "caption_provider", PARSE_JSON("tj"."config") :external_provider_transcription_provider::STRING AS "transcription_provider", "tj"."FRAUD_ISSUES", "tj"."OWNER_ID", "tj"."SANDBOX", coalesce("tj"."ORDER_ID", "tj"."ORDER_UUID") AS "ORDER_ID", coalesce("tj"."ORDER_ID", "tj"."ORDER_UUID") IS NOT NULL AS "ONE_ORDERING", "tj"."SOURCE_TABLE", coalesce("tj"."job_flow_id", "p"."job_flow_id") AS "JOB_FLOW_ID" FROM "MRR"."MRR_VERBIT_TRANSCRIPTION_JOBS" AS "tj" LEFT JOIN "MRR"."MRR_VERBIT_TRANSCRIPTION_JOB_SPLITS" AS "tjs" ON "tj"."transcription_job_split_id" = "tjs"."id" INNER JOIN "MRR"."MRR_VERBIT_PROFILES" AS "p" ON "tj"."profile_id" = "p"."id" LEFT JOIN "MRR"."MRR_VERBIT_TRANSCRIPTION_JOBS" AS "tjj" ON "tjs"."transcription_job_id" = "tjj"."id" LEFT JOIN "MRR"."MRR_VERBIT_TRANSCRIPTION_JOB_SPLITS" AS "tjss" ON "tjj"."transcription_job_split_id" = "tjss"."id"`
]
},
{
title: 'Support Snowflake types: NUMERIC, BYTEINT, BINARY, VARBINARY, GEOGRAPHY, TIMESTAMP_TZ',
sql: [
`
CREATE TABLE TEST_SNOWFLAKE (
"att1" NUMERIC(3,3),
"att2" BYTEINT,
"att3_SAMPLE_3" BINARY,
"att4_SAMPLE_4" VARBINARY,
"att5_SAMPLE_5" GEOGRAPHY,
"att6_SAMPLE_6" TIMESTAMP_TZ,
primary key ("att1")
);
`,
`CREATE TABLE "TEST_SNOWFLAKE" ("att1" NUMERIC(3, 3), "att2" BYTEINT, "att3_SAMPLE_3" BINARY, "att4_SAMPLE_4" VARBINARY, "att5_SAMPLE_5" GEOGRAPHY, "att6_SAMPLE_6" TIMESTAMP_TZ, PRIMARY KEY ("att1"))`
]
}
]
SQL_LIST.forEach(sqlInfo => {
const { title, sql } = sqlInfo
Expand Down

0 comments on commit 33a0dc6

Please sign in to comment.