Skip to content

grain property only supports direct column references #4103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
plaflamme opened this issue Apr 9, 2025 · 3 comments
Open

grain property only supports direct column references #4103

plaflamme opened this issue Apr 9, 2025 · 3 comments
Labels
Bug Something isn't working

Comments

@plaflamme
Copy link
Contributor

We have a table that uses BigQuery's RANGE type like so:

MODEL(
  name foo,
  grain (id, RANGE_START(valid_date_range))
);
SELECT id::STRING, RANGE(valid_from, valid_to)::RANGE<DATE> AS valid_date_range

This works fine except when loading the ui which results in this kind of error:

Reference 'RANGE_START(valid_date_range)' must have an inferrable name or explicit alias.

Traceback (most recent call last):
  File ".venv/lib/python3.11/site-packages/web/server/settings.py", line 102, in get_loaded_context
    yield _get_loaded_context(settings.project_path, settings.config, settings.gateway)
sqlmesh.utils.errors.ConfigError: Reference 'RANGE_START(valid_date_range)' must have an inferrable name or explicit alias.

We do not see errors anywhere else when using RANGE_START(...) in the grain definition, only in the UI.

@izeigerman
Copy link
Member

@tobymao this looks like remnants of the metrics layer?

@izeigerman izeigerman added the Bug Something isn't working label Apr 11, 2025
@tobymao
Copy link
Contributor

tobymao commented Apr 11, 2025

grain is also used in table diff

@georgesittas
Copy link
Contributor

georgesittas commented May 7, 2025

Would it work to specify an alias in this case? E.g., this model doesn't have the issue @plaflamme mentioned:

MODEL(
  name foo,
  grain (id, RANGE_START(valid_date_range) AS valid_date_range_start)
);

WITH t AS (
  SELECT
    '1' AS id,
    DATE('2025-05-06') AS valid_from,
    DATE('2020-05-07') AS valid_to
)
SELECT
  id::STRING AS id,
  RANGE(valid_from, valid_to)::RANGE<DATE> AS valid_date_range
FROM t

I think expecting a valid output name and not an arbitrary expression may be intentional in this case, because as Toby said grains are, e.g., used in table diff where you want to reference it by name. Otherwise how would you specify the expression grain shown here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants