duckdb-in-action / examples

https://www.manning.com/books/duckdb-in-action
Apache License 2.0
85 stars 15 forks source link

Chapter 8, dbt - strptime issue #2

Open leminkhoa opened 4 months ago

leminkhoa commented 4 months ago

In chapter 8, dbt section, the file models/atp/matches.sql has errors when compiling sql query. image

This will cause error

Runtime Error in model matches (models/atp/matches.sql)
  Binder Error: No function matches the given name and argument types 'strptime(BIGINT, STRING_LITERAL)'. You might need to add explicit type casts.
        Candidate functions:
        strptime(VARCHAR, VARCHAR) -> TIMESTAMP
        strptime(VARCHAR, VARCHAR[]) -> TIMESTAMP

The issue happened for strptime function. I did check the documentation of duckdb and it requires the first argument would be of type varchar Reference: https://duckdb.org/docs/sql/functions/dateformat.html

The revised code would be as follows:

{{ config(
    materialized='external', 
    location='output/matches.parquet', 
    format='parquet'
) }} 

WITH noWinLoss AS (
    SELECT COLUMNS(col -> 
      NOT  regexp_matches(col, 'w_.*') AND
      NOT regexp_matches(col, 'l_.*')
    ) 
    FROM {{ source('github', 'matches_file') }}  
)

SELECT * REPLACE (
    cast(strptime(cast(tourney_date AS VARCHAR), '%Y%m%d') AS date) as tourney_date
)
FROM noWinLoss

image

leminkhoa commented 4 months ago

My library version:

michael-simons commented 2 days ago

@mneedham @jexp Care to have look?