Skip to content

DATEPART Function fails if you use text('YEAR') instead of 'YEAR' #177

Not planned

Description

@narquette

Overview

When I attempt to use a datepart function with text('YEAR'), the compile code for the databricks dialect throws and error (see sample code below as it is trying to treat the text part as a quoted identifier common to databricks `.

Environment Information

OS = Windows 2022 Server
Python Version = 3.10.2
Python Package Versions

  • sqlalchemy=1.4.48
  • databricks-sql-connector=2.5.2

Code Sample

from configparser import ConfigParser
from pathlib import Path

from sqlalchemy import create_engine, Identity
from sqlalchemy.orm import Session
from sqlalchemy.engine import URL
from sqlalchemy.schema import Table, Column, MetaData, CreateTable
from sqlalchemy.sql.sqltypes import BIGINT, VARCHAR, DATE
from sqlalchemy.sql.expression import func, and_, or_, text, distinct, insert, union_all


config_path = Path.home() / '.databrickscfg'
config = ConfigParser()
config.read(config_path)

token = config['DEFAULT']['token']

url_info = {
  'drivername': 'databricks',
  'username': 'token',
  'password': token,
  'host': config['DEFAULT']['host'].replace('https://', '').replace('/', ''),
  'port': 43,
  'database': 'curv'
}

catalog = config['DEFAULT']['catalog']
http_path = "/sql/1.0/warehouses/3c0fbf823204df89"

url = URL.create(
  **{key: value for key, value in url_info.items()},
  query={'http_path': http_path, 'catalog': catalog}
)

engine = create_engine(url=url,
                       pool_pre_ping=1)

session = Session(engine)

table_date = Table(
  'test_date',
  MetaData(),
  Column('date_field', DATE, nullable=False)
)

table_date.create(bind=engine)

table_date.insert().values(date_field='2019-02-12')

query = session.query(func.DATEPART(text('YEAR'), table_date.c['date_field']).label('date_field'))

session.execute(query)

Activity

  1. narquette commented on Jul 11, 2023

    @narquette
    Author

    I'm sure that it will fail for other datepart calls as well such as text('MONTH'), text('DAY')

  2. susodapop commented on Jul 11, 2023

    @susodapop
    Contributor

    I'll review this in detail tomorrow but just wanted to throw out: I love that you're reading .databrickscfg in your repro scripts. We have an open feature request to build this support directly into the connector #148. It's good to see another usage of .databrickscfg in the wild.

  3. narquette commented on Jul 13, 2023

    @narquette
    Author

    Did you get a chance to review it?

  4. susodapop commented on Jul 13, 2023

    @susodapop
    Contributor

    Negative. Focused instead on pushing the next release out this week.

  5. added
    bug
    on Apr 17, 2024
  6. cathleeny commented on Dec 3, 2026

    @cathleeny
    Collaborator

    This one isn't a bug. text('YEAR') inserts raw SQL — the word YEAR without quotes — which Databricks reads as a column name. date_part needs a string literal for the unit, so use a bound string instead:

    func.date_part('YEAR', col)   # or: text("'YEAR'")

    Reference: https://docs.databricks.com/aws/en/sql/language-manual/functions/date_part

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions