Skip to content

feat(databricks): support and, or function syntax - #8065

Open
fivetran-amrutabhimsenayachit wants to merge 3 commits into
mainfrom
fix_parser_databricks_AND_function
Open

feat(databricks): support and, or function syntax#8065
fivetran-amrutabhimsenayachit wants to merge 3 commits into
mainfrom
fix_parser_databricks_AND_function

Conversation

@fivetran-amrutabhimsenayachit

Copy link
Copy Markdown
Collaborator

Databricks allows and(a, b) as a function, but SQLGlot's parser only knew AND as a keyword. Adding AND to the function-name whitelist fixed the parsing, and a small override prevented a side-effect where the interval parser mistook AND ( for a function call.

 python3 -c "import sqlglot; expr = sqlglot.parse_one('SELECT and(a IS NOT NULL, b IS NOT NULL) FROM t', dialect='databricks'); print(expr.sql('databricks'))"
SELECT NOT a IS NULL AND NOT b IS NULL FROM t

This comment was marked as resolved.

@georgesittas georgesittas changed the title feat(optimizer): fix databricks AND function parsing issue feat(databricks): support and, or function syntax Aug 7, 2026

@georgesittas georgesittas left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this only relevant to Databricks? What about other dialects in the Hive hierarchy?

Comment thread sqlglot/parsers/databricks.py Outdated
Comment on lines +20 to +21
"AND": lambda args: exp.And(this=seq_get(args, 0), expression=seq_get(args, 1)),
"OR": lambda args: exp.Or(this=seq_get(args, 0), expression=seq_get(args, 1)),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if you mix and() and or() w.r.t. operator precedence? Do we respect Databricks' semantics? Try the following:

and(or(true, false), false)
and(false, or (true, true))

You should also see how this affects other expressions that involve these function calls, such as:

and(false, false) = false
not and(true, false)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The and() or() function calls is supported by Spark as well apart from Databricks, but it is not supported by HIVE.py. So moving the fix to SparkParser.

@georgesittas georgesittas Aug 10, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fivetran-amrutabhimsenayachit did you test the last two cases? I don't think your PR handles them correctly. Similarly for:

or(true, false) and false
and(null, true) is null

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moreover, ClickHouse appears to have a similar problem because the logic is the same.

Comment thread sqlglot/parsers/databricks.py Outdated
Comment thread sqlglot/parsers/databricks.py Outdated
Comment thread sqlglot/parsers/databricks.py Outdated
self, this: exp.Expr, parse_function_unit: bool = True
) -> exp.Interval:
# AND/OR in FUNC_TOKENS would be consumed as interval units; they never are.
if self._curr and self._curr.token_type in (TokenType.AND, TokenType.OR):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these the only tokens that can follow an INTERVAL value and be incorrectly consumed as units? Are there any others? If yes, are the others handled correctly in main today?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it. I tried running the fusion schema tests and caught this failure.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this set is complete, for example this fails to parse in this PR:

SELECT INTERVAL '1 day' LIKE ('a')

The reason I commented on this is because it introduces specialized logic for dealing with a token subset and I don't think it scales well to solve the more general issue. Let's just revert this for now, I don't think we should mix this in.

@github-actions

github-actions Bot commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

SQLGlot Integration Test Results

❌ 2 regressions — see details below

Comparing:

  • this branch (sqlglot:fix_parser_databricks_AND_function @ sqlglot bab8531)
  • baseline (main @ sqlglot 7810755)

By Dialect

dialect main feature branch transitions links
databricks -> databricks 10021/11820 passed (84.8%) 10021/11820 passed (84.8%) 2 fail -> pass
2 pass -> fail
full result / delta

Overall

main: 182937 total, 160861 passed (pass rate: 87.9%)

sqlglot:fix_parser_databricks_AND_function: 170743 total, 149709 passed (pass rate: 87.7%)

Transitions:
2 fail -> pass
2 pass -> fail

Dialect pair changes: 0 previous results not found, 3 current results not found

❌ 2 regressions (view logs)

@fivetran-amrutabhimsenayachit
fivetran-amrutabhimsenayachit force-pushed the fix_parser_databricks_AND_function branch from b65725f to ed5415d Compare August 10, 2026 15:16
@fivetran-amrutabhimsenayachit
fivetran-amrutabhimsenayachit force-pushed the fix_parser_databricks_AND_function branch from ed5415d to 0240b59 Compare August 10, 2026 15:30
@fivetran-amrutabhimsenayachit
fivetran-amrutabhimsenayachit force-pushed the fix_parser_databricks_AND_function branch from 22a62d9 to ff0bb2c Compare August 11, 2026 00:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants