เรียนรู้เกี่ยวกับชนิดข้อมูลและฟีเจอร์ของ Redshift

Introduction to Redshift

Jason Myers

Principal Architect

ชนิดข้อมูลพื้นฐาน

ชนิดตัวเลข

  • SMALLINT, INTEGER, BIGINT
  • DECIMAL / NUMERIC
  • DOUBLE PRECISION
  • REAL

ชนิดวันที่และเวลา

  • DATE
  • TIME, TIMETZ
  • TIMESTAMP, TIMESTAMPTZ

ชนิดอักขระ

  • CHAR
  • VARCHAR

ชนิดบูลีน

  • BOOLEAN
Introduction to Redshift

ชนิดข้อมูลพิเศษ

ชนิด SUPER

  • ข้อมูลกึ่งโครงสร้าง
    • Arrays
    • Tuples
    • โครงสร้างซ้อน (เช่น JSON)
  • ใช้ PartiQL ในการจัดการ
  • ขนาดสูงสุด 16MB

ชนิด VARBYTE

  • ข้อมูลไบนารี (BLOBs)
  • รูปภาพและวิดีโอ
Introduction to Redshift

ชนิดข้อมูล PostgreSQL ที่ไม่รองรับ:

ชนิดใน PostgreSQL ชนิดใน Redshift
DATETIME TIMESTAMP, TIMESTAMPTZ
SERIAL INTEGER, BIGINT
UUID VARCHAR
JSON SUPER, VARCHAR
ARRAY SUPER, VARCHAR
BIT BOOLEAN, SMALLINT, VARCHAR
Introduction to Redshift

ดูคอลัมน์และชนิดข้อมูล

-- View the column details
SELECT column_name, 
       data_type, 
       character_maximum_length AS character_max_len,
       numeric_precision, 
       numeric_scale
  -- Using a view with both internal and external schema's columns
  FROM SVV_ALL_COLUMNS 
 -- Only in the spectrumdb schema
 WHERE schema_name = 'spectrumdb'
   -- For the ecommerce_sales table
   AND table_name = 'ecommerce_sales';
Introduction to Redshift

ดูคอลัมน์และชนิดข้อมูล (ต่อ)

column_name                  | data_type         | character_max_len | numeric_precision | numeric_scale
=============================|===================|===================|===================|==============
year_qtr                     | character varying | 100               | null              | null
total_sales                  | integer           | null              | 32                | 0
ecom_sales                   | integer           | null              | 32                | 0
percent_ecom                 | real              | null              | null              | null
percent_change_quarter_total | real              | null              | null              | null
percent_change_quarter_ecom  | real              | null              | null              | null
percent_change_year_total    | real              | null              | null              | null
percent_change_year_ecom     | real              | null              | null              | null
Introduction to Redshift

"ความเข้ากันได้" ของชนิดข้อมูล

  • การแปลงชนิดข้อมูลโดยอัตโนมัติ: การกำหนดค่า, การเปรียบเทียบ
  • ควรตรวจสอบผลลัพธ์ให้แน่ใจทุกครั้ง!
จากชนิด เป็นชนิด
CHAR VARCHAR
DATE CHAR, VARCHAR, TIMESTAMP, TIMESTAMPTZ
TIMESTAMP CHAR, DATE, VARCHAR, TIMESTAMPTZ
BIGINT BOOLEAN, CHAR, DECIMAL, DOUBLE PRECISION, INTEGER, REAL, SMALLINT, VARCHAR
DECIMAL BIGINT, CHAR, DOUBLE PRECISION, INTEGER, REAL, SMALLINT, VARCHAR
1 https://docs.aws.amazon.com/redshift/latest/dg/c_Supported_data_types.html#r_Type_conversion
Introduction to Redshift

การแปลงชนิดข้อมูลแบบกำหนดเอง

  • CAST
-- Casting a decimal to a integer and aliasing it
SELECT CAST(2.00 AS INTEGER) AS our_int;
our_int
========
2
(1 row)
Introduction to Redshift

ฟังก์ชัน TO

  • TO_CHAR, TO_DATE, TO_NUMBER
-- Convert the string to a date
SELECT CAST('14-01-2024 02:36:48' AS DATE) AS out_date;
date/time field value out of range: "14-01-2024 02:36:48"
HINT:  Perhaps you need a different "datestyle" setting.
-- Parse the string to a date
SELECT TO_DATE('14-01-2024 02:36:48', 'DD-MM-YYYY') AS our_date;
our_date
========
2024-01-14
(1 row)
Introduction to Redshift

ฟังก์ชัน TO (ต่อ)

-- Get the name of the month in a date
SELECT TO_CHAR(date '2024-01-14', 'MONTH') AS month_name;
month_name
========
JANUARY
(1 row)
  • สตริงรูปแบบวันที่เวลาและตัวเลข
1 https://docs.aws.amazon.com/redshift/latest/dg/r_FORMAT_strings.html
Introduction to Redshift

มาฝึกกันเถอะ!

Introduction to Redshift

Preparing Video For Download...