Skip to content

Commit d650c04

Browse files
mbasmanovameta-codesync[bot]
authored andcommitted
feat: Enable editing and recalling from history in CLI (#523)
Summary: Pull Request resolved: #523 Integrate CLI with Readline library to allow editing SQL while typing and recalling previously used SQL commands using up arrow. Readline library - https://tiswww.case.edu/php/chet/readline/rltop.html Part of #522 Reviewed By: kgpai, xiaoxmeng Differential Revision: D84913703 fbshipit-source-id: b24db159da4acc828e350e81d175d24d333e61fa
1 parent 6b76ae2 commit d650c04

File tree

4 files changed

+55
-14
lines changed

4 files changed

+55
-14
lines changed

.github/workflows/build_and_test.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ jobs:
4141
matrix:
4242
build_type: [debug, release]
4343
name: Build Axiom
44-
runs-on: 16-core-ubuntu
44+
runs-on: 32-core-ubuntu
4545
container:
4646
image: ghcr.io/facebookincubator/velox-dev:centos9
4747
env:
@@ -89,6 +89,10 @@ jobs:
8989
run: |
9090
ccache -sz
9191
92+
- name: Install Readline
93+
run: |
94+
dnf install -y readline-devel
95+
9296
- name: Build Axiom ${{ matrix.build_type }}
9397
env:
9498
MAKEFLAGS: NUM_THREADS=4 MAX_HIGH_MEM_JOBS=4 MAX_LINK_JOBS=2

CMakeLists.txt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,35 @@ velox_resolve_dependency(antlr4-runtime)
153153

154154
message(STATUS "Using ANTLR4 runtime from ${ANTLR4_INCLUDE_DIR}")
155155

156+
# Add Readline dependency
157+
find_path(
158+
READLINE_INCLUDE_DIR
159+
readline/readline.h
160+
HINTS ${READLINE_ROOT_DIR}
161+
PATH_SUFFIXES include
162+
REQUIRED
163+
)
164+
find_library(READLINE_LIBRARY readline HINTS ${READLINE_ROOT_DIR} PATH_SUFFIXES lib REQUIRED)
165+
find_library(NCURSES_LIBRARY ncurses REQUIRED) # readline depends on libncurses
166+
167+
mark_as_advanced(READLINE_INCLUDE_DIR READLINE_LIBRARY NCURSES_LIBRARY)
168+
169+
include(FindPackageHandleStandardArgs)
170+
find_package_handle_standard_args(
171+
Readline
172+
DEFAULT_MSG
173+
READLINE_LIBRARY
174+
NCURSES_LIBRARY
175+
READLINE_INCLUDE_DIR
176+
)
177+
178+
set(READLINE_INCLUDE_DIRS ${READLINE_INCLUDE_DIR})
179+
set(READLINE_LIBRARIES ${READLINE_LIBRARY} ${NCURSES_LIBRARY})
180+
181+
message(STATUS "Found Readline: ${READLINE_LIBRARIES}")
182+
183+
include_directories(${READLINE_INCLUDE_DIR})
184+
156185
# Use xxhash and xsimd from Velox for now.
157186
include_directories(.)
158187
include_directories(${CMAKE_BINARY_DIR})

axiom/optimizer/tests/AxiomSql.cpp

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#include <folly/executors/IOThreadPoolExecutor.h>
1919
#include <folly/init/Init.h>
2020
#include <gflags/gflags.h>
21+
#include <readline/history.h>
22+
#include <readline/readline.h>
2123
#include <sys/resource.h>
2224
#include <sys/time.h>
2325
#include <iostream>
@@ -647,14 +649,21 @@ class VeloxRunner {
647649
// more whitespaces.
648650
// @return Command text with leading and trailing whitespaces as well as
649651
// trailing ';' removed.
650-
std::string readCommand(std::istream& in, bool& end) {
652+
std::string readCommand(bool& atEnd) {
651653
std::stringstream command;
652-
end = false;
654+
atEnd = false;
653655

654656
bool stripLeadingSpaces = true;
655657

656-
std::string line;
657-
while (std::getline(in, line)) {
658+
while (char* rawLine = readline("")) {
659+
SCOPE_EXIT {
660+
if (rawLine != nullptr) {
661+
free(rawLine);
662+
}
663+
};
664+
665+
std::string line(rawLine);
666+
658667
int64_t startPos = 0;
659668
if (stripLeadingSpaces) {
660669
for (; startPos < line.size(); ++startPos) {
@@ -677,6 +686,7 @@ std::string readCommand(std::istream& in, bool& end) {
677686

678687
if (line[i] == ';') {
679688
command << line.substr(startPos, i - startPos);
689+
add_history(fmt::format("{};", command.str()).c_str());
680690
return command.str();
681691
}
682692

@@ -686,19 +696,16 @@ std::string readCommand(std::istream& in, bool& end) {
686696
stripLeadingSpaces = false;
687697
command << line.substr(startPos) << std::endl;
688698
}
689-
end = true;
699+
atEnd = true;
690700
return "";
691701
}
692702

693-
void readCommands(
694-
VeloxRunner& runner,
695-
std::string_view prompt,
696-
std::istream& in) {
703+
void readCommands(VeloxRunner& runner, std::string_view prompt) {
697704
for (;;) {
698705
std::cout << prompt;
699-
bool end;
700-
std::string command = readCommand(in, end);
701-
if (end) {
706+
bool atEnd;
707+
std::string command = readCommand(atEnd);
708+
if (atEnd) {
702709
break;
703710
}
704711

@@ -803,7 +810,7 @@ int main(int argc, char** argv) {
803810
"flag name = value; sets a gflag.\n"
804811
"help; prints help text."
805812
<< std::endl;
806-
readCommands(runner, "SQL> ", std::cin);
813+
readCommands(runner, "SQL> ");
807814
}
808815
} catch (std::exception& e) {
809816
std::cerr << "Error: " << e.what() << std::endl;

axiom/optimizer/tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ target_link_libraries(
133133
axiom_hive_connector_metadata
134134
axiom_tpch_connector_metadata
135135
axiom_sql_presto_parser
136+
${READLINE_LIBRARIES}
136137
velox_query_benchmark
137138
velox_exec_test_lib
138139
velox_dwio_common

0 commit comments

Comments
 (0)