Skip to content

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

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

feat(databricks): support and, or function syntax#8065
fivetran-amrutabhimsenayachit wants to merge 2 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.

Comment thread sqlglot/parsers/databricks.py Outdated
COLON_IS_VARIANT_EXTRACT = True
COLON_CHAIN_IS_SINGLE_EXTRACT = False

FUNC_TOKENS = parser.Parser.FUNC_TOKENS | {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.

Why is this unioning the base parser's FUNC_TOKENS instead of Spark's?

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.

Moved this to SparkParser

Comment thread sqlglot/parsers/databricks.py Outdated
def _parse_interval_span(
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.

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.

Suggested change
# AND/OR in FUNC_TOKENS would be consumed as interval units; they never are.

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.

@github-actions

github-actions Bot commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

SQLGlot Integration Test Results

✅ All tests passed

Comparing:

  • this branch (sqlglot:fix_parser_databricks_AND_function @ sqlglot 493fe70)
  • baseline (main @ sqlglot a9c0bea)

By Dialect

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

Overall

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

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

Transitions:
2 fail -> pass

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

✅ All tests passed

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