Multi-Granular Demonstration Retrieval via Syntax-Derived Representations for Few-Shot NL2GQL
Abstract
Translating natural language into graph query languages (NL2GQL) enables non-expert users to access graph databases, but supervised parsers depend on large annotated corpora and costly retraining whenever the schema evolves. Few-shot in-context learning offers a training-free alternative, yet its effectiveness hinges on demonstration selection, and conventional retrieval based on sentence-level matching and a single similarity signal often overlooks beneficial exemplars. We propose MGDR-SDR, a training-free approach that applies constituency parsing to derive three complementary representations of the utterance: a shallow abstraction preserving fine-grained lexical and syntactic detail, a deep abstraction exposing the intent-level syntactic skeleton, and a noun phrase list capturing concrete graph elements. These representations drive a multi-granular retrieval procedure that aggregates evidence across dense and sparse channels, yielding broader and more precise demonstration coverage. We evaluate MGDR-SDR on SPARQL generation over KQA Pro and Cypher generation over ZOGRASCOPE. Without any training, it reaches 86.58% Hits@1 on KQA Pro, surpassing strong supervised parsers and setting a new state-of-the-art among few-shot methods, and it transfers across GQL formalisms, attaining the best iid accuracy and a balanced generalization profile that surpasses all open-source few-shot baselines on ZOGRASCOPE. These results show that syntax-derived, multi-granular retrieval is an effective and generalizable strategy for few-shot NL2GQL semantic parsing.