Skip to content

DATEADD/DATEDIFF/DATEPART/DATENAME first argument parsed as ColumnReferenceExpression instead of datepart keyword #196

@wickedmachinator

Description

@wickedmachinator

When using datepart functions like DATEADD(month, -3, GETDATE()), the TSql170Parser parses the first argument month as a ColumnReferenceExpression with ColumnType.Regular instead of recognizing it as a datepart keyword.

REPRO

var parser = new TSql170Parser(initialQuotedIdentifiers: true);
  using var reader = new StringReader("SELECT * FROM t WHERE d >= DATEADD(month, -3, GETDATE())");
  var fragment = parser.Parse(reader, out var errors);

  // No parse errors
  Debug.Assert(errors.Count == 0);

  // Walk the AST — "month" appears as ColumnReferenceExpression
  var visitor = new MyVisitor();
  fragment.Accept(visitor);
  // visitor finds ColumnReferenceExpression { ColumnType = Regular, MultiPartIdentifier = ["month"] }
  // for the datepart argument of DATEADD

Expected behavior:

The first argument of DATEADD, DATEDIFF, DATEDIFF_BIG, DATEPART, and DATENAME should not produce a ColumnReferenceExpression when it's a
datepart keyword (year, month, day, quarter, week, hour, minute, second, etc.). These are T-SQL datepart specifiers, not column references.

Affected functions: DATEADD, DATEDIFF, DATEDIFF_BIG, DATEPART, DATENAME

Affected keywords: year/yy/yyyy, quarter/qq/q, month/mm/m, day/dd/d, week/wk/ww, weekday/dw, dayofyear/dy/y, hour/hh, minute/mi/n,
second/ss/s, millisecond/ms, microsecond/mcs, nanosecond/ns, iso_week/isowk/isoww

Impact: Any tool using TSqlFragmentVisitor to collect column references (e.g., column lineage analysis, dependency detection) gets false
positives for datepart keywords, producing incorrect "unresolved column" results.

Version: 170.191.0

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions