Repository navigation
Conversation
Replace viewName with functionName in SparkSqlParser.g4 lateralView rule so generator functions in LATERAL VIEW clauses are no longer collected as view entities by getAllEntities, and code completion and syntax error messages expect a function rather than a view. Co-authored-by: Cursor <cursoragent@cursor.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题分析
在 Spark SQL 语法规范中,
LATERAL VIEW [OUTER] generator_function(expression, ...)后接的是表生成函数(UDTF,如EXPLODE、POSEXPLODE等),而非视图(View)。623a004cd)时,该位置定义为qualifiedName。在 PR #179(4d1dfa676)支持 Spark SQL 自动补全时将其改为了viewName。getAllEntities):SparkEntityCollector.exitViewName会将所有viewName收集为EntityContextType.VIEW,导致LATERAL VIEW EXPLODE(...)中的函数名EXPLODE被误收集为视图实体。getSuggestionAtCaretPosition/validate):SparkSQL与SparkErrorListener将RULE_viewName映射为视图,导致在LATERAL VIEW |处触发补全时返回EntityContextType.VIEW(应为EntityContextType.FUNCTION),未完成语句报错提示expecting an existing view(应为expecting an existing function)。viewName(viewIdentifier)最多仅允许两段式名称(db.view),导致LATERAL VIEW无法支持三段式函数名(catalog.schema.func)及IDENTIFIER('func')动态函数调用。改动方案
1. 语法规则修改
将
src/grammar/spark/SparkSqlParser.g4中lateralView规则里的viewName替换为functionName,并重新生成src/lib/spark下的 Parser 代码。2. 为什么采用此方案
全仓支持
LATERAL VIEW语法的方言仅有 Spark 与 Hive。在HiveSqlParser.g4中,lateralView规则使用的是function_(内部对应functionNameForInvoke)。在评估 Spark 的修复方案时对比了以下三种路径,最终选择替换为functionName:qualifiedName(不采用)getAllEntities将生成函数误收集为view的问题,但RULE_qualifiedName在本仓库中属于底层通用标识符规则,不能直接加入SparkSQL.preferredRules(否则会污染列、表、分区等其他引用qualifiedName的位置)。若使用qualifiedName,会导致LATERAL VIEW |处丢失EntityContextType.FUNCTION语法补全,且语法报错退化为仅提示expecting a keyword,无法与 Hive 方言行为对齐。SparkEntityCollector.exitViewName中排除lateralView父节点(不采用)getAllEntities的误报,无法修复代码补全仍提示VIEW、语法报错仍提示expecting an existing view以及viewName不支持三段式函数名和IDENTIFIER(...)的问题,属于治标不治本。functionName(采用)SparkSqlParser.g4中,同为表值函数调用的atomFunctionTable(第 787 行)和普通标量函数调用(第 971 行)均使用functionName。将其统一为functionName既符合 UDTF 的真实语义,又能天然支持三段式限定名(qualifiedName)和IDENTIFIER(...)语法。SparkSQL.preferredRules、SparkSQL.processCandidates和SparkErrorListener已完整配置了RULE_functionName -> EntityContextType.FUNCTION / 'function'的映射;且SparkEntityCollector不监听普通调用态的functionName。SparkSQL与HiveSQL在处理LATERAL VIEW时的表现完全一致:getAllEntities):均不将LATERAL VIEW后的 UDTF 收集为view或其他额外实体,仅收集主查询的表与queryResult实体。getSuggestionAtCaretPosition):在LATERAL VIEW |及LATERAL VIEW OUTER |后均返回syntaxContextType: EntityContextType.FUNCTION。validate):未完成语句均提示Statement is incomplete, expecting an existing function or a keyword。影响范围及风险评估
src/grammar/spark、src/lib/spark)中的lateralView语法规则及其关联的实体收集、代码补全与语法报错提示。lateralView已使用function_,其余方言无lateralView规则)。SparkSqlParser.g4中的 1 行规则替换及对应的 ANTLR 生成产物,不涉及src/parser/common公共逻辑。LATERAL VIEW的实体收集、补全与报错行为与 Hive 方言完全一致;同时functionName完全超集兼容原有的单段与两段式标识符语法,不会破坏已有合法 SQL 的解析。