Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

字面值

字面值是一個常數值表達式,每種資料型別都有特殊的語法規則。

數字

數字字面值可以包含數字字元以及句號、底線和字元 e

如果數字字面值包含點或字元 e,則將其視為浮點數(或稱為 float),否則將其視為整數。

Character e denotes “scientific notation”, where the number after e is the exponent in 10-base.

底線被忽略,所以它們可以放在任意位置,但建議將它們用作千位分隔符。

整數也可以使用十六進位、八進位或二進位表示法分別用這些前綴表示:0x0o0b

PRQL

from numbers
select {
    small = 1.000_000_1,
    big = 5_000_000,
    huge = 5e9,
    binary = 0b0011,
    hex = 0x80,
    octal = 0o777,
}

SQL

SELECT
  1.0000001 AS small,
  5000000 AS big,
  5000000000.0 AS huge,
  3 AS "binary",
  128 AS hex,
  511 AS octal
FROM
  numbers

字串

PRQL 支援字串字面值和多種其他字串格式。如需詳細資訊,請參閱字串頁面。

布林值

布林值可以用 truefalse 關鍵字表示。

空值

空值可以用 null 關鍵字表示。另請參閱 PRQL 如何處理空值的討論。

日期和時間

日期和時間字面值用字元 @ 表示,後面跟著編碼日期和時間的字串。

[!NOTE] PRQL’s notation is designed to be less verbose than SQL’s TIMESTAMP '2004-10-19 10:23:54' and more explicit than SQL’s implicit option that just uses a string '2004-10-19 10:23:54'.

日期

日期由 @{yyyy-mm-dd} 表示 — @ 後面跟著日期格式。

PRQL

from employees
derive age_at_year_end = (@2022-12-31 - dob)

SQL

SELECT
  *,
  DATE '2022-12-31' - dob AS age_at_year_end
FROM
  employees

時間

時間由 @{HH:mm:ss.SSS±Z} 表示,任何未提供的部分預設為零。這包括由 +HH:mm-HH:mmZ 表示的時區。這符合 ISO8601 時間格式。

PRQL

from orders
derive should_have_shipped_today = (order_time < @08:30)

SQL

SELECT
  *,
  order_time < TIME '08:30' AS should_have_shipped_today
FROM
  orders

時間戳記

““時間戳記由 @{yyyy-mm-ddTHH:mm:ss.SSS±Z} / @{date}T{time} 表示,任何未提供的時間部分被四捨五入為零,包括由 +HH:mm-HH:mmZ: 是可選的)表示的時區。這是 @ 後面跟著 ISO8601 日期時間格式,該格式使用 T 分隔日期和時間。

PRQL

from commits
derive first_prql_commit = @2020-01-01T13:19:55-08:00
derive first_prql_commit_utc = @2020-01-02T21:19:55Z

SQL

SELECT
  *,
  TIMESTAMP '2020-01-01T13:19:55-0800' AS first_prql_commit,
  TIMESTAMP '2020-01-02T21:19:55Z' AS first_prql_commit_utc
FROM
  commits

持續時間

持續時間由 {N}{periods} 表示,例如 2years10minutes,中間沒有空格。

[!NOTE] These aren’t the same as ISO8601, because we evaluated P3Y6M4DT12H30M5S to be difficult to understand, but we could support a simplified form if there’s demand for it. We don’t currently support compound expressions, for example 2years10months, but most DBs will allow 2years + 10months. Please raise an issue if this is inconvenient.

PRQL

from projects
derive first_check_in = start + 10days

SQL

SELECT
  *,
  "start" + INTERVAL 10 DAY AS first_check_in
FROM
  projects

範例

Here’s a larger list of date and time examples:

  • @20221231 無效 — 它必須包含完整的標點符號(-:),
  • @2022-12-31 是日期
  • @2022-12 or @2022 are invalid — SQL can’t express a month, only a date
  • @16:54:32.123456 是時間
  • @16:54:32@16:54@16 都是允許的,分別表示 @16:54:32.000000@16:54:00.000000@16:00:00.000000
  • @2022-12-31T16:54:32.123456 是沒有時區的時間戳記
  • @2022-12-31T16:54:32.123456Z 是 UTC 時區的時間戳記
  • @2022-12-31T16:54+02 是 UTC+2 時區的時間戳記
  • @2022-12-31T16:54+02:00@2022-12-31T16:54+02 是 UTC+2 時區的日期時間
  • @16:54+02 無效 — 時間總是本地的,所以它不能有時區
  • @2022-12-31+02 無效 — 日期總是本地的,所以它不能有時區

[!NOTE] Currently prqlc does not parse or validate any of the datetime strings and will pass them to the database engine without adjustment. This might be refined in the future to aid in compatibility across databases. We’ll always support the canonical ISO8601 format described above.

路線圖

日期時間(作為與時間戳記不同的資料型別)受某些資料庫支援(例如 MySql、BigQuery)。通過添加型別轉換,這些可以通過將時間戳記轉換為日期時間來表示:

derive pi_day = @2017-03-14T15:09:26.535898<datetime>

以下是我們可以添加的一些範例:

  • @2022-12-31T16:54<datetime> is datetime without timezone
  • @2022-12-31<datetime> is forbidden — datetime must specify time
  • @16:54<datetime> is forbidden — datetime must specify date