diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..bdc3a3c --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "calcite"] + path = calcite + url = https://github.com/apache/calcite.git diff --git a/calcite b/calcite new file mode 160000 index 0000000..065e323 --- /dev/null +++ b/calcite @@ -0,0 +1 @@ +Subproject commit 065e323da89ca73a46bec195efc7bd4019dc4f2f diff --git a/ledger_sql.sh b/ledger_sql.sh new file mode 100755 index 0000000..5055bb6 --- /dev/null +++ b/ledger_sql.sh @@ -0,0 +1,48 @@ +#!/bin/bash + +if [[ "$#" -ne 2 ]] +then + echo "Illegal number of parameters." + echo "Usage: /path/to/ledger_sql.sh " + exit +fi + +LEDGER_FILE="$1" +SCRATCH_DIR="$2" +CALCITE_LEDGER_SCHEMA_NAME=LEDGER +CALCITE_LEDGER_SCHEMA_DIR="$SCRATCH_DIR/LEDGER" + +clean_up () { + ARG=$? + rm -rf $SCRATCH_DIR + exit $ARG +} +trap clean_up EXIT + +# Initialize Calcite. +git submodule update --init --recursive + +ROOT_DIR="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )" + +pushd $ROOT_DIR/calcite && ./gradlew -q :example:csv:buildSqllineClasspath && popd >/dev/null 2>&1 + +mkdir -p $SCRATCH_DIR + +# Populate model.json from template +sed "s//$CALCITE_LEDGER_SCHEMA_NAME/g" $ROOT_DIR/model.json > "$SCRATCH_DIR/model.json" + +# Use # as separator in sed instead of / to avoid confusion with / in the path. +sed -i "s##$CALCITE_LEDGER_SCHEMA_DIR#g" "$SCRATCH_DIR/model.json" + +mkdir -p $CALCITE_LEDGER_SCHEMA_DIR + +# Generate the CSV output of the ledger file. +$ROOT_DIR/txn_gen.sh $LEDGER_FILE $CALCITE_LEDGER_SCHEMA_DIR/LEDGER.csv "$SCRATCH_DIR/scratchfile" + +# Start sqlline. +java \ + -Djavax.xml.parsers.DocumentBuilderFactory=com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl \ + -jar $ROOT_DIR/calcite/example/csv/build/libs/sqllineClasspath.jar \ + -u jdbc:calcite:model=$SCRATCH_DIR/model.json \ + -n admin \ + -p admin diff --git a/model.json b/model.json new file mode 100644 index 0000000..62c2266 --- /dev/null +++ b/model.json @@ -0,0 +1,14 @@ +{ + "version": "1.0", + "defaultSchema": "", + "schemas": [ + { + "name": "", + "type": "custom", + "factory": "org.apache.calcite.adapter.csv.CsvSchemaFactory", + "operand": { + "directory": "" + } + } + ] +} diff --git a/txn_gen.sh b/txn_gen.sh index 090bc0c..2016c8a 100755 --- a/txn_gen.sh +++ b/txn_gen.sh @@ -1,7 +1,7 @@ #!/bin/bash -# TODO(louiskuang): consider switching to a python script and add unit tests. -# TODO(louiskuang): support the notes column %(quoted(join(note | xact.note))). -# TODO(louiskuang): make filename part of txn id. +# TODO(xwkuang5): consider switching to a python script and add unit tests. +# TODO(xwkuang5): support the notes column %(quoted(join(note | xact.note))). +# TODO(xwkuang5): make filename part of txn id. function join_by { local d=${1-} f=${2-}; if shift 2; then printf %s "$f" "${@/#/$d}"; fi; } @@ -30,7 +30,7 @@ NUM_USER_VISIBLE_FIELDS=7 USER_VISIBLE_FIELD_START_IDX=2 # Calcite header. -HEADER="TXN_ID:long,TXN_DATE:date,CODE:string,PAYEE:string,ACCOUNT:string,COMMODITY:string,QUANTITY:numeric,STATUS:string" +HEADER="TXN_ID:long,TXN_DATE:date,CODE:string,PAYEE:string,ACCOUNT:string,COMMODITY:string,QUANTITY:\"decimal(18,2)\",STATUS:string" # Truncate the output. : > $2